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>
8#include "app/rom.h"
10#include "imgui/imgui.h"
11
12namespace yaze {
13namespace gui {
14
22 // Display settings
23 bool enable_grid = true;
24 bool enable_hex_labels = false;
27 bool is_draggable = false;
28 bool auto_resize = false;
29 bool clamp_rect_to_local_maps = true; // Prevent rectangle wrap across 512x512 boundaries
30 bool use_theme_sizing = true; // Use theme-aware sizing instead of fixed sizes
31 bool enable_metrics = false; // Enable performance/usage tracking
32
33 // Sizing and scale
34 float grid_step = 32.0f;
35 float global_scale = 1.0f;
36 ImVec2 canvas_size = ImVec2(0, 0);
37 ImVec2 content_size = ImVec2(0, 0); // Size of actual content (bitmap, etc.)
38 ImVec2 scrolling = ImVec2(0, 0);
39 bool custom_canvas_size = false;
40
41 // Usage tracking
43
44 // Callbacks for configuration changes (used by modals)
45 std::function<void(const CanvasConfig&)> on_config_changed;
46 std::function<void(const CanvasConfig&)> on_scale_changed;
47
48 // Get theme-aware canvas toolbar height (when use_theme_sizing is true)
49 float GetToolbarHeight() const;
50 // Get theme-aware grid spacing (when use_theme_sizing is true)
51 float GetGridSpacing() const;
52};
53
58 std::vector<ImVec2> selected_tiles;
59 std::vector<ImVec2> selected_points;
60 ImVec2 selected_tile_pos = ImVec2(-1, -1);
61 bool select_rect_active = false;
62
63 void Clear() {
64 selected_tiles.clear();
65 selected_points.clear();
66 selected_tile_pos = ImVec2(-1, -1);
67 select_rect_active = false;
68 }
69};
70
75 std::vector<gfx::SnesPalette> rom_palette_groups;
76 std::vector<std::string> palette_group_names;
78 bool palettes_loaded = false;
81
82 // Live update control
83 bool live_update_enabled = true; // Enable/disable live texture updates
84 bool palette_dirty = false; // Track if palette has changed
85
86 void Clear() {
87 rom_palette_groups.clear();
88 palette_group_names.clear();
90 palettes_loaded = false;
94 palette_dirty = false;
95 }
96};
97
102 std::string label;
103 std::string shortcut;
104 std::function<void()> callback;
105 std::function<bool()> enabled_condition = []() { return true; };
106 std::vector<CanvasContextMenuItem> subitems;
107};
108
112namespace CanvasUtils {
113
114// Core utility functions
115ImVec2 AlignToGrid(ImVec2 pos, float grid_step);
116float CalculateEffectiveScale(ImVec2 canvas_size, ImVec2 content_size, float global_scale);
117int GetTileIdFromPosition(ImVec2 mouse_pos, float tile_size, float scale, int tiles_per_row);
118
119// Palette management utilities
120bool LoadROMPaletteGroups(Rom* rom, CanvasPaletteManager& palette_manager);
121bool ApplyPaletteGroup(gfx::IRenderer* renderer, gfx::Bitmap* bitmap, CanvasPaletteManager& palette_manager,
122 int group_index, int palette_index);
123
127inline void ApplyPendingPaletteUpdates(gfx::IRenderer* renderer, gfx::Bitmap* bitmap, CanvasPaletteManager& palette_manager) {
128 if (palette_manager.palette_dirty && bitmap && renderer) {
131 palette_manager.palette_dirty = false;
132 }
133}
134
135// Drawing utility functions (moved from Canvas class)
136void DrawCanvasRect(ImDrawList* draw_list, ImVec2 canvas_p0, ImVec2 scrolling,
137 int x, int y, int w, int h, ImVec4 color, float global_scale);
138void DrawCanvasText(ImDrawList* draw_list, ImVec2 canvas_p0, ImVec2 scrolling,
139 const std::string& text, int x, int y, float global_scale);
140void DrawCanvasOutline(ImDrawList* draw_list, ImVec2 canvas_p0, ImVec2 scrolling,
141 int x, int y, int w, int h, uint32_t color = IM_COL32(255, 255, 255, 200));
142void DrawCanvasOutlineWithColor(ImDrawList* draw_list, ImVec2 canvas_p0, ImVec2 scrolling,
143 int x, int y, int w, int h, ImVec4 color);
144
145// Grid utility functions
146void DrawCanvasGridLines(ImDrawList* draw_list, ImVec2 canvas_p0, ImVec2 canvas_p1,
147 ImVec2 scrolling, float grid_step, float global_scale);
148void DrawCustomHighlight(ImDrawList* draw_list, ImVec2 canvas_p0, ImVec2 scrolling,
149 int highlight_tile_id, float grid_step);
150void DrawHexTileLabels(ImDrawList* draw_list, ImVec2 canvas_p0, ImVec2 scrolling,
151 ImVec2 canvas_sz, float grid_step, float global_scale);
152
153// Layout and interaction utilities
154ImVec2 CalculateCanvasSize(ImVec2 content_region, ImVec2 custom_size, bool use_custom);
155ImVec2 CalculateScaledCanvasSize(ImVec2 canvas_size, float global_scale);
156bool IsPointInCanvas(ImVec2 point, ImVec2 canvas_p0, ImVec2 canvas_p1);
157
158// Size reporting for ImGui table integration
159ImVec2 CalculateMinimumCanvasSize(ImVec2 content_size, float global_scale, float padding = 4.0f);
160ImVec2 CalculatePreferredCanvasSize(ImVec2 content_size, float global_scale, float min_scale = 1.0f);
161void ReserveCanvasSpace(ImVec2 canvas_size, const std::string& label = "");
162void SetNextCanvasSize(ImVec2 size, bool auto_resize = false);
163
164// High-level canvas operations
175
176// Composite drawing operations
177void DrawCanvasGrid(const CanvasRenderContext& ctx, int highlight_tile_id = -1);
178void DrawCanvasOverlay(const CanvasRenderContext& ctx, const ImVector<ImVec2>& points,
179 const ImVector<ImVec2>& selected_points);
180void DrawCanvasLabels(const CanvasRenderContext& ctx, const ImVector<ImVector<std::string>>& labels,
181 int current_labels, int tile_id_offset);
182
183} // namespace CanvasUtils
184
185} // namespace gui
186} // namespace yaze
187
188#endif // YAZE_APP_GUI_CANVAS_UTILS_H
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:74
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Definition arena.cc:32
static Arena & Get()
Definition arena.cc:15
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:66
Defines an abstract interface for all rendering operations.
Definition irenderer.h:35
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)
ImVec2 CalculateCanvasSize(ImVec2 content_region, ImVec2 custom_size, bool use_custom)
bool LoadROMPaletteGroups(Rom *rom, CanvasPaletteManager &palette_manager)
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.
Main namespace for the application.
Definition controller.cc:20
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