| 1 |
{ |
| 2 |
Copyright 2022-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 |
{ Main view, where most of the application logic takes place. } |
| 16 |
unit GameViewMain; |
| 17 |
|
| 18 |
interface |
| 19 |
|
| 20 |
uses Classes, |
| 21 |
CastleVectors, CastleComponentSerialize, |
| 22 |
CastleUIControls, CastleControls, CastleKeysMouse, CastleViewport, CastleScene; |
| 23 |
|
| 24 |
type |
| 25 |
{ Main view, where most of the application logic takes place. } |
| 26 |
TViewMain = class(TCastleView) |
| 27 |
published |
| 28 |
{ Components designed using CGE editor. |
| 29 |
These fields will be automatically initialized at Start. } |
| 30 |
LabelFps: TCastleLabel; |
| 31 |
MainViewport: TCastleViewport; |
| 32 |
MainSphere: TCastleSphere; |
| 33 |
TextAttached: TCastleText; |
| 34 |
LabelAttached: TCastleLabel; |
| 35 |
public |
| 36 |
constructor Create(AOwner: TComponent); override; |
| 37 |
procedure Start; override; |
| 38 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 39 |
end; |
| 40 |
|
| 41 |
var |
| 42 |
ViewMain: TViewMain; |
| 43 |
|
| 44 |
implementation |
| 45 |
|
| 46 |
uses SysUtils, |
| 47 |
CastleLog; |
| 48 |
|
| 49 |
{ TViewMain ----------------------------------------------------------------- } |
| 50 |
|
| 51 |
constructor TViewMain.Create(AOwner: TComponent); |
| 52 |
begin |
| 53 |
inherited; |
| 54 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 55 |
end; |
| 56 |
|
| 57 |
procedure TViewMain.Start; |
| 58 |
begin |
| 59 |
inherited; |
| 60 |
end; |
| 61 |
|
| 62 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 63 |
var |
| 64 |
SphereWorldPosition, SphereWorldPosition1: TVector3; |
| 65 |
SphereViewportPosition, SphereViewportPosition1: TVector2; |
| 66 |
Sphere1UnitSizeOnScreen, TextAttachedScale: Single; |
| 67 |
begin |
| 68 |
inherited; |
| 69 |
{ This virtual method is executed every frame (many times per second). } |
| 70 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 71 |
|
| 72 |
{ calculate SphereViewportPosition, position of the sphere middle in 2D viewport coordinates. } |
| 73 |
SphereWorldPosition := MainSphere.LocalToWorld(Vector3(0, 0, 0)); |
| 74 |
SphereViewportPosition := MainViewport.PositionFromWorld(SphereWorldPosition); |
| 75 |
|
| 76 |
{ SphereViewportPosition is immediately useful to determine LabelAttached position } |
| 77 |
LabelAttached.Translation := SphereViewportPosition; |
| 78 |
|
| 79 |
{ calculate Sphere1UnitSizeOnScreen, how large is 1 unit in 3D space around sphere, on the screen. } |
| 80 |
SphereWorldPosition1 := SphereWorldPosition + Vector3(0, 1, 0); |
| 81 |
SphereViewportPosition1 := MainViewport.PositionFromWorld(SphereWorldPosition1); |
| 82 |
Sphere1UnitSizeOnScreen := PointsDistance(SphereViewportPosition, SphereViewportPosition1); |
| 83 |
|
| 84 |
{ use Sphere1UnitSizeOnScreen to keep the TextAttached size similar on screen, |
| 85 |
regardless of how far is the scene. } |
| 86 |
TextAttachedScale := 100 / Sphere1UnitSizeOnScreen; |
| 87 |
TextAttached.Scale := Vector3(TextAttachedScale, TextAttachedScale, TextAttachedScale); |
| 88 |
end; |
| 89 |
|
| 90 |
end. |