Update PaletteEditor, remove Delta experiment

This commit is contained in:
scawful
2023-05-15 08:23:13 -05:00
parent 3abfaf3336
commit 46fe8993ea
18 changed files with 247 additions and 852 deletions

View File

@@ -7,34 +7,113 @@
#include "gui/canvas.h"
#include "gui/icons.h"
static inline float ImSaturate(float f) {
return (f < 0.0f) ? 0.0f : (f > 1.0f) ? 1.0f : f;
}
#define IM_F32_TO_INT8_SAT(_VAL) \
((int)(ImSaturate(_VAL) * 255.0f + 0.5f)) // Saturated, always output 0..255
int CustomFormatString(char* buf, size_t buf_size, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
#ifdef IMGUI_USE_STB_SPRINTF
int w = stbsp_vsnprintf(buf, (int)buf_size, fmt, args);
#else
int w = vsnprintf(buf, buf_size, fmt, args);
#endif
va_end(args);
if (buf == NULL) return w;
if (w == -1 || w >= (int)buf_size) w = (int)buf_size - 1;
buf[w] = 0;
return w;
}
namespace yaze {
namespace app {
namespace editor {
absl::Status PaletteEditor::Update() {
for (int i = 0; i < 11; ++i) {
if (ImGui::TreeNode(kPaletteCategoryNames[i].data())) {
auto size = rom_.GetPaletteGroup(kPaletteGroupNames[i].data()).size;
auto palettes = rom_.GetPaletteGroup(kPaletteGroupNames[i].data());
for (int j = 0; j < size; j++) {
ImGui::Text("%d", j);
auto palette = palettes[j];
for (int n = 0; n < size; n++) {
ImGui::PushID(n);
if ((n % 8) != 0)
ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y);
namespace {
void DrawPaletteTooltips(gfx::SNESPalette& palette, int size) {}
ImGuiColorEditFlags palette_button_flags =
ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoPicker;
if (ImGui::ColorButton("##palette", palette[n].RGB(),
palette_button_flags, ImVec2(20, 20)))
current_color_ =
ImVec4(palette[n].rgb.x, palette[n].rgb.y, palette[n].rgb.z,
current_color_.w); // Preserve alpha!
using namespace ImGui;
ImGui::PopID();
}
} // namespace
void PaletteEditor::DrawPaletteGroup(int i) {
const int palettesPerRow = 4;
ImGui::BeginTable("palette_table", palettesPerRow,
ImGuiTableFlags_BordersOuter | ImGuiTableFlags_Resizable);
auto size = rom_.GetPaletteGroup(kPaletteGroupNames[i].data()).size();
auto palettes = rom_.GetPaletteGroup(kPaletteGroupNames[i].data());
for (int j = 0; j < size; j++) {
if (j % palettesPerRow == 0) {
ImGui::TableNextRow();
}
ImGui::TableSetColumnIndex(j % palettesPerRow);
ImGui::Text("%d", j);
auto palette = palettes[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);
std::string popupId = kPaletteCategoryNames[i].data() +
std::to_string(j) + "_" + std::to_string(n);
if (ImGui::ColorButton(popupId.c_str(), palette[n].RGB(),
palette_button_flags)) {
if (ImGui::ColorEdit4(popupId.c_str(), palette[n].ToFloatArray(),
palette_button_flags))
current_color_ =
ImVec4(palette[n].rgb.x, palette[n].rgb.y, palette[n].rgb.z,
palette[n].rgb.w); // Preserve alpha!
}
if (ImGui::BeginPopupContextItem(popupId.c_str())) {
auto col = palette[n].ToFloatArray();
if (ImGui::ColorEdit4(
"Edit Color", col,
ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoAlpha)) {
// palette[n].rgb.x = current_color_rgba.x;
// palette[n].rgb.y = current_color_rgba.y;
// palette[n].rgb.z = current_color_rgba.z;
// rom_.UpdatePaletteColor(kPaletteGroupNames[groupIndex].data(), j,
// n, palette[n]);
}
if (Button("Copy as..", ImVec2(-1, 0))) OpenPopup("Copy");
if (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 (Selectable(buf)) SetClipboardText(buf);
CustomFormatString(buf, IM_ARRAYSIZE(buf), "(%d,%d,%d)", cr, cg, cb);
if (Selectable(buf)) SetClipboardText(buf);
CustomFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X", cr, cg,
cb);
if (Selectable(buf)) SetClipboardText(buf);
EndPopup();
}
ImGui::EndPopup();
}
ImGui::PopID();
}
}
ImGui::EndTable();
}
absl::Status PaletteEditor::Update() {
for (int i = 0; i < kNumPalettes; ++i) {
if (ImGui::TreeNode(kPaletteCategoryNames[i].data())) {
DrawPaletteGroup(i);
ImGui::TreePop();
}
}
@@ -49,13 +128,12 @@ void PaletteEditor::DisplayPalette(gfx::SNESPalette& palette, bool loaded) {
// Generate a default palette. The palette will persist and can be edited.
static bool init = false;
static ImVec4 saved_palette[256] = {};
if (loaded && !init) {
for (int n = 0; n < palette.size_; n++) {
saved_palette[n].x = palette.GetColor(n).rgb.x / 255;
saved_palette[n].y = palette.GetColor(n).rgb.y / 255;
saved_palette[n].z = palette.GetColor(n).rgb.z / 255;
saved_palette[n].w = 255; // Alpha
saved_palette_[n].x = palette.GetColor(n).rgb.x / 255;
saved_palette_[n].y = palette.GetColor(n).rgb.y / 255;
saved_palette_[n].z = palette.GetColor(n).rgb.z / 255;
saved_palette_[n].w = 255; // Alpha
}
init = true;
}
@@ -68,9 +146,9 @@ void PaletteEditor::DisplayPalette(gfx::SNESPalette& palette, bool loaded) {
ImGui::OpenPopup("mypicker");
backup_color = color;
}
if (ImGui::BeginPopup("mypicker")) {
ImGui::Text("Current Overworld Palette");
ImGui::Separator();
TEXT_WITH_SEPARATOR("Current Overworld Palette");
ImGui::ColorPicker4("##picker", (float*)&color,
misc_flags | ImGuiColorEditFlags_NoSidePreview |
ImGuiColorEditFlags_NoSmallPreview);
@@ -94,25 +172,22 @@ void PaletteEditor::DisplayPalette(gfx::SNESPalette& palette, bool loaded) {
color = backup_color;
ImGui::Separator();
ImGui::Text("Palette");
for (int n = 0; n < IM_ARRAYSIZE(saved_palette); n++) {
for (int n = 0; n < IM_ARRAYSIZE(saved_palette_); n++) {
ImGui::PushID(n);
if ((n % 8) != 0) ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y);
ImGuiColorEditFlags palette_button_flags = ImGuiColorEditFlags_NoAlpha |
ImGuiColorEditFlags_NoPicker |
ImGuiColorEditFlags_NoTooltip;
if (ImGui::ColorButton("##palette", saved_palette[n],
palette_button_flags, ImVec2(20, 20)))
color = ImVec4(saved_palette[n].x, saved_palette[n].y,
saved_palette[n].z, color.w); // Preserve alpha!
if (ImGui::ColorButton("##palette", saved_palette_[n],
palette_button_flags_2, ImVec2(20, 20)))
color = ImVec4(saved_palette_[n].x, saved_palette_[n].y,
saved_palette_[n].z, color.w); // Preserve alpha!
if (ImGui::BeginDragDropTarget()) {
if (const ImGuiPayload* payload =
ImGui::AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F))
memcpy((float*)&saved_palette[n], payload->Data, sizeof(float) * 3);
memcpy((float*)&saved_palette_[n], payload->Data, sizeof(float) * 3);
if (const ImGuiPayload* payload =
ImGui::AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_4F))
memcpy((float*)&saved_palette[n], payload->Data, sizeof(float) * 4);
memcpy((float*)&saved_palette_[n], payload->Data, sizeof(float) * 4);
ImGui::EndDragDropTarget();
}

View File

@@ -13,15 +13,18 @@ namespace yaze {
namespace app {
namespace editor {
constexpr int kNumPalettes = 11;
static constexpr absl::string_view kPaletteCategoryNames[] = {
"Sword", "Shield", "Clothes", "World Colors",
"Area Colors", "Enemies", "Dungeons", "World Map",
"Dungeon Map", "Triforce", "Crystal"};
static constexpr absl::string_view kPaletteGroupNames[] = {
"swords", "shields", "armors", "ow_main",
"ow_aux", "global_sprites", "dungeon_main", "ow_mini_map",
"ow_mini_map", "3d_object", "3d_object"};
"swords", "shields", "armors", "ow_main",
"ow_aux", "global_sprites", "dungeon_main", "ow_mini_map",
"ow_mini_map", "3d_object", "3d_object"};
class PaletteEditor {
public:
@@ -31,8 +34,17 @@ class PaletteEditor {
auto SetupROM(ROM& rom) { rom_ = rom; }
private:
void DrawPaletteGroup(int i);
ImVec4 saved_palette_[256] = {};
ImVec4 current_color_;
ROM rom_;
ImGuiColorEditFlags palette_button_flags =
ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoTooltip;
ImGuiColorEditFlags palette_button_flags_2 = ImGuiColorEditFlags_NoAlpha |
ImGuiColorEditFlags_NoPicker |
ImGuiColorEditFlags_NoTooltip;
};
} // namespace editor