diff --git a/Pixi/Common/ColorSpaceUtility.cs b/Pixi/Common/ColorSpaceUtility.cs index 554a843..82c7bae 100644 --- a/Pixi/Common/ColorSpaceUtility.cs +++ b/Pixi/Common/ColorSpaceUtility.cs @@ -4,9 +4,16 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using UnityEngine; +using UnityEngine.AddressableAssets; +using UnityEngine.ResourceManagement.AsyncOperations; +using GamecraftModdingAPI.App; using GamecraftModdingAPI.Blocks; +using GamecraftModdingAPI.Tasks; using GamecraftModdingAPI.Utility; +using MiniJSON; +using Svelto.Tasks; +using UnityEngine.ResourceManagement.ResourceLocations; namespace Pixi.Common { @@ -21,7 +28,7 @@ namespace Pixi.Common [MethodImpl(MethodImplOptions.AggressiveInlining)] public static BlockColor QuantizeToBlockColor(Color pixel) { - if (colorMap == null) BuildColorMap(); + //if (colorMap == null) BuildColorMap(); float[] closest = new float[3] { 1, 1, 1 }; BlockColor c = new BlockColor { @@ -79,7 +86,7 @@ namespace Pixi.Common [MethodImpl(MethodImplOptions.AggressiveInlining)] public static float[] UnquantizeToArray(BlockColor c) { - if (colorMap == null) BuildColorMap(); + //if (colorMap == null) BuildColorMap(); return colorMap[c]; } @@ -110,23 +117,16 @@ namespace Pixi.Common }); } + public static void LoadColorMenuEvent(object caller, MenuEventArgs info) + { + Scheduler.Schedule(new AsyncRunner()); + } + private static void BuildColorMap() { + // old manual version for building color map colorMap = new Dictionary(); - // TODO create actual color map - foreach (BlockColors c in Enum.GetValues(typeof(BlockColors))) - { - for (byte d = 0; d < 10; d++) - { - BlockColor colorStruct = new BlockColor - { - Color = c, - Darkness = d, - }; - colorMap[colorStruct] = new float[3] { 1f, 0f, 1f }; - } - } - // this was done manually -- never again + // this was done manually -- never again // White colorMap[new BlockColor { Color = BlockColors.White, Darkness = 0 }] = new float[3] { 1f, 1f, 1f}; colorMap[new BlockColor { Color = BlockColors.White, Darkness = 1 }] = new float[3] { 0.88f, 0.98f, 0.99f }; @@ -281,5 +281,41 @@ namespace Pixi.Common botColorMap[31] = new BlockColor { Color = BlockColors.Pink, Darkness = 4 }; botColorMap[15] = new BlockColor { Color = BlockColors.Red, Darkness = 3 }; } + + private class AsyncRunner : ISchedulable + { + public IEnumerator Run() + { + AsyncOperationHandle asyncHandle = Addressables.LoadAssetAsync("colours"); + yield return asyncHandle.Continue(); + Dictionary colourData = Json.Deserialize(asyncHandle.Result.text) as Dictionary; + if (colourData == null) yield break; + Client.EnterMenu -= LoadColorMenuEvent; + // Logging.MetaLog((List)((colourData["Colours"] as Dictionary)["Data"] as Dictionary)["Slots"]); + // Generate color map + List hexColors = + (((colourData["Colours"] as Dictionary)?["Data"] as Dictionary)? + ["Slots"] as List); + int count = 0; + colorMap = new Dictionary(); + for (byte d = 0; d < 10; d++) + { + foreach (BlockColors c in Enum.GetValues(typeof(BlockColors))) + { + if (c != BlockColors.Default) + { + BlockColor colorStruct = new BlockColor + { + Color = c, + Darkness = d, + }; + Color pixel = Images.PixelUtility.PixelHex((string)hexColors[count]); + colorMap[colorStruct] = new float[] {pixel.r, pixel.g, pixel.b}; + count++; + } + } + } + } + } } } diff --git a/Pixi/Images/ImageCanvasImporter.cs b/Pixi/Images/ImageCanvasImporter.cs index 76bc4a1..23a8fe2 100644 --- a/Pixi/Images/ImageCanvasImporter.cs +++ b/Pixi/Images/ImageCanvasImporter.cs @@ -26,6 +26,11 @@ namespace Pixi.Images public string Name { get; } = "ImageCanvas~Spell"; public BlueprintProvider BlueprintProvider { get; } = null; + + public ImageCanvasImporter() + { + GamecraftModdingAPI.App.Client.EnterMenu += ColorSpaceUtility.LoadColorMenuEvent; + } public bool Qualifies(string name) { diff --git a/Pixi/Pixi.csproj b/Pixi/Pixi.csproj index 0216950..6aaf0ba 100644 --- a/Pixi/Pixi.csproj +++ b/Pixi/Pixi.csproj @@ -3,7 +3,7 @@ net472 true - 1.0.0 + 1.0.1 NGnius MIT https://git.exmods.org/NGnius/Pixi