Debian Codegraph @

examples/physics/physics_joints_2d_grab/code/gameviewmain.pas

raw
1 {
2 Copyright 2020-2022 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, 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 GrabJoint: TCastleGrabJoint;
33 Viewport: TCastleViewport;
34 public
35 constructor Create(AOwner: TComponent); override;
36 procedure Start; override;
37 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
38 function Press(const Event: TInputPressRelease): Boolean; override;
39 end;
40
41 var
42 ViewMain: TViewMain;
43
44 implementation
45
46 uses SysUtils, CastleLog;
47
48 { TViewMain ----------------------------------------------------------------- }
49
50 constructor TViewMain.Create(AOwner: TComponent);
51 begin
52 inherited;
53 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
54 end;
55
56 procedure TViewMain.Start;
57 begin
58 inherited;
59 end;
60
61 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
62 begin
63 inherited;
64 { This virtual method is executed every frame (many times per second). }
65 Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code');
66 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
67
68 if Container.MousePressed = [buttonLeft] then
69 GrabJoint.TargetWorld := Vector3(Viewport.PositionTo2DWorld(Container.MousePosition, true), 0);
70
71 end;
72
73 function TViewMain.Press(const Event: TInputPressRelease): Boolean;
74 begin
75 Result := inherited;
76 if Result then Exit; // allow the ancestor to handle keys
77
78 if Event.IsMouseButton(buttonLeft) then
79 GrabJoint.TargetWorld := Vector3(Viewport.PositionTo2DWorld(Event.Position, true), 0);
80 end;
81
82 end.