| 1 |
{ Main view, where most of the application logic takes place. |
| 2 |
|
| 3 |
Feel free to use this code as a starting point for your own projects. |
| 4 |
This template code is in public domain, unlike most other CGE code which |
| 5 |
is covered by BSD or LGPL (see https://castle-engine.io/license). } |
| 6 |
unit GameView${MAIN_VIEW}; |
| 7 |
|
| 8 |
interface |
| 9 |
|
| 10 |
uses Classes, |
| 11 |
CastleVectors, CastleComponentSerialize, |
| 12 |
CastleUIControls, CastleControls, CastleKeysMouse; |
| 13 |
|
| 14 |
type |
| 15 |
{ Main view, where most of the application logic takes place. } |
| 16 |
TView${MAIN_VIEW} = class(TCastleView) |
| 17 |
published |
| 18 |
{ Components designed using CGE editor. |
| 19 |
These fields will be automatically initialized at Start. } |
| 20 |
LabelFps: TCastleLabel; |
| 21 |
public |
| 22 |
constructor Create(AOwner: TComponent); override; |
| 23 |
procedure Start; override; |
| 24 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 25 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 26 |
end; |
| 27 |
|
| 28 |
var |
| 29 |
View${MAIN_VIEW}: TView${MAIN_VIEW}; |
| 30 |
|
| 31 |
implementation |
| 32 |
|
| 33 |
uses SysUtils; |
| 34 |
|
| 35 |
{ TView${MAIN_VIEW} ----------------------------------------------------------------- } |
| 36 |
|
| 37 |
constructor TView${MAIN_VIEW}.Create(AOwner: TComponent); |
| 38 |
begin |
| 39 |
inherited; |
| 40 |
DesignUrl := 'castle-data:/gameview${MAIN_VIEW_LOWERCASE}.castle-user-interface'; |
| 41 |
end; |
| 42 |
|
| 43 |
procedure TView${MAIN_VIEW}.Start; |
| 44 |
begin |
| 45 |
inherited; |
| 46 |
end; |
| 47 |
|
| 48 |
procedure TView${MAIN_VIEW}.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 49 |
begin |
| 50 |
inherited; |
| 51 |
{ This virtual method is executed every frame (many times per second). } |
| 52 |
Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code'); |
| 53 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 54 |
end; |
| 55 |
|
| 56 |
function TView${MAIN_VIEW}.Press(const Event: TInputPressRelease): Boolean; |
| 57 |
begin |
| 58 |
Result := inherited; |
| 59 |
if Result then Exit; // allow the ancestor to handle keys |
| 60 |
|
| 61 |
{ This virtual method is executed when user presses |
| 62 |
a key, a mouse button, or touches a touch-screen. |
| 63 |
|
| 64 |
Note that each UI control has also events like OnPress and OnClick. |
| 65 |
These events can be used to handle the "press", if it should do something |
| 66 |
specific when used in that UI control. |
| 67 |
The TView${MAIN_VIEW}.Press method should be used to handle keys |
| 68 |
not handled in children controls. |
| 69 |
} |
| 70 |
|
| 71 |
// Use this to handle keys: |
| 72 |
{ |
| 73 |
if Event.IsKey(keyXxx) then |
| 74 |
begin |
| 75 |
// DoSomething; |
| 76 |
Exit(true); // key was handled |
| 77 |
end; |
| 78 |
} |
| 79 |
end; |
| 80 |
|
| 81 |
end. |