From 234e1f721ff31d68dfcd1a1820076156be596b58 Mon Sep 17 00:00:00 2001
From: Stenzek <stenzek@gmail.com>
Date: Sat, 11 Jan 2025 21:25:11 +1000
Subject: [PATCH] FullscreenUI: Improve controller settings page

- Add 'Clear Bindings' menu item.
- Show icons in controller types.
- Add confirmation to Reset Settings.
---
 dep/imgui/include/IconsPromptFont.h |  1 +
 src/core/controller.cpp             | 11 ++--
 src/core/controller.h               |  4 +-
 src/core/fullscreen_ui.cpp          | 95 +++++++++++++++++++++++------
 src/util/imgui_glyph_ranges.inl     |  2 +-
 5 files changed, 84 insertions(+), 29 deletions(-)

diff --git a/dep/imgui/include/IconsPromptFont.h b/dep/imgui/include/IconsPromptFont.h
index 8eee203b5..63e2093a2 100644
--- a/dep/imgui/include/IconsPromptFont.h
+++ b/dep/imgui/include/IconsPromptFont.h
@@ -351,3 +351,4 @@
 #define ICON_PF_POPN_WL "\xE2\x8B\x86"
 #define ICON_PF_POPN_WR "\xE2\x8B\x87"
 #define ICON_PF_POPN_R "\xE2\x8B\x88"
+#define ICON_PF_NAVIGATION_BACK "\xE2\x8F\x8C"
diff --git a/src/core/controller.cpp b/src/core/controller.cpp
index 1218dbd1c..877aadfb2 100644
--- a/src/core/controller.cpp
+++ b/src/core/controller.cpp
@@ -17,10 +17,11 @@
 
 #include "util/state_wrapper.h"
 
+#include "IconsPromptFont.h"
 #include "fmt/format.h"
 
 static const Controller::ControllerInfo s_none_info = {
-  ControllerType::None, "None", TRANSLATE_NOOP("ControllerType", "Not Connected"), nullptr, {}, {}};
+  ControllerType::None, "None", TRANSLATE_NOOP("ControllerType", "Not Connected"), ICON_PF_QUESTION, {}, {}};
 
 static const Controller::ControllerInfo* s_controller_info[] = {
   &s_none_info,
@@ -174,13 +175,9 @@ const Controller::ControllerInfo* Controller::GetControllerInfo(std::string_view
   return nullptr;
 }
 
-std::vector<std::pair<std::string, std::string>> Controller::GetControllerTypeNames()
+std::span<const Controller::ControllerInfo*> Controller::GetControllerInfoList()
 {
-  std::vector<std::pair<std::string, std::string>> ret;
-  for (const ControllerInfo* info : s_controller_info)
-    ret.emplace_back(info->name, Host::TranslateToString("ControllerType", info->display_name));
-
-  return ret;
+  return s_controller_info;
 }
 
 std::optional<u32> Controller::GetBindIndex(ControllerType type, std::string_view bind_name)
diff --git a/src/core/controller.h b/src/core/controller.h
index 0d3e8df27..9e9266b04 100644
--- a/src/core/controller.h
+++ b/src/core/controller.h
@@ -96,8 +96,8 @@ public:
   /// Returns the default type for the specified port.
   static const char* GetDefaultPadType(u32 pad);
 
-  /// Returns a list of controller type names. Pair of [name, display name].
-  static std::vector<std::pair<std::string, std::string>> GetControllerTypeNames();
+  /// Returns a list of all controller types.
+  static std::span<const ControllerInfo*> GetControllerInfoList();
 
   /// Gets the integer code for an axis in the specified controller type.
   static std::optional<u32> GetBindIndex(ControllerType type, std::string_view bind_name);
diff --git a/src/core/fullscreen_ui.cpp b/src/core/fullscreen_ui.cpp
index 1e1ea7b04..d59a18811 100644
--- a/src/core/fullscreen_ui.cpp
+++ b/src/core/fullscreen_ui.cpp
@@ -297,7 +297,7 @@ static TinyString GetEffectiveTinyStringSetting(SettingsInterface* bsi, const ch
 static void DoCopyGameSettings();
 static void DoClearGameSettings();
 static void CopyGlobalControllerSettingsToGame();
-static void ResetControllerSettings();
+static void BeginResetControllerSettings();
 static void DoLoadInputProfile();
 static void DoSaveInputProfile();
 static void DoSaveNewInputProfile();
@@ -382,7 +382,8 @@ static void DrawInputBindingButton(SettingsInterface* bsi, InputBindingInfo::Typ
                                    const char* name, const char* display_name, const char* icon_name,
                                    bool show_type = true);
 static void ClearInputBindingVariables();
-static void StartAutomaticBinding(u32 port);
+static void StartAutomaticBindingForPort(u32 port);
+static void StartClearBindingsForPort(u32 port);
 
 //////////////////////////////////////////////////////////////////////////
 // Save State List
@@ -2904,7 +2905,7 @@ void FullscreenUI::DrawFolderSetting(SettingsInterface* bsi, const char* title,
   }
 }
 
-void FullscreenUI::StartAutomaticBinding(u32 port)
+void FullscreenUI::StartAutomaticBindingForPort(u32 port)
 {
   InputManager::DeviceList devices = InputManager::EnumerateDevices();
   if (devices.empty())
@@ -2941,6 +2942,22 @@ void FullscreenUI::StartAutomaticBinding(u32 port)
                    });
 }
 
+void FullscreenUI::StartClearBindingsForPort(u32 port)
+{
+  ImGuiFullscreen::OpenConfirmMessageDialog(
+    FSUI_STR("Clear Mappings"),
+    FSUI_STR("Are you sure you want to clear all mappings for this controller?\n\nYou cannot undo this action."),
+    [port](bool result) {
+      if (!result)
+        return;
+
+      auto lock = Host::GetSettingsLock();
+      SettingsInterface* bsi = GetEditingSettingsInterface();
+      InputManager::ClearPortBindings(*bsi, port);
+      ShowToast({}, FSUI_CSTR("Controller mapping cleared."));
+    });
+}
+
 void FullscreenUI::SwitchToSettings()
 {
   s_state.game_settings_entry.reset();
@@ -3149,7 +3166,7 @@ void FullscreenUI::DrawSettingsWindow()
       }
     }
 
-    if (NavButton(ICON_FA_BACKWARD, true, true))
+    if (NavButton(ICON_PF_NAVIGATION_BACK, true, true))
       ReturnToPreviousWindow();
 
     if (s_state.game_settings_entry)
@@ -3949,12 +3966,20 @@ void FullscreenUI::DoSaveInputProfile()
                    });
 }
 
-void FullscreenUI::ResetControllerSettings()
+void FullscreenUI::BeginResetControllerSettings()
 {
-  SettingsInterface* dsi = GetEditingSettingsInterface();
+  OpenConfirmMessageDialog(FSUI_STR("Reset Controller Settings"),
+                           FSUI_STR("Are you sure you want to restore the default controller configuration?\n\nAll "
+                                    "bindings and configuration will be lost. You cannot undo this action."),
+                           [](bool result) {
+                             if (!result)
+                               return;
 
-  Settings::SetDefaultControllerConfig(*dsi);
-  ShowToast(std::string(), FSUI_STR("Controller settings reset to default."));
+                             SettingsInterface* dsi = GetEditingSettingsInterface();
+
+                             Settings::SetDefaultControllerConfig(*dsi);
+                             ShowToast(std::string(), FSUI_STR("Controller settings reset to default."));
+                           });
 }
 
 void FullscreenUI::DrawControllerSettingsPage()
@@ -3993,14 +4018,16 @@ void FullscreenUI::DrawControllerSettingsPage()
   {
     if (MenuButton(FSUI_ICONSTR(ICON_FA_COPY, "Copy Global Settings"),
                    FSUI_CSTR("Copies the global controller configuration to this game.")))
+    {
       CopyGlobalControllerSettingsToGame();
+    }
   }
   else
   {
     if (MenuButton(FSUI_ICONSTR(ICON_FA_DUMPSTER_FIRE, "Reset Settings"),
                    FSUI_CSTR("Resets all configuration to defaults (including bindings).")))
     {
-      ResetControllerSettings();
+      BeginResetControllerSettings();
     }
   }
 
@@ -4062,26 +4089,38 @@ void FullscreenUI::DrawControllerSettingsPage()
     const TinyString type =
       bsi->GetTinyStringValue(section.c_str(), "Type", Controller::GetDefaultPadType(global_slot));
     const Controller::ControllerInfo* ci = Controller::GetControllerInfo(type);
-    if (MenuButton(TinyString::from_format("{}##type{}", FSUI_ICONSTR(ICON_FA_GAMEPAD, "Controller Type"), global_slot),
-                   ci ? Host::TranslateToCString("ControllerType", ci->display_name) : FSUI_CSTR("Unknown")))
+    TinyString value;
+    if (ci && ci->icon_name)
+      value.format("{} {}", ci->icon_name, ci->GetDisplayName());
+    else if (ci)
+      value = ci->GetDisplayName();
+    else
+      value = FSUI_VSTR("Unknown");
+
+    if (MenuButtonWithValue(
+          TinyString::from_format("{}##type{}", FSUI_ICONSTR(ICON_FA_GAMEPAD, "Controller Type"), global_slot),
+          FSUI_CSTR("Selects the type of emulated controller for this port."), value))
     {
-      std::vector<std::pair<std::string, std::string>> raw_options(Controller::GetControllerTypeNames());
+      const std::span<const Controller::ControllerInfo*> infos = Controller::GetControllerInfoList();
       ImGuiFullscreen::ChoiceDialogOptions options;
-      options.reserve(raw_options.size());
-      for (auto& it : raw_options)
+      options.reserve(infos.size());
+      for (const Controller::ControllerInfo* it : infos)
       {
-        options.emplace_back(std::move(it.second), type == it.first);
+        if (it->icon_name)
+          options.emplace_back(fmt::format("{} {}", it->icon_name, it->GetDisplayName()), type == it->name);
+        else
+          options.emplace_back(it->GetDisplayName(), type == it->name);
       }
+
       OpenChoiceDialog(TinyString::from_format(FSUI_FSTR("Port {} Controller Type"), global_slot + 1), false,
                        std::move(options),
-                       [game_settings, section,
-                        raw_options = std::move(raw_options)](s32 index, const std::string& title, bool checked) {
+                       [game_settings, section, infos](s32 index, const std::string& title, bool checked) {
                          if (index < 0)
                            return;
 
                          auto lock = Host::GetSettingsLock();
                          SettingsInterface* bsi = GetEditingSettingsInterface(game_settings);
-                         bsi->SetStringValue(section.c_str(), "Type", raw_options[index].first.c_str());
+                         bsi->SetStringValue(section.c_str(), "Type", infos[index]->name);
                          SetSettingsChanged(bsi);
                          CloseChoiceDialog();
                        });
@@ -4093,9 +4132,19 @@ void FullscreenUI::DrawControllerSettingsPage()
     if (MenuButton(FSUI_ICONSTR(ICON_FA_MAGIC, "Automatic Mapping"),
                    FSUI_CSTR("Attempts to map the selected port to a chosen controller.")))
     {
-      StartAutomaticBinding(global_slot);
+      StartAutomaticBindingForPort(global_slot);
     }
 
+    if (MenuButton(FSUI_ICONSTR(ICON_FA_TRASH, "Clear Mappings"),
+                   FSUI_CSTR("Removes all bindings for this controller port.")))
+    {
+      StartClearBindingsForPort(global_slot);
+    }
+
+    MenuHeading(
+      SmallString::from_format(fmt::runtime(FSUI_ICONSTR(ICON_FA_MICROCHIP, "Controller Port {} Bindings")),
+                               Controller::GetPortDisplayName(mtap_port, mtap_slot, mtap_enabled[mtap_port])));
+
     for (const Controller::ControllerBindingInfo& bi : ci->bindings)
       DrawInputBindingButton(bsi, bi.type, section.c_str(), bi.name, ci->GetBindingDisplayName(bi), bi.icon_name, true);
 
@@ -8149,7 +8198,9 @@ TRANSLATE_NOOP("FullscreenUI", "Always Track Uploads");
 TRANSLATE_NOOP("FullscreenUI", "An error occurred while deleting empty game settings:\n{}");
 TRANSLATE_NOOP("FullscreenUI", "An error occurred while saving game settings:\n{}");
 TRANSLATE_NOOP("FullscreenUI", "Apply Image Patches");
+TRANSLATE_NOOP("FullscreenUI", "Are you sure you want to clear all mappings for this controller?\n\nYou cannot undo this action.");
 TRANSLATE_NOOP("FullscreenUI", "Are you sure you want to clear the current post-processing chain? All configuration will be lost.");
+TRANSLATE_NOOP("FullscreenUI", "Are you sure you want to restore the default controller configuration?\n\nAll bindings and configuration will be lost. You cannot undo this action.");
 TRANSLATE_NOOP("FullscreenUI", "Aspect Ratio");
 TRANSLATE_NOOP("FullscreenUI", "Attempts to detect one pixel high/wide lines that rely on non-upscaled rasterization behavior, filling in gaps introduced by upscaling.");
 TRANSLATE_NOOP("FullscreenUI", "Attempts to map the selected port to a chosen controller.");
@@ -8195,6 +8246,7 @@ TRANSLATE_NOOP("FullscreenUI", "Cheats");
 TRANSLATE_NOOP("FullscreenUI", "Chooses the backend to use for rendering the console/game visuals.");
 TRANSLATE_NOOP("FullscreenUI", "Chooses the language used for UI elements.");
 TRANSLATE_NOOP("FullscreenUI", "Clean Boot");
+TRANSLATE_NOOP("FullscreenUI", "Clear Mappings");
 TRANSLATE_NOOP("FullscreenUI", "Clear Settings");
 TRANSLATE_NOOP("FullscreenUI", "Clear Shaders");
 TRANSLATE_NOOP("FullscreenUI", "Clears a shader from the chain.");
@@ -8210,10 +8262,12 @@ TRANSLATE_NOOP("FullscreenUI", "Confirm Power Off");
 TRANSLATE_NOOP("FullscreenUI", "Console Settings");
 TRANSLATE_NOOP("FullscreenUI", "Contributor List");
 TRANSLATE_NOOP("FullscreenUI", "Controller Port {}");
+TRANSLATE_NOOP("FullscreenUI", "Controller Port {} Bindings");
 TRANSLATE_NOOP("FullscreenUI", "Controller Port {} Macros");
 TRANSLATE_NOOP("FullscreenUI", "Controller Port {} Settings");
 TRANSLATE_NOOP("FullscreenUI", "Controller Settings");
 TRANSLATE_NOOP("FullscreenUI", "Controller Type");
+TRANSLATE_NOOP("FullscreenUI", "Controller mapping cleared.");
 TRANSLATE_NOOP("FullscreenUI", "Controller preset '{}' loaded.");
 TRANSLATE_NOOP("FullscreenUI", "Controller preset '{}' saved.");
 TRANSLATE_NOOP("FullscreenUI", "Controller settings reset to default.");
@@ -8543,11 +8597,13 @@ TRANSLATE_NOOP("FullscreenUI", "Reloads the shaders from disk, applying any chan
 TRANSLATE_NOOP("FullscreenUI", "Remove From Chain");
 TRANSLATE_NOOP("FullscreenUI", "Remove From List");
 TRANSLATE_NOOP("FullscreenUI", "Removed stage {} ({}).");
+TRANSLATE_NOOP("FullscreenUI", "Removes all bindings for this controller port.");
 TRANSLATE_NOOP("FullscreenUI", "Removes this shader from the chain.");
 TRANSLATE_NOOP("FullscreenUI", "Renames existing save states when saving to a backup file.");
 TRANSLATE_NOOP("FullscreenUI", "Rendering");
 TRANSLATE_NOOP("FullscreenUI", "Replaces these settings with a previously saved controller preset.");
 TRANSLATE_NOOP("FullscreenUI", "Rescan All Games");
+TRANSLATE_NOOP("FullscreenUI", "Reset Controller Settings");
 TRANSLATE_NOOP("FullscreenUI", "Reset Memory Card Directory");
 TRANSLATE_NOOP("FullscreenUI", "Reset Play Time");
 TRANSLATE_NOOP("FullscreenUI", "Reset Settings");
@@ -8612,6 +8668,7 @@ TRANSLATE_NOOP("FullscreenUI", "Selects the percentage of the normal clock speed
 TRANSLATE_NOOP("FullscreenUI", "Selects the quality at which screenshots will be compressed.");
 TRANSLATE_NOOP("FullscreenUI", "Selects the resolution scale that will be applied to the final image. 1x will downsample to the original console resolution.");
 TRANSLATE_NOOP("FullscreenUI", "Selects the resolution to use in fullscreen modes.");
+TRANSLATE_NOOP("FullscreenUI", "Selects the type of emulated controller for this port.");
 TRANSLATE_NOOP("FullscreenUI", "Selects the view that the game list will open to.");
 TRANSLATE_NOOP("FullscreenUI", "Serial");
 TRANSLATE_NOOP("FullscreenUI", "Session: {}");
diff --git a/src/util/imgui_glyph_ranges.inl b/src/util/imgui_glyph_ranges.inl
index 240cce6da..88ae3cad1 100644
--- a/src/util/imgui_glyph_ranges.inl
+++ b/src/util/imgui_glyph_ranges.inl
@@ -3,6 +3,6 @@
 
 static constexpr ImWchar FA_ICON_RANGE[] = { 0xe06f,0xe070,0xe086,0xe086,0xf002,0xf002,0xf005,0xf005,0xf007,0xf007,0xf00c,0xf00e,0xf011,0xf013,0xf017,0xf017,0xf019,0xf019,0xf01c,0xf01c,0xf021,0xf021,0xf023,0xf023,0xf025,0xf026,0xf028,0xf028,0xf02e,0xf02e,0xf030,0xf030,0xf03a,0xf03a,0xf03d,0xf03d,0xf04a,0xf04c,0xf050,0xf050,0xf056,0xf056,0xf05e,0xf05e,0xf062,0xf063,0xf065,0xf067,0xf071,0xf071,0xf075,0xf075,0xf077,0xf078,0xf07b,0xf07c,0xf083,0xf085,0xf091,0xf091,0xf0ac,0xf0ae,0xf0b2,0xf0b2,0xf0c3,0xf0c3,0xf0c5,0xf0c5,0xf0c7,0xf0c9,0xf0cb,0xf0cb,0xf0d0,0xf0d0,0xf0dc,0xf0dc,0xf0e0,0xf0e0,0xf0e2,0xf0e2,0xf0e7,0xf0e8,0xf0eb,0xf0eb,0xf0f1,0xf0f1,0xf0f3,0xf0f3,0xf0fe,0xf0fe,0xf110,0xf110,0xf11b,0xf11c,0xf140,0xf140,0xf144,0xf144,0xf146,0xf146,0xf14a,0xf14a,0xf15b,0xf15d,0xf191,0xf192,0xf1ab,0xf1ab,0xf1c0,0xf1c0,0xf1c5,0xf1c5,0xf1de,0xf1de,0xf1e6,0xf1e6,0xf1eb,0xf1eb,0xf1f8,0xf1f8,0xf1fb,0xf1fc,0xf201,0xf201,0xf240,0xf240,0xf242,0xf242,0xf245,0xf245,0xf26c,0xf26c,0xf279,0xf279,0xf2c1,0xf2c1,0xf2d0,0xf2d0,0xf2db,0xf2db,0xf2f1,0xf2f2,0xf302,0xf302,0xf31e,0xf31e,0xf338,0xf338,0xf35d,0xf35d,0xf360,0xf360,0xf362,0xf362,0xf3fd,0xf3fd,0xf410,0xf410,0xf422,0xf422,0xf424,0xf424,0xf462,0xf462,0xf466,0xf466,0xf4ce,0xf4ce,0xf500,0xf500,0xf51f,0xf51f,0xf538,0xf538,0xf53f,0xf53f,0xf545,0xf545,0xf547,0xf548,0xf54c,0xf54c,0xf55b,0xf55b,0xf55d,0xf55d,0xf565,0xf565,0xf56e,0xf570,0xf575,0xf575,0xf5a2,0xf5a2,0xf5aa,0xf5aa,0xf5ae,0xf5ae,0xf5c7,0xf5c7,0xf5cb,0xf5cb,0xf5e7,0xf5e7,0xf5ee,0xf5ee,0xf61f,0xf61f,0xf65d,0xf65e,0xf6a9,0xf6a9,0xf6cf,0xf6cf,0xf70c,0xf70c,0xf70e,0xf70e,0xf78c,0xf78c,0xf794,0xf794,0xf7a0,0xf7a0,0xf7a4,0xf7a5,0xf7c2,0xf7c2,0xf807,0xf807,0xf815,0xf815,0xf818,0xf818,0xf84c,0xf84c,0xf87d,0xf87d,0xf8cc,0xf8cc,0x0,0x0 };
 
-static constexpr ImWchar PF_ICON_RANGE[] = { 0x2196,0x2199,0x219e,0x21a3,0x21b0,0x21b3,0x21ba,0x21c3,0x21c7,0x21ca,0x21d0,0x21d4,0x21e0,0x21e3,0x21e6,0x21e8,0x21ed,0x21ee,0x21f7,0x21f8,0x21fa,0x21fb,0x221a,0x221a,0x227a,0x227f,0x2284,0x2284,0x22bf,0x22c8,0x2349,0x2349,0x235e,0x235e,0x2360,0x2361,0x2364,0x2366,0x23b2,0x23b4,0x23ce,0x23ce,0x23f4,0x23f7,0x2427,0x243a,0x243c,0x243e,0x2446,0x2446,0x2460,0x246b,0x248f,0x248f,0x24f5,0x24fd,0x24ff,0x24ff,0x2717,0x2717,0x278a,0x278e,0x27fc,0x27fc,0xe000,0xe001,0xff21,0xff3a,0x1f52b,0x1f52b,0x0,0x0 };
+static constexpr ImWchar PF_ICON_RANGE[] = { 0x2196,0x2199,0x219e,0x21a3,0x21b0,0x21b3,0x21ba,0x21c3,0x21c7,0x21ca,0x21d0,0x21d4,0x21e0,0x21e3,0x21e6,0x21e8,0x21ed,0x21ee,0x21f7,0x21f8,0x21fa,0x21fb,0x221a,0x221a,0x227a,0x227f,0x2284,0x2284,0x22bf,0x22c8,0x2349,0x2349,0x235e,0x235e,0x2360,0x2361,0x2364,0x2366,0x23b2,0x23b4,0x23cc,0x23cc,0x23ce,0x23ce,0x23f4,0x23f7,0x2427,0x243a,0x243c,0x243e,0x2446,0x2446,0x2460,0x246b,0x248f,0x248f,0x24f5,0x24fd,0x24ff,0x24ff,0x2717,0x2717,0x2753,0x2753,0x278a,0x278e,0x27fc,0x27fc,0xe000,0xe001,0xff21,0xff3a,0x1f52b,0x1f52b,0x0,0x0 };
 
 static constexpr ImWchar EMOJI_ICON_RANGE[] = { 0x2139,0x2139,0x23e9,0x23ea,0x23f8,0x23f8,0x26a0,0x26a0,0x1f4be,0x1f4be,0x1f4c2,0x1f4c2,0x1f4f7,0x1f4f8,0x1f504,0x1f504,0x1f507,0x1f507,0x1f509,0x1f50a,0x1f50d,0x1f50d,0x1f513,0x1f513,0x0,0x0 };