forked from skn3/monkeyspine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspineskeletonjson.monkey
More file actions
416 lines (334 loc) · 13.7 KB
/
spineskeletonjson.monkey
File metadata and controls
416 lines (334 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
'see license.txt for source licenses
Strict
Import spine
Class SpineSkeletonJson
Const TIMELINE_SCALE:String = "scale"
Const TIMELINE_ROTATE:String = "rotate"
Const TIMELINE_TRANSLATE:String = "translate"
Const TIMELINE_ATTACHMENT:String = "attachment"
Const TIMELINE_COLOR:String = "color"
Const ATTACHMENT_REGION:String = "region"
Const ATTACHMENT_REGION_SEQUENCE:String = "regionSequence"
Private
Field attachmentLoader:SpineAttachmentLoader
Field fileLoader:SpineFileLoader
Public
Field Scale:float
Method New(atlas:SpineAtlas = Null, fileLoader:SpineFileLoader = SpineDefaultFileLoader.instance)
If atlas = Null Throw New SpineArgumentNullException("atlas cannot be null.")
If fileLoader = Null Throw New SpineArgumentNullException("file loader cannot be null.")
'create atlas loader
Self.attachmentLoader = New SpineAtlasAttachmentLoader(atlas)
'save the loader objects
Self.fileLoader = fileLoader
'do some final setup
Scale = 1.0
End
Method New(attachmentLoader:SpineAttachmentLoader, fileLoader:SpineFileLoader = SpineDefaultFileLoader.instance)
If attachmentLoader = Null Throw New SpineArgumentNullException("attachment loader cannot be null.")
If fileLoader = Null Throw New SpineArgumentNullException("file loader cannot be null.")
'save the loader objects
Self.attachmentLoader = attachmentLoader
Self.fileLoader = fileLoader
'do some final setup
Scale = 1.0
End
Method ReadSkeletonData:SpineSkeletonData(path:String)
' --- read skeleton data from json ---
'use the file loader to do teh loading
Local fileStream:= fileLoader.LoadFile(path)
'read
Local skeletonData:SpineSkeletonData = New SpineSkeletonData
skeletonData.Name = SpineExtractFilenameWithoutExtension(path)
Local jsonRoot:= JSONObject(JSONData.ReadJSON(fileStream.ReadAll()))
If jsonRoot = Null Throw New SpineException("Invalid JSON.")
Local jsonGroupArray:JSONArray
Local jsonGroupObject:JSONObject
Local jsonName:String
Local jsonObjectDataItem:JSONDataItem
Local jsonObject:JSONObject
Local jsonItem:JSONDataItem
'bones
Local boneParentData:SpineBoneData
Local boneName:String
Local boneData:SpineBoneData
jsonGroupArray = JSONArray(jsonRoot.GetItem("bones"))
If jsonGroupArray <> Null
'iterate over bone objects
For jsonObjectDataItem = EachIn jsonGroupArray
'convert to correct format
jsonObject = JSONObject(jsonObjectDataItem)
If jsonObject = Null Continue
'process this object
boneParentData = Null
jsonItem = jsonObject.GetItem("parent")
If jsonItem <> Null
boneName = jsonItem.ToString()
boneParentData = skeletonData.FindBone(boneName)
If boneParentData = Null Throw New SpineException("Parent not:bone found: " + boneName)
EndIf
boneData = New SpineBoneData(jsonObject.GetItem("name", ""), boneParentData)
boneData.Length = jsonObject.GetItem("length", 0.0) * Scale
boneData.X = jsonObject.GetItem("x", 0.0) * Scale
boneData.Y = jsonObject.GetItem("y", 0.0) * Scale
boneData.Rotation = jsonObject.GetItem("rotation", 0.0)
boneData.ScaleX = jsonObject.GetItem("scaleX", 1.0)
boneData.ScaleY = jsonObject.GetItem("scaleY", 1.0)
skeletonData.AddBone(boneData)
Next
EndIf
'slots
Local slotName:String
Local slotData:SpineSlotData
Local color:String
jsonGroupArray = JSONArray(jsonRoot.GetItem("slots"))
If jsonGroupArray <> Null
'iterate over bone objects
For jsonObjectDataItem = EachIn jsonGroupArray
'convert to correct format
jsonObject = JSONObject(jsonObjectDataItem)
If jsonObject = Null Continue
'process this object
slotName = jsonObject.GetItem("name", "")
boneName = jsonObject.GetItem("bone","")
boneData = skeletonData.FindBone(boneName)
If boneData = Null Throw New SpineException("SpineSlot not:bone found: " + boneName)
slotData = New SpineSlotData(slotName, boneData)
jsonItem = jsonObject.GetItem("color")
If jsonItem <> Null
color = jsonItem.ToString()
slotData.R = ToColor(color, 0)
slotData.G = ToColor(color, 1)
slotData.B = ToColor(color, 2)
slotData.A = ToColor(color, 3)
EndIf
jsonItem = jsonObject.GetItem("attachment")
If jsonItem <> Null slotData.AttachmentName = jsonItem.ToString()
skeletonData.AddSlot(slotData)
Next
EndIf
'skins
Local skin:SpineSkin
Local skinName:String
Local slotIndex:Int
Local attachment:SpineAttachment
Local attachmentName:String
Local jsonSlot:JSONObject
Local jsonAttachment:JSONObject
'iterate over skins
jsonGroupObject = JSONObject(jsonRoot.GetItem("skins"))
If jsonGroupObject <> Null
For jsonName = EachIn jsonGroupObject.Names()
'get skin from name
jsonObject = JSONObject(jsonGroupObject.GetItem(jsonName))
skin = New SpineSkin(jsonName)
'iterate over slots in skin
For slotName = EachIn jsonObject.Names()
jsonSlot = JSONObject(jsonObject.GetItem(slotName))
slotIndex = skeletonData.FindSlotIndex(slotName)
'iterate over attachments in slot
For attachmentName = EachIn jsonSlot.Names()
jsonAttachment = JSONObject(jsonSlot.GetItem(attachmentName))
attachment = ReadAttachment(skin, attachmentName, jsonAttachment)
skin.AddAttachment(slotIndex, attachmentName, attachment)
Next
Next
skeletonData.AddSkin(skin)
If skin.Name = "default" skeletonData.DefaultSkin = skin
Next
EndIf
'animations.
jsonGroupObject = JSONObject(jsonRoot.GetItem("animations"))
If jsonGroupObject <> Null
For jsonName = EachIn jsonGroupObject.Names()
'get animation from name
jsonObject = JSONObject(jsonGroupObject.GetItem(jsonName))
ReadAnimation(jsonName, jsonObject, skeletonData)
Next
EndIf
skeletonData.TrimArrays()
Return skeletonData
End
Method ReadAttachment:SpineAttachment(skin:SpineSkin, name:String, jsonAttachment:JSONObject)
Local jsonItem:JSONDataItem
jsonItem = jsonAttachment.GetItem("name")
If jsonItem <> Null name = jsonItem.ToString()
Local type:Int = SpineAttachmentType.region
jsonItem = jsonAttachment.GetItem("type")
If jsonItem <> Null type = SpineAttachmentType.FromString(jsonItem.ToString())
Local attachment:SpineAttachment = attachmentLoader.NewAttachment(skin, type, name)
Local regionAttachment:= SpineRegionAttachment(attachment)
If regionAttachment
regionAttachment.X = jsonAttachment.GetItem("x", 0.0) * Scale
regionAttachment.Y = jsonAttachment.GetItem("y", 0.0) * Scale
regionAttachment.ScaleX = jsonAttachment.GetItem("scaleX", 1.0)
regionAttachment.ScaleY = jsonAttachment.GetItem("scaleY", 1.0)
regionAttachment.Rotation = jsonAttachment.GetItem("rotation", 0.0)
regionAttachment.Width = jsonAttachment.GetItem("width", 32.0) * Scale
regionAttachment.Height = jsonAttachment.GetItem("height", 32.0) * Scale
regionAttachment.UpdateOffset()
EndIf
return attachment
End
Function ToColor:float(hex:String, colorIndex:int)
If hex.Length <> 8 Throw New SpineArgumentNullException("Color hexidecimal length must be 8, recieved: " + hex)
Local val:Int = 0
Local offset:Int = colorIndex * 2
hex = hex.ToUpper()
For Local i:Int = offset Until offset + 2
val *=16
If hex[i] >= 48 And hex[i] <= 57
val += (hex[i] - 48)
Else
val += (hex[i] - 55)
Endif
Next
Return val / 255.0
End
Private
Method ReadAnimation:Void(name:String, jsonAnimation:JSONObject, skeletonData:SpineSkeletonData)
Local timelines:SpineTimeline[]
Local timelineCount:Int
Local duration:float = 0.0
Local jsonGroupObject:JSONObject
Local jsonBone:JSONObject
Local jsonTimeline:JSONArray
Local jsonTimelineFrameDataItem:JSONDataItem
Local jsonTimelineFrame:JSONObject
Local boneName:String
Local boneIndex:Int
Local timelineName:String
Local frameIndex:Int
Local timelineScale:float
jsonGroupObject = JSONObject(jsonAnimation.GetItem("bones"))
If jsonGroupObject <> Null
For boneName = EachIn jsonGroupObject.Names()
jsonBone = JSONObject(jsonGroupObject.GetItem(boneName))
If jsonBone = Null Continue
boneIndex = skeletonData.FindBoneIndex(boneName)
If boneIndex = -1 Throw New SpineException("SpineBone not found: " + boneName)
For timelineName = EachIn jsonBone.Names()
jsonTimeline = JSONArray(jsonBone.GetItem(timelineName))
If jsonTimeline = Null Continue
Select timelineName
Case TIMELINE_ROTATE
Local timeline:SpineRotateTimeline = New SpineRotateTimeline(jsonTimeline.values.Count())
timeline.BoneIndex = boneIndex
frameIndex = 0
For jsonTimelineFrameDataItem = EachIn jsonTimeline
'convert to correct object format
jsonTimelineFrame = JSONObject(jsonTimelineFrameDataItem)
'process object
timeline.SetFrame(frameIndex, jsonTimelineFrame.GetItem("time", 0.0), jsonTimelineFrame.GetItem("angle", 0.0))
ReadCurve(timeline, frameIndex, jsonTimelineFrame)
frameIndex += 1
Next
If timelineCount >= timelines.Length timelines = timelines.Resize(timelines.Length * 2 + 10)
timelines[timelineCount] = timeline
timelineCount += 1
duration = Max(duration, timeline.Frames[timeline.FrameCount() * 2 - 2])
Case TIMELINE_TRANSLATE, TIMELINE_SCALE
Local timeline:SpineTranslateTimeline
timelineScale = 1.0
If timelineName = TIMELINE_SCALE
timeline = New SpineScaleTimeline(jsonTimeline.values.Count())
Else
timeline = New SpineTranslateTimeline(jsonTimeline.values.Count())
timelineScale = Scale
EndIf
timeline.BoneIndex = boneIndex
frameIndex = 0
For jsonTimelineFrameDataItem = EachIn jsonTimeline
'convert to correct object format
jsonTimelineFrame = JSONObject(jsonTimelineFrameDataItem)
'process object
timeline.SetFrame(frameIndex, jsonTimelineFrame.GetItem("time", 0.0), jsonTimelineFrame.GetItem("x", 0.0) * timelineScale, jsonTimelineFrame.GetItem("y", 0.0) * timelineScale)
ReadCurve(timeline, frameIndex, jsonTimelineFrame)
frameIndex += 1
Next
If timelineCount >= timelines.Length timelines = timelines.Resize(timelines.Length * 2 + 10)
timelines[timelineCount] = timeline
timelineCount += 1
duration = Max(duration, timeline.Frames[timeline.FrameCount() * 3 - 3])
Default
Throw New SpineException("Invalid type:timeline for a bone: " + timelineName + " (" + boneName + ")")
End
Next
Next
EndIf
'slots
Local slotName:String
Local slotIndex:Int
Local jsonSlot:JSONObject
Local color:string
jsonGroupObject = JSONObject(jsonAnimation.GetItem("slots"))
If jsonGroupObject <> Null
For slotName = EachIn jsonGroupObject.Names()
jsonSlot = JSONObject(jsonGroupObject.GetItem(slotName))
If jsonSlot = Null Continue
slotIndex = skeletonData.FindSlotIndex(slotName)
For timelineName = EachIn jsonSlot.Names()
jsonTimeline = JSONArray(jsonSlot.GetItem(timelineName))
Select timelineName
Case TIMELINE_COLOR
Local timeline:SpineColorTimeline = New SpineColorTimeline(jsonTimeline.values.Count())
timeline.SlotIndex = slotIndex
frameIndex = 0
For jsonTimelineFrameDataItem = EachIn jsonTimeline
'convert to correct object format
jsonTimelineFrame = JSONObject(jsonTimelineFrameDataItem)
'process object
color = jsonTimelineFrame.GetItem("color", "")
timeline.SetFrame(frameIndex, jsonTimelineFrame.GetItem("time", 0.0), ToColor(color, 0), ToColor(color, 1), ToColor(color, 2), ToColor(color, 3))
ReadCurve(timeline, frameIndex, jsonTimelineFrame)
frameIndex += 1
Next
If timelineCount >= timelines.Length timelines = timelines.Resize(timelines.Length * 2 + 10)
timelines[timelineCount] = timeline
timelineCount += 1
duration = Max(duration, timeline.Frames[timeline.FrameCount() * 5 - 5])
Case TIMELINE_ATTACHMENT
Local timeline:SpineAttachmentTimeline = New SpineAttachmentTimeline(jsonTimeline.values.Count())
timeline.SlotIndex = slotIndex
frameIndex = 0
For jsonTimelineFrameDataItem = EachIn jsonTimeline
'convert to correct object format
jsonTimelineFrame = JSONObject(jsonTimelineFrameDataItem)
'process object
timeline.SetFrame(frameIndex, jsonTimelineFrame.GetItem("time", 0.0), jsonTimelineFrame.GetItem("name", ""))
frameIndex += 1
Next
If timelineCount >= timelines.Length timelines = timelines.Resize(timelines.Length * 2 + 10)
timelines[timelineCount] = timeline
timelineCount += 1
duration = Max(duration, timeline.Frames[timeline.FrameCount() -1])
Default
Throw New SpineException("Invalid type:timeline for a slot: " + timelineName + " (" + slotName + ")")
End
Next
Next
EndIf
'trim timeline
If timelineCount < timelines.Length timelines = timelines.Resize(timelineCount)
skeletonData.AddAnimation(new SpineAnimation(name, timelines, duration))
End
Method ReadCurve:Void(timeline:SpineCurveTimeline, frameIndex:int, jsonTimelineFrame:JSONObject)
Local jsonItem:JSONDataItem
jsonItem = jsonTimelineFrame.GetItem("curve")
If jsonItem = Null Return
Local jsonArray:JSONArray = JSONArray(jsonItem)
If jsonArray <> Null
'bezier curve
Local curve:= jsonArray.values.ToArray()
timeline.SetCurve(frameIndex, float(curve[0]), float(curve[1]), float(curve[2]), float(curve[3]))
Else
'named curve
Select jsonItem.ToString()
Case "stepped"
timeline.SetStepped(frameIndex)
Default
'linear
End
EndIf
End
End