Add button to override duplicate hotkey bindings

This commit is contained in:
Ivaylo Draganov
2020-06-06 16:35:26 +03:00
committed by teinarss
parent 47f6e407d9
commit 67f8452178
3 changed files with 43 additions and 4 deletions

View File

@@ -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<Widget, Action> init, Func<Widget, Action> 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<ScrollPanelWidget>("HOTKEY_LIST");
hotkeyList = panel.Get<ScrollPanelWidget>("HOTKEY_LIST");
hotkeyList.Layout = new GridLayout(hotkeyList);
var hotkeyHeader = hotkeyList.Get<ScrollItemWidget>("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<ButtonWidget>("OVERRIDE_HOTKEY_BUTTON");
overrideButton.IsDisabled = () => isHotkeyValid;
overrideButton.IsVisible = () => !isHotkeyValid;
overrideButton.OnClick = OverrideHotkey;
hotkeyEntryWidget = panel.Get<HotkeyEntryWidget>("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<ContainerWidget>(duplicateHotkeyDefinition.Name).Get<ButtonWidget>("HOTKEY");
WidgetUtils.TruncateButtonToTooltip(duplicateHotkeyButton, Hotkey.Invalid.DisplayString());
modData.Hotkeys.Set(duplicateHotkeyDefinition.Name, Hotkey.Invalid);
Game.Settings.Save();
hotkeyEntryWidget.YieldKeyboardFocus();
}
}
}

View File

@@ -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

View File

@@ -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