From c965899b8cf2edabf12f10abfcc74059380d7685 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Wed, 6 Jul 2011 00:01:35 +1200 Subject: [PATCH] Better support for dynamic tooltip sizes --- .../Widgets/CncWorldInteractionControllerWidget.cs | 7 ++----- .../Widgets/Logic/SimpleTooltipLogic.cs | 14 +++++++++----- OpenRA.Mods.Cnc/Widgets/Logic/WorldTooltipLogic.cs | 13 ++++++++----- OpenRA.Mods.Cnc/Widgets/PowerBarWidget.cs | 3 +-- OpenRA.Mods.Cnc/Widgets/ProductionPaletteWidget.cs | 7 ++----- OpenRA.Mods.Cnc/Widgets/SiloBarWidget.cs | 3 +-- OpenRA.Mods.Cnc/Widgets/SupportPowersWidget.cs | 7 ++----- OpenRA.Mods.Cnc/Widgets/TooltipButtonWidget.cs | 7 ++----- OpenRA.Mods.Cnc/Widgets/TooltipContainerWidget.cs | 13 ++++++++----- 9 files changed, 35 insertions(+), 39 deletions(-) diff --git a/OpenRA.Mods.Cnc/Widgets/CncWorldInteractionControllerWidget.cs b/OpenRA.Mods.Cnc/Widgets/CncWorldInteractionControllerWidget.cs index 08b4e4594e..cf4777fe40 100644 --- a/OpenRA.Mods.Cnc/Widgets/CncWorldInteractionControllerWidget.cs +++ b/OpenRA.Mods.Cnc/Widgets/CncWorldInteractionControllerWidget.cs @@ -42,11 +42,8 @@ namespace OpenRA.Mods.Cnc.Widgets public override void MouseEntered() { - if (TooltipContainer == null) - return; - - tooltipContainer.Value.SetTooltip( - Widget.LoadWidget(TooltipTemplate, null, new WidgetArgs() {{ "world", world }, { "wic", this }})); + if (TooltipContainer == null) return; + tooltipContainer.Value.SetTooltip(TooltipTemplate, new WidgetArgs() {{ "world", world }, { "wic", this }}); } public override void MouseExited() diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/SimpleTooltipLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/SimpleTooltipLogic.cs index 66b4ceea19..494ddbbc90 100644 --- a/OpenRA.Mods.Cnc/Widgets/Logic/SimpleTooltipLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/Logic/SimpleTooltipLogic.cs @@ -18,22 +18,26 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic { [ObjectCreator.UseCtor] public SimpleTooltipLogic([ObjectCreator.Param] Widget widget, + [ObjectCreator.Param] TooltipContainerWidget tooltipContainer, [ObjectCreator.Param] Func getText) { var label = widget.GetWidget("LABEL"); - var cachedWidth = 0; + var font = Game.Renderer.Fonts[label.Font]; - label.GetText = () => + var cachedWidth = 0; + var labelText = ""; + tooltipContainer.BeforeRender = () => { - var text = getText(); - var textWidth = font.Measure(text).X; + labelText = getText(); + var textWidth = font.Measure(labelText).X; if (textWidth != cachedWidth) { label.Bounds.Width = textWidth; widget.Bounds.Width = 2*label.Bounds.X + textWidth; } - return text; }; + + label.GetText = () => labelText; } } } diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/WorldTooltipLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/WorldTooltipLogic.cs index ce9cef285d..1dee8afe9e 100644 --- a/OpenRA.Mods.Cnc/Widgets/Logic/WorldTooltipLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/Logic/WorldTooltipLogic.cs @@ -18,25 +18,28 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic { [ObjectCreator.UseCtor] public WorldTooltipLogic([ObjectCreator.Param] Widget widget, + [ObjectCreator.Param] TooltipContainerWidget tooltipContainer, [ObjectCreator.Param] CncWorldInteractionControllerWidget wic) { widget.IsVisible = () => wic.TooltipType != T.WorldTooltipType.None; - var label = widget.GetWidget("LABEL"); + var font = Game.Renderer.Fonts[label.Font]; var cachedWidth = 0; - label.GetText = () => + var labelText = ""; + tooltipContainer.BeforeRender = () => { - var text = wic.TooltipType == T.WorldTooltipType.Unexplored ? "Unexplored Terrain" : + labelText = wic.TooltipType == T.WorldTooltipType.Unexplored ? "Unexplored Terrain" : wic.ActorTooltip.Name(); - var textWidth = font.Measure(text).X; + var textWidth = font.Measure(labelText).X; if (textWidth != cachedWidth) { label.Bounds.Width = textWidth; widget.Bounds.Width = 2*label.Bounds.X + textWidth; } - return text; }; + + label.GetText = () => labelText; } } } diff --git a/OpenRA.Mods.Cnc/Widgets/PowerBarWidget.cs b/OpenRA.Mods.Cnc/Widgets/PowerBarWidget.cs index 7039539df0..f1d2f00ef8 100755 --- a/OpenRA.Mods.Cnc/Widgets/PowerBarWidget.cs +++ b/OpenRA.Mods.Cnc/Widgets/PowerBarWidget.cs @@ -39,8 +39,7 @@ namespace OpenRA.Mods.Cnc.Widgets { 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 }})); + tooltipContainer.Value.SetTooltip(TooltipTemplate, new WidgetArgs() {{ "getText", getText }}); } public override void MouseExited() diff --git a/OpenRA.Mods.Cnc/Widgets/ProductionPaletteWidget.cs b/OpenRA.Mods.Cnc/Widgets/ProductionPaletteWidget.cs index c37cfe0b32..7bfd81ada2 100755 --- a/OpenRA.Mods.Cnc/Widgets/ProductionPaletteWidget.cs +++ b/OpenRA.Mods.Cnc/Widgets/ProductionPaletteWidget.cs @@ -92,11 +92,8 @@ namespace OpenRA.Mods.Cnc.Widgets public override void MouseEntered() { - if (TooltipContainer == null) - return; - - var panel = Widget.LoadWidget(TooltipTemplate, null, new WidgetArgs() {{ "palette", this }}); - tooltipContainer.Value.SetTooltip(panel); + if (TooltipContainer == null) return; + tooltipContainer.Value.SetTooltip(TooltipTemplate, new WidgetArgs() {{ "palette", this }}); } public override void MouseExited() diff --git a/OpenRA.Mods.Cnc/Widgets/SiloBarWidget.cs b/OpenRA.Mods.Cnc/Widgets/SiloBarWidget.cs index edc0d4cdc5..360700cbdd 100755 --- a/OpenRA.Mods.Cnc/Widgets/SiloBarWidget.cs +++ b/OpenRA.Mods.Cnc/Widgets/SiloBarWidget.cs @@ -40,8 +40,7 @@ namespace OpenRA.Mods.Cnc.Widgets { 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 }})); + tooltipContainer.Value.SetTooltip(TooltipTemplate, new WidgetArgs() {{ "getText", getText }}); } public override void MouseExited() diff --git a/OpenRA.Mods.Cnc/Widgets/SupportPowersWidget.cs b/OpenRA.Mods.Cnc/Widgets/SupportPowersWidget.cs index e3e1e3dff8..7525a83dc0 100755 --- a/OpenRA.Mods.Cnc/Widgets/SupportPowersWidget.cs +++ b/OpenRA.Mods.Cnc/Widgets/SupportPowersWidget.cs @@ -122,11 +122,8 @@ namespace OpenRA.Mods.Cnc.Widgets public override void MouseEntered() { - if (TooltipContainer == null) - return; - - var panel = Widget.LoadWidget(TooltipTemplate, null, new WidgetArgs() {{ "palette", this }}); - tooltipContainer.Value.SetTooltip(panel); + if (TooltipContainer == null) return; + tooltipContainer.Value.SetTooltip(TooltipTemplate, new WidgetArgs() {{ "palette", this }}); } public override void MouseExited() diff --git a/OpenRA.Mods.Cnc/Widgets/TooltipButtonWidget.cs b/OpenRA.Mods.Cnc/Widgets/TooltipButtonWidget.cs index ccb6654ace..b62ae2bc58 100644 --- a/OpenRA.Mods.Cnc/Widgets/TooltipButtonWidget.cs +++ b/OpenRA.Mods.Cnc/Widgets/TooltipButtonWidget.cs @@ -39,11 +39,8 @@ namespace OpenRA.Mods.Cnc.Widgets public override void MouseEntered() { - if (TooltipContainer == null) - return; - - tooltipContainer.Value.SetTooltip( - Widget.LoadWidget(TooltipTemplate, null, new WidgetArgs() {{ "button", this }})); + if (TooltipContainer == null) return; + tooltipContainer.Value.SetTooltip(TooltipTemplate, new WidgetArgs() {{ "button", this }}); } public override void MouseExited() diff --git a/OpenRA.Mods.Cnc/Widgets/TooltipContainerWidget.cs b/OpenRA.Mods.Cnc/Widgets/TooltipContainerWidget.cs index 6201c4cd4c..6d5838cbc4 100755 --- a/OpenRA.Mods.Cnc/Widgets/TooltipContainerWidget.cs +++ b/OpenRA.Mods.Cnc/Widgets/TooltipContainerWidget.cs @@ -19,9 +19,11 @@ using System; namespace OpenRA.Mods.Cnc.Widgets { - class TooltipContainerWidget : Widget + public class TooltipContainerWidget : Widget { + static readonly Action Nothing = () => {}; public int2 CursorOffset = new int2(0, 20); + public Action BeforeRender = Nothing; public int TooltipDelay = 10; Widget tooltip; @@ -30,18 +32,19 @@ namespace OpenRA.Mods.Cnc.Widgets IsVisible = () => Viewport.TicksSinceLastMove >= TooltipDelay; } - public void SetTooltip(Widget tooltip) + public void SetTooltip(string id, WidgetArgs args) { - this.tooltip = tooltip; - RemoveChildren(); - AddChild(tooltip); + RemoveTooltip(); + tooltip = Widget.LoadWidget(id, this, new WidgetArgs(args) {{ "tooltipContainer", this }}); } public void RemoveTooltip() { RemoveChildren(); + BeforeRender = Nothing; } + public override void Draw() { BeforeRender(); } public override Rectangle GetEventBounds() { return Rectangle.Empty; } public override int2 ChildOrigin {