/usr/src/castle-game-engine-6.4/base/castleapplicationproperties.pas is in castle-game-engine-src 6.4+dfsg1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | {
Copyright 2014-2017 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{ Events and properties of the Castle Game Engine application
(TCastleApplicationProperties). }
unit CastleApplicationProperties;
{$I castleconf.inc}
interface
uses Generics.Collections,
CastleClassUtils;
type
TGLContextEvent = procedure;
TGLContextEventList = class({$ifdef CASTLE_OBJFPC}specialize{$endif} TList<TGLContextEvent>)
public
{ Call all items, first to last. }
procedure ExecuteForward;
{ Call all items, last to first. }
procedure ExecuteBackward;
end;
TWarningEvent = procedure (Sender: TObject; const Category, Message: string) of object;
TWarningEventList = class({$ifdef CASTLE_OBJFPC}specialize{$endif} TList<TWarningEvent>)
public
procedure ExecuteAll(Sender: TObject; const Category, Message: string);
end;
{ Events and properties of the Castle Game Engine application,
usually accessed through the @link(ApplicationProperties) singleton.
These members work regardless if you use CastleWindow or CastleControl.
For more fine-grained application control,
see TCastleApplication (in case you use CastleWindow)
or Lazarus (LCL) TApplication (in case you use CastleControl). }
TCastleApplicationProperties = class
private
FIsGLContextOpen, FFileAccessSafe: boolean;
FOnGLContextOpen, FOnGLContextClose: TGLContextEventList;
FOnUpdate, FOnInitializeJavaActivity,
FOnGLContextOpenObject, FOnGLContextCloseObject,
FOnPause, FOnResume: TNotifyEventList;
FOnWarning: TWarningEventList;
FVersion: string;
FTouchDevice: boolean;
function GetApplicationName: string;
procedure SetApplicationName(const Value: string);
public
constructor Create;
destructor Destroy; override;
{ Application short name.
Used e.g. by @link(InitializeLog) to name the log file.
When compiled with FPC, this returns and sets the same thing
as standard SysUtils.ApplicationName.
When setting this, we automatically set SysUtils.OnGetApplicationName. }
property ApplicationName: string read GetApplicationName write SetApplicationName;
{ Version of this application.
It may be used e.g. by @link(InitializeLog) and
@link(TCastleApplication.ParseStandardParameters). }
property Version: string read FVersion write FVersion;
{ Initialized to @true on touch devices (Android, iOS).
A "touch device" means that:
@unorderedList(
@item(We cannot track @link(TUIContainer.MousePosition)
when nothing is pressed (@link(TUIContainer.MousePressed) is [])
on a touch device.)
@item(The only "mouse button" you will ever see pressed
on a touch device is mbLeft.)
@item(On the other hand, touch devices support multitouch, exposed by
@link(TUIContainer.Touches) and @link(TUIContainer.TouchesCount).
On touch devices, @link(TUIContainer.TouchesCount) can range
from 0 to a few (modern touch devices support up to 5 simultaneous
touches).
On non-touch devices, @link(TUIContainer.TouchesCount) is always 1.)
)
As a debugging feature, you can set this to @true
to simulate touch devices on a desktop.
The idea is that when an application shows a different input behaviour
on touch devices, it should always condition it depending on
this boolean property. So an application may do this:
@longCode(#
Window.AutomaticTouchInterface := ApplicationProperties.TouchDevice;
Window.ScenaManager.WalkCamera.MouseLook := not ApplicationProperties.TouchDevice;
#)
And to test on desktop whether everything behaves OK on mobile,
you can just earlier call this:
@longCode(#
if FakeTouchDeviceOnDesktop then
ApplicationProperties.TouchDevice := true;
#)
}
property TouchDevice: boolean read FTouchDevice write FTouchDevice;
{ Callbacks called when the OpenGL context is opened or closed.
Use when you want to be notified about OpenGL context availability,
but cannot refer to a particular instance of TCastleControl or TCastleWindow.
Note that we may have many OpenGL contexts (many
TCastleWindow or TCastleControl instances) open simultaneously.
They all share OpenGL resources.
OnGLContextOpen is called when first OpenGL context is open,
that is: no previous context was open.
OnGLContextClose is called when last OpenGL context is closed,
that is: no more contexts remain open.
Note that this implies that they may be called many times:
e.g. if you open one window, then close it, then open another
window then close it.
Callbacks on OnGLContextOpen are called from first to last.
Callbacks on OnGLContextClose are called in reverse order,
so OnGLContextClose[0] is called last.
@groupBegin }
property OnGLContextOpen: TGLContextEventList read FOnGLContextOpen;
property OnGLContextOpenObject: TNotifyEventList read FOnGLContextOpenObject;
property OnGLContextClose: TGLContextEventList read FOnGLContextClose;
property OnGLContextCloseObject: TNotifyEventList read FOnGLContextCloseObject;
{ @groupEnd }
{ Is the OpenGL context available. IOW, we are between the first OnGLContextOpen
and last OnGLContextClose. }
property IsGLContextOpen: boolean read FIsGLContextOpen;
{ Callbacks called continously when (at least one) window is open.
You can use this just like @link(TCastleControlCustom.OnUpdate)
or @link(TCastleWindowCustom.OnUpdate) or @link(TCastleApplication.OnUpdate),
but in situations where you cannot access an instance of control/window
and you want to work both with Lazarus @link(TCastleControl)
and our custom @link(TCastleApplication). }
property OnUpdate: TNotifyEventList read FOnUpdate;
{ Callbacks called when Android Java activity started.
Called every time a Java activity is created.
@unorderedList(
@item(For the first time, it's called right before
@link(TCastleApplication.OnInitialize).)
@item(Later this is called when Java activity
died (and is restarting now), but the native code thread survived.
So all native code memory is already cool (no need to call
@link(TCastleApplication.OnInitialize)),
but we need to reinitialize Java part.
Note that this is different from @link(TCastleWindowCustom.OnOpen).
We lose OpenGL context often, actually every time user switches to another
app, without having neither Java nor native threads killed.
)
)
For non-Android applications, this is simply always called exactly
once, exactly before calling @link(TCastleApplication.OnInitialize). }
property OnInitializeJavaActivity: TNotifyEventList read FOnInitializeJavaActivity;
{ Callbacks called when Android Java activity is paused or resumed.
@italic(For now) not called on non-Android, but this may change ---
consider these events somewhat internal for the time being.
@groupBegin }
property OnPause: TNotifyEventList read FOnPause;
property OnResume: TNotifyEventList read FOnResume;
{ @groupEnd }
property OnWarning: TWarningEventList read FOnWarning;
{ Add this to OnWarning to output warnings to standard output (usually, console).
Eventually, on GUI Windows programs, it will make a dialog box.
This is handled by @link(WarningWrite) procedure. }
procedure WriteWarningOnConsole(Sender: TObject; const Category, Message: string);
{ Internal for Castle Game Engine.
Called from CastleWindow or CastleControl.
Don't call these methods yourself.
@groupBegin
@exclude }
procedure _GLContextOpen;
{ @exclude }
procedure _GLContextEarlyOpen;
{ @exclude }
procedure _GLContextClose;
{ @exclude }
procedure _Update;
{ @exclude }
procedure _InitializeJavaActivity;
{ @exclude }
procedure _Pause;
{ @exclude }
procedure _Resume;
{ @exclude }
procedure _Warning(const Category, Message: string);
{ @exclude
Indicates that operating on files (opening, saving, creating dirs) is safe.
Always @true when not using CastleWindow (e.g. in command-line utilities,
Lazarus CastleControl, or custom context situations like
https://gist.github.com/michaliskambi/ca0eb18aeb7e326e5dc79c3b5002bcc5 ).
In case of CastleWindow:
On Android, opening files before Android application started (on the Java side)
is not possible.
On iOS, some things (like ApplicationConfig path) may not be initialized so early. }
property _FileAccessSafe: boolean read FFileAccessSafe write FFileAccessSafe;
{ @groupEnd }
end;
function ApplicationProperties(
const CreateIfNotExisting: boolean = true): TCastleApplicationProperties;
implementation
uses SysUtils,
CastleUtils;
{ TGLContextEventList -------------------------------------------------------- }
procedure TGLContextEventList.ExecuteForward;
var
I: Integer;
begin
for I := 0 to Count - 1 do
Items[I]();
end;
procedure TGLContextEventList.ExecuteBackward;
var
I: Integer;
begin
for I := Count - 1 downto 0 do
Items[I]();
end;
{ TWarningEventList ---------------------------------------------------------- }
procedure TWarningEventList.ExecuteAll(Sender: TObject; const Category, Message: string);
var
I: Integer;
begin
for I := 0 to Count - 1 do
Items[I](Sender, Category, Message);
end;
{ TCastleApplicationProperties ----------------------------------------------- }
var
FApplicationProperties: TCastleApplicationProperties;
function ApplicationProperties(const CreateIfNotExisting: boolean): TCastleApplicationProperties;
begin
if (FApplicationProperties = nil) and CreateIfNotExisting then
FApplicationProperties := TCastleApplicationProperties.Create;
Result := FApplicationProperties;
end;
constructor TCastleApplicationProperties.Create;
begin
inherited;
FOnGLContextOpen := TGLContextEventList.Create;
FOnGLContextOpenObject := TNotifyEventList.Create;
FOnGLContextClose := TGLContextEventList.Create;
FOnGLContextCloseObject := TNotifyEventList.Create;
FOnUpdate := TNotifyEventList.Create;
FOnInitializeJavaActivity := TNotifyEventList.Create;
FOnPause := TNotifyEventList.Create;
FOnResume := TNotifyEventList.Create;
FOnWarning := TWarningEventList.Create;
FFileAccessSafe := true;
FTouchDevice :=
{$ifdef ANDROID} true {$else}
{$ifdef IOS} true {$else}
false {$endif} {$endif};
end;
destructor TCastleApplicationProperties.Destroy;
begin
FreeAndNil(FOnGLContextOpen);
FreeAndNil(FOnGLContextOpenObject);
FreeAndNil(FOnGLContextClose);
FreeAndNil(FOnGLContextCloseObject);
FreeAndNil(FOnUpdate);
FreeAndNil(FOnInitializeJavaActivity);
FreeAndNil(FOnPause);
FreeAndNil(FOnResume);
FreeAndNil(FOnWarning);
inherited;
end;
function TCastleApplicationProperties.GetApplicationName: string;
begin
Result := {$ifdef FPC} SysUtils {$else} CastleUtils {$endif} .ApplicationName;
end;
var
CastleApplicationNameValue: string;
function CastleGetApplicationName: string;
begin
Result := CastleApplicationNameValue;
end;
procedure TCastleApplicationProperties.SetApplicationName(const Value: string);
begin
OnGetApplicationName := @CastleGetApplicationName;
CastleApplicationNameValue := Value;
end;
procedure TCastleApplicationProperties._GLContextEarlyOpen;
begin
FIsGLContextOpen := true;
end;
procedure TCastleApplicationProperties._GLContextOpen;
begin
FIsGLContextOpen := true;
FOnGLContextOpen.ExecuteForward;
FOnGLContextOpenObject.ExecuteForward(Self);
end;
procedure TCastleApplicationProperties._GLContextClose;
begin
FOnGLContextCloseObject.ExecuteBackward(Self);
FOnGLContextClose.ExecuteBackward;
FIsGLContextOpen := false;
end;
procedure TCastleApplicationProperties._Update;
begin
FOnUpdate.ExecuteAll(Self);
end;
procedure TCastleApplicationProperties._InitializeJavaActivity;
begin
FOnInitializeJavaActivity.ExecuteAll(Self);
end;
procedure TCastleApplicationProperties._Pause;
begin
FOnPause.ExecuteAll(Self);
end;
procedure TCastleApplicationProperties._Resume;
begin
FOnResume.ExecuteAll(Self);
end;
procedure TCastleApplicationProperties._Warning(const Category, Message: string);
begin
FOnWarning.ExecuteAll(Self, Category, Message);
end;
procedure TCastleApplicationProperties.WriteWarningOnConsole(
Sender: TObject; const Category, Message: string);
begin
WarningWrite(ApplicationName + ': ' + Category + ' warning: ' + Message);
end;
initialization
finalization
FreeAndNil(FApplicationProperties);
end.
|