7#include "absl/strings/str_format.h"
33 int display_index,
int dungeon_palette_id) {
34 if (display_index < 0 || display_index >= 128) {
37 if (display_index < 32) {
42 const int local_index = display_index - 32;
43 const int row = local_index / 16;
44 const int col = local_index % 16;
45 if (row >= 6 || col == 0) {
50 dungeon_palette_id, row * 15 + (col - 1)};
67 return "HUD / floor-ceiling";
69 return absl::StrFormat(
"Dungeon row %d", target.
color_index / 15 + 2);
73 auto* names =
static_cast<std::vector<std::string>*
>(user_data);
74 if (!names || idx < 0 || idx >=
static_cast<int>(names->size())) {
77 return (*names)[idx].c_str();
80void DrawColorDropTargetOutline(ImVec2 min, ImVec2 max) {
82 ImGui::GetForegroundDrawList()->AddRect(
88 if (!color || !ImGui::BeginDragDropTarget()) {
93 constexpr ImGuiDragDropFlags kFlags =
94 ImGuiDragDropFlags_AcceptBeforeDelivery |
95 ImGuiDragDropFlags_AcceptNoDrawDefaultRect |
96 ImGuiDragDropFlags_AcceptNoPreviewTooltip;
97 auto accept = [&](
const char* type,
size_t components) {
101 if (
const ImGuiPayload* payload =
102 ImGui::AcceptDragDropPayload(type, kFlags)) {
103 DrawColorDropTargetOutline(min, max);
104 if (payload->IsDelivery()) {
105 ImVec4 dropped(0, 0, 0, 1);
106 std::memcpy(&dropped.x, payload->Data,
sizeof(
float) * components);
107 if (components == 3) {
116 accept(IMGUI_PAYLOAD_TYPE_COLOR_3F, 3);
117 accept(IMGUI_PAYLOAD_TYPE_COLOR_4F, 4);
118 ImGui::EndDragDropTarget();
147 int display_index, std::optional<gfx::SnesColor> color) {
149 return absl::FailedPreconditionError(
"GameData not loaded");
154 if (!target.has_value()) {
155 return absl::InvalidArgumentError(absl::StrFormat(
156 "Dungeon render palette slot %d is reserved or out of range",
161 if (!palette_manager.IsManaging(
game_data_)) {
162 return absl::FailedPreconditionError(
163 "PaletteManager is bound to a different ROM session");
165 const char* group_name = PaletteManagerGroupName(target->source);
166 absl::Status status =
168 ? palette_manager.SetColor(group_name, target->palette_index,
169 target->color_index, *color)
170 : palette_manager.ResetColor(group_name, target->palette_index,
171 target->color_index);
179 return absl::OkStatus();
185 ImGui::TextColored(ImVec4(1, 0, 0, 1), tr(
"GameData not loaded"));
195 if (dungeon_pal_group.empty()) {
196 ImGui::TextDisabled(tr(
"Dungeon palettes unavailable"));
223 ImGui::TextDisabled(tr(
"Select a color to edit"));
233 int num_palettes = dungeon_pal_group.
size();
234 if (num_palettes <= 0) {
235 ImGui::TextDisabled(tr(
"No dungeon palettes"));
241 ImGui::Text(tr(
"Dungeon Palette:"));
243 ImGui::SetNextItemWidth(std::min(180.0f, ImGui::GetContentRegionAvail().x));
245 if (ImGui::BeginCombo(
248 for (
int i = 0; i < num_palettes; i++) {
250 if (ImGui::Selectable(absl::StrFormat(
"Palette %d", i).c_str(),
256 ImGui::SetItemDefaultFocus();
267 if (dungeon_pal_group.empty()) {
272 ImGui::SeparatorText(
286 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_PickerHueWheel)) {
301 ImGui::Text(tr(
"RGB (0-255): (%d, %d, %d)"),
305 ImGui::Text(tr(
"SNES BGR555: 0x%04X"), original_color.snes());
309 ImVec4(original_color.rgb().x / 255.0f, original_color.rgb().y / 255.0f,
310 original_color.rgb().z / 255.0f, 1.0f);
323 tr(
"Rows 0-1: HUD / floor / ceiling | Rows 2-7: Dungeon main"));
326 for (
int i = 0; i < 128; ++i) {
332 bool editable =
false;
333 std::string tooltip_label =
"Reserved / transparent";
334 int source_index = -1;
337 if (target.has_value()) {
338 const auto& group = PaletteGroupForTarget(*
game_data_, *target);
339 if (target->palette_index >= 0 &&
340 target->palette_index <
static_cast<int>(group.size())) {
341 const auto& palette = group.palette_ref(target->palette_index);
342 if (target->color_index >= 0 &&
343 target->color_index <
static_cast<int>(palette.size())) {
344 color = palette[target->color_index];
346 tooltip_label = DungeonRenderColorSourceLabel(*target);
347 source_index = target->color_index;
352 ImVec4 display_color = color.
rgb();
355 ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0.25f);
357 const bool clicked = ImGui::ColorButton(
"##render_color", display_color,
358 ImGuiColorEditFlags_NoTooltip,
359 ImVec2(swatch_size, swatch_size));
360 const ImVec2 swatch_min = ImGui::GetItemRectMin();
361 const ImVec2 swatch_max = ImGui::GetItemRectMax();
362 const bool double_clicked =
363 editable && ImGui::IsItemHovered() &&
364 ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left);
366 ImGui::PopStyleVar();
369 if ((clicked || double_clicked) && editable) {
377 AcceptImGuiColorDrop(&dropped_color, swatch_min, swatch_max)) {
385 LOG_ERROR(
"PaletteEditorWidget",
"Failed to apply dropped color: %s",
386 status.message().data());
390 if (ImGui::IsItemHovered()) {
392 ImGui::SetTooltip(tr(
"%s\nSlot %d\nSNES: 0x%04X\nRGB: (%d, %d, %d)"),
393 tooltip_label.c_str(), source_index, color.
snes(),
394 static_cast<int>(display_color.x * 255),
395 static_cast<int>(display_color.y * 255),
396 static_cast<int>(display_color.z * 255));
398 ImGui::SetTooltip(
"%s", tooltip_label.c_str());
406 float max_size)
const {
407 const float available_width =
408 std::max(ImGui::GetContentRegionAvail().x, 1.0f);
409 const float spacing =
410 std::max(ImGui::GetStyle().ItemSpacing.x, 2.0f) * (columns - 1);
411 const float raw = (available_width - spacing) / std::max(columns, 1);
412 return std::clamp(raw, min_size, max_size);
424 const auto& group = PaletteGroupForTarget(*
game_data_, *target);
425 if (target->palette_index < 0 ||
426 target->palette_index >=
static_cast<int>(group.size())) {
431 const auto& palette = group.palette_ref(target->palette_index);
432 if (target->color_index < 0 ||
433 target->color_index >=
static_cast<int>(palette.size())) {
439 ImGui::SeparatorText(absl::StrFormat(
"%s Color %d",
440 DungeonRenderColorSourceLabel(*target),
444 const gfx::SnesColor current_color = palette[target->color_index];
447 "Color", &edited_color,
448 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_PickerHueWheel)) {
449 const absl::Status status =
454 LOG_ERROR(
"PaletteEditorWidget",
"Failed to apply palette color: %s",
455 status.message().data());
459 ImGui::Text(tr(
"RGB (0-255): (%d, %d, %d)"),
463 ImGui::Text(tr(
"SNES BGR555: 0x%04X"), current_color.
snes());
466 const absl::Status status =
470 PaletteManagerGroupName(target->source), target->palette_index,
471 target->color_index);
474 LOG_ERROR(
"PaletteEditorWidget",
"Failed to reset palette color: %s",
475 status.message().data());
483 const std::string& title) {
484 if (ImGui::BeginPopupModal(title.c_str(),
nullptr,
485 ImGuiWindowFlags_AlwaysAutoResize)) {
486 ImGui::Text(tr(
"Enhanced Palette Editor"));
492 if (ImGui::CollapsingHeader(tr(
"Palette Analysis"))) {
496 if (ImGui::CollapsingHeader(tr(
"ROM Palette Manager")) &&
rom_) {
519 ImGui::CloseCurrentPopup();
532 ImGui::Text(tr(
"No ROM loaded"));
545 ImGui::Text(tr(
"Preview of %s:"),
557 const std::string& title) {
562 ImGui::Text(tr(
"Bitmap Color Analysis"));
566 ImGui::Text(tr(
"Bitmap is not active"));
571 std::map<uint8_t, int> pixel_counts;
572 const auto& data = bitmap.
vector();
574 for (uint8_t pixel : data) {
575 uint8_t palette_index = pixel & 0x0F;
576 pixel_counts[palette_index]++;
579 ImGui::Text(tr(
"Bitmap Size: %d x %d (%zu pixels)"), bitmap.
width(),
580 bitmap.
height(), data.size());
583 ImGui::Text(tr(
"Pixel Distribution:"));
585 int total_pixels =
static_cast<int>(data.size());
589 .x_label =
"Palette Index",
591 .flags = ImPlotFlags_NoBoxSelect,
592 .x_axis_flags = ImPlotAxisFlags_AutoFit,
593 .y_axis_flags = ImPlotAxisFlags_AutoFit};
594 std::vector<double> x;
595 std::vector<double> y;
596 x.reserve(pixel_counts.size());
597 y.reserve(pixel_counts.size());
598 for (
const auto& [index, count] : pixel_counts) {
599 x.push_back(
static_cast<double>(index));
600 y.push_back(
static_cast<double>(count));
603 if (plot && !x.empty()) {
604 ImPlot::PlotBars(
"Usage", x.data(), y.data(),
static_cast<int>(x.size()),
605 0.67, 0.0, ImPlotBarsFlags_None);
622 if (palette_index >= 0 && palette_index < 8) {
634 }
catch (
const std::exception& e) {
661 const float swatch_size =
663 for (
int i = 0; i < static_cast<int>(palette.
size()); i++) {
667 auto color = palette[i];
668 ImVec4 display_color = color.rgb();
671 const bool clicked = ImGui::ColorButton(
"##color", display_color,
672 ImGuiColorEditFlags_NoTooltip,
673 ImVec2(swatch_size, swatch_size));
674 const ImVec2 swatch_min = ImGui::GetItemRectMin();
675 const ImVec2 swatch_max = ImGui::GetItemRectMax();
676 const bool double_clicked =
677 ImGui::IsItemHovered() &&
678 ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left);
679 if (clicked || double_clicked) {
685 if (AcceptImGuiColorDrop(&palette[i], swatch_min, swatch_max)) {
696 if (ImGui::BeginPopupContextItem()) {
697 ImGui::Text(tr(
"Color %d (0x%04X)"), i, color.snes());
699 if (ImGui::MenuItem(tr(
"Edit Color"))) {
705 if (ImGui::MenuItem(tr(
"Reset to Black"))) {
715 if (ImGui::IsItemHovered()) {
716 ImGui::SetTooltip(tr(
"Color %d\nSNES: 0x%04X\nRGB: (%d, %d, %d)"), i,
717 color.snes(),
static_cast<int>(display_color.x * 255),
718 static_cast<int>(display_color.y * 255),
719 static_cast<int>(display_color.z * 255));
727 std::string popup_id =
730 ImGui::OpenPopup(popup_id.c_str());
731 if (ImGui::BeginPopupModal(popup_id.c_str(),
nullptr,
732 ImGuiWindowFlags_AlwaysAutoResize)) {
734 if (ImGui::ColorEdit4(
736 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_DisplayRGB)) {
749 ImGui::CloseCurrentPopup();
754 ImGui::CloseCurrentPopup();
767 ImGui::Text(tr(
"No ROM palettes available"));
771 ImGui::Text(tr(
"Palette Group:"));
776 ImGui::Text(tr(
"Palette Index:"));
780 ImGui::Text(tr(
"Preview:"));
782 for (
int i = 0; i < 8 && i < static_cast<int>(preview_palette.size());
786 auto color = preview_palette[i];
787 ImVec4 display_color = color.rgb();
788 ImGui::ColorButton((
"##preview" + std::to_string(i)).c_str(),
789 display_color, ImGuiColorEditFlags_NoTooltip,
797 ImVec4 rgba = color.
rgb();
799 ImGui::PushID(color_index);
801 if (ImGui::ColorEdit4(
802 "##color_edit", &rgba.x,
803 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_DisplayRGB)) {
807 ImGui::Text(tr(
"SNES Color: 0x%04X"), color.
snes());
809 int r = (color.
snes() & 0x1F);
810 int g = (color.
snes() >> 5) & 0x1F;
811 int b = (color.
snes() >> 10) & 0x1F;
813 if (ImGui::SliderInt(tr(
"Red"), &r, 0, 31)) {
814 uint16_t new_color = (color.
snes() & 0xFFE0) | (r & 0x1F);
817 if (ImGui::SliderInt(tr(
"Green"), &g, 0, 31)) {
818 uint16_t new_color = (color.
snes() & 0xFC1F) | ((g & 0x1F) << 5);
821 if (ImGui::SliderInt(tr(
"Blue"), &b, 0, 31)) {
822 uint16_t new_color = (color.
snes() & 0x83FF) | ((b & 0x1F) << 10);
830 ImGui::Text(tr(
"Palette Information:"));
831 ImGui::Text(tr(
"Size: %zu colors"), palette.
size());
833 std::map<uint16_t, int> color_frequency;
834 for (
int i = 0; i < static_cast<int>(palette.
size()); i++) {
835 color_frequency[palette[i].snes()]++;
838 ImGui::Text(tr(
"Unique Colors: %zu"), color_frequency.size());
840 if (color_frequency.size() < palette.
size()) {
841 ImGui::TextColored(ImVec4(1, 1, 0, 1),
842 tr(
"Warning: Duplicate colors detected!"));
843 if (ImGui::TreeNode(
"Duplicate Colors")) {
844 for (
const auto& [
snes_color, count] : color_frequency) {
847 ImGui::ColorButton((
"##dup" + std::to_string(
snes_color)).c_str(),
848 display_color, ImGuiColorEditFlags_NoTooltip,
851 ImGui::Text(tr(
"0x%04X appears %d times"),
snes_color, count);
863 .x_label =
"Color Index",
865 .flags = ImPlotFlags_NoBoxSelect,
866 .x_axis_flags = ImPlotAxisFlags_AutoFit,
867 .y_axis_flags = ImPlotAxisFlags_AutoFit};
868 std::vector<double> x;
869 std::vector<double> y;
870 x.reserve(color_frequency.size());
871 y.reserve(color_frequency.size());
872 for (
const auto& [
snes_color, count] : color_frequency) {
874 y.push_back(
static_cast<double>(count));
877 if (plot && !x.empty()) {
878 ImPlot::PlotBars(
"Count", x.data(), y.data(),
static_cast<int>(x.size()),
879 0.5, 0.0, ImPlotBarsFlags_None);
883 float total_brightness = 0.0f;
884 float min_brightness = 1.0f;
885 float max_brightness = 0.0f;
887 for (
int i = 0; i < static_cast<int>(palette.
size()); i++) {
888 ImVec4 color = palette[i].rgb();
889 float brightness = (color.x + color.y + color.z) / 3.0f;
890 total_brightness += brightness;
891 min_brightness = std::min(min_brightness, brightness);
892 max_brightness = std::max(max_brightness, brightness);
895 float avg_brightness = total_brightness / palette.
size();
898 ImGui::Text(tr(
"Brightness Analysis:"));
899 ImGui::Text(tr(
"Average: %.2f"), avg_brightness);
900 ImGui::Text(tr(
"Range: %.2f - %.2f"), min_brightness, max_brightness);
902 ImGui::Text(tr(
"Brightness Distribution:"));
903 ImGui::ProgressBar(avg_brightness, ImVec2(-1, 0),
"Avg");
915 if (palette_groups.overworld_main.size() > 0) {
919 if (palette_groups.overworld_aux.size() > 0) {
923 if (palette_groups.overworld_animated.size() > 0) {
927 if (palette_groups.dungeon_main.size() > 0) {
931 if (palette_groups.global_sprites.size() > 0) {
935 if (palette_groups.armors.size() > 0) {
939 if (palette_groups.swords.size() > 0) {
945 }
catch (
const std::exception& e) {
946 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 ImVec4 rgb() const
Get RGB values (WARNING: stored as 0-255 in ImVec4)
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
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.
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