Split the last TD UI logic into reuseable chunks.

This commit is contained in:
Paul Chote
2014-07-13 12:07:07 +12:00
parent ef00411931
commit e7ae615ac9
14 changed files with 313 additions and 138 deletions

View File

@@ -0,0 +1,28 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc.Widgets.Logic
{
public class IngameCashCounterLogic
{
[ObjectCreator.UseCtor]
public IngameCashCounterLogic(Widget widget, World world)
{
var playerResources = world.LocalPlayer.PlayerActor.Trait<PlayerResources>();
var cash = widget.Get<LabelWidget>("CASH");
var label = cash.Text;
cash.GetText = () => label.F(playerResources.DisplayCash + playerResources.DisplayResources);
}
}
}

View File

@@ -0,0 +1,38 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Drawing;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic
{
public class IngamePowerBarLogic
{
[ObjectCreator.UseCtor]
public IngamePowerBarLogic(Widget widget, World world)
{
var powerManager = world.LocalPlayer.PlayerActor.Trait<PowerManager>();
var powerBar = widget.Get<ResourceBarWidget>("POWERBAR");
powerBar.GetProvided = () => powerManager.PowerProvided;
powerBar.GetUsed = () => powerManager.PowerDrained;
powerBar.TooltipFormat = "Power Usage: {0}/{1}";
powerBar.GetBarColor = () =>
{
if (powerManager.PowerState == PowerState.Critical)
return Color.Red;
if (powerManager.PowerState == PowerState.Low)
return Color.Orange;
return Color.LimeGreen;
};
}
}
}

View File

@@ -0,0 +1,27 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Mods.RA.Buildings;
using OpenRA.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic
{
public class IngamePowerCounterLogic
{
[ObjectCreator.UseCtor]
public IngamePowerCounterLogic(Widget widget, World world)
{
var powerManager = world.LocalPlayer.PlayerActor.Trait<PowerManager>();
var power = widget.Get<LabelWidget>("POWER");
power.GetText = () => powerManager.PowerProvided == 1000000 ? "inf" : powerManager.ExcessPower.ToString();
}
}
}

View File

@@ -0,0 +1,38 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Linq;
using OpenRA.Mods.RA.Widgets;
using OpenRA.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic
{
public class IngameRadarDisplayLogic
{
[ObjectCreator.UseCtor]
public IngameRadarDisplayLogic(Widget widget, World world)
{
var radarEnabled = false;
var cachedRadarEnabled = false;
widget.Get<RadarWidget>("RADAR_MINIMAP").IsEnabled = () => radarEnabled;
var ticker = widget.Get<LogicTickerWidget>("RADAR_TICKER");
ticker.OnTick = () =>
{
radarEnabled = world.ActorsWithTrait<ProvidesRadar>()
.Any(a => a.Actor.Owner == world.LocalPlayer && a.Trait.IsActive);
if (radarEnabled != cachedRadarEnabled)
Sound.PlayNotification(world.Map.Rules, null, "Sounds", radarEnabled ? "RadarUp" : "RadarDown", null);
cachedRadarEnabled = radarEnabled;
};
}
}
}

View File

@@ -0,0 +1,40 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Drawing;
using OpenRA.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic
{
public class IngameSiloBarLogic
{
[ObjectCreator.UseCtor]
public IngameSiloBarLogic(Widget widget, World world)
{
var playerResources = world.LocalPlayer.PlayerActor.Trait<PlayerResources>();
var siloBar = widget.Get<ResourceBarWidget>("SILOBAR");
siloBar.GetProvided = () => playerResources.ResourceCapacity;
siloBar.GetUsed = () => playerResources.Resources;
siloBar.TooltipFormat = "Silo Usage: {0}/{1}";
siloBar.GetBarColor = () =>
{
if (playerResources.Resources == playerResources.ResourceCapacity)
return Color.Red;
if (playerResources.Resources >= 0.8 * playerResources.ResourceCapacity)
return Color.Orange;
return Color.LimeGreen;
};
}
}
}

View File

@@ -0,0 +1,31 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic
{
public class LoadIngamePlayerOrObserverUILogic
{
[ObjectCreator.UseCtor]
public LoadIngamePlayerOrObserverUILogic(Widget widget, World world)
{
var ingameRoot = widget.Get("INGAME_ROOT");
var playerRoot = ingameRoot.Get("PLAYER_ROOT");
if (world.LocalPlayer == null)
Game.LoadWidget(world, "OBSERVER_WIDGETS", playerRoot, new WidgetArgs());
else
Game.LoadWidget(world, "PLAYER_WIDGETS", playerRoot, new WidgetArgs());
Game.LoadWidget(world, "CHAT_PANEL", ingameRoot, new WidgetArgs());
}
}
}

View File

@@ -42,6 +42,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
Widget optionsBG = null;
optionsBG = Game.LoadWidget(world, "INGAME_OPTIONS_BG", Ui.Root, new WidgetArgs
{
{ "transient", false },
{ "onExit", () =>
{
optionsBG.Visible = false;
@@ -52,6 +53,8 @@ namespace OpenRA.Mods.RA.Widgets.Logic
}
});
optionsBG.Visible = false;
gameRoot.Get<ButtonWidget>("INGAME_OPTIONS_BUTTON").OnClick = () =>
{
optionsBG.Visible ^= true;

View File

@@ -17,7 +17,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
class IngameMenuLogic
{
[ObjectCreator.UseCtor]
public IngameMenuLogic(Widget widget, World world, Action onExit, WorldRenderer worldRenderer)
public IngameMenuLogic(Widget widget, World world, Action onExit, WorldRenderer worldRenderer, bool transient)
{
var resumeDisabled = false;
var mpe = world.WorldActor.TraitOrDefault<MenuPaletteEffect>();
@@ -71,8 +71,17 @@ namespace OpenRA.Mods.RA.Widgets.Logic
};
var resumeButton = widget.Get<ButtonWidget>("RESUME");
resumeButton.OnClick = () => onExit();
resumeButton.IsDisabled = () => resumeDisabled;
resumeButton.OnClick = () =>
{
if (transient)
{
Ui.CloseWindow();
Ui.Root.RemoveChild(widget);
}
onExit();
};
widget.Get<ButtonWidget>("SURRENDER").OnClick = () =>
{

View File

@@ -15,9 +15,17 @@ namespace OpenRA.Mods.RA.Widgets.Logic
{
public class OrderButtonsChromeLogic
{
readonly World world;
readonly Widget ingameRoot;
bool disableSystemButtons;
[ObjectCreator.UseCtor]
public OrderButtonsChromeLogic(Widget widget, World world)
{
this.world = world;
ingameRoot = Ui.Root.Get("INGAME_ROOT");
// Order Buttons
var sell = widget.GetOrNull<ButtonWidget>("SELL_BUTTON");
if (sell != null)
{
@@ -45,6 +53,43 @@ namespace OpenRA.Mods.RA.Widgets.Logic
power.GetKey = _ => Game.Settings.Keys.PowerDownKey;
BindOrderButton<PowerDownOrderGenerator>(world, power, "power");
}
// System buttons
var options = widget.GetOrNull<MenuButtonWidget>("OPTIONS_BUTTON");
if (options != null)
{
options.IsDisabled = () => disableSystemButtons;
options.OnClick = () => OpenMenuPanel(options);
}
}
void OpenMenuPanel(MenuButtonWidget button)
{
disableSystemButtons = true;
var cachedPause = world.PredictedPaused;
if (button.HideIngameUI)
ingameRoot.IsVisible = () => false;
if (button.Pause && world.LobbyInfo.IsSinglePlayer)
world.SetPauseState(true);
Game.LoadWidget(world, button.MenuContainer, Ui.Root, new WidgetArgs()
{
{ "transient", true },
{ "onExit", () =>
{
if (button.HideIngameUI)
ingameRoot.IsVisible = () => true;
if (button.Pause && world.LobbyInfo.IsSinglePlayer)
world.SetPauseState(cachedPause);
disableSystemButtons = false;
}
}
});
}
static void BindOrderButton<T>(World world, ButtonWidget w, string icon)