KeyRebindTab.xaml.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. using System.Numerics;
  2. using Content.Client.Stylesheets;
  3. using Content.Shared.CCVar;
  4. using Content.Shared.Input;
  5. using Robust.Client.AutoGenerated;
  6. using Robust.Client.Input;
  7. using Robust.Client.UserInterface;
  8. using Robust.Client.UserInterface.Controls;
  9. using Robust.Client.UserInterface.XAML;
  10. using Robust.Shared;
  11. using Robust.Shared.Configuration;
  12. using Robust.Shared.Input;
  13. using Robust.Shared.Timing;
  14. using Robust.Shared.Utility;
  15. using static Robust.Client.UserInterface.Controls.BoxContainer;
  16. namespace Content.Client.Options.UI.Tabs
  17. {
  18. [GenerateTypedNameReferences]
  19. public sealed partial class KeyRebindTab : Control
  20. {
  21. // List of key functions that must be registered as toggle instead.
  22. private static readonly HashSet<BoundKeyFunction> ToggleFunctions = new()
  23. {
  24. EngineKeyFunctions.ShowDebugMonitors,
  25. EngineKeyFunctions.HideUI,
  26. };
  27. [Dependency] private readonly IInputManager _inputManager = default!;
  28. [Dependency] private readonly IConfigurationManager _cfg = default!;
  29. private BindButton? _currentlyRebinding;
  30. private readonly Dictionary<BoundKeyFunction, KeyControl> _keyControls =
  31. new();
  32. private readonly List<Action> _deferCommands = new();
  33. private void HandleToggleUSQWERTYCheckbox(BaseButton.ButtonToggledEventArgs args)
  34. {
  35. _cfg.SetCVar(CVars.DisplayUSQWERTYHotkeys, args.Pressed);
  36. _cfg.SaveToFile();
  37. }
  38. private void InitToggleWalk()
  39. {
  40. if (_cfg.GetCVar(CCVars.ToggleWalk))
  41. {
  42. ToggleFunctions.Add(EngineKeyFunctions.Walk);
  43. }
  44. else
  45. {
  46. ToggleFunctions.Remove(EngineKeyFunctions.Walk);
  47. }
  48. }
  49. private void HandleToggleWalk(BaseButton.ButtonToggledEventArgs args)
  50. {
  51. _cfg.SetCVar(CCVars.ToggleWalk, args.Pressed);
  52. _cfg.SaveToFile();
  53. InitToggleWalk();
  54. if (!_keyControls.TryGetValue(EngineKeyFunctions.Walk, out var keyControl))
  55. {
  56. return;
  57. }
  58. var bindingType = args.Pressed ? KeyBindingType.Toggle : KeyBindingType.State;
  59. for (var i = 0; i <= 1; i++)
  60. {
  61. var binding = (i == 0 ? keyControl.BindButton1 : keyControl.BindButton2).Binding;
  62. if (binding == null)
  63. {
  64. continue;
  65. }
  66. var registration = new KeyBindingRegistration
  67. {
  68. Function = EngineKeyFunctions.Walk,
  69. BaseKey = binding.BaseKey,
  70. Mod1 = binding.Mod1,
  71. Mod2 = binding.Mod2,
  72. Mod3 = binding.Mod3,
  73. Priority = binding.Priority,
  74. Type = bindingType,
  75. CanFocus = binding.CanFocus,
  76. CanRepeat = binding.CanRepeat,
  77. };
  78. _deferCommands.Add(() =>
  79. {
  80. _inputManager.RemoveBinding(binding);
  81. _inputManager.RegisterBinding(registration);
  82. });
  83. }
  84. _deferCommands.Add(_inputManager.SaveToUserData);
  85. }
  86. private void HandleStaticStorageUI(BaseButton.ButtonToggledEventArgs args)
  87. {
  88. _cfg.SetCVar(CCVars.StaticStorageUI, args.Pressed);
  89. _cfg.SaveToFile();
  90. }
  91. public KeyRebindTab()
  92. {
  93. IoCManager.InjectDependencies(this);
  94. RobustXamlLoader.Load(this);
  95. ResetAllButton.OnPressed += _ =>
  96. {
  97. _deferCommands.Add(() =>
  98. {
  99. _inputManager.ResetAllBindings();
  100. _inputManager.SaveToUserData();
  101. });
  102. };
  103. var first = true;
  104. void AddHeader(string headerContents)
  105. {
  106. if (!first)
  107. {
  108. KeybindsContainer.AddChild(new Control { MinSize = new Vector2(0, 8) });
  109. }
  110. first = false;
  111. KeybindsContainer.AddChild(new Label
  112. {
  113. Text = Loc.GetString(headerContents),
  114. FontColorOverride = StyleNano.NanoGold,
  115. StyleClasses = { StyleNano.StyleClassLabelKeyText }
  116. });
  117. }
  118. void AddButton(BoundKeyFunction function)
  119. {
  120. var control = new KeyControl(this, function);
  121. KeybindsContainer.AddChild(control);
  122. _keyControls.Add(function, control);
  123. }
  124. void AddCheckBox(string checkBoxName, bool currentState, Action<BaseButton.ButtonToggledEventArgs>? callBackOnClick)
  125. {
  126. CheckBox newCheckBox = new CheckBox() { Text = Loc.GetString(checkBoxName) };
  127. newCheckBox.Pressed = currentState;
  128. newCheckBox.OnToggled += callBackOnClick;
  129. KeybindsContainer.AddChild(newCheckBox);
  130. }
  131. AddHeader("ui-options-header-general");
  132. AddCheckBox("ui-options-hotkey-keymap", _cfg.GetCVar(CVars.DisplayUSQWERTYHotkeys), HandleToggleUSQWERTYCheckbox);
  133. AddHeader("ui-options-header-movement");
  134. AddButton(EngineKeyFunctions.MoveUp);
  135. AddButton(EngineKeyFunctions.MoveLeft);
  136. AddButton(EngineKeyFunctions.MoveDown);
  137. AddButton(EngineKeyFunctions.MoveRight);
  138. AddButton(EngineKeyFunctions.Walk);
  139. AddCheckBox("ui-options-hotkey-toggle-walk", _cfg.GetCVar(CCVars.ToggleWalk), HandleToggleWalk);
  140. InitToggleWalk();
  141. AddHeader("ui-options-header-camera");
  142. AddButton(EngineKeyFunctions.CameraRotateLeft);
  143. AddButton(EngineKeyFunctions.CameraRotateRight);
  144. AddButton(EngineKeyFunctions.CameraReset);
  145. AddButton(ContentKeyFunctions.ZoomIn);
  146. AddButton(ContentKeyFunctions.ZoomOut);
  147. AddButton(ContentKeyFunctions.ResetZoom);
  148. AddHeader("ui-options-header-interaction-basic");
  149. AddButton(EngineKeyFunctions.Use);
  150. AddButton(EngineKeyFunctions.UseSecondary);
  151. AddButton(ContentKeyFunctions.UseItemInHand);
  152. AddButton(ContentKeyFunctions.AltUseItemInHand);
  153. AddButton(ContentKeyFunctions.ActivateItemInWorld);
  154. AddButton(ContentKeyFunctions.AltActivateItemInWorld);
  155. AddButton(ContentKeyFunctions.Drop);
  156. AddButton(ContentKeyFunctions.ExamineEntity);
  157. AddButton(ContentKeyFunctions.SwapHands);
  158. AddButton(ContentKeyFunctions.MoveStoredItem);
  159. AddButton(ContentKeyFunctions.RotateStoredItem);
  160. AddButton(ContentKeyFunctions.SaveItemLocation);
  161. AddHeader("ui-options-header-interaction-adv");
  162. AddButton(ContentKeyFunctions.SmartEquipBackpack);
  163. AddButton(ContentKeyFunctions.SmartEquipBelt);
  164. AddButton(ContentKeyFunctions.OpenBackpack);
  165. AddButton(ContentKeyFunctions.Lay); // Stalker-Changes-UI
  166. AddButton(ContentKeyFunctions.OpenBelt);
  167. AddButton(ContentKeyFunctions.ThrowItemInHand);
  168. AddButton(ContentKeyFunctions.TryPullObject);
  169. AddButton(ContentKeyFunctions.MovePulledObject);
  170. AddButton(ContentKeyFunctions.ReleasePulledObject);
  171. AddButton(ContentKeyFunctions.Point);
  172. AddButton(ContentKeyFunctions.RotateObjectClockwise);
  173. AddButton(ContentKeyFunctions.RotateObjectCounterclockwise);
  174. AddButton(ContentKeyFunctions.FlipObject);
  175. AddHeader("ui-options-header-ui");
  176. AddButton(ContentKeyFunctions.FocusChat);
  177. AddButton(ContentKeyFunctions.FocusLocalChat);
  178. AddButton(ContentKeyFunctions.FocusEmote);
  179. AddButton(ContentKeyFunctions.FocusWhisperChat);
  180. AddButton(ContentKeyFunctions.FocusRadio);
  181. AddButton(ContentKeyFunctions.FocusLOOC);
  182. AddButton(ContentKeyFunctions.FocusOOC);
  183. AddButton(ContentKeyFunctions.FocusAdminChat);
  184. AddButton(ContentKeyFunctions.FocusDeadChat);
  185. AddButton(ContentKeyFunctions.FocusConsoleChat);
  186. AddButton(ContentKeyFunctions.CycleChatChannelForward);
  187. AddButton(ContentKeyFunctions.CycleChatChannelBackward);
  188. AddButton(ContentKeyFunctions.OpenCharacterMenu);
  189. AddButton(ContentKeyFunctions.OpenCraftingMenu);
  190. AddButton(ContentKeyFunctions.OpenGuidebook);
  191. AddButton(ContentKeyFunctions.OpenInventoryMenu);
  192. AddButton(ContentKeyFunctions.OpenAHelp);
  193. AddButton(ContentKeyFunctions.OpenActionsMenu);
  194. AddButton(ContentKeyFunctions.OpenEmotesMenu);
  195. AddButton(ContentKeyFunctions.ToggleRoundEndSummaryWindow);
  196. AddButton(ContentKeyFunctions.OpenEntitySpawnWindow);
  197. AddButton(ContentKeyFunctions.OpenSandboxWindow);
  198. AddButton(ContentKeyFunctions.OpenTileSpawnWindow);
  199. AddButton(ContentKeyFunctions.OpenDecalSpawnWindow);
  200. AddButton(ContentKeyFunctions.OpenAdminMenu);
  201. AddButton(EngineKeyFunctions.WindowCloseAll);
  202. AddButton(EngineKeyFunctions.WindowCloseRecent);
  203. AddButton(EngineKeyFunctions.EscapeMenu);
  204. AddButton(ContentKeyFunctions.EscapeContext);
  205. // Shitmed Change Start - TODO: Add hands, feet and groin targeting.
  206. AddHeader("ui-options-header-targeting");
  207. AddButton(ContentKeyFunctions.TargetHead);
  208. AddButton(ContentKeyFunctions.TargetTorso);
  209. AddButton(ContentKeyFunctions.TargetLeftArm);
  210. AddButton(ContentKeyFunctions.TargetLeftHand);
  211. AddButton(ContentKeyFunctions.TargetRightArm);
  212. AddButton(ContentKeyFunctions.TargetRightHand);
  213. AddButton(ContentKeyFunctions.TargetLeftLeg);
  214. AddButton(ContentKeyFunctions.TargetLeftFoot);
  215. AddButton(ContentKeyFunctions.TargetRightLeg);
  216. AddButton(ContentKeyFunctions.TargetRightFoot);
  217. // Shitmed Change End
  218. AddHeader("ui-options-header-misc");
  219. AddButton(ContentKeyFunctions.TakeScreenshot);
  220. AddButton(ContentKeyFunctions.TakeScreenshotNoUI);
  221. AddButton(ContentKeyFunctions.ToggleFullscreen);
  222. AddHeader("ui-options-header-hotbar");
  223. foreach (var boundKey in ContentKeyFunctions.GetHotbarBoundKeys())
  224. {
  225. AddButton(boundKey);
  226. }
  227. AddHeader("ui-options-header-shuttle");
  228. AddButton(ContentKeyFunctions.ShuttleStrafeUp);
  229. AddButton(ContentKeyFunctions.ShuttleStrafeRight);
  230. AddButton(ContentKeyFunctions.ShuttleStrafeLeft);
  231. AddButton(ContentKeyFunctions.ShuttleStrafeDown);
  232. AddButton(ContentKeyFunctions.ShuttleRotateLeft);
  233. AddButton(ContentKeyFunctions.ShuttleRotateRight);
  234. AddButton(ContentKeyFunctions.ShuttleBrake);
  235. AddHeader("ui-options-header-map-editor");
  236. AddButton(EngineKeyFunctions.EditorPlaceObject);
  237. AddButton(EngineKeyFunctions.EditorCancelPlace);
  238. AddButton(EngineKeyFunctions.EditorGridPlace);
  239. AddButton(EngineKeyFunctions.EditorLinePlace);
  240. AddButton(EngineKeyFunctions.EditorRotateObject);
  241. AddButton(ContentKeyFunctions.EditorFlipObject);
  242. AddButton(ContentKeyFunctions.EditorCopyObject);
  243. AddHeader("ui-options-header-dev");
  244. AddButton(EngineKeyFunctions.ShowDebugConsole);
  245. AddButton(EngineKeyFunctions.ShowDebugMonitors);
  246. AddButton(EngineKeyFunctions.HideUI);
  247. AddButton(ContentKeyFunctions.InspectEntity);
  248. AddHeader("ui-options-header-text-cursor");
  249. AddButton(EngineKeyFunctions.TextCursorLeft);
  250. AddButton(EngineKeyFunctions.TextCursorRight);
  251. AddButton(EngineKeyFunctions.TextCursorUp);
  252. AddButton(EngineKeyFunctions.TextCursorDown);
  253. AddButton(EngineKeyFunctions.TextCursorWordLeft);
  254. AddButton(EngineKeyFunctions.TextCursorWordRight);
  255. AddButton(EngineKeyFunctions.TextCursorBegin);
  256. AddButton(EngineKeyFunctions.TextCursorEnd);
  257. AddHeader("ui-options-header-text-cursor-select");
  258. AddButton(EngineKeyFunctions.TextCursorSelect);
  259. AddButton(EngineKeyFunctions.TextCursorSelectLeft);
  260. AddButton(EngineKeyFunctions.TextCursorSelectRight);
  261. AddButton(EngineKeyFunctions.TextCursorSelectUp);
  262. AddButton(EngineKeyFunctions.TextCursorSelectDown);
  263. AddButton(EngineKeyFunctions.TextCursorSelectWordLeft);
  264. AddButton(EngineKeyFunctions.TextCursorSelectWordRight);
  265. AddButton(EngineKeyFunctions.TextCursorSelectBegin);
  266. AddButton(EngineKeyFunctions.TextCursorSelectEnd);
  267. AddHeader("ui-options-header-text-edit");
  268. AddButton(EngineKeyFunctions.TextBackspace);
  269. AddButton(EngineKeyFunctions.TextDelete);
  270. AddButton(EngineKeyFunctions.TextWordBackspace);
  271. AddButton(EngineKeyFunctions.TextWordDelete);
  272. AddButton(EngineKeyFunctions.TextNewline);
  273. AddButton(EngineKeyFunctions.TextSubmit);
  274. AddButton(EngineKeyFunctions.MultilineTextSubmit);
  275. AddButton(EngineKeyFunctions.TextSelectAll);
  276. AddButton(EngineKeyFunctions.TextCopy);
  277. AddButton(EngineKeyFunctions.TextCut);
  278. AddButton(EngineKeyFunctions.TextPaste);
  279. AddHeader("ui-options-header-text-chat");
  280. AddButton(EngineKeyFunctions.TextHistoryPrev);
  281. AddButton(EngineKeyFunctions.TextHistoryNext);
  282. AddButton(EngineKeyFunctions.TextReleaseFocus);
  283. AddButton(EngineKeyFunctions.TextScrollToBottom);
  284. AddHeader("ui-options-header-text-other");
  285. AddButton(EngineKeyFunctions.TextTabComplete);
  286. AddButton(EngineKeyFunctions.TextCompleteNext);
  287. AddButton(EngineKeyFunctions.TextCompletePrev);
  288. foreach (var control in _keyControls.Values)
  289. {
  290. UpdateKeyControl(control);
  291. }
  292. }
  293. private void UpdateKeyControl(KeyControl control)
  294. {
  295. var activeBinds = _inputManager.GetKeyBindings(control.Function);
  296. IKeyBinding? bind1 = null;
  297. IKeyBinding? bind2 = null;
  298. if (activeBinds.Count > 0)
  299. {
  300. bind1 = activeBinds[0];
  301. if (activeBinds.Count > 1)
  302. {
  303. bind2 = activeBinds[1];
  304. }
  305. }
  306. control.BindButton1.Binding = bind1;
  307. control.BindButton1.UpdateText();
  308. control.BindButton2.Binding = bind2;
  309. control.BindButton2.UpdateText();
  310. control.BindButton2.Button.Disabled = activeBinds.Count == 0;
  311. control.ResetButton.Disabled = !_inputManager.IsKeyFunctionModified(control.Function);
  312. }
  313. protected override void EnteredTree()
  314. {
  315. base.EnteredTree();
  316. _inputManager.FirstChanceOnKeyEvent += InputManagerOnFirstChanceOnKeyEvent;
  317. _inputManager.OnKeyBindingAdded += OnKeyBindAdded;
  318. _inputManager.OnKeyBindingRemoved += OnKeyBindRemoved;
  319. }
  320. protected override void ExitedTree()
  321. {
  322. base.ExitedTree();
  323. _inputManager.FirstChanceOnKeyEvent -= InputManagerOnFirstChanceOnKeyEvent;
  324. _inputManager.OnKeyBindingAdded -= OnKeyBindAdded;
  325. _inputManager.OnKeyBindingRemoved -= OnKeyBindRemoved;
  326. }
  327. private void OnKeyBindRemoved(IKeyBinding obj)
  328. {
  329. OnKeyBindModified(obj, true);
  330. }
  331. private void OnKeyBindAdded(IKeyBinding obj)
  332. {
  333. OnKeyBindModified(obj, false);
  334. }
  335. private void OnKeyBindModified(IKeyBinding bind, bool removal)
  336. {
  337. if (!_keyControls.TryGetValue(bind.Function, out var keyControl))
  338. {
  339. return;
  340. }
  341. if (removal && _currentlyRebinding?.KeyControl == keyControl)
  342. {
  343. // Don't do update if the removal was from initiating a rebind.
  344. return;
  345. }
  346. UpdateKeyControl(keyControl);
  347. if (_currentlyRebinding == keyControl.BindButton1 || _currentlyRebinding == keyControl.BindButton2)
  348. {
  349. _currentlyRebinding = null;
  350. }
  351. }
  352. private void InputManagerOnFirstChanceOnKeyEvent(KeyEventArgs keyEvent, KeyEventType type)
  353. {
  354. DebugTools.Assert(IsInsideTree);
  355. if (_currentlyRebinding == null)
  356. {
  357. return;
  358. }
  359. keyEvent.Handle();
  360. if (type != KeyEventType.Up)
  361. {
  362. return;
  363. }
  364. var key = keyEvent.Key;
  365. // Figure out modifiers based on key event.
  366. // TODO: this won't allow for combinations with keys other than the standard modifier keys,
  367. // even though the input system totally supports it.
  368. var mods = new Keyboard.Key[3];
  369. var i = 0;
  370. if (keyEvent.Control && key != Keyboard.Key.Control)
  371. {
  372. mods[i] = Keyboard.Key.Control;
  373. i += 1;
  374. }
  375. if (keyEvent.Shift && key != Keyboard.Key.Shift)
  376. {
  377. mods[i] = Keyboard.Key.Shift;
  378. i += 1;
  379. }
  380. if (keyEvent.Alt && key != Keyboard.Key.Alt)
  381. {
  382. mods[i] = Keyboard.Key.Alt;
  383. i += 1;
  384. }
  385. // The input system can only handle 3 modifier keys so if you hold all 4 of the modifier keys
  386. // then system gets the shaft, I guess.
  387. if (keyEvent.System && i != 3 && key != Keyboard.Key.LSystem && key != Keyboard.Key.RSystem)
  388. {
  389. mods[i] = Keyboard.Key.LSystem;
  390. }
  391. var function = _currentlyRebinding.KeyControl.Function;
  392. var bindType = KeyBindingType.State;
  393. if (ToggleFunctions.Contains(function))
  394. {
  395. bindType = KeyBindingType.Toggle;
  396. }
  397. var registration = new KeyBindingRegistration
  398. {
  399. Function = function,
  400. BaseKey = key,
  401. Mod1 = mods[0],
  402. Mod2 = mods[1],
  403. Mod3 = mods[2],
  404. Priority = _currentlyRebinding.Binding?.Priority ?? 0,
  405. Type = bindType,
  406. CanFocus = key == Keyboard.Key.MouseLeft
  407. || key == Keyboard.Key.MouseRight
  408. || key == Keyboard.Key.MouseMiddle,
  409. CanRepeat = false
  410. };
  411. _inputManager.RegisterBinding(registration);
  412. // OnKeyBindModified will cause _currentlyRebinding to be reset and the UI to update.
  413. _inputManager.SaveToUserData();
  414. }
  415. private void RebindButtonPressed(BindButton button)
  416. {
  417. if (_currentlyRebinding != null)
  418. {
  419. return;
  420. }
  421. _currentlyRebinding = button;
  422. _currentlyRebinding.Button.Text = Loc.GetString("ui-options-key-prompt");
  423. if (button.Binding != null)
  424. {
  425. _deferCommands.Add(() =>
  426. {
  427. // Have to do defer this or else there will be an exception in InputManager.
  428. // Because this IS fired from an input event.
  429. _inputManager.RemoveBinding(button.Binding);
  430. });
  431. }
  432. }
  433. protected override void FrameUpdate(FrameEventArgs args)
  434. {
  435. base.FrameUpdate(args);
  436. if (_deferCommands.Count == 0)
  437. {
  438. return;
  439. }
  440. foreach (var command in _deferCommands)
  441. {
  442. command();
  443. }
  444. _deferCommands.Clear();
  445. }
  446. private sealed class KeyControl : Control
  447. {
  448. public readonly BoundKeyFunction Function;
  449. public readonly BindButton BindButton1;
  450. public readonly BindButton BindButton2;
  451. public readonly Button ResetButton;
  452. public KeyControl(KeyRebindTab parent, BoundKeyFunction function)
  453. {
  454. Function = function;
  455. var name = new Label
  456. {
  457. Text = Loc.GetString(
  458. $"ui-options-function-{CaseConversion.PascalToKebab(function.FunctionName)}"),
  459. HorizontalExpand = true,
  460. HorizontalAlignment = HAlignment.Left
  461. };
  462. BindButton1 = new BindButton(parent, this, StyleBase.ButtonOpenRight);
  463. BindButton2 = new BindButton(parent, this, StyleBase.ButtonOpenLeft);
  464. ResetButton = new Button { Text = Loc.GetString("ui-options-bind-reset"), StyleClasses = { StyleBase.ButtonCaution } };
  465. var hBox = new BoxContainer
  466. {
  467. Orientation = LayoutOrientation.Horizontal,
  468. Children =
  469. {
  470. new Control {MinSize = new Vector2(5, 0)},
  471. name,
  472. BindButton1,
  473. BindButton2,
  474. new Control {MinSize = new Vector2(10, 0)},
  475. ResetButton
  476. }
  477. };
  478. ResetButton.OnPressed += args =>
  479. {
  480. parent._deferCommands.Add(() =>
  481. {
  482. parent._inputManager.ResetBindingsFor(function);
  483. parent._inputManager.SaveToUserData();
  484. });
  485. };
  486. AddChild(hBox);
  487. }
  488. }
  489. private sealed class BindButton : Control
  490. {
  491. private readonly KeyRebindTab _tab;
  492. public readonly KeyControl KeyControl;
  493. public readonly Button Button;
  494. public IKeyBinding? Binding;
  495. public BindButton(KeyRebindTab tab, KeyControl keyControl, string styleClass)
  496. {
  497. _tab = tab;
  498. KeyControl = keyControl;
  499. Button = new Button { StyleClasses = { styleClass } };
  500. UpdateText();
  501. AddChild(Button);
  502. Button.OnPressed += args =>
  503. {
  504. tab.RebindButtonPressed(this);
  505. };
  506. Button.OnKeyBindDown += ButtonOnOnKeyBindDown;
  507. MinSize = new Vector2(200, 0);
  508. }
  509. protected override void EnteredTree()
  510. {
  511. base.EnteredTree();
  512. _tab._inputManager.OnInputModeChanged += UpdateText;
  513. }
  514. protected override void ExitedTree()
  515. {
  516. base.ExitedTree();
  517. _tab._inputManager.OnInputModeChanged -= UpdateText;
  518. }
  519. private void ButtonOnOnKeyBindDown(GUIBoundKeyEventArgs args)
  520. {
  521. if (args.Function == EngineKeyFunctions.UIRightClick)
  522. {
  523. if (Binding != null)
  524. {
  525. _tab._deferCommands.Add(() =>
  526. {
  527. _tab._inputManager.RemoveBinding(Binding);
  528. _tab._inputManager.SaveToUserData();
  529. });
  530. }
  531. args.Handle();
  532. }
  533. }
  534. public void UpdateText()
  535. {
  536. Button.Text = Binding?.GetKeyString() ?? Loc.GetString("ui-options-unbound");
  537. }
  538. }
  539. }
  540. }