From 06990e356fca53be8461e25b0ae4440e4f238923 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Tue, 5 Jul 2011 19:41:34 +1200 Subject: [PATCH] Simple world tooltip. --- .../WorldInteractionControllerWidget.cs | 4 +- OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj | 2 + .../Widgets/Logic/WorldTooltipLogic.cs | 43 +++++++++ ...TooltipWorldInteractionControllerWidget.cs | 94 +++++++++++++++++++ OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs | 3 +- mods/cnc/chrome/ingame.yaml | 9 +- mods/cnc/chrome/tooltips.yaml | 48 ++++++---- 7 files changed, 178 insertions(+), 25 deletions(-) create mode 100644 OpenRA.Mods.Cnc/Widgets/Logic/WorldTooltipLogic.cs create mode 100644 OpenRA.Mods.Cnc/Widgets/TooltipWorldInteractionControllerWidget.cs diff --git a/OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs b/OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs index 90fb180c2a..2fa1d747f0 100644 --- a/OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs +++ b/OpenRA.Game/Widgets/WorldInteractionControllerWidget.cs @@ -21,7 +21,7 @@ namespace OpenRA.Widgets { public class WorldInteractionControllerWidget : Widget { - readonly World world; + protected readonly World world; readonly WorldRenderer worldRenderer; [ObjectCreator.UseCtor] @@ -51,7 +51,7 @@ namespace OpenRA.Widgets int2 dragStart, dragEnd; public override bool HandleMouseInput(MouseInput mi) - { + { var xy = Game.viewport.ViewToWorldPx(mi); if (mi.Button == MouseButton.Left && mi.Event == MouseInputEvent.Down) { diff --git a/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj b/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj index 995023beaf..85ea902e06 100644 --- a/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj +++ b/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj @@ -101,6 +101,8 @@ + + diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/WorldTooltipLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/WorldTooltipLogic.cs new file mode 100644 index 0000000000..6b3c37480d --- /dev/null +++ b/OpenRA.Mods.Cnc/Widgets/Logic/WorldTooltipLogic.cs @@ -0,0 +1,43 @@ +#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 OpenRA.Support; +using OpenRA.Widgets; +using T = OpenRA.Mods.Cnc.Widgets.TooltipWorldInteractionControllerWidget; + +namespace OpenRA.Mods.Cnc.Widgets.Logic +{ + public class WorldTooltipLogic + { + [ObjectCreator.UseCtor] + public WorldTooltipLogic([ObjectCreator.Param] Widget widget, + [ObjectCreator.Param] TooltipWorldInteractionControllerWidget 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 text = wic.TooltipType == T.WorldTooltipType.Unexplored ? "Unexplored Terrain" : + wic.ActorTooltip.Name(); + 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/TooltipWorldInteractionControllerWidget.cs b/OpenRA.Mods.Cnc/Widgets/TooltipWorldInteractionControllerWidget.cs new file mode 100644 index 0000000000..09074f619a --- /dev/null +++ b/OpenRA.Mods.Cnc/Widgets/TooltipWorldInteractionControllerWidget.cs @@ -0,0 +1,94 @@ +#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 OpenRA.FileFormats; +using OpenRA.Graphics; +using OpenRA.Traits; +using OpenRA.Widgets; +using System.Linq; + +namespace OpenRA.Mods.Cnc.Widgets +{ + public class TooltipWorldInteractionControllerWidget : WorldInteractionControllerWidget + { + public readonly string TooltipTemplate = "WORLD_TOOLTIP"; + public readonly string TooltipContainer; + Lazy tooltipContainer; + + public enum WorldTooltipType { None, Unexplored, Actor } + public WorldTooltipType TooltipType { get; private set; } + public IToolTip ActorTooltip { get; private set; } + + [ObjectCreator.UseCtor] + public TooltipWorldInteractionControllerWidget([ObjectCreator.Param] World world, + [ObjectCreator.Param] WorldRenderer worldRenderer) + : base(world, worldRenderer) + { + tooltipContainer = new Lazy(() => + Widget.RootWidget.GetWidget(TooltipContainer)); + } + + public override void MouseEntered() + { + if (TooltipContainer == null) + return; + + tooltipContainer.Value.SetTooltip( + Widget.LoadWidget(TooltipTemplate, null, new WidgetArgs() {{ "world", world }, { "wic", this }})); + } + + public override void MouseExited() + { + if (TooltipContainer == null) return; + tooltipContainer.Value.RemoveTooltip(); + } + + public void UpdateMouseover() + { + TooltipType = WorldTooltipType.None; + var cell = Game.viewport.ViewToWorld(Viewport.LastMousePos).ToInt2(); + if (!world.Map.IsInMap(cell)) + return; + + if (world.LocalPlayer != null && !world.LocalPlayer.Shroud.IsExplored(cell)) + { + TooltipType = WorldTooltipType.Unexplored; + return; + } + + var actor = world.FindUnitsAtMouse(Viewport.LastMousePos).FirstOrDefault(); + if (actor == null) + return; + + ActorTooltip = actor.TraitsImplementing().FirstOrDefault(); + if (ActorTooltip != null) + TooltipType = WorldTooltipType.Actor; + } + + public override bool HandleMouseInput(MouseInput mi) + { + if (mi.Event == MouseInputEvent.Move) + UpdateMouseover(); + + return base.HandleMouseInput(mi); + } + + float2 cachedLocation; + public override void Tick() + { + if (Game.viewport.Location != cachedLocation) + { + UpdateMouseover(); + cachedLocation = Game.viewport.Location; + } + base.Tick(); + } + } +} \ No newline at end of file diff --git a/OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs b/OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs index 31f64e9953..a3c5693dff 100644 --- a/OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs +++ b/OpenRA.Mods.RA/Widgets/WorldCommandWidget.cs @@ -9,6 +9,7 @@ #endregion using System; +using System.Drawing; using System.Linq; using OpenRA.FileFormats; using OpenRA.Graphics; @@ -36,7 +37,7 @@ namespace OpenRA.Mods.RA.Widgets } public override string GetCursor(int2 pos) { return null; } - + public override Rectangle GetEventBounds() { return Rectangle.Empty; } public override bool HandleKeyPress(KeyInput e) { if (World == null) return false; diff --git a/mods/cnc/chrome/ingame.yaml b/mods/cnc/chrome/ingame.yaml index 92fe1bac69..de11bbb791 100644 --- a/mods/cnc/chrome/ingame.yaml +++ b/mods/cnc/chrome/ingame.yaml @@ -2,10 +2,6 @@ Container@INGAME_ROOT: Id:INGAME_ROOT Logic:CncIngameChromeLogic Children: - WorldInteractionController: - Id:INTERACTION_CONTROLLER - Width:WINDOW_RIGHT - Height:WINDOW_BOTTOM ViewportScrollController: Width:WINDOW_RIGHT Height:WINDOW_BOTTOM @@ -17,6 +13,11 @@ Container@INGAME_ROOT: Id:STRATEGIC_PROGRESS X: WINDOW_RIGHT/2 Y: 40 + TooltipWorldInteractionController: + Id:INTERACTION_CONTROLLER + Width:WINDOW_RIGHT + Height:WINDOW_BOTTOM + TooltipContainer:TOOLTIP_CONTAINER Button@CHEATS_BUTTON: Id:CHEATS_BUTTON X:WINDOW_RIGHT-400 diff --git a/mods/cnc/chrome/tooltips.yaml b/mods/cnc/chrome/tooltips.yaml index 0b9c8c1cb7..170ed60af6 100644 --- a/mods/cnc/chrome/tooltips.yaml +++ b/mods/cnc/chrome/tooltips.yaml @@ -2,7 +2,36 @@ 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@BUTTON_TOOLTIP: + Id:BUTTON_TOOLTIP + Logic:ButtonTooltipLogic + Background:panel-black + Height:25 + Children: + Label@LABEL: + Id:LABEL + X:5 + Height:23 + Font:Bold + Label@HOTKEY: + Id:HOTKEY + Color:255,255,0 + Height:23 + Font:Bold + +Background@WORLD_TOOLTIP: + Id:WORLD_TOOLTIP + Logic:WorldTooltipLogic + Background:panel-black + Width:141 Height:25 Children: Label@LABEL: @@ -23,21 +52,4 @@ Background@PRODUCTION_TOOLTIP: X:5 Width:PARENT_RIGHT-10 Height:23 - Font:Bold -Background@BUTTON_TOOLTIP: - Id:BUTTON_TOOLTIP - Logic:ButtonTooltipLogic - Background:panel-black - Width:100 - Height:25 - Children: - Label@LABEL: - Id:LABEL - X:5 - Height:23 - Font:Bold - Label@HOTKEY: - Id:HOTKEY - Color:255,255,0 - Height:23 Font:Bold \ No newline at end of file