/ src / Ryujinx.Gtk3 / UI / Helper / ButtonHelper.cs
ButtonHelper.cs
  1  using Ryujinx.Common.Configuration.Hid.Controller;
  2  using Ryujinx.Input;
  3  using System;
  4  using System.Collections.Generic;
  5  using Key = Ryujinx.Common.Configuration.Hid.Key;
  6  using StickInputId = Ryujinx.Common.Configuration.Hid.Controller.StickInputId;
  7  
  8  namespace Ryujinx.UI.Helper
  9  {
 10      public static class ButtonHelper
 11      {
 12          private static readonly Dictionary<Key, string> _keysMap = new()
 13          {
 14              { Key.Unknown, "Unknown" },
 15              { Key.ShiftLeft, "ShiftLeft" },
 16              { Key.ShiftRight, "ShiftRight" },
 17              { Key.ControlLeft, "CtrlLeft" },
 18              { Key.ControlRight, "CtrlRight" },
 19              { Key.AltLeft, OperatingSystem.IsMacOS() ? "OptLeft" : "AltLeft" },
 20              { Key.AltRight, OperatingSystem.IsMacOS() ? "OptRight" : "AltRight" },
 21              { Key.WinLeft, OperatingSystem.IsMacOS() ? "CmdLeft" : "WinLeft" },
 22              { Key.WinRight, OperatingSystem.IsMacOS() ? "CmdRight" : "WinRight" },
 23              { Key.Up, "Up" },
 24              { Key.Down, "Down" },
 25              { Key.Left, "Left" },
 26              { Key.Right, "Right" },
 27              { Key.Enter, "Enter" },
 28              { Key.Escape, "Escape" },
 29              { Key.Space, "Space" },
 30              { Key.Tab, "Tab" },
 31              { Key.BackSpace, "Backspace" },
 32              { Key.Insert, "Insert" },
 33              { Key.Delete, "Delete" },
 34              { Key.PageUp, "PageUp" },
 35              { Key.PageDown, "PageDown" },
 36              { Key.Home, "Home" },
 37              { Key.End, "End" },
 38              { Key.CapsLock, "CapsLock" },
 39              { Key.ScrollLock, "ScrollLock" },
 40              { Key.PrintScreen, "PrintScreen" },
 41              { Key.Pause, "Pause" },
 42              { Key.NumLock, "NumLock" },
 43              { Key.Clear, "Clear" },
 44              { Key.Keypad0, "Keypad0" },
 45              { Key.Keypad1, "Keypad1" },
 46              { Key.Keypad2, "Keypad2" },
 47              { Key.Keypad3, "Keypad3" },
 48              { Key.Keypad4, "Keypad4" },
 49              { Key.Keypad5, "Keypad5" },
 50              { Key.Keypad6, "Keypad6" },
 51              { Key.Keypad7, "Keypad7" },
 52              { Key.Keypad8, "Keypad8" },
 53              { Key.Keypad9, "Keypad9" },
 54              { Key.KeypadDivide, "KeypadDivide" },
 55              { Key.KeypadMultiply, "KeypadMultiply" },
 56              { Key.KeypadSubtract, "KeypadSubtract" },
 57              { Key.KeypadAdd, "KeypadAdd" },
 58              { Key.KeypadDecimal, "KeypadDecimal" },
 59              { Key.KeypadEnter, "KeypadEnter" },
 60              { Key.Number0, "0" },
 61              { Key.Number1, "1" },
 62              { Key.Number2, "2" },
 63              { Key.Number3, "3" },
 64              { Key.Number4, "4" },
 65              { Key.Number5, "5" },
 66              { Key.Number6, "6" },
 67              { Key.Number7, "7" },
 68              { Key.Number8, "8" },
 69              { Key.Number9, "9" },
 70              { Key.Tilde, "~" },
 71              { Key.Grave, "`" },
 72              { Key.Minus, "-" },
 73              { Key.Plus, "+" },
 74              { Key.BracketLeft, "[" },
 75              { Key.BracketRight, "]" },
 76              { Key.Semicolon, ";" },
 77              { Key.Quote, "'" },
 78              { Key.Comma, "," },
 79              { Key.Period, "." },
 80              { Key.Slash, "/" },
 81              { Key.BackSlash, "\\" },
 82              { Key.Unbound, "Unbound" },
 83          };
 84  
 85          private static readonly Dictionary<GamepadInputId, string> _gamepadInputIdMap = new()
 86          {
 87              { GamepadInputId.LeftStick, "LeftStick" },
 88              { GamepadInputId.RightStick, "RightStick" },
 89              { GamepadInputId.LeftShoulder, "LeftShoulder" },
 90              { GamepadInputId.RightShoulder, "RightShoulder" },
 91              { GamepadInputId.LeftTrigger, "LeftTrigger" },
 92              { GamepadInputId.RightTrigger, "RightTrigger" },
 93              { GamepadInputId.DpadUp, "DpadUp" },
 94              { GamepadInputId.DpadDown, "DpadDown" },
 95              { GamepadInputId.DpadLeft, "DpadLeft" },
 96              { GamepadInputId.DpadRight, "DpadRight" },
 97              { GamepadInputId.Minus, "Minus" },
 98              { GamepadInputId.Plus, "Plus" },
 99              { GamepadInputId.Guide, "Guide" },
100              { GamepadInputId.Misc1, "Misc1" },
101              { GamepadInputId.Paddle1, "Paddle1" },
102              { GamepadInputId.Paddle2, "Paddle2" },
103              { GamepadInputId.Paddle3, "Paddle3" },
104              { GamepadInputId.Paddle4, "Paddle4" },
105              { GamepadInputId.Touchpad, "Touchpad" },
106              { GamepadInputId.SingleLeftTrigger0, "SingleLeftTrigger0" },
107              { GamepadInputId.SingleRightTrigger0, "SingleRightTrigger0" },
108              { GamepadInputId.SingleLeftTrigger1, "SingleLeftTrigger1" },
109              { GamepadInputId.SingleRightTrigger1, "SingleRightTrigger1" },
110              { GamepadInputId.Unbound, "Unbound" },
111          };
112  
113          private static readonly Dictionary<StickInputId, string> _stickInputIdMap = new()
114          {
115              { StickInputId.Left, "StickLeft" },
116              { StickInputId.Right, "StickRight" },
117              { StickInputId.Unbound, "Unbound" },
118          };
119  
120          public static string ToString(Button button)
121          {
122              string keyString = "";
123  
124              switch (button.Type)
125              {
126                  case ButtonType.Key:
127                      var key = button.AsHidType<Key>();
128  
129                      if (!_keysMap.TryGetValue(button.AsHidType<Key>(), out keyString))
130                      {
131                          keyString = key.ToString();
132                      }
133  
134                      break;
135                  case ButtonType.GamepadButtonInputId:
136                      var gamepadButton = button.AsHidType<GamepadInputId>();
137  
138                      if (!_gamepadInputIdMap.TryGetValue(button.AsHidType<GamepadInputId>(), out keyString))
139                      {
140                          keyString = gamepadButton.ToString();
141                      }
142  
143                      break;
144                  case ButtonType.StickId:
145                      var stickInput = button.AsHidType<StickInputId>();
146  
147                      if (!_stickInputIdMap.TryGetValue(button.AsHidType<StickInputId>(), out keyString))
148                      {
149                          keyString = stickInput.ToString();
150                      }
151  
152                      break;
153              }
154  
155              return keyString;
156          }
157      }
158  }