Debian Codegraph @

src/lcl/castlepropedits_tiledlayers.inc

raw
1 {%MainUnit castlepropedits.pas}
2 {
3 Copyright 2023-2023 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 { TCastleTiledMap.Layers property editor. }
18
19 { TLayersPropertyEditor ------------------------------------------ }
20
21 type
22 { Property editor to select layers in TCastleTiledMap. }
23 TTiledLayersPropertyEditor = class(TSetPropertyEditor)
24 strict private
25 procedure SetAllValues(const NewValue: TCastleTiledMap.TLayers);
26 public
27 { Open dialog with Tiled layer names to select. }
28 procedure Edit; override;
29 { Use TOneLayerPropertyEditor to show nice names for layers when expanded in object inspector. }
30 procedure GetProperties(Proc: TGetPropEditProc); override;
31 { Makes value in object inspector look nice. }
32 function OrdValueToVisualValue(OrdValue: Integer): String; override;
33 function GetAttributes: TPropertyAttributes; override;
34 end;
35
36 { Editor for each layer (row) in TTiledLayersPropertyEditor. }
37 TOneLayerPropertyEditor = class(TSetElementPropertyEditor)
38 protected
39 FElementToShow: Integer;
40 public
41 constructor Create(Parent: TPropertyEditor; AElement: Integer); reintroduce;
42 function GetName: ShortString; override;
43 end;
44
45 { TTiledLayersPropertyEditor ------------------------------------------------- }
46
47 procedure TTiledLayersPropertyEditor.Edit;
48 var
49 D: TTiledLayersDialog;
50 Map: TCastleTiledMap;
51 begin
52 D := TTiledLayersDialog.Create(Application);
53 try
54 Map := GetComponent(0) as TCastleTiledMap;
55 D.Caption := 'Edit ' + Map.Name + '.Layers';
56 if Map.Data <> nil then
57 D.AvailableLayers := Map.Data.Layers;
58 D.Layers := Map.Layers;
59 D.UpdateSelectionUi;
60 if D.ShowModal = mrOK then
61 SetAllValues(D.Layers);
62 finally FreeAndNil(D) end;
63 end;
64
65 procedure TTiledLayersPropertyEditor.SetAllValues(const NewValue: TCastleTiledMap.TLayers);
66 var
67 I: Integer;
68 Map: TCastleTiledMap;
69 begin
70 for I := 0 to PropCount - 1 do
71 begin
72 Map := GetComponent(I) as TCastleTiledMap;
73 Map.Layers := NewValue;
74 end;
75 Modified;
76 end;
77
78 procedure TTiledLayersPropertyEditor.GetProperties(Proc: TGetPropEditProc);
79 var
80 I: Integer;
81 begin
82 { Make TOneLayerPropertyEditor actually used. }
83 with GetTypeData(GetTypeData(GetPropType)^.CompType)^ do
84 for I := MinValue to MaxValue do
85 Proc(TOneLayerPropertyEditor.Create(Self, I));
86 end;
87
88 function TTiledLayersPropertyEditor.OrdValueToVisualValue(OrdValue: Integer): String;
89 var
90 Layers: TCastleTiledMap.TLayers;
91 LayerIndex: TCastleTiledMap.TLayerIndex;
92 begin
93 Layers := TCastleTiledMap.TLayers(OrdValue);
94 if Layers = TCastleTiledMap.AllLayers then
95 Result := '[all]'
96 else
97 begin
98 Result := '';
99 for LayerIndex := Low(LayerIndex) to High(LayerIndex) do
100 if LayerIndex in Layers then
101 Result := SAppendPart(Result, ',', IntToStr(LayerIndex));
102 Result := '[' + Result + ']';
103 end;
104 end;
105
106 function TTiledLayersPropertyEditor.GetAttributes: TPropertyAttributes;
107 begin
108 Result := (inherited GetAttributes) + [paDialog];
109 end;
110
111 { TOneLayerPropertyEditor ---------------------------------------------------- }
112
113 constructor TOneLayerPropertyEditor.Create(Parent: TPropertyEditor;
114 AElement: Integer);
115 begin
116 inherited Create(Parent, AElement);
117 FElementToShow := AElement;
118 end;
119
120 function TOneLayerPropertyEditor.GetName: ShortString;
121 begin
122 { Show single-digit numbers like 1 with a leading space, ' 1',
123 so that the layers are nicely sorted 1, 2, ... 9, 10, 11, ... in the object
124 inspector. }
125 Result := Format('%2d',[FElementToShow]);
126 end;