| 1 |
{%MainUnit castleviewport.pas} |
| 2 |
{ |
| 3 |
Copyright 2013-2023 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
//type (continue exising type declaration) |
| 19 |
|
| 20 |
{ Navigation type that determines various navigation properties, |
| 21 |
used by @link(TCastleAutoNavigationViewport.NavigationType). } |
| 22 |
TNavigationType = ( |
| 23 |
{ Examine mode, comfortable to rotate the scene like an item held in your hand. |
| 24 |
Uses TCastleExamineNavigation. } |
| 25 |
ntExamine, |
| 26 |
{ Turntable mode, similar to examine mode, but with a bit different interpretation |
| 27 |
of moves. |
| 28 |
Uses TCastleExamineNavigation. } |
| 29 |
ntTurntable, |
| 30 |
{ Walk mode, comfortable to walk around the scene with gravity. |
| 31 |
Uses TCastleWalkNavigation. } |
| 32 |
ntWalk, |
| 33 |
{ Fly mode, comfortable to move around around the scene without gravity. |
| 34 |
Uses TCastleWalkNavigation. } |
| 35 |
ntFly, |
| 36 |
{ 2D mode, where the camera always looks along with Z axis, and you can pan / zoom in XY. } |
| 37 |
nt2D, |
| 38 |
{ Disable user navigation on the scene. |
| 39 |
Uses TCastleWalkNavigation. } |
| 40 |
ntNone |
| 41 |
); |
| 42 |
|
| 43 |
{ Automatically create and switch between various navigation classes on a viewport. |
| 44 |
|
| 45 |
@deprecated |
| 46 |
We don't advise using this class -- usually it is more straightforward |
| 47 |
to just explicitly create own instances of classes like TCastleExamineNavigation, |
| 48 |
TCastleWalkNavigation, |
| 49 |
add them as TCastleViewport children, |
| 50 |
control which one is used by @link(TCastleUserInterface.Exists). |
| 51 |
|
| 52 |
This class allows more implicit management of current navigation type. |
| 53 |
Navigation can be adjusted using |
| 54 |
|
| 55 |
@unorderedList( |
| 56 |
@item(auto-created when you use @link(WalkNavigation), @link(ExamineNavigation),) |
| 57 |
|
| 58 |
@item(get and set by @link(NavigationType),) |
| 59 |
|
| 60 |
@item(it can also automatically follow navigation requested in the X3D file |
| 61 |
(using TNavigationInfoNode in MainScene) if @link(AutoNavigation).) |
| 62 |
) |
| 63 |
} |
| 64 |
TCastleAutoNavigationViewport = class(TCastleViewport) |
| 65 |
strict private |
| 66 |
FInternalExamineNavigation: TCastleExamineNavigation; |
| 67 |
FInternalWalkNavigation: TCastleWalkNavigation; |
| 68 |
FInternal2DNavigation: TCastle2DNavigation; |
| 69 |
FAutoNavigation: Boolean; |
| 70 |
FWithinSetNavigationType: Boolean; |
| 71 |
InsideBoundNavigationInfoChanged: Boolean; |
| 72 |
|
| 73 |
function GetNavigationType: TNavigationType; |
| 74 |
procedure SetNavigationType(const Value: TNavigationType); |
| 75 |
|
| 76 |
procedure SetAutoNavigation(const Value: Boolean); |
| 77 |
{ TNavigationType value determined by current NavigationInfo node. } |
| 78 |
class function NavigationTypeFromNavigationInfo(const Scene: TCastleSceneCore): TNavigationType; static; |
| 79 |
private |
| 80 |
{ In addition to ancestor work: |
| 81 |
If AutoNavigation then the @link(Navigation) is automatically created here, |
| 82 |
see @link(AssignDefaultNavigation). } |
| 83 |
procedure ApplyProjection; override; |
| 84 |
protected |
| 85 |
procedure Notification(AComponent: TComponent; Operation: TOperation); override; |
| 86 |
procedure BoundNavigationInfoChanged; override; |
| 87 |
public |
| 88 |
{ Return current navigation. Automatically creates it if missing. } |
| 89 |
function RequiredNavigation: TCastleNavigation; |
| 90 |
|
| 91 |
{ Return the currently used navigation as TCastleWalkNavigation, making sure that current |
| 92 |
NavigationType is something using TCastleWalkNavigation. |
| 93 |
|
| 94 |
@unorderedList( |
| 95 |
@item( |
| 96 |
When SwitchNavigationTypeIfNeeded is @true (the default), |
| 97 |
this method makes sure that the @link(NavigationType) corresponds to a type |
| 98 |
handled by TCastleWalkNavigation, creating and adjusting the navigation if necessary. |
| 99 |
|
| 100 |
If the current NavigationType does not use TCastleWalkNavigation |
| 101 |
(see @link(TNavigationType) documentation for information which |
| 102 |
navigation types use which TCastleNavigation descendants), |
| 103 |
then it's switched to ntWalk. |
| 104 |
) |
| 105 |
|
| 106 |
@item( |
| 107 |
When SwitchNavigationTypeIfNeeded is @false, |
| 108 |
then we return @nil if the current navigation is not already |
| 109 |
a TCastleWalkNavigation instance. |
| 110 |
|
| 111 |
We @italic(never) create a new navigation in this case |
| 112 |
(even if the NavigatinInfo node in MainScene would indicate |
| 113 |
that the new navigation would be a TCastleWalkNavigation). |
| 114 |
) |
| 115 |
) |
| 116 |
} |
| 117 |
function WalkNavigation(const SwitchNavigationTypeIfNeeded: boolean = true): TCastleWalkNavigation; |
| 118 |
deprecated 'instead of this, just get and set "Navigation"; you can check if it is "TCastleWalkNavigation", you can set it to new "TCastleWalkNavigation" instance'; |
| 119 |
|
| 120 |
{ Return the currently used navigation as TCastleExamineNavigation, making sure that current |
| 121 |
NavigationType is something using TCastleExamineNavigation (but not TCastle2DNavigation, |
| 122 |
this wants a 3D navigation). |
| 123 |
|
| 124 |
@unorderedList( |
| 125 |
@item( |
| 126 |
When SwitchNavigationTypeIfNeeded is @true (the default), |
| 127 |
this method makes sure that the @link(NavigationType) corresponds to a type |
| 128 |
handled by TCastleExamineNavigation, creating and adjusting the navigation if necessary. |
| 129 |
|
| 130 |
If the current NavigationType does not use TCastleExamineNavigation |
| 131 |
(see @link(TNavigationType) documentation for information which |
| 132 |
navigation types use which TCastleNavigation descendants), |
| 133 |
then it's switched to ntExamine. |
| 134 |
) |
| 135 |
|
| 136 |
@item( |
| 137 |
When SwitchNavigationTypeIfNeeded is @false, |
| 138 |
then we return @nil if the current navigation is not already |
| 139 |
a TCastleExamineNavigation instance. |
| 140 |
|
| 141 |
We @italic(never) create a new navigation in this case |
| 142 |
(even if the NavigatinInfo node in MainScene would indicate |
| 143 |
that the new navigation would be a TCastleExamineNavigation). |
| 144 |
) |
| 145 |
) |
| 146 |
} |
| 147 |
function ExamineNavigation(const SwitchNavigationTypeIfNeeded: boolean = true): TCastleExamineNavigation; |
| 148 |
deprecated 'instead of this, just get and set "Navigation"; you can check if it is "TCastleExamineNavigation", you can set it to new "TCastleExamineNavigation" instance'; |
| 149 |
|
| 150 |
{ Navigation instances internally used by this viewport. |
| 151 |
Using these methods automatically creates these instances |
| 152 |
(so they are never @nil). |
| 153 |
|
| 154 |
Using these methods @italic(does not) make these |
| 155 |
navigation instances current (in contast to calling @link(ExamineNavigation), |
| 156 |
@link(WalkNavigation) or setting @link(NavigationType)). |
| 157 |
|
| 158 |
When you switch navigation types by calling @link(ExamineNavigation), |
| 159 |
@link(WalkNavigation) or setting @link(NavigationType) |
| 160 |
the viewport keeps using these instances of navigation, |
| 161 |
instead of creating new navigation instances. |
| 162 |
This way all the navigation properties |
| 163 |
are preserved when you switch |
| 164 |
e.g. NavigationType from ntWalk to ntExamine to ntWalk again. |
| 165 |
|
| 166 |
@groupBegin } |
| 167 |
function InternalExamineNavigation: TCastleExamineNavigation; |
| 168 |
function InternalWalkNavigation: TCastleWalkNavigation; |
| 169 |
function Internal2DNavigation: TCastle2DNavigation; |
| 170 |
{ @groupEnd } |
| 171 |
|
| 172 |
{ Assign @link(Navigation) to a default TCastleNavigation suitable |
| 173 |
for navigating in this scene. |
| 174 |
|
| 175 |
This is automatically used when @link(Navigation) is @nil |
| 176 |
and @link(AutoNavigation). |
| 177 |
You can also use it explicitly. |
| 178 |
|
| 179 |
The implementation in base TCastleViewport uses TNavigationInfoNode |
| 180 |
from MainScene. If MainScene is not assigned, we create a simple |
| 181 |
navigation in Examine mode. } |
| 182 |
procedure AssignDefaultNavigation; virtual; |
| 183 |
|
| 184 |
{ Set @link(Navigation) and some of its' parameters |
| 185 |
(like TCastleWalkNavigation.Gravity and so on). |
| 186 |
|
| 187 |
If @link(AutoNavigation), the initial @link(Navigation) |
| 188 |
as well as initial value of this property are automatically determined |
| 189 |
by the currently bound X3D NavigatinInfo node in the @link(TCastleRootTransform.MainScene MainScene), |
| 190 |
and world bounding box. |
| 191 |
They are also automatically adjusted e.g. when current NavigatinInfo |
| 192 |
node changes. |
| 193 |
|
| 194 |
But you can set @link(Navigation), or this property, |
| 195 |
manually to override the detected navigation. |
| 196 |
You should set @link(AutoNavigation) to @false to take control |
| 197 |
of @link(Navigation) and this property completely (no auto-detection |
| 198 |
based on @link(TCastleRootTransform.MainScene MainScene) will then take place). |
| 199 |
|
| 200 |
Note that you can also affect the current NavigationType by directly |
| 201 |
changing the camera properties, |
| 202 |
e.g. you can directly change @link(TCastleWalkNavigation.Gravity) from @false to @true, |
| 203 |
and thus you effectively switch from ntFly to ntWalk navigation types. |
| 204 |
When you read the NavigationType property, we determine the current navigation |
| 205 |
type from current camera properties. |
| 206 |
|
| 207 |
Setting this sets: |
| 208 |
@unorderedList( |
| 209 |
@itemSpacing compact |
| 210 |
@item @link(TCastleNavigation.Input) |
| 211 |
@item @link(TCastleExamineNavigation.Turntable), only in case of @link(TCastleExamineNavigation) |
| 212 |
@item @link(TCastleWalkNavigation.Gravity), only in case of @link(TCastleWalkNavigation) |
| 213 |
@item @link(TCastleWalkNavigation.PreferGravityUpForRotations), only in case of @link(TCastleWalkNavigation) |
| 214 |
@item @link(TCastleWalkNavigation.PreferGravityUpForMoving), only in case of @link(TCastleWalkNavigation) |
| 215 |
) |
| 216 |
|
| 217 |
If you write to NavigationType, then you @italic(should not) touch the |
| 218 |
above properties directly. That's because not every combination of |
| 219 |
above properties correspond to some sensible value of NavigationType. |
| 220 |
If you directly set some weird configuration, reading NavigationType will |
| 221 |
try it's best to determine the closest TNavigationType value |
| 222 |
that is similar to your configuration. } |
| 223 |
property NavigationType: TNavigationType |
| 224 |
read GetNavigationType write SetNavigationType |
| 225 |
default ntNone; |
| 226 |
|
| 227 |
class function InternalGetNavigationType(const V: TCastleViewport): TNavigationType; |
| 228 |
published |
| 229 |
{ Assign sensible @link(Navigation) looking |
| 230 |
at the initial world (@link(Items)) if it is not assigned. |
| 231 |
|
| 232 |
This also allows to later synchronize navigation properties when X3D NavigationInfo |
| 233 |
node changes, or a new NavigationInfo node is bound. |
| 234 |
|
| 235 |
By default it is @false, which means that you control @link(Navigation) on your own. |
| 236 |
} |
| 237 |
property AutoNavigation: Boolean |
| 238 |
read FAutoNavigation write SetAutoNavigation default false; |
| 239 |
end {$ifdef FPC}deprecated 'use TCastleViewport instead of this; it is more straightforward to create TCastleNavigation explicitly (rather than auto-detecting it) and just add it as TCastleViewport child'{$endif}; |
| 240 |
|
| 241 |
{$endif read_interface} |
| 242 |
|
| 243 |
{$ifdef read_implementation} |
| 244 |
|
| 245 |
class function TCastleAutoNavigationViewport.NavigationTypeFromNavigationInfo(const Scene: TCastleSceneCore): TNavigationType; |
| 246 |
|
| 247 |
function StringToNavigationType(const S: string; |
| 248 |
out NavigationType: TNavigationType): boolean; |
| 249 |
begin |
| 250 |
Result := false; |
| 251 |
if S = 'WALK' then |
| 252 |
begin |
| 253 |
Result := true; |
| 254 |
NavigationType := ntWalk; |
| 255 |
end else |
| 256 |
if S = 'FLY' then |
| 257 |
begin |
| 258 |
Result := true; |
| 259 |
NavigationType := ntFly |
| 260 |
end else |
| 261 |
if S = 'NONE' then |
| 262 |
begin |
| 263 |
Result := true; |
| 264 |
NavigationType := ntNone |
| 265 |
end else |
| 266 |
if (S = 'EXAMINE') or |
| 267 |
(S = 'LOOKAT') then |
| 268 |
begin |
| 269 |
if S = 'LOOKAT' then |
| 270 |
WritelnWarning('X3D', 'TODO: Navigation type "LOOKAT" is not yet supported, treating like "EXAMINE"'); |
| 271 |
Result := true; |
| 272 |
NavigationType := ntExamine; |
| 273 |
end else |
| 274 |
if (S = 'ARCHITECTURE') or |
| 275 |
(S = 'TURNTABLE') then |
| 276 |
begin |
| 277 |
Result := true; |
| 278 |
NavigationType := ntTurntable |
| 279 |
end else |
| 280 |
if (S = '2D') or |
| 281 |
{ PLANE and PLANE_create3000.github.io names are used by X_ITE, |
| 282 |
see "Online X3D File Format Converter Powered by X_ITE" thread on x3d-public mailing list. } |
| 283 |
(S = 'PLANE') or |
| 284 |
(S = 'PLANE_create3000.github.io') then |
| 285 |
begin |
| 286 |
Result := true; |
| 287 |
NavigationType := nt2D; |
| 288 |
end else |
| 289 |
if S = 'ANY' then |
| 290 |
begin |
| 291 |
{ Do nothing, also do not report this NavigationInfo.type as unknown. } |
| 292 |
end else |
| 293 |
WritelnWarning('X3D', 'Unknown NavigationInfo.type "%s"', [S]); |
| 294 |
end; |
| 295 |
|
| 296 |
var |
| 297 |
I: Integer; |
| 298 |
NavigationNode: TNavigationInfoNode; |
| 299 |
begin |
| 300 |
NavigationNode := Scene.NavigationInfoStack.Top; |
| 301 |
if NavigationNode <> nil then |
| 302 |
for I := 0 to NavigationNode.FdType.Count - 1 do |
| 303 |
if StringToNavigationType(NavigationNode.FdType.Items[I], Result) then |
| 304 |
Exit; |
| 305 |
|
| 306 |
{ No recognized "type" found, so use default type EXAMINE. } |
| 307 |
Result := ntExamine; |
| 308 |
end; |
| 309 |
|
| 310 |
function TCastleAutoNavigationViewport.RequiredNavigation: TCastleNavigation; |
| 311 |
begin |
| 312 |
{ For backward-compatibility, this also initializes Camera vectors |
| 313 |
(even though the method docs only guarantee that it initializes Navigation, |
| 314 |
but in the past Camera and Navigation were the same thing). } |
| 315 |
EnsureCameraDetected; |
| 316 |
|
| 317 |
{$warnings off} // using deprecated in deprecated |
| 318 |
if Navigation = nil then |
| 319 |
AssignDefaultNavigation; |
| 320 |
// Since AssignDefaultNavigation may leave Navigation nil, make sure it is assigned now |
| 321 |
if Navigation = nil then |
| 322 |
Result := InternalExamineNavigation; |
| 323 |
Result := Navigation; |
| 324 |
{$warnings on} |
| 325 |
end; |
| 326 |
|
| 327 |
function TCastleAutoNavigationViewport.InternalExamineNavigation: TCastleExamineNavigation; |
| 328 |
begin |
| 329 |
if FInternalExamineNavigation = nil then |
| 330 |
begin |
| 331 |
FInternalExamineNavigation := TCastleExamineNavigation.Create(Self); |
| 332 |
FInternalExamineNavigation.SetTransient; |
| 333 |
end; |
| 334 |
Result := FInternalExamineNavigation; |
| 335 |
end; |
| 336 |
|
| 337 |
function TCastleAutoNavigationViewport.InternalWalkNavigation: TCastleWalkNavigation; |
| 338 |
begin |
| 339 |
if FInternalWalkNavigation = nil then |
| 340 |
begin |
| 341 |
FInternalWalkNavigation := TCastleWalkNavigation.Create(Self); |
| 342 |
FInternalWalkNavigation.SetTransient; |
| 343 |
end; |
| 344 |
Result := FInternalWalkNavigation; |
| 345 |
end; |
| 346 |
|
| 347 |
function TCastleAutoNavigationViewport.Internal2DNavigation: TCastle2DNavigation; |
| 348 |
begin |
| 349 |
if FInternal2DNavigation = nil then |
| 350 |
begin |
| 351 |
FInternal2DNavigation := TCastle2DNavigation.Create(Self); |
| 352 |
FInternal2DNavigation.SetTransient; |
| 353 |
end; |
| 354 |
Result := FInternal2DNavigation; |
| 355 |
end; |
| 356 |
|
| 357 |
function TCastleAutoNavigationViewport.ExamineNavigation(const SwitchNavigationTypeIfNeeded: boolean): TCastleExamineNavigation; |
| 358 |
var |
| 359 |
NewNavigation: TCastleExamineNavigation; |
| 360 |
begin |
| 361 |
{$warnings off} // using deprecated in deprecated |
| 362 |
if (not (Navigation is TCastleExamineNavigation)) or |
| 363 |
// when TCastle2DNavigation, we want to change it 3D examine navigation here |
| 364 |
(Navigation is TCastle2DNavigation) then |
| 365 |
begin |
| 366 |
if not SwitchNavigationTypeIfNeeded then |
| 367 |
Exit(nil); |
| 368 |
|
| 369 |
{ For backward-compatibility, this also initializes Camera vectors |
| 370 |
(even though the method docs only guarantee that it initializes Navigation, |
| 371 |
but in the past Camera and Navigation were the same thing). } |
| 372 |
EnsureCameraDetected; |
| 373 |
|
| 374 |
NewNavigation := InternalExamineNavigation; |
| 375 |
if Navigation = nil then |
| 376 |
AssignDefaultNavigation; // initialize defaults from MainScene |
| 377 |
// AssignDefaultNavigation could leave Navigation at nil, in which case ignor |
| 378 |
if Navigation <> nil then |
| 379 |
NewNavigation.Assign(Navigation); |
| 380 |
Navigation := NewNavigation; |
| 381 |
{ make sure it's in ntExamine mode (as we possibly reuse old navigation, |
| 382 |
by reusing InternalExamineNavigation, so we're not sure what state it's in. } |
| 383 |
NavigationType := ntExamine; |
| 384 |
end; |
| 385 |
Result := Navigation as TCastleExamineNavigation; |
| 386 |
{$warnings on} |
| 387 |
end; |
| 388 |
|
| 389 |
function TCastleAutoNavigationViewport.WalkNavigation(const SwitchNavigationTypeIfNeeded: boolean): TCastleWalkNavigation; |
| 390 |
var |
| 391 |
NewNavigation: TCastleWalkNavigation; |
| 392 |
begin |
| 393 |
{$warnings off} // using deprecated in deprecated |
| 394 |
if not (Navigation is TCastleWalkNavigation) then |
| 395 |
begin |
| 396 |
if not SwitchNavigationTypeIfNeeded then |
| 397 |
Exit(nil); |
| 398 |
|
| 399 |
{ For backward-compatibility, this also initializes Camera vectors |
| 400 |
(even though the method docs only guarantee that it initializes Navigation, |
| 401 |
but in the past Camera and Navigation were the same thing). } |
| 402 |
EnsureCameraDetected; |
| 403 |
|
| 404 |
NewNavigation := InternalWalkNavigation; |
| 405 |
if Navigation = nil then |
| 406 |
AssignDefaultNavigation; // initialize defaults from MainScene |
| 407 |
// AssignDefaultNavigation could leave Navigation at nil, in which case ignor |
| 408 |
if Navigation <> nil then |
| 409 |
NewNavigation.Assign(Navigation); |
| 410 |
Navigation := NewNavigation; |
| 411 |
{ make sure it's in ntWalk mode (as we possibly reuse old navigation, |
| 412 |
by reusing InternalWalkNavigation, so we're not sure what state it's in. } |
| 413 |
NavigationType := ntWalk; |
| 414 |
end; |
| 415 |
Result := Navigation as TCastleWalkNavigation; |
| 416 |
{$warnings on} |
| 417 |
end; |
| 418 |
|
| 419 |
class function TCastleAutoNavigationViewport.InternalGetNavigationType(const V: TCastleViewport): TNavigationType; |
| 420 |
var |
| 421 |
Nav: TCastleNavigation; |
| 422 |
begin |
| 423 |
{$warnings off} // using deprecated in deprecated |
| 424 |
Nav := V.Navigation; |
| 425 |
{$warnings on} |
| 426 |
|
| 427 |
{ We are using here Navigation, not RequiredNavigation, as automatically |
| 428 |
creating Navigation could have surprising consequences. |
| 429 |
E.g. it means that SetNavigation(nil) may recreate the navigation, |
| 430 |
as BoundNavigationInfoChanged calls something that checks |
| 431 |
NavigationType. } |
| 432 |
|
| 433 |
if (Nav <> nil) and (Nav.Input = []) then |
| 434 |
Exit(ntNone); |
| 435 |
|
| 436 |
if Nav is TCastle2DNavigation then |
| 437 |
Result:= nt2D |
| 438 |
else |
| 439 |
if Nav is TCastleExamineNavigation then |
| 440 |
begin |
| 441 |
if TCastleExamineNavigation(Nav).Turntable then |
| 442 |
Result := ntTurntable |
| 443 |
else |
| 444 |
Result := ntExamine; |
| 445 |
end else |
| 446 |
if Nav is TCastleWalkNavigation then |
| 447 |
begin |
| 448 |
if TCastleWalkNavigation(Nav).Gravity then |
| 449 |
Result := ntWalk |
| 450 |
else |
| 451 |
Result := ntFly; |
| 452 |
end else |
| 453 |
Result := ntNone; // also accounts for Nav = nil case |
| 454 |
end; |
| 455 |
|
| 456 |
function TCastleAutoNavigationViewport.GetNavigationType: TNavigationType; |
| 457 |
begin |
| 458 |
Result := InternalGetNavigationType(Self); |
| 459 |
end; |
| 460 |
|
| 461 |
procedure TCastleAutoNavigationViewport.SetNavigationType(const Value: TNavigationType); |
| 462 |
var |
| 463 |
E: TCastleExamineNavigation; |
| 464 |
W: TCastleWalkNavigation; |
| 465 |
begin |
| 466 |
{ Do this even if "Value = GetNavigationType". |
| 467 |
This makes sense, in case you set some weird values. |
| 468 |
On the other hand, it makes "NavigationType := NavigationType" sometimes |
| 469 |
a sensible operation that changes something. |
| 470 |
|
| 471 |
It also avoids recursive loop when first assigning navigation |
| 472 |
in AssignDefaultNavigation. } |
| 473 |
|
| 474 |
{ do not change NavigationType when |
| 475 |
SetNavigationType is called from ExamineNavigation or WalkNavigation |
| 476 |
that were already called by NavigationType. |
| 477 |
It's actually harmless, but still useless. } |
| 478 |
if FWithinSetNavigationType then |
| 479 |
Exit; |
| 480 |
FWithinSetNavigationType := true; |
| 481 |
|
| 482 |
case Value of |
| 483 |
ntExamine: |
| 484 |
begin |
| 485 |
{$warnings off} // TODO: this should be internal |
| 486 |
E := ExamineNavigation; |
| 487 |
{$warnings on} |
| 488 |
E.Input := TCastleNavigation.DefaultInput; |
| 489 |
E.Turntable := false; |
| 490 |
end; |
| 491 |
ntTurntable: |
| 492 |
begin |
| 493 |
{$warnings off} // TODO: this should be internal |
| 494 |
E := ExamineNavigation; |
| 495 |
{$warnings on} |
| 496 |
E.Input := TCastleNavigation.DefaultInput; |
| 497 |
E.Turntable := true; |
| 498 |
end; |
| 499 |
ntWalk: |
| 500 |
begin |
| 501 |
{$warnings off} // TODO: this should be internal |
| 502 |
W := WalkNavigation; |
| 503 |
{$warnings on} |
| 504 |
W.Input := TCastleNavigation.DefaultInput; |
| 505 |
W.PreferGravityUpForRotations := true; |
| 506 |
W.PreferGravityUpForMoving := true; |
| 507 |
W.Gravity := true; |
| 508 |
end; |
| 509 |
ntFly: |
| 510 |
begin |
| 511 |
{$warnings off} // TODO: this should be internal |
| 512 |
W := WalkNavigation; |
| 513 |
{$warnings on} |
| 514 |
W.Input := TCastleNavigation.DefaultInput; |
| 515 |
W.PreferGravityUpForRotations := true; |
| 516 |
W.PreferGravityUpForMoving := false; |
| 517 |
W.Gravity := false; |
| 518 |
end; |
| 519 |
nt2D: |
| 520 |
begin |
| 521 |
{$warnings off} // using deprecated Navigation, that should be internal |
| 522 |
if Navigation <> Internal2DNavigation then |
| 523 |
begin |
| 524 |
if Navigation <> nil then // copy properties like Input, Radius from current Navigation |
| 525 |
Internal2DNavigation.Assign(Navigation); |
| 526 |
Navigation := Internal2DNavigation; |
| 527 |
end; |
| 528 |
{$warnings on} |
| 529 |
end; |
| 530 |
ntNone: |
| 531 |
begin |
| 532 |
{ Advantage: This way setting NavigationType to ntNone (default NavigationType value) |
| 533 |
will restore Navigation to nil, which is Navigation default value. } |
| 534 |
// Navigation := nil; |
| 535 |
|
| 536 |
{ Advantage: This way of setting NavigationType to ntNone (by making Navigation non-nil) |
| 537 |
explicitly will prevent |
| 538 |
Navigation from being auto-created (in case AutoNavigation remains @true), |
| 539 |
which would make setting "NavigationType := ntNone" moot. } |
| 540 |
{$warnings off} // TODO: this should be internal |
| 541 |
W := WalkNavigation; |
| 542 |
{$warnings on} |
| 543 |
W.Input := []; |
| 544 |
W.Gravity := false; |
| 545 |
end; |
| 546 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 547 |
else raise EInternalError.Create('TCastleViewport.SetNavigationType: Value?'); |
| 548 |
{$endif} |
| 549 |
end; |
| 550 |
|
| 551 |
{ This assertion should be OK. It is commented out only to prevent |
| 552 |
GetNavigationType from accidentally creating something intermediate, |
| 553 |
and thus making debug and release behavior different) } |
| 554 |
// Assert(GetNavigationType = Value); |
| 555 |
|
| 556 |
FWithinSetNavigationType := false; |
| 557 |
|
| 558 |
{ Call OnBoundNavigationInfoChanged when NavigationType changed. |
| 559 |
Use "inherited" to not do own code that would recursively change NavigationType |
| 560 |
(to something else than Value, possibly). } |
| 561 |
inherited BoundNavigationInfoChanged; |
| 562 |
end; |
| 563 |
|
| 564 |
procedure TCastleAutoNavigationViewport.AssignDefaultNavigation; |
| 565 |
var |
| 566 |
Scene: TCastleScene; |
| 567 |
C: TCastleExamineNavigation; |
| 568 |
NewNavigationType: TNavigationType; |
| 569 |
begin |
| 570 |
{$warnings off} // using deprecated in deprecated |
| 571 |
Scene := Items.MainScene; |
| 572 |
if Scene <> nil then |
| 573 |
begin |
| 574 |
NewNavigationType := NavigationTypeFromNavigationInfo(Scene); |
| 575 |
|
| 576 |
{ Set Navigation explicitly, otherwise SetNavigationType below could call |
| 577 |
ExamineNavigation / WalkNavigation that call AssignDefaultNavigation when Navigation = nil, |
| 578 |
and we would have infinite AssignDefaultNavigation calls loop. } |
| 579 |
case NewNavigationType of |
| 580 |
ntExamine, ntTurntable: |
| 581 |
Navigation := InternalExamineNavigation; |
| 582 |
nt2D: |
| 583 |
Navigation := Internal2DNavigation; |
| 584 |
else |
| 585 |
Navigation := InternalWalkNavigation; |
| 586 |
end; |
| 587 |
|
| 588 |
NavigationType := NewNavigationType; |
| 589 |
|
| 590 |
{ Call InternalUpdateNavigation on all internal navigation instances, |
| 591 |
not only on current Navigation, to keep all properties, even the ones specific |
| 592 |
to a class (like TCastleWalkNavigation.PreferredHeight) always up-to-date. } |
| 593 |
Scene.InternalUpdateNavigation(InternalExamineNavigation); |
| 594 |
Scene.InternalUpdateNavigation(InternalWalkNavigation); |
| 595 |
Scene.InternalUpdateNavigation(Internal2DNavigation); |
| 596 |
end else |
| 597 |
begin |
| 598 |
C := InternalExamineNavigation; |
| 599 |
C.Radius := DefaultCameraRadius; |
| 600 |
Navigation := C; |
| 601 |
end; |
| 602 |
{$warnings on} |
| 603 |
end; |
| 604 |
|
| 605 |
procedure TCastleAutoNavigationViewport.Notification(AComponent: TComponent; Operation: TOperation); |
| 606 |
begin |
| 607 |
inherited; |
| 608 |
|
| 609 |
if Operation = opRemove then |
| 610 |
begin |
| 611 |
// Note that we don't register on FInternalExamine/WalkNavigation destruction |
| 612 |
// when they are not current, so they should never be freed in that case. |
| 613 |
if AComponent = FInternalWalkNavigation then |
| 614 |
FInternalWalkNavigation := nil; |
| 615 |
if AComponent = FInternalExamineNavigation then |
| 616 |
FInternalExamineNavigation := nil; |
| 617 |
if AComponent = FInternal2DNavigation then |
| 618 |
FInternal2DNavigation := nil; |
| 619 |
end; |
| 620 |
end; |
| 621 |
|
| 622 |
procedure TCastleAutoNavigationViewport.SetAutoNavigation(const Value: Boolean); |
| 623 |
begin |
| 624 |
if FAutoNavigation <> Value then |
| 625 |
begin |
| 626 |
FAutoNavigation := Value; |
| 627 |
{ Not necessary, and we actually don't have AssignDefaultNavigationDone. |
| 628 |
The navigation will be auto-assigned when it is nil. } |
| 629 |
// if InternalDesignManipulation and Value then |
| 630 |
// AssignDefaultNavigationDone := false; |
| 631 |
end; |
| 632 |
end; |
| 633 |
|
| 634 |
procedure TCastleAutoNavigationViewport.BoundNavigationInfoChanged; |
| 635 |
begin |
| 636 |
{ Use InsideBoundNavigationInfoChanged to prevent infinite loop |
| 637 |
of BoundNavigationInfoChanged calls, as "NavigationType := " causes |
| 638 |
BoundNavigationInfoChanged. } |
| 639 |
if not InsideBoundNavigationInfoChanged then |
| 640 |
begin |
| 641 |
InsideBoundNavigationInfoChanged := true; |
| 642 |
{$warnings off} // using deprecated to keep it working |
| 643 |
if AutoNavigation and (Navigation <> nil) and (Items.MainScene <> nil) then |
| 644 |
begin |
| 645 |
NavigationType := NavigationTypeFromNavigationInfo(Items.MainScene); |
| 646 |
Items.MainScene.InternalUpdateNavigation(Navigation); |
| 647 |
|
| 648 |
{ Call InternalUpdateNavigation on all internal navigation instances, |
| 649 |
to keep all properties, even the ones not copied by Assign (because specific |
| 650 |
to a class, like TCastleWalkNavigation.PreferredHeight) always up-to-date. } |
| 651 |
if Navigation <> InternalWalkNavigation then |
| 652 |
Items.MainScene.InternalUpdateNavigation(InternalWalkNavigation); |
| 653 |
if Navigation <> InternalExamineNavigation then |
| 654 |
Items.MainScene.InternalUpdateNavigation(InternalExamineNavigation); |
| 655 |
if Navigation <> Internal2DNavigation then |
| 656 |
Items.MainScene.InternalUpdateNavigation(Internal2DNavigation); |
| 657 |
end; |
| 658 |
{$warnings on} |
| 659 |
InsideBoundNavigationInfoChanged := false; |
| 660 |
end; |
| 661 |
inherited; |
| 662 |
end; |
| 663 |
|
| 664 |
procedure TCastleAutoNavigationViewport.ApplyProjection; |
| 665 |
begin |
| 666 |
{$warnings off} // using deprecated to keep it working |
| 667 |
if AutoNavigation and (Navigation = nil) then |
| 668 |
AssignDefaultNavigation; // create Navigation if necessary |
| 669 |
{$warnings on} |
| 670 |
inherited; |
| 671 |
end; |
| 672 |
|
| 673 |
{$endif read_implementation} |