| 1 |
{ |
| 2 |
Copyright 2022-2023 Michalis Kamburelis, Andrzej Kilijański. |
| 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 |
|
| 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 |
{ Components designed using CGE editor, loaded from gameviewmain.castle-user-interface. } |
| 32 |
LabelFps: TCastleLabel; |
| 33 |
ButtonMoveSphere: TCastleButton; |
| 34 |
Sphere: TCastleTransform; |
| 35 |
private |
| 36 |
procedure ButtonMoveSphereClick(Sender: TObject); |
| 37 |
public |
| 38 |
constructor Create(AOwner: TComponent); override; |
| 39 |
procedure Start; override; |
| 40 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 41 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 42 |
end; |
| 43 |
|
| 44 |
var |
| 45 |
ViewMain: TViewMain; |
| 46 |
|
| 47 |
implementation |
| 48 |
|
| 49 |
uses SysUtils; |
| 50 |
|
| 51 |
{ TViewMain ----------------------------------------------------------------- } |
| 52 |
|
| 53 |
procedure TViewMain.ButtonMoveSphereClick(Sender: TObject); |
| 54 |
begin |
| 55 |
Sphere.Translation := Vector3(300, 500, 0); |
| 56 |
end; |
| 57 |
|
| 58 |
constructor TViewMain.Create(AOwner: TComponent); |
| 59 |
begin |
| 60 |
inherited; |
| 61 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 62 |
end; |
| 63 |
|
| 64 |
procedure TViewMain.Start; |
| 65 |
begin |
| 66 |
inherited; |
| 67 |
ButtonMoveSphere.OnClick := {$ifdef FPC}@{$endif}ButtonMoveSphereClick; |
| 68 |
end; |
| 69 |
|
| 70 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 71 |
begin |
| 72 |
inherited; |
| 73 |
{ This virtual method is executed every frame (many times per second). } |
| 74 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 75 |
end; |
| 76 |
|
| 77 |
function TViewMain.Press(const Event: TInputPressRelease): Boolean; |
| 78 |
begin |
| 79 |
Result := inherited; |
| 80 |
if Result then Exit; // allow the ancestor to handle keys |
| 81 |
|
| 82 |
{ This virtual method is executed when user presses |
| 83 |
a key, a mouse button, or touches a touch-screen. |
| 84 |
|
| 85 |
Note that each UI control has also events like OnPress and OnClick. |
| 86 |
These events can be used to handle the "press", if it should do something |
| 87 |
specific when used in that UI control. |
| 88 |
The TViewMain.Press method should be used to handle keys |
| 89 |
not handled in children controls. |
| 90 |
} |
| 91 |
|
| 92 |
// Use this to handle keys: |
| 93 |
{ |
| 94 |
if Event.IsKey(keyXxx) then |
| 95 |
begin |
| 96 |
// DoSomething; |
| 97 |
Exit(true); // key was handled |
| 98 |
end; |
| 99 |
} |
| 100 |
end; |
| 101 |
|
| 102 |
end. |