yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
coordinate_mapper.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GUI_CANVAS_COORDINATE_MAPPER_H
2#define YAZE_APP_GUI_CANVAS_COORDINATE_MAPPER_H
3
4#include <cstdint>
5#include <functional>
6#include <string>
7#include <utility>
8#include <vector>
9
11#include "imgui/imgui.h"
12
13namespace yaze {
14namespace gui {
15
16// Forward declaration (defined in canvas.h)
17struct CanvasRuntime;
18
29 int tile_id = -1;
30 int tile16_index = -1;
31 uint32_t rom_offset = 0;
32 int palette_group = 0;
33 int palette_index = 0;
35 ImVec2 tile_size_px;
36 bool is_valid = false;
37
38 // Extended information for debugging
39 int map_x = -1;
40 int map_y = -1;
41 int local_map_id = -1;
42 std::string canvas_id;
43};
44
52 // Input coordinates
53 float screen_x = 0.0f;
54 float screen_y = 0.0f;
55
56 // Canvas-relative coordinates
57 float canvas_x = 0.0f;
58 float canvas_y = 0.0f;
59
60 // Scaled coordinates (accounting for zoom)
61 float scaled_x = 0.0f;
62 float scaled_y = 0.0f;
63
64 // Scroll-adjusted coordinates
65 float content_x = 0.0f;
66 float content_y = 0.0f;
67
68 // Tile information
70
71 // Validity
72 bool in_canvas_bounds = false;
73};
74
79 float tile_size = 16.0f;
80 int tiles_per_row = 32;
81 int tiles_per_col = 32;
82 int local_map_size = 512;
83 bool use_tile16 = true;
84};
85
114 public:
115 CoordinateMapper() = default;
116 ~CoordinateMapper() = default;
117
118 // Configuration
119 void SetConfig(const CoordinateMapperConfig& config) { config_ = config; }
120 const CoordinateMapperConfig& GetConfig() const { return config_; }
121
122 void SetGeometry(const CanvasGeometry& geometry) { geometry_ = geometry; }
123 const CanvasGeometry& GetGeometry() const { return geometry_; }
124
125 void SetScale(float scale) { scale_ = scale; }
126 float GetScale() const { return scale_; }
127
128 void SetCanvasId(const std::string& id) { canvas_id_ = id; }
129 const std::string& GetCanvasId() const { return canvas_id_; }
130
131 // --- Core Transformation Methods ---
132
139 ScreenToTileResult ScreenToTile(float screen_x, float screen_y) const;
140
147 TileHitInfo CanvasToTile(float canvas_x, float canvas_y) const;
148
155 ImVec2 TileToCanvas(int tile_x, int tile_y) const;
156
163 ImVec2 TileToScreen(int tile_x, int tile_y) const;
164
171 int GetTileId(float canvas_x, float canvas_y) const;
172
179 int GetTile16Index(float canvas_x, float canvas_y) const;
180
181 // --- Bounds Checking ---
182
186 bool IsInCanvasBounds(float screen_x, float screen_y) const;
187
191 bool IsValidTileCoord(int tile_x, int tile_y) const;
192
193 // --- ROM Offset Calculation ---
194
205 uint32_t CalculateRomOffset(int tile_id, uint32_t base_offset = 0) const;
206
210 using RomOffsetCalculator = std::function<uint32_t(int tile_id)>;
211
216 rom_offset_calculator_ = std::move(calculator);
217 }
218
219 // --- Local Map Calculation (for Overworld) ---
220
227 int CalculateLocalMapId(float content_x, float content_y) const;
228
229 // --- Batch Operations ---
230
236 std::vector<ScreenToTileResult> ScreenToTileBatch(
237 const std::vector<std::pair<float, float>>& positions) const;
238
239 private:
240 // Apply scroll offset to get content position
241 ImVec2 ApplyScrollOffset(float canvas_x, float canvas_y) const;
242
243 // Apply inverse scale to get unscaled position
244 ImVec2 ApplyInverseScale(float x, float y) const;
245
246 // Snap position to tile grid
247 std::pair<int, int> SnapToTileGrid(float x, float y) const;
248
251 float scale_ = 1.0f;
252 std::string canvas_id_;
254};
255
256// --- Free Functions for Common Operations ---
257
262 float screen_x, float screen_y,
263 float tile_size = 16.0f);
264
268int CalculateTileIndex(float local_x, float local_y,
269 float tile_size, int tiles_per_row);
270
274ImVec4 GetTileBounds(int tile_x, int tile_y, float tile_size, float scale);
275
276} // namespace gui
277} // namespace yaze
278
279#endif // YAZE_APP_GUI_CANVAS_COORDINATE_MAPPER_H
Maps screen coordinates to tile/ROM data.
void SetRomOffsetCalculator(RomOffsetCalculator calculator)
Set a custom ROM offset calculator.
const CoordinateMapperConfig & GetConfig() const
bool IsInCanvasBounds(float screen_x, float screen_y) const
Check if screen coordinates are within canvas bounds.
int GetTileId(float canvas_x, float canvas_y) const
Get tile ID from canvas position.
void SetConfig(const CoordinateMapperConfig &config)
std::pair< int, int > SnapToTileGrid(float x, float y) const
ImVec2 TileToScreen(int tile_x, int tile_y) const
Convert tile coordinates to screen position.
ImVec2 ApplyInverseScale(float x, float y) const
ScreenToTileResult ScreenToTile(float screen_x, float screen_y) const
Convert screen coordinates to tile information.
std::function< uint32_t(int tile_id)> RomOffsetCalculator
Callback type for custom ROM offset calculation.
std::vector< ScreenToTileResult > ScreenToTileBatch(const std::vector< std::pair< float, float > > &positions) const
Convert multiple screen positions to tile info.
void SetCanvasId(const std::string &id)
TileHitInfo CanvasToTile(float canvas_x, float canvas_y) const
Convert canvas-relative coordinates to tile information.
int GetTile16Index(float canvas_x, float canvas_y) const
Get tile16 index from canvas position.
ImVec2 ApplyScrollOffset(float canvas_x, float canvas_y) const
int CalculateLocalMapId(float content_x, float content_y) const
Calculate local map ID from content coordinates.
void SetGeometry(const CanvasGeometry &geometry)
CoordinateMapperConfig config_
const std::string & GetCanvasId() const
bool IsValidTileCoord(int tile_x, int tile_y) const
Check if tile coordinates are valid.
RomOffsetCalculator rom_offset_calculator_
uint32_t CalculateRomOffset(int tile_id, uint32_t base_offset=0) const
Calculate ROM offset for a given tile ID.
const CanvasGeometry & GetGeometry() const
ImVec2 TileToCanvas(int tile_x, int tile_y) const
Convert tile coordinates to canvas position.
int CalculateTileIndex(float local_x, float local_y, float tile_size, int tiles_per_row)
Calculate tile index from local position and grid settings.
ImVec4 GetTileBounds(int tile_x, int tile_y, float tile_size, float scale)
Get tile bounds in canvas space.
TileHitInfo ScreenToTileQuick(const CanvasRuntime &runtime, float screen_x, float screen_y, float tile_size)
Quick screen-to-tile lookup using canvas runtime state.
Canvas geometry calculated per-frame.
Configuration for coordinate mapping.
bool use_tile16
Use 16x16 tile indexing.
int tiles_per_col
Number of tiles per column.
int local_map_size
Size of local map in pixels (512 for OW)
int tiles_per_row
Number of tiles per row in the map.
float tile_size
Size of tiles in pixels (8 or 16)
Screen coordinate mapping result.
float scaled_x
X after applying inverse scale.
TileHitInfo tile_info
Full tile information.
float scaled_y
Y after applying inverse scale.
float canvas_x
X position relative to canvas origin.
float canvas_y
Y position relative to canvas origin.
float content_y
Y in content space (with scroll)
float screen_x
Original screen X coordinate.
float screen_y
Original screen Y coordinate.
bool in_canvas_bounds
Whether point is within canvas.
float content_x
X in content space (with scroll)
Information about a tile at a specific screen location.
bool is_valid
Whether the hit is valid.
ImVec2 tile_origin_px
Top-left of tile in canvas space (pixels)
int palette_group
Palette group index.
uint32_t rom_offset
ROM offset for tile data.
std::string canvas_id
ID of the canvas that was hit.
int palette_index
Palette index within group.
int tile_id
8x8 tile ID (-1 if invalid)
int map_y
Tile Y coordinate in map space.
int tile16_index
16x16 tile index (-1 if invalid)
int local_map_id
Local map ID (for overworld)
int map_x
Tile X coordinate in map space.
ImVec2 tile_size_px
Size of the tile in pixels.