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 public interface ISelectionDecorations
{ {
IEnumerable<IRenderable> RenderSelectionAnnotations(Actor self, WorldRenderer worldRenderer, Color color); IEnumerable<IRenderable> RenderSelectionAnnotations(Actor self, WorldRenderer worldRenderer, Color color);
int2 GetDecorationOrigin(Actor self, WorldRenderer wr, string pos, int2 margin);
} }
public interface IMapPreviewSignatureInfo : ITraitInfoInterface public interface IMapPreviewSignatureInfo : ITraitInfoInterface

View File

@@ -32,20 +32,38 @@ namespace OpenRA.Mods.Common.Traits.Render
selectable = self.Trait<IsometricSelectable>(); 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); var bounds = selectable.DecorationBounds(self, wr);
switch (pos) switch (pos)
{ {
case DecorationPosition.TopLeft: return bounds.Vertices[1]; case "TopLeft": return bounds.Vertices[1];
case DecorationPosition.TopRight: return bounds.Vertices[5]; case "TopRight": return bounds.Vertices[5];
case DecorationPosition.BottomLeft: return bounds.Vertices[2]; case "BottomLeft": return bounds.Vertices[2];
case DecorationPosition.BottomRight: return bounds.Vertices[4]; case "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 "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); 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) protected override IEnumerable<IRenderable> RenderSelectionBox(Actor self, WorldRenderer wr, Color color)
{ {
var bounds = selectable.DecorationBounds(self, wr); var bounds = selectable.DecorationBounds(self, wr);

View File

@@ -32,20 +32,38 @@ namespace OpenRA.Mods.Common.Traits.Render
interactable = self.Trait<Interactable>(); 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); var bounds = interactable.DecorationBounds(self, wr);
switch (pos) switch (pos)
{ {
case DecorationPosition.TopLeft: return bounds.TopLeft; case "TopLeft": return bounds.TopLeft;
case DecorationPosition.TopRight: return bounds.TopRight; case "TopRight": return bounds.TopRight;
case DecorationPosition.BottomLeft: return bounds.BottomLeft; case "BottomLeft": return bounds.BottomLeft;
case DecorationPosition.BottomRight: return bounds.BottomRight; case "BottomRight": return bounds.BottomRight;
case DecorationPosition.Top: return new int2(bounds.Left + bounds.Size.Width / 2, bounds.Top); 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); 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) protected override IEnumerable<IRenderable> RenderSelectionBox(Actor self, WorldRenderer wr, Color color)
{ {
var bounds = interactable.DecorationBounds(self, wr); 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 public abstract class SelectionDecorationsBase : ISelectionDecorations, IRenderAnnotations, INotifyCreated
{ {
Dictionary<DecorationPosition, IDecoration[]> decorations; IDecoration[] decorations;
Dictionary<DecorationPosition, IDecoration[]> selectedDecorations; IDecoration[] selectedDecorations;
protected readonly SelectionDecorationsBaseInfo info; protected readonly SelectionDecorationsBaseInfo info;
@@ -36,22 +36,8 @@ namespace OpenRA.Mods.Common.Traits.Render
void INotifyCreated.Created(Actor self) void INotifyCreated.Created(Actor self)
{ {
var groupedDecorations = new Dictionary<DecorationPosition, List<IDecoration>>(); selectedDecorations = self.TraitsImplementing<IDecoration>().ToArray();
var groupedSelectionDecorations = new Dictionary<DecorationPosition, List<IDecoration>>(); decorations = selectedDecorations.Where(d => !d.RequiresSelection).ToArray();
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());
} }
IEnumerable<WPos> ActivityTargetPath(Actor self) IEnumerable<WPos> ActivityTargetPath(Actor self)
@@ -116,13 +102,9 @@ namespace OpenRA.Mods.Common.Traits.Render
yield break; yield break;
var renderDecorations = selected ? selectedDecorations : decorations; var renderDecorations = selected ? selectedDecorations : decorations;
foreach (var kv in renderDecorations) foreach (var r in renderDecorations)
{ foreach (var rr in r.RenderDecoration(self, wr, this))
var pos = GetDecorationPosition(self, wr, kv.Key); yield return rr;
foreach (var r in kv.Value)
foreach (var rr in r.RenderDecoration(self, wr, pos))
yield return rr;
}
} }
IEnumerable<IRenderable> ISelectionDecorations.RenderSelectionAnnotations(Actor self, WorldRenderer worldRenderer, Color color) 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); 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> RenderSelectionBox(Actor self, WorldRenderer wr, Color color);
protected abstract IEnumerable<IRenderable> RenderSelectionBars(Actor self, WorldRenderer wr, bool displayHealth, bool displayExtra); 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 public abstract class WithDecorationBaseInfo : ConditionalTraitInfo
{ {
[Desc("Position in the actor's selection box to draw the decoration.")] [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.")] [Desc("Player stances who can view the decoration.")]
public readonly Stance ValidStances = Stance.Ally; public readonly Stance ValidStances = Stance.Ally;
@@ -89,20 +89,16 @@ namespace OpenRA.Mods.Common.Traits.Render
return true; 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; } } bool IDecoration.RequiresSelection { get { return Info.RequiresSelection; } }
protected abstract IEnumerable<IRenderable> RenderDecoration(Actor self, WorldRenderer wr, int2 pos); 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)) if (IsTraitDisabled || self.IsDead || !self.IsInWorld || !ShouldRender(self))
return Enumerable.Empty<IRenderable>(); 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); return RenderDecoration(self, wr, screenPos);
} }

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Mods.Common.Traits.Render
public readonly string GroupSequence = "groups"; public readonly string GroupSequence = "groups";
[Desc("Position in the actor's selection box to draw the decoration.")] [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.")] [Desc("Offset sprite center position from the selection box edge.")]
public readonly int2 Margin = int2.Zero; public readonly int2 Margin = int2.Zero;
@@ -51,13 +51,9 @@ namespace OpenRA.Mods.Common.Traits.Render
anim = new Animation(self.World, Info.Image); 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; } } 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); var group = self.World.Selection.GetControlGroupForActor(self);
if (group == null) if (group == null)
@@ -65,7 +61,7 @@ namespace OpenRA.Mods.Common.Traits.Render
anim.PlayFetchIndex(Info.GroupSequence, () => (int)group); 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); var palette = wr.Palette(Info.Palette);
return new IRenderable[] return new IRenderable[]
{ {

View File

@@ -31,7 +31,7 @@ namespace OpenRA.Mods.Common.Traits.Render
public readonly bool UsePlayerColor = false; public readonly bool UsePlayerColor = false;
[Desc("Position in the actor's selection box to draw the decoration.")] [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.")] [Desc("Offset text center position from the selection box edge.")]
public readonly int2 Margin = int2.Zero; public readonly int2 Margin = int2.Zero;
@@ -63,20 +63,16 @@ namespace OpenRA.Mods.Common.Traits.Render
label = new CachedTransform<int, string>(g => g.ToString()); 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; } } 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); var group = self.World.Selection.GetControlGroupForActor(self);
if (group == null) if (group == null)
return Enumerable.Empty<IRenderable>(); return Enumerable.Empty<IRenderable>();
var text = label.Update(group.Value); 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[] return new IRenderable[]
{ {
new UITextRenderable(font, self.CenterPosition, screenPos, 0, color, text) new UITextRenderable(font, self.CenterPosition, screenPos, 0, color, text)

View File

@@ -639,37 +639,8 @@ namespace OpenRA.Mods.Common.Traits
public interface IDecoration public interface IDecoration
{ {
DecorationPosition Position { get; }
bool RequiresSelection { get; } bool RequiresSelection { get; }
bool Enabled { get; } IEnumerable<IRenderable> RenderDecoration(Actor self, WorldRenderer wr, ISelectionDecorations container);
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;
}
}
} }
} }

View File

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