| 1 |
{%MainUnit castleuicontrols.pas} |
| 2 |
{ |
| 3 |
Copyright 2010-2021 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 |
{ List of UI controls, with a parent control and container. |
| 22 |
Ordered from back to front. |
| 23 |
Used for @link(TCastleContainer.Controls). |
| 24 |
|
| 25 |
User code should not create instances of this class. |
| 26 |
The only way to have an instance of it, is to get a container, |
| 27 |
by creating a @link(TCastleWindow) or @link(TCastleControl). |
| 28 |
Then you can access @code(MyWindow.Container.Controls). |
| 29 |
|
| 30 |
User code should rarely need to use methods and properties of this class. |
| 31 |
That's because most TCastleUserInterface controls should be added to TCastleView, |
| 32 |
so only TCastleView instances should be present at the top-level of the UI hierarchy |
| 33 |
in TInternalChildrenControls. And you should manage TCastleView instances using |
| 34 |
@link(TCastleContainer.View), @link(TCastleContainer.PushView) etc. that will make |
| 35 |
it added / removed from the TInternalChildrenControls automatically. } |
| 36 |
TInternalChildrenControls = class |
| 37 |
strict private |
| 38 |
FParent: TCastleUserInterface; |
| 39 |
FContainer: TCastleContainer; |
| 40 |
|
| 41 |
procedure RegisterContainer(const C: TCastleUserInterface; const AContainer: TCastleContainer); |
| 42 |
procedure UnregisterContainer(const C: TCastleUserInterface; const AContainer: TCastleContainer); |
| 43 |
procedure SetContainer(const AContainer: TCastleContainer); |
| 44 |
|
| 45 |
type |
| 46 |
TMyObjectList = class(TCastleObjectList) |
| 47 |
Parent: TInternalChildrenControls; |
| 48 |
{ Pass notifications to Parent. } |
| 49 |
procedure Notify(Ptr: Pointer; Action: TListNotification); override; |
| 50 |
end; |
| 51 |
TCaptureFreeNotifications = class(TComponent) |
| 52 |
protected |
| 53 |
Parent: TInternalChildrenControls; |
| 54 |
{ Pass notifications to Parent. } |
| 55 |
procedure Notification(AComponent: TComponent; Operation: TOperation); override; |
| 56 |
end; |
| 57 |
var |
| 58 |
FList: TMyObjectList; |
| 59 |
FCaptureFreeNotifications: TCaptureFreeNotifications; |
| 60 |
|
| 61 |
function GetItem(const I: Integer): TCastleUserInterface; |
| 62 |
procedure SetItem(const I: Integer; const Item: TCastleUserInterface); |
| 63 |
{ React to add/remove notifications. } |
| 64 |
procedure Notify(Ptr: Pointer; Action: TListNotification); |
| 65 |
private |
| 66 |
property Container: TCastleContainer read FContainer write SetContainer; |
| 67 |
public |
| 68 |
constructor Create(AParent: TCastleUserInterface); |
| 69 |
destructor Destroy; override; |
| 70 |
|
| 71 |
property Items[const I: Integer]: TCastleUserInterface read GetItem write SetItem; default; |
| 72 |
function Count: Integer; |
| 73 |
procedure Assign(const Source: TInternalChildrenControls); |
| 74 |
{ Remove the Item from this list. |
| 75 |
Note that the given Item should always exist only once on a list |
| 76 |
(it is not allowed to add it multiple times), so there's no @code(RemoveAll) |
| 77 |
method. } |
| 78 |
procedure Remove(const Item: TCastleUserInterface); |
| 79 |
procedure Clear; |
| 80 |
procedure Add(const Item: TCastleUserInterface); deprecated 'use InsertFront or InsertBack'; |
| 81 |
procedure Insert(Index: Integer; const Item: TCastleUserInterface); |
| 82 |
function IndexOf(const Item: TCastleUserInterface): Integer; |
| 83 |
|
| 84 |
{ Make sure that NewItem is the only instance of given ReplaceClass |
| 85 |
on the list, replacing old item if necesssary. |
| 86 |
See TCastleObjectList.MakeSingle for precise description. } |
| 87 |
function MakeSingle(ReplaceClass: TCastleUserInterfaceClass; NewItem: TCastleUserInterface; |
| 88 |
AddFront: boolean = true): TCastleUserInterface; |
| 89 |
deprecated 'this is a complicated method without clear use-case now; do not use'; |
| 90 |
|
| 91 |
{ Add at the end of the list. } |
| 92 |
procedure InsertFront(const NewItem: TCastleUserInterface); overload; |
| 93 |
procedure InsertFrontIfNotExists(const NewItem: TCastleUserInterface); |
| 94 |
procedure InsertFront(const NewItems: TCastleUserInterfaceList); overload; |
| 95 |
|
| 96 |
{ Add at the beginning of the list. } |
| 97 |
procedure InsertBack(const NewItem: TCastleUserInterface); overload; |
| 98 |
procedure InsertBackIfNotExists(const NewItem: TCastleUserInterface); |
| 99 |
procedure InsertBack(const NewItems: TCastleUserInterfaceList); overload; |
| 100 |
|
| 101 |
procedure InsertIfNotExists(const Index: Integer; const NewItem: TCastleUserInterface); deprecated 'use InsertFrontIfNotExists or InsertBackIfNotExists'; |
| 102 |
procedure AddIfNotExists(const NewItem: TCastleUserInterface); deprecated 'use InsertFrontIfNotExists or InsertBackIfNotExists'; |
| 103 |
|
| 104 |
{ BeginDisableContextOpenClose disables sending |
| 105 |
TCastleUserInterface.GLContextOpen and TCastleUserInterface.GLContextClose to all the controls |
| 106 |
on the list. EndDisableContextOpenClose ends this. |
| 107 |
They work by increasing / decreasing the TCastleUserInterface.DisableContextOpenClose |
| 108 |
for all the items on the list. |
| 109 |
|
| 110 |
@groupBegin } |
| 111 |
procedure BeginDisableContextOpenClose; |
| 112 |
procedure EndDisableContextOpenClose; |
| 113 |
{ @groupEnd } |
| 114 |
end; |
| 115 |
|
| 116 |
{$endif read_interface} |
| 117 |
|
| 118 |
{$ifdef read_implementation} |
| 119 |
|
| 120 |
{ TInternalChildrenControls ------------------------------------------------------------- } |
| 121 |
|
| 122 |
constructor TInternalChildrenControls.Create(AParent: TCastleUserInterface); |
| 123 |
begin |
| 124 |
inherited Create; |
| 125 |
FParent := AParent; |
| 126 |
FList := TMyObjectList.Create; |
| 127 |
FList.Parent := Self; |
| 128 |
FCaptureFreeNotifications := TCaptureFreeNotifications.Create(nil); |
| 129 |
FCaptureFreeNotifications.Parent := Self; |
| 130 |
end; |
| 131 |
|
| 132 |
destructor TInternalChildrenControls.Destroy; |
| 133 |
begin |
| 134 |
FreeAndNil(FList); |
| 135 |
FreeAndNil(FCaptureFreeNotifications); |
| 136 |
inherited; |
| 137 |
end; |
| 138 |
|
| 139 |
function TInternalChildrenControls.GetItem(const I: Integer): TCastleUserInterface; |
| 140 |
begin |
| 141 |
Result := TCastleUserInterface(FList.Items[I]); |
| 142 |
end; |
| 143 |
|
| 144 |
procedure TInternalChildrenControls.SetItem(const I: Integer; const Item: TCastleUserInterface); |
| 145 |
begin |
| 146 |
FList.Items[I] := Item; |
| 147 |
end; |
| 148 |
|
| 149 |
function TInternalChildrenControls.Count: Integer; |
| 150 |
begin |
| 151 |
Result := FList.Count; |
| 152 |
end; |
| 153 |
|
| 154 |
procedure TInternalChildrenControls.BeginDisableContextOpenClose; |
| 155 |
var |
| 156 |
I: Integer; |
| 157 |
begin |
| 158 |
for I := 0 to FList.Count - 1 do |
| 159 |
with TCastleUserInterface(FList.Items[I]) do |
| 160 |
DisableContextOpenClose := DisableContextOpenClose + 1; |
| 161 |
end; |
| 162 |
|
| 163 |
procedure TInternalChildrenControls.EndDisableContextOpenClose; |
| 164 |
var |
| 165 |
I: Integer; |
| 166 |
begin |
| 167 |
for I := 0 to FList.Count - 1 do |
| 168 |
with TCastleUserInterface(FList.Items[I]) do |
| 169 |
DisableContextOpenClose := DisableContextOpenClose - 1; |
| 170 |
end; |
| 171 |
|
| 172 |
procedure TInternalChildrenControls.InsertFront(const NewItem: TCastleUserInterface); |
| 173 |
begin |
| 174 |
Insert(Count, NewItem); |
| 175 |
end; |
| 176 |
|
| 177 |
procedure TInternalChildrenControls.InsertFrontIfNotExists(const NewItem: TCastleUserInterface); |
| 178 |
begin |
| 179 |
if FList.IndexOf(NewItem) = -1 then |
| 180 |
InsertFront(NewItem); |
| 181 |
end; |
| 182 |
|
| 183 |
procedure TInternalChildrenControls.InsertFront(const NewItems: TCastleUserInterfaceList); |
| 184 |
var |
| 185 |
I: Integer; |
| 186 |
begin |
| 187 |
for I := 0 to NewItems.Count - 1 do |
| 188 |
InsertFront(NewItems[I]); |
| 189 |
end; |
| 190 |
|
| 191 |
procedure TInternalChildrenControls.InsertBack(const NewItem: TCastleUserInterface); |
| 192 |
begin |
| 193 |
FList.Insert(0, NewItem); |
| 194 |
end; |
| 195 |
|
| 196 |
procedure TInternalChildrenControls.InsertBackIfNotExists(const NewItem: TCastleUserInterface); |
| 197 |
begin |
| 198 |
if FList.IndexOf(NewItem) = -1 then |
| 199 |
InsertBack(NewItem); |
| 200 |
end; |
| 201 |
|
| 202 |
procedure TInternalChildrenControls.Add(const Item: TCastleUserInterface); |
| 203 |
begin |
| 204 |
InsertFront(Item); |
| 205 |
end; |
| 206 |
|
| 207 |
procedure TInternalChildrenControls.Insert(Index: Integer; const Item: TCastleUserInterface); |
| 208 |
var |
| 209 |
I: Integer; |
| 210 |
begin |
| 211 |
{ TODO: code duplicated with TCastleUserInterfaceList.InsertWithZOrder } |
| 212 |
Index := Clamped(Index, 0, Count); |
| 213 |
if Item.KeepInFront or |
| 214 |
(Count = 0) or |
| 215 |
(Index = 0) or |
| 216 |
(not Items[Index - 1].KeepInFront) then |
| 217 |
FList.Insert(Index, Item) else |
| 218 |
begin |
| 219 |
for I := Index - 1 downto 0 do |
| 220 |
if not Items[I].KeepInFront then |
| 221 |
begin |
| 222 |
FList.Insert(I + 1, Item); |
| 223 |
Exit; |
| 224 |
end; |
| 225 |
{ everything has KeepInFront = true } |
| 226 |
FList.Insert(0, Item); |
| 227 |
end; |
| 228 |
end; |
| 229 |
|
| 230 |
procedure TInternalChildrenControls.InsertIfNotExists(const Index: Integer; const NewItem: TCastleUserInterface); |
| 231 |
begin |
| 232 |
Insert(Index, NewItem); |
| 233 |
end; |
| 234 |
|
| 235 |
procedure TInternalChildrenControls.AddIfNotExists(const NewItem: TCastleUserInterface); |
| 236 |
begin |
| 237 |
Insert(Count, NewItem); |
| 238 |
end; |
| 239 |
|
| 240 |
function TInternalChildrenControls.IndexOf(const Item: TCastleUserInterface): Integer; |
| 241 |
begin |
| 242 |
Result := FList.IndexOf(Item); |
| 243 |
end; |
| 244 |
|
| 245 |
procedure TInternalChildrenControls.InsertBack(const NewItems: TCastleUserInterfaceList); |
| 246 |
var |
| 247 |
I: Integer; |
| 248 |
begin |
| 249 |
for I := NewItems.Count - 1 downto 0 do |
| 250 |
InsertBack(NewItems[I]); |
| 251 |
end; |
| 252 |
|
| 253 |
procedure TInternalChildrenControls.Notify(Ptr: Pointer; Action: TListNotification); |
| 254 |
var |
| 255 |
C: TCastleUserInterface; |
| 256 |
begin |
| 257 |
{ TODO: while this updating works cool, |
| 258 |
if the Parent or Container is destroyed |
| 259 |
before children --- the children will keep invalid reference. } |
| 260 |
|
| 261 |
C := TCastleUserInterface(Ptr); |
| 262 |
case Action of |
| 263 |
lnAdded: |
| 264 |
begin |
| 265 |
if ((C.Container <> nil) or (C.Parent <> nil)) and |
| 266 |
((Container <> nil) or (FParent <> nil)) then |
| 267 |
WritelnWarning('UI', 'Inserting to the UI list (InsertFront, InsertBack) an item that is already a part of other UI list: ' + C.DebugName + '. The result is undefined, you cannot insert the same TCastleUserInterface instance multiple times.'); |
| 268 |
C.FreeNotification(FCaptureFreeNotifications); |
| 269 |
if Container <> nil then RegisterContainer(C, FContainer); |
| 270 |
C.FParent := FParent; |
| 271 |
end; |
| 272 |
lnExtracted, lnDeleted: |
| 273 |
begin |
| 274 |
C.FParent := nil; |
| 275 |
if Container <> nil then UnregisterContainer(C, FContainer); |
| 276 |
C.RemoveFreeNotification(FCaptureFreeNotifications); |
| 277 |
end; |
| 278 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 279 |
else raise EInternalError.Create('TInternalChildrenControls.Notify action?'); |
| 280 |
{$endif} |
| 281 |
end; |
| 282 |
|
| 283 |
{ This notification may get called during FreeAndNil(FControls) |
| 284 |
in TCastleContainer.Destroy. Then FControls is already nil, and we're |
| 285 |
getting remove notification for all items (as FreeAndNil first sets |
| 286 |
object to nil). Testcase: lets_take_a_walk exit. } |
| 287 |
if (FContainer <> nil) and |
| 288 |
(FContainer.FControls <> nil) then |
| 289 |
begin |
| 290 |
if (FParent <> nil) and not (csDestroying in FParent.ComponentState) then |
| 291 |
FParent.VisibleChange([chChildren]) |
| 292 |
else |
| 293 |
begin |
| 294 |
{ if this is top-most control inside Container, |
| 295 |
we need to call ControlsVisibleChange directly. } |
| 296 |
Container.ControlsVisibleChange(C, [chChildren], false); |
| 297 |
end; |
| 298 |
end; |
| 299 |
end; |
| 300 |
|
| 301 |
procedure TInternalChildrenControls.TCaptureFreeNotifications.Notification(AComponent: TComponent; Operation: TOperation); |
| 302 |
begin |
| 303 |
inherited; |
| 304 |
|
| 305 |
{ We have to remove a reference to the object from list. |
| 306 |
This is crucial, various methods assume that all objects on |
| 307 |
the list are always valid objects (no invalid references, |
| 308 |
even for a short time). } |
| 309 |
|
| 310 |
if (Operation = opRemove) and (AComponent is TCastleUserInterface) then |
| 311 |
Parent.FList.Remove(AComponent); |
| 312 |
end; |
| 313 |
|
| 314 |
procedure TInternalChildrenControls.Assign(const Source: TInternalChildrenControls); |
| 315 |
begin |
| 316 |
FList.Assign(Source.FList); |
| 317 |
end; |
| 318 |
|
| 319 |
procedure TInternalChildrenControls.Remove(const Item: TCastleUserInterface); |
| 320 |
begin |
| 321 |
FList.Remove(Item); |
| 322 |
end; |
| 323 |
|
| 324 |
procedure TInternalChildrenControls.Clear; |
| 325 |
begin |
| 326 |
FList.Clear; |
| 327 |
end; |
| 328 |
|
| 329 |
function TInternalChildrenControls.MakeSingle(ReplaceClass: TCastleUserInterfaceClass; NewItem: TCastleUserInterface; |
| 330 |
AddFront: boolean): TCastleUserInterface; |
| 331 |
begin |
| 332 |
{$warnings off} // using deprecated in deprecated |
| 333 |
Result := FList.MakeSingle(ReplaceClass, NewItem, AddFront) as TCastleUserInterface; |
| 334 |
{$warnings on} |
| 335 |
end; |
| 336 |
|
| 337 |
procedure TInternalChildrenControls.RegisterContainer( |
| 338 |
const C: TCastleUserInterface; const AContainer: TCastleContainer); |
| 339 |
begin |
| 340 |
{ Register AContainer to be notified of control destruction. } |
| 341 |
C.FreeNotification(AContainer); |
| 342 |
|
| 343 |
C.InternalSetContainer(AContainer); |
| 344 |
C.CheckUIScaleChanged; |
| 345 |
|
| 346 |
{ Make sure Resize is called at the nearest opportunity. |
| 347 |
For historic reasons, some controls may depend that Resize happens |
| 348 |
immediately after adding control to the container, |
| 349 |
they may even initialize GL resources there. } |
| 350 |
C.FLastSeenContainerWidth := 0; |
| 351 |
C.FLastSeenContainerHeight := 0; |
| 352 |
|
| 353 |
if AContainer.GLInitialized then |
| 354 |
begin |
| 355 |
if C.DisableContextOpenClose = 0 then |
| 356 |
C.GLContextOpen; |
| 357 |
AContainer.Invalidate; |
| 358 |
{ Call initial Resize for control. |
| 359 |
If window OpenGL context is not yet initialized, defer it to later |
| 360 |
(we will call CheckResize before BeforeRender). } |
| 361 |
C.CheckResize; |
| 362 |
end; |
| 363 |
end; |
| 364 |
|
| 365 |
procedure TInternalChildrenControls.UnregisterContainer( |
| 366 |
const C: TCastleUserInterface; const AContainer: TCastleContainer); |
| 367 |
begin |
| 368 |
if AContainer.GLInitialized and |
| 369 |
(C.DisableContextOpenClose = 0) then |
| 370 |
C.GLContextClose; |
| 371 |
|
| 372 |
C.RemoveFreeNotification(AContainer); |
| 373 |
AContainer.DetachNotification(C); |
| 374 |
|
| 375 |
C.InternalSetContainer(nil); |
| 376 |
end; |
| 377 |
|
| 378 |
procedure TInternalChildrenControls.SetContainer(const AContainer: TCastleContainer); |
| 379 |
var |
| 380 |
I: Integer; |
| 381 |
begin |
| 382 |
if FContainer <> AContainer then |
| 383 |
begin |
| 384 |
if FContainer <> nil then |
| 385 |
for I := 0 to Count - 1 do |
| 386 |
UnregisterContainer(Items[I], FContainer); |
| 387 |
FContainer := AContainer; |
| 388 |
if FContainer <> nil then |
| 389 |
for I := 0 to Count - 1 do |
| 390 |
RegisterContainer(Items[I], FContainer); |
| 391 |
end; |
| 392 |
end; |
| 393 |
|
| 394 |
{ TInternalChildrenControls.TMyObjectList ----------------------------------------------- } |
| 395 |
|
| 396 |
procedure TInternalChildrenControls.TMyObjectList.Notify(Ptr: Pointer; Action: TListNotification); |
| 397 |
begin |
| 398 |
Parent.Notify(Ptr, Action); |
| 399 |
end; |
| 400 |
|
| 401 |
{$endif read_implementation} |