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

Author Topic: Keep mouse in window  (Read 9963 times)

0 Members and 1 Guest are viewing this topic.

Njifra

  • Guest
Keep mouse in window
« on: March 18, 2014, 11:25:00 am »
So, as title says, it would be awesome if SFML has "mouse keeping in window".
The following functions and variables should be added in sf::Window class:
  - bool m_keepM;
  - void keepMouseIn(bool keep = true) { this->m_keepM = keep; }
  - bool isKeepingMouseIn() { return this->m_keepM; }

And in bool sf::Window::isOpen(), there should be this coding:
if (this->m_keepM &&& !this->m_isFullScr/*check if window is not in fullscreen (this variable doesent exist)*/)
{
        int maxX = this->getSize().x;
        int maxY = this->getSize().y;

        int mX = sf::Mouse::getPosition(*this).x;
        int mY = sf::Mouse::getPosition(*this).y;

        if (mX < 0 || mY < 0 || mX > maxX || mY > maxY)
        {
                if (mX < 0)
                        mX = 0;
                else if (mX > maxX)
                        mX = maxX;

                if (mY < 0)
                        mY = 0;
                else if (mY > maxY)
                        mY = maxY;

                sf::Mouse::setPosition(sf::Vector2i(mX, mY), *this);
        }
}
 

I hope this is useful  ;)
Please say what do you think about this :D

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: Keep mouse in window
« Reply #1 on: March 18, 2014, 01:10:09 pm »
This is a feature many games will need, but since you can easily make it with SFML yourself (see your code), it's too specific to be integrated into SFML itself.
We we really want and already got a few topics on this and an open issue on GitHub, is to be able to actually lock/capture the mouse inside the window. That doesn't work by just resetting the mouse position, because that way you can still move out of the window by moving your mouse very fast and/or with high DPI. Instead the mouse shouldn't even have the option to be outside of the window, i.e. forced by the system.

See GitHub for the current state of this feature. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: Keep mouse in window
« Reply #2 on: March 18, 2014, 01:19:35 pm »
Why ?

Why do you want to force the player/user to be inside your window. There's nothing more annoying than that..
You can still do something full screen or pause the game if the player is out the window.

OSes shouldn't even allow the mouse to be forced IMHO.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: Keep mouse in window
« Reply #3 on: March 18, 2014, 01:27:14 pm »
Well try to play an first person shooter in window mode where every other click activates a window behind your, because your mouse cursor accidentally went out of the window (as said this can easily happen when moving your mouse too fast).

Just because one can implement "bad" behavior it shouldn't just be disallowed. That'd be a very naïve way to approach things. (e.g. "C++ can access any file on the system, let's disallow file access!" or "C can create resource leaks, let's disallow resource management!" or "The WinAPI allows system changes, let's disallow all calls to the WinAPI!"). As with many things in real life, things can be used for good or bad things. ;D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: Keep mouse in window
« Reply #4 on: March 18, 2014, 01:34:43 pm »
FPS in a small window ? No thanks...
I understand that ..sometimes.. you need to keep the mouse in the window. But there are so few cases, this is so annoying to the user that I really don't think that's a feature that should be added, ever.

I already played a lot of games where the mouse was stuc inside the window, that's the kind of game I unistall after having spent minutes to get out of it. People like freedom, don't constraint them.

Again, no one prevent you from making the game window the size of the the screen or fullscreen.

Njifra

  • Guest
Re: Keep mouse in window
« Reply #5 on: March 18, 2014, 01:43:49 pm »
I already played a lot of games where the mouse was stuc inside the window, that's the kind of game I unistall after having spent minutes to get out of it. People like freedom, don't constraint them.
xD

eXpl0it3r and Lo-X: thanks for reply :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: Keep mouse in window
« Reply #6 on: March 18, 2014, 02:26:06 pm »
I already played a lot of games where the mouse was stuc inside the window, that's the kind of game I unistall after having spent minutes to get out of it. People like freedom, don't constraint them.
Well I like the freedom of programming, thus don't constrain me, just because you had bad experience in the past. ;)

Also when I'm in a FPS game, I don't see the cursor at all, it will be locked to the center of the screen anyways, so why shouldn't I be allowed to lock it inside the window? That doesn't mean I have to keep the lock when the window loses focus (alt + tab) or when I'm in the menu with mouse support, it just means that when the mouse should never leave the window, I want to guarantee that.

FPS in a small window ? No thanks...
I understand that ..sometimes.. you need to keep the mouse in the window. But there are so few cases, this is so annoying to the user that I really don't think that's a feature that should be added, ever.

Again, no one prevent you from making the game window the size of the the screen or fullscreen.
I highly disagree. My PC setup uses a multi monitor setup, but I never play a game over multiple monitor (not that there are many game that support this nicely anyways), but instead I have one screen with the game on it, while the other screen shows IRC or so. Regardless of window or fullscreen mode, if the mouse is not locked inside the window, I can just move out of the window and this should be okay when the game is in "mouse mode", but for an first person view where the cursor is invisible and "locked" to the center, I want to guarantee that I never click outside the game window on my second monitor.
And yes I've played games where I had to turn of my other monitor, just so I can properly play the game, without glitching out of it.

As an example I always play Firefall in window mode, just because I'm switching between chat and other things so often. ;)
« Last Edit: March 18, 2014, 02:28:15 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: Keep mouse in window
« Reply #7 on: March 19, 2014, 09:17:54 am »
I find the fact that you can go to your second screen perfectly fine. I use this all the time : hearthstone, Diablo, Wow, I always have my mumble+web browser on my second screen so I can click them anytime.

I guess that's where we have a divergence.

Isn't, like you said, the mouse always in the middle of the screen on a FPS anyway ? And just while playing ? (not in the menu, etc.) ? To me it's different to put the cursor back in the center in that types of games in which you haven't the choice that to lock le mouse inside the window, aka I can't get out of here even if I want to.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: Keep mouse in window
« Reply #8 on: March 19, 2014, 10:08:33 am »
Well you still don't understand my point... ::)

In a FPS game the cursor will be hidden and will get reset to the center of the screen/window every iteration. This works fine in my cases, but since there's a delay between frames and your mouse operates quite independent, it is indeed possible to move your hidden cursor out of the window by moving your mouse quickly. If you click the moment the mouse is outside of the window, it will lose focus and go into pause mode or similar - even worse for fullscreen mode where the whole system will freeze shortly due to switch video modes.
Also not to forget that you can also create casual games that utilize the mouse in a way that you have to move it around a lot, thus making a small window size an acceptable situation.
The only way to ensure your cursor stays inside the window is by locking it in there.

Again, that doesn't mean you can't unlock it when the window loses focus or not locking it at all when your in "mouse mode" (i.e. when the cursor is actually used for clicking stuff).
I just don't understand how you want this not implemented, just because there are games that utilize the functionalities badly. "Punishing" developers for the fault of other developers gets nobody anywhere.

Can it be used badly? Probably.
Aren't the current possibilities sufficient? Not in all cases.
Do the positive points weigh more than the possible negative ones? Yes.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Keep mouse in window
« Reply #9 on: March 19, 2014, 10:18:06 am »
Quote
In a FPS game the cursor will be hidden and will get reset to the center of the screen/window every iteration. This works fine in my cases, but since there's a delay between frames and your mouse operates quite independent, it is indeed possible to move your hidden cursor out of the window by moving your mouse quickly. If you click the moment the mouse is outside of the window, it will lose focus and go into pause mode or similar - even worse for fullscreen mode where the whole system will freeze shortly due to switch video modes.
The proposed implementation for locking the mouse inside the window would suffer the exact same problem, therefore it wouldn't solve anything in this context.

The only reliable solution to this kind of problems is to use native OS functions (ClipCursor in this case). Any user-made solution will suffer from a delay between the mouse movement and the code that corrects it, and what you don't want to happen (a click outside the window) may happen in this short time frame.

Edit: doh... this is exactly what you say in your first reply ;D
« Last Edit: March 19, 2014, 10:20:11 am by Laurent »
Laurent Gomila - SFML developer

therocode

  • Full Member
  • ***
  • Posts: 125
    • View Profile
    • Development blog
Re: Keep mouse in window
« Reply #10 on: March 20, 2014, 05:49:33 am »
I just wanted to add to the discussion that I too have felt the need for a mouse locking feature many times when using SFML. As eXpl0it3r (that name took me more than 10 seconds to write. no tab complete in the forums D:) said, it is a crucial feature for FPS view games. In the cases where I wrote FPS stuff, I just used SDL instead since they have such a functionality.

Now I understand that this feature is already on its way. I just wanted to state this to show that there are definitely people who finds this feature crucial. :)