15#include "imgui/imgui.h"
23 : deps_(deps), callbacks_(callbacks) {}
37 ImVec2 mouse_position =
38 ImVec2(scaled_position.x / scale, scaled_position.y / scale);
59 "Error: tile16_blockset is not properly initialized (active: %s, "
74 const int superY = local_map / 8;
75 const int superX = local_map % 8;
76 int mouse_x_i =
static_cast<int>(mouse_position.x);
77 int mouse_y_i =
static_cast<int>(mouse_position.y);
83 auto& selected_world =
90 int index_x = superX * 32 + tile16_x;
91 int index_y = superY * 32 + tile16_y;
94 int old_tile_id = selected_world[index_x][index_y];
99 index_x, index_y, old_tile_id);
110 const ImVec2& click_position,
const std::vector<uint8_t>& tile_data) {
115 "ERROR: RenderUpdatedMapBitmap - Invalid current_map %d "
116 "(maps_bmp size=%zu)",
128 ImVec2 start_position;
129 start_position.x =
static_cast<float>(tile_index_x *
kTile16Size);
130 start_position.y =
static_cast<float>(tile_index_y *
kTile16Size);
136 if (!current_bitmap.
is_active() || current_bitmap.
size() == 0) {
138 "TilePaintingManager",
139 "ERROR: RenderUpdatedMapBitmap - Bitmap %d is not active or has no "
140 "data (active=%s, size=%zu)",
142 current_bitmap.
size());
152 if (pixel_index < 0 ||
153 pixel_index >=
static_cast<int>(current_bitmap.
size())) {
155 "TilePaintingManager",
156 "ERROR: RenderUpdatedMapBitmap - pixel_index %d out of bounds "
158 pixel_index, current_bitmap.
size());
164 if (tile_data_index < 0 ||
165 tile_data_index >=
static_cast<int>(tile_data.size())) {
167 "TilePaintingManager",
168 "ERROR: RenderUpdatedMapBitmap - tile_data_index %d out of bounds "
169 "(tile_data size=%zu)",
170 tile_data_index, tile_data.size());
174 current_bitmap.
WriteToPixel(pixel_index, tile_data[tile_data_index]);
189 LOG_DEBUG(
"TilePaintingManager",
"CheckForOverworldEdits: Frame %d",
190 ImGui::GetFrameCount());
208 ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
214 const bool allow_special_tail =
223 if (map_x >= 0 && map_x < 8 && map_y >= 0 && map_y < 8) {
226 const int local_map = map_x + (map_y * 8);
230 std::vector<int> pattern_ids;
240 static_cast<int>(std::floor(std::min(start.x, end.x) / 16.0f));
242 static_cast<int>(std::floor(std::max(start.x, end.x) / 16.0f));
244 static_cast<int>(std::floor(std::min(start.y, end.y) / 16.0f));
246 static_cast<int>(std::floor(std::max(start.y, end.y) / 16.0f));
248 pattern_w = std::max(1, end_x - start_x + 1);
249 pattern_h = std::max(1, end_y - start_y + 1);
250 pattern_ids.reserve(pattern_w * pattern_h);
254 for (
int y = start_y; y <= end_y; ++y) {
255 for (
int x = start_x; x <= end_x; ++x) {
271 for (
int y = 0; y < 32; ++y) {
272 for (
int x = 0; x < 32; ++x) {
273 const int pattern_x = x % pattern_w;
274 const int pattern_y = y % pattern_h;
275 const int new_tile_id =
276 pattern_ids[pattern_y * pattern_w + pattern_x];
278 const int global_x = map_x * 32 + x;
279 const int global_y = map_y * 32 + y;
280 if (global_x < 0 || global_x >= 256 || global_y < 0 ||
285 const int old_tile_id = world_tiles[global_x][global_y];
286 if (old_tile_id == new_tile_id) {
291 global_x, global_y, old_tile_id);
292 world_tiles[global_x][global_y] = new_tile_id;
308 if (ImGui::IsMouseClicked(ImGuiMouseButton_Left) ||
309 ImGui::IsMouseDragging(ImGuiMouseButton_Left)) {
311 "CheckForOverworldEdits: About to apply rectangle selection");
313 auto& selected_world =
330 std::swap(start_x, end_x);
332 std::swap(start_y, end_y);
334 constexpr int local_map_size = 512;
336 constexpr int tiles_per_local_map = local_map_size /
kTile16Size;
339 "CheckForOverworldEdits: About to fill rectangle with "
348 for (
int y = start_y;
352 for (
int x = start_x;
357 int local_map_x = x / local_map_size;
358 int local_map_y = y / local_map_size;
365 int index_x = local_map_x * tiles_per_local_map + tile16_x;
366 int index_y = local_map_y * tiles_per_local_map + tile16_y;
371 int rect_width = ((end_x - start_x) /
kTile16Size) + 1;
372 int rect_height = ((end_y - start_y) /
kTile16Size) + 1;
375 int start_local_map_x = start_x / local_map_size;
376 int start_local_map_y = start_y / local_map_size;
377 int end_local_map_x = end_x / local_map_size;
378 int end_local_map_y = end_y / local_map_size;
380 bool in_same_local_map = (start_local_map_x == end_local_map_x) &&
381 (start_local_map_y == end_local_map_y);
383 if (in_same_local_map && index_x >= 0 &&
384 (index_x + rect_width - 1) < 0x200 && index_y >= 0 &&
385 (index_y + rect_height - 1) < 0x200) {
387 int old_tile_id = selected_world[index_x][index_y];
388 if (old_tile_id != tile16_id) {
391 index_y, old_tile_id);
394 selected_world[index_x][index_y] = tile16_id;
398 ImVec2 tile_position(x, y);
401 if (!tile_data.empty()) {
404 "TilePaintingManager",
405 "CheckForOverworldEdits: Updated bitmap at position (%d,%d) "
410 "ERROR: Failed to get tile data for tile16_id=%d",
475 const bool allow_special_tail =
488 if (map_x < 0 || map_x >= 8 || map_y < 0 || map_y >= 8) {
495 const int local_tile_x =
498 const int local_tile_y =
501 if (local_tile_x < 0 || local_tile_x >= 32 || local_tile_y < 0 ||
502 local_tile_y >= 32) {
506 const int world_tile_x = map_x * 32 + local_tile_x;
507 const int world_tile_y = map_y * 32 + local_tile_y;
508 if (world_tile_x < 0 || world_tile_x >= 256 || world_tile_y < 0 ||
509 world_tile_y >= 256) {
519 ? map_tiles->dark_world
520 : map_tiles->special_world;
521 const int tile_id = world_tiles[world_tile_x][world_tile_y];
530 auto set_tile_status =
532 if (!set_tile_status.ok()) {
533 util::logf(
"Failed to sync Tile16 editor after eyedropper: %s",
534 set_tile_status.message().data());
void set_dirty(bool dirty)
absl::Status SetCurrentTile(int id)
void CheckForSelectRectangle()
Draw and create the tile16 IDs that are currently selected.
TilePaintingCallbacks callbacks_
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)
TilePaintingDependencies deps_
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Represents a bitmap image optimized for SNES ROM hacking.
void WriteToPixel(int position, uint8_t value)
Write a value to a pixel at the given position.
const std::vector< uint8_t > & vector() const
void set_modified(bool modified)
auto selected_tile_pos() const
auto global_scale() const
auto select_rect_active() const
void SetUsageMode(CanvasUsage usage)
auto selected_tiles() const
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.
auto hover_mouse_pos() const
auto drawn_tile_position() const
bool DrawTilemapPainter(gfx::Tilemap &tilemap, int current_tile)
void set_selected_tile_pos(ImVec2 pos)
void DrawSelectRect(int current_map, int tile_size=0x10, float scale=1.0f)
bool IsMouseHovering() const
auto selected_points() const
void set_current_world(int world)
int GetTileFromPosition(ImVec2 position) const
void set_current_map(int i)
uint16_t GetTile(int x, int y) const
#define LOG_DEBUG(category, format,...)
#define LOG_ERROR(category, format,...)
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)
void logf(const absl::FormatSpec< Args... > &format, Args &&... args)
constexpr int kNumOverworldMaps
bool kEnableSpecialWorldExpansion
struct yaze::core::FeatureFlags::Flags::Overworld overworld
Callbacks for undo integration and map refresh.
std::function< void(int tile_id)> request_tile16_selection
When set, eyedropper routes here (guarded RequestTileSwitch path).
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()> refresh_overworld_map
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.
Tile16Editor * tile16_editor
gui::Canvas * ow_map_canvas
zelda3::Overworld * overworld
std::vector< int > * selected_tile16_ids
std::array< gfx::Bitmap, zelda3::kNumOverworldMaps > * maps_bmp
gfx::Tilemap * tile16_blockset
EditingMode * current_mode
Bitmap atlas
Master bitmap containing all tiles.