Debian Codegraph @

tools/castle-editor/components/formphysicslayernamespropertyeditor.pas

raw
1 {
2 Copyright 2023-2023 Michalis Kamburelis, Andrzej Kilijanski
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 { Edit PhysicsProperties.LayerNames. }
17 unit FormPhysicsLayerNamesPropertyEditor;
18
19 {$mode ObjFPC}{$H+}
20
21 interface
22
23 uses
24 Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Grids, ComCtrls,
25 StdCtrls, ValEdit, ButtonPanel, CastleTransform, Types;
26
27 type
28 TPhysicsLayerNamesPropertyEditorForm = class(TForm)
29 ButtonPanel: TButtonPanel;
30 NamesAndDescStringGrid: TStringGrid;
31 procedure OkButtonClick(Sender: TObject);
32 procedure NamesAndDescStringGridPrepareCanvas(Sender: TObject; ACol,
33 ARow: Integer; AState: TGridDrawState);
34 procedure NamesAndDescStringGridResize(Sender: TObject);
35 procedure NamesAndDescStringGridSelectEditor(Sender: TObject; ACol,
36 ARow: Integer; var Editor: TWinControl);
37 strict private
38 { Pointer to layer names in physics properties }
39 FLayerNames: TCastleLayerNames;
40 { Pointer to custom editor }
41 FEditor: TMemo;
42 { Column edited by custom editor, used by EditorKeyDown() }
43 FEditorCol: Integer;
44 { Row edited by custom editor, used by EditorKeyDown() }
45 FEditorRow: Integer;
46 { Load layer names to grid }
47 procedure Load;
48 { Save layer names from grid to physics properties }
49 procedure Save;
50 { Recalculate columns widths to fit grid control }
51 procedure RecalculateColumnsWidth;
52 { Recalculate row height to fit text }
53 procedure RecalculateRowWidth(const RowIndex: Integer);
54 { Recalculate all rows height }
55 procedure RecalculateRowsWidth;
56
57 { OnKeyDown event for custom editor }
58 procedure EditorKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
59 public
60 { Initialize window }
61 procedure Init(const LayerNames: TCastleLayerNames);
62 end;
63
64 implementation
65
66 {$R *.lfm}
67
68 uses Math, LCLIntf, LCLType;
69
70 const
71 NumberColIndex = 0;
72 NameColIndex = 1;
73 DescriptionColIndex = 2;
74
75 { TPhysicsLayerNamesPropertyEditorForm -------------------------------------- }
76
77 procedure TPhysicsLayerNamesPropertyEditorForm.OkButtonClick(Sender: TObject);
78 begin
79 Save;
80 end;
81
82 procedure TPhysicsLayerNamesPropertyEditorForm.NamesAndDescStringGridPrepareCanvas
83 (Sender: TObject; ACol, ARow: Integer; AState: TGridDrawState);
84 var
85 ATextStyle: TTextStyle;
86 begin
87 { based on https://forum.lazarus.freepascal.org/index.php?topic=25662.0 }
88 if ACol = DescriptionColIndex then
89 begin
90 ATextStyle := TStringGrid(Sender).Canvas.TextStyle;
91 ATextStyle.SingleLine := False;
92 ATextStyle.Wordbreak := True;
93 TStringGrid(Sender).Canvas.TextStyle := ATextStyle;
94 end;
95 end;
96
97 procedure TPhysicsLayerNamesPropertyEditorForm.NamesAndDescStringGridResize(
98 Sender: TObject);
99 begin
100 RecalculateColumnsWidth;
101 end;
102
103 procedure TPhysicsLayerNamesPropertyEditorForm.NamesAndDescStringGridSelectEditor
104 (Sender: TObject; ACol, ARow: Integer; var Editor: TWinControl);
105 var
106 EditorRect: TRect;
107 begin
108 if ACol = DescriptionColIndex then
109 begin
110 if FEditor = nil then
111 FEditor := TMemo.Create(NamesAndDescStringGrid);
112 Editor := FEditor;
113 EditorRect := NamesAndDescStringGrid.CellRect(ACol, ARow);
114 EditorRect.Right := EditorRect.Right - 1;
115 EditorRect.Bottom := EditorRect.Bottom - 1;
116 FEditor.BoundsRect := EditorRect;
117 FEditorCol := ACol;
118 FEditorRow := ARow;
119 FEditor.WantReturns := false;
120 FEditor.Text := NamesAndDescStringGrid.Cols[ACol][ARow];
121 FEditor.SelLength := 0;
122 FEditor.SelStart := Length(FEditor.Text);
123 FEditor.BorderStyle := bsNone;
124 FEditor.WordWrap := true;
125 FEditor.OnKeyDown := @EditorKeyDown;
126 FEditor.Show;
127 end;
128 end;
129
130 procedure TPhysicsLayerNamesPropertyEditorForm.Load;
131 var
132 I: Integer;
133 begin
134 NamesAndDescStringGrid.RowCount := High(TPhysicsLayer) + 2;
135
136 for I := Low(TPhysicsLayer) + 1 to High(TPhysicsLayer) + 1 do
137 begin
138 NamesAndDescStringGrid.Cols[0][I] := IntToStr(I - 1);
139 NamesAndDescStringGrid.Cols[1][I] := FLayerNames.Names[TPhysicsLayer(I-1)];
140 NamesAndDescStringGrid.Cols[2][I] := FLayerNames.Descriptions[TPhysicsLayer(I-1)];
141 end;
142 end;
143
144 procedure TPhysicsLayerNamesPropertyEditorForm.Save;
145 var
146 I: Integer;
147 begin
148 for I := Low(TPhysicsLayer) + 1 to High(TPhysicsLayer) + 1 do
149 begin
150 FLayerNames.Names[TPhysicsLayer(I-1)] := NamesAndDescStringGrid.Cols[1][I];
151 FLayerNames.Descriptions[TPhysicsLayer(I-1)] := NamesAndDescStringGrid.Cols[2][I];
152 end;
153 end;
154
155 procedure TPhysicsLayerNamesPropertyEditorForm.RecalculateColumnsWidth;
156 const
157 MinColWidth = 30;
158 NumberColPercent: Single = 0.07;
159 NameColPercent: Single = 0.20;
160 DescriptionColPercent: Single = 0.73;
161 var
162 GridClientWidth: Integer;
163 LinesSize: Integer;
164 begin
165 LinesSize := (NamesAndDescStringGrid.ColCount + 1) *
166 NamesAndDescStringGrid.GridLineWidth;
167 NamesAndDescStringGrid.Constraints.MinWidth := MinColWidth * 3 + LinesSize;
168 GridClientWidth := NamesAndDescStringGrid.ClientWidth;
169 NamesAndDescStringGrid.ColWidths[NumberColIndex] := Max(MinColWidth,
170 Round(GridClientWidth * NumberColPercent));
171 NamesAndDescStringGrid.ColWidths[NameColIndex] := Max(MinColWidth,
172 Round(GridClientWidth * NameColPercent));
173 NamesAndDescStringGrid.ColWidths[DescriptionColIndex] := Max(MinColWidth,
174 Round(GridClientWidth * DescriptionColPercent));
175
176 RecalculateRowsWidth;
177 end;
178
179 procedure TPhysicsLayerNamesPropertyEditorForm.RecalculateRowWidth(const RowIndex: Integer);
180 var
181 ARect: TRect;
182 begin
183 ARect.Top := 0;
184 ARect.Left := 0;
185 ARect.Right := NamesAndDescStringGrid.ColWidths[DescriptionColIndex];
186 ARect.Bottom := 0;
187
188 DrawText(NamesAndDescStringGrid.Canvas.Handle,
189 PChar(NamesAndDescStringGrid.Cols[DescriptionColIndex][RowIndex]),
190 Length(NamesAndDescStringGrid.Cols[DescriptionColIndex][RowIndex]),
191 ARect, DT_CALCRECT or DT_WORDBREAK);
192
193 NamesAndDescStringGrid.RowHeights[RowIndex] := Max(
194 NamesAndDescStringGrid.DefaultRowHeight, ARect.Bottom);
195 end;
196
197 procedure TPhysicsLayerNamesPropertyEditorForm.RecalculateRowsWidth;
198 var
199 I: Integer;
200 begin
201 for I := 1 to NamesAndDescStringGrid.RowCount -1 do
202 RecalculateRowWidth(I);
203 end;
204
205 procedure TPhysicsLayerNamesPropertyEditorForm.EditorKeyDown(Sender: TObject;
206 var Key: Word; Shift: TShiftState);
207 begin
208 if (Key = 13) and (Shift = []) then
209 begin
210 NamesAndDescStringGrid.Cols[FEditorCol][FEditorRow] := FEditor.Text;
211 FEditor.Hide;
212 NamesAndDescStringGrid.SetFocus;
213 end else
214 if (Key = 37) and (Shift = []) then
215 begin
216 { Change editor to left }
217 if (FEditor.SelLength = 0) and (FEditor.SelStart = 0) and
218 (NamesAndDescStringGrid.Col > 0) then
219 begin
220 NamesAndDescStringGrid.Cols[FEditorCol][FEditorRow] := FEditor.Text;
221 FEditor.Hide;
222 NamesAndDescStringGrid.SetFocus;
223 NamesAndDescStringGrid.Col := NamesAndDescStringGrid.Col - 1;
224 end;
225 end else
226 if (Key = 39) and (Shift = []) then
227 begin
228 { Change editor to right }
229 if (FEditor.SelLength = 0) and (FEditor.SelStart = Length(FEditor.Text)) and
230 (NamesAndDescStringGrid.Col < NamesAndDescStringGrid.ColCount - 1) then
231 begin
232 NamesAndDescStringGrid.Cols[FEditorCol][FEditorRow] := FEditor.Text;
233 FEditor.Hide;
234 NamesAndDescStringGrid.SetFocus;
235 NamesAndDescStringGrid.Col := NamesAndDescStringGrid.Col + 1;
236 end;
237 end else
238 if (Key = 40) and (Shift = []) then
239 begin
240 { Change editor to next row }
241 if (FEditor.SelLength = 0) and (NamesAndDescStringGrid.Row < NamesAndDescStringGrid.RowCount - 1) then
242 begin
243 NamesAndDescStringGrid.Cols[FEditorCol][FEditorRow] := FEditor.Text;
244 FEditor.Hide;
245 NamesAndDescStringGrid.SetFocus;
246 NamesAndDescStringGrid.Row := NamesAndDescStringGrid.Row + 1;
247 end;
248 end else
249 if (Key = 38) and (Shift = []) then
250 begin
251 { Change editor to previous row }
252 if (FEditor.SelLength = 0) and (NamesAndDescStringGrid.Row > 1) then
253 begin
254 NamesAndDescStringGrid.Cols[FEditorCol][FEditorRow] := FEditor.Text;
255 FEditor.Hide;
256 NamesAndDescStringGrid.SetFocus;
257 NamesAndDescStringGrid.Row := NamesAndDescStringGrid.Row - 1;
258 end;
259 end;
260 end;
261
262 procedure TPhysicsLayerNamesPropertyEditorForm.Init(
263 const LayerNames: TCastleLayerNames);
264 begin
265 FLayerNames := LayerNames;
266
267 Load;
268 RecalculateColumnsWidth;
269 end;
270
271 end.
272