Debian Codegraph @

src/transform/castletransform_reference.inc

raw
1 {%MainUnit castletransform.pas}
2 {
3 Copyright 2017-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 {$ifdef read_interface}
18
19 //type
20 { Reference another TCastleTransform instance, to render one TCastleTransform
21 multiple times within the same viewport. }
22 TCastleTransformReference = class(TCastleTransform)
23 strict private
24 FReference: TCastleTransform;
25 FReferenceObserver: TFreeNotificationObserver;
26 FReferenceWrapper: TCastleTransform;
27 procedure SetReference(const Value: TCastleTransform);
28 procedure ReferenceFreeNotification(const Sender: TFreeNotificationObserver);
29 public
30 constructor Create(AOwner: TComponent); override;
31 destructor Destroy; override;
32 function PropertySections(const PropertyName: String): TPropertySections; override;
33 published
34 { Reference another TCastleTransform instance, rendering and processing
35 it as our child. This makes the referenced instance being visible
36 multiple times within the same viewport.
37
38 The referenced instance doesn't have to be present itself in any
39 viewport. But if it is present in some viewport,
40 it has to be the same viewport as our own.
41 In other words, you cannot use the same TCastleTransform instance
42 in 2 different viewports.
43
44 To be more precise, you actually cannot use the same TCastleTransform instance
45 in 2 different TCastleAbstractRootTransform instances
46 (see @link(TCastleTransform.World)).
47 When 2 viewports share the same @code(Items) then (and only then)
48 the same TCastleTransform instance are present in multiple viewports.
49 See https://castle-engine.io/multiple_viewports_to_display_one_world . }
50 property Reference: TCastleTransform read FReference write SetReference;
51 end;
52
53 {$endif read_interface}
54
55 {$ifdef read_implementation}
56
57 { TCastleTransformReference ----------------------------------------------------- }
58
59 constructor TCastleTransformReference.Create(AOwner: TComponent);
60 begin
61 inherited;
62 FReferenceObserver := TFreeNotificationObserver.Create(Self);
63 FReferenceObserver.OnFreeNotification := {$ifdef FPC}@{$endif}ReferenceFreeNotification;
64 { FReference will be child of FReferenceWrapper, not directly of us.
65 This way we avoid serializing it easily. }
66 FReferenceWrapper := TCastleTransform.Create(Self);
67 FReferenceWrapper.SetTransient;
68 { This means that TCastleTransformReference will not modify children's Parent,
69 which in turn means it will nicely work in CGE editor (both the referenced
70 transform directly, and the one wrapped in TCastleTransformReference). }
71 FReferenceWrapper.MeaningfulParent := false;
72 Add(FReferenceWrapper);
73 end;
74
75 destructor TCastleTransformReference.Destroy;
76 begin
77 inherited;
78 end;
79
80 procedure TCastleTransformReference.ReferenceFreeNotification(const Sender: TFreeNotificationObserver);
81 begin
82 FReference := nil;
83 end;
84
85 procedure TCastleTransformReference.SetReference(const Value: TCastleTransform);
86
87 function HasChild(const Parent, Child: TCastleTransform): Boolean;
88 var
89 I: Integer;
90 begin
91 if Parent = Child then
92 Exit(true);
93
94 for I := 0 to Parent.Count - 1 do
95 if HasChild(Parent[I], Child) then
96 Exit(true);
97
98 Result := false;
99 end;
100
101 begin
102 if FReference <> Value then
103 begin
104 { Check that the operation is valid }
105 if Value <> nil then
106 begin
107 { Note that we check HasChild, not HasParent.
108 HasParent would be faster, but would depend on FParent, and we don't want to depend on it
109 when things may have multiple parents. }
110 if HasChild(Value, Self) then
111 begin
112 if Value = Self then
113 raise Exception.CreateFmt('TCastleTransformReference (%s) cannot have a Reference set to itself', [
114 Name
115 ])
116 else
117 raise Exception.CreateFmt('Cannot make a reference to %s (%s), as it is a parent of %s (TCastleTransformReference)', [
118 Value.Name,
119 Value.ClassName,
120 Name
121 ]);
122 end;
123
124 if (Value.World <> nil) and (World <> nil) and (Value.World <> World) then
125 raise Exception.CreateFmt('Cannot make a reference to %s (%s), as it is in a different viewport than %s (TCastleTransformReference)', [
126 Value.Name,
127 Value.ClassName,
128 Name
129 ]);
130 end;
131
132 if FReference <> nil then
133 FReferenceWrapper.Remove(FReference);
134 FReference := Value;
135 FReferenceObserver.Observed := FReference;
136 if FReference <> nil then
137 FReferenceWrapper.Add(FReference);
138 end;
139 end;
140
141 function TCastleTransformReference.PropertySections(
142 const PropertyName: String): TPropertySections;
143 begin
144 if PropertyName = 'Reference' then
145 Result := [psBasic]
146 else
147 Result := inherited PropertySections(PropertyName);
148 end;
149
150 {$endif read_implementation}