Debian Codegraph @

src/delphi/vcl.castlecontrol.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 { Control with OpenGL context on a Delphi VCL form. }
17 unit Vcl.CastleControl;
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses SysUtils, Classes, Vcl.Controls, Vcl.ExtCtrls, Types, WinApi.Messages,
24 CastleGLVersion, CastleGLUtils,
25 CastleInternalContextBase, CastleInternalContextWgl, CastleInternalContainer,
26 CastleVectors, CastleKeysMouse;
27
28 type
29 { Control rendering OpenGL on VCL form. }
30 TCastleControl = class(TCustomControl)
31 strict private
32 type
33 { Non-abstract implementation of TCastleContainer that cooperates with
34 TCastleControl. }
35 TContainer = class(TCastleContainerEasy)
36 private
37 Parent: TCastleControl;
38 class var
39 UpdatingTimer: TTimer;
40 class procedure UpdatingTimerEvent(Sender: TObject);
41 protected
42 function GetMousePosition: TVector2; override;
43 procedure SetMousePosition(const Value: TVector2); override;
44 procedure AdjustContext(const PlatformContext: TGLContext); override;
45 class procedure UpdatingEnable; override;
46 class procedure UpdatingDisable; override;
47 public
48 constructor Create(AParent: TCastleControl); reintroduce;
49 procedure Invalidate; override;
50 function PixelsWidth: Integer; override;
51 function PixelsHeight: Integer; override;
52 procedure SetInternalCursor(const Value: TMouseCursor); override;
53 end;
54
55 var
56 FContainer: TContainer;
57 FMousePosition: TVector2;
58
59 { Call whenever you have new knowledge about new shift state.
60
61 Sometimes, releasing shift / alt / ctrl keys will not be reported
62 properly to KeyDown / KeyUp. Example: opening a menu
63 through Alt+F for "_File" will make keydown for Alt,
64 but not keyup for it, and DoExit will not be called,
65 so ReleaseAllKeysAndMouse will not be called.
66
67 To counteract this, call this method when Shift state is known,
68 to update Pressed when needed. }
69 procedure UpdateShiftState(const Shift: TShiftState);
70 private
71 procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
72 protected
73 procedure CreateHandle; override;
74 procedure DestroyHandle; override;
75 { // TODO
76 procedure KeyDown(var Key: Word; Shift: TShiftState); override;
77 procedure KeyUp(var Key: Word; Shift: TShiftState); override;
78 procedure KeyPress(var Key: Char); override;
79 }
80 procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
81 X, Y: Integer); override;
82 procedure MouseMove(Shift: TShiftState; NewX, NewY: Integer); override;
83 procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
84 X, Y: Integer); override;
85 function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer;
86 MousePos: TPoint): Boolean; override;
87 procedure KeyDown(var Key: Word; Shift: TShiftState); override;
88 procedure KeyPress(var Key: Char); override;
89 procedure KeyUp(var Key: Word; Shift: TShiftState); override;
90 procedure Resize; override;
91 public
92 constructor Create(AOwner: TComponent); override;
93 destructor Destroy; override;
94 procedure Paint; override;
95 { To handle some special keys, set on form KeyPreview
96 and call these methods from VCL form's OnKeyDown / OnKeyUp. }
97 procedure PreviewFormKeyDown(var Key: Word; Shift: TShiftState);
98 procedure PreviewFormKeyUp(var Key: Word; Shift: TShiftState);
99 published
100 { Access Castle Game Engine container properties and events,
101 not specific for FMX. }
102 property Container: TContainer read FContainer;
103
104 property Align;
105 property Anchors;
106 property OnClick;
107 property OnDblClick;
108 property OnMouseDown;
109 property OnMouseUp;
110 property OnMouseMove;
111 property OnMouseWheel;
112 end;
113
114 procedure Register;
115
116 implementation
117
118 uses Windows,
119 CastleRenderOptions, CastleApplicationProperties, CastleRenderContext,
120 CastleRectangles, CastleUIControls, CastleInternalDelphiUtils;
121
122 procedure Register;
123 begin
124 RegisterComponents('Castle', [
125 TCastleControl
126 ]);
127 end;
128
129 { TCastleControl.TContainer ---------------------------------------------------}
130
131 constructor TCastleControl.TContainer.Create(AParent: TCastleControl);
132 begin
133 inherited Create(AParent); // AParent must be a component Owner to show published properties of container in LFM
134 Parent := AParent;
135 end;
136
137 procedure TCastleControl.TContainer.AdjustContext(const PlatformContext: TGLContext);
138 {$ifdef MSWINDOWS}
139 var
140 WinContext: TGLContextWGL;
141 begin
142 inherited;
143 WinContext := PlatformContext as TGLContextWGL;
144 WinContext.WndPtr := Parent.Handle;
145 WinContext.h_Dc := GetWindowDC(WinContext.WndPtr);
146 {$else}
147 begin
148 {$endif}
149 end;
150
151 function TCastleControl.TContainer.GetMousePosition: TVector2;
152 begin
153 Result := Parent.FMousePosition;
154 end;
155
156 procedure TCastleControl.TContainer.SetMousePosition(const Value: TVector2);
157 begin
158 // TODO
159 end;
160
161 class procedure TCastleControl.TContainer.UpdatingEnable;
162 begin
163 inherited;
164 UpdatingTimer := TTimer.Create(nil);
165 if csDesigning in UpdatingTimer.ComponentState then
166 { At design-time, limit FPS.
167 Otherwise even Delphi IDE may become sluggish on very old GPUs.
168 Observed on old laptops with Delphi 10.2.3 and 10.4 on Win64. }
169 UpdatingTimer.Interval := Round(1000 / 60)
170 else
171 UpdatingTimer.Interval := 1;
172 UpdatingTimer.OnTimer := {$ifdef FPC}@{$endif} UpdatingTimerEvent;
173 end;
174
175 class procedure TCastleControl.TContainer.UpdatingDisable;
176 begin
177 FreeAndNil(UpdatingTimer);
178 inherited;
179 end;
180
181 class procedure TCastleControl.TContainer.UpdatingTimerEvent(Sender: TObject);
182 begin
183 DoUpdateEverything;
184 end;
185
186 function TCastleControl.TContainer.PixelsWidth: Integer;
187 begin
188 Result := Parent.Width;
189 end;
190
191 procedure TCastleControl.WMEraseBkgnd(var Message: TWMEraseBkgnd);
192 begin
193 Message.Result := 1;
194 end;
195
196 function TCastleControl.TContainer.PixelsHeight: Integer;
197 begin
198 Result := Parent.Height;
199 end;
200
201 procedure TCastleControl.TContainer.Invalidate;
202 begin
203 Parent.Invalidate;
204 end;
205
206 procedure TCastleControl.TContainer.SetInternalCursor(const Value: TMouseCursor);
207 begin
208 // TODO
209 end;
210
211 { TCastleControl ---------------------------------------------------- }
212
213 constructor TCastleControl.Create(AOwner: TComponent);
214 begin
215 inherited;
216 FContainer := TContainer.Create(Self);
217 FContainer.SetSubComponent(true);
218 FContainer.Name := 'Container';
219
220 // needed to receive keys, like AWSD, in TCastleControl
221 TabStop := true;
222 end;
223
224 destructor TCastleControl.Destroy;
225 begin
226 { Looks like VCL doesn't (always?) call DestroyHandle.
227 While FContainer would be destroyed anyway,
228 but we need TCastleContainerEasy.DestroyContext call,
229 to e.g. make ApplicationProperties.OnGLContextClose call,
230 that frees various CGE things when last GL context is released. }
231
232 if FContainer.GLInitialized then
233 FContainer.FinalizeContext;
234 inherited;
235 end;
236
237 procedure TCastleControl.CreateHandle;
238 begin
239 inherited;
240 { Handle is only available now, in CreateHandle.
241 So only now call FContainer.CreateContext that does FContainer.AdjustContext. }
242 FContainer.InitializeContext;
243 end;
244
245 procedure TCastleControl.DestroyHandle;
246 begin
247 FContainer.FinalizeContext;
248 inherited;
249 end;
250
251 function TCastleControl.DoMouseWheel(Shift: TShiftState; WheelDelta: Integer;
252 MousePos: TPoint): Boolean;
253 begin
254 Result := Container.EventPress(InputMouseWheel(
255 FMousePosition, WheelDelta / 120, true, ModifiersDown(Container.Pressed)));
256 if Result then Exit;
257
258 Result := inherited;
259 end;
260
261 procedure TCastleControl.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
262 Y: Integer);
263 var
264 MyButton: TCastleMouseButton;
265 begin
266 { Focus on click, to receive e.g. AWSD keys in TCastleControl.
267 Testcase: examples/delphi/vcl with TCastleWalkNavigation in 3D view,
268 should handle AWSD without the need to do anything else. }
269 if not Focused then
270 SetFocus;
271
272 inherited; { VCL OnMouseDown before our callbacks }
273
274 FMousePosition := Vector2(X, Height - 1 - Y);
275
276 if MouseButtonToCastle(Button, MyButton) then
277 Container.MousePressed := Container.MousePressed + [MyButton];
278
279 UpdateShiftState(Shift); { do this after Pressed update above, and before *Event }
280
281 if MouseButtonToCastle(Button, MyButton) then
282 Container.EventPress(InputMouseButton(FMousePosition, MyButton, 0,
283 ModifiersDown(Container.Pressed)));
284 end;
285
286 procedure TCastleControl.MouseMove(Shift: TShiftState; NewX, NewY: Integer);
287 begin
288 inherited;
289
290 Container.EventMotion(InputMotion(FMousePosition,
291 Vector2(NewX, Height - 1 - NewY), Container.MousePressed, 0));
292
293 // change FMousePosition *after* EventMotion, callbacks may depend on it
294 FMousePosition := Vector2(NewX, Height - 1 - NewY);
295
296 UpdateShiftState(Shift);
297 end;
298
299 procedure TCastleControl.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
300 Y: Integer);
301 var
302 MyButton: TCastleMouseButton;
303 begin
304 inherited; { VCL OnMouseUp before our callbacks }
305
306 FMousePosition := Vector2(X, Height - 1 - Y);
307
308 if MouseButtonToCastle(Button, MyButton) then
309 Container.MousePressed := Container.MousePressed - [MyButton];
310
311 UpdateShiftState(Shift); { do this after Pressed update above, and before *Event }
312
313 if MouseButtonToCastle(Button, MyButton) then
314 Container.EventRelease(InputMouseButton(FMousePosition, MyButton, 0));
315 end;
316
317 procedure TCastleControl.Paint;
318 begin
319 //inherited; // inherited not needed, and possibly causes something unnecessary
320 FContainer.DoRender;
321 end;
322
323 procedure TCastleControl.PreviewFormKeyDown(var Key: Word; Shift: TShiftState);
324 begin
325 if //Focused and // TODO: It seems we are never Focused
326 (
327 (Key = VK_Down) or
328 (Key = VK_Up) or
329 (Key = VK_Left) or
330 (Key = VK_Right) or
331 (Key = VK_Space)
332 ) then
333 begin
334 KeyDown(Key, Shift);
335 Key := 0;
336 end;
337 end;
338
339 procedure TCastleControl.PreviewFormKeyUp(var Key: Word; Shift: TShiftState);
340 begin
341 if //Focused and // TODO: It seems we are never Focused
342 (
343 (Key = VK_Down) or
344 (Key = VK_Up) or
345 (Key = VK_Left) or
346 (Key = VK_Right) or
347 (Key = VK_Space)
348 ) then
349 begin
350 KeyUp(Key, Shift);
351 Key := 0;
352 end;
353 end;
354
355 procedure TCastleControl.Resize;
356 begin
357 inherited;
358 if Container.GLInitialized then
359 begin
360 Container.MakeContextCurrent;
361 Container.EventResize;
362 end;
363 end;
364
365 procedure TCastleControl.UpdateShiftState(const Shift: TShiftState);
366 begin
367 Container.Pressed.Keys[keyShift] := ssShift in Shift;
368 Container.Pressed.Keys[keyAlt ] := ssAlt in Shift;
369 Container.Pressed.Keys[keyCtrl ] := ssCtrl in Shift;
370 end;
371
372 procedure TCastleControl.KeyDown(var Key: Word; Shift: TShiftState);
373 var
374 CastleKey: TKey;
375 CastleKeyString: String;
376 CastleEvent: TInputPressRelease;
377 begin
378 inherited;
379 UpdateShiftState(Shift);
380
381 CastleKey := KeyToCastle(Key, Shift);
382 if CastleKey <> keyNone then
383 begin
384 CastleKeyString := SimpleKeyToString(CastleKey, Shift);
385
386 CastleEvent := InputKey(FMousePosition, CastleKey, CastleKeyString,
387 ModifiersDown(Container.Pressed));
388
389 // check this before updating Container.Pressed
390 CastleEvent.KeyRepeated :=
391 // Key already pressed
392 ((CastleEvent.Key = keyNone) or Container.Pressed.Keys[CastleEvent.Key]) and
393 // KeyString already pressed
394 ((CastleEvent.KeyString = '') or Container.Pressed.Strings[CastleEvent.KeyString]);
395
396 Container.Pressed.KeyDown(CastleEvent.Key, CastleEvent.KeyString);
397 if Container.EventPress(CastleEvent) then
398 Key := 0; // handled
399 end;
400 end;
401
402 procedure TCastleControl.KeyUp(var Key: Word; Shift: TShiftState);
403 var
404 CastleKey: TKey;
405 CastleKeyString: String;
406 CastleEvent: TInputPressRelease;
407 begin
408 inherited;
409 UpdateShiftState(Shift);
410
411 CastleKey := KeyToCastle(Key, Shift);
412 if CastleKey <> keyNone then
413 begin
414 CastleKeyString := SimpleKeyToString(CastleKey, Shift);
415
416 CastleEvent := InputKey(FMousePosition, CastleKey, CastleKeyString,
417 ModifiersDown(Container.Pressed));
418
419 Container.Pressed.KeyUp(CastleEvent.Key, CastleEvent.KeyString);
420 if Container.EventRelease(CastleEvent) then
421 Key := 0; // handled
422 end;
423 end;
424
425 procedure TCastleControl.KeyPress(var Key: Char);
426 begin
427 // TODO ignored now
428 inherited;
429 end;
430
431 end.