From 626d38ec0d47b0e8737fc49bc83bdd5e0f2e1345 Mon Sep 17 00:00:00 2001 From: scawful Date: Fri, 2 Feb 2024 23:58:52 -0500 Subject: [PATCH] Update SnesColorButton and PaletteEditor --- src/app/core/labeling.cc | 4 +- src/app/core/labeling.h | 2 + src/app/editor/modules/palette_editor.cc | 51 ++++++++++++++---------- src/app/editor/modules/palette_editor.h | 23 +++++------ src/app/gfx/snes_color.h | 11 ++++- src/app/gui/color.cc | 20 +++++++++- src/app/gui/color.h | 5 ++- src/app/gui/pipeline.cc | 2 +- 8 files changed, 79 insertions(+), 39 deletions(-) diff --git a/src/app/core/labeling.cc b/src/app/core/labeling.cc index 7765d31f..de30023f 100644 --- a/src/app/core/labeling.cc +++ b/src/app/core/labeling.cc @@ -106,7 +106,7 @@ void ResourceLabelManager::SelectableLabelWithNameEdit( ImGui::Selectable(label.c_str(), selected, ImGuiSelectableFlags_AllowDoubleClick); std::string label_id = type + "_" + key; - if (ImGui::IsMouseClicked(ImGuiMouseButton_Right)) { + if (ImGui::IsItemHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Right)) { ImGui::OpenPopup(label_id.c_str()); } @@ -118,8 +118,6 @@ void ResourceLabelManager::SelectableLabelWithNameEdit( } ImGui::EndPopup(); } - - // ImGui::PopID(); } std::string ResourceLabelManager::CreateOrGetLabel( diff --git a/src/app/core/labeling.h b/src/app/core/labeling.h index d9509dc6..feeeafcf 100644 --- a/src/app/core/labeling.h +++ b/src/app/core/labeling.h @@ -35,6 +35,8 @@ class ResourceLabelManager { const std::string& defaultValue); std::string CreateOrGetLabel(const std::string& type, const std::string& key, const std::string& defaultValue); + std::string CreateOrGetLabel(const std::string& type, const std::string& key, + const absl::string_view& defaultValue); private: bool labels_loaded_ = false; diff --git a/src/app/editor/modules/palette_editor.cc b/src/app/editor/modules/palette_editor.cc index 4024a804..b0a3c498 100644 --- a/src/app/editor/modules/palette_editor.cc +++ b/src/app/editor/modules/palette_editor.cc @@ -35,6 +35,15 @@ namespace app { namespace editor { absl::Status PaletteEditor::Update() { + if (rom()->is_loaded()) { + // Initialize the labels + for (int i = 0; i < kNumPalettes; i++) { + rom()->resource_label()->CreateOrGetLabel( + "Palette Group Name", std::to_string(i), + std::string(kPaletteGroupNames[i])); + } + } + if (ImGui::BeginTable("paletteEditorTable", 2, ImGuiTableFlags_Reorderable | ImGuiTableFlags_Resizable | @@ -55,7 +64,9 @@ absl::Status PaletteEditor::Update() { } } ImGui::TableNextColumn(); - ImGui::Text("Test Column"); + if (gui::SnesColorEdit4("Color Picker", current_color_, + ImGuiColorEditFlags_NoAlpha)) { + } ImGui::EndTable(); } @@ -97,38 +108,39 @@ absl::Status PaletteEditor::DrawPaletteGroup(int category) { const auto size = rom()->palette_group(kPaletteGroupNames[category].data()).size(); - auto palettes = rom()->palette_group(kPaletteGroupNames[category].data()); + auto palettes = + rom()->mutable_palette_group(kPaletteGroupNames[category].data()); static bool edit_color = false; for (int j = 0; j < size; j++) { - ImGui::Text("%d", j); - - auto palette = palettes[j]; - auto pal_size = palette.size(); + // ImGui::Text("%d", j); + rom()->resource_label()->SelectableLabelWithNameEdit( + false, "Palette Group Name", std::to_string(j), + std::string(kPaletteGroupNames[category])); + auto palette = palettes->mutable_palette(j); + auto pal_size = palette->size(); for (int n = 0; n < pal_size; n++) { ImGui::PushID(n); - if ((n % 8) != 0) ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y); + if ((n % 7) != 0) ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y); 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 (gui::SnesColorButton(popup_id, *palette->mutable_color(n), + palette_button_flags)) { + current_color_ = palette->GetColor(n); + // EditColorInPalette(*palette, n); } if (ImGui::BeginPopupContextItem(popup_id.c_str())) { - RETURN_IF_ERROR(HandleColorPopup(palette, category, j, n)) + 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 (gui::SnesColorEdit4(popup_id.c_str(), (*palette)[n], + // palette_button_flags)) { + // EditColorInPalette(*palette, n); + // } ImGui::PopID(); } @@ -139,13 +151,12 @@ absl::Status PaletteEditor::DrawPaletteGroup(int category) { 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)) { + if (gui::SnesColorEdit4("Edit Color", palette[n], 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]); diff --git a/src/app/editor/modules/palette_editor.h b/src/app/editor/modules/palette_editor.h index 98ac21be..06f7bc68 100644 --- a/src/app/editor/modules/palette_editor.h +++ b/src/app/editor/modules/palette_editor.h @@ -26,11 +26,11 @@ static constexpr absl::string_view kPaletteGroupNames[] = { "ow_mini_map", "3d_object", "3d_object"}; struct PaletteChange { - std::string groupName; - size_t paletteIndex; - size_t colorIndex; - gfx::SnesColor originalColor; - gfx::SnesColor newColor; + std::string group_name; + size_t palette_index; + size_t color_index; + gfx::SnesColor original_color; + gfx::SnesColor new_color; }; class PaletteEditorHistory { @@ -59,10 +59,10 @@ class PaletteEditorHistory { size_t paletteIndex, size_t colorIndex) const { for (const auto& change : recentChanges) { - if (change.groupName == groupName && - change.paletteIndex == paletteIndex && - change.colorIndex == colorIndex) { - return change.originalColor; + if (change.group_name == groupName && + change.palette_index == paletteIndex && + change.color_index == colorIndex) { + return change.original_color; } } // Handle error or return default (this is just an example, @@ -104,12 +104,11 @@ class PaletteEditor : public SharedROM { PaletteEditorHistory history_; ImVec4 saved_palette_[256] = {}; - ImVec4 current_color_; + gfx::SnesColor current_color_; ImGuiColorEditFlags color_popup_flags = ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoAlpha; - ImGuiColorEditFlags palette_button_flags = - ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoTooltip; + ImGuiColorEditFlags palette_button_flags = ImGuiColorEditFlags_NoAlpha; ImGuiColorEditFlags palette_button_flags_2 = ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_NoTooltip; diff --git a/src/app/gfx/snes_color.h b/src/app/gfx/snes_color.h index a5814889..8072aa47 100644 --- a/src/app/gfx/snes_color.h +++ b/src/app/gfx/snes_color.h @@ -29,7 +29,6 @@ std::vector Convert(const std::vector& palette); class SnesColor { public: SnesColor() : rgb_(0.f, 0.f, 0.f, 0.f), snes_(0) {} - explicit SnesColor(const ImVec4 val) : rgb_(val) { snes_color color; color.red = val.x / 255; @@ -43,6 +42,16 @@ class SnesColor { snes_(ConvertRGBtoSNES(val)), rom_color_(val) {} + SnesColor(uint8_t r, uint8_t g, uint8_t b) { + rgb_ = ImVec4(r, g, b, 255.f); + snes_color color; + color.red = r; + color.green = g; + color.blue = b; + snes_ = ConvertRGBtoSNES(color); + rom_color_ = color; + } + ImVec4 rgb() const { return rgb_; } void set_rgb(const ImVec4 val) { rgb_.x = val.x / 255; diff --git a/src/app/gui/color.cc b/src/app/gui/color.cc index 21cf2f59..3d2fc7fb 100644 --- a/src/app/gui/color.cc +++ b/src/app/gui/color.cc @@ -21,7 +21,7 @@ ImVec4 ConvertSNESColorToImVec4(const SnesColor& color) { ); } -IMGUI_API bool SNESColorButton(absl::string_view id, SnesColor& color, +IMGUI_API bool SnesColorButton(absl::string_view id, SnesColor& color, ImGuiColorEditFlags flags, const ImVec2& size_arg) { // Convert the SNES color values to ImGui color values (normalized to 0-1 @@ -34,6 +34,24 @@ IMGUI_API bool SNESColorButton(absl::string_view id, SnesColor& color, return pressed; } +IMGUI_API bool SnesColorEdit4(absl::string_view label, SnesColor& color, + ImGuiColorEditFlags flags) { + // Convert the SNES color values to ImGui color values (normalized to 0-1 + // range) + ImVec4 displayColor = ConvertSNESColorToImVec4(color); + + // Call the original ImGui::ColorEdit4 with the converted color + bool pressed = ImGui::ColorEdit4(label.data(), (float*)&displayColor, flags); + + // Convert the ImGui color values back to SNES color values (normalized to + // 0-255 range) + color = SnesColor(static_cast(displayColor.x * 255.0f), + static_cast(displayColor.y * 255.0f), + static_cast(displayColor.z * 255.0f)); + + return pressed; +} + void DisplayPalette(app::gfx::SnesPalette& palette, bool loaded) { static ImVec4 color = ImVec4(0, 0, 0, 255.f); ImGuiColorEditFlags misc_flags = ImGuiColorEditFlags_AlphaPreview | diff --git a/src/app/gui/color.h b/src/app/gui/color.h index fbb99123..3ecd3585 100644 --- a/src/app/gui/color.h +++ b/src/app/gui/color.h @@ -20,10 +20,13 @@ using gfx::SnesColor; ImVec4 ConvertSNESColorToImVec4(const SnesColor& color); // The wrapper function for ImGui::ColorButton that takes a SnesColor reference -IMGUI_API bool SNESColorButton(absl::string_view id, SnesColor& color, +IMGUI_API bool SnesColorButton(absl::string_view id, SnesColor& color, ImGuiColorEditFlags flags = 0, const ImVec2& size_arg = ImVec2(0, 0)); +IMGUI_API bool SnesColorEdit4(absl::string_view label, SnesColor& color, + ImGuiColorEditFlags flags = 0); + void DisplayPalette(app::gfx::SnesPalette& palette, bool loaded); } // namespace gui diff --git a/src/app/gui/pipeline.cc b/src/app/gui/pipeline.cc index 5a300fa7..ad7144be 100644 --- a/src/app/gui/pipeline.cc +++ b/src/app/gui/pipeline.cc @@ -43,7 +43,7 @@ void SelectablePalettePipeline(uint64_t& palette_id, bool& refresh_graphics, ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 2.0f); } - if (gui::SNESColorButton("##palette", palette[n], + if (gui::SnesColorButton("##palette", palette[n], ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_NoTooltip,