15#include "imgui/imgui.h"
24 : deps_(deps), callbacks_(callbacks) {}
38 ImVec2 mouse_position =
39 ImVec2(scaled_position.x / scale, scaled_position.y / scale);
60 "TilePaintingManager",
61 "Error: tile16_blockset is not properly initialized (active: %s, "
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);
85 auto& selected_world =
92 int index_x = superX * 32 + tile16_x;
93 int index_y = superY * 32 + tile16_y;
96 int old_tile_id = selected_world[index_x][index_y];
101 index_x, index_y, old_tile_id);
112 const ImVec2& click_position,
const std::vector<uint8_t>& tile_data) {
117 "ERROR: RenderUpdatedMapBitmap - Invalid current_map %d "
118 "(maps_bmp size=%zu)",
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);
138 if (!current_bitmap.
is_active() || current_bitmap.
size() == 0) {
140 "TilePaintingManager",
141 "ERROR: RenderUpdatedMapBitmap - Bitmap %d is not active or has no "
142 "data (active=%s, size=%zu)",
144 current_bitmap.
size());
154 if (pixel_index < 0 ||
155 pixel_index >=
static_cast<int>(current_bitmap.
size())) {
157 "TilePaintingManager",
158 "ERROR: RenderUpdatedMapBitmap - pixel_index %d out of bounds "
160 pixel_index, current_bitmap.
size());
166 if (tile_data_index < 0 ||
167 tile_data_index >=
static_cast<int>(tile_data.size())) {
169 "TilePaintingManager",
170 "ERROR: RenderUpdatedMapBitmap - tile_data_index %d out of bounds "
171 "(tile_data size=%zu)",
172 tile_data_index, tile_data.size());
176 current_bitmap.
WriteToPixel(pixel_index, tile_data[tile_data_index]);
191 LOG_DEBUG(
"TilePaintingManager",
"CheckForOverworldEdits: Frame %d",
192 ImGui::GetFrameCount());
210 ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
216 const bool allow_special_tail =
225 if (map_x >= 0 && map_x < 8 && map_y >= 0 && map_y < 8) {
228 const int local_map = map_x + (map_y * 8);
232 std::vector<int> pattern_ids;
242 static_cast<int>(std::floor(std::min(start.x, end.x) / 16.0f));
244 static_cast<int>(std::floor(std::max(start.x, end.x) / 16.0f));
246 static_cast<int>(std::floor(std::min(start.y, end.y) / 16.0f));
248 static_cast<int>(std::floor(std::max(start.y, end.y) / 16.0f));
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);
256 for (
int y = start_y; y <= end_y; ++y) {
257 for (
int x = start_x; x <= end_x; ++x) {
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];
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 ||
287 const int old_tile_id = world_tiles[global_x][global_y];
288 if (old_tile_id == new_tile_id) {
293 global_x, global_y, old_tile_id);
294 world_tiles[global_x][global_y] = new_tile_id;
310 if (ImGui::IsMouseClicked(ImGuiMouseButton_Left) ||
311 ImGui::IsMouseDragging(ImGuiMouseButton_Left)) {
313 "CheckForOverworldEdits: About to apply rectangle selection");
315 auto& selected_world =
332 std::swap(start_x, end_x);
334 std::swap(start_y, end_y);
336 constexpr int local_map_size = 512;
338 constexpr int tiles_per_local_map = local_map_size /
kTile16Size;
341 "CheckForOverworldEdits: About to fill rectangle with "
350 for (
int y = start_y;
354 for (
int x = start_x;
359 int local_map_x = x / local_map_size;
360 int local_map_y = y / local_map_size;
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;
373 int rect_width = ((end_x - start_x) /
kTile16Size) + 1;
374 int rect_height = ((end_y - start_y) /
kTile16Size) + 1;
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;
382 bool in_same_local_map = (start_local_map_x == end_local_map_x) &&
383 (start_local_map_y == end_local_map_y);
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) {
389 int old_tile_id = selected_world[index_x][index_y];
390 if (old_tile_id != tile16_id) {
393 index_y, old_tile_id);
396 selected_world[index_x][index_y] = tile16_id;
400 ImVec2 tile_position(x, y);
403 if (!tile_data.empty()) {
406 "TilePaintingManager",
407 "CheckForOverworldEdits: Updated bitmap at position (%d,%d) "
412 "ERROR: Failed to get tile data for tile16_id=%d",
478 const bool allow_special_tail =
491 if (map_x < 0 || map_x >= 8 || map_y < 0 || map_y >= 8) {
498 const int local_tile_x =
501 const int local_tile_y =
504 if (local_tile_x < 0 || local_tile_x >= 32 || local_tile_y < 0 ||
505 local_tile_y >= 32) {
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) {
520 const auto& world_tiles =
523 : map_tiles->special_world;
524 const int tile_id = world_tiles[world_tile_x][world_tile_y];
531 if (!set_tile_status.ok()) {
532 util::logf(
"Failed to sync Tile16 editor after eyedropper: %s",
533 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 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.