Debian Codegraph @

src/scene/load/spine/x3dloadinternalspine_skeleton.inc

raw
1 {%MainUnit x3dloadinternalspine.pas}
2 {
3 Copyright 2014-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Spine skeleton, corresponding to a single Spine JSON file. }
18
19 {$ifdef read_interface}
20 TSkeleton = class
21 Root: TBone;
22 { Bones, in the order specified in JSON file, so it's guaranteed
23 that parent is always specified before any of it's children
24 (this is very comfortable for processing). }
25 Bones: TBoneList;
26 Slots: TSlotList;
27 Skins: TSkinList;
28 Animations: TAnimationList;
29 { When parsing the skeleton, we calculate default and current skin
30 (used to search for attachments). They cannot be @nil.
31 It is possible that CurrentSkin = DefaultSkin.
32 @groupBegin }
33 DefaultSkin: TSkin;
34 CurrentSkin: TSkin;
35 { @groupEnd }
36 constructor Create;
37 destructor Destroy; override;
38 procedure Parse(const BaseUrl: String; const Json: TJSONData; const SkinName: String);
39 procedure BuildNodes(const BaseUrl: String; const TextureLoader: TTextureLoader;
40 const Container: TX3DRootNode);
41 end;
42 {$endif}
43
44 {$ifdef read_implementation}
45 constructor TSkeleton.Create;
46 begin
47 inherited;
48 Bones := TBoneList.Create;
49 Slots := TSlotList.Create;
50 Skins := TSkinList.Create;
51 Animations := TAnimationList.Create;
52 end;
53
54 destructor TSkeleton.Destroy;
55 begin
56 DefaultSkin := nil;
57 FreeAndNil(Skins);
58 FreeAndNil(Slots);
59 Root := nil;
60 FreeAndNil(Bones);
61 FreeAndNil(Animations);
62 inherited;
63 end;
64
65 procedure TSkeleton.Parse(const BaseUrl: String; const Json: TJSONData; const SkinName: String);
66
67 procedure CalculateCurrentSkin;
68 begin
69 CurrentSkin := nil;
70 if SkinName <> '' then
71 CurrentSkin := Skins.Find(SkinName, true);
72 if CurrentSkin = nil then
73 begin
74 { Old version:
75 prefer non-default skin, if exists, since default skin may miss some
76 attachments, see goblins.json example. }
77 // if Skins.Count > 1 then
78 // CurrentSkin := Skins[1] else
79 { New version: use DefaultSkin by default, this is more intuitive
80 and naturally expected
81 (see e.g. hero default/girl skins in "The Unholy Society"). }
82 CurrentSkin := DefaultSkin;
83 end;
84 end;
85
86 procedure SkinLog;
87 var
88 S: string;
89 I: Integer;
90 begin
91 if Skins.Count = 1 then
92 begin
93 Assert(CurrentSkin = Skins[0]);
94 //WritelnLog('Spine', BaseUrl + ' available skins: 1 (' + CurrentSkin.Name + ').');
95 end else
96 begin
97 S := Format('%s available skins: %d', [BaseUrl, Skins.Count]) + NL;
98 for I := 0 to Skins.Count - 1 do
99 S := S + (Format(' Available skin %d: %s', [I, Skins[I].Name]) + NL);
100 S := S + Format('Using skin: %s', [CurrentSkin.Name]);
101 WritelnLogMultiline('Spine', S);
102 end;
103 end;
104
105 { Parse version from String.
106 Leaves unchanged the version components not found in file, or invalid.
107
108 Note: We don't want to ever fail at some weird version number.
109 So this is written even to tolerate weird things like "4-alpha.51a.beta9",
110 it will just return zeros. }
111 procedure ParseVersion(const S: String; var Major, Minor, Release: Integer);
112 var
113 SeekPos: Integer;
114 Token: String;
115 begin
116 SeekPos := 1;
117
118 Token := NextToken(S, SeekPos, ['.']);
119 if Token = '' then Exit;
120 if not TryStrToInt(Token, Major) then Exit;
121
122 Token := NextToken(S, SeekPos, ['.']);
123 if Token = '' then Exit;
124 if not TryStrToInt(Token, Minor) then Exit;
125
126 Token := NextToken(S, SeekPos, ['.']);
127 if Token = '' then Exit;
128 if not TryStrToInt(Token, Release) then Exit;
129 end;
130
131 var
132 JsonSkeleton, O: TJSONObject;
133 JsonArmatureArray: TJSONArray;
134 DragonBonesFormat, NormalizeCurvesKeyValues: boolean;
135 VersionMajor, VersionMinor, VersionRelease: Integer;
136 begin
137 if not (Json is TJSONObject) then
138 raise ESpineReadError.Create('Spine JSON skeleton: Expected JSONObject at root');
139 O := TJSONObject(Json);
140
141 // calculate VersionMajor/Minor/Release and JsonSkeleton
142 VersionMajor := 0;
143 VersionMinor := 0;
144 VersionRelease := 0;
145 JsonSkeleton := O.Find('skeleton', jtObject) as TJSONObject;
146 if JsonSkeleton <> nil then
147 begin
148 ParseVersion(JsonSkeleton.Get('spine', '0.0.0'), VersionMajor, VersionMinor, VersionRelease);
149 // WritelnLog('Spine version parsed: %d.%d.%d', [VersionMajor, VersionMinor, VersionRelease]);
150 end;
151
152 // calculate NormalizeCurvesKeyValues
153 NormalizeCurvesKeyValues := VersionMajor >= 4;
154
155 // calculate DragonBonesFormat
156 DragonBonesFormat :=
157 (JsonSkeleton = nil) and
158 (O.Find('armature', jtArray) <> nil);
159
160 if DragonBonesFormat then
161 begin
162 { In Dragon Bones, almost everything is inside "armature".
163 TODO: For now, we only read 1st armature item. }
164 JsonArmatureArray := O.Find('armature', jtArray) as TJSONArray;
165 if JsonArmatureArray.Count = 0 then
166 raise ESpineReadError.Create('Dragon Bones JSON skeleton: no armatures');
167 O := JsonArmatureArray[0] as TJSONObject;
168 end;
169
170 Bones.Parse(O, Root, DragonBonesFormat);
171 Slots.Parse(O, Bones, DragonBonesFormat);
172 Skins.Parse(O, DefaultSkin, Bones, DragonBonesFormat);
173
174 CalculateCurrentSkin; // Animations.Parse below requires CurrentSkin initialized
175 // This is possible in case of empty Spine file
176 if CurrentSkin = nil then
177 begin
178 Assert(DefaultSkin = nil);
179 Exit;
180 end;
181 SkinLog;
182
183 Animations.Parse(O, Bones, Slots, CurrentSkin, NormalizeCurvesKeyValues);
184
185 // WritelnLog('Spine', Format('Skeleton read, bones: %d, slots: %d, skins: %d, animations: %d',
186 // [Bones.Count, Slots.Count, Skins.Count, Animations.Count]));
187 end;
188
189 procedure TSkeleton.BuildNodes(const BaseUrl: String; const TextureLoader: TTextureLoader;
190 const Container: TX3DRootNode);
191 var
192 NavigationInfo: TNavigationInfoNode;
193 begin
194 // This is possible in case of empty Spine
195 if DefaultSkin = nil then Exit;
196
197 { add NavigationInfo indicating blendingSort = 2D }
198 NavigationInfo := TNavigationInfoNode.Create('', BaseUrl);
199 NavigationInfo.BlendingSort := sort2D;
200 Container.AddChildren(NavigationInfo);
201
202 Bones.BuildNodes(BaseUrl);
203 Root.NodeUsedAsChild := true;
204 Container.AddChildren(Root.Node);
205 Skins.BuildNodes(BaseUrl, TextureLoader);
206 Slots.BuildNodes(BaseUrl, CurrentSkin.Attachments, DefaultSkin.Attachments);
207 Animations.BuildNodes(BaseUrl, Container);
208 end;
209 {$endif}