Skip to content

Commit

Permalink
Don't grab mouse when windowed and in menu so it's possible to move w…
Browse files Browse the repository at this point in the history
…indow.
  • Loading branch information
mooflu committed Sep 23, 2024
1 parent 12ed553 commit b511856
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
1 change: 1 addition & 0 deletions game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ void Game::startNewGame(void) {

GameS::instance()->reset();
GameState::context = Context::eInGame;
SDL_SetRelativeMouseMode(SDL_TRUE);
InputS::instance()->disableInterceptor();
AudioS::instance()->playSample("sounds/chirp2");

Expand Down
41 changes: 34 additions & 7 deletions game/MenuManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ MenuManager::MenuManager() :
XTRACE();

updateSettings();

int w = VideoBaseS::instance()->getWidth();
int h = VideoBaseS::instance()->getHeight();
_mouseX = w / 2;
_mouseY = h / 2;
}

MenuManager::~MenuManager() {
Expand Down Expand Up @@ -286,9 +291,16 @@ bool MenuManager::draw(void) {
return true;
}

void MenuManager::reload(void) {}
void MenuManager::reload(void) {
int w = VideoBaseS::instance()->getWidth();
int h = VideoBaseS::instance()->getHeight();
_mouseX = w / 2;
_mouseY = h / 2;
}

void MenuManager::turnMenuOn(void) {
SDL_SetRelativeMouseMode(SDL_FALSE);

AudioS::instance()->playSample("sounds/beep");
_prevContext = GameState::context;
GameState::context = Context::eMenu;
Expand All @@ -309,6 +321,8 @@ void MenuManager::turnMenuOff(void) {
return;
}

SDL_SetRelativeMouseMode(SDL_TRUE);

AudioS::instance()->playSample("sounds/beep");
GameState::context = _prevContext;

Expand All @@ -324,6 +338,24 @@ bool MenuManager::canReturnToGame(void) {
return (_prevContext == Context::eInGame) || (_prevContext == Context::ePaused);
}

void MenuManager::updateMousePosition(const Trigger& trigger) {
if (VideoBaseS::instance()->isFullscreen()) {
_mouseX += trigger.fData1;
_mouseY += trigger.fData2;
} else {
int x;
int y;
SDL_GetMouseState(&x, &y);
float arx = (float)VideoBaseS::instance()->getWidth() / 1000.0;
float ary = (float)VideoBaseS::instance()->getHeight() / 750.0;
_mouseX = x / arx;
_mouseY = 750.0 - (y / ary);
}
VideoBase& video = *VideoBaseS::instance();
Clamp(_mouseX, 0.0f, 1000.0f);
Clamp(_mouseY, 0.0f, 750.0f);
}

void MenuManager::input(const Trigger& trigger, const bool& isDown) {
Trigger t = trigger;
if (isDown) {
Expand Down Expand Up @@ -387,12 +419,7 @@ void MenuManager::input(const Trigger& trigger, const bool& isDown) {
Clamp(_mouseX, 0.0f, 1000.0);
Clamp(_mouseY, 0.0f, 750.0);
#else

_mouseX += trigger.fData1;
_mouseY += trigger.fData2;

Clamp(_mouseX, 0.0f, 1000.0f);
Clamp(_mouseY, 0.0f, 750.0f);
updateMousePosition(trigger);
#endif
activateSelectableUnderMouse();
} break;
Expand Down
1 change: 1 addition & 0 deletions game/MenuManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class MenuManager : public InterceptorI {
void clearActiveSelectables(void);
void updateSettings(void);
void activateSelectableUnderMouse(const bool& useFallback = false);
void updateMousePosition(const Trigger& trigger);

TiXmlDocument* _menu;

Expand Down
5 changes: 4 additions & 1 deletion game/VideoBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ bool VideoBase::setVideoMode(void) {
InputS::instance()->resetMousePosition();

#if 1
SDL_SetRelativeMouseMode(SDL_TRUE);
// don't grab mouse when windowed and in menu so it's possible to move the window
bool grabMouse = _isFullscreen || GameState::context != Context::eMenu;
SDL_SetRelativeMouseMode(grabMouse ? SDL_TRUE : SDL_FALSE);
SDL_ShowCursor(SDL_FALSE);
#else
// SDL_SetRelativeMouseMode used to only work on Mac
SDL_ShowCursor(SDL_DISABLE);
Expand Down

0 comments on commit b511856

Please sign in to comment.