Debian Codegraph @

src/window/castleinternalwindowmodes.pas

raw
1 {
2 Copyright 2003-2023 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Helpers for making modal boxes (TGLMode, TGLModeFrozenScreen)
17 cooperating with the TCastleWindow windows.
18 They allow to easily save / restore TCastleWindow attributes.
19
20 This unit is a tool for creating functions like
21 @link(CastleMessages.MessageOK). To make nice "modal" box,
22 you want to temporarily replace TCastleWindow callbacks with your own,
23 call Application.ProcessMessage method in a loop until user gives an answer,
24 and restore everything. This way you can implement functions that
25 wait for some keypress, or wait until user inputs some
26 string, or wait until user picks something with mouse,
27 or wait for 10 seconds displaying some animation, etc.
28
29 This unit is internal, and in time may be removed.
30 We recommend using TCastleView(s) instances to organize your user-interface. }
31 unit CastleInternalWindowModes;
32
33 {$I castleconf.inc}
34
35 interface
36
37 uses SysUtils, Classes, CastleWindow, CastleGLUtils, CastleImages,
38 CastleUIControls, CastleKeysMouse, CastleGLImages, CastleControls;
39
40 type
41 { Enter / exit modal state on a TCastleWindow.
42 Saves/restores the state of TCastleWindow. }
43 TGLMode = class
44 strict private
45 OldWidth, OldHeight: integer;
46 FFakeMouseDown: boolean;
47 strict protected
48 type
49 TWindowState = class(TComponent)
50 strict private
51 Window: TCastleWindow;
52 OldMotion: TInputMotionEvent;
53 OldPress, OldRelease: TInputPressReleaseEvent;
54 OldOpenObject, OldCloseObject: TContainerObjectEvent;
55 OldBeforeRender, OldRender, OldCloseQuery, OldUpdate, OldTimer: TContainerEvent;
56 OldResize: TContainerEvent;
57 OldMenuClick: TMenuClickFunc;
58 OldCaption: string;
59 OldUserdata: Pointer;
60 OldAutoRedisplay: boolean;
61 OldMainMenu: TMenu;
62 { This is the saved value of OldMainMenu.Enabled.
63 So that you can change MainMenu.Enabled without changing MainMenu
64 and SetWindowState will restore this. }
65 OldMainMenuEnabled: boolean;
66 OldCursor: TMouseCursor;
67 OldSwapFullScreen_Key: TKey;
68 OldClose_KeyString: String;
69 OldControls: TInternalChildrenControls;
70 OldAutomaticTouchControl: boolean;
71 procedure WindowOpen(Container: TCastleContainer);
72 procedure WindowClose(Container: TCastleContainer);
73 public
74 { When adding new attributes to TCastleWindow that should be saved/restored,
75 you must remember to
76 1. expand this class with new fields
77 2. expand constructor, destructor and SetStandardState } { }
78
79 procedure SetStandardState(
80 NewRender, NewResize, NewCloseQuery: TContainerEvent);
81
82 { Constructor saves the TCastleWindow state, destructor applies this state
83 back to the window.
84 Every property that can change when TCastleWindow is open are saved.
85 This way you can save/restore TCastleWindow state, you can also copy
86 a state from one window into another.
87
88 Notes about TCastleWindow.MainMenu saving: only the reference
89 to MainMenu is stored. So:
90
91 @unorderedList(
92 @item(If you use TCastleWindow.MainMenu,
93 be careful when copying it to another window (no two windows
94 may own the same MainMenu instance at the same time;
95 also, you would have to make sure MainMenu instance will not be
96 freed two times).)
97
98 @item(Do not change the MainMenu contents
99 during TGLMode.Create/Free. Although you can change MainMenu
100 to something completely different. Just keep the assumption
101 that MainMenu stays <> nil.)
102
103 @item(As an exception to the previous point, you can freely
104 change MainMenu.Enabled, that is saved specially for this.)
105 )
106 }
107 constructor Create(AWindow: TCastleWindow); reintroduce;
108 destructor Destroy; override;
109 end;
110 var
111 OldState: TWindowState;
112 Window: TCastleWindow;
113 DisabledContextOpenClose: boolean;
114 public
115 { Constructor saves open TCastleWindow and OpenGL state.
116 Destructor will restore them.
117
118 Some gory details (that you will usually not care about...
119 the point is: everything works sensibly of the box) :
120
121 @unorderedList(
122 @item(We save/restore window state.)
123
124 @item(OpenGL context connected to this window is also made current
125 during constructor and destructor. Also, TCastleWindow.Invalidate
126 is called (since new callbacks, as well as original callbacks,
127 probably want to redraw window contents.))
128
129 @item(
130 All pressed keys and mouse butons are saved and faked to be released,
131 by calling TCastleWindow.EventRelease with original
132 callbacks.
133 This way, if user releases some keys/mouse inside modal box,
134 your original TCastleWindow callbacks will not miss this fact.
135 This way e.g. user scripts in VRML/X3D worlds that observe keys
136 work fine.
137
138 If FakeMouseDown then at destruction (after restoring original
139 callbacks) we will also notify your original callbacks that
140 user pressed these buttons (by sending TCastleWindow.EventMouseDown).
141 Note that FakeMouseDown feature turned out to be usually more
142 troublesome than usefull --- too often some unwanted MouseDown
143 event was caused by this mechanism.
144 That's because if original callbacks do something in MouseDown (like
145 e.g. activate some click) then you don't want to generate
146 fake MouseDown by TGLMode.Destroy.
147 So the default value of FakeMouseDown is @false.
148 But this means that original callbacks have to be careful
149 and @italic(never assume) that when some button is pressed
150 (because it's included in MousePressed, or has EventRelease generated for it)
151 then for sure there occurred some MouseDown for it.
152 )
153
154 @item(At destructor, we notify original callbacks about size changes
155 by sending TCastleWindow.EventResize. This way your original callbacks
156 know about size changes, and can set OpenGL projection etc.)
157
158 @item(
159 We call ZeroNextSecondsPassed at the end, when closing our mode,
160 see TFramesPerSecond.ZeroNextSecondsPassed for comments why this is needed.)
161
162 @item(This also performs important optimization to avoid closing /
163 reinitializing window TCastleWindow.Controls OpenGL resources,
164 see TCastleUserInterface.DisableContextOpenClose.)
165 ) }
166 constructor Create(AWindow: TCastleWindow);
167
168 { Save TCastleWindow state, and then change it to a standard
169 state. Destructor will restore saved state.
170
171 For most properties, we simply reset them to some sensible default
172 values. For some important properties, we take their value
173 explicitly by parameter.
174
175 Window properties resetted:
176
177 @unorderedList(
178 @item(All callbacks (OnXxx) are set to @nil.
179
180 Except the open/close callbacks
181 (OnOpen and OnClose, OnOpenObject and OnCloseObject).
182 Actually, OnOpenObject and OnCloseObject are changed for internal purposes,
183 but, assuming you use SetStandardState, the orignal ones will still happen.
184 Global CastleUIControls.OnGLContextOpen, CastleUIControls.OnGLContextClose
185 are also untouched.
186
187 @unorderedList(
188 @item(On standalone, we can expect that the window
189 (and OpenGL context) will stay open during the lifetime of a single
190 TGLMode. So it doesn't really matter what we do with callbacks
191 OnOpen / OnClose.)
192 @item(On mobiles (Android) this is not necessarily true.
193 Window may get closed at any time.
194 So be extra careful when implementing OnOpen / OnClose callbacks,
195 remember that they may happen when we're inside a mode (for example
196 inside a modal message in CastleMessages or a progress bar).)
197 )
198 )
199 @item(TCastleWindow.Caption and TCastleWindow.MainMenu are left as they were.)
200 @item(TCastleWindow.Cursor is reset to mcDefault.)
201 @item(TCastleWindow.UserData is reset to @nil.)
202 @item(TCastleWindow.AutoRedisplay is reset to @false.)
203 @item(TCastleWindow.MainMenu.Enabled will be reset to @false (only if MainMenu <> nil).)
204
205 @item(TCastleWindowDemo.SwapFullScreen_Key will be reset to keyNone.)
206 @item(TCastleWindowDemo.Close_KeyString will be reset to ''.)
207
208 @item(All TCastleWindow.Controls are temporarily removed.)
209 )
210
211 If you're looking for a suitable callback to pass as NewCloseQuery
212 (new TCastleWindow.OnCloseQuery), @@NoClose may be suitable:
213 it's an empty callback, thus using it disables the possibility
214 to close the window by window manager
215 (usually using "close" button in some window corner or Alt+F4). }
216 constructor CreateReset(AWindow: TCastleWindow;
217 NewRender, NewResize, NewCloseQuery: TContainerEvent);
218
219 destructor Destroy; override;
220
221 property FakeMouseDown: boolean
222 read FFakeMouseDown write FFakeMouseDown default false;
223 end;
224
225 { Enter / exit modal box on a TCastleWindow, additionally saving the screen
226 contents before entering modal box. This is nice if you want to wait
227 for some event (like pressing a key), keeping the same screen
228 displayed.
229
230 During this lifetime, we set special TCastleWindow.OnRender and TCastleWindow.OnResize
231 to draw the saved image in a simplest 2D OpenGL projection. }
232 TGLModeFrozenScreen = class(TGLMode)
233 private
234 BackgroundControls: TCastleUserInterface;
235 public
236 constructor Create(AWindow: TCastleWindow);
237 destructor Destroy; override;
238 end;
239
240 { Empty TCastleWindow callback, useful as TCastleWindow.OnCloseQuery
241 to disallow closing the window by user. }
242 procedure NoClose(Container: TCastleContainer);
243
244 implementation
245
246 uses CastleUtils, CastleColors, CastleVectors;
247
248 { TGLMode.TWindowState -------------------------------------------------------------- }
249
250 constructor TGLMode.TWindowState.Create(AWindow: TCastleWindow);
251 begin
252 inherited Create(nil);
253 Window := AWindow;
254
255 {$warnings off} // keep deprecated OnXxx working
256 OldOpenObject := Window.OnOpenObject;
257 OldCloseObject := Window.OnCloseObject;
258 { Note that we do not touch OnOpen and OnClose. Let them happen.
259 Our WindowOpen/Close will also call origina OnOpenObject/Close. }
260 OldMotion := Window.OnMotion;
261 OldPress := Window.OnPress;
262 OldRelease := Window.OnRelease;
263 OldBeforeRender := Window.OnBeforeRender;
264 OldRender := Window.OnRender;
265 OldCloseQuery := Window.OnCloseQuery;
266 OldResize := Window.OnResize;
267 OldUpdate := Window.OnUpdate;
268 {$warnings on}
269 OldMenuClick := Window.OnMenuClick;
270 oldCaption := Window.Caption;
271 oldUserdata := Window.Userdata;
272 oldAutoRedisplay := Window.AutoRedisplay;
273 oldMainMenu := Window.MainMenu;
274 if Window.MainMenu <> nil then
275 oldMainMenuEnabled := Window.MainMenu.Enabled;
276 OldCursor := Window.InternalCursor;
277 oldSwapFullScreen_Key := Window.SwapFullScreen_Key;
278 oldClose_KeyString := Window.Close_KeyString;
279
280 OldControls := TInternalChildrenControls.Create(nil);
281 OldControls.Assign(Window.Controls);
282 OldControls.BeginDisableContextOpenClose;
283 end;
284
285 destructor TGLMode.TWindowState.Destroy;
286 begin
287 {$warnings off} // keep deprecated OnXxx working
288 Window.OnOpenObject := OldOpenObject;
289 Window.OnCloseObject := OldCloseObject;
290 Window.OnMotion := OldMotion;
291 Window.OnPress := OldPress;
292 Window.OnRelease := OldRelease;
293 Window.OnBeforeRender := OldBeforeRender;
294 Window.OnRender := OldRender;
295 Window.OnCloseQuery := OldCloseQuery;
296 Window.OnResize := OldResize;
297 Window.OnUpdate := OldUpdate;
298 {$warnings on}
299 Window.OnMenuClick := OldMenuClick;
300 Window.Caption := oldCaption;
301 Window.Userdata := oldUserdata;
302 Window.AutoRedisplay := oldAutoRedisplay;
303 Window.MainMenu := oldMainMenu;
304 if Window.MainMenu <> nil then
305 Window.MainMenu.Enabled := OldMainMenuEnabled;
306 Window.InternalCursor := OldCursor;
307 Window.SwapFullScreen_Key := oldSwapFullScreen_Key;
308 Window.Close_KeyString := oldClose_KeyString;
309
310 if OldControls <> nil then
311 begin
312 Window.Controls.Assign(OldControls);
313 OldControls.EndDisableContextOpenClose;
314 FreeAndNil(OldControls);
315 end;
316
317 inherited;
318 end;
319
320 procedure TGLMode.TWindowState.WindowOpen(Container: TCastleContainer);
321 var
322 I: Integer;
323 C: TCastleUserInterface;
324 begin
325 if Assigned(OldOpenObject) then
326 OldOpenObject(Container);
327 { Make sure to call GLContextOpen on OldControls,
328 otherwise they would not initialize OpenGL resources even though OpenGL
329 context was open. This goes around the C.DisableContextOpenClose value,
330 so BeginDisableContextOpenClose / EndDisableContextOpenClose also don't matter. }
331 for I := 0 to OldControls.Count - 1 do
332 begin
333 C := OldControls[I];
334 C.GLContextOpen;
335 end;
336 end;
337
338 procedure TGLMode.TWindowState.WindowClose(Container: TCastleContainer);
339 var
340 I: Integer;
341 C: TCastleUserInterface;
342 begin
343 if Assigned(OldCloseObject) then
344 OldCloseObject(Container);
345 { Make sure to call GLContextClose on OldControls,
346 otherwise they would not release OpenGL resources even though OpenGL
347 context was closed. This goes around the C.DisableContextOpenClose value,
348 so BeginDisableContextOpenClose / EndDisableContextOpenClose also don't matter. }
349 for I := 0 to OldControls.Count - 1 do
350 begin
351 C := OldControls[I];
352 C.GLContextClose;
353 end;
354 end;
355
356 procedure TGLMode.TWindowState.SetStandardState(
357 NewRender, NewResize, NewCloseQuery: TContainerEvent);
358 begin
359 {$warnings off} // keep deprecated OnXxx working
360 Window.OnOpenObject := {$ifdef FPC}@{$endif}WindowOpen;
361 Window.OnCloseObject := {$ifdef FPC}@{$endif}WindowClose;
362 Window.OnMotion := nil;
363 Window.OnPress := nil;
364 Window.OnRelease := nil;
365 Window.OnBeforeRender := nil;
366 Window.OnRender := nil;
367 Window.OnCloseQuery := nil;
368 Window.OnUpdate := nil;
369 Window.OnResize := nil;
370 Window.OnRender := NewRender;
371 Window.OnResize := NewResize;
372 Window.OnCloseQuery := NewCloseQuery;
373 {$warnings on}
374 Window.OnMenuClick := nil;
375 {Window.Caption := leave current value}
376 Window.Userdata := nil;
377 Window.AutoRedisplay := false;
378 if Window.MainMenu <> nil then
379 Window.MainMenu.Enabled := false;
380 {Window.MainMenu := leave current value}
381 Window.InternalCursor := mcDefault;
382 Window.SwapFullScreen_Key := keyNone;
383 Window.Close_KeyString := '';
384 Window.Controls.Clear;
385 end;
386
387 { TGLMode -------------------------------------------------------------------- }
388
389 constructor TGLMode.Create(AWindow: TCastleWindow);
390
391 procedure SimulateReleaseAll;
392 var
393 Button: TCastleMouseButton;
394 Key: TKey;
395 C: char;
396 ModifiersDown: TModifierKeys;
397 begin
398 { Simulate (to original callbacks) that user releases
399 all mouse buttons and key presses now. }
400 for Button := Low(Button) to High(Button) do
401 if Button in Window.MousePressed then
402 Window.Container.EventRelease(InputMouseButton(Window.MousePosition, Button, 0, []));
403 ModifiersDown := CastleKeysMouse.ModifiersDown(Window.Container.Pressed);
404 for Key := Low(Key) to High(Key) do
405 if Window.Pressed[Key] then
406 Window.Container.EventRelease(InputKey(Window.MousePosition, Key, '', ModifiersDown));
407 for C := Low(C) to High(C) do
408 if Window.Pressed.Characters[C] then
409 Window.Container.EventRelease(InputKey(Window.MousePosition, keyNone, C, ModifiersDown));
410 end;
411
412 begin
413 inherited Create;
414
415 Window := AWindow;
416
417 FFakeMouseDown := false;
418
419 Check(not Window.Closed, 'TGLMode.Create cannot be called on a closed CastleWindow.');
420
421 OldState := TWindowState.Create(Window);
422 OldWidth := Window.Width;
423 OldHeight := Window.Height;
424
425 Window.MakeCurrent;
426
427 SimulateReleaseAll;
428
429 Window.Invalidate;
430 end;
431
432 constructor TGLMode.CreateReset(AWindow: TCastleWindow;
433 NewRender, NewResize, NewCloseQuery: TContainerEvent);
434 begin
435 Create(AWindow);
436 OldState.SetStandardState(NewRender, NewResize, NewCloseQuery);
437 end;
438
439 destructor TGLMode.Destroy;
440 var
441 btn: TCastleMouseButton;
442 begin
443 FreeAndNil(OldState);
444
445 { Although it's forbidden to use TGLMode on Closed TCastleWindow,
446 in destructor we must take care of every possible situation
447 (because this may be called in finally ... end things when
448 everything should be possible). }
449 if not Window.Closed then
450 begin
451 { fake resize event, and fake mouse presss events.
452 This way original callbacks are notified about current container state. }
453 Window.MakeCurrent;
454 if (OldWidth <> Window.Width) or
455 (OldHeight <> Window.Height) then
456 Window.Container.EventResize;
457 if FakeMouseDown then
458 for Btn in Window.MousePressed do
459 Window.Container.EventPress(InputMouseButton(Window.MousePosition, Btn, 0, []));
460
461 Window.Invalidate;
462
463 Window.Fps.ZeroNextSecondsPassed;
464 end;
465
466 inherited;
467 end;
468
469 { TGLModeFrozenScreen ------------------------------------------------------ }
470
471 constructor TGLModeFrozenScreen.Create(AWindow: TCastleWindow);
472
473 { Fill BackgroundControls with UI to represent frozen screen.
474 This is quite similar to what TStateDialog.Start does. }
475 procedure FillBackgroundControls;
476 var
477 BackgroundColor: TCastleColor;
478 BackgroundImage: TCastleImageControl;
479 BackgroundRect: TCastleRectangleControl;
480 begin
481 if Theme.InternalMessageFallbackLook then
482 BackgroundColor := Vector4(Theme.BackgroundOpaqueColor, 1)
483 else
484 BackgroundColor := Theme.BackgroundColor;
485
486 if BackgroundColor[3] <> 1 then
487 begin
488 BackgroundImage := TCastleImageControl.Create(BackgroundControls);
489 BackgroundImage.Stretch := true;
490 BackgroundImage.FullSize := true;
491 { save screen, before changing state. }
492 BackgroundImage.Image := Window.SaveScreen;
493 BackgroundControls.InsertFront(BackgroundImage);
494 end;
495
496 BackgroundRect := TCastleRectangleControl.Create(BackgroundControls);
497 BackgroundRect.Color := BackgroundColor;
498 BackgroundRect.FullSize := true;
499 {$warnings off} // using deprecated, but this whole TGLMode is hacky, we should rather use TCastleView and TCastleView.InterceptInput
500 BackgroundRect.InterceptInput := true;
501 {$warnings on}
502 BackgroundControls.InsertFront(BackgroundRect);
503 end;
504
505 begin
506 inherited Create(AWindow);
507
508 BackgroundControls := TCastleUserInterface.Create(nil);
509 BackgroundControls.FullSize := true;
510 FillBackgroundControls;
511
512 OldState.SetStandardState(nil, nil, @NoClose);
513
514 AWindow.Controls.InsertFront(BackgroundControls);
515 end;
516
517 destructor TGLModeFrozenScreen.Destroy;
518 begin
519 inherited;
520 { it's a little safer to call this after inherited }
521 FreeAndNil(BackgroundControls);
522 end;
523
524 { routines ------------------------------------------------------------------- }
525
526 procedure NoClose(Container: TCastleContainer);
527 begin
528 end;
529
530 end.