| 1 |
{ |
| 2 |
Copyright 2020-2023 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 |
unit FormSpriteSheetEditor; |
| 17 |
|
| 18 |
{$mode objfpc}{$H+} |
| 19 |
|
| 20 |
interface |
| 21 |
|
| 22 |
uses |
| 23 |
Classes, SysUtils, Generics.Collections, Forms, Controls, Graphics, Dialogs, |
| 24 |
ExtCtrls, ComCtrls, Buttons, ActnList, StdCtrls, Spin, Menus, |
| 25 |
CastleControl, CastleDialogs, CastleScene, CastleInternalSpriteSheet, CastleVectors, |
| 26 |
CastleViewport, CastleCameras, |
| 27 |
DataModuleIcons; |
| 28 |
|
| 29 |
type |
| 30 |
TSpriteSheetEditorForm = class(TForm) |
| 31 |
ActionCloseSpriteSheetEditor: TAction; |
| 32 |
ActionImportAtlas: TAction; |
| 33 |
ActionCreateNewAnimationFromSelection: TAction; |
| 34 |
ActionMoveAnimationEnd: TAction; |
| 35 |
ActionMoveAnimationTop: TAction; |
| 36 |
ActionMoveAnimationDown: TAction; |
| 37 |
ActionMoveAnimationUp: TAction; |
| 38 |
ActionMoveFrameEnd: TAction; |
| 39 |
ActionMoveFrameTop: TAction; |
| 40 |
ActionMoveFrameRight: TAction; |
| 41 |
ActionMoveFrameLeft: TAction; |
| 42 |
ActionAddAnimation: TAction; |
| 43 |
ActionAddFrame: TAction; |
| 44 |
ActionRenameAnimation: TAction; |
| 45 |
ActionDeleteAnimation: TAction; |
| 46 |
ActionDeleteFrame: TAction; |
| 47 |
ActionSaveSpriteSheetAs: TAction; |
| 48 |
ActionSaveSpriteSheet: TAction; |
| 49 |
ActionNewSpriteSheet: TAction; |
| 50 |
ActionOpenSpriteSheet: TAction; |
| 51 |
ActionListSpriteSheet: TActionList; |
| 52 |
ButtonCloseWindow: TButton; |
| 53 |
CastleControlPreview: TCastleControl; |
| 54 |
CastleOpenImageDialog: TCastleOpenImageDialog; |
| 55 |
CastleImportAtlasDialog: TCastleOpenImageDialog; |
| 56 |
ImageAtlasSizeWarning: TImage; |
| 57 |
ImageListFrames: TImageList; |
| 58 |
LabelAtlasWarning: TLabel; |
| 59 |
LabelAtlasSizeError: TLabel; |
| 60 |
LabelMaximumAtlasSize: TLabel; |
| 61 |
LabelNoFrameToShow: TLabel; |
| 62 |
ListViewAnimations: TListView; |
| 63 |
MainMenuItemAddAnimation: TMenuItem; |
| 64 |
MainMenuItemDeleteAnimation: TMenuItem; |
| 65 |
MainMenuItemMoveFrameUp: TMenuItem; |
| 66 |
MainMenuItemMoveFrameDown: TMenuItem; |
| 67 |
MainMenuItemMoveFrameTop: TMenuItem; |
| 68 |
MainMenuItemMoveFrameEnd: TMenuItem; |
| 69 |
MainMenuItemAddFrame: TMenuItem; |
| 70 |
MainMenuItemDeleteFrame: TMenuItem; |
| 71 |
MainMenuItemCreateAnimFromSelection: TMenuItem; |
| 72 |
MainMenuItemImportAtlas: TMenuItem; |
| 73 |
MenuItemClose: TMenuItem; |
| 74 |
MenuItemSeparator: TMenuItem; |
| 75 |
MenuItemImportAtlas: TMenuItem; |
| 76 |
N6: TMenuItem; |
| 77 |
N5: TMenuItem; |
| 78 |
N4: TMenuItem; |
| 79 |
N3: TMenuItem; |
| 80 |
MenuItemCreateNewAnimationFromSelection: TMenuItem; |
| 81 |
N2: TMenuItem; |
| 82 |
N1: TMenuItem; |
| 83 |
MenuItemRenameAnimation: TMenuItem; |
| 84 |
MenuItemFrameMenu: TMenuItem; |
| 85 |
MenuItemAnimationMenu: TMenuItem; |
| 86 |
MenuItemAnimationEnd: TMenuItem; |
| 87 |
MenuItemAnimationTop: TMenuItem; |
| 88 |
MenuItemAnimationDown: TMenuItem; |
| 89 |
MenuItemMoveAnimationUp: TMenuItem; |
| 90 |
MenuItemMoveEnd: TMenuItem; |
| 91 |
MenuItemMoveFrameToTop: TMenuItem; |
| 92 |
MenuItemMoveFrameDown: TMenuItem; |
| 93 |
MenuItemMoveFrameUp: TMenuItem; |
| 94 |
MenuItemAddAnimation: TMenuItem; |
| 95 |
MenuItemAddFrame: TMenuItem; |
| 96 |
MenuItemRename: TMenuItem; |
| 97 |
MenuItemDeleteAnimation: TMenuItem; |
| 98 |
MenuItemDeleteFrame: TMenuItem; |
| 99 |
OpenDialog: TCastleOpenDialog; |
| 100 |
PanelPreviewHead: TPanel; |
| 101 |
PopupMenuAnimations: TPopupMenu; |
| 102 |
PopupMenuFrames: TPopupMenu; |
| 103 |
RadioAnimation: TRadioButton; |
| 104 |
RadioFrame: TRadioButton; |
| 105 |
SaveDialog: TCastleSaveDialog; |
| 106 |
FloatSpinEditFPS: TFloatSpinEdit; |
| 107 |
LabelPreview: TLabel; |
| 108 |
LabelFrames: TLabel; |
| 109 |
LabelAnimations: TLabel; |
| 110 |
LabelFPS: TLabel; |
| 111 |
ListViewFrames: TListView; |
| 112 |
MainMenu: TMainMenu; |
| 113 |
MenuItemNew: TMenuItem; |
| 114 |
MenuItemOpen: TMenuItem; |
| 115 |
MenuItemSave: TMenuItem; |
| 116 |
MenuItemSaveAs: TMenuItem; |
| 117 |
MenuItemFile: TMenuItem; |
| 118 |
PanelFPS: TPanel; |
| 119 |
PanelRight: TPanel; |
| 120 |
PanelMiddle: TPanel; |
| 121 |
PanelTop: TPanel; |
| 122 |
PanelLeft: TPanel; |
| 123 |
SpeedButtonOpenSpriteSheet: TSpeedButton; |
| 124 |
SpeedButtonNewSpriteSheet: TSpeedButton; |
| 125 |
SpeedButtonAddAnimation: TSpeedButton; |
| 126 |
SpeedButtonSaveSpriteSheet: TSpeedButton; |
| 127 |
SpeedButtonRemoveAnimation: TSpeedButton; |
| 128 |
SpeedButtonSaveSpriteSheetAs: TSpeedButton; |
| 129 |
SpinEditMaxAtlasSize: TSpinEdit; |
| 130 |
SplitterRight: TSplitter; |
| 131 |
SplitterLeft: TSplitter; |
| 132 |
procedure ActionAddAnimationExecute(Sender: TObject); |
| 133 |
procedure ActionAddAnimationUpdate(Sender: TObject); |
| 134 |
procedure ActionAddFrameExecute(Sender: TObject); |
| 135 |
procedure ActionAddFrameUpdate(Sender: TObject); |
| 136 |
procedure ActionCloseSpriteSheetEditorExecute(Sender: TObject); |
| 137 |
procedure ActionCreateNewAnimationFromSelectionExecute(Sender: TObject); |
| 138 |
procedure ActionCreateNewAnimationFromSelectionUpdate(Sender: TObject); |
| 139 |
procedure ActionImportAtlasExecute(Sender: TObject); |
| 140 |
procedure ActionMoveAnimationDownExecute(Sender: TObject); |
| 141 |
procedure ActionMoveAnimationDownUpdate(Sender: TObject); |
| 142 |
procedure ActionMoveAnimationEndExecute(Sender: TObject); |
| 143 |
procedure ActionMoveAnimationEndUpdate(Sender: TObject); |
| 144 |
procedure ActionMoveAnimationTopExecute(Sender: TObject); |
| 145 |
procedure ActionMoveAnimationTopUpdate(Sender: TObject); |
| 146 |
procedure ActionMoveAnimationUpExecute(Sender: TObject); |
| 147 |
procedure ActionMoveAnimationUpUpdate(Sender: TObject); |
| 148 |
procedure ActionMoveFrameRightExecute(Sender: TObject); |
| 149 |
procedure ActionMoveFrameRightUpdate(Sender: TObject); |
| 150 |
procedure ActionMoveFrameEndExecute(Sender: TObject); |
| 151 |
procedure ActionMoveFrameEndUpdate(Sender: TObject); |
| 152 |
procedure ActionMoveFrameTopExecute(Sender: TObject); |
| 153 |
procedure ActionMoveFrameTopUpdate(Sender: TObject); |
| 154 |
procedure ActionMoveFrameLeftExecute(Sender: TObject); |
| 155 |
procedure ActionMoveFrameLeftUpdate(Sender: TObject); |
| 156 |
procedure ActionNewSpriteSheetExecute(Sender: TObject); |
| 157 |
procedure ActionOpenSpriteSheetExecute(Sender: TObject); |
| 158 |
procedure ActionDeleteAnimationExecute(Sender: TObject); |
| 159 |
procedure ActionDeleteAnimationUpdate(Sender: TObject); |
| 160 |
procedure ActionDeleteFrameExecute(Sender: TObject); |
| 161 |
procedure ActionDeleteFrameUpdate(Sender: TObject); |
| 162 |
procedure ActionRenameAnimationExecute(Sender: TObject); |
| 163 |
procedure ActionRenameAnimationUpdate(Sender: TObject); |
| 164 |
procedure ActionSaveSpriteSheetAsExecute(Sender: TObject); |
| 165 |
procedure ActionSaveSpriteSheetExecute(Sender: TObject); |
| 166 |
procedure FloatSpinEditFPSChange(Sender: TObject); |
| 167 |
procedure FormCloseQuery(Sender: TObject; var CanClose: boolean); |
| 168 |
procedure FormCreate(Sender: TObject); |
| 169 |
procedure FormDestroy(Sender: TObject); |
| 170 |
procedure FormShow(Sender: TObject); |
| 171 |
procedure ListViewAnimationsDragDrop(Sender, Source: TObject; X, Y: Integer |
| 172 |
); |
| 173 |
procedure ListViewAnimationsDragOver(Sender, Source: TObject; X, |
| 174 |
Y: Integer; State: TDragState; var Accept: Boolean); |
| 175 |
procedure ListViewAnimationsEdited(Sender: TObject; Item: TListItem; |
| 176 |
var AValue: String); |
| 177 |
procedure ListViewAnimationsSelectItem(Sender: TObject; Item: TListItem; |
| 178 |
Selected: Boolean); |
| 179 |
procedure ListViewFramesSelectItem(Sender: TObject; Item: TListItem; |
| 180 |
Selected: Boolean); |
| 181 |
procedure PopupMenuCloseGTK2Fix(Sender: TObject); |
| 182 |
procedure RadioFrameChange(Sender: TObject); |
| 183 |
procedure SpinEditMaxAtlasSizeChange(Sender: TObject); |
| 184 |
procedure SpinEditMaxAtlasSizeEditingDone(Sender: TObject); |
| 185 |
private |
| 186 |
type |
| 187 |
TPreviewMode = (pmAnimation, pmFrame); |
| 188 |
|
| 189 |
TSelectedFrames = class |
| 190 |
strict private |
| 191 |
type |
| 192 |
TListOfSelectedFrames = specialize TList<TCastleSpriteSheetFrame>; |
| 193 |
var |
| 194 |
FSelectedFrames: TListOfSelectedFrames; |
| 195 |
FrameListView: TListView; |
| 196 |
public |
| 197 |
constructor Create(const FrameListViewToMonitor: TListView); |
| 198 |
destructor Destroy; override; |
| 199 |
|
| 200 |
procedure GetCurrentSelection; |
| 201 |
procedure SetSelection; |
| 202 |
procedure Clear; |
| 203 |
function GetFrameByIndex(const Index: Integer): TCastleSpriteSheetFrame; |
| 204 |
function FrameCount: Integer; |
| 205 |
|
| 206 |
{ This function is preparation for adding "+" button to ListViewFrames } |
| 207 |
class function IsFrameListItem(const ItemToCheck: TListItem): Boolean; |
| 208 |
|
| 209 |
property Frame[Index: Integer]: TCastleSpriteSheetFrame read GetFrameByIndex; |
| 210 |
end; |
| 211 |
|
| 212 |
const |
| 213 |
{ Max size for frame image in list view } |
| 214 |
MaxFrameIconSize = 256; |
| 215 |
{ Default size for frame image in list view } |
| 216 |
DefaultFrameIconSize = 128; |
| 217 |
{ Preview margin } |
| 218 |
PreviewMargin = 1; |
| 219 |
var |
| 220 |
FSpriteSheet: TCastleSpriteSheet; |
| 221 |
FPreviewScene: TCastleScene; |
| 222 |
FViewport: TCastleViewport; |
| 223 |
FNavigation: TCastle2DNavigation; |
| 224 |
FWindowTitle: String; |
| 225 |
CurrentFrameIconSize: TVector2Integer; // current frame size in list view |
| 226 |
{ Should we select added animation (not always desirable) } |
| 227 |
FSelectNewAnimation: Boolean; |
| 228 |
{ RC for LockUpdatePreview/UnlockUpdatePreview } |
| 229 |
FLockUpdatePReviewRC: Integer; |
| 230 |
{ If @true, then |
| 231 |
- FPreviewScene <> nil and |
| 232 |
- FPreviewScene.RootNode contents are synchronized to show all |
| 233 |
animations in current FSpriteSheet contents, |
| 234 |
i.e. they are FSpriteSheet.ToX3D. } |
| 235 |
FNodesShowSpriteSheetAnimation: Boolean; |
| 236 |
|
| 237 |
function ProposeSaveSpriteSheet: Boolean; |
| 238 |
{ Returns true if sprite sheet was closed } |
| 239 |
function CloseSpriteSheet: Boolean; |
| 240 |
procedure AssignEventsToSpriteSheet; |
| 241 |
|
| 242 |
procedure ClearAnimations; |
| 243 |
procedure LoadAnimations(const SpriteSheet: TCastleSpriteSheet); |
| 244 |
function AddAnimationToListView(const Animation: TCastleSpriteSheetAnimation): |
| 245 |
TListItem; |
| 246 |
procedure LoadAnimation(const Animation: TCastleSpriteSheetAnimation); |
| 247 |
function GetCurrentAnimation: TCastleSpriteSheetAnimation; |
| 248 |
|
| 249 |
procedure ClearFrames; |
| 250 |
function AddFrameToListView(const Frame: TCastleSpriteSheetFrame; |
| 251 |
const FrameNo: Integer): TListItem; |
| 252 |
procedure LoadFrames(const Animation: TCastleSpriteSheetAnimation); |
| 253 |
function GetFirstSelectedFrame: TCastleSpriteSheetFrame; |
| 254 |
function GetLastSelectedFrame: TCastleSpriteSheetFrame; |
| 255 |
function FrameTitle(const FrameNo: Integer; |
| 256 |
const Frame: TCastleSpriteSheetFrame): String; |
| 257 |
procedure UpdateFrameTitles; |
| 258 |
|
| 259 |
{ Returns current preview mode } |
| 260 |
function GetCurrentPreviewMode: TPreviewMode; |
| 261 |
{ Shows or hides some preview controls. Just to have this switch in |
| 262 |
one place. } |
| 263 |
procedure ShowPreviewControl(const MakeVisible: Boolean); |
| 264 |
{ Creates viewport and scene for preview } |
| 265 |
procedure CreatePreviewUIIfNeeded; |
| 266 |
{ Updates current preview. |
| 267 |
|
| 268 |
If PreviewModesToUpdate is pmAnimation then |
| 269 |
value of FNodesShowSpriteSheetAnimation determines if we need to rebuild |
| 270 |
X3D nodes from FSpriteSheet contents. FNodesShowSpriteSheetAnimation = false |
| 271 |
means we need to rebuild. |
| 272 |
|
| 273 |
In case of PreviewModesToUpdate = pmFrame, right now the X3D nodes |
| 274 |
will be always rebuild. } |
| 275 |
procedure UpdatePreview(const PreviewModesToUpdate: TPreviewMode); |
| 276 |
{ Regenerates and load animation preview } |
| 277 |
procedure RegenerateAnimationPreview; |
| 278 |
{ Regenerates and load frame preview } |
| 279 |
procedure RegenerateFramePreview(const Frame: TCastleSpriteSheetFrame); |
| 280 |
{ Way to lock update preview, useful for multi frames functions - just |
| 281 |
increases FLockUpdatePReviewRC } |
| 282 |
procedure LockUpdatePreview; |
| 283 |
{ Decreases FLockUpdatePReviewRC and calls UpdatePreview() when |
| 284 |
FLockUpdatePReviewRC < 1 } |
| 285 |
procedure UnlockUpdatePreview; |
| 286 |
|
| 287 |
procedure UpdateWindowCaption; |
| 288 |
procedure SetAtlasError(const Message: String); |
| 289 |
procedure SetAtlasWarning(const Message: String); |
| 290 |
|
| 291 |
{ Check atlas size } |
| 292 |
function CheckAtlasMinSize: Boolean; |
| 293 |
procedure LoadAtlasSize; |
| 294 |
|
| 295 |
// events: |
| 296 |
procedure URLChanged(Sender: TObject); |
| 297 |
procedure ModifiedStateChanged(Sender: TObject); |
| 298 |
|
| 299 |
procedure AnimationAdded(NewAnimation: TCastleSpriteSheetAnimation); |
| 300 |
procedure BeforeAnimationRemoved(AnimationToRemove: TCastleSpriteSheetAnimation); |
| 301 |
procedure AnimationMoved(const Animation: TCastleSpriteSheetAnimation; |
| 302 |
const OldIndex, NewIndex: Integer); |
| 303 |
|
| 304 |
procedure FrameAdded(NewFrame: TCastleSpriteSheetFrame); |
| 305 |
procedure BeforeAnimationFrameRemoved(FrameToRemove: TCastleSpriteSheetFrame); |
| 306 |
procedure FrameMoved(const Frame: TCastleSpriteSheetFrame; |
| 307 |
const OldIndex, NewIndex: Integer); |
| 308 |
|
| 309 |
procedure MaxAtlasSizeChanged(const MaxWidth, MaxHeight: Integer); |
| 310 |
|
| 311 |
public |
| 312 |
procedure OpenSpriteSheet(const URL: String; const ProposeSave: Boolean); |
| 313 |
procedure NewSpriteSheet; |
| 314 |
end; |
| 315 |
|
| 316 |
var |
| 317 |
SpriteSheetEditorForm: TSpriteSheetEditorForm; |
| 318 |
|
| 319 |
implementation |
| 320 |
|
| 321 |
{$R *.lfm} |
| 322 |
|
| 323 |
uses GraphType, IntfGraphics, Math, LCLIntf, LCLType, FPImage, |
| 324 |
CastleImages, CastleLog, CastleUtils, CastleUriUtils, CastleFilesUtils, |
| 325 |
X3DNodes, |
| 326 |
EditorUtils, |
| 327 |
FormProject, FormImportAtlas, FormImportStarling |
| 328 |
{$ifdef LCLGTK2},Gtk2Globals, LCLVersion{$endif}; |
| 329 |
|
| 330 |
{ TSpriteSheetEditorForm.TSelectedFrames } |
| 331 |
|
| 332 |
constructor TSpriteSheetEditorForm.TSelectedFrames.Create( |
| 333 |
const FrameListViewToMonitor: TListView); |
| 334 |
begin |
| 335 |
FrameListView := FrameListViewToMonitor; |
| 336 |
FSelectedFrames := TListOfSelectedFrames.Create; |
| 337 |
end; |
| 338 |
|
| 339 |
destructor TSpriteSheetEditorForm.TSelectedFrames.Destroy; |
| 340 |
begin |
| 341 |
FreeAndNil(FSelectedFrames); |
| 342 |
inherited Destroy; |
| 343 |
end; |
| 344 |
|
| 345 |
procedure TSpriteSheetEditorForm.TSelectedFrames.GetCurrentSelection; |
| 346 |
var |
| 347 |
Item: TListItem; |
| 348 |
begin |
| 349 |
Clear; |
| 350 |
Item := FrameListView.Selected; |
| 351 |
|
| 352 |
while Item <> nil do |
| 353 |
begin |
| 354 |
if IsFrameListItem(Item) then |
| 355 |
FSelectedFrames.Add(TCastleSpriteSheetFrame(Item.Data)); |
| 356 |
|
| 357 |
{ About sdAll: |
| 358 |
|
| 359 |
In theory, sdBelow should be OK. |
| 360 |
FrameListView.Selected returns the first selected item, |
| 361 |
and iterating with sdBelow should return the rest. |
| 362 |
However LCL WinAPI seems to have a weird bug, with sdBelow we |
| 363 |
- may not detect all selected |
| 364 |
- may have infinite loop when only one item is selected. |
| 365 |
|
| 366 |
The sdAll behaves correctly. |
| 367 |
} |
| 368 |
Item := FrameListView.GetNextItem(Item, sdAll, [lisSelected]); |
| 369 |
end; |
| 370 |
end; |
| 371 |
|
| 372 |
procedure TSpriteSheetEditorForm.TSelectedFrames.SetSelection; |
| 373 |
var |
| 374 |
Item: TListItem; |
| 375 |
begin |
| 376 |
for Item in FrameListView.Items do |
| 377 |
begin |
| 378 |
if IsFrameListItem(Item) then |
| 379 |
begin |
| 380 |
Item.Selected := FSelectedFrames.Contains(TCastleSpriteSheetFrame(Item.Data)); |
| 381 |
end; |
| 382 |
end; |
| 383 |
end; |
| 384 |
|
| 385 |
procedure TSpriteSheetEditorForm.TSelectedFrames.Clear; |
| 386 |
begin |
| 387 |
FSelectedFrames.Clear; |
| 388 |
end; |
| 389 |
|
| 390 |
function TSpriteSheetEditorForm.TSelectedFrames.GetFrameByIndex( |
| 391 |
const Index: Integer): TCastleSpriteSheetFrame; |
| 392 |
begin |
| 393 |
Result := FSelectedFrames[Index]; |
| 394 |
end; |
| 395 |
|
| 396 |
function TSpriteSheetEditorForm.TSelectedFrames.FrameCount: Integer; |
| 397 |
begin |
| 398 |
Result := FSelectedFrames.Count; |
| 399 |
end; |
| 400 |
|
| 401 |
class function TSpriteSheetEditorForm.TSelectedFrames.IsFrameListItem( |
| 402 |
const ItemToCheck: TListItem): Boolean; |
| 403 |
begin |
| 404 |
Result := ItemToCheck.Data <> nil; |
| 405 |
end; |
| 406 |
|
| 407 |
{ TSpriteSheetEditorForm } |
| 408 |
|
| 409 |
procedure TSpriteSheetEditorForm.ActionNewSpriteSheetExecute(Sender: TObject); |
| 410 |
begin |
| 411 |
NewSpriteSheet; |
| 412 |
end; |
| 413 |
|
| 414 |
procedure TSpriteSheetEditorForm.ActionAddFrameUpdate(Sender: TObject); |
| 415 |
begin |
| 416 |
ActionAddFrame.Enabled := GetCurrentAnimation <> nil; |
| 417 |
end; |
| 418 |
|
| 419 |
procedure TSpriteSheetEditorForm.ActionCloseSpriteSheetEditorExecute( |
| 420 |
Sender: TObject); |
| 421 |
begin |
| 422 |
Close; |
| 423 |
end; |
| 424 |
|
| 425 |
procedure TSpriteSheetEditorForm.ActionCreateNewAnimationFromSelectionExecute( |
| 426 |
Sender: TObject); |
| 427 |
var |
| 428 |
Animation: TCastleSpriteSheetAnimation; |
| 429 |
NewAnimation: TCastleSpriteSheetAnimation; |
| 430 |
I: Integer; |
| 431 |
OldSelectNewAnimation: Boolean; |
| 432 |
SelectedFrames: TSelectedFrames; |
| 433 |
begin |
| 434 |
Animation := GetCurrentAnimation; |
| 435 |
if Animation = nil then |
| 436 |
Exit; |
| 437 |
|
| 438 |
SelectedFrames := TSelectedFrames.Create(ListViewFrames); |
| 439 |
try |
| 440 |
SelectedFrames.GetCurrentSelection; |
| 441 |
|
| 442 |
if SelectedFrames.FrameCount = 0 then |
| 443 |
Exit; |
| 444 |
|
| 445 |
OldSelectNewAnimation := FSelectNewAnimation; |
| 446 |
try |
| 447 |
{ We don't want to jump to new animation in this case } |
| 448 |
FSelectNewAnimation := false; |
| 449 |
NewAnimation := FSpriteSheet.AddAnimation(FSpriteSheet.ProposeAnimationName); |
| 450 |
finally |
| 451 |
FSelectNewAnimation := OldSelectNewAnimation; |
| 452 |
end; |
| 453 |
|
| 454 |
for I := 0 to SelectedFrames.FrameCount -1 do |
| 455 |
begin |
| 456 |
NewAnimation.AddFrameCopy(SelectedFrames.Frame[I]); |
| 457 |
end; |
| 458 |
finally |
| 459 |
FreeAndNil(SelectedFrames); |
| 460 |
end; |
| 461 |
FNodesShowSpriteSheetAnimation := false; |
| 462 |
UpdatePreview(GetCurrentPreviewMode); |
| 463 |
end; |
| 464 |
|
| 465 |
procedure TSpriteSheetEditorForm.ActionCreateNewAnimationFromSelectionUpdate( |
| 466 |
Sender: TObject); |
| 467 |
begin |
| 468 |
ActionCreateNewAnimationFromSelection.Enabled := (GetFirstSelectedFrame <> nil); |
| 469 |
end; |
| 470 |
|
| 471 |
procedure TSpriteSheetEditorForm.ActionImportAtlasExecute(Sender: TObject); |
| 472 |
begin |
| 473 |
if CastleImportAtlasDialog.Execute then |
| 474 |
begin |
| 475 |
ImportAtlasForm.Initialize(FSpriteSheet, CastleImportAtlasDialog.URL); |
| 476 |
ImportAtlasForm.ShowModal; |
| 477 |
end; |
| 478 |
end; |
| 479 |
|
| 480 |
procedure TSpriteSheetEditorForm.ActionMoveAnimationDownExecute(Sender: TObject |
| 481 |
); |
| 482 |
var |
| 483 |
Animation: TCastleSpriteSheetAnimation; |
| 484 |
begin |
| 485 |
Animation := GetCurrentAnimation; |
| 486 |
if Animation = nil then |
| 487 |
Exit; |
| 488 |
|
| 489 |
FSpriteSheet.MoveAnimationDown(Animation); |
| 490 |
end; |
| 491 |
|
| 492 |
procedure TSpriteSheetEditorForm.ActionMoveAnimationDownUpdate(Sender: TObject); |
| 493 |
var |
| 494 |
Animation: TCastleSpriteSheetAnimation; |
| 495 |
begin |
| 496 |
Animation := GetCurrentAnimation; |
| 497 |
ActionMoveAnimationDown.Enabled := (Animation <> nil) |
| 498 |
and (FSpriteSheet.AnimationIndex(Animation) <> FSpriteSheet.AnimationCount - 1); |
| 499 |
end; |
| 500 |
|
| 501 |
procedure TSpriteSheetEditorForm.ActionMoveAnimationEndExecute(Sender: TObject); |
| 502 |
var |
| 503 |
Animation: TCastleSpriteSheetAnimation; |
| 504 |
begin |
| 505 |
Animation := GetCurrentAnimation; |
| 506 |
if Animation = nil then |
| 507 |
Exit; |
| 508 |
|
| 509 |
FSpriteSheet.MoveAnimationToEnd(Animation); |
| 510 |
end; |
| 511 |
|
| 512 |
procedure TSpriteSheetEditorForm.ActionMoveAnimationEndUpdate(Sender: TObject); |
| 513 |
var |
| 514 |
Animation: TCastleSpriteSheetAnimation; |
| 515 |
begin |
| 516 |
Animation := GetCurrentAnimation; |
| 517 |
ActionMoveAnimationEnd.Enabled := (Animation <> nil) |
| 518 |
and (FSpriteSheet.AnimationIndex(Animation) <> FSpriteSheet.AnimationCount - 1); |
| 519 |
end; |
| 520 |
|
| 521 |
procedure TSpriteSheetEditorForm.ActionMoveAnimationTopExecute(Sender: TObject); |
| 522 |
var |
| 523 |
Animation: TCastleSpriteSheetAnimation; |
| 524 |
begin |
| 525 |
Animation := GetCurrentAnimation; |
| 526 |
if Animation = nil then |
| 527 |
Exit; |
| 528 |
|
| 529 |
FSpriteSheet.MoveAnimationToTop(Animation); |
| 530 |
end; |
| 531 |
|
| 532 |
procedure TSpriteSheetEditorForm.ActionMoveAnimationTopUpdate(Sender: TObject); |
| 533 |
var |
| 534 |
Animation: TCastleSpriteSheetAnimation; |
| 535 |
begin |
| 536 |
Animation := GetCurrentAnimation; |
| 537 |
ActionMoveAnimationTop.Enabled := (Animation <> nil) |
| 538 |
and (FSpriteSheet.AnimationIndex(Animation) <> 0); |
| 539 |
end; |
| 540 |
|
| 541 |
procedure TSpriteSheetEditorForm.ActionMoveAnimationUpExecute(Sender: TObject); |
| 542 |
var |
| 543 |
Animation: TCastleSpriteSheetAnimation; |
| 544 |
begin |
| 545 |
Animation := GetCurrentAnimation; |
| 546 |
if Animation = nil then |
| 547 |
Exit; |
| 548 |
|
| 549 |
FSpriteSheet.MoveAnimationUp(Animation); |
| 550 |
end; |
| 551 |
|
| 552 |
procedure TSpriteSheetEditorForm.ActionMoveAnimationUpUpdate(Sender: TObject); |
| 553 |
var |
| 554 |
Animation: TCastleSpriteSheetAnimation; |
| 555 |
begin |
| 556 |
Animation := GetCurrentAnimation; |
| 557 |
ActionMoveAnimationUp.Enabled := (Animation <> nil) |
| 558 |
and (FSpriteSheet.AnimationIndex(Animation) <> 0); |
| 559 |
end; |
| 560 |
|
| 561 |
procedure TSpriteSheetEditorForm.ActionMoveFrameRightExecute(Sender: TObject); |
| 562 |
var |
| 563 |
Animation: TCastleSpriteSheetAnimation; |
| 564 |
I: Integer; |
| 565 |
SelectedFrames: TSelectedFrames; |
| 566 |
begin |
| 567 |
Animation := GetCurrentAnimation; |
| 568 |
if Animation = nil then |
| 569 |
Exit; |
| 570 |
|
| 571 |
SelectedFrames := TSelectedFrames.Create(ListViewFrames); |
| 572 |
try |
| 573 |
SelectedFrames.GetCurrentSelection; |
| 574 |
LockUpdatePreview; |
| 575 |
try |
| 576 |
for I := SelectedFrames.FrameCount - 1 downto 0 do |
| 577 |
Animation.MoveFrameRight(SelectedFrames.Frame[I]); |
| 578 |
finally |
| 579 |
UnlockUpdatePreview; |
| 580 |
end; |
| 581 |
finally |
| 582 |
FreeAndNil(SelectedFrames); |
| 583 |
end; |
| 584 |
end; |
| 585 |
|
| 586 |
procedure TSpriteSheetEditorForm.ActionMoveFrameRightUpdate(Sender: TObject); |
| 587 |
var |
| 588 |
Frame: TCastleSpriteSheetFrame; |
| 589 |
begin |
| 590 |
Frame := GetLastSelectedFrame; |
| 591 |
ActionMoveFrameRight.Enabled := (Frame <> nil) |
| 592 |
and (Frame.Animation.FrameIndex(Frame) < Frame.Animation.FrameCount - 1); |
| 593 |
end; |
| 594 |
|
| 595 |
procedure TSpriteSheetEditorForm.ActionMoveFrameEndExecute(Sender: TObject); |
| 596 |
var |
| 597 |
Frame: TCastleSpriteSheetFrame; |
| 598 |
Animation: TCastleSpriteSheetAnimation; |
| 599 |
begin |
| 600 |
Animation := GetCurrentAnimation; |
| 601 |
Frame := GetFirstSelectedFrame; |
| 602 |
|
| 603 |
if (Animation = nil) or (Frame = nil) then |
| 604 |
Exit; |
| 605 |
|
| 606 |
Animation.MoveFrameToEnd(Frame); |
| 607 |
end; |
| 608 |
|
| 609 |
procedure TSpriteSheetEditorForm.ActionMoveFrameEndUpdate(Sender: TObject); |
| 610 |
var |
| 611 |
Frame: TCastleSpriteSheetFrame; |
| 612 |
begin |
| 613 |
Frame := GetFirstSelectedFrame; |
| 614 |
ActionMoveFrameEnd.Enabled := (Frame <> nil) and (ListViewFrames.SelCount = 1) |
| 615 |
and (Frame.Animation.FrameIndex(Frame) < Frame.Animation.FrameCount - 1); |
| 616 |
end; |
| 617 |
|
| 618 |
procedure TSpriteSheetEditorForm.ActionMoveFrameTopExecute(Sender: TObject); |
| 619 |
var |
| 620 |
Frame: TCastleSpriteSheetFrame; |
| 621 |
Animation: TCastleSpriteSheetAnimation; |
| 622 |
begin |
| 623 |
Animation := GetCurrentAnimation; |
| 624 |
Frame := GetFirstSelectedFrame; |
| 625 |
|
| 626 |
if (Animation = nil) or (Frame = nil) then |
| 627 |
Exit; |
| 628 |
|
| 629 |
Animation.MoveFrameToTop(Frame); |
| 630 |
end; |
| 631 |
|
| 632 |
procedure TSpriteSheetEditorForm.ActionMoveFrameTopUpdate(Sender: TObject); |
| 633 |
var |
| 634 |
Frame: TCastleSpriteSheetFrame; |
| 635 |
begin |
| 636 |
Frame := GetFirstSelectedFrame; |
| 637 |
ActionMoveFrameTop.Enabled := (Frame <> nil) and (ListViewFrames.SelCount = 1) |
| 638 |
and (Frame.Animation.FrameIndex(Frame) > 0); |
| 639 |
end; |
| 640 |
|
| 641 |
procedure TSpriteSheetEditorForm.ActionMoveFrameLeftExecute(Sender: TObject); |
| 642 |
var |
| 643 |
Animation: TCastleSpriteSheetAnimation; |
| 644 |
I: Integer; |
| 645 |
SelectedFrames: TSelectedFrames; |
| 646 |
begin |
| 647 |
Animation := GetCurrentAnimation; |
| 648 |
if Animation = nil then |
| 649 |
Exit; |
| 650 |
|
| 651 |
SelectedFrames := TSelectedFrames.Create(ListViewFrames); |
| 652 |
try |
| 653 |
SelectedFrames.GetCurrentSelection; |
| 654 |
LockUpdatePreview; |
| 655 |
try |
| 656 |
for I := 0 to SelectedFrames.FrameCount - 1 do |
| 657 |
Animation.MoveFrameLeft(SelectedFrames.Frame[I]); |
| 658 |
finally |
| 659 |
UnlockUpdatePreview; |
| 660 |
end; |
| 661 |
finally |
| 662 |
FreeAndNil(SelectedFrames); |
| 663 |
end; |
| 664 |
end; |
| 665 |
|
| 666 |
procedure TSpriteSheetEditorForm.ActionMoveFrameLeftUpdate(Sender: TObject); |
| 667 |
var |
| 668 |
Frame: TCastleSpriteSheetFrame; |
| 669 |
begin |
| 670 |
Frame := GetFirstSelectedFrame; |
| 671 |
ActionMoveFrameLeft.Enabled := (Frame <> nil) |
| 672 |
and (Frame.Animation.FrameIndex(Frame) > 0); |
| 673 |
end; |
| 674 |
|
| 675 |
procedure TSpriteSheetEditorForm.ActionAddFrameExecute(Sender: TObject); |
| 676 |
var |
| 677 |
Animation: TCastleSpriteSheetAnimation; |
| 678 |
I: Integer; |
| 679 |
begin |
| 680 |
Animation := GetCurrentAnimation; |
| 681 |
if Animation = nil then |
| 682 |
Exit; |
| 683 |
|
| 684 |
if CastleOpenImageDialog.Execute then |
| 685 |
begin |
| 686 |
LockUpdatePreview; |
| 687 |
try |
| 688 |
for I := 0 to CastleOpenImageDialog.URLCount - 1 do |
| 689 |
Animation.AddFrame(CastleOpenImageDialog.URLs[I]); |
| 690 |
finally |
| 691 |
UnlockUpdatePreview; |
| 692 |
end; |
| 693 |
end; |
| 694 |
end; |
| 695 |
|
| 696 |
procedure TSpriteSheetEditorForm.ActionAddAnimationUpdate(Sender: TObject); |
| 697 |
begin |
| 698 |
ActionAddAnimation.Enabled := FSpriteSheet <> nil; |
| 699 |
end; |
| 700 |
|
| 701 |
procedure TSpriteSheetEditorForm.ActionAddAnimationExecute(Sender: TObject); |
| 702 |
var |
| 703 |
OldSelectNewAnimation: Boolean; |
| 704 |
begin |
| 705 |
OldSelectNewAnimation := FSelectNewAnimation; |
| 706 |
try |
| 707 |
FSelectNewAnimation := true; |
| 708 |
FSpriteSheet.AddAnimation(FSpriteSheet.ProposeAnimationName); |
| 709 |
finally |
| 710 |
FSelectNewAnimation := OldSelectNewAnimation; |
| 711 |
end; |
| 712 |
FNodesShowSpriteSheetAnimation := false; |
| 713 |
UpdatePreview(GetCurrentPreviewMode); |
| 714 |
end; |
| 715 |
|
| 716 |
procedure TSpriteSheetEditorForm.ActionOpenSpriteSheetExecute(Sender: TObject); |
| 717 |
var |
| 718 |
URLAnchor: String; |
| 719 |
URLToOpen, MimeType: String; |
| 720 |
begin |
| 721 |
if not ProposeSaveSpriteSheet then |
| 722 |
Exit; |
| 723 |
if OpenDialog.Execute then |
| 724 |
begin |
| 725 |
URLToOpen := OpenDialog.URL; |
| 726 |
|
| 727 |
{ Check: if file is Starling } |
| 728 |
MimeType := UriMimeType(URLToOpen); |
| 729 |
if (MimeType = 'application/x-starling-sprite-sheet') or |
| 730 |
(MimeType = 'application/xml') then |
| 731 |
begin |
| 732 |
{ If file has anchors don't show import dialog } |
| 733 |
UriGetAnchor(URLToOpen, URLAnchor); |
| 734 |
if URLAnchor = '' then |
| 735 |
begin |
| 736 |
ImportStarlingForm.Initialize(URLToOpen); |
| 737 |
case ImportStarlingForm.ShowModal of |
| 738 |
mrCancel: |
| 739 |
Exit; |
| 740 |
mrOK: |
| 741 |
URLToOpen := ImportStarlingForm.URL; |
| 742 |
end; |
| 743 |
end; |
| 744 |
|
| 745 |
end; |
| 746 |
OpenSpriteSheet(URLToOpen, false); |
| 747 |
end; |
| 748 |
end; |
| 749 |
|
| 750 |
procedure TSpriteSheetEditorForm.ActionDeleteAnimationExecute(Sender: TObject); |
| 751 |
var |
| 752 |
Animation: TCastleSpriteSheetAnimation; |
| 753 |
begin |
| 754 |
Animation := GetCurrentAnimation; |
| 755 |
if Animation = nil then |
| 756 |
Exit; |
| 757 |
|
| 758 |
FSpriteSheet.RemoveAnimation(Animation); |
| 759 |
FNodesShowSpriteSheetAnimation := false; |
| 760 |
UpdatePreview(GetCurrentPreviewMode); |
| 761 |
end; |
| 762 |
|
| 763 |
procedure TSpriteSheetEditorForm.ActionDeleteAnimationUpdate(Sender: TObject); |
| 764 |
begin |
| 765 |
ActionDeleteAnimation.Enabled := (GetCurrentAnimation <> nil); |
| 766 |
end; |
| 767 |
|
| 768 |
procedure TSpriteSheetEditorForm.ActionDeleteFrameExecute(Sender: TObject); |
| 769 |
var |
| 770 |
Animation: TCastleSpriteSheetAnimation; |
| 771 |
I: Integer; |
| 772 |
SelectedFrames: TSelectedFrames; |
| 773 |
begin |
| 774 |
Animation := GetCurrentAnimation; |
| 775 |
if Animation = nil then |
| 776 |
Exit; |
| 777 |
|
| 778 |
SelectedFrames := TSelectedFrames.Create(ListViewFrames); |
| 779 |
try |
| 780 |
SelectedFrames.GetCurrentSelection; |
| 781 |
LockUpdatePreview; |
| 782 |
try |
| 783 |
for I := 0 to SelectedFrames.FrameCount - 1 do |
| 784 |
Animation.RemoveFrame(SelectedFrames.Frame[I]); |
| 785 |
finally |
| 786 |
UnlockUpdatePreview; |
| 787 |
end; |
| 788 |
finally |
| 789 |
FreeAndNil(SelectedFrames); |
| 790 |
end; |
| 791 |
{ UpdatePreview is not needed here because UnlockUpdatePreview calls that } |
| 792 |
end; |
| 793 |
|
| 794 |
procedure TSpriteSheetEditorForm.ActionDeleteFrameUpdate(Sender: TObject); |
| 795 |
begin |
| 796 |
ActionDeleteFrame.Enabled := (GetFirstSelectedFrame <> nil); |
| 797 |
end; |
| 798 |
|
| 799 |
procedure TSpriteSheetEditorForm.ActionRenameAnimationExecute(Sender: TObject); |
| 800 |
begin |
| 801 |
ListViewAnimations.Selected.EditCaption; |
| 802 |
end; |
| 803 |
|
| 804 |
procedure TSpriteSheetEditorForm.ActionRenameAnimationUpdate(Sender: TObject); |
| 805 |
begin |
| 806 |
ActionRenameAnimation.Enabled := (GetCurrentAnimation <> nil); |
| 807 |
end; |
| 808 |
|
| 809 |
procedure TSpriteSheetEditorForm.ActionSaveSpriteSheetAsExecute(Sender: TObject |
| 810 |
); |
| 811 |
begin |
| 812 |
SaveDialog.DefaultExt := 'castle-sprite-sheet'; |
| 813 |
{ In case FSpriteSheet.URL = '' (not yet saved), the save dialog shows correct UI. } |
| 814 |
SaveDialog.URL := FSpriteSheet.URL; |
| 815 |
if SaveDialog.Execute then |
| 816 |
begin |
| 817 |
try |
| 818 |
FSpriteSheet.Save(SaveDialog.URL, false); |
| 819 |
except |
| 820 |
on E: Exception do |
| 821 |
ErrorBox(E.Message); |
| 822 |
end; |
| 823 |
end; |
| 824 |
end; |
| 825 |
|
| 826 |
procedure TSpriteSheetEditorForm.ActionSaveSpriteSheetExecute(Sender: TObject); |
| 827 |
begin |
| 828 |
if (FSpriteSheet.URL = '') or |
| 829 |
(UriMimeType(FSpriteSheet.URL) <> 'application/x-castle-sprite-sheet') then |
| 830 |
ActionSaveSpriteSheetAsExecute(Sender) |
| 831 |
else |
| 832 |
FSpriteSheet.Save(FSpriteSheet.URL, false); |
| 833 |
end; |
| 834 |
|
| 835 |
procedure TSpriteSheetEditorForm.FloatSpinEditFPSChange(Sender: TObject); |
| 836 |
var |
| 837 |
Animation: TCastleSpriteSheetAnimation; |
| 838 |
begin |
| 839 |
Animation := GetCurrentAnimation; |
| 840 |
if Animation = nil then |
| 841 |
Exit; // ignore if no animation (TODO: SpinEditFPS should be disabled then) |
| 842 |
if not SameValue(Animation.FramesPerSecond, FloatSpinEditFPS.Value) then |
| 843 |
begin |
| 844 |
Animation.FramesPerSecond := FloatSpinEditFPS.Value; |
| 845 |
{ To change frames per second file must be regenerated. } |
| 846 |
FNodesShowSpriteSheetAnimation := false; |
| 847 |
UpdatePreview(GetCurrentPreviewMode); |
| 848 |
end; |
| 849 |
end; |
| 850 |
|
| 851 |
procedure TSpriteSheetEditorForm.FormCloseQuery(Sender: TObject; |
| 852 |
var CanClose: boolean); |
| 853 |
begin |
| 854 |
CanClose := ProposeSaveSpriteSheet; |
| 855 |
|
| 856 |
if CanClose then |
| 857 |
begin |
| 858 |
CloseSpriteSheet; |
| 859 |
NewSpriteSheet; |
| 860 |
end; |
| 861 |
end; |
| 862 |
|
| 863 |
procedure TSpriteSheetEditorForm.FormCreate(Sender: TObject); |
| 864 |
|
| 865 |
{$ifdef LCLCocoa} |
| 866 |
procedure FixCocoa; |
| 867 |
var |
| 868 |
FirstCol: TListColumn; |
| 869 |
begin |
| 870 |
// on Cocoa, image list crashes TListView usage |
| 871 |
ListViewFrames.LargeImages := nil; |
| 872 |
|
| 873 |
// on Cocoa, list always behaves like ViewStyle = vsReport and shows nothing when no columns |
| 874 |
FirstCol := ListViewFrames.Columns.Add; |
| 875 |
FirstCol.Caption := 'Frame name'; |
| 876 |
FirstCol.Width := 200; |
| 877 |
end; |
| 878 |
{$endif} |
| 879 |
|
| 880 |
|
| 881 |
begin |
| 882 |
{$ifdef LCLCocoa} |
| 883 |
FixCocoa; |
| 884 |
{$endif} |
| 885 |
FSelectNewAnimation := true; |
| 886 |
FSpriteSheet := nil; |
| 887 |
FWindowTitle := Caption; |
| 888 |
SetAtlasError(''); |
| 889 |
SetAtlasWarning(''); |
| 890 |
NewSpriteSheet; |
| 891 |
end; |
| 892 |
|
| 893 |
procedure TSpriteSheetEditorForm.FormDestroy(Sender: TObject); |
| 894 |
begin |
| 895 |
FreeAndNil(FSpriteSheet); |
| 896 |
end; |
| 897 |
|
| 898 |
procedure TSpriteSheetEditorForm.FormShow(Sender: TObject); |
| 899 |
begin |
| 900 |
{$ifdef LCLGTK2} |
| 901 |
{$if (LCL_FULLVERSION >= 1080000) and (LCL_FULLVERSION < 2001200)} |
| 902 |
{ On GTK2 SpeedButtons are frozen after window show from popup menu |
| 903 |
See: https://bugs.freepascal.org/view.php?id=38345 } |
| 904 |
LastMouse.Button := 0; |
| 905 |
LastMouse.ClickCount := 0; |
| 906 |
LastMouse.Down := False; |
| 907 |
LastMouse.MousePos := Point(0, 0); |
| 908 |
LastMouse.Time := 0; |
| 909 |
LastMouse.WinControl := nil; |
| 910 |
{$endif} |
| 911 |
{ Update actions state after FormShow - I think this is next GTK2 bug. } |
| 912 |
UpdateActions; |
| 913 |
{$endif} |
| 914 |
|
| 915 |
{ Adjust InitialDir values to make open/save dialogs natural, and clear URL. |
| 916 |
Do this in FormShow, as one instance of TSpriteSheetEditorForm may exist across |
| 917 |
many CGE projects being open. } |
| 918 |
OpenDialog.InitialDir := UriToFilenameSafe('castle-data:/'); |
| 919 |
SaveDialog.InitialDir := UriToFilenameSafe('castle-data:/'); |
| 920 |
CastleOpenImageDialog.InitialDir := UriToFilenameSafe('castle-data:/'); |
| 921 |
CastleImportAtlasDialog.InitialDir := UriToFilenameSafe('castle-data:/'); |
| 922 |
OpenDialog.URL := ''; |
| 923 |
SaveDialog.URL := ''; |
| 924 |
CastleOpenImageDialog.URL := ''; |
| 925 |
CastleImportAtlasDialog.URL := ''; |
| 926 |
end; |
| 927 |
|
| 928 |
procedure TSpriteSheetEditorForm.ListViewAnimationsDragDrop(Sender, |
| 929 |
Source: TObject; X, Y: Integer); |
| 930 |
var |
| 931 |
SelectedFrames: TSelectedFrames; |
| 932 |
AnimationItem: TListItem; |
| 933 |
DestAnimation: TCastleSpriteSheetAnimation; |
| 934 |
SrcAnimation: TCastleSpriteSheetAnimation; |
| 935 |
I: Integer; |
| 936 |
CtrlPressed: Boolean; |
| 937 |
begin |
| 938 |
if Source = ListViewFrames then |
| 939 |
begin |
| 940 |
SelectedFrames := TSelectedFrames.Create(ListViewFrames); |
| 941 |
try |
| 942 |
SelectedFrames.GetCurrentSelection; |
| 943 |
|
| 944 |
AnimationItem := ListViewAnimations.GetItemAt(X,Y); |
| 945 |
if AnimationItem = nil then |
| 946 |
Exit; |
| 947 |
|
| 948 |
DestAnimation := TCastleSpriteSheetAnimation(AnimationItem.Data); |
| 949 |
|
| 950 |
LockUpdatePreview; |
| 951 |
try |
| 952 |
|
| 953 |
{ Copy frame } |
| 954 |
for I := 0 to SelectedFrames.FrameCount - 1 do |
| 955 |
DestAnimation.AddFrameCopy(SelectedFrames.Frame[I]); |
| 956 |
|
| 957 |
{ Delete frames from current animation if ctrl not pressed |
| 958 |
https://forum.lazarus.freepascal.org/index.php?topic=39663.0 } |
| 959 |
{$ifdef darwin} |
| 960 |
CtrlPressed := (GetKeyState(VK_LWIN) < 0) or (GetKeyState(VK_RWIN) < 0); |
| 961 |
{$else} |
| 962 |
CtrlPressed := GetKeyState(VK_CONTROL) < 0; |
| 963 |
{$endif} |
| 964 |
SrcAnimation := GetCurrentAnimation; |
| 965 |
if (SrcAnimation <> nil) and (not CtrlPressed) then |
| 966 |
begin |
| 967 |
for I := 0 to SelectedFrames.FrameCount - 1 do |
| 968 |
SrcAnimation.RemoveFrame(SelectedFrames.Frame[I]); |
| 969 |
end; |
| 970 |
|
| 971 |
finally |
| 972 |
UnlockUpdatePreview; |
| 973 |
end; |
| 974 |
finally |
| 975 |
FreeAndNil(SelectedFrames); |
| 976 |
end; |
| 977 |
end; |
| 978 |
end; |
| 979 |
|
| 980 |
procedure TSpriteSheetEditorForm.ListViewAnimationsDragOver(Sender, |
| 981 |
Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); |
| 982 |
begin |
| 983 |
Accept := (Source = ListViewFrames) |
| 984 |
and (ListViewAnimations.GetItemAt(X,Y) <> nil); |
| 985 |
end; |
| 986 |
|
| 987 |
procedure TSpriteSheetEditorForm.ListViewAnimationsEdited(Sender: TObject; |
| 988 |
Item: TListItem; var AValue: String); |
| 989 |
var |
| 990 |
Animation: TCastleSpriteSheetAnimation; |
| 991 |
begin |
| 992 |
Animation := TCastleSpriteSheetAnimation(Item.Data); |
| 993 |
|
| 994 |
if Animation.Name = AValue then |
| 995 |
Exit; |
| 996 |
|
| 997 |
if FSpriteSheet.HasAnimation(AValue) then |
| 998 |
begin |
| 999 |
ErrorBox('Animation "' + AValue + '" already exist.'); |
| 1000 |
AValue := Animation.Name; |
| 1001 |
Exit; |
| 1002 |
end; |
| 1003 |
|
| 1004 |
Animation.Name := AValue; |
| 1005 |
FNodesShowSpriteSheetAnimation := false; |
| 1006 |
UpdatePreview(GetCurrentPreviewMode); |
| 1007 |
end; |
| 1008 |
|
| 1009 |
procedure TSpriteSheetEditorForm.ListViewAnimationsSelectItem(Sender: TObject; |
| 1010 |
Item: TListItem; Selected: Boolean); |
| 1011 |
var |
| 1012 |
Animation: TCastleSpriteSheetAnimation; |
| 1013 |
begin |
| 1014 |
if not Selected then |
| 1015 |
Exit; |
| 1016 |
|
| 1017 |
Animation := GetCurrentAnimation; |
| 1018 |
|
| 1019 |
if Animation <> nil then |
| 1020 |
LoadAnimation(Animation); |
| 1021 |
end; |
| 1022 |
|
| 1023 |
procedure TSpriteSheetEditorForm.ListViewFramesSelectItem(Sender: TObject; |
| 1024 |
Item: TListItem; Selected: Boolean); |
| 1025 |
begin |
| 1026 |
{ In case of changing frames, animation preview should be ok |
| 1027 |
(frame preview is always regenerated). } |
| 1028 |
if GetCurrentPreviewMode = pmFrame then |
| 1029 |
UpdatePreview(GetCurrentPreviewMode); |
| 1030 |
end; |
| 1031 |
|
| 1032 |
procedure TSpriteSheetEditorForm.PopupMenuCloseGTK2Fix(Sender: TObject); |
| 1033 |
begin |
| 1034 |
{$if defined(LCLGTK2) and (LCL_FULLVERSION >= 1080000) and (LCL_FULLVERSION < 2001200)} |
| 1035 |
{ On GTK2 SpeedButtons are frozen after close popup menu |
| 1036 |
See: https://bugs.freepascal.org/view.php?id=38401 } |
| 1037 |
LastMouse.Button := 0; |
| 1038 |
LastMouse.ClickCount := 0; |
| 1039 |
LastMouse.Down := False; |
| 1040 |
LastMouse.MousePos := Point(0, 0); |
| 1041 |
LastMouse.Time := 0; |
| 1042 |
LastMouse.WinControl := nil; |
| 1043 |
{$endif} |
| 1044 |
end; |
| 1045 |
|
| 1046 |
procedure TSpriteSheetEditorForm.RadioFrameChange(Sender: TObject); |
| 1047 |
begin |
| 1048 |
{ In case of changing from frame preview to animation preview, |
| 1049 |
animation preview can be outdated so we force file regeneration } |
| 1050 |
FNodesShowSpriteSheetAnimation := false; |
| 1051 |
UpdatePreview(GetCurrentPreviewMode); |
| 1052 |
end; |
| 1053 |
|
| 1054 |
procedure TSpriteSheetEditorForm.SpinEditMaxAtlasSizeChange(Sender: TObject); |
| 1055 |
begin |
| 1056 |
end; |
| 1057 |
|
| 1058 |
procedure TSpriteSheetEditorForm.SpinEditMaxAtlasSizeEditingDone(Sender: TObject |
| 1059 |
); |
| 1060 |
begin |
| 1061 |
FSpriteSheet.SetMaxAtlasSize(SpinEditMaxAtlasSize.Value, |
| 1062 |
SpinEditMaxAtlasSize.Value); |
| 1063 |
end; |
| 1064 |
|
| 1065 |
function TSpriteSheetEditorForm.ProposeSaveSpriteSheet: Boolean; |
| 1066 |
var |
| 1067 |
Mr: TModalResult; |
| 1068 |
SpriteSheetName: String; |
| 1069 |
begin |
| 1070 |
Result := true; |
| 1071 |
|
| 1072 |
if FSpriteSheet = nil then |
| 1073 |
Exit; |
| 1074 |
|
| 1075 |
if FSpriteSheet.IsModified then |
| 1076 |
begin |
| 1077 |
if FSpriteSheet.URL <> '' then |
| 1078 |
SpriteSheetName := ' "' + FSpriteSheet.URL + '"' |
| 1079 |
else |
| 1080 |
SpriteSheetName := ''; |
| 1081 |
Mr := MessageDlg('Save sprite sheet', |
| 1082 |
'Sprite sheet' + SpriteSheetName + |
| 1083 |
' was modified but not saved yet. Save it now?', |
| 1084 |
mtConfirmation, mbYesNoCancel, 0); |
| 1085 |
case Mr of |
| 1086 |
mrYes: ActionSaveSpriteSheetExecute(Self); |
| 1087 |
mrCancel: Result := false; |
| 1088 |
end; |
| 1089 |
end; |
| 1090 |
end; |
| 1091 |
|
| 1092 |
function TSpriteSheetEditorForm.CloseSpriteSheet: Boolean; |
| 1093 |
begin |
| 1094 |
ClearFrames; |
| 1095 |
ClearAnimations; |
| 1096 |
FreeAndNil(FSpriteSheet); |
| 1097 |
Result := True; |
| 1098 |
end; |
| 1099 |
|
| 1100 |
procedure TSpriteSheetEditorForm.AssignEventsToSpriteSheet; |
| 1101 |
begin |
| 1102 |
FSpriteSheet.OnModifiedStateChanged := @ModifiedStateChanged; |
| 1103 |
FSpriteSheet.OnAnimationAdded := @AnimationAdded; |
| 1104 |
FSpriteSheet.OnAnimationMoved := @AnimationMoved; |
| 1105 |
FSpriteSheet.BeforeAnimationRemoved := @BeforeAnimationRemoved; |
| 1106 |
FSpriteSheet.OnFrameAdded := @FrameAdded; |
| 1107 |
FSpriteSheet.OnFrameMoved := @FrameMoved; |
| 1108 |
FSpriteSheet.BeforeFrameRemoved := @BeforeAnimationFrameRemoved; |
| 1109 |
FSpriteSheet.OnMaxAtlasSizeChanged := @MaxAtlasSizeChanged; |
| 1110 |
FSpriteSheet.OnURLChanged := @URLChanged; |
| 1111 |
end; |
| 1112 |
|
| 1113 |
procedure TSpriteSheetEditorForm.LoadAnimations(const SpriteSheet: TCastleSpriteSheet); |
| 1114 |
var |
| 1115 |
I: Integer; |
| 1116 |
begin |
| 1117 |
for I := 0 to SpriteSheet.AnimationCount - 1 do |
| 1118 |
AddAnimationToListView(SpriteSheet.AnimationByIndex(I)); |
| 1119 |
|
| 1120 |
if SpriteSheet.AnimationCount > 0 then |
| 1121 |
ListViewAnimations.ItemIndex := 0; |
| 1122 |
|
| 1123 |
{ We need update actions manually here, because some controls is not updated |
| 1124 |
automatically } |
| 1125 |
UpdateActions; |
| 1126 |
end; |
| 1127 |
|
| 1128 |
function TSpriteSheetEditorForm.AddAnimationToListView( |
| 1129 |
const Animation: TCastleSpriteSheetAnimation): TListItem; |
| 1130 |
var |
| 1131 |
ListItem: TListItem; |
| 1132 |
begin |
| 1133 |
ListItem := ListViewAnimations.Items.Add; |
| 1134 |
ListItem.Caption := Animation.Name; |
| 1135 |
ListItem.Data := Animation; |
| 1136 |
Result := ListItem; |
| 1137 |
end; |
| 1138 |
|
| 1139 |
procedure TSpriteSheetEditorForm.LoadAnimation(const Animation: TCastleSpriteSheetAnimation); |
| 1140 |
begin |
| 1141 |
FloatSpinEditFPS.Value := Animation.FramesPerSecond; |
| 1142 |
ClearFrames; |
| 1143 |
LoadFrames(Animation); |
| 1144 |
UpdatePreview(GetCurrentPreviewMode); |
| 1145 |
end; |
| 1146 |
|
| 1147 |
procedure TSpriteSheetEditorForm.ClearFrames; |
| 1148 |
begin |
| 1149 |
ListViewFrames.Items.Clear; |
| 1150 |
end; |
| 1151 |
|
| 1152 |
function TSpriteSheetEditorForm.AddFrameToListView( |
| 1153 |
const Frame: TCastleSpriteSheetFrame; const FrameNo: Integer): TListItem; |
| 1154 |
var |
| 1155 |
ListItem: TListItem; |
| 1156 |
ResizedFrameImage: TCastleImage; |
| 1157 |
Bitmap: TBitmap; |
| 1158 |
IntfImage: TLazIntfImage; |
| 1159 |
ImageIndex: Integer; |
| 1160 |
|
| 1161 |
function FrameImageToLazImage(const FrameImage: TCastleImage; |
| 1162 |
const Width, Height: Integer): TLazIntfImage; |
| 1163 |
var |
| 1164 |
RawImage: TRawImage; |
| 1165 |
CustomFPImage: TFPCustomImage; |
| 1166 |
begin |
| 1167 |
Result := TLazIntfImage.Create(0, 0); |
| 1168 |
RawImage.Init; |
| 1169 |
{ TListView on WIN32 widgetset need RGB image } |
| 1170 |
{$ifdef LCLWIN32} |
| 1171 |
RawImage.Description.Init_BPP24_R8G8B8_BIO_TTB(Width, Height); |
| 1172 |
{$else} |
| 1173 |
RawImage.Description.Init_BPP32_R8G8B8A8_BIO_TTB(Width, Height); |
| 1174 |
{$endif} |
| 1175 |
RawImage.CreateData(True); |
| 1176 |
Result.SetRawImage(RawImage); |
| 1177 |
CustomFPImage := FrameImage.ToFpImage; |
| 1178 |
try |
| 1179 |
Result.CopyPixels(CustomFPImage); |
| 1180 |
finally |
| 1181 |
FreeAndNil(CustomFPImage); |
| 1182 |
end; |
| 1183 |
end; |
| 1184 |
|
| 1185 |
begin |
| 1186 |
ResizedFrameImage := nil; |
| 1187 |
IntfImage := nil; |
| 1188 |
Bitmap := nil; |
| 1189 |
try |
| 1190 |
{ When frame is smaller than CurrentFrameIconSize do not resize frame } |
| 1191 |
if (Frame.FrameWidth = CurrentFrameIconSize.X) and |
| 1192 |
(Frame.FrameHeight = CurrentFrameIconSize.Y) then |
| 1193 |
begin |
| 1194 |
{ On win32 widgetset image with transparency looks ugly, so we |
| 1195 |
blend frame on white background } |
| 1196 |
{$ifdef LCLWIN32} |
| 1197 |
ResizedFrameImage := Frame.MakeImageCopyWithBg(Vector4(1, 1, 1, 1)); |
| 1198 |
{$else} |
| 1199 |
ResizedFrameImage := Frame.MakeImageCopy; |
| 1200 |
{$endif} |
| 1201 |
end else |
| 1202 |
if (Frame.FrameWidth < CurrentFrameIconSize.X) and |
| 1203 |
(Frame.FrameHeight < CurrentFrameIconSize.Y) then |
| 1204 |
begin |
| 1205 |
{ On win32 widgetset image with transparency looks ugly, so we |
| 1206 |
blend frame on white background } |
| 1207 |
{$ifdef LCLWIN32} |
| 1208 |
ResizedFrameImage := Frame.CenterOnBiggerImage(CurrentFrameIconSize.X, |
| 1209 |
CurrentFrameIconSize.Y, Vector4(1, 1, 1, 1)); |
| 1210 |
{$else} |
| 1211 |
ResizedFrameImage := Frame.CenterOnBiggerImage(CurrentFrameIconSize.X, |
| 1212 |
CurrentFrameIconSize.Y, Vector4(0, 0, 0, 0)); |
| 1213 |
{$endif} |
| 1214 |
end else |
| 1215 |
begin |
| 1216 |
{ On win32 widgetset image with transparency looks ugly, so we |
| 1217 |
blend frame on white background } |
| 1218 |
{$ifdef LCLWIN32} |
| 1219 |
ResizedFrameImage := Frame.MakeResizedWithBg(CurrentFrameIconSize.X, |
| 1220 |
CurrentFrameIconSize.Y, Vector4(1, 1, 1, 1)); |
| 1221 |
{$else} |
| 1222 |
ResizedFrameImage := Frame.MakeResized(CurrentFrameIconSize.X, |
| 1223 |
CurrentFrameIconSize.Y); |
| 1224 |
{$endif} |
| 1225 |
end; |
| 1226 |
|
| 1227 |
IntfImage := FrameImageToLazImage(ResizedFrameImage, CurrentFrameIconSize.X, |
| 1228 |
CurrentFrameIconSize.Y); |
| 1229 |
|
| 1230 |
Bitmap := TBitmap.Create; |
| 1231 |
Bitmap.LoadFromIntfImage(IntfImage); |
| 1232 |
ImageIndex := ImageListFrames.Add(Bitmap, nil); |
| 1233 |
finally |
| 1234 |
FreeAndNil(ResizedFrameImage); |
| 1235 |
FreeAndNil(IntfImage); |
| 1236 |
FreeAndNil(Bitmap); |
| 1237 |
end; |
| 1238 |
|
| 1239 |
ListItem := ListViewFrames.Items.Add; |
| 1240 |
ListItem.Caption := FrameTitle(FrameNo, Frame); |
| 1241 |
ListItem.Data := Frame; |
| 1242 |
ListItem.ImageIndex := ImageIndex; |
| 1243 |
Result := ListItem; |
| 1244 |
end; |
| 1245 |
|
| 1246 |
procedure TSpriteSheetEditorForm.LoadFrames(const Animation: TCastleSpriteSheetAnimation); |
| 1247 |
var |
| 1248 |
I: Integer; |
| 1249 |
|
| 1250 |
procedure PrepareImageList; |
| 1251 |
begin |
| 1252 |
ImageListFrames.Clear; |
| 1253 |
|
| 1254 |
{ ListView can have only one size of images so we need decide about size. } |
| 1255 |
CurrentFrameIconSize := Animation.GetBigestFrameSize(MaxFrameIconSize, MaxFrameIconSize); |
| 1256 |
|
| 1257 |
if (CurrentFrameIconSize.X = 0) or (CurrentFrameIconSize.Y = 0) then |
| 1258 |
begin |
| 1259 |
CurrentFrameIconSize.X := DefaultFrameIconSize; |
| 1260 |
CurrentFrameIconSize.Y := DefaultFrameIconSize; |
| 1261 |
end; |
| 1262 |
|
| 1263 |
ImageListFrames.Width := CurrentFrameIconSize.X; |
| 1264 |
ImageListFrames.Height := CurrentFrameIconSize.Y; |
| 1265 |
end; |
| 1266 |
|
| 1267 |
begin |
| 1268 |
PrepareImageList; |
| 1269 |
|
| 1270 |
for I := 0 to Animation.FrameCount - 1 do |
| 1271 |
AddFrameToListView(Animation.Frame[I], I); |
| 1272 |
end; |
| 1273 |
|
| 1274 |
function TSpriteSheetEditorForm.GetFirstSelectedFrame: TCastleSpriteSheetFrame; |
| 1275 |
var |
| 1276 |
Item: TListItem; |
| 1277 |
begin |
| 1278 |
Item := ListViewFrames.Selected; |
| 1279 |
if (Item <> nil) and TSelectedFrames.IsFrameListItem(Item) then |
| 1280 |
Result := TCastleSpriteSheetFrame(Item.Data) |
| 1281 |
else |
| 1282 |
Result := nil; |
| 1283 |
end; |
| 1284 |
|
| 1285 |
|
| 1286 |
function TSpriteSheetEditorForm.GetLastSelectedFrame: TCastleSpriteSheetFrame; |
| 1287 |
var |
| 1288 |
SelectedFrames: TSelectedFrames; |
| 1289 |
begin |
| 1290 |
if ListViewFrames.Selected = nil then |
| 1291 |
Exit(nil); |
| 1292 |
|
| 1293 |
SelectedFrames := TSelectedFrames.Create(ListViewFrames); |
| 1294 |
try |
| 1295 |
SelectedFrames.GetCurrentSelection; |
| 1296 |
Result := SelectedFrames.Frame[SelectedFrames.FrameCount - 1]; |
| 1297 |
finally |
| 1298 |
FreeAndNil(SelectedFrames); |
| 1299 |
end; |
| 1300 |
end; |
| 1301 |
|
| 1302 |
function TSpriteSheetEditorForm.FrameTitle(const FrameNo: Integer; |
| 1303 |
const Frame: TCastleSpriteSheetFrame): String; |
| 1304 |
begin |
| 1305 |
Result := IntToStr(FrameNo + 1) + ' - ' + IntToStr(Frame.FrameWidth) + |
| 1306 |
'x' + IntToStr(Frame.FrameHeight); |
| 1307 |
end; |
| 1308 |
|
| 1309 |
procedure TSpriteSheetEditorForm.UpdateFrameTitles; |
| 1310 |
var |
| 1311 |
I: Integer; |
| 1312 |
AListItem: TListItem; |
| 1313 |
begin |
| 1314 |
for I := 0 to ListViewFrames.Items.Count - 1 do |
| 1315 |
begin |
| 1316 |
AListItem := ListViewFrames.Items[I]; |
| 1317 |
if TSelectedFrames.IsFrameListItem(AListItem) then |
| 1318 |
begin |
| 1319 |
AListItem.Caption := FrameTitle(I, |
| 1320 |
TCastleSpriteSheetFrame(AListItem.Data)); |
| 1321 |
end; |
| 1322 |
end; |
| 1323 |
end; |
| 1324 |
|
| 1325 |
function TSpriteSheetEditorForm.GetCurrentPreviewMode: TPreviewMode; |
| 1326 |
begin |
| 1327 |
if RadioAnimation.Checked then |
| 1328 |
Result:= pmAnimation |
| 1329 |
else |
| 1330 |
Result:= pmFrame; |
| 1331 |
end; |
| 1332 |
|
| 1333 |
procedure TSpriteSheetEditorForm.ShowPreviewControl(const MakeVisible: Boolean); |
| 1334 |
begin |
| 1335 |
CastleControlPreview.Visible := MakeVisible; |
| 1336 |
LabelNoFrameToShow.Visible := not MakeVisible; |
| 1337 |
end; |
| 1338 |
|
| 1339 |
procedure TSpriteSheetEditorForm.CreatePreviewUIIfNeeded; |
| 1340 |
begin |
| 1341 |
if FPreviewScene = nil then |
| 1342 |
begin |
| 1343 |
FNavigation := TCastle2DNavigation.Create(Self); |
| 1344 |
|
| 1345 |
FViewport := TCastleViewport.InternalCreateNonDesign(Self, 0); |
| 1346 |
Assert(FViewport.Camera <> nil); |
| 1347 |
FViewport.FullSize := true; |
| 1348 |
FViewport.InsertFront(FNavigation); |
| 1349 |
FViewport.Setup2D; |
| 1350 |
FViewport.Camera.Orthographic.Origin := Vector2(0.5, 0.5); |
| 1351 |
CastleControlPreview.Controls.InsertFront(FViewport); |
| 1352 |
|
| 1353 |
FPreviewScene := TCastleScene.Create(Self); |
| 1354 |
|
| 1355 |
FViewport.Items.Add(FPreviewScene); |
| 1356 |
FViewport.Items.MainScene := FPreviewScene; // TODO: removing MainScene assignment makes camera not OK, testcase: tentacles |
| 1357 |
end; |
| 1358 |
end; |
| 1359 |
|
| 1360 |
procedure TSpriteSheetEditorForm.UpdatePreview( |
| 1361 |
const PreviewModesToUpdate: TPreviewMode); |
| 1362 |
|
| 1363 |
procedure LoadFrameInPreview( |
| 1364 |
const Frame: TCastleSpriteSheetFrame); |
| 1365 |
begin |
| 1366 |
if Frame = nil then |
| 1367 |
begin |
| 1368 |
ShowPreviewControl(false); |
| 1369 |
Exit; |
| 1370 |
end; |
| 1371 |
RegenerateFramePreview(Frame); |
| 1372 |
FPreviewScene.Exists := true; |
| 1373 |
ShowPreviewControl(true); |
| 1374 |
end; |
| 1375 |
|
| 1376 |
procedure LoadAnimationInPreview( |
| 1377 |
const Animation: TCastleSpriteSheetAnimation); |
| 1378 |
begin |
| 1379 |
ShowPreviewControl(true); |
| 1380 |
if FPreviewScene = nil then |
| 1381 |
RegenerateAnimationPreview; |
| 1382 |
|
| 1383 |
if (Animation = nil) or (Animation.FrameCount = 0) or |
| 1384 |
(not CheckAtlasMinSize) then |
| 1385 |
begin |
| 1386 |
FPreviewScene.Exists := false; |
| 1387 |
FPreviewScene.StopAnimation |
| 1388 |
end else |
| 1389 |
begin |
| 1390 |
FPreviewScene.Exists := true; |
| 1391 |
FViewport.AssignDefaultCamera; |
| 1392 |
// set this after AssignDefaultCamera, as AssignDefaultCamera resets it |
| 1393 |
FViewport.Camera.Orthographic.Width := Animation.Frame[0].FrameWidth + PreviewMargin * 2; |
| 1394 |
FPreviewScene.PlayAnimation(Animation.Name, true, true); |
| 1395 |
end; |
| 1396 |
end; |
| 1397 |
|
| 1398 |
begin |
| 1399 |
if FLockUpdatePReviewRC > 0 then |
| 1400 |
Exit; |
| 1401 |
|
| 1402 |
case PreviewModesToUpdate of |
| 1403 |
pmAnimation: |
| 1404 |
begin |
| 1405 |
if not FNodesShowSpriteSheetAnimation then |
| 1406 |
begin |
| 1407 |
RegenerateAnimationPreview; |
| 1408 |
FNodesShowSpriteSheetAnimation := false; |
| 1409 |
end; |
| 1410 |
LoadAnimationInPreview(GetCurrentAnimation); |
| 1411 |
end; |
| 1412 |
pmFrame: |
| 1413 |
LoadFrameInPreview(GetFirstSelectedFrame); |
| 1414 |
end; |
| 1415 |
end; |
| 1416 |
|
| 1417 |
function TSpriteSheetEditorForm.GetCurrentAnimation: TCastleSpriteSheetAnimation; |
| 1418 |
begin |
| 1419 |
if ListViewAnimations.ItemIndex < 0 then |
| 1420 |
Exit(nil); |
| 1421 |
|
| 1422 |
Result := TCastleSpriteSheetAnimation(ListViewAnimations.Items[ListViewAnimations.ItemIndex].Data); |
| 1423 |
end; |
| 1424 |
|
| 1425 |
procedure TSpriteSheetEditorForm.RegenerateAnimationPreview; |
| 1426 |
begin |
| 1427 |
try |
| 1428 |
CreatePreviewUIIfNeeded; |
| 1429 |
if not CheckAtlasMinSize then |
| 1430 |
Exit; |
| 1431 |
|
| 1432 |
FPreviewScene.Scale := Vector3(1.0, 1.0, 1.0); |
| 1433 |
FPreviewScene.Load(FSpriteSheet.ToX3D, true); |
| 1434 |
FViewport.AssignDefaultCamera; |
| 1435 |
// set this after AssignDefaultCamera, as AssignDefaultCamera resets it |
| 1436 |
FViewport.Camera.Orthographic.Width := DefaultFrameIconSize + PreviewMargin * 2; |
| 1437 |
except |
| 1438 |
on E: Exception do |
| 1439 |
begin |
| 1440 |
ErrorBox(E.Message); |
| 1441 |
end; |
| 1442 |
end; |
| 1443 |
end; |
| 1444 |
|
| 1445 |
procedure TSpriteSheetEditorForm.RegenerateFramePreview(const Frame: TCastleSpriteSheetFrame); |
| 1446 |
|
| 1447 |
function Generate3XDWithImage: TX3DRootNode; |
| 1448 |
var |
| 1449 |
Material: TUnlitMaterialNode; |
| 1450 |
Appearance: TAppearanceNode; |
| 1451 |
Shape: TShapeNode; |
| 1452 |
Tri: TTriangleSetNode; |
| 1453 |
Tex: TPixelTextureNode; |
| 1454 |
TexProperties: TTexturePropertiesNode; |
| 1455 |
HalfFrameWidth: Single; |
| 1456 |
HalfFrameHeight: Single; |
| 1457 |
ShapeCoord: TCoordinateNode; |
| 1458 |
ShapeTexCoord : TTextureCoordinateNode; |
| 1459 |
begin |
| 1460 |
Result := TX3DRootNode.Create; |
| 1461 |
|
| 1462 |
Material := TUnlitMaterialNode.Create; |
| 1463 |
|
| 1464 |
Appearance := TAppearanceNode.Create; |
| 1465 |
Appearance.Material := Material; |
| 1466 |
|
| 1467 |
Shape := TShapeNode.Create; |
| 1468 |
Shape.Appearance := Appearance; |
| 1469 |
|
| 1470 |
Tex := TPixelTextureNode.Create; |
| 1471 |
Tex.FdImage.Value := Frame.MakeImageCopy; |
| 1472 |
{ No point in adjusting RepeatS/T: TextureProperties override it. |
| 1473 |
Tex.RepeatS := false; |
| 1474 |
Tex.RepeatT := false; |
| 1475 |
} |
| 1476 |
Appearance.Texture := Tex; |
| 1477 |
|
| 1478 |
TexProperties := TTexturePropertiesNode.Create; |
| 1479 |
TexProperties.BoundaryModeS := bmClampToEdge; |
| 1480 |
TexProperties.BoundaryModeT := bmClampToEdge; |
| 1481 |
{ Do not force "power of 2" size, which may prevent mipmaps. |
| 1482 |
This seems like a better default (otherwise the resizing underneath |
| 1483 |
may cause longer loading time, and loss of quality, if not expected). |
| 1484 |
Consistent with X3DLoadInternalImage and sprite sheet loaders. } |
| 1485 |
TexProperties.GuiTexture := true; |
| 1486 |
Tex.TextureProperties := TexProperties; |
| 1487 |
|
| 1488 |
Tri := TTriangleSetNode.Create; |
| 1489 |
Tri.Solid := false; |
| 1490 |
|
| 1491 |
HalfFrameWidth := Frame.FrameWidth * 0.5; |
| 1492 |
HalfFrameHeight := Frame.FrameHeight * 0.5; |
| 1493 |
|
| 1494 |
ShapeCoord := TCoordinateNode.Create('coord'); |
| 1495 |
ShapeCoord.SetPoint([ |
| 1496 |
Vector3(-HalfFrameWidth, -HalfFrameHeight, 0), |
| 1497 |
Vector3(HalfFrameWidth, -HalfFrameHeight, 0), |
| 1498 |
Vector3(HalfFrameWidth, HalfFrameHeight, 0), |
| 1499 |
Vector3(-HalfFrameWidth, -HalfFrameHeight, 0), |
| 1500 |
Vector3(HalfFrameWidth, HalfFrameHeight, 0), |
| 1501 |
Vector3(-HalfFrameWidth, HalfFrameHeight, 0)]); |
| 1502 |
|
| 1503 |
ShapeTexCoord := TTextureCoordinateNode.Create('texcoord'); |
| 1504 |
ShapeTexCoord.SetPoint([ |
| 1505 |
Vector2(0, 0), |
| 1506 |
Vector2(1, 0), |
| 1507 |
Vector2(1, 1), |
| 1508 |
Vector2(0, 0), |
| 1509 |
Vector2(1, 1), |
| 1510 |
Vector2(0, 1)]); |
| 1511 |
|
| 1512 |
Tri.Coord := ShapeCoord; |
| 1513 |
Tri.TexCoord := ShapeTexCoord; |
| 1514 |
Shape.Geometry := Tri; |
| 1515 |
|
| 1516 |
Result.AddChildren(Shape); |
| 1517 |
end; |
| 1518 |
|
| 1519 |
begin |
| 1520 |
CreatePreviewUIIfNeeded; |
| 1521 |
|
| 1522 |
FPreviewScene.Scale := Vector3(1.0, 1.0, 1.0); |
| 1523 |
FPreviewScene.Load(Generate3XDWithImage, true); |
| 1524 |
|
| 1525 |
FViewport.AssignDefaultCamera; |
| 1526 |
{ Always set to the width of the first frame because we want to see the |
| 1527 |
size difference } |
| 1528 |
// set this after AssignDefaultCamera, as AssignDefaultCamera resets it |
| 1529 |
FViewport.Camera.Orthographic.Width := Frame.Animation.Frame[0].FrameWidth; |
| 1530 |
end; |
| 1531 |
|
| 1532 |
procedure TSpriteSheetEditorForm.LockUpdatePreview; |
| 1533 |
begin |
| 1534 |
Inc(FLockUpdatePReviewRC); |
| 1535 |
end; |
| 1536 |
|
| 1537 |
procedure TSpriteSheetEditorForm.UnlockUpdatePreview; |
| 1538 |
begin |
| 1539 |
Dec(FLockUpdatePReviewRC); |
| 1540 |
if FLockUpdatePReviewRC < 1 then |
| 1541 |
begin |
| 1542 |
FNodesShowSpriteSheetAnimation := false; |
| 1543 |
UpdatePreview(GetCurrentPreviewMode); |
| 1544 |
end; |
| 1545 |
end; |
| 1546 |
|
| 1547 |
procedure TSpriteSheetEditorForm.UpdateWindowCaption; |
| 1548 |
var |
| 1549 |
ModifiedMark: String; |
| 1550 |
FileName: String; |
| 1551 |
begin |
| 1552 |
if FSpriteSheet = nil then |
| 1553 |
begin |
| 1554 |
Caption := FWindowTitle; |
| 1555 |
Exit; |
| 1556 |
end; |
| 1557 |
|
| 1558 |
if FSpriteSheet.IsModified then |
| 1559 |
ModifiedMark := '*' |
| 1560 |
else |
| 1561 |
ModifiedMark := ''; |
| 1562 |
|
| 1563 |
if FSpriteSheet.URL = '' then |
| 1564 |
FileName := 'New Sprite Sheet' |
| 1565 |
else |
| 1566 |
FileName := ExtractUriName(FSpriteSheet.URL); |
| 1567 |
|
| 1568 |
Caption := ModifiedMark + FileName + ' | ' + FWindowTitle; |
| 1569 |
end; |
| 1570 |
|
| 1571 |
procedure TSpriteSheetEditorForm.SetAtlasError(const Message: String); |
| 1572 |
begin |
| 1573 |
LabelAtlasSizeError.Caption := Message; |
| 1574 |
end; |
| 1575 |
|
| 1576 |
procedure TSpriteSheetEditorForm.SetAtlasWarning(const Message: String); |
| 1577 |
begin |
| 1578 |
ImageAtlasSizeWarning.Visible := (Message <> ''); |
| 1579 |
LabelAtlasWarning.Caption := Message; |
| 1580 |
end; |
| 1581 |
|
| 1582 |
function TSpriteSheetEditorForm.CheckAtlasMinSize: Boolean; |
| 1583 |
var |
| 1584 |
MinAtlasWidth, MinAtlasHeight: Integer; |
| 1585 |
begin |
| 1586 |
Result := true; |
| 1587 |
FSpriteSheet.GetMinAtlasSize(MinAtlasWidth, MinAtlasHeight); |
| 1588 |
|
| 1589 |
if (MinAtlasWidth > FSpriteSheet.MaxAtlasWidth) or |
| 1590 |
(MinAtlasHeight > FSpriteSheet.MaxAtlasHeight) then |
| 1591 |
begin |
| 1592 |
SetAtlasError(Format( |
| 1593 |
'Maximum atlas size too small to fit all the frames. Increase it to at least %d.', |
| 1594 |
[Max(MinAtlasWidth, MinAtlasHeight)]) |
| 1595 |
); |
| 1596 |
Result := false; |
| 1597 |
end else |
| 1598 |
SetAtlasError(''); |
| 1599 |
|
| 1600 |
{ check power of two } |
| 1601 |
if not IsPowerOf2(Max(FSpriteSheet.MaxAtlasHeight, |
| 1602 |
FSpriteSheet.MaxAtlasWidth)) then |
| 1603 |
SetAtlasWarning('We advise using power of 2 size.') |
| 1604 |
else |
| 1605 |
SetAtlasWarning(''); |
| 1606 |
end; |
| 1607 |
|
| 1608 |
procedure TSpriteSheetEditorForm.LoadAtlasSize; |
| 1609 |
begin |
| 1610 |
if FSpriteSheet = nil then |
| 1611 |
SpinEditMaxAtlasSize.Value := 1024 |
| 1612 |
else |
| 1613 |
SpinEditMaxAtlasSize.Value := Max(FSpriteSheet.MaxAtlasHeight, |
| 1614 |
FSpriteSheet.MaxAtlasWidth); |
| 1615 |
end; |
| 1616 |
|
| 1617 |
procedure TSpriteSheetEditorForm.URLChanged(Sender: TObject); |
| 1618 |
begin |
| 1619 |
UpdateWindowCaption; |
| 1620 |
end; |
| 1621 |
|
| 1622 |
procedure TSpriteSheetEditorForm.ModifiedStateChanged(Sender: TObject); |
| 1623 |
begin |
| 1624 |
UpdateWindowCaption; |
| 1625 |
end; |
| 1626 |
|
| 1627 |
procedure TSpriteSheetEditorForm.BeforeAnimationRemoved( |
| 1628 |
AnimationToRemove: TCastleSpriteSheetAnimation); |
| 1629 |
var |
| 1630 |
I: Integer; |
| 1631 |
ItemIndex: Integer; |
| 1632 |
begin |
| 1633 |
if AnimationToRemove = GetCurrentAnimation then |
| 1634 |
begin |
| 1635 |
ClearFrames; |
| 1636 |
ItemIndex := ListViewAnimations.ItemIndex; |
| 1637 |
ListViewAnimations.Items.Delete(ItemIndex); |
| 1638 |
|
| 1639 |
{ Select next animation } |
| 1640 |
if ListViewAnimations.Items.Count > 0 then |
| 1641 |
begin |
| 1642 |
if ListViewAnimations.Items.Count > ItemIndex then |
| 1643 |
ListViewAnimations.ItemIndex := ItemIndex |
| 1644 |
else |
| 1645 |
ListViewAnimations.ItemIndex := ListViewAnimations.Items.Count - 1; |
| 1646 |
end; |
| 1647 |
|
| 1648 |
Exit; |
| 1649 |
end; |
| 1650 |
|
| 1651 |
{ When not current animation was removed } |
| 1652 |
for I := ListViewAnimations.Items.Count - 1 downto 0 do |
| 1653 |
begin |
| 1654 |
if TObject(ListViewAnimations.Items[i].Data) = AnimationToRemove then |
| 1655 |
begin |
| 1656 |
ListViewAnimations.Items.Delete(I); |
| 1657 |
Exit; |
| 1658 |
end; |
| 1659 |
end; |
| 1660 |
end; |
| 1661 |
|
| 1662 |
procedure TSpriteSheetEditorForm.BeforeAnimationFrameRemoved( |
| 1663 |
FrameToRemove: TCastleSpriteSheetFrame); |
| 1664 |
var |
| 1665 |
I: Integer; |
| 1666 |
begin |
| 1667 |
{ Is changed Animation the current one? } |
| 1668 |
if FrameToRemove.Animation <> GetCurrentAnimation then |
| 1669 |
Exit; |
| 1670 |
|
| 1671 |
{ Remove frames from ListViewFrames } |
| 1672 |
for I := ListViewFrames.Items.Count - 1 downto 0 do |
| 1673 |
begin |
| 1674 |
if TCastleSpriteSheetFrame(ListViewFrames.Items[I].Data) = FrameToRemove then |
| 1675 |
ListViewFrames.Items.Delete(I); |
| 1676 |
end; |
| 1677 |
|
| 1678 |
UpdateFrameTitles; |
| 1679 |
{ No preview update here, becouse this is "Before" event so update preview do |
| 1680 |
nothing. Preview update is in action Execute function. } |
| 1681 |
end; |
| 1682 |
|
| 1683 |
procedure TSpriteSheetEditorForm.FrameMoved( |
| 1684 |
const Frame: TCastleSpriteSheetFrame; const OldIndex, NewIndex: Integer); |
| 1685 |
var |
| 1686 |
Selection: TSelectedFrames; |
| 1687 |
begin |
| 1688 |
{ Is changed Animation the current one? } |
| 1689 |
if Frame.Animation = GetCurrentAnimation then |
| 1690 |
begin |
| 1691 |
{ I know this looks weird, but after rearranging the order of items |
| 1692 |
(at least on GTK2) the selection is completely broken. So here I need |
| 1693 |
remember selection and restore it after item rearranging. } |
| 1694 |
Selection := TSelectedFrames.Create(ListViewFrames); |
| 1695 |
try |
| 1696 |
Selection.GetCurrentSelection; |
| 1697 |
ListViewFrames.ClearSelection; |
| 1698 |
|
| 1699 |
ListViewFrames.Items.Move(OldIndex, NewIndex); |
| 1700 |
UpdateFrameTitles; |
| 1701 |
finally |
| 1702 |
Selection.SetSelection; |
| 1703 |
FreeAndNil(Selection); |
| 1704 |
end; |
| 1705 |
end; |
| 1706 |
|
| 1707 |
{ Preview update must be always called here (make preview always correct). } |
| 1708 |
FNodesShowSpriteSheetAnimation := false; |
| 1709 |
UpdatePreview(GetCurrentPreviewMode); |
| 1710 |
end; |
| 1711 |
|
| 1712 |
procedure TSpriteSheetEditorForm.AnimationMoved( |
| 1713 |
const Animation: TCastleSpriteSheetAnimation; const OldIndex, |
| 1714 |
NewIndex: Integer); |
| 1715 |
begin |
| 1716 |
ListViewAnimations.Items.Move(OldIndex, NewIndex); |
| 1717 |
{ Moving the animations on the list does not change the display, |
| 1718 |
so it doesn't need a preview update } |
| 1719 |
end; |
| 1720 |
|
| 1721 |
procedure TSpriteSheetEditorForm.MaxAtlasSizeChanged(const MaxWidth, |
| 1722 |
MaxHeight: Integer); |
| 1723 |
begin |
| 1724 |
SpinEditMaxAtlasSize.Value := Max(MaxWidth, MaxHeight); |
| 1725 |
FNodesShowSpriteSheetAnimation := false; |
| 1726 |
UpdatePreview(GetCurrentPreviewMode); |
| 1727 |
end; |
| 1728 |
|
| 1729 |
procedure TSpriteSheetEditorForm.AnimationAdded( |
| 1730 |
NewAnimation: TCastleSpriteSheetAnimation); |
| 1731 |
var |
| 1732 |
NewAnimationItem: TListItem; |
| 1733 |
begin |
| 1734 |
NewAnimationItem := AddAnimationToListView(NewAnimation); |
| 1735 |
if FSelectNewAnimation then |
| 1736 |
begin |
| 1737 |
ListViewAnimations.Selected := NewAnimationItem; |
| 1738 |
ListViewAnimations.Selected.MakeVisible(false); |
| 1739 |
end; |
| 1740 |
{ On GTK2, actions at this point are not automatically updated } |
| 1741 |
UpdateActions; |
| 1742 |
end; |
| 1743 |
|
| 1744 |
procedure TSpriteSheetEditorForm.FrameAdded(NewFrame: TCastleSpriteSheetFrame); |
| 1745 |
begin |
| 1746 |
{ Is changed Animation the current one? } |
| 1747 |
if NewFrame.Animation <> GetCurrentAnimation then |
| 1748 |
Exit; |
| 1749 |
|
| 1750 |
try |
| 1751 |
{ Cases when we need reload all frames: |
| 1752 |
- When added frame is bigger than CurrentFrameIconSize but smaller than |
| 1753 |
MaxFrameIconSize |
| 1754 |
- When there are no frames we need use LoadFrames to prepare ListView, |
| 1755 |
maybe that should be changed to more obvious solution } |
| 1756 |
if (NewFrame.Animation.FrameCount = 1) or ( |
| 1757 |
((NewFrame.FrameWidth < MaxFrameIconSize) and |
| 1758 |
(NewFrame.FrameWidth > CurrentFrameIconSize.X)) or |
| 1759 |
((NewFrame.FrameHeight < MaxFrameIconSize) and |
| 1760 |
(NewFrame.FrameHeight > CurrentFrameIconSize.Y))) then |
| 1761 |
begin |
| 1762 |
ClearFrames; |
| 1763 |
LoadFrames(NewFrame.Animation); |
| 1764 |
Exit; |
| 1765 |
end; |
| 1766 |
|
| 1767 |
{ Just add frame on last position } |
| 1768 |
AddFrameToListView(NewFrame, NewFrame.Animation.FrameCount - 1); |
| 1769 |
finally |
| 1770 |
FNodesShowSpriteSheetAnimation := false; |
| 1771 |
UpdatePreview(GetCurrentPreviewMode); |
| 1772 |
end; |
| 1773 |
end; |
| 1774 |
|
| 1775 |
procedure TSpriteSheetEditorForm.ClearAnimations; |
| 1776 |
begin |
| 1777 |
ListViewAnimations.Items.Clear; |
| 1778 |
end; |
| 1779 |
|
| 1780 |
procedure TSpriteSheetEditorForm.OpenSpriteSheet(const URL: String; |
| 1781 |
const ProposeSave: Boolean); |
| 1782 |
begin |
| 1783 |
try |
| 1784 |
if ProposeSave and (not ProposeSaveSpriteSheet) then |
| 1785 |
Exit; |
| 1786 |
CloseSpriteSheet; |
| 1787 |
FSpriteSheet := TCastleSpriteSheet.Create(true); // edit mode |
| 1788 |
FSpriteSheet.OnModifiedStateChanged := @ModifiedStateChanged; |
| 1789 |
FSpriteSheet.Load(URL); |
| 1790 |
{ Note that this has to be set early to false, not right before |
| 1791 |
UpdatePreview lower in this routine, in case something will call |
| 1792 |
indirectly UpdatePreview. Right now, LoadAnimations actually call |
| 1793 |
UpdatePreview. Failure to set FNodesShowSpriteSheetAnimation:=false |
| 1794 |
would result in warnings "animation not found", see |
| 1795 |
https://github.com/castle-engine/castle-engine/issues/527 . } |
| 1796 |
FNodesShowSpriteSheetAnimation := false; |
| 1797 |
LoadAtlasSize; |
| 1798 |
UpdateWindowCaption; |
| 1799 |
LoadAnimations(FSpriteSheet); |
| 1800 |
AssignEventsToSpriteSheet; |
| 1801 |
FLockUpdatePReviewRC := 0; |
| 1802 |
UpdatePreview(GetCurrentPreviewMode); |
| 1803 |
except |
| 1804 |
on E:Exception do |
| 1805 |
begin |
| 1806 |
ErrorBox(E.Message); |
| 1807 |
FreeAndNil(FSpriteSheet); |
| 1808 |
NewSpriteSheet; |
| 1809 |
end; |
| 1810 |
end; |
| 1811 |
end; |
| 1812 |
|
| 1813 |
procedure TSpriteSheetEditorForm.NewSpriteSheet; |
| 1814 |
begin |
| 1815 |
try |
| 1816 |
if not ProposeSaveSpriteSheet then |
| 1817 |
Exit; |
| 1818 |
CloseSpriteSheet; |
| 1819 |
FSpriteSheet := TCastleSpriteSheet.Create(true); |
| 1820 |
FNodesShowSpriteSheetAnimation := false; |
| 1821 |
LoadAtlasSize; |
| 1822 |
UpdateWindowCaption; |
| 1823 |
LoadAnimations(FSpriteSheet); |
| 1824 |
AssignEventsToSpriteSheet; |
| 1825 |
FLockUpdatePReviewRC := 0; |
| 1826 |
UpdatePreview(GetCurrentPreviewMode); |
| 1827 |
except |
| 1828 |
on E:Exception do |
| 1829 |
begin |
| 1830 |
ErrorBox(E.Message); |
| 1831 |
end; |
| 1832 |
end; |
| 1833 |
end; |
| 1834 |
|
| 1835 |
end. |