Move hardcoded support power keys into yaml.

This commit is contained in:
Paul Chote
2017-09-01 22:50:13 +01:00
committed by reaperrr
parent 4f00d62237
commit 2a6bb0678e
7 changed files with 58 additions and 32 deletions

View File

@@ -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<TooltipContainerWidget> tooltipContainer;
NamedHotkey[] hotkeys;
Rectangle eventBounds;
public override Rectangle EventBounds { get { return eventBounds; } }
SpriteFont overlayFont;
float2 holdOffset, readyOffset, timeOffset;
[CustomLintableHotkeyNames]
public static IEnumerable<string> LinterHotkeyNames(MiniYamlNode widgetNode, Action<string> emitError, Action<string> 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<int>("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)
{