mapeditor / rs-tiled

Reads files from the Tiled editor into Rust
https://crates.io/crates/tiled
MIT License
267 stars 102 forks source link

Tileset helper for obtaining UVs #166

Open aleokdev opened 2 years ago

aleokdev commented 2 years ago

Add a function for obtaining the UV of a tile given its ID.

aleokdev commented 2 years ago

Perhaps it would be a good idea to also create a TileData and make Tile an encapsulation, which would avoid the extra index if the user already had the tile in question.

Anti-Alias commented 2 years ago

Perhaps it would be a good idea to also create a TileData and make Tile an encapsulation, which would avoid the extra index if the user already had the tile in question.

I am for this, as it keeps the API flatter and more performant. It means, excluding Tileset and maybe some other types, that the user only needs to talk to the facade types rather than sometimes talking to Type1<'map>and sometimes talking to &'map Type2, which is a little inconsistent. (Perhaps, if the situation changes, Tileset may need its own wrapper, though I don't know of any usecases yet)

I like your suggestion in #164 of just returning a rect measured in pixels. I think we can also add a helper function to this new Tile<'map> type called uvs(flip_y: bool) -> ((f32, f32), (f32, f32)), which can make use of the rect function.

In the helper function:

aleokdev commented 2 years ago

It means, excluding Tileset and maybe some other types, that the user only needs to talk to the facade types rather than sometimes talking to Type1<'map> and sometimes talking to &'map Type2, which is a little inconsistent.

It would be inconsistent anyways. The lifetime of the suggested Tile facade would be of 'tileset, not map. But yes, the interface is very inconsistent right now, specially in terms of types and properties.

bjorn commented 2 years ago

Add a function for obtaining the UV of a tile given its ID.

Just to mention, personally I would find it more suitable to pre-calculate and store the UVs (or rather, the pixel location) of each tile and store it in the TileData. That way the rendering loop doesn't need to keep calculating this based on the margin and spacing and such. But, I guess with TileData wrapped in the public API, it would be possible to add that later as well.

aleokdev commented 2 years ago

Postponing to 0.11 since the change is no longer breaking.