| 1 |
{ |
| 2 |
Copyright 2020-2024 Andrzej Kilijanski, Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Sprite sheet representation for loading and editing purposes (TCastleSpriteSheet). } |
| 17 |
unit CastleInternalSpriteSheet; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses |
| 24 |
Classes, SysUtils, Generics.Collections, |
| 25 |
X3DNodes, X3DLoad, CastleImages, CastleVectors; |
| 26 |
|
| 27 |
type |
| 28 |
TCastleSpriteSheetAnimation = class; |
| 29 |
TCastleSpriteSheetFrame = class; |
| 30 |
|
| 31 |
TCastleSpriteSheetAnimationList = {$ifdef FPC}specialize{$endif} TObjectList<TCastleSpriteSheetAnimation>; |
| 32 |
TCastleSpriteSheetFrameList = {$ifdef FPC}specialize{$endif} TObjectList<TCastleSpriteSheetFrame>; |
| 33 |
|
| 34 |
TCastleSpriteSheetAbstractAtlasGen = class; |
| 35 |
|
| 36 |
TCastleSpriteSheetFrameEvent = procedure (Frame: TCastleSpriteSheetFrame) of object; |
| 37 |
TCastleSpriteSheetFrameMoveEvent = procedure ( |
| 38 |
const Frame: TCastleSpriteSheetFrame; |
| 39 |
const OldIndex, NewIndex: Integer) of object; |
| 40 |
TCastleSpriteSheetAnimationEvent = procedure ( |
| 41 |
Animation: TCastleSpriteSheetAnimation) of object; |
| 42 |
TCastleSpriteSheetAnimationMoveEvent = procedure ( |
| 43 |
const Animation: TCastleSpriteSheetAnimation; |
| 44 |
const OldIndex, NewIndex: Integer) of object; |
| 45 |
TCastleSpriteSheetSizeChanged = procedure ( |
| 46 |
const Width, Height: Integer) of object; |
| 47 |
TURLContentChanged = procedure (const ChangedURL: String); |
| 48 |
|
| 49 |
{ Sprite sheet representation for loading and editing purposes. } |
| 50 |
TCastleSpriteSheet = class |
| 51 |
strict private |
| 52 |
FAnimationList: TCastleSpriteSheetAnimationList; |
| 53 |
|
| 54 |
FURL: String; |
| 55 |
FLoadedAtlasPath: String; |
| 56 |
FRelativeAtlasPath: String; |
| 57 |
|
| 58 |
FEditMode: Boolean; |
| 59 |
|
| 60 |
FModifiedState: Boolean; |
| 61 |
FLoadingPending: Boolean; |
| 62 |
|
| 63 |
FOnURLChanged: TNotifyEvent; |
| 64 |
FOnModifiedStateChanged: TNotifyEvent; |
| 65 |
FOnAnimationAdded: TCastleSpriteSheetAnimationEvent; |
| 66 |
FOnAnimationMoved: TCastleSpriteSheetAnimationMoveEvent; |
| 67 |
FBeforeAnimationRemoved: TCastleSpriteSheetAnimationEvent; |
| 68 |
FBeforeAnimationFrameRemoved: TCastleSpriteSheetFrameEvent; |
| 69 |
FOnFrameAdded: TCastleSpriteSheetFrameEvent; |
| 70 |
FOnFrameMoved: TCastleSpriteSheetFrameMoveEvent; |
| 71 |
FOnMaxAtlasSizeChanged: TCastleSpriteSheetSizeChanged; |
| 72 |
FOnFileSaved: TURLContentChanged; |
| 73 |
|
| 74 |
private |
| 75 |
{ Size of atlas after loading } |
| 76 |
FAtlasWidth: Integer; |
| 77 |
FAtlasHeight: Integer; |
| 78 |
{ Max atlas size } |
| 79 |
FMaxAtlasWidth: Integer; |
| 80 |
FMaxAtlasHeight: Integer; |
| 81 |
|
| 82 |
FGeneratedAtlas: TCastleImage; |
| 83 |
|
| 84 |
procedure SetURL(const NewURL: String); |
| 85 |
|
| 86 |
protected |
| 87 |
{ Sets sprite sheet state as modified. } |
| 88 |
procedure SetModifiedState; |
| 89 |
{ Clears modified state e.g. after saving } |
| 90 |
procedure ClearModifiedState; |
| 91 |
{ Blocks ModifiedState changing } |
| 92 |
procedure BeginLoad; |
| 93 |
{ Removes ModifiedState changing blocking } |
| 94 |
procedure EndLoad; |
| 95 |
{ Throws EReadOnlyMode exception when EditMode = false } |
| 96 |
procedure CheckEditMode; |
| 97 |
public |
| 98 |
constructor Create(AEditMode: Boolean); |
| 99 |
destructor Destroy; override; |
| 100 |
|
| 101 |
{ Returns true if spritesheet was modified } |
| 102 |
function IsModified: Boolean; |
| 103 |
|
| 104 |
procedure Load(const URL: String); overload; |
| 105 |
procedure Load(const Stream: TStream; const BaseUrl: String); overload; |
| 106 |
{ Saves file to castle sprite sheet. If SaveCopy = true then don't clear |
| 107 |
Modified state, don't change URL, don't save image paths. } |
| 108 |
procedure Save(const AURL: String; const SaveCopy: Boolean = false); |
| 109 |
|
| 110 |
function ToX3D: TX3DRootNode; |
| 111 |
|
| 112 |
{ Arranges and creates atlas image } |
| 113 |
procedure RegenerateAtlas; |
| 114 |
{ This checks: Have all frames loaded frame images? } |
| 115 |
function AtlasCanBeRegenrated: Boolean; |
| 116 |
procedure SetMaxAtlasSize(const NewMaxAtlasWidth, |
| 117 |
NewMaxAtlasHeight: Integer); |
| 118 |
|
| 119 |
procedure GetMinAtlasSize(out MinWidth, MinHeight: Integer); |
| 120 |
|
| 121 |
function AnimationByName(const Name:String): TCastleSpriteSheetAnimation; |
| 122 |
function AnimationByIndex(const Index: Integer): TCastleSpriteSheetAnimation; |
| 123 |
function AnimationCount: Integer; |
| 124 |
function AnimationIndex(const Animation: TCastleSpriteSheetAnimation): Integer; |
| 125 |
function HasAnimation(const Name:String): Boolean; |
| 126 |
function AddAnimation(const Name:String): TCastleSpriteSheetAnimation; |
| 127 |
procedure MoveAnimationUp(const Animation: TCastleSpriteSheetAnimation); |
| 128 |
procedure MoveAnimationDown(const Animation: TCastleSpriteSheetAnimation); |
| 129 |
procedure MoveAnimationToTop(const Animation: TCastleSpriteSheetAnimation); |
| 130 |
procedure MoveAnimationToEnd(const Animation: TCastleSpriteSheetAnimation); |
| 131 |
procedure MoveAnimation(const OldIndex, NewIndex: Integer); |
| 132 |
procedure RemoveAnimation(const Animation: TCastleSpriteSheetAnimation); |
| 133 |
procedure RemoveAnimationByName(const Name: String); |
| 134 |
procedure RemoveAnimationByIndex(const Index: Integer); |
| 135 |
function ProposeAnimationName: String; |
| 136 |
|
| 137 |
{ Last Load/Save URL. Can be empty when loaded from Starling file. } |
| 138 |
property URL: String read FURL write SetURL; |
| 139 |
{ Full image path to loaded image, if empty sprite sheet was created from |
| 140 |
scratch. } |
| 141 |
property LoadedAtlasPath: String read FLoadedAtlasPath write FLoadedAtlasPath; |
| 142 |
{ Image name with path relative to Castle Sprite Sheet/Starling XML file. |
| 143 |
Usually image is located next to Castle Sprite Sheet/Starling XML so |
| 144 |
this is only file name but it's not a rule. This file name/path is |
| 145 |
saved in Castle Sprite Sheet/Starling XML file. |
| 146 |
|
| 147 |
Can be empty when sprite sheet was created from scratch and never saved. } |
| 148 |
property RelativeAtlasPath: String read FRelativeAtlasPath |
| 149 |
write FRelativeAtlasPath; |
| 150 |
|
| 151 |
property OnURLChanged: TNotifyEvent read FOnURLChanged write FOnURLChanged; |
| 152 |
|
| 153 |
{ OnFileSaved event can be used for reload TCastleScenes with this sprite |
| 154 |
sheet loaded } |
| 155 |
property OnFileSaved: TURLContentChanged read FOnFileSaved |
| 156 |
write FOnFileSaved; |
| 157 |
|
| 158 |
property OnModifiedStateChanged: TNotifyEvent read FOnModifiedStateChanged |
| 159 |
write FOnModifiedStateChanged; |
| 160 |
|
| 161 |
property OnAnimationAdded: TCastleSpriteSheetAnimationEvent |
| 162 |
read FOnAnimationAdded write FOnAnimationAdded; |
| 163 |
|
| 164 |
property OnAnimationMoved: TCastleSpriteSheetAnimationMoveEvent |
| 165 |
read FOnAnimationMoved write FOnAnimationMoved; |
| 166 |
|
| 167 |
property BeforeAnimationRemoved: TCastleSpriteSheetAnimationEvent |
| 168 |
read FBeforeAnimationRemoved write FBeforeAnimationRemoved; |
| 169 |
|
| 170 |
property BeforeFrameRemoved: TCastleSpriteSheetFrameEvent read |
| 171 |
FBeforeAnimationFrameRemoved write FBeforeAnimationFrameRemoved; |
| 172 |
|
| 173 |
property OnFrameAdded: TCastleSpriteSheetFrameEvent read FOnFrameAdded |
| 174 |
write FOnFrameAdded; |
| 175 |
|
| 176 |
property OnFrameMoved: TCastleSpriteSheetFrameMoveEvent read FOnFrameMoved |
| 177 |
write FOnFrameMoved; |
| 178 |
|
| 179 |
property OnMaxAtlasSizeChanged: TCastleSpriteSheetSizeChanged read |
| 180 |
FOnMaxAtlasSizeChanged write FOnMaxAtlasSizeChanged; |
| 181 |
|
| 182 |
property AtlasWidth: Integer read FAtlasWidth; |
| 183 |
property AtlasHeight: Integer read FAtlasHeight; |
| 184 |
property MaxAtlasWidth: Integer read FMaxAtlasWidth; |
| 185 |
property MaxAtlasHeight: Integer read FMaxAtlasHeight; |
| 186 |
property EditMode: Boolean read FEditMode; |
| 187 |
end; |
| 188 |
|
| 189 |
{ Sprite sheet animation, part of TCastleSpriteSheet. } |
| 190 |
TCastleSpriteSheetAnimation = class |
| 191 |
strict private |
| 192 |
FName: String; |
| 193 |
FFramesPerSecond: Single; |
| 194 |
FFrameList: TCastleSpriteSheetFrameList; |
| 195 |
FSpriteSheet: TCastleSpriteSheet; |
| 196 |
|
| 197 |
procedure SetName(const NewName: String); |
| 198 |
procedure SetFramesPerSecond(const NewFPS: Single); |
| 199 |
|
| 200 |
function GetFrame(const Index: Integer): TCastleSpriteSheetFrame; |
| 201 |
|
| 202 |
private |
| 203 |
{ Sets sprite sheet state as modified. } |
| 204 |
procedure SetModifiedState; |
| 205 |
|
| 206 |
{ Throws EReadOnlyMode exception when EditMode = false } |
| 207 |
procedure CheckEditMode; |
| 208 |
public |
| 209 |
constructor Create(SpriteSheet: TCastleSpriteSheet; AName: String); |
| 210 |
destructor Destroy; override; |
| 211 |
|
| 212 |
function FrameCount: Integer; |
| 213 |
function FrameIndex(const Frame: TCastleSpriteSheetFrame): Integer; |
| 214 |
function AddFrame: TCastleSpriteSheetFrame; overload; |
| 215 |
function AddFrameCopy(const SrcFrame: TCastleSpriteSheetFrame |
| 216 |
): TCastleSpriteSheetFrame; |
| 217 |
function AddFrame(const FrameImageURL: String): |
| 218 |
TCastleSpriteSheetFrame; overload; |
| 219 |
function AddFrame(const SourceImage: TCastleImage; const DestX, DestY, |
| 220 |
AFrameWidth, AFrameHeight, SourceX, SourceY, |
| 221 |
SourceWidthToCopy, SourceHeightToCopy: Integer): |
| 222 |
TCastleSpriteSheetFrame; overload; |
| 223 |
function AllFramesHasTheSameSize: Boolean; |
| 224 |
procedure RemoveFrame(const Frame: TCastleSpriteSheetFrame); |
| 225 |
procedure MoveFrameLeft(const Frame: TCastleSpriteSheetFrame); |
| 226 |
procedure MoveFrameRight(const Frame: TCastleSpriteSheetFrame); |
| 227 |
procedure MoveFrameToTop(const Frame: TCastleSpriteSheetFrame); |
| 228 |
procedure MoveFrameToEnd(const Frame: TCastleSpriteSheetFrame); |
| 229 |
procedure MoveFrame(const OldIndex, NewIndex: Integer); |
| 230 |
procedure ImportAtlas(AtlasImageURL: String; Cols, Rows: Integer; |
| 231 |
ImportByColumns: Boolean); |
| 232 |
function GetBigestFrameSize(const MaxWidth, MaxHeight: Integer): TVector2Integer; |
| 233 |
|
| 234 |
property Name: String read FName write SetName; |
| 235 |
property Frame[const Index: Integer]: TCastleSpriteSheetFrame read GetFrame; |
| 236 |
property FramesPerSecond: Single read FFramesPerSecond write SetFramesPerSecond; |
| 237 |
end; |
| 238 |
|
| 239 |
{ Sprite sheet animation frame, part of TCastleSpriteSheetAnimation. } |
| 240 |
TCastleSpriteSheetFrame = class |
| 241 |
strict private |
| 242 |
FAnimation: TCastleSpriteSheetAnimation; |
| 243 |
|
| 244 |
FXInAtlas: Integer; // x in atlas |
| 245 |
FYInAtlas: Integer; // y in atlas |
| 246 |
FWidthInAtlas: Integer; // width in atlas, this always will be shape width |
| 247 |
FHeightInAtlas: Integer; // height in atlas, this always will be shape height |
| 248 |
|
| 249 |
{ When frame is trimmed, then atlas rect is not full size of the |
| 250 |
frame. So our shape is smaller too, to make it apear in right place we |
| 251 |
need shift all the coordinates of the our shape. We do that by calculating |
| 252 |
anchor from LeftOffset, FrameWidth, and shift vertices coordinates. |
| 253 |
See TCastleSpriteSheetX3DExporter.CalculateAnchors(). |
| 254 |
|
| 255 |
In default Anchor is 0.5, 0.5 so shape is centered, when |
| 256 |
(FrameWidth - WidthInAtlas) / 2 = LeftOffset anchor will be 0.5 |
| 257 |
but when LeftOffset * 2 + WidthInAtlas <> FrameWidth anchor will change |
| 258 |
and shape coords will be shifted. |
| 259 |
|
| 260 |
Note that the left offset may be different from the right one. |
| 261 |
Likewise, top and bottom offset. Right and bottom offset is not saved in |
| 262 |
Starling XML. |
| 263 |
} |
| 264 |
FTrimmed: Boolean; // is frame trimmed ? |
| 265 |
FLeftOffset: Integer; // relative x cord offset, used for trimming (left offset) |
| 266 |
FTopOffset: Integer; // relative y cord offset, used for trimming (top offset) |
| 267 |
FFrameWidth: Integer; // real frame width |
| 268 |
FFrameHeight: Integer; // real frame height |
| 269 |
|
| 270 |
FFrameImage: TCastleImage; // Full frame image (even trimmed here is with margins) |
| 271 |
|
| 272 |
procedure SetXInAtlas(const NewX: Integer); |
| 273 |
procedure SetYInAtlas(const NewY: Integer); |
| 274 |
procedure SetWidthInAtlas(const NewWidth: Integer); |
| 275 |
procedure SetHeightInAtlas(const NewHeight: Integer); |
| 276 |
procedure SetLeftOffset(const NewLeftOffset: Integer); |
| 277 |
procedure SetTopOffset(const NewTopOffset: Integer); |
| 278 |
procedure SetFrameWidth(const NewFrameWidth: Integer); |
| 279 |
procedure SetFrameHeight(const NewFrameHeight: Integer); |
| 280 |
procedure SetTrimmed(const NewTrimmed: Boolean); |
| 281 |
|
| 282 |
{ Sets sprite sheet state as modified. } |
| 283 |
procedure SetModifiedState; |
| 284 |
procedure CheckEditMode; |
| 285 |
protected |
| 286 |
{ Currently only reset to full frame size } |
| 287 |
procedure UpdateTrimming; |
| 288 |
public |
| 289 |
constructor Create(const Animation: TCastleSpriteSheetAnimation); overload; |
| 290 |
{ Construct full/deep copy of SrcFrame } |
| 291 |
constructor Create(const Animation: TCastleSpriteSheetAnimation; |
| 292 |
const SrcFrame: TCastleSpriteSheetFrame); overload; |
| 293 |
destructor Destroy; override; |
| 294 |
|
| 295 |
function HasFrameImage: Boolean; |
| 296 |
|
| 297 |
{ Copies image and sets frame size. } |
| 298 |
procedure SetFrameImage(const SourceImage: TCastleImage); overload; |
| 299 |
|
| 300 |
{ Copies a fragment of the image and sets the image parameters } |
| 301 |
procedure SetFrameImage(const SourceImage: TCastleImage; const DestX, DestY, |
| 302 |
AFrameWidth, AFrameHeight, SourceX, SourceY, |
| 303 |
SourceWidthToCopy, SourceHeightToCopy: Integer); overload; |
| 304 |
|
| 305 |
procedure DrawToImage(const DestImage: TCastleImage; const DestX, DestY, |
| 306 |
SourceX, SourceY, SourceWidthToDraw, SourceHeightToDraw: Integer); |
| 307 |
|
| 308 |
function MakeResized(const Width, Height: Integer): TCastleImage; |
| 309 |
function MakeResizedWithBg(const Width, Height: Integer; |
| 310 |
const BgColor: TVector4): TCastleImage; |
| 311 |
function MakeImageCopy: TCastleImage; |
| 312 |
function MakeImageCopyWithBg(const BgColor: TVector4): TCastleImage; |
| 313 |
function CenterOnBiggerImage(const Width, Height: Integer; |
| 314 |
const BgColor: TVector4): TCastleImage; |
| 315 |
|
| 316 |
procedure SaveFrameImage(const URL: String); |
| 317 |
|
| 318 |
class function FlipYCoordToCGE(StarlingY, ImageHeight:Integer): |
| 319 |
Integer; overload; |
| 320 |
class function FlipYCoordToCGE(StarlingY, Height, ImageHeight:Integer): |
| 321 |
Integer; overload; |
| 322 |
|
| 323 |
property XInAtlas: Integer read FXInAtlas write SetXInAtlas; |
| 324 |
property YInAtlas: Integer read FYInAtlas write SetYInAtlas; |
| 325 |
property WidthInAtlas: Integer read FWidthInAtlas write SetWidthInAtlas; |
| 326 |
property HeightInAtlas: Integer read FHeightInAtlas write SetHeightInAtlas; |
| 327 |
property LeftOffset: Integer read FLeftOffset write SetLeftOffset; |
| 328 |
property TopOffset: Integer read FTopOffset write SetTopOffset; |
| 329 |
property FrameWidth: Integer read FFrameWidth write SetFrameWidth; |
| 330 |
property FrameHeight: Integer read FFrameHeight write SetFrameHeight; |
| 331 |
property Trimmed: Boolean read FTrimmed write SetTrimmed; |
| 332 |
property Animation: TCastleSpriteSheetAnimation read FAnimation; |
| 333 |
end; |
| 334 |
|
| 335 |
{ Abstract class for frame image "arranger", it enables the implementation |
| 336 |
of many algorithms } |
| 337 |
TCastleSpriteSheetAbstractAtlasGen = class |
| 338 |
protected |
| 339 |
FSpriteSheet: TCastleSpriteSheet; |
| 340 |
FSpriteSheetMaxWidth: Integer; |
| 341 |
FSpriteSheetMaxHeight: Integer; |
| 342 |
public |
| 343 |
constructor Create(const ASpriteSheet: TCastleSpriteSheet; |
| 344 |
const MaxWidth, MaxHeight: Integer); |
| 345 |
function WillFramesFitInSize( |
| 346 |
AtlasWidth, AtlasHeight: Integer): Boolean; virtual; abstract; |
| 347 |
procedure GetMinAtlasSize( |
| 348 |
out MinWidth, MinHeight: Integer); virtual; abstract; |
| 349 |
procedure Generate; virtual; abstract; |
| 350 |
end; |
| 351 |
|
| 352 |
{ Most simple implementation of frame arranger - for debug purposes } |
| 353 |
TCastleSpriteSheetBasicAtlasGen = class (TCastleSpriteSheetAbstractAtlasGen) |
| 354 |
private |
| 355 |
type |
| 356 |
TLayoutOperation = ( |
| 357 |
loMeasure, |
| 358 |
loArrange |
| 359 |
); |
| 360 |
function LayoutFrames(const Operation: TLayoutOperation; |
| 361 |
const MaxAtlasWidth, MaxAtlasHeight: Integer; |
| 362 |
out MinAtlasWidth, MinAtlasHeight: Integer): Boolean; |
| 363 |
procedure GenerateAtlas(const AtlasWidth, AtlasHeight: Integer); |
| 364 |
public |
| 365 |
function WillFramesFitInSize( |
| 366 |
AtlasWidth, AtlasHeight: Integer): Boolean; override; |
| 367 |
procedure GetMinAtlasSize(out MinWidth, MinHeight: Integer); override; |
| 368 |
procedure Generate; override; |
| 369 |
end; |
| 370 |
|
| 371 |
{ TODO: Advanced sprite sheet generator with all features like trimming, |
| 372 |
padding and everything else we want } |
| 373 |
TCastleSpriteSheetAdvancedImageGen = class (TCastleSpriteSheetAbstractAtlasGen) |
| 374 |
public |
| 375 |
procedure Generate; override; |
| 376 |
end; |
| 377 |
|
| 378 |
{ Castle Sprite Sheet XML file is not correct } |
| 379 |
EInvalidCastleSpriteSheetXml = class(Exception); |
| 380 |
ECastleSpriteSheetAtlasToSmall = class(Exception); |
| 381 |
ECantRegenerateAtlas = class(Exception); |
| 382 |
EReadOnlyMode = class(Exception); |
| 383 |
|
| 384 |
function LoadCastleSpriteSheet(const Stream: TStream; const BaseUrl: String): TX3DRootNode; |
| 385 |
|
| 386 |
implementation |
| 387 |
|
| 388 |
uses StrUtils, DOM, Math, XMLRead, |
| 389 |
CastleDownload, CastleFilesUtils, CastleLog, CastleStringUtils, |
| 390 |
CastleTextureImages, CastleUriUtils, CastleUtils, CastleXmlUtils; |
| 391 |
|
| 392 |
type |
| 393 |
{ Frame names in starling file can be named freely, but in the case of our loader, |
| 394 |
we have to define what is the next frame of the animation and what should |
| 395 |
be recognized as a separate animation. |
| 396 |
See https://castle-engine.io/sprite_sheets } |
| 397 |
TAnimationNaming = ( |
| 398 |
{ Default behavior for Castle Sprite Sheet and Starling. It treats |
| 399 |
as animation frames only those subtextures whose names ends with |
| 400 |
an underscore followed by a number. } |
| 401 |
anStrictUnderscore, |
| 402 |
{ In many cases, the consecutive frames of one animation are named |
| 403 |
without underscore. Only for Starling files. } |
| 404 |
anTralingNumber |
| 405 |
); |
| 406 |
|
| 407 |
TCastleSpriteSheetLoader = class |
| 408 |
strict private |
| 409 |
type |
| 410 |
{ Class that represents SubTexture from Starling xml file } |
| 411 |
TSubTexture = class |
| 412 |
private |
| 413 |
FAnimationNaming: TAnimationNaming; |
| 414 |
procedure ParseAnimationName(const SubTextureName: String); |
| 415 |
public |
| 416 |
AnimationName: String; |
| 417 |
X: Integer; |
| 418 |
Y: Integer; |
| 419 |
Width: Integer; // width in texture |
| 420 |
Height: Integer; // height in texture |
| 421 |
FrameX: Integer; // left offset (for trimming) |
| 422 |
FrameY: Integer; // top offset (for trimming) |
| 423 |
FrameWidth: Integer; |
| 424 |
FrameHeight: Integer; |
| 425 |
Trimmed: Boolean; |
| 426 |
|
| 427 |
procedure ReadFormXMLNode(const SubTextureNode: TDOMElement; |
| 428 |
const ImageWidth, ImageHeight: Integer); |
| 429 |
constructor Create; |
| 430 |
end; |
| 431 |
|
| 432 |
var |
| 433 |
FStream: TStream; |
| 434 |
FBaseUrl: String; |
| 435 |
FDisplayURL: String; |
| 436 |
|
| 437 |
{ Load settings. } |
| 438 |
FFramesPerSecond: Single; |
| 439 |
|
| 440 |
FImageWidth, FImageHeight: Integer; |
| 441 |
{ Image name with full path } |
| 442 |
FAbsoluteImagePath: String; |
| 443 |
{ Image name from CastleSpriteSheet/Starling file } |
| 444 |
FRelativeImagePath: String; |
| 445 |
|
| 446 |
FSubTexture: TSubTexture; |
| 447 |
{ True means that we will load extra edit fields and create |
| 448 |
images in frames } |
| 449 |
FLoadForEdit: Boolean; |
| 450 |
{ True means that we attemp to load Starling file } |
| 451 |
FStarlingLoading : Boolean; |
| 452 |
|
| 453 |
FImage: TCastleImage; |
| 454 |
|
| 455 |
procedure ReadImportSettings; |
| 456 |
|
| 457 |
procedure ReadImageProperties(const URL: String; const AtlasNode: TDOMElement); |
| 458 |
|
| 459 |
procedure ReadFramesPerSecond(const SubTextureNode: TDOMElement); |
| 460 |
|
| 461 |
procedure PrepareSpriteSheet(const SpriteSheet: TCastleSpriteSheet); |
| 462 |
|
| 463 |
procedure AddFrame(const Animation: TCastleSpriteSheetAnimation); |
| 464 |
public |
| 465 |
constructor Create(const Stream: TStream; const BaseUrl: String; |
| 466 |
LoadForEdit: Boolean); |
| 467 |
destructor Destroy; override; |
| 468 |
|
| 469 |
procedure Load(const SpriteSheet: TCastleSpriteSheet); overload; |
| 470 |
function Load: TCastleSpriteSheet; overload; |
| 471 |
end; |
| 472 |
|
| 473 |
TCastleSpriteSheetXMLExporter = class |
| 474 |
strict private |
| 475 |
FSpriteSheet: TCastleSpriteSheet; |
| 476 |
public |
| 477 |
constructor Create(const SpriteSheet: TCastleSpriteSheet); |
| 478 |
|
| 479 |
function ExportToXML: TXMLDocument; |
| 480 |
end; |
| 481 |
|
| 482 |
TCastleSpriteSheetX3DExporter = class |
| 483 |
strict private |
| 484 |
FRoot: TX3DRootNode; |
| 485 |
FShapeCoord: TCoordinateNode; |
| 486 |
FShapeTexCoord: TTextureCoordinateNode; |
| 487 |
|
| 488 |
FCoordArray: array [0..3] of TVector3; |
| 489 |
FTexCoordArray: array [0..3] of TVector2; |
| 490 |
|
| 491 |
TimeSensor: TTimeSensorNode; |
| 492 |
CoordInterp: TCoordinateInterpolatorNode; |
| 493 |
TexCoordInterp: TCoordinateInterpolator2DNode; |
| 494 |
|
| 495 |
FSpriteSheet: TCastleSpriteSheet; |
| 496 |
|
| 497 |
procedure PrepareContainer; |
| 498 |
|
| 499 |
{ In case of X3D here we calculate anchors and set frame cords } |
| 500 |
procedure CalculateFrameCoords(const Frame: TCastleSpriteSheetFrame); |
| 501 |
|
| 502 |
{ In case of X3D here we create TimeSensor, CoordInterp, |
| 503 |
TexCoordInterp for animation } |
| 504 |
procedure PrepareAnimation(const Name: String); |
| 505 |
{ In case of X3D here we add TimeSensor, CoordInterp, TexCoordInterp |
| 506 |
and routes to root } |
| 507 |
procedure AddAnimation(const Animation: TCastleSpriteSheetAnimation); |
| 508 |
{ In case of X3D here we add frame coords to CoordInterp and TexCoordInterp } |
| 509 |
procedure AddFrame; |
| 510 |
public |
| 511 |
constructor Create(const SpriteSheet: TCastleSpriteSheet); |
| 512 |
|
| 513 |
function ExportToX3D: TX3DRootNode; |
| 514 |
end; |
| 515 |
|
| 516 |
function LoadCastleSpriteSheet(const Stream: TStream; const BaseUrl: String): TX3DRootNode; |
| 517 |
var |
| 518 |
SpriteSheet: TCastleSpriteSheet; |
| 519 |
begin |
| 520 |
SpriteSheet := TCastleSpriteSheet.Create(false); |
| 521 |
try |
| 522 |
SpriteSheet.Load(Stream, BaseUrl); |
| 523 |
Result := SpriteSheet.ToX3D; |
| 524 |
finally |
| 525 |
FreeAndNil(SpriteSheet); |
| 526 |
end; |
| 527 |
end; |
| 528 |
|
| 529 |
{ TCastleSpriteSheetX3DExporter } |
| 530 |
|
| 531 |
procedure TCastleSpriteSheetX3DExporter.PrepareContainer; |
| 532 |
var |
| 533 |
Shape: TShapeNode; |
| 534 |
Material: TUnlitMaterialNode; |
| 535 |
Appearance: TAppearanceNode; |
| 536 |
Tri: TIndexedTriangleSetNode; |
| 537 |
Tex: TAbstractTextureNode; |
| 538 |
TexProperties: TTexturePropertiesNode; |
| 539 |
FdUrl: String; |
| 540 |
begin |
| 541 |
FRoot.Meta['generator'] := 'Castle Game Engine, https://castle-engine.io'; |
| 542 |
FRoot.Meta['source'] := ExtractUriName(FSpriteSheet.URL); |
| 543 |
|
| 544 |
Material := TUnlitMaterialNode.Create; |
| 545 |
|
| 546 |
Appearance := TAppearanceNode.Create; |
| 547 |
Appearance.Material := Material; |
| 548 |
|
| 549 |
Shape := TShapeNode.Create; |
| 550 |
Shape.Appearance := Appearance; |
| 551 |
|
| 552 |
TexProperties := TTexturePropertiesNode.Create; |
| 553 |
TexProperties.MagnificationFilter := magDefault; |
| 554 |
TexProperties.MinificationFilter := minDefault; |
| 555 |
TexProperties.BoundaryModeS := bmClampToEdge; |
| 556 |
TexProperties.BoundaryModeT := bmClampToEdge; |
| 557 |
{ Do not force "power of 2" size, which may prevent mipmaps. |
| 558 |
This seems like a better default (otherwise the resizing underneath |
| 559 |
may cause longer loading time, and loss of quality, if not expected). |
| 560 |
See https://github.com/castle-engine/castle-engine/issues/249 } |
| 561 |
TexProperties.GuiTexture := true; |
| 562 |
|
| 563 |
if FSpriteSheet.EditMode and (FSpriteSheet.FGeneratedAtlas <> nil) then |
| 564 |
begin |
| 565 |
Tex := TPixelTextureNode.Create; |
| 566 |
TPixelTextureNode(Tex).FdImage.Value := FSpriteSheet.FGeneratedAtlas.MakeCopy; |
| 567 |
{ No point in adjusting RepeatS/T: TextureProperties override it. |
| 568 |
TPixelTextureNode(Tex).RepeatS := false; |
| 569 |
TPixelTextureNode(Tex).RepeatT := false; } |
| 570 |
TPixelTextureNode(Tex).TextureProperties := TexProperties; |
| 571 |
end else |
| 572 |
begin |
| 573 |
Tex := TImageTextureNode.Create; |
| 574 |
{ Check is this file loaded from Starling in that case use LoadedAtlasPath } |
| 575 |
if FSpriteSheet.URL <> '' then |
| 576 |
FdUrl := ExtractUriPath(FSpriteSheet.URL) + FSpriteSheet.RelativeAtlasPath |
| 577 |
else |
| 578 |
FdUrl := FSpriteSheet.LoadedAtlasPath; |
| 579 |
|
| 580 |
TImageTextureNode(Tex).FdUrl.Send(FdUrl); |
| 581 |
{ No point in adjusting RepeatS/T: TextureProperties override it. |
| 582 |
TImageTextureNode(Tex).RepeatS := false; |
| 583 |
TImageTextureNode(Tex).RepeatT := false; } |
| 584 |
TImageTextureNode(Tex).TextureProperties := TexProperties; |
| 585 |
end; |
| 586 |
Appearance.Texture := Tex; |
| 587 |
|
| 588 |
//Tri := TTriangleSetNode.Create; |
| 589 |
Tri := TIndexedTriangleSetNode.Create; |
| 590 |
Tri.SetIndex([0, 1, 2, 0, 2, 3]); |
| 591 |
Tri.Solid := false; |
| 592 |
|
| 593 |
FShapeCoord := TCoordinateNode.Create; |
| 594 |
FShapeCoord.SetPoint([ |
| 595 |
FCoordArray[0], |
| 596 |
FCoordArray[1], |
| 597 |
FCoordArray[2], |
| 598 |
FCoordArray[3] |
| 599 |
]); |
| 600 |
|
| 601 |
FShapeTexCoord := TTextureCoordinateNode.Create; |
| 602 |
FShapeTexCoord.SetPoint([ |
| 603 |
FTexCoordArray[0], |
| 604 |
FTexCoordArray[1], |
| 605 |
FTexCoordArray[2], |
| 606 |
FTexCoordArray[3] |
| 607 |
]); |
| 608 |
|
| 609 |
Tri.Coord := FShapeCoord; |
| 610 |
Tri.TexCoord := FShapeTexCoord; |
| 611 |
Shape.Geometry := Tri; |
| 612 |
|
| 613 |
FRoot.AddChildren(Shape); |
| 614 |
end; |
| 615 |
|
| 616 |
procedure TCastleSpriteSheetX3DExporter.CalculateFrameCoords( |
| 617 |
const Frame: TCastleSpriteSheetFrame); |
| 618 |
|
| 619 |
procedure CalculateAnchors(const Frame: TCastleSpriteSheetFrame; out AnchorX, AnchorY: Single); |
| 620 |
var |
| 621 |
FrameAnchorX: Integer; |
| 622 |
FrameAnchorY: Integer; |
| 623 |
begin |
| 624 |
{ I found some starling files which may have the last frame of the animation |
| 625 |
with the size set to 0 so we need check this here (division by zero error) |
| 626 |
example: |
| 627 |
https://github.com/pammimeow/fatty-starling-as3-game/blob/master/assets/sprite%20elements.xml } |
| 628 |
if Frame.Trimmed and (Frame.WidthInAtlas <> 0) and (Frame.HeightInAtlas <> 0) then |
| 629 |
begin |
| 630 |
{ When frame is trimmed Width and Height does not mean the full size |
| 631 |
of the frame, so we have to calculate the appropriate |
| 632 |
anchor to get the correct position because it will not be (0.5, 0.5) } |
| 633 |
|
| 634 |
{ Anchor in pixels (Without translation to correct texture point |
| 635 |
because we don't need that. Just add X1, Y1 to have correct position.) } |
| 636 |
FrameAnchorX := Frame.FrameWidth div 2 + Frame.LeftOffset; |
| 637 |
FrameAnchorY := Frame.FrameHeight div 2 + Frame.TopOffset; |
| 638 |
|
| 639 |
{ Convert to 0.0..1.0 coordinate system } |
| 640 |
AnchorX := 1 / Frame.WidthInAtlas * FrameAnchorX; |
| 641 |
AnchorY := 1 / Frame.HeightInAtlas * FrameAnchorY; |
| 642 |
end else |
| 643 |
begin |
| 644 |
AnchorX := 0.5; |
| 645 |
AnchorY := 0.5; |
| 646 |
end; |
| 647 |
end; |
| 648 |
|
| 649 |
procedure AddCoords(const Frame: TCastleSpriteSheetFrame); |
| 650 |
var |
| 651 |
AnchorX, AnchorY: Single; |
| 652 |
X1: Single; |
| 653 |
Y1: Single; |
| 654 |
X2: Single; |
| 655 |
Y2: Single; |
| 656 |
begin |
| 657 |
CalculateAnchors(Frame, AnchorX, AnchorY); |
| 658 |
|
| 659 |
X1 := -Frame.WidthInAtlas * AnchorX; |
| 660 |
Y1 := -Frame.HeightInAtlas * (1 - AnchorY); |
| 661 |
X2 := Frame.WidthInAtlas * (1 - AnchorX); |
| 662 |
Y2 := Frame.HeightInAtlas * AnchorY; |
| 663 |
|
| 664 |
FCoordArray[0] := Vector3(X1, Y1, 0); |
| 665 |
FCoordArray[1] := Vector3(X2, Y1, 0); |
| 666 |
FCoordArray[2] := Vector3(X2, Y2, 0); |
| 667 |
FCoordArray[3] := Vector3(X1, Y2, 0); |
| 668 |
end; |
| 669 |
|
| 670 |
procedure AddTexCords( |
| 671 |
const Frame: TCastleSpriteSheetFrame; const ImageWidth, ImageHeight: Integer); |
| 672 |
var |
| 673 |
{ Current frame tex cords } |
| 674 |
X1: Single; |
| 675 |
Y1: Single; |
| 676 |
X2: Single; |
| 677 |
Y2: Single; |
| 678 |
begin |
| 679 |
X1 := 1 / FSpriteSheet.AtlasWidth * Frame.XInAtlas; |
| 680 |
Y1 := 1 / FSpriteSheet.AtlasHeight * Frame.YInAtlas; |
| 681 |
X2 := 1 / FSpriteSheet.AtlasWidth * (Frame.XInAtlas + Frame.WidthInAtlas); |
| 682 |
Y2 := 1 / FSpriteSheet.AtlasHeight * (Frame.YInAtlas + Frame.HeightInAtlas); |
| 683 |
|
| 684 |
FTexCoordArray[0] := Vector2(X1, Y1); |
| 685 |
FTexCoordArray[1] := Vector2(X2, Y1); |
| 686 |
FTexCoordArray[2] := Vector2(X2, Y2); |
| 687 |
FTexCoordArray[3] := Vector2(X1, Y2); |
| 688 |
end; |
| 689 |
|
| 690 |
begin |
| 691 |
AddCoords(Frame); |
| 692 |
AddTexCords(Frame, FSpriteSheet.FAtlasWidth, FSpriteSheet.FAtlasHeight); |
| 693 |
end; |
| 694 |
|
| 695 |
procedure TCastleSpriteSheetX3DExporter.PrepareAnimation(const Name: String); |
| 696 |
begin |
| 697 |
TimeSensor := TTimeSensorNode.Create(Name); |
| 698 |
CoordInterp := TCoordinateInterpolatorNode.Create(Name + '_Coord'); |
| 699 |
CoordInterp.Interpolation := inStep; |
| 700 |
TexCoordInterp := TCoordinateInterpolator2DNode.Create(Name + '_TexCoord'); |
| 701 |
TexCoordInterp.Interpolation := inStep; |
| 702 |
end; |
| 703 |
|
| 704 |
procedure TCastleSpriteSheetX3DExporter.AddAnimation( |
| 705 |
const Animation: TCastleSpriteSheetAnimation); |
| 706 |
var |
| 707 |
FrameCount: Integer; |
| 708 |
|
| 709 |
{ CoordInterp often has a dummy animation, that actually wants the same coordinates |
| 710 |
every frame. In this case it can be simplified. } |
| 711 |
procedure OptimizeCoordInterp; |
| 712 |
const |
| 713 |
PerFrameValues = 4; |
| 714 |
var |
| 715 |
Values: TVector3List; |
| 716 |
I: Integer; |
| 717 |
begin |
| 718 |
Assert(CoordInterp.FdKeyValue.Count = PerFrameValues * FrameCount); |
| 719 |
Assert(FShapeCoord.FdPoint.Count = PerFrameValues); |
| 720 |
|
| 721 |
Values := CoordInterp.FdKeyValue.Items; |
| 722 |
for I := 1 to FrameCount - 1 do |
| 723 |
begin |
| 724 |
if not CompareMem(Values.L, Values.Ptr(I * PerFrameValues), SizeOf(TVector3) * PerFrameValues) then |
| 725 |
Exit; // optimization not possible |
| 726 |
end; |
| 727 |
|
| 728 |
{ optimization possible: simplify CoordInterp to 1 frame, or even remove CoordInterp } |
| 729 |
if CompareMem(Values.L, FShapeCoord.FdPoint.Items.L, SizeOf(TVector3) * PerFrameValues) then |
| 730 |
begin |
| 731 |
FreeIfUnusedAndNil(CoordInterp); |
| 732 |
end else |
| 733 |
begin |
| 734 |
CoordInterp.FdKey.Count := 1; |
| 735 |
CoordInterp.FdKeyValue.Count := PerFrameValues; |
| 736 |
end; |
| 737 |
end; |
| 738 |
|
| 739 |
var |
| 740 |
I: Integer; |
| 741 |
Key: Single; |
| 742 |
begin |
| 743 |
FrameCount := Animation.FrameCount; |
| 744 |
{ Set Cycle Interval becouse we know now frame count } |
| 745 |
TimeSensor.CycleInterval := FrameCount / Animation.FramesPerSecond; |
| 746 |
|
| 747 |
{ Generate list of keys. } |
| 748 |
for I := 0 to FrameCount - 1 do |
| 749 |
begin |
| 750 |
Key := I / FrameCount; |
| 751 |
CoordInterp.FdKey.Items.Add(Key); |
| 752 |
TexCoordInterp.FdKey.Items.Add(Key); |
| 753 |
end; |
| 754 |
|
| 755 |
OptimizeCoordInterp; |
| 756 |
|
| 757 |
{ Add TimeSensor to Root node } |
| 758 |
FRoot.AddChildren(TimeSensor); |
| 759 |
|
| 760 |
{ Add TextureCoordinate animation } |
| 761 |
FRoot.AddChildren(TexCoordInterp); |
| 762 |
FRoot.AddRoute(TimeSensor.EventFraction_changed, TexCoordInterp.EventSet_fraction); |
| 763 |
FRoot.AddRoute(TexCoordInterp.EventValue_changed, FShapeTexCoord.FdPoint); |
| 764 |
|
| 765 |
{ Add Coordinate animation } |
| 766 |
if CoordInterp <> nil then |
| 767 |
begin |
| 768 |
FRoot.AddChildren(CoordInterp); |
| 769 |
FRoot.AddRoute(TimeSensor.EventFraction_changed, CoordInterp.EventSet_fraction); |
| 770 |
FRoot.AddRoute(CoordInterp.EventValue_changed, FShapeCoord.FdPoint); |
| 771 |
end; |
| 772 |
end; |
| 773 |
|
| 774 |
procedure TCastleSpriteSheetX3DExporter.AddFrame; |
| 775 |
begin |
| 776 |
CoordInterp.FdKeyValue.Items.AddRange(FCoordArray); |
| 777 |
TexCoordInterp.FdKeyValue.Items.AddRange(FTexCoordArray); |
| 778 |
end; |
| 779 |
|
| 780 |
constructor TCastleSpriteSheetX3DExporter.Create( |
| 781 |
const SpriteSheet: TCastleSpriteSheet); |
| 782 |
begin |
| 783 |
inherited Create; |
| 784 |
FSpriteSheet := SpriteSheet; |
| 785 |
end; |
| 786 |
|
| 787 |
function TCastleSpriteSheetX3DExporter.ExportToX3D: TX3DRootNode; |
| 788 |
var |
| 789 |
I, J: Integer; |
| 790 |
Animation: TCastleSpriteSheetAnimation; |
| 791 |
Frame: TCastleSpriteSheetFrame; |
| 792 |
FirstFrameInFirstAnimation: Boolean; |
| 793 |
begin |
| 794 |
FirstFrameInFirstAnimation := true; |
| 795 |
|
| 796 |
FRoot := TX3DRootNode.Create; |
| 797 |
try |
| 798 |
for I := 0 to FSpriteSheet.AnimationCount - 1 do |
| 799 |
begin |
| 800 |
Animation := FSpriteSheet.AnimationByIndex(I); |
| 801 |
|
| 802 |
if Animation.FrameCount = 0 then |
| 803 |
continue; |
| 804 |
|
| 805 |
PrepareAnimation(Animation.Name); |
| 806 |
|
| 807 |
for J := 0 to Animation.FrameCount - 1 do |
| 808 |
begin |
| 809 |
Frame := Animation.Frame[J]; |
| 810 |
CalculateFrameCoords(Frame); |
| 811 |
|
| 812 |
{ We want the shape has by default size of first frame |
| 813 |
of first animation. } |
| 814 |
if FirstFrameInFirstAnimation then |
| 815 |
begin |
| 816 |
FirstFrameInFirstAnimation := false; |
| 817 |
PrepareContainer; |
| 818 |
end; |
| 819 |
|
| 820 |
AddFrame; |
| 821 |
end; |
| 822 |
|
| 823 |
AddAnimation(Animation); |
| 824 |
|
| 825 |
end; |
| 826 |
Result := FRoot; |
| 827 |
except |
| 828 |
FreeAndNil(FRoot); |
| 829 |
raise; |
| 830 |
end; |
| 831 |
end; |
| 832 |
|
| 833 |
{ TCastleSpriteSheetAdvancedImageGen } |
| 834 |
|
| 835 |
procedure TCastleSpriteSheetAdvancedImageGen.Generate; |
| 836 |
begin |
| 837 |
// TODO |
| 838 |
end; |
| 839 |
|
| 840 |
{ TCastleSpriteSheetAbstractAtlasGen } |
| 841 |
|
| 842 |
constructor TCastleSpriteSheetAbstractAtlasGen.Create( |
| 843 |
const ASpriteSheet: TCastleSpriteSheet; const MaxWidth, MaxHeight: Integer); |
| 844 |
begin |
| 845 |
FSpriteSheet := ASpriteSheet; |
| 846 |
FSpriteSheetMaxWidth := MaxWidth; |
| 847 |
FSpriteSheetMaxHeight := MaxHeight; |
| 848 |
end; |
| 849 |
|
| 850 |
{ TCastleSpriteSheetBasicAtlasGen } |
| 851 |
|
| 852 |
function TCastleSpriteSheetBasicAtlasGen.LayoutFrames( |
| 853 |
const Operation: TLayoutOperation; |
| 854 |
const MaxAtlasWidth, MaxAtlasHeight: Integer; |
| 855 |
out MinAtlasWidth, MinAtlasHeight: Integer): Boolean; |
| 856 |
var |
| 857 |
I, J: Integer; |
| 858 |
X, Y: Integer; |
| 859 |
Animation: TCastleSpriteSheetAnimation; |
| 860 |
Frame: TCastleSpriteSheetFrame; |
| 861 |
CurrentMaxLineHeight: Integer; |
| 862 |
BigestFrameWidth, BigestFrameHeight: Integer; |
| 863 |
UsedMaxAtlasWidth: Integer; |
| 864 |
|
| 865 |
procedure CheckBigestFrameSize; |
| 866 |
var |
| 867 |
I, J: Integer; |
| 868 |
begin |
| 869 |
BigestFrameWidth := 0; |
| 870 |
BigestFrameHeight := 0; |
| 871 |
|
| 872 |
for I := 0 to FSpriteSheet.AnimationCount - 1 do |
| 873 |
begin |
| 874 |
Animation := FSpriteSheet.AnimationByIndex(I); |
| 875 |
|
| 876 |
for J := 0 to Animation.FrameCount - 1 do |
| 877 |
begin |
| 878 |
Frame := Animation.Frame[J]; |
| 879 |
Frame.UpdateTrimming; |
| 880 |
|
| 881 |
BigestFrameWidth := Max(BigestFrameWidth, Frame.WidthInAtlas); |
| 882 |
BigestFrameHeight := Max(BigestFrameHeight, Frame.HeightInAtlas); |
| 883 |
end; |
| 884 |
end; |
| 885 |
end; |
| 886 |
|
| 887 |
procedure SetFrameCords(const AFrame: TCastleSpriteSheetFrame; |
| 888 |
const X, Y: Integer); |
| 889 |
begin |
| 890 |
if Operation = loArrange then |
| 891 |
begin |
| 892 |
AFrame.XInAtlas := X; |
| 893 |
AFrame.YInAtlas := Y; |
| 894 |
end; |
| 895 |
end; |
| 896 |
|
| 897 |
begin |
| 898 |
X := 0; |
| 899 |
Y := 0; |
| 900 |
CurrentMaxLineHeight := 0; |
| 901 |
|
| 902 |
{ Min size that we need for atlas } |
| 903 |
MinAtlasHeight := 0; |
| 904 |
MinAtlasWidth := 0; |
| 905 |
|
| 906 |
{ Check if some frame is bigger than max atlas size } |
| 907 |
CheckBigestFrameSize; |
| 908 |
|
| 909 |
{ If BigestFrameWidth > MaxAtlasWidth we use BigestFrameWidth to calculate |
| 910 |
height } |
| 911 |
UsedMaxAtlasWidth := Max(MaxAtlasWidth, BigestFrameWidth); |
| 912 |
|
| 913 |
for I := 0 to FSpriteSheet.AnimationCount - 1 do |
| 914 |
begin |
| 915 |
Animation := FSpriteSheet.AnimationByIndex(I); |
| 916 |
for J := 0 to Animation.FrameCount - 1 do |
| 917 |
begin |
| 918 |
Frame := Animation.Frame[J]; |
| 919 |
|
| 920 |
// add to this line ? |
| 921 |
if X + Frame.FrameWidth <= UsedMaxAtlasWidth then |
| 922 |
begin |
| 923 |
// yes |
| 924 |
|
| 925 |
{ check free height, only save if we don't have free space } |
| 926 |
if Y + Frame.FrameHeight > MaxAtlasHeight then |
| 927 |
begin |
| 928 |
{ return that we need more space } |
| 929 |
MinAtlasHeight := Max(Y + Frame.FrameHeight, |
| 930 |
MinAtlasHeight); |
| 931 |
end; |
| 932 |
|
| 933 |
SetFrameCords(Frame, X, Y); |
| 934 |
|
| 935 |
X := X + Frame.FrameWidth; |
| 936 |
CurrentMaxLineHeight := Max(CurrentMaxLineHeight, Frame.FrameHeight); |
| 937 |
continue; |
| 938 |
end; |
| 939 |
|
| 940 |
{ save max line width } |
| 941 |
MinAtlasWidth := Max(MinAtlasWidth, X); |
| 942 |
|
| 943 |
{ Start new line (add to new line) } |
| 944 |
Inc(Y, CurrentMaxLineHeight); |
| 945 |
X := 0; |
| 946 |
|
| 947 |
// check size |
| 948 |
if (Y + Frame.FrameHeight > MaxAtlasHeight) then |
| 949 |
begin |
| 950 |
MinAtlasHeight := Max(Y + Frame.FrameHeight, |
| 951 |
MinAtlasHeight); |
| 952 |
end; |
| 953 |
|
| 954 |
// add frame |
| 955 |
SetFrameCords(Frame, X, Y); |
| 956 |
|
| 957 |
X := X + Frame.FrameWidth; |
| 958 |
CurrentMaxLineHeight := Max(CurrentMaxLineHeight, Frame.FrameHeight); |
| 959 |
end; |
| 960 |
end; |
| 961 |
|
| 962 |
Result := (MinAtlasWidth <= MaxAtlasWidth) and |
| 963 |
(MinAtlasHeight <= MaxAtlasHeight); |
| 964 |
|
| 965 |
{ Image must have size bigger than zero } |
| 966 |
MinAtlasWidth := Max(MinAtlasWidth, 1); |
| 967 |
MinAtlasHeight := Max(MinAtlasHeight, 1); |
| 968 |
end; |
| 969 |
|
| 970 |
procedure TCastleSpriteSheetBasicAtlasGen.GenerateAtlas(const AtlasWidth, |
| 971 |
AtlasHeight: Integer); |
| 972 |
var |
| 973 |
Animation: TCastleSpriteSheetAnimation; |
| 974 |
Frame: TCastleSpriteSheetFrame; |
| 975 |
I, J: Integer; |
| 976 |
begin |
| 977 |
FreeAndNil(FSpriteSheet.FGeneratedAtlas); |
| 978 |
FSpriteSheet.FGeneratedAtlas := TRGBAlphaImage.Create(AtlasWidth, AtlasHeight); |
| 979 |
FSpriteSheet.FGeneratedAtlas.Clear(Vector4Byte(0, 0, 0, 0)); |
| 980 |
|
| 981 |
for I := 0 to FSpriteSheet.AnimationCount - 1 do |
| 982 |
begin |
| 983 |
Animation := FSpriteSheet.AnimationByIndex(I); |
| 984 |
for J := 0 to Animation.FrameCount - 1 do |
| 985 |
begin |
| 986 |
Frame := Animation.Frame[J]; |
| 987 |
|
| 988 |
Frame.DrawToImage(FSpriteSheet.FGeneratedAtlas, Frame.XInAtlas, Frame.YInAtlas, 0, 0, |
| 989 |
Frame.FrameWidth, Frame.FrameHeight); |
| 990 |
end; |
| 991 |
end; |
| 992 |
FSpriteSheet.FAtlasWidth := AtlasWidth; |
| 993 |
FSpriteSheet.FAtlasHeight := AtlasWidth; |
| 994 |
end; |
| 995 |
|
| 996 |
function TCastleSpriteSheetBasicAtlasGen.WillFramesFitInSize(AtlasWidth, |
| 997 |
AtlasHeight: Integer): Boolean; |
| 998 |
var |
| 999 |
MinWidth, MinHeight: Integer; |
| 1000 |
begin |
| 1001 |
Result := LayoutFrames(loMeasure, AtlasWidth, AtlasHeight, MinWidth, MinHeight); |
| 1002 |
end; |
| 1003 |
|
| 1004 |
procedure TCastleSpriteSheetBasicAtlasGen.GetMinAtlasSize(out MinWidth, |
| 1005 |
MinHeight: Integer); |
| 1006 |
begin |
| 1007 |
LayoutFrames(loMeasure, FSpriteSheetMaxWidth, FSpriteSheetMaxHeight, MinWidth, |
| 1008 |
MinHeight); |
| 1009 |
end; |
| 1010 |
|
| 1011 |
procedure TCastleSpriteSheetBasicAtlasGen.Generate; |
| 1012 |
var |
| 1013 |
MinWidth, MinHeight: Integer; |
| 1014 |
begin |
| 1015 |
if not LayoutFrames(loArrange, FSpriteSheetMaxWidth, FSpriteSheetMaxHeight, |
| 1016 |
MinWidth, MinHeight) then |
| 1017 |
raise ECastleSpriteSheetAtlasToSmall.Create(Format( |
| 1018 |
'Minimum sprite sheet atlas size too small (%d x %d), we need at least %d x %d ', |
| 1019 |
[FSpriteSheetMaxWidth, FSpriteSheetMaxHeight, MinWidth, MinHeight])); |
| 1020 |
GenerateAtlas(FSpriteSheetMaxWidth, FSpriteSheetMaxHeight); |
| 1021 |
end; |
| 1022 |
|
| 1023 |
{ TCastleSpriteSheetXMLExporter } |
| 1024 |
|
| 1025 |
constructor TCastleSpriteSheetXMLExporter.Create( |
| 1026 |
const SpriteSheet: TCastleSpriteSheet); |
| 1027 |
begin |
| 1028 |
inherited Create; |
| 1029 |
FSpriteSheet := SpriteSheet; |
| 1030 |
end; |
| 1031 |
|
| 1032 |
function TCastleSpriteSheetXMLExporter.ExportToXML: TXMLDocument; |
| 1033 |
var |
| 1034 |
I, J: Integer; |
| 1035 |
Animation: TCastleSpriteSheetAnimation; |
| 1036 |
Frame: TCastleSpriteSheetFrame; |
| 1037 |
RootNode: TDOMElement; |
| 1038 |
SubTextureNode: TDOMElement; |
| 1039 |
begin |
| 1040 |
Result := TXMLDocument.Create; |
| 1041 |
|
| 1042 |
RootNode := Result.CreateElement('TextureAtlas'); |
| 1043 |
Result.AppendChild(RootNode); |
| 1044 |
|
| 1045 |
RootNode.AttributeSet('imagePath', FSpriteSheet.RelativeAtlasPath); |
| 1046 |
RootNode.AttributeSet('width', FSpriteSheet.AtlasWidth); |
| 1047 |
RootNode.AttributeSet('height', FSpriteSheet.AtlasHeight); |
| 1048 |
|
| 1049 |
for I := 0 to FSpriteSheet.AnimationCount - 1 do |
| 1050 |
begin |
| 1051 |
Animation := FSpriteSheet.AnimationByIndex(I); |
| 1052 |
|
| 1053 |
if Animation.FrameCount = 0 then |
| 1054 |
continue; |
| 1055 |
|
| 1056 |
for J := 0 to Animation.FrameCount - 1 do |
| 1057 |
begin |
| 1058 |
Frame := Animation.Frame[J]; |
| 1059 |
SubTextureNode := RootNode.CreateChild('SubTexture'); |
| 1060 |
|
| 1061 |
SubTextureNode.AttributeSet('name', Animation.Name + '_' + IntToStr(J + 1)); |
| 1062 |
SubTextureNode.AttributeSet('x', Frame.XInAtlas); |
| 1063 |
SubTextureNode.AttributeSet('y', FSpriteSheet.AtlasHeight - Frame.YInAtlas - Frame.HeightInAtlas); |
| 1064 |
SubTextureNode.AttributeSet('width', Frame.WidthInAtlas); |
| 1065 |
SubTextureNode.AttributeSet('height', Frame.HeightInAtlas); |
| 1066 |
if Frame.Trimmed then |
| 1067 |
begin |
| 1068 |
SubTextureNode.AttributeSet('frameX', Frame.LeftOffset); |
| 1069 |
SubTextureNode.AttributeSet('frameY', Frame.TopOffset); |
| 1070 |
SubTextureNode.AttributeSet('frameWidth', Frame.FrameWidth); |
| 1071 |
SubTextureNode.AttributeSet('frameHeight', Frame.FrameHeight); |
| 1072 |
end; |
| 1073 |
|
| 1074 |
if J = 0 then |
| 1075 |
SubTextureNode.AttributeSet('fps', Animation.FramesPerSecond); |
| 1076 |
end; |
| 1077 |
end; |
| 1078 |
end; |
| 1079 |
|
| 1080 |
{ TCastleSpriteSheetFrame } |
| 1081 |
|
| 1082 |
procedure TCastleSpriteSheetFrame.SetXInAtlas(const NewX: Integer); |
| 1083 |
begin |
| 1084 |
if FXInAtlas = NewX then |
| 1085 |
Exit; |
| 1086 |
|
| 1087 |
FXInAtlas := NewX; |
| 1088 |
SetModifiedState; |
| 1089 |
end; |
| 1090 |
|
| 1091 |
procedure TCastleSpriteSheetFrame.SetYInAtlas(const NewY: Integer); |
| 1092 |
begin |
| 1093 |
if FYInAtlas = NewY then |
| 1094 |
Exit; |
| 1095 |
|
| 1096 |
FYInAtlas := NewY; |
| 1097 |
SetModifiedState; |
| 1098 |
end; |
| 1099 |
|
| 1100 |
procedure TCastleSpriteSheetFrame.SetWidthInAtlas(const NewWidth: Integer); |
| 1101 |
begin |
| 1102 |
if FWidthInAtlas = NewWidth then |
| 1103 |
Exit; |
| 1104 |
|
| 1105 |
FWidthInAtlas := NewWidth; |
| 1106 |
SetModifiedState; |
| 1107 |
end; |
| 1108 |
|
| 1109 |
procedure TCastleSpriteSheetFrame.SetHeightInAtlas(const NewHeight: Integer); |
| 1110 |
begin |
| 1111 |
if FHeightInAtlas = NewHeight then |
| 1112 |
Exit; |
| 1113 |
|
| 1114 |
FHeightInAtlas := NewHeight; |
| 1115 |
SetModifiedState; |
| 1116 |
end; |
| 1117 |
|
| 1118 |
procedure TCastleSpriteSheetFrame.SetLeftOffset(const NewLeftOffset: Integer); |
| 1119 |
begin |
| 1120 |
if FLeftOffset = NewLeftOffset then |
| 1121 |
Exit; |
| 1122 |
|
| 1123 |
FLeftOffset := NewLeftOffset; |
| 1124 |
SetModifiedState; |
| 1125 |
end; |
| 1126 |
|
| 1127 |
procedure TCastleSpriteSheetFrame.SetTopOffset(const NewTopOffset: Integer); |
| 1128 |
begin |
| 1129 |
if FTopOffset = NewTopOffset then |
| 1130 |
Exit; |
| 1131 |
|
| 1132 |
FTopOffset := NewTopOffset; |
| 1133 |
SetModifiedState; |
| 1134 |
end; |
| 1135 |
|
| 1136 |
procedure TCastleSpriteSheetFrame.SetFrameWidth(const NewFrameWidth: Integer); |
| 1137 |
begin |
| 1138 |
if FFrameWidth = NewFrameWidth then |
| 1139 |
Exit; |
| 1140 |
|
| 1141 |
FFrameWidth := NewFrameWidth; |
| 1142 |
SetModifiedState; |
| 1143 |
end; |
| 1144 |
|
| 1145 |
procedure TCastleSpriteSheetFrame.SetFrameHeight(const NewFrameHeight: Integer); |
| 1146 |
begin |
| 1147 |
if FFrameHeight = NewFrameHeight then |
| 1148 |
Exit; |
| 1149 |
|
| 1150 |
FFrameHeight := NewFrameHeight; |
| 1151 |
SetModifiedState; |
| 1152 |
end; |
| 1153 |
|
| 1154 |
procedure TCastleSpriteSheetFrame.SetTrimmed(const NewTrimmed: Boolean); |
| 1155 |
begin |
| 1156 |
if FTrimmed = NewTrimmed then |
| 1157 |
Exit; |
| 1158 |
|
| 1159 |
FTrimmed := NewTrimmed; |
| 1160 |
SetModifiedState; |
| 1161 |
end; |
| 1162 |
|
| 1163 |
procedure TCastleSpriteSheetFrame.SetModifiedState; |
| 1164 |
begin |
| 1165 |
FAnimation.SetModifiedState; |
| 1166 |
end; |
| 1167 |
|
| 1168 |
procedure TCastleSpriteSheetFrame.CheckEditMode; |
| 1169 |
begin |
| 1170 |
Animation.CheckEditMode; |
| 1171 |
end; |
| 1172 |
|
| 1173 |
procedure TCastleSpriteSheetFrame.UpdateTrimming; |
| 1174 |
begin |
| 1175 |
{ TODO: add trimming, currently only reset to full frame size } |
| 1176 |
LeftOffset := 0; |
| 1177 |
TopOffset := 0; |
| 1178 |
|
| 1179 |
WidthInAtlas := FrameWidth; |
| 1180 |
HeightInAtlas := FrameHeight; |
| 1181 |
end; |
| 1182 |
|
| 1183 |
constructor TCastleSpriteSheetFrame.Create( |
| 1184 |
const Animation: TCastleSpriteSheetAnimation); |
| 1185 |
begin |
| 1186 |
Assert(Animation <> nil, 'Animation can''t be nil when creating frame!'); |
| 1187 |
|
| 1188 |
FAnimation := Animation; |
| 1189 |
end; |
| 1190 |
|
| 1191 |
constructor TCastleSpriteSheetFrame.Create( |
| 1192 |
const Animation: TCastleSpriteSheetAnimation; |
| 1193 |
const SrcFrame: TCastleSpriteSheetFrame); |
| 1194 |
begin |
| 1195 |
Create(Animation); |
| 1196 |
|
| 1197 |
FXInAtlas := SrcFrame.FXInAtlas; |
| 1198 |
FYInAtlas := SrcFrame.FXInAtlas; |
| 1199 |
|
| 1200 |
FWidthInAtlas := SrcFrame.FWidthInAtlas; |
| 1201 |
FHeightInAtlas := SrcFrame.FHeightInAtlas; |
| 1202 |
FLeftOffset := SrcFrame.FLeftOffset; |
| 1203 |
FTopOffset := SrcFrame.FTopOffset; |
| 1204 |
FFrameWidth := SrcFrame.FFrameWidth; |
| 1205 |
FFrameHeight := SrcFrame.FFrameHeight; |
| 1206 |
|
| 1207 |
FTrimmed := SrcFrame.FTrimmed; |
| 1208 |
if SrcFrame.FFrameImage <> nil then |
| 1209 |
FFrameImage := SrcFrame.FFrameImage.MakeCopy |
| 1210 |
else |
| 1211 |
FFrameImage := nil; |
| 1212 |
end; |
| 1213 |
|
| 1214 |
destructor TCastleSpriteSheetFrame.Destroy; |
| 1215 |
begin |
| 1216 |
FreeAndNil(FFrameImage); |
| 1217 |
inherited Destroy; |
| 1218 |
end; |
| 1219 |
|
| 1220 |
function TCastleSpriteSheetFrame.HasFrameImage: Boolean; |
| 1221 |
begin |
| 1222 |
Result := FFrameImage <> nil; |
| 1223 |
end; |
| 1224 |
|
| 1225 |
procedure TCastleSpriteSheetFrame.SetFrameImage(const SourceImage: TCastleImage); |
| 1226 |
begin |
| 1227 |
CheckEditMode; |
| 1228 |
|
| 1229 |
FreeAndNil(FFrameImage); |
| 1230 |
WidthInAtlas := SourceImage.Width; |
| 1231 |
HeightInAtlas := SourceImage.Height; |
| 1232 |
|
| 1233 |
LeftOffset := 0; |
| 1234 |
TopOffset := 0; |
| 1235 |
FrameWidth := SourceImage.Width; |
| 1236 |
FrameHeight := SourceImage.Height; |
| 1237 |
|
| 1238 |
FFrameImage := SourceImage.MakeCopy; |
| 1239 |
SetModifiedState; |
| 1240 |
end; |
| 1241 |
|
| 1242 |
procedure TCastleSpriteSheetFrame.SetFrameImage( |
| 1243 |
const SourceImage: TCastleImage; const DestX, DestY, AFrameWidth, |
| 1244 |
AFrameHeight, SourceX, SourceY, SourceWidthToCopy, SourceHeightToCopy: Integer); |
| 1245 |
begin |
| 1246 |
CheckEditMode; |
| 1247 |
|
| 1248 |
Trimmed := not ((DestX = 0) and (DestY = 0)); |
| 1249 |
|
| 1250 |
{ update frame settings } |
| 1251 |
XInAtlas := SourceX; |
| 1252 |
YInAtlas := SourceY; |
| 1253 |
WidthInAtlas := SourceWidthToCopy; |
| 1254 |
HeightInAtlas := SourceHeightToCopy; |
| 1255 |
FrameWidth := AFrameWidth; |
| 1256 |
FrameHeight := AFrameHeight; |
| 1257 |
|
| 1258 |
LeftOffset := -DestX; |
| 1259 |
TopOffset := -AFrameHeight + DestY + HeightInAtlas; |
| 1260 |
|
| 1261 |
FreeAndNil(FFrameImage); |
| 1262 |
|
| 1263 |
if not Trimmed then |
| 1264 |
FFrameImage := SourceImage.MakeExtracted(XInAtlas, YInAtlas, WidthInAtlas, HeightInAtlas) |
| 1265 |
else |
| 1266 |
begin |
| 1267 |
{ When Image is trimmed we can't simply extract image part } |
| 1268 |
FFrameImage := TCastleImageClass(SourceImage.ClassType).Create(FrameWidth, FrameHeight, SourceImage.Depth); |
| 1269 |
FFrameImage.Clear(Vector4Byte(0, 0, 0, 0)); |
| 1270 |
FFrameImage.DrawFrom(SourceImage, |
| 1271 |
DestX, // destination XInAtlas |
| 1272 |
DestY, // destination YInAtlas |
| 1273 |
XInAtlas, // source XInAtlas |
| 1274 |
YInAtlas, // source YInAtlas |
| 1275 |
WidthInAtlas, |
| 1276 |
HeightInAtlas, |
| 1277 |
dmOverwrite |
| 1278 |
); |
| 1279 |
end; |
| 1280 |
|
| 1281 |
SetModifiedState; |
| 1282 |
end; |
| 1283 |
|
| 1284 |
procedure TCastleSpriteSheetFrame.DrawToImage(const DestImage: TCastleImage; |
| 1285 |
const DestX, DestY, SourceX, SourceY, SourceWidthToDraw, |
| 1286 |
SourceHeightToDraw: Integer); |
| 1287 |
begin |
| 1288 |
Assert(FFrameImage <> nil, 'No frame image to draw.'); |
| 1289 |
CheckEditMode; |
| 1290 |
|
| 1291 |
DestImage.DrawFrom(FFrameImage, DestX, DestY, SourceX, SourceY, SourceWidthToDraw, |
| 1292 |
SourceHeightToDraw, dmOverwrite); |
| 1293 |
end; |
| 1294 |
|
| 1295 |
function TCastleSpriteSheetFrame.MakeResized(const Width, Height: Integer |
| 1296 |
): TCastleImage; |
| 1297 |
begin |
| 1298 |
Assert(FFrameImage <> nil, 'No frame image to resize.'); |
| 1299 |
CheckEditMode; |
| 1300 |
|
| 1301 |
Result := FFrameImage.MakeResized(Width, Height); |
| 1302 |
end; |
| 1303 |
|
| 1304 |
function TCastleSpriteSheetFrame.MakeResizedWithBg(const Width, |
| 1305 |
Height: Integer; const BgColor: TVector4): TCastleImage; |
| 1306 |
var |
| 1307 |
ResizedImage: TCastleImage; |
| 1308 |
DrawMode: TDrawMode; |
| 1309 |
begin |
| 1310 |
Assert(FFrameImage <> nil, 'No frame image to resize.'); |
| 1311 |
CheckEditMode; |
| 1312 |
|
| 1313 |
Result := TCastleImageClass(FFrameImage.ClassType).Create(Width, Height, FFrameImage.Depth); |
| 1314 |
Result.Clear(BgColor); |
| 1315 |
|
| 1316 |
if BgColor.IsPerfectlyZero then |
| 1317 |
DrawMode := dmOverwrite |
| 1318 |
else |
| 1319 |
DrawMode := dmBlend; |
| 1320 |
|
| 1321 |
ResizedImage := FFrameImage.MakeResized(Width, Height); |
| 1322 |
try |
| 1323 |
Result.DrawFrom(FFrameImage, 0, 0, 0, 0, ResizedImage.Width, |
| 1324 |
ResizedImage.Height, DrawMode); |
| 1325 |
finally |
| 1326 |
FreeAndNil(ResizedImage); |
| 1327 |
end; |
| 1328 |
end; |
| 1329 |
|
| 1330 |
function TCastleSpriteSheetFrame.MakeImageCopy: TCastleImage; |
| 1331 |
begin |
| 1332 |
Assert(FFrameImage <> nil, 'No frame image to make copy.'); |
| 1333 |
CheckEditMode; |
| 1334 |
|
| 1335 |
Result := FFrameImage.MakeCopy; |
| 1336 |
end; |
| 1337 |
|
| 1338 |
function TCastleSpriteSheetFrame.MakeImageCopyWithBg(const BgColor: TVector4 |
| 1339 |
): TCastleImage; |
| 1340 |
var |
| 1341 |
DrawMode: TDrawMode; |
| 1342 |
begin |
| 1343 |
Assert(FFrameImage <> nil, 'No frame image to make copy.'); |
| 1344 |
CheckEditMode; |
| 1345 |
|
| 1346 |
Result := TCastleImageClass(FFrameImage.ClassType).Create(FFrameImage.Width, |
| 1347 |
FFrameImage.Height, FFrameImage.Depth); |
| 1348 |
Result.Clear(BgColor); |
| 1349 |
|
| 1350 |
if BgColor.IsPerfectlyZero then |
| 1351 |
DrawMode := dmOverwrite |
| 1352 |
else |
| 1353 |
DrawMode := dmBlend; |
| 1354 |
|
| 1355 |
Result.DrawFrom(FFrameImage, 0, 0, 0, 0, FFrameImage.Width, |
| 1356 |
FFrameImage.Height, DrawMode); |
| 1357 |
end; |
| 1358 |
|
| 1359 |
function TCastleSpriteSheetFrame.CenterOnBiggerImage(const Width, |
| 1360 |
Height: Integer; const BgColor: TVector4): TCastleImage; |
| 1361 |
var |
| 1362 |
DrawMode: TDrawMode; |
| 1363 |
begin |
| 1364 |
if (FrameWidth > Width) or (FrameHeight > Height) then |
| 1365 |
raise Exception.Create('Frame image bigger than gived size'); |
| 1366 |
|
| 1367 |
CheckEditMode; |
| 1368 |
|
| 1369 |
Result := TCastleImageClass(FFrameImage.ClassType).Create(Width, Height, FFrameImage.Depth); |
| 1370 |
Result.Clear(BgColor); |
| 1371 |
|
| 1372 |
if BgColor.IsPerfectlyZero then |
| 1373 |
DrawMode := dmOverwrite |
| 1374 |
else |
| 1375 |
DrawMode := dmBlend; |
| 1376 |
|
| 1377 |
Result.DrawFrom(FFrameImage, (Width - FFrameImage.Width) div 2, |
| 1378 |
(Height - FFrameImage.Height) div 2, 0, 0, FFrameImage.Width, |
| 1379 |
FFrameImage.Height, DrawMode); |
| 1380 |
end; |
| 1381 |
|
| 1382 |
procedure TCastleSpriteSheetFrame.SaveFrameImage(const URL: String); |
| 1383 |
begin |
| 1384 |
CheckEditMode; |
| 1385 |
|
| 1386 |
SaveImage(FFrameImage, URL); |
| 1387 |
end; |
| 1388 |
|
| 1389 |
class function TCastleSpriteSheetFrame.FlipYCoordToCGE(StarlingY, |
| 1390 |
ImageHeight: Integer): Integer; |
| 1391 |
begin |
| 1392 |
Result := ImageHeight - StarlingY; |
| 1393 |
end; |
| 1394 |
|
| 1395 |
class function TCastleSpriteSheetFrame.FlipYCoordToCGE(StarlingY, Height, |
| 1396 |
ImageHeight: Integer): Integer; |
| 1397 |
begin |
| 1398 |
Result := ImageHeight - (StarlingY + Height); |
| 1399 |
end; |
| 1400 |
|
| 1401 |
{ TCastleSpriteSheetAnimation } |
| 1402 |
|
| 1403 |
constructor TCastleSpriteSheetAnimation.Create(SpriteSheet: TCastleSpriteSheet; AName: String); |
| 1404 |
begin |
| 1405 |
Assert(SpriteSheet <> nil, 'Sprite sheet can''t be nil when creating animation!'); |
| 1406 |
FSpriteSheet := SpriteSheet; |
| 1407 |
FName := AName; |
| 1408 |
FFramesPerSecond := DefaultSpriteSheetFramesPerSecond; |
| 1409 |
FFrameList := TCastleSpriteSheetFrameList.Create; |
| 1410 |
end; |
| 1411 |
|
| 1412 |
destructor TCastleSpriteSheetAnimation.Destroy; |
| 1413 |
begin |
| 1414 |
FreeAndNil(FFrameList); |
| 1415 |
inherited Destroy; |
| 1416 |
end; |
| 1417 |
|
| 1418 |
procedure TCastleSpriteSheetAnimation.SetModifiedState; |
| 1419 |
begin |
| 1420 |
FSpriteSheet.SetModifiedState; |
| 1421 |
end; |
| 1422 |
|
| 1423 |
procedure TCastleSpriteSheetAnimation.SetName(const NewName: String); |
| 1424 |
begin |
| 1425 |
if NewName = FName then |
| 1426 |
Exit; |
| 1427 |
|
| 1428 |
FName := NewName; |
| 1429 |
SetModifiedState; |
| 1430 |
end; |
| 1431 |
|
| 1432 |
procedure TCastleSpriteSheetAnimation.SetFramesPerSecond(const NewFPS: Single); |
| 1433 |
begin |
| 1434 |
FFramesPerSecond := NewFPS; |
| 1435 |
SetModifiedState; |
| 1436 |
end; |
| 1437 |
|
| 1438 |
function TCastleSpriteSheetAnimation.GetFrame(const Index: Integer |
| 1439 |
): TCastleSpriteSheetFrame; |
| 1440 |
begin |
| 1441 |
Result := FFrameList[Index]; |
| 1442 |
end; |
| 1443 |
|
| 1444 |
procedure TCastleSpriteSheetAnimation.CheckEditMode; |
| 1445 |
begin |
| 1446 |
FSpriteSheet.CheckEditMode; |
| 1447 |
end; |
| 1448 |
|
| 1449 |
function TCastleSpriteSheetAnimation.FrameCount: Integer; |
| 1450 |
begin |
| 1451 |
Result := FFrameList.Count; |
| 1452 |
end; |
| 1453 |
|
| 1454 |
function TCastleSpriteSheetAnimation.FrameIndex( |
| 1455 |
const Frame: TCastleSpriteSheetFrame): Integer; |
| 1456 |
begin |
| 1457 |
Result := FFrameList.IndexOf(Frame); |
| 1458 |
end; |
| 1459 |
|
| 1460 |
function TCastleSpriteSheetAnimation.AddFrame: TCastleSpriteSheetFrame; |
| 1461 |
var |
| 1462 |
AFrame: TCastleSpriteSheetFrame; |
| 1463 |
begin |
| 1464 |
CheckEditMode; |
| 1465 |
|
| 1466 |
AFrame := TCastleSpriteSheetFrame.Create(Self); |
| 1467 |
FFrameList.Add(AFrame); |
| 1468 |
Result := AFrame; |
| 1469 |
if Assigned(FSpriteSheet.OnFrameAdded) then |
| 1470 |
FSpriteSheet.OnFrameAdded(AFrame); |
| 1471 |
SetModifiedState; |
| 1472 |
end; |
| 1473 |
|
| 1474 |
function TCastleSpriteSheetAnimation.AddFrameCopy( |
| 1475 |
const SrcFrame: TCastleSpriteSheetFrame): TCastleSpriteSheetFrame; |
| 1476 |
var |
| 1477 |
AFrame: TCastleSpriteSheetFrame; |
| 1478 |
begin |
| 1479 |
CheckEditMode; |
| 1480 |
|
| 1481 |
AFrame := TCastleSpriteSheetFrame.Create(Self, SrcFrame); |
| 1482 |
FFrameList.Add(AFrame); |
| 1483 |
Result := AFrame; |
| 1484 |
if Assigned(FSpriteSheet.OnFrameAdded) then |
| 1485 |
FSpriteSheet.OnFrameAdded(AFrame); |
| 1486 |
SetModifiedState; |
| 1487 |
end; |
| 1488 |
|
| 1489 |
function TCastleSpriteSheetAnimation.AddFrame(const FrameImageURL: String |
| 1490 |
): TCastleSpriteSheetFrame; |
| 1491 |
var |
| 1492 |
AFrame: TCastleSpriteSheetFrame; |
| 1493 |
Image: TCastleImage; |
| 1494 |
begin |
| 1495 |
CheckEditMode; |
| 1496 |
|
| 1497 |
AFrame := TCastleSpriteSheetFrame.Create(Self); |
| 1498 |
try |
| 1499 |
Image := LoadImage(FrameImageURL); |
| 1500 |
try |
| 1501 |
AFrame.SetFrameImage(Image); |
| 1502 |
finally |
| 1503 |
FreeAndNil(Image); |
| 1504 |
end; |
| 1505 |
|
| 1506 |
FFrameList.Add(AFrame); |
| 1507 |
|
| 1508 |
if Assigned(FSpriteSheet.OnFrameAdded) then |
| 1509 |
FSpriteSheet.OnFrameAdded(AFrame); |
| 1510 |
|
| 1511 |
SetModifiedState; |
| 1512 |
except |
| 1513 |
FreeAndNil(AFrame); |
| 1514 |
end; |
| 1515 |
Result := AFrame; |
| 1516 |
end; |
| 1517 |
|
| 1518 |
function TCastleSpriteSheetAnimation.AddFrame(const SourceImage: TCastleImage; |
| 1519 |
const DestX, DestY, AFrameWidth, AFrameHeight, SourceX, SourceY, |
| 1520 |
SourceWidthToCopy, SourceHeightToCopy: Integer): TCastleSpriteSheetFrame; |
| 1521 |
var |
| 1522 |
AFrame: TCastleSpriteSheetFrame; |
| 1523 |
begin |
| 1524 |
CheckEditMode; |
| 1525 |
|
| 1526 |
AFrame := TCastleSpriteSheetFrame.Create(Self); |
| 1527 |
FFrameList.Add(AFrame); |
| 1528 |
Result := AFrame; |
| 1529 |
AFrame.SetFrameImage(SourceImage, DestX, DestY, AFrameWidth, AFrameHeight, |
| 1530 |
SourceX, SourceY, SourceWidthToCopy, SourceHeightToCopy); |
| 1531 |
if Assigned(FSpriteSheet.OnFrameAdded) then |
| 1532 |
FSpriteSheet.OnFrameAdded(AFrame); |
| 1533 |
SetModifiedState; |
| 1534 |
end; |
| 1535 |
|
| 1536 |
function TCastleSpriteSheetAnimation.AllFramesHasTheSameSize: Boolean; |
| 1537 |
var |
| 1538 |
I: Integer; |
| 1539 |
begin |
| 1540 |
if FrameCount < 2 then |
| 1541 |
Exit(true); |
| 1542 |
|
| 1543 |
for I := 1 to FrameCount - 1 do |
| 1544 |
begin |
| 1545 |
if (Frame[I].FrameWidth <> Frame[I - 1].FrameWidth) or |
| 1546 |
(Frame[I].FrameHeight <> Frame[I - 1].FrameHeight) then |
| 1547 |
Exit(false); |
| 1548 |
end; |
| 1549 |
|
| 1550 |
Result := true; |
| 1551 |
end; |
| 1552 |
|
| 1553 |
procedure TCastleSpriteSheetAnimation.RemoveFrame( |
| 1554 |
const Frame: TCastleSpriteSheetFrame); |
| 1555 |
begin |
| 1556 |
CheckEditMode; |
| 1557 |
|
| 1558 |
if (Assigned(FSpriteSheet.BeforeFrameRemoved)) and |
| 1559 |
(FFrameList.Contains(Frame)) then |
| 1560 |
FSpriteSheet.BeforeFrameRemoved(Frame); |
| 1561 |
FFrameList.Remove(Frame); |
| 1562 |
SetModifiedState; |
| 1563 |
end; |
| 1564 |
|
| 1565 |
procedure TCastleSpriteSheetAnimation.MoveFrameLeft( |
| 1566 |
const Frame: TCastleSpriteSheetFrame); |
| 1567 |
var |
| 1568 |
Index: Integer; |
| 1569 |
begin |
| 1570 |
CheckEditMode; |
| 1571 |
|
| 1572 |
Index := FFrameList.IndexOf(Frame); |
| 1573 |
if Index < 1 then |
| 1574 |
Exit; |
| 1575 |
|
| 1576 |
MoveFrame(Index, Index - 1); |
| 1577 |
end; |
| 1578 |
|
| 1579 |
procedure TCastleSpriteSheetAnimation.MoveFrameRight( |
| 1580 |
const Frame: TCastleSpriteSheetFrame); |
| 1581 |
var |
| 1582 |
Index: Integer; |
| 1583 |
begin |
| 1584 |
CheckEditMode; |
| 1585 |
|
| 1586 |
Index := FFrameList.IndexOf(Frame); |
| 1587 |
if (Index = FFrameList.Count - 1) or (Index = -1) then |
| 1588 |
Exit; |
| 1589 |
|
| 1590 |
MoveFrame(Index, Index + 1); |
| 1591 |
end; |
| 1592 |
|
| 1593 |
procedure TCastleSpriteSheetAnimation.MoveFrameToTop( |
| 1594 |
const Frame: TCastleSpriteSheetFrame); |
| 1595 |
var |
| 1596 |
Index: Integer; |
| 1597 |
begin |
| 1598 |
CheckEditMode; |
| 1599 |
|
| 1600 |
Index := FFrameList.IndexOf(Frame); |
| 1601 |
if Index < 1 then |
| 1602 |
Exit; |
| 1603 |
|
| 1604 |
MoveFrame(Index, 0); |
| 1605 |
end; |
| 1606 |
|
| 1607 |
procedure TCastleSpriteSheetAnimation.MoveFrameToEnd( |
| 1608 |
const Frame: TCastleSpriteSheetFrame); |
| 1609 |
var |
| 1610 |
Index: Integer; |
| 1611 |
begin |
| 1612 |
CheckEditMode; |
| 1613 |
|
| 1614 |
Index := FFrameList.IndexOf(Frame); |
| 1615 |
if (Index = FFrameList.Count - 1) or (Index = -1) then |
| 1616 |
Exit; |
| 1617 |
|
| 1618 |
MoveFrame(Index, FFrameList.Count - 1); |
| 1619 |
end; |
| 1620 |
|
| 1621 |
procedure TCastleSpriteSheetAnimation.MoveFrame(const OldIndex, |
| 1622 |
NewIndex: Integer); |
| 1623 |
begin |
| 1624 |
CheckEditMode; |
| 1625 |
|
| 1626 |
FFrameList.Move(OldIndex, NewIndex); |
| 1627 |
|
| 1628 |
if Assigned(FSpriteSheet.OnFrameMoved) then |
| 1629 |
FSpriteSheet.OnFrameMoved(FFrameList[NewIndex], OldIndex, NewIndex); |
| 1630 |
|
| 1631 |
SetModifiedState; |
| 1632 |
end; |
| 1633 |
|
| 1634 |
procedure TCastleSpriteSheetAnimation.ImportAtlas(AtlasImageURL: String; Cols, |
| 1635 |
Rows: Integer; ImportByColumns: Boolean); |
| 1636 |
var |
| 1637 |
AtlasToImport: TCastleImage; |
| 1638 |
FrameWidth: Integer; |
| 1639 |
FrameHeight: Integer; |
| 1640 |
I, J: Integer; |
| 1641 |
begin |
| 1642 |
CheckEditMode; |
| 1643 |
|
| 1644 |
AtlasToImport := LoadImage(AtlasImageURL); |
| 1645 |
try |
| 1646 |
FrameWidth := AtlasToImport.Width div Cols; |
| 1647 |
FrameHeight := AtlasToImport.Height div Rows; |
| 1648 |
|
| 1649 |
if ImportByColumns then |
| 1650 |
begin |
| 1651 |
for J := Rows - 1 downto 0 do |
| 1652 |
for I := 0 to Cols - 1 do |
| 1653 |
begin |
| 1654 |
AddFrame(AtlasToImport, 0, 0, FrameWidth, FrameHeight, |
| 1655 |
I * FrameWidth, J * FrameHeight, FrameWidth, FrameHeight); |
| 1656 |
end; |
| 1657 |
end else |
| 1658 |
for I := 0 to Cols - 1 do |
| 1659 |
for J := Rows - 1 downto 0 do |
| 1660 |
begin |
| 1661 |
AddFrame(AtlasToImport, 0, 0, FrameWidth, FrameHeight, |
| 1662 |
I * FrameWidth, J * FrameHeight, FrameWidth, FrameHeight); |
| 1663 |
end; |
| 1664 |
|
| 1665 |
finally |
| 1666 |
FreeAndNil(AtlasToImport); |
| 1667 |
end; |
| 1668 |
end; |
| 1669 |
|
| 1670 |
function TCastleSpriteSheetAnimation.GetBigestFrameSize( |
| 1671 |
const MaxWidth, MaxHeight: Integer): TVector2Integer; |
| 1672 |
var |
| 1673 |
I: Integer; |
| 1674 |
AFrame: TCastleSpriteSheetFrame; |
| 1675 |
begin |
| 1676 |
Result.X := 0; |
| 1677 |
Result.Y := 0; |
| 1678 |
for I := 0 to FrameCount - 1 do |
| 1679 |
begin |
| 1680 |
AFrame := Frame[I]; |
| 1681 |
|
| 1682 |
if (AFrame.FrameWidth > MaxWidth) or (AFrame.FrameHeight > MaxHeight) then |
| 1683 |
continue; |
| 1684 |
|
| 1685 |
if AFrame.FrameWidth > Result.X then |
| 1686 |
begin |
| 1687 |
Result.X := AFrame.FrameWidth; |
| 1688 |
Result.Y := AFrame.FrameHeight; |
| 1689 |
end else |
| 1690 |
if (AFrame.FrameHeight > Result.Y) and (AFrame.FrameHeight > Result.X) then |
| 1691 |
begin |
| 1692 |
{ Use bigger frame with Y only if FrameHeight is bigger than FrameWidth } |
| 1693 |
Result.X := AFrame.FrameWidth; |
| 1694 |
Result.Y := AFrame.FrameHeight; |
| 1695 |
end; |
| 1696 |
end; |
| 1697 |
end; |
| 1698 |
|
| 1699 |
{ TCastleSpriteSheet } |
| 1700 |
|
| 1701 |
procedure TCastleSpriteSheet.SetURL(const NewURL: String); |
| 1702 |
begin |
| 1703 |
if FURL = NewURL then |
| 1704 |
Exit; |
| 1705 |
|
| 1706 |
FURL := NewURL; |
| 1707 |
|
| 1708 |
if Assigned(FOnURLChanged) then |
| 1709 |
FOnURLChanged(Self); |
| 1710 |
|
| 1711 |
SetModifiedState; |
| 1712 |
end; |
| 1713 |
|
| 1714 |
procedure TCastleSpriteSheet.SetModifiedState; |
| 1715 |
begin |
| 1716 |
if FModifiedState then |
| 1717 |
Exit; |
| 1718 |
|
| 1719 |
if FLoadingPending then |
| 1720 |
Exit; |
| 1721 |
|
| 1722 |
FModifiedState := true; |
| 1723 |
if Assigned(FOnModifiedStateChanged) then |
| 1724 |
FOnModifiedStateChanged(Self); |
| 1725 |
end; |
| 1726 |
|
| 1727 |
procedure TCastleSpriteSheet.ClearModifiedState; |
| 1728 |
begin |
| 1729 |
if not FModifiedState then |
| 1730 |
Exit; |
| 1731 |
|
| 1732 |
FModifiedState := false; |
| 1733 |
|
| 1734 |
if Assigned(FOnModifiedStateChanged) then |
| 1735 |
FOnModifiedStateChanged(Self); |
| 1736 |
end; |
| 1737 |
|
| 1738 |
function TCastleSpriteSheet.IsModified: Boolean; |
| 1739 |
begin |
| 1740 |
Result := FModifiedState; |
| 1741 |
end; |
| 1742 |
|
| 1743 |
procedure TCastleSpriteSheet.BeginLoad; |
| 1744 |
begin |
| 1745 |
FLoadingPending := true; |
| 1746 |
end; |
| 1747 |
|
| 1748 |
procedure TCastleSpriteSheet.EndLoad; |
| 1749 |
begin |
| 1750 |
FLoadingPending := false; |
| 1751 |
end; |
| 1752 |
|
| 1753 |
procedure TCastleSpriteSheet.CheckEditMode; |
| 1754 |
begin |
| 1755 |
if FLoadingPending then |
| 1756 |
Exit; |
| 1757 |
|
| 1758 |
if not EditMode then |
| 1759 |
raise EReadOnlyMode.Create( |
| 1760 |
'Can''t edit spritesheet in read only state (reload in edit mode).'); |
| 1761 |
end; |
| 1762 |
|
| 1763 |
constructor TCastleSpriteSheet.Create(AEditMode: Boolean); |
| 1764 |
begin |
| 1765 |
FEditMode := AEditMode; |
| 1766 |
FAnimationList := TCastleSpriteSheetAnimationList.Create; |
| 1767 |
FAtlasWidth := 1024; |
| 1768 |
FAtlasHeight := 1024; |
| 1769 |
FMaxAtlasWidth := 1024; |
| 1770 |
FMaxAtlasHeight := 1024; |
| 1771 |
end; |
| 1772 |
|
| 1773 |
destructor TCastleSpriteSheet.Destroy; |
| 1774 |
begin |
| 1775 |
FreeAndNil(FGeneratedAtlas); |
| 1776 |
FreeAndNil(FAnimationList); |
| 1777 |
inherited Destroy; |
| 1778 |
end; |
| 1779 |
|
| 1780 |
procedure TCastleSpriteSheet.Load(const URL: String); |
| 1781 |
var |
| 1782 |
URLWithoutAnchor: String; |
| 1783 |
Stream: TStream; |
| 1784 |
begin |
| 1785 |
URLWithoutAnchor := UriDeleteAnchor(URL); |
| 1786 |
|
| 1787 |
Stream := Download(URLWithoutAnchor); |
| 1788 |
try |
| 1789 |
Load(Stream, URL); |
| 1790 |
finally FreeAndNil(Stream) end; |
| 1791 |
end; |
| 1792 |
|
| 1793 |
procedure TCastleSpriteSheet.Load(const Stream: TStream; const BaseUrl: String); |
| 1794 |
var |
| 1795 |
SpriteSheetLoader: TCastleSpriteSheetLoader; |
| 1796 |
begin |
| 1797 |
SpriteSheetLoader := nil; |
| 1798 |
BeginLoad; |
| 1799 |
try |
| 1800 |
SpriteSheetLoader := TCastleSpriteSheetLoader.Create(Stream, BaseUrl, EditMode); |
| 1801 |
SpriteSheetLoader.Load(Self); |
| 1802 |
finally |
| 1803 |
FreeAndNil(SpriteSheetLoader); |
| 1804 |
EndLoad; |
| 1805 |
end; |
| 1806 |
end; |
| 1807 |
|
| 1808 |
procedure TCastleSpriteSheet.Save(const AURL: String; |
| 1809 |
const SaveCopy: Boolean = false); |
| 1810 |
|
| 1811 |
{ Copy URL contents from source to destination. |
| 1812 |
Converts URLs to filenames automatically. |
| 1813 |
Also automatically aborts copy when source and destination are equal, |
| 1814 |
which happens easily if you just want to open + resave sprite sheet |
| 1815 |
and would cause "Bad file number" exception on Linux. } |
| 1816 |
procedure CopyUrlContents(const SourceUrl, DestUrl: String); |
| 1817 |
var |
| 1818 |
SourceFileName, DestFileName: String; |
| 1819 |
begin |
| 1820 |
SourceFileName := UriToFilenameSafe(SourceUrl); |
| 1821 |
DestFileName := UriToFilenameSafe(DestUrl); |
| 1822 |
if SameFileName( |
| 1823 |
ExpandFileName(SourceFileName), |
| 1824 |
ExpandFileName(DestFileName)) then |
| 1825 |
begin |
| 1826 |
WritelnLog('Trying to copying contents between the same URLs: %s -> %s, ignoring', [ |
| 1827 |
UriDisplay(SourceUrl), |
| 1828 |
UriDisplay(DestUrl) |
| 1829 |
]); |
| 1830 |
Exit; |
| 1831 |
end; |
| 1832 |
CheckCopyFile(SourceFileName, DestFileName); |
| 1833 |
end; |
| 1834 |
|
| 1835 |
var |
| 1836 |
ExporterXML: TCastleSpriteSheetXMLExporter; |
| 1837 |
XMLDoc: TXMLDocument; |
| 1838 |
AtlasURL: String; |
| 1839 |
|
| 1840 |
FOldRelativeImagePath: String; |
| 1841 |
begin |
| 1842 |
if SaveCopy then |
| 1843 |
begin |
| 1844 |
FOldRelativeImagePath := FRelativeAtlasPath; |
| 1845 |
end; |
| 1846 |
|
| 1847 |
if IsModified or (LoadedAtlasPath = '') then |
| 1848 |
RegenerateAtlas; |
| 1849 |
|
| 1850 |
try |
| 1851 |
{ Generate atlas file name/path, use PNG as main image file format. |
| 1852 |
Maybe we should add option to set file name by user. |
| 1853 |
|
| 1854 |
When FRelativeAtlasPath is empty or URL changes (so we have save as, |
| 1855 |
we need generate new name. } |
| 1856 |
if (FRelativeAtlasPath = '') or (AURL <> URL) then |
| 1857 |
begin |
| 1858 |
FRelativeAtlasPath := DeleteUriExt(ExtractUriName(AURL)) + '.png'; |
| 1859 |
end; |
| 1860 |
AtlasURL := UriIncludeSlash(ExtractUriPath(AURL)) + FRelativeAtlasPath; |
| 1861 |
|
| 1862 |
{ Save image file } |
| 1863 |
if FGeneratedAtlas = nil then |
| 1864 |
CopyUrlContents(LoadedAtlasPath, AtlasURL) |
| 1865 |
else |
| 1866 |
SaveImage(FGeneratedAtlas, AtlasURL); |
| 1867 |
|
| 1868 |
{ Save xml (CastleSpriteSheet) file } |
| 1869 |
ExporterXML := nil; |
| 1870 |
XMLDoc := nil; |
| 1871 |
try |
| 1872 |
ExporterXML := TCastleSpriteSheetXMLExporter.Create(Self); |
| 1873 |
XMLDoc := ExporterXML.ExportToXML; |
| 1874 |
URLWriteXML(XMLDoc, AURL); |
| 1875 |
finally |
| 1876 |
FreeAndNil(ExporterXML); |
| 1877 |
FreeAndNil(XMLDoc); |
| 1878 |
end; |
| 1879 |
|
| 1880 |
if not SaveCopy then |
| 1881 |
begin |
| 1882 |
URL := AURL; |
| 1883 |
ClearModifiedState; |
| 1884 |
if Assigned(FOnFileSaved) then |
| 1885 |
FOnFileSaved(URL); |
| 1886 |
end; |
| 1887 |
finally |
| 1888 |
{ In case of copy go back to old relative image path when everything |
| 1889 |
is exported } |
| 1890 |
if SaveCopy then |
| 1891 |
FRelativeAtlasPath := FOldRelativeImagePath; |
| 1892 |
end; |
| 1893 |
end; |
| 1894 |
|
| 1895 |
function TCastleSpriteSheet.ToX3D: TX3DRootNode; |
| 1896 |
var |
| 1897 |
Exporter: TCastleSpriteSheetX3DExporter; |
| 1898 |
begin |
| 1899 |
if IsModified then |
| 1900 |
RegenerateAtlas; |
| 1901 |
|
| 1902 |
Exporter := TCastleSpriteSheetX3DExporter.Create(Self); |
| 1903 |
try |
| 1904 |
Result := Exporter.ExportToX3D; |
| 1905 |
finally |
| 1906 |
FreeAndNil(Exporter); |
| 1907 |
end; |
| 1908 |
end; |
| 1909 |
|
| 1910 |
procedure TCastleSpriteSheet.RegenerateAtlas; |
| 1911 |
var |
| 1912 |
BasicImageGen: TCastleSpriteSheetBasicAtlasGen; |
| 1913 |
begin |
| 1914 |
if not AtlasCanBeRegenrated then |
| 1915 |
raise ECantRegenerateAtlas.Create('Frames images not loaded can''t regenerate atlas.'); |
| 1916 |
|
| 1917 |
BasicImageGen := TCastleSpriteSheetBasicAtlasGen.Create(Self, FMaxAtlasWidth, |
| 1918 |
FMaxAtlasHeight); |
| 1919 |
try |
| 1920 |
BasicImageGen.Generate; |
| 1921 |
finally |
| 1922 |
FreeAndNil(BasicImageGen); |
| 1923 |
end; |
| 1924 |
end; |
| 1925 |
|
| 1926 |
function TCastleSpriteSheet.AtlasCanBeRegenrated: Boolean; |
| 1927 |
var |
| 1928 |
I, J: Integer; |
| 1929 |
Animation: TCastleSpriteSheetAnimation; |
| 1930 |
Frame: TCastleSpriteSheetFrame; |
| 1931 |
begin |
| 1932 |
for I := 0 to AnimationCount - 1 do |
| 1933 |
begin |
| 1934 |
Animation := AnimationByIndex(I); |
| 1935 |
for J := 0 to Animation.FrameCount - 1 do |
| 1936 |
begin |
| 1937 |
Frame := Animation.Frame[J]; |
| 1938 |
if not Frame.HasFrameImage then |
| 1939 |
Exit(false); |
| 1940 |
end; |
| 1941 |
end; |
| 1942 |
Result := true; |
| 1943 |
end; |
| 1944 |
|
| 1945 |
procedure TCastleSpriteSheet.SetMaxAtlasSize(const NewMaxAtlasWidth, |
| 1946 |
NewMaxAtlasHeight: Integer); |
| 1947 |
begin |
| 1948 |
if (FMaxAtlasWidth = NewMaxAtlasWidth) and |
| 1949 |
(FMaxAtlasHeight = NewMaxAtlasHeight) then |
| 1950 |
Exit; |
| 1951 |
|
| 1952 |
CheckEditMode; |
| 1953 |
|
| 1954 |
FMaxAtlasWidth := NewMaxAtlasWidth; |
| 1955 |
FMaxAtlasHeight := NewMaxAtlasHeight; |
| 1956 |
if Assigned(FOnMaxAtlasSizeChanged) then |
| 1957 |
FOnMaxAtlasSizeChanged(FMaxAtlasWidth, FMaxAtlasHeight); |
| 1958 |
SetModifiedState; |
| 1959 |
end; |
| 1960 |
|
| 1961 |
procedure TCastleSpriteSheet.GetMinAtlasSize(out MinWidth, MinHeight: Integer); |
| 1962 |
var |
| 1963 |
BasicImageGen: TCastleSpriteSheetBasicAtlasGen; |
| 1964 |
begin |
| 1965 |
BasicImageGen := TCastleSpriteSheetBasicAtlasGen.Create(Self, FMaxAtlasWidth, |
| 1966 |
FMaxAtlasHeight); |
| 1967 |
try |
| 1968 |
BasicImageGen.GetMinAtlasSize(MinWidth, MinHeight); |
| 1969 |
finally |
| 1970 |
FreeAndNil(BasicImageGen); |
| 1971 |
end; |
| 1972 |
end; |
| 1973 |
|
| 1974 |
function TCastleSpriteSheet.AnimationByName(const Name: String): TCastleSpriteSheetAnimation; |
| 1975 |
var |
| 1976 |
Animation: TCastleSpriteSheetAnimation; |
| 1977 |
begin |
| 1978 |
for Animation in FAnimationList do |
| 1979 |
begin |
| 1980 |
if Animation.Name = Name then |
| 1981 |
Exit(Animation); |
| 1982 |
end; |
| 1983 |
Result := nil; |
| 1984 |
end; |
| 1985 |
|
| 1986 |
function TCastleSpriteSheet.AnimationByIndex(const Index: Integer): TCastleSpriteSheetAnimation; |
| 1987 |
begin |
| 1988 |
Result := FAnimationList[Index]; |
| 1989 |
end; |
| 1990 |
|
| 1991 |
function TCastleSpriteSheet.AnimationCount: Integer; |
| 1992 |
begin |
| 1993 |
Result := FAnimationList.Count; |
| 1994 |
end; |
| 1995 |
|
| 1996 |
function TCastleSpriteSheet.AnimationIndex( |
| 1997 |
const Animation: TCastleSpriteSheetAnimation): Integer; |
| 1998 |
begin |
| 1999 |
Result := FAnimationList.IndexOf(Animation); |
| 2000 |
end; |
| 2001 |
|
| 2002 |
function TCastleSpriteSheet.HasAnimation(const Name: String): Boolean; |
| 2003 |
var |
| 2004 |
Animation: TCastleSpriteSheetAnimation; |
| 2005 |
begin |
| 2006 |
for Animation in FAnimationList do |
| 2007 |
begin |
| 2008 |
if Animation.Name = Name then |
| 2009 |
Exit(true); |
| 2010 |
end; |
| 2011 |
Result := false; |
| 2012 |
end; |
| 2013 |
|
| 2014 |
function TCastleSpriteSheet.AddAnimation(const Name: String): TCastleSpriteSheetAnimation; |
| 2015 |
var |
| 2016 |
Animation: TCastleSpriteSheetAnimation; |
| 2017 |
begin |
| 2018 |
if HasAnimation(Name) then |
| 2019 |
Exit(AnimationByName(Name)); |
| 2020 |
|
| 2021 |
CheckEditMode; |
| 2022 |
|
| 2023 |
Animation := TCastleSpriteSheetAnimation.Create(Self, Name); |
| 2024 |
FAnimationList.Add(Animation); |
| 2025 |
if Assigned(FOnAnimationAdded) then |
| 2026 |
FOnAnimationAdded(Animation); |
| 2027 |
SetModifiedState; |
| 2028 |
Result := Animation; |
| 2029 |
end; |
| 2030 |
|
| 2031 |
procedure TCastleSpriteSheet.MoveAnimationUp( |
| 2032 |
const Animation: TCastleSpriteSheetAnimation); |
| 2033 |
var |
| 2034 |
Index: Integer; |
| 2035 |
begin |
| 2036 |
CheckEditMode; |
| 2037 |
|
| 2038 |
Index := FAnimationList.IndexOf(Animation); |
| 2039 |
if Index < 1 then |
| 2040 |
Exit; |
| 2041 |
|
| 2042 |
MoveAnimation(Index, Index - 1); |
| 2043 |
end; |
| 2044 |
|
| 2045 |
procedure TCastleSpriteSheet.MoveAnimationDown( |
| 2046 |
const Animation: TCastleSpriteSheetAnimation); |
| 2047 |
var |
| 2048 |
Index: Integer; |
| 2049 |
begin |
| 2050 |
CheckEditMode; |
| 2051 |
|
| 2052 |
Index := FAnimationList.IndexOf(Animation); |
| 2053 |
if (Index = FAnimationList.Count - 1) or (Index = -1) then |
| 2054 |
Exit; |
| 2055 |
|
| 2056 |
MoveAnimation(Index, Index + 1); |
| 2057 |
end; |
| 2058 |
|
| 2059 |
procedure TCastleSpriteSheet.MoveAnimationToTop( |
| 2060 |
const Animation: TCastleSpriteSheetAnimation); |
| 2061 |
var |
| 2062 |
Index: Integer; |
| 2063 |
begin |
| 2064 |
CheckEditMode; |
| 2065 |
|
| 2066 |
Index := FAnimationList.IndexOf(Animation); |
| 2067 |
if Index < 1 then |
| 2068 |
Exit; |
| 2069 |
|
| 2070 |
MoveAnimation(Index, 0); |
| 2071 |
end; |
| 2072 |
|
| 2073 |
procedure TCastleSpriteSheet.MoveAnimationToEnd( |
| 2074 |
const Animation: TCastleSpriteSheetAnimation); |
| 2075 |
var |
| 2076 |
Index: Integer; |
| 2077 |
begin |
| 2078 |
CheckEditMode; |
| 2079 |
|
| 2080 |
Index := FAnimationList.IndexOf(Animation); |
| 2081 |
if (Index = FAnimationList.Count - 1) or (Index = -1) then |
| 2082 |
Exit; |
| 2083 |
|
| 2084 |
MoveAnimation(Index, FAnimationList.Count - 1); |
| 2085 |
end; |
| 2086 |
|
| 2087 |
procedure TCastleSpriteSheet.MoveAnimation(const OldIndex, NewIndex: Integer); |
| 2088 |
begin |
| 2089 |
CheckEditMode; |
| 2090 |
|
| 2091 |
FAnimationList.Move(OldIndex, NewIndex); |
| 2092 |
|
| 2093 |
if Assigned(FOnAnimationMoved) then |
| 2094 |
FOnAnimationMoved(FAnimationList[NewIndex], OldIndex, NewIndex); |
| 2095 |
|
| 2096 |
SetModifiedState; |
| 2097 |
end; |
| 2098 |
|
| 2099 |
procedure TCastleSpriteSheet.RemoveAnimation( |
| 2100 |
const Animation: TCastleSpriteSheetAnimation); |
| 2101 |
begin |
| 2102 |
CheckEditMode; |
| 2103 |
|
| 2104 |
if (Assigned(FBeforeAnimationRemoved)) and |
| 2105 |
(FAnimationList.Contains(Animation)) then |
| 2106 |
FBeforeAnimationRemoved(Animation); |
| 2107 |
FAnimationList.Remove(Animation); |
| 2108 |
SetModifiedState; |
| 2109 |
end; |
| 2110 |
|
| 2111 |
procedure TCastleSpriteSheet.RemoveAnimationByName(const Name: String); |
| 2112 |
var |
| 2113 |
I: Integer; |
| 2114 |
Animation: TCastleSpriteSheetAnimation; |
| 2115 |
begin |
| 2116 |
CheckEditMode; |
| 2117 |
|
| 2118 |
for I := 0 to FAnimationList.Count - 1 do |
| 2119 |
begin |
| 2120 |
Animation := FAnimationList[I]; |
| 2121 |
if Animation.Name = Name then |
| 2122 |
begin |
| 2123 |
if Assigned(FBeforeAnimationRemoved) then |
| 2124 |
FBeforeAnimationRemoved(Animation); |
| 2125 |
FAnimationList.Delete(I); |
| 2126 |
SetModifiedState; |
| 2127 |
end; |
| 2128 |
end; |
| 2129 |
end; |
| 2130 |
|
| 2131 |
procedure TCastleSpriteSheet.RemoveAnimationByIndex(const Index: Integer); |
| 2132 |
begin |
| 2133 |
CheckEditMode; |
| 2134 |
|
| 2135 |
if Assigned(FBeforeAnimationRemoved) then |
| 2136 |
FBeforeAnimationRemoved(FAnimationList[Index]); |
| 2137 |
|
| 2138 |
FAnimationList.Delete(Index); |
| 2139 |
SetModifiedState; |
| 2140 |
end; |
| 2141 |
|
| 2142 |
function TCastleSpriteSheet.ProposeAnimationName: String; |
| 2143 |
const |
| 2144 |
NamePrefix = 'NewAnim'; |
| 2145 |
var |
| 2146 |
I: Integer; |
| 2147 |
begin |
| 2148 |
if not HasAnimation(NamePrefix) then |
| 2149 |
Exit (NamePrefix); |
| 2150 |
|
| 2151 |
I := 1; |
| 2152 |
|
| 2153 |
while true do |
| 2154 |
begin |
| 2155 |
if not HasAnimation(NamePrefix + IntTostr(I)) then |
| 2156 |
Exit(NamePrefix + IntTostr(I)); |
| 2157 |
Inc(I); |
| 2158 |
end; |
| 2159 |
end; |
| 2160 |
|
| 2161 |
{ TCastleSpriteSheetLoader ---------------------------------------------------} |
| 2162 |
|
| 2163 |
procedure TCastleSpriteSheetLoader.ReadImportSettings; |
| 2164 |
var |
| 2165 |
SettingsMap: TStringStringMap; |
| 2166 |
Setting: {$ifdef FPC}TStringStringMap.TDictionaryPair{$else}TPair<String, String>{$endif}; |
| 2167 |
begin |
| 2168 |
// default values |
| 2169 |
FFramesPerSecond := DefaultSpriteSheetFramesPerSecond; |
| 2170 |
|
| 2171 |
if FStarlingLoading then |
| 2172 |
begin |
| 2173 |
SettingsMap := TStringStringMap.Create; |
| 2174 |
try |
| 2175 |
UriGetSettingsFromAnchor(FBaseUrl, SettingsMap); |
| 2176 |
for Setting in SettingsMap do |
| 2177 |
begin |
| 2178 |
if LowerCase(Setting.Key) = 'fps' then |
| 2179 |
begin |
| 2180 |
FFramesPerSecond := StrToFloatDot(Setting.Value); |
| 2181 |
end else |
| 2182 |
if LowerCase(Setting.Key) = 'anim-naming' then |
| 2183 |
begin |
| 2184 |
if Setting.Value = 'strict-underscore' then |
| 2185 |
FSubTexture.FAnimationNaming := anStrictUnderscore |
| 2186 |
else if Setting.Value = 'trailing-number' then |
| 2187 |
FSubTexture.FAnimationNaming := anTralingNumber |
| 2188 |
else |
| 2189 |
WritelnWarning('Starling', 'Unknown anim-naming value (%s) in "%s" anchor.', |
| 2190 |
[Setting.Value, FDisplayURL]); |
| 2191 |
end else |
| 2192 |
WritelnWarning('Starling', 'Unknown setting (%s) in "%s" anchor.', |
| 2193 |
[Setting.Key, FDisplayURL]); |
| 2194 |
end; |
| 2195 |
finally |
| 2196 |
FreeAndNil(SettingsMap); |
| 2197 |
end; |
| 2198 |
end else |
| 2199 |
FSubTexture.FAnimationNaming := anStrictUnderscore; |
| 2200 |
end; |
| 2201 |
|
| 2202 |
procedure TCastleSpriteSheetLoader.ReadImageProperties(const URL: String; |
| 2203 |
const AtlasNode: TDOMElement); |
| 2204 |
var |
| 2205 |
Image: TCastleImage; |
| 2206 |
begin |
| 2207 |
FRelativeImagePath := AtlasNode.AttributeString('imagePath'); |
| 2208 |
FAbsoluteImagePath := ExtractUriPath(UriDeleteAnchor(URL)) + FRelativeImagePath; |
| 2209 |
{ Some exporters, like Free Texture Packer and |
| 2210 |
Castle Game Engine itself (in TCastleSpriteSheetXMLExporter.ExportToXML) |
| 2211 |
store width and height attributes. |
| 2212 |
This is good -- in this case we don't need to load image to know them, |
| 2213 |
which avoids loading the image each time at sprite sheet open |
| 2214 |
(image will be loaded later by TImageTextureNode anyway, |
| 2215 |
but it can do it smarter, using cache; |
| 2216 |
trying to use cache here, in a way that remains active later, caused |
| 2217 |
too many code complications). } |
| 2218 |
if AtlasNode.HasAttribute('width') and AtlasNode.HasAttribute('height') then |
| 2219 |
begin |
| 2220 |
FImageWidth := AtlasNode.AttributeInteger('width'); |
| 2221 |
FImageHeight := AtlasNode.AttributeInteger('height'); |
| 2222 |
end else |
| 2223 |
begin |
| 2224 |
WritelnWarning('The sprite sheet "%s" loading is not optimal. We advise to save this using the latest Castle Game Engine sprite sheet editor, to benefit from faster loading. If this is already in ".castle-sprite-sheet" format, then just open + save it from CGE editor.', [ |
| 2225 |
UriDisplay(URL) |
| 2226 |
]); |
| 2227 |
Image := LoadImage(FAbsoluteImagePath); |
| 2228 |
try |
| 2229 |
FImageWidth := Image.Width; |
| 2230 |
FImageHeight := Image.Height; |
| 2231 |
finally |
| 2232 |
FreeAndNil(Image); |
| 2233 |
end; |
| 2234 |
end; |
| 2235 |
end; |
| 2236 |
|
| 2237 |
procedure TCastleSpriteSheetLoader.ReadFramesPerSecond( |
| 2238 |
const SubTextureNode: TDOMElement); |
| 2239 |
begin |
| 2240 |
FFramesPerSecond := SubTextureNode.AttributeSingleDef('fps', FFramesPerSecond); |
| 2241 |
end; |
| 2242 |
|
| 2243 |
procedure TCastleSpriteSheetLoader.PrepareSpriteSheet( |
| 2244 |
const SpriteSheet: TCastleSpriteSheet); |
| 2245 |
begin |
| 2246 |
if not FStarlingLoading then |
| 2247 |
SpriteSheet.URL := FBaseUrl; |
| 2248 |
SpriteSheet.LoadedAtlasPath := FAbsoluteImagePath; |
| 2249 |
SpriteSheet.RelativeAtlasPath := FRelativeImagePath; |
| 2250 |
FreeAndNil(FImage); |
| 2251 |
if FLoadForEdit then |
| 2252 |
FImage := LoadImage(FAbsoluteImagePath); |
| 2253 |
SpriteSheet.FAtlasWidth := FImageWidth; |
| 2254 |
SpriteSheet.FAtlasHeight := FImageHeight; |
| 2255 |
SpriteSheet.FMaxAtlasWidth := FImageWidth; |
| 2256 |
SpriteSheet.FMaxAtlasHeight := FImageHeight; |
| 2257 |
end; |
| 2258 |
|
| 2259 |
procedure TCastleSpriteSheetLoader.AddFrame( |
| 2260 |
const Animation: TCastleSpriteSheetAnimation); |
| 2261 |
var |
| 2262 |
Frame: TCastleSpriteSheetFrame; |
| 2263 |
FrameYInCGECoords: Integer; |
| 2264 |
YInCGECoords: Integer; |
| 2265 |
begin |
| 2266 |
Frame := Animation.AddFrame; |
| 2267 |
|
| 2268 |
// we need to go to CGE coords here |
| 2269 |
YInCGECoords := FImageHeight - FSubTexture.Y - FSubTexture.Height; |
| 2270 |
|
| 2271 |
{ When we load not for edit } |
| 2272 |
if not FLoadForEdit then |
| 2273 |
begin |
| 2274 |
Frame.WidthInAtlas := FSubTexture.Width; |
| 2275 |
Frame.HeightInAtlas := FSubTexture.Height; |
| 2276 |
Frame.Trimmed := FSubTexture.Trimmed; |
| 2277 |
Frame.XInAtlas := FSubTexture.X; |
| 2278 |
Frame.YInAtlas := YInCGECoords; |
| 2279 |
if Frame.Trimmed then |
| 2280 |
begin |
| 2281 |
Frame.FrameWidth := FSubTexture.FrameWidth; |
| 2282 |
Frame.FrameHeight := FSubTexture.FrameHeight; |
| 2283 |
Frame.LeftOffset := FSubTexture.FrameX; |
| 2284 |
Frame.TopOffset := FSubTexture.FrameY; |
| 2285 |
end else |
| 2286 |
begin |
| 2287 |
// make data always OK |
| 2288 |
Frame.LeftOffset := 0; |
| 2289 |
Frame.TopOffset := 0; |
| 2290 |
Frame.FrameWidth := Frame.WidthInAtlas; |
| 2291 |
Frame.FrameHeight := Frame.HeightInAtlas; |
| 2292 |
end; |
| 2293 |
Exit; |
| 2294 |
end; |
| 2295 |
|
| 2296 |
{ If we want load sprite sheet for edit } |
| 2297 |
FrameYInCGECoords := FSubTexture.FrameHeight - (-FSubTexture.FrameY + |
| 2298 |
FSubTexture.Height); |
| 2299 |
|
| 2300 |
Frame.SetFrameImage(FImage, |
| 2301 |
-FSubTexture.FrameX, |
| 2302 |
FrameYInCGECoords, |
| 2303 |
FSubTexture.FrameWidth, |
| 2304 |
FSubTexture.FrameHeight, |
| 2305 |
FSubTexture.X, |
| 2306 |
YInCGECoords, |
| 2307 |
FSubTexture.Width, |
| 2308 |
FSubTexture.Height |
| 2309 |
); |
| 2310 |
end; |
| 2311 |
|
| 2312 |
constructor TCastleSpriteSheetLoader.Create(const Stream: TStream; const BaseUrl: String; LoadForEdit: Boolean); |
| 2313 |
var |
| 2314 |
MimeType: String; |
| 2315 |
begin |
| 2316 |
inherited Create; |
| 2317 |
FStream := Stream; |
| 2318 |
FBaseUrl := BaseUrl; |
| 2319 |
FDisplayURL := UriDisplay(FBaseUrl); |
| 2320 |
|
| 2321 |
MimeType := UriMimeType(FBaseUrl); |
| 2322 |
FStarlingLoading := |
| 2323 |
(MimeType = 'application/x-starling-sprite-sheet') or |
| 2324 |
(MimeType = 'application/xml'); |
| 2325 |
|
| 2326 |
FSubTexture := TSubTexture.Create; |
| 2327 |
FLoadForEdit := LoadForEdit; |
| 2328 |
end; |
| 2329 |
|
| 2330 |
destructor TCastleSpriteSheetLoader.Destroy; |
| 2331 |
begin |
| 2332 |
FreeAndNil(FSubTexture); |
| 2333 |
FreeAndNil(FImage); |
| 2334 |
inherited Destroy; |
| 2335 |
end; |
| 2336 |
|
| 2337 |
procedure TCastleSpriteSheetLoader.Load( |
| 2338 |
const SpriteSheet: TCastleSpriteSheet); |
| 2339 |
var |
| 2340 |
Doc: TXMLDocument; |
| 2341 |
AtlasNode: TDOMElement; |
| 2342 |
I: TXMLElementIterator; |
| 2343 |
LastAnimationName: String; |
| 2344 |
CurrentAnimation: TCastleSpriteSheetAnimation; |
| 2345 |
begin |
| 2346 |
ReadImportSettings; |
| 2347 |
|
| 2348 |
LastAnimationName := ''; |
| 2349 |
CurrentAnimation := nil; |
| 2350 |
|
| 2351 |
Doc := nil; |
| 2352 |
try |
| 2353 |
ReadXMLFile(Doc, FStream); |
| 2354 |
|
| 2355 |
Check(Doc.DocumentElement.TagName8 = 'TextureAtlas', |
| 2356 |
'Root of CastleSpriteSheet file must be <TextureAtlas>'); |
| 2357 |
|
| 2358 |
AtlasNode := Doc.DocumentElement; |
| 2359 |
ReadImageProperties(FBaseUrl, AtlasNode); |
| 2360 |
|
| 2361 |
PrepareSpriteSheet(SpriteSheet); |
| 2362 |
|
| 2363 |
I := AtlasNode.ChildrenIterator('SubTexture'); |
| 2364 |
try |
| 2365 |
while I.GetNext do |
| 2366 |
begin |
| 2367 |
{ Read frame from XML } |
| 2368 |
FSubTexture.ReadFormXMLNode(I.Current, FImageWidth, FImageHeight); |
| 2369 |
|
| 2370 |
if LastAnimationName <> FSubTexture.AnimationName then |
| 2371 |
begin |
| 2372 |
{ First frame of animation loaded. } |
| 2373 |
LastAnimationName := FSubTexture.AnimationName; |
| 2374 |
CurrentAnimation := SpriteSheet.AddAnimation(LastAnimationName); |
| 2375 |
|
| 2376 |
{ Check fps (CastleSpriteSheet extension) } |
| 2377 |
ReadFramesPerSecond(I.Current); |
| 2378 |
CurrentAnimation.FramesPerSecond := FFramesPerSecond; |
| 2379 |
|
| 2380 |
AddFrame(CurrentAnimation); |
| 2381 |
end else |
| 2382 |
begin |
| 2383 |
{ Next frame of animation } |
| 2384 |
AddFrame(CurrentAnimation); |
| 2385 |
end; |
| 2386 |
end; |
| 2387 |
finally |
| 2388 |
FreeAndNil(I); |
| 2389 |
end; |
| 2390 |
finally |
| 2391 |
FreeAndNil(Doc); |
| 2392 |
end; |
| 2393 |
end; |
| 2394 |
|
| 2395 |
function TCastleSpriteSheetLoader.Load: TCastleSpriteSheet; |
| 2396 |
var |
| 2397 |
SpriteSheet: TCastleSpriteSheet; |
| 2398 |
begin |
| 2399 |
SpriteSheet := TCastleSpriteSheet.Create(FLoadForEdit); |
| 2400 |
SpriteSheet.BeginLoad; |
| 2401 |
try |
| 2402 |
try |
| 2403 |
Load(SpriteSheet); |
| 2404 |
Result := SpriteSheet; |
| 2405 |
except |
| 2406 |
FreeAndNil(SpriteSheet); |
| 2407 |
raise; |
| 2408 |
end; |
| 2409 |
finally |
| 2410 |
SpriteSheet.EndLoad; |
| 2411 |
end; |
| 2412 |
end; |
| 2413 |
|
| 2414 |
{ TStarlingSubTexture } |
| 2415 |
|
| 2416 |
procedure TCastleSpriteSheetLoader.TSubTexture.ParseAnimationName( |
| 2417 |
const SubTextureName: String); |
| 2418 |
var |
| 2419 |
UnderscorePos: SizeInt; |
| 2420 |
FrameNumberStr: String; |
| 2421 |
FrameNumber: Integer; |
| 2422 |
AnimationNameLength: Integer; |
| 2423 |
begin |
| 2424 |
{ Some files has file type Extensions like "walk/0001.png" or walk_1.png } |
| 2425 |
AnimationName := DeleteFileExt(SubTextureName); |
| 2426 |
|
| 2427 |
case FAnimationNaming of |
| 2428 |
anStrictUnderscore: |
| 2429 |
begin |
| 2430 |
UnderscorePos := BackPos('_', AnimationName); |
| 2431 |
|
| 2432 |
{ Check characters after underscore is number if not don't change name. } |
| 2433 |
AnimationNameLength := Length(AnimationName); |
| 2434 |
if (UnderscorePos > 0) and (AnimationNameLength > UnderscorePos) then |
| 2435 |
begin |
| 2436 |
FrameNumberStr := Copy(AnimationName, UnderscorePos + 1, AnimationNameLength - UnderscorePos); |
| 2437 |
if TryStrToInt(FrameNumberStr, FrameNumber) then |
| 2438 |
begin |
| 2439 |
Delete(AnimationName, UnderscorePos, AnimationNameLength - UnderscorePos + 1); |
| 2440 |
end; |
| 2441 |
end; |
| 2442 |
end; |
| 2443 |
anTralingNumber: |
| 2444 |
begin |
| 2445 |
{$ifdef FPC} |
| 2446 |
RemoveTrailingChars(AnimationName, ['0'..'9']); |
| 2447 |
{$else} |
| 2448 |
AnimationName := AnimationName.TrimRight(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']); |
| 2449 |
{$endif} |
| 2450 |
|
| 2451 |
if AnimationName <> '_' then // do not remove underscore if it's the only character in name |
| 2452 |
AnimationName := SuffixRemove('_', AnimationName, false); |
| 2453 |
end; |
| 2454 |
end; |
| 2455 |
|
| 2456 |
if AnimationName = '' then |
| 2457 |
begin |
| 2458 |
WritelnWarning('Starling', 'Incorrect animation name (%s), I set to "unknown"', |
| 2459 |
[SubTextureName]); |
| 2460 |
AnimationName := 'unknown'; |
| 2461 |
end; |
| 2462 |
end; |
| 2463 |
|
| 2464 |
procedure TCastleSpriteSheetLoader.TSubTexture.ReadFormXMLNode( |
| 2465 |
const SubTextureNode: TDOMElement; const ImageWidth, ImageHeight: Integer); |
| 2466 |
begin |
| 2467 |
ParseAnimationName(SubTextureNode.AttributeString('name')); |
| 2468 |
|
| 2469 |
X := SubTextureNode.AttributeInteger('x'); |
| 2470 |
Y := SubTextureNode.AttributeInteger('y'); |
| 2471 |
Width := SubTextureNode.AttributeInteger('width'); |
| 2472 |
Height := SubTextureNode.AttributeInteger('height'); |
| 2473 |
|
| 2474 |
Trimmed := SubTextureNode.HasAttribute('frameX'); |
| 2475 |
FrameX := SubTextureNode.AttributeIntegerDef('frameX', 0); |
| 2476 |
FrameY := SubTextureNode.AttributeIntegerDef('frameY', 0); |
| 2477 |
FrameWidth := SubTextureNode.AttributeIntegerDef('frameWidth', Width); |
| 2478 |
FrameHeight := SubTextureNode.AttributeIntegerDef('frameHeight', Height); |
| 2479 |
end; |
| 2480 |
|
| 2481 |
constructor TCastleSpriteSheetLoader.TSubTexture.Create; |
| 2482 |
begin |
| 2483 |
inherited Create; |
| 2484 |
FAnimationNaming := anStrictUnderscore; |
| 2485 |
end; |
| 2486 |
|
| 2487 |
var |
| 2488 |
ModelFormat: TModelFormat; |
| 2489 |
initialization |
| 2490 |
ModelFormat := TModelFormat.Create; |
| 2491 |
ModelFormat.OnLoad := {$ifdef FPC}@{$endif} LoadCastleSpriteSheet; |
| 2492 |
ModelFormat.MimeTypes.Add('application/x-castle-sprite-sheet'); |
| 2493 |
ModelFormat.FileFilterName := 'Castle Sprite Sheet (*.castle-sprite-sheet)'; |
| 2494 |
ModelFormat.Extensions.Add('.castle-sprite-sheet'); |
| 2495 |
RegisterModelFormat(ModelFormat); |
| 2496 |
|
| 2497 |
{ Starling sprite sheets are actually loaded exactly the same way as CGE |
| 2498 |
sprite sheets, but this is implementation detail. |
| 2499 |
We register it as separate format, with separate FileFilterName. } |
| 2500 |
ModelFormat := TModelFormat.Create; |
| 2501 |
ModelFormat.OnLoad := {$ifdef FPC}@{$endif} LoadCastleSpriteSheet; |
| 2502 |
ModelFormat.MimeTypes.Add('application/x-starling-sprite-sheet'); |
| 2503 |
ModelFormat.FileFilterName := 'Starling Sprite Sheet (*.starling-xml)'; |
| 2504 |
ModelFormat.Extensions.Add('.starling-xml'); |
| 2505 |
RegisterModelFormat(ModelFormat); |
| 2506 |
end. |