Question

I'm making a GUI system for my 3D game engine using GLFW and I need my UI objects like my buttons to only perform their functions when they are clicked once.

The program checks if the user clicked in the button's area by checking the cursor's coordinates, but the problem is that if the user clicks and drags within the button area, then the button's functions are called repeatedly. Is there a way to stop this?

void ui_Button::click(void (*f)())
{
    if (mouseX >= pPosX && mouseX <= pWidth && mouseY >= pPosY && mouseY <= pHeight && glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT))
    {
        (*f)();
    }
}

Another option I was thinking about but don't know how to do is having the button's functions work when the mouse is clicked and released on the button or UI object.

Was it helpful?

Solution

The proper way to do this would be to install a mouse listener with glfwSetMouseButtonCallback that keeps a list of things interested in mouse key presses, and informs them of that (basically, like the observer pattern).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top