12namespace CanvasUtils {
15 return ImVec2(std::floor(pos.x / grid_step) * grid_step,
16 std::floor(pos.y / grid_step) * grid_step);
21 if (content_size.x <= 0 || content_size.y <= 0)
24 float scale_x = (canvas_size.x * global_scale) / content_size.x;
25 float scale_y = (canvas_size.y * global_scale) / content_size.y;
26 return std::min(scale_x, scale_y);
31 float scaled_tile_size = tile_size * scale;
32 int tile_x =
static_cast<int>(mouse_pos.x / scaled_tile_size);
33 int tile_y =
static_cast<int>(mouse_pos.y / scaled_tile_size);
35 return tile_x + (tile_y * tiles_per_row);
50 if (palette_groups.overworld_main.size() > 0) {
52 palette_groups.overworld_main[0]);
55 if (palette_groups.overworld_aux.size() > 0) {
57 palette_groups.overworld_aux[0]);
60 if (palette_groups.overworld_animated.size() > 0) {
62 palette_groups.overworld_animated[0]);
67 if (palette_groups.dungeon_main.size() > 0) {
69 palette_groups.dungeon_main[0]);
74 if (palette_groups.global_sprites.size() > 0) {
76 palette_groups.global_sprites[0]);
79 if (palette_groups.armors.size() > 0) {
83 if (palette_groups.swords.size() > 0) {
89 LOG_DEBUG(
"Canvas",
"Loaded %zu ROM palette groups",
93 }
catch (
const std::exception& e) {
94 LOG_ERROR(
"Canvas",
"Failed to load ROM palette groups");
105 if (group_index < 0 ||
114 if (palette_index == 0) {
133 int x,
int y,
int w,
int h, ImVec4 color,
134 float global_scale) {
136 float scaled_x = x * global_scale;
137 float scaled_y = y * global_scale;
138 float scaled_w = w * global_scale;
139 float scaled_h = h * global_scale;
141 ImVec2 origin(canvas_p0.x + scrolling.x + scaled_x,
142 canvas_p0.y + scrolling.y + scaled_y);
143 ImVec2 size(canvas_p0.x + scrolling.x + scaled_x + scaled_w,
144 canvas_p0.y + scrolling.y + scaled_y + scaled_h);
147 IM_COL32(color.x * 255, color.y * 255, color.z * 255, color.w * 255);
148 draw_list->AddRectFilled(origin, size, color_u32);
151 ImVec2 outline_origin(origin.x - 1, origin.y - 1);
152 ImVec2 outline_size(size.x + 1, size.y + 1);
153 draw_list->AddRect(outline_origin, outline_size, IM_COL32(0, 0, 0, 255));
157 const std::string& text,
int x,
int y,
float global_scale) {
159 float scaled_x = x * global_scale;
160 float scaled_y = y * global_scale;
162 ImVec2 text_pos(canvas_p0.x + scrolling.x + scaled_x,
163 canvas_p0.y + scrolling.y + scaled_y);
167 constexpr ImU32 kOutline = IM_COL32(0, 0, 0, 255);
168 for (
const ImVec2 offset : {ImVec2(-1, 0), ImVec2(1, 0), ImVec2(0, -1),
169 ImVec2(0, 1), ImVec2(1, 1)}) {
170 draw_list->AddText(ImVec2(text_pos.x + offset.x, text_pos.y + offset.y),
171 kOutline, text.c_str());
173 draw_list->AddText(text_pos, IM_COL32(255, 255, 255, 255), text.c_str());
177 ImVec2 scrolling,
int x,
int y,
int w,
int h,
178 uint32_t color,
float global_scale) {
179 const float scale = global_scale > 0.0f ? global_scale : 1.0f;
180 const float scaled_x =
static_cast<float>(x) * scale;
181 const float scaled_y =
static_cast<float>(y) * scale;
182 const float scaled_w =
static_cast<float>(w) * scale;
183 const float scaled_h =
static_cast<float>(h) * scale;
184 ImVec2 origin(canvas_p0.x + scrolling.x + scaled_x,
185 canvas_p0.y + scrolling.y + scaled_y);
186 ImVec2 size(origin.x + scaled_w, origin.y + scaled_h);
187 draw_list->AddRect(origin, size, color, 0.0f, 1.5f);
191 ImVec2 scrolling,
int x,
int y,
int w,
int h,
192 ImVec4 color,
float global_scale) {
194 IM_COL32(color.x * 255, color.y * 255, color.z * 255, color.w * 255);
201 ImVec2 canvas_p1, ImVec2 scrolling,
float grid_step,
202 float global_scale) {
203 const uint32_t grid_color = IM_COL32(200, 200, 200, 50);
204 const float grid_thickness = 0.5f;
206 float scaled_grid_step = grid_step * global_scale;
208 for (
float x = fmodf(scrolling.x, scaled_grid_step);
209 x < (canvas_p1.x - canvas_p0.x); x += scaled_grid_step) {
210 draw_list->AddLineV(canvas_p0.x + x, canvas_p0.y, canvas_p1.y, grid_color,
214 for (
float y = fmodf(scrolling.y, scaled_grid_step);
215 y < (canvas_p1.y - canvas_p0.y); y += scaled_grid_step) {
216 draw_list->AddLineH(canvas_p0.x, canvas_p1.x, canvas_p0.y + y, grid_color,
222 ImVec2 scrolling,
int highlight_tile_id,
224 if (highlight_tile_id == -1)
227 int tile_x = highlight_tile_id % 8;
228 int tile_y = highlight_tile_id / 8;
229 ImVec2 tile_pos(canvas_p0.x + scrolling.x + tile_x * grid_step,
230 canvas_p0.y + scrolling.y + tile_y * grid_step);
231 ImVec2 tile_pos_end(tile_pos.x + grid_step, tile_pos.y + grid_step);
233 draw_list->AddRectFilled(tile_pos, tile_pos_end, IM_COL32(255, 0, 255, 255));
237 ImVec2 scrolling, ImVec2 canvas_sz,
float grid_step,
238 float global_scale) {
239 float scaled_grid_step = grid_step * global_scale;
241 for (
float x = fmodf(scrolling.x, scaled_grid_step);
242 x < canvas_sz.x * global_scale; x += scaled_grid_step) {
243 for (
float y = fmodf(scrolling.y, scaled_grid_step);
244 y < canvas_sz.y * global_scale; y += scaled_grid_step) {
245 int tile_x = (x - scrolling.x) / scaled_grid_step;
246 int tile_y = (y - scrolling.y) / scaled_grid_step;
247 int tile_id = tile_x + (tile_y * 16);
250 snprintf(hex_id,
sizeof(hex_id),
"%02X", tile_id);
252 draw_list->AddText(ImVec2(canvas_p0.x + x + (scaled_grid_step / 2) - 4,
253 canvas_p0.y + y + (scaled_grid_step / 2) - 4),
254 IM_COL32(255, 255, 255, 255), hex_id);
262 return use_custom ? custom_size : content_region;
266 return ImVec2(canvas_size.x * global_scale, canvas_size.y * global_scale);
270 return point.x >= canvas_p0.x && point.x <= canvas_p1.x &&
271 point.y >= canvas_p0.y && point.y <= canvas_p1.y;
278 ImVec2 min_size = ImVec2(content_size.x * global_scale + padding * 2,
279 content_size.y * global_scale + padding * 2);
282 min_size.x = std::max(min_size.x, 64.0f);
283 min_size.y = std::max(min_size.y, 64.0f);
291 float effective_scale = std::max(global_scale, min_scale);
292 ImVec2 preferred_size = ImVec2(content_size.x * effective_scale + 8.0f,
293 content_size.y * effective_scale + 8.0f);
296 preferred_size.x = std::min(preferred_size.x, 800.0f);
297 preferred_size.y = std::min(preferred_size.y, 600.0f);
299 return preferred_size;
304 if (!label.empty()) {
305 ImGui::Text(
"%s", label.c_str());
307 ImGui::Dummy(canvas_size);
309 ImGui::SetCursorPosX(ImGui::GetCursorPosX() -
316 ImGui::SetNextWindowContentSize(size);
319 ImGui::SetNextWindowSize(size);
335 if (highlight_tile_id != -1) {
352 const ImVector<ImVec2>& points,
353 const ImVector<ImVec2>& selected_points) {
359 for (
int n = 0; n < points.Size; n += 2) {
361 ImVec2(origin.x + points[n].x, origin.y + points[n].y),
362 ImVec2(origin.x + points[n + 1].x, origin.y + points[n + 1].y),
363 IM_COL32(255, 255, 255, 255), 0.0f, 1.0f);
368 if (!selected_points.empty()) {
369 constexpr float kTile16Size = 16.0f;
370 for (
int n = 0; n < selected_points.size(); n += 2) {
374 std::min(selected_points[n].x, selected_points[n + 1].x),
375 std::min(selected_points[n].y, selected_points[n + 1].y));
377 std::max(selected_points[n].x, selected_points[n + 1].x),
378 std::max(selected_points[n].y, selected_points[n + 1].y));
379 float start_x = origin.x + min.x * scale;
380 float start_y = origin.y + min.y * scale;
381 float end_x = origin.x + (max.x + kTile16Size) * scale;
382 float end_y = origin.y + (max.y + kTile16Size) * scale;
383 ctx.
draw_list->AddRect(ImVec2(start_x, start_y), ImVec2(end_x, end_y),
384 IM_COL32(255, 255, 255, 255), 0.0f, 1.0f);
390 const ImVector<ImVector<std::string>>& labels,
391 int current_labels,
int tile_id_offset) {
392 if (current_labels >= labels.size())
397 for (
float x = fmodf(ctx.
scrolling.x, scaled_grid_step);
399 for (
float y = fmodf(ctx.
scrolling.y, scaled_grid_step);
401 int tile_x = (x - ctx.
scrolling.x) / scaled_grid_step;
402 int tile_y = (y - ctx.
scrolling.y) / scaled_grid_step;
403 int tile_id = tile_x + (tile_y * tile_id_offset);
405 if (tile_id >= labels[current_labels].size()) {
409 const std::string& label = labels[current_labels][tile_id];
411 ImVec2(ctx.
canvas_p0.x + x + (scaled_grid_step / 2) - tile_id_offset,
412 ctx.
canvas_p0.y + y + (scaled_grid_step / 2) - tile_id_offset),
413 IM_COL32(255, 255, 255, 255), label.c_str());
429 return ImGui::GetFontSize() *
440 float base_spacing = ImGui::GetFontSize() * 0.5f;
441 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)
void DrawCanvasOutlineWithColor(ImDrawList *draw_list, ImVec2 canvas_p0, ImVec2 scrolling, int x, int y, int w, int h, ImVec4 color, float global_scale)
void DrawCanvasOutline(ImDrawList *draw_list, ImVec2 canvas_p0, ImVec2 scrolling, int x, int y, int w, int h, uint32_t color, float global_scale)
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)
int GetTileIdFromPosition(ImVec2 mouse_pos, float tile_size, float scale, int tiles_per_row)
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