Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added AsepriteDotNet #1387

Merged
merged 9 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,7 @@ public void GetBottomLeftWorldCoordinateForOrderedTile(int orderedTileIndex, out

/// <summary>
/// Returns the quad index at the argument worldX and worldY. Returns null if no quad is found at this index.
/// If the map has a SortAxis of X or Y, then this funcion uses the sorting to find the quad more quickly.
/// </summary>
/// <param name="worldX">The absolute world X position.</param>
/// <param name="worldY">The absolute world Y position.</param>
Expand Down
3 changes: 3 additions & 0 deletions Engines/FlatRedBallXNA/FlatRedBall.FNA/FlatRedBall.FNA.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<Version>1.0.0</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AsepriteDotNet" Version="1.7.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\3rd Party Libraries\FNA\FNA.Core.csproj">
<ReferenceOutputAssembly>true</ReferenceOutputAssembly>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#if NET6_0_OR_GREATER
using AsepriteDotNet.Aseprite;
using AsepriteDotNet.Processors;
using FlatRedBall.Graphics.Animation;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Text;
using AseSpriteSheet = AsepriteDotNet.SpriteSheet;
using AseTexture = AsepriteDotNet.Texture;
using AseTag = AsepriteDotNet.AnimationTag;
using AseAnimationFrame = AsepriteDotNet.AnimationFrame;
using AseRect = AsepriteDotNet.Common.Rectangle;
using FlatRedBall.Content.AnimationChain;
using AsepriteDotNet;

namespace FlatRedBall.Content.Aseprite
{
public static class AsepriteFileExtensions
{
public static AnimationChainList ToAnimationChainList(this AsepriteFile file)
{

// Generate the Aseprite Spritesheet
AseSpriteSheet spriteSheet = SpriteSheetProcessor.Process(file);
AseTexture aseTexture = spriteSheet.TextureAtlas.Texture;

// Create the MonoGame Texture2D from the Aseprite Spritesheet
Texture2D texture = new Texture2D(FlatRedBallServices.GraphicsDevice, aseTexture.Size.Width, aseTexture.Size.Height);
texture.SetData(aseTexture.Pixels.ToArray());

// Create an AnimationChainList based on the Aseprite spritesheet
AnimationChainList list = new AnimationChainList(spriteSheet.Tags.Length);
for (int i = 0; i < spriteSheet.Tags.Length; i++)
{
AseTag tag = spriteSheet.Tags[i];
Graphics.Animation.AnimationChain chain = new Graphics.Animation.AnimationChain(tag.Frames.Length);
for (int j = 0; j < tag.Frames.Length; j++)
{
AseAnimationFrame aseFrame = tag.Frames[j];
var frame = new Graphics.Animation.AnimationFrame(texture, (float)aseFrame.Duration.TotalSeconds);

AseRect bounds = spriteSheet.TextureAtlas.Regions[aseFrame.FrameIndex].Bounds;
frame.TopCoordinate = bounds.Location.Y / (float)texture.Height;
frame.LeftCoordinate = bounds.Location.X / (float)texture.Width;
frame.BottomCoordinate = frame.TopCoordinate + (bounds.Size.Height / (float)texture.Height);
frame.RightCoordinate = frame.LeftCoordinate + (bounds.Size.Width / (float)texture.Width);

chain.Add(frame);
}
chain.Name = tag.Name;
list.Add(chain);
}

return list;
}

public static AnimationChainListSave ToAnimationChainListSave(this AsepriteFile file)
{
AseSpriteSheet spriteSheet = SpriteSheetProcessor.Process(file);

AnimationChainListSave list = new AnimationChainListSave();
AseTexture aseTexture = spriteSheet.TextureAtlas.Texture;
var width = aseTexture.Size.Width;
var height = aseTexture.Size.Height;
for (int i = 0; i < spriteSheet.Tags.Length; i++)
{
AseTag tag = spriteSheet.Tags[i];

var chain = new AnimationChainSave();

for (int j = 0; j < tag.Frames.Length; j++)
{
AseAnimationFrame aseFrame = tag.Frames[j];
AnimationFrameSave animationFrameSave = new AnimationFrameSave();


AseRect bounds = spriteSheet.TextureAtlas.Regions[aseFrame.FrameIndex].Bounds;
animationFrameSave.TopCoordinate = bounds.Location.Y / (float)width;
animationFrameSave.LeftCoordinate = bounds.Location.X / (float)width;
animationFrameSave.BottomCoordinate = animationFrameSave.TopCoordinate + (bounds.Size.Height / (float)height);
animationFrameSave.RightCoordinate = animationFrameSave.LeftCoordinate + (bounds.Size.Width / (float)height);
}

chain.Name = tag.Name;
list.AnimationChains.Add(chain);
}
return list;
}

}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#if NET6_0_OR_GREATER

using AsepriteDotNet.Aseprite;
using AsepriteDotNet.IO;
using AsepriteDotNet.Processors;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.IO;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace FlatRedBall.Content.ContentLoaders
{
public static class AsepriteFileLoader
{
public static AsepriteFile Load(string absoluteFileName)
{
// Load the aseprite file
AsepriteFile aseFile;
using (Stream titleStream = FileManager.GetStreamForFile(absoluteFileName))
{
string name = Path.GetFileNameWithoutExtension(absoluteFileName);
aseFile = AsepriteDotNet.IO.AsepriteFileLoader.FromStream(name, titleStream);
}

return aseFile;
}
}
}
#endif
12 changes: 11 additions & 1 deletion Engines/FlatRedBallXNA/FlatRedBall/Content/ContentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
using FlatRedBall.Performance.Measurement;
using FlatRedBall.IO;

#if NET6_0_OR_GREATER
using FlatRedBall.Content.Aseprite;
#endif

namespace FlatRedBall.Content
{
//public delegate void UnloadMethod();
Expand Down Expand Up @@ -670,8 +674,14 @@ public T LoadFromFile<T>(string assetName)
//acl[0].ParentGifFileName = assetName;
//loadedAsset = acl;
}
else
#if NET6_0_OR_GREATER
else if(assetName.EndsWith("aseprite") || assetName.EndsWith("ase"))
{
loadedAsset = AsepriteFileLoader.Load(assetName).ToAnimationChainList();
}
#endif
else
{
loadedAsset =
AnimationChainListSave.FromFile(assetName).ToAnimationChainList(mName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
<Compile Include="$(MSBuildThisFileDirectory)Content\AnimationChain\AnimationChainListSave.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Content\AnimationChain\AnimationChainSave.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Content\AnimationChain\AnimationFrameSave.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Content\Aseprite\AsepriteFile.Extensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Content\ContentLoaders\AsepriteFileLoader.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Content\ContentLoaders\SongLoader.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Content\ContentLoaders\TextureContentLoader.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Content\ContentManager.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AsepriteDotNet" Version="1.7.4" />
<PackageReference Include="MonoGame.Framework.Android" Version="3.8.1.303" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Import Project="..\FlatRedBall\FlatRedBallShared.projitems" Label="Shared" />

<ItemGroup>
<PackageReference Include="AsepriteDotNet" Version="1.7.4" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AsepriteDotNet" Version="1.7.4" />
<PackageReference Include="MonoGame.Framework.iOS" Version="3.8.1.303" />
</ItemGroup>

Expand Down
57 changes: 57 additions & 0 deletions FRBDK/Glue/Glue/Content/Aseprite/AsepriteAnimationChainLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using AsepriteDotNet.Aseprite;
using AsepriteDotNet.IO;
using AsepriteDotNet.Processors;
using FlatRedBall.Content.AnimationChain;
using FlatRedBall.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlatRedBall.Glue.Content.Aseprite
{
public class AsepriteAnimationChainLoader
{
public static AnimationChainListSave ToAnimationChainListSave(FilePath filePath)
{
var asepriteFile = AsepriteFileLoader.FromFile(filePath.FullPath);
return ToAnimationChainListSave(asepriteFile);
}
private static AnimationChainListSave ToAnimationChainListSave(AsepriteFile file)
{
var spriteSheet = SpriteSheetProcessor.Process(file);

AnimationChainListSave list = new AnimationChainListSave();
var aseTexture = spriteSheet.TextureAtlas.Texture;
var width = aseTexture.Size.Width;
var height = aseTexture.Size.Height;
for (int i = 0; i < spriteSheet.Tags.Length; i++)
{
var tag = spriteSheet.Tags[i];

var chain = new AnimationChainSave();

for (int j = 0; j < tag.Frames.Length; j++)
{
var aseFrame = tag.Frames[j];
AnimationFrameSave animationFrameSave = new AnimationFrameSave();


var bounds = spriteSheet.TextureAtlas.Regions[aseFrame.FrameIndex].Bounds;
animationFrameSave.TopCoordinate = bounds.Location.Y / (float)width;
animationFrameSave.LeftCoordinate = bounds.Location.X / (float)width;
animationFrameSave.BottomCoordinate = animationFrameSave.TopCoordinate + (bounds.Size.Height / (float)height);
animationFrameSave.RightCoordinate = animationFrameSave.LeftCoordinate + (bounds.Size.Width / (float)height);
}

chain.Name = tag.Name;
list.AnimationChains.Add(chain);
}
return list;
}



}
}
3 changes: 2 additions & 1 deletion FRBDK/Glue/Glue/EditorData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
using FlatRedBall.Content.SpriteFrame;
using GluePropertyGridClasses.StringConverters;
using FlatRedBall.Glue.Plugins.ExportedImplementations;
using AsepriteDotNet.Aseprite;
using AsepriteDotNet.Processors;
using System.Formats.Asn1;
using FlatRedBall.IO;
using AsepriteDotNet.IO;
using FlatRedBall.Glue.Content.Aseprite;

namespace FlatRedBall.Glue.GuiDisplay
{
Expand Down Expand Up @@ -216,12 +222,19 @@ private static AnimationChainListSave LoadAnimationChainListSave(IElement elemen

if (rfs != null)
{
string fullFileName = GlueState.Self.ContentDirectory + rfs.Name;
FilePath filePath = GlueState.Self.ContentDirectory + rfs.Name;

if (System.IO.File.Exists(fullFileName))
if (filePath.Exists())
{
acls = AnimationChainListSave.FromFile(
fullFileName);
var extension = filePath.Extension;
if(extension == "aseprite")
{
acls = AsepriteAnimationChainLoader.ToAnimationChainListSave(filePath);
}
else
{
acls = AnimationChainListSave.FromFile(filePath.FullPath);
}
}
}
return acls;
Expand Down Expand Up @@ -279,6 +292,12 @@ public override StandardValuesCollection
StandardValuesCollection svc = new StandardValuesCollection(mAvailableChains);

return svc;
}
}






}
}
1 change: 1 addition & 0 deletions FRBDK/Glue/Glue/GlueFormsCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AsepriteDotNet" Version="1.7.4" />
<PackageReference Include="CompareNETObjects" Version="4.72.0" />
<PackageReference Include="DotNetZip" Version="1.16.0" />
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.5.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FlatRedBall.Content.AnimationChain;
using FlatRedBall.Glue.Content.Aseprite;
using FlatRedBall.Glue.Errors;
using FlatRedBall.Glue.Plugins.ExportedImplementations;
using FlatRedBall.Glue.SaveClasses;
Expand Down Expand Up @@ -41,16 +42,25 @@ void AddBadReferencesFrom(GlueElement glueElement)

if(rfs != null)
{
var fullFile = GlueCommands.Self.GetAbsoluteFileName(rfs);
var filePath = GlueCommands.Self.GetAbsoluteFilePath(rfs);

if(System.IO.File.Exists( fullFile))
if(filePath.Exists())
{
AnimationChainListSave achSave = AnimationChainListSave.FromFile(fullFile);
AnimationChainListSave achSave = null;

if(filePath.Extension == "aseprite")
{
achSave = AsepriteAnimationChainLoader.ToAnimationChainListSave(filePath);
}
else
{
achSave = AnimationChainListSave.FromFile(filePath.FullPath);
}

if(achSave.AnimationChains.Any(item => item.Name == animationChainName) == false)
{
var error = new AnimationReferenceErrorViewModel(
fullFile, namedObject, animationChainName);
filePath.FullPath, namedObject, animationChainName);

errors.Add(error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public override void StartUp()
this.AddErrorReporter(new AnimationChainErrorReporter());

AchxManager.Initialize(this);

}

private void AssignEvents()
Expand All @@ -56,7 +55,22 @@ private void AssignEvents()
this.ReactToNamedObjectChangedValue += NamedObjectVariableChangeLogic.HandleNamedObjectChangedValue;
this.TryHandleTreeNodeDoubleClicked += TryHandleDoubleClick;
this.ReactToItemSelectHandler += HandleTreeViewItemSelected;
this.ReactToLoadedGluxEarly += HandleLoadedGluxEarly;
this.ReactToUnloadedGlux += HandleUnloadedGlux;
}

private void HandleLoadedGluxEarly()
{
var ati = AssetTypeInfoManager.Self.TryGetAsepriteAti();
if(ati != null)
{
base.AddAssetTypeInfo(ati);
}
}

private void HandleUnloadedGlux()
{
base.UnregisterAssetTypeInfos();
}

private bool TryHandleDoubleClick(ITreeNode tree)
Expand Down
Loading