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 "absl/strings/str_format.h"
4#include "imgui/imgui.h"
5
6namespace yaze {
7namespace gui {
8
14
16 if (!rom_ || !rom_->is_loaded()) {
17 ImGui::TextColored(ImVec4(1, 0, 0, 1), "ROM not loaded");
18 return;
19 }
20
21 ImGui::BeginGroup();
22
23 // Palette selector dropdown
25
26 ImGui::Separator();
27
28 // Color grid display
30
31 ImGui::Separator();
32
33 // Color picker for selected color
34 if (selected_color_index_ >= 0) {
36 } else {
37 ImGui::TextDisabled("Select a color to edit");
38 }
39
40 ImGui::EndGroup();
41}
42
44 auto& dungeon_pal_group = rom_->mutable_palette_group()->dungeon_main;
45 int num_palettes = dungeon_pal_group.size();
46
47 ImGui::Text("Dungeon Palette:");
48 ImGui::SameLine();
49
50 if (ImGui::BeginCombo("##PaletteSelect",
51 absl::StrFormat("Palette %d", current_palette_id_).c_str())) {
52 for (int i = 0; i < num_palettes; i++) {
53 bool is_selected = (current_palette_id_ == i);
54 if (ImGui::Selectable(absl::StrFormat("Palette %d", i).c_str(), is_selected)) {
56 selected_color_index_ = -1; // Reset color selection
57 }
58 if (is_selected) {
59 ImGui::SetItemDefaultFocus();
60 }
61 }
62 ImGui::EndCombo();
63 }
64}
65
67 auto& dungeon_pal_group = rom_->mutable_palette_group()->dungeon_main;
68
69 if (current_palette_id_ < 0 || current_palette_id_ >= (int)dungeon_pal_group.size()) {
70 ImGui::TextColored(ImVec4(1, 0, 0, 1), "Invalid palette ID");
71 return;
72 }
73
74 auto palette = dungeon_pal_group[current_palette_id_];
75 int num_colors = palette.size();
76
77 ImGui::Text("Colors (%d):", num_colors);
78
79 // Draw color grid (15 colors per row for good layout)
80 const int colors_per_row = 15;
81 const float color_button_size = 24.0f;
82
83 for (int i = 0; i < num_colors; i++) {
84 ImGui::PushID(i);
85
86 // Get color as RGB (0-255)
87 auto color = palette[i];
88 ImVec4 col(color.rgb().x / 255.0f,
89 color.rgb().y / 255.0f,
90 color.rgb().z / 255.0f,
91 1.0f);
92
93 // Color button
94 bool is_selected = (i == selected_color_index_);
95 if (is_selected) {
96 ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(1, 1, 0, 1)); // Yellow border
97 ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 2.0f);
98 }
99
100 if (ImGui::ColorButton(absl::StrFormat("##color%d", i).c_str(), col,
101 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoPicker,
102 ImVec2(color_button_size, color_button_size))) {
104 editing_color_ = col;
105 }
106
107 if (is_selected) {
108 ImGui::PopStyleVar();
109 ImGui::PopStyleColor();
110 }
111
112 // Tooltip showing color index and SNES value
113 if (ImGui::IsItemHovered()) {
114 ImGui::SetTooltip("Color %d\nSNES: 0x%04X\nRGB: (%d, %d, %d)",
115 i, color.snes(),
116 (int)color.rgb().x, (int)color.rgb().y, (int)color.rgb().z);
117 }
118
119 // Layout: 15 per row
120 if ((i + 1) % colors_per_row != 0 && i < num_colors - 1) {
121 ImGui::SameLine();
122 }
123
124 ImGui::PopID();
125 }
126}
127
129 ImGui::SeparatorText(absl::StrFormat("Edit Color %d", selected_color_index_).c_str());
130
131 auto& dungeon_pal_group = rom_->mutable_palette_group()->dungeon_main;
132 auto palette = dungeon_pal_group[current_palette_id_]; // Get copy, not reference
133 auto original_color = palette[selected_color_index_];
134
135 // Color picker
136 if (ImGui::ColorEdit3("Color", &editing_color_.x,
137 ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_PickerHueWheel)) {
138 // Convert ImGui color (0-1) to SNES color (0-31 per channel)
139 int r = static_cast<int>(editing_color_.x * 31.0f);
140 int g = static_cast<int>(editing_color_.y * 31.0f);
141 int b = static_cast<int>(editing_color_.z * 31.0f);
142
143 // Create SNES color (15-bit BGR555 format)
144 uint16_t snes_color = (b << 10) | (g << 5) | r;
145
146 // Update palette in ROM (need to write back through the group)
148 dungeon_pal_group[current_palette_id_] = palette; // Write back
149
150 // Notify that palette changed
153 }
154 }
155
156 // Show RGB values
157 ImGui::Text("RGB (0-255): (%d, %d, %d)",
158 (int)(editing_color_.x * 255),
159 (int)(editing_color_.y * 255),
160 (int)(editing_color_.z * 255));
161
162 // Show SNES BGR555 value
163 ImGui::Text("SNES BGR555: 0x%04X", original_color.snes());
164
165 // Reset button
166 if (ImGui::Button("Reset to Original")) {
167 editing_color_ = ImVec4(original_color.rgb().x / 255.0f,
168 original_color.rgb().y / 255.0f,
169 original_color.rgb().z / 255.0f,
170 1.0f);
171 }
172}
173
174} // namespace gui
175} // namespace yaze
176
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:71
auto mutable_palette_group()
Definition rom.h:214
bool is_loaded() const
Definition rom.h:197
SNES Color container.
Definition snes_color.h:38
std::function< void(int palette_id)> on_palette_changed_
Main namespace for the application.
SNES color in 15-bit RGB format (BGR555)
Definition yaze.h:208