| 1 |
{ |
| 2 |
Copyright 2017-2024 Trung Le (kagamma), Andrzej Kilijański (and3md), Michalis Kamburelis. |
| 3 |
|
| 4 |
Based on sprite-sheet-to-x3d source code by Trung Le (kagamma). |
| 5 |
|
| 6 |
This file is part of "Castle Game Engine". |
| 7 |
|
| 8 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 9 |
included in this distribution, for details about the copyright. |
| 10 |
|
| 11 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 |
|
| 15 |
---------------------------------------------------------------------------- |
| 16 |
} |
| 17 |
{ Cocos2d animations loader. |
| 18 |
|
| 19 |
Spec: |
| 20 |
https://docs.cocos2d-x.org/cocos2d-x/v3/en/sprites/spritesheets.html |
| 21 |
https://www.codeandweb.com/blog/2016/01/29/cocos2d-plist-format-explained |
| 22 |
|
| 23 |
} |
| 24 |
unit X3DLoadInternalCocos2d; |
| 25 |
|
| 26 |
{$I castleconf.inc} |
| 27 |
|
| 28 |
interface |
| 29 |
|
| 30 |
uses DOM, CastleVectors; |
| 31 |
|
| 32 |
{ Routines made public only for test purposes. } |
| 33 |
|
| 34 |
function Cocos2dReadDual(const ASrc: String; out V1, V2: Integer): Boolean; overload; |
| 35 |
function Cocos2dReadDual(const ASrc: String; out V1, V2: Single): Boolean; overload; |
| 36 |
function Cocos2dReadDual(const ASrc: String; out V: TVector2): Boolean; overload; |
| 37 |
|
| 38 |
function Cocos2dReadQuad(ASrc: String; out V1, V2, V3, V4: Integer): Boolean; |
| 39 |
|
| 40 |
function Cocos2dReadBool(const ASrc: TDOMElement): Boolean; |
| 41 |
|
| 42 |
implementation |
| 43 |
|
| 44 |
uses Classes, SysUtils, |
| 45 |
Generics.Collections, StrUtils, XMLRead, |
| 46 |
X3DNodes, X3DLoad, CastleImages, CastleLog, CastleStringUtils, |
| 47 |
CastleTextureImages, CastleUriUtils, CastleUtils, CastleXmlUtils; |
| 48 |
|
| 49 |
{ simple reading routines --------------------------------------------------- } |
| 50 |
|
| 51 |
function Cocos2dReadDual(const ASrc: String; out V1, V2: Integer): Boolean; |
| 52 |
var |
| 53 |
OpenBracePos: Integer; |
| 54 |
CloseBracePos: Integer; |
| 55 |
CommaPos: Integer; |
| 56 |
begin |
| 57 |
OpenBracePos := Pos('{', ASrc); |
| 58 |
if OpenBracePos = 0 then |
| 59 |
begin |
| 60 |
WritelnWarning('Cocos2d', 'Invalid dual value "%s".', [ASrc]); |
| 61 |
Exit(false); |
| 62 |
end; |
| 63 |
|
| 64 |
CloseBracePos := PosEx('}', ASrc, OpenBracePos); |
| 65 |
if CloseBracePos = 0 then |
| 66 |
begin |
| 67 |
WritelnWarning('Cocos2d', 'Invalid dual value "%s".', [ASrc]); |
| 68 |
Exit(false); |
| 69 |
end; |
| 70 |
|
| 71 |
CommaPos := PosEx(',', ASrc, OpenBracePos); |
| 72 |
if CommaPos = 0 then |
| 73 |
begin |
| 74 |
WritelnWarning('Cocos2d', 'Invalid dual value "%s".', [ASrc]); |
| 75 |
Exit(false); |
| 76 |
end; |
| 77 |
|
| 78 |
V1 := StrToInt(Copy(ASrc, OpenBracePos + 1, CommaPos - (OpenBracePos + 1))); |
| 79 |
V2 := StrToInt(Copy(ASrc, CommaPos + 1, CloseBracePos - (CommaPos + 1))); |
| 80 |
Result := true; |
| 81 |
end; |
| 82 |
|
| 83 |
function Cocos2dReadDual(const ASrc: String; out V: TVector2): Boolean; |
| 84 |
begin |
| 85 |
Result := Cocos2dReadDual(ASrc, V.X, V.Y); |
| 86 |
end; |
| 87 |
|
| 88 |
function Cocos2dReadDual(const ASrc: String; out V1, V2: Single): Boolean; |
| 89 |
var |
| 90 |
OpenBracePos: Integer; |
| 91 |
CloseBracePos: Integer; |
| 92 |
CommaPos: Integer; |
| 93 |
begin |
| 94 |
OpenBracePos := Pos('{', ASrc); |
| 95 |
if OpenBracePos = 0 then |
| 96 |
begin |
| 97 |
WritelnWarning('Cocos2d', 'Invalid dual value "%s".', [ASrc]); |
| 98 |
Exit(false); |
| 99 |
end; |
| 100 |
|
| 101 |
CloseBracePos := PosEx('}', ASrc, OpenBracePos); |
| 102 |
if CloseBracePos = 0 then |
| 103 |
begin |
| 104 |
WritelnWarning('Cocos2d', 'Invalid dual value "%s".', [ASrc]); |
| 105 |
Exit(false); |
| 106 |
end; |
| 107 |
|
| 108 |
CommaPos := PosEx(',', ASrc, OpenBracePos); |
| 109 |
if CommaPos = 0 then |
| 110 |
begin |
| 111 |
WritelnWarning('Cocos2d', 'Invalid dual value "%s".', [ASrc]); |
| 112 |
Exit(false); |
| 113 |
end; |
| 114 |
|
| 115 |
V1 := StrToFloatDot(Copy(ASrc, OpenBracePos + 1, CommaPos - (OpenBracePos + 1))); |
| 116 |
V2 := StrToFloatDot(Copy(ASrc, CommaPos + 1, CloseBracePos - (CommaPos + 1))); |
| 117 |
Result := true; |
| 118 |
end; |
| 119 |
|
| 120 |
function Cocos2dReadQuad(ASrc: String; out V1, V2, V3, V4: Integer): Boolean; |
| 121 |
var |
| 122 |
OpenBracePos: Integer; |
| 123 |
CloseBracePos: Integer; |
| 124 |
OpenBraceCount, CloseBraceCount: Integer; |
| 125 |
begin |
| 126 |
OpenBraceCount := SCharsCount(ASrc, ['{']); |
| 127 |
CloseBraceCount := SCharsCount(ASrc, ['}']); |
| 128 |
|
| 129 |
if (OpenBraceCount = 3) and (CloseBraceCount = 3) then |
| 130 |
begin |
| 131 |
// read format '{{1,2},{60,88}}' |
| 132 |
if not IsPrefix('{', ASrc, false) then |
| 133 |
begin |
| 134 |
WritelnWarning('Cocos2d', 'Invalid quad value "%s".', [ASrc]); |
| 135 |
Exit(false); |
| 136 |
end; |
| 137 |
ASrc := PrefixRemove('{', ASrc, false); |
| 138 |
|
| 139 |
if not IsSuffix('}', ASrc, false) then |
| 140 |
begin |
| 141 |
WritelnWarning('Cocos2d', 'Invalid quad value "%s".', [ASrc]); |
| 142 |
Exit(false); |
| 143 |
end; |
| 144 |
ASrc := SuffixRemove('}', ASrc, false); |
| 145 |
end else |
| 146 |
if (OpenBraceCount = 2) and (CloseBraceCount = 2) then |
| 147 |
begin |
| 148 |
// read format '{1,2},{60,88}' |
| 149 |
end else |
| 150 |
begin |
| 151 |
WritelnWarning('Cocos2d', 'Invalid quad value "%s".', [ASrc]); |
| 152 |
Exit(false); |
| 153 |
end; |
| 154 |
|
| 155 |
OpenBracePos := Pos('{', ASrc); |
| 156 |
if OpenBracePos = 0 then |
| 157 |
begin |
| 158 |
WritelnWarning('Cocos2d', 'Invalid quad value "%s".', [ASrc]); |
| 159 |
Exit(false); |
| 160 |
end; |
| 161 |
|
| 162 |
CloseBracePos := Pos('}', ASrc); |
| 163 |
if CloseBracePos = 0 then |
| 164 |
begin |
| 165 |
WritelnWarning('Cocos2d', 'Invalid quad value "%s".', [ASrc]); |
| 166 |
Exit(false); |
| 167 |
end; |
| 168 |
|
| 169 |
if not Cocos2dReadDual(Copy(ASrc, OpenBracePos, CloseBracePos - OpenBracePos + 1), V1, V2) then |
| 170 |
begin |
| 171 |
WritelnWarning('Cocos2d', 'Invalid quad value "%s".', [ASrc]); |
| 172 |
Exit(false); |
| 173 |
end; |
| 174 |
|
| 175 |
OpenBracePos := PosEx('{', ASrc, CloseBracePos + 1); |
| 176 |
if OpenBracePos = 0 then |
| 177 |
begin |
| 178 |
WritelnWarning('Cocos2d', 'Invalid quad value "%s".', [ASrc]); |
| 179 |
Exit(false); |
| 180 |
end; |
| 181 |
|
| 182 |
CloseBracePos := PosEx('}', ASrc, CloseBracePos + 1); |
| 183 |
if CloseBracePos = 0 then |
| 184 |
begin |
| 185 |
WritelnWarning('Cocos2d', 'Invalid quad value "%s".', [ASrc]); |
| 186 |
Exit(false); |
| 187 |
end; |
| 188 |
|
| 189 |
if not Cocos2dReadDual(Copy(ASrc, OpenBracePos, CloseBracePos - OpenBracePos + 1), V3, V4) then |
| 190 |
begin |
| 191 |
WritelnWarning('Cocos2d', 'Invalid quad value "%s".', [ASrc]); |
| 192 |
Exit(false); |
| 193 |
end; |
| 194 |
|
| 195 |
Result := true; |
| 196 |
end; |
| 197 |
|
| 198 |
function Cocos2dReadBool(const ASrc: TDOMElement): Boolean; |
| 199 |
begin |
| 200 |
if SameText(ASrc.TagName8, 'true') then |
| 201 |
Result := true |
| 202 |
else |
| 203 |
if SameText(ASrc.TagName8, 'false') then |
| 204 |
Result := false |
| 205 |
else |
| 206 |
begin |
| 207 |
WritelnWarning('Cocos2d', 'Invalid boolean value "%s".', [ASrc.TagName8]); |
| 208 |
Result := false; |
| 209 |
end; |
| 210 |
end; |
| 211 |
|
| 212 |
{ classes to load Cocos2d files --------------------------------------------- } |
| 213 |
|
| 214 |
type |
| 215 |
{ Cocos2d XML file is not correct } |
| 216 |
EInvalidCocos2dPlist = class(Exception); |
| 217 |
|
| 218 |
TCocos2dLoader = class |
| 219 |
strict private |
| 220 |
type |
| 221 |
{ Class that represents frame from Cocos2d file } |
| 222 |
TCocosFrame = class |
| 223 |
private |
| 224 |
FDisplayUrl: String; |
| 225 |
FCocosFormat: Integer; |
| 226 |
|
| 227 |
FParseFrameDictionary: procedure (const DictNode: TDOMElement) of object; |
| 228 |
|
| 229 |
procedure PrepareTexCords(const ImageWidth, ImageHeight: Integer); |
| 230 |
procedure ParseAnimationName(const FrameFileName: String); |
| 231 |
procedure ParseFrameDictionaryFormat2(const DictNode: TDOMElement); |
| 232 |
procedure ParseFrameDictionaryFormat3(const DictNode: TDOMElement); |
| 233 |
public |
| 234 |
AnimationName: String; |
| 235 |
{ Origin texture coordinate (top-left corner of frame part, |
| 236 |
in Cocos2d coordinates where top-left corner of the image is 0,0). } |
| 237 |
TexCoordOrigin: TVector2Integer; |
| 238 |
{ Texture coordinates calculated by PrepareTexCords. |
| 239 |
In 0..1 range. |
| 240 |
Texture coordinate for each of 4 quad corners. } |
| 241 |
TexCoords: array [0..3] of TVector2; |
| 242 |
{ Width of sprite. If sprite is trimmed this is trimmed width - not full frame width. } |
| 243 |
Width: Integer; |
| 244 |
{ Height of sprite. If sprite is trimmed this is trimmed height - not full frame height. } |
| 245 |
Height: Integer; |
| 246 |
AnchorX: Single; |
| 247 |
AnchorY: Single; |
| 248 |
|
| 249 |
Rotated: Boolean; |
| 250 |
Offset: TVector2; |
| 251 |
|
| 252 |
constructor Create(const DisplayUrl: String); |
| 253 |
{ We support format version 2 and 3. This procedure sets suitable |
| 254 |
ParseFrameDictionaryFormatX procedure. } |
| 255 |
procedure SetCocosFormat(const Format: Integer); |
| 256 |
|
| 257 |
procedure ReadFormDict(const KeyNode, DictNode: TDOMElement; |
| 258 |
const ImageWidth, ImageHeight: Integer); |
| 259 |
end; |
| 260 |
var |
| 261 |
FStream: TStream; |
| 262 |
FBaseUrl: String; |
| 263 |
FDisplayUrl: String; |
| 264 |
|
| 265 |
{ Load settings. } |
| 266 |
FFramesPerSecond: Single; |
| 267 |
|
| 268 |
FImageWidth, FImageHeight: Integer; |
| 269 |
FImagePath: String; |
| 270 |
|
| 271 |
{ We currently support the format version 2 and 3. The frames have |
| 272 |
different parameters depending on the version. } |
| 273 |
FCocosFormat: Integer; |
| 274 |
|
| 275 |
FCocosFrame: TCocosFrame; |
| 276 |
|
| 277 |
FRoot: TX3DRootNode; |
| 278 |
FShapeCoord: TCoordinateNode; |
| 279 |
FShapeTexCoord: TTextureCoordinateNode; |
| 280 |
|
| 281 |
FCoordArray: array of TVector3; |
| 282 |
FTexCoordArray: array of TVector2; |
| 283 |
|
| 284 |
{ Animation list to check if the file has any mixed SubTexture nodes. } |
| 285 |
FAnimationList: TStringList; |
| 286 |
|
| 287 |
procedure ReadImportSettings; |
| 288 |
|
| 289 |
procedure PrepareX3DRoot; |
| 290 |
|
| 291 |
procedure ReadMetadata(const MetadataNode: TDOMElement); |
| 292 |
|
| 293 |
procedure ReadFrames(const FramesNode: TDOMElement); |
| 294 |
|
| 295 |
procedure CalculateFrameCoords(const CocosFrame: TCocosFrame); |
| 296 |
|
| 297 |
procedure PrepareShape(const CoordArray: array of TVector3; |
| 298 |
const TexCoordArray: array of TVector2); |
| 299 |
|
| 300 |
procedure AddFrameCoords(const CoordInterp: TCoordinateInterpolatorNode; |
| 301 |
const TexCoordInterp: TCoordinateInterpolator2DNode); |
| 302 |
|
| 303 |
procedure AddAnimation(const FrameCount: Integer; |
| 304 |
const TimeSensor: TTimeSensorNode; |
| 305 |
const CoordInterp: TCoordinateInterpolatorNode; |
| 306 |
const TexCoordInterp: TCoordinateInterpolator2DNode); |
| 307 |
|
| 308 |
procedure AddRoutes(const TimeSensor: TTimeSensorNode; |
| 309 |
const CoordInterp: TCoordinateInterpolatorNode; |
| 310 |
const TexCoordInterp: TCoordinateInterpolator2DNode); |
| 311 |
|
| 312 |
function CheckAnimationNameAvailable(const AnimationName: String): Boolean; |
| 313 |
|
| 314 |
public |
| 315 |
constructor Create(const Stream: TStream; const BaseUrl: String); |
| 316 |
destructor Destroy; override; |
| 317 |
|
| 318 |
function Load: TX3DRootNode; |
| 319 |
end; |
| 320 |
|
| 321 |
function LoadCocos2d(const Stream: TStream; const BaseUrl: String): TX3DRootNode; |
| 322 |
var |
| 323 |
Cocos2dLoader: TCocos2dLoader; |
| 324 |
begin |
| 325 |
Cocos2dLoader := TCocos2dLoader.Create(Stream, BaseUrl); |
| 326 |
try |
| 327 |
Result := Cocos2dLoader.Load; |
| 328 |
finally |
| 329 |
FreeAndNil(Cocos2dLoader); |
| 330 |
end; |
| 331 |
end; |
| 332 |
|
| 333 |
{ TCocos2dLoader.TCocosFrame ------------------------------------------------ } |
| 334 |
|
| 335 |
procedure TCocos2dLoader.TCocosFrame.PrepareTexCords( |
| 336 |
const ImageWidth, ImageHeight: Integer); |
| 337 |
var |
| 338 |
I: Integer; |
| 339 |
begin |
| 340 |
if Rotated then |
| 341 |
begin |
| 342 |
TexCoords[0] := Vector2(TexCoordOrigin.X + Height, TexCoordOrigin.Y); |
| 343 |
TexCoords[1] := Vector2(TexCoordOrigin.X + Height, TexCoordOrigin.Y + Width); |
| 344 |
TexCoords[2] := Vector2(TexCoordOrigin.X , TexCoordOrigin.Y + Width); |
| 345 |
TexCoords[3] := Vector2(TexCoordOrigin.X , TexCoordOrigin.Y); |
| 346 |
end else |
| 347 |
begin |
| 348 |
TexCoords[0] := Vector2(TexCoordOrigin.X , TexCoordOrigin.Y); |
| 349 |
TexCoords[1] := Vector2(TexCoordOrigin.X + Width, TexCoordOrigin.Y); |
| 350 |
TexCoords[2] := Vector2(TexCoordOrigin.X + Width, TexCoordOrigin.Y + Height); |
| 351 |
TexCoords[3] := Vector2(TexCoordOrigin.X , TexCoordOrigin.Y + Height); |
| 352 |
end; |
| 353 |
|
| 354 |
// convert all 4 corners to 0..1 range, and flip Y |
| 355 |
for I := 0 to 3 do |
| 356 |
begin |
| 357 |
TexCoords[I].X := TexCoords[I].X / ImageWidth; |
| 358 |
TexCoords[I].Y := 1 - TexCoords[I].Y / ImageHeight; |
| 359 |
end; |
| 360 |
end; |
| 361 |
|
| 362 |
procedure TCocos2dLoader.TCocosFrame.ParseAnimationName(const FrameFileName: String); |
| 363 |
begin |
| 364 |
{ Some times this names can be like "walk/0001.png" } |
| 365 |
AnimationName := DeleteFileExt(FrameFileName); |
| 366 |
|
| 367 |
{$ifdef FPC} |
| 368 |
RemoveTrailingChars(AnimationName, ['0'..'9']); |
| 369 |
{$else} |
| 370 |
AnimationName := AnimationName.TrimRight(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']); |
| 371 |
{$endif} |
| 372 |
|
| 373 |
if (Length(AnimationName) > 1) and ((AnimationName[Length(AnimationName)] = '_') |
| 374 |
or (AnimationName[Length(AnimationName)] = '-') |
| 375 |
or (AnimationName[Length(AnimationName)] = '/') |
| 376 |
or (AnimationName[Length(AnimationName)] = '\')) then |
| 377 |
delete(AnimationName, Length(AnimationName), 1); |
| 378 |
|
| 379 |
if AnimationName = '' then |
| 380 |
begin |
| 381 |
WritelnWarning('Cocos2d', |
| 382 |
'Incorrect animation name (%s), setting the name to "unknown"', |
| 383 |
[FrameFileName]); |
| 384 |
AnimationName := 'unknown'; |
| 385 |
end; |
| 386 |
end; |
| 387 |
|
| 388 |
procedure TCocos2dLoader.TCocosFrame.ParseFrameDictionaryFormat2(const DictNode: TDOMElement); |
| 389 |
var |
| 390 |
I: TXMLElementIterator; |
| 391 |
KeyNode, ValueNode: TDOMElement; |
| 392 |
|
| 393 |
X: Integer; |
| 394 |
Y: Integer; |
| 395 |
FrameXTrimed: Integer; |
| 396 |
FrameYTrimed: Integer; |
| 397 |
FrameWidthTrimed: Integer; |
| 398 |
FrameHeightTrimed: Integer; |
| 399 |
Trimmed: Boolean; |
| 400 |
FullFrameWidth: Integer; |
| 401 |
FullFrameHeight: Integer; |
| 402 |
|
| 403 |
HasAnchor: Boolean; |
| 404 |
FrameAnchorX: Integer; |
| 405 |
FrameAnchorY: Integer; |
| 406 |
|
| 407 |
WasFrame: Boolean; |
| 408 |
WasFrameSize: Boolean; |
| 409 |
begin |
| 410 |
HasAnchor := false; |
| 411 |
WasFrame := false; |
| 412 |
WasFrameSize := false; |
| 413 |
Trimmed := false; |
| 414 |
I := DictNode.ChildrenIterator; |
| 415 |
try |
| 416 |
while I.GetNext do |
| 417 |
begin |
| 418 |
KeyNode := I.Current; |
| 419 |
if KeyNode.NodeName <> 'key' then |
| 420 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - key node expected.', [FDisplayUrl]); |
| 421 |
|
| 422 |
if not I.GetNext then |
| 423 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - value node expected.', [FDisplayUrl]); |
| 424 |
|
| 425 |
ValueNode := I.Current; |
| 426 |
|
| 427 |
if KeyNode.TextData = 'frame' then |
| 428 |
begin |
| 429 |
{ Sprite position and size in the texture - the same as textureRect in format 3 } |
| 430 |
if Cocos2dReadQuad(ValueNode.TextData, X, Y, Width, Height) then |
| 431 |
begin |
| 432 |
TexCoordOrigin := Vector2Integer(X, Y); |
| 433 |
WasFrame := true; |
| 434 |
end; |
| 435 |
end else |
| 436 |
if KeyNode.TextData = 'sourceColorRect' then |
| 437 |
begin |
| 438 |
{ rect of the trimmed sprite } |
| 439 |
Cocos2dReadQuad(ValueNode.TextData, FrameXTrimed, FrameYTrimed, FrameWidthTrimed, FrameHeightTrimed); |
| 440 |
Trimmed := true; |
| 441 |
end else |
| 442 |
if KeyNode.TextData = 'anchor' then |
| 443 |
begin |
| 444 |
{ Anchor point for the sprite in coordinates relative to the original sprite size } |
| 445 |
if Cocos2dReadDual(ValueNode.TextData, AnchorX, AnchorY) then |
| 446 |
HasAnchor := true; |
| 447 |
end else |
| 448 |
if KeyNode.TextData = 'sourceSize' then |
| 449 |
begin |
| 450 |
{ full size of the sprite, the same as spriteSourceSize in format 3 } |
| 451 |
if Cocos2dReadDual(ValueNode.TextData, FullFrameWidth, FullFrameHeight) then |
| 452 |
WasFrameSize := true; |
| 453 |
end else |
| 454 |
if KeyNode.TextData = 'rotated' then |
| 455 |
begin |
| 456 |
Rotated := Cocos2dReadBool(ValueNode); |
| 457 |
end else |
| 458 |
if KeyNode.TextData = 'offset' then |
| 459 |
begin |
| 460 |
Cocos2dReadDual(ValueNode.TextData, Offset); |
| 461 |
if not Offset.IsZero then |
| 462 |
WritelnWarning('Cocos2d', 'Offset (non-zero) is not supported in "%s".', [FDisplayUrl]); |
| 463 |
end else |
| 464 |
begin |
| 465 |
WritelnWarning('Cocos2d', 'Unknown key "%s" in frame of "%s"', [KeyNode.TextData, FDisplayUrl]); |
| 466 |
end; |
| 467 |
end; |
| 468 |
finally |
| 469 |
FreeAndNil(I); |
| 470 |
end; |
| 471 |
|
| 472 |
if not WasFrameSize or not WasFrame then |
| 473 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - frame data incomplete.', [FDisplayUrl]); |
| 474 |
|
| 475 |
if not HasAnchor then |
| 476 |
begin |
| 477 |
{ When frame is trimmed Width and Height does not mean the full size |
| 478 |
of the frame, so we have to calculate the appropriate |
| 479 |
anchor to get the correct position because it will not be (0.5, 0.5) } |
| 480 |
if Trimmed then |
| 481 |
begin |
| 482 |
{ Anchor in pixels (Without translation to correct texture point |
| 483 |
because we don't need that. Just add X1, Y1 to have correct position.) } |
| 484 |
FrameAnchorX := FullFrameWidth div 2 - FrameXTrimed; |
| 485 |
FrameAnchorY := FullFrameHeight div 2 - FrameYTrimed; |
| 486 |
|
| 487 |
{ Convert to 0.0..1.0 coordinate system } |
| 488 |
AnchorX := 1 / Width * FrameAnchorX; |
| 489 |
AnchorY := 1 / Height * FrameAnchorY; |
| 490 |
end else |
| 491 |
begin |
| 492 |
AnchorX := 0.5; |
| 493 |
AnchorY := 0.5; |
| 494 |
end; |
| 495 |
end else |
| 496 |
begin |
| 497 |
{ TODO: need example with anchors, in spritesheet to x3d in this case |
| 498 |
nothing is done but I haven't validated that } |
| 499 |
end; |
| 500 |
end; |
| 501 |
|
| 502 |
procedure TCocos2dLoader.TCocosFrame.ParseFrameDictionaryFormat3( |
| 503 |
const DictNode: TDOMElement); |
| 504 |
var |
| 505 |
I: TXMLElementIterator; |
| 506 |
KeyNode, ValueNode: TDOMElement; |
| 507 |
|
| 508 |
X: Integer; |
| 509 |
Y: Integer; |
| 510 |
FrameXTrimOffset: Single; |
| 511 |
FrameYTrimOffset: Single; |
| 512 |
Trimmed: Boolean; |
| 513 |
FullFrameWidth: Integer; |
| 514 |
FullFrameHeight: Integer; |
| 515 |
|
| 516 |
HasAnchor: Boolean; |
| 517 |
FrameAnchorX: Single; |
| 518 |
FrameAnchorY: Single; |
| 519 |
|
| 520 |
WasTextureFrame: Boolean; |
| 521 |
WasFrameFullSize: Boolean; |
| 522 |
begin |
| 523 |
HasAnchor := false; |
| 524 |
WasTextureFrame := false; |
| 525 |
WasFrameFullSize := false; |
| 526 |
Trimmed := false; |
| 527 |
I := DictNode.ChildrenIterator; |
| 528 |
try |
| 529 |
while I.GetNext do |
| 530 |
begin |
| 531 |
KeyNode := I.Current; |
| 532 |
if KeyNode.NodeName <> 'key' then |
| 533 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - key node expected.', [FDisplayUrl]); |
| 534 |
|
| 535 |
if not I.GetNext then |
| 536 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - value node expected.', [FDisplayUrl]); |
| 537 |
|
| 538 |
ValueNode := I.Current; |
| 539 |
|
| 540 |
if KeyNode.TextData = 'textureRect' then |
| 541 |
begin |
| 542 |
{ Sprite position and size in the texture - the same as frame in format 2 } |
| 543 |
if Cocos2dReadQuad(ValueNode.TextData, X, Y, Width, Height) then |
| 544 |
WasTextureFrame := true; |
| 545 |
end else |
| 546 |
if KeyNode.TextData = 'spriteOffset' then |
| 547 |
begin |
| 548 |
{ offset in sprite } |
| 549 |
Cocos2dReadDual(ValueNode.TextData, FrameXTrimOffset, FrameYTrimOffset); |
| 550 |
Trimmed := true; |
| 551 |
end else |
| 552 |
if KeyNode.TextData = 'anchor' then |
| 553 |
begin |
| 554 |
{ Anchor point for the sprite in coordinates relative to the original sprite size } |
| 555 |
if Cocos2dReadDual(ValueNode.TextData, AnchorX, AnchorY) then |
| 556 |
HasAnchor := true; |
| 557 |
end else |
| 558 |
if KeyNode.TextData = 'spriteSourceSize' then |
| 559 |
begin |
| 560 |
{ full size of the sprite, the same as sourceSize in format 2 } |
| 561 |
if Cocos2dReadDual(ValueNode.TextData, FullFrameWidth, FullFrameHeight) then |
| 562 |
WasFrameFullSize := true; |
| 563 |
end; |
| 564 |
end; |
| 565 |
finally |
| 566 |
FreeAndNil(I); |
| 567 |
end; |
| 568 |
|
| 569 |
if not WasTextureFrame or not WasFrameFullSize then |
| 570 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - frame data incomplete.', [FDisplayUrl]); |
| 571 |
|
| 572 |
TexCoordOrigin := Vector2Integer(X, Y); |
| 573 |
|
| 574 |
if not HasAnchor then |
| 575 |
begin |
| 576 |
{ When frame is trimmed Width and Height does not mean the full size |
| 577 |
of the frame, so we have to calculate the appropriate |
| 578 |
anchor to get the correct position because it will not be (0.5, 0.5) } |
| 579 |
if Trimmed then |
| 580 |
begin |
| 581 |
{ Anchor in pixels (Without translation to correct texture point |
| 582 |
because we don't need that. Just add X1, Y1 to have correct position.) } |
| 583 |
FrameAnchorX := FullFrameWidth / 2 - FrameXTrimOffset; |
| 584 |
FrameAnchorY := FullFrameHeight / 2 - FrameYTrimOffset; |
| 585 |
|
| 586 |
{ Convert to 0.0..1.0 coordinate system } |
| 587 |
AnchorX := 1 / Width * FrameAnchorX; |
| 588 |
AnchorY := 1 / Height * FrameAnchorY; |
| 589 |
end else |
| 590 |
begin |
| 591 |
AnchorX := 0.5; |
| 592 |
AnchorY := 0.5; |
| 593 |
end; |
| 594 |
end else |
| 595 |
begin |
| 596 |
{ TODO: need example with anchors, in spritesheet-to-x3d in this case |
| 597 |
nothing is done but I haven't validated that } |
| 598 |
end; |
| 599 |
end; |
| 600 |
|
| 601 |
constructor TCocos2dLoader.TCocosFrame.Create(const DisplayUrl: String); |
| 602 |
begin |
| 603 |
inherited Create; |
| 604 |
FDisplayUrl := DisplayUrl; |
| 605 |
{ By default, use format 3 } |
| 606 |
FParseFrameDictionary := {$ifdef FPC}@{$endif}ParseFrameDictionaryFormat3; |
| 607 |
end; |
| 608 |
|
| 609 |
procedure TCocos2dLoader.TCocosFrame.SetCocosFormat(const Format: Integer); |
| 610 |
begin |
| 611 |
FCocosFormat := Format; |
| 612 |
case Format of |
| 613 |
2: |
| 614 |
FParseFrameDictionary := {$ifdef FPC}@{$endif}ParseFrameDictionaryFormat2; |
| 615 |
3: |
| 616 |
FParseFrameDictionary := {$ifdef FPC}@{$endif}ParseFrameDictionaryFormat3; |
| 617 |
else |
| 618 |
{ If format is unsupported try use latest supported version and add warning. } |
| 619 |
FParseFrameDictionary := {$ifdef FPC}@{$endif}ParseFrameDictionaryFormat3; |
| 620 |
WritelnWarning('Cocos2d', |
| 621 |
'Unsupported format version %d in "%s", trying to load with the latest importer (format = 3).', |
| 622 |
[Format, FDisplayUrl]); |
| 623 |
end; |
| 624 |
end; |
| 625 |
|
| 626 |
procedure TCocos2dLoader.TCocosFrame.ReadFormDict(const KeyNode, DictNode: TDOMElement; |
| 627 |
const ImageWidth, ImageHeight: Integer); |
| 628 |
begin |
| 629 |
ParseAnimationName(KeyNode.TextData); |
| 630 |
FParseFrameDictionary(DictNode); |
| 631 |
PrepareTexCords(ImageWidth, ImageHeight); |
| 632 |
end; |
| 633 |
|
| 634 |
{ TCocos2dLoader ------------------------------------------------------------ } |
| 635 |
|
| 636 |
procedure TCocos2dLoader.ReadImportSettings; |
| 637 |
var |
| 638 |
SettingsMap: TStringStringMap; |
| 639 |
Setting: {$ifdef FPC}TStringStringMap.TDictionaryPair{$else}TPair<String, String>{$endif}; |
| 640 |
begin |
| 641 |
// default values |
| 642 |
FFramesPerSecond := DefaultSpriteSheetFramesPerSecond; |
| 643 |
|
| 644 |
SettingsMap := TStringStringMap.Create; |
| 645 |
try |
| 646 |
URIGetSettingsFromAnchor(FBaseUrl, SettingsMap); |
| 647 |
for Setting in SettingsMap do |
| 648 |
begin |
| 649 |
if LowerCase(Setting.Key) = 'fps' then |
| 650 |
begin |
| 651 |
FFramesPerSecond := StrToFloatDot(Setting.Value); |
| 652 |
end else |
| 653 |
WritelnWarning('Starling', 'Unknown setting (%s) in "%s" anchor.', |
| 654 |
[Setting.Key, FDisplayUrl]); |
| 655 |
end; |
| 656 |
finally |
| 657 |
FreeAndNil(SettingsMap); |
| 658 |
end; |
| 659 |
end; |
| 660 |
|
| 661 |
|
| 662 |
procedure TCocos2dLoader.PrepareX3DRoot; |
| 663 |
begin |
| 664 |
FRoot.Meta['generator'] := 'Castle Game Engine, https://castle-engine.io'; |
| 665 |
FRoot.Meta['source'] := ExtractURIName(FBaseUrl); |
| 666 |
end; |
| 667 |
|
| 668 |
procedure TCocos2dLoader.ReadMetadata(const MetadataNode: TDOMElement); |
| 669 |
var |
| 670 |
I: TXMLElementIterator; |
| 671 |
KeyNode: TDOMElement; |
| 672 |
ValueNode: TDOMElement; |
| 673 |
RealTextureFileName: String; |
| 674 |
TextureFileName: String; |
| 675 |
Image: TCastleImage; |
| 676 |
begin |
| 677 |
I := MetadataNode.ChildrenIterator; |
| 678 |
try |
| 679 |
while I.GetNext do |
| 680 |
begin |
| 681 |
KeyNode := I.Current; |
| 682 |
if KeyNode.NodeName <> 'key' then |
| 683 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - key node expected.', [FDisplayUrl]); |
| 684 |
|
| 685 |
if not I.GetNext then |
| 686 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - value node expected.', [FDisplayUrl]); |
| 687 |
|
| 688 |
ValueNode := I.Current; |
| 689 |
|
| 690 |
if KeyNode.TextData = 'realTextureFileName' then |
| 691 |
RealTextureFileName := ValueNode.TextData |
| 692 |
else |
| 693 |
if KeyNode.TextData = 'textureFileName' then |
| 694 |
TextureFileName := ValueNode.TextData |
| 695 |
else |
| 696 |
if KeyNode.TextData = 'size' then |
| 697 |
begin |
| 698 |
if not Cocos2dReadDual(ValueNode.TextData, FImageWidth, FImageHeight) then |
| 699 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - invalid size.', [FDisplayUrl]); |
| 700 |
end else |
| 701 |
if KeyNode.TextData = 'format' then |
| 702 |
FCocosFormat := StrToInt(ValueNode.TextData) |
| 703 |
else |
| 704 |
if KeyNode.TextData = 'smartupdate' then |
| 705 |
begin |
| 706 |
{ We don't need this information } |
| 707 |
end else |
| 708 |
if KeyNode.TextData = 'pixelFormat' then |
| 709 |
begin |
| 710 |
{ We don't need this information -- we support any image format there is in the atlas file. } |
| 711 |
end else |
| 712 |
if KeyNode.TextData = 'premultiplyAlpha' then |
| 713 |
begin |
| 714 |
if Cocos2dReadBool(ValueNode) then |
| 715 |
WritelnWarning('Cocos2d', 'premultiplyAlpha is specified as "true", but is not supported in "%s". You must use premultiplyAlpha=false.', [FDisplayUrl]); |
| 716 |
end else |
| 717 |
begin |
| 718 |
WritelnWarning('Cocos2d', 'Unknown key "%s" in metadata of "%s"', [KeyNode.TextData, FDisplayUrl]); |
| 719 |
end; |
| 720 |
end; |
| 721 |
finally |
| 722 |
FreeAndNil(I); |
| 723 |
end; |
| 724 |
|
| 725 |
{ In correct files texture file name should be in realTextureFileName, |
| 726 |
but there are a lot files with name in textureFileName which has often |
| 727 |
file name without extension when realTextureFileName exists. } |
| 728 |
if RealTextureFileName <> '' then |
| 729 |
FImagePath := ExtractURIPath(FBaseUrl) + RealTextureFileName |
| 730 |
else |
| 731 |
FImagePath := ExtractURIPath(FBaseUrl) + TextureFileName; |
| 732 |
|
| 733 |
{ This should never be needed. } |
| 734 |
if (FImageWidth = 0) or (FImageHeight = 0) then |
| 735 |
begin |
| 736 |
Image := LoadImage(FImagePath); |
| 737 |
try |
| 738 |
FImageWidth := Image.Width; |
| 739 |
FImageHeight := Image.Height; |
| 740 |
finally |
| 741 |
FreeAndNil(Image); |
| 742 |
end; |
| 743 |
end; |
| 744 |
end; |
| 745 |
|
| 746 |
procedure TCocos2dLoader.ReadFrames(const FramesNode: TDOMElement); |
| 747 |
var |
| 748 |
I: TXMLElementIterator; |
| 749 |
KeyNode: TDOMElement; |
| 750 |
DictNode: TDOMElement; |
| 751 |
|
| 752 |
LastAnimationName: String; |
| 753 |
CurrentAnimFrameCount: Integer; |
| 754 |
TimeSensor: TTimeSensorNode; |
| 755 |
CoordInterp: TCoordinateInterpolatorNode; |
| 756 |
TexCoordInterp: TCoordinateInterpolator2DNode; |
| 757 |
FirstFrameInFirstAnimation: Boolean; |
| 758 |
begin |
| 759 |
CurrentAnimFrameCount := 0; |
| 760 |
FirstFrameInFirstAnimation := true; |
| 761 |
LastAnimationName := ''; |
| 762 |
TexCoordInterp := nil; |
| 763 |
CoordInterp := nil; |
| 764 |
TimeSensor := nil; |
| 765 |
|
| 766 |
I := FramesNode.ChildrenIterator; |
| 767 |
try |
| 768 |
while I.GetNext do |
| 769 |
begin |
| 770 |
KeyNode := I.Current; |
| 771 |
if KeyNode.NodeName <> 'key' then |
| 772 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - key node expected.', [FDisplayUrl]); |
| 773 |
|
| 774 |
if not I.GetNext then |
| 775 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - dict node expected.', [FDisplayUrl]); |
| 776 |
|
| 777 |
DictNode := I.Current; |
| 778 |
if DictNode.NodeName <> 'dict' then |
| 779 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - dict node expected.', [FDisplayUrl]); |
| 780 |
|
| 781 |
{ Read frame from plist } |
| 782 |
FCocosFrame.ReadFormDict(KeyNode, DictNode, FImageWidth, FImageHeight); |
| 783 |
|
| 784 |
CalculateFrameCoords(FCocosFrame); |
| 785 |
{ After calculate first frame cords and tex cord we need create shape. } |
| 786 |
if FirstFrameInFirstAnimation then |
| 787 |
begin |
| 788 |
PrepareShape(FCoordArray, FTexCoordArray); |
| 789 |
FirstFrameInFirstAnimation := false; |
| 790 |
end; |
| 791 |
|
| 792 |
if LastAnimationName <> FCocosFrame.AnimationName then |
| 793 |
begin |
| 794 |
{ First frame of animation loaded. } |
| 795 |
|
| 796 |
if CurrentAnimFrameCount > 0 then |
| 797 |
AddAnimation(CurrentAnimFrameCount, TimeSensor, CoordInterp, TexCoordInterp); |
| 798 |
|
| 799 |
if not CheckAnimationNameAvailable(FCocosFrame.AnimationName) then |
| 800 |
begin |
| 801 |
CurrentAnimFrameCount := 0; |
| 802 |
Continue; |
| 803 |
end; |
| 804 |
|
| 805 |
{ Reset variables for new animation } |
| 806 |
LastAnimationName := FCocosFrame.AnimationName; |
| 807 |
CurrentAnimFrameCount := 1; |
| 808 |
TimeSensor := TTimeSensorNode.Create(LastAnimationName); |
| 809 |
CoordInterp := TCoordinateInterpolatorNode.Create(LastAnimationName + '_Coord'); |
| 810 |
TexCoordInterp := TCoordinateInterpolator2DNode.Create(LastAnimationName + '_TexCoord'); |
| 811 |
|
| 812 |
AddFrameCoords(CoordInterp, TexCoordInterp); |
| 813 |
end else |
| 814 |
begin |
| 815 |
{ Next frame of animation } |
| 816 |
Inc(CurrentAnimFrameCount); |
| 817 |
|
| 818 |
AddFrameCoords(CoordInterp, TexCoordInterp); |
| 819 |
end; |
| 820 |
|
| 821 |
end; |
| 822 |
|
| 823 |
{ Add last animation } |
| 824 |
if CurrentAnimFrameCount > 0 then |
| 825 |
AddAnimation(CurrentAnimFrameCount, TimeSensor, CoordInterp, TexCoordInterp); |
| 826 |
|
| 827 |
finally |
| 828 |
FreeAndNil(I); |
| 829 |
end; |
| 830 |
|
| 831 |
end; |
| 832 |
|
| 833 |
procedure TCocos2dLoader.CalculateFrameCoords(const CocosFrame: TCocosFrame); |
| 834 |
begin |
| 835 |
// 1st triangle |
| 836 |
FCoordArray[0] := Vector3(-CocosFrame.Width * (CocosFrame.AnchorX), |
| 837 |
CocosFrame.Height * (CocosFrame.AnchorY), 0); |
| 838 |
|
| 839 |
FCoordArray[1] := Vector3(CocosFrame.Width * (1 - CocosFrame.AnchorX), |
| 840 |
CocosFrame.Height * (CocosFrame.AnchorY), 0); |
| 841 |
|
| 842 |
FCoordArray[2] := Vector3(CocosFrame.Width * (1 - CocosFrame.AnchorX), |
| 843 |
-CocosFrame.Height * (1 - CocosFrame.AnchorY), 0); |
| 844 |
|
| 845 |
// 2nd triangle |
| 846 |
FCoordArray[3] := Vector3(-CocosFrame.Width * CocosFrame.AnchorX, |
| 847 |
CocosFrame.Height * CocosFrame.AnchorY, 0); |
| 848 |
|
| 849 |
FCoordArray[4] := Vector3(CocosFrame.Width * (1 - CocosFrame.AnchorX), |
| 850 |
-CocosFrame.Height * (1 - CocosFrame.AnchorY), 0); |
| 851 |
|
| 852 |
FCoordArray[5] := Vector3(-CocosFrame.Width * CocosFrame.AnchorX, |
| 853 |
-CocosFrame.Height * (1 - CocosFrame.AnchorY), 0); |
| 854 |
|
| 855 |
// 1st triangle |
| 856 |
FTexCoordArray[0] := CocosFrame.TexCoords[0]; |
| 857 |
FTexCoordArray[1] := CocosFrame.TexCoords[1]; |
| 858 |
FTexCoordArray[2] := CocosFrame.TexCoords[2]; |
| 859 |
|
| 860 |
// 2nd triangle |
| 861 |
FTexCoordArray[3] := CocosFrame.TexCoords[0]; |
| 862 |
FTexCoordArray[4] := CocosFrame.TexCoords[2]; |
| 863 |
FTexCoordArray[5] := CocosFrame.TexCoords[3]; |
| 864 |
end; |
| 865 |
|
| 866 |
procedure TCocos2dLoader.PrepareShape(const CoordArray: array of TVector3; |
| 867 |
const TexCoordArray: array of TVector2); |
| 868 |
var |
| 869 |
Shape: TShapeNode; |
| 870 |
Appearance: TAppearanceNode; |
| 871 |
Material: TUnlitMaterialNode; |
| 872 |
Tri: TTriangleSetNode; |
| 873 |
Tex: TImageTextureNode; |
| 874 |
TexProperties: TTexturePropertiesNode; |
| 875 |
begin |
| 876 |
Material := TUnlitMaterialNode.Create; |
| 877 |
|
| 878 |
Appearance := TAppearanceNode.Create; |
| 879 |
Appearance.Material := Material; |
| 880 |
|
| 881 |
Shape := TShapeNode.Create; |
| 882 |
Shape.Appearance := Appearance; |
| 883 |
|
| 884 |
Tex := TImageTextureNode.Create; |
| 885 |
Tex.FdUrl.Send(FImagePath); |
| 886 |
{ No point in adjusting RepeatS/T: TextureProperties override it. |
| 887 |
Tex.RepeatS := false; |
| 888 |
Tex.RepeatT := false; } |
| 889 |
Appearance.Texture := Tex; |
| 890 |
|
| 891 |
TexProperties := TTexturePropertiesNode.Create; |
| 892 |
TexProperties.MagnificationFilter := magDefault; |
| 893 |
TexProperties.MinificationFilter := minDefault; |
| 894 |
TexProperties.BoundaryModeS := bmClampToEdge; |
| 895 |
TexProperties.BoundaryModeT := bmClampToEdge; |
| 896 |
{ Do not force "power of 2" size, which may prevent mipmaps. |
| 897 |
This seems like a better default (otherwise the resizing underneath |
| 898 |
may cause longer loading time, and loss of quality, if not expected). |
| 899 |
See https://github.com/castle-engine/castle-engine/issues/249 } |
| 900 |
TexProperties.GuiTexture := true; |
| 901 |
Tex.TextureProperties := TexProperties; |
| 902 |
|
| 903 |
Tri := TTriangleSetNode.Create; |
| 904 |
Tri.Solid := false; |
| 905 |
|
| 906 |
FShapeCoord := TCoordinateNode.Create('coord'); |
| 907 |
FShapeCoord.SetPoint([ |
| 908 |
CoordArray[0], |
| 909 |
CoordArray[1], |
| 910 |
CoordArray[2], |
| 911 |
CoordArray[3], |
| 912 |
CoordArray[4], |
| 913 |
CoordArray[5]]); |
| 914 |
|
| 915 |
FShapeTexCoord := TTextureCoordinateNode.Create('texcoord'); |
| 916 |
FShapeTexCoord.SetPoint([ |
| 917 |
TexCoordArray[0], |
| 918 |
TexCoordArray[1], |
| 919 |
TexCoordArray[2], |
| 920 |
TexCoordArray[3], |
| 921 |
TexCoordArray[4], |
| 922 |
TexCoordArray[5]]); |
| 923 |
|
| 924 |
Tri.Coord := FShapeCoord; |
| 925 |
Tri.TexCoord := FShapeTexCoord; |
| 926 |
Shape.Geometry := Tri; |
| 927 |
|
| 928 |
FRoot.AddChildren(Shape); |
| 929 |
end; |
| 930 |
|
| 931 |
procedure TCocos2dLoader.AddFrameCoords( |
| 932 |
const CoordInterp: TCoordinateInterpolatorNode; |
| 933 |
const TexCoordInterp: TCoordinateInterpolator2DNode); |
| 934 |
begin |
| 935 |
CoordInterp.FdKeyValue.Items.AddRange(FCoordArray); |
| 936 |
TexCoordInterp.FdKeyValue.Items.AddRange(FTexCoordArray); |
| 937 |
{ Repeat all keyValues, to avoid interpolating them smoothly between two keys } |
| 938 |
CoordInterp.FdKeyValue.Items.AddRange(FCoordArray); |
| 939 |
TexCoordInterp.FdKeyValue.Items.AddRange(FTexCoordArray); |
| 940 |
end; |
| 941 |
|
| 942 |
procedure TCocos2dLoader.AddAnimation(const FrameCount: Integer; |
| 943 |
const TimeSensor: TTimeSensorNode; |
| 944 |
const CoordInterp: TCoordinateInterpolatorNode; |
| 945 |
const TexCoordInterp: TCoordinateInterpolator2DNode); |
| 946 |
var |
| 947 |
I: Integer; |
| 948 |
Key: Single; |
| 949 |
begin |
| 950 |
{ Set Cycle Interval becouse we know now frame count } |
| 951 |
TimeSensor.CycleInterval := FrameCount / FFramesPerSecond; |
| 952 |
|
| 953 |
{ Generate list of keys. } |
| 954 |
for I := 0 to FrameCount - 1 do |
| 955 |
begin |
| 956 |
Key := I / FrameCount; |
| 957 |
|
| 958 |
CoordInterp.FdKey.Items.Add(Key); |
| 959 |
TexCoordInterp.FdKey.Items.Add(Key); |
| 960 |
if I > 0 then |
| 961 |
begin |
| 962 |
CoordInterp.FdKey.Items.Add(Key); |
| 963 |
TexCoordInterp.FdKey.Items.Add(Key); |
| 964 |
end; |
| 965 |
end; |
| 966 |
|
| 967 |
{ This way, we have keys like |
| 968 |
0 0.333 0.333 0.666 0.666 1 |
| 969 |
That is, all keys are repeated, except 0 and 1. } |
| 970 |
CoordInterp.FdKey.Items.Add(1.0); |
| 971 |
TexCoordInterp.FdKey.Items.Add(1.0); |
| 972 |
|
| 973 |
{ Add TimeSensor, CoordinateInterpolatorNode, |
| 974 |
CoordinateInterpolator2DNode to Root node } |
| 975 |
FRoot.AddChildren(TimeSensor); |
| 976 |
FRoot.AddChildren(CoordInterp); |
| 977 |
FRoot.AddChildren(TexCoordInterp); |
| 978 |
AddRoutes(TimeSensor, CoordInterp, TexCoordInterp); |
| 979 |
end; |
| 980 |
|
| 981 |
procedure TCocos2dLoader.AddRoutes(const TimeSensor: TTimeSensorNode; |
| 982 |
const CoordInterp: TCoordinateInterpolatorNode; |
| 983 |
const TexCoordInterp: TCoordinateInterpolator2DNode); |
| 984 |
begin |
| 985 |
{ Create routes. } |
| 986 |
FRoot.AddRoute(TimeSensor.EventFraction_changed, CoordInterp.EventSet_fraction); |
| 987 |
FRoot.AddRoute(TimeSensor.EventFraction_changed, TexCoordInterp.EventSet_fraction); |
| 988 |
FRoot.AddRoute(CoordInterp.EventValue_changed, FShapeCoord.FdPoint); |
| 989 |
FRoot.AddRoute(TexCoordInterp.EventValue_changed, FShapeTexCoord.FdPoint); |
| 990 |
end; |
| 991 |
|
| 992 |
function TCocos2dLoader.CheckAnimationNameAvailable( |
| 993 |
const AnimationName: String): Boolean; |
| 994 |
begin |
| 995 |
if FAnimationList.IndexOf(AnimationName) > -1 then |
| 996 |
begin |
| 997 |
WritelnWarning('Starling', 'Mixed animations tags (animation: %s) in "%s".', |
| 998 |
[AnimationName, FDisplayUrl]); |
| 999 |
Exit(false); |
| 1000 |
end; |
| 1001 |
|
| 1002 |
FAnimationList.Add(AnimationName); |
| 1003 |
Result := true; |
| 1004 |
end; |
| 1005 |
|
| 1006 |
constructor TCocos2dLoader.Create(const Stream: TStream; const BaseUrl: String); |
| 1007 |
begin |
| 1008 |
inherited Create; |
| 1009 |
FStream := Stream; |
| 1010 |
FBaseUrl := BaseUrl; |
| 1011 |
FDisplayUrl := UriDisplay(FBaseUrl); |
| 1012 |
|
| 1013 |
FCocosFrame := TCocosFrame.Create(FDisplayUrl); |
| 1014 |
SetLength(FCoordArray, 6); |
| 1015 |
SetLength(FTexCoordArray, 6); |
| 1016 |
FAnimationList := TStringList.Create; |
| 1017 |
end; |
| 1018 |
|
| 1019 |
destructor TCocos2dLoader.Destroy; |
| 1020 |
begin |
| 1021 |
FreeAndNil(FCocosFrame); |
| 1022 |
FreeAndNil(FAnimationList); |
| 1023 |
inherited Destroy; |
| 1024 |
end; |
| 1025 |
|
| 1026 |
function TCocos2dLoader.Load: TX3DRootNode; |
| 1027 |
var |
| 1028 |
Doc: TXMLDocument; |
| 1029 |
Node: TDOMNode; |
| 1030 |
PlistNode: TDOMElement; |
| 1031 |
PlistDictNode: TDOMElement; |
| 1032 |
FramesDictNode: TDOMElement; // available on format ver 1, 2, 3 |
| 1033 |
MetadataDictNode: TDOMElement; // available on format ver 2, 3 |
| 1034 |
TextureDictNode: TDOMElement; // available on format 1 |
| 1035 |
|
| 1036 |
KeyNode: TDOMElement; |
| 1037 |
DictNode: TDOMElement; |
| 1038 |
|
| 1039 |
I: TXMLElementIterator; |
| 1040 |
begin |
| 1041 |
ReadImportSettings; |
| 1042 |
|
| 1043 |
FRoot := nil; |
| 1044 |
Doc := nil; |
| 1045 |
PlistNode := nil; |
| 1046 |
FramesDictNode := nil; |
| 1047 |
MetadataDictNode := nil; |
| 1048 |
TextureDictNode := nil; |
| 1049 |
try |
| 1050 |
try |
| 1051 |
FRoot := TX3DRootNode.Create; |
| 1052 |
ReadXMLFile(Doc, FStream); |
| 1053 |
|
| 1054 |
{ Doc.FindNode('plist') can fail here because of plist Apple DOCTYPE |
| 1055 |
above plist element. } |
| 1056 |
{$ifdef FPC} |
| 1057 |
Node := Doc.FirstChild; |
| 1058 |
while Node <> nil do |
| 1059 |
begin |
| 1060 |
if (Node.NodeName = 'plist') and (Node.NodeType = ELEMENT_NODE) then |
| 1061 |
begin |
| 1062 |
PlistNode := Node as TDOMElement; |
| 1063 |
break; |
| 1064 |
end; |
| 1065 |
Node := Node.NextSibling; |
| 1066 |
end; |
| 1067 |
{$else} |
| 1068 |
// Should work in delphi because current we have only one element in document |
| 1069 |
Node := Doc.DocumentElement; |
| 1070 |
if (Node.NodeName = 'plist') and (Node.NodeType = ELEMENT_NODE) then |
| 1071 |
PlistNode := Node as TDOMElement; |
| 1072 |
{$endif} |
| 1073 |
|
| 1074 |
if PlistNode = nil then |
| 1075 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - plist node not found.', [FDisplayUrl]); |
| 1076 |
|
| 1077 |
{ With Required = false just return nil, because we want to raise a nicer |
| 1078 |
exception. } |
| 1079 |
PlistDictNode := PlistNode.Child('dict', false); |
| 1080 |
if (PlistDictNode = nil) then |
| 1081 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - plist dictionary node not found.', [FDisplayUrl]); |
| 1082 |
|
| 1083 |
{ Iterate file to find frames, metadata, textures nodes } |
| 1084 |
I := PlistDictNode.ChildrenIterator; |
| 1085 |
try |
| 1086 |
while I.GetNext do |
| 1087 |
begin |
| 1088 |
{ Should be key node. } |
| 1089 |
KeyNode := I.Current; |
| 1090 |
if KeyNode.NodeName <> 'key' then |
| 1091 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - key node expected.', [FDisplayUrl]); |
| 1092 |
|
| 1093 |
if not I.GetNext then |
| 1094 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - dict node expected.', [FDisplayUrl]); |
| 1095 |
|
| 1096 |
DictNode := I.Current; |
| 1097 |
|
| 1098 |
if DictNode.NodeName <> 'dict' then |
| 1099 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - dict node expected.', [FDisplayUrl]); |
| 1100 |
|
| 1101 |
{ Check KeyNode.TextData to decide what this is metadata, texture or frames} |
| 1102 |
if KeyNode.TextData = 'frames' then |
| 1103 |
FramesDictNode := DictNode |
| 1104 |
else |
| 1105 |
if KeyNode.TextData = 'textures' then |
| 1106 |
TextureDictNode := DictNode |
| 1107 |
else |
| 1108 |
if KeyNode.TextData = 'metadata' then |
| 1109 |
MetadataDictNode := DictNode; |
| 1110 |
end; |
| 1111 |
finally |
| 1112 |
FreeAndNil(I); |
| 1113 |
end; |
| 1114 |
|
| 1115 |
{ TextureDictNode exists only in Cocos2d files version 1 which are |
| 1116 |
outdated/unsupported. We can't simply check metadata because version 1 |
| 1117 |
don't have metadata dictionary. Frames also have different structure. |
| 1118 |
The files of this version (1) are so outdated that nobody will ever need |
| 1119 |
to use them (I think). } |
| 1120 |
if TextureDictNode <> nil then |
| 1121 |
raise EInvalidCocos2dPlist.CreateFmt('Cocos2d plist file "%s" - file version 1 is unsupported.', [FDisplayUrl]); |
| 1122 |
|
| 1123 |
{ First read metadata - texture properties } |
| 1124 |
if MetadataDictNode = nil then |
| 1125 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - metadata dict node not found.', [FDisplayUrl]); |
| 1126 |
|
| 1127 |
ReadMetadata(MetadataDictNode); |
| 1128 |
|
| 1129 |
{ Now when we know texture properties and format number so we can |
| 1130 |
set CocoSFrame to use suitable ParseFrame procedure. } |
| 1131 |
FCocosFrame.SetCocosFormat(FCocosFormat); |
| 1132 |
|
| 1133 |
{ Now we can read frames } |
| 1134 |
if FramesDictNode = nil then |
| 1135 |
raise EInvalidCocos2dPlist.CreateFmt('Invalid Cocos2d plist file "%s" - frames dict node not found.', [FDisplayUrl]); |
| 1136 |
|
| 1137 |
ReadFrames(FramesDictNode); |
| 1138 |
|
| 1139 |
Result := FRoot; |
| 1140 |
except |
| 1141 |
FreeAndNil(FRoot); |
| 1142 |
raise; |
| 1143 |
end; |
| 1144 |
finally |
| 1145 |
FreeAndNil(Doc); |
| 1146 |
end; |
| 1147 |
end; |
| 1148 |
|
| 1149 |
var |
| 1150 |
ModelFormat: TModelFormat; |
| 1151 |
initialization |
| 1152 |
ModelFormat := TModelFormat.Create; |
| 1153 |
ModelFormat.OnLoad := {$ifdef FPC}@{$endif} LoadCocos2d; |
| 1154 |
ModelFormat.MimeTypes.Add('application/x-plist'); |
| 1155 |
ModelFormat.MimeTypes.Add('application/x-cocos2d-sprite-sheet'); |
| 1156 |
ModelFormat.FileFilterName := 'Cocos2d Sprite Sheet (*.cocos2d-plist, *.plist)'; |
| 1157 |
ModelFormat.Extensions.Add('.cocos2d-plist'); |
| 1158 |
ModelFormat.Extensions.Add('.plist'); |
| 1159 |
RegisterModelFormat(ModelFormat); |
| 1160 |
end. |