Debian Codegraph @

examples/short_api_samples/animation_stop_notification/animation_stop_notification.dpr

raw
1 { Example of using TPlayAnimationParameters.StopNotification.
2
3 Play animation by pressing Space.
4
5 There are 3 cases when the animation stops in this application:
6
7 - if you press S,
8 - if you just wait for animation to finish,
9 since we leave TPlayAnimationParameters.Loop = false.
10 (it's a short attack animation).
11 - note that the animation also stops when another animation is run.
12 (the OnStopNotification in this case is not called immediately
13 when Scene.PlayAnimation for new animation is called,
14 the actual animation change happens in nearest update).
15
16 Regardless of why the animation stopped, AnimationStopped method is called. }
17
18 {$ifdef MSWINDOWS} {$apptype GUI} {$endif}
19
20 uses SysUtils,
21 CastleWindow, CastleSceneCore, CastleScene, CastleViewport, CastleCameras,
22 CastleVectors, CastleUIControls, CastleControls, X3DNodes,
23 CastleKeysMouse, CastleColors, CastleNotifications, CastleLog;
24
25 { TViewMain ----------------------------------------------------------------- }
26
27 type
28 TViewMain = class(TCastleView)
29 strict private
30 Viewport: TCastleViewport;
31 Scene: TCastleScene;
32 Notifications: TCastleNotifications;
33 procedure AnimationStopped(const AScene: TCastleSceneCore; const Animation: TTimeSensorNode);
34 public
35 procedure Start; override;
36 function Press(const Event: TInputPressRelease): Boolean; override;
37 end;
38
39 procedure TViewMain.Start;
40 begin
41 inherited;
42
43 Viewport := TCastleViewport.Create(FreeAtStop);
44 Viewport.FullSize := true;
45 Viewport.AutoCamera := true;
46 Viewport.InsertBack(TCastleExamineNavigation.Create(Application));
47 InsertFront(Viewport);
48
49 Scene := TCastleScene.Create(FreeAtStop);
50 Scene.Load('castle-data:/knight.gltf');
51 Viewport.Items.Add(Scene);
52 Viewport.Items.MainScene := Scene;
53
54 Notifications := TCastleNotifications.Create(FreeAtStop);
55 Notifications.Timeout := 2;
56 Notifications.Fade := 0.25;
57 Notifications.Color := Yellow;
58 Notifications.FontSize := 30;
59 Notifications.Anchor(vpTop, -10);
60 Notifications.Anchor(hpMiddle);
61 Notifications.TextAlignment := hpMiddle;
62 Viewport.InsertFront(Notifications);
63 end;
64
65 function TViewMain.Press(const Event: TInputPressRelease): Boolean;
66 var
67 PlayAnimationParams: TPlayAnimationParameters;
68 begin
69 Result := inherited;
70 if Result then Exit;
71
72 if Event.IsKey(keySpace) then
73 begin
74 PlayAnimationParams := TPlayAnimationParameters.Create;
75 try
76 PlayAnimationParams.Name := 'Attack';
77 PlayAnimationParams.StopNotification := {$ifdef FPC}@{$endif} AnimationStopped;
78 Scene.PlayAnimation(PlayAnimationParams);
79 finally FreeAndNil(PlayAnimationParams) end;
80
81 Notifications.Show('Playing animation...');
82 end;
83
84 if Event.IsKey(keyS) then
85 Scene.StopAnimation;
86 end;
87
88 procedure TViewMain.AnimationStopped(const AScene: TCastleSceneCore; const Animation: TTimeSensorNode);
89 begin
90 Notifications.Show('Stopped.');
91 end;
92
93 { application initialization ------------------------------------------------- }
94
95 var
96 Window: TCastleWindow;
97 ViewMain: TViewMain;
98 begin
99 InitializeLog;
100
101 Window := TCastleWindow.Create(Application);
102 Window.Open;
103 Application.MainWindow := Window;
104
105 ViewMain := TViewMain.Create(Application);
106 Window.Container.View := ViewMain;
107
108 Application.Run;
109 end.