yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
canvas_utils.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GUI_CANVAS_UTILS_H
2#define YAZE_APP_GUI_CANVAS_UTILS_H
3
4#include <string>
5#include <vector>
6
11#include "imgui/imgui.h"
12#include "rom/rom.h"
13#include "zelda3/game_data.h"
14
15namespace yaze {
16namespace gui {
17
25 // Display settings
26 bool enable_grid = true;
27 bool enable_hex_labels = false;
30 bool show_builtin_context_menu = true; // Show built-in canvas debug items
31 bool is_draggable = false;
32 bool auto_resize = false;
34 true; // Prevent rectangle wrap across 512x512 boundaries
36 true; // Use theme-aware sizing instead of fixed sizes
37 bool enable_metrics = false; // Enable performance/usage tracking
38
39 // Sizing and scale
40 float grid_step = 32.0f;
41 float global_scale = 1.0f;
42 ImVec2 canvas_size = ImVec2(0, 0);
43 ImVec2 content_size = ImVec2(0, 0); // Size of actual content (bitmap, etc.)
44 ImVec2 scrolling = ImVec2(0, 0);
45 bool custom_canvas_size = false;
46
47 // Usage tracking
49
50 // What this canvas is for. Sibling of CanvasMode (which describes the
51 // current input behavior: paint vs. select). Editors set this once at
52 // Init time; Canvas widgets read it but do not gate behavior on it.
54
55 // Callbacks for configuration changes (used by modals)
56 std::function<void(const CanvasConfig&)> on_config_changed;
57 std::function<void(const CanvasConfig&)> on_scale_changed;
58
59 // Get theme-aware canvas toolbar height (when use_theme_sizing is true)
60 float GetToolbarHeight() const;
61 // Get theme-aware grid spacing (when use_theme_sizing is true)
62 float GetGridSpacing() const;
63};
64
69 std::vector<ImVec2> selected_tiles;
70 std::vector<ImVec2> selected_points;
71 ImVec2 selected_tile_pos = ImVec2(-1, -1);
72 bool select_rect_active = false;
73
74 void Clear() {
75 selected_tiles.clear();
76 selected_points.clear();
77 selected_tile_pos = ImVec2(-1, -1);
78 select_rect_active = false;
79 }
80};
81
86 std::vector<gfx::SnesPalette> rom_palette_groups;
87 std::vector<std::string> palette_group_names;
89 bool palettes_loaded = false;
92
93 // Live update control
94 bool live_update_enabled = true; // Enable/disable live texture updates
95 bool palette_dirty = false; // Track if palette has changed
96
97 void Clear() {
98 rom_palette_groups.clear();
99 palette_group_names.clear();
101 palettes_loaded = false;
104 live_update_enabled = true;
105 palette_dirty = false;
106 }
107};
108
113 std::string label;
114 std::string shortcut;
115 std::function<void()> callback;
116 std::function<bool()> enabled_condition = []() {
117 return true;
118 };
119 std::vector<CanvasContextMenuItem> subitems;
120};
121
125namespace CanvasUtils {
126
127// Core utility functions
128ImVec2 AlignToGrid(ImVec2 pos, float grid_step);
129float CalculateEffectiveScale(ImVec2 canvas_size, ImVec2 content_size,
130 float global_scale);
131int GetTileIdFromPosition(ImVec2 mouse_pos, float tile_size, float scale,
132 int tiles_per_row);
133
134// Palette management utilities
136 CanvasPaletteManager& palette_manager);
137bool ApplyPaletteGroup(gfx::IRenderer* renderer, gfx::Bitmap* bitmap,
138 CanvasPaletteManager& palette_manager, int group_index,
139 int palette_index);
140
145 gfx::Bitmap* bitmap,
146 CanvasPaletteManager& palette_manager) {
147 if (palette_manager.palette_dirty && bitmap && renderer) {
150 palette_manager.palette_dirty = false;
151 }
152}
153
154// Drawing utility functions (moved from Canvas class)
155void DrawCanvasRect(ImDrawList* draw_list, ImVec2 canvas_p0, ImVec2 scrolling,
156 int x, int y, int w, int h, ImVec4 color,
157 float global_scale);
158void DrawCanvasText(ImDrawList* draw_list, ImVec2 canvas_p0, ImVec2 scrolling,
159 const std::string& text, int x, int y, float global_scale);
160void DrawCanvasOutline(ImDrawList* draw_list, ImVec2 canvas_p0,
161 ImVec2 scrolling, int x, int y, int w, int h,
162 uint32_t color = IM_COL32(255, 255, 255, 200));
163void DrawCanvasOutlineWithColor(ImDrawList* draw_list, ImVec2 canvas_p0,
164 ImVec2 scrolling, int x, int y, int w, int h,
165 ImVec4 color);
166
167// Grid utility functions
168void DrawCanvasGridLines(ImDrawList* draw_list, ImVec2 canvas_p0,
169 ImVec2 canvas_p1, ImVec2 scrolling, float grid_step,
170 float global_scale);
171void DrawCustomHighlight(ImDrawList* draw_list, ImVec2 canvas_p0,
172 ImVec2 scrolling, int highlight_tile_id,
173 float grid_step);
174void DrawHexTileLabels(ImDrawList* draw_list, ImVec2 canvas_p0,
175 ImVec2 scrolling, ImVec2 canvas_sz, float grid_step,
176 float global_scale);
177
178// Layout and interaction utilities
179ImVec2 CalculateCanvasSize(ImVec2 content_region, ImVec2 custom_size,
180 bool use_custom);
181ImVec2 CalculateScaledCanvasSize(ImVec2 canvas_size, float global_scale);
182bool IsPointInCanvas(ImVec2 point, ImVec2 canvas_p0, ImVec2 canvas_p1);
183
184// Size reporting for ImGui table integration
185ImVec2 CalculateMinimumCanvasSize(ImVec2 content_size, float global_scale,
186 float padding = 4.0f);
187ImVec2 CalculatePreferredCanvasSize(ImVec2 content_size, float global_scale,
188 float min_scale = 1.0f);
189void ReserveCanvasSpace(ImVec2 canvas_size, const std::string& label = "");
190void SetNextCanvasSize(ImVec2 size, bool auto_resize = false);
191
192// High-level canvas operations
203
204// Composite drawing operations
205void DrawCanvasGrid(const CanvasRenderContext& ctx, int highlight_tile_id = -1);
207 const ImVector<ImVec2>& points,
208 const ImVector<ImVec2>& selected_points);
210 const ImVector<ImVector<std::string>>& labels,
211 int current_labels, int tile_id_offset);
212
213} // namespace CanvasUtils
214
215} // namespace gui
216} // namespace yaze
217
218#endif // YAZE_APP_GUI_CANVAS_UTILS_H
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Definition arena.cc:36
static Arena & Get()
Definition arena.cc:21
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:67
Defines an abstract interface for all rendering operations.
Definition irenderer.h:60
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
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)
void ApplyPendingPaletteUpdates(gfx::IRenderer *renderer, gfx::Bitmap *bitmap, CanvasPaletteManager &palette_manager)
Apply pending palette updates (when live_update is disabled)
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)
CanvasUsage
Canvas usage patterns and tracking.
Unified configuration for canvas display and interaction.
float GetGridSpacing() const
std::function< void(const CanvasConfig &) on_config_changed)
std::function< void(const CanvasConfig &) on_scale_changed)
float GetToolbarHeight() const
Context menu item configuration.
std::function< void()> callback
std::function< bool()> enabled_condition
std::vector< CanvasContextMenuItem > subitems
Palette management state for canvas.
std::vector< gfx::SnesPalette > rom_palette_groups
gfx::SnesPalette original_palette
std::vector< std::string > palette_group_names
Selection state for canvas interactions.
std::vector< ImVec2 > selected_tiles
std::vector< ImVec2 > selected_points