| 1 |
{ |
| 2 |
Copyright 2023-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, CastleTransform, CastleTiledMap, |
| 23 |
CastleUIControls, CastleControls, CastleKeysMouse, CastleScene; |
| 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 |
TiledMap: TCastleTiledMap; |
| 33 |
ButtonOpen, ButtonAnimations: TCastleButton; |
| 34 |
CheckboxSmoothScaling, CheckboxSmoothScalingSafeBorder: TCastleCheckbox; |
| 35 |
MapCamera: TCastleCamera; |
| 36 |
private |
| 37 |
TiledAnimations: Boolean; |
| 38 |
procedure ClickOpen(Sender: TObject); |
| 39 |
procedure ClickAnimations(Sender: TObject); |
| 40 |
procedure CheckboxSmoothScalingChange(Sender: TObject); |
| 41 |
procedure CheckboxSmoothScalingSafeBorderChange(Sender: TObject); |
| 42 |
procedure OpenMap(const MapUrl: String); |
| 43 |
public |
| 44 |
constructor Create(AOwner: TComponent); override; |
| 45 |
procedure Start; override; |
| 46 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 47 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 48 |
end; |
| 49 |
|
| 50 |
var |
| 51 |
ViewMain: TViewMain; |
| 52 |
|
| 53 |
implementation |
| 54 |
|
| 55 |
uses SysUtils, |
| 56 |
CastleParameters, CastleRenderOptions, CastleWindow, CastleUriUtils; |
| 57 |
|
| 58 |
{ TViewMain ----------------------------------------------------------------- } |
| 59 |
|
| 60 |
constructor TViewMain.Create(AOwner: TComponent); |
| 61 |
begin |
| 62 |
inherited; |
| 63 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 64 |
end; |
| 65 |
|
| 66 |
procedure TViewMain.Start; |
| 67 |
begin |
| 68 |
inherited; |
| 69 |
|
| 70 |
{ Assign events } |
| 71 |
ButtonOpen.OnClick := {$ifdef FPC}@{$endif} ClickOpen; |
| 72 |
ButtonAnimations.OnClick := {$ifdef FPC}@{$endif} ClickAnimations; |
| 73 |
CheckboxSmoothScaling.OnChange := {$ifdef FPC}@{$endif} CheckboxSmoothScalingChange; |
| 74 |
CheckboxSmoothScalingSafeBorder.OnChange := {$ifdef FPC}@{$endif} CheckboxSmoothScalingSafeBorderChange; |
| 75 |
|
| 76 |
{ Synchronize initial checkbox state with map properties } |
| 77 |
CheckboxSmoothScaling.Checked := TiledMap.SmoothScaling ; |
| 78 |
CheckboxSmoothScalingSafeBorder.Checked := TiledMap.SmoothScalingSafeBorder; |
| 79 |
|
| 80 |
{ Load the map from parameter or default. } |
| 81 |
if Parameters.High = 1 then |
| 82 |
OpenMap(Parameters[1]) |
| 83 |
else |
| 84 |
OpenMap('castle-data:/maps/desert.tmx'); |
| 85 |
end; |
| 86 |
|
| 87 |
procedure TViewMain.OpenMap(const MapUrl: String); |
| 88 |
begin |
| 89 |
TiledMap.Url := MapUrl; |
| 90 |
MapCamera.Translation := TVector3.Zero; |
| 91 |
MapCamera.Orthographic.Height := 1000; // resets zoom in/out |
| 92 |
|
| 93 |
TiledAnimations := true; |
| 94 |
ButtonAnimations.Caption := 'Stop Animations'; |
| 95 |
ButtonAnimations.Enabled := TiledMap.HasAnimations; |
| 96 |
end; |
| 97 |
|
| 98 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 99 |
begin |
| 100 |
inherited; |
| 101 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 102 |
end; |
| 103 |
|
| 104 |
procedure TViewMain.ClickOpen(Sender: TObject); |
| 105 |
var |
| 106 |
Url: String; |
| 107 |
begin |
| 108 |
Url := TiledMap.Url; |
| 109 |
if Application.MainWindow.FileDialog('Open Map', Url, true, 'Tiled Map (*.tmx)|*.tmx|All Files|*') then |
| 110 |
OpenMap(Url); |
| 111 |
end; |
| 112 |
|
| 113 |
procedure TViewMain.ClickAnimations(Sender: TObject); |
| 114 |
begin |
| 115 |
TiledAnimations := not TiledAnimations; |
| 116 |
if TiledAnimations then |
| 117 |
begin |
| 118 |
TiledMap.PlayAnimations; |
| 119 |
ButtonAnimations.Caption := 'Stop Animations'; |
| 120 |
end else |
| 121 |
begin |
| 122 |
TiledMap.StopAnimations(false); |
| 123 |
ButtonAnimations.Caption := 'Play Animations'; |
| 124 |
end; |
| 125 |
end; |
| 126 |
|
| 127 |
procedure TViewMain.CheckboxSmoothScalingChange(Sender: TObject); |
| 128 |
begin |
| 129 |
TiledMap.SmoothScaling := CheckboxSmoothScaling.Checked; |
| 130 |
end; |
| 131 |
|
| 132 |
procedure TViewMain.CheckboxSmoothScalingSafeBorderChange(Sender: TObject); |
| 133 |
begin |
| 134 |
TiledMap.SmoothScalingSafeBorder := CheckboxSmoothScalingSafeBorder.Checked; |
| 135 |
end; |
| 136 |
|
| 137 |
function TViewMain.Press(const Event: TInputPressRelease): Boolean; |
| 138 |
begin |
| 139 |
Result := inherited; |
| 140 |
if Result then Exit; |
| 141 |
|
| 142 |
{ TODO: Simple test of TiledMap.Data.Layers[xxx].Exists. |
| 143 |
See https://castle-engine.io/roadmap#tiled_layers for plans. } |
| 144 |
if Event.IsKey(key0) then |
| 145 |
TiledMap.Data.Layers[0].Exists := not TiledMap.Data.Layers[0].Exists; |
| 146 |
if Event.IsKey(key1) then |
| 147 |
if TiledMap.Data.Layers.Count > 1 then |
| 148 |
TiledMap.Data.Layers[1].Exists := not TiledMap.Data.Layers[1].Exists; |
| 149 |
if Event.IsKey(key2) then |
| 150 |
if TiledMap.Data.Layers.Count > 2 then |
| 151 |
TiledMap.Data.Layers[2].Exists := not TiledMap.Data.Layers[2].Exists; |
| 152 |
end; |
| 153 |
|
| 154 |
end. |