Debian Codegraph @

src/ui/castleuicontrols_miscellaneous_globals.inc

raw
1 {%MainUnit castleuicontrols.pas}
2 {
3 Copyright 2010-2022 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 { Called when first OpenGL context opens. }
20 function OnGLContextOpen: TGLContextEventList; deprecated 'use ApplicationProperties.OnGLContextOpen';
21 { Called when last OpenGL context closes. }
22 function OnGLContextClose: TGLContextEventList; deprecated 'use ApplicationProperties.OnGLContextClose';
23 { Is any OpenGL context open. }
24 function IsGLContextOpen: boolean; deprecated 'use ApplicationProperties.IsGLContextOpen';
25
26 const
27 prLeft = prLow deprecated;
28 prRight = prHigh deprecated;
29 prBottom = prLow deprecated;
30 prTop = prHigh deprecated;
31
32 hpLeft = CastleRectangles.hpLeft ;
33 hpMiddle = CastleRectangles.hpMiddle;
34 hpRight = CastleRectangles.hpRight ;
35 vpBottom = CastleRectangles.vpBottom;
36 vpMiddle = CastleRectangles.vpMiddle;
37 vpTop = CastleRectangles.vpTop ;
38
39 type
40 { Internal for communication with CastleWindow or CastleControl.
41 See @link(GetMainContainer).
42 @exclude }
43 TOnMainContainer = function: TCastleContainer of object;
44
45 var
46 { Internal for communication with CastleWindow or CastleControl.
47 You can set this, but read it only through GetMainContainer function.
48 See @link(GetMainContainer).
49 @exclude }
50 OnMainContainer: TOnMainContainer = nil;
51
52 { Internal for communication with CastleWindow or CastleControl.
53 Returns the main UI container.
54 It may be used in rare situations when the UI control may not be added
55 to the container yet, but it needs to know the container.
56
57 Returns @nil if OnMainContainer not assigned (possible if you don't use
58 CastleWindow or CastleControl), or OnMainContainer returns @nil
59 (possible if you don't set Application.MainWindow or TCastleControl.MainControl).
60
61 @exclude }
62 function GetMainContainer: TCastleContainer;
63
64 { Render control contents to an RGBA image, using off-screen rendering.
65 The background behind the control is filled with BackgroundColor
66 (which may be transparent, e.g. with alpha = 0).
67
68 The rendering is done using off-screen FBO.
69 Which means that you can request any size, you are @bold(not) limited
70 to your current window / control size.
71
72 Make sure that the control is nicely positioned to fill the ViewportRect.
73 Usually you want to adjust control size and position,
74 and disable UI scaling (set TCastleUserInterface.EnableUIScaling = @false
75 if you use TCastleContainer.UIScaling).
76
77 This is the @italic(easiest) way to make off-screen rendering,
78 i.e. to render 3D things (like TCastleScene in TCastleViewport)
79 into an image. This is @italic(not the fastest way), as it creates
80 new TGLRenderToTexture instance each time,
81 and it grabs the image contents to CPU.
82 If you want a faster approach, use @link(TCastleContainer.RenderControl)
83 and render into @link(TDrawableImage) using @link(TDrawableImage.RenderToImageBegin)
84 and @link(TDrawableImage.RenderToImageEnd).
85 }
86 function RenderControlToImage(const Container: TCastleContainer;
87 const Control: TCastleUserInterface;
88 const ViewportRect: TRectangle;
89 const BackgroundColor: TCastleColor): TRGBAlphaImage; overload;
90
91 {$endif read_interface}
92
93 {$ifdef read_implementation}
94
95 function OnGLContextOpen: TGLContextEventList;
96 begin
97 Result := ApplicationProperties.OnGLContextOpen;
98 end;
99
100 function OnGLContextClose: TGLContextEventList;
101 begin
102 Result := ApplicationProperties.OnGLContextClose;
103 end;
104
105 function IsGLContextOpen: boolean;
106 begin
107 Result := ApplicationProperties.IsGLContextOpen;
108 end;
109
110 function RenderControlToImage(const Container: TCastleContainer;
111 const Control: TCastleUserInterface;
112 const ViewportRect: TRectangle;
113 const BackgroundColor: TCastleColor): TRGBAlphaImage;
114
115 function CreateTargetTexture(const W, H: Integer): TGLTextureId;
116 var
117 InitialImage: TCastleImage;
118 begin
119 InitialImage := TRGBAlphaImage.Create(W, H);
120 try
121 InitialImage.URL := 'generated:/temporary-render-to-texture';
122 InitialImage.Clear(Vector4Byte(255, 0, 255, 255));
123 Result := LoadGLTexture(InitialImage,
124 TextureFilter(minNearest, magNearest),
125 Texture2DClampToEdge, nil, true);
126 finally FreeAndNil(InitialImage) end;
127 end;
128
129 var
130 W, H: Integer;
131 RenderToTexture: TGLRenderToTexture;
132 TargetTexture: TGLTextureId;
133 begin
134 W := ViewportRect.Width;
135 H := ViewportRect.Height;
136 RenderToTexture := TGLRenderToTexture.Create(W, H);
137 try
138 // RenderToTexture.Buffer := tbNone;
139 // RenderToTexture.ColorBufferAlpha := true;
140
141 RenderToTexture.Buffer := tbColor;
142 TargetTexture := CreateTargetTexture(W, H);
143 RenderToTexture.SetTexture(TargetTexture, GL_TEXTURE_2D);
144 RenderToTexture.GLContextOpen;
145 RenderToTexture.RenderBegin;
146
147 { actually render }
148 RenderContext.Clear([cbColor], BackgroundColor);
149 Container.RenderControl(Control, ViewportRect);
150
151 RenderToTexture.RenderEnd;
152
153 Result := TRGBAlphaImage.Create(W, H);
154 SaveTextureContents(Result, TargetTexture);
155
156 RenderToTexture.GLContextClose;
157
158 glFreeTexture(TargetTexture);
159 finally FreeAndNil(RenderToTexture) end;
160 end;
161
162 function GetMainContainer: TCastleContainer;
163 begin
164 if Assigned(OnMainContainer) then
165 Result := OnMainContainer()
166 else
167 //raise Exception.Create('OnMainContainer not assigned. Use CastleWindow or CastleControl unit before starting TCastleView');
168 // returning nil is more flexible to the caller
169 Result := nil;
170 end;
171
172 procedure InitializeInputInspectorDebug;
173 begin
174 TCastleContainer.InputInspector.Key := keyF8;
175 TCastleContainer.InputInspector.PressFingers := 3;
176 end;
177
178 procedure InitializeInputInspector;
179 begin
180 TCastleContainer.FInputInspector := TInputInspector.Create(nil);
181 TCastleContainer.InputInspector.Key := keyNone;
182 TCastleContainer.InputInspector.KeyModifiers := [];
183 TCastleContainer.InputInspector.PressFingers := 0;
184 TCastleContainer.InputInspector.PressTime := 1;
185
186 ApplicationProperties.AddInitializeDebugListener(
187 {$ifdef FPC}@{$endif} InitializeInputInspectorDebug);
188 end;
189
190 procedure FinalizeInputInspector;
191 begin
192 FreeAndNil(TCastleContainer.FInputInspector);
193 end;
194
195 {$endif read_implementation}