Debian Codegraph @

examples/viewport_and_scenes/deprecated_x3d_call_pascal_code/code/gameviewmain.pas

raw
1 {
2 Copyright 2003-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,
22 CastleUIControls, CastleControls, CastleKeysMouse, CastleScene,
23 X3DNodes, X3DFields, X3DTime;
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 MainScene: TCastleScene;
33 private
34 procedure ScriptTouchInitialize(Value: TX3DField; const Time: TX3DTime);
35 procedure ScriptTouch(Value: TX3DField; const Time: TX3DTime);
36 public
37 constructor Create(AOwner: TComponent); override;
38 procedure Start; override;
39 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
40 function Press(const Event: TInputPressRelease): Boolean; override;
41 end;
42
43 var
44 ViewMain: TViewMain;
45
46 implementation
47
48 uses SysUtils,
49 CastleLog;
50
51 { TViewMain ----------------------------------------------------------------- }
52
53 constructor TViewMain.Create(AOwner: TComponent);
54 begin
55 inherited;
56 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
57 end;
58
59 procedure TViewMain.Start;
60 begin
61 inherited;
62
63 { initialize events procesing }
64 MainScene.RegisterCompiledScript('touch_initialize', {$ifdef FPC}@{$endif} ScriptTouchInitialize);
65 MainScene.RegisterCompiledScript('touch', {$ifdef FPC}@{$endif} ScriptTouch);
66
67 { initialize ProcessEvents *after* attaching ScriptTouchInitialize, to receive it }
68 MainScene.ProcessEvents := true;
69 end;
70
71 procedure TViewMain.ScriptTouchInitialize(Value: TX3DField; const Time: TX3DTime);
72 begin
73 WritelnLog(Format('Script is initialized (absolute time: %f, time since load: %f)', [
74 Time.Seconds,
75 Time.Seconds - MainScene.TimeAtLoad
76 ]));
77 end;
78
79 procedure TViewMain.ScriptTouch(Value: TX3DField; const Time: TX3DTime);
80 begin
81 WritelnLog(Format('Touch! (absolute time: %f, time since load: %f)', [
82 Time.Seconds,
83 Time.Seconds - MainScene.TimeAtLoad
84 ]));
85 end;
86
87 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
88 begin
89 inherited;
90 { This virtual method is executed every frame (many times per second). }
91 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
92 end;
93
94 function TViewMain.Press(const Event: TInputPressRelease): Boolean;
95 begin
96 Result := inherited;
97 if Result then Exit; // allow the ancestor to handle keys
98
99 { This virtual method is executed when user presses
100 a key, a mouse button, or touches a touch-screen.
101
102 Note that each UI control has also events like OnPress and OnClick.
103 These events can be used to handle the "press", if it should do something
104 specific when used in that UI control.
105 The TViewMain.Press method should be used to handle keys
106 not handled in children controls.
107 }
108
109 // Use this to handle keys:
110 {
111 if Event.IsKey(keyXxx) then
112 begin
113 // DoSomething;
114 Exit(true); // key was handled
115 end;
116 }
117 end;
118
119 end.