Debian Codegraph @

src/window/castlewindow_library.inc

raw
1 {%MainUnit castlewindow.pas}
2 {
3 Copyright 2004-2024 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 { CastleWindow backend that depends on external code to initialize
18 OpenGL context.
19 The external code must notify us about events by calling LibraryXxx methods,
20 like TCastleWindow.LibraryKeyDown or TCastleApplication.LibraryUpdate.
21 The external code will get notified about our events using LibraryCallbackProc. }
22
23 {$ifdef read_interface_uses}
24 {$endif}
25
26 {$ifdef read_implementation_uses}
27 CastleJoysticks,
28 {$endif}
29
30 {$ifdef read_interface_types}
31 TLibraryCallbackProc = function (eCode, iParam1, iParam2: cInt32; szParam: pcchar): cInt32; cdecl;
32 {$endif read_interface_types}
33
34 {$ifdef read_window_interface}
35 public
36 { Only the library in src/deprecated_library/castleengine.lpr uses this,
37 to notify our window instance about various events. }
38 procedure LibraryMouseWheel(const Scroll: Single; const Vertical: boolean);
39 {$endif read_window_interface}
40
41 {$ifdef read_application_interface}
42 private
43 FScreenWidth, FScreenHeight: Integer;
44 procedure WarningToCallback(const Category, S: String);
45 {$endif read_application_interface}
46
47 {$ifdef read_global_interface}
48
49 { Initialize / Finalize set up some global things.
50 Call CGEApp_Initialize / CGEApp_Finalize once and only once during the entire application. }
51 procedure CGEApp_Initialize(ApplicationConfigDirectory: PChar); cdecl;
52 procedure CGEApp_Finalize; cdecl;
53
54 { Used by the external process to ask for context properties from CGE
55 (whether to use alpha channel, depth buffer etc.).
56 CGE answers looking at Application.MainWindow properties. }
57 procedure CGEApp_ContextProperties(RedBits, GreenBits, BlueBits, AlphaBits, DepthBits, StencilBits, MultiSampling: PCInt); cdecl; export;
58
59 { Notifications from the external process about various events:
60 rendering context was opened, closed, resized etc. }
61 procedure CGEApp_Open(InitialWidth, InitialHeight, InitialStatusBarHeight, ADpi: cUInt32); cdecl;
62 procedure CGEApp_Close(QuitWhenLastWindowClosed: CBool); cdecl;
63 procedure CGEApp_Resize(uiViewWidth, uiViewHeight, uiStatusBarHeight: cUInt32); cdecl;
64 procedure CGEApp_Render; cdecl;
65 procedure CGEApp_SetLibraryCallbackProc(aProc: TLibraryCallbackProc); cdecl;
66 procedure CGEApp_Update; cdecl;
67 procedure CGEApp_MouseDown(X, Y: CInt32; bLeftBtn: cBool; FingerIndex: CInt32); cdecl;
68 procedure CGEApp_Motion(X, Y: CInt32; FingerIndex: CInt32); cdecl;
69 procedure CGEApp_MouseUp(X, Y: cInt32; bLeftBtn: cBool; FingerIndex: CInt32; TrackReleased: cBool); cdecl;
70 procedure CGEApp_KeyDown(eKey: CInt32); cdecl;
71 procedure CGEApp_KeyUp(eKey: CInt32); cdecl;
72 procedure CGEApp_JoystickAxis(JoystickIndex: CInt; X, Y: Single); cdecl;
73 procedure CGEApp_JoystickCount(JoystickCount: CInt); cdecl;
74 function CGEApp_HandleOpenUrl(szUrl: PChar): CInt32; cdecl;
75
76 const
77 { Constants used for callbacks.
78 Must be synchronized with constants in
79 ../library/castlelib_dynloader.pas and
80 ../library/castleengine.h }
81
82 // library callback codes
83 ecgelibNeedsDisplay = 0;
84 ecgelibSetMouseCursor = 1; // sends mouse cursor code in iParam1
85 ecgelibNavigationTypeChanged = 2; // sends ECgeNavigationType in iParam1 (see castleengine.h)
86 ecgelibSetMousePosition = 3; // sends x in iParam1 and y in iParam2
87 ecgelibWarning = 4; // sends message in szParam
88
89 // navigation types (ECgeNavigationType enum)
90 ecgenavWalk = 0;
91 ecgenavFly = 1;
92 ecgenavExamine = 2;
93 ecgenavTurntable = 3;
94 ecgenavNone = 4;
95
96 // mouse cursor codes
97 ecgecursorDefault = 0;
98 ecgecursorWait = 1;
99 ecgecursorHand = 2;
100 ecgecursorText = 3;
101 ecgecursorNone = 4;
102
103 var
104 LibraryCallbackProc: TLibraryCallbackProc;
105
106 {$endif read_global_interface}
107
108 {$ifdef read_implementation}
109
110 { TCastleWindow ---------------------------------------------------------- }
111
112 procedure TCastleWindow.CreateBackend;
113 begin
114 { Nothing to do. }
115 end;
116
117 procedure TCastleWindow.OpenBackend;
118 begin
119 { We just assume that we already have OpenGL context initialized.
120 Nothing to do. }
121 Application.OpenWindowsAdd(Self);
122 end;
123
124 procedure TCastleWindow.SwapBuffers;
125 begin
126 { The external code must take care of swapping buffers or doing glFlush. }
127 end;
128
129 procedure TCastleWindow.CloseBackend;
130 begin
131 { Nothing to do. }
132 end;
133
134 procedure TCastleWindow.Invalidate;
135 begin
136 if not Closed then Invalidated := true;
137 if Assigned(LibraryCallbackProc) then // tell the parent window to repaint
138 LibraryCallbackProc(ecgelibNeedsDisplay, 0, 0, nil);
139 end;
140
141 procedure TCastleWindow.BackendMakeCurrent;
142 begin
143 { Nothing to do, we assume external code makes sure this is always
144 the current OpenGL context. }
145 end;
146
147 procedure TCastleWindow.SetCursor(const Value: TMouseCursor);
148 var
149 CursorCode: cInt32;
150 begin
151 if FCursor <> Value then
152 begin
153 FCursor := Value;
154
155 if Assigned(LibraryCallbackProc) then
156 begin
157 // send to client
158 case Value of
159 mcNone, mcForceNone: CursorCode := ecgecursorNone;
160 mcWait: CursorCode := ecgecursorWait;
161 mcHand: CursorCode := ecgecursorHand;
162 mcText: CursorCode := ecgecursorText;
163 else CursorCode := ecgecursorDefault;
164 end;
165 LibraryCallbackProc(ecgelibSetMouseCursor, CursorCode, 0, nil);
166 end;
167 end;
168 end;
169
170 procedure TCastleWindow.SetCaption(const Part: TCaptionPart; const Value: String);
171 begin
172 FCaption[Part] := Value;
173 if not Closed then
174 { TODO: call LibraryCallbackProc to change caption to GetWholeCaption };
175 end;
176
177 procedure TCastleWindow.SetMousePosition(const Value: TVector2);
178 begin
179 if (not Closed) and Assigned(LibraryCallbackProc) then
180 begin
181
182 { Do not set Mouse.CursorPos to the same value, to make sure we don't cause
183 unnecessary OnMotion on some systems while actual MousePosition didn't change. }
184 if TVector2.PerfectlyEquals(Value, FMousePosition) then Exit;
185
186 LibraryCallbackProc(ecgelibSetMousePosition, Floor(Value[0]), Floor(Value[1]), nil);
187 end;
188 end;
189
190 procedure TCastleWindow.UpdateFullScreenBackend;
191 begin
192 { TODO: call LibraryCallbackProc to switch fullscreen mode }
193 FFullScreenBackend := FFullScreenWanted;
194 end;
195
196 procedure TCastleWindow.BackendInsideUpdate;
197 begin
198 end;
199
200 { LibraryXxx methods --------------------------------------------------------- }
201
202 procedure TCastleWindow.LibraryMouseWheel(const Scroll: Single; const Vertical: boolean);
203 begin
204 DoMouseWheel(Scroll, Vertical);
205 end;
206
207 { TCastleWindow menu ----------------------------------------------------
208 Does not do anything, this backend cannot manage the menu bar. }
209
210 procedure TCastleWindow.BackendMenuInitialize;
211 begin
212 end;
213
214 procedure TCastleWindow.BackendMenuFinalize;
215 begin
216 end;
217
218 procedure TCastleWindow.MenuUpdateCaption(Entry: TMenuEntryWithCaption);
219 begin
220 end;
221
222 procedure TCastleWindow.MenuUpdateEnabled(Entry: TMenuEntryWithCaption);
223 begin
224 end;
225
226 procedure TCastleWindow.MenuUpdateChecked(Entry: TMenuItemChecked);
227 begin
228 end;
229
230 function TCastleWindow.MenuUpdateCheckedFast: boolean;
231 begin
232 Result := false;
233 end;
234
235 procedure TCastleWindow.MenuInsert(const Parent: TMenu;
236 const ParentPosition: Integer; const Entry: TMenuEntry);
237 begin
238 end;
239
240 procedure TCastleWindow.MenuDelete(const Parent: TMenu;
241 const ParentPosition: Integer; const Entry: TMenuEntry);
242 begin
243 end;
244
245 function TCastleWindow.RedirectKeyDownToMenuClick: boolean;
246 begin
247 Result := true;
248 end;
249
250 { TCastleWindow dialogs ---------------------------------------------------------- }
251
252 { Methods below should make native-looking dialog boxes. }
253
254 function TCastleWindow.BackendFileDialog(const Title: String; var FileName: String;
255 OpenDialog: boolean; FileFilters: TFileFilterList): boolean;
256 begin
257 { TODO } Result := false;
258 end;
259
260 function TCastleWindow.ColorDialog(var Color: TCastleColor): Boolean;
261 begin
262 { TODO } Result := false;
263 end;
264
265 procedure TCastleWindow.MessageOK(const S: String; const MessageType: TWindowMessageType);
266 begin
267 { TODO }
268 end;
269
270 function TCastleWindow.MessageYesNo(const S: String;
271 const MessageType: TWindowMessageType): boolean;
272 begin
273 { TODO } Result := true;
274 end;
275
276 { TCastleApplication ---------------------------------------------------------- }
277
278 procedure TCastleApplication.CreateBackend;
279 begin
280 end;
281
282 procedure TCastleApplication.DestroyBackend;
283 begin
284 end;
285
286 function TCastleApplication.ProcessMessage(WaitForMessage, WaitToLimitFPS: boolean): boolean;
287 begin
288 Result := true;
289 end;
290
291 function TCastleApplication.ProcessAllMessages: boolean;
292 begin
293 Result := ProcessMessage(false, false);
294 end;
295
296 procedure TCastleApplication.Run;
297 begin
298 { for this CastleWindow backend, Run makes no sense, the external code
299 must perform event loop }
300 end;
301
302 procedure TCastleApplication.BackendTerminate;
303 begin
304 end;
305
306 function TCastleApplication.ScreenWidth: integer;
307 begin
308 if FScreenWidth = 0 then
309 WritelnWarning('Window', 'Application.ScreenWidth is queried before window is open, returns zero');
310 Result := FScreenWidth;
311 end;
312
313 function TCastleApplication.ScreenHeight: integer;
314 begin
315 if FScreenHeight = 0 then
316 WritelnWarning('Window', 'Application.ScreenHeight is queried before window is open, returns zero');
317 Result := FScreenHeight;
318 end;
319
320 function TCastleApplication.BackendName: String;
321 begin
322 Result := 'Library (Using Existing OpenGL Context)';
323 end;
324
325 procedure TCastleApplication.WarningToCallback(const Category, S: String);
326 var
327 sMsg: String;
328 szBuffer: PChar;
329 nBufSize: cardinal;
330 begin
331 if Assigned(LibraryCallbackProc) then
332 begin
333 sMsg := Category + ': ' + S;
334 nBufSize := Length(sMsg);
335 szBuffer := StrAlloc(nBufSize+1);
336 StrPLCopy(szBuffer, sMsg, nBufSize);
337 LibraryCallbackProc(4 {ecgelibWarning}, 0, 0, pcchar(szBuffer));
338 StrDispose(szBuffer);
339 end;
340 end;
341
342 {$define read_implementation}
343
344 procedure CheckMainWindow;
345 begin
346 if Application.MainWindow = nil then
347 raise Exception.Create('Application.MainWindow not set. One of the game units *must* set Application.MainWindow value in the "initialization" section');
348 end;
349
350 procedure CGEApp_Initialize(ApplicationConfigDirectory: PChar); cdecl;
351 begin
352 try
353 // use given ApplicationConfigDirectory
354 ApplicationConfigOverride := FilenameToUriSafe(InclPathDelim(ApplicationConfigDirectory));
355 WritelnLog('Library initialized, set application config directory to URL: %s', [ApplicationConfigOverride]);
356
357 ApplicationProperties.OnWarning.Add(@Application.WarningToCallback);
358 except
359 on E: TObject do WritelnWarning('CGEApp_Initialize', ExceptMessage(E));
360 end;
361 end;
362
363 procedure CGEApp_Finalize; cdecl;
364 begin
365 try
366 ApplicationProperties.OnWarning.Remove(@Application.WarningToCallback);
367 except
368 on E: TObject do WritelnWarning('CGEApp_Finalize', ExceptMessage(E));
369 end;
370 end;
371
372 procedure CGEApp_ContextProperties(RedBits, GreenBits, BlueBits, AlphaBits, DepthBits, StencilBits, MultiSampling: PCInt); cdecl;
373 var
374 W: TCastleWindow;
375 begin
376 try
377 CheckMainWindow;
378 W := Application.MainWindow;
379 RedBits ^ := Max(1, W.RedBits);
380 GreenBits ^ := Max(1, W.GreenBits);
381 BlueBits ^ := Max(1, W.BlueBits);
382 AlphaBits ^ := W.AlphaBits;
383 DepthBits ^ := W.DepthBits;
384 StencilBits ^ := W.StencilBits;
385 MultiSampling^ := W.MultiSampling;
386 WritelnLog('Rendering context will be initialized with these (minimal) buffer sizes: red %d, green %d, blue %d, alpha %d, depth %d, stencil %d, multi-sampling %d', [
387 RedBits ^,
388 GreenBits ^,
389 BlueBits ^,
390 AlphaBits ^,
391 DepthBits ^,
392 StencilBits ^,
393 MultiSampling^
394 ]);
395 except
396 on E: TObject do WritelnWarning('CGEApp_ContextProperties', ExceptMessage(E));
397 end;
398 end;
399
400 { Safe border (as 4D vector: top, right, bottom, left) with only
401 a status bar (top) non-zero.
402 This is passed from our Objective-C code on iOS to Pascal now. }
403 function SafeBorderWithStatusBar(const StatusBarHeight: Single): TVector4;
404 begin
405 Result := Vector4(StatusBarHeight, 0, 0, 0);
406 end;
407
408 procedure CGEApp_Open(InitialWidth, InitialHeight, InitialStatusBarHeight, ADpi: cUInt32); cdecl;
409 begin
410 try
411 CheckMainWindow;
412
413 { We have to set window sizes, and Application.Screen* sizes,
414 before calling MainWindow.Open. That's because EventOpen may already
415 look at these values.
416 Yes, other CastleWindow backends also make sure that these are set early. }
417
418 WritelnLog('Library received CGEApp_Open (window size: %d %d)', [InitialWidth, InitialHeight]);
419 Application.FScreenWidth := InitialWidth;
420 Application.FScreenHeight := InitialHeight;
421 //Window.FullScreen := true; // TODO: setting fullscreen should work like that 4 lines below. Also, should be default?
422 Application.MainWindow.FLeft := 0;
423 Application.MainWindow.FTop := 0;
424 Application.MainWindow.FWidth := InitialWidth;
425 Application.MainWindow.FHeight := InitialHeight;
426 Application.MainWindow.Container.Dpi := ADpi;
427 Application.MainWindow.Container.InternalSetSafeBorderScaled(
428 SafeBorderWithStatusBar(InitialStatusBarHeight));
429 Application.MainWindow.Open;
430 except
431 on E: TObject do WritelnWarning('CGEApp_Open', ExceptMessage(E));
432 end;
433 end;
434
435 procedure CGEApp_Close(QuitWhenLastWindowClosed: CBool); cdecl;
436 begin
437 try
438 CheckMainWindow;
439 Application.MainWindow.Close(QuitWhenLastWindowClosed);
440 except
441 on E: TObject do WritelnWarning('CGEApp_Close', ExceptMessage(E));
442 end;
443 end;
444
445 procedure CGEApp_Resize(uiViewWidth, uiViewHeight, uiStatusBarHeight: cUInt32); cdecl;
446 begin
447 try
448 CheckMainWindow;
449 Application.FScreenWidth := uiViewWidth;
450 Application.FScreenHeight := uiViewHeight;
451 Application.MainWindow.Container.InternalSetSafeBorderScaled(
452 SafeBorderWithStatusBar(uiStatusBarHeight));
453 Application.MainWindow.DoResize(uiViewWidth, uiViewHeight, false);
454 except
455 on E: TObject do WritelnWarning('CGEApp_Resize', ExceptMessage(E));
456 end;
457 end;
458
459 procedure CGEApp_Render; cdecl;
460 begin
461 try
462 CheckMainWindow;
463 Application.MainWindow.DoRender;
464 except
465 on E: TObject do WritelnWarning('CGEApp_Render', ExceptMessage(E));
466 end;
467 end;
468
469 procedure CGEApp_SetLibraryCallbackProc(aProc: TLibraryCallbackProc); cdecl;
470 begin
471 try
472 LibraryCallbackProc := aProc;
473 except
474 on E: TObject do WritelnWarning('CGEApp_SetLibraryCallbackProc', ExceptMessage(E));
475 end;
476 end;
477
478 procedure CGEApp_Update; cdecl;
479 begin
480 try
481 ApplicationProperties._Update;
482 Application.FOpenWindows.DoUpdate;
483 ApplicationProperties._UpdateEnd;
484 except
485 on E: TObject do WritelnWarning('CGEApp_Update', ExceptMessage(E));
486 end;
487 end;
488
489 procedure CGEApp_MouseDown(X, Y: CInt32; bLeftBtn: cBool; FingerIndex: CInt32); cdecl;
490 var
491 MyButton: TCastleMouseButton;
492 begin
493 try
494 CheckMainWindow;
495 if (bLeftBtn) then MyButton := buttonLeft else MyButton := buttonRight;
496 Application.MainWindow.DoMouseDown(Vector2(X, Y), MyButton, FingerIndex);
497 except
498 on E: TObject do WritelnWarning('CGEApp_MouseDown', ExceptMessage(E));
499 end;
500 end;
501
502 procedure CGEApp_Motion(X, Y: CInt32; FingerIndex: CInt32); cdecl;
503 var
504 W: TCastleWindow;
505 begin
506 try
507 CheckMainWindow;
508 { TODO: using MousePressed as Pressed is only OK without multi-touch. }
509 W := Application.MainWindow;
510 W.DoMotion(InputMotion(W.FTouches.FingerIndexPosition[FingerIndex],
511 Vector2(X, Y), W.MousePressed, FingerIndex));
512 except
513 on E: TObject do WritelnWarning('CGEApp_Motion', ExceptMessage(E));
514 end;
515 end;
516
517 procedure CGEApp_MouseUp(X, Y: cInt32; bLeftBtn: cBool;
518 FingerIndex: CInt32; TrackReleased: cBool); cdecl;
519 var
520 MyButton: TCastleMouseButton;
521 begin
522 try
523 CheckMainWindow;
524 if bLeftBtn then
525 MyButton := buttonLeft
526 else
527 MyButton := buttonRight;
528 Application.MainWindow.DoMouseUp(Vector2(X, Y), MyButton, FingerIndex, TrackReleased);
529 except
530 on E: TObject do WritelnWarning('CGEApp_MouseUp', ExceptMessage(E));
531 end;
532 end;
533
534 procedure CGEApp_KeyDown(eKey: CInt32); cdecl;
535
536 { Guess key as String, used e.g. by X3D KeySensor processing.
537 Without this, demo-models/navigation/navigation_info_bind.x3dv would not react to keys 1,2,3. }
538 function GuessKeyStringFromKey(const Key: TKey): String;
539 begin
540 Result := '';
541 case Key of
542 key0..key9: Result := Chr(Ord(Key));
543 keyA..keyZ: Result := LoCase(Chr(Ord(Key)));
544 end;
545 end;
546
547 var
548 Key: TKey;
549 begin
550 try
551 CheckMainWindow;
552 Key := TKey(eKey);
553 if Key <> keyNone then
554 begin
555 // Writeln(Format('Library received key down %s "%s"', [
556 // KeyToStr(Key),
557 // GuessKeyStringFromKey(Key)
558 // ]));
559 Application.MainWindow.DoKeyDown(Key, GuessKeyStringFromKey(Key));
560 end;
561 except
562 on E: TObject do WritelnWarning('CGEApp_KeyDown', ExceptMessage(E));
563 end;
564 end;
565
566 procedure CGEApp_KeyUp(eKey: CInt32); cdecl;
567 var
568 Key: TKey;
569 begin
570 try
571 CheckMainWindow;
572 Key := TKey(eKey);
573 if Key <> keyNone then
574 begin
575 // Writeln(Format('Library received key up %s', [
576 // KeyToStr(Key)
577 // ]));
578 Application.MainWindow.DoKeyUp(Key);
579 end;
580 except
581 on E: TObject do WritelnWarning('CGEApp_KeyUp', ExceptMessage(E));
582 end;
583 end;
584
585 procedure CGEApp_JoystickCount(JoystickCount: CInt); cdecl;
586 begin
587 try
588 CheckMainWindow;
589 Joysticks.InternalSetJoystickCount(JoystickCount);
590 except
591 on E: TObject do WritelnWarning('CGEApp_JoystickCount', ExceptMessage(E));
592 end;
593 end;
594
595 procedure CGEApp_JoystickAxis(JoystickIndex: CInt; X, Y: Single); cdecl;
596 begin
597 try
598 CheckMainWindow;
599 Joysticks.InternalSetJoystickAxis(JoystickIndex, Vector2(X, Y));
600 // if (X <> 0) or (Y <> 0) then
601 // WritelnLog('Joy axis %d %f %f %f', [JoystickIndex, X, Y]);
602 except
603 on E: TObject do WritelnWarning('CGEApp_JoystickAxis', ExceptMessage(E));
604 end;
605 end;
606
607 function CGEApp_HandleOpenUrl(szUrl: PChar): CInt32; cdecl;
608 begin
609 Result := 0;
610 try
611 CheckMainWindow;
612 Application.MainWindow.DoDropFiles([StrPas(PChar(szUrl))]);
613 Result := 1;
614 except
615 on E: TObject do WritelnWarning('CGEApp_HandleOpenUrl', ExceptMessage(E));
616 end;
617 end;
618
619 { TWindowContainer ----------------------------------------------------------- }
620
621 function TWindowContainer.SettingMousePositionCausesMotion: Boolean;
622 begin
623 Result := true;
624 end;
625
626 {$endif read_implementation}