From c43d581e7fdb77775409e68b09bf90863e41a578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sun, 5 Jan 2020 15:14:10 +0100 Subject: [PATCH] Add cursor palette support to GIMP/Jasc palette definitions. --- .../Traits/World/PaletteFromGimpOrJascFile.cs | 53 +++++++++++-------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/World/PaletteFromGimpOrJascFile.cs b/OpenRA.Mods.Common/Traits/World/PaletteFromGimpOrJascFile.cs index 9d64d0855a..33ad25c50b 100644 --- a/OpenRA.Mods.Common/Traits/World/PaletteFromGimpOrJascFile.cs +++ b/OpenRA.Mods.Common/Traits/World/PaletteFromGimpOrJascFile.cs @@ -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 PaletteNames