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();
149 int display_index, std::optional<gfx::SnesColor> color) {
151 return absl::FailedPreconditionError(
"GameData not loaded");
156 if (!target.has_value()) {
157 return absl::InvalidArgumentError(absl::StrFormat(
158 "Dungeon render palette slot %d is reserved or out of range",
163 if (!palette_manager.IsManaging(
game_data_)) {
164 return absl::FailedPreconditionError(
165 "PaletteManager is bound to a different ROM session");
167 const char* group_name = PaletteManagerGroupName(target->source);
168 absl::Status status =
170 ? palette_manager.SetColor(group_name, target->palette_index,
171 target->color_index, *color)
172 : palette_manager.ResetColor(group_name, target->palette_index,
173 target->color_index);
181 return absl::OkStatus();
187 ImGui::TextColored(ImVec4(1, 0, 0, 1), tr(
"GameData not loaded"));
197 if (dungeon_pal_group.empty()) {
198 ImGui::TextDisabled(tr(
"Dungeon palettes unavailable"));
225 ImGui::TextDisabled(tr(
"Select a color to edit"));
235 int num_palettes = dungeon_pal_group.
size();
236 if (num_palettes <= 0) {
237 ImGui::TextDisabled(tr(
"No dungeon palettes"));
243 ImGui::Text(tr(
"Dungeon Palette:"));
245 ImGui::SetNextItemWidth(std::min(180.0f, ImGui::GetContentRegionAvail().x));
247 if (ImGui::BeginCombo(
250 for (
int i = 0; i < num_palettes; i++) {
252 if (ImGui::Selectable(absl::StrFormat(
"Palette %d", i).c_str(),
258 ImGui::SetItemDefaultFocus();
265 ImGui::TextUnformatted(tr(
"Palette ID"));
267 ImGui::SetNextItemWidth(72.0f);
269 "##DungeonPaletteId", ImGuiDataType_S32, &requested_palette_id,
"%d", 0,
272 "Dungeon main palette ID");
273 if (palette_id_changed) {
283 if (dungeon_pal_group.empty()) {
288 ImGui::SeparatorText(
302 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_PickerHueWheel)) {
317 ImGui::Text(tr(
"RGB (0-255): (%d, %d, %d)"),
321 ImGui::Text(tr(
"SNES BGR555: 0x%04X"), original_color.snes());
337 tr(
"Rows 0-1: HUD / floor / ceiling | Rows 2-7: Dungeon main"));
340 for (
int i = 0; i < 128; ++i) {
346 bool editable =
false;
347 std::string tooltip_label =
"Reserved / transparent";
348 int source_index = -1;
351 if (target.has_value()) {
352 const auto& group = PaletteGroupForTarget(*
game_data_, *target);
353 if (target->palette_index >= 0 &&
354 target->palette_index <
static_cast<int>(group.size())) {
355 const auto& palette = group.palette_ref(target->palette_index);
356 if (target->color_index >= 0 &&
357 target->color_index <
static_cast<int>(palette.size())) {
358 color = palette[target->color_index];
360 tooltip_label = DungeonRenderColorSourceLabel(*target);
361 source_index = target->color_index;
369 ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0.25f);
371 const bool clicked = ImGui::ColorButton(
"##render_color", display_color,
372 ImGuiColorEditFlags_NoTooltip,
373 ImVec2(swatch_size, swatch_size));
375 absl::StrFormat(
"palette_swatch_%03d", i),
376 "Dungeon render palette swatch");
377 const ImVec2 swatch_min = ImGui::GetItemRectMin();
378 const ImVec2 swatch_max = ImGui::GetItemRectMax();
379 const bool double_clicked =
380 editable && ImGui::IsItemHovered() &&
381 ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left);
383 ImGui::PopStyleVar();
386 if ((clicked || double_clicked) && editable) {
394 AcceptImGuiColorDrop(&dropped_color, swatch_min, swatch_max)) {
402 LOG_ERROR(
"PaletteEditorWidget",
"Failed to apply dropped color: %s",
403 status.message().data());
407 if (ImGui::IsItemHovered()) {
409 ImGui::SetTooltip(tr(
"%s\nSlot %d\nSNES: 0x%04X\nRGB: (%d, %d, %d)"),
410 tooltip_label.c_str(), source_index, color.
snes(),
411 static_cast<int>(display_color.x * 255),
412 static_cast<int>(display_color.y * 255),
413 static_cast<int>(display_color.z * 255));
415 ImGui::SetTooltip(
"%s", tooltip_label.c_str());
423 float max_size)
const {
424 const float available_width =
425 std::max(ImGui::GetContentRegionAvail().x, 1.0f);
426 const float spacing =
427 std::max(ImGui::GetStyle().ItemSpacing.x, 2.0f) * (columns - 1);
428 const float raw = (available_width - spacing) / std::max(columns, 1);
429 return std::clamp(raw, min_size, max_size);
441 const auto& group = PaletteGroupForTarget(*
game_data_, *target);
442 if (target->palette_index < 0 ||
443 target->palette_index >=
static_cast<int>(group.size())) {
448 const auto& palette = group.palette_ref(target->palette_index);
449 if (target->color_index < 0 ||
450 target->color_index >=
static_cast<int>(palette.size())) {
456 ImGui::SeparatorText(absl::StrFormat(
"%s Color %d",
457 DungeonRenderColorSourceLabel(*target),
461 const gfx::SnesColor current_color = palette[target->color_index];
464 "Color", &edited_color,
465 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_PickerHueWheel)) {
466 const absl::Status status =
471 LOG_ERROR(
"PaletteEditorWidget",
"Failed to apply palette color: %s",
472 status.message().data());
476 uint16_t raw_color = current_color.
snes();
477 ImGui::AlignTextToFramePadding();
478 ImGui::TextUnformatted(tr(
"Raw BGR555"));
480 ImGui::SetNextItemWidth(96.0f);
482 "##SelectedDungeonPaletteBgr555", ImGuiDataType_U16, &raw_color,
"%04X",
483 ImGuiInputTextFlags_CharsHexadecimal,
485 static_cast<uint64_t
>(target->source),
486 static_cast<uint64_t
>(target->palette_index),
487 static_cast<uint64_t
>(target->color_index)});
489 "Selected dungeon palette raw BGR555 color");
493 const absl::Status status =
498 LOG_ERROR(
"PaletteEditorWidget",
"Failed to apply raw palette color: %s",
499 status.message().data());
503 ImGui::Text(tr(
"RGB (0-255): (%d, %d, %d)"),
507 ImGui::Text(tr(
"SNES BGR555: 0x%04X"), current_color.
snes());
510 const absl::Status status =
514 PaletteManagerGroupName(target->source), target->palette_index,
515 target->color_index);
518 LOG_ERROR(
"PaletteEditorWidget",
"Failed to reset palette color: %s",
519 status.message().data());
527 const std::string& title) {
528 if (ImGui::BeginPopupModal(title.c_str(),
nullptr,
529 ImGuiWindowFlags_AlwaysAutoResize)) {
530 ImGui::Text(tr(
"Enhanced Palette Editor"));
536 if (ImGui::CollapsingHeader(tr(
"Palette Analysis"))) {
540 if (ImGui::CollapsingHeader(tr(
"ROM Palette Manager")) &&
rom_) {
563 ImGui::CloseCurrentPopup();
576 ImGui::Text(tr(
"No ROM loaded"));
589 ImGui::Text(tr(
"Preview of %s:"),
601 const std::string& title) {
606 ImGui::Text(tr(
"Bitmap Color Analysis"));
610 ImGui::Text(tr(
"Bitmap is not active"));
615 std::map<uint8_t, int> pixel_counts;
616 const auto& data = bitmap.
vector();
618 for (uint8_t pixel : data) {
619 uint8_t palette_index = pixel & 0x0F;
620 pixel_counts[palette_index]++;
623 ImGui::Text(tr(
"Bitmap Size: %d x %d (%zu pixels)"), bitmap.
width(),
624 bitmap.
height(), data.size());
627 ImGui::Text(tr(
"Pixel Distribution:"));
629 int total_pixels =
static_cast<int>(data.size());
633 .x_label =
"Palette Index",
635 .flags = ImPlotFlags_NoBoxSelect,
636 .x_axis_flags = ImPlotAxisFlags_AutoFit,
637 .y_axis_flags = ImPlotAxisFlags_AutoFit};
638 std::vector<double> x;
639 std::vector<double> y;
640 x.reserve(pixel_counts.size());
641 y.reserve(pixel_counts.size());
642 for (
const auto& [index, count] : pixel_counts) {
643 x.push_back(
static_cast<double>(index));
644 y.push_back(
static_cast<double>(count));
647 if (plot && !x.empty()) {
648 ImPlot::PlotBars(
"Usage", x.data(), y.data(),
static_cast<int>(x.size()),
649 0.67, 0.0, ImPlotBarsFlags_None);
666 if (palette_index >= 0 && palette_index < 8) {
678 }
catch (
const std::exception& e) {
705 const float swatch_size =
707 for (
int i = 0; i < static_cast<int>(palette.
size()); i++) {
711 auto color = palette[i];
715 const bool clicked = ImGui::ColorButton(
"##color", display_color,
716 ImGuiColorEditFlags_NoTooltip,
717 ImVec2(swatch_size, swatch_size));
718 const ImVec2 swatch_min = ImGui::GetItemRectMin();
719 const ImVec2 swatch_max = ImGui::GetItemRectMax();
720 const bool double_clicked =
721 ImGui::IsItemHovered() &&
722 ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left);
723 if (clicked || double_clicked) {
729 if (AcceptImGuiColorDrop(&palette[i], swatch_min, swatch_max)) {
740 if (ImGui::BeginPopupContextItem()) {
741 ImGui::Text(tr(
"Color %d (0x%04X)"), i, color.snes());
743 if (ImGui::MenuItem(tr(
"Edit Color"))) {
749 if (ImGui::MenuItem(tr(
"Reset to Black"))) {
759 if (ImGui::IsItemHovered()) {
760 ImGui::SetTooltip(tr(
"Color %d\nSNES: 0x%04X\nRGB: (%d, %d, %d)"), i,
761 color.snes(),
static_cast<int>(display_color.x * 255),
762 static_cast<int>(display_color.y * 255),
763 static_cast<int>(display_color.z * 255));
771 std::string popup_id =
774 ImGui::OpenPopup(popup_id.c_str());
775 if (ImGui::BeginPopupModal(popup_id.c_str(),
nullptr,
776 ImGuiWindowFlags_AlwaysAutoResize)) {
778 if (ImGui::ColorEdit4(
780 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_DisplayRGB)) {
793 ImGui::CloseCurrentPopup();
798 ImGui::CloseCurrentPopup();
811 ImGui::Text(tr(
"No ROM palettes available"));
815 ImGui::Text(tr(
"Palette Group:"));
820 ImGui::Text(tr(
"Palette Index:"));
824 ImGui::Text(tr(
"Preview:"));
826 for (
int i = 0; i < 8 && i < static_cast<int>(preview_palette.size());
830 auto color = preview_palette[i];
832 ImGui::ColorButton((
"##preview" + std::to_string(i)).c_str(),
833 display_color, ImGuiColorEditFlags_NoTooltip,
843 ImGui::PushID(color_index);
845 if (ImGui::ColorEdit4(
846 "##color_edit", &rgba.x,
847 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_DisplayRGB)) {
851 ImGui::Text(tr(
"SNES Color: 0x%04X"), color.
snes());
853 int r = (color.
snes() & 0x1F);
854 int g = (color.
snes() >> 5) & 0x1F;
855 int b = (color.
snes() >> 10) & 0x1F;
857 if (ImGui::SliderInt(tr(
"Red"), &r, 0, 31)) {
858 uint16_t new_color = (color.
snes() & 0xFFE0) | (r & 0x1F);
861 if (ImGui::SliderInt(tr(
"Green"), &g, 0, 31)) {
862 uint16_t new_color = (color.
snes() & 0xFC1F) | ((g & 0x1F) << 5);
865 if (ImGui::SliderInt(tr(
"Blue"), &b, 0, 31)) {
866 uint16_t new_color = (color.
snes() & 0x83FF) | ((b & 0x1F) << 10);
874 ImGui::Text(tr(
"Palette Information:"));
875 ImGui::Text(tr(
"Size: %zu colors"), palette.
size());
877 std::map<uint16_t, int> color_frequency;
878 for (
int i = 0; i < static_cast<int>(palette.
size()); i++) {
879 color_frequency[palette[i].snes()]++;
882 ImGui::Text(tr(
"Unique Colors: %zu"), color_frequency.size());
884 if (color_frequency.size() < palette.
size()) {
885 ImGui::TextColored(ImVec4(1, 1, 0, 1),
886 tr(
"Warning: Duplicate colors detected!"));
887 if (ImGui::TreeNode(
"Duplicate Colors")) {
888 for (
const auto& [
snes_color, count] : color_frequency) {
890 ImVec4 display_color =
892 ImGui::ColorButton((
"##dup" + std::to_string(
snes_color)).c_str(),
893 display_color, ImGuiColorEditFlags_NoTooltip,
896 ImGui::Text(tr(
"0x%04X appears %d times"),
snes_color, count);
908 .x_label =
"Color Index",
910 .flags = ImPlotFlags_NoBoxSelect,
911 .x_axis_flags = ImPlotAxisFlags_AutoFit,
912 .y_axis_flags = ImPlotAxisFlags_AutoFit};
913 std::vector<double> x;
914 std::vector<double> y;
915 x.reserve(color_frequency.size());
916 y.reserve(color_frequency.size());
917 for (
const auto& [
snes_color, count] : color_frequency) {
919 y.push_back(
static_cast<double>(count));
922 if (plot && !x.empty()) {
923 ImPlot::PlotBars(
"Count", x.data(), y.data(),
static_cast<int>(x.size()),
924 0.5, 0.0, ImPlotBarsFlags_None);
928 float total_brightness = 0.0f;
929 float min_brightness = 1.0f;
930 float max_brightness = 0.0f;
932 for (
int i = 0; i < static_cast<int>(palette.
size()); i++) {
934 float brightness = (color.x + color.y + color.z) / 3.0f;
935 total_brightness += brightness;
936 min_brightness = std::min(min_brightness, brightness);
937 max_brightness = std::max(max_brightness, brightness);
940 float avg_brightness = total_brightness / palette.
size();
943 ImGui::Text(tr(
"Brightness Analysis:"));
944 ImGui::Text(tr(
"Average: %.2f"), avg_brightness);
945 ImGui::Text(tr(
"Range: %.2f - %.2f"), min_brightness, max_brightness);
947 ImGui::Text(tr(
"Brightness Distribution:"));
948 ImGui::ProgressBar(avg_brightness, ImVec2(-1, 0),
"Avg");
960 if (palette_groups.overworld_main.size() > 0) {
964 if (palette_groups.overworld_aux.size() > 0) {
968 if (palette_groups.overworld_animated.size() > 0) {
972 if (palette_groups.dungeon_main.size() > 0) {
976 if (palette_groups.global_sprites.size() > 0) {
980 if (palette_groups.armors.size() > 0) {
984 if (palette_groups.swords.size() > 0) {
990 }
catch (
const std::exception& e) {
991 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)
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