Skip to content

Commit

Permalink
Hotkeys: Throttle pause requests
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Nov 27, 2023
1 parent acb4545 commit 88989bf
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions pcsx2/Hotkeys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "common/Assertions.h"
#include "common/FileSystem.h"
#include "common/Path.h"
#include "common/Timer.h"

static s32 s_current_save_slot = 1;
static std::optional<LimiterModeType> s_limiter_mode_prior_to_hold_interaction;
Expand Down Expand Up @@ -141,25 +142,51 @@ static void HotkeySaveStateSlot(s32 slot)
VMManager::SaveStateToSlot(slot);
}

static bool CanPause()
{
static constexpr const float PAUSE_INTERVAL = 3.0f;
static Common::Timer::Value s_last_pause_time = 0;

if (!Achievements::IsHardcoreModeActive() || VMManager::GetState() == VMState::Paused)
return true;

const Common::Timer::Value time = Common::Timer::GetCurrentValue();
const float delta = static_cast<float>(Common::Timer::ConvertValueToSeconds(time - s_last_pause_time));
if (delta < PAUSE_INTERVAL)
{
Host::AddIconOSDMessage(
"PauseCooldown", ICON_FA_CLOCK,
fmt::format(TRANSLATE_FS("Hotkeys", "You cannot pause until another {:.1f} seconds have passed."),
PAUSE_INTERVAL - delta),
Host::OSD_QUICK_DURATION);
return false;
}

Host::RemoveKeyedOSDMessage("PauseCooldown");
s_last_pause_time = time;

return true;
}

BEGIN_HOTKEY_LIST(g_common_hotkeys)
DEFINE_HOTKEY("OpenPauseMenu", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Open Pause Menu"),
[](s32 pressed) {
if (!pressed && VMManager::HasValidVM())
if (!pressed && VMManager::HasValidVM() && CanPause())
FullscreenUI::OpenPauseMenu();
})
DEFINE_HOTKEY("OpenAchievementsList", TRANSLATE_NOOP("Hotkeys", "System"),
TRANSLATE_NOOP("Hotkeys", "Open Achievements List"), [](s32 pressed) {
if (!pressed)
if (!pressed && CanPause())
FullscreenUI::OpenAchievementsWindow();
})
DEFINE_HOTKEY("OpenLeaderboardsList", TRANSLATE_NOOP("Hotkeys", "System"),
TRANSLATE_NOOP("Hotkeys", "Open Leaderboards List"), [](s32 pressed) {
if (!pressed)
if (!pressed && CanPause())
FullscreenUI::OpenLeaderboardsWindow();
})
DEFINE_HOTKEY(
"TogglePause", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Toggle Pause"), [](s32 pressed) {
if (!pressed && VMManager::HasValidVM())
if (!pressed && VMManager::HasValidVM() && CanPause())
VMManager::SetPaused(VMManager::GetState() != VMState::Paused);
})
DEFINE_HOTKEY("ToggleFullscreen", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Toggle Fullscreen"),
Expand Down

0 comments on commit 88989bf

Please sign in to comment.