Debian Codegraph @

examples/animations/expose_transformations_to_animate_children/code/gameviewmain.pas

raw
1 {
2 Copyright 2020-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
16 { Main view, where most of the application logic takes place. }
17 unit GameViewMain;
18
19 interface
20
21 uses Classes,
22 CastleComponentSerialize, CastleUIControls, CastleControls,
23 CastleKeysMouse, CastleScene;
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 ButtonAnimationWalk: TCastleButton;
33 ButtonAnimationAttack: TCastleButton;
34 ButtonAnimationStop: TCastleButton;
35 ButtonWeaponAxe: TCastleButton;
36 ButtonWeaponSword: TCastleButton;
37 ButtonWeaponNone: TCastleButton;
38 SceneHumanoid: TCastleScene;
39 SceneAxe: TCastleScene;
40 SceneSword: TCastleScene;
41 private
42 procedure ClickAnimationWalk(Sender: TObject);
43 procedure ClickAnimationAttack(Sender: TObject);
44 procedure ClickAnimationStop(Sender: TObject);
45 procedure ClickWeaponAxe(Sender: TObject);
46 procedure ClickWeaponSword(Sender: TObject);
47 procedure ClickWeaponNone(Sender: TObject);
48 public
49 constructor Create(AOwner: TComponent); override;
50 procedure Start; override;
51 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
52 function Press(const Event: TInputPressRelease): Boolean; override;
53 end;
54
55 var
56 ViewMain: TViewMain;
57
58 implementation
59
60 uses SysUtils;
61
62 { TViewMain ----------------------------------------------------------------- }
63
64 constructor TViewMain.Create(AOwner: TComponent);
65 begin
66 inherited;
67 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
68 end;
69
70 procedure TViewMain.Start;
71 begin
72 inherited;
73
74 ButtonAnimationWalk.OnClick := {$ifdef FPC}@{$endif}ClickAnimationWalk;
75 ButtonAnimationAttack.OnClick := {$ifdef FPC}@{$endif}ClickAnimationAttack;
76 ButtonAnimationStop.OnClick := {$ifdef FPC}@{$endif}ClickAnimationStop;
77 ButtonWeaponAxe.OnClick := {$ifdef FPC}@{$endif}ClickWeaponAxe;
78 ButtonWeaponSword.OnClick := {$ifdef FPC}@{$endif}ClickWeaponSword;
79 ButtonWeaponNone.OnClick := {$ifdef FPC}@{$endif}ClickWeaponNone;
80
81 { initially hold axe }
82 ClickWeaponAxe(nil);
83 end;
84
85 procedure TViewMain.ClickAnimationWalk(Sender: TObject);
86 begin
87 SceneHumanoid.PlayAnimation('walk', true);
88 end;
89
90 procedure TViewMain.ClickAnimationAttack(Sender: TObject);
91 begin
92 SceneHumanoid.PlayAnimation('attack', true);
93 end;
94
95 procedure TViewMain.ClickAnimationStop(Sender: TObject);
96 begin
97 SceneHumanoid.StopAnimation;
98 end;
99
100 procedure TViewMain.ClickWeaponAxe(Sender: TObject);
101 begin
102 SceneAxe.Exists := true;
103 SceneSword.Exists := false;
104 end;
105
106 procedure TViewMain.ClickWeaponSword(Sender: TObject);
107 begin
108 SceneAxe.Exists := false;
109 SceneSword.Exists := true;
110 end;
111
112 procedure TViewMain.ClickWeaponNone(Sender: TObject);
113 begin
114 SceneAxe.Exists := false;
115 SceneSword.Exists := false;
116 end;
117
118 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
119 begin
120 inherited;
121 { This virtual method is executed every frame (many times per second). }
122 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
123 end;
124
125 function TViewMain.Press(const Event: TInputPressRelease): Boolean;
126 begin
127 Result := inherited;
128 if Result then Exit; // allow the ancestor to handle keys
129
130 { This virtual method is executed when user presses
131 a key, a mouse button, or touches a touch-screen.
132
133 Note that each UI control has also events like OnPress and OnClick.
134 These events can be used to handle the "press", if it should do something
135 specific when used in that UI control.
136 The TViewMain.Press method should be used to handle keys
137 not handled in children controls.
138 }
139
140 // Use this to handle keys:
141 {
142 if Event.IsKey(keyXxx) then
143 begin
144 // DoSomething;
145 Exit(true); // key was handled
146 end;
147 }
148 end;
149
150 end.