Move Palette traits to their own directory.

Also adds missing TraitLocation definitions.
This commit is contained in:
Paul Chote
2021-04-23 21:27:34 +01:00
committed by teinarss
parent 3bc42543fa
commit 98caae106f
13 changed files with 10 additions and 6 deletions

View File

@@ -1,78 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2021 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;
using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.World | SystemActors.EditorWorld)]
[Desc("Create a color picker palette from another palette.")]
class ColorPickerPaletteInfo : TraitInfo
{
[PaletteDefinition]
[FieldLoader.Require]
[Desc("Internal palette name.")]
public readonly string Name = null;
[PaletteReference]
[FieldLoader.Require]
[Desc("The name of the palette to base off.")]
public readonly string BasePalette = null;
[FieldLoader.Require]
[Desc("Remap these indices to player colors.")]
public readonly int[] RemapIndex = { };
[Desc("Allow palette modifiers to change the palette.")]
public readonly bool AllowModifiers = true;
public override object Create(ActorInitializer init) { return new ColorPickerPalette(init.World, this); }
}
class ColorPickerPalette : ILoadsPalettes, IProvidesAssetBrowserColorPickerPalettes, ITickRender
{
readonly ColorPickerPaletteInfo info;
readonly ColorPickerManagerInfo colorManager;
Color color;
public ColorPickerPalette(World world, ColorPickerPaletteInfo info)
{
// All users need to use the same TraitInfo instance, chosen as the default mod rules
colorManager = Game.ModData.DefaultRules.Actors[SystemActors.World].TraitInfo<ColorPickerManagerInfo>();
this.info = info;
}
void ILoadsPalettes.LoadPalettes(WorldRenderer wr)
{
color = colorManager.Color;
var (_, h, s, _) = color.ToAhsv();
var remap = new PlayerColorRemap(info.RemapIndex, h, s);
wr.AddPalette(info.Name, new ImmutablePalette(wr.Palette(info.BasePalette).Palette, remap), info.AllowModifiers);
}
IEnumerable<string> IProvidesAssetBrowserColorPickerPalettes.ColorPickerPaletteNames { get { yield return info.Name; } }
void ITickRender.TickRender(WorldRenderer wr, Actor self)
{
if (color == colorManager.Color)
return;
color = colorManager.Color;
var (_, h, s, _) = color.ToAhsv();
var remap = new PlayerColorRemap(info.RemapIndex, h, s);
wr.ReplacePalette(info.Name, new ImmutablePalette(wr.Palette(info.BasePalette).Palette, remap));
}
}
}

View File

@@ -1,91 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2020 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.IO;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
[TraitLocation(SystemActors.World | SystemActors.EditorWorld)]
[Desc("Define a palette by swapping palette indices.")]
public class IndexedPaletteInfo : TraitInfo, IRulesetLoaded
{
[PaletteDefinition]
[FieldLoader.Require]
[Desc("Internal palette name")]
public readonly string Name = null;
[PaletteReference]
[Desc("The name of the palette to base off.")]
public readonly string BasePalette = null;
[FieldLoader.Require]
[Desc("Indices from BasePalette to be swapped with ReplaceIndex.")]
public readonly int[] Index = { };
[FieldLoader.Require]
[Desc("Indices from BasePalette to replace from Index.")]
public readonly int[] ReplaceIndex = { };
[Desc("Allow palette modifiers to change the palette.")]
public readonly bool AllowModifiers = true;
public override object Create(ActorInitializer init) { return new IndexedPalette(this); }
public void RulesetLoaded(Ruleset rules, ActorInfo ai)
{
if (Index.Length != ReplaceIndex.Length)
throw new YamlException($"ReplaceIndex length does not match Index length for palette {Name}");
}
}
public class IndexedPalette : ILoadsPalettes
{
readonly IndexedPaletteInfo info;
public IndexedPalette(IndexedPaletteInfo info)
{
this.info = info;
}
public void LoadPalettes(WorldRenderer wr)
{
var basePalette = wr.Palette(info.BasePalette).Palette;
var pal = new ImmutablePalette(basePalette, new IndexedColorRemap(basePalette, info.Index, info.ReplaceIndex));
wr.AddPalette(info.Name, pal, info.AllowModifiers);
}
}
public class IndexedColorRemap : IPaletteRemap
{
Dictionary<int, int> replacements = new Dictionary<int, int>();
IPalette basePalette;
public IndexedColorRemap(IPalette basePalette, int[] ramp, int[] remap)
{
this.basePalette = basePalette;
if (ramp.Length != remap.Length)
throw new InvalidDataException("ramp and remap lengths do no match.");
for (var i = 0; i < ramp.Length; i++)
replacements[ramp[i]] = remap[i];
}
public Color GetRemappedColor(Color original, int index)
{
return replacements.TryGetValue(index, out var c)
? basePalette.GetColor(c) : original;
}
}
}

View File

@@ -1,70 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2020 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 OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
[Desc("Define a player palette by swapping palette indices.")]
public class IndexedPlayerPaletteInfo : TraitInfo, IRulesetLoaded
{
[PaletteReference]
[Desc("The name of the palette to base off.")]
public readonly string BasePalette = null;
[PaletteDefinition(true)]
[Desc("The prefix for the resulting player palettes")]
public readonly string BaseName = "player";
[Desc("Remap these indices to player colors.")]
public readonly int[] RemapIndex = { };
[Desc("Allow palette modifiers to change the palette.")]
public readonly bool AllowModifiers = true;
public readonly Dictionary<string, int[]> PlayerIndex;
public override object Create(ActorInitializer init) { return new IndexedPlayerPalette(this); }
public void RulesetLoaded(Ruleset rules, ActorInfo ai)
{
foreach (var p in PlayerIndex)
if (p.Value.Length != RemapIndex.Length)
throw new YamlException($"PlayerIndex for player `{p.Key}` length does not match RemapIndex!");
}
}
public class IndexedPlayerPalette : ILoadsPlayerPalettes
{
readonly IndexedPlayerPaletteInfo info;
public IndexedPlayerPalette(IndexedPlayerPaletteInfo info)
{
this.info = info;
}
public void LoadPlayerPalettes(WorldRenderer wr, string playerName, Color color, bool replaceExisting)
{
var basePalette = wr.Palette(info.BasePalette).Palette;
ImmutablePalette pal;
if (info.PlayerIndex.TryGetValue(playerName, out var remap))
pal = new ImmutablePalette(basePalette, new IndexedColorRemap(basePalette, info.RemapIndex, remap));
else
pal = new ImmutablePalette(basePalette);
wr.AddPalette(info.BaseName + playerName, pal, info.AllowModifiers, replaceExisting);
}
}
}

View File

@@ -1,66 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2020 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 : TraitInfo, IProvidesCursorPaletteInfo
{
[PaletteDefinition]
[FieldLoader.Require]
[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 override object Create(ActorInitializer init) { return new PaletteFromEmbeddedSpritePalette(this); }
string IProvidesCursorPaletteInfo.Palette => 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; } }
}
}

View File

@@ -1,82 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2020 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 OpenRA.FileSystem;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.World | SystemActors.EditorWorld)]
[Desc("Load VGA palette (.pal) registers.")]
class PaletteFromFileInfo : TraitInfo, IProvidesCursorPaletteInfo
{
[PaletteDefinition]
[FieldLoader.Require]
[Desc("internal palette name")]
public readonly string Name = null;
[Desc("If defined, load the palette only for this tileset.")]
public readonly string Tileset = null;
[FieldLoader.Require]
[Desc("filename to load")]
public readonly string Filename = null;
[Desc("Map listed indices to transparent. Ignores previous color.")]
public readonly int[] TransparentIndex = { 0 };
[Desc("Map listed indices to shadow. Ignores previous color.")]
public readonly int[] ShadowIndex = { };
public readonly bool AllowModifiers = true;
[Desc("Whether this palette is available for cursors.")]
public readonly bool CursorPalette = false;
public override object Create(ActorInitializer init) { return new PaletteFromFile(init.World, this); }
string IProvidesCursorPaletteInfo.Palette => CursorPalette ? Name : null;
ImmutablePalette IProvidesCursorPaletteInfo.ReadPalette(IReadOnlyFileSystem fileSystem)
{
return new ImmutablePalette(fileSystem.Open(Filename), TransparentIndex, ShadowIndex);
}
}
class PaletteFromFile : ILoadsPalettes, IProvidesAssetBrowserPalettes
{
readonly World world;
readonly PaletteFromFileInfo info;
public PaletteFromFile(World world, PaletteFromFileInfo info)
{
this.world = world;
this.info = info;
}
public void LoadPalettes(WorldRenderer wr)
{
if (info.Tileset == null || info.Tileset.ToLowerInvariant() == world.Map.Tileset.ToLowerInvariant())
wr.AddPalette(info.Name, ((IProvidesCursorPaletteInfo)info).ReadPalette(world.Map), info.AllowModifiers);
}
public IEnumerable<string> PaletteNames
{
get
{
// Only expose the palette if it is available for the shellmap's tileset (which is a requirement for its use).
if (info.Tileset == null || info.Tileset == world.Map.Rules.TerrainInfo.Id)
yield return info.Name;
}
}
}
}

View File

@@ -1,140 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2020 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;
using System.Collections.Generic;
using System.IO;
using OpenRA.FileSystem;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.World | SystemActors.EditorWorld)]
[Desc("Load a GIMP .gpl or JASC .pal palette file. Supports per-color alpha.")]
class PaletteFromGimpOrJascFileInfo : TraitInfo, IProvidesCursorPaletteInfo
{
[PaletteDefinition]
[FieldLoader.Require]
[Desc("Palette name used internally.")]
public readonly string Name = null;
[Desc("Defines for which tileset IDs this palette should be loaded.",
"If none specified, it applies to all tileset IDs not explicitly excluded.")]
public readonly HashSet<string> Tilesets = new HashSet<string>();
[Desc("Don't load palette for these tileset IDs.")]
public readonly HashSet<string> ExcludeTilesets = new HashSet<string>();
[FieldLoader.Require]
[Desc("Name of the file to load.")]
public readonly string Filename = null;
[Desc("Premultiply colors with their alpha values.")]
public readonly bool Premultiply = true;
public readonly bool AllowModifiers = true;
[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 override object Create(ActorInitializer init) { return new PaletteFromGimpOrJascFile(init.World, this); }
string IProvidesCursorPaletteInfo.Palette => CursorPalette ? Name : null;
ImmutablePalette IProvidesCursorPaletteInfo.ReadPalette(IReadOnlyFileSystem fileSystem)
{
using (var s = fileSystem.Open(Filename))
{
var colors = new uint[Palette.Size];
using (var lines = s.ReadAllLines().GetEnumerator())
{
if (!lines.MoveNext() || (lines.Current != "GIMP Palette" && lines.Current != "JASC-PAL"))
throw new InvalidDataException($"File `{Filename}` is not a valid GIMP or JASC palette.");
byte a;
a = 255;
var i = 0;
while (lines.MoveNext() && i < Palette.Size)
{
// Skip until first color. Ignore # comments, Name/Columns and blank lines as well as JASC header values.
if (string.IsNullOrEmpty(lines.Current) || !char.IsDigit(lines.Current.Trim()[0]) || lines.Current == "0100" || lines.Current == "256")
continue;
var rgba = lines.Current.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
if (rgba.Length < 3)
throw new InvalidDataException($"Invalid RGB(A) triplet/quartet: ({string.Join(" ", rgba)})");
if (!byte.TryParse(rgba[0], out var r))
throw new InvalidDataException($"Invalid R value: {rgba[0]}");
if (!byte.TryParse(rgba[1], out var g))
throw new InvalidDataException($"Invalid G value: {rgba[1]}");
if (!byte.TryParse(rgba[2], out var b))
throw new InvalidDataException($"Invalid B value: {rgba[2]}");
// Check if color has a (valid) alpha value.
// Note: We can't throw on "rgba.Length > 3 but parse failed", because in GIMP palettes the 'invalid' value is probably a color name string.
var noAlpha = rgba.Length > 3 ? !byte.TryParse(rgba[3], out a) : true;
// Index should be completely transparent/background color
if (i == TransparentIndex)
colors[i] = 0;
else if (noAlpha)
colors[i] = (uint)Color.FromArgb(r, g, b).ToArgb();
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();
i++;
}
}
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
{
get
{
// Only expose the palette if it is available for the shellmap's tileset (which is a requirement for its use).
if ((info.Tilesets.Count == 0 || info.Tilesets.Contains(world.Map.Rules.TerrainInfo.Id))
&& !info.ExcludeTilesets.Contains(world.Map.Rules.TerrainInfo.Id))
yield return info.Name;
}
}
}
}

View File

@@ -1,81 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2020 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 OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.World | SystemActors.EditorWorld)]
[Desc("Create a palette by applying alpha transparency to another palette.")]
class PaletteFromPaletteWithAlphaInfo : TraitInfo
{
[PaletteDefinition]
[FieldLoader.Require]
[Desc("Internal palette name")]
public readonly string Name = null;
[PaletteReference]
[FieldLoader.Require]
[Desc("The name of the palette to base off.")]
public readonly string BasePalette = null;
[Desc("Allow palette modifiers to change the palette.")]
public readonly bool AllowModifiers = true;
[Desc("Alpha component that is applied to the base palette.")]
public readonly float Alpha = 1.0f;
[Desc("Premultiply color by the alpha component.")]
public readonly bool Premultiply = true;
public override object Create(ActorInitializer init) { return new PaletteFromPaletteWithAlpha(this); }
}
class PaletteFromPaletteWithAlpha : ILoadsPalettes, IProvidesAssetBrowserPalettes
{
readonly PaletteFromPaletteWithAlphaInfo info;
public PaletteFromPaletteWithAlpha(PaletteFromPaletteWithAlphaInfo info) { this.info = info; }
public void LoadPalettes(WorldRenderer wr)
{
var remap = new AlphaPaletteRemap(info.Alpha, info.Premultiply);
wr.AddPalette(info.Name, new ImmutablePalette(wr.Palette(info.BasePalette).Palette, remap), info.AllowModifiers);
}
public IEnumerable<string> PaletteNames { get { yield return info.Name; } }
}
class AlphaPaletteRemap : IPaletteRemap
{
readonly float alpha;
readonly bool premultiply;
public AlphaPaletteRemap(float alpha, bool premultiply)
{
this.alpha = alpha;
this.premultiply = premultiply;
}
public Color GetRemappedColor(Color original, int index)
{
var a = (int)(original.A * alpha).Clamp(0, 255);
var r = premultiply ? (int)(alpha * original.R + 0.5f).Clamp(0, 255) : original.R;
var g = premultiply ? (int)(alpha * original.G + 0.5f).Clamp(0, 255) : original.G;
var b = premultiply ? (int)(alpha * original.B + 0.5f).Clamp(0, 255) : original.B;
return Color.FromArgb(a, r, g, b);
}
}
}

View File

@@ -1,57 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2020 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 OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.World | SystemActors.EditorWorld)]
[Desc("Create player palettes by applying alpha transparency to another player palette.")]
class PaletteFromPlayerPaletteWithAlphaInfo : TraitInfo
{
[FieldLoader.Require]
[PaletteDefinition(true)]
[Desc("The prefix for the resulting player palettes")]
public readonly string BaseName = null;
[FieldLoader.Require]
[PaletteReference(true)]
[Desc("The name of the player palette to base off.")]
public readonly string BasePalette = null;
[Desc("Allow palette modifiers to change the palette.")]
public readonly bool AllowModifiers = true;
[Desc("Alpha component that is applied to the base palette.")]
public readonly float Alpha = 1.0f;
[Desc("Premultiply color by the alpha component.")]
public readonly bool Premultiply = true;
public override object Create(ActorInitializer init) { return new PaletteFromPlayerPaletteWithAlpha(this); }
}
class PaletteFromPlayerPaletteWithAlpha : ILoadsPlayerPalettes
{
readonly PaletteFromPlayerPaletteWithAlphaInfo info;
public PaletteFromPlayerPaletteWithAlpha(PaletteFromPlayerPaletteWithAlphaInfo info) { this.info = info; }
public void LoadPlayerPalettes(WorldRenderer wr, string playerName, Color color, bool replaceExisting)
{
var remap = new AlphaPaletteRemap(info.Alpha, info.Premultiply);
var pal = new ImmutablePalette(wr.Palette(info.BasePalette + playerName).Palette, remap);
wr.AddPalette(info.BaseName + playerName, pal, info.AllowModifiers, replaceExisting);
}
}
}

View File

@@ -1,93 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2020 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;
using System.Collections.Generic;
using OpenRA.FileFormats;
using OpenRA.FileSystem;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.World | SystemActors.EditorWorld)]
[Desc("Load a PNG and use its embedded palette.")]
class PaletteFromPngInfo : TraitInfo, IProvidesCursorPaletteInfo
{
[PaletteDefinition]
[FieldLoader.Require]
[Desc("Internal palette name")]
public readonly string Name = null;
[Desc("If defined, load the palette only for this tileset.")]
public readonly string Tileset = null;
[FieldLoader.Require]
[Desc("Filename to load")]
public readonly string Filename = null;
[Desc("Map listed indices to shadow. Ignores previous color.")]
public readonly int[] ShadowIndex = { };
public readonly bool AllowModifiers = true;
[Desc("Whether this palette is available for cursors.")]
public readonly bool CursorPalette = false;
public override object Create(ActorInitializer init) { return new PaletteFromPng(init.World, this); }
string IProvidesCursorPaletteInfo.Palette => CursorPalette ? Name : null;
ImmutablePalette IProvidesCursorPaletteInfo.ReadPalette(IReadOnlyFileSystem fileSystem)
{
var png = new Png(fileSystem.Open(Filename));
if (png.Palette == null)
throw new InvalidOperationException($"Unable to load palette `{Name}` from non-paletted png `{Filename}`");
var colors = new uint[Palette.Size];
for (var i = 0; i < png.Palette.Length; i++)
colors[i] = (uint)png.Palette[i].ToArgb();
return new ImmutablePalette(colors);
}
}
class PaletteFromPng : ILoadsPalettes, IProvidesAssetBrowserPalettes
{
readonly World world;
readonly PaletteFromPngInfo info;
public PaletteFromPng(World world, PaletteFromPngInfo info)
{
this.world = world;
this.info = info;
}
public void LoadPalettes(WorldRenderer wr)
{
if (info.Tileset != null && info.Tileset.ToLowerInvariant() != world.Map.Tileset.ToLowerInvariant())
return;
wr.AddPalette(info.Name, ((IProvidesCursorPaletteInfo)info).ReadPalette(world.Map), info.AllowModifiers);
}
public IEnumerable<string> PaletteNames
{
get
{
// Only expose the palette if it is available for the shellmap's tileset (which is a requirement for its use).
if (info.Tileset == null || info.Tileset == world.Map.Rules.TerrainInfo.Id)
yield return info.Name;
}
}
}
}

View File

@@ -1,75 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2020 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.Linq;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[TraitLocation(SystemActors.World | SystemActors.EditorWorld)]
[Desc("Creates a single color palette without any base palette file.")]
class PaletteFromRGBAInfo : TraitInfo
{
[PaletteDefinition]
[FieldLoader.Require]
[Desc("internal palette name")]
public readonly string Name = null;
[Desc("If defined, load the palette only for this tileset.")]
public readonly string Tileset = null;
[Desc("red color component")]
public readonly int R = 0;
[Desc("green color component")]
public readonly int G = 0;
[Desc("blue color component")]
public readonly int B = 0;
[Desc("alpha channel (transparency)")]
public readonly int A = 255;
public readonly bool AllowModifiers = true;
[Desc("Index set to be fully transparent/invisible.")]
public readonly int TransparentIndex = 0;
public override object Create(ActorInitializer init) { return new PaletteFromRGBA(init.World, this); }
}
class PaletteFromRGBA : ILoadsPalettes
{
readonly World world;
readonly PaletteFromRGBAInfo info;
public PaletteFromRGBA(World world, PaletteFromRGBAInfo info)
{
this.world = world;
this.info = info;
}
public void LoadPalettes(WorldRenderer wr)
{
// Enable palette only for a specific tileset
if (info.Tileset != null && info.Tileset.ToLowerInvariant() != world.Map.Tileset.ToLowerInvariant())
return;
var a = info.A / 255f;
var r = (int)(a * info.R + 0.5f).Clamp(0, 255);
var g = (int)(a * info.G + 0.5f).Clamp(0, 255);
var b = (int)(a * info.B + 0.5f).Clamp(0, 255);
var c = (uint)Color.FromArgb(info.A, r, g, b).ToArgb();
wr.AddPalette(info.Name, new ImmutablePalette(Enumerable.Range(0, Palette.Size).Select(i => (i == info.TransparentIndex) ? 0 : c)), info.AllowModifiers);
}
}
}