ControllerWindow.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. using Gtk;
  2. using OpenTK.Input;
  3. using System;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Threading;
  7. using Ryujinx.Configuration;
  8. using Ryujinx.Common.Configuration.Hid;
  9. using Ryujinx.Common.Utilities;
  10. using Ryujinx.HLE.FileSystem;
  11. using GUI = Gtk.Builder.ObjectAttribute;
  12. using Key = Ryujinx.Configuration.Hid.Key;
  13. namespace Ryujinx.Ui
  14. {
  15. public class ControllerWindow : Window
  16. {
  17. private PlayerIndex _playerIndex;
  18. private InputConfig _inputConfig;
  19. private bool _isWaitingForInput;
  20. private VirtualFileSystem _virtualFileSystem;
  21. #pragma warning disable CS0649, IDE0044
  22. [GUI] Adjustment _controllerDeadzoneLeft;
  23. [GUI] Adjustment _controllerDeadzoneRight;
  24. [GUI] Adjustment _controllerTriggerThreshold;
  25. [GUI] ComboBoxText _inputDevice;
  26. [GUI] ComboBoxText _profile;
  27. [GUI] ToggleButton _refreshInputDevicesButton;
  28. [GUI] Box _settingsBox;
  29. [GUI] Grid _leftStickKeyboard;
  30. [GUI] Grid _leftStickController;
  31. [GUI] Box _deadZoneLeftBox;
  32. [GUI] Grid _rightStickKeyboard;
  33. [GUI] Grid _rightStickController;
  34. [GUI] Box _deadZoneRightBox;
  35. [GUI] Grid _leftSideTriggerBox;
  36. [GUI] Grid _rightSideTriggerBox;
  37. [GUI] Box _triggerThresholdBox;
  38. [GUI] ComboBoxText _controllerType;
  39. [GUI] ToggleButton _lStickX;
  40. [GUI] CheckButton _invertLStickX;
  41. [GUI] ToggleButton _lStickY;
  42. [GUI] CheckButton _invertLStickY;
  43. [GUI] ToggleButton _lStickUp;
  44. [GUI] ToggleButton _lStickDown;
  45. [GUI] ToggleButton _lStickLeft;
  46. [GUI] ToggleButton _lStickRight;
  47. [GUI] ToggleButton _lStickButton;
  48. [GUI] ToggleButton _dpadUp;
  49. [GUI] ToggleButton _dpadDown;
  50. [GUI] ToggleButton _dpadLeft;
  51. [GUI] ToggleButton _dpadRight;
  52. [GUI] ToggleButton _minus;
  53. [GUI] ToggleButton _l;
  54. [GUI] ToggleButton _zL;
  55. [GUI] ToggleButton _rStickX;
  56. [GUI] CheckButton _invertRStickX;
  57. [GUI] ToggleButton _rStickY;
  58. [GUI] CheckButton _invertRStickY;
  59. [GUI] ToggleButton _rStickUp;
  60. [GUI] ToggleButton _rStickDown;
  61. [GUI] ToggleButton _rStickLeft;
  62. [GUI] ToggleButton _rStickRight;
  63. [GUI] ToggleButton _rStickButton;
  64. [GUI] ToggleButton _a;
  65. [GUI] ToggleButton _b;
  66. [GUI] ToggleButton _x;
  67. [GUI] ToggleButton _y;
  68. [GUI] ToggleButton _plus;
  69. [GUI] ToggleButton _r;
  70. [GUI] ToggleButton _zR;
  71. [GUI] ToggleButton _lSl;
  72. [GUI] ToggleButton _lSr;
  73. [GUI] ToggleButton _rSl;
  74. [GUI] ToggleButton _rSr;
  75. [GUI] Image _controllerImage;
  76. #pragma warning restore CS0649, IDE0044
  77. public ControllerWindow(PlayerIndex controllerId, VirtualFileSystem virtualFileSystem) : this(new Builder("Ryujinx.Ui.ControllerWindow.glade"), controllerId, virtualFileSystem) { }
  78. private ControllerWindow(Builder builder, PlayerIndex controllerId, VirtualFileSystem virtualFileSystem) : base(builder.GetObject("_controllerWin").Handle)
  79. {
  80. builder.Autoconnect(this);
  81. this.Icon = new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.assets.Icon.png");
  82. _playerIndex = controllerId;
  83. _virtualFileSystem = virtualFileSystem;
  84. _inputConfig = ConfigurationState.Instance.Hid.InputConfig.Value.Find(inputConfig => inputConfig.PlayerIndex == _playerIndex);
  85. //Bind Events
  86. _lStickX.Clicked += Button_Pressed;
  87. _lStickY.Clicked += Button_Pressed;
  88. _lStickUp.Clicked += Button_Pressed;
  89. _lStickDown.Clicked += Button_Pressed;
  90. _lStickLeft.Clicked += Button_Pressed;
  91. _lStickRight.Clicked += Button_Pressed;
  92. _lStickButton.Clicked += Button_Pressed;
  93. _dpadUp.Clicked += Button_Pressed;
  94. _dpadDown.Clicked += Button_Pressed;
  95. _dpadLeft.Clicked += Button_Pressed;
  96. _dpadRight.Clicked += Button_Pressed;
  97. _minus.Clicked += Button_Pressed;
  98. _l.Clicked += Button_Pressed;
  99. _zL.Clicked += Button_Pressed;
  100. _lSl.Clicked += Button_Pressed;
  101. _lSr.Clicked += Button_Pressed;
  102. _rStickX.Clicked += Button_Pressed;
  103. _rStickY.Clicked += Button_Pressed;
  104. _rStickUp.Clicked += Button_Pressed;
  105. _rStickDown.Clicked += Button_Pressed;
  106. _rStickLeft.Clicked += Button_Pressed;
  107. _rStickRight.Clicked += Button_Pressed;
  108. _rStickButton.Clicked += Button_Pressed;
  109. _a.Clicked += Button_Pressed;
  110. _b.Clicked += Button_Pressed;
  111. _x.Clicked += Button_Pressed;
  112. _y.Clicked += Button_Pressed;
  113. _plus.Clicked += Button_Pressed;
  114. _r.Clicked += Button_Pressed;
  115. _zR.Clicked += Button_Pressed;
  116. _rSl.Clicked += Button_Pressed;
  117. _rSr.Clicked += Button_Pressed;
  118. // Setup current values
  119. UpdateInputDeviceList();
  120. SetAvailableOptions();
  121. ClearValues();
  122. if (_inputDevice.ActiveId != null) SetCurrentValues();
  123. }
  124. private void UpdateInputDeviceList()
  125. {
  126. _inputDevice.RemoveAll();
  127. _inputDevice.Append("disabled", "Disabled");
  128. _inputDevice.SetActiveId("disabled");
  129. for (int i = 0; i < 20; i++)
  130. {
  131. if (Keyboard.GetState(i).IsConnected)
  132. _inputDevice.Append($"keyboard/{i}", $"Keyboard/{i}");
  133. if (GamePad.GetState(i).IsConnected)
  134. _inputDevice.Append($"controller/{i}", $"Controller/{i} ({GamePad.GetName(i)})");
  135. }
  136. switch (_inputConfig)
  137. {
  138. case KeyboardConfig keyboard:
  139. _inputDevice.SetActiveId($"keyboard/{keyboard.Index}");
  140. break;
  141. case ControllerConfig controller:
  142. _inputDevice.SetActiveId($"controller/{controller.Index}");
  143. break;
  144. }
  145. }
  146. private void SetAvailableOptions()
  147. {
  148. if (_inputDevice.ActiveId != null && _inputDevice.ActiveId.StartsWith("keyboard"))
  149. {
  150. this.ShowAll();
  151. _leftStickController.Hide();
  152. _rightStickController.Hide();
  153. _deadZoneLeftBox.Hide();
  154. _deadZoneRightBox.Hide();
  155. _triggerThresholdBox.Hide();
  156. }
  157. else if (_inputDevice.ActiveId != null && _inputDevice.ActiveId.StartsWith("controller"))
  158. {
  159. this.ShowAll();
  160. _leftStickKeyboard.Hide();
  161. _rightStickKeyboard.Hide();
  162. }
  163. else
  164. {
  165. _settingsBox.Hide();
  166. }
  167. ClearValues();
  168. }
  169. private void SetCurrentValues()
  170. {
  171. SetControllerSpecificFields();
  172. SetProfiles();
  173. if (_inputDevice.ActiveId.StartsWith("keyboard") && _inputConfig is KeyboardConfig)
  174. {
  175. SetValues(_inputConfig);
  176. }
  177. else if (_inputDevice.ActiveId.StartsWith("controller") && _inputConfig is ControllerConfig)
  178. {
  179. SetValues(_inputConfig);
  180. }
  181. }
  182. private void SetControllerSpecificFields()
  183. {
  184. _leftSideTriggerBox.Hide();
  185. _rightSideTriggerBox.Hide();
  186. switch (_controllerType.ActiveId)
  187. {
  188. case "JoyconLeft":
  189. _leftSideTriggerBox.Show();
  190. break;
  191. case "JoyconRight":
  192. _rightSideTriggerBox.Show();
  193. break;
  194. }
  195. switch (_controllerType.ActiveId)
  196. {
  197. case "ProController":
  198. _controllerImage.Pixbuf = new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.assets.ProCon.svg", 400, 400);
  199. break;
  200. case "JoyconLeft":
  201. _controllerImage.Pixbuf = new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.assets.JoyConLeft.svg", 400, 400);
  202. break;
  203. case "JoyconRight":
  204. _controllerImage.Pixbuf = new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.assets.JoyConRight.svg", 400, 400);
  205. break;
  206. default:
  207. _controllerImage.Pixbuf = new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.assets.JoyConPair.svg", 400, 400);
  208. break;
  209. }
  210. }
  211. private void ClearValues()
  212. {
  213. _lStickX.Label = "Unbound";
  214. _lStickY.Label = "Unbound";
  215. _lStickUp.Label = "Unbound";
  216. _lStickDown.Label = "Unbound";
  217. _lStickLeft.Label = "Unbound";
  218. _lStickRight.Label = "Unbound";
  219. _lStickButton.Label = "Unbound";
  220. _dpadUp.Label = "Unbound";
  221. _dpadDown.Label = "Unbound";
  222. _dpadLeft.Label = "Unbound";
  223. _dpadRight.Label = "Unbound";
  224. _minus.Label = "Unbound";
  225. _l.Label = "Unbound";
  226. _zL.Label = "Unbound";
  227. _lSl.Label = "Unbound";
  228. _lSr.Label = "Unbound";
  229. _rStickX.Label = "Unbound";
  230. _rStickY.Label = "Unbound";
  231. _rStickUp.Label = "Unbound";
  232. _rStickDown.Label = "Unbound";
  233. _rStickLeft.Label = "Unbound";
  234. _rStickRight.Label = "Unbound";
  235. _rStickButton.Label = "Unbound";
  236. _a.Label = "Unbound";
  237. _b.Label = "Unbound";
  238. _x.Label = "Unbound";
  239. _y.Label = "Unbound";
  240. _plus.Label = "Unbound";
  241. _r.Label = "Unbound";
  242. _zR.Label = "Unbound";
  243. _rSl.Label = "Unbound";
  244. _rSr.Label = "Unbound";
  245. _controllerDeadzoneLeft.Value = 0;
  246. _controllerDeadzoneRight.Value = 0;
  247. _controllerTriggerThreshold.Value = 0;
  248. }
  249. private void SetValues(InputConfig config)
  250. {
  251. switch (config)
  252. {
  253. case KeyboardConfig keyboardConfig:
  254. _controllerType.SetActiveId(keyboardConfig.ControllerType.ToString());
  255. _lStickUp.Label = keyboardConfig.LeftJoycon.StickUp.ToString();
  256. _lStickDown.Label = keyboardConfig.LeftJoycon.StickDown.ToString();
  257. _lStickLeft.Label = keyboardConfig.LeftJoycon.StickLeft.ToString();
  258. _lStickRight.Label = keyboardConfig.LeftJoycon.StickRight.ToString();
  259. _lStickButton.Label = keyboardConfig.LeftJoycon.StickButton.ToString();
  260. _dpadUp.Label = keyboardConfig.LeftJoycon.DPadUp.ToString();
  261. _dpadDown.Label = keyboardConfig.LeftJoycon.DPadDown.ToString();
  262. _dpadLeft.Label = keyboardConfig.LeftJoycon.DPadLeft.ToString();
  263. _dpadRight.Label = keyboardConfig.LeftJoycon.DPadRight.ToString();
  264. _minus.Label = keyboardConfig.LeftJoycon.ButtonMinus.ToString();
  265. _l.Label = keyboardConfig.LeftJoycon.ButtonL.ToString();
  266. _zL.Label = keyboardConfig.LeftJoycon.ButtonZl.ToString();
  267. _lSl.Label = keyboardConfig.LeftJoycon.ButtonSl.ToString();
  268. _lSr.Label = keyboardConfig.LeftJoycon.ButtonSr.ToString();
  269. _rStickUp.Label = keyboardConfig.RightJoycon.StickUp.ToString();
  270. _rStickDown.Label = keyboardConfig.RightJoycon.StickDown.ToString();
  271. _rStickLeft.Label = keyboardConfig.RightJoycon.StickLeft.ToString();
  272. _rStickRight.Label = keyboardConfig.RightJoycon.StickRight.ToString();
  273. _rStickButton.Label = keyboardConfig.RightJoycon.StickButton.ToString();
  274. _a.Label = keyboardConfig.RightJoycon.ButtonA.ToString();
  275. _b.Label = keyboardConfig.RightJoycon.ButtonB.ToString();
  276. _x.Label = keyboardConfig.RightJoycon.ButtonX.ToString();
  277. _y.Label = keyboardConfig.RightJoycon.ButtonY.ToString();
  278. _plus.Label = keyboardConfig.RightJoycon.ButtonPlus.ToString();
  279. _r.Label = keyboardConfig.RightJoycon.ButtonR.ToString();
  280. _zR.Label = keyboardConfig.RightJoycon.ButtonZr.ToString();
  281. _rSl.Label = keyboardConfig.RightJoycon.ButtonSl.ToString();
  282. _rSr.Label = keyboardConfig.RightJoycon.ButtonSr.ToString();
  283. break;
  284. case ControllerConfig controllerConfig:
  285. _controllerType.SetActiveId(controllerConfig.ControllerType.ToString());
  286. _lStickX.Label = controllerConfig.LeftJoycon.StickX.ToString();
  287. _invertLStickX.Active = controllerConfig.LeftJoycon.InvertStickX;
  288. _lStickY.Label = controllerConfig.LeftJoycon.StickY.ToString();
  289. _invertLStickY.Active = controllerConfig.LeftJoycon.InvertStickY;
  290. _lStickButton.Label = controllerConfig.LeftJoycon.StickButton.ToString();
  291. _dpadUp.Label = controllerConfig.LeftJoycon.DPadUp.ToString();
  292. _dpadDown.Label = controllerConfig.LeftJoycon.DPadDown.ToString();
  293. _dpadLeft.Label = controllerConfig.LeftJoycon.DPadLeft.ToString();
  294. _dpadRight.Label = controllerConfig.LeftJoycon.DPadRight.ToString();
  295. _minus.Label = controllerConfig.LeftJoycon.ButtonMinus.ToString();
  296. _l.Label = controllerConfig.LeftJoycon.ButtonL.ToString();
  297. _zL.Label = controllerConfig.LeftJoycon.ButtonZl.ToString();
  298. _lSl.Label = controllerConfig.LeftJoycon.ButtonSl.ToString();
  299. _lSr.Label = controllerConfig.LeftJoycon.ButtonSr.ToString();
  300. _rStickX.Label = controllerConfig.RightJoycon.StickX.ToString();
  301. _invertRStickX.Active = controllerConfig.RightJoycon.InvertStickX;
  302. _rStickY.Label = controllerConfig.RightJoycon.StickY.ToString();
  303. _invertRStickY.Active = controllerConfig.RightJoycon.InvertStickY;
  304. _rStickButton.Label = controllerConfig.RightJoycon.StickButton.ToString();
  305. _a.Label = controllerConfig.RightJoycon.ButtonA.ToString();
  306. _b.Label = controllerConfig.RightJoycon.ButtonB.ToString();
  307. _x.Label = controllerConfig.RightJoycon.ButtonX.ToString();
  308. _y.Label = controllerConfig.RightJoycon.ButtonY.ToString();
  309. _plus.Label = controllerConfig.RightJoycon.ButtonPlus.ToString();
  310. _r.Label = controllerConfig.RightJoycon.ButtonR.ToString();
  311. _zR.Label = controllerConfig.RightJoycon.ButtonZr.ToString();
  312. _rSl.Label = controllerConfig.RightJoycon.ButtonSl.ToString();
  313. _rSr.Label = controllerConfig.RightJoycon.ButtonSr.ToString();
  314. _controllerDeadzoneLeft.Value = controllerConfig.DeadzoneLeft;
  315. _controllerDeadzoneRight.Value = controllerConfig.DeadzoneRight;
  316. _controllerTriggerThreshold.Value = controllerConfig.TriggerThreshold;
  317. break;
  318. }
  319. }
  320. private InputConfig GetValues()
  321. {
  322. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  323. {
  324. Enum.TryParse(_lStickUp.Label, out Key lStickUp);
  325. Enum.TryParse(_lStickDown.Label, out Key lStickDown);
  326. Enum.TryParse(_lStickLeft.Label, out Key lStickLeft);
  327. Enum.TryParse(_lStickRight.Label, out Key lStickRight);
  328. Enum.TryParse(_lStickButton.Label, out Key lStickButton);
  329. Enum.TryParse(_dpadUp.Label, out Key lDPadUp);
  330. Enum.TryParse(_dpadDown.Label, out Key lDPadDown);
  331. Enum.TryParse(_dpadLeft.Label, out Key lDPadLeft);
  332. Enum.TryParse(_dpadRight.Label, out Key lDPadRight);
  333. Enum.TryParse(_minus.Label, out Key lButtonMinus);
  334. Enum.TryParse(_l.Label, out Key lButtonL);
  335. Enum.TryParse(_zL.Label, out Key lButtonZl);
  336. Enum.TryParse(_lSl.Label, out Key lButtonSl);
  337. Enum.TryParse(_lSr.Label, out Key lButtonSr);
  338. Enum.TryParse(_rStickUp.Label, out Key rStickUp);
  339. Enum.TryParse(_rStickDown.Label, out Key rStickDown);
  340. Enum.TryParse(_rStickLeft.Label, out Key rStickLeft);
  341. Enum.TryParse(_rStickRight.Label, out Key rStickRight);
  342. Enum.TryParse(_rStickButton.Label, out Key rStickButton);
  343. Enum.TryParse(_a.Label, out Key rButtonA);
  344. Enum.TryParse(_b.Label, out Key rButtonB);
  345. Enum.TryParse(_x.Label, out Key rButtonX);
  346. Enum.TryParse(_y.Label, out Key rButtonY);
  347. Enum.TryParse(_plus.Label, out Key rButtonPlus);
  348. Enum.TryParse(_r.Label, out Key rButtonR);
  349. Enum.TryParse(_zR.Label, out Key rButtonZr);
  350. Enum.TryParse(_rSl.Label, out Key rButtonSl);
  351. Enum.TryParse(_rSr.Label, out Key rButtonSr);
  352. return new KeyboardConfig
  353. {
  354. Index = int.Parse(_inputDevice.ActiveId.Split("/")[1]),
  355. ControllerType = Enum.Parse<ControllerType>(_controllerType.ActiveId),
  356. PlayerIndex = _playerIndex,
  357. LeftJoycon = new NpadKeyboardLeft
  358. {
  359. StickUp = lStickUp,
  360. StickDown = lStickDown,
  361. StickLeft = lStickLeft,
  362. StickRight = lStickRight,
  363. StickButton = lStickButton,
  364. DPadUp = lDPadUp,
  365. DPadDown = lDPadDown,
  366. DPadLeft = lDPadLeft,
  367. DPadRight = lDPadRight,
  368. ButtonMinus = lButtonMinus,
  369. ButtonL = lButtonL,
  370. ButtonZl = lButtonZl,
  371. ButtonSl = lButtonSl,
  372. ButtonSr = lButtonSr
  373. },
  374. RightJoycon = new NpadKeyboardRight
  375. {
  376. StickUp = rStickUp,
  377. StickDown = rStickDown,
  378. StickLeft = rStickLeft,
  379. StickRight = rStickRight,
  380. StickButton = rStickButton,
  381. ButtonA = rButtonA,
  382. ButtonB = rButtonB,
  383. ButtonX = rButtonX,
  384. ButtonY = rButtonY,
  385. ButtonPlus = rButtonPlus,
  386. ButtonR = rButtonR,
  387. ButtonZr = rButtonZr,
  388. ButtonSl = rButtonSl,
  389. ButtonSr = rButtonSr
  390. },
  391. Hotkeys = new KeyboardHotkeys
  392. {
  393. ToggleVsync = Key.Tab //TODO: Make this an option in the GUI
  394. }
  395. };
  396. }
  397. if (_inputDevice.ActiveId.StartsWith("controller"))
  398. {
  399. Enum.TryParse(_lStickX.Label, out ControllerInputId lStickX);
  400. Enum.TryParse(_lStickY.Label, out ControllerInputId lStickY);
  401. Enum.TryParse(_lStickButton.Label, out ControllerInputId lStickButton);
  402. Enum.TryParse(_minus.Label, out ControllerInputId lButtonMinus);
  403. Enum.TryParse(_l.Label, out ControllerInputId lButtonL);
  404. Enum.TryParse(_zL.Label, out ControllerInputId lButtonZl);
  405. Enum.TryParse(_lSl.Label, out ControllerInputId lButtonSl);
  406. Enum.TryParse(_lSr.Label, out ControllerInputId lButtonSr);
  407. Enum.TryParse(_dpadUp.Label, out ControllerInputId lDPadUp);
  408. Enum.TryParse(_dpadDown.Label, out ControllerInputId lDPadDown);
  409. Enum.TryParse(_dpadLeft.Label, out ControllerInputId lDPadLeft);
  410. Enum.TryParse(_dpadRight.Label, out ControllerInputId lDPadRight);
  411. Enum.TryParse(_rStickX.Label, out ControllerInputId rStickX);
  412. Enum.TryParse(_rStickY.Label, out ControllerInputId rStickY);
  413. Enum.TryParse(_rStickButton.Label, out ControllerInputId rStickButton);
  414. Enum.TryParse(_a.Label, out ControllerInputId rButtonA);
  415. Enum.TryParse(_b.Label, out ControllerInputId rButtonB);
  416. Enum.TryParse(_x.Label, out ControllerInputId rButtonX);
  417. Enum.TryParse(_y.Label, out ControllerInputId rButtonY);
  418. Enum.TryParse(_plus.Label, out ControllerInputId rButtonPlus);
  419. Enum.TryParse(_r.Label, out ControllerInputId rButtonR);
  420. Enum.TryParse(_zR.Label, out ControllerInputId rButtonZr);
  421. Enum.TryParse(_rSl.Label, out ControllerInputId rButtonSl);
  422. Enum.TryParse(_rSr.Label, out ControllerInputId rButtonSr);
  423. return new ControllerConfig
  424. {
  425. Index = int.Parse(_inputDevice.ActiveId.Split("/")[1]),
  426. ControllerType = Enum.Parse<ControllerType>(_controllerType.ActiveId),
  427. PlayerIndex = _playerIndex,
  428. DeadzoneLeft = (float)_controllerDeadzoneLeft.Value,
  429. DeadzoneRight = (float)_controllerDeadzoneRight.Value,
  430. TriggerThreshold = (float)_controllerTriggerThreshold.Value,
  431. LeftJoycon = new NpadControllerLeft
  432. {
  433. InvertStickX = _invertLStickX.Active,
  434. StickX = lStickX,
  435. InvertStickY = _invertLStickY.Active,
  436. StickY = lStickY,
  437. StickButton = lStickButton,
  438. ButtonMinus = lButtonMinus,
  439. ButtonL = lButtonL,
  440. ButtonZl = lButtonZl,
  441. ButtonSl = lButtonSl,
  442. ButtonSr = lButtonSr,
  443. DPadUp = lDPadUp,
  444. DPadDown = lDPadDown,
  445. DPadLeft = lDPadLeft,
  446. DPadRight = lDPadRight
  447. },
  448. RightJoycon = new NpadControllerRight
  449. {
  450. InvertStickX = _invertRStickX.Active,
  451. StickX = rStickX,
  452. InvertStickY = _invertRStickY.Active,
  453. StickY = rStickY,
  454. StickButton = rStickButton,
  455. ButtonA = rButtonA,
  456. ButtonB = rButtonB,
  457. ButtonX = rButtonX,
  458. ButtonY = rButtonY,
  459. ButtonPlus = rButtonPlus,
  460. ButtonR = rButtonR,
  461. ButtonZr = rButtonZr,
  462. ButtonSl = rButtonSl,
  463. ButtonSr = rButtonSr
  464. }
  465. };
  466. }
  467. if (!_inputDevice.ActiveId.StartsWith("disabled"))
  468. {
  469. GtkDialog.CreateErrorDialog("Some fields entered where invalid and therefore your config was not saved.");
  470. }
  471. return null;
  472. }
  473. private static bool IsAnyKeyPressed(out Key pressedKey, int index = 0)
  474. {
  475. KeyboardState keyboardState = Keyboard.GetState(index);
  476. foreach (Key key in Enum.GetValues(typeof(Key)))
  477. {
  478. if (keyboardState.IsKeyDown((OpenTK.Input.Key)key))
  479. {
  480. pressedKey = key;
  481. return true;
  482. }
  483. }
  484. pressedKey = Key.Unbound;
  485. return false;
  486. }
  487. private static bool IsAnyButtonPressed(out ControllerInputId pressedButton, int index, double triggerThreshold)
  488. {
  489. JoystickState joystickState = Joystick.GetState(index);
  490. JoystickCapabilities joystickCapabilities = Joystick.GetCapabilities(index);
  491. //Buttons
  492. for (int i = 0; i != joystickCapabilities.ButtonCount; i++)
  493. {
  494. if (joystickState.IsButtonDown(i))
  495. {
  496. Enum.TryParse($"Button{i}", out pressedButton);
  497. return true;
  498. }
  499. }
  500. //Axis
  501. for (int i = 0; i != joystickCapabilities.AxisCount; i++)
  502. {
  503. if (joystickState.GetAxis(i) > 0.5f && joystickState.GetAxis(i) > triggerThreshold)
  504. {
  505. Enum.TryParse($"Axis{i}", out pressedButton);
  506. return true;
  507. }
  508. }
  509. //Hats
  510. for (int i = 0; i != joystickCapabilities.HatCount; i++)
  511. {
  512. JoystickHatState hatState = joystickState.GetHat((JoystickHat)i);
  513. string pos = null;
  514. if (hatState.IsUp) pos = "Up";
  515. if (hatState.IsDown) pos = "Down";
  516. if (hatState.IsLeft) pos = "Left";
  517. if (hatState.IsRight) pos = "Right";
  518. if (pos == null) continue;
  519. Enum.TryParse($"Hat{i}{pos}", out pressedButton);
  520. return true;
  521. }
  522. pressedButton = ControllerInputId.Unbound;
  523. return false;
  524. }
  525. private string GetProfileBasePath()
  526. {
  527. string path = System.IO.Path.Combine(_virtualFileSystem.GetBasePath(), "profiles");
  528. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  529. {
  530. path = System.IO.Path.Combine(path, "keyboard");
  531. }
  532. else if (_inputDevice.ActiveId.StartsWith("controller"))
  533. {
  534. path = System.IO.Path.Combine(path, "controller");
  535. }
  536. return path;
  537. }
  538. //Events
  539. private void InputDevice_Changed(object sender, EventArgs args)
  540. {
  541. SetAvailableOptions();
  542. SetControllerSpecificFields();
  543. if (_inputDevice.ActiveId != null) SetProfiles();
  544. }
  545. private void Controller_Changed(object sender, EventArgs args)
  546. {
  547. SetControllerSpecificFields();
  548. }
  549. private void RefreshInputDevicesButton_Pressed(object sender, EventArgs args)
  550. {
  551. UpdateInputDeviceList();
  552. _refreshInputDevicesButton.SetStateFlags(0, true);
  553. }
  554. private void Button_Pressed(object sender, EventArgs args)
  555. {
  556. if (_isWaitingForInput)
  557. {
  558. return;
  559. }
  560. _isWaitingForInput = true;
  561. Thread inputThread = new Thread(() =>
  562. {
  563. Button button = (ToggleButton)sender;
  564. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  565. {
  566. Key pressedKey;
  567. int index = int.Parse(_inputDevice.ActiveId.Split("/")[1]);
  568. while (!IsAnyKeyPressed(out pressedKey, index))
  569. {
  570. if (Mouse.GetState().IsAnyButtonDown || Keyboard.GetState().IsKeyDown(OpenTK.Input.Key.Escape))
  571. {
  572. Application.Invoke(delegate
  573. {
  574. button.SetStateFlags(0, true);
  575. });
  576. _isWaitingForInput = false;
  577. return;
  578. }
  579. }
  580. Application.Invoke(delegate
  581. {
  582. button.Label = pressedKey.ToString();
  583. button.SetStateFlags(0, true);
  584. });
  585. }
  586. else if (_inputDevice.ActiveId.StartsWith("controller"))
  587. {
  588. ControllerInputId pressedButton;
  589. int index = int.Parse(_inputDevice.ActiveId.Split("/")[1]);
  590. while (!IsAnyButtonPressed(out pressedButton, index, _controllerTriggerThreshold.Value))
  591. {
  592. if (Mouse.GetState().IsAnyButtonDown || Keyboard.GetState().IsAnyKeyDown)
  593. {
  594. Application.Invoke(delegate
  595. {
  596. button.SetStateFlags(0, true);
  597. });
  598. _isWaitingForInput = false;
  599. return;
  600. }
  601. }
  602. Application.Invoke(delegate
  603. {
  604. button.Label = pressedButton.ToString();
  605. button.SetStateFlags(0, true);
  606. });
  607. }
  608. _isWaitingForInput = false;
  609. });
  610. inputThread.Name = "GUI.InputThread";
  611. inputThread.IsBackground = true;
  612. inputThread.Start();
  613. }
  614. private void SetProfiles()
  615. {
  616. string basePath = GetProfileBasePath();
  617. if (!Directory.Exists(basePath))
  618. {
  619. Directory.CreateDirectory(basePath);
  620. }
  621. _profile.RemoveAll();
  622. _profile.Append("default", "Default");
  623. foreach (string profile in Directory.GetFiles(basePath, "*.*", SearchOption.AllDirectories))
  624. {
  625. _profile.Append(System.IO.Path.GetFileName(profile), System.IO.Path.GetFileNameWithoutExtension(profile));
  626. }
  627. }
  628. private void ProfileLoad_Activated(object sender, EventArgs args)
  629. {
  630. ((ToggleButton)sender).SetStateFlags(0, true);
  631. if (_inputDevice.ActiveId == "disabled" || _profile.ActiveId == null) return;
  632. InputConfig config = null;
  633. int pos = _profile.Active;
  634. if (_profile.ActiveId == "default")
  635. {
  636. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  637. {
  638. config = new KeyboardConfig
  639. {
  640. Index = 0,
  641. ControllerType = ControllerType.JoyconPair,
  642. LeftJoycon = new NpadKeyboardLeft
  643. {
  644. StickUp = Key.W,
  645. StickDown = Key.S,
  646. StickLeft = Key.A,
  647. StickRight = Key.D,
  648. StickButton = Key.F,
  649. DPadUp = Key.Up,
  650. DPadDown = Key.Down,
  651. DPadLeft = Key.Left,
  652. DPadRight = Key.Right,
  653. ButtonMinus = Key.Minus,
  654. ButtonL = Key.E,
  655. ButtonZl = Key.Q,
  656. ButtonSl = Key.Unbound,
  657. ButtonSr = Key.Unbound
  658. },
  659. RightJoycon = new NpadKeyboardRight
  660. {
  661. StickUp = Key.I,
  662. StickDown = Key.K,
  663. StickLeft = Key.J,
  664. StickRight = Key.L,
  665. StickButton = Key.H,
  666. ButtonA = Key.Z,
  667. ButtonB = Key.X,
  668. ButtonX = Key.C,
  669. ButtonY = Key.V,
  670. ButtonPlus = Key.Plus,
  671. ButtonR = Key.U,
  672. ButtonZr = Key.O,
  673. ButtonSl = Key.Unbound,
  674. ButtonSr = Key.Unbound
  675. },
  676. Hotkeys = new KeyboardHotkeys
  677. {
  678. ToggleVsync = Key.Tab
  679. }
  680. };
  681. }
  682. else if (_inputDevice.ActiveId.StartsWith("controller"))
  683. {
  684. config = new ControllerConfig
  685. {
  686. Index = 0,
  687. ControllerType = ControllerType.ProController,
  688. DeadzoneLeft = 0.1f,
  689. DeadzoneRight = 0.1f,
  690. TriggerThreshold = 0.5f,
  691. LeftJoycon = new NpadControllerLeft
  692. {
  693. StickX = ControllerInputId.Axis0,
  694. StickY = ControllerInputId.Axis1,
  695. StickButton = ControllerInputId.Button8,
  696. DPadUp = ControllerInputId.Hat0Up,
  697. DPadDown = ControllerInputId.Hat0Down,
  698. DPadLeft = ControllerInputId.Hat0Left,
  699. DPadRight = ControllerInputId.Hat0Right,
  700. ButtonMinus = ControllerInputId.Button6,
  701. ButtonL = ControllerInputId.Button4,
  702. ButtonZl = ControllerInputId.Axis2,
  703. ButtonSl = ControllerInputId.Unbound,
  704. ButtonSr = ControllerInputId.Unbound,
  705. InvertStickX = false,
  706. InvertStickY = false
  707. },
  708. RightJoycon = new NpadControllerRight
  709. {
  710. StickX = ControllerInputId.Axis3,
  711. StickY = ControllerInputId.Axis4,
  712. StickButton = ControllerInputId.Button9,
  713. ButtonA = ControllerInputId.Button1,
  714. ButtonB = ControllerInputId.Button0,
  715. ButtonX = ControllerInputId.Button3,
  716. ButtonY = ControllerInputId.Button2,
  717. ButtonPlus = ControllerInputId.Button7,
  718. ButtonR = ControllerInputId.Button5,
  719. ButtonZr = ControllerInputId.Axis5,
  720. ButtonSl = ControllerInputId.Unbound,
  721. ButtonSr = ControllerInputId.Unbound,
  722. InvertStickX = false,
  723. InvertStickY = false
  724. }
  725. };
  726. }
  727. }
  728. else
  729. {
  730. string path = System.IO.Path.Combine(GetProfileBasePath(), _profile.ActiveId);
  731. if (!File.Exists(path))
  732. {
  733. if (pos >= 0)
  734. {
  735. _profile.Remove(pos);
  736. }
  737. return;
  738. }
  739. using (Stream stream = File.OpenRead(path))
  740. {
  741. try
  742. {
  743. config = JsonHelper.Deserialize<ControllerConfig>(stream);
  744. }
  745. catch (ArgumentException)
  746. {
  747. try
  748. {
  749. config = JsonHelper.Deserialize<KeyboardConfig>(stream);
  750. }
  751. catch { }
  752. }
  753. }
  754. }
  755. SetValues(config);
  756. }
  757. private void ProfileAdd_Activated(object sender, EventArgs args)
  758. {
  759. ((ToggleButton)sender).SetStateFlags(0, true);
  760. if (_inputDevice.ActiveId == "disabled") return;
  761. InputConfig inputConfig = GetValues();
  762. ProfileDialog profileDialog = new ProfileDialog();
  763. if (inputConfig == null) return;
  764. if (profileDialog.Run() == (int)ResponseType.Ok)
  765. {
  766. string path = System.IO.Path.Combine(GetProfileBasePath(), profileDialog.FileName);
  767. string jsonString;
  768. if (inputConfig is KeyboardConfig keyboardConfig)
  769. {
  770. jsonString = JsonHelper.Serialize(keyboardConfig, true);
  771. }
  772. else
  773. {
  774. jsonString = JsonHelper.Serialize(inputConfig as ControllerConfig, true);
  775. }
  776. File.WriteAllText(path, jsonString);
  777. }
  778. profileDialog.Dispose();
  779. SetProfiles();
  780. }
  781. private void ProfileRemove_Activated(object sender, EventArgs args)
  782. {
  783. ((ToggleButton) sender).SetStateFlags(0, true);
  784. if (_inputDevice.ActiveId == "disabled" || _profile.ActiveId == "default" || _profile.ActiveId == null) return;
  785. MessageDialog confirmDialog = GtkDialog.CreateConfirmationDialog("Deleting Profile", "This action is irreversible, are your sure you want to continue?");
  786. if (confirmDialog.Run() == (int)ResponseType.Yes)
  787. {
  788. string path = System.IO.Path.Combine(GetProfileBasePath(), _profile.ActiveId);
  789. if (File.Exists(path))
  790. {
  791. File.Delete(path);
  792. }
  793. SetProfiles();
  794. }
  795. }
  796. private void SaveToggle_Activated(object sender, EventArgs args)
  797. {
  798. InputConfig inputConfig = GetValues();
  799. if (_inputConfig == null && inputConfig != null)
  800. {
  801. ConfigurationState.Instance.Hid.InputConfig.Value.Add(inputConfig);
  802. }
  803. else
  804. {
  805. if (_inputDevice.ActiveId == "disabled")
  806. {
  807. ConfigurationState.Instance.Hid.InputConfig.Value.Remove(_inputConfig);
  808. }
  809. else if (inputConfig != null)
  810. {
  811. int index = ConfigurationState.Instance.Hid.InputConfig.Value.IndexOf(_inputConfig);
  812. ConfigurationState.Instance.Hid.InputConfig.Value[index] = inputConfig;
  813. }
  814. }
  815. Dispose();
  816. }
  817. private void CloseToggle_Activated(object sender, EventArgs args)
  818. {
  819. Dispose();
  820. }
  821. }
  822. }