Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent crash from selecting palette button #88

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions src/Murder.Editor/CustomFields/ColorField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ internal static (bool modified, Vector4 result) ProcessInputImpl(EditorMember _,
modified = true;
}

if (modified)
{
Game.Data.AddPaletteColor(color);
}

ImGui.EndPopup();
}

Expand Down Expand Up @@ -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();
Expand Down
29 changes: 29 additions & 0 deletions src/Murder/Data/GameDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,35 @@ protected virtual LocalizationAsset GetLocalization(LanguageId id)
return Game.Data.GetAsset<LocalizationAsset>(resourceGuid);
}

/// <summary>
/// Adds color to the game data's current palette, if it does not already exist.
/// </summary>
/// <param name="color"></param>
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<Color> 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)
Expand Down
Loading