Rework sequence docs plumbing.

This commit is contained in:
Paul Chote
2022-06-26 19:01:01 +01:00
committed by Pavel Penev
parent 2037e37d4e
commit c8df1e864c
4 changed files with 199 additions and 161 deletions

View File

@@ -29,25 +29,24 @@ namespace OpenRA.Mods.Cnc.Graphics
[Desc("A sprite sequence that has the oddities that come with first-generation Westwood titles.")] [Desc("A sprite sequence that has the oddities that come with first-generation Westwood titles.")]
public class ClassicSpriteSequence : DefaultSpriteSequence public class ClassicSpriteSequence : DefaultSpriteSequence
{ {
// This needs to be a public property for the documentation generation to work. [Desc("Incorporate a compensation factor for the rotational distortion present in the first-generation Westwood games.")]
[Desc("Incorporate a compensation factor due to the distortion caused by 3D-Studio " + static readonly SpriteSequenceField<bool> UseClassicFacings = new SpriteSequenceField<bool>(nameof(UseClassicFacings), false);
"when it tried to render 45% angles which was used by Westwood Studios at that time.")] readonly bool useClassicFacings;
public bool UseClassicFacings { get; }
public ClassicSpriteSequence(ModData modData, string tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info) public ClassicSpriteSequence(ModData modData, string tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info)
: base(modData, tileSet, cache, loader, sequence, animation, info) : base(modData, tileSet, cache, loader, sequence, animation, info)
{ {
var d = info.ToDictionary(); var d = info.ToDictionary();
UseClassicFacings = LoadField(d, nameof(UseClassicFacings), UseClassicFacings); useClassicFacings = LoadField(d, UseClassicFacings);
if (UseClassicFacings && Facings != 32) if (useClassicFacings && facings != 32)
throw new InvalidOperationException( throw new InvalidOperationException(
$"{info.Nodes[0].Location}: Sequence {sequence}.{animation}: UseClassicFacings is only valid for 32 facings"); $"{info.Nodes[0].Location}: Sequence {sequence}.{animation}: UseClassicFacings is only valid for 32 facings");
} }
protected override int GetFacingFrameOffset(WAngle facing) protected override int GetFacingFrameOffset(WAngle facing)
{ {
return UseClassicFacings ? Util.ClassicIndexFacing(facing, Facings) : Common.Util.IndexFacing(facing, Facings); return useClassicFacings ? Util.ClassicIndexFacing(facing, facings) : Common.Util.IndexFacing(facing, facings);
} }
} }
} }

View File

@@ -12,6 +12,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
namespace OpenRA.Mods.Cnc.Graphics namespace OpenRA.Mods.Cnc.Graphics
{ {
@@ -45,21 +46,20 @@ namespace OpenRA.Mods.Cnc.Graphics
"that come with first-generation Westwood titles.")] "that come with first-generation Westwood titles.")]
public class ClassicTilesetSpecificSpriteSequence : ClassicSpriteSequence public class ClassicTilesetSpecificSpriteSequence : ClassicSpriteSequence
{ {
// These need to be public properties for the documentation generation to work.
[Desc("Dictionary of <string: string> with tileset name to override -> tileset name to use instead.")] [Desc("Dictionary of <string: string> with tileset name to override -> tileset name to use instead.")]
public static Dictionary<string, string> TilesetOverrides => null; static readonly SpriteSequenceField<Dictionary<string, string>> TilesetOverrides = new SpriteSequenceField<Dictionary<string, string>>(nameof(TilesetOverrides), null);
[Desc("Use `TilesetCodes` as defined in `mod.yaml` to add a letter as a second character " + [Desc("Use `TilesetCodes` as defined in `mod.yaml` to add a letter as a second character " +
"into the sprite filename like the Westwood 2.5D titles did for tileset-specific variants.")] "into the sprite filename like the Westwood 2.5D titles did for tileset-specific variants.")]
public static bool UseTilesetCode => false; static readonly SpriteSequenceField<bool> UseTilesetCode = new SpriteSequenceField<bool>(nameof(UseTilesetCode), false);
[Desc("Append a tileset-specific extension to the file name " + [Desc("Append a tileset-specific extension to the file name " +
"- either as defined in `mod.yaml`'s `TilesetExtensions` (if `UseTilesetExtension` is used) " + "- either as defined in `mod.yaml`'s `TilesetExtensions` (if `UseTilesetExtension` is used) " +
"or the default hardcoded one for this sequence type (.shp).")] "or the default hardcoded one for this sequence type (.shp).")]
public static bool AddExtension => true; static readonly SpriteSequenceField<bool> AddExtension = new SpriteSequenceField<bool>(nameof(AddExtension), true);
[Desc("Whether `mod.yaml`'s `TilesetExtensions` should be used with the sequence's file name.")] [Desc("Whether `mod.yaml`'s `TilesetExtensions` should be used with the sequence's file name.")]
public static bool UseTilesetExtension { get; private set; } static readonly SpriteSequenceField<bool> UseTilesetExtension = new SpriteSequenceField<bool>(nameof(UseTilesetExtension), false);
public ClassicTilesetSpecificSpriteSequence(ModData modData, string tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info) public ClassicTilesetSpecificSpriteSequence(ModData modData, string tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info)
: base(modData, tileSet, cache, loader, sequence, animation, info) { } : base(modData, tileSet, cache, loader, sequence, animation, info) { }
@@ -82,16 +82,15 @@ namespace OpenRA.Mods.Cnc.Graphics
var spriteName = sprite ?? sequence; var spriteName = sprite ?? sequence;
if (LoadField(d, nameof(UseTilesetCode), UseTilesetCode)) if (LoadField(d, UseTilesetCode))
{ {
if (loader.TilesetCodes.TryGetValue(ResolveTilesetId(tileSet, d), out var code)) if (loader.TilesetCodes.TryGetValue(ResolveTilesetId(tileSet, d), out var code))
spriteName = spriteName.Substring(0, 1) + code + spriteName.Substring(2, spriteName.Length - 2); spriteName = spriteName.Substring(0, 1) + code + spriteName.Substring(2, spriteName.Length - 2);
} }
if (LoadField(d, nameof(AddExtension), AddExtension)) if (LoadField(d, AddExtension))
{ {
UseTilesetExtension = LoadField(d, nameof(UseTilesetExtension), UseTilesetExtension); if (LoadField(d, UseTilesetExtension) && loader.TilesetExtensions.TryGetValue(ResolveTilesetId(tileSet, d), out var tilesetExtension))
if (UseTilesetExtension && loader.TilesetExtensions.TryGetValue(ResolveTilesetId(tileSet, d), out var tilesetExtension))
return spriteName + tilesetExtension; return spriteName + tilesetExtension;
return spriteName + loader.DefaultSpriteExtension; return spriteName + loader.DefaultSpriteExtension;

View File

@@ -122,98 +122,131 @@ namespace OpenRA.Mods.Common.Graphics
float ISpriteSequence.GetAlpha(int frame) { throw exception; } float ISpriteSequence.GetAlpha(int frame) { throw exception; }
} }
public struct SpriteSequenceField<T>
{
public string Key;
public T DefaultValue;
public SpriteSequenceField(string key, T defaultValue)
{
Key = key;
DefaultValue = defaultValue;
}
}
[Desc("Generic sprite sequence implementation, mostly unencumbered with game- or artwork-specific logic.")] [Desc("Generic sprite sequence implementation, mostly unencumbered with game- or artwork-specific logic.")]
public class DefaultSpriteSequence : ISpriteSequence public class DefaultSpriteSequence : ISpriteSequence
{ {
static readonly WDist DefaultShadowSpriteZOffset = new WDist(-5);
protected Sprite[] sprites; protected Sprite[] sprites;
readonly bool reverseFacings, transpose; readonly bool reverseFacings, transpose;
readonly string sequence; readonly string sequence;
protected readonly ISpriteSequenceLoader Loader; protected readonly ISpriteSequenceLoader Loader;
public Rectangle Bounds { get; }
public string Name { get; } public string Name { get; }
[Desc("Frame index to start from.")] [Desc("Frame index to start from.")]
public int Start { get; private set; } static readonly SpriteSequenceField<int> Start = new SpriteSequenceField<int>(nameof(Start), 0);
int ISpriteSequence.Start => start;
int start;
[Desc("Number of frames to use. Does not have to be the total amount the sprite sheet has.")] [Desc("Number of frames to use. Does not have to be the total amount the sprite sheet has.")]
public int Length { get; private set; } = 1; static readonly SpriteSequenceField<int> Length = new SpriteSequenceField<int>(nameof(Length), 1);
int ISpriteSequence.Length => length;
int length;
[Desc("Multiplier for the number of facings.")] [Desc("Overrides Length if a different number of frames is defined between facings.")]
public int Stride { get; private set; } static readonly SpriteSequenceField<int> Stride = new SpriteSequenceField<int>(nameof(Stride), -1);
int ISpriteSequence.Stride => stride;
int stride;
[Desc("The amount of directions the unit faces. Use negative values to rotate counter-clockwise.")] [Desc("The amount of directions the unit faces. Use negative values to rotate counter-clockwise.")]
public int Facings { get; } = 1; static readonly SpriteSequenceField<int> Facings = new SpriteSequenceField<int>(nameof(Facings), 1);
int ISpriteSequence.Facings => facings;
protected int facings;
[Desc("Time (in milliseconds) to wait until playing the next frame in the animation.")] [Desc("Time (in milliseconds at default game speed) to wait until playing the next frame in the animation.")]
public int Tick { get; } = 40; static readonly SpriteSequenceField<int> Tick = new SpriteSequenceField<int>(nameof(Tick), 40);
int ISpriteSequence.Tick => tick;
readonly int tick;
[Desc("Value controlling the Z-order. A higher values means rendering on top of other sprites at the same position. " + [Desc("Value controlling the Z-order. A higher values means rendering on top of other sprites at the same position. " +
"Use power of 2 values to avoid glitches.")] "Use power of 2 values to avoid glitches.")]
public int ZOffset { get; } static readonly SpriteSequenceField<WDist> ZOffset = new SpriteSequenceField<WDist>(nameof(ZOffset), WDist.Zero);
int ISpriteSequence.ZOffset => zOffset;
readonly int zOffset;
[Desc("")] [Desc("Additional sprite depth Z offset to apply as a function of sprite Y (0: vertical, 1: flat on terrain)")]
public float ZRamp { get; } static readonly SpriteSequenceField<int> ZRamp = new SpriteSequenceField<int>(nameof(ZRamp), 0);
[Desc("If the shadow is not part of the sprite, but baked into the same sprite sheet at a fixed offset, " + [Desc("If the shadow is not part of the sprite, but baked into the same sprite sheet at a fixed offset, " +
"set this to the frame index where it starts.")] "set this to the frame index where it starts.")]
public int ShadowStart { get; } = -1; static readonly SpriteSequenceField<int> ShadowStart = new SpriteSequenceField<int>(nameof(ShadowStart), -1);
int ISpriteSequence.ShadowStart => shadowStart;
readonly int shadowStart;
[Desc("Set Z-Offset for the separate shadow. Used by the later Westwood 2.5D titles. Defined in WDist units!")] [Desc("Set Z-Offset for the separate shadow. Used by the later Westwood 2.5D titles.")]
public int ShadowZOffset { get; } static readonly SpriteSequenceField<WDist> ShadowZOffset = new SpriteSequenceField<WDist>(nameof(ShadowZOffset), new WDist(-5));
int ISpriteSequence.ShadowZOffset => shadowZOffset;
readonly int shadowZOffset;
[Desc("The individual frames to play instead of going through them sequentially from the `Start`.")] [Desc("The individual frames to play instead of going through them sequentially from the `Start`.")]
public int[] Frames { get; private set; } static readonly SpriteSequenceField<int[]> Frames = new SpriteSequenceField<int[]>(nameof(Frames), null);
int[] ISpriteSequence.Frames => frames;
public Rectangle Bounds { get; } int[] frames;
[Desc("Don't apply terrain lighting or colored overlays.")] [Desc("Don't apply terrain lighting or colored overlays.")]
public bool IgnoreWorldTint { get; } static readonly SpriteSequenceField<bool> IgnoreWorldTint = new SpriteSequenceField<bool>(nameof(IgnoreWorldTint), false);
bool ISpriteSequence.IgnoreWorldTint => ignoreWorldTint;
readonly bool ignoreWorldTint;
[Desc("")] [Desc("Adjusts the rendered size of the sprite")]
public float Scale { get; } = 1f; static readonly SpriteSequenceField<float> Scale = new SpriteSequenceField<float>(nameof(Scale), 1);
float ISpriteSequence.Scale => scale;
readonly float scale;
// These need to be public properties for the documentation generation to work.
[Desc("Play the sprite sequence back and forth.")] [Desc("Play the sprite sequence back and forth.")]
public static bool Reverses => false; static readonly SpriteSequenceField<bool> Reverses = new SpriteSequenceField<bool>(nameof(Reverses), false);
[Desc("Support a frame order where each animation step is split per each direction.")] [Desc("Support a frame order where each animation step is split per each direction.")]
public static bool Transpose => false; static readonly SpriteSequenceField<bool> Transpose = new SpriteSequenceField<bool>(nameof(Transpose), false);
[Desc("Mirror on the X axis.")] [Desc("Mirror on the X axis.")]
public bool FlipX { get; } static readonly SpriteSequenceField<bool> FlipX = new SpriteSequenceField<bool>(nameof(FlipX), false);
[Desc("Mirror on the Y axis.")] [Desc("Mirror on the Y axis.")]
public bool FlipY { get; } static readonly SpriteSequenceField<bool> FlipY = new SpriteSequenceField<bool>(nameof(FlipY), false);
[Desc("Change the position in-game on X, Y, Z.")] [Desc("Change the position in-game on X, Y, Z.")]
public float3 Offset { get; } = float3.Zero; static readonly SpriteSequenceField<float3> Offset = new SpriteSequenceField<float3>(nameof(Offset), float3.Zero);
[Desc("Apply an OpenGL/Photoshop inspired blend mode.")] [Desc("Apply an OpenGL/Photoshop inspired blend mode.")]
public BlendMode BlendMode { get; } = BlendMode.Alpha; static readonly SpriteSequenceField<BlendMode> BlendMode = new SpriteSequenceField<BlendMode>(nameof(BlendMode), OpenRA.BlendMode.Alpha);
[Desc("Allows to append multiple sequence definitions which are indented below this node " + [Desc("Allows to append multiple sequence definitions which are indented below this node " +
"like when offsets differ per frame or a sequence is spread across individual files.")] "like when offsets differ per frame or a sequence is spread across individual files.")]
public static object Combine => null; static readonly SpriteSequenceField<MiniYaml> Combine = new SpriteSequenceField<MiniYaml>(nameof(Combine), null);
[Desc("Sets transparency - use one value to set for all frames or provide a value for each frame.")] [Desc("Sets transparency - use one value to set for all frames or provide a value for each frame.")]
public float[] Alpha { get; } static readonly SpriteSequenceField<float[]> Alpha = new SpriteSequenceField<float[]>(nameof(Alpha), null);
readonly float[] alpha;
[Desc("Plays a fade out effect.")] [Desc("Fade the animation from fully opaque on the first frame to fully transparent after the last frame.")]
public static bool AlphaFade => false; static readonly SpriteSequenceField<bool> AlphaFade = new SpriteSequenceField<bool>(nameof(AlphaFade), false);
[Desc("Name of the file containing the depth data sprite.")] [Desc("Name of the file containing the depth data sprite.")]
public string DepthSprite { get; } static readonly SpriteSequenceField<string> DepthSprite = new SpriteSequenceField<string>(nameof(DepthSprite), null);
[Desc("Frame index containing the depth data.")] [Desc("Frame index containing the depth data.")]
public static int DepthSpriteFrame => 0; static readonly SpriteSequenceField<int> DepthSpriteFrame = new SpriteSequenceField<int>(nameof(DepthSpriteFrame), 0);
[Desc("")] [Desc("X, Y offset to apply to the depth sprite.")]
public static float2 DepthSpriteOffset => float2.Zero; static readonly SpriteSequenceField<float2> DepthSpriteOffset = new SpriteSequenceField<float2>(nameof(DepthSpriteOffset), float2.Zero);
[Desc("Make a custom palette embedded in the sprite available to the PaletteFromEmbeddedSpritePalette trait.")] [Desc("Make a custom palette embedded in the sprite available to the PaletteFromEmbeddedSpritePalette trait.")]
public static bool HasEmbeddedPalette => false; static readonly SpriteSequenceField<bool> HasEmbeddedPalette = new SpriteSequenceField<bool>(nameof(HasEmbeddedPalette), false);
public readonly uint[] EmbeddedPalette; public readonly uint[] EmbeddedPalette;
@@ -230,6 +263,14 @@ namespace OpenRA.Mods.Common.Graphics
return fallback; return fallback;
} }
protected static T LoadField<T>(Dictionary<string, MiniYaml> d, SpriteSequenceField<T> field)
{
if (d.TryGetValue(field.Key, out var value))
return FieldLoader.GetValue<T>(field.Key, value.Value);
return field.DefaultValue;
}
protected static Rectangle FlipRectangle(Rectangle rect, bool flipX, bool flipY) protected static Rectangle FlipRectangle(Rectangle rect, bool flipX, bool flipY)
{ {
var left = flipX ? rect.Right : rect.Left; var left = flipX ? rect.Right : rect.Left;
@@ -249,89 +290,90 @@ namespace OpenRA.Mods.Common.Graphics
try try
{ {
Start = LoadField(d, nameof(Start), 0); start = LoadField(d, Start);
ShadowStart = LoadField(d, nameof(ShadowStart), ShadowStart); shadowStart = LoadField(d, ShadowStart);
ShadowZOffset = LoadField(d, nameof(ShadowZOffset), DefaultShadowSpriteZOffset).Length; shadowZOffset = LoadField(d, ShadowZOffset).Length;
ZOffset = LoadField(d, nameof(ZOffset), WDist.Zero).Length; zOffset = LoadField(d, ZOffset).Length;
ZRamp = LoadField(d, nameof(ZRamp), 0f); tick = LoadField(d, Tick);
Tick = LoadField(d, nameof(Tick), Tick); transpose = LoadField(d, Transpose);
transpose = LoadField(d, nameof(Transpose), false); frames = LoadField(d, Frames);
Frames = LoadField<int[]>(d, nameof(Frames), null); ignoreWorldTint = LoadField(d, IgnoreWorldTint);
IgnoreWorldTint = LoadField(d, nameof(IgnoreWorldTint), false); scale = LoadField(d, Scale);
Scale = LoadField(d, nameof(Scale), Scale);
FlipX = LoadField(d, nameof(FlipX), false); var flipX = LoadField(d, FlipX);
FlipY = LoadField(d, nameof(FlipY), false); var flipY = LoadField(d, FlipY);
var zRamp = LoadField(d, ZRamp);
Facings = LoadField(d, nameof(Facings), Facings); facings = LoadField(d, Facings);
if (Facings < 0) if (facings < 0)
{ {
reverseFacings = true; reverseFacings = true;
Facings = -Facings; facings = -facings;
} }
Offset = LoadField(d, nameof(Offset), Offset); var offset = LoadField(d, Offset);
BlendMode = LoadField(d, nameof(BlendMode), BlendMode); var blendMode = LoadField(d, BlendMode);
Func<int, IEnumerable<int>> getUsedFrames = frameCount => Func<int, IEnumerable<int>> getUsedFrames = frameCount =>
{ {
if (d.TryGetValue(nameof(Length), out var length) && length.Value == "*") if (d.TryGetValue(Length.Key, out var lengthYaml) && lengthYaml.Value == "*")
Length = Frames?.Length ?? frameCount - Start; length = frames?.Length ?? frameCount - start;
else else
Length = LoadField(d, nameof(Length), Length); length = LoadField(d, Length);
// Plays the animation forwards, and then in reverse // Plays the animation forwards, and then in reverse
if (LoadField(d, nameof(Reverses), false)) if (LoadField(d, Reverses))
{ {
var frames = Frames != null ? Frames.Skip(Start).Take(Length).ToArray() : Exts.MakeArray(Length, i => Start + i); var frames = this.frames != null ? this.frames.Skip(start).Take(length).ToArray() : Exts.MakeArray(length, i => start + i);
Frames = frames.Concat(frames.Skip(1).Take(Length - 2).Reverse()).ToArray(); this.frames = frames.Concat(frames.Skip(1).Take(length - 2).Reverse()).ToArray();
Length = 2 * Length - 2; length = 2 * length - 2;
Start = 0; start = 0;
} }
Stride = LoadField(d, nameof(Stride), Length); // Overrides Length with a custom stride
stride = LoadField(d, Stride.Key, length);
if (Length > Stride) if (length > stride)
throw new YamlException($"Sequence {sequence}.{animation}: Length must be <= stride"); throw new YamlException($"Sequence {sequence}.{animation}: Length must be <= stride");
if (Frames != null && Length > Frames.Length) if (frames != null && length > frames.Length)
throw new YamlException($"Sequence {sequence}.{animation}: Length must be <= Frames.Length"); throw new YamlException($"Sequence {sequence}.{animation}: Length must be <= Frames.Length");
var end = Start + (Facings - 1) * Stride + Length - 1; var end = start + (facings - 1) * stride + length - 1;
if (Frames != null) if (frames != null)
{ {
foreach (var f in Frames) foreach (var f in frames)
if (f < 0 || f >= frameCount) if (f < 0 || f >= frameCount)
throw new YamlException($"Sequence {sequence}.{animation} defines a Frames override that references frame {f}, but only [{Start}..{end}] actually exist"); throw new YamlException($"Sequence {sequence}.{animation} defines a Frames override that references frame {f}, but only [{start}..{end}] actually exist");
if (Start < 0 || end >= Frames.Length) if (start < 0 || end >= frames.Length)
throw new YamlException($"Sequence {sequence}.{animation} uses indices [{Start}..{end}] of the Frames list, but only {Frames.Length} frames are defined"); throw new YamlException($"Sequence {sequence}.{animation} uses indices [{start}..{end}] of the Frames list, but only {frames.Length} frames are defined");
} }
else if (Start < 0 || end >= frameCount) else if (start < 0 || end >= frameCount)
throw new YamlException($"Sequence {sequence}.{animation} uses frames [{Start}..{end}], but only [0..{frameCount - 1}] actually exist"); throw new YamlException($"Sequence {sequence}.{animation} uses frames [{start}..{end}], but only [0..{frameCount - 1}] actually exist");
if (ShadowStart >= 0 && ShadowStart + (Facings - 1) * Stride + Length > frameCount) if (shadowStart >= 0 && shadowStart + (facings - 1) * stride + length > frameCount)
throw new YamlException($"Sequence {sequence}.{animation}'s shadow frames use frames [{ShadowStart}..{ShadowStart + (Facings - 1) * Stride + Length - 1}], but only [0..{frameCount - 1}] actually exist"); throw new YamlException($"Sequence {sequence}.{animation}'s shadow frames use frames [{shadowStart}..{shadowStart + (facings - 1) * stride + length - 1}], but only [0..{frameCount - 1}] actually exist");
var usedFrames = new List<int>(); var usedFrames = new List<int>();
for (var facing = 0; facing < Facings; facing++) for (var facing = 0; facing < facings; facing++)
{ {
for (var frame = 0; frame < Length; frame++) for (var frame = 0; frame < length; frame++)
{ {
var i = transpose ? (frame % Length) * Facings + facing : var i = transpose ? (frame % length) * facings + facing :
(facing * Stride) + (frame % Length); (facing * stride) + (frame % length);
usedFrames.Add(Frames != null ? Frames[i] : Start + i); usedFrames.Add(frames != null ? frames[i] : start + i);
} }
} }
if (ShadowStart >= 0) if (shadowStart >= 0)
return usedFrames.Concat(usedFrames.Select(i => i + ShadowStart - Start)); return usedFrames.Concat(usedFrames.Select(i => i + shadowStart - start));
return usedFrames; return usedFrames;
}; };
if (d.TryGetValue(nameof(Combine), out var combine)) if (d.TryGetValue(Combine.Key, out var combine))
{ {
var combined = Enumerable.Empty<Sprite>(); var combined = Enumerable.Empty<Sprite>();
foreach (var sub in combine.Nodes) foreach (var sub in combine.Nodes)
@@ -339,19 +381,20 @@ namespace OpenRA.Mods.Common.Graphics
var sd = sub.Value.ToDictionary(); var sd = sub.Value.ToDictionary();
// Allow per-sprite offset, flipping, start, and length // Allow per-sprite offset, flipping, start, and length
var subStart = LoadField(sd, nameof(Start), 0); // These shouldn't inherit Start/Offset/etc from the main definition
var subOffset = LoadField(sd, nameof(Offset), Offset); var subStart = LoadField(sd, Start);
var subFlipX = LoadField(sd, nameof(FlipX), false); var subOffset = LoadField(sd, Offset);
var subFlipY = LoadField(sd, nameof(FlipY), false); var subFlipX = LoadField(sd, FlipX);
var subFrames = LoadField<int[]>(sd, nameof(Frames), null); var subFlipY = LoadField(sd, FlipY);
var subFrames = LoadField(sd, Frames);
var subLength = 0; var subLength = 0;
Func<int, IEnumerable<int>> subGetUsedFrames = subFrameCount => Func<int, IEnumerable<int>> subGetUsedFrames = subFrameCount =>
{ {
if (sd.TryGetValue(nameof(Length), out var subLengthYaml) && subLengthYaml.Value == "*") if (sd.TryGetValue(Length.Key, out var subLengthYaml) && subLengthYaml.Value == "*")
subLength = subFrames != null ? subFrames.Length : subFrameCount - subStart; subLength = subFrames != null ? subFrames.Length : subFrameCount - subStart;
else else
subLength = LoadField(sd, nameof(Length), Length); subLength = LoadField(sd, Length);
return subFrames != null ? subFrames.Skip(subStart).Take(subLength) : Enumerable.Range(subStart, subLength); return subFrames != null ? subFrames.Skip(subStart).Take(subLength) : Enumerable.Range(subStart, subLength);
}; };
@@ -363,11 +406,11 @@ namespace OpenRA.Mods.Common.Graphics
return null; return null;
var bounds = FlipRectangle(s.Bounds, subFlipX, subFlipY); var bounds = FlipRectangle(s.Bounds, subFlipX, subFlipY);
var dx = subOffset.X + Offset.X + (subFlipX ? -s.Offset.X : s.Offset.X); var dx = subOffset.X + offset.X + (subFlipX ? -s.Offset.X : s.Offset.X);
var dy = subOffset.Y + Offset.Y + (subFlipY ? -s.Offset.Y : s.Offset.Y); var dy = subOffset.Y + offset.Y + (subFlipY ? -s.Offset.Y : s.Offset.Y);
var dz = subOffset.Z + Offset.Z + s.Offset.Z + ZRamp * dy; var dz = subOffset.Z + offset.Z + s.Offset.Z + zRamp * dy;
return new Sprite(s.Sheet, bounds, ZRamp, new float3(dx, dy, dz), s.Channel, BlendMode); return new Sprite(s.Sheet, bounds, zRamp, new float3(dx, dy, dz), s.Channel, blendMode);
}).ToList(); }).ToList();
var frames = subFrames != null ? subFrames.Skip(subStart).Take(subLength).ToArray() : Exts.MakeArray(subLength, i => subStart + i); var frames = subFrames != null ? subFrames.Skip(subStart).Take(subLength).ToArray() : Exts.MakeArray(subLength, i => subStart + i);
@@ -387,39 +430,39 @@ namespace OpenRA.Mods.Common.Graphics
if (s == null) if (s == null)
return null; return null;
var bounds = FlipRectangle(s.Bounds, FlipX, FlipY); var bounds = FlipRectangle(s.Bounds, flipX, flipY);
var dx = Offset.X + (FlipX ? -s.Offset.X : s.Offset.X); var dx = offset.X + (flipX ? -s.Offset.X : s.Offset.X);
var dy = Offset.Y + (FlipY ? -s.Offset.Y : s.Offset.Y); var dy = offset.Y + (flipY ? -s.Offset.Y : s.Offset.Y);
var dz = Offset.Z + s.Offset.Z + ZRamp * dy; var dz = offset.Z + s.Offset.Z + zRamp * dy;
return new Sprite(s.Sheet, bounds, ZRamp, new float3(dx, dy, dz), s.Channel, BlendMode); return new Sprite(s.Sheet, bounds, zRamp, new float3(dx, dy, dz), s.Channel, blendMode);
}).ToArray(); }).ToArray();
} }
Alpha = LoadField(d, nameof(Alpha), (float[])null); alpha = LoadField(d, Alpha);
if (Alpha != null) if (alpha != null)
{ {
if (Alpha.Length == 1) if (alpha.Length == 1)
Alpha = Exts.MakeArray(Length, _ => Alpha[0]); alpha = Exts.MakeArray(length, _ => alpha[0]);
else if (Alpha.Length != Length) else if (alpha.Length != length)
throw new YamlException($"Sequence {sequence}.{animation} must define either 1 or {Length} Alpha values."); throw new YamlException($"Sequence {sequence}.{animation} must define either 1 or {length} Alpha values.");
} }
if (LoadField(d, nameof(AlphaFade), false)) if (LoadField(d, AlphaFade))
{ {
if (Alpha != null) if (alpha != null)
throw new YamlException($"Sequence {sequence}.{animation} cannot define both AlphaFade and Alpha."); throw new YamlException($"Sequence {sequence}.{animation} cannot define both AlphaFade and Alpha.");
Alpha = Exts.MakeArray(Length, i => float2.Lerp(1f, 0f, i / (Length - 1f))); alpha = Exts.MakeArray(length, i => float2.Lerp(1f, 0f, i / (length - 1f)));
} }
DepthSprite = LoadField<string>(d, nameof(DepthSprite), null); var depthSprite = LoadField(d, DepthSprite);
if (!string.IsNullOrEmpty(DepthSprite)) if (!string.IsNullOrEmpty(depthSprite))
{ {
var depthSpriteFrame = LoadField(d, nameof(DepthSpriteFrame), 0); var depthSpriteFrame = LoadField(d, DepthSpriteFrame);
var depthOffset = LoadField(d, nameof(DepthSpriteOffset), DepthSpriteOffset); var depthOffset = LoadField(d, DepthSpriteOffset);
IEnumerable<int> GetDepthFrame(int _) => new[] { depthSpriteFrame }; IEnumerable<int> GetDepthFrame(int _) => new[] { depthSpriteFrame };
var ds = cache[DepthSprite, GetDepthFrame][depthSpriteFrame]; var ds = cache[depthSprite, GetDepthFrame][depthSpriteFrame];
sprites = sprites.Select(s => sprites = sprites.Select(s =>
{ {
@@ -436,21 +479,20 @@ namespace OpenRA.Mods.Common.Graphics
}).ToArray(); }).ToArray();
} }
var hasEmbeddedPalette = LoadField<bool>(d, nameof(HasEmbeddedPalette), HasEmbeddedPalette); if (LoadField(d, HasEmbeddedPalette))
if (hasEmbeddedPalette)
{ {
var src = GetSpriteSrc(modData, tileSet, sequence, animation, info.Value, d); var src = GetSpriteSrc(modData, tileSet, sequence, animation, info.Value, d);
var metadata = cache.FrameMetadata(src); var metadata = cache.FrameMetadata(src);
var i = Frames != null ? Frames[0] : Start; var i = frames != null ? frames[0] : start;
var palettes = metadata?.GetOrDefault<EmbeddedSpritePalette>(); var palettes = metadata?.GetOrDefault<EmbeddedSpritePalette>();
if (palettes == null || !palettes.TryGetPaletteForFrame(i, out EmbeddedPalette)) if (palettes == null || !palettes.TryGetPaletteForFrame(i, out EmbeddedPalette))
throw new YamlException($"Cannot export palette from {src}: frame {i} does not define an embedded palette"); throw new YamlException($"Cannot export palette from {src}: frame {i} does not define an embedded palette");
} }
var boundSprites = SpriteBounds(sprites, Frames, Start, Facings, Length, Stride, transpose); var boundSprites = SpriteBounds(sprites, frames, start, facings, length, stride, transpose);
if (ShadowStart > 0) if (shadowStart > 0)
boundSprites = boundSprites.Concat(SpriteBounds(sprites, Frames, ShadowStart, Facings, Length, Stride, transpose)); boundSprites = boundSprites.Concat(SpriteBounds(sprites, frames, shadowStart, facings, length, stride, transpose));
Bounds = boundSprites.Union(); Bounds = boundSprites.Union();
} }
@@ -481,29 +523,29 @@ namespace OpenRA.Mods.Common.Graphics
public Sprite GetSprite(int frame) public Sprite GetSprite(int frame)
{ {
return GetSprite(Start, frame, WAngle.Zero); return GetSprite(start, frame, WAngle.Zero);
} }
public Sprite GetSprite(int frame, WAngle facing) public Sprite GetSprite(int frame, WAngle facing)
{ {
return GetSprite(Start, frame, facing); return GetSprite(start, frame, facing);
} }
public Sprite GetShadow(int frame, WAngle facing) public Sprite GetShadow(int frame, WAngle facing)
{ {
return ShadowStart >= 0 ? GetSprite(ShadowStart, frame, facing) : null; return shadowStart >= 0 ? GetSprite(shadowStart, frame, facing) : null;
} }
protected virtual Sprite GetSprite(int start, int frame, WAngle facing) protected virtual Sprite GetSprite(int start, int frame, WAngle facing)
{ {
var f = GetFacingFrameOffset(facing); var f = GetFacingFrameOffset(facing);
if (reverseFacings) if (reverseFacings)
f = (Facings - f) % Facings; f = (facings - f) % facings;
var i = transpose ? (frame % Length) * Facings + f : var i = transpose ? (frame % length) * facings + f :
(f * Stride) + (frame % Length); (f * stride) + (frame % length);
var j = Frames != null ? Frames[i] : start + i; var j = frames != null ? frames[i] : start + i;
if (sprites[j] == null) if (sprites[j] == null)
throw new InvalidOperationException($"Attempted to query unloaded sprite from {Name}.{sequence} start={start} frame={frame} facing={facing}"); throw new InvalidOperationException($"Attempted to query unloaded sprite from {Name}.{sequence} start={start} frame={frame} facing={facing}");
@@ -512,12 +554,12 @@ namespace OpenRA.Mods.Common.Graphics
protected virtual int GetFacingFrameOffset(WAngle facing) protected virtual int GetFacingFrameOffset(WAngle facing)
{ {
return Util.IndexFacing(facing, Facings); return Util.IndexFacing(facing, facings);
} }
public virtual float GetAlpha(int frame) public virtual float GetAlpha(int frame)
{ {
return Alpha?[frame] ?? 1f; return alpha?[frame] ?? 1f;
} }
} }
} }

View File

@@ -44,21 +44,20 @@ namespace OpenRA.Mods.Common.Graphics
[Desc("A sprite sequence that can have tileset-specific variants.")] [Desc("A sprite sequence that can have tileset-specific variants.")]
public class TilesetSpecificSpriteSequence : DefaultSpriteSequence public class TilesetSpecificSpriteSequence : DefaultSpriteSequence
{ {
// These need to be public properties for the documentation generation to work.
[Desc("Dictionary of <string: string> with tileset name to override -> tileset name to use instead.")] [Desc("Dictionary of <string: string> with tileset name to override -> tileset name to use instead.")]
public static Dictionary<string, string> TilesetOverrides => null; static readonly SpriteSequenceField<Dictionary<string, string>> TilesetOverrides = new SpriteSequenceField<Dictionary<string, string>>(nameof(TilesetOverrides), null);
[Desc("Use `TilesetCodes` as defined in `mod.yaml` to add a letter as a second character " + [Desc("Use `TilesetCodes` as defined in `mod.yaml` to add a letter as a second character " +
"into the sprite filename like the Westwood 2.5D titles did for tileset-specific variants.")] "into the sprite filename like the Westwood 2.5D titles did for tileset-specific variants.")]
public static bool UseTilesetCode => false; static readonly SpriteSequenceField<bool> UseTilesetCode = new SpriteSequenceField<bool>(nameof(UseTilesetCode), false);
[Desc("Append a tileset-specific extension to the file name " + [Desc("Append a tileset-specific extension to the file name " +
"- either as defined in `mod.yaml`'s `TilesetExtensions` (if `UseTilesetExtension` is used) " + "- either as defined in `mod.yaml`'s `TilesetExtensions` (if `UseTilesetExtension` is used) " +
"or the default hardcoded one for this sequence type (.shp).")] "or the default hardcoded one for this sequence type (.shp).")]
public static bool AddExtension => true; static readonly SpriteSequenceField<bool> AddExtension = new SpriteSequenceField<bool>(nameof(AddExtension), true);
[Desc("Whether `mod.yaml`'s `TilesetExtensions` should be used with the sequence's file name.")] [Desc("Whether `mod.yaml`'s `TilesetExtensions` should be used with the sequence's file name.")]
public static bool UseTilesetExtension { get; private set; } static readonly SpriteSequenceField<bool> UseTilesetExtension = new SpriteSequenceField<bool>(nameof(UseTilesetExtension), false);
public TilesetSpecificSpriteSequence(ModData modData, string tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info) public TilesetSpecificSpriteSequence(ModData modData, string tileSet, SpriteCache cache, ISpriteSequenceLoader loader, string sequence, string animation, MiniYaml info)
: base(modData, tileSet, cache, loader, sequence, animation, info) { } : base(modData, tileSet, cache, loader, sequence, animation, info) { }
@@ -81,16 +80,15 @@ namespace OpenRA.Mods.Common.Graphics
var spriteName = sprite ?? sequence; var spriteName = sprite ?? sequence;
if (LoadField(d, nameof(UseTilesetCode), UseTilesetCode)) if (LoadField(d, UseTilesetCode))
{ {
if (loader.TilesetCodes.TryGetValue(ResolveTilesetId(tileSet, d), out var code)) if (loader.TilesetCodes.TryGetValue(ResolveTilesetId(tileSet, d), out var code))
spriteName = spriteName.Substring(0, 1) + code + spriteName.Substring(2, spriteName.Length - 2); spriteName = spriteName.Substring(0, 1) + code + spriteName.Substring(2, spriteName.Length - 2);
} }
if (LoadField(d, nameof(AddExtension), AddExtension)) if (LoadField(d, AddExtension))
{ {
UseTilesetExtension = LoadField(d, nameof(UseTilesetExtension), UseTilesetExtension); if (LoadField(d, UseTilesetExtension) && loader.TilesetExtensions.TryGetValue(ResolveTilesetId(tileSet, d), out var tilesetExtension))
if (UseTilesetExtension && loader.TilesetExtensions.TryGetValue(ResolveTilesetId(tileSet, d), out var tilesetExtension))
return spriteName + tilesetExtension; return spriteName + tilesetExtension;
return spriteName + loader.DefaultSpriteExtension; return spriteName + loader.DefaultSpriteExtension;