yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
yaze::gui::CoordinateMapper Class Reference

Maps screen coordinates to tile/ROM data. More...

#include <coordinate_mapper.h>

Collaboration diagram for yaze::gui::CoordinateMapper:

Public Types

using RomOffsetCalculator = std::function<uint32_t(int tile_id)>
 Callback type for custom ROM offset calculation.
 

Public Member Functions

 CoordinateMapper ()=default
 
 ~CoordinateMapper ()=default
 
void SetConfig (const CoordinateMapperConfig &config)
 
const CoordinateMapperConfigGetConfig () const
 
void SetGeometry (const CanvasGeometry &geometry)
 
const CanvasGeometryGetGeometry () const
 
void SetScale (float scale)
 
float GetScale () const
 
void SetCanvasId (const std::string &id)
 
const std::string & GetCanvasId () const
 
ScreenToTileResult ScreenToTile (float screen_x, float screen_y) const
 Convert screen coordinates to tile information.
 
TileHitInfo CanvasToTile (float canvas_x, float canvas_y) const
 Convert canvas-relative coordinates to tile information.
 
ImVec2 TileToCanvas (int tile_x, int tile_y) const
 Convert tile coordinates to canvas position.
 
ImVec2 TileToScreen (int tile_x, int tile_y) const
 Convert tile coordinates to screen position.
 
int GetTileId (float canvas_x, float canvas_y) const
 Get tile ID from canvas position.
 
int GetTile16Index (float canvas_x, float canvas_y) const
 Get tile16 index from canvas position.
 
bool IsInCanvasBounds (float screen_x, float screen_y) const
 Check if screen coordinates are within canvas bounds.
 
bool IsValidTileCoord (int tile_x, int tile_y) const
 Check if tile coordinates are valid.
 
uint32_t CalculateRomOffset (int tile_id, uint32_t base_offset=0) const
 Calculate ROM offset for a given tile ID.
 
void SetRomOffsetCalculator (RomOffsetCalculator calculator)
 Set a custom ROM offset calculator.
 
int CalculateLocalMapId (float content_x, float content_y) const
 Calculate local map ID from content coordinates.
 
std::vector< ScreenToTileResultScreenToTileBatch (const std::vector< std::pair< float, float > > &positions) const
 Convert multiple screen positions to tile info.
 

Private Member Functions

ImVec2 ApplyScrollOffset (float canvas_x, float canvas_y) const
 
ImVec2 ApplyInverseScale (float x, float y) const
 
std::pair< int, int > SnapToTileGrid (float x, float y) const
 

Private Attributes

CoordinateMapperConfig config_
 
CanvasGeometry geometry_
 
float scale_ = 1.0f
 
std::string canvas_id_
 
RomOffsetCalculator rom_offset_calculator_
 

Detailed Description

Maps screen coordinates to tile/ROM data.

Provides comprehensive coordinate transformation for mapping user interactions (mouse clicks, touches) to underlying tile data in the ROM. Handles all necessary transformations including:

  • Screen space to canvas space
  • Scroll offset compensation
  • Zoom/scale adjustment
  • Tile grid snapping
  • ROM offset calculation

Usage:

mapper.SetGeometry(canvas.geometry());
mapper.SetConfig({.tile_size = 16.0f, .tiles_per_row = 32});
auto result = mapper.ScreenToTile(mouse_x, mouse_y);
if (result.in_canvas_bounds && result.tile_info.is_valid) {
int tile_id = result.tile_info.tile_id;
uint32_t rom_offset = result.tile_info.rom_offset;
}
Maps screen coordinates to tile/ROM data.
void SetConfig(const CoordinateMapperConfig &config)
ScreenToTileResult ScreenToTile(float screen_x, float screen_y) const
Convert screen coordinates to tile information.
void SetGeometry(const CanvasGeometry &geometry)
TileHitInfo tile_info
Full tile information.
int tile_id
8x8 tile ID (-1 if invalid)

Definition at line 113 of file coordinate_mapper.h.

Member Typedef Documentation

◆ RomOffsetCalculator

using yaze::gui::CoordinateMapper::RomOffsetCalculator = std::function<uint32_t(int tile_id)>

Callback type for custom ROM offset calculation.

Definition at line 210 of file coordinate_mapper.h.

Constructor & Destructor Documentation

◆ CoordinateMapper()

yaze::gui::CoordinateMapper::CoordinateMapper ( )
default

◆ ~CoordinateMapper()

yaze::gui::CoordinateMapper::~CoordinateMapper ( )
default

Member Function Documentation

◆ SetConfig()

void yaze::gui::CoordinateMapper::SetConfig ( const CoordinateMapperConfig & config)
inline

Definition at line 119 of file coordinate_mapper.h.

References config_.

◆ GetConfig()

const CoordinateMapperConfig & yaze::gui::CoordinateMapper::GetConfig ( ) const
inline

Definition at line 120 of file coordinate_mapper.h.

References config_.

◆ SetGeometry()

void yaze::gui::CoordinateMapper::SetGeometry ( const CanvasGeometry & geometry)
inline

Definition at line 122 of file coordinate_mapper.h.

References geometry_.

◆ GetGeometry()

const CanvasGeometry & yaze::gui::CoordinateMapper::GetGeometry ( ) const
inline

Definition at line 123 of file coordinate_mapper.h.

References geometry_.

◆ SetScale()

void yaze::gui::CoordinateMapper::SetScale ( float scale)
inline

Definition at line 125 of file coordinate_mapper.h.

References scale_.

◆ GetScale()

float yaze::gui::CoordinateMapper::GetScale ( ) const
inline

Definition at line 126 of file coordinate_mapper.h.

References scale_.

◆ SetCanvasId()

void yaze::gui::CoordinateMapper::SetCanvasId ( const std::string & id)
inline

Definition at line 128 of file coordinate_mapper.h.

References canvas_id_.

◆ GetCanvasId()

const std::string & yaze::gui::CoordinateMapper::GetCanvasId ( ) const
inline

Definition at line 129 of file coordinate_mapper.h.

References canvas_id_.

◆ ScreenToTile()

ScreenToTileResult yaze::gui::CoordinateMapper::ScreenToTile ( float screen_x,
float screen_y ) const

◆ CanvasToTile()

◆ TileToCanvas()

ImVec2 yaze::gui::CoordinateMapper::TileToCanvas ( int tile_x,
int tile_y ) const

Convert tile coordinates to canvas position.

Parameters
tile_xTile X coordinate
tile_yTile Y coordinate
Returns
Canvas position of tile top-left corner

Definition at line 87 of file coordinate_mapper.cc.

References config_, geometry_, scale_, yaze::gui::CanvasGeometry::scrolling, and yaze::gui::CoordinateMapperConfig::tile_size.

Referenced by TileToScreen().

◆ TileToScreen()

ImVec2 yaze::gui::CoordinateMapper::TileToScreen ( int tile_x,
int tile_y ) const

Convert tile coordinates to screen position.

Parameters
tile_xTile X coordinate
tile_yTile Y coordinate
Returns
Screen position of tile top-left corner

Definition at line 93 of file coordinate_mapper.cc.

References yaze::gui::CanvasGeometry::canvas_p0, geometry_, and TileToCanvas().

Here is the call graph for this function:

◆ GetTileId()

int yaze::gui::CoordinateMapper::GetTileId ( float canvas_x,
float canvas_y ) const

Get tile ID from canvas position.

Parameters
canvas_xCanvas-relative X position
canvas_yCanvas-relative Y position
Returns
Tile ID or -1 if out of bounds

Definition at line 99 of file coordinate_mapper.cc.

References config_, IsValidTileCoord(), yaze::gui::CoordinateMapperConfig::tile_size, and yaze::gui::CoordinateMapperConfig::tiles_per_row.

Referenced by CanvasToTile().

Here is the call graph for this function:

◆ GetTile16Index()

int yaze::gui::CoordinateMapper::GetTile16Index ( float canvas_x,
float canvas_y ) const

Get tile16 index from canvas position.

Parameters
canvas_xCanvas-relative X position
canvas_yCanvas-relative Y position
Returns
Tile16 index or -1 if out of bounds

Definition at line 114 of file coordinate_mapper.cc.

References config_, yaze::gui::CoordinateMapperConfig::tile_size, and yaze::gui::CoordinateMapperConfig::tiles_per_row.

Referenced by CanvasToTile().

◆ IsInCanvasBounds()

bool yaze::gui::CoordinateMapper::IsInCanvasBounds ( float screen_x,
float screen_y ) const

Check if screen coordinates are within canvas bounds.

Definition at line 134 of file coordinate_mapper.cc.

References yaze::gui::CanvasGeometry::canvas_p0, yaze::gui::CanvasGeometry::canvas_p1, and geometry_.

Referenced by ScreenToTile().

◆ IsValidTileCoord()

bool yaze::gui::CoordinateMapper::IsValidTileCoord ( int tile_x,
int tile_y ) const

Check if tile coordinates are valid.

Definition at line 141 of file coordinate_mapper.cc.

References config_, yaze::gui::CoordinateMapperConfig::tiles_per_col, and yaze::gui::CoordinateMapperConfig::tiles_per_row.

Referenced by CanvasToTile(), and GetTileId().

◆ CalculateRomOffset()

uint32_t yaze::gui::CoordinateMapper::CalculateRomOffset ( int tile_id,
uint32_t base_offset = 0 ) const

Calculate ROM offset for a given tile ID.

Parameters
tile_idThe tile ID
base_offsetBase ROM offset for tile data
Returns
ROM offset where tile data is stored

Note: This is a simplified calculation. Actual ROM offsets depend on the specific data type (overworld, dungeon, sprites, etc.) and may need to be overridden or configured per-editor.

Definition at line 147 of file coordinate_mapper.cc.

References config_, and yaze::gui::CoordinateMapperConfig::use_tile16.

Referenced by CanvasToTile().

◆ SetRomOffsetCalculator()

void yaze::gui::CoordinateMapper::SetRomOffsetCalculator ( RomOffsetCalculator calculator)
inline

Set a custom ROM offset calculator.

Definition at line 215 of file coordinate_mapper.h.

References rom_offset_calculator_.

◆ CalculateLocalMapId()

int yaze::gui::CoordinateMapper::CalculateLocalMapId ( float content_x,
float content_y ) const

Calculate local map ID from content coordinates.

Parameters
content_xX coordinate in content space
content_yY coordinate in content space
Returns
Local map ID (0-159 for light world, etc.)

Definition at line 163 of file coordinate_mapper.cc.

References config_, and yaze::gui::CoordinateMapperConfig::local_map_size.

Referenced by CanvasToTile().

◆ ScreenToTileBatch()

std::vector< ScreenToTileResult > yaze::gui::CoordinateMapper::ScreenToTileBatch ( const std::vector< std::pair< float, float > > & positions) const

Convert multiple screen positions to tile info.

Parameters
positionsVector of (x, y) screen positions
Returns
Vector of tile hit results

Definition at line 177 of file coordinate_mapper.cc.

References ScreenToTile().

Here is the call graph for this function:

◆ ApplyScrollOffset()

ImVec2 yaze::gui::CoordinateMapper::ApplyScrollOffset ( float canvas_x,
float canvas_y ) const
private

Definition at line 189 of file coordinate_mapper.cc.

References geometry_, and yaze::gui::CanvasGeometry::scrolling.

Referenced by ScreenToTile().

◆ ApplyInverseScale()

ImVec2 yaze::gui::CoordinateMapper::ApplyInverseScale ( float x,
float y ) const
private

Definition at line 195 of file coordinate_mapper.cc.

References scale_.

Referenced by ScreenToTile().

◆ SnapToTileGrid()

std::pair< int, int > yaze::gui::CoordinateMapper::SnapToTileGrid ( float x,
float y ) const
private

Definition at line 202 of file coordinate_mapper.cc.

References config_, and yaze::gui::CoordinateMapperConfig::tile_size.

Referenced by CanvasToTile().

Member Data Documentation

◆ config_

◆ geometry_

CanvasGeometry yaze::gui::CoordinateMapper::geometry_
private

◆ scale_

float yaze::gui::CoordinateMapper::scale_ = 1.0f
private

Definition at line 251 of file coordinate_mapper.h.

Referenced by ApplyInverseScale(), GetScale(), SetScale(), and TileToCanvas().

◆ canvas_id_

std::string yaze::gui::CoordinateMapper::canvas_id_
private

Definition at line 252 of file coordinate_mapper.h.

Referenced by GetCanvasId(), ScreenToTile(), and SetCanvasId().

◆ rom_offset_calculator_

RomOffsetCalculator yaze::gui::CoordinateMapper::rom_offset_calculator_
private

Definition at line 253 of file coordinate_mapper.h.

Referenced by CanvasToTile(), and SetRomOffsetCalculator().


The documentation for this class was generated from the following files: