From 63b36db1879c3c272c2014d00328d227e9965299 Mon Sep 17 00:00:00 2001 From: hidwood <78058766+hidwood@users.noreply.github.com> Date: Mon, 2 Feb 2026 12:53:24 -0500 Subject: [PATCH] access: speak mana percentage with Shift+health key Hold Shift when pressing the health percentage key to hear mana instead. Extract ComputePercentage helper to deduplicate the HP/mana calculation. Co-Authored-By: Claude Opus 4.5 --- Source/controls/accessibility_keys.cpp | 35 +++++++++++++++++++++----- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/Source/controls/accessibility_keys.cpp b/Source/controls/accessibility_keys.cpp index ea8b1d802..3e076ce03 100644 --- a/Source/controls/accessibility_keys.cpp +++ b/Source/controls/accessibility_keys.cpp @@ -13,7 +13,14 @@ #include #include "control/control.hpp" +#ifdef USE_SDL3 +#include +#else +#include +#endif + #include "controls/plrctrls.h" +#include "utils/sdl_compat.h" #include "diablo.h" #include "gamemenu.h" #include "help.h" @@ -38,6 +45,18 @@ namespace devilution { +namespace { + +/** Computes a rounded percentage (0--100) from a current and maximum value. */ +int ComputePercentage(int current, int maximum) +{ + const int clamped = std::max(current, 0); + int percent = static_cast((static_cast(clamped) * 100 + maximum / 2) / maximum); + return std::clamp(percent, 0, 100); +} + +} // namespace + void SpeakPlayerHealthPercentageKeyPressed() { if (!CanPlayerTakeAction()) @@ -45,14 +64,18 @@ void SpeakPlayerHealthPercentageKeyPressed() if (MyPlayer == nullptr) return; - const int maxHp = MyPlayer->_pMaxHP; - if (maxHp <= 0) + const SDL_Keymod modState = SDL_GetModState(); + const bool speakMana = (modState & SDL_KMOD_SHIFT) != 0; + if (speakMana) { + if (MyPlayer->_pMaxMana <= 0) + return; + SpeakText(fmt::format("{:d}%", ComputePercentage(MyPlayer->_pMana, MyPlayer->_pMaxMana)), /*force=*/true); return; + } - const int currentHp = std::max(MyPlayer->_pHitPoints, 0); - int hpPercent = static_cast((static_cast(currentHp) * 100 + maxHp / 2) / maxHp); - hpPercent = std::clamp(hpPercent, 0, 100); - SpeakText(fmt::format("{:d}%", hpPercent), /*force=*/true); + if (MyPlayer->_pMaxHP <= 0) + return; + SpeakText(fmt::format("{:d}%", ComputePercentage(MyPlayer->_pHitPoints, MyPlayer->_pMaxHP)), /*force=*/true); } void SpeakExperienceToNextLevelKeyPressed()