Debian Codegraph @

examples/animations/animate_bones_by_code/code/gameviewmain.pas

raw
1 {
2 Copyright 2022-2023 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 { Main view, where most of the application logic takes place. }
16 unit GameViewMain;
17
18 interface
19
20 uses Classes,
21 CastleVectors, CastleComponentSerialize, CastleTimeUtils,
22 CastleUIControls, CastleControls, CastleKeysMouse, CastleScene,
23 X3DNodes;
24
25 type
26 { Main view, where most of the application logic takes place. }
27 TViewMain = class(TCastleView)
28 published
29 { Components designed using CGE editor.
30 These fields will be automatically initialized at Start. }
31 LabelFps: TCastleLabel;
32 SceneHumanoid: TCastleScene;
33 ButtonPlayWalk, ButtonPlayHead, ButtonPlayBoth: TCastleButton;
34 private
35 { Other fields }
36 TransformNeck: TTransformNode;
37 HeadAnimation: Boolean; //< whether we play animation of TransformNeck, controled by code
38 HeadAnimationTime: TFloatTime;
39 procedure DebugLogNodeName(Node: TX3DNode);
40 procedure ClickPlayWalk(Sender: TObject);
41 procedure ClickPlayHead(Sender: TObject);
42 procedure ClickPlayBoth(Sender: TObject);
43 public
44 constructor Create(AOwner: TComponent); override;
45 procedure Start; override;
46 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
47 end;
48
49 var
50 ViewMain: TViewMain;
51
52 implementation
53
54 uses SysUtils,
55 CastleLog, CastleUtils;
56
57 { TViewMain ----------------------------------------------------------------- }
58
59 constructor TViewMain.Create(AOwner: TComponent);
60 begin
61 inherited;
62 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
63 end;
64
65 procedure TViewMain.DebugLogNodeName(Node: TX3DNode);
66 var
67 ParentNode: TX3DNode;
68 ParentNodeStr: String;
69 begin
70 case Node.ParentFieldsCount of
71 0: ParentNodeStr := '(no parent)'; // possible for root node
72 1: begin
73 ParentNode := Node.ParentFields[0].ParentNode as TX3DNode;
74 ParentNodeStr := ParentNode.X3DName;
75 if ParentNodeStr = '' then
76 ParentNodeStr := '(unnamed)';
77 end;
78 else
79 ParentNodeStr := '(multiple parents)';
80 end;
81
82 WritelnLog('Found node: %s, parent: %s', [
83 Node.X3DName,
84 ParentNodeStr
85 ]);
86 end;
87
88 procedure TViewMain.Start;
89 begin
90 inherited;
91
92 { for debug: write to log all transform nodes }
93 SceneHumanoid.RootNode.EnumerateNodes(TTransformNode, {$ifdef FPC}@{$endif} DebugLogNodeName, false);
94
95 ButtonPlayWalk.OnClick := {$ifdef FPC}@{$endif} ClickPlayWalk;
96 ButtonPlayHead.OnClick := {$ifdef FPC}@{$endif} ClickPlayHead;
97 ButtonPlayBoth.OnClick := {$ifdef FPC}@{$endif} ClickPlayBoth;
98
99 TransformNeck := SceneHumanoid.Node('Neck') as TTransformNode;
100 end;
101
102 procedure TViewMain.ClickPlayWalk(Sender: TObject);
103 begin
104 SceneHumanoid.AutoAnimation := 'walk';
105 HeadAnimation := false;
106 end;
107
108 procedure TViewMain.ClickPlayHead(Sender: TObject);
109 begin
110 SceneHumanoid.AutoAnimation := ''; // stop walking
111 HeadAnimation := true;
112 end;
113
114 procedure TViewMain.ClickPlayBoth(Sender: TObject);
115 begin
116 SceneHumanoid.AutoAnimation := 'walk';
117 HeadAnimation := true;
118 end;
119
120 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
121
122 function AnimateKeyFrames(const TimeNow: TFloatTime;
123 const KeyTimes: array of Single;
124 const KeyValues: array of Single): Single;
125 var
126 T: Single;
127 Range: Integer;
128 begin
129 Assert(High(KeyTimes) = High(KeyValues));
130 Assert(High(KeyTimes) >= 1); // we need at least 2 keyframes
131 Assert(KeyTimes[0] = 0); // this logic is only prepared for case when KeyTimes[0] = 0
132
133 T := FloatModulo(TimeNow, KeyTimes[High(KeyTimes)]);
134 for Range := 1 to High(KeyTimes) do // TODO: do binary search, knowing that KeyTimes are sorted - faster
135 if KeyTimes[Range] >= T then
136 Break;
137
138 Result := MapRange(T,
139 KeyTimes[Range - 1], KeyTimes[Range],
140 KeyValues[Range - 1], KeyValues[Range]);
141 end;
142
143 begin
144 inherited;
145 { This virtual method is executed every frame (many times per second). }
146 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
147
148 if HeadAnimation then
149 begin
150 HeadAnimationTime := HeadAnimationTime + SecondsPassed;
151 TransformNeck.Rotation := Vector4(
152 // rotation axis
153 1, 0, 0,
154 // rotation angle
155 AnimateKeyFrames(HeadAnimationTime,
156 [ 0, 0.5, 1],
157 [-Pi/6, Pi/6, -Pi/6]
158 )
159 );
160 end;
161 end;
162
163 end.