| 1 |
{ |
| 2 |
Copyright 2014-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 |
{ Scene manager (TCastle2DSceneManager) and scene (TCastle2DScene) best suited for 2D worlds. } |
| 17 |
unit Castle2DSceneManager deprecated 'use CastleViewport'; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
// This whole unit is deprecated and refers to some deprecated items, don't warn about it |
| 22 |
{$warn symbol_deprecated off} |
| 23 |
{$warn unit_deprecated off} |
| 24 |
|
| 25 |
interface |
| 26 |
|
| 27 |
uses Classes, |
| 28 |
CastleScene, CastleSceneManager, CastleUIControls, CastleCameras, |
| 29 |
CastleProjection, CastleVectors; |
| 30 |
|
| 31 |
type |
| 32 |
{ Scene manager best suited for 2D worlds. |
| 33 |
|
| 34 |
@unorderedList( |
| 35 |
@item( |
| 36 |
See @link(TCastleViewport.Setup2D) for a description what it does. |
| 37 |
) |
| 38 |
|
| 39 |
@item(Also, the navigation by default remains @nil, |
| 40 |
because @link(AutoNavigation) is by default @false. |
| 41 |
|
| 42 |
That is because you typically want to |
| 43 |
code yourself all camera movement for 2D games. |
| 44 |
|
| 45 |
Of course, just like with any @link(TCastleViewport), you can set |
| 46 |
@link(Navigation) to any custom navigation component. |
| 47 |
) |
| 48 |
) |
| 49 |
} |
| 50 |
TCastle2DSceneManager = class(TCastleSceneManager) |
| 51 |
strict private |
| 52 |
function GetProjectionAutoSize: Boolean; |
| 53 |
function GetProjectionWidth: Single; |
| 54 |
function GetProjectionHeight: Single; |
| 55 |
function GetProjectionSpan: Single; |
| 56 |
function GetProjectionOriginCenter: Boolean; |
| 57 |
procedure SetProjectionAutoSize(const Value: Boolean); |
| 58 |
procedure SetProjectionWidth(const Value: Single); |
| 59 |
procedure SetProjectionHeight(const Value: Single); |
| 60 |
procedure SetProjectionSpan(const Value: Single); |
| 61 |
procedure SetProjectionOriginCenter(const Value: Boolean); |
| 62 |
public |
| 63 |
const |
| 64 |
DefaultProjectionSpan = TCastleViewport.Default2DProjectionFar deprecated 'use Default2DProjectionFar'; |
| 65 |
DefaultCameraZ = TCastleViewport.Default2DCameraZ deprecated 'use Default2DCameraZ'; |
| 66 |
|
| 67 |
constructor Create(AOwner: TComponent); override; |
| 68 |
|
| 69 |
function CurrentProjectionWidth: Single; deprecated 'use Camera.Orthographic.EffectiveWidth'; |
| 70 |
function CurrentProjectionHeight: Single; deprecated 'use Camera.Orthographic.EffectiveHeight'; |
| 71 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 72 |
published |
| 73 |
property AutoCamera default false; |
| 74 |
property AutoNavigation default false; |
| 75 |
|
| 76 |
{ When ProjectionAutoSize is @true, the size of the world visible |
| 77 |
in our viewport depends on scene manager size. |
| 78 |
ProjectionHeight and ProjectionWidth are ignored then. |
| 79 |
|
| 80 |
When ProjectionAutoSize is @false, ProjectionHeight and ProjectionWidth |
| 81 |
are used to determine the world visible in our viewport. |
| 82 |
If one of them is zero, the other is automatically adjusted to |
| 83 |
follow aspect ratio of viewport size. |
| 84 |
If both of them are zero, projection is automatically calculated just as |
| 85 |
if ProjectionAutoSize was @true. |
| 86 |
|
| 87 |
In all cases, CurrentProjectionWidth and CurrentProjectionHeight |
| 88 |
can be checked to see actual projection dimensions. } |
| 89 |
property ProjectionAutoSize: Boolean |
| 90 |
read GetProjectionAutoSize write SetProjectionAutoSize default true; |
| 91 |
{$ifdef FPC}deprecated 'use Camera.Orthographic.Width and Height; only when both are zero, it is auto-sized';{$endif} |
| 92 |
property ProjectionHeight: Single |
| 93 |
read GetProjectionHeight write SetProjectionHeight |
| 94 |
{$ifdef FPC}default 0{$endif}; |
| 95 |
{$ifdef FPC}deprecated 'use Camera.Orthographic.Height, and note that ProjectionAutoSize is ignored';{$endif} |
| 96 |
property ProjectionWidth: Single |
| 97 |
read GetProjectionWidth write SetProjectionWidth |
| 98 |
{$ifdef FPC}default 0{$endif}; |
| 99 |
{$ifdef FPC}deprecated 'use Camera.Orthographic.Width, and note that ProjectionAutoSize is ignored';{$endif} |
| 100 |
|
| 101 |
property ProjectionSpan: Single |
| 102 |
read GetProjectionSpan write SetProjectionSpan |
| 103 |
{$ifdef FPC}default Default2DProjectionFar{$endif}; |
| 104 |
{$ifdef FPC}deprecated 'use Camera.ProjectionFar';{$endif} |
| 105 |
|
| 106 |
{ Where is the (0,0) world point with respect to the viewport. |
| 107 |
|
| 108 |
If @false, the (0,0) is in the left-bottom corner, which matches |
| 109 |
the typical 2D drawing coordinates used throughout our engine. |
| 110 |
In other words, if the camera is at position (0,0,whatever), |
| 111 |
then the (0,0) position in 2D is in the left-bottom corner of the scene manager |
| 112 |
rectangle. |
| 113 |
|
| 114 |
If @true, the (0,0) is in the middle of the viewport. |
| 115 |
In other words, if the camera is at position (0,0,whatever), |
| 116 |
then the (0,0) position is in the center of the scene manager |
| 117 |
rectangle. |
| 118 |
|
| 119 |
Both values of @name make sense, |
| 120 |
it depends on the game type and how you prefer to think in 2D coordinates. |
| 121 |
And how do you want the result to behave when aspect ratio changes: |
| 122 |
|
| 123 |
@unorderedList( |
| 124 |
@item(With ProjectionOriginCenter = @true, things will stay "glued" |
| 125 |
to the center.) |
| 126 |
@item(With ProjectionOriginCenter = @false, things will stay "glued" |
| 127 |
to the left-bottom corner.) |
| 128 |
) |
| 129 |
} |
| 130 |
property ProjectionOriginCenter: Boolean |
| 131 |
read GetProjectionOriginCenter write SetProjectionOriginCenter default false; |
| 132 |
{$ifdef FPC}deprecated 'use Camera.Orthographic.Origin';{$endif} |
| 133 |
end |
| 134 |
{$ifdef FPC}deprecated 'use TCastleViewport. To have the same initial behavior call Setup2D method, and set FullSize:=true'{$endif}; |
| 135 |
|
| 136 |
T2DSceneManager = class(TCastle2DSceneManager) |
| 137 |
public |
| 138 |
constructor Create(AOwner: TComponent); override; |
| 139 |
published |
| 140 |
property Transparent default true; |
| 141 |
end deprecated 'use TCastleViewport. To have the same initial behavior call Setup2D method, and set FullSize:=true, and set Transparent:=true'; |
| 142 |
|
| 143 |
{ Scene best suited for 2D models. |
| 144 |
Does nothing now (used to control blending sort in this scene). } |
| 145 |
TCastle2DScene = class(TCastleScene) |
| 146 |
public |
| 147 |
constructor Create(AOwner: TComponent); override; |
| 148 |
|
| 149 |
{ Create a scene with the same contents (X3D scene graph) as this one. |
| 150 |
Note that this @bold(does not copy other scene attributes), |
| 151 |
like @link(ProcessEvents) or @link(Spatial) or rendering attributes |
| 152 |
in @link(RenderOptions). } |
| 153 |
function Clone(const AOwner: TComponent): TCastle2DScene; |
| 154 |
end deprecated 'use TCastleScene, it is equivalent to TCastle2DScene now'; |
| 155 |
|
| 156 |
{$warnings off} // refering to deprecated from deprecated |
| 157 |
T2DScene = TCastle2DScene deprecated 'use TCastleScene, it is equivalent to T2DScene now'; |
| 158 |
{$warnings on} |
| 159 |
|
| 160 |
implementation |
| 161 |
|
| 162 |
uses SysUtils, |
| 163 |
CastleBoxes, CastleGLUtils, X3DNodes, CastleComponentSerialize, CastleUtils, |
| 164 |
CastleRectangles, CastleLog; |
| 165 |
|
| 166 |
{ TCastle2DSceneManager -------------------------------------------------------- } |
| 167 |
|
| 168 |
constructor TCastle2DSceneManager.Create(AOwner: TComponent); |
| 169 |
begin |
| 170 |
inherited; |
| 171 |
{ When not deserializing, do Setup2D now. |
| 172 |
When deserializing, the camera should already be saved with 2D configuration. } |
| 173 |
if Camera <> nil then |
| 174 |
Setup2D; // sets AutoCamera := false |
| 175 |
AutoNavigation := false; |
| 176 |
end; |
| 177 |
|
| 178 |
{ Below we allow all getters to tolerate Camera=nil case, |
| 179 |
as undo system needs to be able to serialize this component in any state. } |
| 180 |
|
| 181 |
function TCastle2DSceneManager.CurrentProjectionWidth: Single; |
| 182 |
begin |
| 183 |
if Camera = nil then Exit(0); |
| 184 |
Result := Camera.Orthographic.EffectiveRect.Width; |
| 185 |
end; |
| 186 |
|
| 187 |
function TCastle2DSceneManager.CurrentProjectionHeight: Single; |
| 188 |
begin |
| 189 |
if Camera = nil then Exit(0); |
| 190 |
Result := Camera.Orthographic.EffectiveRect.Height; |
| 191 |
end; |
| 192 |
|
| 193 |
function TCastle2DSceneManager.GetProjectionAutoSize: Boolean; |
| 194 |
begin |
| 195 |
Result := false; // it behaves like always false, now |
| 196 |
end; |
| 197 |
|
| 198 |
function TCastle2DSceneManager.GetProjectionWidth: Single; |
| 199 |
begin |
| 200 |
if Camera = nil then Exit(0); |
| 201 |
Result := Camera.Orthographic.Width; |
| 202 |
end; |
| 203 |
|
| 204 |
function TCastle2DSceneManager.GetProjectionHeight: Single; |
| 205 |
begin |
| 206 |
if Camera = nil then Exit(0); |
| 207 |
Result := Camera.Orthographic.Height; |
| 208 |
end; |
| 209 |
|
| 210 |
function TCastle2DSceneManager.GetProjectionSpan: Single; |
| 211 |
begin |
| 212 |
if Camera = nil then Exit(Default2DProjectionFar); |
| 213 |
Result := Camera.ProjectionFar; |
| 214 |
end; |
| 215 |
|
| 216 |
function TCastle2DSceneManager.GetProjectionOriginCenter: Boolean; |
| 217 |
begin |
| 218 |
if Camera = nil then Exit(false); |
| 219 |
Result := not Camera.Orthographic.Origin.IsPerfectlyZero; |
| 220 |
end; |
| 221 |
|
| 222 |
procedure TCastle2DSceneManager.SetProjectionAutoSize(const Value: Boolean); |
| 223 |
begin |
| 224 |
if Value then |
| 225 |
WritelnWarning('TCastle2DSceneManager always behaves as if ProjectionAutoSize = false. We will automatically calculate projection width or height if one (or both) of Camera.Orthographic.Width/Height are zero.'); |
| 226 |
end; |
| 227 |
|
| 228 |
procedure TCastle2DSceneManager.SetProjectionWidth(const Value: Single); |
| 229 |
begin |
| 230 |
Camera.Orthographic.Width := Value; |
| 231 |
end; |
| 232 |
|
| 233 |
procedure TCastle2DSceneManager.SetProjectionHeight(const Value: Single); |
| 234 |
begin |
| 235 |
Camera.Orthographic.Height := Value; |
| 236 |
end; |
| 237 |
|
| 238 |
procedure TCastle2DSceneManager.SetProjectionSpan(const Value: Single); |
| 239 |
begin |
| 240 |
Camera.ProjectionFar := Value; |
| 241 |
end; |
| 242 |
|
| 243 |
procedure TCastle2DSceneManager.SetProjectionOriginCenter(const Value: Boolean); |
| 244 |
begin |
| 245 |
if Value then |
| 246 |
Camera.Orthographic.Origin := Vector2(0.5, 0.5) |
| 247 |
else |
| 248 |
Camera.Orthographic.Origin := TVector2.Zero; |
| 249 |
end; |
| 250 |
|
| 251 |
procedure TCastle2DSceneManager.Update(const SecondsPassed: Single; |
| 252 |
var HandleInput: Boolean); |
| 253 |
begin |
| 254 |
inherited; |
| 255 |
|
| 256 |
{ At run-time, auto-create camera, for backward compatibility. |
| 257 |
Why it can remain nil? |
| 258 |
When deserializing, and no camera were contained in design file. |
| 259 |
Testcase: castle-engine-priv/contrib/scaling_bug/my_new_project_standalone.lpr . } |
| 260 |
if (not CastleDesignMode) and (Camera = nil) then |
| 261 |
begin |
| 262 |
WritelnWarning('Creating camera, because it was missing in design file. For the future, please uppgrade to use TCastleViewport instead of deprecated TCastle2DSceneManager.'); |
| 263 |
SetupCamera; |
| 264 |
Setup2D; |
| 265 |
end; |
| 266 |
end; |
| 267 |
|
| 268 |
{ T2DSceneManager ------------------------------------------------------------ } |
| 269 |
|
| 270 |
constructor T2DSceneManager.Create(AOwner: TComponent); |
| 271 |
begin |
| 272 |
inherited; |
| 273 |
Transparent := true; |
| 274 |
end; |
| 275 |
|
| 276 |
{ TCastle2DScene --------------------------------------------------------------- } |
| 277 |
|
| 278 |
constructor TCastle2DScene.Create(AOwner: TComponent); |
| 279 |
begin |
| 280 |
inherited; |
| 281 |
end; |
| 282 |
|
| 283 |
function TCastle2DScene.Clone(const AOwner: TComponent): TCastle2DScene; |
| 284 |
begin |
| 285 |
{$warnings off} // refering to deprecated from deprecated |
| 286 |
Result := TCastle2DScene.Create(AOwner); |
| 287 |
{$warnings on} |
| 288 |
if RootNode <> nil then |
| 289 |
Result.Load(RootNode.DeepCopy as TX3DRootNode, true); |
| 290 |
end; |
| 291 |
|
| 292 |
var |
| 293 |
R: TRegisteredComponent; |
| 294 |
initialization |
| 295 |
R := TRegisteredComponent.Create; |
| 296 |
{$warnings off} // using deprecated, to keep reading it from castle-user-interface working |
| 297 |
R.ComponentClass := TCastle2DSceneManager; |
| 298 |
{$warnings on} |
| 299 |
R.Caption := ['2D Scene Manager']; |
| 300 |
R.IsDeprecated := true; |
| 301 |
RegisterSerializableComponent(R); |
| 302 |
|
| 303 |
R := TRegisteredComponent.Create; |
| 304 |
{$warnings off} // using deprecated, to keep reading it from castle-user-interface working |
| 305 |
R.ComponentClass := TCastle2DScene; |
| 306 |
{$warnings on} |
| 307 |
R.Caption := ['2D Scene']; |
| 308 |
R.IsDeprecated := true; |
| 309 |
RegisterSerializableComponent(R); |
| 310 |
end. |