From da70683c03ff77e38e3c5b17132d0e045cf7418b Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 26 Apr 2015 19:17:05 +1200 Subject: [PATCH] Split OrderButtonsChromeLogic into smaller classes. --- OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 1 + .../Logic/Ingame/MenuButtonsChromeLogic.cs | 125 +++++++++++++++ .../Logic/Ingame/OrderButtonsChromeLogic.cs | 148 +++++------------- mods/cnc/chrome/ingame.yaml | 7 +- mods/d2k/chrome/ingame-observer.yaml | 5 +- mods/d2k/chrome/ingame-player.yaml | 6 +- mods/ra/chrome/ingame-observer.yaml | 2 +- mods/ra/chrome/ingame-player.yaml | 10 +- mods/ts/chrome/ingame-player.yaml | 10 +- 9 files changed, 184 insertions(+), 130 deletions(-) create mode 100644 OpenRA.Mods.Common/Widgets/Logic/Ingame/MenuButtonsChromeLogic.cs diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index 5887e79d16..b9bd9b17e9 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -624,6 +624,7 @@ + diff --git a/OpenRA.Mods.Common/Widgets/Logic/Ingame/MenuButtonsChromeLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Ingame/MenuButtonsChromeLogic.cs new file mode 100644 index 0000000000..f6972aceae --- /dev/null +++ b/OpenRA.Mods.Common/Widgets/Logic/Ingame/MenuButtonsChromeLogic.cs @@ -0,0 +1,125 @@ +#region Copyright & License Information +/* + * Copyright 2007-2015 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.Linq; +using OpenRA.Mods.Common.Orders; +using OpenRA.Mods.Common.Traits; +using OpenRA.Mods.Common.Widgets; +using OpenRA.Widgets; + +namespace OpenRA.Mods.Common.Widgets.Logic +{ + public class MenuButtonsChromeLogic + { + readonly World world; + readonly Widget worldRoot; + readonly Widget menuRoot; + bool disableSystemButtons; + Widget currentWidget; + + [ObjectCreator.UseCtor] + public MenuButtonsChromeLogic(Widget widget, World world) + { + this.world = world; + + worldRoot = Ui.Root.Get("WORLD_ROOT"); + menuRoot = Ui.Root.Get("MENU_ROOT"); + + Action removeCurrentWidget = () => menuRoot.RemoveChild(currentWidget); + world.GameOver += removeCurrentWidget; + + // System buttons + var options = widget.GetOrNull("OPTIONS_BUTTON"); + if (options != null) + { + var blinking = false; + var lp = world.LocalPlayer; + options.IsDisabled = () => disableSystemButtons; + options.OnClick = () => + { + blinking = false; + OpenMenuPanel(options, new WidgetArgs() + { + { "activePanel", IngameInfoPanel.AutoSelect } + }); + }; + options.IsHighlighted = () => blinking && Game.LocalTick % 50 < 25; + + if (lp != null) + { + Action startBlinking = player => + { + if (player == world.LocalPlayer) + blinking = true; + }; + + var mo = lp.PlayerActor.TraitOrDefault(); + + if (mo != null) + mo.ObjectiveAdded += startBlinking; + } + } + + var diplomacy = widget.GetOrNull("DIPLOMACY_BUTTON"); + if (diplomacy != null) + { + diplomacy.Visible = !world.Map.Visibility.HasFlag(MapVisibility.MissionSelector) && world.Players.Any(a => a != world.LocalPlayer && !a.NonCombatant); + diplomacy.IsDisabled = () => disableSystemButtons; + diplomacy.OnClick = () => OpenMenuPanel(diplomacy); + } + + var debug = widget.GetOrNull("DEBUG_BUTTON"); + if (debug != null) + { + debug.IsVisible = () => world.LobbyInfo.GlobalSettings.AllowCheats; + debug.IsDisabled = () => disableSystemButtons; + debug.OnClick = () => OpenMenuPanel(debug, new WidgetArgs() + { + { "activePanel", IngameInfoPanel.Debug } + }); + } + + var stats = widget.GetOrNull("OBSERVER_STATS_BUTTON"); + if (stats != null) + { + stats.IsDisabled = () => disableSystemButtons; + stats.OnClick = () => OpenMenuPanel(stats); + } + } + + void OpenMenuPanel(MenuButtonWidget button, WidgetArgs widgetArgs = null) + { + disableSystemButtons = true; + var cachedPause = world.PredictedPaused; + + if (button.HideIngameUI) + worldRoot.IsVisible = () => false; + + if (button.Pause && world.LobbyInfo.IsSinglePlayer) + world.SetPauseState(true); + + widgetArgs = widgetArgs ?? new WidgetArgs(); + widgetArgs.Add("onExit", () => + { + if (button.HideIngameUI) + worldRoot.IsVisible = () => true; + + if (button.Pause && world.LobbyInfo.IsSinglePlayer) + world.SetPauseState(cachedPause); + + menuRoot.RemoveChild(currentWidget); + disableSystemButtons = false; + }); + + currentWidget = Game.LoadWidget(world, button.MenuContainer, menuRoot, widgetArgs); + } + } +} diff --git a/OpenRA.Mods.Common/Widgets/Logic/Ingame/OrderButtonsChromeLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Ingame/OrderButtonsChromeLogic.cs index e0ac35b121..0c1ee36baf 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Ingame/OrderButtonsChromeLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Ingame/OrderButtonsChromeLogic.cs @@ -17,141 +17,65 @@ using OpenRA.Widgets; namespace OpenRA.Mods.Common.Widgets.Logic { - public class OrderButtonsChromeLogic + public class SellOrderButtonLogic { - readonly World world; - readonly Widget worldRoot; - readonly Widget menuRoot; - bool disableSystemButtons; - Widget currentWidget; - [ObjectCreator.UseCtor] - public OrderButtonsChromeLogic(Widget widget, World world) + public SellOrderButtonLogic(Widget widget, World world) { - this.world = world; - var ingameRoot = Ui.Root.Get("INGAME_ROOT"); - worldRoot = ingameRoot.Get("WORLD_ROOT"); - menuRoot = ingameRoot.Get("MENU_ROOT"); - - Action removeCurrentWidget = () => menuRoot.RemoveChild(currentWidget); - world.GameOver += removeCurrentWidget; - - // Order Buttons - var sell = widget.GetOrNull("SELL_BUTTON"); + var sell = widget as ButtonWidget; if (sell != null) { sell.GetKey = _ => Game.Settings.Keys.SellKey; - BindOrderButton(world, sell, "sell"); + OrderButtonsChromeUtils.BindOrderButton(world, sell, "sell"); } + } + } - var repair = widget.GetOrNull("REPAIR_BUTTON"); + public class RepairOrderButtonLogic + { + [ObjectCreator.UseCtor] + public RepairOrderButtonLogic(Widget widget, World world) + { + var repair = widget as ButtonWidget; if (repair != null) { repair.GetKey = _ => Game.Settings.Keys.RepairKey; - BindOrderButton(world, repair, "repair"); + OrderButtonsChromeUtils.BindOrderButton(world, repair, "repair"); } + } + } - var beacon = widget.GetOrNull("BEACON_BUTTON"); - if (beacon != null) - { - beacon.GetKey = _ => Game.Settings.Keys.PlaceBeaconKey; - BindOrderButton(world, beacon, "beacon"); - } - - var power = widget.GetOrNull("POWER_BUTTON"); + public class PowerdownOrderButtonLogic + { + [ObjectCreator.UseCtor] + public PowerdownOrderButtonLogic(Widget widget, World world) + { + var power = widget as ButtonWidget; if (power != null) { power.GetKey = _ => Game.Settings.Keys.PowerDownKey; - BindOrderButton(world, power, "power"); - } - - // System buttons - var options = widget.GetOrNull("OPTIONS_BUTTON"); - if (options != null) - { - var blinking = false; - var lp = world.LocalPlayer; - options.IsDisabled = () => disableSystemButtons; - options.OnClick = () => - { - blinking = false; - OpenMenuPanel(options, new WidgetArgs() - { - { "activePanel", IngameInfoPanel.AutoSelect } - }); - }; - options.IsHighlighted = () => blinking && Game.LocalTick % 50 < 25; - - if (lp != null) - { - Action startBlinking = player => - { - if (player == world.LocalPlayer) - blinking = true; - }; - - var mo = lp.PlayerActor.TraitOrDefault(); - - if (mo != null) - mo.ObjectiveAdded += startBlinking; - } - } - - var diplomacy = widget.GetOrNull("DIPLOMACY_BUTTON"); - if (diplomacy != null) - { - diplomacy.Visible = !world.Map.Visibility.HasFlag(MapVisibility.MissionSelector) && world.Players.Any(a => a != world.LocalPlayer && !a.NonCombatant); - diplomacy.IsDisabled = () => disableSystemButtons; - diplomacy.OnClick = () => OpenMenuPanel(diplomacy); - } - - var debug = widget.GetOrNull("DEBUG_BUTTON"); - if (debug != null) - { - debug.IsVisible = () => world.LobbyInfo.GlobalSettings.AllowCheats; - debug.IsDisabled = () => disableSystemButtons; - debug.OnClick = () => OpenMenuPanel(debug, new WidgetArgs() - { - { "activePanel", IngameInfoPanel.Debug } - }); - } - - var stats = widget.GetOrNull("OBSERVER_STATS_BUTTON"); - if (stats != null) - { - stats.IsDisabled = () => disableSystemButtons; - stats.OnClick = () => OpenMenuPanel(stats); + OrderButtonsChromeUtils.BindOrderButton(world, power, "power"); } } + } - void OpenMenuPanel(MenuButtonWidget button, WidgetArgs widgetArgs = null) + public class BeaconOrderButtonLogic + { + [ObjectCreator.UseCtor] + public BeaconOrderButtonLogic(Widget widget, World world) { - disableSystemButtons = true; - var cachedPause = world.PredictedPaused; - - if (button.HideIngameUI) - worldRoot.IsVisible = () => false; - - if (button.Pause && world.LobbyInfo.IsSinglePlayer) - world.SetPauseState(true); - - widgetArgs = widgetArgs ?? new WidgetArgs(); - widgetArgs.Add("onExit", () => + var beacon = widget as ButtonWidget; + if (beacon != null) { - if (button.HideIngameUI) - worldRoot.IsVisible = () => true; - - if (button.Pause && world.LobbyInfo.IsSinglePlayer) - world.SetPauseState(cachedPause); - - menuRoot.RemoveChild(currentWidget); - disableSystemButtons = false; - }); - - currentWidget = Game.LoadWidget(world, button.MenuContainer, menuRoot, widgetArgs); + beacon.GetKey = _ => Game.Settings.Keys.PlaceBeaconKey; + OrderButtonsChromeUtils.BindOrderButton(world, beacon, "beacon"); + } } + } - static void BindOrderButton(World world, ButtonWidget w, string icon) + public class OrderButtonsChromeUtils + { + public static void BindOrderButton(World world, ButtonWidget w, string icon) where T : IOrderGenerator, new() { w.OnClick = () => world.ToggleInputMode(); diff --git a/mods/cnc/chrome/ingame.yaml b/mods/cnc/chrome/ingame.yaml index 01cb2840f3..50e5d7591e 100644 --- a/mods/cnc/chrome/ingame.yaml +++ b/mods/cnc/chrome/ingame.yaml @@ -61,7 +61,7 @@ Container@INGAME_ROOT: TooltipContainer@TOOLTIP_CONTAINER: Container@OBSERVER_WIDGETS: - Logic: OrderButtonsChromeLogic + Logic: MenuButtonsChromeLogic Children: MenuButton@OPTIONS_BUTTON: Key: escape @@ -237,7 +237,7 @@ Container@PLAYER_WIDGETS: ReadyText: Ready HoldText: On Hold Background@SIDEBAR_BACKGROUND: - Logic: OrderButtonsChromeLogic + Logic: MenuButtonsChromeLogic X: WINDOW_RIGHT - 204 Y: 30 Width: 194 @@ -260,6 +260,7 @@ Container@PLAYER_WIDGETS: ImageCollection: order-icons ImageName: options Button@BEACON_BUTTON: + Logic: BeaconOrderButtonLogic X: 62 Y: 0-24 Width: 30 @@ -273,6 +274,7 @@ Container@PLAYER_WIDGETS: Y: 5 ImageCollection: order-icons Button@SELL_BUTTON: + Logic: SellOrderButtonLogic X: 102 Y: 0-24 Width: 30 @@ -286,6 +288,7 @@ Container@PLAYER_WIDGETS: Y: 5 ImageCollection: order-icons Button@REPAIR_BUTTON: + Logic: RepairOrderButtonLogic X: 142 Y: 0-24 Width: 30 diff --git a/mods/d2k/chrome/ingame-observer.yaml b/mods/d2k/chrome/ingame-observer.yaml index 5e4ff4dfab..6f16621021 100644 --- a/mods/d2k/chrome/ingame-observer.yaml +++ b/mods/d2k/chrome/ingame-observer.yaml @@ -1,16 +1,13 @@ Container@OBSERVER_WIDGETS: + Logic: MenuButtonsChromeLogic Children: MenuButton@OPTIONS_BUTTON: - Logic: OrderButtonsChromeLogic - X: 0 - Y: 0 Width: 160 Height: 25 Text: Options (Esc) Font: Bold Key: escape MenuButton@OBSERVER_STATS_BUTTON: - Logic: OrderButtonsChromeLogic MenuContainer: INGAME_OBSERVERSTATS_BG HideIngameUI: False Pause: False diff --git a/mods/d2k/chrome/ingame-player.yaml b/mods/d2k/chrome/ingame-player.yaml index 8609ab63f1..bc351a325d 100644 --- a/mods/d2k/chrome/ingame-player.yaml +++ b/mods/d2k/chrome/ingame-player.yaml @@ -34,7 +34,7 @@ Container@PLAYER_WIDGETS: ClickThrough: false Children: Container@TOP_BUTTONS: - Logic: OrderButtonsChromeLogic + Logic: MenuButtonsChromeLogic X: 16 Y: 236 Children: @@ -54,6 +54,7 @@ Container@PLAYER_WIDGETS: ImageCollection: order-icons ImageName: debug Button@REPAIR_BUTTON: + Logic: RepairOrderButtonLogic X: 29 Width: 34 Height: 35 @@ -67,6 +68,7 @@ Container@PLAYER_WIDGETS: Y: 0 ImageCollection: order-icons Button@SELL_BUTTON: + Logic: SellOrderButtonLogic X: 54 Width: 34 Height: 35 @@ -80,6 +82,7 @@ Container@PLAYER_WIDGETS: Y: 0 ImageCollection: order-icons Button@BEACON_BUTTON: + Logic: BeaconOrderButtonLogic X: 108 Width: 36 Height: 35 @@ -94,6 +97,7 @@ Container@PLAYER_WIDGETS: ImageCollection: order-icons Button@POWER_BUTTON: + Logic: PowerdownOrderButtonLogic X: 133 Width: 36 Height: 35 diff --git a/mods/ra/chrome/ingame-observer.yaml b/mods/ra/chrome/ingame-observer.yaml index eb90d6e311..ce8959b869 100644 --- a/mods/ra/chrome/ingame-observer.yaml +++ b/mods/ra/chrome/ingame-observer.yaml @@ -31,7 +31,7 @@ Container@OBSERVER_WIDGETS: Align: Right Font: TinyBold Container@TOP_BUTTONS: - Logic: OrderButtonsChromeLogic + Logic: MenuButtonsChromeLogic X: 9 Y: 7 Children: diff --git a/mods/ra/chrome/ingame-player.yaml b/mods/ra/chrome/ingame-player.yaml index 53556edf87..a317560c81 100644 --- a/mods/ra/chrome/ingame-player.yaml +++ b/mods/ra/chrome/ingame-player.yaml @@ -36,12 +36,12 @@ Container@PLAYER_WIDGETS: ClickThrough: false Children: Container@TOP_BUTTONS: - Logic: OrderButtonsChromeLogic + Logic: MenuButtonsChromeLogic X: 9 Y: 7 Children: Button@BEACON_BUTTON: - Logic: AddRaceSuffixLogic + Logic: BeaconOrderButtonLogic, AddRaceSuffixLogic Width: 28 Height: 28 Background: sidebar-button @@ -54,7 +54,7 @@ Container@PLAYER_WIDGETS: Y: 6 ImageCollection: order-icons Button@SELL_BUTTON: - Logic: AddRaceSuffixLogic + Logic: SellOrderButtonLogic, AddRaceSuffixLogic X: 32 Width: 28 Height: 28 @@ -68,7 +68,7 @@ Container@PLAYER_WIDGETS: Y: 6 ImageCollection: order-icons Button@POWER_BUTTON: - Logic: AddRaceSuffixLogic + Logic: PowerdownOrderButtonLogic, AddRaceSuffixLogic X: 64 Width: 28 Height: 28 @@ -82,7 +82,7 @@ Container@PLAYER_WIDGETS: Y: 6 ImageCollection: order-icons Button@REPAIR_BUTTON: - Logic: AddRaceSuffixLogic + Logic: RepairOrderButtonLogic, AddRaceSuffixLogic X: 96 Width: 28 Height: 28 diff --git a/mods/ts/chrome/ingame-player.yaml b/mods/ts/chrome/ingame-player.yaml index 72eed29d42..b9a9ec4dc6 100644 --- a/mods/ts/chrome/ingame-player.yaml +++ b/mods/ts/chrome/ingame-player.yaml @@ -36,7 +36,7 @@ Container@PLAYER_WIDGETS: ClickThrough: false Children: Container@TOP_BUTTONS: - Logic: OrderButtonsChromeLogic + Logic: MenuButtonsChromeLogic X: 0 Y: 21 Children: @@ -58,7 +58,7 @@ Container@PLAYER_WIDGETS: ImageCollection: order-icons ImageName: debug Button@REPAIR_BUTTON: - Logic: AddRaceSuffixLogic + Logic: RepairOrderButtonLogic, AddRaceSuffixLogic X: 43 Width: 30 Height: 31 @@ -73,7 +73,7 @@ Container@PLAYER_WIDGETS: Y: 0 ImageCollection: order-icons Button@SELL_BUTTON: - Logic: AddRaceSuffixLogic + Logic: SellOrderButtonLogic, AddRaceSuffixLogic X: 73 Width: 30 Height: 31 @@ -88,7 +88,7 @@ Container@PLAYER_WIDGETS: Y: 0 ImageCollection: order-icons Button@BEACON_BUTTON: - Logic: AddRaceSuffixLogic + Logic: BeaconOrderButtonLogic, AddRaceSuffixLogic X: 103 Width: 30 Height: 31 @@ -103,7 +103,7 @@ Container@PLAYER_WIDGETS: Y: 0 ImageCollection: order-icons Button@POWER_BUTTON: - Logic: AddRaceSuffixLogic + Logic: PowerdownOrderButtonLogic, AddRaceSuffixLogic X: 133 Width: 30 Height: 31