Move SupportPowersWidget into RA and generalize.

This commit is contained in:
Paul Chote
2014-07-16 16:14:24 +12:00
parent ae9a3075a9
commit 17d601113e
6 changed files with 123 additions and 25 deletions

View File

@@ -79,8 +79,6 @@
<Compile Include="Widgets\Logic\CncConquestObjectivesLogic.cs" /> <Compile Include="Widgets\Logic\CncConquestObjectivesLogic.cs" />
<Compile Include="Widgets\Logic\CncIngameMenuLogic.cs" /> <Compile Include="Widgets\Logic\CncIngameMenuLogic.cs" />
<Compile Include="Widgets\Logic\CncMainMenuLogic.cs" /> <Compile Include="Widgets\Logic\CncMainMenuLogic.cs" />
<Compile Include="Widgets\Logic\SupportPowerTooltipLogic.cs" />
<Compile Include="Widgets\SupportPowersWidget.cs" />
<Compile Include="WithFire.cs" /> <Compile Include="WithFire.cs" />
<Compile Include="WithRoof.cs" /> <Compile Include="WithRoof.cs" />
<Compile Include="Widgets\Logic\ProductionTabsLogic.cs" /> <Compile Include="Widgets\Logic\ProductionTabsLogic.cs" />

View File

@@ -540,6 +540,9 @@
<Compile Include="Widgets\Logic\Ingame\IngamePowerBarLogic.cs" /> <Compile Include="Widgets\Logic\Ingame\IngamePowerBarLogic.cs" />
<Compile Include="Widgets\Logic\Ingame\IngameSiloBarLogic.cs" /> <Compile Include="Widgets\Logic\Ingame\IngameSiloBarLogic.cs" />
<Compile Include="Widgets\Logic\Ingame\AddRaceSuffixLogic.cs" /> <Compile Include="Widgets\Logic\Ingame\AddRaceSuffixLogic.cs" />
<Compile Include="Widgets\SupportPowersWidget.cs" />
<Compile Include="Widgets\Logic\SupportPowerTooltipLogic.cs" />
<Compile Include="Widgets\Logic\SupportPowerBinLogic.cs" />
<Compile Include="Widgets\Logic\Ingame\LoadIngamePlayerOrObserverUILogic.cs" /> <Compile Include="Widgets\Logic\Ingame\LoadIngamePlayerOrObserverUILogic.cs" />
<Compile Include="Widgets\MenuButtonWidget.cs" /> <Compile Include="Widgets\MenuButtonWidget.cs" />
<Compile Include="Widgets\Logic\DebugMenuLogic.cs" /> <Compile Include="Widgets\Logic\DebugMenuLogic.cs" />

View File

@@ -0,0 +1,72 @@
#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;
using OpenRA.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic
{
public class SupportPowerBinLogic
{
[ObjectCreator.UseCtor]
public SupportPowerBinLogic(Widget widget, World world)
{
var palette = widget.Get<SupportPowersWidget>("SUPPORT_PALETTE");
var background = widget.GetOrNull("PALETTE_BACKGROUND");
var foreground = widget.GetOrNull("PALETTE_FOREGROUND");
if (background != null || foreground != null)
{
Widget backgroundTemplate = null;
Widget foregroundTemplate = null;
if (background != null)
backgroundTemplate = background.Get("ICON_TEMPLATE");
if (foreground != null)
foregroundTemplate = foreground.Get("ICON_TEMPLATE");
Action<int, int> updateBackground = (_, icons) =>
{
var rowHeight = palette.IconSize.Y + palette.IconMargin;
if (background != null)
{
background.RemoveChildren();
for (var i = 0; i < icons; i++)
{
var row = backgroundTemplate.Clone();
row.Bounds.Y += i * rowHeight;
background.AddChild(row);
}
}
if (foreground != null)
{
foreground.RemoveChildren();
for (var i = 0; i < icons; i++)
{
var row = foregroundTemplate.Clone();
row.Bounds.Y += i * rowHeight;
foreground.AddChild(row);
}
}
};
palette.OnIconCountChanged += updateBackground;
// Set the initial palette state
updateBackground(0, 0);
}
}
}
}

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS) * Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made * 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 * available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information, * as published by the Free Software Foundation. For more information,
@@ -12,7 +12,7 @@ using System;
using OpenRA.Mods.RA; using OpenRA.Mods.RA;
using OpenRA.Widgets; using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc.Widgets.Logic namespace OpenRA.Mods.RA.Widgets.Logic
{ {
public class SupportPowerTooltipLogic public class SupportPowerTooltipLogic
{ {

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS) * Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made * 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 * available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information, * as published by the Free Software Foundation. For more information,
@@ -16,17 +16,22 @@ using OpenRA.Graphics;
using OpenRA.Mods.RA; using OpenRA.Mods.RA;
using OpenRA.Widgets; using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc.Widgets namespace OpenRA.Mods.RA.Widgets
{ {
public class SupportPowersWidget : Widget public class SupportPowersWidget : Widget
{ {
[Translate] public readonly string ReadyText = ""; [Translate] public readonly string ReadyText = "";
[Translate] public readonly string HoldText = ""; [Translate] public readonly string HoldText = "";
public readonly int2 IconSize = new int2(64, 48);
public readonly int IconMargin = 10;
public readonly int2 IconSpriteOffset = int2.Zero;
public readonly string TooltipContainer; public readonly string TooltipContainer;
public readonly string TooltipTemplate = "SUPPORT_POWER_TOOLTIP"; public readonly string TooltipTemplate = "SUPPORT_POWER_TOOLTIP";
public int Spacing = 10; public int IconCount { get; private set; }
public event Action<int, int> OnIconCountChanged = (a, b) => {};
readonly WorldRenderer worldRenderer; readonly WorldRenderer worldRenderer;
readonly SupportPowerManager spm; readonly SupportPowerManager spm;
@@ -67,12 +72,15 @@ namespace OpenRA.Mods.Cnc.Widgets
icons = new Dictionary<Rectangle, SupportPowerIcon>(); icons = new Dictionary<Rectangle, SupportPowerIcon>();
var powers = spm.Powers.Values.Where(p => !p.Disabled); var powers = spm.Powers.Values.Where(p => !p.Disabled);
var i = 0; var oldIconCount = IconCount;
IconCount = 0;
var rb = RenderBounds; var rb = RenderBounds;
foreach (var p in powers) foreach (var p in powers)
{ {
var rect = new Rectangle(rb.X + 1, rb.Y + i * (48 + Spacing) + 1, 64, 48); var rect = new Rectangle(rb.X, rb.Y + IconCount * (IconSize.Y + IconMargin), IconSize.X, IconSize.Y);
icon.Play(p.Info.Icon); icon.Play(p.Info.Icon);
var power = new SupportPowerIcon() var power = new SupportPowerIcon()
{ {
Power = p, Power = p,
@@ -81,26 +89,24 @@ namespace OpenRA.Mods.Cnc.Widgets
}; };
icons.Add(rect, power); icons.Add(rect, power);
i++; IconCount++;
} }
eventBounds = (icons.Count == 0) ? Rectangle.Empty : icons.Keys.Aggregate(Rectangle.Union); eventBounds = icons.Any() ? icons.Keys.Aggregate(Rectangle.Union) : Rectangle.Empty;
if (oldIconCount != IconCount)
OnIconCountChanged(oldIconCount, IconCount);
} }
public override void Draw() public override void Draw()
{ {
var iconSize = new float2(64, 48); var iconOffset = 0.5f * IconSize.ToFloat2() + IconSpriteOffset;
var iconOffset = 0.5f * iconSize;
overlayFont = Game.Renderer.Fonts["TinyBold"]; overlayFont = Game.Renderer.Fonts["TinyBold"];
holdOffset = iconOffset - overlayFont.Measure(HoldText) / 2; holdOffset = iconOffset - overlayFont.Measure(HoldText) / 2;
readyOffset = iconOffset - overlayFont.Measure(ReadyText) / 2; readyOffset = iconOffset - overlayFont.Measure(ReadyText) / 2;
timeOffset = iconOffset - overlayFont.Measure(WidgetUtils.FormatTime(0)) / 2; timeOffset = iconOffset - overlayFont.Measure(WidgetUtils.FormatTime(0)) / 2;
// Background
foreach (var rect in icons.Keys)
WidgetUtils.DrawPanel("panel-black", rect.InflateBy(1, 1, 1, 1));
// Icons // Icons
foreach (var p in icons.Values) foreach (var p in icons.Values)
{ {
@@ -110,6 +116,7 @@ namespace OpenRA.Mods.Cnc.Widgets
clock.PlayFetchIndex("idle", clock.PlayFetchIndex("idle",
() => (p.Power.TotalTime - p.Power.RemainingTime) () => (p.Power.TotalTime - p.Power.RemainingTime)
* (clock.CurrentSequence.Length - 1) / p.Power.TotalTime); * (clock.CurrentSequence.Length - 1) / p.Power.TotalTime);
clock.Tick(); clock.Tick();
WidgetUtils.DrawSHPCentered(clock.Image, p.Pos + iconOffset, worldRenderer); WidgetUtils.DrawSHPCentered(clock.Image, p.Pos + iconOffset, worldRenderer);
} }
@@ -140,14 +147,18 @@ namespace OpenRA.Mods.Cnc.Widgets
public override void MouseEntered() public override void MouseEntered()
{ {
if (TooltipContainer == null) return; if (TooltipContainer == null)
return;
tooltipContainer.Value.SetTooltip(TooltipTemplate, tooltipContainer.Value.SetTooltip(TooltipTemplate,
new WidgetArgs() { { "palette", this } }); new WidgetArgs() { { "palette", this } });
} }
public override void MouseExited() public override void MouseExited()
{ {
if (TooltipContainer == null) return; if (TooltipContainer == null)
return;
tooltipContainer.Value.RemoveTooltip(); tooltipContainer.Value.RemoveTooltip();
} }
@@ -157,7 +168,8 @@ namespace OpenRA.Mods.Cnc.Widgets
{ {
var icon = icons.Where(i => i.Key.Contains(mi.Location)) var icon = icons.Where(i => i.Key.Contains(mi.Location))
.Select(i => i.Value).FirstOrDefault(); .Select(i => i.Value).FirstOrDefault();
TooltipPower = (icon != null) ? icon.Power : null;
TooltipPower = icon != null ? icon.Power : null;
return false; return false;
} }
@@ -171,6 +183,7 @@ namespace OpenRA.Mods.Cnc.Widgets
{ {
if (!clicked.Power.Active) if (!clicked.Power.Active)
Sound.PlayToPlayer(spm.self.Owner, clicked.Power.Info.InsufficientPowerSound); Sound.PlayToPlayer(spm.self.Owner, clicked.Power.Info.InsufficientPowerSound);
spm.Target(clicked.Power.Info.OrderName); spm.Target(clicked.Power.Info.OrderName);
} }

View File

@@ -182,12 +182,24 @@ Container@PLAYER_WIDGETS:
WorldCommand: WorldCommand:
Width: WINDOW_RIGHT Width: WINDOW_RIGHT
Height: WINDOW_BOTTOM Height: WINDOW_BOTTOM
SupportPowers: Container@SUPPORT_POWERS:
TooltipContainer: TOOLTIP_CONTAINER Logic: SupportPowerBinLogic
X: 10 X: 10
Y: 10 Y: 10
ReadyText: Ready Children:
HoldText: On Hold Container@PALETTE_BACKGROUND:
Children:
Background@ICON_TEMPLATE:
Width: 66
Height: 50
ClickThrough: false
Background: panel-black
SupportPowers@SUPPORT_PALETTE:
X: 1
Y: 1
TooltipContainer: TOOLTIP_CONTAINER
ReadyText: Ready
HoldText: On Hold
Background@SIDEBAR_BACKGROUND: Background@SIDEBAR_BACKGROUND:
Logic: OrderButtonsChromeLogic Logic: OrderButtonsChromeLogic
X: WINDOW_RIGHT - 204 X: WINDOW_RIGHT - 204