From f7f01cc08e5ac1d39045b66af9e03a70d970e8b7 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Thu, 17 Jul 2014 17:56:16 +1200 Subject: [PATCH] Add hotkey support to ProductionPaletteWidget. --- .../Widgets/Logic/ProductionTooltipLogic.cs | 11 +++++++- .../Widgets/ProductionPaletteWidget.cs | 25 +++++++++++++++++-- mods/cnc/chrome/tooltips.yaml | 5 ++++ mods/ra/chrome/tooltips.yaml | 6 +++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/OpenRA.Mods.RA/Widgets/Logic/ProductionTooltipLogic.cs b/OpenRA.Mods.RA/Widgets/Logic/ProductionTooltipLogic.cs index ad5efca263..bfadec4898 100644 --- a/OpenRA.Mods.RA/Widgets/Logic/ProductionTooltipLogic.cs +++ b/OpenRA.Mods.RA/Widgets/Logic/ProductionTooltipLogic.cs @@ -29,6 +29,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic widget.IsVisible = () => palette.TooltipActor != null; var nameLabel = widget.Get("NAME"); + var hotkeyLabel = widget.Get("HOTKEY"); var requiresLabel = widget.Get("REQUIRES"); var powerLabel = widget.Get("POWER"); var timeLabel = widget.Get("TIME"); @@ -54,6 +55,14 @@ namespace OpenRA.Mods.RA.Widgets.Logic nameLabel.GetText = () => tooltip.Name; + var nameWidth = font.Measure(tooltip.Name).X; + var hotkeyText = "({0})".F(buildable.Hotkey.DisplayString()); + var hotkeyWidth = buildable.Hotkey.IsValid() ? font.Measure(hotkeyText).X + 2 * nameLabel.Bounds.X : 0; + hotkeyLabel.GetText = () => hotkeyText; + hotkeyLabel.Bounds.X = nameWidth + 2 * nameLabel.Bounds.X; + hotkeyLabel.Visible = buildable.Hotkey.IsValid(); + + var prereqs = buildable.Prerequisites.Select(a => ActorName(mapRules, a)).Where(s => !s.StartsWith("~")); var requiresString = prereqs.Any() ? requiresLabel.Text.F(prereqs.JoinWith(", ")) : ""; requiresLabel.GetText = () => requiresString; @@ -80,7 +89,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic var descString = tooltip.Description.Replace("\\n", "\n"); descLabel.GetText = () => descString; - var leftWidth = new[] { font.Measure(tooltip.Name).X, requiresFont.Measure(requiresString).X, descFont.Measure(descString).X }.Aggregate(Math.Max); + var leftWidth = new[] { nameWidth + hotkeyWidth, requiresFont.Measure(requiresString).X, descFont.Measure(descString).X }.Aggregate(Math.Max); var rightWidth = new[] { font.Measure(powerString).X, font.Measure(timeString).X, font.Measure(costString).X }.Aggregate(Math.Max); timeLabel.Bounds.X = powerLabel.Bounds.X = costLabel.Bounds.X = leftWidth + 2 * nameLabel.Bounds.X; widget.Bounds.Width = leftWidth + rightWidth + 3 * nameLabel.Bounds.X; diff --git a/OpenRA.Mods.RA/Widgets/ProductionPaletteWidget.cs b/OpenRA.Mods.RA/Widgets/ProductionPaletteWidget.cs index eacb0bf746..b8088a9398 100644 --- a/OpenRA.Mods.RA/Widgets/ProductionPaletteWidget.cs +++ b/OpenRA.Mods.RA/Widgets/ProductionPaletteWidget.cs @@ -26,6 +26,7 @@ namespace OpenRA.Mods.RA.Widgets public class ProductionIcon { public string Name; + public Hotkey Hotkey; public Sprite Sprite; public float2 Pos; public List Queued; @@ -120,14 +121,23 @@ namespace OpenRA.Mods.RA.Widgets if (icon == null) return false; + // Only support left and right clicks + if (mi.Button != MouseButton.Left && mi.Button != MouseButton.Right) + return false; + // Eat mouse-up events if (mi.Event != MouseInputEvent.Down) return true; + return HandleEvent(icon, mi.Button == MouseButton.Left); + } + + bool HandleEvent(ProductionIcon icon, bool isLeftClick) + { var actor = World.Map.Rules.Actors[icon.Name]; var first = icon.Queued.FirstOrDefault(); - if (mi.Button == MouseButton.Left) + if (isLeftClick) { // Pick up a completed building if (first != null && first.Done && actor.Traits.Contains()) @@ -152,7 +162,7 @@ namespace OpenRA.Mods.RA.Widgets else Sound.Play(DisabledTabClick); } - else if (mi.Button == MouseButton.Right) + else { // Hold/Cancel an existing item if (first != null) @@ -179,6 +189,16 @@ namespace OpenRA.Mods.RA.Widgets return true; } + public override bool HandleKeyPress(KeyInput e) + { + if (e.Event == KeyInputEvent.Up || CurrentQueue == null) + return false; + + var hotkey = Hotkey.FromKeyInput(e); + var toBuild = icons.Values.FirstOrDefault(i => i.Hotkey == hotkey); + return toBuild != null ? HandleEvent(toBuild, true) : false; + } + public void RefreshIcons() { icons = new Dictionary(); @@ -210,6 +230,7 @@ namespace OpenRA.Mods.RA.Widgets var pi = new ProductionIcon() { Name = item.Name, + Hotkey = item.Traits.Get().Hotkey, Sprite = icon.Image, Pos = new float2(rect.Location), Queued = CurrentQueue.AllQueued().Where(a => a.Item == item.Name).ToList(), diff --git a/mods/cnc/chrome/tooltips.yaml b/mods/cnc/chrome/tooltips.yaml index 04405c57b5..5ce66514c3 100644 --- a/mods/cnc/chrome/tooltips.yaml +++ b/mods/cnc/chrome/tooltips.yaml @@ -56,6 +56,11 @@ Background@PRODUCTION_TOOLTIP: X: 5 Height: 23 Font: Bold + Label@HOTKEY: + Visible: false + Height: 23 + TextColor: 255,255,0 + Font: Bold Label@REQUIRES: X: 5 Y: 19 diff --git a/mods/ra/chrome/tooltips.yaml b/mods/ra/chrome/tooltips.yaml index ecb57dd4bf..a63948d198 100644 --- a/mods/ra/chrome/tooltips.yaml +++ b/mods/ra/chrome/tooltips.yaml @@ -119,6 +119,12 @@ Background@PRODUCTION_TOOLTIP: Y: 2 Height: 23 Font: Bold + Label@HOTKEY: + Visible: false + Y: 2 + Height: 23 + TextColor: 255,255,0 + Font: Bold Label@REQUIRES: X: 7 Y: 21