From 177ba45f8d825b6f440e591e6a71cdc7bf643e42 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sat, 6 Jul 2024 11:34:56 +0100 Subject: [PATCH] Controller buttons: Make most of the code private The compiler constant-folds the lookups in `printItemMiscGamepad` code to the same asm as the manual version. --- Source/controls/controller_buttons.cpp | 89 +++++++++---------- Source/controls/controller_buttons.h | 114 ------------------------- Source/items.cpp | 19 +---- 3 files changed, 49 insertions(+), 173 deletions(-) diff --git a/Source/controls/controller_buttons.cpp b/Source/controls/controller_buttons.cpp index 3c407225c..8b6379c9c 100644 --- a/Source/controls/controller_buttons.cpp +++ b/Source/controls/controller_buttons.cpp @@ -3,6 +3,7 @@ #include "plrctrls.h" namespace devilution { +namespace { namespace controller_button_icon { const std::string_view Playstation_Triangle = "\uE000"; const std::string_view Playstation_Square = "\uE001"; @@ -113,6 +114,50 @@ const std::string_view Xbox_RStick_Click = "\uE069"; const std::string_view Xbox_Xbox = "\uE06A"; } // namespace controller_button_icon +std::string_view ToGenericButtonText(ControllerButton button) +{ + switch (button) { + case devilution::ControllerButton_BUTTON_A: + return "A"; + case devilution::ControllerButton_BUTTON_B: + return "B"; + case devilution::ControllerButton_BUTTON_X: + return "X"; + case devilution::ControllerButton_BUTTON_Y: + return "Y"; + case devilution::ControllerButton_BUTTON_START: + return "Start"; + case devilution::ControllerButton_BUTTON_BACK: + return "Select"; + case devilution::ControllerButton_AXIS_TRIGGERLEFT: + return "LT"; + case devilution::ControllerButton_AXIS_TRIGGERRIGHT: + return "RT"; + case devilution::ControllerButton_BUTTON_LEFTSHOULDER: + return "LB"; + case devilution::ControllerButton_BUTTON_RIGHTSHOULDER: + return "RB"; + case devilution::ControllerButton_BUTTON_LEFTSTICK: + return "LS"; + case devilution::ControllerButton_BUTTON_RIGHTSTICK: + return "RS"; + case devilution::ControllerButton_BUTTON_DPAD_UP: + return "Up"; + case devilution::ControllerButton_BUTTON_DPAD_DOWN: + return "Down"; + case devilution::ControllerButton_BUTTON_DPAD_LEFT: + return "Left"; + case devilution::ControllerButton_BUTTON_DPAD_RIGHT: + return "Right"; + case devilution::ControllerButton_NONE: + return "None"; + case devilution::ControllerButton_IGNORE: + return "Ignored"; + default: + return "Unknown"; + } +} + std::string_view ToPlayStationIcon(ControllerButton button) { switch (button) { @@ -233,49 +278,7 @@ std::string_view ToXboxIcon(ControllerButton button) } } -std::string_view ToGenericButtonText(ControllerButton button) -{ - switch (button) { - case devilution::ControllerButton_BUTTON_A: - return "A"; - case devilution::ControllerButton_BUTTON_B: - return "B"; - case devilution::ControllerButton_BUTTON_X: - return "X"; - case devilution::ControllerButton_BUTTON_Y: - return "Y"; - case devilution::ControllerButton_BUTTON_START: - return "Start"; - case devilution::ControllerButton_BUTTON_BACK: - return "Select"; - case devilution::ControllerButton_AXIS_TRIGGERLEFT: - return "LT"; - case devilution::ControllerButton_AXIS_TRIGGERRIGHT: - return "RT"; - case devilution::ControllerButton_BUTTON_LEFTSHOULDER: - return "LB"; - case devilution::ControllerButton_BUTTON_RIGHTSHOULDER: - return "RB"; - case devilution::ControllerButton_BUTTON_LEFTSTICK: - return "LS"; - case devilution::ControllerButton_BUTTON_RIGHTSTICK: - return "RS"; - case devilution::ControllerButton_BUTTON_DPAD_UP: - return "Up"; - case devilution::ControllerButton_BUTTON_DPAD_DOWN: - return "Down"; - case devilution::ControllerButton_BUTTON_DPAD_LEFT: - return "Left"; - case devilution::ControllerButton_BUTTON_DPAD_RIGHT: - return "Right"; - case devilution::ControllerButton_NONE: - return "None"; - case devilution::ControllerButton_IGNORE: - return "Ignored"; - default: - return "Unknown"; - } -} +} // namespace std::string_view ToString(ControllerButton button) { diff --git a/Source/controls/controller_buttons.h b/Source/controls/controller_buttons.h index 1eb100068..bfe02a003 100644 --- a/Source/controls/controller_buttons.h +++ b/Source/controls/controller_buttons.h @@ -63,120 +63,6 @@ inline bool IsDPadButton(ControllerButton button) || button == ControllerButton_BUTTON_DPAD_RIGHT; } -namespace controller_button_icon { -extern const std::string_view Playstation_Triangle; -extern const std::string_view Playstation_Square; -extern const std::string_view Playstation_X; -extern const std::string_view Playstation_Circle; -extern const std::string_view Playstation_Options; -extern const std::string_view Playstation_Share; -extern const std::string_view Playstation_L2; -extern const std::string_view Playstation_R2; -extern const std::string_view Playstation_L1; -extern const std::string_view Playstation_R1; -extern const std::string_view Playstation_DPad_Up; -extern const std::string_view Playstation_DPad_Right; -extern const std::string_view Playstation_DPad_Down; -extern const std::string_view Playstation_DPad_Left; -extern const std::string_view Playstation_LStick_NW; -extern const std::string_view Playstation_LStick_W; -extern const std::string_view Playstation_LStick_SW; -extern const std::string_view Playstation_LStick_N; -extern const std::string_view Playstation_LStick; -extern const std::string_view Playstation_LStick_S; -extern const std::string_view Playstation_LStick_NE; -extern const std::string_view Playstation_LStick_E; -extern const std::string_view Playstation_LStick_SE; -extern const std::string_view Playstation_L3; -extern const std::string_view Playstation_RStick_NW; -extern const std::string_view Playstation_RStick_W; -extern const std::string_view Playstation_RStick_SW; -extern const std::string_view Playstation_RStick_N; -extern const std::string_view Playstation_RStick; -extern const std::string_view Playstation_RStick_S; -extern const std::string_view Playstation_RStick_NE; -extern const std::string_view Playstation_RStick_E; -extern const std::string_view Playstation_RStick_SE; -extern const std::string_view Playstation_R3; -extern const std::string_view Playstation_Touchpad; -extern const std::string_view Nintendo_X; -extern const std::string_view Nintendo_Y; -extern const std::string_view Nintendo_B; -extern const std::string_view Nintendo_A; -extern const std::string_view Nintendo_Plus; -extern const std::string_view Nintendo_Minus; -extern const std::string_view Nintendo_ZL; -extern const std::string_view Nintendo_ZR; -extern const std::string_view Nintendo_L; -extern const std::string_view Nintendo_R; -extern const std::string_view Nintendo_DPad_Up; -extern const std::string_view Nintendo_DPad_Right; -extern const std::string_view Nintendo_DPad_Down; -extern const std::string_view Nintendo_DPad_Left; -extern const std::string_view Nintendo_LStick_NW; -extern const std::string_view Nintendo_LStick_W; -extern const std::string_view Nintendo_LStick_SW; -extern const std::string_view Nintendo_LStick_N; -extern const std::string_view Nintendo_LStick; -extern const std::string_view Nintendo_LStick_S; -extern const std::string_view Nintendo_LStick_NE; -extern const std::string_view Nintendo_LStick_E; -extern const std::string_view Nintendo_LStick_SE; -extern const std::string_view Nintendo_LStick_Click; -extern const std::string_view Nintendo_RStick_NW; -extern const std::string_view Nintendo_RStick_W; -extern const std::string_view Nintendo_RStick_SW; -extern const std::string_view Nintendo_RStick_N; -extern const std::string_view Nintendo_RStick; -extern const std::string_view Nintendo_RStick_S; -extern const std::string_view Nintendo_RStick_NE; -extern const std::string_view Nintendo_RStick_E; -extern const std::string_view Nintendo_RStick_SE; -extern const std::string_view Nintendo_RStick_Click; -extern const std::string_view Nintendo_Home; -extern const std::string_view Nintendo_Screenshot; -extern const std::string_view Nintendo_SL; -extern const std::string_view Nintendo_SR; -extern const std::string_view Xbox_Y; -extern const std::string_view Xbox_X; -extern const std::string_view Xbox_A; -extern const std::string_view Xbox_B; -extern const std::string_view Xbox_Menu; -extern const std::string_view Xbox_View; -extern const std::string_view Xbox_LT; -extern const std::string_view Xbox_RT; -extern const std::string_view Xbox_LB; -extern const std::string_view Xbox_RB; -extern const std::string_view Xbox_DPad_Up; -extern const std::string_view Xbox_DPad_Right; -extern const std::string_view Xbox_DPad_Down; -extern const std::string_view Xbox_DPad_Left; -extern const std::string_view Xbox_LStick_NW; -extern const std::string_view Xbox_LStick_W; -extern const std::string_view Xbox_LStick_SW; -extern const std::string_view Xbox_LStick_N; -extern const std::string_view Xbox_LStick; -extern const std::string_view Xbox_LStick_NE; -extern const std::string_view Xbox_LStick_E; -extern const std::string_view Xbox_LStick_SE; -extern const std::string_view Xbox_LStick_Click; -extern const std::string_view Xbox_RStick_NW; -extern const std::string_view Xbox_RStick_W; -extern const std::string_view Xbox_RStick_SW; -extern const std::string_view Xbox_RStick_N; -extern const std::string_view Xbox_RStick; -extern const std::string_view Xbox_RStick_S; -extern const std::string_view Xbox_RStick_NE; -extern const std::string_view Xbox_RStick_E; -extern const std::string_view Xbox_RStick_SE; -extern const std::string_view Xbox_RStick_Click; -extern const std::string_view Xbox_Xbox; -} // namespace controller_button_icon - -std::string_view ToPlayStationIcon(ControllerButton button); -std::string_view ToNintendoIcon(ControllerButton button); -std::string_view ToXboxIcon(ControllerButton button); -std::string_view ToGenericButtonText(ControllerButton button); std::string_view ToString(ControllerButton button); } // namespace devilution diff --git a/Source/items.cpp b/Source/items.cpp index 6971a715d..b34ffa5cc 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -1844,25 +1844,12 @@ void printItemMiscGenericGamepad(const Item &item, const bool isOil, bool isCast void printItemMiscGamepad(const Item &item, bool isOil, bool isCastOnTarget) { - std::string_view activateButton; - std::string_view castButton; - switch (GamepadType) { - case GamepadLayout::Generic: + if (GamepadType == GamepadLayout::Generic) { printItemMiscGenericGamepad(item, isOil, isCastOnTarget); return; - case GamepadLayout::Xbox: - activateButton = controller_button_icon::Xbox_Y; - castButton = controller_button_icon::Xbox_X; - break; - case GamepadLayout::PlayStation: - activateButton = controller_button_icon::Playstation_Triangle; - castButton = controller_button_icon::Playstation_Square; - break; - case GamepadLayout::Nintendo: - activateButton = controller_button_icon::Nintendo_X; - castButton = controller_button_icon::Nintendo_Y; - break; } + const std::string_view activateButton = ToString(ControllerButton_BUTTON_Y); + const std::string_view castButton = ToString(ControllerButton_BUTTON_X); if (item._iMiscId == IMISC_MAPOFDOOM) { AddPanelString(fmt::format(fmt::runtime(_("{} to view")), activateButton));