From 450dc70375d58d178ea7140e4b25bff91613c803 Mon Sep 17 00:00:00 2001 From: Andre Mohren Date: Sat, 29 Sep 2018 19:17:51 +0200 Subject: [PATCH] Refactored cursors.yaml to use palettes from rules. --- OpenRA.Game/Graphics/CursorProvider.cs | 22 +++++++-------- OpenRA.Game/Traits/TraitsInterfaces.cs | 8 ++++++ .../Traits/World/PaletteFromFile.cs | 15 +++++++++-- .../Traits/World/PaletteFromPng.cs | 27 +++++++++++++------ mods/cnc/cursors.yaml | 15 +++++------ mods/cnc/rules/palettes.yaml | 1 + mods/d2k/cursors.yaml | 13 +++------ mods/d2k/rules/palettes.yaml | 1 + mods/modcontent/cursors.yaml | 3 --- mods/modcontent/mod.yaml | 3 +++ mods/modcontent/rules.yaml | 5 ++++ mods/ra/cursors.yaml | 9 +++---- mods/ra/rules/palettes.yaml | 1 + mods/ts/cursors.yaml | 9 +++---- mods/ts/rules/palettes.yaml | 1 + 15 files changed, 78 insertions(+), 55 deletions(-) create mode 100644 mods/modcontent/rules.yaml diff --git a/OpenRA.Game/Graphics/CursorProvider.cs b/OpenRA.Game/Graphics/CursorProvider.cs index 2a2f7347fa..1c190e8b40 100644 --- a/OpenRA.Game/Graphics/CursorProvider.cs +++ b/OpenRA.Game/Graphics/CursorProvider.cs @@ -12,6 +12,7 @@ using System; using System.Collections.Generic; using System.Linq; +using OpenRA.Traits; namespace OpenRA.Graphics { @@ -26,21 +27,18 @@ namespace OpenRA.Graphics var sequenceYaml = MiniYaml.Merge(modData.Manifest.Cursors.Select( s => MiniYaml.FromStream(fileSystem.Open(s), s))); - var shadowIndex = new int[] { }; - var nodesDict = new MiniYaml(null, sequenceYaml).ToDictionary(); - if (nodesDict.ContainsKey("ShadowIndex")) - { - Array.Resize(ref shadowIndex, shadowIndex.Length + 1); - Exts.TryParseIntegerInvariant(nodesDict["ShadowIndex"].Value, - out shadowIndex[shadowIndex.Length - 1]); - } - var palettes = new Dictionary(); - foreach (var p in nodesDict["Palettes"].Nodes) - palettes.Add(p.Key, new ImmutablePalette(fileSystem.Open(p.Value.Value), shadowIndex)); + // Overwrite previous definitions if there are duplicates + var pals = new Dictionary(); + foreach (var p in modData.DefaultRules.Actors["world"].TraitInfos()) + if (p.Palette != null) + pals[p.Palette] = p; - Palettes = palettes.AsReadOnly(); + Palettes = nodesDict["Cursors"].Nodes.Select(n => n.Value.Value) + .Distinct() + .ToDictionary(p => p, p => pals[p].ReadPalette(modData.DefaultFileSystem)) + .AsReadOnly(); var frameCache = new FrameCache(fileSystem, modData.SpriteLoaders); var cursors = new Dictionary(); diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index c3ef7d7ba9..573fb6b607 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -14,6 +14,7 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Drawing; using OpenRA.Activities; +using OpenRA.FileSystem; using OpenRA.Graphics; using OpenRA.Network; using OpenRA.Primitives; @@ -271,6 +272,13 @@ namespace OpenRA.Traits IEnumerable ModifyScreenBounds(Actor self, WorldRenderer wr, IEnumerable r); } + [RequireExplicitImplementation] + public interface IProvidesCursorPaletteInfo : ITraitInfoInterface + { + string Palette { get; } + ImmutablePalette ReadPalette(IReadOnlyFileSystem fileSystem); + } + public interface ILoadsPalettes { void LoadPalettes(WorldRenderer wr); } public interface ILoadsPlayerPalettes { void LoadPlayerPalettes(WorldRenderer wr, string playerName, HSLColor playerColor, bool replaceExisting); } public interface IPaletteModifier { void AdjustPalette(IReadOnlyDictionary b); } diff --git a/OpenRA.Mods.Common/Traits/World/PaletteFromFile.cs b/OpenRA.Mods.Common/Traits/World/PaletteFromFile.cs index 20e3f061b5..dd8f4fb327 100644 --- a/OpenRA.Mods.Common/Traits/World/PaletteFromFile.cs +++ b/OpenRA.Mods.Common/Traits/World/PaletteFromFile.cs @@ -10,13 +10,14 @@ #endregion using System.Collections.Generic; +using OpenRA.FileSystem; using OpenRA.Graphics; using OpenRA.Traits; namespace OpenRA.Mods.Common.Traits { [Desc("Load VGA palette (.pal) registers.")] - class PaletteFromFileInfo : ITraitInfo + class PaletteFromFileInfo : ITraitInfo, IProvidesCursorPaletteInfo { [FieldLoader.Require, PaletteDefinition] [Desc("internal palette name")] @@ -34,7 +35,17 @@ namespace OpenRA.Mods.Common.Traits 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 PaletteFromFile(init.World, this); } + + string IProvidesCursorPaletteInfo.Palette { get { return CursorPalette ? Name : null; } } + + ImmutablePalette IProvidesCursorPaletteInfo.ReadPalette(IReadOnlyFileSystem fileSystem) + { + return new ImmutablePalette(fileSystem.Open(Filename), ShadowIndex); + } } class PaletteFromFile : ILoadsPalettes, IProvidesAssetBrowserPalettes @@ -50,7 +61,7 @@ namespace OpenRA.Mods.Common.Traits public void LoadPalettes(WorldRenderer wr) { if (info.Tileset == null || info.Tileset.ToLowerInvariant() == world.Map.Tileset.ToLowerInvariant()) - wr.AddPalette(info.Name, new ImmutablePalette(world.Map.Open(info.Filename), info.ShadowIndex), info.AllowModifiers); + wr.AddPalette(info.Name, ((IProvidesCursorPaletteInfo)info).ReadPalette(world.Map), info.AllowModifiers); } public IEnumerable PaletteNames diff --git a/OpenRA.Mods.Common/Traits/World/PaletteFromPng.cs b/OpenRA.Mods.Common/Traits/World/PaletteFromPng.cs index 74c9e6371b..96e43251fe 100644 --- a/OpenRA.Mods.Common/Traits/World/PaletteFromPng.cs +++ b/OpenRA.Mods.Common/Traits/World/PaletteFromPng.cs @@ -11,13 +11,14 @@ using System.Collections.Generic; using OpenRA.FileFormats; +using OpenRA.FileSystem; using OpenRA.Graphics; using OpenRA.Traits; namespace OpenRA.Mods.Common.Traits { [Desc("Load a PNG and use its embedded palette.")] - class PaletteFromPngInfo : ITraitInfo + class PaletteFromPngInfo : ITraitInfo, IProvidesCursorPaletteInfo { [FieldLoader.Require, PaletteDefinition] [Desc("Internal palette name")] @@ -35,7 +36,23 @@ namespace OpenRA.Mods.Common.Traits 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 PaletteFromPng(init.World, this); } + + string IProvidesCursorPaletteInfo.Palette { get { return CursorPalette ? Name : null; } } + + ImmutablePalette IProvidesCursorPaletteInfo.ReadPalette(IReadOnlyFileSystem fileSystem) + { + var png = new Png(fileSystem.Open(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 @@ -53,13 +70,7 @@ namespace OpenRA.Mods.Common.Traits if (info.Tileset != null && info.Tileset.ToLowerInvariant() != world.Map.Tileset.ToLowerInvariant()) return; - var png = new Png(world.Map.Open(info.Filename)); - var colors = new uint[Palette.Size]; - - for (var i = 0; i < png.Palette.Length; i++) - colors[i] = (uint)png.Palette[i].ToArgb(); - - wr.AddPalette(info.Name, new ImmutablePalette(colors), info.AllowModifiers); + wr.AddPalette(info.Name, ((IProvidesCursorPaletteInfo)info).ReadPalette(world.Map), info.AllowModifiers); } public IEnumerable PaletteNames diff --git a/mods/cnc/cursors.yaml b/mods/cnc/cursors.yaml index 96ed001f37..dbc9f6ee64 100644 --- a/mods/cnc/cursors.yaml +++ b/mods/cnc/cursors.yaml @@ -1,8 +1,5 @@ -Palettes: - cursor: temperat.pal - Cursors: - mouse2.shp: cursor + mouse2.shp: chrome scroll-t: Start: 1 Y: -12 @@ -139,7 +136,7 @@ Cursors: sell-vehicle: Start: 154 Length: 24 - mouse3.shp: cursor + mouse3.shp: chrome default: Start: 0 X: -16 @@ -151,7 +148,7 @@ Cursors: deploy-blocked: Start: 1 Length: 1 - mouse4.shp: cursor + mouse4.shp: chrome move: Start: 0 Length: 8 @@ -191,14 +188,14 @@ Cursors: assaultmove-blocked-minimap: Start: 40 Length: 1 - mouse5.shp: cursor + mouse5.shp: chrome guard: Start: 0 Length: 8 guard-minimap: Start: 8 Length: 8 - mouse6.shp: cursor + mouse6.shp: chrome goldwrench: Start: 0 Length: 3 @@ -229,7 +226,7 @@ Cursors: enter-blocked-minimap: Start: 17 Length: 1 - mouse7.shp: cursor + mouse7.shp: chrome attackoutsiderange: Start: 0 Length: 8 diff --git a/mods/cnc/rules/palettes.yaml b/mods/cnc/rules/palettes.yaml index 48dabbe795..2da87c9e57 100644 --- a/mods/cnc/rules/palettes.yaml +++ b/mods/cnc/rules/palettes.yaml @@ -54,6 +54,7 @@ Filename: temperat.pal ShadowIndex: 3 AllowModifiers: false + CursorPalette: true PaletteFromFile@beaconposter: Name: beaconposter Filename: temperat.pal diff --git a/mods/d2k/cursors.yaml b/mods/d2k/cursors.yaml index 949f708d5a..346f20cc08 100644 --- a/mods/d2k/cursors.yaml +++ b/mods/d2k/cursors.yaml @@ -1,10 +1,5 @@ -ShadowIndex: 1 - -Palettes: - mouse: PALETTE.BIN - Cursors: - MOUSE.R8: mouse + MOUSE.R8: d2k scroll-t: Start: 112 X: 24 @@ -284,7 +279,7 @@ Cursors: Length: 8 X: 24 Y: 24 - nopower.shp: mouse + nopower.shp: d2k powerdown-blocked: Start: 0 Length: 1 @@ -295,7 +290,7 @@ Cursors: Length: 3 X: 12 Y: 12 - attackmove.shp: mouse + attackmove.shp: d2k attackmove: Start: 0 Length: 8 @@ -314,7 +309,7 @@ Cursors: Start: 8 X: -2 Y: -2 - assaultmove.shp: mouse + assaultmove.shp: d2k assaultmove: Start: 0 Length: 8 diff --git a/mods/d2k/rules/palettes.yaml b/mods/d2k/rules/palettes.yaml index 2600050e5a..4db052a5e6 100644 --- a/mods/d2k/rules/palettes.yaml +++ b/mods/d2k/rules/palettes.yaml @@ -6,6 +6,7 @@ Name: d2k Filename: PALETTE.BIN ShadowIndex: 1 + CursorPalette: true PaletteFromFile@chrome: Name: chrome Filename: PALETTE.BIN diff --git a/mods/modcontent/cursors.yaml b/mods/modcontent/cursors.yaml index 36996e12ca..61d0a8d6a8 100644 --- a/mods/modcontent/cursors.yaml +++ b/mods/modcontent/cursors.yaml @@ -1,6 +1,3 @@ -Palettes: - cursor: cursor.pal - Cursors: cursor.shp: cursor default: diff --git a/mods/modcontent/mod.yaml b/mods/modcontent/mod.yaml index d61dfad589..c267424308 100644 --- a/mods/modcontent/mod.yaml +++ b/mods/modcontent/mod.yaml @@ -8,6 +8,9 @@ Packages: ./mods/modcontent: modcontent ./mods/common: common +Rules: + modcontent|rules.yaml + Cursors: modcontent|cursors.yaml diff --git a/mods/modcontent/rules.yaml b/mods/modcontent/rules.yaml new file mode 100644 index 0000000000..9f69b62495 --- /dev/null +++ b/mods/modcontent/rules.yaml @@ -0,0 +1,5 @@ +World: + PaletteFromFile: + Name: cursor + Filename: cursor.pal + CursorPalette: true diff --git a/mods/ra/cursors.yaml b/mods/ra/cursors.yaml index 8368d95623..4eea43d54f 100644 --- a/mods/ra/cursors.yaml +++ b/mods/ra/cursors.yaml @@ -1,8 +1,5 @@ -Palettes: - cursor: temperat.pal - Cursors: - mouse.shp: cursor + mouse.shp: chrome scroll-t: Start: 1 Y: -7 @@ -203,14 +200,14 @@ Cursors: sell2: Start: 148 Length: 12 - nopower.shp: cursor + nopower.shp: chrome powerdown-blocked: Start: 0 Length: 1 powerdown: Start: 1 Length: 3 - attackmove.shp: cursor + attackmove.shp: chrome attackmove: Start: 0 Length: 4 diff --git a/mods/ra/rules/palettes.yaml b/mods/ra/rules/palettes.yaml index 02d6a42975..55b763245d 100644 --- a/mods/ra/rules/palettes.yaml +++ b/mods/ra/rules/palettes.yaml @@ -28,6 +28,7 @@ Filename: temperat.pal ShadowIndex: 3 AllowModifiers: false + CursorPalette: true PaletteFromFile@effect: Name: effect Filename: temperat.pal diff --git a/mods/ts/cursors.yaml b/mods/ts/cursors.yaml index bc0310c307..420867f0e3 100644 --- a/mods/ts/cursors.yaml +++ b/mods/ts/cursors.yaml @@ -1,8 +1,5 @@ -Palettes: - cursor: mousepal.pal - Cursors: - mouse.shp: cursor + mouse.shp: mouse scroll-t: Start: 2 Y: -19 @@ -218,7 +215,7 @@ Cursors: Start: 385 joystick-tl-blocked: Start: 386 - attackmove.shp: cursor + attackmove.shp: mouse attackmove: Start: 0 Length: 10 @@ -231,7 +228,7 @@ Cursors: attackmove-minimap-blocked: Start: 21 Length: 1 - assaultmove.shp: cursor + assaultmove.shp: mouse assaultmove: Start: 0 Length: 10 diff --git a/mods/ts/rules/palettes.yaml b/mods/ts/rules/palettes.yaml index a78cf12276..39dd6980de 100644 --- a/mods/ts/rules/palettes.yaml +++ b/mods/ts/rules/palettes.yaml @@ -2,6 +2,7 @@ PaletteFromFile@mouse: Name: mouse Filename: mousepal.pal + CursorPalette: true PaletteFromFile@terraintem: Name: terrain Tileset: TEMPERATE