Debian Codegraph @

src/delphi/fmx.castlecontrol.pas

raw
1 {
2 Copyright 2022-2024 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 { Control rendering "Castle Game Engine" on Delphi FMX (FireMonkey) form. }
17 unit Fmx.CastleControl;
18
19 {$I castleconf.inc}
20
21 { How to implement calling Update continuously?
22 Similar choice as castlewindow_form.inc discusses.
23
24 - USE_TIMER is preferred.
25 It "just works" from user point of view.
26 We use this on all platforms (Windows)...
27
28 - ...except Linux, when with FMXLinux we failed to make it work.
29 It means user code must execute loop in a special way.
30 In the main program code, instead of
31
32 Application.Run;
33
34 do
35
36 TCastleControl.ApplicationRun;
37
38 And if anywhere you do
39
40 Application.ProcessMessages;
41
42 enhance it with
43
44 Application.ProcessMessages;
45 TCastleControl.ProcessTasks; // additional CGE processing
46 }
47 {$if not defined(LINUX)}
48 {$define USE_TIMER}
49 {$endif}
50
51 interface
52
53 uses // standard units
54 SysUtils, Classes,
55 // fmx
56 {$ifdef MSWINDOWS} FMX.Presentation.Win, {$endif}
57 FMX.Controls, FMX.Controls.Presentation, FMX.Types, UITypes,
58 // cge
59 CastleGLVersion, CastleGLUtils, CastleVectors, CastleKeysMouse,
60 CastleInternalContextBase, CastleInternalContainer, CastleInternalFmxUtils;
61
62 type
63 { Control rendering "Castle Game Engine" on FMX form. }
64 TCastleControl = class(TPresentedControl)
65 strict private
66 type
67 { Non-abstract implementation of TCastleContainer that cooperates with
68 TCastleControl. }
69 TContainer = class(TCastleContainerEasy)
70 private
71 Parent: TCastleControl;
72 {$ifdef USE_TIMER}
73 class var
74 UpdatingTimer: TTimer;
75 {$endif}
76 class procedure UpdatingTimerEvent(Sender: TObject);
77 protected
78 function GetMousePosition: TVector2; override;
79 procedure SetMousePosition(const Value: TVector2); override;
80 procedure AdjustContext(const PlatformContext: TGLContext); override;
81 class procedure UpdatingEnable; override;
82 class procedure UpdatingDisable; override;
83 public
84 constructor Create(AParent: TCastleControl); reintroduce;
85 procedure Invalidate; override;
86 function PixelsWidth: Integer; override;
87 function PixelsHeight: Integer; override;
88 procedure SetInternalCursor(const Value: TMouseCursor); override;
89 end;
90
91 var
92 FContainer: TContainer;
93 FMousePosition: TVector2;
94 FGLUtility: TFmxOpenGLUtility;
95
96 function GetCurrentShift: TShiftState;
97 procedure SetCurrentShift(const Value: TShiftState);
98
99 { Current knowledge about shift state, based on Container.Pressed.
100
101 Call this whenever you have new new shift state.
102
103 Sometimes, releasing shift / alt / ctrl keys will not be reported
104 properly to KeyDown / KeyUp. Example: opening a menu
105 through Alt+F for "_File" will make keydown for Alt,
106 but not keyup for it, and DoExit will not be called,
107 so ReleaseAllKeysAndMouse will not be called.
108
109 To counteract this, set this whenever Shift state is known,
110 to update Container.Pressed when needed. }
111 property CurrentShift: TShiftState
112 read GetCurrentShift write SetCurrentShift;
113
114 function MousePosToCastle(const X, Y: Single): TVector2;
115 private
116 procedure CreateHandle;
117 procedure DestroyHandle;
118 protected
119 procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
120 X, Y: Single); override;
121 procedure MouseMove(Shift: TShiftState; NewX, NewY: Single); override;
122 procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
123 X, Y: Single); override;
124 procedure MouseWheel(Shift: TShiftState; WheelDelta: Integer; var Handled: Boolean); override;
125 procedure KeyDown(var Key: Word; var KeyChar: WideChar; Shift: TShiftState); override;
126 procedure KeyUp(var Key: Word; var KeyChar: WideChar; Shift: TShiftState); override;
127 function DefinePresentationName: String; override;
128 procedure Resize; override;
129 // Not needed in the end // procedure DoRootChanged; override;
130 procedure DoRootChanging(const NewRoot: IRoot); override;
131 procedure SetVisible(const AValue: Boolean); override;
132 public
133 constructor Create(AOwner: TComponent); override;
134 destructor Destroy; override;
135 procedure Paint; override;
136
137 { If Handle not allocated yet, allocate it now.
138 This makes sure we have OpenGL context created.
139 Our OpenBackend must guarantee it, we want to initialize GLVersion
140 afterwards etc. }
141 procedure InternalHandleNeeded;
142
143 { This control must always have "native style", which means
144 it has ControlType = Platform. See FMX docs about native controls:
145 https://docwiki.embarcadero.com/RADStudio/Sydney/en/FireMonkey_Native_Windows_Controls
146 Native controls are always on top of non-native controls. }
147 property ControlType default TControlType.Platform;
148
149 { On some platforms (Linux now) if you call Application.ProcessMessages,
150 make sure to also call this method. E.g.
151
152 @longCode(#
153 while SomeCondition do
154 begin
155 Application.ProcessMessages;
156 TCastleControl.ProcessTasks;
157 end;
158 #)
159
160 This does nothing on some other platforms (Windows)
161 when it is not necessary and Application.ProcessMessages does all CGE job. }
162 class procedure ProcessTasks;
163
164 { On some platforms (Linux now) you cannot just call Application.Run,
165 as we need to call CGE processing regularly. So instead call
166 this method.
167 This ensures that engine updates / renders regularly.
168
169 On some other platforms (Windows)
170 this just calls Application.Run, which already makes
171 engine processing correct. }
172 class procedure ApplicationRun;
173 published
174 { Access Castle Game Engine container properties and events,
175 not specific for FMX. }
176 property Container: TContainer read FContainer;
177
178 property Align;
179 property Anchors;
180 property OnClick;
181 property OnDblClick;
182 property Height;
183 property Width;
184 property Size;
185 property Position;
186 property Margins;
187 property TabStop default true;
188 property TabOrder;
189 property CanFocus default True;
190 end;
191
192 procedure Register;
193
194 implementation
195
196 uses FMX.Presentation.Factory, Types, FMX.Graphics, FMX.Forms,
197 CastleRenderOptions, CastleApplicationProperties, CastleRenderContext,
198 CastleRectangles, CastleUtils, CastleUIControls, CastleInternalDelphiUtils,
199 CastleLog;
200
201 procedure Register;
202 begin
203 RegisterComponents('Castle', [
204 TCastleControl
205 ]);
206 end;
207
208 {$ifdef MSWINDOWS}
209
210 { TWinNativeGLControl -------------------------------------------------------- }
211
212 type
213 { Presentation for TCastleControl.
214 This class is necessary to manage WinAPI HWND associated with FMX control. }
215 TWinNativeGLControl = class(TWinPresentation)
216 protected
217 procedure CreateHandle; override;
218 procedure DestroyHandle; override;
219 public
220 function CastleControl: TCastleControl;
221 end;
222
223 function TWinNativeGLControl.CastleControl: TCastleControl;
224 begin
225 Result := Control as TCastleControl;
226 end;
227
228 procedure TWinNativeGLControl.CreateHandle;
229 begin
230 inherited;
231 { Looking at TWinNativeMemo.CreateHandle confirms this can be called with Handle = null }
232 if Handle <> NullHWnd then
233 CastleControl.CreateHandle;
234 end;
235
236 procedure TWinNativeGLControl.DestroyHandle;
237 begin
238 if Handle <> NullHWnd then
239 CastleControl.DestroyHandle;
240 inherited;
241 end;
242
243 {$endif}
244
245 { TCastleControl.TContainer ---------------------------------------------------}
246
247 constructor TCastleControl.TContainer.Create(AParent: TCastleControl);
248 begin
249 inherited Create(AParent); // AParent must be a component Owner to show published properties of container in LFM
250 Parent := AParent;
251 end;
252
253 procedure TCastleControl.TContainer.AdjustContext(const PlatformContext: TGLContext);
254 begin
255 Parent.FGLUtility.ContextAdjustEarly(PlatformContext);
256 end;
257
258 function TCastleControl.TContainer.GetMousePosition: TVector2;
259 begin
260 Result := Parent.FMousePosition;
261 end;
262
263 procedure TCastleControl.TContainer.SetMousePosition(const Value: TVector2);
264 begin
265 { TODO
266
267 There's no facility to do this using FMX, it seems.
268 We need platform-specific code.
269 - So far in CastleFmxUtils we have FmxSetMousePos for Linux, use it.
270 - And implement equivalent for Windows.
271 }
272 end;
273
274 function TCastleControl.TContainer.PixelsWidth: Integer;
275 var
276 Scale: Single;
277 begin
278 if Parent.FGLUtility <> nil then
279 Scale := Parent.FGLUtility.Scale
280 else
281 Scale := 1;
282
283 Result := Round(Parent.Width * Scale);
284 end;
285
286 function TCastleControl.TContainer.PixelsHeight: Integer;
287 var
288 Scale: Single;
289 begin
290 if Parent.FGLUtility <> nil then
291 Scale := Parent.FGLUtility.Scale
292 else
293 Scale := 1;
294
295 Result := Round(Parent.Height * Scale);
296 end;
297
298 procedure TCastleControl.TContainer.Invalidate;
299 begin
300 Parent.InvalidateRect(TRectF.Create(0, 0, Width, Height));
301 end;
302
303 procedure TCastleControl.TContainer.SetInternalCursor(const Value: TMouseCursor);
304 begin
305 { TODO (use similar code from CASTLE_WINDOW_FORM implementation).
306
307 Note: This is commonly used by MouseLook, but it will not work OK
308 until both this and SetMousePosition are implemented. }
309 end;
310
311 { TCastleControl ---------------------------------------------------- }
312
313 constructor TCastleControl.Create(AOwner: TComponent);
314 begin
315 inherited;
316
317 FContainer := TContainer.Create(Self);
318 FContainer.SetSubComponent(true);
319 FContainer.Name := 'Container';
320
321 FGLUtility := TFmxOpenGLUtility.Create;
322 FGLUtility.Control := Self;
323 FGLUtility.OnHandleAfterCreateEvent := CreateHandle;
324 FGLUtility.OnHandleBeforeDestroyEvent := DestroyHandle;
325
326 { In FMX, this causes adding WS_TABSTOP to Params.Style
327 in TWinPresentation.CreateParams. So it is more efficient to call
328 before we actually create window by setting ControlType. }
329 TabStop := true;
330
331 CanFocus := True;
332
333 { Makes the Presentation be TWinNativeGLControl, which has HWND.
334 Do this after FContainer is initialized, as it may call CreateHandle,
335 which in turn requires FContainer to be created.
336
337 Note that we cannnot do this at design-time (in Delphi IDE):
338
339 - Switching to TControlType.Platform at design-time
340 creates additional weird (visible in task bar, detached from form designer)
341 window on Windows, alongside main Delphi IDE window.
342
343 User can even close this window, causing crashes
344 (later when closing the FMX form, there will be exception,
345 because the Windows handle went away and FMX is not prepared for it).
346
347 - We *can* create OpenGL context in this weird window, and render there...
348 But all my experiments to attach it to form designer in Delphi IDE failed.
349 Overriding TWinNativeGLControl.CreateParams to
350
351 1. Params.Style := Params.Style or WS_CHILD
352 2. Params.WndParent := ContainerHandle;
353 3. CreateSubClass(Params, 'CastleControl');
354
355 .. yield nothing.
356
357 1 and 2 should indeed not be necessary, this is done by default by FMX,
358 we have WS_CHILD by default.
359
360 None of the above cause our rendering to be attached to Delphi IDE
361 as you would expect.
362
363 - FMX controls cannot render in native style at design-time it seems.
364
365 That is also the case for TMemo or TEdit,
366 their rendering can be native only at runtime (if you set their ControlType
367 to platform), at design-time they just display the "styled" (non-native)
368 along with an icon informing they will be native at runtime.
369
370 See
371 https://docwiki.embarcadero.com/RADStudio/Alexandria/en/FireMonkey_Native_Windows_Controls#Visual_Changes_to_Native_Windows_Controls
372 }
373 if not (csDesigning in ComponentState) then
374 ControlType := TControlType.Platform;
375 end;
376
377 destructor TCastleControl.Destroy;
378 begin
379 FreeAndNil(FGLUtility);
380 inherited;
381 end;
382
383 procedure TCastleControl.CreateHandle;
384 begin
385 { Do not create context at design-time.
386 We don't even set "ControlType := TControlType.Platform" at design-time now
387 (see constructor comments),
388 this line only secures in case user would set ControlType in a TCastleControl
389 descendant at design-time. }
390 if csDesigning in ComponentState then
391 Exit;
392
393 { Thanks to TWinNativeGLControl, we have Windows HWND for this control now in
394 (Presentation as TWinNativeGLControl).Handle
395 This is used in AdjustContext and
396 is necessary to create OpenGL context that only renders to this control.
397
398 Note: The only other way in FMX to get HWND seems to be to get form HWND,
399 WindowHandleToPlatform(Handle).Wnd
400 but this is not useful for us (we don't want to always render to full window).
401 }
402 FContainer.InitializeContext;
403 end;
404
405 procedure TCastleControl.DestroyHandle;
406 begin
407 FContainer.FinalizeContext;
408 end;
409
410 procedure TCastleControl.Paint;
411 var
412 R: TRectF;
413 begin
414 { See our constructor comments:
415 looks like native drawing at design-time in FMX is just not possible reliably. }
416
417 if csDesigning in ComponentState then
418 begin
419 inherited;
420 R := LocalRect;
421
422 Canvas.Fill.Kind := TBrushKind.Solid;
423 Canvas.Fill.Color := $A0909090;
424 Canvas.FillRect(R, 0, 0, [], 1.0);
425
426 Canvas.Fill.Color := TAlphaColors.Yellow;
427 Canvas.FillText(R,
428 'Run the project to see actual rendering of ' + Name + ' (' + ClassName + ')',
429 true, 1.0, [], TTextAlign.Center);
430 end else
431 begin
432 { We must have OpenGL context at this point,
433 and on Delphi/Linux there is no way to register "on native handle creation",
434 we must manually perform native handle creation (with GL context)
435 using our FGLUtility.
436
437 Maybe in the future it will make sense to do this also from some other events,
438 like update or mouse/key press?
439 Doesn't seem necessary in practice for now. }
440 InternalHandleNeeded;
441
442 { TODO: Not the best place to call this.
443 This assumes we render often (like every frame)
444 and we can update our GTK control size (thus OpenGL size)
445 right before render.
446 Hm, but we don't have in this class comfortable Update (for this TCastleWindow)
447 now, though our Container could expose it. }
448 FGLUtility.Update;
449
450 // inherited not needed, and possibly causes something unnecessary
451 FContainer.DoRender;
452 end;
453 end;
454
455 class procedure TCastleControl.TContainer.UpdatingEnable;
456 begin
457 inherited;
458
459 {$ifdef USE_TIMER}
460 UpdatingTimer := TTimer.Create(nil);
461 UpdatingTimer.Interval := 1;
462 UpdatingTimer.OnTimer := {$ifdef FPC}@{$endif} UpdatingTimerEvent;
463 {$endif}
464 end;
465
466 class procedure TCastleControl.TContainer.UpdatingDisable;
467 begin
468 {$ifdef USE_TIMER}
469 FreeAndNil(UpdatingTimer);
470 {$endif}
471 inherited;
472 end;
473
474 class procedure TCastleControl.TContainer.UpdatingTimerEvent(Sender: TObject);
475 begin
476 DoUpdateEverything;
477 end;
478
479 class procedure TCastleControl.ProcessTasks;
480 begin
481 // Does nothing when USE_TIMER, which is OK, timer does everything
482 {$ifndef USE_TIMER}
483 TContainer.UpdatingTimerEvent(nil);
484 {$endif}
485 end;
486
487 procedure TCastleControl.Resize;
488 begin
489 inherited;
490 if Container.GLInitialized then
491 begin
492 Container.MakeContextCurrent;
493 Container.EventResize;
494 end;
495 end;
496
497 class procedure TCastleControl.ApplicationRun;
498 begin
499 {$ifdef USE_TIMER}
500 Application.Run;
501 {$else}
502 Application.RealCreateForms;
503
504 { On Linux, it's especially important to check Terminating (not just Terminated)
505 below.
506
507 That's because FMXLinux seems to rely on GTK mechanism to exit
508 main loop when user calls Application.Terminate, which means it assumes
509 that you have executed Application.Run. (But we cannot use Application.Run
510 because timer is unreliable on FMXLinux and would sometimes hang application
511 rendering, see USE_TIMER comments.) Calling "Application.Run" sets
512 only Terminating:=true and makes GTK error:
513
514 gtk_main_quit: assertion 'main_loops != NULL' failed
515
516 We can't really prevent GTK error,
517 but at least we can react to Terminating and exit. }
518
519 while not (ApplicationState in [
520 TApplicationState.Terminating,
521 TApplicationState.Terminated]) do
522 begin
523 Application.ProcessMessages;
524 ProcessTasks;
525 end;
526
527 { TODO (but it seems FMXLinux issue -- we should report):
528 On FMXLinux, not using "Application.Run"
529 means that FMXLinux never frees the Application singleton.
530
531 Consequently, things owned by Application are never freed,
532 TForm.OnDestroy callbacks are not run,
533 and component destructors like TCastleControl.Destroy are not run.
534 Things just leak at program exit.
535
536 It seems FMXLinux "TPlatformLinux.Destroy"
537 should just call "FreeAndNil(Application)". This is what Delphi built-in
538 "TPlatformWin.Destroy" and "TPlatformCocoa.Destroy" are doing.
539 It also makes sense since "TPlatformLinux.Create" creates the Application
540 singleton.
541
542 In fact, I haven't found *how is Application freed when
543 we call Application.Run*. FMXLinux never seems to free Application.
544 Unlike Windows and macOS FMX platforms.
545 But logging shows that forms *are* freed at application exit
546 if we run Application.Run, but not otherwise.
547
548 This is reproducible in FMX almost-blank application, without any CGE,
549 just remove Application.Run but call Application.RealCreateForms .
550 You will notice that form OnCreate callback executes,
551 but form OnDestroy callback never happens. }
552
553 FreeAndNil(Application);
554 {$endif}
555 end;
556
557 function TCastleControl.GetCurrentShift: TShiftState;
558 begin
559 Result := [];
560 if Container.Pressed.Keys[keyShift] then
561 Include(Result, ssShift);
562 if Container.Pressed.Keys[keyAlt] then
563 Include(Result, ssAlt);
564 if Container.Pressed.Keys[keyCtrl] then
565 Include(Result, ssCtrl);
566 end;
567
568 procedure TCastleControl.SetCurrentShift(const Value: TShiftState);
569 begin
570 Container.Pressed.Keys[keyShift] := ssShift in Value;
571 Container.Pressed.Keys[keyAlt ] := ssAlt in Value;
572 Container.Pressed.Keys[keyCtrl ] := ssCtrl in Value;
573 end;
574
575 function TCastleControl.MousePosToCastle(const X, Y: Single): TVector2;
576 begin
577 Result := Vector2(X, Height - 1 - Y) * FGLUtility.Scale;
578 end;
579
580 procedure TCastleControl.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
581 Y: Single);
582 var
583 MyButton: TCastleMouseButton;
584 begin
585 if not IsFocused then // TODO: doesn't seem to help with focus
586 SetFocus;
587
588 inherited; { FMX OnMouseDown before our callbacks }
589
590 FMousePosition := MousePosToCastle(X, Y);
591
592 if MouseButtonToCastle(Button, MyButton) then
593 Container.MousePressed := Container.MousePressed + [MyButton];
594
595 CurrentShift := Shift; { do this after Pressed update above, and before *Event }
596
597 if MouseButtonToCastle(Button, MyButton) then
598 Container.EventPress(InputMouseButton(FMousePosition, MyButton, 0,
599 ModifiersDown(Container.Pressed)));
600 end;
601
602 procedure TCastleControl.MouseMove(Shift: TShiftState; NewX, NewY: Single);
603 var
604 NewMousePos: TVector2;
605 begin
606 inherited;
607
608 NewMousePos := MousePosToCastle(NewX, NewY);
609
610 Container.EventMotion(InputMotion(FMousePosition,
611 NewMousePos, Container.MousePressed, 0));
612
613 // change FMousePosition *after* EventMotion, callbacks may depend on it
614 FMousePosition := NewMousePos;
615
616 CurrentShift := Shift;
617 end;
618
619 procedure TCastleControl.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
620 Y: Single);
621 var
622 MyButton: TCastleMouseButton;
623 begin
624 inherited; { FMX OnMouseUp before our callbacks }
625
626 FMousePosition := MousePosToCastle(X, Y);
627
628 if MouseButtonToCastle(Button, MyButton) then
629 Container.MousePressed := Container.MousePressed - [MyButton];
630
631 CurrentShift := Shift; { do this after Pressed update above, and before *Event }
632
633 if MouseButtonToCastle(Button, MyButton) then
634 Container.EventRelease(InputMouseButton(FMousePosition, MyButton, 0));
635 end;
636
637 procedure TCastleControl.MouseWheel(Shift: TShiftState; WheelDelta: Integer;
638 var Handled: Boolean);
639 begin
640 if not Handled then
641 Handled := Container.EventPress(InputMouseWheel(
642 FMousePosition, WheelDelta / 120, true, ModifiersDown(Container.Pressed)));
643
644 inherited;
645 end;
646
647 procedure TCastleControl.KeyDown(var Key: Word; var KeyChar: WideChar;
648 Shift: TShiftState);
649 var
650 CastleKey: TKey;
651 CastleKeyString: String;
652 CastleEvent: TInputPressRelease;
653 begin
654 inherited;
655 CurrentShift := Shift;
656
657 FmxKeysToCastle(Key, KeyChar, Shift, CastleKey, CastleKeyString);
658
659 if (CastleKey <> keyNone) or (CastleKeyString <> '') then
660 begin
661 CastleEvent := InputKey(FMousePosition, CastleKey, CastleKeyString,
662 ModifiersDown(Container.Pressed));
663
664 // check this before updating Container.Pressed
665 CastleEvent.KeyRepeated :=
666 // Key already pressed
667 ((CastleEvent.Key = keyNone) or Container.Pressed.Keys[CastleEvent.Key]) and
668 // KeyString already pressed
669 ((CastleEvent.KeyString = '') or Container.Pressed.Strings[CastleEvent.KeyString]);
670
671 Container.Pressed.KeyDown(CastleEvent.Key, CastleEvent.KeyString);
672 if Container.EventPress(CastleEvent) then
673 begin
674 Key := 0;
675 KeyChar := #0;
676 end;
677 end;
678 end;
679
680 procedure TCastleControl.KeyUp(var Key: Word; var KeyChar: WideChar;
681 Shift: TShiftState);
682 var
683 CastleKey: TKey;
684 CastleKeyString: String;
685 CastleEvent: TInputPressRelease;
686 begin
687 inherited;
688 CurrentShift := Shift;
689
690 FmxKeysToCastle(Key, KeyChar, Shift, CastleKey, CastleKeyString);
691
692 { Note that KeyUp seems to have additional issue with FMXLinux,
693 not fixed in this code:
694
695 When the key is held (so we expect to receive multiple KeyDown
696 for one KeyUp), FMXLinux sends additional KeyUp before each KeyDown.
697 So for code, it seems as if user is pressing and releasing the key.
698 We cannot easily workaround it on CGE side.
699 To track keys being held, we advise to check Pressed[keyXxx] in Update
700 methods, instead of relying on KeyDown/KeyUp. }
701
702 if (CastleKey <> keyNone) or (CastleKeyString <> '') then
703 begin
704 CastleEvent := InputKey(FMousePosition, CastleKey, CastleKeyString,
705 ModifiersDown(Container.Pressed));
706
707 Container.Pressed.KeyUp(CastleEvent.Key, CastleEvent.KeyString);
708 if Container.EventRelease(CastleEvent) then
709 begin
710 Key := 0;
711 KeyChar := #0;
712 end;
713 end;
714 end;
715
716 procedure TCastleControl.InternalHandleNeeded;
717 begin
718 FGLUtility.HandleNeeded;
719 end;
720
721 function TCastleControl.DefinePresentationName: String;
722 begin
723 { This method may seem not necessary in some tests: if your application
724 just instantiates exactly TCastleControl (not a descendant of it),
725 then this method is not necessary.
726 E.g. CastleFmx example doesn't need it to work properly.
727
728 But this method becomes necessary if you instantiate *descendants of
729 TCastleControl*. Like TGoodOpenGLControl created by CASTLE_WINDOW_FORM.
730 Without this method, these descendants would not
731 use TWinNativeGLControl (they would use default FMX
732 TWinStyledPresentation) and in effect critical code from CGE
733 TWinNativeGLControl would not run.
734
735 See also:
736 - FMX TMemo does it, likely for above reasons.
737 - https://stackoverflow.com/questions/37281970/a-descendant-of-tstyledpresentationproxy-has-not-been-registered-for-class
738 - See also
739 http://yaroslavbrovin.ru/new-approach-of-development-of-firemonkey-control-control-model-presentation-part-1-en/
740 https://github.com/tothpaul/Firemonkey/tree/master/GLPanel
741 for hints how to use platform-specific controls with FMX.
742 }
743
744 Result := 'CastleControl-' + GetPresentationSuffix;
745 end;
746
747 procedure TCastleControl.SetVisible(const AValue: Boolean);
748 begin
749 inherited;
750 { This happens e.g. when developer does "CastleControl.Visible := false".
751 We will recreate this handle later by FGLUtility.HandleNeeded,
752 at first paint. }
753 if not AValue then
754 FGLUtility.HandleRelease;
755 end;
756
757 procedure TCastleControl.DoRootChanging(const NewRoot: IRoot);
758 begin
759 inherited;
760 { This happens e.g. when developer does "CastleControl.Parent := nil"
761 or when TCastleControl is destroyed (e.g. because parent form is destroyed).
762 We will recreate this handle later by FGLUtility.HandleNeeded,
763 at first paint. }
764 FGLUtility.HandleRelease;
765 end;
766
767 (*
768 procedure TCastleControl.DoRootChanged;
769 begin
770 inherited;
771 { Not really needed, we'll create context from Paint anyway.
772 And this was too early in some cases. }
773 // if FGLUtility.HandlePossible then
774 // FGLUtility.HandleNeeded;
775 end;
776 *)
777
778 {$ifdef MSWINDOWS}
779 initialization
780 { Make TWinNativeGLControl used
781 for TCastleControl with ControlType = TControlType.Platform. }
782 TPresentationProxyFactory.Current.Register(TCastleControl, TControlType.Platform, TWinPresentationProxy<TWinNativeGLControl>);
783 finalization
784 TPresentationProxyFactory.Current.Unregister(TCastleControl, TControlType.Platform, TWinPresentationProxy<TWinNativeGLControl>);
785 {$endif}
786 end.