Skip to content

Commit

Permalink
add option to capture mouse in game focus mode (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras authored Oct 10, 2024
1 parent 4881aa1 commit 45a7468
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,15 @@ bool Application::init(const char* title, int width, int height)
}

SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);

_coreName.clear();
_gameData = NULL;
_validSlots = 0;
_isDriveFloppy = false;
lastHardcore = hardcore();
cancelLoad = false;
_absViewMouseX = _absViewMouseY = 0;

updateMenu();
updateDiscMenu(true);
return true;
Expand Down Expand Up @@ -2420,6 +2423,7 @@ void Application::handle(const SDL_SysWMEvent* syswm)

case IDM_EMULATOR_CONFIG:
_config.showEmulatorSettingsDialog();
updateMouseCapture();
updateSpeedIndicator();
_video.redraw();
break;
Expand Down Expand Up @@ -2527,34 +2531,45 @@ void Application::handle(const SDL_MouseMotionEvent* motion)
else if (viewY >= viewScaledHeight)
viewY = viewScaledHeight - 1;

if (SDL_GetRelativeMouseMode())
{
_absViewMouseX += motion->xrel;
_absViewMouseY += motion->yrel;
}
else
{
_absViewMouseX = viewX;
_absViewMouseY = viewY;
}

switch (_video.getRotation())
{
default:
rel_x = ((viewX << 16) / viewScaledWidth) - 32767;
rel_y = ((viewY << 16) / viewScaledHeight) - 32767;
abs_x = (viewX * viewWidth) / viewScaledWidth;
abs_y = (viewY * viewHeight) / viewScaledHeight;
abs_x = (_absViewMouseX * viewWidth) / viewScaledWidth;
abs_y = (_absViewMouseY * viewHeight) / viewScaledHeight;
break;

case Video::Rotation::Ninety:
rel_x = 32767 - ((viewY << 16) / viewScaledHeight);
rel_y = ((viewX << 16) / viewScaledWidth) - 32767;
abs_x = viewWidth - ((viewY * viewWidth) / viewScaledHeight) - 1;
abs_y = (viewX * viewHeight) / viewScaledWidth;
abs_x = viewWidth - ((_absViewMouseY * viewWidth) / viewScaledHeight) - 1;
abs_y = (_absViewMouseX * viewHeight) / viewScaledWidth;
break;

case Video::Rotation::OneEighty:
rel_x = 32767 - ((viewX << 16) / viewScaledWidth);
rel_y = 32767 - ((viewY << 16) / viewScaledHeight);
abs_x = viewWidth - ((viewX * viewWidth) / viewScaledWidth) - 1;
abs_y = viewHeight - ((viewY * viewHeight) / viewScaledHeight) - 1;
abs_x = viewWidth - ((_absViewMouseX * viewWidth) / viewScaledWidth) - 1;
abs_y = viewHeight - ((_absViewMouseY * viewHeight) / viewScaledHeight) - 1;
break;

case Video::Rotation::TwoSeventy:
rel_x = ((viewY << 16) / viewScaledHeight) - 32767;
rel_y = 32767 - ((viewX << 16) / viewScaledWidth);
abs_x = (viewY * viewWidth) / viewScaledHeight;
abs_y = viewHeight - ((viewX * viewHeight) / viewScaledWidth) - 1;
abs_x = (_absViewMouseY * viewWidth) / viewScaledHeight;
abs_y = viewHeight - ((_absViewMouseX * viewHeight) / viewScaledWidth) - 1;
break;
}

Expand Down Expand Up @@ -2684,11 +2699,16 @@ void Application::handle(const KeyBinds::Action action, unsigned extra)
updateMenu();

_video.showMessage(_keybinds.hasGameFocus() ? "Game focus enabled" : "Game focus disabled", 60);
SDL_SetRelativeMouseMode(_keybinds.hasGameFocus() ? SDL_TRUE : SDL_FALSE);
updateMouseCapture();
break;
}
}

void Application::updateMouseCapture()
{
SDL_SetRelativeMouseMode(_keybinds.hasGameFocus() && _config.getGameFocusCaptureMouse() ? SDL_TRUE : SDL_FALSE);
}

void Application::toggleFastForwarding(unsigned extra)
{
// get the current fast forward selection
Expand Down
4 changes: 4 additions & 0 deletions src/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class Application
void loadConfiguration(int* window_x, int* window_y, int* window_width, int* window_height);
void saveConfiguration();
std::string serializeRecentList();
void updateMouseCapture();
void updateSpeedIndicator();
void toggleFastForwarding(unsigned extra);
void toggleBackgroundInput();
Expand Down Expand Up @@ -180,4 +181,7 @@ class Application

HMENU _menu;
HMENU _cdRomMenu;

int _absViewMouseX;
int _absViewMouseY;
};
14 changes: 14 additions & 0 deletions src/components/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ bool Config::init(libretro::LoggerComponent* logger)
_fastForwardRatio = 5;
_backgroundInput = false;
_showSpeedIndicator = true;
_gameFocusCaptureMouse = false;

reset();
return true;
Expand Down Expand Up @@ -570,6 +571,10 @@ std::string Config::serializeEmulatorSettings() const

json.append("\"showSpeedIndicator\":");
json.append(_showSpeedIndicator ? "true" : "false");
json.append(",");

json.append("\"gameFocusCaptureMouse\":");
json.append(_gameFocusCaptureMouse ? "true" : "false");

json.append("}");
return json;
Expand Down Expand Up @@ -608,6 +613,10 @@ bool Config::deserializeEmulatorSettings(const char* json)
{
ud->self->_showSpeedIndicator = num != 0;
}
else if (ud->key == "gameFocusCaptureMouse")
{
ud->self->_gameFocusCaptureMouse = num != 0;
}
}
else if (event == JSONSAX_NUMBER)
{
Expand Down Expand Up @@ -939,6 +948,10 @@ void Config::showEmulatorSettingsDialog()
db.addCheckbox("Show Indicator when Paused or Fast Forwarding", 51004, 0, y, WIDTH - 10, 8, &showSpeedIndicator);
y += LINE;

bool gameFocusCaptureMouse = _gameFocusCaptureMouse;
db.addCheckbox("Capture mouse in Game Focus mode", 51005, 0, y, WIDTH - 10, 8, &gameFocusCaptureMouse);
y += LINE;

db.addButton("OK", IDOK, WIDTH - 55 - 50, y, 50, 14, true);
db.addButton("Cancel", IDCANCEL, WIDTH - 50, y, 50, 14, false);

Expand All @@ -947,6 +960,7 @@ void Config::showEmulatorSettingsDialog()
_audioWhileFastForwarding = playAudio;
_fastForwardRatio = fastForwardRatio + 2;
_showSpeedIndicator = showSpeedIndicator;
_gameFocusCaptureMouse = gameFocusCaptureMouse;
}
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions src/components/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class Config: public libretro::ConfigComponent
virtual bool getShowSpeedIndicator() override { return _showSpeedIndicator; }
virtual void setShowSpeedIndicator(bool value) override { _showSpeedIndicator = value; }

virtual bool getGameFocusCaptureMouse() override { return _gameFocusCaptureMouse; }

void setSaveDirectory(const std::string& path) { _saveFolder = path; }

const char* getRootFolder()
Expand Down Expand Up @@ -152,6 +154,7 @@ class Config: public libretro::ConfigComponent
bool _audioWhileFastForwarding;
bool _backgroundInput;
bool _showSpeedIndicator;
bool _gameFocusCaptureMouse;

int _fastForwardRatio;

Expand Down
2 changes: 2 additions & 0 deletions src/libretro/Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ namespace libretro

virtual bool getShowSpeedIndicator() = 0;
virtual void setShowSpeedIndicator(bool value) = 0;

virtual bool getGameFocusCaptureMouse() = 0;
};

/**
Expand Down
5 changes: 5 additions & 0 deletions src/libretro/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ namespace
{
(void)value;
}

virtual bool getGameFocusCaptureMouse() override
{
return false;
}
};

class DummyVideoContext : public libretro::VideoContextComponent
Expand Down

0 comments on commit 45a7468

Please sign in to comment.