Debian Codegraph @

src/delphi/castleinternaldelphiutils.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 { Utilities for Delphi UI that works the same in VCL and FMX. }
17 unit CastleInternalDelphiUtils;
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses UITypes, Classes,
24 CastleKeysMouse, CastleInternalContextBase;
25
26 { Convert mouse button (from VCL or FMX) to CGE. }
27 function MouseButtonToCastle(const MouseButton: TMouseButton;
28 out CastleMouseButton: TCastleMouseButton): Boolean;
29
30 { Convert key (from VCL or FMX) to CGE.
31 If not possible, returns keyNone. }
32 function KeyToCastle(const Key: Word; const Shift: TShiftState) : TKey;
33
34 { Convert key from CGE to VCL or FMX.
35 If not possible, sets KeyCode to 0 and Shift to []. }
36 procedure KeyFromCastle(const Key: TKey; const KeyString: String;
37 const Modifiers: TModifierKeys;
38 out KeyCode: Word; out Shift: TShiftState);
39
40 { Convert FMX information about keys (received on key up or key down)
41 to CGE. }
42 procedure FmxKeysToCastle(
43 const Key: Word; const KeyChar: WideChar; const Shift: TShiftState;
44 out CastleKey: TKey; out CastleKeyString: String);
45
46 { Hacky and quick conversion of TKey to String representation.
47 Knows additionally modifiers state from Shift.
48
49 Using this should be avoided: it's not localized (will not generate
50 your language-specific chars), not complete, ignores caps lock,
51 assumes specific keyboard layout (e.g. keyboard with = and + on
52 the same physical key).
53 IOW, it does a bunch of things it shouldn't do -- because it does not have
54 necessary knowledge about your keyboard layout and state.
55 Querying the underlying system (like WinAPI or VCL) for proper message
56 is better, though sometimes complicated. }
57 function SimpleKeyToString(const Key: TKey; const Shift: TShiftState): String;
58
59 { Convert cursor from CGE (TMouseCursor) to VCL or FMX (TCursor). }
60 function CursorFromCastle(const Cursor: TMouseCursor): TCursor;
61
62 type
63 TDesignTimeProjectPathEvent = function: String;
64
65 var
66 OnGetDesignTimeProjectPath: TDesignTimeProjectPathEvent;
67
68 { Best TGLContext descendant for this platform. }
69 function ContextCreateBestInstance: TGLContext;
70
71 implementation
72
73 uses
74 {$ifdef MSWINDOWS} CastleInternalContextWgl, {$endif}
75 {$ifdef LINUX} CastleInternalContextEgl, {$endif}
76 CastleStringUtils, CastleLog;
77
78 function MouseButtonToCastle(const MouseButton: TMouseButton;
79 out CastleMouseButton: TCastleMouseButton): Boolean;
80 begin
81 Result := true;
82 case MouseButton of
83 TMouseButton.mbLeft : CastleMouseButton := buttonLeft;
84 TMouseButton.mbRight : CastleMouseButton := buttonRight;
85 TMouseButton.mbMiddle: CastleMouseButton := buttonMiddle;
86 {$ifndef COMPILER_CASE_ANALYSIS}
87 else Result := false;
88 {$endif}
89 end;
90 end;
91
92 function KeyToCastle(const Key: Word; const Shift: TShiftState): TKey;
93 begin
94 { Note: vkXxx constants from FMX seem to be deliberately declared so that
95 they match Windows VK_xxx constants, used also by VCL.
96 So this routine should be good to convert both VCL and FMX keys. }
97
98 Result := keyNone;
99 case Key of
100 vkBack: Result := keyBackSpace;
101 vkTab: Result := keyTab;
102 vkReturn: Result := keyEnter;
103 vkShift: Result := keyShift;
104 vkControl: Result := keyCtrl;
105 vkMenu: Result := keyAlt;
106 vkEscape: Result := keyEscape;
107 vkSpace: Result := keySpace;
108 vkPrior: Result := keyPageUp;
109 vkNext: Result := keyPageDown;
110 vkEnd: Result := keyEnd;
111 vkHome: Result := keyHome;
112 vkLeft: Result := keyArrowLeft;
113 vkUp: Result := keyArrowUp;
114 vkRight: Result := keyArrowRight;
115 vkDown: Result := keyArrowDown;
116 vkInsert: Result := keyInsert;
117 vkDelete: Result := keyDelete;
118 vkAdd: Result := keyNumpadPlus;
119 vkSubtract: Result := keyNumpadMinus;
120 vkSnapshot: Result := keyPrintScreen;
121 vkNumlock: Result := keyNumLock;
122 vkScroll: Result := keyScrollLock;
123 vkCapital: Result := keyCapsLock;
124 vkPause: Result := keyPause;
125 vkComma: Result := keyComma;
126 vkPeriod: Result := keyPeriod;
127 vkNumpad0: Result := keyNumpad0;
128 vkNumpad1: Result := keyNumpad1;
129 vkNumpad2: Result := keyNumpad2;
130 vkNumpad3: Result := keyNumpad3;
131 vkNumpad4: Result := keyNumpad4;
132 vkNumpad5: Result := keyNumpad5;
133 vkNumpad6: Result := keyNumpad6;
134 vkNumpad7: Result := keyNumpad7;
135 vkNumpad8: Result := keyNumpad8;
136 vkNumpad9: Result := keyNumpad9;
137 vkClear: Result := keyNumpadBegin;
138 vkMultiply: Result := keyNumpadMultiply;
139 vkDivide: Result := keyNumpadDivide;
140 vkMinus: Result := keyMinus;
141 vkEqual:
142 if ssShift in Shift then
143 Result := keyPlus
144 else
145 Result := keyEqual;
146 vk0..vk9:
147 begin
148 Result := TKey(Ord(key0) + Ord(Key) - vk0);
149 end;
150 vkA .. vkZ:
151 begin
152 Result := TKey(Ord(keyA) + Ord(Key) - vkA);
153 end;
154 vkF1 .. vkF12:
155 Result := TKey(Ord(keyF1) + Ord(Key) - vkF1);
156 end;
157 end;
158
159 procedure KeyFromCastle(const Key: TKey; const KeyString: String;
160 const Modifiers: TModifierKeys;
161 out KeyCode: Word; out Shift: TShiftState);
162 const
163 { Ctrl key on most systems, Command key on macOS. }
164 ssCtrlOrCommand = {$ifdef DARWIN} ssMeta {$else} ssCtrl {$endif};
165 var
166 KeyChar: Char;
167 begin
168 Shift := [];
169 KeyCode := 0;
170 case Key of
171 keyBackSpace: KeyCode := vkBack;
172 keyTab: KeyCode := vkTab;
173 keyEnter: KeyCode := vkReturn;
174 keyShift: KeyCode := vkShift;
175 keyCtrl: KeyCode := vkControl;
176 keyAlt: KeyCode := vkMenu;
177 keyEscape: KeyCode := vkEscape;
178 keySpace: KeyCode := vkSpace;
179 keyPageUp: KeyCode := vkPrior;
180 keyPageDown: KeyCode := vkNext;
181 keyEnd: KeyCode := vkEnd;
182 keyHome: KeyCode := vkHome;
183 keyArrowLeft: KeyCode := vkLeft;
184 keyArrowUp: KeyCode := vkUp;
185 keyArrowRight: KeyCode := vkRight;
186 keyArrowDown: KeyCode := vkDown;
187 keyInsert: KeyCode := vkInsert;
188 keyDelete: KeyCode := vkDelete;
189 keyNumpadPlus: KeyCode := vkAdd;
190 keyNumpadMinus: KeyCode := vkSubtract;
191 keyPrintScreen: KeyCode := vkSnapshot;
192 keyNumLock: KeyCode := vkNumlock;
193 keyScrollLock: KeyCode := vkScroll;
194 keyCapsLock: KeyCode := vkCapital;
195 keyPause: KeyCode := vkPause;
196 keyComma: KeyCode := vkComma;
197 keyPeriod: KeyCode := vkPeriod;
198 keyNumpad0: KeyCode := vkNumpad0;
199 keyNumpad1: KeyCode := vkNumpad1;
200 keyNumpad2: KeyCode := vkNumpad2;
201 keyNumpad3: KeyCode := vkNumpad3;
202 keyNumpad4: KeyCode := vkNumpad4;
203 keyNumpad5: KeyCode := vkNumpad5;
204 keyNumpad6: KeyCode := vkNumpad6;
205 keyNumpad7: KeyCode := vkNumpad7;
206 keyNumpad8: KeyCode := vkNumpad8;
207 keyNumpad9: KeyCode := vkNumpad9;
208 keyNumpadBegin: KeyCode := vkClear;
209 keyNumpadMultiply: KeyCode := vkMultiply;
210 keyNumpadDivide: KeyCode := vkDivide;
211 keyMinus: KeyCode := vkMinus;
212 keyEqual: KeyCode := vkEqual;
213 { TKey ranges }
214 key0 ..key9 : KeyCode := vk0 + Ord(Key) - Ord(key0);
215 keyA ..keyZ : KeyCode := vkA + Ord(Key) - Ord(keyA);
216 keyF1..keyF12: KeyCode := vkF1 + Ord(Key) - Ord(keyF1);
217
218 else
219 if Length(KeyString) = 1 then
220 begin
221 KeyChar := KeyString[1];
222 case KeyChar of
223 { follow TMenuItem.Key docs: when Key is keyNone, only KeyChar indicates
224 CharBackSpace / CharTab / CharEnter, convert them to Ctrl+xxx shortcuts }
225 //CharBackSpace: KeyCode := vkBack;
226 //CharTab: KeyCode := vkTab;
227 //CharEnter: KeyCode := vkReturn;
228 CharEscape: KeyCode := vkEscape;
229 ' ': KeyCode := vkSpace;
230 CharDelete: KeyCode := vkDelete;
231 '+': KeyCode := vkAdd;
232 '-': KeyCode := vkSubtract;
233 ',': KeyCode := vkComma;
234 '.': KeyCode := vkPeriod;
235 '*': KeyCode := vkMultiply;
236 '/': KeyCode := vkDivide;
237 '=': KeyCode := vkEqual;
238
239 { Char ranges }
240 '0' .. '9' : KeyCode := Ord(KeyChar);
241 { for latter: uppercase letters are VK_xxx codes }
242 'A' .. 'Z' : begin KeyCode := Ord(KeyChar); Shift := [ssShift]; end;
243 'a' .. 'z' : begin KeyCode := Ord(UpCase(KeyChar)); end;
244 CtrlA .. CtrlZ:
245 begin
246 KeyCode := vkA + Ord(KeyChar) - Ord(CtrlA);
247 Shift := [ssCtrlOrCommand];
248 end;
249 end;
250 end;
251 end;
252
253 if mkShift in Modifiers then
254 Shift := Shift + [ssShift];
255 if mkCtrl in Modifiers then
256 Shift := Shift + [ssCtrlOrCommand];
257 if mkAlt in Modifiers then
258 Shift := Shift + [ssAlt];
259 end;
260
261 function SimpleKeyToString(const Key: TKey; const Shift: TShiftState): String;
262 const
263 Map: array [TKey] of String = (
264 '',
265 '',
266 '',
267 '',
268 '',
269 '',
270 '''',
271 ';',
272 CharBackSpace,
273 CharTab,
274 '/',
275 '`',
276 '-',
277 CharEnter,
278 '=',
279 '',
280 '',
281 '',
282 '',
283 '+',
284 '',
285 '',
286 '',
287 '',
288 '',
289 '',
290 '',
291 CharEscape,
292 '',
293 '',
294 '',
295 '',
296 ' ',
297 '',
298 '',
299 '',
300 '',
301 '',
302 '',
303 '',
304 '',
305 '',
306 '',
307 '',
308 '',
309 '',
310 '',
311 '',
312 '0',
313 '1',
314 '2',
315 '3',
316 '4',
317 '5',
318 '6',
319 '7',
320 '8',
321 '9',
322 '',
323 '',
324 '',
325 '',
326 '',
327 '',
328 '',
329 'a',
330 'b',
331 'c',
332 'd',
333 'e',
334 'f',
335 'g',
336 'h',
337 'i',
338 'j',
339 'k',
340 'l',
341 'm',
342 'n',
343 'o',
344 'p',
345 'q',
346 'r',
347 's',
348 't',
349 'u',
350 'v',
351 'w',
352 'x',
353 'y',
354 'z',
355 '[',
356 '',
357 ']',
358 '',
359 '',
360 '',
361 '',
362 '',
363 '',
364 '',
365 '',
366 '',
367 '',
368 '',
369 '',
370 '',
371 '+',
372 '',
373 '-',
374 '',
375 '',
376 '',
377 '',
378 '',
379 '',
380 '',
381 '',
382 '',
383 '',
384 '',
385 '',
386 '',
387 '',
388 '',
389 '',
390 '',
391 '',
392 '',
393 '',
394 '',
395 '',
396 '',
397 '',
398 '',
399 '',
400 '',
401 '',
402 '',
403 '',
404 '0',
405 '1',
406 '2',
407 '3',
408 '4',
409 '5',
410 '6',
411 '7',
412 '8',
413 '9',
414 '',
415 '',
416 '',
417 '',
418 '',
419 '',
420 '',
421 '',
422 '',
423 '',
424 '',
425 '',
426 '*',
427 '/',
428 '',
429 '',
430 '',
431 '',
432 '',
433 '',
434 '',
435 '',
436 '',
437 '',
438 '',
439 '',
440 '',
441 '',
442 '',
443 '',
444 '',
445 '',
446 '',
447 '',
448 '',
449 '',
450 '',
451 '',
452 ',',
453 '',
454 '.',
455 ''
456 );
457 begin
458 Result := Map[Key];
459
460 if (ssShift in Shift) and (Length(Result) = 1) then
461 begin
462 if Key in [keyA..keyZ] then
463 Result := UpCase(Result[1])
464 else
465 if Key = keyEqual then
466 Result := '+';
467 end;
468 end;
469
470 {$ifdef LINUX}
471 { Hacky way to convert key as WideChar into a TKey.
472
473 Used only to workaround Delphi FMXLinux: FMXLinux never sends a key code
474 (as Word, like vkA, vkSpace etc.) to OnKeyDown.
475 It only sends the key as WideChar, and the key code is always 0.
476
477 Confirmed with almost-blank FMX application (not using CGE), compiled for Linux.
478 TForm.OnKeyDown, TEdit.OnKeyDown report Key = 0, and KeyChar = 'a' when
479 you press key A.
480 Weirdly, the non-zero Key *is* passed properly by FMXLinux for things that have
481 no KeyChar representation, like arrow keys.
482 Tested that doing
483 "g_signal_connect(GLAreaGtk, 'key_press_event', @signal_key_press_event,
484 LinuxHandle.NativeDrawingArea);" inside
485 castleinternalfmxutils_linux.inc doesn't help (makes sense, since almost-blank
486 FMX application, not using CGE, also shows this problem).
487
488 So we workaround it in TCastleControl.KeyDown and TCastleWindow.OpenGLControlKeyDown,
489 so that CGE applications don't see this problem. }
490
491 function SimpleWideCharToKey(const KeyChar: WideChar): TKey;
492 begin
493 Result := keyNone;
494 case KeyChar of
495 CharBackSpace: Result := keyBackSpace;
496 CharTab: Result := keyTab;
497 CharEnter: Result := keyEnter;
498 CharEscape: Result := keyEscape;
499 ' ': Result := keySpace;
500 ',': Result := keyComma;
501 '.': Result := keyPeriod;
502 '*': Result := keyNumpadMultiply;
503 '/': Result := keyNumpadDivide;
504 '-': Result := keyMinus;
505 '=': Result := KeyEqual;
506 '+': Result := keyPlus;
507 '0'..'9':
508 begin
509 Result := TKey(Ord(key0) + Ord(KeyChar) - Ord('0'));
510 end;
511 'A'..'Z':
512 begin
513 Result := TKey(Ord(keyA) + Ord(KeyChar) - Ord('A'));
514 end;
515 'a'..'z':
516 begin
517 Result := TKey(Ord(keyA) + Ord(KeyChar) - Ord('a'));
518 end;
519 end;
520 end;
521
522 { Workaround 2 FMXLinux bugs:
523
524 - FMXLinux always passes Key=0 for regular
525 keys that represent printable characters, like 'a' or space,
526 see SimpleWideCharToKey comments.
527
528 - On Enter, it passes vkReturn as Key, but KeyChar = #0.
529 We want to pass CharEnter.
530 Same thing for a few other special chars.
531 }
532 procedure FmxLinuxFixKeys(
533 const FmxKey: Word; const FmxKeyChar: WideChar; const FmxShift: TShiftState;
534 var CastleKey: TKey; var CastleKeyString: String);
535 begin
536 if (FmxKey = 0) and
537 (FmxKeyChar <> #0) and
538 (CastleKey = keyNone) then
539 CastleKey := SimpleWideCharToKey(FmxKeyChar);
540
541 if CastleKeyString = '' then
542 case CastleKey of
543 keyEnter : CastleKeyString := CharEnter;
544 keyEscape: CastleKeyString := CharEscape;
545 keyTab: CastleKeyString := CharTab;
546 keyBackSpace: CastleKeyString := CharBackSpace;
547 keyA..keyZ:
548 begin
549 if ssCtrl in FmxShift then
550 CastleKeyString := Chr(Ord(CtrlA) + Ord(CastleKey) - Ord(keyA));
551 end;
552 else ;
553 end;
554 end;
555 {$endif}
556
557 procedure FmxKeysToCastle(
558 const Key: Word; const KeyChar: WideChar; const Shift: TShiftState;
559 out CastleKey: TKey; out CastleKeyString: String);
560 begin
561 // calculate CastleKey
562 CastleKey := KeyToCastle(Key, Shift);
563
564 // calculate CastleKeyString
565 if KeyChar <> #0 then
566 CastleKeyString := KeyChar
567 else
568 CastleKeyString := '';
569
570 // apply FMXLinux fixes
571 {$ifdef LINUX}
572 FmxLinuxFixKeys(Key, KeyChar, Shift, CastleKey, CastleKeyString);
573 {$endif}
574
575 // Debug what happens
576 {
577 WritelnLog('FMX Key', 'Key=%d KeyChar=%s CastleKey=%s CastleKeyString=%s', [
578 Key,
579 SReadableForm(KeyChar),
580 KeyToStr(CastleKey),
581 SReadableForm(CastleKeyString)
582 ]);
583 }
584 end;
585
586 function CursorFromCastle(const Cursor: TMouseCursor): TCursor;
587 const
588 Map: array [TMouseCursor] of TCursor =
589 ( crDefault, //< mcDefault
590 crNone, //< mcNone
591 crNone, //< mcForceNone
592
593 crArrow, //< mcStandard
594 crHourGlass, //< mcWait
595 crIBeam, //< mcText
596 crHandPoint, //< mcHand
597 crSizeNS, //< mcResizeVertical
598 crSizeWE, //< mcResizeHorizontal
599 crSizeNS { nothing better in Delphi }, //< mcResizeTopLeft
600 crSizeNS { nothing better in Delphi }, //< mcResizeTop
601 crSizeNS { nothing better in Delphi }, //< mcResizeTopRight
602 crSizeWE { nothing better in Delphi }, //< mcResizeLeft
603 crSizeWE { nothing better in Delphi }, //< mcResizeRight
604 crSizeNS { nothing better in Delphi }, //< mcResizeBottomLeft
605 crSizeNS { nothing better in Delphi }, //< mcResizeBottom
606 crSizeNS { nothing better in Delphi } //< mcResizeBottomRight
607 );
608 begin
609 Result := Map[Cursor];
610 end;
611
612 function ContextCreateBestInstance: TGLContext;
613 begin
614 Result :=
615 {$if defined(MSWINDOWS)} TGLContextWgl.Create
616 {$elseif defined(LINUX)} TGLContextEgl.Create
617 {$else}
618 {$message fatal 'Define how to create OpenGL context for this platform.'}
619 {$endif}
620 ;
621 end;
622
623 end.