From 2f9114fcf8f73a8242f356206818464682fc831d Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Tue, 5 Jul 2011 18:10:35 +1200 Subject: [PATCH] Power/Silo bar tooltips. --- OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj | 1 + .../Widgets/Logic/SimpleTooltipLogic.cs | 40 +++++++++++++++++++ OpenRA.Mods.Cnc/Widgets/PowerBarWidget.cs | 23 ++++++++++- OpenRA.Mods.Cnc/Widgets/SiloBarWidget.cs | 21 ++++++++++ mods/cnc/chrome/ingame.yaml | 2 + mods/cnc/chrome/tooltips.yaml | 13 ++++++ 6 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 OpenRA.Mods.Cnc/Widgets/Logic/SimpleTooltipLogic.cs diff --git a/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj b/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj index 6565b299ef..995023beaf 100644 --- a/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj +++ b/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj @@ -100,6 +100,7 @@ + diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/SimpleTooltipLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/SimpleTooltipLogic.cs new file mode 100644 index 0000000000..66b4ceea19 --- /dev/null +++ b/OpenRA.Mods.Cnc/Widgets/Logic/SimpleTooltipLogic.cs @@ -0,0 +1,40 @@ +#region Copyright & License Information +/* + * Copyright 2007-2011 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see COPYING. + */ +#endregion + +using System; +using OpenRA.Support; +using OpenRA.Widgets; + +namespace OpenRA.Mods.Cnc.Widgets.Logic +{ + public class SimpleTooltipLogic + { + [ObjectCreator.UseCtor] + public SimpleTooltipLogic([ObjectCreator.Param] Widget widget, + [ObjectCreator.Param] Func getText) + { + var label = widget.GetWidget("LABEL"); + var cachedWidth = 0; + var font = Game.Renderer.Fonts[label.Font]; + label.GetText = () => + { + var text = getText(); + var textWidth = font.Measure(text).X; + if (textWidth != cachedWidth) + { + label.Bounds.Width = textWidth; + widget.Bounds.Width = 2*label.Bounds.X + textWidth; + } + return text; + }; + } + } +} + diff --git a/OpenRA.Mods.Cnc/Widgets/PowerBarWidget.cs b/OpenRA.Mods.Cnc/Widgets/PowerBarWidget.cs index 19e703dfc7..7039539df0 100755 --- a/OpenRA.Mods.Cnc/Widgets/PowerBarWidget.cs +++ b/OpenRA.Mods.Cnc/Widgets/PowerBarWidget.cs @@ -10,6 +10,7 @@ using System; using System.Drawing; +using OpenRA.FileFormats; using OpenRA.Graphics; using OpenRA.Mods.RA.Buildings; using OpenRA.Widgets; @@ -18,14 +19,34 @@ namespace OpenRA.Mods.Cnc.Widgets { public class PowerBarWidget : Widget { + public readonly string TooltipTemplate = "SIMPLE_TOOLTIP"; + public readonly string TooltipContainer; + Lazy tooltipContainer; + float? lastProvidedFrac; float? lastDrainedFrac; - readonly PowerManager pm; + [ObjectCreator.UseCtor] public PowerBarWidget( [ObjectCreator.Param] World world ) { pm = world.LocalPlayer.PlayerActor.Trait(); + tooltipContainer = new Lazy(() => + Widget.RootWidget.GetWidget(TooltipContainer)); + } + + public override void MouseEntered() + { + if (TooltipContainer == null) return; + Func getText = () => "Power Usage: {0}/{1}".F(pm.PowerDrained, pm.PowerProvided); + tooltipContainer.Value.SetTooltip( + Widget.LoadWidget(TooltipTemplate, null, new WidgetArgs() {{ "getText", getText }})); + } + + public override void MouseExited() + { + if (TooltipContainer == null) return; + tooltipContainer.Value.RemoveTooltip(); } public override void Draw() diff --git a/OpenRA.Mods.Cnc/Widgets/SiloBarWidget.cs b/OpenRA.Mods.Cnc/Widgets/SiloBarWidget.cs index ac114cf9dc..edc0d4cdc5 100755 --- a/OpenRA.Mods.Cnc/Widgets/SiloBarWidget.cs +++ b/OpenRA.Mods.Cnc/Widgets/SiloBarWidget.cs @@ -10,6 +10,7 @@ using System; using System.Drawing; +using OpenRA.FileFormats; using OpenRA.Graphics; using OpenRA.Traits; using OpenRA.Widgets; @@ -18,6 +19,10 @@ namespace OpenRA.Mods.Cnc.Widgets { public class SiloBarWidget : Widget { + public readonly string TooltipTemplate = "SIMPLE_TOOLTIP"; + public readonly string TooltipContainer; + Lazy tooltipContainer; + public float LowStorageThreshold = 0.8f; float? lastCapacityFrac; float? lastStoredFrac; @@ -27,6 +32,22 @@ namespace OpenRA.Mods.Cnc.Widgets public SiloBarWidget( [ObjectCreator.Param] World world ) { pr = world.LocalPlayer.PlayerActor.Trait(); + tooltipContainer = new Lazy(() => + Widget.RootWidget.GetWidget(TooltipContainer)); + } + + public override void MouseEntered() + { + if (TooltipContainer == null) return; + Func getText = () => "Silo Usage: {0}/{1}".F(pr.Ore, pr.OreCapacity); + tooltipContainer.Value.SetTooltip( + Widget.LoadWidget(TooltipTemplate, null, new WidgetArgs() {{ "getText", getText }})); + } + + public override void MouseExited() + { + if (TooltipContainer == null) return; + tooltipContainer.Value.RemoveTooltip(); } public override void Draw() diff --git a/mods/cnc/chrome/ingame.yaml b/mods/cnc/chrome/ingame.yaml index 2ccfa4d024..92fe1bac69 100644 --- a/mods/cnc/chrome/ingame.yaml +++ b/mods/cnc/chrome/ingame.yaml @@ -151,6 +151,7 @@ Container@INGAME_ROOT: Y:1 Width:PARENT_RIGHT-2 Height:PARENT_BOTTOM-2 + TooltipContainer:TOOLTIP_CONTAINER Label@POWERICON: X:4 Y:180 @@ -168,6 +169,7 @@ Container@INGAME_ROOT: Y:1 Width:PARENT_RIGHT-2 Height:PARENT_BOTTOM-2 + TooltipContainer:TOOLTIP_CONTAINER Label@SILOICON: X:181 Y:180 diff --git a/mods/cnc/chrome/tooltips.yaml b/mods/cnc/chrome/tooltips.yaml index 555de23f6c..0b9c8c1cb7 100644 --- a/mods/cnc/chrome/tooltips.yaml +++ b/mods/cnc/chrome/tooltips.yaml @@ -1,3 +1,16 @@ +Background@SIMPLE_TOOLTIP: + Id:SIMPLE_TOOLTIP + Logic:SimpleTooltipLogic + Background:panel-black + Width:100 + Height:25 + Children: + Label@LABEL: + Id:LABEL + X:5 + Height:23 + Font:Bold + Background@PRODUCTION_TOOLTIP: Id:PRODUCTION_TOOLTIP Logic:ProductionTooltipLogic