1
0

KeyRebindTab.xaml.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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.OpenFactionsMenu);
  192. AddButton(ContentKeyFunctions.OpenInventoryMenu);
  193. AddButton(ContentKeyFunctions.OpenAHelp);
  194. AddButton(ContentKeyFunctions.OpenActionsMenu);
  195. AddButton(ContentKeyFunctions.OpenEmotesMenu);
  196. AddButton(ContentKeyFunctions.ToggleRoundEndSummaryWindow);
  197. AddButton(ContentKeyFunctions.OpenEntitySpawnWindow);
  198. AddButton(ContentKeyFunctions.OpenSandboxWindow);
  199. AddButton(ContentKeyFunctions.OpenTileSpawnWindow);
  200. AddButton(ContentKeyFunctions.OpenDecalSpawnWindow);
  201. AddButton(ContentKeyFunctions.OpenAdminMenu);
  202. AddButton(EngineKeyFunctions.WindowCloseAll);
  203. AddButton(EngineKeyFunctions.WindowCloseRecent);
  204. AddButton(EngineKeyFunctions.EscapeMenu);
  205. AddButton(ContentKeyFunctions.EscapeContext);
  206. // Shitmed Change Start - TODO: Add hands, feet and groin targeting.
  207. AddHeader("ui-options-header-targeting");
  208. AddButton(ContentKeyFunctions.TargetHead);
  209. AddButton(ContentKeyFunctions.TargetTorso);
  210. AddButton(ContentKeyFunctions.TargetLeftArm);
  211. AddButton(ContentKeyFunctions.TargetLeftHand);
  212. AddButton(ContentKeyFunctions.TargetRightArm);
  213. AddButton(ContentKeyFunctions.TargetRightHand);
  214. AddButton(ContentKeyFunctions.TargetLeftLeg);
  215. AddButton(ContentKeyFunctions.TargetLeftFoot);
  216. AddButton(ContentKeyFunctions.TargetRightLeg);
  217. AddButton(ContentKeyFunctions.TargetRightFoot);
  218. // Shitmed Change End
  219. AddHeader("ui-options-header-misc");
  220. AddButton(ContentKeyFunctions.TakeScreenshot);
  221. AddButton(ContentKeyFunctions.TakeScreenshotNoUI);
  222. AddButton(ContentKeyFunctions.ToggleFullscreen);
  223. AddHeader("ui-options-header-hotbar");
  224. foreach (var boundKey in ContentKeyFunctions.GetHotbarBoundKeys())
  225. {
  226. AddButton(boundKey);
  227. }
  228. AddHeader("ui-options-header-shuttle");
  229. AddButton(ContentKeyFunctions.ShuttleStrafeUp);
  230. AddButton(ContentKeyFunctions.ShuttleStrafeRight);
  231. AddButton(ContentKeyFunctions.ShuttleStrafeLeft);
  232. AddButton(ContentKeyFunctions.ShuttleStrafeDown);
  233. AddButton(ContentKeyFunctions.ShuttleRotateLeft);
  234. AddButton(ContentKeyFunctions.ShuttleRotateRight);
  235. AddButton(ContentKeyFunctions.ShuttleBrake);
  236. AddHeader("ui-options-header-map-editor");
  237. AddButton(EngineKeyFunctions.EditorPlaceObject);
  238. AddButton(EngineKeyFunctions.EditorCancelPlace);
  239. AddButton(EngineKeyFunctions.EditorGridPlace);
  240. AddButton(EngineKeyFunctions.EditorLinePlace);
  241. AddButton(EngineKeyFunctions.EditorRotateObject);
  242. AddButton(ContentKeyFunctions.EditorFlipObject);
  243. AddButton(ContentKeyFunctions.EditorCopyObject);
  244. AddHeader("ui-options-header-dev");
  245. AddButton(EngineKeyFunctions.ShowDebugConsole);
  246. AddButton(EngineKeyFunctions.ShowDebugMonitors);
  247. AddButton(EngineKeyFunctions.HideUI);
  248. AddButton(ContentKeyFunctions.InspectEntity);
  249. AddHeader("ui-options-header-text-cursor");
  250. AddButton(EngineKeyFunctions.TextCursorLeft);
  251. AddButton(EngineKeyFunctions.TextCursorRight);
  252. AddButton(EngineKeyFunctions.TextCursorUp);
  253. AddButton(EngineKeyFunctions.TextCursorDown);
  254. AddButton(EngineKeyFunctions.TextCursorWordLeft);
  255. AddButton(EngineKeyFunctions.TextCursorWordRight);
  256. AddButton(EngineKeyFunctions.TextCursorBegin);
  257. AddButton(EngineKeyFunctions.TextCursorEnd);
  258. AddHeader("ui-options-header-text-cursor-select");
  259. AddButton(EngineKeyFunctions.TextCursorSelect);
  260. AddButton(EngineKeyFunctions.TextCursorSelectLeft);
  261. AddButton(EngineKeyFunctions.TextCursorSelectRight);
  262. AddButton(EngineKeyFunctions.TextCursorSelectUp);
  263. AddButton(EngineKeyFunctions.TextCursorSelectDown);
  264. AddButton(EngineKeyFunctions.TextCursorSelectWordLeft);
  265. AddButton(EngineKeyFunctions.TextCursorSelectWordRight);
  266. AddButton(EngineKeyFunctions.TextCursorSelectBegin);
  267. AddButton(EngineKeyFunctions.TextCursorSelectEnd);
  268. AddHeader("ui-options-header-text-edit");
  269. AddButton(EngineKeyFunctions.TextBackspace);
  270. AddButton(EngineKeyFunctions.TextDelete);
  271. AddButton(EngineKeyFunctions.TextWordBackspace);
  272. AddButton(EngineKeyFunctions.TextWordDelete);
  273. AddButton(EngineKeyFunctions.TextNewline);
  274. AddButton(EngineKeyFunctions.TextSubmit);
  275. AddButton(EngineKeyFunctions.MultilineTextSubmit);
  276. AddButton(EngineKeyFunctions.TextSelectAll);
  277. AddButton(EngineKeyFunctions.TextCopy);
  278. AddButton(EngineKeyFunctions.TextCut);
  279. AddButton(EngineKeyFunctions.TextPaste);
  280. AddHeader("ui-options-header-text-chat");
  281. AddButton(EngineKeyFunctions.TextHistoryPrev);
  282. AddButton(EngineKeyFunctions.TextHistoryNext);
  283. AddButton(EngineKeyFunctions.TextReleaseFocus);
  284. AddButton(EngineKeyFunctions.TextScrollToBottom);
  285. AddHeader("ui-options-header-text-other");
  286. AddButton(EngineKeyFunctions.TextTabComplete);
  287. AddButton(EngineKeyFunctions.TextCompleteNext);
  288. AddButton(EngineKeyFunctions.TextCompletePrev);
  289. foreach (var control in _keyControls.Values)
  290. {
  291. UpdateKeyControl(control);
  292. }
  293. }
  294. private void UpdateKeyControl(KeyControl control)
  295. {
  296. var activeBinds = _inputManager.GetKeyBindings(control.Function);
  297. IKeyBinding? bind1 = null;
  298. IKeyBinding? bind2 = null;
  299. if (activeBinds.Count > 0)
  300. {
  301. bind1 = activeBinds[0];
  302. if (activeBinds.Count > 1)
  303. {
  304. bind2 = activeBinds[1];
  305. }
  306. }
  307. control.BindButton1.Binding = bind1;
  308. control.BindButton1.UpdateText();
  309. control.BindButton2.Binding = bind2;
  310. control.BindButton2.UpdateText();
  311. control.BindButton2.Button.Disabled = activeBinds.Count == 0;
  312. control.ResetButton.Disabled = !_inputManager.IsKeyFunctionModified(control.Function);
  313. }
  314. protected override void EnteredTree()
  315. {
  316. base.EnteredTree();
  317. _inputManager.FirstChanceOnKeyEvent += InputManagerOnFirstChanceOnKeyEvent;
  318. _inputManager.OnKeyBindingAdded += OnKeyBindAdded;
  319. _inputManager.OnKeyBindingRemoved += OnKeyBindRemoved;
  320. }
  321. protected override void ExitedTree()
  322. {
  323. base.ExitedTree();
  324. _inputManager.FirstChanceOnKeyEvent -= InputManagerOnFirstChanceOnKeyEvent;
  325. _inputManager.OnKeyBindingAdded -= OnKeyBindAdded;
  326. _inputManager.OnKeyBindingRemoved -= OnKeyBindRemoved;
  327. }
  328. private void OnKeyBindRemoved(IKeyBinding obj)
  329. {
  330. OnKeyBindModified(obj, true);
  331. }
  332. private void OnKeyBindAdded(IKeyBinding obj)
  333. {
  334. OnKeyBindModified(obj, false);
  335. }
  336. private void OnKeyBindModified(IKeyBinding bind, bool removal)
  337. {
  338. if (!_keyControls.TryGetValue(bind.Function, out var keyControl))
  339. {
  340. return;
  341. }
  342. if (removal && _currentlyRebinding?.KeyControl == keyControl)
  343. {
  344. // Don't do update if the removal was from initiating a rebind.
  345. return;
  346. }
  347. UpdateKeyControl(keyControl);
  348. if (_currentlyRebinding == keyControl.BindButton1 || _currentlyRebinding == keyControl.BindButton2)
  349. {
  350. _currentlyRebinding = null;
  351. }
  352. }
  353. private void InputManagerOnFirstChanceOnKeyEvent(KeyEventArgs keyEvent, KeyEventType type)
  354. {
  355. DebugTools.Assert(IsInsideTree);
  356. if (_currentlyRebinding == null)
  357. {
  358. return;
  359. }
  360. keyEvent.Handle();
  361. if (type != KeyEventType.Up)
  362. {
  363. return;
  364. }
  365. var key = keyEvent.Key;
  366. // Figure out modifiers based on key event.
  367. // TODO: this won't allow for combinations with keys other than the standard modifier keys,
  368. // even though the input system totally supports it.
  369. var mods = new Keyboard.Key[3];
  370. var i = 0;
  371. if (keyEvent.Control && key != Keyboard.Key.Control)
  372. {
  373. mods[i] = Keyboard.Key.Control;
  374. i += 1;
  375. }
  376. if (keyEvent.Shift && key != Keyboard.Key.Shift)
  377. {
  378. mods[i] = Keyboard.Key.Shift;
  379. i += 1;
  380. }
  381. if (keyEvent.Alt && key != Keyboard.Key.Alt)
  382. {
  383. mods[i] = Keyboard.Key.Alt;
  384. i += 1;
  385. }
  386. // The input system can only handle 3 modifier keys so if you hold all 4 of the modifier keys
  387. // then system gets the shaft, I guess.
  388. if (keyEvent.System && i != 3 && key != Keyboard.Key.LSystem && key != Keyboard.Key.RSystem)
  389. {
  390. mods[i] = Keyboard.Key.LSystem;
  391. }
  392. var function = _currentlyRebinding.KeyControl.Function;
  393. var bindType = KeyBindingType.State;
  394. if (ToggleFunctions.Contains(function))
  395. {
  396. bindType = KeyBindingType.Toggle;
  397. }
  398. var registration = new KeyBindingRegistration
  399. {
  400. Function = function,
  401. BaseKey = key,
  402. Mod1 = mods[0],
  403. Mod2 = mods[1],
  404. Mod3 = mods[2],
  405. Priority = _currentlyRebinding.Binding?.Priority ?? 0,
  406. Type = bindType,
  407. CanFocus = key == Keyboard.Key.MouseLeft
  408. || key == Keyboard.Key.MouseRight
  409. || key == Keyboard.Key.MouseMiddle,
  410. CanRepeat = false
  411. };
  412. _inputManager.RegisterBinding(registration);
  413. // OnKeyBindModified will cause _currentlyRebinding to be reset and the UI to update.
  414. _inputManager.SaveToUserData();
  415. }
  416. private void RebindButtonPressed(BindButton button)
  417. {
  418. if (_currentlyRebinding != null)
  419. {
  420. return;
  421. }
  422. _currentlyRebinding = button;
  423. _currentlyRebinding.Button.Text = Loc.GetString("ui-options-key-prompt");
  424. if (button.Binding != null)
  425. {
  426. _deferCommands.Add(() =>
  427. {
  428. // Have to do defer this or else there will be an exception in InputManager.
  429. // Because this IS fired from an input event.
  430. _inputManager.RemoveBinding(button.Binding);
  431. });
  432. }
  433. }
  434. protected override void FrameUpdate(FrameEventArgs args)
  435. {
  436. base.FrameUpdate(args);
  437. if (_deferCommands.Count == 0)
  438. {
  439. return;
  440. }
  441. foreach (var command in _deferCommands)
  442. {
  443. command();
  444. }
  445. _deferCommands.Clear();
  446. }
  447. private sealed class KeyControl : Control
  448. {
  449. public readonly BoundKeyFunction Function;
  450. public readonly BindButton BindButton1;
  451. public readonly BindButton BindButton2;
  452. public readonly Button ResetButton;
  453. public KeyControl(KeyRebindTab parent, BoundKeyFunction function)
  454. {
  455. Function = function;
  456. var name = new Label
  457. {
  458. Text = Loc.GetString(
  459. $"ui-options-function-{CaseConversion.PascalToKebab(function.FunctionName)}"),
  460. HorizontalExpand = true,
  461. HorizontalAlignment = HAlignment.Left
  462. };
  463. BindButton1 = new BindButton(parent, this, StyleBase.ButtonOpenRight);
  464. BindButton2 = new BindButton(parent, this, StyleBase.ButtonOpenLeft);
  465. ResetButton = new Button { Text = Loc.GetString("ui-options-bind-reset"), StyleClasses = { StyleBase.ButtonCaution } };
  466. var hBox = new BoxContainer
  467. {
  468. Orientation = LayoutOrientation.Horizontal,
  469. Children =
  470. {
  471. new Control {MinSize = new Vector2(5, 0)},
  472. name,
  473. BindButton1,
  474. BindButton2,
  475. new Control {MinSize = new Vector2(10, 0)},
  476. ResetButton
  477. }
  478. };
  479. ResetButton.OnPressed += args =>
  480. {
  481. parent._deferCommands.Add(() =>
  482. {
  483. parent._inputManager.ResetBindingsFor(function);
  484. parent._inputManager.SaveToUserData();
  485. });
  486. };
  487. AddChild(hBox);
  488. }
  489. }
  490. private sealed class BindButton : Control
  491. {
  492. private readonly KeyRebindTab _tab;
  493. public readonly KeyControl KeyControl;
  494. public readonly Button Button;
  495. public IKeyBinding? Binding;
  496. public BindButton(KeyRebindTab tab, KeyControl keyControl, string styleClass)
  497. {
  498. _tab = tab;
  499. KeyControl = keyControl;
  500. Button = new Button { StyleClasses = { styleClass } };
  501. UpdateText();
  502. AddChild(Button);
  503. Button.OnPressed += args =>
  504. {
  505. tab.RebindButtonPressed(this);
  506. };
  507. Button.OnKeyBindDown += ButtonOnOnKeyBindDown;
  508. MinSize = new Vector2(200, 0);
  509. }
  510. protected override void EnteredTree()
  511. {
  512. base.EnteredTree();
  513. _tab._inputManager.OnInputModeChanged += UpdateText;
  514. }
  515. protected override void ExitedTree()
  516. {
  517. base.ExitedTree();
  518. _tab._inputManager.OnInputModeChanged -= UpdateText;
  519. }
  520. private void ButtonOnOnKeyBindDown(GUIBoundKeyEventArgs args)
  521. {
  522. if (args.Function == EngineKeyFunctions.UIRightClick)
  523. {
  524. if (Binding != null)
  525. {
  526. _tab._deferCommands.Add(() =>
  527. {
  528. _tab._inputManager.RemoveBinding(Binding);
  529. _tab._inputManager.SaveToUserData();
  530. });
  531. }
  532. args.Handle();
  533. }
  534. }
  535. public void UpdateText()
  536. {
  537. Button.Text = Binding?.GetKeyString() ?? Loc.GetString("ui-options-unbound");
  538. }
  539. }
  540. }
  541. }