Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework.Content.Pipeline;
- using Psiren.Asperite.Models;
- using System.Collections.Generic;
- using TInput = System.String;
- using TOutput = System.String;
- namespace Psiren.ContentPipelineExtensions.AsepriteModel
- {
- [ContentProcessor(DisplayName = "Aseprite Definition Processor")]
- public class AsepriteModelProcessor : ContentProcessor<TInput, TOutput>
- {
- public override TOutput Process(TInput input, ContentProcessorContext context)
- {
- // Deserialize the json
- DeserializeModel dmodel = Newtonsoft.Json.JsonConvert.DeserializeObject<DeserializeModel>(input);
- // Create a dictionary of the animations
- Dictionary<string, AnimationDefinition> animations = new Dictionary<string, AnimationDefinition>();
- // Add the animation definitions
- foreach(var def in dmodel.meta.frameTags)
- {
- AnimationDefinition definition = new AnimationDefinition(def.name, def.from, def.to, Asperite.AnimationDirection.forward, true);
- animations.Add(definition.Name, definition);
- }
- // Create the frame definition array
- FrameDefinition[] frames = new FrameDefinition[dmodel.frames.Length];
- // Add the frame definitions
- for (int i = 0; i < dmodel.frames.Length; i++)
- {
- FrameDefinition definition = new FrameDefinition()
- {
- FileName = dmodel.frames[i].filename,
- Frame = new Microsoft.Xna.Framework.Rectangle(dmodel.frames[i].frame.x, dmodel.frames[i].frame.y, dmodel.frames[i].frame.w, dmodel.frames[i].frame.h),
- Rotated = dmodel.frames[i].rotated,
- Trimmed = dmodel.frames[i].trimmed,
- SpriteSourceSize = new Microsoft.Xna.Framework.Rectangle(dmodel.frames[i].spriteSourceSize.x, dmodel.frames[i].spriteSourceSize.y, dmodel.frames[i].spriteSourceSize.w, dmodel.frames[i].spriteSourceSize.h),
- SourceSize = new Util.Size(dmodel.frames[i].sourceSize.w, dmodel.frames[i].sourceSize.h),
- Duration = dmodel.frames[i].duration / 1000.0f
- };
- frames[i] = definition;
- }
- // Create an AsepriteModel from the model
- Psiren.Asperite.Models.AsepriteModel model = new Asperite.Models.AsepriteModel()
- {
- Animations = animations,
- Frames = frames
- };
- return Newtonsoft.Json.JsonConvert.SerializeObject(model);
- }
- private struct DeserializeModel
- {
- // Frames
- public DeserializeFrameModel[] frames { get; set; }
- // Meta
- public DeserializeMetaModel meta { get; set; }
- }
- private struct DeserializeFrameModel
- {
- public string filename { get; set; }
- public DeserializeRectangleModel frame { get; set; }
- public bool rotated { get; set; }
- public bool trimmed { get; set; }
- public DeserializeRectangleModel spriteSourceSize { get; set; }
- public DeserializeSizeModel sourceSize { get; set; }
- public int duration { get; set; }
- }
- private struct DeserializeMetaModel
- {
- public string app { get; set; }
- public string version { get; set; }
- public string image { get; set; }
- public string format { get; set; }
- public DeserializeSizeModel size { get; set; }
- public string scale { get; set; }
- public DeserializeAnimationModel[] frameTags { get; set; }
- }
- private struct DeserializeAnimationModel
- {
- public string name { get; set; }
- public int from { get; set; }
- public int to { get; set; }
- public string direction { get; set; }
- }
- private struct DeserializeSizeModel
- {
- public int w { get; set; }
- public int h { get; set; }
- }
- private struct DeserializeRectangleModel
- {
- public int x { get; set; }
- public int y { get; set; }
- public int w { get; set; }
- public int h { get; set; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement