Merge pull request #11565 from Mailaender/withtextdecoration

Added WithTextDecoration
This commit is contained in:
reaperrr
2016-07-04 18:37:28 +02:00
committed by GitHub
10 changed files with 180 additions and 52 deletions

View File

@@ -439,6 +439,7 @@
<Compile Include="Traits\Render\WithDamageOverlay.cs" />
<Compile Include="Traits\Render\WithDeathAnimation.cs" />
<Compile Include="Traits\Render\WithDecoration.cs" />
<Compile Include="Traits\Render\WithTextDecoration.cs" />
<Compile Include="Traits\Render\WithDockingAnimation.cs" />
<Compile Include="Traits\Render\WithDockedOverlay.cs" />
<Compile Include="Traits\Render\WithHarvestAnimation.cs" />

View File

@@ -47,7 +47,7 @@ namespace OpenRA.Mods.Common.Traits.Render
public readonly int ZOffset = 1;
[Desc("Player stances who can view the decoration.")]
public readonly Stance Stances = Stance.Ally;
public readonly Stance ValidStances = Stance.Ally;
[Desc("Should this be visible only when selected?")]
public readonly bool RequiresSelection = false;
@@ -91,7 +91,7 @@ namespace OpenRA.Mods.Common.Traits.Render
if (self.World.RenderPlayer != null)
{
var stance = self.Owner.Stances[self.World.RenderPlayer];
if (!Info.Stances.HasStance(stance))
if (!Info.ValidStances.HasStance(stance))
return Enumerable.Empty<IRenderable>();
}

View File

@@ -0,0 +1,133 @@
#region Copyright & License Information
/*
* Copyright 2007-2016 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.Drawing;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits.Render
{
[Desc("Displays a text overlay relative to the selection box.")]
public class WithTextDecorationInfo : UpgradableTraitInfo
{
[FieldLoader.Require] [Translate] public readonly string Text = null;
public readonly string Font = "TinyBold";
[Desc("Display in this color when not using the player color.")]
public readonly Color Color = Color.White;
[Desc("Use the player color of the current owner.")]
public readonly bool UsePlayerColor = false;
[Desc("Point in the actor's selection box used as reference for offsetting the decoration image. " +
"Possible values are combinations of Center, Top, Bottom, Left, Right.")]
public readonly ReferencePoints ReferencePoint = ReferencePoints.Top | ReferencePoints.Left;
[Desc("The Z offset to apply when rendering this decoration.")]
public readonly int ZOffset = 1;
[Desc("Player stances who can view the decoration.")]
public readonly Stance ValidStances = Stance.Ally;
[Desc("Should this be visible only when selected?")]
public readonly bool RequiresSelection = false;
public override object Create(ActorInitializer init) { return new WithTextDecoration(init.Self, this); }
}
public class WithTextDecoration : UpgradableTrait<WithTextDecorationInfo>, IRender, IPostRenderSelection, INotifyCapture
{
readonly Actor self;
readonly SpriteFont font;
Color color;
public WithTextDecoration(Actor self, WithTextDecorationInfo info)
: base(info)
{
this.self = self;
if (!Game.Renderer.Fonts.TryGetValue(info.Font, out font))
throw new YamlException("Could not find font '{0}'".F(info.Font));
color = Info.UsePlayerColor ? self.Owner.Color.RGB : Info.Color;
}
public virtual bool ShouldRender(Actor self) { return true; }
public IEnumerable<IRenderable> Render(Actor self, WorldRenderer wr)
{
return !Info.RequiresSelection ? RenderInner(self, wr) : Enumerable.Empty<IRenderable>();
}
public IEnumerable<IRenderable> RenderAfterWorld(WorldRenderer wr)
{
return Info.RequiresSelection ? RenderInner(self, wr) : Enumerable.Empty<IRenderable>();
}
IEnumerable<IRenderable> RenderInner(Actor self, WorldRenderer wr)
{
if (IsTraitDisabled || self.IsDead || !self.IsInWorld)
return Enumerable.Empty<IRenderable>();
if (self.World.RenderPlayer != null)
{
var stance = self.Owner.Stances[self.World.RenderPlayer];
if (!Info.ValidStances.HasStance(stance))
return Enumerable.Empty<IRenderable>();
}
if (!ShouldRender(self) || self.World.FogObscures(self))
return Enumerable.Empty<IRenderable>();
var bounds = self.VisualBounds;
var halfSize = font.Measure(Info.Text) / 2;
var boundsOffset = new int2(bounds.Left + bounds.Right, bounds.Top + bounds.Bottom) / 2;
var sizeOffset = new int2();
if (Info.ReferencePoint.HasFlag(ReferencePoints.Top))
{
boundsOffset -= new int2(0, bounds.Height / 2);
sizeOffset += new int2(0, halfSize.Y);
}
else if (Info.ReferencePoint.HasFlag(ReferencePoints.Bottom))
{
boundsOffset += new int2(0, bounds.Height / 2);
sizeOffset -= new int2(0, halfSize.Y);
}
if (Info.ReferencePoint.HasFlag(ReferencePoints.Left))
{
boundsOffset -= new int2(bounds.Width / 2, 0);
sizeOffset += new int2(halfSize.X, 0);
}
else if (Info.ReferencePoint.HasFlag(ReferencePoints.Right))
{
boundsOffset += new int2(bounds.Width / 2, 0);
sizeOffset -= new int2(halfSize.X, 0);
}
var screenPos = wr.ScreenPxPosition(self.CenterPosition) + boundsOffset + sizeOffset;
return new IRenderable[] { new TextRenderable(font, wr.ProjectedPosition(screenPos), Info.ZOffset, color, Info.Text) };
}
void INotifyCapture.OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner)
{
if (Info.UsePlayerColor)
color = newOwner.Color.RGB;
}
}
}

View File

@@ -212,6 +212,16 @@ namespace OpenRA.Mods.Common.UtilityCommands
}
}
if (engineVersion < 20160703)
{
if (node.Key.StartsWith("WithDecoration") || node.Key.StartsWith("WithRankDecoration") || node.Key.StartsWith("WithDecorationCarryable"))
{
var stancesNode = node.Value.Nodes.FirstOrDefault(n => n.Key == "Stances");
if (stancesNode != null)
stancesNode.Key = "ValidStances";
}
}
UpgradeActorRules(modData, engineVersion, ref node.Value.Nodes, node, depth + 1);
}

Binary file not shown.

View File

@@ -97,10 +97,9 @@ construction_yard:
ZOffset: 256
UpgradeTypes: stardecoration
UpgradeMinEnabledLevel: 1
WithDecoration@primary:
WithTextDecoration@primary:
RequiresSelection: true
Image: pips
Sequence: tag-primary
Text: PRIMARY
ReferencePoint: Top
ZOffset: 256
UpgradeTypes: primary
@@ -212,10 +211,9 @@ barracks:
ZOffset: 256
UpgradeTypes: stardecoration
UpgradeMinEnabledLevel: 1
WithDecoration@primary:
WithTextDecoration@primary:
RequiresSelection: true
Image: pips
Sequence: tag-primary
Text: PRIMARY
ReferencePoint: Top
ZOffset: 256
UpgradeTypes: primary
@@ -394,10 +392,9 @@ light_factory:
ZOffset: 256
UpgradeTypes: stardecoration
UpgradeMinEnabledLevel: 1
WithDecoration@primary:
WithTextDecoration@primary:
RequiresSelection: true
Image: pips
Sequence: tag-primary
Text: PRIMARY
ReferencePoint: Top
ZOffset: 256
UpgradeTypes: primary
@@ -476,10 +473,9 @@ heavy_factory:
ZOffset: 256
UpgradeTypes: stardecoration
UpgradeMinEnabledLevel: 1
WithDecoration@primary:
WithTextDecoration@primary:
RequiresSelection: true
Image: pips
Sequence: tag-primary
Text: PRIMARY
ReferencePoint: Top
ZOffset: 256
UpgradeTypes: primary
@@ -593,10 +589,9 @@ starport:
Power:
Amount: -150
ProvidesPrerequisite@buildingname:
WithDecoration@primary:
WithTextDecoration@primary:
RequiresSelection: true
Image: pips
Sequence: tag-primary
Text: PRIMARY
ReferencePoint: Top
ZOffset: 256
UpgradeTypes: primary

View File

@@ -126,8 +126,6 @@ pips:
Length: 10
pickup-indicator: DATA.R8
Start: 112
tag-primary: primary.shp
Offset: 0, 2
pip-empty: DATA.R8
Start: 15
pip-green: DATA.R8

View File

@@ -109,12 +109,11 @@ GAPILE:
ProvidesPrerequisite@buildingname:
SelectionDecorations:
VisualBounds: 88, 56, 0, -8
WithDecoration@primary:
WithTextDecoration@primary:
RequiresSelection: true
Image: pips
Sequence: tag-primary
Palette: pips
ReferencePoint: Center
Text: PRIMARY
ReferencePoint: Top
Color: E0D048
ZOffset: 256
UpgradeTypes: primary
UpgradeMinEnabledLevel: 1
@@ -171,12 +170,11 @@ GAWEAP:
ProvidesPrerequisite@buildingname:
SelectionDecorations:
VisualBounds: 154, 100, -2, -12
WithDecoration@primary:
WithTextDecoration@primary:
RequiresSelection: true
Image: pips
Sequence: tag-primary
Palette: pips
ReferencePoint: Center
Text: PRIMARY
ReferencePoint: Top
Color: E0D048
ZOffset: 256
UpgradeTypes: primary
UpgradeMinEnabledLevel: 1
@@ -224,12 +222,11 @@ GAHPAD:
ProvidesPrerequisite@buildingname:
SelectionDecorations:
VisualBounds: 88, 66, 0, -5
WithDecoration@primary:
WithTextDecoration@primary:
RequiresSelection: true
Image: pips
Sequence: tag-primary
Palette: pips
ReferencePoint: Center
Text: PRIMARY
ReferencePoint: Top
Color: E0D048
ZOffset: 256
UpgradeTypes: primary
UpgradeMinEnabledLevel: 1

View File

@@ -118,12 +118,11 @@ NAHAND:
ProvidesPrerequisite@buildingname:
SelectionDecorations:
VisualBounds: 116, 78, 3, -8
WithDecoration@primary:
WithTextDecoration@primary:
RequiresSelection: true
Image: pips
Sequence: tag-primary
Palette: pips
ReferencePoint: Center
Text: PRIMARY
ReferencePoint: Top
Color: E0D048
ZOffset: 256
UpgradeTypes: primary
UpgradeMinEnabledLevel: 1
@@ -176,12 +175,11 @@ NAWEAP:
ProvidesPrerequisite@buildingname:
SelectionDecorations:
VisualBounds: 149, 116, -3, -20
WithDecoration@primary:
WithTextDecoration@primary:
RequiresSelection: true
Image: pips
Sequence: tag-primary
Palette: pips
ReferencePoint: Center
Text: PRIMARY
ReferencePoint: Top
Color: E0D048
ZOffset: 256
UpgradeTypes: primary
UpgradeMinEnabledLevel: 1
@@ -229,12 +227,11 @@ NAHPAD:
ProvidesPrerequisite@buildingname:
SelectionDecorations:
VisualBounds: 78, 54, 0, -8
WithDecoration@primary:
WithTextDecoration@primary:
RequiresSelection: true
Image: pips
Sequence: tag-primary
Palette: pips
ReferencePoint: Center
Text: PRIMARY
ReferencePoint: Top
Color: E0D048
ZOffset: 256
UpgradeTypes: primary
UpgradeMinEnabledLevel: 1

View File

@@ -90,9 +90,6 @@ pips:
groups: pipsra #TODO: backfall to RA asset
Start: 8
Length: 10
tag-primary: pipsra #TODO: backfall to RA asset
Start: 2
Offset: 0, 2
pip-empty: pips2
pip-green: pips2
Start: 1