| 1 |
{ |
| 2 |
Copyright 2008-2022 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 cooperation between LCL and "Castle Game Engine". } |
| 17 |
unit CastleLCLUtils; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses Dialogs, Classes, Controls, LCLType, Graphics, EditBtn, |
| 24 |
CastleFileFilters, CastleKeysMouse, CastleVectors; |
| 25 |
|
| 26 |
{ Convert file filters into LCL Dialog.Filter, Dialog.FilterIndex. |
| 27 |
Suitable for both open and save dialogs (TOpenDialog, TSaveDialog |
| 28 |
both descend from TFileDialog). |
| 29 |
|
| 30 |
Input filters are either given as a string FileFilters |
| 31 |
(encoded just like for TFileFilterList.AddFiltersFromString), |
| 32 |
or as TFileFilterList instance. |
| 33 |
|
| 34 |
Output filters are either written to LCLFilter, LCLFilterIndex |
| 35 |
variables, or set appropriate properties of given Dialog instance. |
| 36 |
|
| 37 |
When AllFields is false, then filters starting with "All " in the name, |
| 38 |
like "All files", "All images", are not included in the output. |
| 39 |
|
| 40 |
@groupBegin } |
| 41 |
procedure FileFiltersToDialog(const FileFilters: string; |
| 42 |
const Dialog: TFileDialog; const AllFields: boolean = true); |
| 43 |
procedure FileFiltersToDialog(const FileFilters: string; |
| 44 |
const Edit: TFileNameEdit; const AllFields: boolean = true); |
| 45 |
procedure FileFiltersToDialog(FFList: TFileFilterList; |
| 46 |
const Dialog: TFileDialog; const AllFields: boolean = true); |
| 47 |
|
| 48 |
procedure FileFiltersToDialog(const FileFilters: string; |
| 49 |
out LCLFilter: string; out LCLFilterIndex: Integer; const AllFields: boolean = true); |
| 50 |
deprecated 'use TFileFilterList.LclFmxFiltersFromString'; |
| 51 |
|
| 52 |
procedure FileFiltersToDialog(FFList: TFileFilterList; |
| 53 |
out LCLFilter: string; out LCLFilterIndex: Integer; const AllFields: boolean = true); |
| 54 |
deprecated 'use TFileFilterList.LclFmxFilters'; |
| 55 |
{ @groupEnd } |
| 56 |
|
| 57 |
{ Make each '&' inside string '&&', this way the string will not contain |
| 58 |
special '&x' sequences when used as a TMenuItem.Caption and such. } |
| 59 |
function SQuoteLCLCaption(const S: string): string; |
| 60 |
|
| 61 |
{ Deprecated names, use the identifiers without "Open" in new code. |
| 62 |
@deprecated |
| 63 |
@groupBegin } |
| 64 |
procedure FileFiltersToOpenDialog(const FileFilters: string; |
| 65 |
Dialog: TFileDialog); deprecated; |
| 66 |
procedure FileFiltersToOpenDialog(const FileFilters: string; |
| 67 |
out LCLFilter: string; out LCLFilterIndex: Integer); deprecated; |
| 68 |
procedure FileFiltersToOpenDialog(FFList: TFileFilterList; |
| 69 |
out LCLFilter: string; out LCLFilterIndex: Integer); deprecated; |
| 70 |
{ @groupEnd } |
| 71 |
|
| 72 |
{ Convert Key (Lazarus key code) to Castle Game Engine TKey. |
| 73 |
Returns keyNone if not possible. } |
| 74 |
function KeyToCastle(const Key: Word; const Shift: TShiftState): TKey; |
| 75 |
|
| 76 |
{ Convert TKey and/or character code into Lazarus key code (VK_xxx) |
| 77 |
and shift state. |
| 78 |
Sets KeyCode to VK_UNKNOWN (zero) when conversion not possible |
| 79 |
(or when Key = keyNone and KeyString = ''). |
| 80 |
|
| 81 |
@groupBegin } |
| 82 |
procedure KeyFromCastle(const Key: TKey; const KeyString: String; |
| 83 |
out KeyCode: Word; out Shift: TShiftState); |
| 84 |
procedure KeyFromCastle(const Key: TKey; KeyString: String; |
| 85 |
const Modifiers: TModifierKeys; |
| 86 |
out KeyCode: Word; out Shift: TShiftState); |
| 87 |
{ @groupEnd } |
| 88 |
|
| 89 |
{ Convert Lazarus TMouseButton value to Castle Game Engine |
| 90 |
TCastleMouseButton. } |
| 91 |
function MouseButtonToCastle( |
| 92 |
const MouseButton: TMouseButton; |
| 93 |
out MyMouseButton: TCastleMouseButton): boolean; |
| 94 |
|
| 95 |
{ Convert CGE to LCL cursor type. } |
| 96 |
function CursorFromCastle(const Cursor: TMouseCursor): TCursor; |
| 97 |
|
| 98 |
{ Not necessary. |
| 99 |
Converts between Lazarus String encoding for filenames (which is UTF-8) |
| 100 |
and CGE encoding for filenames (which is also UTF-8). |
| 101 |
So this in practice does nothing. } |
| 102 |
function FilenameToUriSafeUTF8(const FileName: string): string; |
| 103 |
function UriToFilenameSafeUTF8(const Url: String): string; |
| 104 |
|
| 105 |
{ Convert LCL color values to CGE colors (vectors). } |
| 106 |
function ColorToVector3(const Color: TColor): TVector3; |
| 107 |
function ColorToVector3Byte(const Color: TColor): TVector3Byte; |
| 108 |
|
| 109 |
type |
| 110 |
TControlInputPressReleaseEvent = procedure (Sender: TObject; const Event: TInputPressRelease) of object; |
| 111 |
TControlInputMotionEvent = procedure (Sender: TObject; const Event: TInputMotion) of object; |
| 112 |
|
| 113 |
{ Convert LCL OnKeyDown and OnUTF8KeyPress into a single CGE event OnPress. |
| 114 |
|
| 115 |
In VCL and LCL KeyPress (or UTF8KeyPress in LCL) and KeyDown |
| 116 |
are separate events. While in CGE they are one event, |
| 117 |
and passed as one event to TCastleUserInterface.KeyDown, |
| 118 |
because this is more comfortable to process (as it corresponds |
| 119 |
to one user action, and allows configurable TInputShortcut to work). |
| 120 |
} |
| 121 |
TLCLKeyPressHandler = class |
| 122 |
private |
| 123 |
FOnPress: TControlInputPressReleaseEvent; |
| 124 |
FUnfinishedKeyDown: Boolean; |
| 125 |
FUnfinishedKeyDownKey: Word; |
| 126 |
FUnfinishedKeyDownShift: TShiftState; |
| 127 |
FUnfinishedKeyPress: Boolean; |
| 128 |
FUnfinishedKeyPressKey: TUTF8Char; |
| 129 |
public |
| 130 |
{ Call these events when corresponding Lazarus event |
| 131 |
(OnKeyDown, OnUTF8KeyPress) occurs. |
| 132 |
|
| 133 |
They may call OnPress in turn. } |
| 134 |
procedure KeyDown(const Key: Word; const Shift: TShiftState); |
| 135 |
procedure UTF8KeyPress(const UTF8Key: TUTF8Char); |
| 136 |
|
| 137 |
{ Call this when Lazarus OnKeyUp event occurs. |
| 138 |
|
| 139 |
It may call OnPress in return. |
| 140 |
Receiving "key up" event from Lazarus may indicate that we have |
| 141 |
to do OnPress now. |
| 142 |
|
| 143 |
This matters for keys without corresponding String/Char representation |
| 144 |
(thus, without UTF8KeyPress call), like keyArrowLeft. |
| 145 |
If we have buffered to "send the OnPress for this key", |
| 146 |
we need to send it now, before the OnRelease for the same key is generated. |
| 147 |
Otherwise the KeyUp would cause OnRelease of the arrow key, |
| 148 |
and later Flush call would send OnPress for the same key. } |
| 149 |
procedure BeforeKeyUp(const Key: Word; const Shift: TShiftState); |
| 150 |
|
| 151 |
{ If some keypress is half-finished, report it now. |
| 152 |
This should be called before e.g. Update event, to report |
| 153 |
events that result in OnKeyDown but not OnUTF8KeyPress, |
| 154 |
or OnUTF8KeyPress but not OnKeyDown. } |
| 155 |
procedure Flush; |
| 156 |
|
| 157 |
{ Called when we collect enough information to make a CGE press event. } |
| 158 |
property OnPress: TControlInputPressReleaseEvent read FOnPress write FOnPress; |
| 159 |
end; |
| 160 |
|
| 161 |
implementation |
| 162 |
|
| 163 |
uses SysUtils, FileUtil, LazUTF8, LCLProc, |
| 164 |
CastleClassUtils, CastleStringUtils, CastleUriUtils, CastleLog; |
| 165 |
|
| 166 |
procedure FileFiltersToDialog(const FileFilters: string; |
| 167 |
const Dialog: TFileDialog; const AllFields: boolean); |
| 168 |
var |
| 169 |
LCLFilter: string; |
| 170 |
LCLFilterIndex: Integer; |
| 171 |
begin |
| 172 |
TFileFilterList.LclFmxFiltersFromString(FileFilters, LCLFilter, LCLFilterIndex, AllFields); |
| 173 |
Dialog.Filter := LCLFilter; |
| 174 |
Dialog.FilterIndex := LCLFilterIndex; |
| 175 |
end; |
| 176 |
|
| 177 |
procedure FileFiltersToDialog(const FileFilters: string; |
| 178 |
const Edit: TFileNameEdit; const AllFields: boolean); |
| 179 |
var |
| 180 |
LCLFilter: string; |
| 181 |
LCLFilterIndex: Integer; |
| 182 |
begin |
| 183 |
TFileFilterList.LclFmxFiltersFromString(FileFilters, LCLFilter, LCLFilterIndex, AllFields); |
| 184 |
Edit.Filter := LCLFilter; |
| 185 |
Edit.FilterIndex := LCLFilterIndex; |
| 186 |
end; |
| 187 |
|
| 188 |
procedure FileFiltersToDialog(FFList: TFileFilterList; |
| 189 |
const Dialog: TFileDialog; const AllFields: boolean); |
| 190 |
var |
| 191 |
LCLFilter: string; |
| 192 |
LCLFilterIndex: Integer; |
| 193 |
begin |
| 194 |
FFList.LclFmxFilters(LCLFilter, LCLFilterIndex, AllFields); |
| 195 |
Dialog.Filter := LCLFilter; |
| 196 |
Dialog.FilterIndex := LCLFilterIndex; |
| 197 |
end; |
| 198 |
|
| 199 |
procedure FileFiltersToDialog(const FileFilters: string; |
| 200 |
out LCLFilter: string; out LCLFilterIndex: Integer; const AllFields: boolean = true); |
| 201 |
begin |
| 202 |
TFileFilterList.LclFmxFiltersFromString(FileFilters, |
| 203 |
LCLFilter, LCLFilterIndex, AllFields); |
| 204 |
end; |
| 205 |
|
| 206 |
procedure FileFiltersToDialog(FFList: TFileFilterList; |
| 207 |
out LCLFilter: string; out LCLFilterIndex: Integer; const AllFields: boolean = true); |
| 208 |
begin |
| 209 |
FFList.LclFmxFilters( |
| 210 |
LCLFilter, LCLFilterIndex, AllFields); |
| 211 |
end; |
| 212 |
|
| 213 |
function SQuoteLCLCaption(const S: string): string; |
| 214 |
begin |
| 215 |
Result := StringReplace(S, '&', '&&', [rfReplaceAll]); |
| 216 |
end; |
| 217 |
|
| 218 |
{ FileFiltersToOpenDialog are deprecated, just call versions without "Open". } |
| 219 |
procedure FileFiltersToOpenDialog(const FileFilters: string; |
| 220 |
Dialog: TFileDialog); |
| 221 |
begin |
| 222 |
FileFiltersToDialog(FileFilters, Dialog); |
| 223 |
end; |
| 224 |
|
| 225 |
procedure FileFiltersToOpenDialog(const FileFilters: string; |
| 226 |
out LCLFilter: string; out LCLFilterIndex: Integer); |
| 227 |
begin |
| 228 |
TFileFilterList.LclFmxFiltersFromString(FileFilters, LCLFilter, LCLFilterIndex); |
| 229 |
end; |
| 230 |
|
| 231 |
procedure FileFiltersToOpenDialog(FFList: TFileFilterList; |
| 232 |
out LCLFilter: string; out LCLFilterIndex: Integer); |
| 233 |
begin |
| 234 |
FFList.LclFmxFilters(LCLFilter, LCLFilterIndex); |
| 235 |
end; |
| 236 |
|
| 237 |
const |
| 238 |
{ Ctrl key on most systems, Command key on macOS. } |
| 239 |
ssCtrlOrCommand = {$ifdef DARWIN} ssMeta {$else} ssCtrl {$endif}; |
| 240 |
|
| 241 |
function KeyToCastle(const Key: Word; const Shift: TShiftState): TKey; |
| 242 |
begin |
| 243 |
Result := keyNone; |
| 244 |
case Key of |
| 245 |
VK_BACK: Result := keyBackSpace; |
| 246 |
VK_TAB: Result := keyTab; |
| 247 |
VK_RETURN: Result := keyEnter; |
| 248 |
VK_SHIFT: Result := keyShift; |
| 249 |
VK_CONTROL: Result := keyCtrl; |
| 250 |
VK_MENU: Result := keyAlt; |
| 251 |
VK_ESCAPE: Result := keyEscape; |
| 252 |
VK_SPACE: Result := keySpace; |
| 253 |
VK_PRIOR: Result := keyPageUp; |
| 254 |
VK_NEXT: Result := keyPageDown; |
| 255 |
VK_END: Result := keyEnd; |
| 256 |
VK_HOME: Result := keyHome; |
| 257 |
VK_LEFT: Result := keyArrowLeft; |
| 258 |
VK_UP: Result := keyArrowUp; |
| 259 |
VK_RIGHT: Result := keyArrowRight; |
| 260 |
VK_DOWN: Result := keyArrowDown; |
| 261 |
VK_INSERT: Result := keyInsert; |
| 262 |
VK_DELETE: Result := keyDelete; |
| 263 |
VK_ADD: Result := keyNumpadPlus; |
| 264 |
VK_SUBTRACT: Result := keyNumpadMinus; |
| 265 |
VK_SNAPSHOT: Result := keyPrintScreen; |
| 266 |
VK_NUMLOCK: Result := keyNumLock; |
| 267 |
VK_SCROLL: Result := keyScrollLock; |
| 268 |
VK_CAPITAL: Result := keyCapsLock; |
| 269 |
VK_PAUSE: Result := keyPause; |
| 270 |
VK_OEM_COMMA: Result := keyComma; |
| 271 |
VK_OEM_PERIOD: Result := keyPeriod; |
| 272 |
VK_NUMPAD0: Result := keyNumpad0; |
| 273 |
VK_NUMPAD1: Result := keyNumpad1; |
| 274 |
VK_NUMPAD2: Result := keyNumpad2; |
| 275 |
VK_NUMPAD3: Result := keyNumpad3; |
| 276 |
VK_NUMPAD4: Result := keyNumpad4; |
| 277 |
VK_NUMPAD5: Result := keyNumpad5; |
| 278 |
VK_NUMPAD6: Result := keyNumpad6; |
| 279 |
VK_NUMPAD7: Result := keyNumpad7; |
| 280 |
VK_NUMPAD8: Result := keyNumpad8; |
| 281 |
VK_NUMPAD9: Result := keyNumpad9; |
| 282 |
VK_CLEAR: Result := keyNumpadBegin; |
| 283 |
VK_MULTIPLY: Result := keyNumpadMultiply; |
| 284 |
VK_DIVIDE: Result := keyNumpadDivide; |
| 285 |
VK_OEM_MINUS: Result := keyMinus; |
| 286 |
VK_OEM_PLUS: |
| 287 |
if ssShift in Shift then |
| 288 |
Result := keyPlus |
| 289 |
else |
| 290 |
Result := keyEqual; |
| 291 |
Ord('0') .. Ord('9'): |
| 292 |
Result := TKey(Ord(key0) + Ord(Key) - Ord('0')); |
| 293 |
Ord('A') .. Ord('Z'): |
| 294 |
Result := TKey(Ord(keyA) + Ord(Key) - Ord('A')); |
| 295 |
VK_F1 .. VK_F12: |
| 296 |
Result := TKey(Ord(keyF1) + Ord(Key) - VK_F1); |
| 297 |
end; |
| 298 |
end; |
| 299 |
|
| 300 |
procedure KeyFromCastle(const Key: TKey; const KeyString: String; |
| 301 |
out KeyCode: Word; out Shift: TShiftState); |
| 302 |
begin |
| 303 |
KeyFromCastle(Key, KeyString, [], KeyCode, Shift); |
| 304 |
end; |
| 305 |
|
| 306 |
procedure KeyFromCastle(const Key: TKey; KeyString: String; |
| 307 |
const Modifiers: TModifierKeys; |
| 308 |
out KeyCode: Word; out Shift: TShiftState); |
| 309 |
var |
| 310 |
KeyChar: Char; |
| 311 |
begin |
| 312 |
// only for backward compatibility (when this parameter was Char) convert #0 to '' |
| 313 |
if KeyString = #0 then |
| 314 |
KeyString := ''; |
| 315 |
|
| 316 |
Shift := []; |
| 317 |
KeyCode := VK_UNKNOWN; |
| 318 |
case Key of |
| 319 |
keyBackSpace: KeyCode := VK_BACK; |
| 320 |
keyTab: KeyCode := VK_TAB; |
| 321 |
keyEnter: KeyCode := VK_RETURN; |
| 322 |
keyShift: KeyCode := VK_SHIFT; |
| 323 |
keyCtrl: KeyCode := VK_CONTROL; |
| 324 |
keyAlt: KeyCode := VK_MENU; |
| 325 |
keyEscape: KeyCode := VK_ESCAPE; |
| 326 |
keySpace: KeyCode := VK_SPACE; |
| 327 |
keyPageUp: KeyCode := VK_PRIOR; |
| 328 |
keyPageDown: KeyCode := VK_NEXT; |
| 329 |
keyEnd: KeyCode := VK_END; |
| 330 |
keyHome: KeyCode := VK_HOME; |
| 331 |
keyArrowLeft: KeyCode := VK_LEFT; |
| 332 |
keyArrowUp: KeyCode := VK_UP; |
| 333 |
keyArrowRight: KeyCode := VK_RIGHT; |
| 334 |
keyArrowDown: KeyCode := VK_DOWN; |
| 335 |
keyInsert: KeyCode := VK_INSERT; |
| 336 |
keyDelete: KeyCode := VK_DELETE; |
| 337 |
keyNumpadPlus: KeyCode := VK_ADD; |
| 338 |
keyNumpadMinus: KeyCode := VK_SUBTRACT; |
| 339 |
keyPrintScreen: KeyCode := VK_SNAPSHOT; |
| 340 |
keyNumLock: KeyCode := VK_NUMLOCK; |
| 341 |
keyScrollLock: KeyCode := VK_SCROLL; |
| 342 |
keyCapsLock: KeyCode := VK_CAPITAL; |
| 343 |
keyPause: KeyCode := VK_PAUSE; |
| 344 |
keyComma: KeyCode := VK_OEM_COMMA; |
| 345 |
keyPeriod: KeyCode := VK_OEM_PERIOD; |
| 346 |
keyNumpad0: KeyCode := VK_NUMPAD0; |
| 347 |
keyNumpad1: KeyCode := VK_NUMPAD1; |
| 348 |
keyNumpad2: KeyCode := VK_NUMPAD2; |
| 349 |
keyNumpad3: KeyCode := VK_NUMPAD3; |
| 350 |
keyNumpad4: KeyCode := VK_NUMPAD4; |
| 351 |
keyNumpad5: KeyCode := VK_NUMPAD5; |
| 352 |
keyNumpad6: KeyCode := VK_NUMPAD6; |
| 353 |
keyNumpad7: KeyCode := VK_NUMPAD7; |
| 354 |
keyNumpad8: KeyCode := VK_NUMPAD8; |
| 355 |
keyNumpad9: KeyCode := VK_NUMPAD9; |
| 356 |
keyNumpadBegin: KeyCode := VK_CLEAR; |
| 357 |
keyNumpadMultiply: KeyCode := VK_MULTIPLY; |
| 358 |
keyNumpadDivide: KeyCode := VK_DIVIDE; |
| 359 |
keyMinus: KeyCode := VK_OEM_MINUS; |
| 360 |
keyEqual: KeyCode := VK_OEM_PLUS; |
| 361 |
|
| 362 |
{ TKey ranges } |
| 363 |
key0 ..key9 : KeyCode := Ord('0') + Ord(Key) - Ord(key0); |
| 364 |
keyA ..keyZ : KeyCode := Ord('A') + Ord(Key) - Ord(keyA); |
| 365 |
keyF1..keyF12: KeyCode := VK_F1 + Ord(Key) - Ord(keyF1); |
| 366 |
|
| 367 |
else |
| 368 |
if Length(KeyString) = 1 then |
| 369 |
begin |
| 370 |
KeyChar := KeyString[1]; |
| 371 |
case KeyChar of |
| 372 |
{ follow TMenuItem.Key docs: when Key is keyNone, only KeyChar indicates |
| 373 |
CharBackSpace / CharTab / CharEnter, convert them to Ctrl+xxx shortcuts } |
| 374 |
//CharBackSpace: KeyCode := VK_BACK; |
| 375 |
//CharTab: KeyCode := VK_TAB; |
| 376 |
//CharEnter: KeyCode := VK_RETURN; |
| 377 |
CharEscape: KeyCode := VK_ESCAPE; |
| 378 |
' ': KeyCode := VK_SPACE; |
| 379 |
CharDelete: KeyCode := VK_DELETE; |
| 380 |
'+': KeyCode := VK_ADD; |
| 381 |
'-': KeyCode := VK_SUBTRACT; |
| 382 |
',': KeyCode := VK_OEM_COMMA; |
| 383 |
'.': KeyCode := VK_OEM_PERIOD; |
| 384 |
'*': KeyCode := VK_MULTIPLY; |
| 385 |
'/': KeyCode := VK_DIVIDE; |
| 386 |
'=': KeyCode := VK_OEM_PLUS; |
| 387 |
|
| 388 |
{ Char ranges } |
| 389 |
'0' .. '9' : KeyCode := Ord(KeyChar); |
| 390 |
{ for latter: uppercase letters are VK_xxx codes } |
| 391 |
'A' .. 'Z' : begin KeyCode := Ord(KeyChar); Shift := [ssShift]; end; |
| 392 |
'a' .. 'z' : begin KeyCode := Ord(UpCase(KeyChar)); end; |
| 393 |
CtrlA .. CtrlZ: |
| 394 |
begin |
| 395 |
KeyCode := Ord('A') + Ord(KeyChar) - Ord(CtrlA); |
| 396 |
Shift := [ssCtrlOrCommand]; |
| 397 |
end; |
| 398 |
end; |
| 399 |
end; |
| 400 |
end; |
| 401 |
|
| 402 |
if mkShift in Modifiers then |
| 403 |
Shift += [ssShift]; |
| 404 |
if mkCtrl in Modifiers then |
| 405 |
Shift += [ssCtrlOrCommand]; |
| 406 |
if mkAlt in Modifiers then |
| 407 |
Shift += [ssAlt]; |
| 408 |
end; |
| 409 |
|
| 410 |
function MouseButtonToCastle( |
| 411 |
const MouseButton: Controls.TMouseButton; |
| 412 |
out MyMouseButton: TCastleMouseButton): boolean; |
| 413 |
begin |
| 414 |
Result := true; |
| 415 |
case MouseButton of |
| 416 |
Controls.mbLeft : MyMouseButton := CastleKeysMouse.buttonLeft; |
| 417 |
Controls.mbRight : MyMouseButton := CastleKeysMouse.buttonRight; |
| 418 |
Controls.mbMiddle: MyMouseButton := CastleKeysMouse.buttonMiddle; |
| 419 |
Controls.mbExtra1: MyMouseButton := CastleKeysMouse.buttonExtra1; |
| 420 |
Controls.mbExtra2: MyMouseButton := CastleKeysMouse.buttonExtra2; |
| 421 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 422 |
else Result := false; |
| 423 |
{$endif} |
| 424 |
end; |
| 425 |
end; |
| 426 |
|
| 427 |
function CursorFromCastle(const Cursor: TMouseCursor): TCursor; |
| 428 |
const |
| 429 |
Map: array [TMouseCursor] of TCursor = |
| 430 |
( crDefault, //< mcDefault |
| 431 |
crNone, //< mcNone |
| 432 |
crNone, //< mcForceNone |
| 433 |
|
| 434 |
crArrow, //< mcStandard |
| 435 |
crHourGlass, //< mcWait |
| 436 |
crIBeam, //< mcText |
| 437 |
crHandPoint, //< mcHand |
| 438 |
crSizeNS, //< mcResizeVertical |
| 439 |
crSizeWE, //< mcResizeHorizontal |
| 440 |
crSizeNW, //< mcResizeTopLeft |
| 441 |
crSizeN, //< mcResizeTop |
| 442 |
crSizeNE, //< mcResizeTopRight |
| 443 |
crSizeW, //< mcResizeLeft |
| 444 |
crSizeE, //< mcResizeRight |
| 445 |
crSizeSW, //< mcResizeBottomLeft |
| 446 |
crSizeS, //< mcResizeBottom |
| 447 |
crSizeSE //< mcResizeBottomRight |
| 448 |
); |
| 449 |
begin |
| 450 |
Result := Map[Cursor]; |
| 451 |
end; |
| 452 |
|
| 453 |
function FilenameToUriSafeUTF8(const FileName: string): string; |
| 454 |
begin |
| 455 |
Result := FilenameToUriSafe(UTF8ToSys(FileName)); |
| 456 |
end; |
| 457 |
|
| 458 |
function UriToFilenameSafeUTF8(const Url: String): string; |
| 459 |
begin |
| 460 |
Result := SysToUTF8(UriToFilenameSafe(Url)); |
| 461 |
end; |
| 462 |
|
| 463 |
function ColorToVector3(const Color: TColor): TVector3; |
| 464 |
begin |
| 465 |
Result := Vector3(ColorToVector3Byte(Color)); |
| 466 |
end; |
| 467 |
|
| 468 |
function ColorToVector3Byte(const Color: TColor): TVector3Byte; |
| 469 |
var |
| 470 |
Col: Integer; |
| 471 |
begin |
| 472 |
Col := ColorToRGB(Color); |
| 473 |
RedGreenBlue(Col, Result.X, Result.Y, Result.Z); |
| 474 |
end; |
| 475 |
|
| 476 |
{ TLCLKeyPressHandler -------------------------------------------------------- } |
| 477 |
|
| 478 |
procedure TLCLKeyPressHandler.KeyDown(const Key: Word; const Shift: TShiftState); |
| 479 |
begin |
| 480 |
if FUnfinishedKeyDown then |
| 481 |
Flush; // the previous press will only have KeyDown information |
| 482 |
Assert(not FUnfinishedKeyDown); |
| 483 |
|
| 484 |
FUnfinishedKeyDown := true; |
| 485 |
FUnfinishedKeyDownKey := Key; |
| 486 |
FUnfinishedKeyDownShift := Shift; |
| 487 |
|
| 488 |
// collected both KeyDown and KeyPress |
| 489 |
if FUnfinishedKeyDown and FUnfinishedKeyPress then |
| 490 |
Flush; |
| 491 |
end; |
| 492 |
|
| 493 |
procedure TLCLKeyPressHandler.BeforeKeyUp(const Key: Word; const Shift: TShiftState); |
| 494 |
begin |
| 495 |
if FUnfinishedKeyDown and (FUnfinishedKeyDownKey = Key) then |
| 496 |
Flush; |
| 497 |
end; |
| 498 |
|
| 499 |
procedure TLCLKeyPressHandler.UTF8KeyPress(const UTF8Key: TUTF8Char); |
| 500 |
begin |
| 501 |
if FUnfinishedKeyPress then |
| 502 |
Flush; // the previous press will only have KeyPress information |
| 503 |
Assert(not FUnfinishedKeyPress); |
| 504 |
|
| 505 |
FUnfinishedKeyPress := true; |
| 506 |
FUnfinishedKeyPressKey := UTF8Key; |
| 507 |
|
| 508 |
// collected both KeyDown and KeyPress |
| 509 |
if FUnfinishedKeyDown and FUnfinishedKeyPress then |
| 510 |
Flush; |
| 511 |
end; |
| 512 |
|
| 513 |
procedure TLCLKeyPressHandler.Flush; |
| 514 |
var |
| 515 |
Modifiers: TModifierKeys; |
| 516 |
Key: TKey; |
| 517 |
KeyString: String; |
| 518 |
begin |
| 519 |
// early exit in most usual case |
| 520 |
if not (FUnfinishedKeyDown or FUnfinishedKeyPress) then Exit; |
| 521 |
|
| 522 |
Modifiers := []; |
| 523 |
Key := keyNone; |
| 524 |
KeyString := ''; |
| 525 |
|
| 526 |
if FUnfinishedKeyDown then |
| 527 |
begin |
| 528 |
if ssShift in FUnfinishedKeyDownShift then Include(Modifiers, mkShift); |
| 529 |
if ssAlt in FUnfinishedKeyDownShift then Include(Modifiers, mkAlt); |
| 530 |
if ssCtrl in FUnfinishedKeyDownShift then Include(Modifiers, mkCtrl); |
| 531 |
Key := KeyToCastle(FUnfinishedKeyDownKey, FUnfinishedKeyDownShift); |
| 532 |
end; |
| 533 |
|
| 534 |
if FUnfinishedKeyPress then |
| 535 |
KeyString := FUnfinishedKeyPressKey; |
| 536 |
|
| 537 |
{ Thanks to checking this, |
| 538 |
1. LCL presses that cannot be represented as either TKey or KeyString |
| 539 |
will not be reported to CGE, |
| 540 |
2. If Flush was called when FUnfinishedKeyDown = false and |
| 541 |
FUnfinishedKeyPress = false, then nothing will happen. |
| 542 |
(Although right now we prevent this anyway by early exit above.) } |
| 543 |
|
| 544 |
if Assigned(OnPress) and ((Key <> keyNone) or (KeyString <> '')) then |
| 545 |
begin |
| 546 |
// fix empty KeyString in some cases (needed at least with GTK2 backend) |
| 547 |
if KeyString = '' then |
| 548 |
case Key of |
| 549 |
keyTab : KeyString := CharTab; |
| 550 |
keyDelete: KeyString := CharDelete; |
| 551 |
else ; |
| 552 |
end; |
| 553 |
|
| 554 |
OnPress(Self, InputKey(TVector2.Zero, Key, KeyString, Modifiers)); |
| 555 |
end; |
| 556 |
|
| 557 |
FUnfinishedKeyDown := false; |
| 558 |
FUnfinishedKeyPress := false; |
| 559 |
end; |
| 560 |
|
| 561 |
end. |