Debian Codegraph @

src/delphi/castleinternalfmxutils.pas

raw
1 {
2 Copyright 2022-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 { Utilities specific to FMX in Castle Game Engine.
17 This allows sharing of solutions between FMX TOpenGLControl and FMX TCastleControl. }
18 unit CastleInternalFmxUtils;
19
20 interface
21
22 uses FMX.Controls, FMX.Controls.Presentation, FMX.Types, UITypes,
23 {$ifdef MSWINDOWS} FMX.Presentation.Win, {$endif}
24 {$ifdef LINUX} FMX.Platform.Linux, {$endif}
25 CastleInternalContextBase, CastleVectors;
26
27 type
28 THandleEvent = procedure of object;
29
30 { Utility for FMX controls to help them initialize OpenGL context.
31
32 This tries to abstract as much as possible the platform-specific ways
33 how to get OpenGL context (and native handle) on a given control,
34 in a way that is useful for both FMX TOpenGLControl and FMX TCastleControl.
35
36 The current state:
37
38 - On Windows: make sure native Windows handle is initialized when necessary,
39 and pass it to TGLContextWgl.
40 - On Linux: we have to create our own Gtk widget (since FMXLinux only ever
41 creates native handle for the whole form, it seems).
42 And insert it into FMX form, keeping the existing FMX drawing area too.
43 And then use TGLContextEgl to connect to our own Gtk widget.
44
45 Note: We could not make TCastleControl descend from TOpenGLControl on FMX
46 (like we did on LCL), since the GL work of TCastleControl is partially
47 done by the container (to be shared, in turn,
48 with VCL and in future LCL implementations).
49 So to have enough flexibility how to organize hierarchy, this is rather
50 a separate class that is just created and used by both
51 FMX TOpenGLControl and FMX TCastleControl. }
52 TFmxOpenGLUtility = class
53 {$ifdef LINUX}
54 private
55 GLAreaInitialized: Boolean;
56 GLAreaGtk: Pointer;
57 GLAreaRect: TVector4Integer; //< x, y, width, height
58 DrawingAreaParent: Pointer;
59 {$endif}
60 public
61 { Set before calling HandleNeeded.
62 Cannot change during lifetime of this instance, for now. }
63 Control: TPresentedControl;
64
65 { Called, if assigned, after creation (OnHandleAfterCreateEvent)
66 or before destruction (OnHandleBeforeDestroyEvent)
67 of a native handle.
68
69 @bold(This is called only on platforms where
70 FMX Presentation is not available.)
71 This also implies it is called only on platforms
72 where our code creates the native handle we need,
73 e.g. Gtk handle on Linux.
74
75 In practice this means this is called now only on Delphi/Linux.
76
77 On Delphi/Windows, use FMX Presentation features
78 instead of register notifications when handle is created/
79 destroyed. }
80 OnHandleAfterCreateEvent: THandleEvent;
81 OnHandleBeforeDestroyEvent: THandleEvent;
82
83 { Is initializing internal resources, needed by HandleNeeded,
84 possible now. Use this if calling HandleNeeded is not necessary now,
85
86 This does something only on platforms where
87 FMX Presentation is not available and we need to take care ourselves
88 of the necessary internal per-control handle where OpenGL context
89 exists. In practice: only on Delphi/Linux now.
90
91 On other platforms, it always returns true. }
92 // Not needed in the end // function HandlePossible: Boolean;
93
94 { Make sure that Control has initialized internal handle,
95 necessary to later initialize OpenGL context for this.
96 This must be called before ContextAdjustEarly.
97
98 - On some platforms, like Windows,
99 creating a handle should provoke ContextAdjustEarly
100 and TGLContext.Initialize.
101 The caller should make it happen: see
102 TPresentationProxyFactory.Current.Register and our presentation classes.
103 This works nicely when FMX platform defines
104 Presentation and classes like TWinPresentation.
105
106 - On other platforms, like Linux, we create handle ourselves.
107 Manually make sure context is created
108 after handle is obtained.
109 Needed for Delphi/Linux that doesn't define any "presentation"
110 stuff and only creates a handle for the entire form. }
111 procedure HandleNeeded;
112
113 { Release handle that was created by @link(HandleNeeded).
114
115 This does something only on platforms where
116 FMX Presentation is not available and we need to take care ourselves
117 of the necessary internal per-control handle where OpenGL context
118 exists. }
119 procedure HandleRelease;
120
121 { Adjust TGLContext parameters before calling TGLContext.CreateContext.
122 You must call HandleNeeded earlier.
123
124 Extracts platform-specific bits from given FMX Control,
125 and puts in platform-specific bits of TGLContext descendants.
126 This is used by TCastleControl and TOpenGLControl.
127 It does quite low-level and platform-specific job, dictated
128 by the necessity of how FMX works, to be able to get context.
129
130 This is synchronized with what ContextCreateBestInstance does on this platform. }
131 procedure ContextAdjustEarly(const PlatformContext: TGLContext);
132
133 { Call this often to perform platform-specific adjustments.
134
135 At this point, this is required on Linux: we need to synchronize
136 internal GTK control with desired position and size of FMX TCastleControl. }
137 procedure Update;
138
139 { Size reported by FMX controls needs to be multiplied by this
140 to get size in physical pixels (which we need e.g. for OpenGL context). }
141 function Scale: Single;
142 end;
143
144 implementation
145
146 {$if defined(MSWINDOWS)}
147 {$I castleinternalfmxutils_windows.inc}
148 {$elseif defined(LINUX)}
149 {$I castleinternalfmxutils_linux.inc}
150 {$else}
151 {$I castleinternalfmxutils_other_os.inc}
152 {$endif}
153
154 end.