SFML community forums

Help => General => Topic started by: thatoneguy on March 15, 2014, 07:50:36 am

Title: Keeping mouse within window when dragging.
Post by: thatoneguy on March 15, 2014, 07:50:36 am
I'm using borderless style for my window. I've set up an area where you can click and drag the window itself. My issue is keeping the mouse inside the window. I remember reading a reply in a thread about this too but still unsure of it.

int main (int argc, char **argv) {
    -- Snip --
    while (root.GetGameState() != Root::GameState::Quit) {
        switch (root.GetGameState()) {
            case Root::GameState::Menu:
                sf::Event currentEvent;
                while (root.GetRenderWindow()->pollEvent(currentEvent)) {
                    switch (currentEvent.type) {
                        case sf::Event::MouseButtonPressed:
                            Utilities::currentMouseButton = currentEvent.mouseButton.button;
                            Utilities::lastMousePosition = sf::Mouse::getPosition(*root.GetRenderWindow());
                            break;
                        case sf::Event::MouseButtonReleased:
                            Utilities::lastMousePosition = sf::Mouse::getPosition(*root.GetRenderWindow());
                            -- Snip --
                            break;
                        case sf::Event::MouseEntered:
                            Utilities::mouseWithinWindow = true;
                            break;
                        case sf::Event::MouseLeft:
                            Utilities::mouseWithinWindow = false;
                            break;
                        default:
                            break;
                    }
                    if (Utilities::IsMouseButtonHeld(sf::Mouse::Button::Left)) {
                        root.GetTitleBar()->HandleDrag(root.GetRenderWindow());
                    }
                }
                root.GetRenderWindow()->clear(sf::Color(48, 48, 48, 255));
                root.Update();
                root.GetRenderWindow()->display();
                break;
            case Root::GameState::Quit:
                root.GetRenderWindow()->close();
                break;
            default:
                break;
        }
    }
    return root.GetExitStatus();
}
 
Title: AW: Keeping mouse within window when dragging.
Post by: eXpl0it3r on March 15, 2014, 09:12:48 am
Since you usually want to drag the window to a place "outside" the window, I wonder how you want to implement it the. ;)

SFML doesn't have a "mouse locking" feature (yet). In your case you might want to go with real-time input (sf::Mouse::getPosition()), which can also track the mouse states outside the window. ;)
Title: Re: AW: Keeping mouse within window when dragging.
Post by: thatoneguy on March 15, 2014, 10:32:29 am
Since you usually want to drag the window to a place "outside" the window, I wonder how you want to implement it the. ;)

SFML doesn't have a "mouse locking" feature (yet). In your case you might want to go with real-time input (sf::Mouse::getPosition()), which can also track the mouse states outside the window. ;)

Ah okay. So I changed up my approach a bit. I was using a single variable for both calculating the offset and checking whether the mouse was within the area.