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

@@ -297,15 +297,6 @@ namespace OpenRA
public Hotkey PrevMusicKey = new Hotkey(Keycode.AUDIOPREV, Modifiers.None); public Hotkey PrevMusicKey = new Hotkey(Keycode.AUDIOPREV, Modifiers.None);
public Hotkey NextMusicKey = new Hotkey(Keycode.AUDIONEXT, Modifiers.None); public Hotkey NextMusicKey = new Hotkey(Keycode.AUDIONEXT, Modifiers.None);
static readonly Func<KeySettings, Hotkey>[] SupportPowerKeys = GetKeys(6, "SupportPower");
static Func<KeySettings, Hotkey>[] GetKeys(int count, string prefix)
{
var keySettings = Expression.Parameter(typeof(KeySettings), "keySettings");
return Exts.MakeArray(count, i => Expression.Lambda<Func<KeySettings, Hotkey>>(
Expression.Field(keySettings, "{0}{1:D2}Key".F(prefix, i + 1)), keySettings).Compile());
}
internal Func<Hotkey> GetHotkeyReference(string name) internal Func<Hotkey> GetHotkeyReference(string name)
{ {
var field = typeof(KeySettings).GetField(name + "Key"); var field = typeof(KeySettings).GetField(name + "Key");
@@ -314,19 +305,6 @@ namespace OpenRA
return () => (Hotkey)field.GetValue(this); return () => (Hotkey)field.GetValue(this);
} }
public Hotkey GetSupportPowerHotkey(int index)
{
return GetKey(SupportPowerKeys, index);
}
Hotkey GetKey(Func<KeySettings, Hotkey>[] keys, int index)
{
if (index < 0 || index >= keys.Length)
return Hotkey.Invalid;
return keys[index](this);
}
} }
public class ChatSettings public class ChatSettings

View File

@@ -57,13 +57,19 @@ namespace OpenRA.Mods.Common.Widgets.Logic
name = sp.Info.Description; name = sp.Info.Description;
desc = sp.Info.LongDesc.Replace("\\n", "\n"); desc = sp.Info.LongDesc.Replace("\\n", "\n");
var hotkey = icon.Hotkey; var hotkeyWidth = 0;
var hotkeyText = "({0})".F(hotkey.DisplayString()); var hotkey = icon.Hotkey != null ? icon.Hotkey.GetValue() : Hotkey.Invalid;
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;
hotkeyLabel.Visible = hotkey.IsValid(); 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 timeWidth = timeFont.Measure(time).X;
var topWidth = nameFont.Measure(name).X + hotkeyWidth + timeWidth + timeOffset; var topWidth = nameFont.Measure(name).X + hotkeyWidth + timeWidth + timeOffset;
var descSize = descFont.Measure(desc); var descSize = descFont.Measure(desc);

View File

@@ -14,6 +14,7 @@ using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.Common.Lint;
using OpenRA.Mods.Common.Traits; using OpenRA.Mods.Common.Traits;
using OpenRA.Widgets; using OpenRA.Widgets;
@@ -31,6 +32,10 @@ namespace OpenRA.Mods.Common.Widgets
public readonly string TooltipContainer; public readonly string TooltipContainer;
public readonly string TooltipTemplate = "SUPPORT_POWER_TOOLTIP"; 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 ClockAnimation = "clock";
public readonly string ClockSequence = "idle"; public readonly string ClockSequence = "idle";
public readonly string ClockPalette = "chrome"; public readonly string ClockPalette = "chrome";
@@ -47,12 +52,35 @@ namespace OpenRA.Mods.Common.Widgets
public SupportPowerIcon TooltipIcon { get; private set; } public SupportPowerIcon TooltipIcon { get; private set; }
Lazy<TooltipContainerWidget> tooltipContainer; Lazy<TooltipContainerWidget> tooltipContainer;
NamedHotkey[] hotkeys;
Rectangle eventBounds; Rectangle eventBounds;
public override Rectangle EventBounds { get { return eventBounds; } } public override Rectangle EventBounds { get { return eventBounds; } }
SpriteFont overlayFont; SpriteFont overlayFont;
float2 holdOffset, readyOffset, timeOffset; 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] [ObjectCreator.UseCtor]
public SupportPowersWidget(World world, WorldRenderer worldRenderer) public SupportPowersWidget(World world, WorldRenderer worldRenderer)
{ {
@@ -65,6 +93,14 @@ namespace OpenRA.Mods.Common.Widgets
clock = new Animation(world, ClockAnimation); 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 class SupportPowerIcon
{ {
public SupportPowerInstance Power; public SupportPowerInstance Power;
@@ -72,7 +108,7 @@ namespace OpenRA.Mods.Common.Widgets
public Sprite Sprite; public Sprite Sprite;
public PaletteReference Palette; public PaletteReference Palette;
public PaletteReference IconClockPalette; public PaletteReference IconClockPalette;
public Hotkey Hotkey; public NamedHotkey Hotkey;
} }
public void RefreshIcons() public void RefreshIcons()
@@ -84,8 +120,6 @@ namespace OpenRA.Mods.Common.Widgets
IconCount = 0; IconCount = 0;
var rb = RenderBounds; var rb = RenderBounds;
var ks = Game.Settings.Keys;
foreach (var p in powers) foreach (var p in powers)
{ {
var rect = new Rectangle(rb.X, rb.Y + IconCount * (IconSize.Y + IconMargin), IconSize.X, IconSize.Y); 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, Sprite = icon.Image,
Palette = worldRenderer.Palette(p.Info.IconPalette), Palette = worldRenderer.Palette(p.Info.IconPalette),
IconClockPalette = worldRenderer.Palette(ClockPalette), IconClockPalette = worldRenderer.Palette(ClockPalette),
Hotkey = ks.GetSupportPowerHotkey(IconCount) Hotkey = IconCount < HotkeyCount ? hotkeys[IconCount] : null,
}; };
icons.Add(rect, power); icons.Add(rect, power);
@@ -128,7 +162,7 @@ namespace OpenRA.Mods.Common.Widgets
if (e.Event == KeyInputEvent.Down) if (e.Event == KeyInputEvent.Down)
{ {
var hotkey = Hotkey.FromKeyInput(e); 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) if (a != null)
{ {

View File

@@ -272,6 +272,8 @@ Container@PLAYER_WIDGETS:
TooltipContainer: TOOLTIP_CONTAINER TooltipContainer: TOOLTIP_CONTAINER
ReadyText: Ready ReadyText: Ready
HoldText: On Hold HoldText: On Hold
HotkeyPrefix: SupportPower
HotkeyCount: 6
Background@COMMAND_BAR: Background@COMMAND_BAR:
Logic: CommandBarLogic Logic: CommandBarLogic
X: 5 X: 5

View File

@@ -14,6 +14,8 @@ Container@PLAYER_WIDGETS:
TooltipContainer: TOOLTIP_CONTAINER TooltipContainer: TOOLTIP_CONTAINER
ReadyText: READY ReadyText: READY
HoldText: ON HOLD HoldText: ON HOLD
HotkeyPrefix: SupportPower
HotkeyCount: 6
Image@COMMAND_BAR_BACKGROUND: Image@COMMAND_BAR_BACKGROUND:
X: 0 X: 0
Y: WINDOW_BOTTOM - HEIGHT Y: WINDOW_BOTTOM - HEIGHT

View File

@@ -14,6 +14,8 @@ Container@PLAYER_WIDGETS:
TooltipContainer: TOOLTIP_CONTAINER TooltipContainer: TOOLTIP_CONTAINER
ReadyText: READY ReadyText: READY
HoldText: ON HOLD HoldText: ON HOLD
HotkeyPrefix: SupportPower
HotkeyCount: 6
Container@PALETTE_FOREGROUND: Container@PALETTE_FOREGROUND:
Children: Children:
Image@ICON_TEMPLATE: Image@ICON_TEMPLATE:

View File

@@ -15,6 +15,8 @@ Container@PLAYER_WIDGETS:
ReadyText: READY ReadyText: READY
HoldText: ON HOLD HoldText: ON HOLD
ClockPalette: iconclock ClockPalette: iconclock
HotkeyPrefix: SupportPower
HotkeyCount: 6
Image@COMMAND_BAR_BACKGROUND: Image@COMMAND_BAR_BACKGROUND:
Logic: AddFactionSuffixLogic Logic: AddFactionSuffixLogic
X: 5 X: 5