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