From a4814b82c840f8152b5a0f03aa4aa1b2ca77639a Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Wed, 27 Jul 2011 17:38:37 +1200 Subject: [PATCH] Display cost/power tooltip labels in red if there are insufficient funds/power. Fixes #1076. --- .../Widgets/Logic/ProductionTooltipLogic.cs | 56 +++++++++++-------- .../Widgets/ProductionPaletteWidget.cs | 3 +- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/ProductionTooltipLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/ProductionTooltipLogic.cs index 114904c40f..2f262ec6ba 100644 --- a/OpenRA.Mods.Cnc/Widgets/Logic/ProductionTooltipLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/Logic/ProductionTooltipLogic.cs @@ -9,11 +9,13 @@ #endregion using System; +using System.Drawing; using System.Linq; +using OpenRA.Support; +using OpenRA.Traits; +using OpenRA.Widgets; using OpenRA.Mods.RA; using OpenRA.Mods.RA.Buildings; -using OpenRA.Support; -using OpenRA.Widgets; namespace OpenRA.Mods.Cnc.Widgets.Logic { @@ -24,6 +26,9 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic [ObjectCreator.Param] TooltipContainerWidget tooltipContainer, [ObjectCreator.Param] ProductionPaletteWidget palette) { + var pm = palette.world.LocalPlayer.PlayerActor.Trait(); + var pr = palette.world.LocalPlayer.PlayerActor.Trait(); + widget.IsVisible = () => palette.TooltipActor != null; var nameLabel = widget.GetWidget("NAME"); var requiresLabel = widget.GetWidget("REQUIRES"); @@ -33,13 +38,8 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic var font = Game.Renderer.Fonts[nameLabel.Font]; var requiresFont = Game.Renderer.Fonts[requiresLabel.Font]; - var name = ""; - var requires = ""; - var power = ""; - var time = ""; - var cost = ""; - string lastActor = null; + tooltipContainer.BeforeRender = () => { var actor = palette.TooltipActor; @@ -49,30 +49,38 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic var info = Rules.Info[actor]; var tooltip = info.Traits.Get(); var buildable = info.Traits.Get(); - name = tooltip.Name; - cost = "$: {0}".F(info.Traits.Get().Cost); - time = "T: {0}".F(WidgetUtils.FormatTime(palette.CurrentQueue.GetBuildTime(actor))); - + var cost = info.Traits.Get().Cost; var bi = info.Traits.GetOrDefault(); - power = bi != null ? "P: {0}".F(bi.Power) : ""; + + nameLabel.GetText = () => tooltip.Name; var prereqs = buildable.Prerequisites.Select(a => ActorName(a)); - requires = prereqs.Any() ? "Requires {0}".F(string.Join(", ", prereqs.ToArray())) : ""; + var requiresString = prereqs.Any() ? "Requires {0}".F(string.Join(", ", prereqs.ToArray())) : ""; + requiresLabel.GetText = () => requiresString; - var leftWidth = Math.Max(font.Measure(name).X, requiresFont.Measure(requires).X); - var rightWidth = new [] {font.Measure(power).X, font.Measure(time).X, font.Measure(cost).X}.Aggregate(Math.Max); + var power = bi != null ? bi.Power : 0; + var powerString = "P: {0}".F(power); + powerLabel.GetText = () => powerString; + powerLabel.GetColor = () => ((pm.PowerProvided - pm.PowerDrained) >= -power || power > 0) + ? Color.White : Color.Red; + powerLabel.IsVisible = () => power != 0; + + var timeString = "T: {0}".F(WidgetUtils.FormatTime(palette.CurrentQueue.GetBuildTime(actor))); + timeLabel.GetText = () => timeString; + + var costString = "$: {0}".F(cost); + costLabel.GetText = () => costString; + costLabel.GetColor = () => pr.DisplayCash + pr.DisplayOre >= cost + ? Color.White : Color.Red; + + var leftWidth = Math.Max(font.Measure(tooltip.Name).X, requiresFont.Measure(requiresString).X); + 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; + + widget.Bounds.Height = power != 0 ? 65 : 45; lastActor = actor; - - widget.Bounds.Height = bi != null ? 65 : 45; }; - - nameLabel.GetText = () => name; - requiresLabel.GetText = () => requires; - powerLabel.GetText = () => power; - timeLabel.GetText = () => time; - costLabel.GetText = () => cost; } static string ActorName( string a ) diff --git a/OpenRA.Mods.Cnc/Widgets/ProductionPaletteWidget.cs b/OpenRA.Mods.Cnc/Widgets/ProductionPaletteWidget.cs index 8e75cb6f7e..1dd48f068e 100755 --- a/OpenRA.Mods.Cnc/Widgets/ProductionPaletteWidget.cs +++ b/OpenRA.Mods.Cnc/Widgets/ProductionPaletteWidget.cs @@ -39,6 +39,8 @@ namespace OpenRA.Mods.Cnc.Widgets public readonly string TooltipTemplate = "PRODUCTION_TOOLTIP"; public string TooltipActor { get; private set; } + public readonly World world; + Lazy tooltipContainer; ProductionQueue currentQueue; public ProductionQueue CurrentQueue @@ -60,7 +62,6 @@ namespace OpenRA.Mods.Cnc.Widgets Animation cantBuild, clock; Rectangle eventBounds = Rectangle.Empty; readonly WorldRenderer worldRenderer; - readonly World world; readonly SpriteFont overlayFont; readonly float2 holdOffset, readyOffset, timeOffset, queuedOffset;