From bf3ddcfbf1745c3d5437cdbc69487f744bbd465a Mon Sep 17 00:00:00 2001 From: WolfGaming Date: Thu, 5 Mar 2015 13:55:21 +0000 Subject: [PATCH 1/4] Implemented an interface that allows traits to add custom information to tooltips. --- OpenRA.Game/Traits/TraitsInterfaces.cs | 6 ++++ .../Widgets/ViewportControllerWidget.cs | 3 ++ .../Widgets/Logic/Ingame/WorldTooltipLogic.cs | 29 ++++++++++++++++++- mods/cnc/chrome/tooltips.yaml | 5 ++++ mods/d2k/chrome/tooltips.yaml | 5 ++++ mods/ra/chrome/tooltips.yaml | 5 ++++ 6 files changed, 52 insertions(+), 1 deletion(-) diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index 748de195d8..850b5e92ff 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -136,6 +136,12 @@ namespace OpenRA.Traits bool IsOwnerRowVisible { get; } } + public interface IProvideTooltipInfo + { + bool IsTooltipVisible(Player forPlayer); + string TooltipText { get; } + } + public interface IDisabledTrait { bool IsTraitDisabled { get; } } public interface IDisable { bool Disabled { get; } } public interface IExplodeModifier { bool ShouldExplode(Actor self); } diff --git a/OpenRA.Game/Widgets/ViewportControllerWidget.cs b/OpenRA.Game/Widgets/ViewportControllerWidget.cs index 1c580dcc44..888b56e52b 100644 --- a/OpenRA.Game/Widgets/ViewportControllerWidget.cs +++ b/OpenRA.Game/Widgets/ViewportControllerWidget.cs @@ -27,6 +27,7 @@ namespace OpenRA.Widgets public WorldTooltipType TooltipType { get; private set; } public IToolTip ActorTooltip { get; private set; } + public IEnumerable ActorTooltipExtra { get; private set; } public FrozenActor FrozenActorTooltip { get; private set; } public int EdgeScrollThreshold = 15; @@ -109,6 +110,7 @@ namespace OpenRA.Widgets if (underCursor != null) { ActorTooltip = underCursor.TraitsImplementing().First(); + ActorTooltipExtra = underCursor.TraitsImplementing(); TooltipType = WorldTooltipType.Actor; return; } @@ -120,6 +122,7 @@ namespace OpenRA.Widgets if (frozen != null) { FrozenActorTooltip = frozen; + ActorTooltipExtra = frozen.Actor.TraitsImplementing(); TooltipType = WorldTooltipType.FrozenActor; } } diff --git a/OpenRA.Mods.Common/Widgets/Logic/Ingame/WorldTooltipLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Ingame/WorldTooltipLogic.cs index 30424c6783..0d39760a31 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Ingame/WorldTooltipLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Ingame/WorldTooltipLogic.cs @@ -9,6 +9,7 @@ #endregion using System; +using System.Collections.Generic; using System.Drawing; using OpenRA.Traits; using OpenRA.Widgets; @@ -24,6 +25,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic var label = widget.Get("LABEL"); var flag = widget.Get("FLAG"); var owner = widget.Get("OWNER"); + var extras = widget.Get("EXTRA"); var font = Game.Renderer.Fonts[label.Font]; var ownerFont = Game.Renderer.Fonts[owner.Font]; @@ -33,6 +35,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic var flagRace = ""; var ownerName = ""; var ownerColor = Color.White; + var extraText = ""; var singleHeight = widget.Get("SINGLE_HEIGHT").Bounds.Height; var doubleHeight = widget.Get("DOUBLE_HEIGHT").Bounds.Height; @@ -42,6 +45,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic if (viewport == null || viewport.TooltipType == WorldTooltipType.None) return; + var index = 0; + extraText = ""; showOwner = false; Player o = null; @@ -71,7 +76,22 @@ namespace OpenRA.Mods.Common.Widgets.Logic } } - var textWidth = font.Measure(labelText).X; + if (viewport.ActorTooltipExtra != null) + { + foreach (var info in viewport.ActorTooltipExtra) + { + if (info.IsTooltipVisible(world.LocalPlayer)) + { + if (index != 0) + extraText += "\n"; + extraText += info.TooltipText; + index++; + } + } + } + + var textWidth = Math.Max(font.Measure(labelText).X, font.Measure(extraText).X); + if (textWidth != cachedWidth) { label.Bounds.Width = textWidth; @@ -86,9 +106,15 @@ namespace OpenRA.Mods.Common.Widgets.Logic widget.Bounds.Height = doubleHeight; widget.Bounds.Width = Math.Max(widget.Bounds.Width, owner.Bounds.X + ownerFont.Measure(ownerName).X + label.Bounds.X); + index++; } else widget.Bounds.Height = singleHeight; + + if (extraText != "") + { + widget.Bounds.Height += font.Measure(extraText).Y + extras.Bounds.Height; + } }; label.GetText = () => labelText; @@ -98,6 +124,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic owner.IsVisible = () => showOwner; owner.GetText = () => ownerName; owner.GetColor = () => ownerColor; + extras.GetText = () => extraText; } } } diff --git a/mods/cnc/chrome/tooltips.yaml b/mods/cnc/chrome/tooltips.yaml index a2164612ff..4bc3042ab6 100644 --- a/mods/cnc/chrome/tooltips.yaml +++ b/mods/cnc/chrome/tooltips.yaml @@ -45,6 +45,11 @@ Background@WORLD_TOOLTIP: Y: 19 Height: 23 Font: Bold + Label@EXTRA: + X: 5 + Y: 45 + Height: 4 + Font: Bold Background@PRODUCTION_TOOLTIP: Logic: ProductionTooltipLogic diff --git a/mods/d2k/chrome/tooltips.yaml b/mods/d2k/chrome/tooltips.yaml index 1be6cd1480..f6faef849a 100644 --- a/mods/d2k/chrome/tooltips.yaml +++ b/mods/d2k/chrome/tooltips.yaml @@ -49,6 +49,11 @@ Background@WORLD_TOOLTIP: Y: 25 Height: 23 Font: Bold + Label@EXTRA: + X: 7 + Y: 55 + Height: 4 + Font: Bold Background@SPAWN_TOOLTIP: Logic: SpawnSelectorTooltipLogic diff --git a/mods/ra/chrome/tooltips.yaml b/mods/ra/chrome/tooltips.yaml index 5b78684304..9a6b06d65f 100644 --- a/mods/ra/chrome/tooltips.yaml +++ b/mods/ra/chrome/tooltips.yaml @@ -49,6 +49,11 @@ Background@WORLD_TOOLTIP: Y: 22 Height: 23 Font: Bold + Label@EXTRA: + X: 7 + Y: 48 + Height: 4 + Font: Bold Background@SPAWN_TOOLTIP: Logic: SpawnSelectorTooltipLogic From c460906ed5dd166a75390778715ef70ca510a20d Mon Sep 17 00:00:00 2001 From: WolfGaming Date: Thu, 5 Mar 2015 14:48:13 +0000 Subject: [PATCH 2/4] Added refund tooltip. --- OpenRA.Mods.Common/Traits/Sellable.cs | 39 ++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Sellable.cs b/OpenRA.Mods.Common/Traits/Sellable.cs index 9bbfb3cc23..c2af65272f 100644 --- a/OpenRA.Mods.Common/Traits/Sellable.cs +++ b/OpenRA.Mods.Common/Traits/Sellable.cs @@ -8,8 +8,10 @@ */ #endregion +using System; using System.Linq; using OpenRA.Mods.Common.Activities; +using OpenRA.Mods.Common.Orders; using OpenRA.Traits; namespace OpenRA.Mods.Common.Traits @@ -20,13 +22,20 @@ namespace OpenRA.Mods.Common.Traits public readonly int RefundPercent = 50; public readonly string[] SellSounds = { }; - public object Create(ActorInitializer init) { return new Sellable(this); } + public object Create(ActorInitializer init) { return new Sellable(init.Self, this); } } - public class Sellable : UpgradableTrait, IResolveOrder + public class Sellable : UpgradableTrait, IResolveOrder, IProvideTooltipInfo { - public Sellable(SellableInfo info) - : base(info) { } + readonly Actor self; + readonly Lazy health; + + public Sellable(Actor self, SellableInfo info) + : base(info) + { + this.self = self; + health = Exts.Lazy(() => self.TraitOrDefault()); + } public void ResolveOrder(Actor self, Order order) { @@ -57,5 +66,27 @@ namespace OpenRA.Mods.Common.Traits else self.QueueActivity(false, new Sell()); } + + public bool IsTooltipVisible(Player forPlayer) + { + if (self.World.OrderGenerator is SellOrderGenerator) + return forPlayer == self.Owner; + return false; + } + + public string TooltipText + { + get + { + var sellValue = self.GetSellValue() * Info.RefundPercent / 100; + if (health.Value != null) + { + sellValue *= health.Value.HP; + sellValue /= health.Value.MaxHP; + } + + return "Refund: " + sellValue; + } + } } } From d2f02e7a4b7fed3aad7b4cb4e2618f25130b7653 Mon Sep 17 00:00:00 2001 From: WolfGaming Date: Fri, 6 Mar 2015 21:56:56 +0000 Subject: [PATCH 3/4] Fix missions showing tooltips wrong. --- .../Widgets/Logic/Ingame/WorldTooltipLogic.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/OpenRA.Mods.Common/Widgets/Logic/Ingame/WorldTooltipLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Ingame/WorldTooltipLogic.cs index 0d39760a31..bb9b2aaddf 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Ingame/WorldTooltipLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Ingame/WorldTooltipLogic.cs @@ -39,6 +39,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic var singleHeight = widget.Get("SINGLE_HEIGHT").Bounds.Height; var doubleHeight = widget.Get("DOUBLE_HEIGHT").Bounds.Height; + var extraHeightOnDouble = extras.Bounds.Y; + var extraHeightOnSingle = extraHeightOnDouble - (doubleHeight - singleHeight); tooltipContainer.BeforeRender = () => { @@ -114,6 +116,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic if (extraText != "") { widget.Bounds.Height += font.Measure(extraText).Y + extras.Bounds.Height; + if (showOwner) + extras.Bounds.Y = extraHeightOnDouble; + else + extras.Bounds.Y = extraHeightOnSingle; } }; From 068a96fd460b4f0f2bfceecfc9db9ffa4d85a2e7 Mon Sep 17 00:00:00 2001 From: WolfGaming Date: Sat, 7 Mar 2015 21:09:29 +0000 Subject: [PATCH 4/4] Fixes NullPointerException when frozen.Actor is null. --- OpenRA.Game/Widgets/ViewportControllerWidget.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/OpenRA.Game/Widgets/ViewportControllerWidget.cs b/OpenRA.Game/Widgets/ViewportControllerWidget.cs index 888b56e52b..f1c3d436ed 100644 --- a/OpenRA.Game/Widgets/ViewportControllerWidget.cs +++ b/OpenRA.Game/Widgets/ViewportControllerWidget.cs @@ -93,6 +93,7 @@ namespace OpenRA.Widgets public void UpdateMouseover() { TooltipType = WorldTooltipType.None; + ActorTooltipExtra = null; var cell = worldRenderer.Viewport.ViewToWorld(Viewport.LastMousePos); if (!world.Map.Contains(cell)) return; @@ -122,7 +123,8 @@ namespace OpenRA.Widgets if (frozen != null) { FrozenActorTooltip = frozen; - ActorTooltipExtra = frozen.Actor.TraitsImplementing(); + if (frozen.Actor != null) + ActorTooltipExtra = frozen.Actor.TraitsImplementing(); TooltipType = WorldTooltipType.FrozenActor; } }