Skip to content

Commit

Permalink
Added widget showing the army for players in spec
Browse files Browse the repository at this point in the history
  • Loading branch information
teinarss committed Oct 20, 2019
1 parent 060ea80 commit 5e3d163
Show file tree
Hide file tree
Showing 11 changed files with 713 additions and 1 deletion.
16 changes: 16 additions & 0 deletions OpenRA.Mods.Common/Traits/Player/PlayerStatistics.cs
Expand Up @@ -10,6 +10,7 @@
#endregion

using System.Collections.Generic;
using OpenRA;
using OpenRA.Graphics;
using OpenRA.Traits;

Expand Down Expand Up @@ -64,6 +65,7 @@ public int Experience

bool armyGraphDisabled;
bool incomeGraphDisabled;
public Dictionary<string, Unit> Units = new Dictionary<string, Unit>();

public PlayerStatistics(Actor self) { }

Expand Down Expand Up @@ -151,6 +153,12 @@ public void WorldLoaded(World w, WorldRenderer wr)
}
}

public class Unit
{
public int Count { get; set; }
public string Name { get; set; }
}

[Desc("Attach this to a unit to update observer stats.")]
public class UpdatesPlayerStatisticsInfo : ITraitInfo
{
Expand Down Expand Up @@ -199,14 +207,19 @@ void INotifyKilled.Killed(Actor self, AttackInfo e)
{
defenderStats.ArmyValue -= cost;
includedInArmyValue = false;
playerStats.Units[self.Info.Name].Count--;
}
}

void INotifyCreated.Created(Actor self)
{
includedInArmyValue = info.AddToArmyValue;

if (includedInArmyValue)
{
playerStats.ArmyValue += cost;
playerStats.Units.GetOrAdd(self.Info.Name).Count++;
}
}

void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
Expand All @@ -216,6 +229,8 @@ void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newO
{
playerStats.ArmyValue -= cost;
newOwnerStats.ArmyValue += cost;
playerStats.Units[self.Info.Name].Count--;
newOwnerStats.Units.GetOrAdd(self.Info.Name).Count++;
}

playerStats = newOwnerStats;
Expand All @@ -227,6 +242,7 @@ void INotifyActorDisposing.Disposing(Actor self)
{
playerStats.ArmyValue -= cost;
includedInArmyValue = false;
playerStats.Units[self.Info.Name].Count--;
}
}
}
Expand Down
70 changes: 70 additions & 0 deletions OpenRA.Mods.Common/Widgets/Logic/Ingame/ArmyTooltipLogic.cs
@@ -0,0 +1,70 @@
#region Copyright & License Information
/*
* Copyright 2007-2019 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<ActorInfo> 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];

ActorInfo lastActor = null;
var descLabelPadding = descLabel.Bounds.Height;

tooltipContainer.BeforeRender = () =>
{
var actor = getTooltipUnit();
if (actor == null)
return;
if (actor == lastActor)
return;
var tooltip = actor.TraitInfos<TooltipInfo>().FirstOrDefault(info => info.EnabledByDefault);
var name = tooltip != null ? tooltip.Name : actor.Name;
var buildable = actor.TraitInfo<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 = new[] { nameSize.X, descSize.X }.Aggregate(Math.Max);
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;
lastActor = actor;
};
}
}
}
29 changes: 28 additions & 1 deletion OpenRA.Mods.Common/Widgets/Logic/Ingame/ObserverStatsLogic.cs
Expand Up @@ -21,7 +21,7 @@

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, Army, Combat, Graph, ArmyGraph }

[ChromeLogicArgsHotkeys("StatisticsBasicKey", "StatisticsEconomyKey", "StatisticsProductionKey", "StatisticsSupportPowersKey", "StatisticsCombatKey", "StatisticsGraphKey",
"StatisticsArmyGraphKey")]
Expand All @@ -32,11 +32,13 @@ public class ObserverStatsLogic : ChromeLogic
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;
Expand Down Expand Up @@ -72,6 +74,7 @@ public ObserverStatsLogic(World world, ModData modData, WorldRenderer worldRende
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");
Expand All @@ -93,6 +96,7 @@ public ObserverStatsLogic(World world, ModData modData, WorldRenderer worldRende
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");
Expand Down Expand Up @@ -143,6 +147,7 @@ public ObserverStatsLogic(World world, ModData modData, WorldRenderer worldRende
createStatsOption("Economy", ObserverStatsPanel.Economy, economyPlayerTemplate, () => DisplayStats(EconomyStats)),
createStatsOption("Production", ObserverStatsPanel.Production, productionPlayerTemplate, () => DisplayStats(ProductionStats)),
createStatsOption("Support Powers", ObserverStatsPanel.SupportPowers, supportPowersPlayerTemplate, () => DisplayStats(SupportPowerStats)),
createStatsOption("Army", ObserverStatsPanel.Army, armyPlayerTemplate, () => DisplayStats(ArmyStats)),
createStatsOption("Combat", ObserverStatsPanel.Combat, combatPlayerTemplate, () => DisplayStats(CombatStats)),
createStatsOption("Earnings (graph)", ObserverStatsPanel.Graph, null, () => IncomeGraph()),
createStatsOption("Army (graph)", ObserverStatsPanel.ArmyGraph, null, () => ArmyValueGraph()),
Expand Down Expand Up @@ -190,6 +195,7 @@ void ClearStats()
economyStatsHeaders.Visible = false;
productionStatsHeaders.Visible = false;
supportPowerStatsHeaders.Visible = false;
armyHeaders.Visible = false;
combatStatsHeaders.Visible = false;

incomeGraphContainer.Visible = false;
Expand Down Expand Up @@ -343,6 +349,27 @@ ScrollItemWidget SupportPowerStats(Player player)
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;
Expand Down

0 comments on commit 5e3d163

Please sign in to comment.