How to detect mouse button release correctly in SDL2

Hello.

I'm trying to make a GUI library in C++ with SDL and I'm currently making buttons. They have three visual states. Idle (not pressed or hovered). Hovered (hovered but not pressed). Pressed (pressed). The thing is that when I try to use event.button.state == SDL_RELEASED, it returns true when I'm holding the mouse button (not released yet) and moving it. It also returns true when I release like normal. Do you guys have any idea how to solve this? Here's a block of my code:
1
2
3
4
5
6
7
8
9
  if (e.type = SDL_MOUSEBUTTONDOWN) {
        if(e.button.button == SDL_BUTTON_LEFT) {
            if (e.button.state == SDL_PRESSED) {
                mouseClickDown();
            } else if(e.button.state == SDL_RELEASED) {
                mouseClickUp();
            }
        }
    }


Solution (with more previous code exposed):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	if (x >= m_area.x && x <= m_area.x + m_area.w && y >= m_area.y && y <= m_area.y + m_area.h) {
		isHovered = true;
		
		if (e.type == SDL_MOUSEBUTTONDOWN) {
			if(e.button.button == SDL_BUTTON_LEFT) {
				if (e.button.type == SDL_MOUSEBUTTONDOWN) {
					mouseClickDown();
				}
			}
		}
		
	} else {
		isHovered = false;
	}
	if (e.type == SDL_MOUSEBUTTONUP) {
		if(e.button.button == SDL_BUTTON_LEFT) {
			if (e.button.type == SDL_MOUSEBUTTONUP) {
				mouseClickUp();
			}
		}
	}
Last edited on
Topic archived. No new replies allowed.