From 2a6bb0678e9e9996282c0bc5c60f1c894dd7ea64 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 1 Sep 2017 22:50:13 +0100 Subject: [PATCH] Move hardcoded support power keys into yaml. --- OpenRA.Game/Settings.cs | 22 ---------- .../Logic/Ingame/SupportPowerTooltipLogic.cs | 16 ++++--- .../Widgets/SupportPowersWidget.cs | 44 ++++++++++++++++--- mods/cnc/chrome/ingame.yaml | 2 + mods/d2k/chrome/ingame-player.yaml | 2 + mods/ra/chrome/ingame-player.yaml | 2 + mods/ts/chrome/ingame-player.yaml | 2 + 7 files changed, 58 insertions(+), 32 deletions(-) diff --git a/OpenRA.Game/Settings.cs b/OpenRA.Game/Settings.cs index 262bab3edd..08a931a646 100644 --- a/OpenRA.Game/Settings.cs +++ b/OpenRA.Game/Settings.cs @@ -297,15 +297,6 @@ namespace OpenRA public Hotkey PrevMusicKey = new Hotkey(Keycode.AUDIOPREV, Modifiers.None); public Hotkey NextMusicKey = new Hotkey(Keycode.AUDIONEXT, Modifiers.None); - static readonly Func[] SupportPowerKeys = GetKeys(6, "SupportPower"); - - static Func[] GetKeys(int count, string prefix) - { - var keySettings = Expression.Parameter(typeof(KeySettings), "keySettings"); - return Exts.MakeArray(count, i => Expression.Lambda>( - Expression.Field(keySettings, "{0}{1:D2}Key".F(prefix, i + 1)), keySettings).Compile()); - } - internal Func GetHotkeyReference(string name) { var field = typeof(KeySettings).GetField(name + "Key"); @@ -314,19 +305,6 @@ namespace OpenRA return () => (Hotkey)field.GetValue(this); } - - public Hotkey GetSupportPowerHotkey(int index) - { - return GetKey(SupportPowerKeys, index); - } - - Hotkey GetKey(Func[] keys, int index) - { - if (index < 0 || index >= keys.Length) - return Hotkey.Invalid; - - return keys[index](this); - } } public class ChatSettings diff --git a/OpenRA.Mods.Common/Widgets/Logic/Ingame/SupportPowerTooltipLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Ingame/SupportPowerTooltipLogic.cs index 247f0e69e9..06cd5ceef4 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Ingame/SupportPowerTooltipLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Ingame/SupportPowerTooltipLogic.cs @@ -57,13 +57,19 @@ namespace OpenRA.Mods.Common.Widgets.Logic name = sp.Info.Description; desc = sp.Info.LongDesc.Replace("\\n", "\n"); - var hotkey = icon.Hotkey; - var hotkeyText = "({0})".F(hotkey.DisplayString()); - var hotkeyWidth = hotkey.IsValid() ? nameFont.Measure(hotkeyText).X + 2 * nameLabel.Bounds.X : 0; - hotkeyLabel.GetText = () => hotkeyText; - hotkeyLabel.Bounds.X = nameFont.Measure(name).X + 2 * nameLabel.Bounds.X; + var hotkeyWidth = 0; + var hotkey = icon.Hotkey != null ? icon.Hotkey.GetValue() : Hotkey.Invalid; hotkeyLabel.Visible = hotkey.IsValid(); + if (hotkeyLabel.Visible) + { + var hotkeyText = "({0})".F(hotkey.DisplayString()); + + hotkeyWidth = nameFont.Measure(hotkeyText).X + 2 * nameLabel.Bounds.X; + hotkeyLabel.Text = hotkeyText; + hotkeyLabel.Bounds.X = nameFont.Measure(name).X + 2 * nameLabel.Bounds.X; + } + var timeWidth = timeFont.Measure(time).X; var topWidth = nameFont.Measure(name).X + hotkeyWidth + timeWidth + timeOffset; var descSize = descFont.Measure(desc); diff --git a/OpenRA.Mods.Common/Widgets/SupportPowersWidget.cs b/OpenRA.Mods.Common/Widgets/SupportPowersWidget.cs index 9c463268cf..1d649ec1fb 100644 --- a/OpenRA.Mods.Common/Widgets/SupportPowersWidget.cs +++ b/OpenRA.Mods.Common/Widgets/SupportPowersWidget.cs @@ -14,6 +14,7 @@ using System.Collections.Generic; using System.Drawing; using System.Linq; using OpenRA.Graphics; +using OpenRA.Mods.Common.Lint; using OpenRA.Mods.Common.Traits; using OpenRA.Widgets; @@ -31,6 +32,10 @@ namespace OpenRA.Mods.Common.Widgets public readonly string TooltipContainer; public readonly string TooltipTemplate = "SUPPORT_POWER_TOOLTIP"; + // Note: LinterHotkeyNames assumes that these are disabled by default + public readonly string HotkeyPrefix = null; + public readonly int HotkeyCount = 0; + public readonly string ClockAnimation = "clock"; public readonly string ClockSequence = "idle"; public readonly string ClockPalette = "chrome"; @@ -47,12 +52,35 @@ namespace OpenRA.Mods.Common.Widgets public SupportPowerIcon TooltipIcon { get; private set; } Lazy tooltipContainer; + NamedHotkey[] hotkeys; Rectangle eventBounds; public override Rectangle EventBounds { get { return eventBounds; } } SpriteFont overlayFont; float2 holdOffset, readyOffset, timeOffset; + [CustomLintableHotkeyNames] + public static IEnumerable LinterHotkeyNames(MiniYamlNode widgetNode, Action emitError, Action emitWarning) + { + var prefix = ""; + var prefixNode = widgetNode.Value.Nodes.FirstOrDefault(n => n.Key == "HotkeyPrefix"); + if (prefixNode != null) + prefix = prefixNode.Value.Value; + + var count = 0; + var countNode = widgetNode.Value.Nodes.FirstOrDefault(n => n.Key == "HotkeyCount"); + if (countNode != null) + count = FieldLoader.GetValue("HotkeyCount", countNode.Value.Value); + + if (count == 0) + return new string[0]; + + if (string.IsNullOrEmpty(prefix)) + emitError("{0} must define HotkeyPrefix if HotkeyCount > 0.".F(widgetNode.Location)); + + return Exts.MakeArray(count, i => prefix + (i + 1).ToString("D2")); + } + [ObjectCreator.UseCtor] public SupportPowersWidget(World world, WorldRenderer worldRenderer) { @@ -65,6 +93,14 @@ namespace OpenRA.Mods.Common.Widgets clock = new Animation(world, ClockAnimation); } + public override void Initialize(WidgetArgs args) + { + base.Initialize(args); + + hotkeys = Exts.MakeArray(HotkeyCount, + i => new NamedHotkey(HotkeyPrefix + (i + 1).ToString("D2"), Game.Settings.Keys)); + } + public class SupportPowerIcon { public SupportPowerInstance Power; @@ -72,7 +108,7 @@ namespace OpenRA.Mods.Common.Widgets public Sprite Sprite; public PaletteReference Palette; public PaletteReference IconClockPalette; - public Hotkey Hotkey; + public NamedHotkey Hotkey; } public void RefreshIcons() @@ -84,8 +120,6 @@ namespace OpenRA.Mods.Common.Widgets IconCount = 0; var rb = RenderBounds; - var ks = Game.Settings.Keys; - foreach (var p in powers) { var rect = new Rectangle(rb.X, rb.Y + IconCount * (IconSize.Y + IconMargin), IconSize.X, IconSize.Y); @@ -98,7 +132,7 @@ namespace OpenRA.Mods.Common.Widgets Sprite = icon.Image, Palette = worldRenderer.Palette(p.Info.IconPalette), IconClockPalette = worldRenderer.Palette(ClockPalette), - Hotkey = ks.GetSupportPowerHotkey(IconCount) + Hotkey = IconCount < HotkeyCount ? hotkeys[IconCount] : null, }; icons.Add(rect, power); @@ -128,7 +162,7 @@ namespace OpenRA.Mods.Common.Widgets if (e.Event == KeyInputEvent.Down) { var hotkey = Hotkey.FromKeyInput(e); - var a = icons.Values.FirstOrDefault(i => i.Hotkey == hotkey); + var a = icons.Values.FirstOrDefault(i => i.Hotkey != null && i.Hotkey.GetValue() == hotkey); if (a != null) { diff --git a/mods/cnc/chrome/ingame.yaml b/mods/cnc/chrome/ingame.yaml index 0b788f8a20..a57898cadd 100644 --- a/mods/cnc/chrome/ingame.yaml +++ b/mods/cnc/chrome/ingame.yaml @@ -272,6 +272,8 @@ Container@PLAYER_WIDGETS: TooltipContainer: TOOLTIP_CONTAINER ReadyText: Ready HoldText: On Hold + HotkeyPrefix: SupportPower + HotkeyCount: 6 Background@COMMAND_BAR: Logic: CommandBarLogic X: 5 diff --git a/mods/d2k/chrome/ingame-player.yaml b/mods/d2k/chrome/ingame-player.yaml index 9635de756a..969dd2d2ac 100644 --- a/mods/d2k/chrome/ingame-player.yaml +++ b/mods/d2k/chrome/ingame-player.yaml @@ -14,6 +14,8 @@ Container@PLAYER_WIDGETS: TooltipContainer: TOOLTIP_CONTAINER ReadyText: READY HoldText: ON HOLD + HotkeyPrefix: SupportPower + HotkeyCount: 6 Image@COMMAND_BAR_BACKGROUND: X: 0 Y: WINDOW_BOTTOM - HEIGHT diff --git a/mods/ra/chrome/ingame-player.yaml b/mods/ra/chrome/ingame-player.yaml index 414e7f8919..41a781508d 100644 --- a/mods/ra/chrome/ingame-player.yaml +++ b/mods/ra/chrome/ingame-player.yaml @@ -14,6 +14,8 @@ Container@PLAYER_WIDGETS: TooltipContainer: TOOLTIP_CONTAINER ReadyText: READY HoldText: ON HOLD + HotkeyPrefix: SupportPower + HotkeyCount: 6 Container@PALETTE_FOREGROUND: Children: Image@ICON_TEMPLATE: diff --git a/mods/ts/chrome/ingame-player.yaml b/mods/ts/chrome/ingame-player.yaml index 56ae8af438..4cacd57206 100644 --- a/mods/ts/chrome/ingame-player.yaml +++ b/mods/ts/chrome/ingame-player.yaml @@ -15,6 +15,8 @@ Container@PLAYER_WIDGETS: ReadyText: READY HoldText: ON HOLD ClockPalette: iconclock + HotkeyPrefix: SupportPower + HotkeyCount: 6 Image@COMMAND_BAR_BACKGROUND: Logic: AddFactionSuffixLogic X: 5