From f368556b2390def12c9a0dc3da65eddebf0f4891 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Mon, 8 Apr 2013 23:01:21 +1200 Subject: [PATCH] Merge tooltip support into Button and remove ToggleButton. --- OpenRA.Game/Widgets/ButtonWidget.cs | 51 +++++++++++++---- OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj | 1 - .../Widgets/Logic/ButtonTooltipLogic.cs | 2 +- .../Widgets/Logic/CncIngameChromeLogic.cs | 6 +- .../Widgets/ProductionTypeButtonWidget.cs | 2 +- OpenRA.Mods.Cnc/Widgets/ToggleButtonWidget.cs | 55 ------------------- mods/cnc/chrome/ingame.yaml | 8 +-- 7 files changed, 48 insertions(+), 77 deletions(-) delete mode 100644 OpenRA.Mods.Cnc/Widgets/ToggleButtonWidget.cs diff --git a/OpenRA.Game/Widgets/ButtonWidget.cs b/OpenRA.Game/Widgets/ButtonWidget.cs index 41bfa1870e..0773bc8534 100644 --- a/OpenRA.Game/Widgets/ButtonWidget.cs +++ b/OpenRA.Game/Widgets/ButtonWidget.cs @@ -11,6 +11,7 @@ using System; using System.Collections.Generic; using System.Drawing; +using OpenRA.FileFormats; using OpenRA.Graphics; namespace OpenRA.Widgets @@ -36,6 +37,11 @@ namespace OpenRA.Widgets public Action OnMouseDown = _ => {}; public Action OnMouseUp = _ => {}; + public readonly string TooltipTemplate = "BUTTON_TOOLTIP"; + public readonly string TooltipText; + public readonly string TooltipContainer; + Lazy tooltipContainer; + // Equivalent to OnMouseUp, but without an input arg public Action OnClick = () => {}; public Action OnDoubleClick = () => {}; @@ -49,24 +55,32 @@ namespace OpenRA.Widgets OnKeyPress = _ => OnClick(); IsDisabled = () => Disabled; IsHighlighted = () => Highlighted; + tooltipContainer = Lazy.New(() => + Ui.Root.Get(TooltipContainer)); } - protected ButtonWidget(ButtonWidget widget) - : base(widget) + protected ButtonWidget(ButtonWidget other) + : base(other) { - Text = widget.Text; - Font = widget.Font; - Depressed = widget.Depressed; - VisualHeight = widget.VisualHeight; - GetText = widget.GetText; - OnMouseDown = widget.OnMouseDown; - Disabled = widget.Disabled; - IsDisabled = widget.IsDisabled; - Highlighted = widget.Highlighted; - IsHighlighted = widget.IsHighlighted; + Text = other.Text; + Font = other.Font; + Depressed = other.Depressed; + VisualHeight = other.VisualHeight; + GetText = other.GetText; + OnMouseDown = other.OnMouseDown; + Disabled = other.Disabled; + IsDisabled = other.IsDisabled; + Highlighted = other.Highlighted; + IsHighlighted = other.IsHighlighted; OnMouseUp = mi => OnClick(); OnKeyPress = _ => OnClick(); + + TooltipTemplate = other.TooltipTemplate; + TooltipText = other.TooltipText; + TooltipContainer = other.TooltipContainer; + tooltipContainer = Lazy.New(() => + Ui.Root.Get(TooltipContainer)); } public override bool LoseFocus(MouseInput mi) @@ -137,6 +151,19 @@ namespace OpenRA.Widgets return Depressed; } + public override void MouseEntered() + { + if (TooltipContainer == null) return; + tooltipContainer.Value.SetTooltip(TooltipTemplate, + new WidgetArgs() {{ "button", this }}); + } + + public override void MouseExited() + { + if (TooltipContainer == null) return; + tooltipContainer.Value.RemoveTooltip(); + } + public override int2 ChildOrigin { get { return RenderOrigin + ((Depressed) ? new int2(VisualHeight, VisualHeight) : new int2(0, 0)); } } diff --git a/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj b/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj index c3db98869f..c7d8551f80 100644 --- a/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj +++ b/OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj @@ -110,7 +110,6 @@ - diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/ButtonTooltipLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/ButtonTooltipLogic.cs index d9e6efd096..32d9a57cda 100644 --- a/OpenRA.Mods.Cnc/Widgets/Logic/ButtonTooltipLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/Logic/ButtonTooltipLogic.cs @@ -15,7 +15,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic public class ButtonTooltipLogic { [ObjectCreator.UseCtor] - public ButtonTooltipLogic(Widget widget, ToggleButtonWidget button) + public ButtonTooltipLogic(Widget widget, ButtonWidget button) { var label = widget.Get("LABEL"); var hotkey = widget.Get("HOTKEY"); diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/CncIngameChromeLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/CncIngameChromeLogic.cs index c8dde9418b..316e660252 100644 --- a/OpenRA.Mods.Cnc/Widgets/Logic/CncIngameChromeLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/Logic/CncIngameChromeLogic.cs @@ -90,8 +90,8 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic BindOrderButton(world, sidebarRoot, "SELL_BUTTON", "sell"); BindOrderButton(world, sidebarRoot, "REPAIR_BUTTON", "repair"); - sidebarRoot.Get("SELL_BUTTON").Key = Game.Settings.Keys.SellKey; - sidebarRoot.Get("REPAIR_BUTTON").Key = Game.Settings.Keys.RepairKey; + sidebarRoot.Get("SELL_BUTTON").Key = Game.Settings.Keys.SellKey; + sidebarRoot.Get("REPAIR_BUTTON").Key = Game.Settings.Keys.RepairKey; var powerManager = world.LocalPlayer.PlayerActor.Trait(); var playerResources = world.LocalPlayer.PlayerActor.Trait(); @@ -139,7 +139,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic static void BindOrderButton(World world, Widget parent, string button, string icon) where T : IOrderGenerator, new() { - var w = parent.Get(button); + var w = parent.Get(button); w.OnClick = () => world.ToggleInputMode(); w.IsHighlighted = () => world.OrderGenerator is T; diff --git a/OpenRA.Mods.Cnc/Widgets/ProductionTypeButtonWidget.cs b/OpenRA.Mods.Cnc/Widgets/ProductionTypeButtonWidget.cs index f521509f97..f7643ba15b 100644 --- a/OpenRA.Mods.Cnc/Widgets/ProductionTypeButtonWidget.cs +++ b/OpenRA.Mods.Cnc/Widgets/ProductionTypeButtonWidget.cs @@ -15,7 +15,7 @@ using OpenRA.Widgets; namespace OpenRA.Mods.Cnc.Widgets { - public class ProductionTypeButtonWidget : ToggleButtonWidget + public class ProductionTypeButtonWidget : ButtonWidget { public readonly string ProductionGroup; diff --git a/OpenRA.Mods.Cnc/Widgets/ToggleButtonWidget.cs b/OpenRA.Mods.Cnc/Widgets/ToggleButtonWidget.cs deleted file mode 100644 index 662aef4c30..0000000000 --- a/OpenRA.Mods.Cnc/Widgets/ToggleButtonWidget.cs +++ /dev/null @@ -1,55 +0,0 @@ -#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 System.Drawing; -using OpenRA.FileFormats; -using OpenRA.Widgets; - -namespace OpenRA.Mods.Cnc.Widgets -{ - public class ToggleButtonWidget : ButtonWidget - { - public readonly string TooltipTemplate = "BUTTON_TOOLTIP"; - public readonly string TooltipText; - public readonly string TooltipContainer; - Lazy tooltipContainer; - - public ToggleButtonWidget() - : base() - { - tooltipContainer = Lazy.New(() => - Ui.Root.Get(TooltipContainer)); - } - - protected ToggleButtonWidget(ToggleButtonWidget other) - : base(other) - { - TooltipTemplate = other.TooltipTemplate; - TooltipText = other.TooltipText; - TooltipContainer = other.TooltipContainer; - tooltipContainer = Lazy.New(() => - Ui.Root.Get(TooltipContainer)); - } - - public override void MouseEntered() - { - if (TooltipContainer == null) return; - tooltipContainer.Value.SetTooltip(TooltipTemplate, - new WidgetArgs() {{ "button", this }}); - } - - public override void MouseExited() - { - if (TooltipContainer == null) return; - tooltipContainer.Value.RemoveTooltip(); - } - } -} diff --git a/mods/cnc/chrome/ingame.yaml b/mods/cnc/chrome/ingame.yaml index 1e864cb18c..3ce898813e 100644 --- a/mods/cnc/chrome/ingame.yaml +++ b/mods/cnc/chrome/ingame.yaml @@ -44,7 +44,7 @@ Container@INGAME_ROOT: TooltipContainer@TOOLTIP_CONTAINER: Container@OBSERVER_WIDGETS: Children: - ToggleButton@OPTIONS_BUTTON: + Button@OPTIONS_BUTTON: Key:escape X:WINDOW_RIGHT-202 Y:5 @@ -98,7 +98,7 @@ Container@PLAYER_WIDGETS: Height:240 Background:panel-black Children: - ToggleButton@OPTIONS_BUTTON: + Button@OPTIONS_BUTTON: Key:escape X:42 Y:0-24 @@ -113,7 +113,7 @@ Container@PLAYER_WIDGETS: Y:5 ImageCollection:order-icons ImageName:options - ToggleButton@SELL_BUTTON: + Button@SELL_BUTTON: X:82 Y:0-24 Width:30 @@ -126,7 +126,7 @@ Container@PLAYER_WIDGETS: X:7 Y:5 ImageCollection:order-icons - ToggleButton@REPAIR_BUTTON: + Button@REPAIR_BUTTON: X:122 Y:0-24 Width:30