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
10#include "rom/rom.h"
11#include "zelda3/game_data.h"
12#include "imgui/imgui.h"
13
14namespace yaze {
15namespace gui {
16
24 // Display settings
25 bool enable_grid = true;
26 bool enable_hex_labels = false;
29 bool show_builtin_context_menu = true; // Show built-in canvas debug items
30 bool is_draggable = false;
31 bool auto_resize = false;
33 true; // Prevent rectangle wrap across 512x512 boundaries
35 true; // Use theme-aware sizing instead of fixed sizes
36 bool enable_metrics = false; // Enable performance/usage tracking
37
38 // Sizing and scale
39 float grid_step = 32.0f;
40 float global_scale = 1.0f;
41 ImVec2 canvas_size = ImVec2(0, 0);
42 ImVec2 content_size = ImVec2(0, 0); // Size of actual content (bitmap, etc.)
43 ImVec2 scrolling = ImVec2(0, 0);
44 bool custom_canvas_size = false;
45
46 // Usage tracking
48
49 // Callbacks for configuration changes (used by modals)
50 std::function<void(const CanvasConfig&)> on_config_changed;
51 std::function<void(const CanvasConfig&)> on_scale_changed;
52
53 // Get theme-aware canvas toolbar height (when use_theme_sizing is true)
54 float GetToolbarHeight() const;
55 // Get theme-aware grid spacing (when use_theme_sizing is true)
56 float GetGridSpacing() const;
57};
58
63 std::vector<ImVec2> selected_tiles;
64 std::vector<ImVec2> selected_points;
65 ImVec2 selected_tile_pos = ImVec2(-1, -1);
66 bool select_rect_active = false;
67
68 void Clear() {
69 selected_tiles.clear();
70 selected_points.clear();
71 selected_tile_pos = ImVec2(-1, -1);
72 select_rect_active = false;
73 }
74};
75
80 std::vector<gfx::SnesPalette> rom_palette_groups;
81 std::vector<std::string> palette_group_names;
83 bool palettes_loaded = false;
86
87 // Live update control
88 bool live_update_enabled = true; // Enable/disable live texture updates
89 bool palette_dirty = false; // Track if palette has changed
90
91 void Clear() {
92 rom_palette_groups.clear();
93 palette_group_names.clear();
95 palettes_loaded = false;
99 palette_dirty = false;
100 }
101};
102
107 std::string label;
108 std::string shortcut;
109 std::function<void()> callback;
110 std::function<bool()> enabled_condition = []() {
111 return true;
112 };
113 std::vector<CanvasContextMenuItem> subitems;
114};
115
119namespace CanvasUtils {
120
121// Core utility functions
122ImVec2 AlignToGrid(ImVec2 pos, float grid_step);
123float CalculateEffectiveScale(ImVec2 canvas_size, ImVec2 content_size,
124 float global_scale);
125int GetTileIdFromPosition(ImVec2 mouse_pos, float tile_size, float scale,
126 int tiles_per_row);
127
128// Palette management utilities
129bool LoadROMPaletteGroups(zelda3::GameData* game_data, CanvasPaletteManager& palette_manager);
130bool ApplyPaletteGroup(gfx::IRenderer* renderer, gfx::Bitmap* bitmap,
131 CanvasPaletteManager& palette_manager, int group_index,
132 int palette_index);
133
138 gfx::Bitmap* bitmap,
139 CanvasPaletteManager& palette_manager) {
140 if (palette_manager.palette_dirty && bitmap && renderer) {
143 palette_manager.palette_dirty = false;
144 }
145}
146
147// Drawing utility functions (moved from Canvas class)
148void DrawCanvasRect(ImDrawList* draw_list, ImVec2 canvas_p0, ImVec2 scrolling,
149 int x, int y, int w, int h, ImVec4 color,
150 float global_scale);
151void DrawCanvasText(ImDrawList* draw_list, ImVec2 canvas_p0, ImVec2 scrolling,
152 const std::string& text, int x, int y, float global_scale);
153void DrawCanvasOutline(ImDrawList* draw_list, ImVec2 canvas_p0,
154 ImVec2 scrolling, int x, int y, int w, int h,
155 uint32_t color = IM_COL32(255, 255, 255, 200));
156void DrawCanvasOutlineWithColor(ImDrawList* draw_list, ImVec2 canvas_p0,
157 ImVec2 scrolling, int x, int y, int w, int h,
158 ImVec4 color);
159
160// Grid utility functions
161void DrawCanvasGridLines(ImDrawList* draw_list, ImVec2 canvas_p0,
162 ImVec2 canvas_p1, ImVec2 scrolling, float grid_step,
163 float global_scale);
164void DrawCustomHighlight(ImDrawList* draw_list, ImVec2 canvas_p0,
165 ImVec2 scrolling, int highlight_tile_id,
166 float grid_step);
167void DrawHexTileLabels(ImDrawList* draw_list, ImVec2 canvas_p0,
168 ImVec2 scrolling, ImVec2 canvas_sz, float grid_step,
169 float global_scale);
170
171// Layout and interaction utilities
172ImVec2 CalculateCanvasSize(ImVec2 content_region, ImVec2 custom_size,
173 bool use_custom);
174ImVec2 CalculateScaledCanvasSize(ImVec2 canvas_size, float global_scale);
175bool IsPointInCanvas(ImVec2 point, ImVec2 canvas_p0, ImVec2 canvas_p1);
176
177// Size reporting for ImGui table integration
178ImVec2 CalculateMinimumCanvasSize(ImVec2 content_size, float global_scale,
179 float padding = 4.0f);
180ImVec2 CalculatePreferredCanvasSize(ImVec2 content_size, float global_scale,
181 float min_scale = 1.0f);
182void ReserveCanvasSpace(ImVec2 canvas_size, const std::string& label = "");
183void SetNextCanvasSize(ImVec2 size, bool auto_resize = false);
184
185// High-level canvas operations
196
197// Composite drawing operations
198void DrawCanvasGrid(const CanvasRenderContext& ctx, int highlight_tile_id = -1);
200 const ImVector<ImVec2>& points,
201 const ImVector<ImVec2>& selected_points);
203 const ImVector<ImVector<std::string>>& labels,
204 int current_labels, int tile_id_offset);
205
206} // namespace CanvasUtils
207
208} // namespace gui
209} // namespace yaze
210
211#endif // YAZE_APP_GUI_CANVAS_UTILS_H
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Definition arena.cc:34
static Arena & Get()
Definition arena.cc:19
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:67
Defines an abstract interface for all rendering operations.
Definition irenderer.h:40
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