yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
canvas_automation_api.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cmath>
5
7
8namespace yaze {
9namespace gui {
10
12
13// ============================================================================
14// Tile Operations
15// ============================================================================
16
17bool CanvasAutomationAPI::SetTileAt(int x, int y, int tile_id) {
18 if (!IsInBounds(x, y)) {
19 return false;
20 }
21
23 return tile_paint_callback_(x, y, tile_id);
24 }
25
26 // Default behavior: add to canvas points for drawing
27 // Note: Actual tile painting depends on the editor's canvas integration
28 ImVec2 canvas_pos = TileToCanvas(x, y);
29 canvas_->mutable_points()->push_back(canvas_pos);
30 return true;
31}
32
33int CanvasAutomationAPI::GetTileAt(int x, int y) const {
34 if (!IsInBounds(x, y)) {
35 return -1;
36 }
37
39 return tile_query_callback_(x, y);
40 }
41
42 // Default: return -1 (requires callback for actual implementation)
43 return -1;
44}
45
47 const std::vector<std::tuple<int, int, int>>& tiles) {
48 bool all_success = true;
49 for (const auto& [x, y, tile_id] : tiles) {
50 if (!SetTileAt(x, y, tile_id)) {
51 all_success = false;
52 }
53 }
54 return all_success;
55}
56
57// ============================================================================
58// Selection Operations
59// ============================================================================
60
62 if (!IsInBounds(x, y)) {
63 return;
64 }
65
66 ImVec2 canvas_pos = TileToCanvas(x, y);
68 canvas_->mutable_selected_points()->push_back(canvas_pos);
69 canvas_->mutable_selected_points()->push_back(canvas_pos);
70}
71
72void CanvasAutomationAPI::SelectTileRect(int x1, int y1, int x2, int y2) {
73 // Ensure x1 <= x2 and y1 <= y2
74 if (x1 > x2)
75 std::swap(x1, x2);
76 if (y1 > y2)
77 std::swap(y1, y2);
78
79 if (!IsInBounds(x1, y1) || !IsInBounds(x2, y2)) {
80 return;
81 }
82
83 ImVec2 start = TileToCanvas(x1, y1);
84 ImVec2 end = TileToCanvas(x2, y2);
85
87 canvas_->mutable_selected_points()->push_back(start);
88 canvas_->mutable_selected_points()->push_back(end);
89}
90
92 SelectionState state;
93
94 const auto& selected_points = canvas_->selected_points();
95 if (selected_points.size() >= 2) {
96 state.has_selection = true;
97 state.selection_start = selected_points[0];
98 state.selection_end = selected_points[1];
99
100 // Convert canvas positions back to tile coordinates
101 ImVec2 tile_start = CanvasToTile(state.selection_start);
102 ImVec2 tile_end = CanvasToTile(state.selection_end);
103
104 // Ensure proper ordering
105 int min_x =
106 std::min(static_cast<int>(tile_start.x), static_cast<int>(tile_end.x));
107 int max_x =
108 std::max(static_cast<int>(tile_start.x), static_cast<int>(tile_end.x));
109 int min_y =
110 std::min(static_cast<int>(tile_start.y), static_cast<int>(tile_end.y));
111 int max_y =
112 std::max(static_cast<int>(tile_start.y), static_cast<int>(tile_end.y));
113
114 // Generate all tiles in selection rectangle
115 for (int y = min_y; y <= max_y; ++y) {
116 for (int x = min_x; x <= max_x; ++x) {
117 state.selected_tiles.push_back(
118 ImVec2(static_cast<float>(x), static_cast<float>(y)));
119 }
120 }
121 }
122
123 return state;
124}
125
129
130// ============================================================================
131// View Operations
132// ============================================================================
133
134void CanvasAutomationAPI::ScrollToTile(int x, int y, bool center) {
135 if (!IsInBounds(x, y)) {
136 return;
137 }
138
139 if (center) {
140 CenterOn(x, y);
141 return;
142 }
143
144 // Check if tile is already visible
145 if (IsTileVisible(x, y)) {
146 return;
147 }
148
149 // Scroll to make tile visible
150 ImVec2 tile_canvas_pos = TileToCanvas(x, y);
151
152 // Get current scroll and canvas size
153 ImVec2 current_scroll = canvas_->scrolling();
154 ImVec2 canvas_size = canvas_->canvas_size();
155
156 // Calculate new scroll to make tile visible at top-left
157 float new_scroll_x = -tile_canvas_pos.x;
158 float new_scroll_y = -tile_canvas_pos.y;
159
160 canvas_->set_scrolling(ImVec2(new_scroll_x, new_scroll_y));
161}
162
164 if (!IsInBounds(x, y)) {
165 return;
166 }
167
168 ImVec2 tile_canvas_pos = TileToCanvas(x, y);
169 ImVec2 canvas_size = canvas_->canvas_size();
170
171 // Center the tile in the canvas view
172 float new_scroll_x = -(tile_canvas_pos.x - canvas_size.x / 2.0f);
173 float new_scroll_y = -(tile_canvas_pos.y - canvas_size.y / 2.0f);
174
175 canvas_->set_scrolling(ImVec2(new_scroll_x, new_scroll_y));
176}
177
179 // Clamp zoom to reasonable range
180 zoom = std::max(0.25f, std::min(zoom, 4.0f));
182}
183
185 return canvas_->global_scale();
186}
187
188// ============================================================================
189// Query Operations
190// ============================================================================
191
193 Dimensions dims;
194
195 // Get canvas size in pixels
196 ImVec2 canvas_size = canvas_->canvas_size();
197 float scale = canvas_->global_scale();
198
199 // Determine tile size from canvas grid size
200 int tile_size = 16; // Default
201 switch (canvas_->grid_size()) {
203 tile_size = 8;
204 break;
206 tile_size = 16;
207 break;
209 tile_size = 32;
210 break;
212 tile_size = 64;
213 break;
214 }
215
216 dims.tile_size = tile_size;
217 dims.width_tiles = static_cast<int>(canvas_size.x / (tile_size * scale));
218 dims.height_tiles = static_cast<int>(canvas_size.y / (tile_size * scale));
219
220 return dims;
221}
222
224 const {
225 VisibleRegion region;
226
227 ImVec2 scroll = canvas_->scrolling();
228 ImVec2 canvas_size = canvas_->canvas_size();
229 float scale = canvas_->global_scale();
230 int tile_size = GetDimensions().tile_size;
231
232 // Top-left corner of visible region
233 ImVec2 top_left = CanvasToTile(ImVec2(-scroll.x, -scroll.y));
234
235 // Bottom-right corner of visible region
236 ImVec2 bottom_right = CanvasToTile(
237 ImVec2(-scroll.x + canvas_size.x, -scroll.y + canvas_size.y));
238
239 region.min_x = std::max(0, static_cast<int>(top_left.x));
240 region.min_y = std::max(0, static_cast<int>(top_left.y));
241
242 Dimensions dims = GetDimensions();
243 region.max_x =
244 std::min(dims.width_tiles - 1, static_cast<int>(bottom_right.x));
245 region.max_y =
246 std::min(dims.height_tiles - 1, static_cast<int>(bottom_right.y));
247
248 return region;
249}
250
251bool CanvasAutomationAPI::IsTileVisible(int x, int y) const {
252 if (!IsInBounds(x, y)) {
253 return false;
254 }
255
257 return x >= region.min_x && x <= region.max_x && y >= region.min_y &&
258 y <= region.max_y;
259}
260
261bool CanvasAutomationAPI::IsInBounds(int x, int y) const {
262 if (x < 0 || y < 0) {
263 return false;
264 }
265
266 Dimensions dims = GetDimensions();
267 return x < dims.width_tiles && y < dims.height_tiles;
268}
269
270// ============================================================================
271// Coordinate Conversion
272// ============================================================================
273
274ImVec2 CanvasAutomationAPI::TileToCanvas(int x, int y) const {
275 int tile_size = GetDimensions().tile_size;
276 float scale = canvas_->global_scale();
277
278 float canvas_x = x * tile_size * scale;
279 float canvas_y = y * tile_size * scale;
280
281 return ImVec2(canvas_x, canvas_y);
282}
283
284ImVec2 CanvasAutomationAPI::CanvasToTile(ImVec2 canvas_pos) const {
285 int tile_size = GetDimensions().tile_size;
286 float scale = canvas_->global_scale();
287
288 float tile_x = canvas_pos.x / (tile_size * scale);
289 float tile_y = canvas_pos.y / (tile_size * scale);
290
291 return ImVec2(std::floor(tile_x), std::floor(tile_y));
292}
293
294// ============================================================================
295// Callback Registration
296// ============================================================================
297
301
305
306} // namespace gui
307} // namespace yaze
bool IsTileVisible(int x, int y) const
Check if a tile is currently visible.
void SetTileQueryCallback(TileQueryCallback callback)
void SetTilePaintCallback(TilePaintCallback callback)
bool IsInBounds(int x, int y) const
Check if coordinates are within canvas bounds.
bool SetTileAt(int x, int y, int tile_id)
Paint a single tile at logical coordinates.
std::function< bool(int x, int y, int tile_id)> TilePaintCallback
Set callback for tile painting operations. Allows external systems (CLI, AI agents) to implement cust...
void ClearSelection()
Clear current selection.
void SetZoom(float zoom)
Set canvas zoom level.
void SelectTileRect(int x1, int y1, int x2, int y2)
Select a rectangular region of tiles.
VisibleRegion GetVisibleRegion() const
Get currently visible tile region.
Dimensions GetDimensions() const
Get canvas dimensions in logical tile units.
SelectionState GetSelection() const
Query current selection state.
void CenterOn(int x, int y)
Center canvas view on a specific tile.
ImVec2 CanvasToTile(ImVec2 canvas_pos) const
Convert canvas pixel coordinates to logical tile coordinates.
float GetZoom() const
Get current zoom level.
bool SetTiles(const std::vector< std::tuple< int, int, int > > &tiles)
Paint multiple tiles in a batch operation.
ImVec2 TileToCanvas(int x, int y) const
Convert logical tile coordinates to canvas pixel coordinates.
std::function< int(int x, int y)> TileQueryCallback
Set callback for tile querying operations. Allows external systems to provide tile data.
void SelectTile(int x, int y)
Select a single tile.
int GetTileAt(int x, int y) const
Query tile ID at logical coordinates.
void ScrollToTile(int x, int y, bool center=false)
Scroll canvas to make tile visible.
Modern, robust canvas for drawing and manipulating graphics.
Definition canvas.h:150
void set_scrolling(ImVec2 scroll)
Definition canvas.h:446
auto global_scale() const
Definition canvas.h:494
ImVector< ImVec2 > * mutable_points()
Definition canvas.h:440
auto mutable_selected_points()
Definition canvas.h:555
CanvasGridSize grid_size() const
Definition canvas.h:223
auto canvas_size() const
Definition canvas.h:451
void set_global_scale(float scale)
Definition canvas.h:453
auto scrolling() const
Definition canvas.h:445
auto selected_points() const
Definition canvas.h:556
Canvas dimensions in logical tile units.
Selection state returned by GetSelection().
Visible region in logical tile coordinates.