Replace DecorationPosition with mod-defined string ids.

This commit is contained in:
Paul Chote
2020-08-14 18:49:47 +01:00
committed by abcdefg30
parent b16cbeacb6
commit 9cd6df2929
9 changed files with 81 additions and 98 deletions

View File

@@ -267,6 +267,7 @@ namespace OpenRA.Traits
public interface ISelectionDecorations
{
IEnumerable<IRenderable> RenderSelectionAnnotations(Actor self, WorldRenderer worldRenderer, Color color);
int2 GetDecorationOrigin(Actor self, WorldRenderer wr, string pos, int2 margin);
}
public interface IMapPreviewSignatureInfo : ITraitInfoInterface

View File

@@ -32,20 +32,38 @@ namespace OpenRA.Mods.Common.Traits.Render
selectable = self.Trait<IsometricSelectable>();
}
protected override int2 GetDecorationPosition(Actor self, WorldRenderer wr, DecorationPosition pos)
int2 GetDecorationPosition(Actor self, WorldRenderer wr, string pos)
{
var bounds = selectable.DecorationBounds(self, wr);
switch (pos)
{
case DecorationPosition.TopLeft: return bounds.Vertices[1];
case DecorationPosition.TopRight: return bounds.Vertices[5];
case DecorationPosition.BottomLeft: return bounds.Vertices[2];
case DecorationPosition.BottomRight: return bounds.Vertices[4];
case DecorationPosition.Top: return new int2((bounds.Vertices[1].X + bounds.Vertices[5].X) / 2, bounds.Vertices[1].Y);
case "TopLeft": return bounds.Vertices[1];
case "TopRight": return bounds.Vertices[5];
case "BottomLeft": return bounds.Vertices[2];
case "BottomRight": return bounds.Vertices[4];
case "Top": return new int2((bounds.Vertices[1].X + bounds.Vertices[5].X) / 2, bounds.Vertices[1].Y);
default: return bounds.BoundingRect.TopLeft + new int2(bounds.BoundingRect.Size.Width / 2, bounds.BoundingRect.Size.Height / 2);
}
}
static int2 GetDecorationMargin(string pos, int2 margin)
{
switch (pos)
{
case "TopLeft": return margin;
case "TopRight": return new int2(-margin.X, margin.Y);
case "BottomLeft": return new int2(margin.X, -margin.Y);
case "BottomRight": return -margin;
case "Top": return new int2(0, margin.Y);
default: return int2.Zero;
}
}
protected override int2 GetDecorationOrigin(Actor self, WorldRenderer wr, string pos, int2 margin)
{
return wr.Viewport.WorldToViewPx(GetDecorationPosition(self, wr, pos)) + GetDecorationMargin(pos, margin);
}
protected override IEnumerable<IRenderable> RenderSelectionBox(Actor self, WorldRenderer wr, Color color)
{
var bounds = selectable.DecorationBounds(self, wr);

View File

@@ -32,20 +32,38 @@ namespace OpenRA.Mods.Common.Traits.Render
interactable = self.Trait<Interactable>();
}
protected override int2 GetDecorationPosition(Actor self, WorldRenderer wr, DecorationPosition pos)
int2 GetDecorationPosition(Actor self, WorldRenderer wr, string pos)
{
var bounds = interactable.DecorationBounds(self, wr);
switch (pos)
{
case DecorationPosition.TopLeft: return bounds.TopLeft;
case DecorationPosition.TopRight: return bounds.TopRight;
case DecorationPosition.BottomLeft: return bounds.BottomLeft;
case DecorationPosition.BottomRight: return bounds.BottomRight;
case DecorationPosition.Top: return new int2(bounds.Left + bounds.Size.Width / 2, bounds.Top);
case "TopLeft": return bounds.TopLeft;
case "TopRight": return bounds.TopRight;
case "BottomLeft": return bounds.BottomLeft;
case "BottomRight": return bounds.BottomRight;
case "Top": return new int2(bounds.Left + bounds.Size.Width / 2, bounds.Top);
default: return bounds.TopLeft + new int2(bounds.Size.Width / 2, bounds.Size.Height / 2);
}
}
static int2 GetDecorationMargin(string pos, int2 margin)
{
switch (pos)
{
case "TopLeft": return margin;
case "TopRight": return new int2(-margin.X, margin.Y);
case "BottomLeft": return new int2(margin.X, -margin.Y);
case "BottomRight": return -margin;
case "Top": return new int2(0, margin.Y);
default: return int2.Zero;
}
}
protected override int2 GetDecorationOrigin(Actor self, WorldRenderer wr, string pos, int2 margin)
{
return wr.Viewport.WorldToViewPx(GetDecorationPosition(self, wr, pos)) + GetDecorationMargin(pos, margin);
}
protected override IEnumerable<IRenderable> RenderSelectionBox(Actor self, WorldRenderer wr, Color color)
{
var bounds = interactable.DecorationBounds(self, wr);

View File

@@ -24,8 +24,8 @@ namespace OpenRA.Mods.Common.Traits.Render
public abstract class SelectionDecorationsBase : ISelectionDecorations, IRenderAnnotations, INotifyCreated
{
Dictionary<DecorationPosition, IDecoration[]> decorations;
Dictionary<DecorationPosition, IDecoration[]> selectedDecorations;
IDecoration[] decorations;
IDecoration[] selectedDecorations;
protected readonly SelectionDecorationsBaseInfo info;
@@ -36,22 +36,8 @@ namespace OpenRA.Mods.Common.Traits.Render
void INotifyCreated.Created(Actor self)
{
var groupedDecorations = new Dictionary<DecorationPosition, List<IDecoration>>();
var groupedSelectionDecorations = new Dictionary<DecorationPosition, List<IDecoration>>();
foreach (var d in self.TraitsImplementing<IDecoration>())
{
groupedSelectionDecorations.GetOrAdd(d.Position).Add(d);
if (!d.RequiresSelection)
groupedDecorations.GetOrAdd(d.Position).Add(d);
}
decorations = groupedDecorations.ToDictionary(
d => d.Key,
d => d.Value.ToArray());
selectedDecorations = groupedSelectionDecorations.ToDictionary(
d => d.Key,
d => d.Value.ToArray());
selectedDecorations = self.TraitsImplementing<IDecoration>().ToArray();
decorations = selectedDecorations.Where(d => !d.RequiresSelection).ToArray();
}
IEnumerable<WPos> ActivityTargetPath(Actor self)
@@ -116,13 +102,9 @@ namespace OpenRA.Mods.Common.Traits.Render
yield break;
var renderDecorations = selected ? selectedDecorations : decorations;
foreach (var kv in renderDecorations)
{
var pos = GetDecorationPosition(self, wr, kv.Key);
foreach (var r in kv.Value)
foreach (var rr in r.RenderDecoration(self, wr, pos))
yield return rr;
}
foreach (var r in renderDecorations)
foreach (var rr in r.RenderDecoration(self, wr, this))
yield return rr;
}
IEnumerable<IRenderable> ISelectionDecorations.RenderSelectionAnnotations(Actor self, WorldRenderer worldRenderer, Color color)
@@ -130,7 +112,12 @@ namespace OpenRA.Mods.Common.Traits.Render
return RenderSelectionBox(self, worldRenderer, color);
}
protected abstract int2 GetDecorationPosition(Actor self, WorldRenderer wr, DecorationPosition pos);
int2 ISelectionDecorations.GetDecorationOrigin(Actor self, WorldRenderer wr, string pos, int2 margin)
{
return GetDecorationOrigin(self, wr, pos, margin);
}
protected abstract int2 GetDecorationOrigin(Actor self, WorldRenderer wr, string pos, int2 margin);
protected abstract IEnumerable<IRenderable> RenderSelectionBox(Actor self, WorldRenderer wr, Color color);
protected abstract IEnumerable<IRenderable> RenderSelectionBars(Actor self, WorldRenderer wr, bool displayHealth, bool displayExtra);
}

View File

@@ -22,7 +22,7 @@ namespace OpenRA.Mods.Common.Traits.Render
public abstract class WithDecorationBaseInfo : ConditionalTraitInfo
{
[Desc("Position in the actor's selection box to draw the decoration.")]
public readonly DecorationPosition Position = DecorationPosition.TopLeft;
public readonly string Position = "TopLeft";
[Desc("Player stances who can view the decoration.")]
public readonly Stance ValidStances = Stance.Ally;
@@ -89,20 +89,16 @@ namespace OpenRA.Mods.Common.Traits.Render
return true;
}
DecorationPosition IDecoration.Position { get { return Info.Position; } }
bool IDecoration.Enabled { get { return !IsTraitDisabled && self.IsInWorld && ShouldRender(self); } }
bool IDecoration.RequiresSelection { get { return Info.RequiresSelection; } }
protected abstract IEnumerable<IRenderable> RenderDecoration(Actor self, WorldRenderer wr, int2 pos);
IEnumerable<IRenderable> IDecoration.RenderDecoration(Actor self, WorldRenderer wr, int2 pos)
IEnumerable<IRenderable> IDecoration.RenderDecoration(Actor self, WorldRenderer wr, ISelectionDecorations container)
{
if (IsTraitDisabled || self.IsDead || !self.IsInWorld || !ShouldRender(self))
return Enumerable.Empty<IRenderable>();
var screenPos = wr.Viewport.WorldToViewPx(pos) + Info.Position.CreateMargin(Info.Margin) + conditionalOffset;
var screenPos = container.GetDecorationOrigin(self, wr, Info.Position, Info.Margin) + conditionalOffset;
return RenderDecoration(self, wr, screenPos);
}

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Mods.Common.Traits.Render
public readonly string GroupSequence = "groups";
[Desc("Position in the actor's selection box to draw the decoration.")]
public readonly DecorationPosition Position = DecorationPosition.TopLeft;
public readonly string Position = "TopLeft";
[Desc("Offset sprite center position from the selection box edge.")]
public readonly int2 Margin = int2.Zero;
@@ -51,13 +51,9 @@ namespace OpenRA.Mods.Common.Traits.Render
anim = new Animation(self.World, Info.Image);
}
DecorationPosition IDecoration.Position { get { return Info.Position; } }
bool IDecoration.Enabled { get { return self.Owner == self.World.LocalPlayer && self.World.Selection.GetControlGroupForActor(self) != null; } }
bool IDecoration.RequiresSelection { get { return true; } }
IEnumerable<IRenderable> IDecoration.RenderDecoration(Actor self, WorldRenderer wr, int2 pos)
IEnumerable<IRenderable> IDecoration.RenderDecoration(Actor self, WorldRenderer wr, ISelectionDecorations container)
{
var group = self.World.Selection.GetControlGroupForActor(self);
if (group == null)
@@ -65,7 +61,7 @@ namespace OpenRA.Mods.Common.Traits.Render
anim.PlayFetchIndex(Info.GroupSequence, () => (int)group);
var screenPos = wr.Viewport.WorldToViewPx(pos) + Info.Position.CreateMargin(Info.Margin) - (0.5f * anim.Image.Size.XY).ToInt2();
var screenPos = container.GetDecorationOrigin(self, wr, Info.Position, Info.Margin) - (0.5f * anim.Image.Size.XY).ToInt2();
var palette = wr.Palette(Info.Palette);
return new IRenderable[]
{

View File

@@ -31,7 +31,7 @@ namespace OpenRA.Mods.Common.Traits.Render
public readonly bool UsePlayerColor = false;
[Desc("Position in the actor's selection box to draw the decoration.")]
public readonly DecorationPosition Position = DecorationPosition.TopLeft;
public readonly string Position = "TopLeft";
[Desc("Offset text center position from the selection box edge.")]
public readonly int2 Margin = int2.Zero;
@@ -63,20 +63,16 @@ namespace OpenRA.Mods.Common.Traits.Render
label = new CachedTransform<int, string>(g => g.ToString());
}
DecorationPosition IDecoration.Position { get { return info.Position; } }
bool IDecoration.Enabled { get { return self.Owner == self.World.LocalPlayer && self.World.Selection.GetControlGroupForActor(self) != null; } }
bool IDecoration.RequiresSelection { get { return true; } }
IEnumerable<IRenderable> IDecoration.RenderDecoration(Actor self, WorldRenderer wr, int2 pos)
IEnumerable<IRenderable> IDecoration.RenderDecoration(Actor self, WorldRenderer wr, ISelectionDecorations container)
{
var group = self.World.Selection.GetControlGroupForActor(self);
if (group == null)
return Enumerable.Empty<IRenderable>();
var text = label.Update(group.Value);
var screenPos = wr.Viewport.WorldToViewPx(pos) + info.Position.CreateMargin(info.Margin);
var screenPos = container.GetDecorationOrigin(self, wr, info.Position, info.Margin);
return new IRenderable[]
{
new UITextRenderable(font, self.CenterPosition, screenPos, 0, color, text)

View File

@@ -639,37 +639,8 @@ namespace OpenRA.Mods.Common.Traits
public interface IDecoration
{
DecorationPosition Position { get; }
bool RequiresSelection { get; }
bool Enabled { get; }
IEnumerable<IRenderable> RenderDecoration(Actor self, WorldRenderer wr, int2 pos);
}
public enum DecorationPosition
{
Center,
TopLeft,
TopRight,
BottomLeft,
BottomRight,
Top
}
public static class DecorationExtensions
{
public static int2 CreateMargin(this DecorationPosition pos, int2 margin)
{
switch (pos)
{
case DecorationPosition.TopLeft: return margin;
case DecorationPosition.TopRight: return new int2(-margin.X, margin.Y);
case DecorationPosition.BottomLeft: return new int2(margin.X, -margin.Y);
case DecorationPosition.BottomRight: return -margin;
case DecorationPosition.Top: return new int2(0, margin.Y);
default: return int2.Zero;
}
}
IEnumerable<IRenderable> RenderDecoration(Actor self, WorldRenderer wr, ISelectionDecorations container);
}
}

View File

@@ -45,14 +45,14 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules
Right = 8,
}
static readonly Dictionary<LegacyReferencePoints, DecorationPosition> PositionMap = new Dictionary<LegacyReferencePoints, DecorationPosition>()
static readonly Dictionary<LegacyReferencePoints, string> PositionMap = new Dictionary<LegacyReferencePoints, string>()
{
{ LegacyReferencePoints.Center, DecorationPosition.Center },
{ LegacyReferencePoints.Top, DecorationPosition.Top },
{ LegacyReferencePoints.Top | LegacyReferencePoints.Left, DecorationPosition.TopLeft },
{ LegacyReferencePoints.Top | LegacyReferencePoints.Right, DecorationPosition.TopRight },
{ LegacyReferencePoints.Bottom | LegacyReferencePoints.Left, DecorationPosition.BottomLeft },
{ LegacyReferencePoints.Bottom | LegacyReferencePoints.Right, DecorationPosition.BottomRight }
{ LegacyReferencePoints.Center, "Center" },
{ LegacyReferencePoints.Top, "Top" },
{ LegacyReferencePoints.Top | LegacyReferencePoints.Left, "TopLeft" },
{ LegacyReferencePoints.Top | LegacyReferencePoints.Right, "TopRight" },
{ LegacyReferencePoints.Bottom | LegacyReferencePoints.Left, "BottomLeft" },
{ LegacyReferencePoints.Bottom | LegacyReferencePoints.Right, "BottomRight" }
};
readonly Dictionary<string, List<string>> locations = new Dictionary<string, List<string>>();
@@ -83,9 +83,9 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules
if (positionNode != null)
{
if (!PositionMap.TryGetValue(positionNode.NodeValue<LegacyReferencePoints>(), out var value))
value = DecorationPosition.TopLeft;
value = "TopLeft";
if (value != DecorationPosition.TopLeft)
if (value != "TopLeft")
{
positionNode.RenameKey("Position");
positionNode.ReplaceValue(FieldSaver.FormatValue(value));