From 8edfe314d5ac0dd631bfc3a7322c2aa6a9dd6555 Mon Sep 17 00:00:00 2001 From: scawful Date: Fri, 10 Nov 2023 23:21:41 -0500 Subject: [PATCH] Cleanup `PaletteEditor` for saving updated colors --- src/app/editor/palette_editor.cc | 131 ++++++++++++++++++++----------- src/app/editor/palette_editor.h | 10 ++- 2 files changed, 91 insertions(+), 50 deletions(-) diff --git a/src/app/editor/palette_editor.cc b/src/app/editor/palette_editor.cc index 43c19199..73f16be2 100644 --- a/src/app/editor/palette_editor.cc +++ b/src/app/editor/palette_editor.cc @@ -5,6 +5,7 @@ #include "absl/status/status.h" #include "app/gfx/snes_palette.h" #include "app/gui/canvas.h" +#include "app/gui/color.h" #include "app/gui/icons.h" static inline float ImSaturate(float f) { @@ -23,7 +24,7 @@ int CustomFormatString(char* buf, size_t buf_size, const char* fmt, ...) { int w = vsnprintf(buf, buf_size, fmt, args); #endif va_end(args); - if (buf == NULL) return w; + if (buf == nullptr) return w; if (w == -1 || w >= (int)buf_size) w = (int)buf_size - 1; buf[w] = 0; return w; @@ -34,12 +35,32 @@ namespace app { namespace editor { absl::Status PaletteEditor::Update() { - for (int i = 0; i < kNumPalettes; ++i) { - if (ImGui::TreeNode(kPaletteCategoryNames[i].data())) { - RETURN_IF_ERROR(DrawPaletteGroup(i)) - ImGui::TreePop(); + if (ImGui::BeginTable("paletteEditorTable", 2, + ImGuiTableFlags_Reorderable | + ImGuiTableFlags_Resizable | + ImGuiTableFlags_SizingStretchSame, + ImVec2(0, 0))) { + ImGui::TableSetupColumn("Palette Groups", + ImGuiTableColumnFlags_WidthStretch, + ImGui::GetContentRegionAvail().x); + ImGui::TableSetupColumn("Editor", ImGuiTableColumnFlags_WidthStretch, + ImGui::GetContentRegionAvail().x); + ImGui::TableHeadersRow(); + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + for (int category = 0; category < kNumPalettes; ++category) { + if (ImGui::TreeNode(kPaletteCategoryNames[category].data())) { + status_ = DrawPaletteGroup(category); + ImGui::TreePop(); + } } + ImGui::TableNextColumn(); + ImGui::Text("Test Column"); + ImGui::EndTable(); } + + CLEAR_AND_RETURN_STATUS(status_) + return absl::OkStatus(); } @@ -51,7 +72,7 @@ void PaletteEditor::EditColorInPalette(gfx::SNESPalette& palette, int index) { // Get the current color auto currentColor = palette.GetColor(index).GetRGB(); - if (ImGui::ColorPicker4("Color Picker", (float*)¤tColor)) { + if (ImGui::ColorPicker4("Color Picker", (float*)&palette[index])) { // The color was modified, update it in the palette palette(index, currentColor); } @@ -69,12 +90,18 @@ void PaletteEditor::ResetColorToOriginal( 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]); +absl::Status PaletteEditor::DrawPaletteGroup(int category) { + if (!rom()->isLoaded()) { + return absl::NotFoundError("ROM not open, no palettes to display"); } + + const auto size = + rom()->GetPaletteGroup(kPaletteGroupNames[category].data()).size(); + auto palettes = rom()->GetPaletteGroup(kPaletteGroupNames[category].data()); + // if (static bool init = false; !init) { + // InitializeSavedPalette(palettes[0]); + // } + static bool edit_color = false; for (int j = 0; j < size; j++) { ImGui::Text("%d", j); @@ -85,54 +112,64 @@ absl::Status PaletteEditor::DrawPaletteGroup(int i) { ImGui::PushID(n); if ((n % 8) != 0) ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y); - std::string popupId = kPaletteCategoryNames[i].data() + - std::to_string(j) + "_" + std::to_string(n); - 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(), + auto popup_id = + absl::StrCat(kPaletteCategoryNames[category].data(), j, "_", n); + + // Small icon of the color in the palette + if (gui::SNESColorButton(popup_id, palette[n], palette_button_flags)) { + edit_color = true; + } + + if (ImGui::BeginPopupContextItem(popup_id.c_str())) { + RETURN_IF_ERROR(HandleColorPopup(palette, category, j, n)) + } + + if (edit_color) { + // The color button was clicked, open the popup + if (ImGui::ColorEdit4(popup_id.c_str(), gfx::ToFloatArray(palette[n]).data(), palette_button_flags)) { EditColorInPalette(palette, n); } } - if (ImGui::BeginPopupContextItem(popupId.c_str())) { - auto col = gfx::ToFloatArray(palette[n]); - if (ImGui::ColorEdit4( - "Edit Color", col.data(), - ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoAlpha)) { - RETURN_IF_ERROR(rom()->UpdatePaletteColor( - kPaletteGroupNames[i].data(), j, n, palette[n])) - } - if (ImGui::Button("Copy as..", ImVec2(-1, 0))) ImGui::OpenPopup("Copy"); - if (ImGui::BeginPopup("Copy")) { - int cr = IM_F32_TO_INT8_SAT(col[0]); - int cg = IM_F32_TO_INT8_SAT(col[1]); - int cb = IM_F32_TO_INT8_SAT(col[2]); - char buf[64]; - CustomFormatString(buf, IM_ARRAYSIZE(buf), "(%.3ff, %.3ff, %.3ff)", - col[0], col[1], col[2]); - if (ImGui::Selectable(buf)) ImGui::SetClipboardText(buf); - CustomFormatString(buf, IM_ARRAYSIZE(buf), "(%d,%d,%d)", cr, cg, cb); - if (ImGui::Selectable(buf)) ImGui::SetClipboardText(buf); - CustomFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X", cr, cg, - cb); - if (ImGui::Selectable(buf)) ImGui::SetClipboardText(buf); - ImGui::EndPopup(); - } - - ImGui::EndPopup(); - } - ImGui::PopID(); } } return absl::OkStatus(); } +absl::Status PaletteEditor::HandleColorPopup(gfx::SNESPalette& palette, int i, + int j, int n) { + auto col = gfx::ToFloatArray(palette[n]); + if (ImGui::ColorEdit4("Edit Color", col.data(), color_popup_flags)) { + RETURN_IF_ERROR(rom()->UpdatePaletteColor(kPaletteGroupNames[i].data(), j, + n, palette[n])) + } + + if (ImGui::Button("Copy as..", ImVec2(-1, 0))) ImGui::OpenPopup("Copy"); + + if (ImGui::BeginPopup("Copy")) { + int cr = IM_F32_TO_INT8_SAT(col[0]); + int cg = IM_F32_TO_INT8_SAT(col[1]); + int cb = IM_F32_TO_INT8_SAT(col[2]); + char buf[64]; + + CustomFormatString(buf, IM_ARRAYSIZE(buf), "(%.3ff, %.3ff, %.3ff)", col[0], + col[1], col[2]); + + if (ImGui::Selectable(buf)) ImGui::SetClipboardText(buf); + CustomFormatString(buf, IM_ARRAYSIZE(buf), "(%d,%d,%d)", cr, cg, cb); + if (ImGui::Selectable(buf)) ImGui::SetClipboardText(buf); + CustomFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X", cr, cg, cb); + if (ImGui::Selectable(buf)) ImGui::SetClipboardText(buf); + ImGui::EndPopup(); + } + + ImGui::EndPopup(); + return absl::OkStatus(); +} + void PaletteEditor::DisplayPalette(gfx::SNESPalette& palette, bool loaded) { static ImVec4 color = ImVec4(0, 0, 0, 255.f); ImGuiColorEditFlags misc_flags = ImGuiColorEditFlags_AlphaPreview | diff --git a/src/app/editor/palette_editor.h b/src/app/editor/palette_editor.h index 79cd6889..dcd263ad 100644 --- a/src/app/editor/palette_editor.h +++ b/src/app/editor/palette_editor.h @@ -78,18 +78,18 @@ class PaletteEditorHistory { class PaletteEditor : public SharedROM { public: absl::Status Update(); + absl::Status DrawPaletteGroups(); 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); private: - absl::Status DrawPaletteGroup(int i); + absl::Status DrawPaletteGroup(int category); + absl::Status HandleColorPopup(gfx::SNESPalette& palette, int i, int j, int n); - 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; @@ -99,11 +99,15 @@ class PaletteEditor : public SharedROM { } } + absl::Status status_; + PaletteEditorHistory history_; ImVec4 saved_palette_[256] = {}; ImVec4 current_color_; + ImGuiColorEditFlags color_popup_flags = + ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoAlpha; ImGuiColorEditFlags palette_button_flags = ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoTooltip; ImGuiColorEditFlags palette_button_flags_2 = ImGuiColorEditFlags_NoAlpha |