From 67f8452178fed1e185cb52a0095ad7cd8c4abac2 Mon Sep 17 00:00:00 2001 From: Ivaylo Draganov Date: Sat, 6 Jun 2020 16:35:26 +0300 Subject: [PATCH] Add button to override duplicate hotkey bindings --- .../Widgets/Logic/SettingsLogic.cs | 34 ++++++++++++++++--- mods/cnc/chrome/settings.yaml | 6 ++++ mods/common/chrome/settings.yaml | 7 ++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs index 2e104069f2..01ef10bd20 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs @@ -44,9 +44,12 @@ namespace OpenRA.Mods.Common.Widgets.Logic SoundDevice soundDevice; PanelType settingsPanel = PanelType.Display; + ScrollPanelWidget hotkeyList; ButtonWidget selectedHotkeyButton; HotkeyEntryWidget hotkeyEntryWidget; HotkeyDefinition duplicateHotkeyDefinition, selectedHotkeyDefinition; + int validHotkeyEntryWidth; + int invalidHotkeyEntryWidth; bool isHotkeyValid; bool isHotkeyDefault; @@ -172,7 +175,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic ss.OnChange += x => field.SetValue(group, (int)x); } - void BindHotkeyPref(HotkeyDefinition hd, Widget template, Widget parent) + void BindHotkeyPref(HotkeyDefinition hd, Widget template) { var key = template.Clone() as Widget; key.Id = hd.Name; @@ -209,7 +212,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic hotkeyEntryWidget.TakeKeyboardFocus(); }; - parent.AddChild(key); + hotkeyList.AddChild(key); } void RegisterSettingsPanel(PanelType type, Func init, Func reset, string panelID, string buttonID) @@ -578,7 +581,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic Action InitHotkeysPanel(Widget panel) { var hotkeyDialogRoot = panel.Get("HOTKEY_DIALOG_ROOT"); - var hotkeyList = panel.Get("HOTKEY_LIST"); + hotkeyList = panel.Get("HOTKEY_LIST"); hotkeyList.Layout = new GridLayout(hotkeyList); var hotkeyHeader = hotkeyList.Get("HEADER"); var templates = hotkeyList.Get("TEMPLATES"); @@ -616,7 +619,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic if (selectedHotkeyDefinition == null) selectedHotkeyDefinition = hd; - BindHotkeyPref(hd, template, hotkeyList); + BindHotkeyPref(hd, template); } } } @@ -1057,6 +1060,11 @@ namespace OpenRA.Mods.Common.Widgets.Logic clearButton.IsDisabled = () => !hotkeyEntryWidget.Key.IsValid(); clearButton.OnClick = ClearHotkey; + var overrideButton = panel.Get("OVERRIDE_HOTKEY_BUTTON"); + overrideButton.IsDisabled = () => isHotkeyValid; + overrideButton.IsVisible = () => !isHotkeyValid; + overrideButton.OnClick = OverrideHotkey; + hotkeyEntryWidget = panel.Get("HOTKEY_ENTRY"); hotkeyEntryWidget.IsValid = () => isHotkeyValid; hotkeyEntryWidget.OnLoseFocus = ValidateHotkey; @@ -1064,6 +1072,9 @@ namespace OpenRA.Mods.Common.Widgets.Logic { hotkeyEntryWidget.Key = modData.Hotkeys[selectedHotkeyDefinition.Name].GetValue(); }; + + validHotkeyEntryWidth = hotkeyEntryWidget.Bounds.Width; + invalidHotkeyEntryWidth = validHotkeyEntryWidth - (clearButton.Bounds.X - overrideButton.Bounds.X); } void ValidateHotkey() @@ -1073,9 +1084,15 @@ namespace OpenRA.Mods.Common.Widgets.Logic isHotkeyDefault = hotkeyEntryWidget.Key == selectedHotkeyDefinition.Default || (!hotkeyEntryWidget.Key.IsValid() && !selectedHotkeyDefinition.Default.IsValid()); if (isHotkeyValid) + { + hotkeyEntryWidget.Bounds.Width = validHotkeyEntryWidth; SaveHotkey(); + } else + { + hotkeyEntryWidget.Bounds.Width = invalidHotkeyEntryWidth; hotkeyEntryWidget.TakeKeyboardFocus(); + } } void SaveHotkey() @@ -1096,5 +1113,14 @@ namespace OpenRA.Mods.Common.Widgets.Logic hotkeyEntryWidget.Key = Hotkey.Invalid; hotkeyEntryWidget.YieldKeyboardFocus(); } + + void OverrideHotkey() + { + var duplicateHotkeyButton = hotkeyList.Get(duplicateHotkeyDefinition.Name).Get("HOTKEY"); + WidgetUtils.TruncateButtonToTooltip(duplicateHotkeyButton, Hotkey.Invalid.DisplayString()); + modData.Hotkeys.Set(duplicateHotkeyDefinition.Name, Hotkey.Invalid); + Game.Settings.Save(); + hotkeyEntryWidget.YieldKeyboardFocus(); + } } } diff --git a/mods/cnc/chrome/settings.yaml b/mods/cnc/chrome/settings.yaml index 9d89f9d42b..d2bfa4fd87 100644 --- a/mods/cnc/chrome/settings.yaml +++ b/mods/cnc/chrome/settings.yaml @@ -706,6 +706,12 @@ Container@SETTINGS_PANEL: Font: Tiny Align: Left Text: This is already used for "{0}" + Button@OVERRIDE_HOTKEY_BUTTON: + X: PARENT_RIGHT - 50 - 15 - WIDTH - 20 + Y: 20 + Width: 70 + Height: 25 + Text: Override Button@CLEAR_HOTKEY_BUTTON: X: PARENT_RIGHT - 25 - 15 - WIDTH - 10 Y: 20 diff --git a/mods/common/chrome/settings.yaml b/mods/common/chrome/settings.yaml index 46f87be14f..62da738f24 100644 --- a/mods/common/chrome/settings.yaml +++ b/mods/common/chrome/settings.yaml @@ -719,6 +719,13 @@ Background@SETTINGS_PANEL: Font: Tiny Align: Left Text: This is already used for "{0}" + Button@OVERRIDE_HOTKEY_BUTTON: + X: PARENT_RIGHT - 3 * WIDTH - 30 + Y: 20 + Width: 70 + Height: 25 + Text: Override + Font: Bold Button@CLEAR_HOTKEY_BUTTON: X: PARENT_RIGHT - 2 * WIDTH - 30 Y: 20