Skip to content

Commit

Permalink
Add support for readonly hotkeys and expose chat input hotkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
dragunoff committed May 11, 2022
1 parent 581b5cf commit f241d9b
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 13 deletions.
5 changes: 5 additions & 0 deletions OpenRA.Game/HotkeyDefinition.cs
Expand Up @@ -21,6 +21,7 @@ public sealed class HotkeyDefinition
public readonly string Description = "";
public readonly HashSet<string> Types = new HashSet<string>();
public readonly HashSet<string> Contexts = new HashSet<string>();
public readonly bool Readonly = false;
public bool HasDuplicates { get; internal set; }

public HotkeyDefinition(string name, MiniYaml node)
Expand Down Expand Up @@ -49,6 +50,10 @@ public HotkeyDefinition(string name, MiniYaml node)
if (platformOverride != null)
Default = FieldLoader.GetValue<Hotkey>("value", platformOverride.Value.Value);
}

var readonlyNode = node.Nodes.FirstOrDefault(n => n.Key == "Readonly");
if (readonlyNode != null)
Readonly = FieldLoader.GetValue<bool>("Readonly", readonlyNode.Value.Value);
}
}
}
5 changes: 4 additions & 1 deletion OpenRA.Game/HotkeyManager.cs
Expand Up @@ -35,7 +35,7 @@ public HotkeyManager(IReadOnlyFileSystem fileSystem, Dictionary<string, Hotkey>

foreach (var kv in settings)
{
if (definitions.ContainsKey(kv.Key))
if (definitions.ContainsKey(kv.Key) && !definitions[kv.Key].Readonly)
keys[kv.Key] = kv.Value;
}

Expand All @@ -61,6 +61,9 @@ public void Set(string name, Hotkey value)
if (!definitions.TryGetValue(name, out var definition))
return;

if (definition.Readonly)
return;

keys[name] = value;
if (value != definition.Default)
settings[name] = value;
Expand Down
24 changes: 18 additions & 6 deletions OpenRA.Mods.Common/Widgets/Logic/Settings/HotkeysSettingsLogic.cs
Expand Up @@ -86,7 +86,11 @@ void BindHotkeyPref(HotkeyDefinition hd, Widget template)
selectedHotkeyButton = remapButton;
hotkeyEntryWidget.Key = modData.Hotkeys[hd.Name].GetValue();
ValidateHotkey();
hotkeyEntryWidget.TakeKeyboardFocus();
if (hd.Readonly)
hotkeyEntryWidget.YieldKeyboardFocus();
else
hotkeyEntryWidget.TakeKeyboardFocus();
};

hotkeyList.AddChild(key);
Expand Down Expand Up @@ -228,17 +232,21 @@ void InitHotkeyRemapDialog(Widget panel)
var originalNoticeText = new CachedTransform<HotkeyDefinition, string>(hd => originalNotice.Text.F(hd?.Default.DisplayString()));
originalNotice.GetText = () => originalNoticeText.Update(selectedHotkeyDefinition);

var readonlyNotice = panel.Get<LabelWidget>("READONLY_NOTICE");
readonlyNotice.TextColor = ChromeMetrics.Get<Color>("NoticeInfoColor");
readonlyNotice.IsVisible = () => selectedHotkeyDefinition.Readonly;

var resetButton = panel.Get<ButtonWidget>("RESET_HOTKEY_BUTTON");
resetButton.IsDisabled = () => isHotkeyDefault;
resetButton.IsDisabled = () => isHotkeyDefault || selectedHotkeyDefinition.Readonly;
resetButton.OnClick = ResetHotkey;

var clearButton = panel.Get<ButtonWidget>("CLEAR_HOTKEY_BUTTON");
clearButton.IsDisabled = () => !hotkeyEntryWidget.Key.IsValid();
clearButton.IsDisabled = () => !hotkeyEntryWidget.Key.IsValid() || selectedHotkeyDefinition.Readonly;
clearButton.OnClick = ClearHotkey;

var overrideButton = panel.Get<ButtonWidget>("OVERRIDE_HOTKEY_BUTTON");
overrideButton.IsDisabled = () => isHotkeyValid;
overrideButton.IsVisible = () => !isHotkeyValid;
overrideButton.IsVisible = () => !isHotkeyValid && !duplicateHotkeyDefinition.Readonly;
overrideButton.OnClick = OverrideHotkey;

hotkeyEntryWidget = panel.Get<HotkeyEntryWidget>("HOTKEY_ENTRY");
Expand All @@ -248,6 +256,7 @@ void InitHotkeyRemapDialog(Widget panel)
{
hotkeyEntryWidget.Key = modData.Hotkeys[selectedHotkeyDefinition.Name].GetValue();
};
hotkeyEntryWidget.IsDisabled = () => selectedHotkeyDefinition.Readonly;

validHotkeyEntryWidth = hotkeyEntryWidget.Bounds.Width;
invalidHotkeyEntryWidth = validHotkeyEntryWidth - (clearButton.Bounds.X - overrideButton.Bounds.X);
Expand All @@ -259,7 +268,7 @@ void ValidateHotkey()
return;

duplicateHotkeyDefinition = modData.Hotkeys.GetFirstDuplicate(selectedHotkeyDefinition, hotkeyEntryWidget.Key);
isHotkeyValid = duplicateHotkeyDefinition == null;
isHotkeyValid = duplicateHotkeyDefinition == null || selectedHotkeyDefinition.Readonly;
isHotkeyDefault = hotkeyEntryWidget.Key == selectedHotkeyDefinition.Default || (!hotkeyEntryWidget.Key.IsValid() && !selectedHotkeyDefinition.Default.IsValid());

if (isHotkeyValid)
Expand All @@ -269,13 +278,16 @@ void ValidateHotkey()
}
else
{
hotkeyEntryWidget.Bounds.Width = invalidHotkeyEntryWidth;
hotkeyEntryWidget.Bounds.Width = duplicateHotkeyDefinition.Readonly ? validHotkeyEntryWidth : invalidHotkeyEntryWidth;
hotkeyEntryWidget.TakeKeyboardFocus();
}
}

void SaveHotkey()
{
if (selectedHotkeyDefinition.Readonly)
return;

WidgetUtils.TruncateButtonToTooltip(selectedHotkeyButton, hotkeyEntryWidget.Key.DisplayString());
modData.Hotkeys.Set(selectedHotkeyDefinition.Name, hotkeyEntryWidget.Key);
Game.Settings.Save();
Expand Down
2 changes: 1 addition & 1 deletion mods/cnc/chrome/ingame-chat.yaml
Expand Up @@ -32,7 +32,7 @@ Container@CHAT_PANEL:
Height: 25
Text: Team
Font: Bold
Key: Tab SHIFT
Key: ToggleChatMode
TooltipText: Toggle chat mode
TooltipTemplate: BUTTON_TOOLTIP_FACTIONSUFFIX
TooltipContainer: TOOLTIP_CONTAINER
Expand Down
2 changes: 1 addition & 1 deletion mods/cnc/chrome/ingame-infochat.yaml
Expand Up @@ -19,7 +19,7 @@ Container@CHAT_CONTAINER:
Height: 25
Text: Team
Font: Bold
Key: Tab SHIFT
Key: ToggleChatMode
TooltipText: Toggle chat mode
TooltipContainer: TOOLTIP_CONTAINER
TextField@CHAT_TEXTFIELD:
Expand Down
2 changes: 1 addition & 1 deletion mods/cnc/chrome/lobby.yaml
Expand Up @@ -110,7 +110,7 @@ Container@SERVER_LOBBY:
Height: 25
Text: Team
Font: Bold
Key: Tab SHIFT
Key: ToggleChatMode
TooltipText: Toggle chat mode
TooltipContainer: TOOLTIP_CONTAINER
TextField@CHAT_TEXTFIELD:
Expand Down
5 changes: 5 additions & 0 deletions mods/cnc/chrome/settings-hotkeys.yaml
Expand Up @@ -131,6 +131,11 @@ Container@HOTKEYS_PANEL:
Height: PARENT_BOTTOM
Font: Tiny
Text: This is already used for "{0}" in the {1} context
Label@READONLY_NOTICE:
Width: PARENT_RIGHT
Height: PARENT_BOTTOM
Font: Tiny
Text: This hotkey cannot be modified
Button@OVERRIDE_HOTKEY_BUTTON:
X: PARENT_RIGHT - 3 * WIDTH - 30
Y: 20
Expand Down
2 changes: 1 addition & 1 deletion mods/common/chrome/ingame-chat.yaml
Expand Up @@ -31,7 +31,7 @@ Container@CHAT_PANEL:
Height: 25
Text: Team
Font: Bold
Key: Tab SHIFT
Key: ToggleChatMode
TooltipText: Toggle chat mode
TooltipContainer: TOOLTIP_CONTAINER
TextField@CHAT_TEXTFIELD:
Expand Down
2 changes: 1 addition & 1 deletion mods/common/chrome/ingame-infochat.yaml
Expand Up @@ -19,7 +19,7 @@ Container@CHAT_CONTAINER:
Height: 25
Text: Team
Font: Bold
Key: Tab SHIFT
Key: ToggleChatMode
TooltipText: Toggle chat mode
TooltipContainer: TOOLTIP_CONTAINER
TextField@CHAT_TEXTFIELD:
Expand Down
2 changes: 1 addition & 1 deletion mods/common/chrome/lobby.yaml
Expand Up @@ -114,7 +114,7 @@ Background@SERVER_LOBBY:
Height: 25
Text: Team
Font: Bold
Key: Tab SHIFT
Key: ToggleChatMode
TooltipText: Toggle chat mode
TooltipContainer: TOOLTIP_CONTAINER
TextField@CHAT_TEXTFIELD:
Expand Down
5 changes: 5 additions & 0 deletions mods/common/chrome/settings-hotkeys.yaml
Expand Up @@ -131,6 +131,11 @@ Container@HOTKEYS_PANEL:
Height: PARENT_BOTTOM
Font: Tiny
Text: This is already used for "{0}" in the {1} context
Label@READONLY_NOTICE:
Width: PARENT_RIGHT
Height: PARENT_BOTTOM
Font: Tiny
Text: This hotkey cannot be modified
Button@OVERRIDE_HOTKEY_BUTTON:
X: PARENT_RIGHT - 3 * WIDTH - 30
Y: 20
Expand Down
12 changes: 12 additions & 0 deletions mods/common/hotkeys/chat.yaml
Expand Up @@ -7,3 +7,15 @@ OpenGeneralChat: Return Shift
Description: Open General Chat
Types: Chat
Contexts: Player, Spectator

ToggleChatMode: Tab Shift
Description: Toggle Chat Mode
Types: Chat
Contexts: Chat Input, Menu
Readonly: True

Autocomplete: Tab
Description: Autocomplete
Types: Chat
Contexts: Chat Input
Readonly: True
5 changes: 5 additions & 0 deletions mods/ts/chrome/settings-hotkeys.yaml
Expand Up @@ -133,6 +133,11 @@ Container@HOTKEYS_PANEL:
Height: PARENT_BOTTOM
Font: Tiny
Text: This is already used for "{0}" in the {1} context
Label@READONLY_NOTICE:
Width: PARENT_RIGHT
Height: PARENT_BOTTOM
Font: Tiny
Text: This hotkey cannot be modified
Button@OVERRIDE_HOTKEY_BUTTON:
X: PARENT_RIGHT - 3 * WIDTH - 30
Y: 20
Expand Down

0 comments on commit f241d9b

Please sign in to comment.