From 04b191a9fc26332c3b49fc527cd263b9f347f049 Mon Sep 17 00:00:00 2001 From: Andre M Germain Date: Sun, 20 Oct 2024 10:39:07 -0400 Subject: [PATCH] Prevent crash from selecting palette button --- src/Murder.Editor/CustomFields/ColorField.cs | 39 ++++++++++++-------- src/Murder/Data/GameDataManager.cs | 29 +++++++++++++++ 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/Murder.Editor/CustomFields/ColorField.cs b/src/Murder.Editor/CustomFields/ColorField.cs index e3e25e250..c85819040 100644 --- a/src/Murder.Editor/CustomFields/ColorField.cs +++ b/src/Murder.Editor/CustomFields/ColorField.cs @@ -44,6 +44,11 @@ internal static (bool modified, Vector4 result) ProcessInputImpl(EditorMember _, modified = true; } + if (modified) + { + Game.Data.AddPaletteColor(color); + } + ImGui.EndPopup(); } @@ -127,25 +132,29 @@ internal static void DrawPalettePicker(EditorMember member, Murder.Core.Graphics { (modified, vector4Color) = Vector4Field.ProcessInputImpl(member, new(color.R, color.G, color.B, color.A)); ImGui.SameLine(); - if (ImGui.BeginCombo($"##{member.Name}", color.ToString())) - { - - var i = 0; - foreach (var c in Game.Data.CurrentPalette) + if (Game.Data.CurrentPalette == null) { + ImGui.TextColored(Architect.Profile.Theme.Faded, "(No colors stored yet)"); + } else { + if (ImGui.BeginCombo($"##{member.Name}", color.ToString())) { - if (ImGuiHelpers.SelectableColor($"pal_{color}_{i++}", c.ToSysVector4())) + + var i = 0; + foreach (var c in Game.Data.CurrentPalette) { - modified = true; - vector4Color = c.ToSysVector4(); + if (ImGuiHelpers.SelectableColor($"pal_{color}_{i++}", c.ToSysVector4())) + { + modified = true; + vector4Color = c.ToSysVector4(); + } } + ImGui.EndCombo(); + } + else + { + var p_min = ImGui.GetItemRectMin(); + var p_max = ImGui.GetItemRectMax(); + ImGui.GetWindowDrawList().AddRectFilled(p_min, p_max, ImGuiHelpers.MakeColor32(vector4Color)); } - ImGui.EndCombo(); - } - else - { - var p_min = ImGui.GetItemRectMin(); - var p_max = ImGui.GetItemRectMax(); - ImGui.GetWindowDrawList().AddRectFilled(p_min, p_max, ImGuiHelpers.MakeColor32(vector4Color)); } } ImGui.EndChild(); diff --git a/src/Murder/Data/GameDataManager.cs b/src/Murder/Data/GameDataManager.cs index 6cf5ef908..73ac3233f 100644 --- a/src/Murder/Data/GameDataManager.cs +++ b/src/Murder/Data/GameDataManager.cs @@ -178,6 +178,35 @@ protected virtual LocalizationAsset GetLocalization(LanguageId id) return Game.Data.GetAsset(resourceGuid); } + /// + /// Adds color to the game data's current palette, if it does not already exist. + /// + /// + public void AddPaletteColor(Color color) + { + if (color.R < 0 || color.R > 1 || color.G < 0 || color.G > 1 || color.B < 0 || color.B > 1) { + return; + } + if (color.A < 1) { + return; + } + if (CurrentPalette == null) + { + CurrentPalette = [color]; + } + else + { + if (CurrentPalette.Any(color.Equals)) + { + return; + } + + List colors = [color]; + colors.AddRange(CurrentPalette.ToList().Take(7)); + CurrentPalette = ImmutableArray.CreateRange(colors); + } + } + public void ChangeLanguage(LanguageId id) => ChangeLanguage(Languages.Get(id)); public void ChangeLanguage(LanguageIdData data)