7#include "absl/strings/str_format.h"
35 int display_index,
int dungeon_palette_id) {
36 if (display_index < 0 || display_index >= 128) {
39 if (display_index < 32) {
44 const int local_index = display_index - 32;
45 const int row = local_index / 16;
46 const int col = local_index % 16;
47 if (row >= 6 || col == 0) {
52 dungeon_palette_id, row * 15 + (col - 1)};
69 return "HUD / floor-ceiling";
71 return absl::StrFormat(
"Dungeon row %d", target.
color_index / 15 + 2);
75 auto* names =
static_cast<std::vector<std::string>*
>(user_data);
76 if (!names || idx < 0 || idx >=
static_cast<int>(names->size())) {
79 return (*names)[idx].c_str();
82void DrawColorDropTargetOutline(ImVec2 min, ImVec2 max) {
84 ImGui::GetForegroundDrawList()->AddRect(
90 if (!color || !ImGui::BeginDragDropTarget()) {
95 constexpr ImGuiDragDropFlags kFlags =
96 ImGuiDragDropFlags_AcceptBeforeDelivery |
97 ImGuiDragDropFlags_AcceptNoDrawDefaultRect |
98 ImGuiDragDropFlags_AcceptNoPreviewTooltip;
99 auto accept = [&](
const char* type,
size_t components) {
103 if (
const ImGuiPayload* payload =
104 ImGui::AcceptDragDropPayload(type, kFlags)) {
105 DrawColorDropTargetOutline(min, max);
106 if (payload->IsDelivery()) {
107 ImVec4 dropped(0, 0, 0, 1);
108 std::memcpy(&dropped.x, payload->Data,
sizeof(
float) * components);
109 if (components == 3) {
118 accept(IMGUI_PAYLOAD_TYPE_COLOR_3F, 3);
119 accept(IMGUI_PAYLOAD_TYPE_COLOR_4F, 4);
120 ImGui::EndDragDropTarget();
127 float min_swatch_size,
128 float item_spacing) {
129 const float safe_width = std::max(available_width, 1.0f);
130 const float safe_swatch = std::max(min_swatch_size, 1.0f);
131 const float safe_spacing = std::max(item_spacing, 0.0f);
132 for (
const int columns : {16, 8, 4, 2, 1}) {
133 const float required_width =
134 safe_swatch * columns + safe_spacing * (columns - 1);
135 if (safe_width >= required_width) {
165 int display_index, std::optional<gfx::SnesColor> color) {
167 return absl::FailedPreconditionError(
"GameData not loaded");
172 if (!target.has_value()) {
173 return absl::InvalidArgumentError(absl::StrFormat(
174 "Dungeon render palette slot %d is reserved or out of range",
179 if (!palette_manager.IsManaging(
game_data_)) {
180 return absl::FailedPreconditionError(
181 "PaletteManager is bound to a different ROM session");
183 const char* group_name = PaletteManagerGroupName(target->source);
184 absl::Status status =
186 ? palette_manager.SetColor(group_name, target->palette_index,
187 target->color_index, *color)
188 : palette_manager.ResetColor(group_name, target->palette_index,
189 target->color_index);
197 return absl::OkStatus();
203 ImGui::TextColored(ImVec4(1, 0, 0, 1), tr(
"GameData not loaded"));
213 if (dungeon_pal_group.empty()) {
214 ImGui::TextDisabled(tr(
"Dungeon palettes unavailable"));
241 ImGui::TextDisabled(tr(
"Select a color to edit"));
251 int num_palettes = dungeon_pal_group.
size();
252 if (num_palettes <= 0) {
253 ImGui::TextDisabled(tr(
"No dungeon palettes"));
259 ImGui::Text(tr(
"Dungeon Palette:"));
261 ImGui::SetNextItemWidth(std::min(180.0f, ImGui::GetContentRegionAvail().x));
263 if (ImGui::BeginCombo(
266 for (
int i = 0; i < num_palettes; i++) {
268 if (ImGui::Selectable(absl::StrFormat(
"Palette %d", i).c_str(),
274 ImGui::SetItemDefaultFocus();
281 ImGui::TextUnformatted(tr(
"Palette ID"));
283 ImGui::SetNextItemWidth(72.0f);
285 "##DungeonPaletteId", ImGuiDataType_S32, &requested_palette_id,
"%d", 0,
288 "Dungeon main palette ID");
289 if (palette_id_changed) {
299 if (dungeon_pal_group.empty()) {
304 ImGui::SeparatorText(
318 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_PickerHueWheel)) {
333 ImGui::Text(tr(
"RGB (0-255): (%d, %d, %d)"),
337 ImGui::Text(tr(
"SNES BGR555: 0x%04X"), original_color.snes());
352 ImGui::TextDisabled(tr(
"Slots 00-1F: HUD / floor / ceiling"));
353 ImGui::TextDisabled(tr(
"Slots 20-7F: Dungeon main"));
354 constexpr float kSwatchSpacing = 2.0f;
355 const ImVec2 previous_spacing = ImGui::GetStyle().ItemSpacing;
356 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing,
357 ImVec2(kSwatchSpacing, previous_spacing.y));
359 ImGui::GetContentRegionAvail().x, 14.0f,
363 for (
int i = 0; i < 128; ++i) {
364 if (i % columns != 0) {
369 bool editable =
false;
370 std::string tooltip_label =
"Reserved / transparent";
371 int source_index = -1;
374 if (target.has_value()) {
375 const auto& group = PaletteGroupForTarget(*
game_data_, *target);
376 if (target->palette_index >= 0 &&
377 target->palette_index <
static_cast<int>(group.size())) {
378 const auto& palette = group.palette_ref(target->palette_index);
379 if (target->color_index >= 0 &&
380 target->color_index <
static_cast<int>(palette.size())) {
381 color = palette[target->color_index];
383 tooltip_label = DungeonRenderColorSourceLabel(*target);
384 source_index = target->color_index;
392 ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0.25f);
394 const bool clicked = ImGui::ColorButton(
"##render_color", display_color,
395 ImGuiColorEditFlags_NoTooltip,
396 ImVec2(swatch_size, swatch_size));
398 absl::StrFormat(
"palette_swatch_%03d", i),
399 "Dungeon render palette swatch");
400 const ImVec2 swatch_min = ImGui::GetItemRectMin();
401 const ImVec2 swatch_max = ImGui::GetItemRectMax();
402 const bool double_clicked =
403 editable && ImGui::IsItemHovered() &&
404 ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left);
406 ImGui::PopStyleVar();
409 if ((clicked || double_clicked) && editable) {
417 AcceptImGuiColorDrop(&dropped_color, swatch_min, swatch_max)) {
425 LOG_ERROR(
"PaletteEditorWidget",
"Failed to apply dropped color: %s",
426 status.message().data());
430 if (ImGui::IsItemHovered()) {
432 ImGui::SetTooltip(tr(
"%s\nSlot %d\nSNES: 0x%04X\nRGB: (%d, %d, %d)"),
433 tooltip_label.c_str(), source_index, color.
snes(),
434 static_cast<int>(display_color.x * 255),
435 static_cast<int>(display_color.y * 255),
436 static_cast<int>(display_color.z * 255));
438 ImGui::SetTooltip(
"%s", tooltip_label.c_str());
443 ImGui::PopStyleVar();
447 float max_size)
const {
448 const float available_width =
449 std::max(ImGui::GetContentRegionAvail().x, 1.0f);
450 const float spacing =
451 std::max(ImGui::GetStyle().ItemSpacing.x, 2.0f) * (columns - 1);
452 const float raw = (available_width - spacing) / std::max(columns, 1);
453 return std::clamp(raw, min_size, max_size);
465 const auto& group = PaletteGroupForTarget(*
game_data_, *target);
466 if (target->palette_index < 0 ||
467 target->palette_index >=
static_cast<int>(group.size())) {
472 const auto& palette = group.palette_ref(target->palette_index);
473 if (target->color_index < 0 ||
474 target->color_index >=
static_cast<int>(palette.size())) {
480 ImGui::SeparatorText(absl::StrFormat(
"%s Color %d",
481 DungeonRenderColorSourceLabel(*target),
485 const gfx::SnesColor current_color = palette[target->color_index];
488 "Color", &edited_color,
489 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_PickerHueWheel)) {
490 const absl::Status status =
495 LOG_ERROR(
"PaletteEditorWidget",
"Failed to apply palette color: %s",
496 status.message().data());
500 uint16_t raw_color = current_color.
snes();
501 ImGui::AlignTextToFramePadding();
502 ImGui::TextUnformatted(tr(
"Raw BGR555"));
504 ImGui::SetNextItemWidth(96.0f);
506 "##SelectedDungeonPaletteBgr555", ImGuiDataType_U16, &raw_color,
"%04X",
507 ImGuiInputTextFlags_CharsHexadecimal,
509 static_cast<uint64_t
>(target->source),
510 static_cast<uint64_t
>(target->palette_index),
511 static_cast<uint64_t
>(target->color_index)});
513 "Selected dungeon palette raw BGR555 color");
517 const absl::Status status =
522 LOG_ERROR(
"PaletteEditorWidget",
"Failed to apply raw palette color: %s",
523 status.message().data());
527 ImGui::Text(tr(
"RGB (0-255): (%d, %d, %d)"),
531 ImGui::Text(tr(
"SNES BGR555: 0x%04X"), current_color.
snes());
534 const absl::Status status =
538 PaletteManagerGroupName(target->source), target->palette_index,
539 target->color_index);
542 LOG_ERROR(
"PaletteEditorWidget",
"Failed to reset palette color: %s",
543 status.message().data());
551 const std::string& title) {
552 if (ImGui::BeginPopupModal(title.c_str(),
nullptr,
553 ImGuiWindowFlags_AlwaysAutoResize)) {
554 ImGui::Text(tr(
"Enhanced Palette Editor"));
560 if (ImGui::CollapsingHeader(tr(
"Palette Analysis"))) {
564 if (ImGui::CollapsingHeader(tr(
"ROM Palette Manager")) &&
rom_) {
587 ImGui::CloseCurrentPopup();
600 ImGui::Text(tr(
"No ROM loaded"));
613 ImGui::Text(tr(
"Preview of %s:"),
625 const std::string& title) {
630 ImGui::Text(tr(
"Bitmap Color Analysis"));
634 ImGui::Text(tr(
"Bitmap is not active"));
639 std::map<uint8_t, int> pixel_counts;
640 const auto& data = bitmap.
vector();
642 for (uint8_t pixel : data) {
643 uint8_t palette_index = pixel & 0x0F;
644 pixel_counts[palette_index]++;
647 ImGui::Text(tr(
"Bitmap Size: %d x %d (%zu pixels)"), bitmap.
width(),
648 bitmap.
height(), data.size());
651 ImGui::Text(tr(
"Pixel Distribution:"));
653 int total_pixels =
static_cast<int>(data.size());
657 .x_label =
"Palette Index",
659 .flags = ImPlotFlags_NoBoxSelect,
660 .x_axis_flags = ImPlotAxisFlags_AutoFit,
661 .y_axis_flags = ImPlotAxisFlags_AutoFit};
662 std::vector<double> x;
663 std::vector<double> y;
664 x.reserve(pixel_counts.size());
665 y.reserve(pixel_counts.size());
666 for (
const auto& [index, count] : pixel_counts) {
667 x.push_back(
static_cast<double>(index));
668 y.push_back(
static_cast<double>(count));
671 if (plot && !x.empty()) {
672 ImPlot::PlotBars(
"Usage", x.data(), y.data(),
static_cast<int>(x.size()),
673 0.67, 0.0, ImPlotBarsFlags_None);
690 if (palette_index >= 0 && palette_index < 8) {
702 }
catch (
const std::exception& e) {
729 const float swatch_size =
731 for (
int i = 0; i < static_cast<int>(palette.
size()); i++) {
735 auto color = palette[i];
739 const bool clicked = ImGui::ColorButton(
"##color", display_color,
740 ImGuiColorEditFlags_NoTooltip,
741 ImVec2(swatch_size, swatch_size));
742 const ImVec2 swatch_min = ImGui::GetItemRectMin();
743 const ImVec2 swatch_max = ImGui::GetItemRectMax();
744 const bool double_clicked =
745 ImGui::IsItemHovered() &&
746 ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left);
747 if (clicked || double_clicked) {
753 if (AcceptImGuiColorDrop(&palette[i], swatch_min, swatch_max)) {
764 if (ImGui::BeginPopupContextItem()) {
765 ImGui::Text(tr(
"Color %d (0x%04X)"), i, color.snes());
767 if (ImGui::MenuItem(tr(
"Edit Color"))) {
773 if (ImGui::MenuItem(tr(
"Reset to Black"))) {
783 if (ImGui::IsItemHovered()) {
784 ImGui::SetTooltip(tr(
"Color %d\nSNES: 0x%04X\nRGB: (%d, %d, %d)"), i,
785 color.snes(),
static_cast<int>(display_color.x * 255),
786 static_cast<int>(display_color.y * 255),
787 static_cast<int>(display_color.z * 255));
795 std::string popup_id =
798 ImGui::OpenPopup(popup_id.c_str());
799 if (ImGui::BeginPopupModal(popup_id.c_str(),
nullptr,
800 ImGuiWindowFlags_AlwaysAutoResize)) {
802 if (ImGui::ColorEdit4(
804 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_DisplayRGB)) {
817 ImGui::CloseCurrentPopup();
822 ImGui::CloseCurrentPopup();
835 ImGui::Text(tr(
"No ROM palettes available"));
839 ImGui::Text(tr(
"Palette Group:"));
844 ImGui::Text(tr(
"Palette Index:"));
848 ImGui::Text(tr(
"Preview:"));
850 for (
int i = 0; i < 8 && i < static_cast<int>(preview_palette.size());
854 auto color = preview_palette[i];
856 ImGui::ColorButton((
"##preview" + std::to_string(i)).c_str(),
857 display_color, ImGuiColorEditFlags_NoTooltip,
867 ImGui::PushID(color_index);
869 if (ImGui::ColorEdit4(
870 "##color_edit", &rgba.x,
871 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_DisplayRGB)) {
875 ImGui::Text(tr(
"SNES Color: 0x%04X"), color.
snes());
877 int r = (color.
snes() & 0x1F);
878 int g = (color.
snes() >> 5) & 0x1F;
879 int b = (color.
snes() >> 10) & 0x1F;
881 if (ImGui::SliderInt(tr(
"Red"), &r, 0, 31)) {
882 uint16_t new_color = (color.
snes() & 0xFFE0) | (r & 0x1F);
885 if (ImGui::SliderInt(tr(
"Green"), &g, 0, 31)) {
886 uint16_t new_color = (color.
snes() & 0xFC1F) | ((g & 0x1F) << 5);
889 if (ImGui::SliderInt(tr(
"Blue"), &b, 0, 31)) {
890 uint16_t new_color = (color.
snes() & 0x83FF) | ((b & 0x1F) << 10);
898 ImGui::Text(tr(
"Palette Information:"));
899 ImGui::Text(tr(
"Size: %zu colors"), palette.
size());
901 std::map<uint16_t, int> color_frequency;
902 for (
int i = 0; i < static_cast<int>(palette.
size()); i++) {
903 color_frequency[palette[i].snes()]++;
906 ImGui::Text(tr(
"Unique Colors: %zu"), color_frequency.size());
908 if (color_frequency.size() < palette.
size()) {
909 ImGui::TextColored(ImVec4(1, 1, 0, 1),
910 tr(
"Warning: Duplicate colors detected!"));
911 if (ImGui::TreeNode(
"Duplicate Colors")) {
912 for (
const auto& [
snes_color, count] : color_frequency) {
914 ImVec4 display_color =
916 ImGui::ColorButton((
"##dup" + std::to_string(
snes_color)).c_str(),
917 display_color, ImGuiColorEditFlags_NoTooltip,
920 ImGui::Text(tr(
"0x%04X appears %d times"),
snes_color, count);
932 .x_label =
"Color Index",
934 .flags = ImPlotFlags_NoBoxSelect,
935 .x_axis_flags = ImPlotAxisFlags_AutoFit,
936 .y_axis_flags = ImPlotAxisFlags_AutoFit};
937 std::vector<double> x;
938 std::vector<double> y;
939 x.reserve(color_frequency.size());
940 y.reserve(color_frequency.size());
941 for (
const auto& [
snes_color, count] : color_frequency) {
943 y.push_back(
static_cast<double>(count));
946 if (plot && !x.empty()) {
947 ImPlot::PlotBars(
"Count", x.data(), y.data(),
static_cast<int>(x.size()),
948 0.5, 0.0, ImPlotBarsFlags_None);
952 float total_brightness = 0.0f;
953 float min_brightness = 1.0f;
954 float max_brightness = 0.0f;
956 for (
int i = 0; i < static_cast<int>(palette.
size()); i++) {
958 float brightness = (color.x + color.y + color.z) / 3.0f;
959 total_brightness += brightness;
960 min_brightness = std::min(min_brightness, brightness);
961 max_brightness = std::max(max_brightness, brightness);
964 float avg_brightness = total_brightness / palette.
size();
967 ImGui::Text(tr(
"Brightness Analysis:"));
968 ImGui::Text(tr(
"Average: %.2f"), avg_brightness);
969 ImGui::Text(tr(
"Range: %.2f - %.2f"), min_brightness, max_brightness);
971 ImGui::Text(tr(
"Brightness Distribution:"));
972 ImGui::ProgressBar(avg_brightness, ImVec2(-1, 0),
"Avg");
984 if (palette_groups.overworld_main.size() > 0) {
988 if (palette_groups.overworld_aux.size() > 0) {
992 if (palette_groups.overworld_animated.size() > 0) {
996 if (palette_groups.dungeon_main.size() > 0) {
1000 if (palette_groups.global_sprites.size() > 0) {
1004 if (palette_groups.armors.size() > 0) {
1008 if (palette_groups.swords.size() > 0) {
1014 }
catch (
const std::exception& e) {
1015 LOG_ERROR(
"Enhanced Palette Editor",
"Failed to load ROM palettes");
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Represents a bitmap image optimized for SNES ROM hacking.
const SnesPalette & palette() const
const std::vector< uint8_t > & vector() const
void SetPalette(const SnesPalette &palette)
Set the palette for the bitmap using SNES palette format.
void SetPaletteWithTransparent(const SnesPalette &palette, size_t index, int length=7)
Set the palette with a transparent color.
static PaletteManager & Get()
Get the singleton instance.
SnesColor GetColor(const std::string &group_name, int palette_index, int color_index) const
Get a color from a palette.
constexpr uint16_t snes() const
Get SNES 15-bit color.
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
const Theme & GetCurrentTheme() const
static ThemeManager & Get()
#define LOG_ERROR(category, format,...)
ImVec4 ConvertColorToImVec4(const Color &color)
bool PrimaryButton(const char *label, const ImVec2 &size, const char *panel_id, const char *anim_id)
Draw a primary action button (accented color).
DungeonRenderPaletteSource
void AutoRegisterLastItem(const std::string &widget_type, const std::string &explicit_label, const std::string &description)
Automatically register the last ImGui item.
std::string MakePopupIdWithInstance(const std::string &editor_name, const std::string &popup_name, const void *instance)
Generate popup ID with instance pointer for guaranteed uniqueness.
bool ThemedButton(const char *label, const ImVec2 &size, const char *panel_id, const char *anim_id)
Draw a standard text button with theme colors.
ImVec4 ConvertSnesColorToImVec4(const gfx::SnesColor &color)
Convert SnesColor to standard ImVec4 for display.
bool InputScalarDeferred(const char *label, ImGuiDataType data_type, void *data, const char *format, ImGuiInputTextFlags flags, InputScalarTargetIdentity target_identity)
int ResolveDungeonRenderPaletteColumns(float available_width, float min_swatch_size, float item_spacing)
IMGUI_API bool SnesColorEdit4(absl::string_view label, gfx::SnesColor *color, ImGuiColorEditFlags flags)
SNES color in 15-bit RGB format (BGR555)
PaletteGroup dungeon_main
Represents a group of palettes.
gfx::PaletteGroupMap palette_groups