Debian Codegraph @

examples/deprecated_to_upgrade/joystick/code/gameinitialize.pas

raw
1 {
2 Copyright 2016-2019, 2022 Tomasz Wojtyś, 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 { List all avalaible joysticks/gamepads in the system,
17 and allows to test their inputs. }
18 unit GameInitialize;
19
20 interface
21
22 implementation
23
24 uses SysUtils, Classes,
25 CastleVectors, CastleWindow, CastleControls, CastleOnScreenMenu,
26 CastleImages, CastleFilesUtils, CastleColors,
27 CastleUIControls, CastleNotifications, CastleLog, CastleJoysticks,
28 CastleApplicationProperties, CastleUtils;
29
30 { TJoyAxisVisualize ---------------------------------------------------------- }
31
32 type
33 { Visualize joystick axis (2D vector) state. }
34 TJoyAxisVisualize = class(TCastleRectangleControl)
35 strict private
36 FAxis: TVector2;
37 Shape: TCastleShape;
38 Lab: TCastleLabel;
39 procedure SetAxis(const Value: TVector2);
40 public
41 constructor Create(AOwner: TComponent); override;
42 property Axis: TVector2 read FAxis write SetAxis;
43 end;
44
45 constructor TJoyAxisVisualize.Create(AOwner: TComponent);
46 begin
47 inherited;
48 Color := Vector4(0, 0.5, 0, 1); // dark green
49 Border.AllSides := 2;
50 BorderColor := Yellow;
51 Width := 500;
52 Height := 500;
53
54 Shape := TCastleShape.Create(Self);
55 Shape.ShapeType := stCircle;
56 Shape.Color := Yellow;
57 Shape.Anchor(hpMiddle);
58 Shape.Anchor(vpMiddle);
59 Shape.Width := 100;
60 Shape.Height := 100;
61 InsertFront(Shape);
62
63 Lab := TCastleLabel.Create(Self);
64 Lab.Caption := 'Joystick Axis';
65 Lab.Color := Yellow;
66 Lab.Anchor(hpLeft);
67 Lab.Anchor(vpBottom);
68 InsertFront(Lab);
69 end;
70
71 procedure TJoyAxisVisualize.SetAxis(const Value: TVector2);
72 begin
73 FAxis := Value;
74 Shape.Translation := Value * Vector2(Width - Shape.Width, Height - Shape.Height) / 2;
75 end;
76
77 { globals -------------------------------------------------------------------- }
78
79 const
80 AxisNames: array [JOY_AXIS_X..JOY_POVY] of string =
81 ( 'JOY_AXIS_X',
82 'JOY_AXIS_Y',
83 'JOY_AXIS_Z',
84 'JOY_AXIS_R',
85 'JOY_AXIS_U',
86 'JOY_AXIS_V',
87 'JOY_POVX',
88 'JOY_POVY'
89 );
90
91 var
92 Window: TCastleWindow;
93 Notifications: TCastleNotifications;
94 ButtonReinitialize: TCastleButton;
95 OnScreenMenu: TCastleOnScreenMenu;
96 LabelJoysticksCount: TCastleLabel;
97 LabelSelectedJoystick: TCastleLabel;
98 SelectedJoystick: Integer = -1;
99 JoyButtons: array of TCastleButton;
100 JoyAxes: array of TCastleLabel;
101 JoyAxisVisualize: TJoyAxisVisualize;
102
103 { TEventsHandler ------------------------------------------------------------- }
104
105 type
106 TEventsHandler = class
107 strict private
108 class procedure ClearJoystickUI;
109 class procedure ClearSelectedJoystickUI;
110 public
111 class procedure InitializeJoystickUI(Sender: TObject);
112 class procedure JoystickDisconnected;
113 class procedure JoystickConnected;
114 class procedure ClickReinitialize(Sender: TObject);
115 class procedure ClickJoystickSelect(Sender: TObject);
116 class procedure JoyAxisMove(const Joy: TJoystick; const Axis: Byte; const Value: Single);
117 class procedure JoyButtonPress(const Joy: TJoystick; const Button: Byte);
118 class procedure JoyButtonUp(const Joy: TJoystick; const Button: Byte);
119 class procedure JoyButtonDown(const Joy: TJoystick; const Button: Byte);
120 end;
121
122 class procedure TEventsHandler.JoyAxisMove(const Joy: TJoystick; const Axis: Byte;
123 const Value: Single);
124 begin
125 // We show axes position only for the selected joystick.
126 if (SelectedJoystick = -1) or
127 (Joy <> Joysticks[SelectedJoystick]) then
128 Exit;
129
130 // If axes labels not initialized yet then exit.
131 if Length(JoyAxes) = 0 then Exit;
132
133 // Check whether Axis is in range, because some joysticks report Axis
134 // numbers outside of their declared Axis count,
135 // see https://github.com/castle-engine/castle-engine/issues/106 .
136 if Axis <= High(JoyAxes) then
137 JoyAxes[Axis].Caption := AxisNames[Axis] + ': ' + FloatToStrDot(Value);
138 end;
139
140 class procedure TEventsHandler.JoyButtonPress(const Joy: TJoystick; const Button: Byte);
141 begin
142 Notifications.Show(Format('Button press event (b: %d)', [Button]));
143 end;
144
145 class procedure TEventsHandler.JoyButtonUp(const Joy: TJoystick; const Button: Byte);
146 begin
147 if (SelectedJoystick = -1) or
148 (Joy <> Joysticks[SelectedJoystick]) then
149 Exit;
150 if Length(JoyButtons) = 0 then Exit;
151
152 JoyButtons[Button].Pressed := False;
153
154 Notifications.Show('Joy button up');
155 end;
156
157 class procedure TEventsHandler.JoyButtonDown(const Joy: TJoystick; const Button: Byte);
158 begin
159 if (SelectedJoystick = -1) or
160 (Joy <> Joysticks[SelectedJoystick]) then
161 Exit;
162 if Length(JoyButtons) = 0 then Exit;
163
164 JoyButtons[Button].Pressed := True;
165
166 Notifications.Show('Joy button down');
167 end;
168
169 class procedure TEventsHandler.ClickReinitialize(Sender: TObject);
170 begin
171 // If any joystick is plugged or unplugged then reinitialize is needed
172 Notifications.Show('Detecting joysticks again...');
173 Joysticks.Initialize;
174 end;
175
176 class procedure TEventsHandler.ClearSelectedJoystickUI;
177 var
178 I: Integer;
179 begin
180 for I := Low(JoyButtons) to High(JoyButtons) do
181 JoyButtons[i].Free;
182 SetLength(JoyButtons, 0);
183 for I := Low(JoyAxes) to High(JoyAxes) do
184 JoyAxes[i].Free;
185 SetLength(JoyAxes, 0);
186 FreeAndNil(JoyAxisVisualize);
187
188 SelectedJoystick := -1;
189 LabelSelectedJoystick.Caption := 'Selected: none';
190 end;
191
192 class procedure TEventsHandler.ClearJoystickUI;
193 begin
194 OnScreenMenu.MenuItems.ClearControls;
195 ClearSelectedJoystickUI;
196 end;
197
198 class procedure TEventsHandler.ClickJoystickSelect(Sender: TObject);
199 var
200 i: Integer;
201 begin
202 ClearSelectedJoystickUI;
203
204 // Show selected joystick details
205 SelectedJoystick := (Sender as TComponent).Tag;
206 LabelSelectedJoystick.Caption := Format('Selected: [%d] %s',
207 [SelectedJoystick, Joysticks.GetInfo(SelectedJoystick)^.Name]);
208
209 // Create array of buttons
210 SetLength(JoyButtons, Joysticks.GetInfo(SelectedJoystick)^.Count.Buttons);
211 for I := 0 to High(JoyButtons) do
212 begin
213 JoyButtons[i] := TCastleButton.Create(Application);
214 JoyButtons[i].Toggle := True;
215 JoyButtons[i].Enabled := False;
216 JoyButtons[i].Left := 10 + 45 * i;
217 JoyButtons[i].Bottom := 60;
218 JoyButtons[i].AutoSize := False;
219 JoyButtons[i].Width := 40;
220 JoyButtons[i].Height := 40;
221 JoyButtons[i].Caption := IntToStr(i);
222 Window.Controls.InsertFront(JoyButtons[i]);
223 end;
224 Notifications.Show(Format('Found %d buttons',
225 [Joysticks.GetInfo(SelectedJoystick)^.Count.Buttons]));
226
227 // Create axis labels
228 //SetLength(JoyAxes, Joysticks.GetInfo(SelectedJoystick)^.Count.Axes);
229 { Show all 8 possible axes, as D-Pad/POV axes are sometimes "further than Count.Axes"
230 This is a temporary measure until we can separate D-Pad/POV correctly in the backend }
231 SetLength(JoyAxes, 8);
232 for I := 0 to High(JoyAxes) do
233 begin
234 JoyAxes[i] := TCastleLabel.Create(Application);
235 JoyAxes[i].Left := 10;
236 JoyAxes[i].Bottom := 120 + i * 45;
237 JoyAxes[i].Caption := 'Axis: ' + IntToStr(i);
238 JoyAxes[i].Color := White;
239 Window.Controls.InsertFront(JoyAxes[i]);
240 end;
241 Notifications.Show(Format('Found %d axes',
242 [Joysticks.GetInfo(SelectedJoystick)^.Count.Axes]));
243
244 JoyAxisVisualize := TJoyAxisVisualize.Create(Application);
245 JoyAxisVisualize.Anchor(hpRight, -10);
246 JoyAxisVisualize.Anchor(vpTop, -10);
247 Window.Controls.InsertFront(JoyAxisVisualize);
248 end;
249
250 class procedure TEventsHandler.InitializeJoystickUI(Sender: TObject);
251 var
252 I: Integer;
253 JoystickSelect: TCastleOnScreenMenuItem;
254 begin
255 ClearJoystickUI;
256
257 LabelJoysticksCount.Caption := Format('Number of joysticks found: %d', [Joysticks.Count]);
258
259 // List all joysticks
260 for I := 0 to Joysticks.Count - 1 do
261 begin
262 JoystickSelect := TCastleOnScreenMenuItem.Create(Application);
263 JoystickSelect.Caption := Format('[%d] %s', [I, Joysticks.GetInfo(I)^.Name]);
264 JoystickSelect.OnClick := {$ifdef FPC}@{$endif} TEventsHandler {$ifdef FPC}(nil){$endif}.ClickJoystickSelect;
265 JoystickSelect.Tag := I;
266 OnScreenMenu.Add(JoystickSelect);
267 end;
268 end;
269
270 class procedure TEventsHandler.JoystickDisconnected;
271 begin
272 Joysticks.Initialize;
273 //will call OnChange and therefore InitializeJoystickUI
274 end;
275
276 class procedure TEventsHandler.JoystickConnected;
277 begin
278 Joysticks.Initialize;
279 //will call OnChange and therefore InitializeJoystickUI
280 end;
281
282 { other routines ------------------------------------------------------------- }
283
284 procedure WindowUpdate(Container: TCastleContainer);
285 begin
286 { update JoyAxisVisualize.Axis }
287 if SelectedJoystick <> -1 then
288 begin
289 Assert(JoyAxisVisualize <> nil);
290 JoyAxisVisualize.Axis := Joysticks[SelectedJoystick].Axis;
291 end;
292 end;
293
294 { One-time initialization of resources. }
295 procedure ApplicationInitialize;
296 var
297 MenuGroup: TCastleVerticalGroup;
298 begin
299 { Adjust container settings for a scalable UI (adjusts to any window size in a smart way). }
300 Window.Container.LoadSettings('castle-data:/CastleSettings.xml');
301
302 Window.Container.BackgroundColor := Green;
303 Window.OnUpdate := @WindowUpdate;
304
305 Notifications := TCastleNotifications.Create(Application);
306 Notifications.Anchor(vpBottom, 10);
307 Notifications.Anchor(hpMiddle);
308 Notifications.TextAlignment := hpMiddle;
309 Notifications.Timeout := 2.0;
310 Notifications.Fade := 0.5;
311 Window.Controls.InsertBack(Notifications);
312
313 ButtonReinitialize := TCastleButton.Create(Application);
314 ButtonReinitialize.Caption := 'Detect connected joysticks again (Joysticks.Initialize)';
315 ButtonReinitialize.Left := 10;
316 ButtonReinitialize.Bottom := 10;
317 ButtonReinitialize.OnClick := {$ifdef FPC}@{$endif} TEventsHandler {$ifdef FPC}(nil){$endif}.ClickReinitialize;
318 Window.Controls.InsertFront(ButtonReinitialize);
319
320 MenuGroup := TCastleVerticalGroup.Create(Application);
321 MenuGroup.Left := 10;
322 MenuGroup.Anchor(vpTop, -10);
323 MenuGroup.Spacing := 10;
324 Window.Controls.InsertFront(MenuGroup);
325
326 LabelJoysticksCount := TCastleLabel.Create(Application);
327 LabelJoysticksCount.Color := White;
328 MenuGroup.InsertFront(LabelJoysticksCount);
329
330 OnScreenMenu := TCastleOnScreenMenu.Create(Application);
331 MenuGroup.InsertFront(OnScreenMenu);
332
333 LabelSelectedJoystick := TCastleLabel.Create(Application);
334 LabelSelectedJoystick.Color := White;
335 LabelSelectedJoystick.Caption := 'Selected: none';
336 MenuGroup.InsertFront(LabelSelectedJoystick);
337
338 Joysticks.OnChange := {$ifdef FPC}@{$endif} TEventsHandler {$ifdef FPC}(nil){$endif}.InitializeJoystickUI;
339 Joysticks.OnDisconnect := {$ifdef FPC}@{$endif} TEventsHandler {$ifdef FPC}(nil){$endif}.JoystickDisconnected;
340 Joysticks.OnConnect := {$ifdef FPC}@{$endif} TEventsHandler {$ifdef FPC}(nil){$endif}.JoystickConnected;
341 Joysticks.OnAxisMove := {$ifdef FPC}@{$endif} TEventsHandler {$ifdef FPC}(nil){$endif}.JoyAxisMove;
342 Joysticks.OnButtonDown := {$ifdef FPC}@{$endif} TEventsHandler {$ifdef FPC}(nil){$endif}.JoyButtonDown;
343 Joysticks.OnButtonPress := {$ifdef FPC}@{$endif} TEventsHandler {$ifdef FPC}(nil){$endif}.JoyButtonPress;
344 Joysticks.OnButtonUp := {$ifdef FPC}@{$endif} TEventsHandler {$ifdef FPC}(nil){$endif}.JoyButtonUp;
345
346 { Actually detect joysticks.
347 This will automatically call TEventsHandler.JoysticksChanged on some platforms. }
348 Joysticks.Initialize;
349 end;
350
351 initialization
352 { This initialization section configures:
353 - Application.OnInitialize
354 - Application.MainWindow
355 - determines initial window size
356
357 You should not need to do anything more in this initialization section.
358 Most of your actual application initialization (in particular, any file reading)
359 should happen inside ApplicationInitialize. }
360
361 Application.OnInitialize := @ApplicationInitialize;
362
363 Window := TCastleWindow.Create(Application);
364 Application.MainWindow := Window;
365
366 { Optionally, adjust window fullscreen state and size at this point.
367 Examples:
368
369 Run fullscreen:
370
371 Window.FullScreen := true;
372
373 Run in a 600x400 window:
374
375 Window.FullScreen := false; // default
376 Window.Width := 600;
377 Window.Height := 400;
378
379 Run in a window taking 2/3 of screen (width and height):
380
381 Window.FullScreen := false; // default
382 Window.Width := Application.ScreenWidth * 2 div 3;
383 Window.Height := Application.ScreenHeight * 2 div 3;
384
385 Note that some platforms (like mobile) ignore these window sizes.
386 }
387
388 { Handle command-line parameters like --fullscreen and --window.
389 By doing this last, you let user to override your fullscreen / mode setup. }
390 Window.ParseParameters;
391 end.