Advertisement
ZeronSix

Yoba atlas parser

Apr 5th, 2014
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using Newtonsoft.Json;
  5.  
  6. namespace Gamiron7 {
  7.     struct JsonSize {
  8.         [JsonProperty( "w" )]
  9.         public int W { get; set; }
  10.         [JsonProperty( "h" )]
  11.         public int H { get; set; }
  12.     }
  13.  
  14.     struct JsonRect {
  15.         [JsonProperty( "x" )]
  16.         public int X { get; set; }
  17.         [JsonProperty( "y" )]
  18.         public int Y { get; set; }
  19.         [JsonProperty( "w" )]
  20.         public int W { get; set; }
  21.         [JsonProperty( "h" )]
  22.         public int H { get; set; }
  23.     }
  24.  
  25.     struct AtlasFrame {
  26.         [JsonProperty("filename")]
  27.         public string FileName { get; set; }
  28.  
  29.         [JsonProperty("frame")]
  30.         public JsonRect Frame { get; set; }
  31.  
  32.         [JsonProperty( "rotated" )]
  33.         public bool Rotated { get; set; }
  34.  
  35.         [JsonProperty( "trimmed" )]
  36.         public bool Trimmed { get; set; }
  37.  
  38.         [JsonProperty( "spriteSourceSize" )]
  39.         public JsonRect SpriteSourceSize { get; set; }
  40.  
  41.         [JsonProperty( "sourceSize" )]
  42.         public JsonSize SourceSize { get; set; }
  43.     }
  44.  
  45.     class TextureAtlas {
  46.         [JsonProperty( "frames" )]
  47.         public IList<AtlasFrame> Frames { get; set; }
  48.  
  49.         public AtlasFrame GetFrame( string name ) {
  50.             foreach ( var f in Frames ) {
  51.                 if ( f.FileName == name ) {
  52.                     return f;
  53.                 }
  54.             }
  55.  
  56.             throw new Exception( "There is no frame named " + name + " in this atlas!");
  57.         }
  58.  
  59.         public AtlasFrame this[string frame] {
  60.             get {
  61.                 return GetFrame( frame );
  62.             }
  63.         }
  64.  
  65.         public static TextureAtlas LoadFromFile( string fileName ) {
  66.             return JsonConvert.DeserializeObject<TextureAtlas>( File.ReadAllText( fileName ) );
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement