Cleanup PaletteEditor for saving updated colors
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
#include "absl/status/status.h"
|
#include "absl/status/status.h"
|
||||||
#include "app/gfx/snes_palette.h"
|
#include "app/gfx/snes_palette.h"
|
||||||
#include "app/gui/canvas.h"
|
#include "app/gui/canvas.h"
|
||||||
|
#include "app/gui/color.h"
|
||||||
#include "app/gui/icons.h"
|
#include "app/gui/icons.h"
|
||||||
|
|
||||||
static inline float ImSaturate(float f) {
|
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);
|
int w = vsnprintf(buf, buf_size, fmt, args);
|
||||||
#endif
|
#endif
|
||||||
va_end(args);
|
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;
|
if (w == -1 || w >= (int)buf_size) w = (int)buf_size - 1;
|
||||||
buf[w] = 0;
|
buf[w] = 0;
|
||||||
return w;
|
return w;
|
||||||
@@ -34,12 +35,32 @@ namespace app {
|
|||||||
namespace editor {
|
namespace editor {
|
||||||
|
|
||||||
absl::Status PaletteEditor::Update() {
|
absl::Status PaletteEditor::Update() {
|
||||||
for (int i = 0; i < kNumPalettes; ++i) {
|
if (ImGui::BeginTable("paletteEditorTable", 2,
|
||||||
if (ImGui::TreeNode(kPaletteCategoryNames[i].data())) {
|
ImGuiTableFlags_Reorderable |
|
||||||
RETURN_IF_ERROR(DrawPaletteGroup(i))
|
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::TreePop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ImGui::TableNextColumn();
|
||||||
|
ImGui::Text("Test Column");
|
||||||
|
ImGui::EndTable();
|
||||||
|
}
|
||||||
|
|
||||||
|
CLEAR_AND_RETURN_STATUS(status_)
|
||||||
|
|
||||||
return absl::OkStatus();
|
return absl::OkStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +72,7 @@ void PaletteEditor::EditColorInPalette(gfx::SNESPalette& palette, int index) {
|
|||||||
|
|
||||||
// Get the current color
|
// Get the current color
|
||||||
auto currentColor = palette.GetColor(index).GetRGB();
|
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
|
// The color was modified, update it in the palette
|
||||||
palette(index, currentColor);
|
palette(index, currentColor);
|
||||||
}
|
}
|
||||||
@@ -69,12 +90,18 @@ void PaletteEditor::ResetColorToOriginal(
|
|||||||
palette(index, originalColor);
|
palette(index, originalColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
absl::Status PaletteEditor::DrawPaletteGroup(int i) {
|
absl::Status PaletteEditor::DrawPaletteGroup(int category) {
|
||||||
auto size = rom()->GetPaletteGroup(kPaletteGroupNames[i].data()).size();
|
if (!rom()->isLoaded()) {
|
||||||
auto palettes = rom()->GetPaletteGroup(kPaletteGroupNames[i].data());
|
return absl::NotFoundError("ROM not open, no palettes to display");
|
||||||
if (static bool init = false; !init) {
|
|
||||||
InitializeSavedPalette(palettes[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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++) {
|
for (int j = 0; j < size; j++) {
|
||||||
ImGui::Text("%d", j);
|
ImGui::Text("%d", j);
|
||||||
|
|
||||||
@@ -85,51 +112,61 @@ absl::Status PaletteEditor::DrawPaletteGroup(int i) {
|
|||||||
ImGui::PushID(n);
|
ImGui::PushID(n);
|
||||||
if ((n % 8) != 0) ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y);
|
if ((n % 8) != 0) ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y);
|
||||||
|
|
||||||
std::string popupId = kPaletteCategoryNames[i].data() +
|
auto popup_id =
|
||||||
std::to_string(j) + "_" + std::to_string(n);
|
absl::StrCat(kPaletteCategoryNames[category].data(), j, "_", n);
|
||||||
if (ImGui::ColorButton(
|
|
||||||
popupId.c_str(),
|
// Small icon of the color in the palette
|
||||||
ImVec4(palette[n].GetRGB().x / 255, palette[n].GetRGB().y / 255,
|
if (gui::SNESColorButton(popup_id, palette[n], palette_button_flags)) {
|
||||||
palette[n].GetRGB().z / 255, palette[n].GetRGB().w / 255),
|
edit_color = true;
|
||||||
palette_button_flags)) {
|
}
|
||||||
if (ImGui::ColorEdit4(popupId.c_str(),
|
|
||||||
|
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(),
|
gfx::ToFloatArray(palette[n]).data(),
|
||||||
palette_button_flags)) {
|
palette_button_flags)) {
|
||||||
EditColorInPalette(palette, n);
|
EditColorInPalette(palette, n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ImGui::BeginPopupContextItem(popupId.c_str())) {
|
ImGui::PopID();
|
||||||
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]))
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
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::Button("Copy as..", ImVec2(-1, 0))) ImGui::OpenPopup("Copy");
|
||||||
|
|
||||||
if (ImGui::BeginPopup("Copy")) {
|
if (ImGui::BeginPopup("Copy")) {
|
||||||
int cr = IM_F32_TO_INT8_SAT(col[0]);
|
int cr = IM_F32_TO_INT8_SAT(col[0]);
|
||||||
int cg = IM_F32_TO_INT8_SAT(col[1]);
|
int cg = IM_F32_TO_INT8_SAT(col[1]);
|
||||||
int cb = IM_F32_TO_INT8_SAT(col[2]);
|
int cb = IM_F32_TO_INT8_SAT(col[2]);
|
||||||
char buf[64];
|
char buf[64];
|
||||||
CustomFormatString(buf, IM_ARRAYSIZE(buf), "(%.3ff, %.3ff, %.3ff)",
|
|
||||||
col[0], col[1], col[2]);
|
CustomFormatString(buf, IM_ARRAYSIZE(buf), "(%.3ff, %.3ff, %.3ff)", col[0],
|
||||||
|
col[1], col[2]);
|
||||||
|
|
||||||
if (ImGui::Selectable(buf)) ImGui::SetClipboardText(buf);
|
if (ImGui::Selectable(buf)) ImGui::SetClipboardText(buf);
|
||||||
CustomFormatString(buf, IM_ARRAYSIZE(buf), "(%d,%d,%d)", cr, cg, cb);
|
CustomFormatString(buf, IM_ARRAYSIZE(buf), "(%d,%d,%d)", cr, cg, cb);
|
||||||
if (ImGui::Selectable(buf)) ImGui::SetClipboardText(buf);
|
if (ImGui::Selectable(buf)) ImGui::SetClipboardText(buf);
|
||||||
CustomFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X", cr, cg,
|
CustomFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X", cr, cg, cb);
|
||||||
cb);
|
|
||||||
if (ImGui::Selectable(buf)) ImGui::SetClipboardText(buf);
|
if (ImGui::Selectable(buf)) ImGui::SetClipboardText(buf);
|
||||||
ImGui::EndPopup();
|
ImGui::EndPopup();
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGui::EndPopup();
|
ImGui::EndPopup();
|
||||||
}
|
|
||||||
|
|
||||||
ImGui::PopID();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return absl::OkStatus();
|
return absl::OkStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -78,18 +78,18 @@ class PaletteEditorHistory {
|
|||||||
class PaletteEditor : public SharedROM {
|
class PaletteEditor : public SharedROM {
|
||||||
public:
|
public:
|
||||||
absl::Status Update();
|
absl::Status Update();
|
||||||
|
absl::Status DrawPaletteGroups();
|
||||||
|
|
||||||
void EditColorInPalette(gfx::SNESPalette& palette, int index);
|
void EditColorInPalette(gfx::SNESPalette& palette, int index);
|
||||||
void ResetColorToOriginal(gfx::SNESPalette& palette, int index,
|
void ResetColorToOriginal(gfx::SNESPalette& palette, int index,
|
||||||
const gfx::SNESPalette& originalPalette);
|
const gfx::SNESPalette& originalPalette);
|
||||||
void DisplayPalette(gfx::SNESPalette& palette, bool loaded);
|
void DisplayPalette(gfx::SNESPalette& palette, bool loaded);
|
||||||
|
|
||||||
void DrawPortablePalette(gfx::SNESPalette& palette);
|
void DrawPortablePalette(gfx::SNESPalette& palette);
|
||||||
|
|
||||||
private:
|
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) {
|
void InitializeSavedPalette(const gfx::SNESPalette& palette) {
|
||||||
for (int n = 0; n < palette.size(); n++) {
|
for (int n = 0; n < palette.size(); n++) {
|
||||||
saved_palette_[n].x = palette.GetColor(n).GetRGB().x / 255;
|
saved_palette_[n].x = palette.GetColor(n).GetRGB().x / 255;
|
||||||
@@ -99,11 +99,15 @@ class PaletteEditor : public SharedROM {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
absl::Status status_;
|
||||||
|
|
||||||
PaletteEditorHistory history_;
|
PaletteEditorHistory history_;
|
||||||
|
|
||||||
ImVec4 saved_palette_[256] = {};
|
ImVec4 saved_palette_[256] = {};
|
||||||
ImVec4 current_color_;
|
ImVec4 current_color_;
|
||||||
|
|
||||||
|
ImGuiColorEditFlags color_popup_flags =
|
||||||
|
ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoAlpha;
|
||||||
ImGuiColorEditFlags palette_button_flags =
|
ImGuiColorEditFlags palette_button_flags =
|
||||||
ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoTooltip;
|
ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoTooltip;
|
||||||
ImGuiColorEditFlags palette_button_flags_2 = ImGuiColorEditFlags_NoAlpha |
|
ImGuiColorEditFlags palette_button_flags_2 = ImGuiColorEditFlags_NoAlpha |
|
||||||
|
|||||||
Reference in New Issue
Block a user