Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added widget showing the army for players in spec
- Loading branch information
Showing
11 changed files
with
713 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
OpenRA.Mods.Common/Widgets/Logic/Ingame/ArmyTooltipLogic.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.