| 1 |
{%MainUnit castleuicontrols.pas} |
| 2 |
{ |
| 3 |
Copyright 2010-2024 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 |
|
| 19 |
//type |
| 20 |
|
| 21 |
{ Abstract user interface container. Connects OpenGL context management |
| 22 |
code with Castle Game Engine controls (TCastleUserInterface, that is the basis |
| 23 |
for all our 2D and 3D rendering). When you use TCastleWindow |
| 24 |
(a window) or TCastleControl (Lazarus component), they provide |
| 25 |
you a non-abstract implementation of TCastleContainer. |
| 26 |
|
| 27 |
Basically, this class manages a @link(Controls) list. |
| 28 |
|
| 29 |
We pass our inputs (mouse / key / touch events) to the controls |
| 30 |
on this list. Input goes to the front-most |
| 31 |
(that is, last on the @link(Controls) list) control under |
| 32 |
the event position (or mouse position, or the appropriate touch position). |
| 33 |
We use @link(TCastleUserInterface.CapturesEventsAtPosition) to decide this |
| 34 |
(by default it simply checks control's @link(TCastleUserInterface.RenderRect) |
| 35 |
vs the given position). |
| 36 |
As long as the event is not handled, |
| 37 |
we search for the next control that can handle this event and |
| 38 |
returns @link(TCastleUserInterface.CapturesEventsAtPosition) = @true. |
| 39 |
|
| 40 |
We also call various methods to every control. |
| 41 |
These include @link(TCastleUserInterface.Update), @link(TCastleUserInterface.Render), |
| 42 |
@link(TCastleUserInterface.Resize). } |
| 43 |
TCastleContainer = class abstract(TComponent) |
| 44 |
strict private |
| 45 |
type |
| 46 |
TFingerIndexCaptureMap = {$ifdef FPC}specialize{$endif} TDictionary<TFingerIndex, TCastleUserInterface>; |
| 47 |
var |
| 48 |
FOnOpen, FOnClose: TContainerEvent; |
| 49 |
FOnOpenObject, FOnCloseObject: TContainerObjectEvent; |
| 50 |
FOnBeforeRender, FOnRender: TContainerEvent; |
| 51 |
FOnResize: TContainerEvent; |
| 52 |
FOnPress, FOnRelease: TInputPressReleaseEvent; |
| 53 |
FOnMotion: TInputMotionEvent; |
| 54 |
FOnUpdate: TContainerEvent; |
| 55 |
FFocus, FNewFocus: TCastleUserInterfaceList; |
| 56 |
{ Capture controls, for each FingerIndex. |
| 57 |
The values in this map are never nil. } |
| 58 |
FCaptureInput: TFingerIndexCaptureMap; |
| 59 |
FForceCaptureInput: TCastleUserInterface; |
| 60 |
FForceCaptureInputObserver: TFreeNotificationObserver; |
| 61 |
FTooltipDelay: Single; |
| 62 |
FTooltipDistance: Cardinal; |
| 63 |
FTooltipPosition: TVector2; |
| 64 |
FTooltipParent: TCastleUserInterface; // @nil if do not display |
| 65 |
HasLastPositionForTooltip: Boolean; |
| 66 |
LastPositionForTooltip: TVector2; |
| 67 |
LastPositionForTooltipTime: TTimerResult; |
| 68 |
Mouse3d: T3DConnexionDevice; |
| 69 |
Mouse3dPollTimer: Single; |
| 70 |
FDpi: Single; |
| 71 |
FUIScaling: TUIScaling; |
| 72 |
FUIReferenceWidth: Single; |
| 73 |
FUIReferenceHeight: Single; |
| 74 |
FUIExplicitScale: Single; |
| 75 |
FUIScale: Single; |
| 76 |
FFps: TFramesPerSecond; |
| 77 |
FPressed: TKeysPressed; |
| 78 |
FMousePressed: TCastleMouseButtons; |
| 79 |
FMouseLookIgnoreNextMotion: Boolean; |
| 80 |
FMouseLookWaitingForMiddle: Boolean; |
| 81 |
FMouseLookMotionToSubtract: TVector2; |
| 82 |
FFocusAndMouseCursorValid: Boolean; |
| 83 |
FOverrideCursor: TMouseCursor; |
| 84 |
FDefaultFont: TCastleAbstractFont; |
| 85 |
FContext: TRenderContext; |
| 86 |
FBackgroundEnable: Boolean; |
| 87 |
FBackgroundColor: TCastleColor; |
| 88 |
FInspector: TCastleUserInterface; //< always TCastleInspector |
| 89 |
// SafeBorder, without UIScale applied, in CSS order (top, right, bottom, left) |
| 90 |
FSafeBorderScaled: TVector4; |
| 91 |
FSafeBorder: TBorder; |
| 92 |
FOnSafeBorderChanged: TNotifyEvent; |
| 93 |
|
| 94 |
function GetView: TCastleView; |
| 95 |
procedure SetView(const Value: TCastleView); |
| 96 |
function GetFrontView: TCastleView; |
| 97 |
function GetViewStack(const Index: Integer): TCastleView; |
| 98 |
|
| 99 |
function UseForceCaptureInput: Boolean; |
| 100 |
function TryGetFingerOfControl(const C: TCastleUserInterface; out Finger: TFingerIndex): Boolean; |
| 101 |
procedure SetDpi(const Value: Single); |
| 102 |
procedure SetUIScaling(const Value: TUIScaling); |
| 103 |
procedure SetUIReferenceWidth(const Value: Single); |
| 104 |
procedure SetUIReferenceHeight(const Value: Single); |
| 105 |
procedure SetUIExplicitScale(const Value: Single); |
| 106 |
procedure UpdateUIScale; |
| 107 |
procedure SetForceCaptureInput(const Value: TCastleUserInterface); |
| 108 |
function PassEvents(const C: TCastleUserInterface; |
| 109 |
const CheckMousePosition: Boolean = true): Boolean; overload; |
| 110 |
function PassEvents(const C: TCastleUserInterface; |
| 111 |
const EventPosition: TVector2; |
| 112 |
const CheckEventPosition: Boolean = true): Boolean; overload; |
| 113 |
function PassEvents(const C: TCastleUserInterface; |
| 114 |
const Event: TInputPressRelease; |
| 115 |
const CheckEventPosition: Boolean = true): Boolean; overload; |
| 116 |
function PassEvents(const C: TCastleUserInterface; |
| 117 |
const Event: TInputMotion; |
| 118 |
const CheckEventPosition: Boolean = true): Boolean; overload; |
| 119 |
function GetInspectorKey: TKey; |
| 120 |
procedure SetInspectorKey(const Value: TKey); |
| 121 |
{ Create / destroy FInspector. } |
| 122 |
procedure ToggleInspector; |
| 123 |
procedure ForceCaptureInputFreeNotification(const Sender: TFreeNotificationObserver); |
| 124 |
function MessageReceived(const Received: TCastleStringList; |
| 125 |
const ReceivedStream: TMemoryStream): Boolean; |
| 126 |
private |
| 127 |
var |
| 128 |
FViewStack: TCastleViewList; |
| 129 |
FDisableStackChange: Cardinal; |
| 130 |
FControls: TInternalChildrenControls; |
| 131 |
class var |
| 132 |
FInputInspector: TInputInspector; |
| 133 |
procedure ControlsVisibleChange(const Sender: TCastleUserInterface; |
| 134 |
const Changes: TCastleUserInterfaceChanges; const ChangeInitiatedByChildren: Boolean); |
| 135 |
class procedure RenderControlPrepare(const ViewportRect: TRectangle); |
| 136 |
{ Called when the control C is destroyed or just removed from Controls list. } |
| 137 |
procedure DetachNotification(const C: TCastleUserInterface); |
| 138 |
protected |
| 139 |
procedure Notification(AComponent: TComponent; Operation: TOperation); override; |
| 140 |
|
| 141 |
{ These properties are protected, |
| 142 |
they should only be get/set by a container owner, |
| 143 |
like TCastleWindow or TCastleControl. |
| 144 |
|
| 145 |
@deprecated These events, just like similar public events |
| 146 |
at TCastleWindow or TCastleControl, are deprecated. |
| 147 |
Because |
| 148 |
|
| 149 |
1. Most of them are pointers to global routines, not methods ("of object") |
| 150 |
and we generally prefer to have pointers to methods ("of object"). |
| 151 |
|
| 152 |
2. We advise to manage things by overriding methods, |
| 153 |
or using events, of TCastleUserInterface and descendants. |
| 154 |
This includes using methods of TCastleView. |
| 155 |
|
| 156 |
@groupBegin } |
| 157 |
property OnOpen: TContainerEvent read FOnOpen write FOnOpen; |
| 158 |
property OnOpenObject: TContainerObjectEvent read FOnOpenObject write FOnOpenObject; |
| 159 |
property OnBeforeRender: TContainerEvent read FOnBeforeRender write FOnBeforeRender; |
| 160 |
property OnRender: TContainerEvent read FOnRender write FOnRender; |
| 161 |
property OnResize: TContainerEvent read FOnResize write FOnResize; |
| 162 |
property OnClose: TContainerEvent read FOnClose write FOnClose; |
| 163 |
property OnCloseObject: TContainerObjectEvent read FOnCloseObject write FOnCloseObject; |
| 164 |
property OnPress: TInputPressReleaseEvent read FOnPress write FOnPress; |
| 165 |
property OnRelease: TInputPressReleaseEvent read FOnRelease write FOnRelease; |
| 166 |
property OnMotion: TInputMotionEvent read FOnMotion write FOnMotion; |
| 167 |
property OnUpdate: TContainerEvent read FOnUpdate write FOnUpdate; |
| 168 |
{ @groupEnd } |
| 169 |
|
| 170 |
procedure SetInternalCursor(const Value: TMouseCursor); virtual; |
| 171 |
{$ifdef FPC} |
| 172 |
property Cursor: TMouseCursor write SetInternalCursor; |
| 173 |
deprecated 'do not set this, engine will override this. Set TCastleUserInterface.Cursor of your UI controls to control the Cursor.'; |
| 174 |
{$endif} |
| 175 |
property InternalCursor: TMouseCursor write SetInternalCursor; |
| 176 |
|
| 177 |
function GetMousePosition: TVector2; virtual; |
| 178 |
procedure SetMousePosition(const Value: TVector2); virtual; |
| 179 |
function GetTouches(const Index: Integer): TTouch; virtual; |
| 180 |
|
| 181 |
{ Does setting MousePosition also causes Motion / OnMotion events. |
| 182 |
While we tried to make everything work reliably always, |
| 183 |
the mouse look logic just needs to know this. |
| 184 |
|
| 185 |
It is easy to test it in practice. |
| 186 |
Just run examples/window/window_events.lpr, |
| 187 |
move mouse around, and press "5" (this does "MousePosition := window middle"). } |
| 188 |
function SettingMousePositionCausesMotion: Boolean; virtual; |
| 189 |
public |
| 190 |
var |
| 191 |
{ Optimize rendering of user interface. |
| 192 |
This activates batching of TDrawableImage around whole rendering, |
| 193 |
and TDrawableImage is used for most UI rendering (labels, images). |
| 194 |
|
| 195 |
Where it works, where it doesn't: |
| 196 |
The automatic batching offered by @link(TDrawableImage.BatchingBegin) |
| 197 |
and @link(TDrawableImage.BatchingEnd) gives a benefit |
| 198 |
when the same TDrawableImage is rendered multiple times in a row, |
| 199 |
with the same parameters. Consider these examples: |
| 200 |
|
| 201 |
@unorderedList( |
| 202 |
@item(The batching helps if you have multiple TCastleImageControl |
| 203 |
(rendered one after another) with the same image. |
| 204 |
|
| 205 |
Note that by default TCastleImageControl |
| 206 |
(actually TCastleImagePersistent underneath) |
| 207 |
does caching so same image URL -> same TDrawableImage instance. |
| 208 |
This is good for batching. |
| 209 |
) |
| 210 |
|
| 211 |
@item(The batching helps if you have multiple TCastleLabel instances |
| 212 |
rendered one after another, |
| 213 |
and/or they have multiline text, |
| 214 |
all using the same font. |
| 215 |
) |
| 216 |
|
| 217 |
@item(The batching @italic(will not help) for typical TCastleButton, |
| 218 |
as a button is typically some image (background) + some text, |
| 219 |
so the TDrawableImage drawn switches between background / text, |
| 220 |
and nothing gets batched. |
| 221 |
) |
| 222 |
) |
| 223 |
|
| 224 |
Observe @code(TDrawableImage.Statistics.ToString) to actually measure |
| 225 |
how much you gain by this. |
| 226 |
See example @code(examples/user_interface/ui_batching/). } |
| 227 |
UserInterfaceBatching: Boolean; |
| 228 |
|
| 229 |
const |
| 230 |
DefaultBackgroundColor: TVector4 = (X: 0.1; Y: 0.1; Z: 0.1; W: 1); |
| 231 |
|
| 232 |
constructor Create(AOwner: TComponent); override; |
| 233 |
destructor Destroy; override; |
| 234 |
|
| 235 |
{ Propagate the event to all the @link(Controls). |
| 236 |
You can call these methods explicitly if you want to "fake" some event, |
| 237 |
e.g. fake pressing or releasing a key by calling @link(EventPress) or |
| 238 |
@link(EventRelease). |
| 239 |
|
| 240 |
Deprecated: these methods also call our (deprecated) callbacks like OnPress. |
| 241 |
|
| 242 |
These methods are called automatically by TCastleWindow or TCastleControl. |
| 243 |
So if you don't need to "fake" any event, you should not call them from |
| 244 |
your application. |
| 245 |
|
| 246 |
Note: @link(EventUpdate) is special in that it also calls (for now) |
| 247 |
@link(EventJoyAxisMove), |
| 248 |
@link(EventJoyButtonPress), |
| 249 |
@link(EventSensorRotation), |
| 250 |
@link(EventSensorTranslation). |
| 251 |
|
| 252 |
InitializedContextsCount should specify the number of initialized contexts |
| 253 |
at this point. This is always >= 1 since the current context is always |
| 254 |
initialized at the point of this call. |
| 255 |
So for EventOpen, InitializedContextsCount = 1 means we initialize |
| 256 |
1st context, and for EventClose, InitializedContextsCount = 1 means we |
| 257 |
finalize last context. |
| 258 |
|
| 259 |
@groupBegin } |
| 260 |
procedure EventOpen(const InitializedContextsCount: Cardinal); virtual; |
| 261 |
procedure EventClose(const InitializedContextsCount: Cardinal); virtual; |
| 262 |
function EventPress(const Event: TInputPressRelease): Boolean; virtual; |
| 263 |
function EventRelease(const Event: TInputPressRelease): Boolean; virtual; |
| 264 |
procedure EventUpdate; virtual; |
| 265 |
procedure EventMotion(const Event: TInputMotion); virtual; |
| 266 |
function AllowSuspendForInput: Boolean; |
| 267 |
procedure EventBeforeRender; virtual; |
| 268 |
procedure EventRender; virtual; |
| 269 |
procedure EventResize; virtual; |
| 270 |
function EventJoyAxisMove(const JoyID, Axis: Byte): Boolean; virtual; |
| 271 |
function EventJoyButtonPress(const JoyID, Button: Byte): Boolean; virtual; |
| 272 |
function EventSensorRotation(const X, Y, Z, Angle: Double; const SecondsPassed: Single): Boolean; virtual; |
| 273 |
function EventSensorTranslation(const X, Y, Z, Length: Double; const SecondsPassed: Single): Boolean; virtual; |
| 274 |
{ @groupEnd } |
| 275 |
|
| 276 |
{ Dots (pixels) per inch. Describes how many pixels fit on a physical inch. |
| 277 |
So this is determined by the screen resolution in pixels, |
| 278 |
and by the physical size of the device. |
| 279 |
|
| 280 |
Some systems may expose a value that actually reflects user preference |
| 281 |
"how to scale the user-interface", where 96 (DefaultDpi) is default. |
| 282 |
So do not depend that it is actually related to the physical monitor size. |
| 283 |
See https://developer.gnome.org/gdk2/stable/GdkScreen.html#gdk-screen-set-resolution . } |
| 284 |
property Dpi: Single read FDpi write SetDpi {$ifdef FPC}default DefaultDpi{$endif}; |
| 285 |
|
| 286 |
{ Get the default UI scale of controls. |
| 287 |
Useful only when GLInitialized, when we know that our size is sensible. |
| 288 |
|
| 289 |
UI code should prefer to use TCastleUserInterface.UIScale, |
| 290 |
not directly accessing this TCastleContainer.UIScale |
| 291 |
(this way the code respects the TCastleUserInterface.EnableUIScaling value). } |
| 292 |
property UIScale: Single read FUIScale; |
| 293 |
|
| 294 |
{ @exclude } |
| 295 |
class function InternalCalculateUIScale( |
| 296 |
const AUIScaling: TUIScaling; |
| 297 |
AUIReferenceWidth, AUIReferenceHeight: Single; |
| 298 |
const AUIExplicitScale: Single; |
| 299 |
const ADpi, AWidth, AHeight: Single): Single; |
| 300 |
|
| 301 |
{ Controls listening for events (user input, resize, and such) of this container. |
| 302 |
|
| 303 |
Usually you explicitly add / remove controls to this list |
| 304 |
using the @link(TInternalChildrenControls.InsertFront Controls.InsertFront) or |
| 305 |
@link(TInternalChildrenControls.InsertBack Controls.InsertBack) methods. |
| 306 |
Freeing any control that is on this list |
| 307 |
automatically removes it from this list (we use the TComponent.Notification |
| 308 |
mechanism). |
| 309 |
|
| 310 |
Controls on the list should be specified in back-to-front order. |
| 311 |
That is, controls at the beginning of this list |
| 312 |
are rendered first, and are last to catch some events, since the rest |
| 313 |
of controls cover them. } |
| 314 |
function Controls: TInternalChildrenControls; |
| 315 |
|
| 316 |
{ Returns the controls that should receive input events, |
| 317 |
from back to front. So the front-most control, that should receive events first, |
| 318 |
is last on this list. } |
| 319 |
property Focus: TCastleUserInterfaceList read FFocus; |
| 320 |
|
| 321 |
{ First control that receives input events, determined by @link(Focus) list |
| 322 |
and @link(ForceCaptureInput). |
| 323 |
@nil if none. |
| 324 |
|
| 325 |
Be careful when using this: most logic should account for the fact |
| 326 |
that multiple controls may be focused, that's why @link(Focus) is a list. |
| 327 |
Use this only when you need to know the front-most focused control. } |
| 328 |
function FocusFront: TCastleUserInterface; |
| 329 |
|
| 330 |
{ Redraw the contents of of this window, at the nearest suitable time. |
| 331 |
This method does not redraw immediately |
| 332 |
(it does not call @link(EventBeforeRender) and @link(EventRender) inside), |
| 333 |
it only makes sure that they will be called @italic(very soon). |
| 334 |
Calling this on a closed container (with GLInitialized = @false) |
| 335 |
is allowed and ignored. } |
| 336 |
procedure Invalidate; virtual; |
| 337 |
|
| 338 |
{ Is the OpenGL context initialized. } |
| 339 |
function GLInitialized: Boolean; virtual; |
| 340 |
|
| 341 |
{ Container size, in pixels. |
| 342 |
This is expressed in real device pixels. |
| 343 |
Prefer using @link(UnscaledWidth) instead of this. @link(UnscaledWidth) |
| 344 |
is more natural when you use UI scaling (@link(UIScaling)), |
| 345 |
and it's simply equal to @name when UI scaling is not used. } |
| 346 |
function PixelsWidth: Integer; virtual; abstract; |
| 347 |
|
| 348 |
{ Container size, in pixels. |
| 349 |
This is expressed in real device pixels. |
| 350 |
Prefer using @link(UnscaledHeight) instead of this. @link(UnscaledHeight) |
| 351 |
is more natural when you use UI scaling (@link(UIScaling)), |
| 352 |
and it's simply equal to @name when UI scaling is not used. } |
| 353 |
function PixelsHeight: Integer; virtual; abstract; |
| 354 |
|
| 355 |
{ Container size, in pixels. |
| 356 |
This is expressed in real device pixels, using @link(Width) and @link(Height). |
| 357 |
Prefer using @link(UnscaledRect) instead of this. @link(UnscaledRect) |
| 358 |
is more natural when you use UI scaling (@link(UIScaling)), |
| 359 |
and it's simply equal to @name when UI scaling is not used. } |
| 360 |
function PixelsRect: TRectangle; virtual; |
| 361 |
|
| 362 |
function Width: Integer; deprecated 'use PixelsWidth or UnscaledWidth, depending on what you need; for backward compatibility, "Width" is equal to "PixelsWidth" now, but it will change to "UnscaledWidth" in the future'; |
| 363 |
function Height: Integer; deprecated 'use PixelsHeight or UnscaledHeight, depending on what you need; for backward compatibility, "Height" is equal to "PixelsHeight" now, but it will change to "UnscaledHeight" in the future'; |
| 364 |
function Rect: TRectangle; deprecated 'use PixelsRect or UnscaledRect, depending on what you need; for backward compatibility, "Rect" is equal to "PixelsRect" now, but it will change to "UnscaledRect" in the future'; |
| 365 |
|
| 366 |
{ Translucent status bar height in the container, in pixels. |
| 367 |
This is expressed in real device pixels. |
| 368 |
|
| 369 |
Prefer using @link(SafeBorder) instead of this, |
| 370 |
@link(SafeBorder) accounts for all 4 sides and |
| 371 |
is in size accounting for UI scaling. } |
| 372 |
function ScaledStatusBarHeight: Cardinal; deprecated 'use SafeBorder'; |
| 373 |
|
| 374 |
{ Container width as seen by controls with UI scaling. |
| 375 |
In other words, this is the real @link(Width) with UI scaling |
| 376 |
reversed (divided). Suitable to adjust size of your UI controls |
| 377 |
to container, when UI scaling is used. |
| 378 |
|
| 379 |
This is equivalent to just @link(Width) when UIScaling is usNone |
| 380 |
(default). |
| 381 |
|
| 382 |
Note: the name "unscaled" may seem a little unintuitive, but it's consistent. |
| 383 |
We call UI sizes "scaled" when they are expressed in real device pixels, |
| 384 |
because they are usually calculated as "desired size * UIScaling". |
| 385 |
So the UI size is "unscaled" when it's expressed in your "desired size". |
| 386 |
We usually don't use the prefix "unscaled" (e.g. @link(TCastleUserInterface.Width) |
| 387 |
is "unscaled" by we don't call it "UnscaledWidth"). But here, we use prefix "unscaled", |
| 388 |
because the @link(TCastleContainer.Width) is (for historic reasons) the "real" size. |
| 389 |
|
| 390 |
@seealso UnscaledHeight } |
| 391 |
function UnscaledWidth: Single; |
| 392 |
|
| 393 |
{ Container height as seen by controls with UI scaling. |
| 394 |
@seealso UnscaledWidth } |
| 395 |
function UnscaledHeight: Single; |
| 396 |
|
| 397 |
{ Container rectangle as seen by controls with UI scaling. |
| 398 |
@seealso UnscaledWidth } |
| 399 |
function UnscaledRect: TFloatRectangle; |
| 400 |
|
| 401 |
{ Translucent status bar height inside the container as seen by controls |
| 402 |
with UI scaling. |
| 403 |
|
| 404 |
Status bar occupies the top part of the container height. Invisible |
| 405 |
status bar returns height equal zero. |
| 406 |
|
| 407 |
Prefer using @link(SafeBorder) instead of this, |
| 408 |
@link(SafeBorder) accounts for all 4 sides and |
| 409 |
(just like this) is also in size accounting for UI scaling. |
| 410 |
|
| 411 |
@seealso UnscaledWidth } |
| 412 |
function StatusBarHeight: Single; deprecated 'use SafeBorder'; |
| 413 |
|
| 414 |
{ Current mouse position. |
| 415 |
See @link(TTouch.Position) for a documentation how this is expressed. } |
| 416 |
property MousePosition: TVector2 |
| 417 |
read GetMousePosition write SetMousePosition; |
| 418 |
|
| 419 |
{ Currently pressed mouse buttons. When this changes, you're always |
| 420 |
notified by @link(OnPress) or @link(OnRelease) events. |
| 421 |
|
| 422 |
This value is always current, in particular it's already updated |
| 423 |
before we call events @link(OnPress) or @link(OnRelease). } |
| 424 |
property MousePressed: TCastleMouseButtons read FMousePressed write FMousePressed; |
| 425 |
|
| 426 |
{ Is the window focused now, which means that keys/mouse events |
| 427 |
are directed to this window. } |
| 428 |
function Focused: Boolean; virtual; |
| 429 |
|
| 430 |
{ Keys currently pressed. } |
| 431 |
property Pressed: TKeysPressed read FPressed; |
| 432 |
|
| 433 |
{ Measures application speed. } |
| 434 |
property Fps: TFramesPerSecond read FFps; |
| 435 |
|
| 436 |
{ Currently active touches on the screen. |
| 437 |
This tracks currently pressed fingers, in case of touch devices |
| 438 |
(mobile, like Android and iOS). |
| 439 |
In case of desktops, it tracks the current mouse position, |
| 440 |
regardless if any mouse button is currently pressed. |
| 441 |
|
| 442 |
Indexed from 0 to TouchesCount - 1. |
| 443 |
@seealso TouchesCount |
| 444 |
@seealso TTouch } |
| 445 |
property Touches[const Index: Integer]: TTouch read GetTouches; |
| 446 |
|
| 447 |
{ Count of currently active touches (mouse or fingers pressed) on the screen. |
| 448 |
@seealso Touches } |
| 449 |
function TouchesCount: Integer; virtual; |
| 450 |
|
| 451 |
property Context: TRenderContext read FContext; |
| 452 |
|
| 453 |
{ Render a TCastleUserInterface (along with all it's children). |
| 454 |
|
| 455 |
This method can be used to render UI control into an image, |
| 456 |
@link(TDrawableImage), when it is surrounded by |
| 457 |
@link(TDrawableImage.RenderToImageBegin) |
| 458 |
and @link(TDrawableImage.RenderToImageEnd). |
| 459 |
See CGE examples/viewport_and_scenes/render_3d_to_image/ . |
| 460 |
|
| 461 |
It can also be used with more low-level @link(TGLRenderToTexture). |
| 462 |
See CGE examples/viewport_and_scenes/render_3d_to_texture_and_use_as_quad/ . |
| 463 |
|
| 464 |
This is a good method to render the UI control off-screen. |
| 465 |
It can render any UI control, including e.g. TCastleViewport |
| 466 |
with 3D stuff inside TCastleScene. |
| 467 |
|
| 468 |
The contents of the @link(Controls) list doesn't matter for this method. |
| 469 |
In particular, it doesn't matter if the Control (given as a parameter) |
| 470 |
is present on the list of current @link(Controls). |
| 471 |
This method explicitly renders the given Control parameter (and it's children), |
| 472 |
nothing more, nothing less. |
| 473 |
|
| 474 |
More details what this method does: |
| 475 |
|
| 476 |
@unorderedList( |
| 477 |
@item(Temporarily sets |
| 478 |
@link(TCastleUserInterface.Container Control.Container), if needed.) |
| 479 |
|
| 480 |
@item(Makes sure OpenGL resources of the control are initialized. |
| 481 |
If needed, it calls |
| 482 |
@link(TCastleUserInterface.GLContextOpen Control.GLContextOpen) and |
| 483 |
@link(TCastleUserInterface.GLContextClose Control.GLContextClose) |
| 484 |
around. |
| 485 |
This is needed when you want to perform off-screen rendering, |
| 486 |
but the control's OpenGL resources are not initialized yet, |
| 487 |
e.g. because it is not present on the @link(Controls) list. |
| 488 |
|
| 489 |
Note that doing this repeatedly may be a slowdown |
| 490 |
(how much, it depends on the actual TCastleUserInterface |
| 491 |
-- some controls do nothing in TCastleUserInterface.GLContextOpen, |
| 492 |
some controls do a lot). |
| 493 |
If you want to repeatedly call @link(RenderControl) on the |
| 494 |
same Control, it is more efficient |
| 495 |
to first explicitly create it's OpenGL resources, |
| 496 |
e.g. by calling |
| 497 |
@link(TCastleUserInterface.GLContextOpen Control.GLContextOpen) explicitly. |
| 498 |
Or adding the control to the @link(Controls) list. |
| 499 |
) |
| 500 |
|
| 501 |
@item(Calls @link(TCastleUserInterface.Resize Control.Resize), |
| 502 |
which may be expected by some controls.) |
| 503 |
|
| 504 |
@item(Calls @link(TCastleUserInterface.BeforeRender Control.BeforeRender), |
| 505 |
which may be expected by some controls.) |
| 506 |
) |
| 507 |
} |
| 508 |
procedure RenderControl(const Control: TCastleUserInterface; |
| 509 |
const ViewportRect: TRectangle); |
| 510 |
|
| 511 |
{ Capture the current container (window) contents to an image |
| 512 |
(or straight to an image file, like png). |
| 513 |
|
| 514 |
Note that only capturing from the double-buffered OpenGL |
| 515 |
windows (which the default for our TCastleWindow and TCastleControl) |
| 516 |
is reliable. Internally, these methods may need to redraw the screen |
| 517 |
to the back buffer, because that's the only guaranteed way to capture |
| 518 |
OpenGL drawing (you have to capture the back buffer, before swap). |
| 519 |
|
| 520 |
@groupBegin } |
| 521 |
procedure SaveScreen(const Url: String); overload; |
| 522 |
function SaveScreen: TRGBImage; overload; |
| 523 |
function SaveScreen(const SaveRect: TRectangle): TRGBImage; overload; virtual; |
| 524 |
function SaveScreen(const SaveRect: TFloatRectangle): TRGBImage; overload; |
| 525 |
{ @groupEnd } |
| 526 |
|
| 527 |
{ Capture the current container (window) contents to an image with alpha. |
| 528 |
|
| 529 |
An example: |
| 530 |
@includeCode(../../examples/short_api_samples/save_screen_rgba/save_screen_rgba.dpr) |
| 531 |
@groupBegin } |
| 532 |
function SaveScreenRgba(const SaveRect: TRectangle): TRGBAlphaImage; overload; |
| 533 |
function SaveScreenRgba: TRGBAlphaImage; overload; |
| 534 |
{ @groupEnd } |
| 535 |
|
| 536 |
{ Capture the current container (window) contents to an image and save it to file, |
| 537 |
following the current platform/user preferred directory to store screenshots. |
| 538 |
|
| 539 |
On Windows, this saves files to user's "My Pictures" directory. |
| 540 |
On Unix (using freedesktop standard) this saves files to directory like ~/Pictures . |
| 541 |
On macOS, this saves files to the home directory right now. |
| 542 |
On other platforms, it may follow the most established convention, |
| 543 |
or abort if no place (where we have permissions to store screenshots) exists. |
| 544 |
|
| 545 |
You can use SaveScreenPath yourself to have more control over the target location. |
| 546 |
|
| 547 |
Returns the saved file URL, so that you can e.g. show it to user. } |
| 548 |
function SaveScreenToDefaultFile: String; |
| 549 |
|
| 550 |
{ This is internal, and public only for historic reasons. |
| 551 |
@exclude |
| 552 |
|
| 553 |
Called by controls within this container when something could |
| 554 |
change the container focused control (in @link(TCastleContainer.Focus)) |
| 555 |
(or it's cursor) or @link(TCastleContainer.Focused) or MouseLook. |
| 556 |
In practice, called when TCastleUserInterface.Cursor or |
| 557 |
@link(TCastleUserInterface.CapturesEventsAtPosition) (and so also |
| 558 |
@link(TCastleUserInterface.RenderRect)) results change. |
| 559 |
|
| 560 |
In practice, it's called through VisibleChange now. |
| 561 |
|
| 562 |
This recalculates the focused control and the final cursor of |
| 563 |
the container, looking at Container's Controls, |
| 564 |
testing @link(TCastleUserInterface.CapturesEventsAtPosition) with current mouse position, |
| 565 |
and looking at Cursor property of various controls. |
| 566 |
|
| 567 |
When you add / remove some control |
| 568 |
from the Controls list, or when you move mouse (focused changes) |
| 569 |
this will also be automatically called |
| 570 |
(since focused control or final container cursor may also change then). } |
| 571 |
procedure UpdateFocusAndMouseCursor; |
| 572 |
|
| 573 |
{ Internal. @exclude |
| 574 |
See @link(MouseLookDelta). } |
| 575 |
procedure MouseLookIgnoreNextMotion; |
| 576 |
|
| 577 |
{ For tracking mouse look. See @link(MouseLookDelta). } |
| 578 |
procedure MouseLookPress; |
| 579 |
|
| 580 |
{ For tracking mouse look. See @link(MouseLookDelta). } |
| 581 |
procedure MouseLookUpdate; |
| 582 |
|
| 583 |
{ Read mouse position delta from ControlRect middle, |
| 584 |
and try to set mouse position to ControlRect middle. |
| 585 |
|
| 586 |
ControlRect is usually the container rectangle (in final device coordinates) |
| 587 |
but in general it should be the rectangle of control over which we should do mouse look |
| 588 |
(so TCastleUserInterface.RenderRect). |
| 589 |
|
| 590 |
This can be used to perform "mouse look" or a similar effect, |
| 591 |
when user doesn't see the mouse cursor, but user can move something by dragging with mouse. |
| 592 |
Moreover, user should not notice any "bounds" to this dragging (that's why we try |
| 593 |
to keep mouse position in ControlRect middle, |
| 594 |
to avoid screen borders from acting as constrains on mouse movement). |
| 595 |
|
| 596 |
This is automatically used by @link(TCastleWalkNavigation.MouseLook). |
| 597 |
You can use it yourself for custom effects "like mouse look". The template to use this |
| 598 |
is below. |
| 599 |
See the CGE example examples/user_interface/dragging_test/ for a working code |
| 600 |
demonstrating this. |
| 601 |
|
| 602 |
@longCode(# |
| 603 |
|
| 604 |
function TMyState.Press(const Event: TInputPressRelease): Boolean; |
| 605 |
begin |
| 606 |
Result := inherited; |
| 607 |
if Result then Exit; |
| 608 |
|
| 609 |
if Event.IsMouseButton(buttonLeft) then |
| 610 |
begin |
| 611 |
Drag := true; |
| 612 |
Cursor := mcForceNone; |
| 613 |
Container.MouseLookPress; |
| 614 |
end; |
| 615 |
end; |
| 616 |
|
| 617 |
function TMyState.Release(const Event: TInputPressRelease): Boolean; |
| 618 |
begin |
| 619 |
Result := inherited; |
| 620 |
if Result then Exit; |
| 621 |
|
| 622 |
if Event.IsMouseButton(buttonLeft) then |
| 623 |
begin |
| 624 |
Drag := false; |
| 625 |
Cursor := mcDefault; |
| 626 |
end; |
| 627 |
end; |
| 628 |
|
| 629 |
procedure TMyState.Update(const SecondsPassed: Single; |
| 630 |
var HandleInput: Boolean); |
| 631 |
begin |
| 632 |
inherited; |
| 633 |
if Drag then |
| 634 |
Container.MouseLookUpdate; |
| 635 |
end; |
| 636 |
|
| 637 |
function TNewFightUi.Motion(const Event: TInputMotion): Boolean; |
| 638 |
var |
| 639 |
Delta: TVector2; |
| 640 |
begin |
| 641 |
Result := inherited; |
| 642 |
if Result then Exit; |
| 643 |
|
| 644 |
if Drag then |
| 645 |
begin |
| 646 |
Delta := Container.MouseLookDelta(Event, Container.Rect); |
| 647 |
// ... |
| 648 |
// Use Delta to perform any logic you want. |
| 649 |
// It may be zero if mouse was not positioned correctly yet, |
| 650 |
// just make sure that Delta=zero does nothing. |
| 651 |
// You can use Delta / UiScale to adjust to UI scale |
| 652 |
// (user will then have to move mouse by more pixels on a larger screen to achieve the same Delta). |
| 653 |
// ... |
| 654 |
end; |
| 655 |
end; |
| 656 |
|
| 657 |
#) |
| 658 |
} |
| 659 |
function MouseLookDelta(const Event: TInputMotion; const ControlRect: TFloatRectangle): TVector2; overload; |
| 660 |
|
| 661 |
function MouseLookDelta(const Event: TInputMotion): TVector2; overload; |
| 662 |
deprecated 'use MouseLookDelta overload with ControlRect parameter'; |
| 663 |
|
| 664 |
{ Force passing events to the given control first, |
| 665 |
regardless if this control is under the mouse cursor. |
| 666 |
Before we even send events to the currently "capturing" control |
| 667 |
(for example, when you're dragging the slider, it is "capturing" mouse |
| 668 |
events until you release the mouse), they are send to this control. |
| 669 |
|
| 670 |
The control given here will always have focus |
| 671 |
(that is, @link(TCastleUserInterface.Focused) will be set to true |
| 672 |
shortly after it becomes the ForceCaptureInput). |
| 673 |
|
| 674 |
An example when this is useful is when you use camera MouseLook, |
| 675 |
and the associated viewport does not fill the full window |
| 676 |
(TCastleViewport.FullSize is @false, and actual sizes are smaller |
| 677 |
than window, and may not include window center). In this case you want |
| 678 |
to make sure that motion events get passed to this control, |
| 679 |
and that this control has focus (to keep mouse cursor hidden). |
| 680 |
|
| 681 |
The engine itself @italic(never) automatically sets this property. |
| 682 |
It is up to your application code to set this, if you need. } |
| 683 |
property ForceCaptureInput: TCastleUserInterface |
| 684 |
read FForceCaptureInput write SetForceCaptureInput; |
| 685 |
|
| 686 |
{ When this is not mcDefault, it sets the cursor, regardless of |
| 687 |
cursor specified at the @link(TCastleUserInterface.Cursor) value of |
| 688 |
the focused control. It even takes precedence over any control using |
| 689 |
mcForceNone (so it can force the cursor to be visible anyway). } |
| 690 |
property OverrideCursor: TMouseCursor read FOverrideCursor write FOverrideCursor |
| 691 |
default mcDefault; |
| 692 |
|
| 693 |
{ When the control accepts the "press" event, it automatically captures |
| 694 |
the following motion and release events, hijacking them from other controls, |
| 695 |
regardless of the mouse cursor position. This is usually desirable, |
| 696 |
to allow the control to handle the dragging. |
| 697 |
But sometimes you want to cancel the dragging, and allow other controls |
| 698 |
to handle the following motion and release events, in which case calling this |
| 699 |
method helps. } |
| 700 |
procedure ReleaseCapture(const C: TCastleUserInterface); |
| 701 |
|
| 702 |
{ Load application settings from an XML file. |
| 703 |
See https://castle-engine.io/manual_castle_settings.php |
| 704 |
for a documentation of the file format. |
| 705 |
|
| 706 |
This loads UIScaling, UIReferenceWidth, UIReferenceHeight, DefaultFont. |
| 707 |
It allows "warms the cache" by loading some assets into it -- which |
| 708 |
means that actually instatiating them at runtime will be faster. |
| 709 |
|
| 710 |
Note: It is allowed to call this at any moment, even before the rendering |
| 711 |
context is initialized. That's because it makes sense in some cases: |
| 712 |
e.g. if using with LCL / FMX form, it is natural to call this |
| 713 |
in TForm.OnCreate callback, without the additional complexity of |
| 714 |
waiting for context (e.g. by waiting for GLContextOpen call on some |
| 715 |
TCastleUserInterface instance). |
| 716 |
|
| 717 |
We still recommend to call this only once rendering context is available, |
| 718 |
in case you use "warmup cache" feature ( https://castle-engine.io/manual_castle_settings.php#section_warmup_cache ). |
| 719 |
The cache will be initialized better (with rendering resoruces too) |
| 720 |
if you call this after rendering context is available. |
| 721 |
It is easiest if you use TCastleWindow: just do this in Application.OnInitialize |
| 722 |
callback, which is called after rendering context is available |
| 723 |
and is natural place to initialize stuff. } |
| 724 |
procedure LoadSettings(const SettingsUrl: String); |
| 725 |
|
| 726 |
{ Delay in seconds before showing the tooltip. } |
| 727 |
property TooltipDelay: Single read FTooltipDelay write FTooltipDelay |
| 728 |
{$ifdef FPC}default DefaultTooltipDelay{$endif}; |
| 729 |
property TooltipDistance: Cardinal read FTooltipDistance write FTooltipDistance |
| 730 |
default DefaultTooltipDistance; |
| 731 |
|
| 732 |
{ Hide the tooltip, if was displayed, right now. |
| 733 |
Useful if you e.g. want to make a screenshot now - e.g. for castle-model-viewer-mobile. |
| 734 |
TODO: Would be more natural to hide tooltip when parent exists:=false, |
| 735 |
and then this is unnecessary. } |
| 736 |
procedure InternalTooltipHide; |
| 737 |
|
| 738 |
{ Enable automatic scaling of the UI. |
| 739 |
|
| 740 |
This allows your UI to look correctly on various window sizes |
| 741 |
(great both for mobile and desktop, where window size may vary wildly). |
| 742 |
The idea is that you can set UI controls sizes |
| 743 |
(like @link(TCastleUserInterface.Width), |
| 744 |
@link(TCastleUserInterface.Height)) to a simple constant values. |
| 745 |
And you should also set appropriate anchors |
| 746 |
(choose wisely whether to anchor e.g. to left or right, |
| 747 |
as the simulated window size still has variable aspect ratio). |
| 748 |
And the result will look good on any actual window size. |
| 749 |
All the controls will be scaled to fill the same window part. |
| 750 |
The scaling is actually done by scaling the coordinates, so there's |
| 751 |
no quality loss, whole rendering just adjusts to the actual window size |
| 752 |
in a smart way. |
| 753 |
|
| 754 |
See @link(TUIScaling) values for precise description how it works. } |
| 755 |
property UIScaling: TUIScaling |
| 756 |
read FUIScaling write SetUIScaling default usNone; |
| 757 |
|
| 758 |
{ Reference width and height to which we fit the container size |
| 759 |
when UIScaling is |
| 760 |
usEncloseReferenceSize, |
| 761 |
usEncloseReferenceSizeAutoOrientation, |
| 762 |
usFitReferenceSize. |
| 763 |
|
| 764 |
See the documentation of these values and @url(https://castle-engine.io/manual_castle_settings.php manual) |
| 765 |
to understand how UI scaling works. |
| 766 |
|
| 767 |
You can set both UIReferenceWidth and UIReferenceHeight, |
| 768 |
or set only one (and leave the other as zero). |
| 769 |
|
| 770 |
Note: If only one of UIReferenceWidth and UIReferenceHeight is non-zero, |
| 771 |
then these 3 UI scaling modes are equivalent: |
| 772 |
- usEncloseReferenceSize and |
| 773 |
- usEncloseReferenceSizeAutoOrientation |
| 774 |
- usFitReferenceSize |
| 775 |
When only one UIReferenceWidth and UIReferenceHeight is non-zero, |
| 776 |
then all these 3 modes just adjust scaling based on this one |
| 777 |
non-zero reference size. |
| 778 |
|
| 779 |
@groupBegin } |
| 780 |
property UIReferenceWidth: Single |
| 781 |
read FUIReferenceWidth write SetUIReferenceWidth {$ifdef FPC}default 0{$endif}; |
| 782 |
property UIReferenceHeight: Single |
| 783 |
read FUIReferenceHeight write SetUIReferenceHeight {$ifdef FPC}default 0{$endif}; |
| 784 |
{ @groupEnd } |
| 785 |
|
| 786 |
{ Scale of the container size (as seen by TCastleUserInterface implementations) |
| 787 |
when UIScaling is usExplicitScale. |
| 788 |
See @link(usExplicitScale) for precise description how this works. } |
| 789 |
property UIExplicitScale: Single |
| 790 |
read FUIExplicitScale write SetUIExplicitScale {$ifdef FPC}default 1.0{$endif}; |
| 791 |
|
| 792 |
{ Default font (type, size) to be used by all user interface controls. |
| 793 |
Note that each UI control can customize the used font and/or size |
| 794 |
using properties @link(TCastleUserInterfaceFont.CustomFont), |
| 795 |
@link(TCastleUserInterfaceFont.FontSize). |
| 796 |
|
| 797 |
If this is @nil, we use the global font @link(UIFont) |
| 798 |
that is always assigned. } |
| 799 |
property DefaultFont: TCastleAbstractFont read FDefaultFont write FDefaultFont; |
| 800 |
|
| 801 |
{ Before rendering anything else, |
| 802 |
fill the color buffer with @link(BackgroundColor). |
| 803 |
By default this is @true. |
| 804 |
|
| 805 |
You can set this to @false to gain a little speed, |
| 806 |
if you know you always draw something that fills the whole container. |
| 807 |
For example: |
| 808 |
|
| 809 |
@unorderedList( |
| 810 |
@item(Use @link(TCastleRectangleControl) with |
| 811 |
@link(TCastleUserInterface.FullSize FullSize) = @true and set |
| 812 |
@link(TCastleRectangleControl.Color) as desired,) |
| 813 |
|
| 814 |
@item(or use @link(TCastleViewport) with |
| 815 |
@link(TCastleUserInterface.FullSize) = @true and |
| 816 |
@link(TCastleViewport.Transparent) = @false and set |
| 817 |
@link(TCastleViewport.BackgroundColor) as desired,) |
| 818 |
|
| 819 |
@item(eventually you can also call |
| 820 |
@link(TRenderContext.Clear RenderContext.Clear) |
| 821 |
at the beginning of your rendering in @link(OnRender). |
| 822 |
This is the least advised method, as @link(OnRender) |
| 823 |
is performed after drawing all other controls, |
| 824 |
so doing |
| 825 |
@link(TRenderContext.Clear RenderContext.Clear) |
| 826 |
there would force you to make |
| 827 |
all your drawing in @link(OnRender).) |
| 828 |
) |
| 829 |
|
| 830 |
If you set this to @false, but do not draw something else |
| 831 |
over the entire container, then the |
| 832 |
screen contents at the beginning are undefined. |
| 833 |
} |
| 834 |
property BackgroundEnable: Boolean |
| 835 |
read FBackgroundEnable write FBackgroundEnable default true; |
| 836 |
|
| 837 |
{ Color that fills the window by default. |
| 838 |
By default it is @link(DefaultBackgroundColor), which is very dark gray. } |
| 839 |
property BackgroundColor: TCastleColor |
| 840 |
read FBackgroundColor write FBackgroundColor; |
| 841 |
|
| 842 |
{ Input (key, touch) to toggle the inspector at any point in the application. |
| 843 |
By default this is possible in debug builds, using key F8, |
| 844 |
or pressing 3 fingers for 1 second. } |
| 845 |
class property InputInspector: TInputInspector read FInputInspector; |
| 846 |
|
| 847 |
{ @deprecated Use InputInspector.Key now. } |
| 848 |
property InspectorKey: TKey read GetInspectorKey write SetInspectorKey; |
| 849 |
{$ifdef FPC}deprecated 'use InputInspector';{$endif} |
| 850 |
|
| 851 |
{ Current view. Simply assign to this property to change the current view. |
| 852 |
|
| 853 |
In case multiple views are active (only possible |
| 854 |
if you used the @link(PushView) method), this property returns the @italic(bottom) view |
| 855 |
(use @link(FrontView) to get @italic(top) view). |
| 856 |
Setting this property resets whole view stack. |
| 857 |
|
| 858 |
@bold(When is it allowed to change the view?) |
| 859 |
|
| 860 |
While in theory you can change current view stack (assigning @link(View) |
| 861 |
or using @link(PushView) / @link(PopView)) at any moment, |
| 862 |
but remember that stopping the view frees also the view UI. |
| 863 |
So you should not change the current view stack within events/overriden methods |
| 864 |
of classes like TCastleUserInterface, TCastleTransform, TCastleBehavior |
| 865 |
that could be destroyed by the view stop. |
| 866 |
|
| 867 |
The simpler advise is: @italic(Assign to @link(View) or use @link(PushView) / @link(PopView) |
| 868 |
only from the overridden TCastleView methods. |
| 869 |
Like TMyView.Update or TMyView.Press). |
| 870 |
|
| 871 |
Note that you cannot change current view stack when another change is in progress. |
| 872 |
That is, you cannot change view from within TMyView.Start/Resume/Pause/Stop. |
| 873 |
} |
| 874 |
property View: TCastleView read GetView write SetView; |
| 875 |
|
| 876 |
{ The view in the front (top-most view on the stack). |
| 877 |
|
| 878 |
In case you used @link(PushView), this returns the top-most (most recently pushed) view. |
| 879 |
|
| 880 |
If there is only one (or none) view, e.g. because you never used @link(PushView), |
| 881 |
then this property returns the same thing as @link(View). } |
| 882 |
property FrontView: TCastleView read GetFrontView; |
| 883 |
|
| 884 |
{ Pushing the view adds it at the top of the view stack, |
| 885 |
this makes new view to be displayed on top of previous ones. |
| 886 |
|
| 887 |
The view known as @link(View) is conceptually at the bottom of view stack, always. |
| 888 |
When it is nil, then pushing new view sets the @link(View) view. |
| 889 |
Otherwise @link(View) view is left as-it-is, new view is added on top. } |
| 890 |
procedure PushView(const NewView: TCastleView); |
| 891 |
|
| 892 |
{ Pop the current top-most view, reversing the @link(PushView) operation. } |
| 893 |
procedure PopView; overload; |
| 894 |
|
| 895 |
{ Pop the current top-most view, reversing the @link(PushView) operation, |
| 896 |
also checking whether the current top-most view is as expected. |
| 897 |
|
| 898 |
Makes a warning, and does nothing, if the current top-most view |
| 899 |
is different than indicated. This is usually a safer (more chance |
| 900 |
to easily catch bugs) version of PopView than the parameter-less version. } |
| 901 |
procedure PopView(const CheckFrontView: TCastleView); overload; |
| 902 |
|
| 903 |
{ Count of views in the view stack. |
| 904 |
View stack is managed using @link(View) / @link(PushView) / @link(PopView). } |
| 905 |
function ViewStackCount: Integer; |
| 906 |
|
| 907 |
{ Access any view within the view stack. |
| 908 |
Use with indexes between 0 and ViewStackCount - 1. |
| 909 |
View stack is managed using @link(View) / @link(PushView) / @link(PopView). } |
| 910 |
property ViewStack [const Index: Integer]: TCastleView read GetViewStack; |
| 911 |
|
| 912 |
{ Safe border sizes you should honor in your UI design (do not place there |
| 913 |
UI elements that have to be readable or clickable, like labels or buttons) |
| 914 |
on mobile devices. |
| 915 |
|
| 916 |
This is important for full-screen Android and iOS applications. |
| 917 |
You have to leave this space empty, that is: place there simple unimportant |
| 918 |
UI -- solid color, gradients. And be prepared it may be partially or |
| 919 |
completely obscured by the device's status bar (on top, with stuff like |
| 920 |
clock, battery, network status) or physical things obscuring the screen. |
| 921 |
|
| 922 |
This is also sometimes called "safe area", "display cutout", |
| 923 |
"statusBarHeight" (top), "bottomBarHeight" (bottom). |
| 924 |
See e.g. Android docs: |
| 925 |
https://developer.android.com/design/ui/mobile/guides/foundations/system-bars . } |
| 926 |
function SafeBorder: TBorder; |
| 927 |
|
| 928 |
{ Event called right after @link(SafeBorder) changed. } |
| 929 |
property OnSafeBorderChanged: TNotifyEvent read FOnSafeBorderChanged write FOnSafeBorderChanged; |
| 930 |
|
| 931 |
{ @exclude } |
| 932 |
procedure InternalSetSafeBorderScaled(const Value: TVector4); |
| 933 |
end; |
| 934 |
|
| 935 |
{$endif read_interface} |
| 936 |
|
| 937 |
{$ifdef read_implementation} |
| 938 |
|
| 939 |
{ Changing view during view Start/Stop/Push/Pop is not reliable, |
| 940 |
e.g. doing TCastleContainer.PushView during another TCastleContainer.PushView will not result |
| 941 |
in proper stack. } |
| 942 |
procedure ErrorStackChangeDisabled(const Message: String); |
| 943 |
begin |
| 944 |
raise EInternalError.Create(Message); |
| 945 |
end; |
| 946 |
|
| 947 |
{ TCastleContainer --------------------------------------------------------------- } |
| 948 |
|
| 949 |
constructor TCastleContainer.Create(AOwner: TComponent); |
| 950 |
begin |
| 951 |
inherited; |
| 952 |
|
| 953 |
FControls := TInternalChildrenControls.Create(nil); |
| 954 |
FControls.Container := Self; |
| 955 |
FTooltipDelay := DefaultTooltipDelay; |
| 956 |
FTooltipDistance := DefaultTooltipDistance; |
| 957 |
FCaptureInput := TFingerIndexCaptureMap.Create; |
| 958 |
FDpi := DefaultDpi; |
| 959 |
FUIScaling := usNone; |
| 960 |
FUIExplicitScale := 1.0; |
| 961 |
FUIScale := 1.0; // default safe value, in case some TCastleUserInterface will look here |
| 962 |
FFocus := TCastleUserInterfaceList.Create(false); |
| 963 |
FNewFocus := TCastleUserInterfaceList.Create(false); |
| 964 |
FFps := TFramesPerSecond.Create; |
| 965 |
FPressed := TKeysPressed.Create; |
| 966 |
FContext := TRenderContext.Create(nil); |
| 967 |
FBackgroundEnable := true; |
| 968 |
FBackgroundColor := DefaultBackgroundColor; |
| 969 |
FForceCaptureInputObserver := TFreeNotificationObserver.Create(Self); |
| 970 |
FForceCaptureInputObserver.OnFreeNotification := {$ifdef FPC}@{$endif} ForceCaptureInputFreeNotification; |
| 971 |
FSafeBorder := TBorder.Create(nil); |
| 972 |
|
| 973 |
{ connect 3D device - 3Dconnexion device } |
| 974 |
Mouse3dPollTimer := 0; |
| 975 |
try |
| 976 |
Mouse3d := T3DConnexionDevice.Create('Castle Control'); |
| 977 |
except |
| 978 |
on E: Exception do |
| 979 |
WritelnLog('3D Mouse', 'Exception %s when initializing T3DConnexionDevice: %s', |
| 980 |
[E.ClassName, E.Message]); |
| 981 |
end; |
| 982 |
|
| 983 |
if Messaging <> nil then |
| 984 |
Messaging.OnReceive.Add({$ifdef FPC}@{$endif} MessageReceived); |
| 985 |
end; |
| 986 |
|
| 987 |
destructor TCastleContainer.Destroy; |
| 988 |
begin |
| 989 |
if Messaging <> nil then |
| 990 |
Messaging.OnReceive.Remove({$ifdef FPC}@{$endif} MessageReceived); |
| 991 |
|
| 992 |
if FViewStack <> nil then |
| 993 |
begin |
| 994 |
View := nil; // should clean FViewStack |
| 995 |
FreeAndNil(FViewStack); |
| 996 |
end; |
| 997 |
if RenderContext = FContext then |
| 998 |
RenderContext := nil; |
| 999 |
FreeAndNil(FSafeBorder); |
| 1000 |
FreeAndNil(FContext); |
| 1001 |
FreeAndNil(FPressed); |
| 1002 |
FreeAndNil(FFps); |
| 1003 |
FreeAndNil(FControls); |
| 1004 |
FreeAndNil(Mouse3d); |
| 1005 |
FreeAndNil(FCaptureInput); |
| 1006 |
FreeAndNil(FFocus); |
| 1007 |
FreeAndNil(FNewFocus); |
| 1008 |
{ set to nil by SetForceCaptureInput, to detach free notification } |
| 1009 |
ForceCaptureInput := nil; |
| 1010 |
inherited; |
| 1011 |
end; |
| 1012 |
|
| 1013 |
procedure TCastleContainer.Notification(AComponent: TComponent; Operation: TOperation); |
| 1014 |
begin |
| 1015 |
inherited; |
| 1016 |
if (Operation = opRemove) and (AComponent is TCastleUserInterface) then |
| 1017 |
DetachNotification(TCastleUserInterface(AComponent)); |
| 1018 |
end; |
| 1019 |
|
| 1020 |
procedure TCastleContainer.SetForceCaptureInput(const Value: TCastleUserInterface); |
| 1021 |
begin |
| 1022 |
if FForceCaptureInput <> Value then |
| 1023 |
begin |
| 1024 |
FForceCaptureInput := Value; |
| 1025 |
FForceCaptureInputObserver.Observed := Value; |
| 1026 |
end; |
| 1027 |
end; |
| 1028 |
|
| 1029 |
procedure TCastleContainer.ForceCaptureInputFreeNotification(const Sender: TFreeNotificationObserver); |
| 1030 |
begin |
| 1031 |
ForceCaptureInput := nil; |
| 1032 |
end; |
| 1033 |
|
| 1034 |
procedure TCastleContainer.DetachNotification(const C: TCastleUserInterface); |
| 1035 |
var |
| 1036 |
Index: Integer; |
| 1037 |
FingerIndex: TFingerIndex; |
| 1038 |
begin |
| 1039 |
if C = FTooltipParent then |
| 1040 |
begin |
| 1041 |
if FTooltipParent <> nil then |
| 1042 |
FTooltipParent.InternalSetTooltipExists(false); |
| 1043 |
FTooltipParent := nil; |
| 1044 |
end; |
| 1045 |
|
| 1046 |
if FFocus <> nil then |
| 1047 |
begin |
| 1048 |
Index := FFocus.IndexOf(C); |
| 1049 |
if Index <> -1 then |
| 1050 |
begin |
| 1051 |
C.Focused := false; |
| 1052 |
FFocus.Delete(Index); |
| 1053 |
end; |
| 1054 |
end; |
| 1055 |
|
| 1056 |
if FCaptureInput <> nil then |
| 1057 |
begin |
| 1058 |
while TryGetFingerOfControl(C, FingerIndex) do |
| 1059 |
FCaptureInput.Remove(FingerIndex); |
| 1060 |
end; |
| 1061 |
end; |
| 1062 |
|
| 1063 |
function TCastleContainer.TryGetFingerOfControl(const C: TCastleUserInterface; out Finger: TFingerIndex): Boolean; |
| 1064 |
var |
| 1065 |
FingerControlPair: {$ifdef FPC}TFingerIndexCaptureMap.TDictionaryPair{$else}TPair<TFingerIndex, TCastleUserInterface>{$endif}; |
| 1066 |
begin |
| 1067 |
{ search for control C among the FCaptureInput values, and return corresponding key } |
| 1068 |
for FingerControlPair in FCaptureInput do |
| 1069 |
if FingerControlPair.Value = C then |
| 1070 |
begin |
| 1071 |
Finger := FingerControlPair.Key; |
| 1072 |
Result := true; |
| 1073 |
Exit; |
| 1074 |
end; |
| 1075 |
Result := false; |
| 1076 |
end; |
| 1077 |
|
| 1078 |
function TCastleContainer.UseForceCaptureInput: Boolean; |
| 1079 |
begin |
| 1080 |
Result := |
| 1081 |
(ForceCaptureInput <> nil) and |
| 1082 |
(ForceCaptureInput.Container = Self) { currently added to our Controls tree } and |
| 1083 |
ForceCaptureInput.Exists; |
| 1084 |
end; |
| 1085 |
|
| 1086 |
function TCastleContainer.PassEvents(const C: TCastleUserInterface; |
| 1087 |
const EventPosition: TVector2; |
| 1088 |
const CheckEventPosition: Boolean = true): Boolean; |
| 1089 |
begin |
| 1090 |
Result := |
| 1091 |
(not (csDestroying in C.ComponentState)) and |
| 1092 |
C.Exists and |
| 1093 |
((not CheckEventPosition) or C.CapturesEventsAtPosition(EventPosition)) and |
| 1094 |
C.Visible; |
| 1095 |
end; |
| 1096 |
|
| 1097 |
function TCastleContainer.PassEvents(const C: TCastleUserInterface; |
| 1098 |
const Event: TInputPressRelease; |
| 1099 |
const CheckEventPosition: Boolean = true): Boolean; |
| 1100 |
begin |
| 1101 |
Result := PassEvents(C, Event.Position, CheckEventPosition); |
| 1102 |
end; |
| 1103 |
|
| 1104 |
function TCastleContainer.PassEvents(const C: TCastleUserInterface; |
| 1105 |
const Event: TInputMotion; |
| 1106 |
const CheckEventPosition: Boolean = true): Boolean; |
| 1107 |
begin |
| 1108 |
Result := PassEvents(C, Event.Position, CheckEventPosition); |
| 1109 |
end; |
| 1110 |
|
| 1111 |
function TCastleContainer.PassEvents(const C: TCastleUserInterface; |
| 1112 |
const CheckMousePosition: Boolean): Boolean; |
| 1113 |
begin |
| 1114 |
Result := PassEvents(C, MousePosition, CheckMousePosition); |
| 1115 |
end; |
| 1116 |
|
| 1117 |
procedure TCastleContainer.UpdateFocusAndMouseCursor; |
| 1118 |
var |
| 1119 |
AnythingForcesNoneCursor: Boolean; |
| 1120 |
|
| 1121 |
{ Scan all Controls, recursively. |
| 1122 |
Update (add) to FNewFocus, update (set to true) AnythingForcesNoneCursor. } |
| 1123 |
procedure CalculateNewFocus; |
| 1124 |
|
| 1125 |
procedure RecursiveCalculateNewFocus(const C: TCastleUserInterface); |
| 1126 |
var |
| 1127 |
I: Integer; |
| 1128 |
begin |
| 1129 |
if PassEvents(C) then |
| 1130 |
begin |
| 1131 |
if C.Cursor = mcForceNone then |
| 1132 |
AnythingForcesNoneCursor := true; |
| 1133 |
|
| 1134 |
FNewFocus.Add(C); |
| 1135 |
|
| 1136 |
// Iterate in back-to-front order, because that's the order on Focus list. |
| 1137 |
for I := 0 to C.ControlsCount - 1 do |
| 1138 |
RecursiveCalculateNewFocus(C.Controls[I]); |
| 1139 |
end; |
| 1140 |
end; |
| 1141 |
|
| 1142 |
var |
| 1143 |
I: Integer; |
| 1144 |
begin |
| 1145 |
{ Note that even if one sibling obscures another (they both satisfy |
| 1146 |
CapturesEventsAtPosition) both siblings are added to the FNewFocus |
| 1147 |
(later to Focus) list. |
| 1148 |
That's because they all can receive input event (like Press), |
| 1149 |
assuming that all controls return "not handled" (return false from Press). |
| 1150 |
This is crucial to make some "invisible" controls (like TCastleNavigation |
| 1151 |
or TCastleInspector) work seamlessly, they should not prevent |
| 1152 |
other controls from appearing on Focus list. |
| 1153 |
} |
| 1154 |
|
| 1155 |
// Iterate in back-to-front order, because that's the order on Focus list. |
| 1156 |
for I := 0 to Controls.Count - 1 do |
| 1157 |
RecursiveCalculateNewFocus(Controls[I]); |
| 1158 |
end; |
| 1159 |
|
| 1160 |
{ Possibly adds the control to FNewFocus and |
| 1161 |
updates the AnythingForcesNoneCursor if needed. } |
| 1162 |
procedure AddInFrontOfNewFocus(const C: TCastleUserInterface); |
| 1163 |
begin |
| 1164 |
if (not (csDestroying in C.ComponentState)) and |
| 1165 |
(FNewFocus.IndexOf(C) = -1) then |
| 1166 |
begin |
| 1167 |
FNewFocus.Add(C); |
| 1168 |
if C.Cursor = mcForceNone then |
| 1169 |
AnythingForcesNoneCursor := true; |
| 1170 |
end; |
| 1171 |
end; |
| 1172 |
|
| 1173 |
function CalculateMouseCursor: TMouseCursor; |
| 1174 |
var |
| 1175 |
FocusIndex: Integer; |
| 1176 |
begin |
| 1177 |
Result := mcDefault; |
| 1178 |
|
| 1179 |
if Focus.Count <> 0 then |
| 1180 |
begin |
| 1181 |
{ Calculate cursor looking at Focus.Last.Cursor, |
| 1182 |
unless that's mcDefault then look at previous control on Focus list, |
| 1183 |
and so on. |
| 1184 |
This is crucial e.g. to allow TCastleViewport to display |
| 1185 |
"hand" cursor over TouchSensor, even when TCastleXxxNavigation within |
| 1186 |
this viewport has focus. |
| 1187 |
} |
| 1188 |
FocusIndex := Focus.Count; |
| 1189 |
repeat |
| 1190 |
Dec(FocusIndex); |
| 1191 |
Result := Focus[FocusIndex].Cursor; |
| 1192 |
until (Result <> mcDefault) or (FocusIndex = 0); |
| 1193 |
end; |
| 1194 |
|
| 1195 |
if AnythingForcesNoneCursor then |
| 1196 |
Result := mcForceNone; |
| 1197 |
|
| 1198 |
if OverrideCursor <> mcDefault then |
| 1199 |
Result := OverrideCursor; |
| 1200 |
|
| 1201 |
{ do not hide when container is not focused (mouse look doesn't work |
| 1202 |
then too, so better to not hide mouse) } |
| 1203 |
if (not Focused) and (Result in [mcNone, mcForceNone]) then |
| 1204 |
Result := mcDefault; |
| 1205 |
end; |
| 1206 |
|
| 1207 |
var |
| 1208 |
I: Integer; |
| 1209 |
Tmp: TCastleUserInterfaceList; |
| 1210 |
ControlUnderFinger0: TCastleUserInterface; |
| 1211 |
begin |
| 1212 |
{ since this is called at the end of TInternalChildrenControls.Notify after |
| 1213 |
some control is removed, we're paranoid here about checking csDestroying. } |
| 1214 |
|
| 1215 |
{ FNewFocus is only used by this method. It is only managed by TCastleUserInterface |
| 1216 |
to avoid constructing/destructing it in every |
| 1217 |
TCastleContainer.UpdateFocusAndMouseCursor call. } |
| 1218 |
FNewFocus.Clear; |
| 1219 |
AnythingForcesNoneCursor := false; |
| 1220 |
|
| 1221 |
{ Do not scan Controls for focus when csDestroying (in which case Controls |
| 1222 |
list may be invalid). Testcase: exit with Alt + F4 from zombie_fighter |
| 1223 |
StateAskDialog. } |
| 1224 |
if not (csDestroying in ComponentState) then |
| 1225 |
begin |
| 1226 |
{ calculate new FNewFocus value, update AnythingForcesNoneCursor } |
| 1227 |
CalculateNewFocus; |
| 1228 |
{ add controls capturing the input (since they should have Focused = true to |
| 1229 |
show them as receiving input) on top of other controls |
| 1230 |
(so that e.g. TCastleOnScreenMenu underneath pressed-down button is |
| 1231 |
also still focused) } |
| 1232 |
if UseForceCaptureInput then |
| 1233 |
AddInFrontOfNewFocus(ForceCaptureInput) else |
| 1234 |
|
| 1235 |
if FCaptureInput.TryGetValue(0, ControlUnderFinger0) then |
| 1236 |
AddInFrontOfNewFocus(ControlUnderFinger0); |
| 1237 |
|
| 1238 |
{ update TCastleUserInterface.Focused values, based on differences between FFocus and FNewFocus } |
| 1239 |
if not (ApplicationProperties.TouchDevice) then |
| 1240 |
begin |
| 1241 |
for I := 0 to FNewFocus.Count - 1 do |
| 1242 |
if FFocus.IndexOf(FNewFocus[I]) = -1 then |
| 1243 |
FNewFocus[I].Focused := true; |
| 1244 |
for I := 0 to FFocus.Count - 1 do |
| 1245 |
if FNewFocus.IndexOf(FFocus[I]) = -1 then |
| 1246 |
FFocus[I].Focused := false; |
| 1247 |
end |
| 1248 |
end; |
| 1249 |
|
| 1250 |
{ swap FFocus and FNewFocus, so that FFocus changes to new value, |
| 1251 |
and the next UpdateFocusAndMouseCursor has ready FNewFocus value. } |
| 1252 |
Tmp := FFocus; |
| 1253 |
FFocus := FNewFocus; |
| 1254 |
FNewFocus := Tmp; |
| 1255 |
|
| 1256 |
InternalCursor := CalculateMouseCursor; |
| 1257 |
FFocusAndMouseCursorValid := true; |
| 1258 |
end; |
| 1259 |
|
| 1260 |
function TCastleContainer.EventSensorRotation(const X, Y, Z, Angle: Double; const SecondsPassed: Single): Boolean; |
| 1261 |
|
| 1262 |
function RecursiveSensorRotation(const C: TCastleUserInterface): Boolean; |
| 1263 |
var |
| 1264 |
I: Integer; |
| 1265 |
begin |
| 1266 |
if PassEvents(C) then |
| 1267 |
begin |
| 1268 |
for I := C.ControlsCount - 1 downto 0 do |
| 1269 |
if RecursiveSensorRotation(C.Controls[I]) then |
| 1270 |
Exit(true); |
| 1271 |
|
| 1272 |
if C.SensorRotation(X, Y, Z, Angle, SecondsPassed) then |
| 1273 |
Exit(true); |
| 1274 |
end; |
| 1275 |
|
| 1276 |
Result := false; |
| 1277 |
end; |
| 1278 |
|
| 1279 |
var |
| 1280 |
I: Integer; |
| 1281 |
begin |
| 1282 |
{ exit as soon as something returns "true", meaning the event is handled } |
| 1283 |
for I := Controls.Count - 1 downto 0 do |
| 1284 |
if RecursiveSensorRotation(Controls[I]) then |
| 1285 |
Exit(true); |
| 1286 |
Result := false; |
| 1287 |
end; |
| 1288 |
|
| 1289 |
function TCastleContainer.EventSensorTranslation(const X, Y, Z, Length: Double; const SecondsPassed: Single): Boolean; |
| 1290 |
|
| 1291 |
function RecursiveSensorTranslation(const C: TCastleUserInterface): Boolean; |
| 1292 |
var |
| 1293 |
I: Integer; |
| 1294 |
begin |
| 1295 |
if PassEvents(C) then |
| 1296 |
begin |
| 1297 |
for I := C.ControlsCount - 1 downto 0 do |
| 1298 |
if RecursiveSensorTranslation(C.Controls[I]) then |
| 1299 |
Exit(true); |
| 1300 |
|
| 1301 |
if C.SensorTranslation(X, Y, Z, Length, SecondsPassed) then |
| 1302 |
Exit(true); |
| 1303 |
end; |
| 1304 |
|
| 1305 |
Result := false; |
| 1306 |
end; |
| 1307 |
|
| 1308 |
var |
| 1309 |
I: Integer; |
| 1310 |
begin |
| 1311 |
{ exit as soon as something returns "true", meaning the event is handled } |
| 1312 |
for I := Controls.Count - 1 downto 0 do |
| 1313 |
if RecursiveSensorTranslation(Controls[I]) then |
| 1314 |
Exit(true); |
| 1315 |
Result := false; |
| 1316 |
end; |
| 1317 |
|
| 1318 |
function TCastleContainer.EventJoyAxisMove(const JoyID, Axis: Byte): Boolean; |
| 1319 |
|
| 1320 |
function RecursiveJoyAxisMove(const C: TCastleUserInterface): Boolean; |
| 1321 |
var |
| 1322 |
I: Integer; |
| 1323 |
begin |
| 1324 |
if PassEvents(C) then |
| 1325 |
begin |
| 1326 |
for I := C.ControlsCount - 1 downto 0 do |
| 1327 |
if RecursiveJoyAxisMove(C.Controls[I]) then |
| 1328 |
Exit(true); |
| 1329 |
|
| 1330 |
{$warnings off} // keeping deprecated working |
| 1331 |
if C.JoyAxisMove(JoyID, Axis) then |
| 1332 |
Exit(true); |
| 1333 |
{$warnings on} |
| 1334 |
end; |
| 1335 |
|
| 1336 |
Result := false; |
| 1337 |
end; |
| 1338 |
|
| 1339 |
var |
| 1340 |
I: Integer; |
| 1341 |
begin |
| 1342 |
{ exit as soon as something returns "true", meaning the event is handled } |
| 1343 |
for I := Controls.Count - 1 downto 0 do |
| 1344 |
if RecursiveJoyAxisMove(Controls[I]) then |
| 1345 |
Exit(true); |
| 1346 |
Result := false; |
| 1347 |
end; |
| 1348 |
|
| 1349 |
function TCastleContainer.EventJoyButtonPress(const JoyID, Button: Byte): Boolean; |
| 1350 |
|
| 1351 |
function RecursiveJoyButtonPress(const C: TCastleUserInterface): Boolean; |
| 1352 |
var |
| 1353 |
I: Integer; |
| 1354 |
begin |
| 1355 |
if PassEvents(C) then |
| 1356 |
begin |
| 1357 |
for I := C.ControlsCount - 1 downto 0 do |
| 1358 |
if RecursiveJoyButtonPress(C.Controls[I]) then |
| 1359 |
Exit(true); |
| 1360 |
|
| 1361 |
{$warnings off} // keeping deprecated working |
| 1362 |
if C.JoyButtonPress(JoyID, Button) then |
| 1363 |
Exit(true); |
| 1364 |
{$warnings on} |
| 1365 |
end; |
| 1366 |
|
| 1367 |
Result := false; |
| 1368 |
end; |
| 1369 |
|
| 1370 |
var |
| 1371 |
I: Integer; |
| 1372 |
begin |
| 1373 |
{ exit as soon as something returns "true", meaning the event is handled } |
| 1374 |
for I := Controls.Count - 1 downto 0 do |
| 1375 |
if RecursiveJoyButtonPress(Controls[I]) then |
| 1376 |
Exit(true); |
| 1377 |
Result := false; |
| 1378 |
end; |
| 1379 |
|
| 1380 |
procedure TCastleContainer.ToggleInspector; |
| 1381 |
begin |
| 1382 |
if FInspector = nil then |
| 1383 |
begin |
| 1384 |
FInspector := TCastleInspector.Create(Self); |
| 1385 |
Controls.InsertFront(FInspector); |
| 1386 |
end else |
| 1387 |
begin |
| 1388 |
{ Turning off inspector does not merely hide it by Exists:=false, |
| 1389 |
to make sure it doesn't consume any resources when not used. |
| 1390 |
This is important, as it will be used in wildest scenarios |
| 1391 |
(all CGE applications) and it may gather logs when existing. } |
| 1392 |
FreeAndNil(FInspector); |
| 1393 |
end; |
| 1394 |
end; |
| 1395 |
|
| 1396 |
procedure TCastleContainer.InternalTooltipHide; |
| 1397 |
begin |
| 1398 |
if FTooltipParent <> nil then |
| 1399 |
begin |
| 1400 |
FTooltipParent.InternalSetTooltipExists(false); |
| 1401 |
HasLastPositionForTooltip := false; |
| 1402 |
FTooltipParent := nil; |
| 1403 |
end; |
| 1404 |
end; |
| 1405 |
|
| 1406 |
procedure TCastleContainer.EventUpdate; |
| 1407 |
|
| 1408 |
procedure UpdateTooltip; |
| 1409 |
var |
| 1410 |
T: TTimerResult; |
| 1411 |
NewTooltipParent: TCastleUserInterface; |
| 1412 |
I: Integer; |
| 1413 |
begin |
| 1414 |
{ Update FTooltipParent and LastPositionForTooltip*. |
| 1415 |
Idea is that user must move the mouse very slowly to activate tooltip. } |
| 1416 |
|
| 1417 |
NewTooltipParent := nil; |
| 1418 |
T := Fps.UpdateStartTime; |
| 1419 |
|
| 1420 |
if (not HasLastPositionForTooltip) or |
| 1421 |
{ reset the time counter to show tooltip, if you moved mouse/finger |
| 1422 |
significantly } |
| 1423 |
(PointsDistanceSqr(LastPositionForTooltip, MousePosition) > |
| 1424 |
Sqr(TooltipDistance)) or |
| 1425 |
{ on touch devices, the time counter to show tooltip doesn't advance |
| 1426 |
if we don't keep the finger pressed down } |
| 1427 |
(ApplicationProperties.TouchDevice and (MousePressed = [])) then |
| 1428 |
begin |
| 1429 |
HasLastPositionForTooltip := true; |
| 1430 |
LastPositionForTooltip := MousePosition; |
| 1431 |
LastPositionForTooltipTime := T; |
| 1432 |
end else |
| 1433 |
if TimerSeconds(T, LastPositionForTooltipTime) > TooltipDelay then |
| 1434 |
begin |
| 1435 |
{ Any control on Focus can cause a tooltip. |
| 1436 |
This is especially useful when an invisible control like TCastleWalkNavigation |
| 1437 |
covers other controls. } |
| 1438 |
for I := Focus.Count - 1 downto 0 do |
| 1439 |
{$warnings off} // using deprecated, it should be internal |
| 1440 |
if Focus[I].TooltipExists then |
| 1441 |
{$warnings on} |
| 1442 |
begin |
| 1443 |
NewTooltipParent := Focus[I]; |
| 1444 |
Break; |
| 1445 |
end; |
| 1446 |
end; |
| 1447 |
|
| 1448 |
if FTooltipParent <> NewTooltipParent then |
| 1449 |
begin |
| 1450 |
{ hide tooltip } |
| 1451 |
if FTooltipParent <> nil then |
| 1452 |
FTooltipParent.InternalSetTooltipExists(false); |
| 1453 |
|
| 1454 |
FTooltipParent := NewTooltipParent; |
| 1455 |
|
| 1456 |
if FTooltipParent <> nil then |
| 1457 |
begin |
| 1458 |
{ When changing FTooltipParent, update LastPositionForTooltip. } |
| 1459 |
LastPositionForTooltip := MousePosition; |
| 1460 |
{ also update TooltipPosition } |
| 1461 |
FTooltipPosition := MousePosition; |
| 1462 |
{ show tooltip } |
| 1463 |
FTooltipParent.InternalSetTooltipExists(true); |
| 1464 |
end; |
| 1465 |
|
| 1466 |
Invalidate; |
| 1467 |
end; |
| 1468 |
end; |
| 1469 |
|
| 1470 |
procedure RecursiveUpdate(const C: TCastleUserInterface; var HandleInput: Boolean); |
| 1471 |
var |
| 1472 |
I: Integer; |
| 1473 |
Dummy: Boolean; |
| 1474 |
begin |
| 1475 |
if PassEvents(C, false) then |
| 1476 |
begin |
| 1477 |
{ go downward, from front to back. |
| 1478 |
Important for controls watching/setting HandleInput, |
| 1479 |
e.g. for sliders/OnScreenMenu to block the navigation in TCastleViewport underneath |
| 1480 |
from processing arrow keys. } |
| 1481 |
I := C.ControlsCount - 1; |
| 1482 |
while I >= 0 do |
| 1483 |
begin |
| 1484 |
// coded this way in case some Update method changes the Controls list |
| 1485 |
if I < C.ControlsCount then |
| 1486 |
RecursiveUpdate(C.Controls[I], HandleInput); |
| 1487 |
Dec(I); |
| 1488 |
end; |
| 1489 |
|
| 1490 |
if C <> ForceCaptureInput then |
| 1491 |
begin |
| 1492 |
{ Although we call Update for all the existing controls, we look |
| 1493 |
at CapturesEventsAtPosition and track HandleInput values. |
| 1494 |
See TCastleUserInterface.Update for explanation. } |
| 1495 |
if C.CapturesEventsAtPosition(MousePosition) then |
| 1496 |
begin |
| 1497 |
C.Update(Fps.SecondsPassed, HandleInput); |
| 1498 |
end else |
| 1499 |
begin |
| 1500 |
{ controls where CapturesEventsAtPosition = false always get |
| 1501 |
HandleInput parameter set to false. } |
| 1502 |
Dummy := false; |
| 1503 |
C.Update(Fps.SecondsPassed, Dummy); |
| 1504 |
end; |
| 1505 |
end; |
| 1506 |
end; |
| 1507 |
end; |
| 1508 |
|
| 1509 |
procedure Update3dMouse; |
| 1510 |
const |
| 1511 |
Mouse3dPollDelay = 0.05; |
| 1512 |
var |
| 1513 |
Tx, Ty, Tz, TLength, Rx, Ry, Rz, RAngle: Double; |
| 1514 |
Mouse3dPollSpeed: Single; |
| 1515 |
begin |
| 1516 |
if Assigned(Mouse3D) and Mouse3D.Loaded then |
| 1517 |
begin |
| 1518 |
Mouse3dPollTimer := Mouse3dPollTimer - Fps.SecondsPassed; |
| 1519 |
if Mouse3dPollTimer < 0 then |
| 1520 |
begin |
| 1521 |
{ get values from sensor } |
| 1522 |
Mouse3dPollSpeed := -Mouse3dPollTimer + Mouse3dPollDelay; |
| 1523 |
Tx := 0; { make sure they are initialized } |
| 1524 |
Ty := 0; |
| 1525 |
Tz := 0; |
| 1526 |
TLength := 0; |
| 1527 |
Mouse3D.GetSensorTranslation(Tx, Ty, Tz, TLength); |
| 1528 |
Rx := 0; { make sure they are initialized } |
| 1529 |
Ry := 0; |
| 1530 |
Rz := 0; |
| 1531 |
RAngle := 0; |
| 1532 |
Mouse3D.GetSensorRotation(Rx, Ry, Rz, RAngle); |
| 1533 |
|
| 1534 |
{ send to all 2D controls, including viewports } |
| 1535 |
EventSensorTranslation(Tx, Ty, Tz, TLength, Mouse3dPollSpeed); |
| 1536 |
EventSensorRotation(Rx, Ry, Rz, RAngle, Mouse3dPollSpeed); |
| 1537 |
|
| 1538 |
{ set timer. |
| 1539 |
The "repeat ... until" below should not be necessary under normal |
| 1540 |
circumstances, as Mouse3dPollDelay should be much larger than typical |
| 1541 |
frequency of how often this is checked. But we do it for safety |
| 1542 |
(in case something else, like AI or collision detection, |
| 1543 |
slows us down *a lot*). } |
| 1544 |
repeat |
| 1545 |
Mouse3dPollTimer := Mouse3dPollTimer + Mouse3dPollDelay; |
| 1546 |
until Mouse3dPollTimer > 0; |
| 1547 |
end; |
| 1548 |
end; |
| 1549 |
end; |
| 1550 |
|
| 1551 |
procedure UpdateJoysticks; |
| 1552 |
var |
| 1553 |
Joys: TJoysticks; |
| 1554 |
I, J: Integer; |
| 1555 |
begin |
| 1556 |
Joys := Joysticks; |
| 1557 |
if Joys.Initialized then |
| 1558 |
begin |
| 1559 |
Joys.InternalPoll; |
| 1560 |
|
| 1561 |
for I := 0 to Joys.Count - 1 do |
| 1562 |
begin |
| 1563 |
for J := 0 to Joys[I].Info.Count.Buttons -1 do |
| 1564 |
begin |
| 1565 |
//Joys.Down(I, J); |
| 1566 |
//Joys.Up(I, J); |
| 1567 |
if Joys.Press(I, J) then |
| 1568 |
EventJoyButtonPress(I, J); |
| 1569 |
end; |
| 1570 |
for J := 0 to Joys[I].Info.Count.Axes -1 do |
| 1571 |
begin |
| 1572 |
if Joys.AxisPos(I, J) <> 0 then |
| 1573 |
EventJoyAxisMove(I, J); |
| 1574 |
end; |
| 1575 |
end; |
| 1576 |
end; |
| 1577 |
end; |
| 1578 |
|
| 1579 |
var |
| 1580 |
I: Integer; |
| 1581 |
HandleInput: Boolean; |
| 1582 |
begin |
| 1583 |
Fps.InternalUpdateBegin; |
| 1584 |
|
| 1585 |
if FInputInspector.IsPressed(Self, Fps.SecondsPassed) then |
| 1586 |
ToggleInspector; |
| 1587 |
|
| 1588 |
UpdateTooltip; |
| 1589 |
|
| 1590 |
if not FFocusAndMouseCursorValid then |
| 1591 |
UpdateFocusAndMouseCursor; // sets FFocusAndMouseCursorValid to true |
| 1592 |
|
| 1593 |
Update3dMouse; |
| 1594 |
UpdateJoysticks; |
| 1595 |
|
| 1596 |
HandleInput := true; |
| 1597 |
|
| 1598 |
{ ForceCaptureInput has the 1st chance to process inputs } |
| 1599 |
if UseForceCaptureInput then |
| 1600 |
ForceCaptureInput.Update(Fps.SecondsPassed, HandleInput); |
| 1601 |
|
| 1602 |
I := Controls.Count - 1; |
| 1603 |
while I >= 0 do |
| 1604 |
begin |
| 1605 |
// coded this way in case some Update method changes the Controls list |
| 1606 |
if I < Controls.Count then |
| 1607 |
RecursiveUpdate(Controls[I], HandleInput); |
| 1608 |
Dec(I); |
| 1609 |
end; |
| 1610 |
|
| 1611 |
if Assigned(OnUpdate) then OnUpdate(Self); |
| 1612 |
end; |
| 1613 |
|
| 1614 |
function TCastleContainer.EventPress(const Event: TInputPressRelease): Boolean; |
| 1615 |
|
| 1616 |
function RecursivePress(const C: TCastleUserInterface): Boolean; |
| 1617 |
var |
| 1618 |
I: Integer; |
| 1619 |
begin |
| 1620 |
if PassEvents(C, Event) then |
| 1621 |
begin |
| 1622 |
{ try C.PreviewPress } |
| 1623 |
if (C <> ForceCaptureInput) and C.PreviewPress(Event) then |
| 1624 |
begin |
| 1625 |
if (Event.EventType = itMouseButton) and |
| 1626 |
// See below for explanation of "C.Container = Self" comparison. |
| 1627 |
(C.Container = Self) then |
| 1628 |
FCaptureInput.AddOrSetValue(Event.FingerIndex, C); |
| 1629 |
Exit(true); |
| 1630 |
end; |
| 1631 |
|
| 1632 |
{ try to pass press to C children } |
| 1633 |
for I := C.ControlsCount - 1 downto 0 do |
| 1634 |
{ checking "I < C.ControlsCount" below is a poor safeguard in case |
| 1635 |
some Press handler changes the Controls.Count. |
| 1636 |
At least we will not crash. } |
| 1637 |
if (I < C.ControlsCount) and RecursivePress(C.Controls[I]) then |
| 1638 |
Exit(true); |
| 1639 |
|
| 1640 |
{ try C.Press } |
| 1641 |
if (C <> ForceCaptureInput) and C.Press(Event) then |
| 1642 |
begin |
| 1643 |
{ We have to check whether C.Container = Self. That is because |
| 1644 |
the implementation of control's Press method could remove itself |
| 1645 |
from our Controls list. Consider e.g. TCastleOnScreenMenu.Press |
| 1646 |
that may remove itself from the Window.Controls list when clicking |
| 1647 |
"close menu" item. We cannot, in such case, save a reference to |
| 1648 |
this control in FCaptureInput, because we should not speak with it |
| 1649 |
anymore (we don't know when it's destroyed, we cannot call it's |
| 1650 |
Release method because it has Container = nil, and so on). } |
| 1651 |
if (Event.EventType = itMouseButton) and |
| 1652 |
(C.Container = Self) then |
| 1653 |
FCaptureInput.AddOrSetValue(Event.FingerIndex, C); |
| 1654 |
Exit(true); |
| 1655 |
end; |
| 1656 |
end; |
| 1657 |
|
| 1658 |
Result := false; |
| 1659 |
end; |
| 1660 |
|
| 1661 |
var |
| 1662 |
I: Integer; |
| 1663 |
begin |
| 1664 |
Result := false; |
| 1665 |
|
| 1666 |
if FInputInspector.IsEvent(Event) then |
| 1667 |
begin |
| 1668 |
ToggleInspector; |
| 1669 |
Exit(true); |
| 1670 |
end; |
| 1671 |
|
| 1672 |
{ pass to ForceCaptureInput } |
| 1673 |
if UseForceCaptureInput then |
| 1674 |
begin |
| 1675 |
if ForceCaptureInput.PreviewPress(Event) then |
| 1676 |
Exit(true); |
| 1677 |
if ForceCaptureInput.Press(Event) then |
| 1678 |
Exit(true); |
| 1679 |
end; |
| 1680 |
|
| 1681 |
{ pass to all Controls with TCastleUserInterface.Press event } |
| 1682 |
for I := Controls.Count - 1 downto 0 do |
| 1683 |
{ checking "I < Controls.Count" below is a poor safeguard in case |
| 1684 |
some Press handler changes the Controls.Count. |
| 1685 |
At least we will not crash. } |
| 1686 |
if (I < Controls.Count) and RecursivePress(Controls[I]) then |
| 1687 |
Exit(true); |
| 1688 |
|
| 1689 |
{ pass to container event } |
| 1690 |
if Assigned(OnPress) then |
| 1691 |
begin |
| 1692 |
OnPress(Self, Event); |
| 1693 |
Result := true; |
| 1694 |
end; |
| 1695 |
end; |
| 1696 |
|
| 1697 |
function TCastleContainer.EventRelease(const Event: TInputPressRelease): Boolean; |
| 1698 |
|
| 1699 |
function RecursiveRelease(const C: TCastleUserInterface): Boolean; |
| 1700 |
var |
| 1701 |
I: Integer; |
| 1702 |
begin |
| 1703 |
if PassEvents(C, Event) then |
| 1704 |
begin |
| 1705 |
{ try C.PreviewRelease } |
| 1706 |
if (C <> ForceCaptureInput) and C.PreviewRelease(Event) then |
| 1707 |
Exit(true); |
| 1708 |
|
| 1709 |
{ try to pass release to C children } |
| 1710 |
for I := C.ControlsCount - 1 downto 0 do |
| 1711 |
if RecursiveRelease(C.Controls[I]) then |
| 1712 |
Exit(true); |
| 1713 |
|
| 1714 |
{ try C.Release } |
| 1715 |
if (C <> ForceCaptureInput) and C.Release(Event) then |
| 1716 |
Exit(true); |
| 1717 |
end; |
| 1718 |
|
| 1719 |
Result := false; |
| 1720 |
end; |
| 1721 |
|
| 1722 |
var |
| 1723 |
I: Integer; |
| 1724 |
Capture: TCastleUserInterface; |
| 1725 |
begin |
| 1726 |
Result := false; |
| 1727 |
|
| 1728 |
{ pass to ForceCaptureInput } |
| 1729 |
if UseForceCaptureInput then |
| 1730 |
begin |
| 1731 |
if ForceCaptureInput.PreviewRelease(Event) then |
| 1732 |
Exit(true); |
| 1733 |
if ForceCaptureInput.Release(Event) then |
| 1734 |
Exit(true); |
| 1735 |
end; |
| 1736 |
|
| 1737 |
{ pass to control holding capture } |
| 1738 |
|
| 1739 |
if not FCaptureInput.TryGetValue(Event.FingerIndex, Capture) then |
| 1740 |
Capture := nil; |
| 1741 |
|
| 1742 |
if (Capture <> nil) and not Capture.Exists then |
| 1743 |
begin |
| 1744 |
{ No longer capturing, since the Exists returns false now. |
| 1745 |
We do not send any events to non-existing controls. } |
| 1746 |
FCaptureInput.Remove(Event.FingerIndex); |
| 1747 |
Capture := nil; |
| 1748 |
end; |
| 1749 |
|
| 1750 |
if (Capture <> nil) and (MousePressed = []) then |
| 1751 |
begin |
| 1752 |
{ No longer capturing, but do not set Capture to nil (it should receive the Release event). } |
| 1753 |
FCaptureInput.Remove(Event.FingerIndex); |
| 1754 |
end; |
| 1755 |
|
| 1756 |
if (Capture <> nil) and (Capture <> ForceCaptureInput) then |
| 1757 |
begin |
| 1758 |
Result := Capture.PreviewRelease(Event); |
| 1759 |
Result := Capture.Release(Event); |
| 1760 |
Exit; // if something is capturing the input, prevent other controls from getting the events |
| 1761 |
end; |
| 1762 |
|
| 1763 |
{ pass to all Controls with TCastleUserInterface.Release event } |
| 1764 |
for I := Controls.Count - 1 downto 0 do |
| 1765 |
if RecursiveRelease(Controls[I]) then |
| 1766 |
Exit(true); |
| 1767 |
|
| 1768 |
{ pass to container event } |
| 1769 |
if Assigned(OnRelease) then |
| 1770 |
begin |
| 1771 |
OnRelease(Self, Event); |
| 1772 |
Result := true; |
| 1773 |
end; |
| 1774 |
end; |
| 1775 |
|
| 1776 |
procedure TCastleContainer.ReleaseCapture(const C: TCastleUserInterface); |
| 1777 |
var |
| 1778 |
FingerIndex: TFingerIndex; |
| 1779 |
begin |
| 1780 |
while TryGetFingerOfControl(C, FingerIndex) do |
| 1781 |
FCaptureInput.Remove(FingerIndex); |
| 1782 |
end; |
| 1783 |
|
| 1784 |
procedure TCastleContainer.EventOpen(const InitializedContextsCount: Cardinal); |
| 1785 |
|
| 1786 |
procedure RecursiveGLContextOpen(const C: TCastleUserInterface); |
| 1787 |
var |
| 1788 |
I: Integer; |
| 1789 |
begin |
| 1790 |
for I := C.ControlsCount - 1 downto 0 do |
| 1791 |
RecursiveGLContextOpen(C.Controls[I]); |
| 1792 |
|
| 1793 |
{ Check here C.GLInitialized to not call C.GLContextOpen twice. |
| 1794 |
Control may have GL resources already initialized if it was added |
| 1795 |
e.g. from Application.OnInitialize before EventOpen. } |
| 1796 |
if not C.GLInitialized then |
| 1797 |
C.GLContextOpen; |
| 1798 |
end; |
| 1799 |
|
| 1800 |
var |
| 1801 |
I: Integer; |
| 1802 |
begin |
| 1803 |
if InitializedContextsCount = 1 then |
| 1804 |
ApplicationProperties._GLContextOpen; |
| 1805 |
|
| 1806 |
{ Call GLContextOpen on controls before OnOpen, |
| 1807 |
this way OnOpen has controls with GLInitialized = true, |
| 1808 |
so using SaveScreen etc. makes more sense there. } |
| 1809 |
for I := Controls.Count - 1 downto 0 do |
| 1810 |
RecursiveGLContextOpen(Controls[I]); |
| 1811 |
|
| 1812 |
if Assigned(OnOpen) then OnOpen(Self); |
| 1813 |
if Assigned(OnOpenObject) then OnOpenObject(Self); |
| 1814 |
end; |
| 1815 |
|
| 1816 |
procedure TCastleContainer.EventClose(const InitializedContextsCount: Cardinal); |
| 1817 |
|
| 1818 |
procedure RecursiveGLContextClose(const C: TCastleUserInterface); |
| 1819 |
var |
| 1820 |
I: Integer; |
| 1821 |
begin |
| 1822 |
for I := C.ControlsCount - 1 downto 0 do |
| 1823 |
RecursiveGLContextClose(C.Controls[I]); |
| 1824 |
|
| 1825 |
if C.GLInitialized then |
| 1826 |
C.GLContextClose; |
| 1827 |
end; |
| 1828 |
|
| 1829 |
var |
| 1830 |
I: Integer; |
| 1831 |
begin |
| 1832 |
{ Call SetFocused(false) for all focused controls, |
| 1833 |
to e.g. enable DB-aware controls to react. } |
| 1834 |
for I := 0 to FFocus.Count - 1 do |
| 1835 |
FFocus[I].Focused := false; |
| 1836 |
|
| 1837 |
{ Call GLContextClose on controls after OnClose, |
| 1838 |
consistent with inverse order in OnOpen. } |
| 1839 |
if Assigned(OnCloseObject) then OnCloseObject(Self); |
| 1840 |
if Assigned(OnClose) then OnClose(Self); |
| 1841 |
|
| 1842 |
{ call GLContextClose on controls before OnClose. |
| 1843 |
This may be called from Close, which may be called from TCastleWindow destructor, |
| 1844 |
so prepare for Controls being possibly nil now. } |
| 1845 |
if Controls <> nil then |
| 1846 |
for I := Controls.Count - 1 downto 0 do |
| 1847 |
RecursiveGLContextClose(Controls[I]); |
| 1848 |
|
| 1849 |
if InitializedContextsCount = 1 then |
| 1850 |
begin |
| 1851 |
ApplicationProperties._GLContextClose; |
| 1852 |
|
| 1853 |
{ recreate FContext instance, to reset every variable when context is closed } |
| 1854 |
if RenderContext = FContext then |
| 1855 |
RenderContext := nil; |
| 1856 |
FreeAndNil(FContext); |
| 1857 |
FContext := TRenderContext.Create(nil); |
| 1858 |
end; |
| 1859 |
end; |
| 1860 |
|
| 1861 |
function TCastleContainer.AllowSuspendForInput: Boolean; |
| 1862 |
|
| 1863 |
function RecursiveAllowSuspendForInput(const C: TCastleUserInterface): Boolean; |
| 1864 |
var |
| 1865 |
I: Integer; |
| 1866 |
begin |
| 1867 |
if PassEvents(C, false) then |
| 1868 |
begin |
| 1869 |
for I := C.ControlsCount - 1 downto 0 do |
| 1870 |
if not RecursiveAllowSuspendForInput(C.Controls[I]) then |
| 1871 |
Exit(false); |
| 1872 |
|
| 1873 |
if not C.AllowSuspendForInput then |
| 1874 |
Exit(false); |
| 1875 |
end; |
| 1876 |
|
| 1877 |
Result := true; |
| 1878 |
end; |
| 1879 |
|
| 1880 |
var |
| 1881 |
I: Integer; |
| 1882 |
begin |
| 1883 |
{ Do not suspend when you're over a control that may have a tooltip, |
| 1884 |
as EventUpdate must track and eventually show tooltip. } |
| 1885 |
if FTooltipParent <> nil then |
| 1886 |
Exit(false); |
| 1887 |
|
| 1888 |
for I := Controls.Count - 1 downto 0 do |
| 1889 |
if not RecursiveAllowSuspendForInput(Controls[I]) then |
| 1890 |
Exit(false); |
| 1891 |
|
| 1892 |
Result := true; |
| 1893 |
end; |
| 1894 |
|
| 1895 |
procedure TCastleContainer.EventMotion(const Event: TInputMotion); |
| 1896 |
|
| 1897 |
function RecursiveMotion(const C: TCastleUserInterface): Boolean; |
| 1898 |
var |
| 1899 |
I: Integer; |
| 1900 |
begin |
| 1901 |
if PassEvents(C, Event) then |
| 1902 |
begin |
| 1903 |
{ try to pass release to C children } |
| 1904 |
for I := C.ControlsCount - 1 downto 0 do |
| 1905 |
if RecursiveMotion(C.Controls[I]) then |
| 1906 |
Exit(true); |
| 1907 |
|
| 1908 |
{ try C.Motion itself } |
| 1909 |
if (C <> ForceCaptureInput) and C.Motion(Event) then |
| 1910 |
Exit(true); |
| 1911 |
end; |
| 1912 |
|
| 1913 |
Result := false; |
| 1914 |
end; |
| 1915 |
|
| 1916 |
var |
| 1917 |
I: Integer; |
| 1918 |
Capture: TCastleUserInterface; |
| 1919 |
begin |
| 1920 |
UpdateFocusAndMouseCursor; |
| 1921 |
|
| 1922 |
{ pass to ForceCaptureInput } |
| 1923 |
if UseForceCaptureInput then |
| 1924 |
begin |
| 1925 |
if ForceCaptureInput.Motion(Event) then |
| 1926 |
Exit; |
| 1927 |
end; |
| 1928 |
|
| 1929 |
{ pass to control holding capture } |
| 1930 |
|
| 1931 |
if not FCaptureInput.TryGetValue(Event.FingerIndex, Capture) then |
| 1932 |
Capture := nil; |
| 1933 |
|
| 1934 |
if (Capture <> nil) and not Capture.Exists then |
| 1935 |
begin |
| 1936 |
{ No longer capturing, since the Exists returns false now. |
| 1937 |
We do not send any events to non-existing controls. } |
| 1938 |
FCaptureInput.Remove(Event.FingerIndex); |
| 1939 |
Capture := nil; |
| 1940 |
end; |
| 1941 |
|
| 1942 |
if (Capture <> nil) and (Capture <> ForceCaptureInput) then |
| 1943 |
begin |
| 1944 |
Capture.Motion(Event); |
| 1945 |
Exit; // if something is capturing the input, prevent other controls from getting the events |
| 1946 |
end; |
| 1947 |
|
| 1948 |
{ pass to all Controls } |
| 1949 |
for I := Controls.Count - 1 downto 0 do |
| 1950 |
if RecursiveMotion(Controls[I]) then |
| 1951 |
Exit; |
| 1952 |
|
| 1953 |
{ pass to container event } |
| 1954 |
if Assigned(OnMotion) then |
| 1955 |
OnMotion(Self, Event); |
| 1956 |
end; |
| 1957 |
|
| 1958 |
procedure TCastleContainer.ControlsVisibleChange(const Sender: TCastleUserInterface; |
| 1959 |
const Changes: TCastleUserInterfaceChanges; const ChangeInitiatedByChildren: Boolean); |
| 1960 |
begin |
| 1961 |
{ We abort when ChangeInitiatedByChildren = true, |
| 1962 |
because this event will be also called with ChangeInitiatedByChildren = false |
| 1963 |
for every possible change. |
| 1964 |
So this makes a possible optimization for some TCastleContainer descendants: |
| 1965 |
no need to call Invalidate so many times. } |
| 1966 |
|
| 1967 |
if not ChangeInitiatedByChildren then |
| 1968 |
begin |
| 1969 |
Invalidate; |
| 1970 |
if [chRectangle, chCursor, chExists, chChildren] * Changes <> [] then |
| 1971 |
FFocusAndMouseCursorValid := false; |
| 1972 |
end; |
| 1973 |
end; |
| 1974 |
|
| 1975 |
procedure TCastleContainer.EventBeforeRender; |
| 1976 |
|
| 1977 |
procedure RecursiveBeforeRender(const C: TCastleUserInterface); |
| 1978 |
var |
| 1979 |
I: Integer; |
| 1980 |
begin |
| 1981 |
if PassEvents(C, false) then |
| 1982 |
begin |
| 1983 |
for I := C.ControlsCount - 1 downto 0 do |
| 1984 |
RecursiveBeforeRender(C.Controls[I]); |
| 1985 |
|
| 1986 |
if C.GLInitialized then |
| 1987 |
begin |
| 1988 |
C.CheckUIScaleChanged; |
| 1989 |
C.CheckResize; |
| 1990 |
C.BeforeRender; |
| 1991 |
end; |
| 1992 |
end; |
| 1993 |
end; |
| 1994 |
|
| 1995 |
var |
| 1996 |
I: Integer; |
| 1997 |
begin |
| 1998 |
for I := Controls.Count - 1 downto 0 do |
| 1999 |
RecursiveBeforeRender(Controls[I]); |
| 2000 |
|
| 2001 |
if Assigned(OnBeforeRender) then OnBeforeRender(Self); |
| 2002 |
end; |
| 2003 |
|
| 2004 |
class procedure TCastleContainer.RenderControlPrepare(const ViewportRect: TRectangle); |
| 2005 |
begin |
| 2006 |
if GLFeatures.EnableFixedFunction then |
| 2007 |
begin |
| 2008 |
{ Set OpenGL state that is guaranteed for TCastleUserInterface.Render calls, |
| 2009 |
but TCastleUserInterface.Render cannot change it carelessly. } |
| 2010 |
{$ifndef OpenGLES} |
| 2011 |
RenderContext.FixedFunctionLighting := false; |
| 2012 |
glDisable(GL_FOG); |
| 2013 |
{$endif} |
| 2014 |
GLEnableTexture(etNone); |
| 2015 |
end; |
| 2016 |
|
| 2017 |
RenderContext.DepthTest := false; |
| 2018 |
|
| 2019 |
RenderContext.Viewport := ViewportRect; |
| 2020 |
OrthoProjection(FloatRectangle(0, 0, ViewportRect.Width, ViewportRect.Height)); |
| 2021 |
|
| 2022 |
{$ifndef OpenGLES} |
| 2023 |
if GLFeatures.EnableFixedFunction then |
| 2024 |
begin |
| 2025 |
{ Set OpenGL state that may be changed carelessly, and has some |
| 2026 |
guaranteed value, for TCastleUserInterface.Render calls. } |
| 2027 |
glLoadIdentity; |
| 2028 |
{$warnings off} |
| 2029 |
CastleInternalGLUtils.WindowPos := Vector2Integer(0, 0); |
| 2030 |
{$warnings on} |
| 2031 |
end; |
| 2032 |
{$endif} |
| 2033 |
end; |
| 2034 |
|
| 2035 |
procedure TCastleContainer.EventRender; |
| 2036 |
|
| 2037 |
procedure RenderCore; |
| 2038 |
var |
| 2039 |
I: Integer; |
| 2040 |
begin |
| 2041 |
{ draw controls in "to" order, back to front } |
| 2042 |
for I := 0 to Controls.Count - 1 do |
| 2043 |
Controls[I].InternalRecursiveRender(PixelsRect); |
| 2044 |
|
| 2045 |
if FTooltipParent <> nil then |
| 2046 |
begin |
| 2047 |
RenderControlPrepare(PixelsRect); |
| 2048 |
{$warnings off} // using deprecated to keep it working, also to enable TCastleUserInterfaceFont to display tooltip |
| 2049 |
FTooltipParent.TooltipRender(FTooltipPosition); |
| 2050 |
{$warnings on} |
| 2051 |
end; |
| 2052 |
|
| 2053 |
RenderControlPrepare(PixelsRect); |
| 2054 |
if Assigned(OnRender) then OnRender(Self); |
| 2055 |
end; |
| 2056 |
|
| 2057 |
begin |
| 2058 |
if BackgroundEnable then |
| 2059 |
RenderContext.Clear([cbColor], BackgroundColor); |
| 2060 |
|
| 2061 |
FillChar(TDrawableImage.Statistics, SizeOf(TDrawableImage.Statistics), #0); |
| 2062 |
|
| 2063 |
if UserInterfaceBatching then |
| 2064 |
begin |
| 2065 |
TDrawableImage.BatchingBegin; |
| 2066 |
RenderCore; |
| 2067 |
TDrawableImage.BatchingEnd; |
| 2068 |
end else |
| 2069 |
RenderCore; |
| 2070 |
end; |
| 2071 |
|
| 2072 |
procedure TCastleContainer.RenderControl(const Control: TCastleUserInterface; |
| 2073 |
const ViewportRect: TRectangle); |
| 2074 |
var |
| 2075 |
NeedsContainerSet, NeedsGLOpen: Boolean; |
| 2076 |
OldContainer: TCastleContainer; |
| 2077 |
begin |
| 2078 |
NeedsContainerSet := Control.Container <> Self; |
| 2079 |
NeedsGLOpen := not Control.GLInitialized; |
| 2080 |
|
| 2081 |
{ TODO: calling the methods below is not recursive, |
| 2082 |
it will not prepare the children correctly. } |
| 2083 |
if NeedsContainerSet then |
| 2084 |
begin |
| 2085 |
OldContainer := Control.Container; |
| 2086 |
Control.InternalSetContainer(Self); |
| 2087 |
end else |
| 2088 |
OldContainer := nil; // only to silence warning |
| 2089 |
if NeedsGLOpen then |
| 2090 |
Control.GLContextOpen; |
| 2091 |
Control.CheckResize; |
| 2092 |
Control.BeforeRender; |
| 2093 |
Control.InternalRecursiveRender(ViewportRect); |
| 2094 |
|
| 2095 |
{ TODO: calling the methods below is not recursive, |
| 2096 |
it will not unprepare the children correctly. } |
| 2097 |
if NeedsContainerSet then |
| 2098 |
Control.InternalSetContainer(OldContainer); |
| 2099 |
if NeedsGLOpen then |
| 2100 |
Control.GLContextClose; |
| 2101 |
end; |
| 2102 |
|
| 2103 |
const |
| 2104 |
UiScalingUsesContainerSize = [ |
| 2105 |
usEncloseReferenceSize, |
| 2106 |
usEncloseReferenceSizeAutoOrientation, |
| 2107 |
usFitReferenceSize |
| 2108 |
]; |
| 2109 |
|
| 2110 |
procedure TCastleContainer.EventResize; |
| 2111 |
begin |
| 2112 |
if UIScaling in UiScalingUsesContainerSize then |
| 2113 |
{ usXxxReferenceSize adjust current Width/Height to reference, |
| 2114 |
so the FUIScale must be adjusted on each resize. } |
| 2115 |
UpdateUIScale; |
| 2116 |
|
| 2117 |
{ Note that we don't cause TCastleUserInterface.Resize calls now. |
| 2118 |
They are done before BeforeRender, this way culled UI controls |
| 2119 |
(when using @link(TCastleUserInterface.Culling)) |
| 2120 |
are only updated when they are actually visible. |
| 2121 |
This can significantly speed things up when Culling makes sense |
| 2122 |
(lots of off-screen controls). } |
| 2123 |
|
| 2124 |
if Assigned(OnResize) then OnResize(Self); |
| 2125 |
end; |
| 2126 |
|
| 2127 |
function TCastleContainer.Controls: TInternalChildrenControls; |
| 2128 |
begin |
| 2129 |
Result := FControls; |
| 2130 |
end; |
| 2131 |
|
| 2132 |
function TCastleContainer.SettingMousePositionCausesMotion: Boolean; |
| 2133 |
begin |
| 2134 |
Result := true; |
| 2135 |
end; |
| 2136 |
|
| 2137 |
procedure TCastleContainer.MouseLookPress; |
| 2138 |
begin |
| 2139 |
FMouseLookIgnoreNextMotion := false; |
| 2140 |
FMouseLookWaitingForMiddle := false; |
| 2141 |
FMouseLookMotionToSubtract := TVector2.Zero; |
| 2142 |
end; |
| 2143 |
|
| 2144 |
procedure TCastleContainer.MouseLookUpdate; |
| 2145 |
begin |
| 2146 |
{ Never "capture" the motion of the mouse when MouseLook. |
| 2147 |
|
| 2148 |
Without this, using mouse look and then pressing some mouse button that it handled |
| 2149 |
(e.g. to shoot), makes the subsequent dragging not work |
| 2150 |
-- it is blocked for a short time before releasing left mouse button |
| 2151 |
and then it is blocked until you release right mouse button too |
| 2152 |
(because of "if (Capture <> nil) and (MousePressed = []) then" condition). |
| 2153 |
|
| 2154 |
Testcases: |
| 2155 |
|
| 2156 |
- examples/physics/physics_3d_shooter/ |
| 2157 |
|
| 2158 |
Mouse look is active when buttonRight is held. |
| 2159 |
buttonLeft does shoot. |
| 2160 |
|
| 2161 |
So without this fix, shooting while mouse looking |
| 2162 |
(clicking buttonLeft while holding buttonRight) |
| 2163 |
stops mouse look, even though user still holds buttonRight. |
| 2164 |
|
| 2165 |
- examples/fps_game/ |
| 2166 |
|
| 2167 |
As above, though mouse look can also be permanent (after F4). |
| 2168 |
|
| 2169 |
It's a problem in any case (whether mouse look is permanent by F4, |
| 2170 |
or temporary by holding buttonRight): clicking with buttonLeft stops |
| 2171 |
mouse look from working. |
| 2172 |
|
| 2173 |
- examples/third_person_navigation/ |
| 2174 |
|
| 2175 |
We have permanent mouse look by default. |
| 2176 |
|
| 2177 |
As both buttonLeft (shoot) and buttonRight (toggle aim) perform some function, |
| 2178 |
they both stop mouse look from working. |
| 2179 |
} |
| 2180 |
FCaptureInput.Clear; |
| 2181 |
// this is like ReleaseCapture(...) for all possible arguments |
| 2182 |
end; |
| 2183 |
|
| 2184 |
procedure TCastleContainer.MouseLookIgnoreNextMotion; |
| 2185 |
begin |
| 2186 |
FMouseLookIgnoreNextMotion := true; |
| 2187 |
end; |
| 2188 |
|
| 2189 |
function TCastleContainer.MouseLookDelta(const Event: TInputMotion): TVector2; |
| 2190 |
begin |
| 2191 |
Result := MouseLookDelta(Event, FloatRectangle(PixelsRect)); |
| 2192 |
end; |
| 2193 |
|
| 2194 |
function TCastleContainer.MouseLookDelta(const Event: TInputMotion; |
| 2195 |
const ControlRect: TFloatRectangle): TVector2; |
| 2196 |
|
| 2197 |
{ There's a lot of tricky things in implementing this correctly. |
| 2198 |
|
| 2199 |
1. Initially we tried to set mouse position to container center very often |
| 2200 |
(in every Update, and/or in every MouseLookDelta). |
| 2201 |
|
| 2202 |
But then: |
| 2203 |
Doing "Result := Event.Position - Event.OldPosition" is bad. |
| 2204 |
Instead "Result := Event.Position - Middle" is better: |
| 2205 |
|
| 2206 |
That is because setting MousePosition may (but doesn't have to) |
| 2207 |
generate another Motion in the container to destination position. |
| 2208 |
|
| 2209 |
Subtracting Middle (instead of Container.Position, previous |
| 2210 |
known mouse position) solves it. This way |
| 2211 |
|
| 2212 |
- The Motion caused by MakeMousePositionedForMouseLook will not do |
| 2213 |
anything bad, as MouseChange wil be 0 then. |
| 2214 |
|
| 2215 |
- In case MakeMousePositionedForMouseLook does not cause Motion, |
| 2216 |
we will not measure the changes as too much. Consider this: |
| 2217 |
|
| 2218 |
- player moves mouse to MiddleX-10 |
| 2219 |
- Motion is generated, I rotate camera by "-10" horizontally |
| 2220 |
- Setting MousePosition sets mouse to the Middle, |
| 2221 |
but this time no Motion is generated |
| 2222 |
- player moved mouse to MiddleX+10. Although mouse was |
| 2223 |
positioned on Middle, TCastleWindow thinks that the mouse |
| 2224 |
is still positioned on Middle-10, and I will get "+20" move |
| 2225 |
for player (while I should get only "+10") |
| 2226 |
|
| 2227 |
2. Unfortunately "Result := Event.Position - Middle" |
| 2228 |
is also bad, in case there's unspecified delay in applying SetMousePosition. |
| 2229 |
Effectively, we would apply some differences in positions many times, |
| 2230 |
making the speed of MouseLookDelta faster than normal dragging |
| 2231 |
(and the exact factor "how much faster" can depend on mouse sensitivity, |
| 2232 |
how much is SetMousePosition delayed (on Windows 10 it can be delayed a lot, |
| 2233 |
more than one frame very often)). |
| 2234 |
|
| 2235 |
This was easily seen in Unholy fight, and dragging_test example in CGE. |
| 2236 |
|
| 2237 |
The new fix is that we allow mouse in a large area in the center, |
| 2238 |
and we do SetMousePosition only when it's outside of this large area. |
| 2239 |
This allows to easily reject Motion events that report such move, |
| 2240 |
as they will have ValidArea(new position)=true but ValidArea(old position)=false. |
| 2241 |
|
| 2242 |
In case the Motion event has a summed value of "moving to center by SetMousePosition |
| 2243 |
+ movement from user", we will ignore too much, but this should |
| 2244 |
not have noticeable effect on speed (as it will happen seldom). |
| 2245 |
|
| 2246 |
3. Actually, 2 was also invalid. |
| 2247 |
|
| 2248 |
The new fix is to just use motion delta always, |
| 2249 |
and use explicit SettingMousePositionCausesMotion knowwledge |
| 2250 |
to eventually subtract FMouseLookMotionToSubtract. |
| 2251 |
|
| 2252 |
4. Another problem is when player switches to another window, moves the mouse, |
| 2253 |
than goes Alt+Tab back to our window. |
| 2254 |
Next mouse move would cause huge change, |
| 2255 |
because it's really *not* from the middle of the screen. |
| 2256 |
|
| 2257 |
Solution to this is to use FMouseLookIgnoreNextMotion |
| 2258 |
for the next event. |
| 2259 |
|
| 2260 |
5. Note that earlier I tried to reposition mouse for mouse look at various |
| 2261 |
moments. I tried calling MakeMousePositionedForMouseLook from Press, Motion, |
| 2262 |
Update... all these approaches had some drawbacks, and eventually |
| 2263 |
they have one big drawback: this could result in Motion that isn't rejected |
| 2264 |
below, causing weird first movement. |
| 2265 |
} |
| 2266 |
|
| 2267 |
{ Position is roughly within the center of the window. } |
| 2268 |
function ValidArea(const P, Middle: TVector2; const Margin: Single): Boolean; |
| 2269 |
begin |
| 2270 |
Result := (Abs(P.X - Middle.X) < Margin * ControlRect.Width) and |
| 2271 |
(Abs(P.Y - Middle.Y) < Margin * ControlRect.Height); |
| 2272 |
end; |
| 2273 |
|
| 2274 |
var |
| 2275 |
Middle: TVector2; |
| 2276 |
begin |
| 2277 |
Result := TVector2.Zero; |
| 2278 |
|
| 2279 |
Middle := ControlRect.Center; |
| 2280 |
{ Round to integers, because if CastleWindow backend doesn't support |
| 2281 |
sup-pixel accuracy for SetMousePosition, then it ignores the fractional |
| 2282 |
part of SetMousePosition argument. |
| 2283 |
Such fractional part would only cause later "error by 0.5 (or other epsilon)" |
| 2284 |
when using FMouseLookMotionToSubtract to modify Result. } |
| 2285 |
Middle.X := Trunc(Middle.X); |
| 2286 |
Middle.Y := Trunc(Middle.Y); |
| 2287 |
|
| 2288 |
if FMouseLookIgnoreNextMotion then |
| 2289 |
begin |
| 2290 |
FMouseLookIgnoreNextMotion := false; |
| 2291 |
FMouseLookWaitingForMiddle := false; |
| 2292 |
FMouseLookMotionToSubtract := TVector2.Zero; |
| 2293 |
Exit; |
| 2294 |
end; |
| 2295 |
|
| 2296 |
Result := Event.Position - Event.OldPosition; |
| 2297 |
|
| 2298 |
{ If FMouseLookWaitingForMiddle then we have set MousePosition := Middle, |
| 2299 |
and now we wait for MousePosition to be reported near Middle. |
| 2300 |
|
| 2301 |
Until this happens, we don't apply FMouseLookMotionToSubtract, |
| 2302 |
(as it would cause unexpected shift before the "MousePosition := Middle" |
| 2303 |
is actually realized). |
| 2304 |
|
| 2305 |
We still set MousePosition := Middle again |
| 2306 |
(although we already wait for it to happen), |
| 2307 |
in case waiting on "ValidArea(Event.Position, Middle, 1 / 8)" is not reliable. |
| 2308 |
} |
| 2309 |
if FMouseLookWaitingForMiddle and ValidArea(Event.Position, Middle, 1 / 8) then |
| 2310 |
FMouseLookWaitingForMiddle := false; |
| 2311 |
|
| 2312 |
if (not FMouseLookWaitingForMiddle) and SettingMousePositionCausesMotion then |
| 2313 |
begin |
| 2314 |
Result := Result - FMouseLookMotionToSubtract; |
| 2315 |
FMouseLookMotionToSubtract := TVector2.Zero; |
| 2316 |
end; |
| 2317 |
|
| 2318 |
// When mouse gets too far away from Middle, move it back to Middle. |
| 2319 |
if not ValidArea(Event.Position, Middle, 1 / 4) then |
| 2320 |
begin |
| 2321 |
if not FMouseLookWaitingForMiddle then |
| 2322 |
FMouseLookMotionToSubtract := FMouseLookMotionToSubtract + Middle - Event.Position; |
| 2323 |
MousePosition := Middle; |
| 2324 |
FMouseLookWaitingForMiddle := true; |
| 2325 |
end; |
| 2326 |
end; |
| 2327 |
|
| 2328 |
procedure TCastleContainer.SetUIScaling(const Value: TUIScaling); |
| 2329 |
begin |
| 2330 |
if FUIScaling <> Value then |
| 2331 |
begin |
| 2332 |
FUIScaling := Value; |
| 2333 |
UpdateUIScale; |
| 2334 |
end; |
| 2335 |
end; |
| 2336 |
|
| 2337 |
procedure TCastleContainer.SetDpi(const Value: Single); |
| 2338 |
begin |
| 2339 |
if FDpi <> Value then |
| 2340 |
begin |
| 2341 |
FDpi := Value; |
| 2342 |
if UIScaling = usDpiScale then |
| 2343 |
UpdateUIScale; |
| 2344 |
end; |
| 2345 |
end; |
| 2346 |
|
| 2347 |
procedure TCastleContainer.SetUIReferenceWidth(const Value: Single); |
| 2348 |
begin |
| 2349 |
if FUIReferenceWidth <> Value then |
| 2350 |
begin |
| 2351 |
FUIReferenceWidth := Value; |
| 2352 |
UpdateUIScale; |
| 2353 |
end; |
| 2354 |
end; |
| 2355 |
|
| 2356 |
procedure TCastleContainer.SetUIReferenceHeight(const Value: Single); |
| 2357 |
begin |
| 2358 |
if FUIReferenceHeight <> Value then |
| 2359 |
begin |
| 2360 |
FUIReferenceHeight := Value; |
| 2361 |
UpdateUIScale; |
| 2362 |
end; |
| 2363 |
end; |
| 2364 |
|
| 2365 |
procedure TCastleContainer.SetUIExplicitScale(const Value: Single); |
| 2366 |
begin |
| 2367 |
if FUIExplicitScale <> Value then |
| 2368 |
begin |
| 2369 |
FUIExplicitScale := Value; |
| 2370 |
UpdateUIScale; |
| 2371 |
end; |
| 2372 |
end; |
| 2373 |
|
| 2374 |
class function TCastleContainer.InternalCalculateUIScale( |
| 2375 |
const AUIScaling: TUIScaling; |
| 2376 |
AUIReferenceWidth, AUIReferenceHeight: Single; |
| 2377 |
const AUIExplicitScale: Single; |
| 2378 |
const ADpi, AWidth, AHeight: Single): Single; |
| 2379 |
begin |
| 2380 |
case AUIScaling of |
| 2381 |
usNone : Result := 1; |
| 2382 |
usExplicitScale: Result := AUIExplicitScale; |
| 2383 |
usDpiScale : |
| 2384 |
begin |
| 2385 |
WritelnLog('UI scaling', 'Using dots (pixels) per inch %f (%f * default %f)', |
| 2386 |
[ADpi, ADpi / DefaultDpi, DefaultDpi]); |
| 2387 |
Result := ADpi / DefaultDpi; |
| 2388 |
end; |
| 2389 |
usEncloseReferenceSize, usEncloseReferenceSizeAutoOrientation, usFitReferenceSize: |
| 2390 |
begin |
| 2391 |
Result := 1; |
| 2392 |
|
| 2393 |
{ Implement usEncloseReferenceSizeAutoOrientation difference over |
| 2394 |
usEncloseReferenceSize: swap reference width/height to match |
| 2395 |
current screen orientation. } |
| 2396 |
if (AUIScaling = usEncloseReferenceSizeAutoOrientation) and |
| 2397 |
(AWidth > 0) and |
| 2398 |
(AHeight > 0) and |
| 2399 |
(AUIReferenceWidth > 0) and |
| 2400 |
(AUIReferenceHeight > 0) then |
| 2401 |
begin |
| 2402 |
if AWidth > AHeight then |
| 2403 |
begin |
| 2404 |
if AUIReferenceWidth < AUIReferenceHeight then |
| 2405 |
SwapValues(AUIReferenceWidth, AUIReferenceHeight); |
| 2406 |
end else |
| 2407 |
if AWidth < AHeight then |
| 2408 |
begin |
| 2409 |
if AUIReferenceWidth > AUIReferenceHeight then |
| 2410 |
SwapValues(AUIReferenceWidth, AUIReferenceHeight); |
| 2411 |
end; |
| 2412 |
Assert( |
| 2413 |
(AWidth > AHeight) = |
| 2414 |
(AUIReferenceWidth > AUIReferenceHeight) |
| 2415 |
); |
| 2416 |
end; |
| 2417 |
|
| 2418 |
if (AUIReferenceWidth <> 0) and (AWidth > 0) then |
| 2419 |
begin |
| 2420 |
Result := AWidth / AUIReferenceWidth; |
| 2421 |
if (AUIReferenceHeight <> 0) and (AHeight > 0) then |
| 2422 |
if AUIScaling in [ |
| 2423 |
usEncloseReferenceSize, |
| 2424 |
usEncloseReferenceSizeAutoOrientation |
| 2425 |
] then |
| 2426 |
MinVar(Result, AHeight / AUIReferenceHeight) |
| 2427 |
else |
| 2428 |
MaxVar(Result, AHeight / AUIReferenceHeight); |
| 2429 |
end else |
| 2430 |
if (AUIReferenceHeight <> 0) and (AHeight > 0) then |
| 2431 |
Result := AHeight / AUIReferenceHeight; |
| 2432 |
// Too talkative when resizing a window (also in castle-editor) |
| 2433 |
// WritelnLog('Scaling', 'Automatic scaling to reference sizes %f x %f in effect. Actual window size is %d x %d. Calculated scale is %f, which simulates surface of size %f x %f.', |
| 2434 |
// [AUIReferenceWidth, AUIReferenceHeight, |
| 2435 |
// AWidth, AHeight, |
| 2436 |
// Result, AWidth / Result, AHeight / Result]); |
| 2437 |
end; |
| 2438 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 2439 |
else raise EInternalError.Create('UIScaling unknown'); |
| 2440 |
{$endif} |
| 2441 |
end; |
| 2442 |
end; |
| 2443 |
|
| 2444 |
|
| 2445 |
procedure TCastleContainer.UpdateUIScale; |
| 2446 |
begin |
| 2447 |
if (not GLInitialized) and |
| 2448 |
(UIScaling in UiScalingUsesContainerSize) then |
| 2449 |
// don't adjust FUIScale before our Width/Height are sensible |
| 2450 |
FUIScale := 1 |
| 2451 |
else |
| 2452 |
FUIScale := InternalCalculateUIScale( |
| 2453 |
UIScaling, UIReferenceWidth, UIReferenceHeight, UIExplicitScale, |
| 2454 |
Dpi, PixelsWidth, PixelsHeight); |
| 2455 |
|
| 2456 |
{ When FSafeBorderScaled is non-zero, then changing FUIScale also changed |
| 2457 |
SafeBorder result. } |
| 2458 |
if not FSafeBorderScaled.IsPerfectlyZero then |
| 2459 |
if Assigned(FOnSafeBorderChanged) then |
| 2460 |
FOnSafeBorderChanged(Self); |
| 2461 |
|
| 2462 |
{ Note that we don't cause TCastleUserInterface.UIScaleChanged calls now. |
| 2463 |
They are done before BeforeRender, this way culled UI controls |
| 2464 |
(when using @link(TCastleUserInterface.Culling)) |
| 2465 |
are only updated when they are actually visible. |
| 2466 |
This can significantly speed things up when Culling makes sense |
| 2467 |
(lots of off-screen controls). } |
| 2468 |
end; |
| 2469 |
|
| 2470 |
function TCastleContainer.UnscaledWidth: Single; |
| 2471 |
begin |
| 2472 |
Result := PixelsWidth / FUIScale; |
| 2473 |
end; |
| 2474 |
|
| 2475 |
function TCastleContainer.UnscaledHeight: Single; |
| 2476 |
begin |
| 2477 |
Result := PixelsHeight / FUIScale; |
| 2478 |
end; |
| 2479 |
|
| 2480 |
function TCastleContainer.UnscaledRect: TFloatRectangle; |
| 2481 |
begin |
| 2482 |
Result := FloatRectangle(PixelsRect); |
| 2483 |
if not Result.IsEmpty then |
| 2484 |
begin |
| 2485 |
Result.Width := Result.Width / FUIScale; |
| 2486 |
Result.Height := Result.Height / FUIScale; |
| 2487 |
end; |
| 2488 |
end; |
| 2489 |
|
| 2490 |
function TCastleContainer.ScaledStatusBarHeight: Cardinal; |
| 2491 |
begin |
| 2492 |
Result := Round(FSafeBorderScaled.X); |
| 2493 |
end; |
| 2494 |
|
| 2495 |
function TCastleContainer.StatusBarHeight: Single; |
| 2496 |
begin |
| 2497 |
Result := SafeBorder.Top; |
| 2498 |
end; |
| 2499 |
|
| 2500 |
function TCastleContainer.SaveScreen(const SaveRect: TRectangle): TRGBImage; |
| 2501 |
begin |
| 2502 |
EventBeforeRender; |
| 2503 |
EventRender; |
| 2504 |
{ This is correct if we use double-buffer. } |
| 2505 |
Result := SaveScreen_NoFlush(SaveRect, cbBack); |
| 2506 |
end; |
| 2507 |
|
| 2508 |
procedure TCastleContainer.SaveScreen(const Url: String); |
| 2509 |
var |
| 2510 |
Image: TRGBImage; |
| 2511 |
begin |
| 2512 |
Image := SaveScreen; |
| 2513 |
try |
| 2514 |
WritelnLog('SaveScreen', 'Screen saved to ' + Url); |
| 2515 |
SaveImage(Image, Url); |
| 2516 |
finally FreeAndNil(Image) end; |
| 2517 |
end; |
| 2518 |
|
| 2519 |
function TCastleContainer.SaveScreen: TRGBImage; |
| 2520 |
begin |
| 2521 |
Result := SaveScreen(PixelsRect); |
| 2522 |
end; |
| 2523 |
|
| 2524 |
function TCastleContainer.SaveScreen(const SaveRect: TFloatRectangle): TRGBImage; |
| 2525 |
begin |
| 2526 |
Result := SaveScreen(SaveRect.Round); |
| 2527 |
end; |
| 2528 |
|
| 2529 |
function TCastleContainer.SaveScreenToDefaultFile: String; |
| 2530 |
var |
| 2531 |
Path: String; |
| 2532 |
begin |
| 2533 |
Path := SaveScreenPath; |
| 2534 |
if Path <> '' then |
| 2535 |
begin |
| 2536 |
Result := FilenameToUriSafe(FileNameAutoInc(Path + ApplicationName, '_screen_%d.png')); |
| 2537 |
SaveScreen(Result); |
| 2538 |
WritelnLog('Screen saved to ' + Result); |
| 2539 |
end else |
| 2540 |
Result := ''; |
| 2541 |
end; |
| 2542 |
|
| 2543 |
function TCastleContainer.SaveScreenRgba(const SaveRect: TRectangle): TRGBAlphaImage; |
| 2544 |
begin |
| 2545 |
EventBeforeRender; |
| 2546 |
EventRender; |
| 2547 |
{ This is correct if we use double-buffer. } |
| 2548 |
Result := SaveScreen_NoFlush(TRGBAlphaImage, SaveRect, cbBack) as TRGBAlphaImage; |
| 2549 |
end; |
| 2550 |
|
| 2551 |
function TCastleContainer.SaveScreenRgba: TRGBAlphaImage; |
| 2552 |
begin |
| 2553 |
Result := SaveScreenRgba(PixelsRect); |
| 2554 |
end; |
| 2555 |
|
| 2556 |
function TCastleContainer.Width: Integer; |
| 2557 |
begin |
| 2558 |
Result := PixelsWidth; |
| 2559 |
end; |
| 2560 |
|
| 2561 |
function TCastleContainer.Height: Integer; |
| 2562 |
begin |
| 2563 |
Result := PixelsHeight; |
| 2564 |
end; |
| 2565 |
|
| 2566 |
function TCastleContainer.Rect: TRectangle; |
| 2567 |
begin |
| 2568 |
Result := PixelsRect; |
| 2569 |
end; |
| 2570 |
|
| 2571 |
function TCastleContainer.PixelsRect: TRectangle; |
| 2572 |
begin |
| 2573 |
Result := Rectangle(0, 0, PixelsWidth, PixelsHeight); |
| 2574 |
end; |
| 2575 |
|
| 2576 |
procedure TCastleContainer.Invalidate; |
| 2577 |
begin |
| 2578 |
{ Default implementation, does nothing, assuming the main program redraws in a loop. } |
| 2579 |
end; |
| 2580 |
|
| 2581 |
function TCastleContainer.Focused: Boolean; |
| 2582 |
begin |
| 2583 |
{ Default implementation, assuming that the context is always focused. } |
| 2584 |
Result := true; |
| 2585 |
end; |
| 2586 |
|
| 2587 |
procedure TCastleContainer.SetInternalCursor(const Value: TMouseCursor); |
| 2588 |
begin |
| 2589 |
{ Default implementation, ignores new cursor value. } |
| 2590 |
end; |
| 2591 |
|
| 2592 |
function TCastleContainer.GetMousePosition: TVector2; |
| 2593 |
begin |
| 2594 |
{ Default implementation, assuming mouse is glued to the middle of the screen. |
| 2595 |
Some methods, like TCastleContainer.UpdateFocusAndMouseCursor, |
| 2596 |
use this to calculate "focused" CGE control. |
| 2597 |
} |
| 2598 |
Result := Vector2(PixelsWidth / 2, PixelsHeight / 2); |
| 2599 |
end; |
| 2600 |
|
| 2601 |
procedure TCastleContainer.SetMousePosition(const Value: TVector2); |
| 2602 |
begin |
| 2603 |
{ Default implementation, ignores new mouse position. } |
| 2604 |
end; |
| 2605 |
|
| 2606 |
function TCastleContainer.GetTouches(const Index: Integer): TTouch; |
| 2607 |
begin |
| 2608 |
Assert(Index = 0, 'Base TCastleContainer implementation does not support multi-touch, so you can only access Touches[0]'); |
| 2609 |
Result.FingerIndex := 0; |
| 2610 |
Result.Position := MousePosition; |
| 2611 |
end; |
| 2612 |
|
| 2613 |
function TCastleContainer.TouchesCount: Integer; |
| 2614 |
begin |
| 2615 |
Result := 1; |
| 2616 |
end; |
| 2617 |
|
| 2618 |
function TCastleContainer.GLInitialized: Boolean; |
| 2619 |
begin |
| 2620 |
{ Default implementation, assuming the OpenGL context is always initialized. } |
| 2621 |
Result := true; |
| 2622 |
end; |
| 2623 |
|
| 2624 |
procedure TCastleContainer.LoadSettings(const SettingsUrl: String); |
| 2625 |
begin |
| 2626 |
SettingsLoad(Self, SettingsUrl); |
| 2627 |
end; |
| 2628 |
|
| 2629 |
function TCastleContainer.GetInspectorKey: TKey; |
| 2630 |
begin |
| 2631 |
Result := FInputInspector.Key; |
| 2632 |
end; |
| 2633 |
|
| 2634 |
procedure TCastleContainer.SetInspectorKey(const Value: TKey); |
| 2635 |
begin |
| 2636 |
FInputInspector.Key := Value; |
| 2637 |
end; |
| 2638 |
|
| 2639 |
function TCastleContainer.GetView: TCastleView; |
| 2640 |
begin |
| 2641 |
if (FViewStack = nil) or |
| 2642 |
(FViewStack.Count = 0) then |
| 2643 |
Result := nil |
| 2644 |
else |
| 2645 |
Result := FViewStack[0]; |
| 2646 |
end; |
| 2647 |
|
| 2648 |
function TCastleContainer.GetFrontView: TCastleView; |
| 2649 |
begin |
| 2650 |
if (FViewStack = nil) or |
| 2651 |
(FViewStack.Count = 0) then |
| 2652 |
Result := nil |
| 2653 |
else |
| 2654 |
Result := FViewStack[FViewStack.Count - 1]; |
| 2655 |
end; |
| 2656 |
|
| 2657 |
procedure TCastleContainer.SetView(const Value: TCastleView); |
| 2658 |
begin |
| 2659 |
{ exit early if there's nothing to do } |
| 2660 |
if (ViewStackCount = 0) and (Value = nil) then |
| 2661 |
Exit; |
| 2662 |
if (ViewStackCount = 1) and (FViewStack[0] = Value) then |
| 2663 |
Exit; |
| 2664 |
|
| 2665 |
if FDisableStackChange <> 0 then |
| 2666 |
ErrorStackChangeDisabled('Cannot change TCastleView.View from inside of TCastleView.Start/Resume/Pause/Stop'); |
| 2667 |
|
| 2668 |
{ Remove and finish topmost view. |
| 2669 |
The loop is written to work even when some view Stop method |
| 2670 |
changes views. } |
| 2671 |
while ViewStackCount <> 0 do |
| 2672 |
PopView; |
| 2673 |
{ deallocate empty FViewStack } |
| 2674 |
if Value = nil then |
| 2675 |
FreeAndNil(FViewStack); |
| 2676 |
|
| 2677 |
PushView(Value); |
| 2678 |
end; |
| 2679 |
|
| 2680 |
procedure TCastleContainer.PushView(const NewView: TCastleView); |
| 2681 |
begin |
| 2682 |
if NewView <> nil then |
| 2683 |
begin |
| 2684 |
if FDisableStackChange <> 0 then |
| 2685 |
ErrorStackChangeDisabled('Cannot call TCastleContainer.Push from inside of TCastleView.Start/Resume/Pause/Stop'); |
| 2686 |
|
| 2687 |
Inc(FDisableStackChange); |
| 2688 |
try |
| 2689 |
{ pause previous top-most view } |
| 2690 |
if (FViewStack <> nil) and |
| 2691 |
(FViewStack.Count <> 0) then |
| 2692 |
FViewStack.Last.Pause; |
| 2693 |
|
| 2694 |
{ create FViewStack on demand now } |
| 2695 |
if FViewStack = nil then |
| 2696 |
FViewStack := TCastleViewList.Create(false); |
| 2697 |
FViewStack.Add(NewView); |
| 2698 |
NewView.InternalStart(Self); |
| 2699 |
NewView.Resume; |
| 2700 |
finally |
| 2701 |
Dec(FDisableStackChange) |
| 2702 |
end; |
| 2703 |
end; |
| 2704 |
end; |
| 2705 |
|
| 2706 |
procedure TCastleContainer.PopView; |
| 2707 |
var |
| 2708 |
SavedFrontView: TCastleView; |
| 2709 |
begin |
| 2710 |
if FDisableStackChange <> 0 then |
| 2711 |
ErrorStackChangeDisabled('Cannot call TCastleContainer.Pop from inside of TCastleView.Start/Resume/Pause/Stop'); |
| 2712 |
|
| 2713 |
Inc(FDisableStackChange); |
| 2714 |
try |
| 2715 |
SavedFrontView := FrontView; |
| 2716 |
SavedFrontView.Pause; |
| 2717 |
SavedFrontView.InternalStop; |
| 2718 |
if SavedFrontView = FrontView then |
| 2719 |
FViewStack.Delete(FViewStack.Count - 1) |
| 2720 |
else |
| 2721 |
WritelnWarning('TCastleView', 'Topmost view is no longer topmost after its Stop method. Do not change view stack from view Stop methods.'); |
| 2722 |
|
| 2723 |
if SavedFrontView.FFreeWhenStopped then |
| 2724 |
FreeAndNil(SavedFrontView); |
| 2725 |
|
| 2726 |
{ resume new top-most view } |
| 2727 |
if (FViewStack <> nil) and |
| 2728 |
(FViewStack.Count <> 0) then |
| 2729 |
FViewStack.Last.Resume; |
| 2730 |
finally |
| 2731 |
Dec(FDisableStackChange) |
| 2732 |
end; |
| 2733 |
end; |
| 2734 |
|
| 2735 |
procedure TCastleContainer.PopView(const CheckFrontView: TCastleView); |
| 2736 |
begin |
| 2737 |
if (FViewStack = nil) or (FViewStack.Count = 0) then |
| 2738 |
begin |
| 2739 |
WritelnWarning('TCastleView', 'Cannot pop UI view, that stack is empty'); |
| 2740 |
Exit; |
| 2741 |
end; |
| 2742 |
if FViewStack.Last <> CheckFrontView then |
| 2743 |
begin |
| 2744 |
WritelnWarning('TCastleView', 'Cannot pop UI view, front (top-most) view is expected to be ' + CheckFrontView.ClassName + ', but is ' + FViewStack.Last.ClassName); |
| 2745 |
Exit; |
| 2746 |
end; |
| 2747 |
|
| 2748 |
PopView; |
| 2749 |
end; |
| 2750 |
|
| 2751 |
function TCastleContainer.ViewStackCount: Integer; |
| 2752 |
begin |
| 2753 |
if FViewStack = nil then |
| 2754 |
Result := 0 |
| 2755 |
else |
| 2756 |
Result := FViewStack.Count; |
| 2757 |
end; |
| 2758 |
|
| 2759 |
function TCastleContainer.GetViewStack(const Index: Integer): TCastleView; |
| 2760 |
begin |
| 2761 |
if FViewStack = nil then |
| 2762 |
raise EInternalError.CreateFmt('TCastleView.GetViewStack: view stack is empty, cannot get view index %d', |
| 2763 |
[Index]); |
| 2764 |
Result := FViewStack[Index]; |
| 2765 |
end; |
| 2766 |
|
| 2767 |
procedure TCastleContainer.InternalSetSafeBorderScaled(const Value: TVector4); |
| 2768 |
begin |
| 2769 |
FSafeBorderScaled := Value; |
| 2770 |
WritelnLog('Safe borders updated. Size scaled (in final pixels): %s, size accounting for UI scaling: %s', [ |
| 2771 |
FSafeBorderScaled.ToString, |
| 2772 |
SafeBorder.ToString |
| 2773 |
]); |
| 2774 |
if Assigned(FOnSafeBorderChanged) then |
| 2775 |
FOnSafeBorderChanged(Self); |
| 2776 |
end; |
| 2777 |
|
| 2778 |
function TCastleContainer.MessageReceived(const Received: TCastleStringList; |
| 2779 |
const ReceivedStream: TMemoryStream): Boolean; |
| 2780 |
var |
| 2781 |
NewSafeBorderScaled: TVector4; |
| 2782 |
begin |
| 2783 |
Result := false; |
| 2784 |
if (Received.Count = 5) and |
| 2785 |
(Received[0] = 'safe-borders') then |
| 2786 |
begin |
| 2787 |
NewSafeBorderScaled := Vector4( |
| 2788 |
StrToInt(Received[1]), |
| 2789 |
StrToInt(Received[2]), |
| 2790 |
StrToInt(Received[3]), |
| 2791 |
StrToInt(Received[4]) |
| 2792 |
); |
| 2793 |
InternalSetSafeBorderScaled(NewSafeBorderScaled); |
| 2794 |
Result := true; |
| 2795 |
end |
| 2796 |
end; |
| 2797 |
|
| 2798 |
function TCastleContainer.SafeBorder: TBorder; |
| 2799 |
begin |
| 2800 |
// assign from FSafeBorderScaled and adjust for UI scaling |
| 2801 |
FSafeBorder.Top := FSafeBorderScaled.X / UIScale; |
| 2802 |
FSafeBorder.Right := FSafeBorderScaled.Y / UIScale; |
| 2803 |
FSafeBorder.Bottom := FSafeBorderScaled.Z / UIScale; |
| 2804 |
FSafeBorder.Left := FSafeBorderScaled.W / UIScale; |
| 2805 |
Result := FSafeBorder; |
| 2806 |
end; |
| 2807 |
|
| 2808 |
function TCastleContainer.FocusFront: TCastleUserInterface; |
| 2809 |
begin |
| 2810 |
if ForceCaptureInput <> nil then |
| 2811 |
Result := ForceCaptureInput |
| 2812 |
else |
| 2813 |
if FFocus.Count <> 0 then |
| 2814 |
Result := FFocus.Last // last item is front-most |
| 2815 |
else |
| 2816 |
Result := nil; |
| 2817 |
end; |
| 2818 |
|
| 2819 |
{$endif read_implementation} |