Add cursor palette support to GIMP/Jasc palette definitions.

This commit is contained in:
Matthias Mailänder
2020-01-05 15:14:10 +01:00
committed by abcdefg30
parent f0b69f8b8d
commit c43d581e7f

View File

@@ -12,6 +12,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using OpenRA.FileSystem;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
@@ -19,7 +20,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Load a GIMP .gpl or JASC .pal palette file. Supports per-color alpha.")]
class PaletteFromGimpOrJascFileInfo : ITraitInfo
class PaletteFromGimpOrJascFileInfo : ITraitInfo, IProvidesCursorPaletteInfo
{
[PaletteDefinition]
[FieldLoader.Require]
@@ -45,32 +46,25 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Index set to be fully transparent/invisible.")]
public readonly int TransparentIndex = 0;
[Desc("Whether this palette is available for cursors.")]
public readonly bool CursorPalette = false;
public object Create(ActorInitializer init) { return new PaletteFromGimpOrJascFile(init.World, this); }
}
class PaletteFromGimpOrJascFile : ILoadsPalettes, IProvidesAssetBrowserPalettes
{
readonly World world;
readonly PaletteFromGimpOrJascFileInfo info;
string IProvidesCursorPaletteInfo.Palette { get { return CursorPalette ? Name : null; } }
public PaletteFromGimpOrJascFile(World world, PaletteFromGimpOrJascFileInfo info)
ImmutablePalette IProvidesCursorPaletteInfo.ReadPalette(IReadOnlyFileSystem fileSystem)
{
this.world = world;
this.info = info;
}
public void LoadPalettes(WorldRenderer wr)
{
var colors = new uint[Palette.Size];
using (var s = world.Map.Open(info.Filename))
using (var s = fileSystem.Open(Filename))
{
var colors = new uint[Palette.Size];
using (var lines = s.ReadAllLines().GetEnumerator())
{
if (lines == null)
return;
throw new InvalidDataException("File {0} is empty.".F(Filename));
if (!lines.MoveNext() || (lines.Current != "GIMP Palette" && lines.Current != "JASC-PAL"))
throw new InvalidDataException("File `{0}` is not a valid GIMP or JASC palette.".F(info.Filename));
throw new InvalidDataException("File `{0}` is not a valid GIMP or JASC palette.".F(Filename));
byte r, g, b, a;
a = 255;
@@ -100,11 +94,11 @@ namespace OpenRA.Mods.Common.Traits
var noAlpha = rgba.Length > 3 ? !byte.TryParse(rgba[3], out a) : true;
// Index should be completely transparent/background color
if (i == info.TransparentIndex)
if (i == TransparentIndex)
colors[i] = 0;
else if (noAlpha)
colors[i] = (uint)Color.FromArgb(r, g, b).ToArgb();
else if (info.Premultiply)
else if (Premultiply)
colors[i] = (uint)Color.FromArgb(a, r * a / 255, g * a / 255, b * a / 255).ToArgb();
else
colors[i] = (uint)Color.FromArgb(a, r, g, b).ToArgb();
@@ -112,9 +106,26 @@ namespace OpenRA.Mods.Common.Traits
i++;
}
}
}
wr.AddPalette(info.Name, new ImmutablePalette(colors), info.AllowModifiers);
return new ImmutablePalette(colors);
}
}
}
class PaletteFromGimpOrJascFile : ILoadsPalettes, IProvidesAssetBrowserPalettes
{
readonly World world;
readonly PaletteFromGimpOrJascFileInfo info;
public PaletteFromGimpOrJascFile(World world, PaletteFromGimpOrJascFileInfo info)
{
this.world = world;
this.info = info;
}
public void LoadPalettes(WorldRenderer wr)
{
wr.AddPalette(info.Name, ((IProvidesCursorPaletteInfo)info).ReadPalette(world.Map), info.AllowModifiers);
}
public IEnumerable<string> PaletteNames