Add EmbeddedSpritePalette sprite metadata.

This commit is contained in:
Paul Chote
2018-10-07 17:40:05 +00:00
parent dee6d03626
commit eb61c45e14
4 changed files with 106 additions and 0 deletions

View File

@@ -18,6 +18,26 @@ using OpenRA.Graphics;
namespace OpenRA.Mods.Common.Graphics namespace OpenRA.Mods.Common.Graphics
{ {
public class EmbeddedSpritePalette
{
readonly uint[] filePalette = null;
readonly Dictionary<int, uint[]> framePalettes = null;
public EmbeddedSpritePalette(uint[] filePalette = null, Dictionary<int, uint[]> framePalettes = null)
{
this.filePalette = filePalette;
this.framePalettes = framePalettes;
}
public bool TryGetPaletteForFrame(int frame, out uint[] palette)
{
if (framePalettes == null || !framePalettes.TryGetValue(frame, out palette))
palette = filePalette;
return palette != null;
}
}
public class DefaultSpriteSequenceLoader : ISpriteSequenceLoader public class DefaultSpriteSequenceLoader : ISpriteSequenceLoader
{ {
public Action<string> OnMissingSpriteError { get; set; } public Action<string> OnMissingSpriteError { get; set; }
@@ -93,6 +113,8 @@ namespace OpenRA.Mods.Common.Graphics
public int[] Frames { get; private set; } public int[] Frames { get; private set; }
public Rectangle Bounds { get; private set; } public Rectangle Bounds { get; private set; }
public readonly uint[] EmbeddedPalette;
protected virtual string GetSpriteSrc(ModData modData, TileSet tileSet, string sequence, string animation, string sprite, Dictionary<string, MiniYaml> d) protected virtual string GetSpriteSrc(ModData modData, TileSet tileSet, string sequence, string animation, string sprite, Dictionary<string, MiniYaml> d)
{ {
return sprite ?? sequence; return sprite ?? sequence;
@@ -286,6 +308,18 @@ namespace OpenRA.Mods.Common.Graphics
}).ToArray(); }).ToArray();
} }
var exportPalette = LoadField<string>(d, "EmbeddedPalette", null);
if (exportPalette != null)
{
var src = GetSpriteSrc(modData, tileSet, sequence, animation, info.Value, d);
var metadata = cache.FrameMetadata(src);
var i = Frames != null ? Frames[0] : Start;
var palettes = metadata != null ? metadata.GetOrDefault<EmbeddedSpritePalette>() : null;
if (palettes == null || !palettes.TryGetPaletteForFrame(i, out EmbeddedPalette))
throw new YamlException("Cannot export palettes from {0}: frame {1} does not define an embedded palette".F(src, i));
}
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));

View File

@@ -555,6 +555,7 @@
<Compile Include="Traits\World\DomainIndex.cs" /> <Compile Include="Traits\World\DomainIndex.cs" />
<Compile Include="Traits\World\Locomotor.cs" /> <Compile Include="Traits\World\Locomotor.cs" />
<Compile Include="Traits\World\JumpjetLocomotor.cs" /> <Compile Include="Traits\World\JumpjetLocomotor.cs" />
<Compile Include="Traits\World\PaletteFromEmbeddedSpritePalette.cs" />
<Compile Include="Traits\World\SubterraneanLocomotor.cs" /> <Compile Include="Traits\World\SubterraneanLocomotor.cs" />
<Compile Include="Traits\World\LoadWidgetAtGameStart.cs" /> <Compile Include="Traits\World\LoadWidgetAtGameStart.cs" />
<Compile Include="Traits\World\MPStartLocations.cs" /> <Compile Include="Traits\World\MPStartLocations.cs" />

View File

@@ -16,6 +16,7 @@ using System.IO;
using System.Linq; using System.Linq;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Primitives; using OpenRA.Primitives;
namespace OpenRA.Mods.Common.SpriteLoaders namespace OpenRA.Mods.Common.SpriteLoaders
@@ -68,6 +69,11 @@ namespace OpenRA.Mods.Common.SpriteLoaders
Array.Copy(png.Data, frameStart + y * png.Width, frames[i].Data, y * frames[i].Size.Width, frames[i].Size.Width); Array.Copy(png.Data, frameStart + y * png.Width, frames[i].Data, y * frames[i].Size.Width, frames[i].Size.Width);
} }
metadata = new TypeDictionary
{
new EmbeddedSpritePalette(png.Palette.Select(x => (uint)x.ToArgb()).ToArray())
};
return true; return true;
} }

View File

@@ -0,0 +1,65 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 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.Collections.Generic;
using System.Linq;
using OpenRA.FileSystem;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class PaletteFromEmbeddedSpritePaletteInfo : ITraitInfo, IProvidesCursorPaletteInfo
{
[FieldLoader.Require, PaletteDefinition]
[Desc("Internal palette name")]
public readonly string Name = null;
[FieldLoader.Require]
[Desc("Sequence image holding the palette definition")]
public readonly string Image = null;
[FieldLoader.Require]
[Desc("Sequence holding the palette definition")]
public readonly string Sequence = null;
[Desc("Allow palette modifiers to change the palette.")]
public readonly bool AllowModifiers = true;
[Desc("Whether this palette is available for cursors.")]
public readonly bool CursorPalette = false;
public object Create(ActorInitializer init) { return new PaletteFromEmbeddedSpritePalette(this); }
string IProvidesCursorPaletteInfo.Palette { get { return CursorPalette ? Name : null; } }
ImmutablePalette IProvidesCursorPaletteInfo.ReadPalette(IReadOnlyFileSystem fileSystem)
{
var sequence = (DefaultSpriteSequence)Game.ModData.DefaultSequences.Values.First().GetSequence(Image, Sequence);
return new ImmutablePalette(sequence.EmbeddedPalette);
}
}
public class PaletteFromEmbeddedSpritePalette : ILoadsPalettes, IProvidesAssetBrowserPalettes
{
readonly PaletteFromEmbeddedSpritePaletteInfo info;
public PaletteFromEmbeddedSpritePalette(PaletteFromEmbeddedSpritePaletteInfo info) { this.info = info; }
public void LoadPalettes(WorldRenderer wr)
{
var sequence = (DefaultSpriteSequence)wr.World.Map.Rules.Sequences.GetSequence(info.Image, info.Sequence);
wr.AddPalette(info.Name, new ImmutablePalette(sequence.EmbeddedPalette), info.AllowModifiers);
}
public IEnumerable<string> PaletteNames { get { yield return info.Name; } }
}
}