Debian Codegraph @

tools/castle-editor/components/castleeditorpropedits_physics_layer.inc

raw
1 {%MainUnit castleeditorpropedits.pas}
2 {
3 Copyright 2022-2022 Michalis Kamburelis, Andrzej Kilijanski
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 {$ifdef read_interface}
18 type
19
20 { Property editor for layer collisions }
21 TPhysicsLayerCollisionsPropertyEditor = class(TSubPropertiesEditor)
22 public
23 function GetAttributes: TPropertyAttributes; override;
24 procedure Edit; override;
25 end;
26
27 TPhysicsLayerNamesPropertyEditor = class(TSubPropertiesEditor)
28 public
29 function GetAttributes: TPropertyAttributes; override;
30 procedure Edit; override;
31 end;
32
33 { Property editor for choosing physics layer in rigid body }
34 TPhysicsLayerPropertyEditor = class(TOrdinalPropertyEditor)
35 strict private
36 function GetLayerName(OrdValue: LongInt): String;
37 public
38 function GetAttributes: TPropertyAttributes; override;
39 function OrdValueToVisualValue(OrdValue: LongInt): string; override;
40 procedure GetValues(Proc: TGetStrProc); override;
41 procedure SetValue(const NewValue: ansistring); override;
42 end;
43 {$endif read_interface}
44
45 {$ifdef read_implementation}
46 { TPhysicsLayerNamesPropertyEditor ------------------------------------------ }
47
48 function TPhysicsLayerNamesPropertyEditor.GetAttributes: TPropertyAttributes;
49 begin
50 Result := inherited GetAttributes + [paDialog, paRevertable] - [paSubProperties];
51 end;
52
53 procedure TPhysicsLayerNamesPropertyEditor.Edit;
54 var
55 LayerNamesForm: TPhysicsLayerNamesPropertyEditorForm;
56 LayerNames: TCastleLayerNames;
57 begin
58 LayerNames := GetObjectValue as TCastleLayerNames;
59 if LayerNames = nil then
60 Exit;
61
62 LayerNamesForm := TPhysicsLayerNamesPropertyEditorForm.Create(nil);
63 try
64 LayerNamesForm.Init(LayerNames);
65 if LayerNamesForm.ShowModal = mrOK then
66 Modified;
67 finally
68 FreeAndNil(LayerNamesForm);
69 end;
70 end;
71
72 { TLayerCollisionsPropertyEditor --------------------------------------------- }
73
74 function TPhysicsLayerCollisionsPropertyEditor.GetAttributes: TPropertyAttributes;
75 begin
76 Result := inherited GetAttributes + [paDialog, paRevertable] - [paSubProperties];
77 end;
78
79 procedure TPhysicsLayerCollisionsPropertyEditor.Edit;
80 var
81 LayerCollisionsForm: TLayerCollisionsPropertyEditorForm;
82 LayerCollisions: TCastleLayerCollisions;
83 begin
84 LayerCollisions := GetObjectValue as TCastleLayerCollisions;
85 if LayerCollisions = nil then
86 Exit;
87
88 LayerCollisionsForm := TLayerCollisionsPropertyEditorForm.Create(nil);
89 try
90 LayerCollisionsForm.Init(LayerCollisions);
91 if LayerCollisionsForm.ShowModal = mrOK then
92 Modified;
93 finally
94 FreeAndNil(LayerCollisionsForm);
95 end;
96 end;
97
98 { TPhysicsLayerPropertyEditor ------------------------------------------------ }
99
100 function TPhysicsLayerPropertyEditor.GetLayerName(OrdValue: LongInt): String;
101 var
102 RigidBody: TCastleRigidBody;
103 Transform: TCastleTransform;
104 begin
105 { We assume that Layer property is in TCastleRigidBody }
106 if (GetInstProp^.Instance = nil) then
107 Exit('');
108
109 if not (GetInstProp^.Instance is TCastleRigidBody) then
110 Exit('');
111
112 RigidBody := GetInstProp^.Instance as TCastleRigidBody;
113 Transform := RigidBody.Parent;
114
115 if (Transform = nil) or (Transform.World = nil) then
116 Exit('');
117
118 if High(TPhysicsLayer) < OrdValue then
119 Exit('');
120
121 Result := Transform.World.PhysicsProperties.LayerNames.Names[TPhysicsLayer(OrdValue)];
122 end;
123
124 function TPhysicsLayerPropertyEditor.GetAttributes: TPropertyAttributes;
125 begin
126 Result := (inherited GetAttributes) + [paValueList];
127 end;
128
129 function TPhysicsLayerPropertyEditor.OrdValueToVisualValue(OrdValue: LongInt): string;
130 var
131 L: Longint;
132 begin
133 L := OrdValue;
134 Result := IntToStr(L) + ': ' + GetLayerName(L);
135 end;
136
137 procedure TPhysicsLayerPropertyEditor.GetValues(Proc: TGetStrProc);
138 var
139 I: Integer;
140 begin
141 for I := Low(TPhysicsLayer) to High(TPhysicsLayer) do
142 Proc(IntToStr(I) + ': ' + GetLayerName(I));
143 end;
144
145 procedure TPhysicsLayerPropertyEditor.SetValue(const NewValue: AnsiString);
146 var
147 ColonPos: Integer;
148 StringValue: AnsiString;
149 IntValue: Integer;
150 begin
151 ColonPos := Pos(':', NewValue);
152 if ColonPos < 2 then
153 StringValue := NewValue
154 else
155 StringValue := Copy(NewValue, 1, ColonPos -1);
156
157 if TryStrToInt(StringValue, IntValue) then
158 SetOrdValue(IntValue);
159 end;
160
161 {$endif read_implementation}
162