Debian Codegraph @

src/lcl/castleinternaltiledlayersdialog.pas

raw
1 {
2 Copyright 2023-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 { Dialog for selecting Tiled layers to show, used by CastlePropEdits. }
17 unit CastleInternalTiledLayersDialog;
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses
24 Generics.Collections, Contnrs,
25 Classes, SysUtils, Forms, Controls, Graphics, Dialogs,
26 ButtonPanel, StdCtrls, ExtCtrls, CastleTiledMap;
27
28 type
29 TTiledLayersDialog = class(TForm)
30 ButtonPanel1: TButtonPanel;
31 CheckLayers: TScrollBox;
32 Label1: TLabel;
33 procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
34 procedure SelectAllButtonClick(Sender: TObject);
35 private
36 Checkboxes: TObjectList;
37 public
38 AvailableLayers: TCastleTiledMapData.TLayerList;
39 Layers: TCastleTiledMap.TLayers;
40 constructor Create(AOwner: TComponent); override;
41 destructor Destroy; override;
42 procedure UpdateSelectionUi;
43 end;
44
45 implementation
46
47 uses Math,
48 CastleLclUtils;
49
50 {$R *.lfm}
51
52 { TTiledLayersDialog ---------------------------------------------------- }
53
54 constructor TTiledLayersDialog.Create(AOwner: TComponent);
55 begin
56 inherited;
57 Checkboxes := TObjectList.Create(false);
58 end;
59
60 destructor TTiledLayersDialog.Destroy;
61 begin
62 FreeAndNil(Checkboxes);
63 inherited;
64 end;
65
66 procedure TTiledLayersDialog.FormCloseQuery(Sender: TObject;
67 var CanClose: boolean);
68 var
69 I: Integer;
70 begin
71 if ModalResult = mrOK then
72 begin
73 { calculate Layers from Checkboxes }
74 Layers := [];
75 for I := 0 to Min(Checkboxes.Count - 1, High(TCastleTiledMap.TLayerIndex)) do
76 begin
77 if TCheckBox(Checkboxes[I]).Checked then
78 Include(Layers, I);
79 end;
80 { Set to true remaining layers, so that future added layers are visible.
81 This also means that "all layers" state is reliably expressed as TCastleTiledMap.AllLayers,
82 and is not saved to JSON as it is the default value,
83 and is displayed as simple "[all]" by TTiledLayersPropertyEditor.OrdValueToVisualValue. }
84 for I := Checkboxes.Count to High(TCastleTiledMap.TLayerIndex) do
85 Include(Layers, I);
86 end;
87 end;
88
89 procedure TTiledLayersDialog.SelectAllButtonClick(Sender: TObject);
90 var
91 I: Integer;
92 begin
93 for I := 0 to Checkboxes.Count - 1 do
94 TCheckBox(Checkboxes[I]).Checked := true;
95 end;
96
97 procedure TTiledLayersDialog.UpdateSelectionUi;
98 var
99 S: String;
100 Checkbox: TCheckBox;
101 I: Integer;
102 begin
103 Checkboxes.Clear;
104
105 if AvailableLayers <> nil then // AvailableLayers may be nil when no map loaded
106 begin
107 for I := 0 to AvailableLayers.Count - 1 do
108 begin
109 S := Format('%d: %s', [
110 I,
111 AvailableLayers[I].Name
112 ]);
113 if not AvailableLayers[I].Visible then
114 S := S + ' (always hidden; toggle layer visibility in Tiled to show it)';
115
116 if I > High(TCastleTiledMap.TLayerIndex) then
117 S := S + Format(' (index > %d, always visible)', [High(TCastleTiledMap.TLayerIndex)]);
118
119 Checkbox := TCheckBox.Create(Self);
120 Checkbox.Caption := S;
121 Checkbox.Checked :=
122 (I > High(TCastleTiledMap.TLayerIndex)) or
123 (I in Layers);
124 Checkbox.Parent := CheckLayers;
125 Checkboxes.Add(Checkbox);
126 end;
127 end;
128 end;
129
130 end.