Skip to content

Commit 45a7468

Browse files
authored
add option to capture mouse in game focus mode (RetroAchievements#429)
1 parent 4881aa1 commit 45a7468

6 files changed

Lines changed: 57 additions & 9 deletions

File tree

src/Application.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,15 @@ bool Application::init(const char* title, int width, int height)
321321
}
322322

323323
SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
324+
324325
_coreName.clear();
325326
_gameData = NULL;
326327
_validSlots = 0;
327328
_isDriveFloppy = false;
328329
lastHardcore = hardcore();
329330
cancelLoad = false;
331+
_absViewMouseX = _absViewMouseY = 0;
332+
330333
updateMenu();
331334
updateDiscMenu(true);
332335
return true;
@@ -2420,6 +2423,7 @@ void Application::handle(const SDL_SysWMEvent* syswm)
24202423

24212424
case IDM_EMULATOR_CONFIG:
24222425
_config.showEmulatorSettingsDialog();
2426+
updateMouseCapture();
24232427
updateSpeedIndicator();
24242428
_video.redraw();
24252429
break;
@@ -2527,34 +2531,45 @@ void Application::handle(const SDL_MouseMotionEvent* motion)
25272531
else if (viewY >= viewScaledHeight)
25282532
viewY = viewScaledHeight - 1;
25292533

2534+
if (SDL_GetRelativeMouseMode())
2535+
{
2536+
_absViewMouseX += motion->xrel;
2537+
_absViewMouseY += motion->yrel;
2538+
}
2539+
else
2540+
{
2541+
_absViewMouseX = viewX;
2542+
_absViewMouseY = viewY;
2543+
}
2544+
25302545
switch (_video.getRotation())
25312546
{
25322547
default:
25332548
rel_x = ((viewX << 16) / viewScaledWidth) - 32767;
25342549
rel_y = ((viewY << 16) / viewScaledHeight) - 32767;
2535-
abs_x = (viewX * viewWidth) / viewScaledWidth;
2536-
abs_y = (viewY * viewHeight) / viewScaledHeight;
2550+
abs_x = (_absViewMouseX * viewWidth) / viewScaledWidth;
2551+
abs_y = (_absViewMouseY * viewHeight) / viewScaledHeight;
25372552
break;
25382553

25392554
case Video::Rotation::Ninety:
25402555
rel_x = 32767 - ((viewY << 16) / viewScaledHeight);
25412556
rel_y = ((viewX << 16) / viewScaledWidth) - 32767;
2542-
abs_x = viewWidth - ((viewY * viewWidth) / viewScaledHeight) - 1;
2543-
abs_y = (viewX * viewHeight) / viewScaledWidth;
2557+
abs_x = viewWidth - ((_absViewMouseY * viewWidth) / viewScaledHeight) - 1;
2558+
abs_y = (_absViewMouseX * viewHeight) / viewScaledWidth;
25442559
break;
25452560

25462561
case Video::Rotation::OneEighty:
25472562
rel_x = 32767 - ((viewX << 16) / viewScaledWidth);
25482563
rel_y = 32767 - ((viewY << 16) / viewScaledHeight);
2549-
abs_x = viewWidth - ((viewX * viewWidth) / viewScaledWidth) - 1;
2550-
abs_y = viewHeight - ((viewY * viewHeight) / viewScaledHeight) - 1;
2564+
abs_x = viewWidth - ((_absViewMouseX * viewWidth) / viewScaledWidth) - 1;
2565+
abs_y = viewHeight - ((_absViewMouseY * viewHeight) / viewScaledHeight) - 1;
25512566
break;
25522567

25532568
case Video::Rotation::TwoSeventy:
25542569
rel_x = ((viewY << 16) / viewScaledHeight) - 32767;
25552570
rel_y = 32767 - ((viewX << 16) / viewScaledWidth);
2556-
abs_x = (viewY * viewWidth) / viewScaledHeight;
2557-
abs_y = viewHeight - ((viewX * viewHeight) / viewScaledWidth) - 1;
2571+
abs_x = (_absViewMouseY * viewWidth) / viewScaledHeight;
2572+
abs_y = viewHeight - ((_absViewMouseX * viewHeight) / viewScaledWidth) - 1;
25582573
break;
25592574
}
25602575

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

26862701
_video.showMessage(_keybinds.hasGameFocus() ? "Game focus enabled" : "Game focus disabled", 60);
2687-
SDL_SetRelativeMouseMode(_keybinds.hasGameFocus() ? SDL_TRUE : SDL_FALSE);
2702+
updateMouseCapture();
26882703
break;
26892704
}
26902705
}
26912706

2707+
void Application::updateMouseCapture()
2708+
{
2709+
SDL_SetRelativeMouseMode(_keybinds.hasGameFocus() && _config.getGameFocusCaptureMouse() ? SDL_TRUE : SDL_FALSE);
2710+
}
2711+
26922712
void Application::toggleFastForwarding(unsigned extra)
26932713
{
26942714
// get the current fast forward selection

src/Application.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class Application
125125
void loadConfiguration(int* window_x, int* window_y, int* window_width, int* window_height);
126126
void saveConfiguration();
127127
std::string serializeRecentList();
128+
void updateMouseCapture();
128129
void updateSpeedIndicator();
129130
void toggleFastForwarding(unsigned extra);
130131
void toggleBackgroundInput();
@@ -180,4 +181,7 @@ class Application
180181

181182
HMENU _menu;
182183
HMENU _cdRomMenu;
184+
185+
int _absViewMouseX;
186+
int _absViewMouseY;
183187
};

src/components/Config.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ bool Config::init(libretro::LoggerComponent* logger)
9595
_fastForwardRatio = 5;
9696
_backgroundInput = false;
9797
_showSpeedIndicator = true;
98+
_gameFocusCaptureMouse = false;
9899

99100
reset();
100101
return true;
@@ -570,6 +571,10 @@ std::string Config::serializeEmulatorSettings() const
570571

571572
json.append("\"showSpeedIndicator\":");
572573
json.append(_showSpeedIndicator ? "true" : "false");
574+
json.append(",");
575+
576+
json.append("\"gameFocusCaptureMouse\":");
577+
json.append(_gameFocusCaptureMouse ? "true" : "false");
573578

574579
json.append("}");
575580
return json;
@@ -608,6 +613,10 @@ bool Config::deserializeEmulatorSettings(const char* json)
608613
{
609614
ud->self->_showSpeedIndicator = num != 0;
610615
}
616+
else if (ud->key == "gameFocusCaptureMouse")
617+
{
618+
ud->self->_gameFocusCaptureMouse = num != 0;
619+
}
611620
}
612621
else if (event == JSONSAX_NUMBER)
613622
{
@@ -939,6 +948,10 @@ void Config::showEmulatorSettingsDialog()
939948
db.addCheckbox("Show Indicator when Paused or Fast Forwarding", 51004, 0, y, WIDTH - 10, 8, &showSpeedIndicator);
940949
y += LINE;
941950

951+
bool gameFocusCaptureMouse = _gameFocusCaptureMouse;
952+
db.addCheckbox("Capture mouse in Game Focus mode", 51005, 0, y, WIDTH - 10, 8, &gameFocusCaptureMouse);
953+
y += LINE;
954+
942955
db.addButton("OK", IDOK, WIDTH - 55 - 50, y, 50, 14, true);
943956
db.addButton("Cancel", IDCANCEL, WIDTH - 50, y, 50, 14, false);
944957

@@ -947,6 +960,7 @@ void Config::showEmulatorSettingsDialog()
947960
_audioWhileFastForwarding = playAudio;
948961
_fastForwardRatio = fastForwardRatio + 2;
949962
_showSpeedIndicator = showSpeedIndicator;
963+
_gameFocusCaptureMouse = gameFocusCaptureMouse;
950964
}
951965
}
952966
#endif

src/components/Config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class Config: public libretro::ConfigComponent
6060
virtual bool getShowSpeedIndicator() override { return _showSpeedIndicator; }
6161
virtual void setShowSpeedIndicator(bool value) override { _showSpeedIndicator = value; }
6262

63+
virtual bool getGameFocusCaptureMouse() override { return _gameFocusCaptureMouse; }
64+
6365
void setSaveDirectory(const std::string& path) { _saveFolder = path; }
6466

6567
const char* getRootFolder()
@@ -152,6 +154,7 @@ class Config: public libretro::ConfigComponent
152154
bool _audioWhileFastForwarding;
153155
bool _backgroundInput;
154156
bool _showSpeedIndicator;
157+
bool _gameFocusCaptureMouse;
155158

156159
int _fastForwardRatio;
157160

src/libretro/Components.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ namespace libretro
159159

160160
virtual bool getShowSpeedIndicator() = 0;
161161
virtual void setShowSpeedIndicator(bool value) = 0;
162+
163+
virtual bool getGameFocusCaptureMouse() = 0;
162164
};
163165

164166
/**

src/libretro/Core.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ namespace
164164
{
165165
(void)value;
166166
}
167+
168+
virtual bool getGameFocusCaptureMouse() override
169+
{
170+
return false;
171+
}
167172
};
168173

169174
class DummyVideoContext : public libretro::VideoContextComponent

0 commit comments

Comments
 (0)