Advertisement
manbeardgames

AsepriteModelProcessor

Sep 26th, 2018
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.40 KB | None | 0 0
  1. using Microsoft.Xna.Framework.Content.Pipeline;
  2. using Psiren.Asperite.Models;
  3.  
  4. using System.Collections.Generic;
  5.  
  6.  
  7. using TInput = System.String;
  8. using TOutput = System.String;
  9.  
  10. namespace Psiren.ContentPipelineExtensions.AsepriteModel
  11. {
  12.     [ContentProcessor(DisplayName = "Aseprite Definition Processor")]
  13.     public class AsepriteModelProcessor : ContentProcessor<TInput, TOutput>
  14.     {
  15.         public override TOutput Process(TInput input, ContentProcessorContext context)
  16.         {
  17.             //  Deserialize the json
  18.             DeserializeModel dmodel = Newtonsoft.Json.JsonConvert.DeserializeObject<DeserializeModel>(input);
  19.  
  20.             //  Create a dictionary of the animations
  21.             Dictionary<string, AnimationDefinition> animations = new Dictionary<string, AnimationDefinition>();
  22.  
  23.             //  Add the animation definitions
  24.             foreach(var def in dmodel.meta.frameTags)
  25.             {
  26.                 AnimationDefinition definition = new AnimationDefinition(def.name, def.from, def.to, Asperite.AnimationDirection.forward, true);
  27.  
  28.                 animations.Add(definition.Name, definition);
  29.             }
  30.  
  31.             //  Create the frame definition array
  32.             FrameDefinition[] frames = new FrameDefinition[dmodel.frames.Length];
  33.  
  34.             //  Add the frame definitions
  35.             for (int i = 0; i < dmodel.frames.Length; i++)
  36.             {
  37.                 FrameDefinition definition = new FrameDefinition()
  38.                 {
  39.                     FileName = dmodel.frames[i].filename,
  40.                     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),
  41.                     Rotated = dmodel.frames[i].rotated,
  42.                     Trimmed = dmodel.frames[i].trimmed,
  43.                     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),
  44.                     SourceSize = new Util.Size(dmodel.frames[i].sourceSize.w, dmodel.frames[i].sourceSize.h),
  45.                     Duration = dmodel.frames[i].duration / 1000.0f
  46.                 };
  47.  
  48.                 frames[i] = definition;
  49.             }
  50.  
  51.             //  Create an AsepriteModel from the model
  52.             Psiren.Asperite.Models.AsepriteModel model = new Asperite.Models.AsepriteModel()
  53.             {
  54.                 Animations = animations,
  55.                 Frames = frames
  56.             };
  57.  
  58.             return Newtonsoft.Json.JsonConvert.SerializeObject(model);
  59.            
  60.         }
  61.  
  62.         private struct DeserializeModel
  63.         {
  64.             //  Frames
  65.             public DeserializeFrameModel[] frames { get; set; }
  66.  
  67.             //  Meta
  68.             public DeserializeMetaModel meta { get; set; }
  69.  
  70.            
  71.         }
  72.  
  73.         private struct DeserializeFrameModel
  74.         {
  75.             public string filename { get; set; }
  76.             public DeserializeRectangleModel frame { get; set; }
  77.             public bool rotated { get; set; }
  78.             public bool trimmed { get; set; }
  79.             public DeserializeRectangleModel spriteSourceSize { get; set; }
  80.             public DeserializeSizeModel sourceSize { get; set; }
  81.             public int duration { get; set; }
  82.         }
  83.  
  84.         private struct DeserializeMetaModel
  85.         {
  86.             public string app { get; set; }
  87.             public string version { get; set; }
  88.             public string image { get; set; }
  89.             public string format { get; set; }
  90.             public DeserializeSizeModel size { get; set; }
  91.             public string scale { get; set; }
  92.             public DeserializeAnimationModel[] frameTags { get; set; }
  93.         }
  94.  
  95.         private struct DeserializeAnimationModel
  96.         {
  97.             public string name { get; set; }
  98.             public int from { get; set; }
  99.             public int to { get; set; }
  100.             public string direction { get; set; }
  101.         }
  102.  
  103.         private struct DeserializeSizeModel
  104.         {
  105.             public int w { get; set; }
  106.             public int h { get; set; }
  107.         }
  108.  
  109.         private struct DeserializeRectangleModel
  110.         {
  111.             public int x { get; set; }
  112.             public int y { get; set; }
  113.             public int w { get; set; }
  114.             public int h { get; set; }
  115.         }
  116.  
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement