Skip to content

Commit

Permalink
Update all types to import from AsperiteDotNet.Aseprite.AsepriteFIle
Browse files Browse the repository at this point in the history
  • Loading branch information
AristurtleDev committed Mar 25, 2024
1 parent b9ab57a commit 834c781
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 170 deletions.
51 changes: 22 additions & 29 deletions source/MonoGame.Aseprite/Sprites/Sprite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Aseprite.RawTypes;
using MonoGame.Aseprite.Utils;

namespace MonoGame.Aseprite.Sprites;

Expand Down Expand Up @@ -309,42 +309,35 @@ public Sprite(string name, Texture2D texture)
/// </param>
public void Draw(SpriteBatch spriteBatch, Vector2 position) => spriteBatch.Draw(this, position);

/// <summary>
/// Creates a new instance of the <see cref="Sprite"/> class from the given <see cref="RawSprite"/>.
/// </summary>
/// <param name="device">
/// The <see cref="Microsoft.Xna.Framework.Graphics.GraphicsDevice"/> used to create graphical resources.
/// </param>
/// <param name="rawSprite">
/// The <see cref="RawSprite"/> to create the <see cref="Sprite"/> from.
/// </param>
/// <returns>
/// The <see cref="Sprite"/> created by this method.
/// </returns>
public static Sprite FromRaw(GraphicsDevice device, RawSprite rawSprite)
public static Sprite FromFile(GraphicsDevice device, AseFile file, int frameNumber, AseProcessorOptions options)
{
RawTexture rawTexture = rawSprite.RawTexture;

Texture2D texture = new(device, rawTexture.Width, rawTexture.Height, mipmap: false, SurfaceFormat.Color);
texture.Name = rawTexture.Name;
texture.SetData<Color>(rawTexture.Pixels.ToArray());

TextureRegion textureRegion = new(texture.Name, texture, texture.Bounds);

for (int i = 0; i < rawSprite.Slices.Length; i++)
options ??= AseProcessorOptions.Default;
AseSprite aseSprite = AseSpriteProcessor.Process(file, frameNumber, options);
Texture2D texture = aseSprite.Texture.ToTexture2D(device);
texture.Name = aseSprite.Name;
texture.SetData(aseSprite.Texture.Pixels.ToArray());

TextureRegion textureRegion = new TextureRegion(texture.Name, texture, texture.Bounds);
for(int i = 0; i < aseSprite.Slices.Length; i++)
{
RawSlice slice = rawSprite.Slices[i];

if (slice is RawNinePatchSlice ninePatch)
AseSlice aseSlice = aseSprite.Slices[i];
if (aseSlice is AseNinepatchSlice aseNinePatchSlice)
{
_ = textureRegion.CreateNinePatchSlice(ninePatch.Name, ninePatch.Bounds, ninePatch.CenterBounds, ninePatch.Origin, ninePatch.Color);
textureRegion.CreateNinePatchSlice(aseNinePatchSlice.Name,
aseNinePatchSlice.Bounds.ToXnaRectangle(),
aseNinePatchSlice.CenterBounds.ToXnaRectangle(),
aseNinePatchSlice.Origin.ToXnaVector2(),
aseNinePatchSlice.Color.ToXnaColor());
}
else
{
_ = textureRegion.CreateSlice(slice.Name, slice.Bounds, slice.Origin, slice.Color);
textureRegion.CreateSlice(aseSlice.Name,
aseSlice.Bounds.ToXnaRectangle(),
aseSlice.Origin.ToXnaVector2(),
aseSlice.Color.ToXnaColor());
}
}

return new(rawSprite.Name, textureRegion);
return new Sprite(aseSprite.Name, textureRegion);
}
}
71 changes: 45 additions & 26 deletions source/MonoGame.Aseprite/Sprites/SpriteSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

using System.Diagnostics.CodeAnalysis;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Aseprite.RawTypes;
using MonoGame.Aseprite.Utils;

namespace MonoGame.Aseprite.Sprites;

Expand Down Expand Up @@ -297,38 +297,57 @@ public AnimatedSprite CreateAnimatedSprite(string tagName)

#endregion Animations

/// <summary>
/// Creates a new instance of the <see cref="Sprite"/> class from the given <see cref="RawSprite"/>.
/// </summary>
/// <param name="device">
/// The <see cref="Microsoft.Xna.Framework.Graphics.GraphicsDevice"/> used to create graphical resources.
/// </param>
/// <param name="rawSpriteSheet">
/// The <see cref="RawSpriteSheet"/> to create the <see cref="SpriteSheet"/> from.
/// </param>
/// <returns>
/// The <see cref="SpriteSheet"/> created by this method.
/// </returns>
public static SpriteSheet FromRaw(GraphicsDevice device, RawSpriteSheet rawSpriteSheet)
public static SpriteSheet FromFile(GraphicsDevice device, AseFile file, AseProcessorOptions options)
{
TextureAtlas atlas = TextureAtlas.FromRaw(device, rawSpriteSheet.RawTextureAtlas);
SpriteSheet spriteSheet = new(rawSpriteSheet.Name, atlas);
options ??= AseProcessorOptions.Default;
AseSpriteSheet aseSpriteSheet = AseSpriteSheetProcessor.Process(file, options);
Texture2D texture = aseSpriteSheet.TextureAtlas.Texture.ToTexture2D(device);
TextureAtlas atlas = new TextureAtlas(texture.Name, texture);

for (int i = 0; i < aseSpriteSheet.TextureAtlas.Regions.Length; i++)
{
AseTextureRegion aseTextureRegion = aseSpriteSheet.TextureAtlas.Regions[i];
TextureRegion textureRegion = atlas.CreateRegion(aseTextureRegion.Name, aseTextureRegion.Bounds.ToXnaRectangle());

for (int s = 0; s < aseTextureRegion.Slices.Length; s++)
{
AseSlice aseSlice = aseTextureRegion.Slices[i];

if (aseSlice is AseNinepatchSlice aseNinePatchSlice)
{
textureRegion.CreateNinePatchSlice(aseNinePatchSlice.Name,
aseNinePatchSlice.Bounds.ToXnaRectangle(),
aseNinePatchSlice.CenterBounds.ToXnaRectangle(),
aseNinePatchSlice.Origin.ToXnaVector2(),
aseNinePatchSlice.Color.ToXnaColor());
}
else
{
textureRegion.CreateSlice(aseSlice.Name,
aseSlice.Bounds.ToXnaRectangle(),
aseSlice.Origin.ToXnaVector2(),
aseSlice.Color.ToXnaColor());
}
}
}

SpriteSheet spriteSheet = new SpriteSheet(atlas.Name, atlas);


for (int i = 0; i < rawSpriteSheet.RawAnimationTags.Length; i++)
for (int i = 0; i < aseSpriteSheet.Tags.Length; i++)
{
RawAnimationTag tag = rawSpriteSheet.RawAnimationTags[i];
AseTag aseTag = aseSpriteSheet.Tags[i];

spriteSheet.CreateAnimationTag(tag.Name, builder =>
spriteSheet.CreateAnimationTag(aseTag.Name, builder =>
{
builder.LoopCount(tag.LoopCount)
.IsReversed(tag.IsReversed)
.IsPingPong(tag.IsPingPong);
builder.LoopCount(aseTag.LoopCount)
.IsReversed(aseTag.IsReversed)
.IsPingPong(aseTag.IsPingPong);

for (int j = 0; j < tag.RawAnimationFrames.Length; j++)
for (int j = 0; j < aseTag.Frames.Length; j++)
{
RawAnimationFrame rawAnimationFrame = tag.RawAnimationFrames[j];
TimeSpan duration = TimeSpan.FromMilliseconds(rawAnimationFrame.DurationInMilliseconds);
builder.AddFrame(rawAnimationFrame.FrameIndex, duration);
AseAnimationFrame aseAnimationFrame = aseTag.Frames[j];
builder.AddFrame(aseAnimationFrame.FrameIndex, aseAnimationFrame.Duration);
}
});
}
Expand Down
54 changes: 23 additions & 31 deletions source/MonoGame.Aseprite/Sprites/TextureAtlas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
---------------------------------------------------------------------------- */

using System.Collections;
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Aseprite.RawTypes;
using MonoGame.Aseprite.Utils;


namespace MonoGame.Aseprite.Sprites;

Expand Down Expand Up @@ -545,46 +547,36 @@ public void Clear()
/// </returns>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

/// <summary>
/// Creates a new instance of the <see cref="TextureAtlas"/> class from the given <see cref="RawTextureAtlas"/>.
/// </summary>
/// <param name="device">
/// The <see cref="Microsoft.Xna.Framework.Graphics.GraphicsDevice"/> used to create graphical resources.
/// </param>
/// <param name="rawTextureAtlas">
/// The <see cref="RawTextureAtlas"/> to create the <see cref="TextureAtlas"/> from.
/// </param>
/// <returns>
/// The <see cref="TextureAtlas"/> created by this method.
/// </returns>
public static TextureAtlas FromRaw(GraphicsDevice device, RawTextureAtlas rawTextureAtlas)
public static TextureAtlas FromFile(GraphicsDevice device, AseFile file, AseProcessorOptions options)
{
RawTexture rawTexture = rawTextureAtlas.RawTexture;

Texture2D texture = new(device, rawTexture.Width, rawTexture.Height, mipmap: false, SurfaceFormat.Color);
texture.SetData<Color>(rawTexture.Pixels.ToArray());
texture.Name = rawTexture.Name;

TextureAtlas atlas = new(rawTextureAtlas.Name, texture);

ReadOnlySpan<RawTextureRegion> rawTextureRegions = rawTextureAtlas.RawTextureRegions;
options ??= AseProcessorOptions.Default;
AseTextureAtlas aseTextureAtlas = AseTextureAtlasProcessor.Process(file, options);
Texture2D texture = aseTextureAtlas.Texture.ToTexture2D(device);
TextureAtlas atlas = new TextureAtlas(texture.Name, texture);

for (int i = 0; i < rawTextureRegions.Length; i++)
for(int i = 0; i < aseTextureAtlas.Regions.Length; i++)
{
RawTextureRegion rawTextureRegion = rawTextureRegions[i];
TextureRegion textureRegion = atlas.CreateRegion(rawTextureRegion.Name, rawTextureRegion.Bounds);
AseTextureRegion aseTextureRegion = aseTextureAtlas.Regions[i];
TextureRegion textureRegion = atlas.CreateRegion(aseTextureRegion.Name, aseTextureRegion.Bounds.ToXnaRectangle());

for (int s = 0; s < rawTextureRegion.Slices.Length; s++)
for(int s = 0; s < aseTextureRegion.Slices.Length; s++)
{
RawSlice slice = rawTextureRegion.Slices[s];
AseSlice aseSlice = aseTextureRegion.Slices[i];

if (slice is RawNinePatchSlice ninePatch)
if(aseSlice is AseNinepatchSlice aseNinePatchSlice)
{
_ = textureRegion.CreateNinePatchSlice(ninePatch.Name, ninePatch.Bounds, ninePatch.CenterBounds, ninePatch.Origin, ninePatch.Color);
textureRegion.CreateNinePatchSlice(aseNinePatchSlice.Name,
aseNinePatchSlice.Bounds.ToXnaRectangle(),
aseNinePatchSlice.CenterBounds.ToXnaRectangle(),
aseNinePatchSlice.Origin.ToXnaVector2(),
aseNinePatchSlice.Color.ToXnaColor());
}
else
{
_ = textureRegion.CreateSlice(slice.Name, slice.Bounds, slice.Origin, slice.Color);
textureRegion.CreateSlice(aseSlice.Name,
aseSlice.Bounds.ToXnaRectangle(),
aseSlice.Origin.ToXnaVector2(),
aseSlice.Color.ToXnaColor());
}
}
}
Expand Down
66 changes: 30 additions & 36 deletions source/MonoGame.Aseprite/Tilemaps/AnimatedTilemap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using System.Collections;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Aseprite.RawTypes;
using MonoGame.Aseprite.Utils;


namespace MonoGame.Aseprite.Tilemaps;

Expand Down Expand Up @@ -652,50 +653,43 @@ public void Draw(SpriteBatch spriteBatch, Vector2 position, Color color, Vector2
/// </returns>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

/// <summary>
/// Creates a new instance of the <see cref="AnimatedTilemap"/> class from the given
/// <see cref="RawAnimatedTilemap"/>.
/// </summary>
/// <param name="device">
/// The <see cref="Microsoft.Xna.Framework.Graphics.GraphicsDevice"/> used to create graphical resources.
/// </param>
/// <param name="rawTilemap">
/// The <see cref="RawAnimatedTilemap"/> to create the <see cref="AnimatedTilemap"/> from.
/// </param>
/// <returns>
/// The <see cref="AnimatedTilemap"/> created by this method.
/// </returns>
public static AnimatedTilemap FromRaw(GraphicsDevice device, RawAnimatedTilemap rawTilemap)
public static AnimatedTilemap FromFile(GraphicsDevice device, AseFile file, AseProcessorOptions options)
{
AnimatedTilemap animatedTilemap = new(rawTilemap.Name);

Dictionary<int, Tileset> tilesetLookup = new();
options ??= AseProcessorOptions.Default;
AseAnimatedTilemap aseAnimatedTilemap = AseAnimatedTilemapProcessor.Process(file, options);
AnimatedTilemap animatedTilemap = new AnimatedTilemap(aseAnimatedTilemap.Name);

for (int i = 0; i < rawTilemap.RawTilesets.Length; i++)
Dictionary<int, Tileset> tilesetLookup = new Dictionary<int, Tileset>();
for (int i = 0; i < aseAnimatedTilemap.Tilesets.Length; i++)
{
RawTileset rawTileset = rawTilemap.RawTilesets[i];
Tileset tileset = Tileset.FromRaw(device, rawTileset);
tilesetLookup.Add(rawTileset.ID, tileset);
AseTileset aseTileset = aseAnimatedTilemap.Tilesets[i];
Texture2D texture = aseTileset.Texture.ToTexture2D(device);
Tileset tileset = new Tileset(aseTileset.Name, texture, aseTileset.TileSize.Width, aseTileset.TileSize.Height);
tilesetLookup.Add(aseTileset.ID, tileset);
}

for (int f = 0; f < rawTilemap.RawTilemapFrames.Length; f++)
for(int f = 0; f < aseAnimatedTilemap.Frames.Length; f++)
{
RawTilemapFrame rawFrame = rawTilemap.RawTilemapFrames[f];

TimeSpan duration = TimeSpan.FromMilliseconds(rawFrame.DurationInMilliseconds);
AnimatedTilemapFrame animatedTilemapFrame = animatedTilemap.CreateFrame(duration);
AseTilemapFrame aseTilemapFrame = aseAnimatedTilemap.Frames[f];
AnimatedTilemapFrame animatedTilemapFrame = animatedTilemap.CreateFrame(aseTilemapFrame.Duration);

for (int l = 0; l < rawFrame.RawTilemapLayers.Length; l++)
for(int l = 0; l < aseTilemapFrame.Layers.Length; l++)
{
RawTilemapLayer rawLayer = rawFrame.RawTilemapLayers[l];

TilemapLayer layer = animatedTilemapFrame.CreateLayer(rawLayer.Name, tilesetLookup[rawLayer.TilesetID], rawLayer.Columns, rawLayer.Rows, rawLayer.Offset.ToVector2());

for (int t = 0; t < rawLayer.RawTilemapTiles.Length; t++)
AseTilemapLayer aseTilemapLayer = aseTilemapFrame.Layers[l];
TilemapLayer tilemapLayer = animatedTilemapFrame.CreateLayer(aseTilemapLayer.Name,
tilesetLookup[aseTilemapLayer.TilesetID],
aseTilemapLayer.Columns,
aseTilemapLayer.Rows,
aseTilemapLayer.Offset.ToXnaVector2());

for (int t = 0; t < aseTilemapLayer.Tiles.Length; t++)
{
RawTilemapTile rawTile = rawLayer.RawTilemapTiles[t];

layer.SetTile(t, rawTile.TilesetTileID, rawTile.FlipVertically, rawTile.FlipHorizontally, rawTile.FlipDiagonally);
AseTilemapTile aseTilemapTile = aseTilemapLayer.Tiles[t];
tilemapLayer.SetTile(t,
aseTilemapTile.TilesetTileID,
aseTilemapTile.FlipVertically,
aseTilemapTile.FlipHorizontally,
aseTilemapTile.FlipDiagonally);
}
}
}
Expand Down
Loading

0 comments on commit 834c781

Please sign in to comment.