From 3a7558c273bdebd2e7466173331f1100d9ece1e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sun, 3 Jul 2016 12:26:29 +0200 Subject: [PATCH 1/4] Document and lint test pips and groups. --- .../Traits/Render/SelectionDecorations.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs b/OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs index a291aa224a..b4e3d5776d 100644 --- a/OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs +++ b/OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs @@ -34,6 +34,11 @@ namespace OpenRA.Mods.Common.Traits.Render public readonly Color SelectionBoxColor = Color.White; + public readonly string Image = "pips"; + + [Desc("Sprite sequence used to render the control group 0-9 numbers.")] + [SequenceReference("Image")] public readonly string GroupSequence = "groups"; + public object Create(ActorInitializer init) { return new SelectionDecorations(init.Self, this); } public int[] SelectionBoxBounds { get { return VisualBounds; } } @@ -104,9 +109,9 @@ namespace OpenRA.Mods.Common.Traits.Render if (group == null) yield break; - var pipImages = new Animation(self.World, "pips"); + var pipImages = new Animation(self.World, Info.Image); var pal = wr.Palette(Info.Palette); - pipImages.PlayFetchIndex("groups", () => (int)group); + pipImages.PlayFetchIndex(Info.GroupSequence, () => (int)group); pipImages.Tick(); var pos = basePosition - (0.5f * pipImages.Image.Size.XY).ToInt2() + new int2(9, 5); @@ -119,7 +124,7 @@ namespace OpenRA.Mods.Common.Traits.Render if (!pipSources.Any()) yield break; - var pipImages = new Animation(self.World, "pips"); + var pipImages = new Animation(self.World, Info.Image); pipImages.PlayRepeating(PipStrings[0]); var pipSize = pipImages.Image.Size.XY.ToInt2(); From 3e5155fa7dd871efc02c515c797d650692fb621a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sun, 3 Jul 2016 14:40:14 +0200 Subject: [PATCH 2/4] Cache pipImages in the constructor. --- OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs b/OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs index b4e3d5776d..a9510414aa 100644 --- a/OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs +++ b/OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs @@ -50,12 +50,16 @@ namespace OpenRA.Mods.Common.Traits.Render static readonly string[] PipStrings = { "pip-empty", "pip-green", "pip-yellow", "pip-red", "pip-gray", "pip-blue", "pip-ammo", "pip-ammoempty" }; public readonly SelectionDecorationsInfo Info; + readonly Actor self; + readonly Animation pipImages; public SelectionDecorations(Actor self, SelectionDecorationsInfo info) { this.self = self; Info = info; + + pipImages = new Animation(self.World, Info.Image); } IEnumerable ActivityTargetPath() @@ -109,7 +113,6 @@ namespace OpenRA.Mods.Common.Traits.Render if (group == null) yield break; - var pipImages = new Animation(self.World, Info.Image); var pal = wr.Palette(Info.Palette); pipImages.PlayFetchIndex(Info.GroupSequence, () => (int)group); pipImages.Tick(); @@ -124,7 +127,6 @@ namespace OpenRA.Mods.Common.Traits.Render if (!pipSources.Any()) yield break; - var pipImages = new Animation(self.World, Info.Image); pipImages.PlayRepeating(PipStrings[0]); var pipSize = pipImages.Image.Size.XY.ToInt2(); From 4325e1b400881ecda66b89c9ffa37f23a134e4d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sun, 3 Jul 2016 14:40:47 +0200 Subject: [PATCH 3/4] Let pipImages tick using the appropriate interface. --- OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs b/OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs index a9510414aa..5a9507c21d 100644 --- a/OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs +++ b/OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs @@ -44,7 +44,7 @@ namespace OpenRA.Mods.Common.Traits.Render public int[] SelectionBoxBounds { get { return VisualBounds; } } } - public class SelectionDecorations : IPostRenderSelection + public class SelectionDecorations : IPostRenderSelection, ITick { // depends on the order of pips in TraitsInterfaces.cs! static readonly string[] PipStrings = { "pip-empty", "pip-green", "pip-yellow", "pip-red", "pip-gray", "pip-blue", "pip-ammo", "pip-ammoempty" }; @@ -115,7 +115,6 @@ namespace OpenRA.Mods.Common.Traits.Render var pal = wr.Palette(Info.Palette); pipImages.PlayFetchIndex(Info.GroupSequence, () => (int)group); - pipImages.Tick(); var pos = basePosition - (0.5f * pipImages.Image.Size.XY).ToInt2() + new int2(9, 5); yield return new UISpriteRenderable(pipImages.Image, self.CenterPosition, pos, 0, pal, 1f); @@ -156,5 +155,10 @@ namespace OpenRA.Mods.Common.Traits.Render pipxyOffset = new int2(0, pipxyOffset.Y - (pipSize.Y + 1)); } } + + void ITick.Tick(Actor self) + { + pipImages.Tick(); + } } } From 889bbcf428d10f16caab17e2aae70b54a5af315c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sun, 3 Jul 2016 14:41:15 +0200 Subject: [PATCH 4/4] Cache the palette lookup. --- .../Traits/Render/SelectionDecorations.cs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs b/OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs index 5a9507c21d..9524e8cc33 100644 --- a/OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs +++ b/OpenRA.Mods.Common/Traits/Render/SelectionDecorations.cs @@ -99,28 +99,28 @@ namespace OpenRA.Mods.Common.Traits.Render var pos = wr.ScreenPxPosition(self.CenterPosition); var tl = wr.Viewport.WorldToViewPx(pos + new int2(b.Left, b.Top)); var bl = wr.Viewport.WorldToViewPx(pos + new int2(b.Left, b.Bottom)); + var pal = wr.Palette(Info.Palette); - foreach (var r in DrawControlGroup(wr, self, tl)) + foreach (var r in DrawControlGroup(wr, self, tl, pal)) yield return r; - foreach (var r in DrawPips(wr, self, bl)) + foreach (var r in DrawPips(wr, self, bl, pal)) yield return r; } - IEnumerable DrawControlGroup(WorldRenderer wr, Actor self, int2 basePosition) + IEnumerable DrawControlGroup(WorldRenderer wr, Actor self, int2 basePosition, PaletteReference palette) { var group = self.World.Selection.GetControlGroupForActor(self); if (group == null) yield break; - var pal = wr.Palette(Info.Palette); pipImages.PlayFetchIndex(Info.GroupSequence, () => (int)group); var pos = basePosition - (0.5f * pipImages.Image.Size.XY).ToInt2() + new int2(9, 5); - yield return new UISpriteRenderable(pipImages.Image, self.CenterPosition, pos, 0, pal, 1f); + yield return new UISpriteRenderable(pipImages.Image, self.CenterPosition, pos, 0, palette, 1f); } - IEnumerable DrawPips(WorldRenderer wr, Actor self, int2 basePosition) + IEnumerable DrawPips(WorldRenderer wr, Actor self, int2 basePosition, PaletteReference palette) { var pipSources = self.TraitsImplementing(); if (!pipSources.Any()) @@ -131,7 +131,6 @@ namespace OpenRA.Mods.Common.Traits.Render var pipSize = pipImages.Image.Size.XY.ToInt2(); var pipxyBase = basePosition + new int2(1 - pipSize.X / 2, -(3 + pipSize.Y / 2)); var pipxyOffset = new int2(0, 0); - var pal = wr.Palette(Info.Palette); var width = self.VisualBounds.Width; foreach (var pips in pipSources) @@ -148,7 +147,7 @@ namespace OpenRA.Mods.Common.Traits.Render pipImages.PlayRepeating(PipStrings[(int)pip]); pipxyOffset += new int2(pipSize.X, 0); - yield return new UISpriteRenderable(pipImages.Image, self.CenterPosition, pipxyBase + pipxyOffset, 0, pal, 1f); + yield return new UISpriteRenderable(pipImages.Image, self.CenterPosition, pipxyBase + pipxyOffset, 0, palette, 1f); } // Increment row