| 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 skins. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
TSkin = class |
| 21 |
Attachments: TAttachmentList; |
| 22 |
Name: string; |
| 23 |
constructor Create; |
| 24 |
destructor Destroy; override; |
| 25 |
procedure Parse(const Json: TJSONObject; const Bones: TBoneList; |
| 26 |
const DragonBonesFormat: boolean); |
| 27 |
procedure BuildNodes(const BaseUrl: String; const TextureLoader: TTextureLoader); |
| 28 |
end; |
| 29 |
|
| 30 |
TSkinList = class({$ifdef FPC}specialize{$endif} TObjectList<TSkin>) |
| 31 |
{ Find by name. |
| 32 |
@raises ESpineReadError If does not exist and NilOnError = @false. } |
| 33 |
function Find(const Name: string; const NilOnError: boolean): TSkin; |
| 34 |
procedure Parse(const Json: TJSONObject; var DefaultSkin: TSkin; const Bones: TBoneList; |
| 35 |
const DragonBonesFormat: boolean); |
| 36 |
procedure BuildNodes(const BaseUrl: String; const TextureLoader: TTextureLoader); |
| 37 |
end; |
| 38 |
{$endif} |
| 39 |
|
| 40 |
{$ifdef read_implementation} |
| 41 |
|
| 42 |
{ TSkin ---------------------------------------------------------------------- } |
| 43 |
|
| 44 |
constructor TSkin.Create; |
| 45 |
begin |
| 46 |
inherited; |
| 47 |
Attachments := TAttachmentList.Create; |
| 48 |
end; |
| 49 |
|
| 50 |
destructor TSkin.Destroy; |
| 51 |
begin |
| 52 |
FreeAndNil(Attachments); |
| 53 |
inherited; |
| 54 |
end; |
| 55 |
|
| 56 |
procedure TSkin.Parse(const Json: TJSONObject; const Bones: TBoneList; |
| 57 |
const DragonBonesFormat: boolean); |
| 58 |
|
| 59 |
procedure ParseSlotMap(const Json: TJSONObject; const SlotName: string); |
| 60 |
var |
| 61 |
I: Integer; |
| 62 |
Attachment: TAttachment; |
| 63 |
begin |
| 64 |
for I := 0 to Json.Count - 1 do |
| 65 |
if Json.Items[I] is TJSONObject then |
| 66 |
begin |
| 67 |
Attachment := TAttachment.CreateAndParse( |
| 68 |
TJSONObject(Json.Items[I]), SlotName, Json.Names[I], Bones); |
| 69 |
if Attachment <> nil then |
| 70 |
Attachments.Add(Attachment); |
| 71 |
end; |
| 72 |
end; |
| 73 |
|
| 74 |
var |
| 75 |
I: Integer; |
| 76 |
begin |
| 77 |
{ TODO: this does not read skin in Dragon Bones version now. } |
| 78 |
|
| 79 |
for I := 0 to Json.Count - 1 do |
| 80 |
if Json.Items[I] is TJSONObject then |
| 81 |
ParseSlotMap(TJSONObject(Json.Items[I]), Json.Names[I]); |
| 82 |
end; |
| 83 |
|
| 84 |
procedure TSkin.BuildNodes(const BaseUrl: String; const TextureLoader: TTextureLoader); |
| 85 |
var |
| 86 |
I: Integer; |
| 87 |
begin |
| 88 |
for I := 0 to Attachments.Count - 1 do |
| 89 |
Attachments[I].BuildNodes(BaseUrl, TextureLoader); |
| 90 |
end; |
| 91 |
|
| 92 |
{ TSkinList ------------------------------------------------------------------ } |
| 93 |
|
| 94 |
function TSkinList.Find(const Name: string; const NilOnError: boolean): TSkin; |
| 95 |
var |
| 96 |
I: Integer; |
| 97 |
begin |
| 98 |
for I := 0 to Count - 1 do |
| 99 |
if Items[I].Name = Name then |
| 100 |
Exit(Items[I]); |
| 101 |
if NilOnError then |
| 102 |
begin |
| 103 |
WritelnWarning('Spine', Format('Skin name "%s" not found', [Name])); |
| 104 |
Result := nil; |
| 105 |
end else |
| 106 |
raise ESpineReadError.CreateFmt('Skin name "%s" not found', [Name]); |
| 107 |
end; |
| 108 |
|
| 109 |
procedure TSkinList.Parse(const Json: TJSONObject; var DefaultSkin: TSkin; const Bones: TBoneList; |
| 110 |
const DragonBonesFormat: boolean); |
| 111 |
|
| 112 |
procedure ParseSpine; |
| 113 |
var |
| 114 |
I: Integer; |
| 115 |
Skin: TSkin; |
| 116 |
OneSkinObject, SkinsObject: TJSONObject; |
| 117 |
SkinsData: TJSONData; |
| 118 |
SkinsArray: TJSONArray; |
| 119 |
begin |
| 120 |
SkinsData := Json.Find('skins'); |
| 121 |
if SkinsData = nil then |
| 122 |
begin |
| 123 |
WritelnWarning('Spine JSON skeleton', 'Missing "skins" data, Spine JSON is empty'); |
| 124 |
Exit; |
| 125 |
end; |
| 126 |
|
| 127 |
if SkinsData is TJSONObject then |
| 128 |
begin |
| 129 |
// older Spine (at least <= 3.6.53) has "skins" as object, with skin names as keys |
| 130 |
SkinsObject := TJSONObject(SkinsData); |
| 131 |
|
| 132 |
for I := 0 to SkinsObject.Count - 1 do |
| 133 |
if SkinsObject.Items[I] is TJSONObject then |
| 134 |
begin |
| 135 |
OneSkinObject := TJSONObject(SkinsObject.Items[I]); |
| 136 |
Skin := TSkin.Create; |
| 137 |
Add(Skin); |
| 138 |
Skin.Name := SkinsObject.Names[I]; |
| 139 |
Skin.Parse(OneSkinObject, Bones, DragonBonesFormat); |
| 140 |
|
| 141 |
if Skin.Name = 'default' then |
| 142 |
DefaultSkin := Skin; |
| 143 |
end; |
| 144 |
end else |
| 145 |
if SkinsData is TJSONArray then |
| 146 |
begin |
| 147 |
// newer Spine (at least >= 3.8.28-beta) has "skins" as array, quite like Dragon Bones |
| 148 |
SkinsArray := TJSONArray(SkinsData); |
| 149 |
|
| 150 |
for I := 0 to SkinsArray.Count - 1 do |
| 151 |
begin |
| 152 |
OneSkinObject := SkinsArray[I] as TJSONObject; |
| 153 |
Skin := TSkin.Create; |
| 154 |
Add(Skin); |
| 155 |
Skin.Name := OneSkinObject.Get('name', ''); |
| 156 |
|
| 157 |
// Skin.Parse expects object with attachments inside |
| 158 |
OneSkinObject := OneSkinObject.Find('attachments', jtObject) as TJSONObject; |
| 159 |
if OneSkinObject = nil then |
| 160 |
begin |
| 161 |
WritelnWarning('Spine JSON skeleton', 'No "attachments" within skin "%s"', [Skin.Name]); |
| 162 |
Continue; |
| 163 |
end; |
| 164 |
|
| 165 |
Skin.Parse(OneSkinObject, Bones, DragonBonesFormat); |
| 166 |
|
| 167 |
if Skin.Name = 'default' then |
| 168 |
DefaultSkin := Skin; |
| 169 |
end; |
| 170 |
end else |
| 171 |
begin |
| 172 |
WritelnWarning('Spine JSON skeleton', '"skins" data is neither object nor array, not supported'); |
| 173 |
end; |
| 174 |
end; |
| 175 |
|
| 176 |
procedure ParseDragonBones; |
| 177 |
var |
| 178 |
I: Integer; |
| 179 |
Skin: TSkin; |
| 180 |
OneSkinObject: TJSONObject; |
| 181 |
SkinsArray: TJSONArray; |
| 182 |
begin |
| 183 |
SkinsArray := Json.Find('skin', jtArray) as TJSONArray; |
| 184 |
if SkinsArray = nil then |
| 185 |
raise ESpineReadError.Create('Dragon Bones JSON skeleton: Missing "skin" array'); |
| 186 |
|
| 187 |
for I := 0 to SkinsArray.Count - 1 do |
| 188 |
begin |
| 189 |
OneSkinObject := SkinsArray[I] as TJSONObject; |
| 190 |
Skin := TSkin.Create; |
| 191 |
Add(Skin); |
| 192 |
Skin.Name := OneSkinObject.Get('name', ''); |
| 193 |
Skin.Parse(OneSkinObject, Bones, DragonBonesFormat); |
| 194 |
|
| 195 |
if DefaultSkin = nil then // just use 1st skin as default for now |
| 196 |
DefaultSkin := Skin; |
| 197 |
end; |
| 198 |
end; |
| 199 |
|
| 200 |
begin |
| 201 |
if DragonBonesFormat then |
| 202 |
ParseDragonBones |
| 203 |
else |
| 204 |
ParseSpine; |
| 205 |
|
| 206 |
if DefaultSkin = nil then // DefaultSkin may be nil in case of empty model |
| 207 |
WritelnWarning('Spine JSON skeleton', 'Missing "default" skin definition, Spine JSON is empty'); |
| 208 |
end; |
| 209 |
|
| 210 |
procedure TSkinList.BuildNodes(const BaseUrl: String; const TextureLoader: TTextureLoader); |
| 211 |
var |
| 212 |
I: Integer; |
| 213 |
begin |
| 214 |
for I := 0 to Count - 1 do |
| 215 |
Items[I].BuildNodes(BaseUrl, TextureLoader); |
| 216 |
end; |
| 217 |
{$endif} |