KeyRebindTab.xaml.cs 24 KB

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