Debian Codegraph @

examples/viewport_and_scenes/detect_scene_hit/code/gameviewmain.pas

raw
1 {
2 Copyright 2021-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 CastleVectors, CastleComponentSerialize,
23 CastleUIControls, CastleControls, CastleKeysMouse, CastleViewport;
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 LabelSceneUnderMouse: TCastleLabel;
33 LabelSceneClicked: TCastleLabel;
34 MainViewport: TCastleViewport;
35 public
36 constructor Create(AOwner: TComponent); override;
37 procedure Start; override;
38 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
39 function Press(const Event: TInputPressRelease): Boolean; override;
40 end;
41
42 var
43 ViewMain: TViewMain;
44
45 implementation
46
47 uses SysUtils;
48
49 { TViewMain ----------------------------------------------------------------- }
50
51 constructor TViewMain.Create(AOwner: TComponent);
52 begin
53 inherited;
54 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
55 end;
56
57 procedure TViewMain.Start;
58 begin
59 inherited;
60 end;
61
62 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
63 var
64 SceneName: String;
65 begin
66 inherited;
67 { This virtual method is executed every frame (many times per second). }
68 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
69
70 if MainViewport.TransformUnderMouse <> nil then
71 SceneName := MainViewport.TransformUnderMouse.Name
72 else
73 SceneName := 'none';
74 LabelSceneUnderMouse.Caption := 'Scene Under Mouse/Touch Position: ' + SceneName;
75 end;
76
77 function TViewMain.Press(const Event: TInputPressRelease): Boolean;
78 var
79 SceneName: String;
80 begin
81 Result := inherited;
82 if Result then Exit; // allow the ancestor to handle keys
83
84 { This virtual method is executed when user presses
85 a key, a mouse button, or touches a touch-screen.
86
87 Note that each UI control has also events like OnPress and OnClick.
88 These events can be used to handle the "press", if it should do something
89 specific when used in that UI control.
90 The TViewMain.Press method should be used to handle keys
91 not handled in children controls.
92 }
93
94 if Event.IsMouseButton(buttonLeft) then
95 begin
96 if MainViewport.TransformUnderMouse <> nil then
97 SceneName := MainViewport.TransformUnderMouse.Name
98 else
99 SceneName := 'none';
100 LabelSceneClicked.Caption := 'Scene Last Clicked/Touched: ' + SceneName;
101 Exit(true); // event was handled
102 end;
103 end;
104
105 end.