| 1 |
unit Unit1; |
| 2 |
|
| 3 |
interface |
| 4 |
|
| 5 |
uses |
| 6 |
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, |
| 7 |
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts, |
| 8 |
FMX.StdCtrls, FMX.Controls.Presentation, Fmx.CastleControl, |
| 9 |
CastleScene, CastleViewport, CastleControls, CastleUiControls; |
| 10 |
|
| 11 |
type |
| 12 |
TForm1 = class(TForm) |
| 13 |
CastleControl1: TCastleControl; |
| 14 |
ButtonLoadUrl: TButton; |
| 15 |
LayoutAnimations: TGridLayout; |
| 16 |
Label1: TLabel; |
| 17 |
OpenDialog1: TOpenDialog; |
| 18 |
procedure FormCreate(Sender: TObject); |
| 19 |
procedure ButtonLoadUrlClick(Sender: TObject); |
| 20 |
private |
| 21 |
MainScene: TCastleScene; |
| 22 |
MainViewport: TCastleViewport; |
| 23 |
LabelFps: TCastleLabel; |
| 24 |
procedure LoadScene(const Url: String); |
| 25 |
procedure AnimationButtonClick(Sender: TObject); |
| 26 |
procedure DoUpdate(const Sender: TCastleUserInterface; |
| 27 |
const SecondsPassed: Single; var HandleInput: Boolean); |
| 28 |
public |
| 29 |
{ Public declarations } |
| 30 |
end; |
| 31 |
|
| 32 |
var |
| 33 |
Form1: TForm1; |
| 34 |
|
| 35 |
implementation |
| 36 |
|
| 37 |
uses CastleSceneCore, CastleFmxUtils, X3DLoad, CastleUriUtils, CastleRenderOptions; |
| 38 |
|
| 39 |
{$R *.fmx} |
| 40 |
|
| 41 |
procedure TForm1.ButtonLoadUrlClick(Sender: TObject); |
| 42 |
begin |
| 43 |
if OpenDialog1.Execute then |
| 44 |
LoadScene(FilenameToUriSafe(OpenDialog1.FileName)); |
| 45 |
end; |
| 46 |
|
| 47 |
procedure TForm1.FormCreate(Sender: TObject); |
| 48 |
begin |
| 49 |
{ Initialize UI scaling in CGE. |
| 50 |
Not really needed in this application, that doesn't have any CGE UI, |
| 51 |
we only use CGE for TCastleScene display in viewport. } |
| 52 |
CastleControl1.Container.LoadSettings('castle-data:/CastleSettings.xml'); |
| 53 |
|
| 54 |
{ Initialize references to components designed using CGE editor, |
| 55 |
saved in data/main.castle-user-interface. } |
| 56 |
MainScene := CastleControl1.Container.DesignedComponent('MainScene') as TCastleScene; |
| 57 |
MainViewport := CastleControl1.Container.DesignedComponent('MainViewport') as TCastleViewport; |
| 58 |
LabelFps := CastleControl1.Container.DesignedComponent('LabelFps') as TCastleLabel; |
| 59 |
|
| 60 |
{ Load initial scene. } |
| 61 |
LoadScene('castle-data:/spine_dragon/dragon.json'); |
| 62 |
|
| 63 |
{ Initialize OpenDialog1 to allow loading all supported scene formats. } |
| 64 |
FileFiltersToDialog(LoadScene_FileFilters, OpenDialog1); |
| 65 |
|
| 66 |
{ Assign event to some OnUpdate, to update FPS display } |
| 67 |
LabelFps.OnUpdate := DoUpdate; |
| 68 |
end; |
| 69 |
|
| 70 |
procedure TForm1.DoUpdate(const Sender: TCastleUserInterface; |
| 71 |
const SecondsPassed: Single; var HandleInput: Boolean); |
| 72 |
begin |
| 73 |
{ update LabelFps every frame } |
| 74 |
LabelFps.Caption := 'FPS: ' + CastleControl1.Container.Fps.ToString; |
| 75 |
end; |
| 76 |
|
| 77 |
procedure TForm1.AnimationButtonClick(Sender: TObject); |
| 78 |
var |
| 79 |
AnimName: String; |
| 80 |
begin |
| 81 |
AnimName := (Sender as TButton).Text; |
| 82 |
MainScene.PlayAnimation(AnimName, true); |
| 83 |
|
| 84 |
{ Example how to use TPlayAnimationParameters to adjust |
| 85 |
TransitionDuration (animation cross-fading). } |
| 86 |
{ |
| 87 |
var Params := TPlayAnimationParameters.Create; |
| 88 |
try |
| 89 |
Params.Name := AnimName; |
| 90 |
Params.TransitionDuration := 0.25; |
| 91 |
Params.Loop := true; |
| 92 |
MainScene.PlayAnimation(Params); |
| 93 |
finally |
| 94 |
FreeAndNil(Params); |
| 95 |
end; |
| 96 |
} |
| 97 |
end; |
| 98 |
|
| 99 |
procedure TForm1.LoadScene(const Url: String); |
| 100 |
var |
| 101 |
AnimName: String; |
| 102 |
NewButton: TButton; |
| 103 |
begin |
| 104 |
MainScene.Load(Url); |
| 105 |
|
| 106 |
{ Set blending sort following "NavigationInfo.blendingSort" info from scene. |
| 107 |
This means we use 2D sorting e.g. for Spine models by default. } |
| 108 |
if (MainScene.NavigationInfoStack.Top <> nil) and |
| 109 |
(MainScene.NavigationInfoStack.Top.BlendingSort <> sortAuto) then |
| 110 |
MainViewport.BlendingSort := MainScene.NavigationInfoStack.Top.BlendingSort |
| 111 |
else |
| 112 |
MainViewport.BlendingSort := sortAuto; |
| 113 |
|
| 114 |
MainViewport.AssignDefaultCamera; // adjust camera to scene size |
| 115 |
|
| 116 |
LayoutAnimations.DeleteChildren; |
| 117 |
|
| 118 |
for AnimName in MainScene.AnimationsList do |
| 119 |
begin |
| 120 |
NewButton := TButton.Create(Self); |
| 121 |
NewButton.Text := AnimName; |
| 122 |
NewButton.OnClick := AnimationButtonClick; |
| 123 |
LayoutAnimations.AddObject(NewButton); |
| 124 |
end; |
| 125 |
end; |
| 126 |
|
| 127 |
end. |