Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Keeping mouse within window when dragging.  (Read 1373 times)

0 Members and 1 Guest are viewing this topic.

thatoneguy

  • Newbie
  • *
  • Posts: 25
    • View Profile
Keeping mouse within window when dragging.
« 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();
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10998
    • View Profile
    • development blog
    • Email
AW: Keeping mouse within window when dragging.
« Reply #1 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

thatoneguy

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: AW: Keeping mouse within window when dragging.
« Reply #2 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.
« Last Edit: March 15, 2014, 10:36:59 am by thatoneguy »

 

anything