Add hotkey filtering functionality (by name and by context)
This commit is contained in:
committed by
abcdefg30
parent
56153aac9f
commit
f0e69c3f64
@@ -40,7 +40,7 @@ namespace OpenRA
|
||||
}
|
||||
|
||||
foreach (var hd in definitions)
|
||||
hd.Value.HasDuplicates = GetFirstDuplicate(hd.Value.Name, this[hd.Value.Name].GetValue(), hd.Value) != null;
|
||||
hd.Value.HasDuplicates = GetFirstDuplicate(hd.Value, this[hd.Value.Name].GetValue()) != null;
|
||||
}
|
||||
|
||||
internal Func<Hotkey> GetHotkeyReference(string name)
|
||||
@@ -68,7 +68,7 @@ namespace OpenRA
|
||||
settings.Remove(name);
|
||||
|
||||
var hadDuplicates = definition.HasDuplicates;
|
||||
definition.HasDuplicates = GetFirstDuplicate(definition.Name, this[definition.Name].GetValue(), definition) != null;
|
||||
definition.HasDuplicates = GetFirstDuplicate(definition, this[definition.Name].GetValue()) != null;
|
||||
|
||||
if (hadDuplicates || definition.HasDuplicates)
|
||||
{
|
||||
@@ -77,16 +77,19 @@ namespace OpenRA
|
||||
if (hd.Value == definition)
|
||||
continue;
|
||||
|
||||
hd.Value.HasDuplicates = GetFirstDuplicate(hd.Value.Name, this[hd.Value.Name].GetValue(), hd.Value) != null;
|
||||
hd.Value.HasDuplicates = GetFirstDuplicate(hd.Value, this[hd.Value.Name].GetValue()) != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public HotkeyDefinition GetFirstDuplicate(string name, Hotkey value, HotkeyDefinition definition)
|
||||
public HotkeyDefinition GetFirstDuplicate(HotkeyDefinition definition, Hotkey value)
|
||||
{
|
||||
if (definition == null)
|
||||
return null;
|
||||
|
||||
foreach (var kv in keys)
|
||||
{
|
||||
if (kv.Key == name)
|
||||
if (kv.Key == definition.Name)
|
||||
continue;
|
||||
|
||||
if (kv.Value == value && definitions[kv.Key].Contexts.Overlaps(definition.Contexts))
|
||||
|
||||
@@ -31,6 +31,16 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
bool isHotkeyValid;
|
||||
bool isHotkeyDefault;
|
||||
|
||||
string currentContext = "Any";
|
||||
readonly HashSet<string> contexts = new HashSet<string>() { "Any" };
|
||||
readonly Dictionary<string, HashSet<string>> hotkeyGroups = new Dictionary<string, HashSet<string>>();
|
||||
TextFieldWidget filterInput;
|
||||
|
||||
Widget headerTemplate;
|
||||
Widget template;
|
||||
Widget emptyListMessage;
|
||||
Widget remapDialog;
|
||||
|
||||
static HotkeysSettingsLogic() { }
|
||||
|
||||
[ObjectCreator.UseCtor]
|
||||
@@ -44,7 +54,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
|
||||
void BindHotkeyPref(HotkeyDefinition hd, Widget template)
|
||||
{
|
||||
var key = template.Clone() as Widget;
|
||||
var key = template.Clone();
|
||||
key.Id = hd.Name;
|
||||
key.IsVisible = () => true;
|
||||
|
||||
@@ -86,46 +96,57 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
{
|
||||
hotkeyList = panel.Get<ScrollPanelWidget>("HOTKEY_LIST");
|
||||
hotkeyList.Layout = new GridLayout(hotkeyList);
|
||||
var headerTemplate = hotkeyList.Get("HEADER");
|
||||
var template = hotkeyList.Get("TEMPLATE");
|
||||
hotkeyList.RemoveChildren();
|
||||
headerTemplate = hotkeyList.Get("HEADER");
|
||||
template = hotkeyList.Get("TEMPLATE");
|
||||
emptyListMessage = panel.Get("HOTKEY_EMPTY_LIST");
|
||||
remapDialog = panel.Get("HOTKEY_REMAP_DIALOG");
|
||||
|
||||
if (logicArgs.TryGetValue("HotkeyGroups", out var hotkeyGroups))
|
||||
foreach (var hd in modData.Hotkeys.Definitions)
|
||||
contexts.UnionWith(hd.Contexts);
|
||||
|
||||
filterInput = panel.Get<TextFieldWidget>("FILTER_INPUT");
|
||||
filterInput.OnTextEdited = () => InitHotkeyList();
|
||||
filterInput.OnEscKey = _ =>
|
||||
{
|
||||
InitHotkeyRemapDialog(panel);
|
||||
if (string.IsNullOrEmpty(filterInput.Text))
|
||||
filterInput.YieldKeyboardFocus();
|
||||
else
|
||||
{
|
||||
filterInput.Text = "";
|
||||
filterInput.OnTextEdited();
|
||||
}
|
||||
|
||||
foreach (var hg in hotkeyGroups.Nodes)
|
||||
return true;
|
||||
};
|
||||
|
||||
var contextDropdown = panel.GetOrNull<DropDownButtonWidget>("CONTEXT_DROPDOWN");
|
||||
if (contextDropdown != null)
|
||||
{
|
||||
contextDropdown.OnMouseDown = _ => ShowContextDropdown(contextDropdown);
|
||||
var contextName = new CachedTransform<string, string>(GetContextDisplayName);
|
||||
contextDropdown.GetText = () => contextName.Update(currentContext);
|
||||
}
|
||||
|
||||
if (logicArgs.TryGetValue("HotkeyGroups", out var hotkeyGroupsYaml))
|
||||
{
|
||||
foreach (var hg in hotkeyGroupsYaml.Nodes)
|
||||
{
|
||||
var typesNode = hg.Value.Nodes.FirstOrDefault(n => n.Key == "Types");
|
||||
if (typesNode == null)
|
||||
continue;
|
||||
|
||||
var header = headerTemplate.Clone();
|
||||
header.Get<LabelWidget>("LABEL").GetText = () => hg.Key;
|
||||
hotkeyList.AddChild(header);
|
||||
|
||||
var types = FieldLoader.GetValue<string[]>("Types", typesNode.Value.Value);
|
||||
var added = new HashSet<HotkeyDefinition>();
|
||||
|
||||
foreach (var t in types)
|
||||
{
|
||||
foreach (var hd in modData.Hotkeys.Definitions.Where(k => k.Types.Contains(t)))
|
||||
{
|
||||
if (added.Add(hd))
|
||||
{
|
||||
if (selectedHotkeyDefinition == null)
|
||||
selectedHotkeyDefinition = hd;
|
||||
|
||||
BindHotkeyPref(hd, template);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (typesNode != null)
|
||||
hotkeyGroups.Add(hg.Key, FieldLoader.GetValue<HashSet<string>>("Types", typesNode.Value.Value));
|
||||
}
|
||||
|
||||
InitHotkeyRemapDialog(panel);
|
||||
InitHotkeyList();
|
||||
}
|
||||
|
||||
return () =>
|
||||
{
|
||||
hotkeyEntryWidget.Key = modData.Hotkeys[selectedHotkeyDefinition.Name].GetValue();
|
||||
hotkeyEntryWidget.Key =
|
||||
selectedHotkeyDefinition != null ?
|
||||
modData.Hotkeys[selectedHotkeyDefinition.Name].GetValue() :
|
||||
Hotkey.Invalid;
|
||||
|
||||
hotkeyEntryWidget.ForceYieldKeyboardFocus();
|
||||
|
||||
return false;
|
||||
@@ -144,21 +165,67 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
};
|
||||
}
|
||||
|
||||
void InitHotkeyList()
|
||||
{
|
||||
hotkeyList.RemoveChildren();
|
||||
selectedHotkeyDefinition = null;
|
||||
|
||||
foreach (var hg in hotkeyGroups)
|
||||
{
|
||||
var typesInGroup = hg.Value;
|
||||
var keysInGroup = modData.Hotkeys.Definitions
|
||||
.Where(hd => IsHotkeyVisibleInFilter(hd) && hd.Types.Overlaps(typesInGroup));
|
||||
|
||||
if (!keysInGroup.Any())
|
||||
continue;
|
||||
|
||||
var header = headerTemplate.Clone();
|
||||
header.Get<LabelWidget>("LABEL").GetText = () => hg.Key;
|
||||
hotkeyList.AddChild(header);
|
||||
|
||||
var added = new HashSet<HotkeyDefinition>();
|
||||
|
||||
foreach (var type in typesInGroup)
|
||||
{
|
||||
foreach (var hd in keysInGroup.Where(k => k.Types.Contains(type)))
|
||||
{
|
||||
if (added.Add(hd))
|
||||
{
|
||||
if (selectedHotkeyDefinition == null)
|
||||
selectedHotkeyDefinition = hd;
|
||||
|
||||
BindHotkeyPref(hd, template);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
emptyListMessage.Visible = selectedHotkeyDefinition == null;
|
||||
remapDialog.Visible = selectedHotkeyDefinition != null;
|
||||
|
||||
hotkeyList.ScrollToTop();
|
||||
}
|
||||
|
||||
void InitHotkeyRemapDialog(Widget panel)
|
||||
{
|
||||
var label = new CachedTransform<HotkeyDefinition, string>(hd => hd.Description + ":");
|
||||
panel.Get<LabelWidget>("HOTKEY_LABEL").GetText = () => label.Update(selectedHotkeyDefinition);
|
||||
var label = panel.Get<LabelWidget>("HOTKEY_LABEL");
|
||||
var labelText = new CachedTransform<HotkeyDefinition, string>(hd => hd?.Description + ":");
|
||||
label.IsVisible = () => selectedHotkeyDefinition != null;
|
||||
label.GetText = () => labelText.Update(selectedHotkeyDefinition);
|
||||
|
||||
var duplicateNotice = panel.Get<LabelWidget>("DUPLICATE_NOTICE");
|
||||
duplicateNotice.TextColor = ChromeMetrics.Get<Color>("NoticeErrorColor");
|
||||
duplicateNotice.IsVisible = () => !isHotkeyValid;
|
||||
var duplicateNoticeText = new CachedTransform<HotkeyDefinition, string>(hd => hd != null ? duplicateNotice.Text.F(hd.Description) : duplicateNotice.Text);
|
||||
var duplicateNoticeText = new CachedTransform<HotkeyDefinition, string>(hd =>
|
||||
hd != null ?
|
||||
duplicateNotice.Text.F(hd.Description, hd.Contexts.First(c => selectedHotkeyDefinition.Contexts.Contains(c))) :
|
||||
"");
|
||||
duplicateNotice.GetText = () => duplicateNoticeText.Update(duplicateHotkeyDefinition);
|
||||
|
||||
var originalNotice = panel.Get<LabelWidget>("ORIGINAL_NOTICE");
|
||||
originalNotice.TextColor = ChromeMetrics.Get<Color>("NoticeInfoColor");
|
||||
originalNotice.IsVisible = () => isHotkeyValid && !isHotkeyDefault;
|
||||
var originalNoticeText = new CachedTransform<HotkeyDefinition, string>(hd => originalNotice.Text.F(hd.Default.DisplayString()));
|
||||
var originalNoticeText = new CachedTransform<HotkeyDefinition, string>(hd => originalNotice.Text.F(hd?.Default.DisplayString()));
|
||||
originalNotice.GetText = () => originalNoticeText.Update(selectedHotkeyDefinition);
|
||||
|
||||
var resetButton = panel.Get<ButtonWidget>("RESET_HOTKEY_BUTTON");
|
||||
@@ -188,7 +255,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
|
||||
void ValidateHotkey()
|
||||
{
|
||||
duplicateHotkeyDefinition = modData.Hotkeys.GetFirstDuplicate(selectedHotkeyDefinition.Name, hotkeyEntryWidget.Key, selectedHotkeyDefinition);
|
||||
if (selectedHotkeyDefinition == null)
|
||||
return;
|
||||
|
||||
duplicateHotkeyDefinition = modData.Hotkeys.GetFirstDuplicate(selectedHotkeyDefinition, hotkeyEntryWidget.Key);
|
||||
isHotkeyValid = duplicateHotkeyDefinition == null;
|
||||
isHotkeyDefault = hotkeyEntryWidget.Key == selectedHotkeyDefinition.Default || (!hotkeyEntryWidget.Key.IsValid() && !selectedHotkeyDefinition.Default.IsValid());
|
||||
|
||||
@@ -231,5 +301,42 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
Game.Settings.Save();
|
||||
hotkeyEntryWidget.YieldKeyboardFocus();
|
||||
}
|
||||
|
||||
bool IsHotkeyVisibleInFilter(HotkeyDefinition hd)
|
||||
{
|
||||
var filter = filterInput.Text;
|
||||
var isFilteredByName = string.IsNullOrWhiteSpace(filter) || hd.Description.Contains(filter, StringComparison.OrdinalIgnoreCase);
|
||||
var isFilteredByContext = currentContext == "Any" || hd.Contexts.Contains(currentContext);
|
||||
|
||||
return isFilteredByName && isFilteredByContext;
|
||||
}
|
||||
|
||||
bool ShowContextDropdown(DropDownButtonWidget dropdown)
|
||||
{
|
||||
hotkeyEntryWidget.YieldKeyboardFocus();
|
||||
|
||||
var contextName = new CachedTransform<string, string>(GetContextDisplayName);
|
||||
ScrollItemWidget SetupItem(string context, ScrollItemWidget itemTemplate)
|
||||
{
|
||||
var item = ScrollItemWidget.Setup(itemTemplate,
|
||||
() => currentContext == context,
|
||||
() => { currentContext = context; InitHotkeyList(); });
|
||||
|
||||
item.Get<LabelWidget>("LABEL").GetText = () => contextName.Update(context);
|
||||
return item;
|
||||
}
|
||||
|
||||
dropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 280, contexts, SetupItem);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static string GetContextDisplayName(string context)
|
||||
{
|
||||
if (string.IsNullOrEmpty(context))
|
||||
return "Any";
|
||||
|
||||
return context;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,9 +25,31 @@ Container@HOTKEYS_PANEL:
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM
|
||||
Children:
|
||||
Label@FILTER_INPUT_LABEL:
|
||||
Width: 100
|
||||
Height: 25
|
||||
Font: Bold
|
||||
Text: Filter by name:
|
||||
TextField@FILTER_INPUT:
|
||||
X: 108
|
||||
Width: 180
|
||||
Height: 25
|
||||
Label@CONTEXT_DROPDOWN_LABEL:
|
||||
X: PARENT_RIGHT - WIDTH - 195 - 5
|
||||
Width: 100
|
||||
Height: 25
|
||||
Font: Bold
|
||||
Text: Context:
|
||||
Align: Right
|
||||
DropDownButton@CONTEXT_DROPDOWN:
|
||||
X: PARENT_RIGHT - WIDTH
|
||||
Width: 195
|
||||
Height: 25
|
||||
Font: Bold
|
||||
ScrollPanel@HOTKEY_LIST:
|
||||
Y: 35
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM - 65
|
||||
Height: PARENT_BOTTOM - 65 - 35
|
||||
TopBottomSpacing: 5
|
||||
ItemSpacing: 5
|
||||
Children:
|
||||
@@ -60,11 +82,25 @@ Container@HOTKEYS_PANEL:
|
||||
Width: 90
|
||||
Height: 25
|
||||
TooltipContainer: SETTINGS_TOOLTIP_CONTAINER
|
||||
Background@HOTKEY_DIALOG_ROOT:
|
||||
Container@HOTKEY_EMPTY_LIST:
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM
|
||||
Visible: false
|
||||
Children:
|
||||
Label@HOTKEY_EMPTY_LIST_MESSAGE:
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM
|
||||
Align: Center
|
||||
Text: No hotkeys match the filter criteria.
|
||||
Background@HOTKEY_REMAP_BGND:
|
||||
Y: PARENT_BOTTOM - HEIGHT - 1
|
||||
Width: PARENT_RIGHT
|
||||
Height: 65 + 1
|
||||
Background: panel-gray
|
||||
Children:
|
||||
Container@HOTKEY_REMAP_DIALOG:
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM
|
||||
Children:
|
||||
Label@HOTKEY_LABEL:
|
||||
X: 15
|
||||
@@ -93,7 +129,7 @@ Container@HOTKEYS_PANEL:
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM
|
||||
Font: Tiny
|
||||
Text: This is already used for "{0}"
|
||||
Text: This is already used for "{0}" in the {1} context
|
||||
Button@OVERRIDE_HOTKEY_BUTTON:
|
||||
X: PARENT_RIGHT - 3 * WIDTH - 30
|
||||
Y: 20
|
||||
|
||||
@@ -25,9 +25,31 @@ Container@HOTKEYS_PANEL:
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM
|
||||
Children:
|
||||
Label@FILTER_INPUT_LABEL:
|
||||
Width: 100
|
||||
Height: 25
|
||||
Font: Bold
|
||||
Text: Filter by name:
|
||||
TextField@FILTER_INPUT:
|
||||
X: 108
|
||||
Width: 180
|
||||
Height: 25
|
||||
Label@CONTEXT_DROPDOWN_LABEL:
|
||||
X: PARENT_RIGHT - WIDTH - 195 - 5
|
||||
Width: 100
|
||||
Height: 25
|
||||
Font: Bold
|
||||
Text: Context:
|
||||
Align: Right
|
||||
DropDownButton@CONTEXT_DROPDOWN:
|
||||
X: PARENT_RIGHT - WIDTH
|
||||
Width: 195
|
||||
Height: 25
|
||||
Font: Bold
|
||||
ScrollPanel@HOTKEY_LIST:
|
||||
Y: 35
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM - 65
|
||||
Height: PARENT_BOTTOM - 65 - 35
|
||||
TopBottomSpacing: 5
|
||||
ItemSpacing: 5
|
||||
Children:
|
||||
@@ -60,11 +82,25 @@ Container@HOTKEYS_PANEL:
|
||||
Width: 120
|
||||
Height: 25
|
||||
TooltipContainer: SETTINGS_TOOLTIP_CONTAINER
|
||||
Background@HOTKEY_DIALOG_ROOT:
|
||||
Container@HOTKEY_EMPTY_LIST:
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM
|
||||
Visible: false
|
||||
Children:
|
||||
Label@HOTKEY_EMPTY_LIST_MESSAGE:
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM
|
||||
Align: Center
|
||||
Text: No hotkeys match the filter criteria.
|
||||
Background@HOTKEY_REMAP_BGND:
|
||||
Y: PARENT_BOTTOM - HEIGHT
|
||||
Width: PARENT_RIGHT
|
||||
Height: 65
|
||||
Background: dialog3
|
||||
Children:
|
||||
Container@HOTKEY_REMAP_DIALOG:
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM
|
||||
Children:
|
||||
Label@HOTKEY_LABEL:
|
||||
X: 15
|
||||
@@ -93,7 +129,7 @@ Container@HOTKEYS_PANEL:
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM
|
||||
Font: Tiny
|
||||
Text: This is already used for "{0}"
|
||||
Text: This is already used for "{0}" in the {1} context
|
||||
Button@OVERRIDE_HOTKEY_BUTTON:
|
||||
X: PARENT_RIGHT - 3 * WIDTH - 30
|
||||
Y: 20
|
||||
|
||||
@@ -27,9 +27,31 @@ Container@HOTKEYS_PANEL:
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM
|
||||
Children:
|
||||
Label@FILTER_INPUT_LABEL:
|
||||
Width: 100
|
||||
Height: 25
|
||||
Font: Bold
|
||||
Text: Filter by name:
|
||||
TextField@FILTER_INPUT:
|
||||
X: 108
|
||||
Width: 180
|
||||
Height: 25
|
||||
Label@CONTEXT_DROPDOWN_LABEL:
|
||||
X: PARENT_RIGHT - WIDTH - 195 - 5
|
||||
Width: 100
|
||||
Height: 25
|
||||
Font: Bold
|
||||
Text: Context:
|
||||
Align: Right
|
||||
DropDownButton@CONTEXT_DROPDOWN:
|
||||
X: PARENT_RIGHT - WIDTH
|
||||
Width: 195
|
||||
Height: 25
|
||||
Font: Bold
|
||||
ScrollPanel@HOTKEY_LIST:
|
||||
Y: 35
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM - 65
|
||||
Height: PARENT_BOTTOM - 65 - 35
|
||||
TopBottomSpacing: 5
|
||||
ItemSpacing: 5
|
||||
Children:
|
||||
@@ -62,11 +84,25 @@ Container@HOTKEYS_PANEL:
|
||||
Width: 120
|
||||
Height: 25
|
||||
TooltipContainer: SETTINGS_TOOLTIP_CONTAINER
|
||||
Background@HOTKEY_DIALOG_ROOT:
|
||||
Container@HOTKEY_EMPTY_LIST:
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM
|
||||
Visible: false
|
||||
Children:
|
||||
Label@HOTKEY_EMPTY_LIST_MESSAGE:
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM
|
||||
Align: Center
|
||||
Text: No hotkeys match the filter criteria.
|
||||
Background@HOTKEY_REMAP_BGND:
|
||||
Y: PARENT_BOTTOM - HEIGHT
|
||||
Width: PARENT_RIGHT
|
||||
Height: 65
|
||||
Background: dialog3
|
||||
Children:
|
||||
Container@HOTKEY_REMAP_DIALOG:
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM
|
||||
Children:
|
||||
Label@HOTKEY_LABEL:
|
||||
X: 15
|
||||
@@ -95,7 +131,7 @@ Container@HOTKEYS_PANEL:
|
||||
Width: PARENT_RIGHT
|
||||
Height: PARENT_BOTTOM
|
||||
Font: Tiny
|
||||
Text: This is already used for "{0}"
|
||||
Text: This is already used for "{0}" in the {1} context
|
||||
Button@OVERRIDE_HOTKEY_BUTTON:
|
||||
X: PARENT_RIGHT - 3 * WIDTH - 30
|
||||
Y: 20
|
||||
|
||||
Reference in New Issue
Block a user