| 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.LayerCollisions. } |
| 17 |
unit FormLayerCollisionsPropertyEditor; |
| 18 |
|
| 19 |
{$mode ObjFPC}{$H+} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses |
| 24 |
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls, |
| 25 |
ButtonPanel, CastleTransform; |
| 26 |
|
| 27 |
type |
| 28 |
TLayerCollisionsPropertyEditorForm = class(TForm) |
| 29 |
ButtonPanel1: TButtonPanel; |
| 30 |
LayerNamesButton: TButton; |
| 31 |
CheckboxesPanel: TPanel; |
| 32 |
RevertButton: TButton; |
| 33 |
VerticalNamesPanel: TPanel; |
| 34 |
HorizontalNamesPanel: TPanel; |
| 35 |
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); |
| 36 |
procedure FormResize(Sender: TObject); |
| 37 |
procedure LayerNamesButtonClick(Sender: TObject); |
| 38 |
procedure RevertButtonClick(Sender: TObject); |
| 39 |
strict private |
| 40 |
{ |
| 41 |
Checkboxes matrix looking like that: |
| 42 |
19,0 18,0 17,0 ... |
| 43 |
19,1 18,1 17,1 ... |
| 44 |
19,2 18,2 17,1 ... |
| 45 |
... |
| 46 |
} |
| 47 |
Checkboxes: array [TPhysicsLayer, TPhysicsLayer] of TCheckBox; |
| 48 |
{ Horizontal panels for horizontal checkboxes arrangement } |
| 49 |
CheckboxesPanels: array [TPhysicsLayer] of TPanel; |
| 50 |
{ Horizontal names labels from 0 to 19 } |
| 51 |
HorizontalNames: array [TPhysicsLayer] of TLabel; |
| 52 |
{ Pointer to physics properties layer collisions object } |
| 53 |
FLayerCollisions: TCastleLayerCollisions; |
| 54 |
|
| 55 |
{ Method to create panels checkboxes when window is created } |
| 56 |
procedure CreateCheckboxes; |
| 57 |
{ Creates horizontal names on the left side of the window } |
| 58 |
procedure CreateHorizontalNames; |
| 59 |
{ Creates vertical names on the top of the window } |
| 60 |
procedure CreateVerticalNames; |
| 61 |
|
| 62 |
{ Helper to get name of the layer assuming the parent is TPhysicsProperties } |
| 63 |
function GetLayerName(PhysicsLayer: TPhysicsLayer): String; |
| 64 |
{ Helper to get description of the layer assuming the parent is TPhysicsProperties } |
| 65 |
function GetLayerDescription(PhysicsLayer: TPhysicsLayer): String; |
| 66 |
|
| 67 |
{ Updates vertical coordinate of names TLabel's } |
| 68 |
procedure UpdateHorizontalNamesTop; |
| 69 |
{ Vertical names are drawn on canvas in repaint event } |
| 70 |
procedure RepaintVerticalNames(Sender: TObject); |
| 71 |
{ Updates horzontal layers names - used after closing layers names dialog } |
| 72 |
procedure UpdateHorizontalNames; |
| 73 |
{ Updates layers names in checkboxes } |
| 74 |
procedure UpdateCheckboxHint(Checkbox: TCheckBox; X, Y: TPhysicsLayer); |
| 75 |
{ Updates layers names - used after closing layers names dialog } |
| 76 |
procedure UpdateCheckboxesHints; |
| 77 |
{ Load layers config to grid } |
| 78 |
procedure Load; |
| 79 |
{ Save layers config to physics properties object } |
| 80 |
procedure Save; |
| 81 |
public |
| 82 |
{ Initialize the gui and load data from physics properties object } |
| 83 |
procedure Init(const LayerCollisions: TCastleLayerCollisions); |
| 84 |
end; |
| 85 |
|
| 86 |
implementation |
| 87 |
|
| 88 |
{$R *.lfm} |
| 89 |
|
| 90 |
uses FormPhysicsLayerNamesPropertyEditor; |
| 91 |
|
| 92 |
{ TLayerCollisionsPropertyEditorForm ----------------------------------------- } |
| 93 |
|
| 94 |
procedure TLayerCollisionsPropertyEditorForm.FormResize(Sender: TObject); |
| 95 |
begin |
| 96 |
UpdateHorizontalNamesTop; |
| 97 |
end; |
| 98 |
|
| 99 |
procedure TLayerCollisionsPropertyEditorForm.FormCloseQuery(Sender: TObject; |
| 100 |
var CanClose: Boolean); |
| 101 |
begin |
| 102 |
if ModalResult = mrOK then |
| 103 |
begin |
| 104 |
Save; |
| 105 |
end; |
| 106 |
end; |
| 107 |
|
| 108 |
procedure TLayerCollisionsPropertyEditorForm.LayerNamesButtonClick( |
| 109 |
Sender: TObject); |
| 110 |
var |
| 111 |
LayerNamesForm: TPhysicsLayerNamesPropertyEditorForm; |
| 112 |
LayerNames: TCastleLayerNames; |
| 113 |
begin |
| 114 |
if not (FLayerCollisions.Owner is TPhysicsProperties) then |
| 115 |
Exit; |
| 116 |
|
| 117 |
LayerNames := TPhysicsProperties(FLayerCollisions.Owner).LayerNames; |
| 118 |
|
| 119 |
LayerNamesForm := TPhysicsLayerNamesPropertyEditorForm.Create(nil); |
| 120 |
try |
| 121 |
LayerNamesForm.Init(LayerNames); |
| 122 |
if LayerNamesForm.ShowModal = mrOK then |
| 123 |
begin |
| 124 |
// update layers names and |
| 125 |
RepaintVerticalNames(Self); |
| 126 |
UpdateHorizontalNames; |
| 127 |
UpdateCheckboxesHints; |
| 128 |
end; |
| 129 |
finally |
| 130 |
FreeAndNil(LayerNamesForm); |
| 131 |
end; |
| 132 |
end; |
| 133 |
|
| 134 |
procedure TLayerCollisionsPropertyEditorForm.RevertButtonClick(Sender: TObject); |
| 135 |
begin |
| 136 |
Load; |
| 137 |
end; |
| 138 |
|
| 139 |
procedure TLayerCollisionsPropertyEditorForm.CreateCheckboxes; |
| 140 |
var |
| 141 |
Panel: TPanel; |
| 142 |
PreviousPanel: TPanel; |
| 143 |
I, J : TPhysicsLayer; |
| 144 |
|
| 145 |
function AddCheckbox(X, Y: TPhysicsLayer; Panel: TPanel): TCheckBox; |
| 146 |
var |
| 147 |
C: TCheckBox; |
| 148 |
begin |
| 149 |
C := TCheckBox.Create(Panel); |
| 150 |
C.Parent := Panel; |
| 151 |
C.AnchorSide[akTop].Side := asrCenter; |
| 152 |
C.AnchorSide[akTop].Control := Panel; |
| 153 |
C.Caption := ''; |
| 154 |
UpdateCheckboxHint(C, X, Y); |
| 155 |
C.ShowHint := true; |
| 156 |
C.ParentShowHint := false; |
| 157 |
C.AutoSize := true; |
| 158 |
|
| 159 |
if X = High(TPhysicsLayer) then |
| 160 |
begin |
| 161 |
C.AnchorSide[akLeft].Side := asrLeft; |
| 162 |
C.AnchorSide[akLeft].Control := Panel |
| 163 |
end else |
| 164 |
begin |
| 165 |
C.AnchorSide[akLeft].Side := asrRight; |
| 166 |
C.AnchorSide[akLeft].Control := CheckBoxes[X + 1, Y]; |
| 167 |
end; |
| 168 |
Result := C; |
| 169 |
end; |
| 170 |
|
| 171 |
begin |
| 172 |
PreviousPanel := nil; |
| 173 |
CheckboxesPanel.AutoSize := true; |
| 174 |
CheckboxesPanel.Caption := ''; |
| 175 |
CheckboxesPanel.BevelOuter := bvNone; |
| 176 |
|
| 177 |
for I := Low(TPhysicsLayer) to High(TPhysicsLayer) do |
| 178 |
begin |
| 179 |
Panel := TPanel.Create(CheckboxesPanel); |
| 180 |
Panel.Parent := CheckboxesPanel; |
| 181 |
Panel.Caption := ''; |
| 182 |
Panel.BevelOuter := bvNone; |
| 183 |
|
| 184 |
if PreviousPanel = nil then |
| 185 |
begin |
| 186 |
Panel.AnchorSide[akTop].Side := asrTop; |
| 187 |
Panel.AnchorSide[akTop].Control := CheckboxesPanel |
| 188 |
end else |
| 189 |
begin |
| 190 |
Panel.AnchorSide[akTop].Side := asrBottom; |
| 191 |
Panel.AnchorSide[akTop].Control := PreviousPanel; |
| 192 |
end; |
| 193 |
|
| 194 |
for J := High(TPhysicsLayer) downto I do |
| 195 |
Checkboxes[J, I] := AddCheckbox(J, I, Panel); |
| 196 |
|
| 197 |
Panel.AutoSize := true; |
| 198 |
CheckboxesPanels[I] := Panel; |
| 199 |
PreviousPanel := Panel; |
| 200 |
end; |
| 201 |
end; |
| 202 |
|
| 203 |
procedure TLayerCollisionsPropertyEditorForm.CreateHorizontalNames; |
| 204 |
var |
| 205 |
ALabel: TLabel; |
| 206 |
I: TPhysicsLayer; |
| 207 |
begin |
| 208 |
HorizontalNamesPanel.BevelOuter := bvNone; |
| 209 |
HorizontalNamesPanel.Caption := ''; |
| 210 |
|
| 211 |
for I := Low(TPhysicsLayer) to High(TPhysicsLayer) do |
| 212 |
begin |
| 213 |
ALabel := TLabel.Create(HorizontalNamesPanel); |
| 214 |
ALabel.Parent := HorizontalNamesPanel; |
| 215 |
ALabel.Caption := IntToStr(I) + ': ' + GetLayerName(I); |
| 216 |
ALabel.Hint := GetLayerDescription(I); |
| 217 |
ALabel.ShowHint := true; |
| 218 |
ALabel.AutoSize := true; |
| 219 |
|
| 220 |
ALabel.Anchors := [akTop, akRight]; |
| 221 |
ALabel.AnchorSide[akTop].Side := asrTop; |
| 222 |
ALabel.AnchorSide[akTop].Control := nil; |
| 223 |
ALabel.AnchorSide[akRight].Side := asrRight; |
| 224 |
ALabel.AnchorSide[akRight].Control := HorizontalNamesPanel; |
| 225 |
ALabel.BorderSpacing.Right := 2; |
| 226 |
|
| 227 |
HorizontalNames[I] := ALabel; |
| 228 |
end; |
| 229 |
HorizontalNamesPanel.AutoSize := true; |
| 230 |
end; |
| 231 |
|
| 232 |
procedure TLayerCollisionsPropertyEditorForm.CreateVerticalNames; |
| 233 |
begin |
| 234 |
VerticalNamesPanel.BevelOuter := bvNone; |
| 235 |
VerticalNamesPanel.Caption := ''; |
| 236 |
VerticalNamesPanel.OnPaint := @RepaintVerticalNames; |
| 237 |
RepaintVerticalNames(VerticalNamesPanel); |
| 238 |
end; |
| 239 |
|
| 240 |
function TLayerCollisionsPropertyEditorForm.GetLayerName( |
| 241 |
PhysicsLayer: TPhysicsLayer): String; |
| 242 |
begin |
| 243 |
Result := ''; |
| 244 |
|
| 245 |
if FLayerCollisions = nil then |
| 246 |
Exit; |
| 247 |
|
| 248 |
if FLayerCollisions.Owner is TPhysicsProperties then |
| 249 |
Result := TPhysicsProperties(FLayerCollisions.Owner).LayerNames.Names[PhysicsLayer]; |
| 250 |
end; |
| 251 |
|
| 252 |
function TLayerCollisionsPropertyEditorForm.GetLayerDescription( |
| 253 |
PhysicsLayer: TPhysicsLayer): String; |
| 254 |
begin |
| 255 |
Result := ''; |
| 256 |
|
| 257 |
if FLayerCollisions = nil then |
| 258 |
Exit; |
| 259 |
|
| 260 |
if FLayerCollisions.Owner is TPhysicsProperties then |
| 261 |
Result := TPhysicsProperties(FLayerCollisions.Owner).LayerNames.Descriptions[PhysicsLayer]; |
| 262 |
end; |
| 263 |
|
| 264 |
procedure TLayerCollisionsPropertyEditorForm.UpdateHorizontalNamesTop; |
| 265 |
var |
| 266 |
Margin: Integer; |
| 267 |
I: TPhysicsLayer; |
| 268 |
begin |
| 269 |
Margin := (CheckboxesPanels[0].Height - HorizontalNames[0].Height) div 2; |
| 270 |
for I := Low(TPhysicsLayer) to High(TPhysicsLayer) do |
| 271 |
HorizontalNames[I].Top := CheckboxesPanels[I].Top + Margin; |
| 272 |
end; |
| 273 |
|
| 274 |
procedure TLayerCollisionsPropertyEditorForm.RepaintVerticalNames(Sender: TObject); |
| 275 |
var |
| 276 |
I: TPhysicsLayer; |
| 277 |
X, Y: Integer; |
| 278 |
CheckboxWidth: Integer; |
| 279 |
MaxWidth: Integer; |
| 280 |
Margin: Integer; |
| 281 |
VName: String; |
| 282 |
VNameWidth: Integer; |
| 283 |
begin |
| 284 |
VerticalNamesPanel.Canvas.Font.Orientation := 900; |
| 285 |
VerticalNamesPanel.Canvas.Font.Color := clWindowText; |
| 286 |
VerticalNamesPanel.Color := clWindow; |
| 287 |
|
| 288 |
MaxWidth := 20; |
| 289 |
X := 0; |
| 290 |
CheckboxWidth := Checkboxes[High(TPhysicsLayer), Low(TPhysicsLayer)].Width; |
| 291 |
Y := VerticalNamesPanel.Height; |
| 292 |
Margin := (CheckboxWidth - VerticalNamesPanel.Canvas.TextExtent('I').Height) div 2; |
| 293 |
|
| 294 |
for I := High(TPhysicsLayer) downto Low(TPhysicsLayer) do |
| 295 |
begin |
| 296 |
VName := IntToStr(I) + ': ' + GetLayerName(I); |
| 297 |
VNameWidth := VerticalNamesPanel.Canvas.TextExtent(VName).Width; |
| 298 |
if VNameWidth > MaxWidth then |
| 299 |
MaxWidth := VNameWidth; |
| 300 |
VerticalNamesPanel.Canvas.TextOut(X + Margin, Y, VName); |
| 301 |
X := X + CheckboxWidth; |
| 302 |
end; |
| 303 |
|
| 304 |
if MaxWidth <> VerticalNamesPanel.Constraints.MinHeight then |
| 305 |
begin |
| 306 |
VerticalNamesPanel.Constraints.MinHeight := MaxWidth; |
| 307 |
VerticalNamesPanel.Constraints.MaxHeight := MaxWidth; |
| 308 |
end; |
| 309 |
end; |
| 310 |
|
| 311 |
procedure TLayerCollisionsPropertyEditorForm.UpdateHorizontalNames; |
| 312 |
var |
| 313 |
I: TPhysicsLayer; |
| 314 |
begin |
| 315 |
for I := Low(TPhysicsLayer) to High(TPhysicsLayer) do |
| 316 |
begin |
| 317 |
HorizontalNames[I].Caption := IntToStr(I) + ': ' + GetLayerName(I); |
| 318 |
HorizontalNames[I].Hint := GetLayerDescription(I); |
| 319 |
end; |
| 320 |
end; |
| 321 |
|
| 322 |
procedure TLayerCollisionsPropertyEditorForm.UpdateCheckboxHint( |
| 323 |
Checkbox: TCheckBox; X, Y: TPhysicsLayer); |
| 324 |
var |
| 325 |
LayerXName, LayerYName: String; |
| 326 |
begin |
| 327 |
LayerXName := GetLayerName(X); |
| 328 |
LayerYName := GetLayerName(Y); |
| 329 |
|
| 330 |
if (LayerXName = '') and (LayerYName = '') then |
| 331 |
Checkbox.Hint := '[' + IntToStr(Y) + ',' + IntToStr(X) + ']' |
| 332 |
else |
| 333 |
begin |
| 334 |
if LayerXName = '' then |
| 335 |
LayerXName := IntToStr(X); |
| 336 |
if LayerYName = '' then |
| 337 |
LayerYName := IntToStr(Y); |
| 338 |
Checkbox.Hint := LayerYName + ' x ' + LayerXName + |
| 339 |
' [' + IntToStr(Y) + ',' + IntToStr(X) + ']' |
| 340 |
end; |
| 341 |
end; |
| 342 |
|
| 343 |
procedure TLayerCollisionsPropertyEditorForm.UpdateCheckboxesHints; |
| 344 |
var |
| 345 |
I, J : TPhysicsLayer; |
| 346 |
begin |
| 347 |
for I := Low(TPhysicsLayer) to High(TPhysicsLayer) do |
| 348 |
begin |
| 349 |
for J := High(TPhysicsLayer) downto I do |
| 350 |
UpdateCheckboxHint(Checkboxes[J, I], J, I); |
| 351 |
end; |
| 352 |
end; |
| 353 |
|
| 354 |
procedure TLayerCollisionsPropertyEditorForm.Load; |
| 355 |
var |
| 356 |
I, J: TPhysicsLayer; |
| 357 |
begin |
| 358 |
for I := Low(TPhysicsLayer) to High(TPhysicsLayer) do |
| 359 |
begin |
| 360 |
for J := High(TPhysicsLayer) downto I do |
| 361 |
Checkboxes[J, I].Checked := FLayerCollisions.Collides[I, J]; |
| 362 |
end; |
| 363 |
end; |
| 364 |
|
| 365 |
procedure TLayerCollisionsPropertyEditorForm.Save; |
| 366 |
var |
| 367 |
I, J: TPhysicsLayer; |
| 368 |
begin |
| 369 |
for I := Low(TPhysicsLayer) to High(TPhysicsLayer) do |
| 370 |
begin |
| 371 |
for J := High(TPhysicsLayer) downto I do |
| 372 |
begin |
| 373 |
FLayerCollisions.Collides[I, J] := Checkboxes[J, I].Checked; |
| 374 |
// 2nd assignment should not be necessary, as Collides setter already sets both directions |
| 375 |
//FLayerCollisions.Collides[J, I] := Checkboxes[J, I].Checked; |
| 376 |
Assert(FLayerCollisions.Collides[J, I] = Checkboxes[J, I].Checked); |
| 377 |
end; |
| 378 |
end; |
| 379 |
end; |
| 380 |
|
| 381 |
procedure TLayerCollisionsPropertyEditorForm.Init(const LayerCollisions: TCastleLayerCollisions); |
| 382 |
begin |
| 383 |
FLayerCollisions := LayerCollisions; |
| 384 |
|
| 385 |
CreateCheckboxes; |
| 386 |
CreateHorizontalNames; |
| 387 |
CreateVerticalNames; |
| 388 |
UpdateHorizontalNamesTop; |
| 389 |
|
| 390 |
Load; |
| 391 |
end; |
| 392 |
|
| 393 |
end. |
| 394 |
|