Debian Codegraph @

src/lcl/castlepropedits_component_design.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 TCastleDesign. }
19 TCastleDesignComponentEditor = class(TComponentEditor)
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 TCastleDesignComponentEditor.GetVerbCount: Integer;
27 begin
28 Result := (inherited GetVerbCount) + 3;
29 end;
30
31 function TCastleDesignComponentEditor.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 TCastleDesignComponentEditor.ExecuteVerb(Index: Integer);
45
46 procedure Edit(const E: TCastleComponentEditorDesigner; const C: TCastleDesign);
47 var
48 NewChild: TCastleUserInterface;
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 := UserInterfaceLoad(C.URL, C.Owner);
56 NewChild.FullSize := C.Stretch;
57 C.InsertFront(NewChild);
58
59 InternalCastleDesignInvalidate := true;
60
61 C.RevertURL := C.URL;
62 C.URL := '';
63 E.Modified;
64 end;
65 end;
66
67 procedure Revert(const E: TCastleComponentEditorDesigner; const C: TCastleDesign);
68 var
69 I: Integer;
70 begin
71 if C.RevertURL <> '' then
72 begin
73 for I := C.ControlsCount - 1 downto 0 do
74 { Note: we don't free csTransient, e.g. do not free editor gizmos.
75 This is now checked by FreeComponentRecursively, no need for explicit
76 check here, just be aware that some FreeComponentRecursively calls
77 will be ignored.
78
79 Note: It is critical to call E.FreeComponentRecursively,
80 not just C.Items[I].Free.
81 Because otherwise the components inside will be "left hanging":
82 they will keep taking names in DesignOwner,
83 they will keep being referenced (e.g. if design contained TCastleCamera,
84 it can be referenced by some TCastleViewport.Camera outside of this design),
85 and they are not part of hierarchy (so they would not be serialized to JSON). }
86 E.FreeComponentRecursively(C.Controls[I]);
87
88 InternalCastleDesignInvalidate := true;
89
90 C.URL := C.RevertURL;
91 E.Modified;
92 end;
93 end;
94
95 procedure Open(const E: TCastleComponentEditorDesigner; const C: TCastleDesign);
96 begin
97 if C.URL <> '' then
98 E.ProposeOpenDesign(C.URL)
99 else
100 if C.RevertURL <> '' then
101 E.ProposeOpenDesign(C.RevertURL);
102 end;
103
104 var
105 InheritedCount: Integer;
106 begin
107 InheritedCount := inherited GetVerbCount;
108 case Index - InheritedCount of
109 0:begin
110 if GetDesigner is TCastleComponentEditorDesigner then
111 Edit(GetDesigner as TCastleComponentEditorDesigner, Component as TCastleDesign);
112 end;
113 1:begin
114 if GetDesigner is TCastleComponentEditorDesigner then
115 Revert(GetDesigner as TCastleComponentEditorDesigner, Component as TCastleDesign);
116 end;
117 2:begin
118 if GetDesigner is TCastleComponentEditorDesigner then
119 Open(GetDesigner as TCastleComponentEditorDesigner, Component as TCastleDesign);
120 end;
121 else inherited ExecuteVerb(Index);
122 end;
123 end;