yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
canvas_utils.cc
Go to the documentation of this file.
1#include "canvas_utils.h"
2
3#include <cmath>
4#include "app/gfx/arena.h"
6#include "util/log.h"
7
8namespace yaze {
9namespace gui {
10namespace CanvasUtils {
11
12ImVec2 AlignToGrid(ImVec2 pos, float grid_step) {
13 return ImVec2(std::floor(pos.x / grid_step) * grid_step,
14 std::floor(pos.y / grid_step) * grid_step);
15}
16
17float CalculateEffectiveScale(ImVec2 canvas_size, ImVec2 content_size,
18 float global_scale) {
19 if (content_size.x <= 0 || content_size.y <= 0)
20 return global_scale;
21
22 float scale_x = (canvas_size.x * global_scale) / content_size.x;
23 float scale_y = (canvas_size.y * global_scale) / content_size.y;
24 return std::min(scale_x, scale_y);
25}
26
27int GetTileIdFromPosition(ImVec2 mouse_pos, float tile_size, float scale,
28 int tiles_per_row) {
29 float scaled_tile_size = tile_size * scale;
30 int tile_x = static_cast<int>(mouse_pos.x / scaled_tile_size);
31 int tile_y = static_cast<int>(mouse_pos.y / scaled_tile_size);
32
33 return tile_x + (tile_y * tiles_per_row);
34}
35
36bool LoadROMPaletteGroups(Rom* rom, CanvasPaletteManager& palette_manager) {
37 if (!rom || palette_manager.palettes_loaded) {
38 return palette_manager.palettes_loaded;
39 }
40
41 try {
42 const auto& palette_groups = rom->palette_group();
43 palette_manager.rom_palette_groups.clear();
44 palette_manager.palette_group_names.clear();
45
46 // Overworld palettes
47 if (palette_groups.overworld_main.size() > 0) {
48 palette_manager.rom_palette_groups.push_back(
49 palette_groups.overworld_main[0]);
50 palette_manager.palette_group_names.push_back("Overworld Main");
51 }
52 if (palette_groups.overworld_aux.size() > 0) {
53 palette_manager.rom_palette_groups.push_back(
54 palette_groups.overworld_aux[0]);
55 palette_manager.palette_group_names.push_back("Overworld Aux");
56 }
57 if (palette_groups.overworld_animated.size() > 0) {
58 palette_manager.rom_palette_groups.push_back(
59 palette_groups.overworld_animated[0]);
60 palette_manager.palette_group_names.push_back("Overworld Animated");
61 }
62
63 // Dungeon palettes
64 if (palette_groups.dungeon_main.size() > 0) {
65 palette_manager.rom_palette_groups.push_back(
66 palette_groups.dungeon_main[0]);
67 palette_manager.palette_group_names.push_back("Dungeon Main");
68 }
69
70 // Sprite palettes
71 if (palette_groups.global_sprites.size() > 0) {
72 palette_manager.rom_palette_groups.push_back(
73 palette_groups.global_sprites[0]);
74 palette_manager.palette_group_names.push_back("Global Sprites");
75 }
76 if (palette_groups.armors.size() > 0) {
77 palette_manager.rom_palette_groups.push_back(palette_groups.armors[0]);
78 palette_manager.palette_group_names.push_back("Armor");
79 }
80 if (palette_groups.swords.size() > 0) {
81 palette_manager.rom_palette_groups.push_back(palette_groups.swords[0]);
82 palette_manager.palette_group_names.push_back("Swords");
83 }
84
85 palette_manager.palettes_loaded = true;
86 LOG_DEBUG("Canvas", "Loaded %zu ROM palette groups",
87 palette_manager.rom_palette_groups.size());
88 return true;
89
90 } catch (const std::exception& e) {
91 LOG_ERROR("Canvas", "Failed to load ROM palette groups");
92 return false;
93 }
94}
95
96bool ApplyPaletteGroup(gfx::IRenderer* renderer, gfx::Bitmap* bitmap, const CanvasPaletteManager& palette_manager,
97 int group_index, int palette_index) {
98 if (!bitmap) return false;
99
100 if (group_index < 0 || group_index >= palette_manager.rom_palette_groups.size()) {
101 return false;
102 }
103
104 const auto& palette = palette_manager.rom_palette_groups[group_index];
105
106 // Apply the full palette or use SetPaletteWithTransparent if palette_index is specified
107 if (palette_index == 0) {
108 bitmap->SetPalette(palette);
109 } else {
110 bitmap->SetPaletteWithTransparent(palette, palette_index);
111 }
112 bitmap->set_modified(true);
113
114 // Queue texture update via Arena's deferred system
115 if (renderer) {
118 }
119 return true;
120}
121
122// Drawing utility functions
123void DrawCanvasRect(ImDrawList* draw_list, ImVec2 canvas_p0, ImVec2 scrolling,
124 int x, int y, int w, int h, ImVec4 color,
125 float global_scale) {
126 // Apply global scale to position and size
127 float scaled_x = x * global_scale;
128 float scaled_y = y * global_scale;
129 float scaled_w = w * global_scale;
130 float scaled_h = h * global_scale;
131
132 ImVec2 origin(canvas_p0.x + scrolling.x + scaled_x,
133 canvas_p0.y + scrolling.y + scaled_y);
134 ImVec2 size(canvas_p0.x + scrolling.x + scaled_x + scaled_w,
135 canvas_p0.y + scrolling.y + scaled_y + scaled_h);
136
137 uint32_t color_u32 = IM_COL32(color.x * 255, color.y * 255, color.z * 255, color.w * 255);
138 draw_list->AddRectFilled(origin, size, color_u32);
139
140 // Add a black outline
141 ImVec2 outline_origin(origin.x - 1, origin.y - 1);
142 ImVec2 outline_size(size.x + 1, size.y + 1);
143 draw_list->AddRect(outline_origin, outline_size, IM_COL32(0, 0, 0, 255));
144}
145
146void DrawCanvasText(ImDrawList* draw_list, ImVec2 canvas_p0, ImVec2 scrolling,
147 const std::string& text, int x, int y, float global_scale) {
148 // Apply global scale to text position
149 float scaled_x = x * global_scale;
150 float scaled_y = y * global_scale;
151
152 ImVec2 text_pos(canvas_p0.x + scrolling.x + scaled_x,
153 canvas_p0.y + scrolling.y + scaled_y);
154
155 // Draw text with black shadow for better visibility
156 draw_list->AddText(ImVec2(text_pos.x + 1, text_pos.y + 1),
157 IM_COL32(0, 0, 0, 255), text.c_str());
158 draw_list->AddText(text_pos, IM_COL32(255, 255, 255, 255), text.c_str());
159}
160
161void DrawCanvasOutline(ImDrawList* draw_list, ImVec2 canvas_p0,
162 ImVec2 scrolling, int x, int y, int w, int h,
163 uint32_t color) {
164 ImVec2 origin(canvas_p0.x + scrolling.x + x, canvas_p0.y + scrolling.y + y);
165 ImVec2 size(canvas_p0.x + scrolling.x + x + w,
166 canvas_p0.y + scrolling.y + y + h);
167 draw_list->AddRect(origin, size, color, 0, 0, 1.5f);
168}
169
170void DrawCanvasOutlineWithColor(ImDrawList* draw_list, ImVec2 canvas_p0,
171 ImVec2 scrolling, int x, int y, int w, int h,
172 ImVec4 color) {
173 uint32_t color_u32 =
174 IM_COL32(color.x * 255, color.y * 255, color.z * 255, color.w * 255);
175 DrawCanvasOutline(draw_list, canvas_p0, scrolling, x, y, w, h, color_u32);
176}
177
178// Grid utility functions
179void DrawCanvasGridLines(ImDrawList* draw_list, ImVec2 canvas_p0,
180 ImVec2 canvas_p1, ImVec2 scrolling, float grid_step,
181 float global_scale) {
182 const uint32_t grid_color = IM_COL32(200, 200, 200, 50);
183 const float grid_thickness = 0.5f;
184
185 float scaled_grid_step = grid_step * global_scale;
186
187 for (float x = fmodf(scrolling.x, scaled_grid_step);
188 x < (canvas_p1.x - canvas_p0.x); x += scaled_grid_step) {
189 draw_list->AddLine(ImVec2(canvas_p0.x + x, canvas_p0.y),
190 ImVec2(canvas_p0.x + x, canvas_p1.y), grid_color,
191 grid_thickness);
192 }
193
194 for (float y = fmodf(scrolling.y, scaled_grid_step);
195 y < (canvas_p1.y - canvas_p0.y); y += scaled_grid_step) {
196 draw_list->AddLine(ImVec2(canvas_p0.x, canvas_p0.y + y),
197 ImVec2(canvas_p1.x, canvas_p0.y + y), grid_color,
198 grid_thickness);
199 }
200}
201
202void DrawCustomHighlight(ImDrawList* draw_list, ImVec2 canvas_p0,
203 ImVec2 scrolling, int highlight_tile_id,
204 float grid_step) {
205 if (highlight_tile_id == -1)
206 return;
207
208 int tile_x = highlight_tile_id % 8;
209 int tile_y = highlight_tile_id / 8;
210 ImVec2 tile_pos(canvas_p0.x + scrolling.x + tile_x * grid_step,
211 canvas_p0.y + scrolling.y + tile_y * grid_step);
212 ImVec2 tile_pos_end(tile_pos.x + grid_step, tile_pos.y + grid_step);
213
214 draw_list->AddRectFilled(tile_pos, tile_pos_end, IM_COL32(255, 0, 255, 255));
215}
216
217void DrawHexTileLabels(ImDrawList* draw_list, ImVec2 canvas_p0,
218 ImVec2 scrolling, ImVec2 canvas_sz, float grid_step,
219 float global_scale) {
220 float scaled_grid_step = grid_step * global_scale;
221
222 for (float x = fmodf(scrolling.x, scaled_grid_step);
223 x < canvas_sz.x * global_scale; x += scaled_grid_step) {
224 for (float y = fmodf(scrolling.y, scaled_grid_step);
225 y < canvas_sz.y * global_scale; y += scaled_grid_step) {
226 int tile_x = (x - scrolling.x) / scaled_grid_step;
227 int tile_y = (y - scrolling.y) / scaled_grid_step;
228 int tile_id = tile_x + (tile_y * 16);
229
230 char hex_id[8];
231 snprintf(hex_id, sizeof(hex_id), "%02X", tile_id);
232
233 draw_list->AddText(ImVec2(canvas_p0.x + x + (scaled_grid_step / 2) - 4,
234 canvas_p0.y + y + (scaled_grid_step / 2) - 4),
235 IM_COL32(255, 255, 255, 255), hex_id);
236 }
237 }
238}
239
240// Layout and interaction utilities
241ImVec2 CalculateCanvasSize(ImVec2 content_region, ImVec2 custom_size,
242 bool use_custom) {
243 return use_custom ? custom_size : content_region;
244}
245
246ImVec2 CalculateScaledCanvasSize(ImVec2 canvas_size, float global_scale) {
247 return ImVec2(canvas_size.x * global_scale, canvas_size.y * global_scale);
248}
249
250bool IsPointInCanvas(ImVec2 point, ImVec2 canvas_p0, ImVec2 canvas_p1) {
251 return point.x >= canvas_p0.x && point.x <= canvas_p1.x &&
252 point.y >= canvas_p0.y && point.y <= canvas_p1.y;
253}
254
255// Size reporting for ImGui table integration
256ImVec2 CalculateMinimumCanvasSize(ImVec2 content_size, float global_scale,
257 float padding) {
258 // Calculate minimum size needed to display content with padding
259 ImVec2 min_size = ImVec2(content_size.x * global_scale + padding * 2,
260 content_size.y * global_scale + padding * 2);
261
262 // Ensure minimum practical size
263 min_size.x = std::max(min_size.x, 64.0f);
264 min_size.y = std::max(min_size.y, 64.0f);
265
266 return min_size;
267}
268
269ImVec2 CalculatePreferredCanvasSize(ImVec2 content_size, float global_scale,
270 float min_scale) {
271 // Calculate preferred size with minimum scale constraint
272 float effective_scale = std::max(global_scale, min_scale);
273 ImVec2 preferred_size = ImVec2(content_size.x * effective_scale + 8.0f,
274 content_size.y * effective_scale + 8.0f);
275
276 // Cap to reasonable maximum sizes for table integration
277 preferred_size.x = std::min(preferred_size.x, 800.0f);
278 preferred_size.y = std::min(preferred_size.y, 600.0f);
279
280 return preferred_size;
281}
282
283void ReserveCanvasSpace(ImVec2 canvas_size, const std::string& label) {
284 // Reserve space in ImGui layout so tables know the size
285 if (!label.empty()) {
286 ImGui::Text("%s", label.c_str());
287 }
288 ImGui::Dummy(canvas_size);
289 ImGui::SameLine();
290 ImGui::SetCursorPosX(ImGui::GetCursorPosX() -
291 canvas_size.x); // Move back to start
292}
293
294void SetNextCanvasSize(ImVec2 size, bool auto_resize) {
295 if (auto_resize) {
296 // Use auto-sizing child window for table integration
297 ImGui::SetNextWindowContentSize(size);
298 } else {
299 // Fixed size
300 ImGui::SetNextWindowSize(size);
301 }
302}
303
304// High-level composite operations
305void DrawCanvasGrid(const CanvasRenderContext& ctx, int highlight_tile_id) {
306 if (!ctx.enable_grid)
307 return;
308
309 ctx.draw_list->PushClipRect(ctx.canvas_p0, ctx.canvas_p1, true);
310
311 // Draw grid lines
313 ctx.scrolling, ctx.grid_step, ctx.global_scale);
314
315 // Draw highlight if specified
316 if (highlight_tile_id != -1) {
318 highlight_tile_id, ctx.grid_step * ctx.global_scale);
319 }
320
321 // Draw hex labels if enabled
322 if (ctx.enable_hex_labels) {
324 ImVec2(ctx.canvas_p1.x - ctx.canvas_p0.x,
325 ctx.canvas_p1.y - ctx.canvas_p0.y),
326 ctx.grid_step, ctx.global_scale);
327 }
328
329 ctx.draw_list->PopClipRect();
330}
331
333 const ImVector<ImVec2>& points,
334 const ImVector<ImVec2>& selected_points) {
335 const ImVec2 origin(ctx.canvas_p0.x + ctx.scrolling.x,
336 ctx.canvas_p0.y + ctx.scrolling.y);
337
338 // Draw hover points
339 for (int n = 0; n < points.Size; n += 2) {
340 ctx.draw_list->AddRect(
341 ImVec2(origin.x + points[n].x, origin.y + points[n].y),
342 ImVec2(origin.x + points[n + 1].x, origin.y + points[n + 1].y),
343 IM_COL32(255, 255, 255, 255), 1.0f);
344 }
345
346 // Draw selection rectangles
347 if (!selected_points.empty()) {
348 for (int n = 0; n < selected_points.size(); n += 2) {
349 ctx.draw_list->AddRect(ImVec2(origin.x + selected_points[n].x,
350 origin.y + selected_points[n].y),
351 ImVec2(origin.x + selected_points[n + 1].x + 0x10,
352 origin.y + selected_points[n + 1].y + 0x10),
353 IM_COL32(255, 255, 255, 255), 1.0f);
354 }
355 }
356}
357
359 const ImVector<ImVector<std::string>>& labels,
360 int current_labels, int tile_id_offset) {
361 if (current_labels >= labels.size())
362 return;
363
364 float scaled_grid_step = ctx.grid_step * ctx.global_scale;
365
366 for (float x = fmodf(ctx.scrolling.x, scaled_grid_step);
367 x < (ctx.canvas_p1.x - ctx.canvas_p0.x); x += scaled_grid_step) {
368 for (float y = fmodf(ctx.scrolling.y, scaled_grid_step);
369 y < (ctx.canvas_p1.y - ctx.canvas_p0.y); y += scaled_grid_step) {
370 int tile_x = (x - ctx.scrolling.x) / scaled_grid_step;
371 int tile_y = (y - ctx.scrolling.y) / scaled_grid_step;
372 int tile_id = tile_x + (tile_y * tile_id_offset);
373
374 if (tile_id >= labels[current_labels].size()) {
375 break;
376 }
377
378 const std::string& label = labels[current_labels][tile_id];
379 ctx.draw_list->AddText(
380 ImVec2(ctx.canvas_p0.x + x + (scaled_grid_step / 2) - tile_id_offset,
381 ctx.canvas_p0.y + y + (scaled_grid_step / 2) - tile_id_offset),
382 IM_COL32(255, 255, 255, 255), label.c_str());
383 }
384 }
385}
386
387} // namespace CanvasUtils
388} // namespace gui
389} // namespace yaze
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:71
auto palette_group() const
Definition rom.h:213
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Definition arena.cc:32
static Arena & Get()
Definition arena.cc:15
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:66
void set_modified(bool modified)
Definition bitmap.h:267
void SetPalette(const SnesPalette &palette)
Set the palette for the bitmap.
Definition bitmap.cc:292
void SetPaletteWithTransparent(const SnesPalette &palette, size_t index, int length=7)
Set the palette with a transparent color.
Definition bitmap.cc:303
Defines an abstract interface for all rendering operations.
Definition irenderer.h:35
#define LOG_DEBUG(category, format,...)
Definition log.h:104
#define LOG_ERROR(category, format,...)
Definition log.h:110
void ReserveCanvasSpace(ImVec2 canvas_size, const std::string &label)
void SetNextCanvasSize(ImVec2 size, bool auto_resize)
void DrawCanvasRect(ImDrawList *draw_list, ImVec2 canvas_p0, ImVec2 scrolling, int x, int y, int w, int h, ImVec4 color, float global_scale)
void DrawCanvasLabels(const CanvasRenderContext &ctx, const ImVector< ImVector< std::string > > &labels, int current_labels, int tile_id_offset)
bool IsPointInCanvas(ImVec2 point, ImVec2 canvas_p0, ImVec2 canvas_p1)
void DrawCanvasOverlay(const CanvasRenderContext &ctx, const ImVector< ImVec2 > &points, const ImVector< ImVec2 > &selected_points)
ImVec2 CalculateCanvasSize(ImVec2 content_region, ImVec2 custom_size, bool use_custom)
bool LoadROMPaletteGroups(Rom *rom, CanvasPaletteManager &palette_manager)
ImVec2 CalculateMinimumCanvasSize(ImVec2 content_size, float global_scale, float padding)
bool ApplyPaletteGroup(gfx::IRenderer *renderer, gfx::Bitmap *bitmap, const CanvasPaletteManager &palette_manager, int group_index, int palette_index)
float CalculateEffectiveScale(ImVec2 canvas_size, ImVec2 content_size, float global_scale)
void DrawCanvasOutline(ImDrawList *draw_list, ImVec2 canvas_p0, ImVec2 scrolling, int x, int y, int w, int h, uint32_t color)
int GetTileIdFromPosition(ImVec2 mouse_pos, float tile_size, float scale, int tiles_per_row)
void DrawCanvasOutlineWithColor(ImDrawList *draw_list, ImVec2 canvas_p0, ImVec2 scrolling, int x, int y, int w, int h, ImVec4 color)
void DrawCanvasGrid(const CanvasRenderContext &ctx, int highlight_tile_id)
ImVec2 CalculatePreferredCanvasSize(ImVec2 content_size, float global_scale, float min_scale)
void DrawCanvasText(ImDrawList *draw_list, ImVec2 canvas_p0, ImVec2 scrolling, const std::string &text, int x, int y, float global_scale)
ImVec2 CalculateScaledCanvasSize(ImVec2 canvas_size, float global_scale)
void DrawCustomHighlight(ImDrawList *draw_list, ImVec2 canvas_p0, ImVec2 scrolling, int highlight_tile_id, float grid_step)
void DrawCanvasGridLines(ImDrawList *draw_list, ImVec2 canvas_p0, ImVec2 canvas_p1, ImVec2 scrolling, float grid_step, float global_scale)
ImVec2 AlignToGrid(ImVec2 pos, float grid_step)
void DrawHexTileLabels(ImDrawList *draw_list, ImVec2 canvas_p0, ImVec2 scrolling, ImVec2 canvas_sz, float grid_step, float global_scale)
Main namespace for the application.
Palette management state for canvas.
std::vector< gfx::SnesPalette > rom_palette_groups
std::vector< std::string > palette_group_names