yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
tile_painting_manager.cc
Go to the documentation of this file.
1// Related header
3
4#include <algorithm>
5#include <cmath>
6#include <vector>
7
14#include "core/features.h"
15#include "imgui/imgui.h"
16#include "util/log.h"
18
19namespace yaze::editor {
20
22 const TilePaintingDependencies& deps,
23 const TilePaintingCallbacks& callbacks)
24 : deps_(deps), callbacks_(callbacks) {}
25
26// ---------------------------------------------------------------------------
27// DrawOverworldEdits - single-tile painting on left-click / drag
28// ---------------------------------------------------------------------------
30 // Determine which overworld map the user is currently editing.
31 // drawn_tile_position() returns scaled coordinates, need to unscale
32 auto scaled_position = deps_.ow_map_canvas->drawn_tile_position();
33 float scale = deps_.ow_map_canvas->global_scale();
34 if (scale <= 0.0f)
35 scale = 1.0f;
36
37 // Convert scaled position to world coordinates
38 ImVec2 mouse_position =
39 ImVec2(scaled_position.x / scale, scaled_position.y / scale);
40
41 int map_x = static_cast<int>(mouse_position.x) / kOverworldMapSize;
42 int map_y = static_cast<int>(mouse_position.y) / kOverworldMapSize;
43 *deps_.current_map = map_x + map_y * 8;
44 if (*deps_.current_world == 1) {
45 *deps_.current_map += 0x40;
46 } else if (*deps_.current_world == 2) {
47 *deps_.current_map += 0x80;
48 }
49
50 // Bounds checking to prevent crashes
51 if (*deps_.current_map < 0 ||
52 *deps_.current_map >= static_cast<int>(deps_.maps_bmp->size())) {
53 return; // Invalid map index, skip drawing
54 }
55
56 // Validate tile16_blockset_ before calling GetTilemapData
58 deps_.tile16_blockset->atlas.vector().empty()) {
60 "TilePaintingManager",
61 "Error: tile16_blockset is not properly initialized (active: %s, "
62 "size: %zu)",
63 deps_.tile16_blockset->atlas.is_active() ? "true" : "false",
65 return; // Skip drawing if blockset is invalid
66 }
67
68 // Render the updated map bitmap.
69 auto tile_data =
71 RenderUpdatedMapBitmap(mouse_position, tile_data);
72
73 // Calculate the correct superX and superY values
74 const int world_offset = *deps_.current_world * 0x40;
75 const int local_map = *deps_.current_map - world_offset;
76 const int superY = local_map / 8;
77 const int superX = local_map % 8;
78 int mouse_x_i = static_cast<int>(mouse_position.x);
79 int mouse_y_i = static_cast<int>(mouse_position.y);
80 // Calculate the correct tile16_x and tile16_y positions
81 int tile16_x = (mouse_x_i % kOverworldMapSize) / (kOverworldMapSize / 32);
82 int tile16_y = (mouse_y_i % kOverworldMapSize) / (kOverworldMapSize / 32);
83
84 // Update the overworld map_tiles based on tile16 ID and current world
85 auto& selected_world =
86 (*deps_.current_world == 0)
87 ? deps_.overworld->mutable_map_tiles()->light_world
88 : (*deps_.current_world == 1)
89 ? deps_.overworld->mutable_map_tiles()->dark_world
90 : deps_.overworld->mutable_map_tiles()->special_world;
91
92 int index_x = superX * 32 + tile16_x;
93 int index_y = superY * 32 + tile16_y;
94
95 // Get old tile value for undo tracking
96 int old_tile_id = selected_world[index_x][index_y];
97
98 // Only record undo if tile is actually changing
99 if (old_tile_id != *deps_.current_tile16) {
101 index_x, index_y, old_tile_id);
102 deps_.rom->set_dirty(true);
103 }
104
105 selected_world[index_x][index_y] = *deps_.current_tile16;
106}
107
108// ---------------------------------------------------------------------------
109// RenderUpdatedMapBitmap - update bitmap pixels after tile paint
110// ---------------------------------------------------------------------------
112 const ImVec2& click_position, const std::vector<uint8_t>& tile_data) {
113 // Bounds checking to prevent crashes
114 if (*deps_.current_map < 0 ||
115 *deps_.current_map >= static_cast<int>(deps_.maps_bmp->size())) {
116 LOG_ERROR("TilePaintingManager",
117 "ERROR: RenderUpdatedMapBitmap - Invalid current_map %d "
118 "(maps_bmp size=%zu)",
119 *deps_.current_map, deps_.maps_bmp->size());
120 return; // Invalid map index, skip rendering
121 }
122
123 // Calculate the tile index for x and y based on the click_position
124 int tile_index_x =
125 (static_cast<int>(click_position.x) % kOverworldMapSize) / kTile16Size;
126 int tile_index_y =
127 (static_cast<int>(click_position.y) % kOverworldMapSize) / kTile16Size;
128
129 // Calculate the pixel start position based on tile index and tile size
130 ImVec2 start_position;
131 start_position.x = static_cast<float>(tile_index_x * kTile16Size);
132 start_position.y = static_cast<float>(tile_index_y * kTile16Size);
133
134 // Update the bitmap's pixel data based on the start_position and tile_data
135 gfx::Bitmap& current_bitmap = (*deps_.maps_bmp)[*deps_.current_map];
136
137 // Validate bitmap state before writing
138 if (!current_bitmap.is_active() || current_bitmap.size() == 0) {
139 LOG_ERROR(
140 "TilePaintingManager",
141 "ERROR: RenderUpdatedMapBitmap - Bitmap %d is not active or has no "
142 "data (active=%s, size=%zu)",
143 *deps_.current_map, current_bitmap.is_active() ? "true" : "false",
144 current_bitmap.size());
145 return;
146 }
147
148 for (int y = 0; y < kTile16Size; ++y) {
149 for (int x = 0; x < kTile16Size; ++x) {
150 int pixel_index =
151 (start_position.y + y) * kOverworldMapSize + (start_position.x + x);
152
153 // Bounds check for pixel index
154 if (pixel_index < 0 ||
155 pixel_index >= static_cast<int>(current_bitmap.size())) {
156 LOG_ERROR(
157 "TilePaintingManager",
158 "ERROR: RenderUpdatedMapBitmap - pixel_index %d out of bounds "
159 "(bitmap size=%zu)",
160 pixel_index, current_bitmap.size());
161 continue;
162 }
163
164 // Bounds check for tile data
165 int tile_data_index = y * kTile16Size + x;
166 if (tile_data_index < 0 ||
167 tile_data_index >= static_cast<int>(tile_data.size())) {
168 LOG_ERROR(
169 "TilePaintingManager",
170 "ERROR: RenderUpdatedMapBitmap - tile_data_index %d out of bounds "
171 "(tile_data size=%zu)",
172 tile_data_index, tile_data.size());
173 continue;
174 }
175
176 current_bitmap.WriteToPixel(pixel_index, tile_data[tile_data_index]);
177 }
178 }
179
180 current_bitmap.set_modified(true);
181
182 // Immediately update the texture to reflect changes
184 &current_bitmap);
185}
186
187// ---------------------------------------------------------------------------
188// CheckForOverworldEdits - main painting entry point
189// ---------------------------------------------------------------------------
191 LOG_DEBUG("TilePaintingManager", "CheckForOverworldEdits: Frame %d",
192 ImGui::GetFrameCount());
193
195
196 // User has selected a tile they want to draw from the blockset
197 // and clicked on the canvas.
199 *deps_.current_tile16 >= 0 &&
204 }
205
206 // Fill tool: fill the entire 32x32 tile16 screen under the cursor using the
207 // current selection pattern (if any) or the current tile16.
210 ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
211 float scale = deps_.ow_map_canvas->global_scale();
212 if (scale <= 0.0f) {
213 scale = 1.0f;
214 }
215
216 const bool allow_special_tail =
218 const auto scaled_position = deps_.ow_map_canvas->hover_mouse_pos();
219 const int map_x =
220 static_cast<int>(scaled_position.x / scale) / kOverworldMapSize;
221 const int map_y =
222 static_cast<int>(scaled_position.y / scale) / kOverworldMapSize;
223
224 // Bounds guard.
225 if (map_x >= 0 && map_x < 8 && map_y >= 0 && map_y < 8) {
226 // Special world only renders 4 rows unless tail expansion is enabled.
227 if (allow_special_tail || *deps_.current_world != 2 || map_y < 4) {
228 const int local_map = map_x + (map_y * 8);
229 const int target_map = local_map + (*deps_.current_world * 0x40);
230 if (target_map >= 0 && target_map < zelda3::kNumOverworldMaps) {
231 // Build pattern from active rectangle selection (if present).
232 std::vector<int> pattern_ids;
233 int pattern_w = 1;
234 int pattern_h = 1;
235
237 deps_.ow_map_canvas->selected_points().size() >= 2) {
238 const auto start = deps_.ow_map_canvas->selected_points()[0];
239 const auto end = deps_.ow_map_canvas->selected_points()[1];
240
241 const int start_x =
242 static_cast<int>(std::floor(std::min(start.x, end.x) / 16.0f));
243 const int end_x =
244 static_cast<int>(std::floor(std::max(start.x, end.x) / 16.0f));
245 const int start_y =
246 static_cast<int>(std::floor(std::min(start.y, end.y) / 16.0f));
247 const int end_y =
248 static_cast<int>(std::floor(std::max(start.y, end.y) / 16.0f));
249
250 pattern_w = std::max(1, end_x - start_x + 1);
251 pattern_h = std::max(1, end_y - start_y + 1);
252 pattern_ids.reserve(pattern_w * pattern_h);
253
255 deps_.overworld->set_current_map(target_map);
256 for (int y = start_y; y <= end_y; ++y) {
257 for (int x = start_x; x <= end_x; ++x) {
258 pattern_ids.push_back(deps_.overworld->GetTile(x, y));
259 }
260 }
261 } else {
262 pattern_ids = {*deps_.current_tile16};
263 }
264
265 auto& world_tiles =
266 (*deps_.current_world == 0)
267 ? deps_.overworld->mutable_map_tiles()->light_world
268 : (*deps_.current_world == 1)
269 ? deps_.overworld->mutable_map_tiles()->dark_world
270 : deps_.overworld->mutable_map_tiles()->special_world;
271
272 // Apply the fill (repeat pattern across 32x32).
273 for (int y = 0; y < 32; ++y) {
274 for (int x = 0; x < 32; ++x) {
275 const int pattern_x = x % pattern_w;
276 const int pattern_y = y % pattern_h;
277 const int new_tile_id =
278 pattern_ids[pattern_y * pattern_w + pattern_x];
279
280 const int global_x = map_x * 32 + x;
281 const int global_y = map_y * 32 + y;
282 if (global_x < 0 || global_x >= 256 || global_y < 0 ||
283 global_y >= 256) {
284 continue;
285 }
286
287 const int old_tile_id = world_tiles[global_x][global_y];
288 if (old_tile_id == new_tile_id) {
289 continue;
290 }
291
293 global_x, global_y, old_tile_id);
294 world_tiles[global_x][global_y] = new_tile_id;
295 }
296 }
297
298 deps_.rom->set_dirty(true);
300 *deps_.current_map = target_map;
302 }
303 }
304 }
305 }
306
307 // Rectangle selection stamping (brush mode only).
310 if (ImGui::IsMouseClicked(ImGuiMouseButton_Left) ||
311 ImGui::IsMouseDragging(ImGuiMouseButton_Left)) {
312 LOG_DEBUG("TilePaintingManager",
313 "CheckForOverworldEdits: About to apply rectangle selection");
314
315 auto& selected_world =
316 (*deps_.current_world == 0)
317 ? deps_.overworld->mutable_map_tiles()->light_world
318 : (*deps_.current_world == 1)
319 ? deps_.overworld->mutable_map_tiles()->dark_world
320 : deps_.overworld->mutable_map_tiles()->special_world;
321 // selected_points are now stored in world coordinates
322 auto start = deps_.ow_map_canvas->selected_points()[0];
323 auto end = deps_.ow_map_canvas->selected_points()[1];
324
325 // Calculate the bounds of the rectangle in terms of 16x16 tile indices
326 int start_x = std::floor(start.x / kTile16Size) * kTile16Size;
327 int start_y = std::floor(start.y / kTile16Size) * kTile16Size;
328 int end_x = std::floor(end.x / kTile16Size) * kTile16Size;
329 int end_y = std::floor(end.y / kTile16Size) * kTile16Size;
330
331 if (start_x > end_x)
332 std::swap(start_x, end_x);
333 if (start_y > end_y)
334 std::swap(start_y, end_y);
335
336 constexpr int local_map_size = 512; // Size of each local map
337 // Number of tiles per local map (since each tile is 16x16)
338 constexpr int tiles_per_local_map = local_map_size / kTile16Size;
339
340 LOG_DEBUG("TilePaintingManager",
341 "CheckForOverworldEdits: About to fill rectangle with "
342 "current_tile16=%d",
344
345 // Apply the selected tiles to each position in the rectangle
346 // CRITICAL FIX: Use pre-computed tile16_ids instead of recalculating
347 // from selected_tiles. This prevents wrapping issues when dragging near
348 // boundaries.
349 int i = 0;
350 for (int y = start_y;
351 y <= end_y &&
352 i < static_cast<int>(deps_.selected_tile16_ids->size());
353 y += kTile16Size) {
354 for (int x = start_x;
355 x <= end_x &&
356 i < static_cast<int>(deps_.selected_tile16_ids->size());
357 x += kTile16Size, ++i) {
358 // Determine which local map (512x512) the tile is in
359 int local_map_x = x / local_map_size;
360 int local_map_y = y / local_map_size;
361
362 // Calculate the tile's position within its local map
363 int tile16_x = (x % local_map_size) / kTile16Size;
364 int tile16_y = (y % local_map_size) / kTile16Size;
365
366 // Calculate the index within the overall map structure
367 int index_x = local_map_x * tiles_per_local_map + tile16_x;
368 int index_y = local_map_y * tiles_per_local_map + tile16_y;
369
370 // FIXED: Use pre-computed tile ID from the ORIGINAL selection
371 int tile16_id = (*deps_.selected_tile16_ids)[i];
372 // Bounds check for the selected world array
373 int rect_width = ((end_x - start_x) / kTile16Size) + 1;
374 int rect_height = ((end_y - start_y) / kTile16Size) + 1;
375
376 // Prevent painting from wrapping around at the edges of large maps
377 int start_local_map_x = start_x / local_map_size;
378 int start_local_map_y = start_y / local_map_size;
379 int end_local_map_x = end_x / local_map_size;
380 int end_local_map_y = end_y / local_map_size;
381
382 bool in_same_local_map = (start_local_map_x == end_local_map_x) &&
383 (start_local_map_y == end_local_map_y);
384
385 if (in_same_local_map && index_x >= 0 &&
386 (index_x + rect_width - 1) < 0x200 && index_y >= 0 &&
387 (index_y + rect_height - 1) < 0x200) {
388 // Get old tile value for undo tracking
389 int old_tile_id = selected_world[index_x][index_y];
390 if (old_tile_id != tile16_id) {
392 *deps_.current_world, index_x,
393 index_y, old_tile_id);
394 }
395
396 selected_world[index_x][index_y] = tile16_id;
397
398 // CRITICAL FIX: Also update the bitmap directly like single tile
399 // drawing
400 ImVec2 tile_position(x, y);
401 auto tile_data =
403 if (!tile_data.empty()) {
404 RenderUpdatedMapBitmap(tile_position, tile_data);
405 LOG_DEBUG(
406 "TilePaintingManager",
407 "CheckForOverworldEdits: Updated bitmap at position (%d,%d) "
408 "with tile16_id=%d",
409 x, y, tile16_id);
410 } else {
411 LOG_ERROR("TilePaintingManager",
412 "ERROR: Failed to get tile data for tile16_id=%d",
413 tile16_id);
414 }
415 }
416 }
417 }
418
419 // Finalize the undo batch operation after all tiles are placed
421
422 deps_.rom->set_dirty(true);
424 }
425 }
426}
427
428// ---------------------------------------------------------------------------
429// CheckForSelectRectangle - rectangle drag-to-select tiles
430// ---------------------------------------------------------------------------
432 // Pass the canvas scale for proper zoom handling
433 float scale = deps_.ow_map_canvas->global_scale();
434 if (scale <= 0.0f)
435 scale = 1.0f;
437
438 // Single tile case
439 if (deps_.ow_map_canvas->selected_tile_pos().x != -1) {
444
445 // Scroll blockset canvas to show the selected tile
447 }
448
449 // Rectangle selection case - use member variable instead of static local
451 // Get the tile16 IDs from the selected tile ID positions
452 deps_.selected_tile16_ids->clear();
453
454 if (deps_.ow_map_canvas->selected_tiles().size() > 0) {
455 // Set the current world and map in overworld for proper tile lookup
458 for (auto& each : deps_.ow_map_canvas->selected_tiles()) {
459 deps_.selected_tile16_ids->push_back(
461 }
462 }
463 }
464 // Create a composite image of all the tile16s selected
466 *deps_.tile16_blockset, 0x10,
468}
469
470// ---------------------------------------------------------------------------
471// PickTile16FromHoveredCanvas - eyedropper tool
472// ---------------------------------------------------------------------------
475 return false;
476 }
477
478 const bool allow_special_tail =
480
481 const ImVec2 scaled_position = deps_.ow_map_canvas->hover_mouse_pos();
482 float scale = deps_.ow_map_canvas->global_scale();
483 if (scale <= 0.0f) {
484 scale = 1.0f;
485 }
486
487 const int map_x =
488 static_cast<int>(scaled_position.x / scale) / kOverworldMapSize;
489 const int map_y =
490 static_cast<int>(scaled_position.y / scale) / kOverworldMapSize;
491 if (map_x < 0 || map_x >= 8 || map_y < 0 || map_y >= 8) {
492 return false;
493 }
494 if (!allow_special_tail && *deps_.current_world == 2 && map_y >= 4) {
495 return false;
496 }
497
498 const int local_tile_x =
499 (static_cast<int>(scaled_position.x / scale) % kOverworldMapSize) /
501 const int local_tile_y =
502 (static_cast<int>(scaled_position.y / scale) % kOverworldMapSize) /
504 if (local_tile_x < 0 || local_tile_x >= 32 || local_tile_y < 0 ||
505 local_tile_y >= 32) {
506 return false;
507 }
508
509 const int world_tile_x = map_x * 32 + local_tile_x;
510 const int world_tile_y = map_y * 32 + local_tile_y;
511 if (world_tile_x < 0 || world_tile_x >= 256 || world_tile_y < 0 ||
512 world_tile_y >= 256) {
513 return false;
514 }
515
516 const auto* map_tiles = deps_.overworld->mutable_map_tiles();
517 if (!map_tiles) {
518 return false;
519 }
520 const auto& world_tiles =
521 (*deps_.current_world == 0) ? map_tiles->light_world
522 : (*deps_.current_world == 1) ? map_tiles->dark_world
523 : map_tiles->special_world;
524 const int tile_id = world_tiles[world_tile_x][world_tile_y];
525 if (tile_id < 0) {
526 return false;
527 }
528
529 *deps_.current_tile16 = tile_id;
530 auto set_tile_status = deps_.tile16_editor->SetCurrentTile(*deps_.current_tile16);
531 if (!set_tile_status.ok()) {
532 util::logf("Failed to sync Tile16 editor after eyedropper: %s",
533 set_tile_status.message().data());
534 }
535
537 return true;
538}
539
540// ---------------------------------------------------------------------------
541// ToggleBrushTool - switch between DRAW_TILE and MOUSE modes
542// ---------------------------------------------------------------------------
556
557// ---------------------------------------------------------------------------
558// ActivateFillTool - toggle FILL_TILE mode on/off
559// ---------------------------------------------------------------------------
570
571} // namespace yaze::editor
void set_dirty(bool dirty)
Definition rom.h:134
static Flags & get()
Definition features.h:118
absl::Status SetCurrentTile(int id)
void CheckForSelectRectangle()
Draw and create the tile16 IDs that are currently selected.
void DrawOverworldEdits()
Handle the actual drawing of a single tile (called by CheckForOverworldEdits when DrawTilemapPainter ...
void CheckForOverworldEdits()
Main entry point: check for tile edits (paint, fill, stamp).
void RenderUpdatedMapBitmap(const ImVec2 &click_position, const std::vector< uint8_t > &tile_data)
Update bitmap pixels after a single tile paint.
bool PickTile16FromHoveredCanvas()
Eyedropper: pick the tile16 under the hovered canvas position.
void ActivateFillTool()
Toggle FILL_TILE mode on/off.
void ToggleBrushTool()
Toggle between DRAW_TILE and MOUSE modes.
TilePaintingManager(const TilePaintingDependencies &deps, const TilePaintingCallbacks &callbacks)
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
void WriteToPixel(int position, uint8_t value)
Write a value to a pixel at the given position.
Definition bitmap.cc:581
const std::vector< uint8_t > & vector() const
Definition bitmap.h:381
auto size() const
Definition bitmap.h:376
bool is_active() const
Definition bitmap.h:384
void set_modified(bool modified)
Definition bitmap.h:388
auto selected_tile_pos() const
Definition canvas.h:489
auto global_scale() const
Definition canvas.h:491
auto select_rect_active() const
Definition canvas.h:487
void SetUsageMode(CanvasUsage usage)
Definition canvas.cc:280
auto selected_tiles() const
Definition canvas.h:488
void DrawBitmapGroup(std::vector< int > &group, gfx::Tilemap &tilemap, int tile_size, float scale=1.0f, int local_map_size=0x200, ImVec2 total_map_size=ImVec2(0x1000, 0x1000))
Draw group of bitmaps for multi-tile selection preview.
Definition canvas.cc:1236
auto hover_mouse_pos() const
Definition canvas.h:555
auto drawn_tile_position() const
Definition canvas.h:450
bool DrawTilemapPainter(gfx::Tilemap &tilemap, int current_tile)
Definition canvas.cc:980
void set_selected_tile_pos(ImVec2 pos)
Definition canvas.h:490
void DrawSelectRect(int current_map, int tile_size=0x10, float scale=1.0f)
Definition canvas.cc:1123
bool IsMouseHovering() const
Definition canvas.h:433
auto selected_points() const
Definition canvas.h:553
void set_current_world(int world)
Definition overworld.h:590
int GetTileFromPosition(ImVec2 position) const
Definition overworld.h:504
void set_current_map(int i)
Definition overworld.h:589
uint16_t GetTile(int x, int y) const
Definition overworld.h:591
#define LOG_DEBUG(category, format,...)
Definition log.h:103
#define LOG_ERROR(category, format,...)
Definition log.h:109
Editors are the view controllers for the application.
constexpr unsigned int kOverworldMapSize
constexpr int kTile16Size
std::vector< uint8_t > GetTilemapData(Tilemap &tilemap, int tile_id)
Definition tilemap.cc:270
void logf(const absl::FormatSpec< Args... > &format, Args &&... args)
Definition log.h:115
constexpr int kNumOverworldMaps
Definition common.h:85
struct yaze::core::FeatureFlags::Flags::Overworld overworld
Callbacks for undo integration and map refresh.
std::function< void(int map_index)> refresh_overworld_map_on_demand
std::function< void()> scroll_blockset_to_current_tile
std::function< void()> finalize_paint_operation
std::function< void(int map_id, int world, int x, int y, int old_tile_id)> create_undo_point
Shared state for the tile painting system.
std::array< gfx::Bitmap, zelda3::kNumOverworldMaps > * maps_bmp
Bitmap atlas
Master bitmap containing all tiles.
Definition tilemap.h:119