This file is indexed.

/usr/src/castle-game-engine-5.2.0/components/castlelclutils.pas is in castle-game-engine-src 5.2.0-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
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
405
406
407
408
409
410
411
412
413
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
{
  Copyright 2008-2014 Michalis Kamburelis.

  This file is part of "Castle Game Engine".

  "Castle Game Engine" is free software; see the file COPYING.txt,
  included in this distribution, for details about the copyright.

  "Castle Game Engine" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  ----------------------------------------------------------------------------
}

{ Utilities for cooperation between LCL and "Castle Game Engine". }
unit CastleLCLUtils;

interface

uses Dialogs, Classes, Controls, CastleFileFilters, CastleKeysMouse,
  Graphics, CastleVectors;

{ Convert file filters into LCL Dialog.Filter, Dialog.FilterIndex.
  Suitable for both open and save dialogs (TOpenDialog, TSaveDialog
  both descend from TFileDialog).

  Input filters are either given as a string FileFilters
  (encoded just like for TFileFilterList.AddFiltersFromString),
  or as TFileFilterList instance.

  Output filters are either written to LCLFilter, LCLFilterIndex
  variables, or set appropriate properties of given Dialog instance.

  When AllFields is false, then filters starting with "All " in the name,
  like "All files", "All images", are not included in the output.

  @groupBegin }
procedure FileFiltersToDialog(const FileFilters: string;
  Dialog: TFileDialog; const AllFields: boolean = true);
procedure FileFiltersToDialog(const FileFilters: string;
  out LCLFilter: string; out LCLFilterIndex: Integer; const AllFields: boolean = true);
procedure FileFiltersToDialog(FFList: TFileFilterList;
  Dialog: TFileDialog; const AllFields: boolean = true);
procedure FileFiltersToDialog(FFList: TFileFilterList;
  out LCLFilter: string; out LCLFilterIndex: Integer; const AllFields: boolean = true);
{ @groupEnd }

{ Make each '&' inside string '&&', this way the string will not contain
  special '&x' sequences when used as a TMenuItem.Caption and such. }
function SQuoteLCLCaption(const S: string): string;

{ Deprecated names, use the identifiers without "Open" in new code.
  @deprecated
  @groupBegin }
procedure FileFiltersToOpenDialog(const FileFilters: string;
  Dialog: TFileDialog); deprecated;
procedure FileFiltersToOpenDialog(const FileFilters: string;
  out LCLFilter: string; out LCLFilterIndex: Integer); deprecated;
procedure FileFiltersToOpenDialog(FFList: TFileFilterList;
  out LCLFilter: string; out LCLFilterIndex: Integer); deprecated;
{ @groupEnd }

{ Convert Key (Lazarus key code) to Castle Game Engine TKey.

  In addition, this tries to convert Key to a character (MyCharKey).
  It's awful that this function has to do convertion to Char,
  but that's the way of VCL and LCL: KeyPress and KeyDown
  are separate events. While I want to have them in one event,
  and passed as one event to TUIControl.KeyDown. }
procedure KeyLCLToCastle(const Key: Word; const Shift: TShiftState;
  out MyKey: TKey; out MyCharKey: char);

{ Convert TKey and/or character code into Lazarus key code (VK_xxx)
  and shift state.
  Sets LazKey to VK_UNKNOWN (zero) when convertion not possible
  (or when Key is K_None and CharKey = #0).

  Note that this is not a perfect reverse of KeyLCLToCastle function.
  It can't, as there are ambiguities (e.g. character 'A' may
  be a key K_A with mkShift in modifiers).

  @groupBegin }
procedure KeyCastleToLCL(const Key: TKey; const CharKey: char;
  const Modifiers: TModifierKeys;
  out LazKey: Word; out Shift: TShiftState);
procedure KeyCastleToLCL(const Key: TKey; const CharKey: char;
  out LazKey: Word; out Shift: TShiftState);
{ @groupEnd }

{ Convert Lazarus Controls.TMouseButton value to Castle Game Engine
  CastleKeysMouse.TMouseButton.

  (By coincidence, my type name and values are the same as used by LCL;
  but beware --- the order of values in my type is different (mbMiddle
  is in the middle in my type)). }
function MouseButtonLCLToCastle(
  const MouseButton: Controls.TMouseButton;
  out MyMouseButton: CastleKeysMouse.TMouseButton): boolean;

const
  CursorCastleToLCL: array [TMouseCursor] of TCursor =
  ( crDefault, crNone, crDefault { mcCustom treat like mcDefault },
    crArrow, crHourGlass, crIBeam, crHandPoint );

function FilenameToURISafeUTF8(const FileName: string): string;
function URIToFilenameSafeUTF8(const URL: string): string;

{ Convert LCL color values to our colors (vectors). }
function ColorToVector3(const Color: TColor): TVector3Single;
function ColorToVector3Byte(const Color: TColor): TVector3Byte;

implementation

uses SysUtils, FileUtil, LCLType, LCLProc,
  CastleClassUtils, CastleStringUtils, CastleURIUtils, CastleLog;

procedure FileFiltersToDialog(const FileFilters: string;
  Dialog: TFileDialog; const AllFields: boolean);
var
  LCLFilter: string;
  LCLFilterIndex: Integer;
begin
  FileFiltersToDialog(FileFilters, LCLFilter, LCLFilterIndex, AllFields);
  Dialog.Filter := LCLFilter;
  Dialog.FilterIndex := LCLFilterIndex;
end;

procedure FileFiltersToDialog(const FileFilters: string;
  out LCLFilter: string; out LCLFilterIndex: Integer; const AllFields: boolean);
var
  FFList: TFileFilterList;
begin
  FFList := TFileFilterList.Create(true);
  try
    FFList.AddFiltersFromString(FileFilters);
    FileFiltersToDialog(FFList, LCLFilter, LCLFilterIndex, AllFields);
  finally FreeAndNil(FFList) end;
end;

procedure FileFiltersToDialog(FFList: TFileFilterList;
  Dialog: TFileDialog; const AllFields: boolean);
var
  LCLFilter: string;
  LCLFilterIndex: Integer;
begin
  FileFiltersToDialog(FFList, LCLFilter, LCLFilterIndex, AllFields);
  Dialog.Filter := LCLFilter;
  Dialog.FilterIndex := LCLFilterIndex;
end;

procedure FileFiltersToDialog(FFList: TFileFilterList;
  out LCLFilter: string; out LCLFilterIndex: Integer; const AllFields: boolean);
var
  Filter: TFileFilter;
  I, J: Integer;
begin
  LCLFilter := '';

  { initialize LCLFilterIndex.
    Will be corrected for AllFields=false case, and will be incremented
    (because LCL FilterIndex counts from 1) later. }

  LCLFilterIndex := FFList.DefaultFilter;

  for I := 0 to FFList.Count - 1 do
  begin
    Filter := FFList[I];
    if (not AllFields) and IsPrefix('All ', Filter.Name) then
    begin
      { then we don't want to add this to LCLFilter.
        We also need to fix LCLFilterIndex, to shift it. }
      if I = FFList.DefaultFilter then
        LCLFilterIndex := 0 else
      if I < FFList.DefaultFilter then
        Dec(LCLFilterIndex);
      Continue;
    end;

    LCLFilter += Filter.Name + '|';

    for J := 0 to Filter.Patterns.Count - 1 do
    begin
      if J <> 0 then LCLFilter += ';';
      LCLFilter += Filter.Patterns[J];
    end;

    LCLFilter += '|';
  end;

  { LCL FilterIndex counts from 1. }
  Inc(LCLFilterIndex);
end;

function SQuoteLCLCaption(const S: string): string;
begin
  Result := StringReplace(S, '&', '&&', [rfReplaceAll]);
end;

{ FileFiltersToOpenDialog are deprecated, just call versions without "Open". }
procedure FileFiltersToOpenDialog(const FileFilters: string;
  Dialog: TFileDialog);
begin
  FileFiltersToDialog(FileFilters, Dialog);
end;

procedure FileFiltersToOpenDialog(const FileFilters: string;
  out LCLFilter: string; out LCLFilterIndex: Integer);
begin
  FileFiltersToDialog(FileFilters, LCLFilter, LCLFilterIndex);
end;

procedure FileFiltersToOpenDialog(FFList: TFileFilterList;
  out LCLFilter: string; out LCLFilterIndex: Integer);
begin
  FileFiltersToDialog(FFList, LCLFilter, LCLFilterIndex);
end;

const
  { Ctrl key on most systems, Command key on Mac OS X. }
  ssCtrlOrCommand = {$ifdef DARWIN} ssMeta {$else} ssCtrl {$endif};

procedure KeyLCLToCastle(const Key: Word; const Shift: TShiftState;
  out MyKey: TKey; out MyCharKey: char);
begin
  MyKey := K_None;
  MyCharKey := #0;

  case Key of
    VK_BACK:       begin MyKey := K_BackSpace;       MyCharKey := CharBackSpace; end;
    VK_TAB:        begin MyKey := K_Tab;             MyCharKey := CharTab;       end;
    VK_RETURN:     begin MyKey := K_Enter;           MyCharKey := CharEnter;     end;
    VK_SHIFT:            MyKey := K_Shift;
    VK_CONTROL:          MyKey := K_Ctrl;
    VK_MENU:             MyKey := K_Alt;
    VK_ESCAPE:     begin MyKey := K_Escape;          MyCharKey := CharEscape;    end;
    VK_SPACE:      begin MyKey := K_Space;           MyCharKey := ' ';           end;
    VK_PRIOR:            MyKey := K_PageUp;
    VK_NEXT:             MyKey := K_PageDown;
    VK_END:              MyKey := K_End;
    VK_HOME:             MyKey := K_Home;
    VK_LEFT:             MyKey := K_Left;
    VK_UP:               MyKey := K_Up;
    VK_RIGHT:            MyKey := K_Right;
    VK_DOWN:             MyKey := K_Down;
    VK_INSERT:           MyKey := K_Insert;
    VK_DELETE:     begin MyKey := K_Delete;          MyCharKey := CharDelete; end;
    VK_ADD:        begin MyKey := K_Numpad_Plus;     MyCharKey := '+';        end;
    VK_SUBTRACT:   begin MyKey := K_Numpad_Minus;    MyCharKey := '-';        end;
    VK_SNAPSHOT:         MyKey := K_PrintScreen;
    VK_NUMLOCK:          MyKey := K_NumLock;
    VK_SCROLL:           MyKey := K_ScrollLock;
    VK_CAPITAL:          MyKey := K_CapsLock;
    VK_PAUSE:            MyKey := K_Pause;
    VK_OEM_COMMA:  begin MyKey := K_Comma;           MyCharKey := ','; end;
    VK_OEM_PERIOD: begin MyKey := K_Period;          MyCharKey := '.'; end;
    VK_NUMPAD0:    begin MyKey := K_Numpad_0;        MyCharKey := '0'; end;
    VK_NUMPAD1:    begin MyKey := K_Numpad_1;        MyCharKey := '1'; end;
    VK_NUMPAD2:    begin MyKey := K_Numpad_2;        MyCharKey := '2'; end;
    VK_NUMPAD3:    begin MyKey := K_Numpad_3;        MyCharKey := '3'; end;
    VK_NUMPAD4:    begin MyKey := K_Numpad_4;        MyCharKey := '4'; end;
    VK_NUMPAD5:    begin MyKey := K_Numpad_5;        MyCharKey := '5'; end;
    VK_NUMPAD6:    begin MyKey := K_Numpad_6;        MyCharKey := '6'; end;
    VK_NUMPAD7:    begin MyKey := K_Numpad_7;        MyCharKey := '7'; end;
    VK_NUMPAD8:    begin MyKey := K_Numpad_8;        MyCharKey := '8'; end;
    VK_NUMPAD9:    begin MyKey := K_Numpad_9;        MyCharKey := '9'; end;
    VK_CLEAR:            MyKey := K_Numpad_Begin;
    VK_MULTIPLY:   begin MyKey := K_Numpad_Multiply; MyCharKey := '*'; end;
    VK_DIVIDE:     begin MyKey := K_Numpad_Divide;   MyCharKey := '/'; end;
    VK_OEM_MINUS:  begin MyKey := K_Minus;           MyCharKey := '-'; end;
    VK_OEM_PLUS:
      if ssShift in Shift then
      begin
        MyKey := K_Plus ; MyCharKey := '+';
      end else
      begin
        MyKey := K_Equal; MyCharKey := '=';
      end;

    Ord('0') .. Ord('9'):
      begin
        MyKey := TKey(Ord(K_0)  + Ord(Key) - Ord('0'));
        MyCharKey := Chr(Key);
      end;

    Ord('A') .. Ord('Z'):
      begin
        MyKey := TKey(Ord(K_A)  + Ord(Key) - Ord('A'));
        if ssCtrlOrCommand in Shift then
          MyCharKey := Chr(Ord(CtrlA) + Ord(Key) - Ord('A')) else
        begin
          MyCharKey := Chr(Key);
          if not (ssShift in Shift) then
            MyCharKey := LoCase(MyCharKey);
        end;
      end;

    VK_F1 .. VK_F12  : MyKey := TKey(Ord(K_F1) + Ord(Key) - VK_F1);
  end;

  if (MyKey = K_None) and (MyCharKey = #0) then
    WritelnLog('LCL', 'Cannot translate LCL VK_xxx key %s with shift %s to Castle Game Engine key',
      [DbgsVKCode(Key), DbgS(Shift)]);
end;

procedure KeyCastleToLCL(const Key: TKey; const CharKey: char;
  out LazKey: Word; out Shift: TShiftState);
begin
  KeyCastleToLCL(Key, CharKey, [], LazKey, Shift);
end;

procedure KeyCastleToLCL(const Key: TKey; const CharKey: char;
  const Modifiers: TModifierKeys;
  out LazKey: Word; out Shift: TShiftState);
begin
  Shift := [];
  LazKey := VK_UNKNOWN;
  case Key of
    K_BackSpace:        LazKey := VK_BACK;
    K_Tab:              LazKey := VK_TAB;
    K_Enter:            LazKey := VK_RETURN;
    K_Shift:            LazKey := VK_SHIFT;
    K_Ctrl:             LazKey := VK_CONTROL;
    K_Alt:              LazKey := VK_MENU;
    K_Escape:           LazKey := VK_ESCAPE;
    K_Space:            LazKey := VK_SPACE;
    K_PageUp:           LazKey := VK_PRIOR;
    K_PageDown:         LazKey := VK_NEXT;
    K_End:              LazKey := VK_END;
    K_Home:             LazKey := VK_HOME;
    K_Left:             LazKey := VK_LEFT;
    K_Up:               LazKey := VK_UP;
    K_Right:            LazKey := VK_RIGHT;
    K_Down:             LazKey := VK_DOWN;
    K_Insert:           LazKey := VK_INSERT;
    K_Delete:           LazKey := VK_DELETE;
    K_Numpad_Plus:      LazKey := VK_ADD;
    K_Numpad_Minus:     LazKey := VK_SUBTRACT;
    K_PrintScreen:      LazKey := VK_SNAPSHOT;
    K_NumLock:          LazKey := VK_NUMLOCK;
    K_ScrollLock:       LazKey := VK_SCROLL;
    K_CapsLock:         LazKey := VK_CAPITAL;
    K_Pause:            LazKey := VK_PAUSE;
    K_Comma:            LazKey := VK_OEM_COMMA;
    K_Period:           LazKey := VK_OEM_PERIOD;
    K_Numpad_0:         LazKey := VK_NUMPAD0;
    K_Numpad_1:         LazKey := VK_NUMPAD1;
    K_Numpad_2:         LazKey := VK_NUMPAD2;
    K_Numpad_3:         LazKey := VK_NUMPAD3;
    K_Numpad_4:         LazKey := VK_NUMPAD4;
    K_Numpad_5:         LazKey := VK_NUMPAD5;
    K_Numpad_6:         LazKey := VK_NUMPAD6;
    K_Numpad_7:         LazKey := VK_NUMPAD7;
    K_Numpad_8:         LazKey := VK_NUMPAD8;
    K_Numpad_9:         LazKey := VK_NUMPAD9;
    K_Numpad_Begin:     LazKey := VK_CLEAR;
    K_Numpad_Multiply:  LazKey := VK_MULTIPLY;
    K_Numpad_Divide:    LazKey := VK_DIVIDE;
    K_Minus:            LazKey := VK_OEM_MINUS;
    K_Equal:            LazKey := VK_OEM_PLUS;

    { TKey ranges }
    K_0 ..K_9  : LazKey := Ord('0') + Ord(Key) - Ord(K_0);
    K_A ..K_Z  : LazKey := Ord('A') + Ord(Key) - Ord(K_A);
    K_F1..K_F12: LazKey :=    VK_F1 + Ord(Key) - Ord(K_F1);

    else
      case CharKey of
        { follow TMenuItem.Key docs: when Key is K_None, only CharKey indicates
          CharBackSpace / CharTab / CharEnter, convert them to Ctrl+xxx shortcuts }
        //CharBackSpace:              LazKey := VK_BACK;
        //CharTab:                    LazKey := VK_TAB;
        //CharEnter:                  LazKey := VK_RETURN;
        CharEscape:                 LazKey := VK_ESCAPE;
        ' ':                        LazKey := VK_SPACE;
        CharDelete:                 LazKey := VK_DELETE;
        '+':                        LazKey := VK_ADD;
        '-':                        LazKey := VK_SUBTRACT;
        ',':                        LazKey := VK_OEM_COMMA;
        '.':                        LazKey := VK_OEM_PERIOD;
        '*':                        LazKey := VK_MULTIPLY;
        '/':                        LazKey := VK_DIVIDE;
        '=':                        LazKey := VK_OEM_PLUS;

        { Char ranges }
        '0' .. '9' : LazKey := Ord(CharKey);
        { for latter: uppercase letters are VK_xxx codes }
        'A' .. 'Z' : begin LazKey := Ord(CharKey); Shift := [ssShift]; end;
        'a' .. 'z' : begin LazKey := Ord(UpCase(CharKey)); end;
        CtrlA .. CtrlZ:
          begin
            LazKey := Ord('A') + Ord(CharKey) - Ord(CtrlA);
            Shift := [ssCtrlOrCommand];
          end;
      end;
  end;

  if mkShift in Modifiers then
    Shift += [ssShift];
  if mkCtrl in Modifiers then
    Shift += [ssCtrlOrCommand];
  if mkAlt in Modifiers then
    Shift += [ssAlt];
end;

function MouseButtonLCLToCastle(
  const MouseButton: Controls.TMouseButton;
  out MyMouseButton: CastleKeysMouse.TMouseButton): boolean;
begin
  Result := true;
  case MouseButton of
    Controls.mbLeft  : MyMouseButton := CastleKeysMouse.mbLeft;
    Controls.mbRight : MyMouseButton := CastleKeysMouse.mbRight;
    Controls.mbMiddle: MyMouseButton := CastleKeysMouse.mbMiddle;
    else Result := false;
  end;
end;

function FilenameToURISafeUTF8(const FileName: string): string;
begin
  Result := FilenameToURISafe(UTF8ToSys(FileName));
end;

function URIToFilenameSafeUTF8(const URL: string): string;
begin
  Result := SysToUTF8(URIToFilenameSafe(URL));
end;

function ColorToVector3(const Color: TColor): TVector3Single;
begin
  Result := Vector3Single(ColorToVector3Byte(Color));
end;

function ColorToVector3Byte(const Color: TColor): TVector3Byte;
var
  Col: LongInt;
begin
  Col := ColorToRGB(Color);
  RedGreenBlue(Col, Result[0], Result[1], Result[2]);
end;

end.