diff --git a/src/app/editor/palette_editor.cc b/src/app/editor/palette_editor.cc index ca26a914..43c19199 100644 --- a/src/app/editor/palette_editor.cc +++ b/src/app/editor/palette_editor.cc @@ -43,9 +43,38 @@ absl::Status PaletteEditor::Update() { return absl::OkStatus(); } +void PaletteEditor::EditColorInPalette(gfx::SNESPalette& palette, int index) { + if (index >= palette.size()) { + // Handle error: the index is out of bounds + return; + } + + // Get the current color + auto currentColor = palette.GetColor(index).GetRGB(); + if (ImGui::ColorPicker4("Color Picker", (float*)¤tColor)) { + // The color was modified, update it in the palette + palette(index, currentColor); + } +} + +void PaletteEditor::ResetColorToOriginal( + gfx::SNESPalette& palette, int index, + const gfx::SNESPalette& originalPalette) { + if (index >= palette.size() || index >= originalPalette.size()) { + // Handle error: the index is out of bounds + return; + } + + auto originalColor = originalPalette.GetColor(index).GetRGB(); + palette(index, originalColor); +} + absl::Status PaletteEditor::DrawPaletteGroup(int i) { auto size = rom()->GetPaletteGroup(kPaletteGroupNames[i].data()).size(); auto palettes = rom()->GetPaletteGroup(kPaletteGroupNames[i].data()); + if (static bool init = false; !init) { + InitializeSavedPalette(palettes[0]); + } for (int j = 0; j < size; j++) { ImGui::Text("%d", j); @@ -58,14 +87,16 @@ absl::Status PaletteEditor::DrawPaletteGroup(int i) { std::string popupId = kPaletteCategoryNames[i].data() + std::to_string(j) + "_" + std::to_string(n); - if (ImGui::ColorButton(popupId.c_str(), palette[n].GetRGB(), - palette_button_flags)) { - static auto float_array = gfx::ToFloatArray(palette[n]); - if (ImGui::ColorEdit4(popupId.c_str(), float_array.data(), - palette_button_flags)) - current_color_ = ImVec4(palette[n].GetRGB().x, palette[n].GetRGB().y, - palette[n].GetRGB().z, - palette[n].GetRGB().w); // Prese rve alpha! + if (ImGui::ColorButton( + popupId.c_str(), + ImVec4(palette[n].GetRGB().x / 255, palette[n].GetRGB().y / 255, + palette[n].GetRGB().z / 255, palette[n].GetRGB().w / 255), + palette_button_flags)) { + if (ImGui::ColorEdit4(popupId.c_str(), + gfx::ToFloatArray(palette[n]).data(), + palette_button_flags)) { + EditColorInPalette(palette, n); + } } if (ImGui::BeginPopupContextItem(popupId.c_str())) { @@ -111,12 +142,7 @@ void PaletteEditor::DisplayPalette(gfx::SNESPalette& palette, bool loaded) { // Generate a default palette. The palette will persist and can be edited. static bool init = false; if (loaded && !init) { - for (int n = 0; n < palette.size(); n++) { - saved_palette_[n].x = palette.GetColor(n).GetRGB().x / 255; - saved_palette_[n].y = palette.GetColor(n).GetRGB().y / 255; - saved_palette_[n].z = palette.GetColor(n).GetRGB().z / 255; - saved_palette_[n].w = 255; // Alpha - } + InitializeSavedPalette(palette); init = true; } @@ -188,12 +214,7 @@ void PaletteEditor::DisplayPalette(gfx::SNESPalette& palette, bool loaded) { void PaletteEditor::DrawPortablePalette(gfx::SNESPalette& palette) { static bool init = false; if (!init) { - for (int n = 0; n < palette.size(); n++) { - saved_palette_[n].x = palette.GetColor(n).GetRGB().x / 255; - saved_palette_[n].y = palette.GetColor(n).GetRGB().y / 255; - saved_palette_[n].z = palette.GetColor(n).GetRGB().z / 255; - saved_palette_[n].w = 255; // Alpha - } + InitializeSavedPalette(palette); init = true; } diff --git a/src/app/editor/palette_editor.h b/src/app/editor/palette_editor.h index 468bb767..982afa4f 100644 --- a/src/app/editor/palette_editor.h +++ b/src/app/editor/palette_editor.h @@ -3,6 +3,8 @@ #include +#include + #include "absl/status/status.h" #include "app/gfx/snes_palette.h" #include "app/gui/canvas.h" @@ -26,9 +28,21 @@ static constexpr absl::string_view kPaletteGroupNames[] = { "ow_aux", "global_sprites", "dungeon_main", "ow_mini_map", "ow_mini_map", "3d_object", "3d_object"}; +struct PaletteChange { + std::string groupName; + size_t paletteIndex; + size_t colorIndex; + gfx::SNESColor originalColor; +}; + class PaletteEditor : public SharedROM { public: absl::Status Update(); + + void EditColorInPalette(gfx::SNESPalette& palette, int index); + void ResetColorToOriginal(gfx::SNESPalette& palette, int index, + const gfx::SNESPalette& originalPalette); + void DisplayPalette(gfx::SNESPalette& palette, bool loaded); void DrawPortablePalette(gfx::SNESPalette& palette); @@ -36,6 +50,18 @@ class PaletteEditor : public SharedROM { private: absl::Status DrawPaletteGroup(int i); + private: + void InitializeSavedPalette(const gfx::SNESPalette& palette) { + for (int n = 0; n < palette.size(); n++) { + saved_palette_[n].x = palette.GetColor(n).GetRGB().x / 255; + saved_palette_[n].y = palette.GetColor(n).GetRGB().y / 255; + saved_palette_[n].z = palette.GetColor(n).GetRGB().z / 255; + saved_palette_[n].w = 255; // Alpha + } + } + + std::stack changeHistory; + ImVec4 saved_palette_[256] = {}; ImVec4 current_color_;