Added widget showing the army for players in spec

This commit is contained in:
teinarss
2019-06-18 18:44:28 +02:00
committed by abcdefg30
parent 6ab0ace9e1
commit b81ede2d64
14 changed files with 754 additions and 10 deletions

View File

@@ -0,0 +1,67 @@
#region Copyright & License Information
/*
* Copyright 2007-2020 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, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
{
public class ArmyTooltipLogic : ChromeLogic
{
[ObjectCreator.UseCtor]
public ArmyTooltipLogic(Widget widget, TooltipContainerWidget tooltipContainer, Func<ArmyUnit> getTooltipUnit)
{
widget.IsVisible = () => getTooltipUnit() != null;
var nameLabel = widget.Get<LabelWidget>("NAME");
var descLabel = widget.Get<LabelWidget>("DESC");
var font = Game.Renderer.Fonts[nameLabel.Font];
var descFont = Game.Renderer.Fonts[descLabel.Font];
ArmyUnit lastArmyUnit = null;
var descLabelPadding = descLabel.Bounds.Height;
tooltipContainer.BeforeRender = () =>
{
var armyUnit = getTooltipUnit();
if (armyUnit == null || armyUnit == lastArmyUnit)
return;
var tooltip = armyUnit.TooltipInfo;
var name = tooltip != null ? tooltip.Name : armyUnit.ActorInfo.Name;
var buildable = armyUnit.BuildableInfo;
nameLabel.Text = name;
var nameSize = font.Measure(name);
descLabel.Text = buildable.Description.Replace("\\n", "\n");
var descSize = descFont.Measure(descLabel.Text);
descLabel.Bounds.Width = descSize.X;
descLabel.Bounds.Height = descSize.Y + descLabelPadding;
var leftWidth = Math.Max(nameSize.X, descSize.X);
widget.Bounds.Width = leftWidth + 2 * nameLabel.Bounds.X;
// Set the bottom margin to match the left margin
var leftHeight = descLabel.Bounds.Bottom + descLabel.Bounds.X;
widget.Bounds.Height = leftHeight;
lastArmyUnit = armyUnit;
};
}
}
}

View File

@@ -21,9 +21,9 @@ using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
{
public enum ObserverStatsPanel { None, Basic, Economy, Production, SupportPowers, Combat, Graph, ArmyGraph }
public enum ObserverStatsPanel { None, Basic, Economy, Production, SupportPowers, Combat, Army, Graph, ArmyGraph }
[ChromeLogicArgsHotkeys("StatisticsBasicKey", "StatisticsEconomyKey", "StatisticsProductionKey", "StatisticsSupportPowersKey", "StatisticsCombatKey", "StatisticsGraphKey",
[ChromeLogicArgsHotkeys("StatisticsBasicKey", "StatisticsEconomyKey", "StatisticsProductionKey", "StatisticsSupportPowersKey", "StatisticsCombatKey", "StatisticsArmyKey", "StatisticsGraphKey",
"StatisticsArmyGraphKey")]
public class ObserverStatsLogic : ChromeLogic
{
@@ -32,11 +32,13 @@ namespace OpenRA.Mods.Common.Widgets.Logic
readonly ContainerWidget productionStatsHeaders;
readonly ContainerWidget supportPowerStatsHeaders;
readonly ContainerWidget combatStatsHeaders;
readonly ContainerWidget armyHeaders;
readonly ScrollPanelWidget playerStatsPanel;
readonly ScrollItemWidget basicPlayerTemplate;
readonly ScrollItemWidget economyPlayerTemplate;
readonly ScrollItemWidget productionPlayerTemplate;
readonly ScrollItemWidget supportPowersPlayerTemplate;
readonly ScrollItemWidget armyPlayerTemplate;
readonly ScrollItemWidget combatPlayerTemplate;
readonly ContainerWidget incomeGraphContainer;
readonly ContainerWidget armyValueGraphContainer;
@@ -72,6 +74,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
economyStatsHeaders = widget.Get<ContainerWidget>("ECONOMY_STATS_HEADERS");
productionStatsHeaders = widget.Get<ContainerWidget>("PRODUCTION_STATS_HEADERS");
supportPowerStatsHeaders = widget.Get<ContainerWidget>("SUPPORT_POWERS_HEADERS");
armyHeaders = widget.Get<ContainerWidget>("ARMY_HEADERS");
combatStatsHeaders = widget.Get<ContainerWidget>("COMBAT_STATS_HEADERS");
playerStatsPanel = widget.Get<ScrollPanelWidget>("PLAYER_STATS_PANEL");
@@ -87,12 +90,14 @@ namespace OpenRA.Mods.Common.Widgets.Logic
AdjustHeader(productionStatsHeaders);
AdjustHeader(supportPowerStatsHeaders);
AdjustHeader(combatStatsHeaders);
AdjustHeader(armyHeaders);
}
basicPlayerTemplate = playerStatsPanel.Get<ScrollItemWidget>("BASIC_PLAYER_TEMPLATE");
economyPlayerTemplate = playerStatsPanel.Get<ScrollItemWidget>("ECONOMY_PLAYER_TEMPLATE");
productionPlayerTemplate = playerStatsPanel.Get<ScrollItemWidget>("PRODUCTION_PLAYER_TEMPLATE");
supportPowersPlayerTemplate = playerStatsPanel.Get<ScrollItemWidget>("SUPPORT_POWERS_PLAYER_TEMPLATE");
armyPlayerTemplate = playerStatsPanel.Get<ScrollItemWidget>("ARMY_PLAYER_TEMPLATE");
combatPlayerTemplate = playerStatsPanel.Get<ScrollItemWidget>("COMBAT_PLAYER_TEMPLATE");
incomeGraphContainer = widget.Get<ContainerWidget>("INCOME_GRAPH_CONTAINER");
@@ -144,6 +149,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
createStatsOption("Production", ObserverStatsPanel.Production, productionPlayerTemplate, () => DisplayStats(ProductionStats)),
createStatsOption("Support Powers", ObserverStatsPanel.SupportPowers, supportPowersPlayerTemplate, () => DisplayStats(SupportPowerStats)),
createStatsOption("Combat", ObserverStatsPanel.Combat, combatPlayerTemplate, () => DisplayStats(CombatStats)),
createStatsOption("Army", ObserverStatsPanel.Army, armyPlayerTemplate, () => DisplayStats(ArmyStats)),
createStatsOption("Earnings (graph)", ObserverStatsPanel.Graph, null, () => IncomeGraph()),
createStatsOption("Army (graph)", ObserverStatsPanel.ArmyGraph, null, () => ArmyValueGraph()),
};
@@ -157,7 +163,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var statsDropDownPanelTemplate = logicArgs.TryGetValue("StatsDropDownPanelTemplate", out yaml) ? yaml.Value : "LABEL_DROPDOWN_TEMPLATE";
statsDropDown.OnMouseDown = _ => statsDropDown.ShowDropDown(statsDropDownPanelTemplate, 205, statsDropDownOptions, setupItem);
statsDropDown.OnMouseDown = _ => statsDropDown.ShowDropDown(statsDropDownPanelTemplate, 230, statsDropDownOptions, setupItem);
statsDropDownOptions[0].OnClick();
var keyListener = statsDropDown.Get<LogicKeyListenerWidget>("STATS_DROPDOWN_KEYHANDLER");
@@ -190,6 +196,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
economyStatsHeaders.Visible = false;
productionStatsHeaders.Visible = false;
supportPowerStatsHeaders.Visible = false;
armyHeaders.Visible = false;
combatStatsHeaders.Visible = false;
incomeGraphContainer.Visible = false;
@@ -343,6 +350,27 @@ namespace OpenRA.Mods.Common.Widgets.Logic
return template;
}
ScrollItemWidget ArmyStats(Player player)
{
armyHeaders.Visible = true;
var template = SetupPlayerScrollItemWidget(armyPlayerTemplate, player);
AddPlayerFlagAndName(template, player);
var playerName = template.Get<LabelWidget>("PLAYER");
playerName.GetColor = () => Color.White;
var playerColor = template.Get<ColorBlockWidget>("PLAYER_COLOR");
var playerGradient = template.Get<GradientColorBlockWidget>("PLAYER_GRADIENT");
SetupPlayerColor(player, template, playerColor, playerGradient);
template.Get<ObserverArmyIconsWidget>("ARMY_ICONS").GetPlayer = () => player;
template.IgnoreChildMouseOver = false;
return template;
}
ScrollItemWidget EconomyStats(Player player)
{
economyStatsHeaders.Visible = true;

View File

@@ -0,0 +1,209 @@
#region Copyright & License Information
/*
* Copyright 2007-2020 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, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets
{
public class ObserverArmyIconsWidget : Widget
{
public Func<Player> GetPlayer;
readonly World world;
readonly WorldRenderer worldRenderer;
public int IconWidth = 32;
public int IconHeight = 24;
public int IconSpacing = 1;
float2 iconSize;
public int MinWidth = 240;
public ArmyUnit TooltipUnit { get; private set; }
public Func<ArmyUnit> GetTooltipUnit;
public readonly string TooltipTemplate = "ARMY_TOOLTIP";
public readonly string TooltipContainer;
readonly Lazy<TooltipContainerWidget> tooltipContainer;
readonly List<ArmyIcon> armyIcons = new List<ArmyIcon>();
readonly CachedTransform<Player, PlayerStatistics> stats = new CachedTransform<Player, PlayerStatistics>(player => player.PlayerActor.TraitOrDefault<PlayerStatistics>());
int lastIconIdx;
Rectangle currentIconBounds;
[ObjectCreator.UseCtor]
public ObserverArmyIconsWidget(World world, WorldRenderer worldRenderer)
{
this.world = world;
this.worldRenderer = worldRenderer;
GetTooltipUnit = () => TooltipUnit;
tooltipContainer = Exts.Lazy(() =>
Ui.Root.Get<TooltipContainerWidget>(TooltipContainer));
}
protected ObserverArmyIconsWidget(ObserverArmyIconsWidget other)
: base(other)
{
GetPlayer = other.GetPlayer;
world = other.world;
worldRenderer = other.worldRenderer;
IconWidth = other.IconWidth;
IconHeight = other.IconHeight;
IconSpacing = other.IconSpacing;
iconSize = new float2(IconWidth, IconHeight);
MinWidth = other.MinWidth;
TooltipUnit = other.TooltipUnit;
GetTooltipUnit = () => TooltipUnit;
TooltipTemplate = other.TooltipTemplate;
TooltipContainer = other.TooltipContainer;
tooltipContainer = Exts.Lazy(() =>
Ui.Root.Get<TooltipContainerWidget>(TooltipContainer));
}
public override void Draw()
{
armyIcons.Clear();
var player = GetPlayer();
if (player == null)
return;
var playerStatistics = stats.Update(player);
var items = playerStatistics.Units.Values
.Where(u => u.Count > 0 && u.Icon != null)
.OrderBy(u => u.ProductionQueueOrder)
.ThenBy(u => u.BuildPaletteOrder);
Game.Renderer.EnableAntialiasingFilter();
var queueCol = 0;
foreach (var unit in items)
{
var icon = unit.Icon;
var topLeftOffset = new int2(queueCol * (IconWidth + IconSpacing), 0);
var iconTopLeft = RenderOrigin + topLeftOffset;
var centerPosition = iconTopLeft;
WidgetUtils.DrawSHPCentered(icon.Image, centerPosition + 0.5f * iconSize, worldRenderer.Palette(unit.IconPalette), 0.5f);
armyIcons.Add(new ArmyIcon
{
Bounds = new Rectangle(iconTopLeft.X, iconTopLeft.Y, (int)iconSize.X, (int)iconSize.Y),
Unit = unit
});
queueCol++;
}
Bounds.Width = queueCol * (IconWidth + IconSpacing);
Game.Renderer.DisableAntialiasingFilter();
var bold = Game.Renderer.Fonts["TinyBold"];
foreach (var armyIcon in armyIcons)
{
var text = armyIcon.Unit.Count.ToString();
bold.DrawTextWithContrast(text, armyIcon.Bounds.Location + new float2(iconSize.X, 0) - new float2(bold.Measure(text).X, bold.TopOffset),
Color.White, Color.Black, 1);
}
var parentWidth = Bounds.X + Bounds.Width;
Parent.Bounds.Width = parentWidth;
var gradient = Parent.Get<GradientColorBlockWidget>("PLAYER_GRADIENT");
var offset = gradient.Bounds.X - Bounds.X;
var gradientWidth = Math.Max(MinWidth - offset, queueCol * (IconWidth + IconSpacing));
gradient.Bounds.Width = gradientWidth;
var widestChildWidth = Parent.Parent.Children.Max(x => x.Bounds.Width);
Parent.Parent.Bounds.Width = Math.Max(25 + widestChildWidth, Bounds.Left + MinWidth);
}
public override Widget Clone()
{
return new ObserverArmyIconsWidget(this);
}
public override void MouseEntered()
{
if (TooltipContainer == null)
return;
foreach (var armyIcon in armyIcons)
{
if (!armyIcon.Bounds.Contains(Viewport.LastMousePos))
continue;
TooltipUnit = armyIcon.Unit;
break;
}
tooltipContainer.Value.SetTooltip(TooltipTemplate, new WidgetArgs { { "getTooltipUnit", GetTooltipUnit } });
}
public override void MouseExited()
{
if (TooltipContainer == null)
return;
tooltipContainer.Value.RemoveTooltip();
}
public override void Tick()
{
if (lastIconIdx >= armyIcons.Count)
{
TooltipUnit = null;
return;
}
if (TooltipUnit != null && currentIconBounds.Contains(Viewport.LastMousePos))
return;
for (var i = 0; i < armyIcons.Count; i++)
{
var armyIcon = armyIcons[i];
if (!armyIcon.Bounds.Contains(Viewport.LastMousePos))
continue;
lastIconIdx = i;
currentIconBounds = armyIcon.Bounds;
TooltipUnit = armyIcon.Unit;
return;
}
TooltipUnit = null;
}
class ArmyIcon
{
public Rectangle Bounds { get; set; }
public ArmyUnit Unit { get; set; }
}
}
}