| 1 |
{ |
| 2 |
Copyright 2022-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, CastleViewport, |
| 24 |
CastleTransform; |
| 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 |
ButtonHinge: TCastleButton; |
| 34 |
ButtonHingePrettier: TCastleButton; |
| 35 |
ButtonBall: TCastleButton; |
| 36 |
ButtonGrab: TCastleButton; |
| 37 |
ButtonRope: TCastleButton; |
| 38 |
ButtonDistance: TCastleButton; |
| 39 |
DesignContent: TCastleDesign; |
| 40 |
private |
| 41 |
// Only defined during Grab demo |
| 42 |
InteractiveGrabJoint: TCastleGrabJoint; |
| 43 |
ViewportGrab: TCastleViewport; |
| 44 |
|
| 45 |
procedure ClickButtonHinge(Sender: TObject); |
| 46 |
procedure ClickButtonHingePrettier(Sender: TObject); |
| 47 |
procedure ClickButtonBall(Sender: TObject); |
| 48 |
procedure ClickButtonGrab(Sender: TObject); |
| 49 |
procedure ClickButtonRope(Sender: TObject); |
| 50 |
procedure ClickButtonDistance(Sender: TObject); |
| 51 |
procedure DesignChanged; |
| 52 |
procedure UpdateGrabPosition(const EventPosition: TVector2); |
| 53 |
public |
| 54 |
constructor Create(AOwner: TComponent); override; |
| 55 |
procedure Start; override; |
| 56 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 57 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 58 |
function Motion(const Event: TInputMotion): Boolean; override; |
| 59 |
end; |
| 60 |
|
| 61 |
var |
| 62 |
ViewMain: TViewMain; |
| 63 |
|
| 64 |
implementation |
| 65 |
|
| 66 |
uses SysUtils, |
| 67 |
CastleLog; |
| 68 |
|
| 69 |
{ TViewMain ----------------------------------------------------------------- } |
| 70 |
|
| 71 |
constructor TViewMain.Create(AOwner: TComponent); |
| 72 |
begin |
| 73 |
inherited; |
| 74 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 75 |
end; |
| 76 |
|
| 77 |
procedure TViewMain.Start; |
| 78 |
begin |
| 79 |
inherited; |
| 80 |
|
| 81 |
ButtonHinge.OnClick := {$ifdef FPC}@{$endif} ClickButtonHinge; |
| 82 |
ButtonHingePrettier.OnClick := {$ifdef FPC}@{$endif} ClickButtonHingePrettier; |
| 83 |
ButtonBall.OnClick := {$ifdef FPC}@{$endif} ClickButtonBall; |
| 84 |
ButtonGrab.OnClick := {$ifdef FPC}@{$endif} ClickButtonGrab; |
| 85 |
ButtonRope.OnClick := {$ifdef FPC}@{$endif} ClickButtonRope; |
| 86 |
ButtonDistance.OnClick := {$ifdef FPC}@{$endif} ClickButtonDistance; |
| 87 |
end; |
| 88 |
|
| 89 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 90 |
begin |
| 91 |
inherited; |
| 92 |
{ This virtual method is executed every frame (many times per second). } |
| 93 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 94 |
end; |
| 95 |
|
| 96 |
procedure TViewMain.DesignChanged; |
| 97 |
begin |
| 98 |
// These will be defined only in Grab demo, nil otherwise |
| 99 |
ViewportGrab := DesignContent.DesignedComponent('ViewportGrab', false) as TCastleViewport; |
| 100 |
InteractiveGrabJoint := DesignContent.DesignedComponent('InteractiveGrabJoint', false) as TCastleGrabJoint; |
| 101 |
end; |
| 102 |
|
| 103 |
procedure TViewMain.ClickButtonHinge(Sender: TObject); |
| 104 |
begin |
| 105 |
{ Each joint demo is in a separate file, and we switch between them |
| 106 |
by changing DesignContent.Url. |
| 107 |
|
| 108 |
This is in contrast to another solution: |
| 109 |
having multiple controls all in the gameviewmain.castle-user-interface |
| 110 |
design and switching their existence like ViewporHinge.Exists := true/false. |
| 111 |
|
| 112 |
Using the DesignContent.Url is better in this case because: |
| 113 |
|
| 114 |
- It means that each change of demo restarts this demo |
| 115 |
(e.g. go to "Hinge", then "Ball", then "Hinge" again -- the "Hinge" |
| 116 |
demo will start again). This is desirable in case of this demo. |
| 117 |
|
| 118 |
- It's a bit easier to design and test in CGE editor: |
| 119 |
each joint demo is separate, must use separate components, |
| 120 |
and you can run the simulation of it. |
| 121 |
} |
| 122 |
DesignContent.Url := 'castle-data:/viewport_hinge.castle-user-interface'; |
| 123 |
DesignChanged; |
| 124 |
end; |
| 125 |
|
| 126 |
procedure TViewMain.ClickButtonHingePrettier(Sender: TObject); |
| 127 |
begin |
| 128 |
DesignContent.Url := 'castle-data:/viewport_hinge_prettier.castle-user-interface'; |
| 129 |
DesignChanged; |
| 130 |
end; |
| 131 |
|
| 132 |
procedure TViewMain.ClickButtonBall(Sender: TObject); |
| 133 |
begin |
| 134 |
DesignContent.Url := 'castle-data:/viewport_ball.castle-user-interface'; |
| 135 |
DesignChanged; |
| 136 |
end; |
| 137 |
|
| 138 |
procedure TViewMain.ClickButtonGrab(Sender: TObject); |
| 139 |
begin |
| 140 |
DesignContent.Url := 'castle-data:/viewport_grab.castle-user-interface'; |
| 141 |
DesignChanged; |
| 142 |
end; |
| 143 |
|
| 144 |
procedure TViewMain.ClickButtonRope(Sender: TObject); |
| 145 |
begin |
| 146 |
DesignContent.Url := 'castle-data:/viewport_rope.castle-user-interface'; |
| 147 |
DesignChanged; |
| 148 |
end; |
| 149 |
|
| 150 |
procedure TViewMain.ClickButtonDistance(Sender: TObject); |
| 151 |
begin |
| 152 |
DesignContent.Url := 'castle-data:/viewport_distance.castle-user-interface'; |
| 153 |
DesignChanged; |
| 154 |
end; |
| 155 |
|
| 156 |
procedure TViewMain.UpdateGrabPosition(const EventPosition: TVector2); |
| 157 |
var |
| 158 |
PickedPoint: TVector3; |
| 159 |
begin |
| 160 |
if InteractiveGrabJoint <> nil then |
| 161 |
begin |
| 162 |
if ViewportGrab.PositionToWorldPlane(EventPosition, true, 1, 0, PickedPoint) then |
| 163 |
begin |
| 164 |
//WritelnLog('Grabbing to point %s', [PickedPoint.ToString]); |
| 165 |
InteractiveGrabJoint.TargetWorld := PickedPoint; |
| 166 |
end; |
| 167 |
end; |
| 168 |
end; |
| 169 |
|
| 170 |
function TViewMain.Press(const Event: TInputPressRelease): Boolean; |
| 171 |
begin |
| 172 |
Result := inherited; |
| 173 |
if Result then Exit; // allow the ancestor to handle keys |
| 174 |
|
| 175 |
if Event.IsMouseButton(buttonLeft) and (mkShift in Event.ModifiersDown) then |
| 176 |
begin |
| 177 |
UpdateGrabPosition(Event.Position); |
| 178 |
Exit(true); |
| 179 |
end; |
| 180 |
end; |
| 181 |
|
| 182 |
function TViewMain.Motion(const Event: TInputMotion): Boolean; |
| 183 |
begin |
| 184 |
Result := inherited; |
| 185 |
if Result then Exit; // allow the ancestor to handle keys |
| 186 |
|
| 187 |
if (buttonLeft in Event.Pressed) and (mkShift in Container.Pressed.Modifiers) then |
| 188 |
begin |
| 189 |
UpdateGrabPosition(Event.Position); |
| 190 |
Exit(true); |
| 191 |
end; |
| 192 |
end; |
| 193 |
|
| 194 |
end. |