3using Newtonsoft.Json.Linq;
11internal sealed class FrameJsonConverter : JsonConverter<Frame>
19 public override void WriteJson(JsonWriter writer,
21 JsonSerializer serializer)
29 writer.WriteStartObject();
31 writer.WritePropertyName(
"tilesheet");
34 writer.WritePropertyName(
"regionName");
37 writer.WritePropertyName(
"xTile");
38 writer.WriteValue(value.
XTile);
40 writer.WritePropertyName(
"yTile");
41 writer.WriteValue(value.
YTile);
43 writer.WriteEndObject();
55 public override Frame ReadJson(JsonReader reader,
58 bool hasExistingValue,
59 JsonSerializer serializer)
61 if (reader.TokenType == JsonToken.Null)
64 var obj = JObject.Load(reader);
66 var tilesheetName = obj.Value<
string>(
"tilesheet");
67 var regionName = obj.Value<
string>(
"regionName") ?? TilesheetRegion.DefaultRegionName;
69 var xTile = obj.Value<
int?>(
"xTile") ?? 0;
70 var yTile = obj.Value<
int?>(
"yTile") ?? 0;
72 if (
string.IsNullOrWhiteSpace(tilesheetName))
73 throw new JsonSerializationException(
"Frame is missing required tilesheet name.");
75 var tilesheet = TilesheetRegistry.Instance.GetOrNull(tilesheetName);
77 if (tilesheet is
null)
79 throw new JsonSerializationException(
80 $
"Could not resolve Tilesheet '{tilesheetName}' while deserializing Frame.");
83 return new Frame(tilesheet, regionName, xTile, yTile);
string Name
Gets the name.
readonly int YTile
The vertical tile coordinate (row index) within the tilesheet.
readonly Tilesheet Tilesheet
The tilesheet that contains the source bitmap for this frame.
readonly string RegionName
The tilesheet region that contains the source bitmap for this frame.
readonly int XTile
The horizontal tile coordinate (column index) within the tilesheet.