Debian Codegraph @

src/lcl/castlepropedits_component_transformdesign.inc

raw
1 {%MainUnit castlepropedits.pas}
2 {
3 Copyright 2022-2022 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 type
18 { Editor for TCastleTransformDesign. }
19 TCastleTransformDesignComponentEditor = class(TCastleTransformComponentEditor)
20 public
21 function GetVerbCount: Integer; override;
22 function GetVerb(Index: Integer): string; override;
23 procedure ExecuteVerb(Index: Integer); override;
24 end;
25
26 function TCastleTransformDesignComponentEditor.GetVerbCount: Integer;
27 begin
28 Result := (inherited GetVerbCount) + 3;
29 end;
30
31 function TCastleTransformDesignComponentEditor.GetVerb(Index: Integer): string;
32 var
33 InheritedCount: Integer;
34 begin
35 InheritedCount := inherited GetVerbCount;
36 case Index - InheritedCount of
37 0: Result := 'Edit (Copy Here) Referenced Design';
38 1: Result := 'Revert To Referenced Design';
39 2: Result := 'Open Referenced Design';
40 else Result := inherited GetVerb(Index);
41 end;
42 end;
43
44 procedure TCastleTransformDesignComponentEditor.ExecuteVerb(Index: Integer);
45
46 procedure Edit(const E: TCastleComponentEditorDesigner; const C: TCastleTransformDesign);
47 var
48 NewChild: TCastleTransform;
49 begin
50 if C.URL <> '' then
51 begin
52 { We don't just remove csTransient from C.FDesign.
53 We need to reload it, to set new Owner for it (same as our owner,
54 DesignOwner in editor) and make names inside unique in new Owner. }
55 NewChild := TransformLoad(C.URL, C.Owner);
56 C.Add(NewChild);
57
58 InternalCastleDesignInvalidate := true;
59
60 C.RevertURL := C.URL;
61 C.URL := '';
62 E.Modified;
63 end;
64 end;
65
66 procedure Revert(const E: TCastleComponentEditorDesigner; const C: TCastleTransformDesign);
67 var
68 I: Integer;
69 begin
70 if C.RevertURL <> '' then
71 begin
72 for I := C.Count - 1 downto 0 do
73 { Note: we don't free csTransient, e.g. do not free editor gizmos.
74 This is now checked by FreeComponentRecursively, no need for explicit
75 check here, just be aware that some FreeComponentRecursively calls
76 will be ignored.
77
78 Note: It is critical to call E.FreeComponentRecursively,
79 not just C.Items[I].Free.
80 Because otherwise the components inside will be "left hanging":
81 they will keep taking names in DesignOwner,
82 they will keep being referenced (e.g. if design contained TCastleCamera,
83 it can be referenced by some TCastleViewport.Camera outside of this design),
84 and they are not part of hierarchy (so they would not be serialized to JSON). }
85 E.FreeComponentRecursively(C.Items[I]);
86
87 InternalCastleDesignInvalidate := true;
88
89 C.URL := C.RevertURL;
90 E.Modified;
91 end;
92 end;
93
94 procedure Open(const E: TCastleComponentEditorDesigner; const C: TCastleTransformDesign);
95 begin
96 if C.URL <> '' then
97 E.ProposeOpenDesign(C.URL)
98 else
99 if C.RevertURL <> '' then
100 E.ProposeOpenDesign(C.RevertURL);
101 end;
102
103 var
104 InheritedCount: Integer;
105 begin
106 InheritedCount := inherited GetVerbCount;
107 case Index - InheritedCount of
108 0:begin
109 if GetDesigner is TCastleComponentEditorDesigner then
110 Edit(GetDesigner as TCastleComponentEditorDesigner, Component as TCastleTransformDesign);
111 end;
112 1:begin
113 if GetDesigner is TCastleComponentEditorDesigner then
114 Revert(GetDesigner as TCastleComponentEditorDesigner, Component as TCastleTransformDesign);
115 end;
116 2:begin
117 if GetDesigner is TCastleComponentEditorDesigner then
118 Open(GetDesigner as TCastleComponentEditorDesigner, Component as TCastleTransformDesign);
119 end;
120 else inherited ExecuteVerb(Index);
121 end;
122 end;