Button problems

Started by
5 comments, last by Ganoosh_ 17 years, 9 months ago
Ok, I'm stumped on this one. I created a Button class to have buttons in game. It works fine except for 2 small glitches. First of all here's some code:

// the Button update code, runs every frame
// mouseOver is used for nothing except to signal drawing code to change colors if mouse is over
void Button::update() {
    if (testMouse()) {
        mouseOver = true;
        if (GetAsyncKeyState(0x01)) {
            mouseDown = true;
        }
        if (mouseDown && !GetAsyncKeyState(0x01)) {
            mouseDown = false;
            pressed = true;
        }
    } else {
        mouseOver = false;
        mouseDown = false;
    }
}

// testMouse(), tests if the cursor is within the given rectangle boudnary
bool Button::testMouse() const {
    Point2D mouse = Mouse::getMousePos();
    mouse.y = 599-mouse.y; // convert from screen coords to opengl coords
    if (mouse.x > corners[0].x && mouse.x < corners[1].x)
        if (mouse.y < corners[0].y && mouse.y > corners[1].y)
            return true;
    return false;
}

Now here's the big problem. It only happens in windowed mode. If you move the window, and then just put the mouse over the button, it acts as if it was pressed (setting pressed to true). Now by the way the code goes, and some debugging test, my conclusion is that while dragging the window, testMouse is passing even though the mouse isn't within boudnaries (why?), and because the button's being held down, it's settings mouseDown to true. Therefore when you point the mouse over it, mouseDown is true, and the button is not being pressed, so it sets pressed to true. Makes perfect sense, except after dragging, while not being on the button, it should set mouseDown back to false as seen. But it's not, meaning it's passing testMouse, meaning it should auto activate after dragging, because the button was released and mouseDown was set to true, but it's not. Do why is it doing this, and how do I fix it? Any help would be appreciated. The other glitch is obvious if you look at the code good enough, but not as important, and I could figure it out myself, so I'm not going to worry about posting it. I just want to get this first major glitch fixed as soon as possible. Thanks in advance
Advertisement
Are corners[] in screen coordinates, or in client coordinates? I mean, when you move the window do they change or they remain the same?
Client, they stay the same
In that case(assumming by the comment "// convert from screen coords to opengl coords" that Mouse::getMousePos() returns the position in screen coords) you can't compare the two. Cursor position is in screen cordinates, rectangle is in client coordinates. You probably have to use ScreenToClient to get the cursor position on client space.
That's already incorperated in the getMousePos function. The comparison is fine, it works as long as I don't move the window, but once I move the window that when it starts glitching up. The spot moves along with it, so when I move the window it tests in the same spot, but while dragging, something's happening, and causing it to glitch.
I think it might be this. While you drag the window, the mouse button is pressed then released (of course). This information is saved until the next time you ask for GetAsyncKeyState(VK_LMOUSE) (which I presume is 0x01? Magic numbers = teh 3vil :P). That doesn't get called until you next move over a mouse-handling control.

You shouldn't call GetAsyncKeyState within every control, IMHO; you should call the multi-key version (I think it's GetAsyncKeyboardState) every frame, fill an array of bools (which keys have been pressed this frame) then your controls should look into that array instead.
Quote:Original post by Bob Janova
I think it might be this. While you drag the window, the mouse button is pressed then released (of course). This information is saved until the next time you ask for GetAsyncKeyState(VK_LMOUSE) (which I presume is 0x01? Magic numbers = teh 3vil :P). That doesn't get called until you next move over a mouse-handling control.

You shouldn't call GetAsyncKeyState within every control, IMHO; you should call the multi-key version (I think it's GetAsyncKeyboardState) every frame, fill an array of bools (which keys have been pressed this frame) then your controls should look into that array instead.

Wow, you were right, I checked the mouse button everyframe and set a variable and used that, it works now. Thanks a lot Bob

This topic is closed to new replies.

Advertisement