Debian Codegraph @

examples/window/multiple_windows_and_viewports/code/gameviewmain.pas

raw
1 {
2 Copyright 2023-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, CastleTransform,
24 CastleViewport, CastleCameras;
25
26 type
27 { Main view, where most of the application logic takes place. }
28 TViewMain = class(TCastleView)
29 published
30 { Components designed using CGE editor.
31 These fields will be automatically initialized at Start. }
32 LabelFps: TCastleLabel;
33 ViewportTop, ViewportBottom: TCastleViewport;
34 WalkNavigationTop, WalkNavigationBottom: TCastleWalkNavigation;
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 ViewMainInWindow1: TViewMain;
44 ViewMainInWindow2: TViewMain;
45
46 implementation
47
48 uses SysUtils;
49
50 { TViewMain ----------------------------------------------------------------- }
51
52 constructor TViewMain.Create(AOwner: TComponent);
53 begin
54 inherited;
55 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
56 end;
57
58 procedure TViewMain.Start;
59 begin
60 inherited;
61 end;
62
63 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
64 begin
65 inherited;
66 { This virtual method is executed every frame (many times per second). }
67 Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code');
68 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
69
70 WalkNavigationBottom.MouseLook :=
71 (buttonRight in Container.MousePressed) and ViewportBottom.Focused;
72 WalkNavigationTop.MouseLook :=
73 (buttonRight in Container.MousePressed) and ViewportTop.Focused;
74 end;
75
76 function TViewMain.Press(const Event: TInputPressRelease): Boolean;
77
78 procedure DropFromViewport(const Viewport: TCastleViewport);
79 var
80 Box: TCastleTransform;
81 CamPos, CamDir, CamUp: TVector3;
82 begin
83 Box := TransformLoad('castle-data:/drop_box.castle-transform', FreeAtStop);
84 Viewport.Camera.GetWorldView(CamPos, CamDir, CamUp);
85 Box.Translation := CamPos + CamDir * 10.0;
86 Box.Direction := CamDir;
87 Viewport.Items.Add(Box);
88 end;
89
90 begin
91 Result := inherited;
92 if Result then Exit; // allow the ancestor to handle keys
93
94 { We check Container.Focused and viewport's Focused,
95 to only drop from 1 viewport in focused window. }
96 if Event.IsKey(keyEnter) and Container.Focused then
97 begin
98 if ViewportBottom.Focused then
99 DropFromViewport(ViewportBottom)
100 else
101 if ViewportTop.Focused then
102 DropFromViewport(ViewportTop);
103 Exit(true); // key was handled
104 end;
105 end;
106
107 end.