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 enable_zoom_to_fit = true;
32 bool enable_center_view = true;
33 bool is_draggable = false;
34 bool auto_resize = false;
36 true; // Prevent rectangle wrap across 512x512 boundaries
38 true; // Use theme-aware sizing instead of fixed sizes
39 bool enable_metrics = false; // Enable performance/usage tracking
40
41 // Sizing and scale
42 float grid_step = 32.0f;
43 float global_scale = 1.0f;
44 ImVec2 canvas_size = ImVec2(0, 0);
45 ImVec2 content_size = ImVec2(0, 0); // Size of actual content (bitmap, etc.)
46 ImVec2 scrolling = ImVec2(0, 0);
47 bool custom_canvas_size = false;
48
49 // Usage tracking
51
52 // What this canvas is for. Sibling of CanvasMode (which describes the
53 // current input behavior: paint vs. select). Editors set this once at
54 // Init time; Canvas widgets read it but do not gate behavior on it.
56
57 // Callbacks for configuration changes (used by modals)
58 std::function<void(const CanvasConfig&)> on_config_changed;
59 std::function<void(const CanvasConfig&)> on_scale_changed;
60
61 // Get theme-aware canvas toolbar height (when use_theme_sizing is true)
62 float GetToolbarHeight() const;
63 // Get theme-aware grid spacing (when use_theme_sizing is true)
64 float GetGridSpacing() const;
65};
66
71 std::vector<ImVec2> selected_tiles;
72 std::vector<ImVec2> selected_points;
73 ImVec2 selected_tile_pos = ImVec2(-1, -1);
74 bool select_rect_active = false;
75
76 void Clear() {
77 selected_tiles.clear();
78 selected_points.clear();
79 selected_tile_pos = ImVec2(-1, -1);
80 select_rect_active = false;
81 }
82};
83
88 std::vector<gfx::SnesPalette> rom_palette_groups;
89 std::vector<std::string> palette_group_names;
91 bool palettes_loaded = false;
94
95 // Live update control
96 bool live_update_enabled = true; // Enable/disable live texture updates
97 bool palette_dirty = false; // Track if palette has changed
98
99 void Clear() {
100 rom_palette_groups.clear();
101 palette_group_names.clear();
103 palettes_loaded = false;
106 live_update_enabled = true;
107 palette_dirty = false;
108 }
109};
110
115 std::string label;
116 std::string shortcut;
117 std::function<void()> callback;
118 std::function<bool()> enabled_condition = []() {
119 return true;
120 };
121 std::vector<CanvasContextMenuItem> subitems;
122};
123
127namespace CanvasUtils {
128
129// Core utility functions
130ImVec2 AlignToGrid(ImVec2 pos, float grid_step);
131float CalculateEffectiveScale(ImVec2 canvas_size, ImVec2 content_size,
132 float global_scale);
133int GetTileIdFromPosition(ImVec2 mouse_pos, float tile_size, float scale,
134 int tiles_per_row);
135
136// Palette management utilities
138 CanvasPaletteManager& palette_manager);
139bool ApplyPaletteGroup(gfx::IRenderer* renderer, gfx::Bitmap* bitmap,
140 CanvasPaletteManager& palette_manager, int group_index,
141 int palette_index);
142
147 gfx::Bitmap* bitmap,
148 CanvasPaletteManager& palette_manager) {
149 if (palette_manager.palette_dirty && bitmap && renderer) {
152 palette_manager.palette_dirty = false;
153 }
154}
155
156// Drawing utility functions (moved from Canvas class)
157void DrawCanvasRect(ImDrawList* draw_list, ImVec2 canvas_p0, ImVec2 scrolling,
158 int x, int y, int w, int h, ImVec4 color,
159 float global_scale);
160void DrawCanvasText(ImDrawList* draw_list, ImVec2 canvas_p0, ImVec2 scrolling,
161 const std::string& text, int x, int y, float global_scale);
162void DrawCanvasOutline(ImDrawList* draw_list, ImVec2 canvas_p0,
163 ImVec2 scrolling, int x, int y, int w, int h,
164 uint32_t color = IM_COL32(255, 255, 255, 200),
165 float global_scale = 1.0f);
166void DrawCanvasOutlineWithColor(ImDrawList* draw_list, ImVec2 canvas_p0,
167 ImVec2 scrolling, int x, int y, int w, int h,
168 ImVec4 color, float global_scale = 1.0f);
169
170// Grid utility functions
171void DrawCanvasGridLines(ImDrawList* draw_list, ImVec2 canvas_p0,
172 ImVec2 canvas_p1, ImVec2 scrolling, float grid_step,
173 float global_scale);
174void DrawCustomHighlight(ImDrawList* draw_list, ImVec2 canvas_p0,
175 ImVec2 scrolling, int highlight_tile_id,
176 float grid_step);
177void DrawHexTileLabels(ImDrawList* draw_list, ImVec2 canvas_p0,
178 ImVec2 scrolling, ImVec2 canvas_sz, float grid_step,
179 float global_scale);
180
181// Layout and interaction utilities
182ImVec2 CalculateCanvasSize(ImVec2 content_region, ImVec2 custom_size,
183 bool use_custom);
184ImVec2 CalculateScaledCanvasSize(ImVec2 canvas_size, float global_scale);
185bool IsPointInCanvas(ImVec2 point, ImVec2 canvas_p0, ImVec2 canvas_p1);
186
187// Size reporting for ImGui table integration
188ImVec2 CalculateMinimumCanvasSize(ImVec2 content_size, float global_scale,
189 float padding = 4.0f);
190ImVec2 CalculatePreferredCanvasSize(ImVec2 content_size, float global_scale,
191 float min_scale = 1.0f);
192void ReserveCanvasSpace(ImVec2 canvas_size, const std::string& label = "");
193void SetNextCanvasSize(ImVec2 size, bool auto_resize = false);
194
195// High-level canvas operations
206
207// Composite drawing operations
208void DrawCanvasGrid(const CanvasRenderContext& ctx, int highlight_tile_id = -1);
210 const ImVector<ImVec2>& points,
211 const ImVector<ImVec2>& selected_points);
213 const ImVector<ImVector<std::string>>& labels,
214 int current_labels, int tile_id_offset);
215
216} // namespace CanvasUtils
217
218} // namespace gui
219} // namespace yaze
220
221#endif // YAZE_APP_GUI_CANVAS_UTILS_H
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Definition arena.cc:39
static Arena & Get()
Definition arena.cc:24
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:70
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)
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)
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)
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)
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