11namespace CanvasUtils {
14 return ImVec2(std::floor(pos.x / grid_step) * grid_step,
15 std::floor(pos.y / grid_step) * grid_step);
20 if (content_size.x <= 0 || content_size.y <= 0)
23 float scale_x = (canvas_size.x * global_scale) / content_size.x;
24 float scale_y = (canvas_size.y * global_scale) / content_size.y;
25 return std::min(scale_x, scale_y);
30 float scaled_tile_size = tile_size * scale;
31 int tile_x =
static_cast<int>(mouse_pos.x / scaled_tile_size);
32 int tile_y =
static_cast<int>(mouse_pos.y / scaled_tile_size);
34 return tile_x + (tile_y * tiles_per_row);
48 if (palette_groups.overworld_main.size() > 0) {
50 palette_groups.overworld_main[0]);
53 if (palette_groups.overworld_aux.size() > 0) {
55 palette_groups.overworld_aux[0]);
58 if (palette_groups.overworld_animated.size() > 0) {
60 palette_groups.overworld_animated[0]);
65 if (palette_groups.dungeon_main.size() > 0) {
67 palette_groups.dungeon_main[0]);
72 if (palette_groups.global_sprites.size() > 0) {
74 palette_groups.global_sprites[0]);
77 if (palette_groups.armors.size() > 0) {
81 if (palette_groups.swords.size() > 0) {
87 LOG_DEBUG(
"Canvas",
"Loaded %zu ROM palette groups",
91 }
catch (
const std::exception& e) {
92 LOG_ERROR(
"Canvas",
"Failed to load ROM palette groups");
103 if (group_index < 0 ||
112 if (palette_index == 0) {
131 int x,
int y,
int w,
int h, ImVec4 color,
132 float global_scale) {
134 float scaled_x = x * global_scale;
135 float scaled_y = y * global_scale;
136 float scaled_w = w * global_scale;
137 float scaled_h = h * global_scale;
139 ImVec2 origin(canvas_p0.x + scrolling.x + scaled_x,
140 canvas_p0.y + scrolling.y + scaled_y);
141 ImVec2 size(canvas_p0.x + scrolling.x + scaled_x + scaled_w,
142 canvas_p0.y + scrolling.y + scaled_y + scaled_h);
145 IM_COL32(color.x * 255, color.y * 255, color.z * 255, color.w * 255);
146 draw_list->AddRectFilled(origin, size, color_u32);
149 ImVec2 outline_origin(origin.x - 1, origin.y - 1);
150 ImVec2 outline_size(size.x + 1, size.y + 1);
151 draw_list->AddRect(outline_origin, outline_size, IM_COL32(0, 0, 0, 255));
155 const std::string& text,
int x,
int y,
float global_scale) {
157 float scaled_x = x * global_scale;
158 float scaled_y = y * global_scale;
160 ImVec2 text_pos(canvas_p0.x + scrolling.x + scaled_x,
161 canvas_p0.y + scrolling.y + scaled_y);
164 draw_list->AddText(ImVec2(text_pos.x + 1, text_pos.y + 1),
165 IM_COL32(0, 0, 0, 255), text.c_str());
166 draw_list->AddText(text_pos, IM_COL32(255, 255, 255, 255), text.c_str());
170 ImVec2 scrolling,
int x,
int y,
int w,
int h,
172 ImVec2 origin(canvas_p0.x + scrolling.x + x, canvas_p0.y + scrolling.y + y);
173 ImVec2 size(canvas_p0.x + scrolling.x + x + w,
174 canvas_p0.y + scrolling.y + y + h);
175 draw_list->AddRect(origin, size, color, 0, 0, 1.5f);
179 ImVec2 scrolling,
int x,
int y,
int w,
int h,
182 IM_COL32(color.x * 255, color.y * 255, color.z * 255, color.w * 255);
188 ImVec2 canvas_p1, ImVec2 scrolling,
float grid_step,
189 float global_scale) {
190 const uint32_t grid_color = IM_COL32(200, 200, 200, 50);
191 const float grid_thickness = 0.5f;
193 float scaled_grid_step = grid_step * global_scale;
195 for (
float x = fmodf(scrolling.x, scaled_grid_step);
196 x < (canvas_p1.x - canvas_p0.x); x += scaled_grid_step) {
197 draw_list->AddLine(ImVec2(canvas_p0.x + x, canvas_p0.y),
198 ImVec2(canvas_p0.x + x, canvas_p1.y), grid_color,
202 for (
float y = fmodf(scrolling.y, scaled_grid_step);
203 y < (canvas_p1.y - canvas_p0.y); y += scaled_grid_step) {
204 draw_list->AddLine(ImVec2(canvas_p0.x, canvas_p0.y + y),
205 ImVec2(canvas_p1.x, canvas_p0.y + y), grid_color,
211 ImVec2 scrolling,
int highlight_tile_id,
213 if (highlight_tile_id == -1)
216 int tile_x = highlight_tile_id % 8;
217 int tile_y = highlight_tile_id / 8;
218 ImVec2 tile_pos(canvas_p0.x + scrolling.x + tile_x * grid_step,
219 canvas_p0.y + scrolling.y + tile_y * grid_step);
220 ImVec2 tile_pos_end(tile_pos.x + grid_step, tile_pos.y + grid_step);
222 draw_list->AddRectFilled(tile_pos, tile_pos_end, IM_COL32(255, 0, 255, 255));
226 ImVec2 scrolling, ImVec2 canvas_sz,
float grid_step,
227 float global_scale) {
228 float scaled_grid_step = grid_step * global_scale;
230 for (
float x = fmodf(scrolling.x, scaled_grid_step);
231 x < canvas_sz.x * global_scale; x += scaled_grid_step) {
232 for (
float y = fmodf(scrolling.y, scaled_grid_step);
233 y < canvas_sz.y * global_scale; y += scaled_grid_step) {
234 int tile_x = (x - scrolling.x) / scaled_grid_step;
235 int tile_y = (y - scrolling.y) / scaled_grid_step;
236 int tile_id = tile_x + (tile_y * 16);
239 snprintf(hex_id,
sizeof(hex_id),
"%02X", tile_id);
241 draw_list->AddText(ImVec2(canvas_p0.x + x + (scaled_grid_step / 2) - 4,
242 canvas_p0.y + y + (scaled_grid_step / 2) - 4),
243 IM_COL32(255, 255, 255, 255), hex_id);
251 return use_custom ? custom_size : content_region;
255 return ImVec2(canvas_size.x * global_scale, canvas_size.y * global_scale);
259 return point.x >= canvas_p0.x && point.x <= canvas_p1.x &&
260 point.y >= canvas_p0.y && point.y <= canvas_p1.y;
267 ImVec2 min_size = ImVec2(content_size.x * global_scale + padding * 2,
268 content_size.y * global_scale + padding * 2);
271 min_size.x = std::max(min_size.x, 64.0f);
272 min_size.y = std::max(min_size.y, 64.0f);
280 float effective_scale = std::max(global_scale, min_scale);
281 ImVec2 preferred_size = ImVec2(content_size.x * effective_scale + 8.0f,
282 content_size.y * effective_scale + 8.0f);
285 preferred_size.x = std::min(preferred_size.x, 800.0f);
286 preferred_size.y = std::min(preferred_size.y, 600.0f);
288 return preferred_size;
293 if (!label.empty()) {
294 ImGui::Text(
"%s", label.c_str());
296 ImGui::Dummy(canvas_size);
298 ImGui::SetCursorPosX(ImGui::GetCursorPosX() -
305 ImGui::SetNextWindowContentSize(size);
308 ImGui::SetNextWindowSize(size);
324 if (highlight_tile_id != -1) {
341 const ImVector<ImVec2>& points,
342 const ImVector<ImVec2>& selected_points) {
348 for (
int n = 0; n < points.Size; n += 2) {
350 ImVec2(origin.x + points[n].x, origin.y + points[n].y),
351 ImVec2(origin.x + points[n + 1].x, origin.y + points[n + 1].y),
352 IM_COL32(255, 255, 255, 255), 1.0f);
357 if (!selected_points.empty()) {
358 constexpr float kTile16Size = 16.0f;
359 const float scaled_tile_offset = kTile16Size * scale;
360 for (
int n = 0; n < selected_points.size(); n += 2) {
362 float start_x = origin.x + selected_points[n].x * scale;
363 float start_y = origin.y + selected_points[n].y * scale;
364 float end_x = origin.x + (selected_points[n + 1].x + kTile16Size) * scale;
365 float end_y = origin.y + (selected_points[n + 1].y + kTile16Size) * scale;
366 ctx.
draw_list->AddRect(ImVec2(start_x, start_y), ImVec2(end_x, end_y),
367 IM_COL32(255, 255, 255, 255), 1.0f);
373 const ImVector<ImVector<std::string>>& labels,
374 int current_labels,
int tile_id_offset) {
375 if (current_labels >= labels.size())
380 for (
float x = fmodf(ctx.
scrolling.x, scaled_grid_step);
382 for (
float y = fmodf(ctx.
scrolling.y, scaled_grid_step);
384 int tile_x = (x - ctx.
scrolling.x) / scaled_grid_step;
385 int tile_y = (y - ctx.
scrolling.y) / scaled_grid_step;
386 int tile_id = tile_x + (tile_y * tile_id_offset);
388 if (tile_id >= labels[current_labels].size()) {
392 const std::string& label = labels[current_labels][tile_id];
394 ImVec2(ctx.
canvas_p0.x + x + (scaled_grid_step / 2) - tile_id_offset,
395 ctx.
canvas_p0.y + y + (scaled_grid_step / 2) - tile_id_offset),
396 IM_COL32(255, 255, 255, 255), label.c_str());
412 return ImGui::GetFontSize() *
423 float base_spacing = ImGui::GetFontSize() * 0.5f;
424 return std::max(
grid_step, base_spacing);
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Represents a bitmap image optimized for SNES ROM hacking.
void set_modified(bool modified)
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.
Defines an abstract interface for all rendering operations.
#define LOG_DEBUG(category, format,...)
#define LOG_ERROR(category, format,...)
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)
bool LoadROMPaletteGroups(zelda3::GameData *game_data, CanvasPaletteManager &palette_manager)
ImVec2 CalculateCanvasSize(ImVec2 content_region, ImVec2 custom_size, bool use_custom)
ImVec2 CalculateMinimumCanvasSize(ImVec2 content_size, float global_scale, float padding)
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)
bool ApplyPaletteGroup(gfx::IRenderer *renderer, gfx::Bitmap *bitmap, CanvasPaletteManager &palette_manager, int group_index, int palette_index)
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)
float GetGridSpacing() const
float GetToolbarHeight() const
Palette management state for canvas.
std::vector< gfx::SnesPalette > rom_palette_groups
std::vector< std::string > palette_group_names
gfx::PaletteGroupMap palette_groups