Debian Codegraph @

examples/user_interface/test_all_view_events/code/gameviewmain.pas

raw
1 {
2 Copyright 2004-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, CastleNotifications;
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 RootGroup: TCastleUserInterface;
32 LabelFps: TCastleLabel;
33 Timer: TCastleTimer;
34 LabelCharsPressed: TCastleLabel;
35 LabelKeysPressed: TCastleLabel;
36 LabelMousePressed: TCastleLabel;
37 LabelTouches: TCastleLabel;
38 LabelModifierKeys: TCastleLabel;
39 private
40 Notifications: TCastleNotifications;
41 procedure DoTimer(Sender: TObject);
42 public
43 constructor Create(AOwner: TComponent); override;
44 procedure Start; override;
45 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
46 function Press(const Event: TInputPressRelease): Boolean; override;
47 function Release(const Event: TInputPressRelease): Boolean; override;
48 function Motion(const Event: TInputMotion): Boolean; override;
49 procedure Resize; override;
50 end;
51
52 var
53 ViewMain: TViewMain;
54
55 implementation
56
57 uses SysUtils,
58 CastleWindow, CastleVectors, CastleColors, CastleStringUtils;
59
60 { TViewMain ----------------------------------------------------------------- }
61
62 constructor TViewMain.Create(AOwner: TComponent);
63 begin
64 inherited;
65 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
66 end;
67
68 procedure TViewMain.Start;
69 begin
70 inherited;
71
72 { Add TCastleNotifications from code, as for now we don't expose them in editor. }
73 Notifications := TCastleNotifications.Create(FreeAtStop);
74 Notifications.Anchor(hpMiddle);
75 Notifications.Anchor(vpTop, -5);
76 Notifications.TextAlignment := hpMiddle;
77 Notifications.Color := Green;
78 Notifications.MaxMessages := 15;
79 Notifications.Timeout := 20000;
80 InsertFront(Notifications);
81
82 Timer.OnTimer := {$ifdef FPC}@{$endif}DoTimer;
83
84 Notifications.Show('View started');
85 end;
86
87 function TViewMain.Press(const Event: TInputPressRelease): Boolean;
88 var
89 W: TCastleWindow;
90 begin
91 Result := inherited;
92 if Result then Exit; // allow the ancestor to handle event
93
94 { This virtual method is executed when user presses
95 a key, a mouse button, or touches a touch-screen.
96
97 Note that each UI control has also events like OnPress and OnClick.
98 These events can be used to handle the "press", if it should do something
99 specific when used in that UI control.
100 The TViewMain.Press method should be used to handle keys
101 not handled in children controls.
102 }
103
104 Notifications.Show('Press: ' + Event.ToString);
105
106 W := Application.MainWindow;
107
108 case Event.KeyCharacter of
109 { cursor tests: }
110 'n': Container.OverrideCursor := mcNone;
111 'd': Container.OverrideCursor := mcDefault;
112 'w': Container.OverrideCursor := mcWait;
113 's': Container.OverrideCursor := mcResizeHorizontal;
114 { setting mouse position tests: }
115 '1': Container.MousePosition := Vector2(0 , 0);
116 '2': Container.MousePosition := Vector2(Container.PixelsWidth , 0);
117 '3': Container.MousePosition := Vector2(Container.PixelsWidth , Container.PixelsHeight);
118 '4': Container.MousePosition := Vector2(0 , Container.PixelsHeight);
119 '5': Container.MousePosition := Vector2(Container.PixelsWidth / 2, Container.PixelsHeight / 2);
120 { test TCastleWindow.MessageXxx }
121 'm': W.MessageOK('Test information.', mtInfo);
122 'q':
123 begin
124 if W.MessageYesNo('Test question. Would you like to?', mtError) then
125 W.MessageOK('You answered yes.', mtInfo)
126 else
127 W.MessageOK('You answered no.', mtInfo);
128 end;
129 end;
130
131 // switching FullScreen
132 if Event.IsKey(keyF11) {$ifdef DARWIN} and Container.Pressed[keyCtrl] {$endif} then
133 Application.MainWindow.FullScreen := not Application.MainWindow.FullScreen;
134 end;
135
136 function TViewMain.Release(const Event: TInputPressRelease): Boolean;
137 begin
138 Result := inherited;
139 if Result then Exit; // allow the ancestor to handle event
140
141 Notifications.Show('Release: ' + Event.ToString);
142 end;
143
144 function TViewMain.Motion(const Event: TInputMotion): Boolean;
145 begin
146 Result := inherited;
147 if Result then Exit; // allow the ancestor to handle event
148
149 Notifications.Show(Format('Motion: old position %s, new position %s, delta %s', [
150 Event.OldPosition.ToString,
151 Event.Position.ToString,
152 (Event.Position - Event.OldPosition).ToString
153 ]));
154 end;
155
156 procedure TViewMain.Resize;
157 begin
158 inherited; // allow the ancestor to handle event
159
160 Assert(Container.UnscaledWidth = RootGroup.EffectiveWidth);
161 Assert(Container.UnscaledHeight = RootGroup.EffectiveHeight);
162
163 Notifications.Show(Format('Resize: new size (in real device pixels) %d %d, new size with UI scaling: %f %f', [
164 Container.PixelsWidth,
165 Container.PixelsHeight,
166 Container.UnscaledWidth,
167 Container.UnscaledHeight
168 ]));
169 end;
170
171 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
172 var
173 C: Char;
174 Key: TKey;
175 MouseButton: TCastleMouseButton;
176 S: String;
177 I: Integer;
178 begin
179 inherited; // allow the ancestor to handle event
180
181 { This virtual method is executed every frame (many times per second). }
182
183 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
184
185 S := '';
186 for C := Low(C) to High(C) do
187 if Container.Pressed.Characters[C] then
188 S := SAppendPart(S, ', ', CharToNiceStr(C));
189 S := 'Characters pressed: [' + S + ']';
190 LabelCharsPressed.Caption := S;
191
192 S := '';
193 for Key := Low(Key) to High(Key) do
194 if Container.Pressed[Key] then
195 S := SAppendPart(S, ', ', KeyToStr(Key));
196 S := 'Keys pressed: [' + S + ']';
197 LabelKeysPressed.Caption := S;
198
199 LabelModifierKeys.Caption := 'Modifier keys pressed: ' +
200 ModifierKeysToNiceStr(Container.Pressed.Modifiers);
201
202 S := '';
203 for MouseButton := Low(MouseButton) to High(MouseButton) do
204 if MouseButton in Container.MousePressed then
205 S := SAppendPart(S, ', ', MouseButtonStr[MouseButton]);
206 S := 'Mouse buttons pressed: [' + S + ']';
207 LabelMousePressed.Caption := S;
208
209 S := '';
210 for I := 0 to Container.TouchesCount - 1 do
211 S := SAppendPart(S, ', ', IntToStr(Container.Touches[I].FingerIndex));
212 S := 'Fingers touched: [' + S + ']';
213 LabelTouches.Caption := S;
214
215 // Just a test that checking for keys, and using MessageOk, works from Update
216 if Container.Pressed[keyF12] then
217 Application.MainWindow.MessageOk('F12 key pressed. This is just a test that MessageOk works.', mtInfo);
218 end;
219
220 procedure TViewMain.DoTimer(Sender: TObject);
221 begin
222 Notifications.Show(Format('Timer (every 5 seconds): Time now is %s', [FormatDateTime('tt', Time)]));
223 end;
224
225 end.