yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
palette_editor_widget.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <map>
5
6#include "absl/strings/str_format.h"
12#include "util/log.h"
13
14namespace yaze {
15namespace gui {
16
17// Merged implementation from PaletteWidget and PaletteEditorWidget
18
20 game_data_ = game_data;
21 rom_ = nullptr;
23 if (game_data_) {
25 }
28}
29
31 rom_ = rom;
32 game_data_ = nullptr;
34 // Legacy mode - no palette loading without game_data
37}
38
39// --- Embedded Draw Method (from simple editor) ---
41 if (!game_data_) {
42 ImGui::TextColored(ImVec4(1, 0, 0, 1), "GameData not loaded");
43 return;
44 }
45
46 ImGui::BeginGroup();
47
49 ImGui::Separator();
50
51 auto& dungeon_pal_group = game_data_->palette_groups.dungeon_main;
52 if (current_palette_id_ >= 0 &&
53 current_palette_id_ < (int)dungeon_pal_group.size()) {
54 auto palette = dungeon_pal_group[current_palette_id_];
55 DrawPaletteGrid(palette, 15);
56 dungeon_pal_group[current_palette_id_] = palette;
57 }
58
59 ImGui::Separator();
60
61 if (selected_color_index_ >= 0) {
63 } else {
64 ImGui::TextDisabled("Select a color to edit");
65 }
66
67 ImGui::EndGroup();
68}
69
71 if (!game_data_)
72 return;
73 auto& dungeon_pal_group = game_data_->palette_groups.dungeon_main;
74 int num_palettes = dungeon_pal_group.size();
75
76 ImGui::Text("Dungeon Palette:");
77 ImGui::SameLine();
78
79 if (ImGui::BeginCombo(
80 "##PaletteSelect",
81 absl::StrFormat("Palette %d", current_palette_id_).c_str())) {
82 for (int i = 0; i < num_palettes; i++) {
83 bool is_selected = (current_palette_id_ == i);
84 if (ImGui::Selectable(absl::StrFormat("Palette %d", i).c_str(),
85 is_selected)) {
88 }
89 if (is_selected) {
90 ImGui::SetItemDefaultFocus();
91 }
92 }
93 ImGui::EndCombo();
94 }
95}
96
98 if (!game_data_)
99 return;
100 ImGui::SeparatorText(
101 absl::StrFormat("Edit Color %d", selected_color_index_).c_str());
102
103 auto& dungeon_pal_group = game_data_->palette_groups.dungeon_main;
104 auto palette = dungeon_pal_group[current_palette_id_];
105 auto original_color = palette[selected_color_index_];
106
107 // Use standardized SnesColorEdit4 for consistency
109 "Color", &palette[selected_color_index_],
110 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_PickerHueWheel)) {
111 // Update the palette group with the modified palette
112 dungeon_pal_group[current_palette_id_] = palette;
113
114 // Update editing_color_ to match (for consistency with other parts of the
115 // widget)
118
121 }
122 }
123
124 ImGui::Text("RGB (0-255): (%d, %d, %d)",
125 static_cast<int>(editing_color_.x * 255),
126 static_cast<int>(editing_color_.y * 255),
127 static_cast<int>(editing_color_.z * 255));
128 ImGui::Text("SNES BGR555: 0x%04X", original_color.snes());
129
130 if (ImGui::Button("Reset to Original")) {
132 ImVec4(original_color.rgb().x / 255.0f, original_color.rgb().y / 255.0f,
133 original_color.rgb().z / 255.0f, 1.0f);
134 // Also reset the actual palette color
135 palette[selected_color_index_] = original_color;
136 dungeon_pal_group[current_palette_id_] = palette;
139 }
140 }
141}
142
143// --- Modal/Popup Methods (from feature-rich widget) ---
144
146 const std::string& title) {
147 if (ImGui::BeginPopupModal(title.c_str(), nullptr,
148 ImGuiWindowFlags_AlwaysAutoResize)) {
149 ImGui::Text("Enhanced Palette Editor");
150 ImGui::Separator();
151
152 DrawPaletteGrid(palette);
153 ImGui::Separator();
154
155 if (ImGui::CollapsingHeader("Palette Analysis")) {
156 DrawPaletteAnalysis(palette);
157 }
158
159 if (ImGui::CollapsingHeader("ROM Palette Manager") && rom_) {
161
162 if (ImGui::Button("Apply ROM Palette") && !rom_palette_groups_.empty()) {
164 static_cast<int>(rom_palette_groups_.size())) {
166 }
167 }
168 }
169
170 ImGui::Separator();
171
172 if (ImGui::Button("Save Backup")) {
173 SavePaletteBackup(palette);
174 }
175 ImGui::SameLine();
176 if (ImGui::Button("Restore Backup")) {
177 RestorePaletteBackup(palette);
178 }
179 ImGui::SameLine();
180 if (ImGui::Button("Close")) {
181 ImGui::CloseCurrentPopup();
182 }
183
184 ImGui::EndPopup();
185 }
186}
187
190 return;
191
192 if (ImGui::Begin("ROM Palette Manager", &show_rom_manager_)) {
193 if (!rom_) {
194 ImGui::Text("No ROM loaded");
195 ImGui::End();
196 return;
197 }
198
201 }
202
204
205 if (current_group_index_ < static_cast<int>(rom_palette_groups_.size())) {
206 ImGui::Separator();
207 ImGui::Text("Preview of %s:",
209
210 const auto& preview_palette = rom_palette_groups_[current_group_index_];
211 DrawPaletteGrid(const_cast<gfx::SnesPalette&>(preview_palette));
212 DrawPaletteAnalysis(preview_palette);
213 }
214 }
215 ImGui::End();
216}
217
219 const std::string& title) {
221 return;
222
223 if (ImGui::Begin(title.c_str(), &show_color_analysis_)) {
224 ImGui::Text("Bitmap Color Analysis");
225 ImGui::Separator();
226
227 if (!bitmap.is_active()) {
228 ImGui::Text("Bitmap is not active");
229 ImGui::End();
230 return;
231 }
232
233 std::map<uint8_t, int> pixel_counts;
234 const auto& data = bitmap.vector();
235
236 for (uint8_t pixel : data) {
237 uint8_t palette_index = pixel & 0x0F;
238 pixel_counts[palette_index]++;
239 }
240
241 ImGui::Text("Bitmap Size: %d x %d (%zu pixels)", bitmap.width(),
242 bitmap.height(), data.size());
243
244 ImGui::Separator();
245 ImGui::Text("Pixel Distribution:");
246
247 int total_pixels = static_cast<int>(data.size());
248 plotting::PlotStyleScope plot_style(
249 gui::ThemeManager::Get().GetCurrentTheme());
250 plotting::PlotConfig plot_cfg{.id = "Pixel Distribution",
251 .x_label = "Palette Index",
252 .y_label = "Count",
253 .flags = ImPlotFlags_NoBoxSelect,
254 .x_axis_flags = ImPlotAxisFlags_AutoFit,
255 .y_axis_flags = ImPlotAxisFlags_AutoFit};
256 std::vector<double> x;
257 std::vector<double> y;
258 x.reserve(pixel_counts.size());
259 y.reserve(pixel_counts.size());
260 for (const auto& [index, count] : pixel_counts) {
261 x.push_back(static_cast<double>(index));
262 y.push_back(static_cast<double>(count));
263 }
264 plotting::PlotGuard plot(plot_cfg);
265 if (plot && !x.empty()) {
266 ImPlot::PlotBars("Usage", x.data(), y.data(), static_cast<int>(x.size()),
267 0.67, 0.0, ImPlotBarsFlags_None);
268 }
269 }
270 ImGui::End();
271}
272
274 int palette_index) {
275 if (!bitmap || !rom_palettes_loaded_ || group_index < 0 ||
276 group_index >= static_cast<int>(rom_palette_groups_.size())) {
277 return false;
278 }
279
280 try {
281 const auto& selected_palette = rom_palette_groups_[group_index];
282 SavePaletteBackup(bitmap->palette());
283
284 if (palette_index >= 0 && palette_index < 8) {
285 bitmap->SetPaletteWithTransparent(selected_palette, palette_index);
286 } else {
287 bitmap->SetPalette(selected_palette);
288 }
289
292
293 current_group_index_ = group_index;
294 current_palette_index_ = palette_index;
295 return true;
296 } catch (const std::exception& e) {
297 return false;
298 }
299}
300
303 current_group_index_ >= static_cast<int>(rom_palette_groups_.size())) {
304 return nullptr;
305 }
307}
308
312
314 if (backup_palette_.size() == 0) {
315 return false;
316 }
317 palette = backup_palette_;
318 return true;
319}
320
321// Unified grid drawing function
323 for (int i = 0; i < static_cast<int>(palette.size()); i++) {
324 if (i % cols != 0)
325 ImGui::SameLine();
326
327 auto color = palette[i];
328 ImVec4 display_color = color.rgb();
329
330 ImGui::PushID(i);
331 if (ImGui::ColorButton("##color", display_color,
332 ImGuiColorEditFlags_NoTooltip, ImVec2(30, 30))) {
335 temp_color_ = display_color;
336 editing_color_ = display_color;
337 }
338
339 if (ImGui::BeginPopupContextItem()) {
340 ImGui::Text("Color %d (0x%04X)", i, color.snes());
341 ImGui::Separator();
342 if (ImGui::MenuItem("Edit Color")) {
345 temp_color_ = display_color;
346 editing_color_ = display_color;
347 }
348 if (ImGui::MenuItem("Reset to Black")) {
349 palette[i] = gfx::SnesColor(0);
350 }
351 ImGui::EndPopup();
352 }
353
354 if (ImGui::IsItemHovered()) {
355 ImGui::SetTooltip("Color %d\nSNES: 0x%04X\nRGB: (%d, %d, %d)", i,
356 color.snes(), static_cast<int>(display_color.x * 255),
357 static_cast<int>(display_color.y * 255),
358 static_cast<int>(display_color.z * 255));
359 }
360
361 ImGui::PopID();
362 }
363
364 if (editing_color_index_ >= 0) {
365 // Use a unique ID for the popup to prevent conflicts
366 std::string popup_id =
367 gui::MakePopupIdWithInstance("PaletteEditorWidget", "EditColor", this);
368
369 ImGui::OpenPopup(popup_id.c_str());
370 if (ImGui::BeginPopupModal(popup_id.c_str(), nullptr,
371 ImGuiWindowFlags_AlwaysAutoResize)) {
372 ImGui::Text("Editing Color %d", editing_color_index_);
373 if (ImGui::ColorEdit4(
374 "Color", &temp_color_.x,
375 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_DisplayRGB)) {
376 auto new_snes_color = gfx::SnesColor(temp_color_);
377 palette[editing_color_index_] = new_snes_color;
378 }
379 if (ImGui::Button("Apply")) {
381 ImGui::CloseCurrentPopup();
382 }
383 ImGui::SameLine();
384 if (ImGui::Button("Cancel")) {
386 ImGui::CloseCurrentPopup();
387 }
388 ImGui::EndPopup();
389 }
390 }
391}
392
396 }
397
398 if (rom_palette_groups_.empty()) {
399 ImGui::Text("No ROM palettes available");
400 return;
401 }
402
403 ImGui::Text("Palette Group:");
404 if (ImGui::Combo(
405 "##PaletteGroup", &current_group_index_,
406 [](void* data, int idx, const char** out_text) -> bool {
407 auto* names = static_cast<std::vector<std::string>*>(data);
408 if (idx < 0 || idx >= static_cast<int>(names->size()))
409 return false;
410 *out_text = (*names)[idx].c_str();
411 return true;
412 },
414 static_cast<int>(palette_group_names_.size()))) {}
415
416 ImGui::Text("Palette Index:");
417 ImGui::SliderInt("##PaletteIndex", &current_palette_index_, 0, 7, "%d");
418
419 if (current_group_index_ < static_cast<int>(rom_palette_groups_.size())) {
420 ImGui::Text("Preview:");
421 const auto& preview_palette = rom_palette_groups_[current_group_index_];
422 for (int i = 0; i < 8 && i < static_cast<int>(preview_palette.size());
423 i++) {
424 if (i > 0)
425 ImGui::SameLine();
426 auto color = preview_palette[i];
427 ImVec4 display_color = color.rgb();
428 ImGui::ColorButton(("##preview" + std::to_string(i)).c_str(),
429 display_color, ImGuiColorEditFlags_NoTooltip,
430 ImVec2(20, 20));
431 }
432 }
433}
434
436 int color_index) {
437 ImVec4 rgba = color.rgb();
438
439 ImGui::PushID(color_index);
440
441 if (ImGui::ColorEdit4(
442 "##color_edit", &rgba.x,
443 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_DisplayRGB)) {
444 color = gfx::SnesColor(rgba);
445 }
446
447 ImGui::Text("SNES Color: 0x%04X", color.snes());
448
449 int r = (color.snes() & 0x1F);
450 int g = (color.snes() >> 5) & 0x1F;
451 int b = (color.snes() >> 10) & 0x1F;
452
453 if (ImGui::SliderInt("Red", &r, 0, 31)) {
454 uint16_t new_color = (color.snes() & 0xFFE0) | (r & 0x1F);
455 color = gfx::SnesColor(new_color);
456 }
457 if (ImGui::SliderInt("Green", &g, 0, 31)) {
458 uint16_t new_color = (color.snes() & 0xFC1F) | ((g & 0x1F) << 5);
459 color = gfx::SnesColor(new_color);
460 }
461 if (ImGui::SliderInt("Blue", &b, 0, 31)) {
462 uint16_t new_color = (color.snes() & 0x83FF) | ((b & 0x1F) << 10);
463 color = gfx::SnesColor(new_color);
464 }
465
466 ImGui::PopID();
467}
468
470 ImGui::Text("Palette Information:");
471 ImGui::Text("Size: %zu colors", palette.size());
472
473 std::map<uint16_t, int> color_frequency;
474 for (int i = 0; i < static_cast<int>(palette.size()); i++) {
475 color_frequency[palette[i].snes()]++;
476 }
477
478 ImGui::Text("Unique Colors: %zu", color_frequency.size());
479
480 if (color_frequency.size() < palette.size()) {
481 ImGui::TextColored(ImVec4(1, 1, 0, 1),
482 "Warning: Duplicate colors detected!");
483 if (ImGui::TreeNode("Duplicate Colors")) {
484 for (const auto& [snes_color, count] : color_frequency) {
485 if (count > 1) {
486 ImVec4 display_color = gfx::SnesColor(snes_color).rgb();
487 ImGui::ColorButton(("##dup" + std::to_string(snes_color)).c_str(),
488 display_color, ImGuiColorEditFlags_NoTooltip,
489 ImVec2(16, 16));
490 ImGui::SameLine();
491 ImGui::Text("0x%04X appears %d times", snes_color, count);
492 }
493 }
494 ImGui::TreePop();
495 }
496 }
497
498 // Visual histogram of color reuse
499 {
500 plotting::PlotStyleScope plot_style(
501 gui::ThemeManager::Get().GetCurrentTheme());
502 plotting::PlotConfig plot_cfg{.id = "Palette Color Frequency",
503 .x_label = "Color Index",
504 .y_label = "Count",
505 .flags = ImPlotFlags_NoBoxSelect,
506 .x_axis_flags = ImPlotAxisFlags_AutoFit,
507 .y_axis_flags = ImPlotAxisFlags_AutoFit};
508 std::vector<double> x;
509 std::vector<double> y;
510 x.reserve(color_frequency.size());
511 y.reserve(color_frequency.size());
512 for (const auto& [snes_color, count] : color_frequency) {
513 x.push_back(static_cast<double>(snes_color));
514 y.push_back(static_cast<double>(count));
515 }
516 plotting::PlotGuard plot(plot_cfg);
517 if (plot && !x.empty()) {
518 ImPlot::PlotBars("Count", x.data(), y.data(), static_cast<int>(x.size()),
519 0.5, 0.0, ImPlotBarsFlags_None);
520 }
521 }
522
523 float total_brightness = 0.0f;
524 float min_brightness = 1.0f;
525 float max_brightness = 0.0f;
526
527 for (int i = 0; i < static_cast<int>(palette.size()); i++) {
528 ImVec4 color = palette[i].rgb();
529 float brightness = (color.x + color.y + color.z) / 3.0f;
530 total_brightness += brightness;
531 min_brightness = std::min(min_brightness, brightness);
532 max_brightness = std::max(max_brightness, brightness);
533 }
534
535 float avg_brightness = total_brightness / palette.size();
536
537 ImGui::Separator();
538 ImGui::Text("Brightness Analysis:");
539 ImGui::Text("Average: %.2f", avg_brightness);
540 ImGui::Text("Range: %.2f - %.2f", min_brightness, max_brightness);
541
542 ImGui::Text("Brightness Distribution:");
543 ImGui::ProgressBar(avg_brightness, ImVec2(-1, 0), "Avg");
544}
545
548 return;
549
550 try {
551 const auto& palette_groups = game_data_->palette_groups;
553 palette_group_names_.clear();
554
555 if (palette_groups.overworld_main.size() > 0) {
556 rom_palette_groups_.push_back(palette_groups.overworld_main[0]);
557 palette_group_names_.push_back("Overworld Main");
558 }
559 if (palette_groups.overworld_aux.size() > 0) {
560 rom_palette_groups_.push_back(palette_groups.overworld_aux[0]);
561 palette_group_names_.push_back("Overworld Aux");
562 }
563 if (palette_groups.overworld_animated.size() > 0) {
564 rom_palette_groups_.push_back(palette_groups.overworld_animated[0]);
565 palette_group_names_.push_back("Overworld Animated");
566 }
567 if (palette_groups.dungeon_main.size() > 0) {
568 rom_palette_groups_.push_back(palette_groups.dungeon_main[0]);
569 palette_group_names_.push_back("Dungeon Main");
570 }
571 if (palette_groups.global_sprites.size() > 0) {
572 rom_palette_groups_.push_back(palette_groups.global_sprites[0]);
573 palette_group_names_.push_back("Global Sprites");
574 }
575 if (palette_groups.armors.size() > 0) {
576 rom_palette_groups_.push_back(palette_groups.armors[0]);
577 palette_group_names_.push_back("Armor");
578 }
579 if (palette_groups.swords.size() > 0) {
580 rom_palette_groups_.push_back(palette_groups.swords[0]);
581 palette_group_names_.push_back("Swords");
582 }
583
585 } catch (const std::exception& e) {
586 LOG_ERROR("Enhanced Palette Editor", "Failed to load ROM palettes");
587 }
588}
589
590} // namespace gui
591} // namespace yaze
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:24
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Definition arena.cc:35
static Arena & Get()
Definition arena.cc:20
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:67
const SnesPalette & palette() const
Definition bitmap.h:368
const std::vector< uint8_t > & vector() const
Definition bitmap.h:381
bool is_active() const
Definition bitmap.h:384
int height() const
Definition bitmap.h:374
void SetPalette(const SnesPalette &palette)
Set the palette for the bitmap using SNES palette format.
Definition bitmap.cc:384
int width() const
Definition bitmap.h:373
void SetPaletteWithTransparent(const SnesPalette &palette, size_t index, int length=7)
Set the palette with a transparent color.
Definition bitmap.cc:456
SNES Color container.
Definition snes_color.h:110
constexpr ImVec4 rgb() const
Get RGB values (WARNING: stored as 0-255 in ImVec4)
Definition snes_color.h:183
constexpr uint16_t snes() const
Get SNES 15-bit color.
Definition snes_color.h:193
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
std::vector< gfx::SnesPalette > rom_palette_groups_
std::function< void(int palette_id)> on_palette_changed_
bool RestorePaletteBackup(gfx::SnesPalette &palette)
void ShowPaletteEditor(gfx::SnesPalette &palette, const std::string &title="Palette Editor")
bool ApplyROMPalette(gfx::Bitmap *bitmap, int group_index, int palette_index)
const gfx::SnesPalette * GetSelectedROMPalette() const
void SavePaletteBackup(const gfx::SnesPalette &palette)
void ShowColorAnalysis(const gfx::Bitmap &bitmap, const std::string &title="Color Analysis")
void Initialize(zelda3::GameData *game_data)
void DrawColorEditControls(gfx::SnesColor &color, int color_index)
void DrawPaletteAnalysis(const gfx::SnesPalette &palette)
std::vector< std::string > palette_group_names_
void DrawPaletteGrid(gfx::SnesPalette &palette, int cols=15)
static ThemeManager & Get()
#define LOG_ERROR(category, format,...)
Definition log.h:109
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.
Definition popup_id.h:45
ImVec4 ConvertSnesColorToImVec4(const gfx::SnesColor &color)
Convert SnesColor to standard ImVec4 for display.
Definition color.cc:20
IMGUI_API bool SnesColorEdit4(absl::string_view label, gfx::SnesColor *color, ImGuiColorEditFlags flags)
Definition color.cc:55
SNES color in 15-bit RGB format (BGR555)
Definition yaze.h:218
gfx::PaletteGroupMap palette_groups
Definition game_data.h:89