Files
MonoGame.Extended/source/MonoGame.Extended.Tiled/TiledMapTileset.cs
T
Christopher Whitley 8850875fb4 Texture Atlas Refactor (#890)
* Moved `TextureRegion2D` and `NinePatchRegion2D` into Graphics namespace
These are used for more than just texture atlas related things and are primarly for working with `Texture2D` instances and creating sub textures from them.  Moving them to Graphics namespace where `Texture2D` lives in MonoGame

* Dropped the `2D` suffix from `TextureRegion2D` and `NinePatchRegion2D`

* Moved `RectanglExtensions` to project root
This moves the extensions for `Rectangle` to the same relative namespace location as `Rectangle` is in MonoGame.

* Renamed to `Rectangle.Extensions.cs`

* Separate `Rectangle` from `RectangleF` extensions

* Added `GetRelativeRectangle` method

* Updated documentation

* Added `GetRelativeRectangle` method

* Updated documentation

* Top-level namespace

* Remove whitespace

* Move properties up

* Top-level namespace

* Don't recreate structs for `Bounds` and Size`
By having the property return a new struct, this allocates on the stack during situations like a loop during draw.

* Size should be `Size` not `SizeF` value
Textures deal in pixels (ints not floats)

* Removed setter for `Texture` property
Texture should not be set after region is created

* Added UV properties

* Refactor constructors

* Added documentation

* Added license header

* Make it an actual extension method

* Added `GetSubRegion` extension methods for `TextureRegion`

* Update documentation about ObjectDisposedException

* Move const values to top

* Resolve errors from`TextureRegion` refactor

* Update unit tests for TextureRegion refactor

* Started NinePatch rework

* Refactored NinePatch

* Update for texture region constructor change

* Remove ninepatch from atlas

* Moved `TextureAtlasExtensions` into `SpriteBatch.Extensions`
These are extension methods for the sprite batch

* Updated to use new `DrawTextureRegion` method after rename

* Renamed to NinePatchJsonConverter

* Removed `GetRegion<T>`
TextureAtlas no longer contains nine patch regions, so this method isn't needed

* Update after `NinePatchRegion2DJsonConverter` name change

* Resolve errors from NinePatch refactor

* TextureAtlas rework

* Renamed `TextureRegion` to `Texture2DRegion`

* Moved ContentReaders to new Content\ContentReaders directory

* Moved TextureAtlasJsonConvert to Serialization directory

* Adjusted name from `Texture*` to `Texture2D*`

* Update runtimereader and runtimettype strings
2024-06-15 01:37:18 -04:00

71 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.Graphics;
namespace MonoGame.Extended.Tiled
{
public interface ITileset
{
int ActualWidth { get; }
int Columns { get; }
int ActualHeight { get; }
int Rows { get; }
int TileWidth { get; }
int TileHeight { get; }
Texture2D Texture { get; }
Texture2DRegion GetRegion(int column, int row);
}
public class TiledMapTileset : ITileset
{
public TiledMapTileset(Texture2D texture, string type, int tileWidth, int tileHeight, int tileCount, int spacing, int margin, int columns)
{
Texture = texture;
Type = type;
TileWidth = tileWidth;
TileHeight = tileHeight;
TileCount = tileCount;
Spacing = spacing;
Margin = margin;
Columns = columns;
Properties = new TiledMapProperties();
Tiles = new List<TiledMapTilesetTile>();
}
public string Name => Texture.Name;
public Texture2D Texture { get; }
public Texture2DRegion GetRegion(int column, int row)
{
var x = Margin + column * (TileWidth + Spacing);
var y = Margin + row * (TileHeight + Spacing);
return new Texture2DRegion(Texture, x, y, TileWidth, TileHeight);
}
public string Type { get; }
public int TileWidth { get; }
public int TileHeight { get; }
public int Spacing { get; }
public int Margin { get; }
public int TileCount { get; }
public int Columns { get; }
public List<TiledMapTilesetTile> Tiles { get; }
public TiledMapProperties Properties { get; }
public int Rows => (int)Math.Ceiling((double) TileCount / Columns);
public int ActualWidth => TileWidth * Columns;
public int ActualHeight => TileHeight * Rows;
public Rectangle GetTileRegion(int localTileIdentifier)
{
return Texture is not null
? TiledMapHelper.GetTileSourceRectangle(localTileIdentifier, TileWidth, TileHeight, Columns, Margin,
Spacing)
: Tiles.FirstOrDefault(x => x.LocalTileIdentifier == localTileIdentifier).Texture.Bounds;
}
}
}