Debian Codegraph @

tools/castle-editor/code/castlecomponenteditordesigner.pas

raw
1 {
2 Copyright 2022-2022 Michalis Kamburelis.
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 { Define TConcreteEditorDesigner, necessary to use LCL component editors
17 from CGE editor. }
18 unit CastleComponentEditorDesigner;
19
20 interface
21
22 uses Classes, Controls, ComponentReg, ComponentEditors, PropEdits, Menus,
23 LMessages, LCLType,
24 // CGE units
25 CastleClassUtils, CastleEditorAccess,
26 // editor units
27 FrameDesign, DesignUndoSystem;
28
29 type
30 { To get registered component editors (and our CastlePropEdits registers some,
31 e.g. to define some verbs, e.g. "Reset Transform" on TCastleTransform)
32 we need to define TComponentEditorDesigner descendant that defines
33 some functionality using TComponentEditorDesigner API.
34
35 An instance of this corresponds 1-1 to each TDesignFrame,
36 and will be passed as parameter of GetComponentEditor. }
37 TConcreteEditorDesigner = class(TCastleComponentEditorDesigner)
38 strict private
39 FDesignFrame: TDesignFrame;
40 FPropertyEditorHook: TPropertyEditorHook;
41 protected
42 function GetPropertyEditorHook: TPropertyEditorHook; override;
43 function GetShowNonVisualComponents: boolean; override;
44 procedure SetShowNonVisualComponents(AValue: boolean); override;
45 public
46 constructor Create(const ADesignFrame: TDesignFrame;
47 const APropertyEditorHook: TPropertyEditorHook); reintroduce;
48 procedure Modified; override;
49 function CopySelection: boolean; override;
50 function CutSelection: boolean; override;
51 function CanCopy: Boolean; override;
52 function CanPaste: Boolean; override;
53 function PasteSelection(Flags: TComponentPasteSelectionFlags): boolean; override;
54 function ClearSelection: boolean; override;
55 function DeleteSelection: boolean; override;
56 function CopySelectionToStream(s: TStream): boolean; override;
57 function InsertFromStream(s: TStream; Parent: TWinControl; Flags: TComponentPasteSelectionFlags): Boolean; override;
58 function InvokeComponentEditor(AComponent: TComponent): boolean; override;
59 function ChangeClass: boolean; override;
60 function CanUndo: Boolean; override;
61 function CanRedo: Boolean; override;
62 function Undo: Boolean; override;
63 function Redo: Boolean; override;
64 function AddUndoAction(const aPersistent: TPersistent; aOpType: TUndoOpType;
65 IsSetNewId: boolean; aFieldName: string; const aOldVal, aNewVal: variant): boolean; override;
66 function IsUndoLocked: boolean; override;
67 procedure AddComponent(const NewRegisteredComponent: TRegisteredComponent;
68 const NewComponentClass: TComponentClass;
69 const NewParent: TComponent;
70 const NewLeft,NewTop,NewWidth,NewHeight: Integer); override;
71 procedure AddComponentCheckParent(var NewParent: TComponent;
72 const OriginComponent: TComponent; const OriginWinControl: TWinControl;
73 const NewComponentClass: TComponentClass); override;
74 procedure DrawDesignerItems(OnlyIfNeeded: boolean); override;
75 function CreateUniqueComponentName(const AClassName: string): string; override;
76 function IsDesignMsg(Sender: TControl; var Message: TLMessage): Boolean; override;
77 procedure UTF8KeyPress(var UTF8Key: TUTF8Char); override;
78 procedure Notification(AComponent: TComponent;
79 Operation: TOperation); override;
80 procedure PaintGrid; override;
81 procedure ValidateRename(AComponent: TComponent;
82 const CurName, NewName: string); override;
83 function GetShiftState: TShiftState; override;
84 procedure SelectOnlyThisComponent(AComponent: TComponent); override;
85 function UniqueName(const BaseName: string): string; override;
86 procedure PrepareFreeDesigner(AFreeComponent: boolean); override;
87 procedure ProposeOpenDesign(const DesignUrl: String); override;
88 procedure FreeComponentRecursively(const C: TComponent); override;
89 end;
90
91 { Helpful class to put in menu to execute a verb from given component editor. }
92 TMenuItemToExecuteVerb = class(TMenuItem)
93 strict private
94 FComponentToExecute: TComponent;
95 FComponentToExecuteObserver: TFreeNotificationObserver;
96 procedure SetComponentToExecute(const AValue: TComponent);
97 procedure ComponentToExecuteFreeNotification(const Sender: TFreeNotificationObserver);
98 public
99 VerbIndex: Integer;
100 ComponentEditor: TBaseComponentEditor;
101 constructor Create(AOwner: TComponent); override;
102 property ComponentToExecute: TComponent read FComponentToExecute write SetComponentToExecute;
103 procedure Click; override;
104 end;
105
106 implementation
107
108 uses CastleLog;
109
110 { TConcreteEditorDesigner --------------------------------------------- }
111
112 constructor TConcreteEditorDesigner.Create(const ADesignFrame: TDesignFrame;
113 const APropertyEditorHook: TPropertyEditorHook);
114 begin
115 inherited Create;
116 FDesignFrame := ADesignFrame;
117 FPropertyEditorHook := APropertyEditorHook;
118 end;
119
120 function TConcreteEditorDesigner.GetPropertyEditorHook: TPropertyEditorHook;
121 begin
122 Result := FPropertyEditorHook;
123 end;
124
125 function TConcreteEditorDesigner.GetShowNonVisualComponents: boolean;
126 begin
127 Result := true; // TODO, unused in practice by CGE components
128 end;
129
130 procedure TConcreteEditorDesigner.SetShowNonVisualComponents(
131 AValue: boolean);
132 begin
133 // TODO, unused in practice by CGE components
134 end;
135
136 procedure TConcreteEditorDesigner.Modified;
137 begin
138 inherited;
139 FDesignFrame.ModifiedOutsideObjectInspector('', ucLow);
140 end;
141
142 function TConcreteEditorDesigner.CopySelection: boolean;
143 begin
144 Result := false; // TODO, unused in practice by CGE components
145 end;
146
147 function TConcreteEditorDesigner.CutSelection: boolean;
148 begin
149 Result := false; // TODO, unused in practice by CGE components
150 end;
151
152 function TConcreteEditorDesigner.CanCopy: Boolean;
153 begin
154 Result := false; // TODO, unused in practice by CGE components
155 end;
156
157 function TConcreteEditorDesigner.CanPaste: Boolean;
158 begin
159 Result := false; // TODO, unused in practice by CGE components
160 end;
161
162 function TConcreteEditorDesigner.PasteSelection(
163 Flags: TComponentPasteSelectionFlags): boolean;
164 begin
165 Result := false; // TODO, unused in practice by CGE components
166 end;
167
168 function TConcreteEditorDesigner.ClearSelection: boolean;
169 begin
170 Result := false; // TODO, unused in practice by CGE components
171 end;
172
173 function TConcreteEditorDesigner.DeleteSelection: boolean;
174 begin
175 Result := false; // TODO, unused in practice by CGE components
176 end;
177
178 function TConcreteEditorDesigner.CopySelectionToStream(s: TStream
179 ): boolean;
180 begin
181 Result := false; // TODO, unused in practice by CGE components
182 end;
183
184 function TConcreteEditorDesigner.InsertFromStream(s: TStream;
185 Parent: TWinControl; Flags: TComponentPasteSelectionFlags): Boolean;
186 begin
187 Result := false; // TODO, unused in practice by CGE components
188 end;
189
190 function TConcreteEditorDesigner.InvokeComponentEditor(
191 AComponent: TComponent): boolean;
192 begin
193 Result := false; // TODO, unused in practice by CGE components
194 end;
195
196 function TConcreteEditorDesigner.ChangeClass: boolean;
197 begin
198 Result := false; // TODO, unused in practice by CGE components
199 end;
200
201 function TConcreteEditorDesigner.CanUndo: Boolean;
202 begin
203 Result := false; // TODO, unused in practice by CGE components
204 end;
205
206 function TConcreteEditorDesigner.CanRedo: Boolean;
207 begin
208 Result := false; // TODO, unused in practice by CGE components
209 end;
210
211 function TConcreteEditorDesigner.Undo: Boolean;
212 begin
213 Result := false; // TODO, unused in practice by CGE components
214 end;
215
216 function TConcreteEditorDesigner.Redo: Boolean;
217 begin
218 Result := false; // TODO, unused in practice by CGE components
219 end;
220
221 function TConcreteEditorDesigner.AddUndoAction(
222 const aPersistent: TPersistent; aOpType: TUndoOpType; IsSetNewId: boolean;
223 aFieldName: string; const aOldVal, aNewVal: variant): boolean;
224 begin
225 Result := false; // TODO, unused in practice by CGE components
226 end;
227
228 function TConcreteEditorDesigner.IsUndoLocked: boolean;
229 begin
230 Result := false; // TODO, unused in practice by CGE components
231 end;
232
233 procedure TConcreteEditorDesigner.AddComponent(
234 const NewRegisteredComponent: TRegisteredComponent;
235 const NewComponentClass: TComponentClass; const NewParent: TComponent;
236 const NewLeft, NewTop, NewWidth, NewHeight: Integer);
237 begin
238 // TODO, unused in practice by CGE components
239 end;
240
241 procedure TConcreteEditorDesigner.AddComponentCheckParent(
242 var NewParent: TComponent; const OriginComponent: TComponent;
243 const OriginWinControl: TWinControl; const NewComponentClass: TComponentClass
244 );
245 begin
246 // TODO, unused in practice by CGE components
247 end;
248
249 procedure TConcreteEditorDesigner.DrawDesignerItems(OnlyIfNeeded: boolean
250 );
251 begin
252 // TODO, unused in practice by CGE components
253 end;
254
255 function TConcreteEditorDesigner.CreateUniqueComponentName(
256 const AClassName: string): string;
257 begin
258 // TODO, unused in practice by CGE components
259 Result := AClassName;
260 end;
261
262 function TConcreteEditorDesigner.IsDesignMsg(Sender: TControl;
263 var Message: TLMessage): Boolean;
264 begin
265 Result := false; // TODO, unused in practice by CGE components
266 end;
267
268 procedure TConcreteEditorDesigner.UTF8KeyPress(var UTF8Key: TUTF8Char);
269 begin
270 // TODO, unused in practice by CGE components
271 end;
272
273 procedure TConcreteEditorDesigner.Notification(AComponent: TComponent;
274 Operation: TOperation);
275 begin
276 // TODO, unused in practice by CGE components
277 end;
278
279 procedure TConcreteEditorDesigner.PaintGrid;
280 begin
281 // TODO, unused in practice by CGE components
282 end;
283
284 procedure TConcreteEditorDesigner.ValidateRename(AComponent: TComponent;
285 const CurName, NewName: string);
286 begin
287 // TODO, unused in practice by CGE components
288 end;
289
290 function TConcreteEditorDesigner.GetShiftState: TShiftState;
291 begin
292 Result := []; // TODO, unused in practice by CGE components
293 end;
294
295 procedure TConcreteEditorDesigner.SelectOnlyThisComponent(
296 AComponent: TComponent);
297 begin
298 // TODO, unused in practice by CGE components
299 end;
300
301 function TConcreteEditorDesigner.UniqueName(const BaseName: string
302 ): string;
303 begin
304 Result := BaseName; // TODO, unused in practice by CGE components
305 end;
306
307 procedure TConcreteEditorDesigner.PrepareFreeDesigner(
308 AFreeComponent: boolean);
309 begin
310 // TODO, unused in practice by CGE components
311 end;
312
313 procedure TConcreteEditorDesigner.ProposeOpenDesign(const DesignUrl: String);
314 begin
315 if Assigned(FDesignFrame.OnProposeOpenDesign) then
316 FDesignFrame.OnProposeOpenDesign(DesignUrl);
317 end;
318
319 procedure TConcreteEditorDesigner.FreeComponentRecursively(const C: TComponent);
320 begin
321 FDesignFrame.FreeComponentRecursively(C);
322 end;
323
324 { TMenuItemToExecuteVerb ----------------------------------------------------- }
325
326 constructor TMenuItemToExecuteVerb.Create(AOwner: TComponent);
327 begin
328 inherited;
329 { We secure this menu item in case ComponentToExecute will be freed.
330 This didn't happen in practice, but it seems possible (if we would
331 not update popup menu often enough) so better secure from it. }
332 FComponentToExecuteObserver := TFreeNotificationObserver.Create(Self);
333 FComponentToExecuteObserver.OnFreeNotification := {$ifdef FPC}@{$endif} ComponentToExecuteFreeNotification;
334 end;
335
336 procedure TMenuItemToExecuteVerb.Click;
337 begin
338 inherited;
339 if ComponentToExecute = nil then
340 begin
341 WritelnWarning('Cannot execute verb of component editor, because the component was freed in the meantime');
342 Exit;
343 end;
344 ComponentEditor.ExecuteVerb(VerbIndex);
345 end;
346
347 procedure TMenuItemToExecuteVerb.SetComponentToExecute(const AValue: TComponent);
348 begin
349 if FComponentToExecute <> AValue then
350 begin
351 FComponentToExecute := AValue;
352 FComponentToExecuteObserver.Observed := AValue;
353 end;
354 end;
355
356 procedure TMenuItemToExecuteVerb.ComponentToExecuteFreeNotification(
357 const Sender: TFreeNotificationObserver);
358 begin
359 ComponentToExecute := nil;
360 end;
361
362 end.