Debian Codegraph @

src/scene/x3d/castlefields_x3dfieldorevent.inc

raw
1 {%MainUnit x3dfields.pas}
2 {
3 Copyright 2002-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 { Base class for VRML/X3D field or event. }
20 TX3DFieldOrEvent = class(TX3DFileItem)
21 strict private
22 { To optimize memory usage (which may really be huge in case of VRML/X3D
23 with many nodes, esp. when VRML/X3D uses a lot of prototypes),
24 this is only created when needed (when it is not empty).
25 Otherwise it is @nil. }
26 FIsClauseNames: TCastleStringList;
27
28 FParentNode: TX3DFileItem;
29 FParentInterfaceDeclaration: TX3DFileItem;
30
31 function GetIsClauseNames(const Index: Integer): string;
32
33 { Return FIsClauseNames, initializing it if necessary to not be nil. }
34 function IsClauseNamesCreate: TCastleStringList;
35 private
36 FX3DName: string;
37
38 { Implementation of AddAlternativeName uses it like this:
39 - FAlternativeNames[0] is alternative for any VRML/X3D version
40 - FAlternativeNames[1], [2], [3] is alternative for VRML 1.0, VRML 2.0 (97) and X3D
41 - any FAlternativeNames[...] that is '' should be ignored
42 }
43 FAlternativeNames: array of string;
44 strict protected
45 procedure FieldOrEventAssignCommon(Source: TX3DFieldOrEvent);
46 public
47 constructor Create(const AParentNode: TX3DFileItem; const AX3DName: string);
48 destructor Destroy; override;
49
50 { Name of the field or event.
51
52 Most fields/events are inside some X3D node, and then
53 they have a non-empty name. But in some special cases we
54 also use temporary fields with an empty name.
55
56 Note that you cannot change this after object creation, since
57 Name is used for various purposes (like to generate names for
58 TX3DField.ExposedEvents).
59
60 Note that this property is deliberately not called @code(Name).
61 In the future, this class may descend from the standard TComponent
62 class, that defines a @code(Name) field with a special restrictions
63 (it must be a valid Pascal identifier), which cannot apply to X3D node names
64 (that can have quite free names, see
65 http://www.web3d.org/documents/specifications/19776-2/V3.3/Part02/grammar.html ).
66 We don't want to confuse these two properties. }
67 property X3DName: string read FX3DName;
68 {$ifdef FPC} property Name: string read FX3DName; deprecated 'use X3DName';{$endif}
69
70 { X3D node containing this field/event.
71 This must always contain an instance
72 of TX3DNode class (although it cannot be declared such, since X3DFields
73 unit cannot depend on X3DNodes interface).
74
75 It may be @nil for special fields/events when parent node is unknown. }
76 property ParentNode: TX3DFileItem read FParentNode write FParentNode;
77
78 { "IS" clauses of this field/event, used when this field/event
79 is inside prototype definition.
80
81 This is an array, as one item may have many "IS" clauses (for a field,
82 only one "IS" clause should refer to another field;
83 but you can have many "IS" clauses connecting events,
84 also exposedField may have "IS" clause that should be interpreted
85 actually as links to it's exposed events).
86 See e.g. @code(demo_models/x3d/proto_events_test_3.x3dv).
87
88 Note that having "IS" clauses doesn't mean that the field should
89 be considered "without any value". This is not a good way of thinking,
90 as an exposed field may have an "IS" clause, but linking it to an event,
91 and thus such field has it's value (default value, if not specified
92 in the file), event though it also has an "IS" clause.
93 Although there is TX3DField.ValueFromIsClause, which indicates
94 whether current value was obtained from "IS" clause.
95
96 To be able to significantly optimize memory, we do not expose IsClauseNames
97 as TCastleStringList. Instead operate on them only using below functions.
98 Note that IsClauseNamesAssign can also accept @nil as parameter.
99
100 @groupBegin }
101 property IsClauseNames[const Index: Integer]: string read GetIsClauseNames;
102 function IsClauseNamesCount: Integer;
103 procedure IsClauseNamesAssign(const SourceIsClauseNames: TCastleStringList);
104 procedure IsClauseNamesAdd(const S: string);
105 { @groupEnd }
106
107 { Parse only "IS" clause, if it's not present --- don't do nothing.
108 For example, for the TX3DField descendant, this does not try to parse
109 field value. }
110 procedure ParseIsClause(Lexer: TX3DLexer);
111
112 { Add alternative name for the same field/event, to be used in different
113 VRML version.
114
115 When VRML major version is exactly equal X3DMajorVersion,
116 the AlternativeName should be used --- for both reading and writing
117 of this field/event. In some cases, when reading, we may also allow
118 all versions (both original and alternative), but this is mostly
119 for implementation simplicity --- don't count on it.
120
121 A special value 0 for X3DMajorVersion means that this is just
122 an alternative name, that should be allowed when reading (as alternative
123 to normal Name), and never used when writing.
124
125 Alternative names is a very handy mechanism for cases when
126 the only thing that changed between VRML versions is the field
127 name. Example: Switch node's children/choice, LOD node's children/level,
128 Polyline2D lineSegments/point.
129
130 Note that this also works for ExposedEvents with exposed TX3DField:
131 if a field has alternative names, then it's exposed events always also
132 have appropriate alternative names. }
133 procedure AddAlternativeName(const AlternativeName: string;
134 const X3DMajorVersion: Integer); virtual;
135 procedure RemoveAlternativeName(const X3DMajorVersion: Integer);
136
137 { Returns if S matches current Name or one of the alternative names.
138 Think about it like simple test "Name = S", but actually this
139 checks also names added by AddAlternativeName method. }
140 function IsName(const S: string): boolean;
141
142 { Return how this field should be named for given VRML version.
143 In almost all cases, this simply returns current Name.
144 But it can also return a name added by AddAlternativeName method. }
145 function NameForVersion(Version: TX3DVersion): string; overload;
146 function NameForVersion(Writer: TX3DWriter): string; overload;
147
148 { For fields contained in TX3DInterfaceDeclaration.
149
150 This should always be @nil (if the field is normal, standard field,
151 not coming from interface declaration in X3D file) or an instance of
152 TX3DInterfaceDeclaration. (But it cannot be declared such,
153 since TX3DInterfaceDeclaration is not known in this unit). }
154 property ParentInterfaceDeclaration: TX3DFileItem
155 read FParentInterfaceDeclaration write FParentInterfaceDeclaration;
156
157 { Nice and concise field description for user.
158 Describes parent node type, name and field/event's name. }
159 function NiceName: string;
160
161 { Save IS clauses to stream, only for classic encoding.
162 For each IS clause, writeln field/event name followed by "IS" clause. }
163 procedure SaveToStreamClassicIsClauses(Writer: TX3DWriter);
164
165 { Similar to Assign, but for SFNode / MFNode fields they are copied by DeepCopy,
166 not merely by copying references.
167 Given CopyState is always TX3DNodeDeepCopyState,
168 but it cannot be declared as such here.
169 @exclude }
170 procedure InternalAssignDeepCopy(const Source: TX3DFieldOrEvent;
171 const CopyState: TObject); virtual;
172 end;
173
174 TX3DFieldOrEventList = {$ifdef FPC}specialize{$endif} TObjectList<TX3DFieldOrEvent>;
175
176 {$endif read_interface}
177
178 {$ifdef read_implementation}
179
180 { TX3DFieldOrEvent ---------------------------------------------------------- }
181
182 constructor TX3DFieldOrEvent.Create(const AParentNode: TX3DFileItem;
183 const AX3DName: string);
184 begin
185 inherited Create;
186 FParentNode := AParentNode;
187 FX3DName := AX3DName;
188 end;
189
190 destructor TX3DFieldOrEvent.Destroy;
191 begin
192 FreeAndNil(FIsClauseNames);
193 inherited;
194 end;
195
196 function TX3DFieldOrEvent.IsClauseNamesCreate: TCastleStringList;
197 begin
198 if FIsClauseNames = nil then
199 FIsClauseNames := TCastleStringList.Create;
200 Result := FIsClauseNames;
201 end;
202
203 function TX3DFieldOrEvent.GetIsClauseNames(const Index: Integer): string;
204 begin
205 if FIsClauseNames = nil then
206 raise Exception.CreateFmt('IsClauseNames item index %d does not exist, because IsClauseNames is empty',
207 [Index]);
208 Result := FIsClauseNames[Index];
209 end;
210
211 function TX3DFieldOrEvent.IsClauseNamesCount: Integer;
212 begin
213 if FIsClauseNames = nil then
214 Result := 0 else
215 Result := FIsClauseNames.Count;
216 end;
217
218 procedure TX3DFieldOrEvent.IsClauseNamesAssign(
219 const SourceIsClauseNames: TCastleStringList);
220 begin
221 if (SourceIsClauseNames <> nil) and
222 (SourceIsClauseNames.Count <> 0) then
223 IsClauseNamesCreate.Assign(SourceIsClauseNames) else
224 FreeAndNil(FIsClauseNames);
225 end;
226
227 procedure TX3DFieldOrEvent.IsClauseNamesAdd(const S: string);
228 begin
229 IsClauseNamesCreate.Add(S);
230 end;
231
232 procedure TX3DFieldOrEvent.ParseIsClause(Lexer: TX3DLexer);
233 begin
234 if Lexer.TokenIsKeyword(vkIS) then
235 begin
236 Lexer.NextToken;
237 IsClauseNamesCreate.Add(Lexer.TokenName);
238 Lexer.NextToken;
239 end;
240 end;
241
242 procedure TX3DFieldOrEvent.AddAlternativeName(const AlternativeName: string;
243 const X3DMajorVersion: Integer);
244 begin
245 if X3DMajorVersion >= Length(FAlternativeNames) then
246 SetLength(FAlternativeNames, X3DMajorVersion + 1);
247
248 FAlternativeNames[X3DMajorVersion] := AlternativeName;
249 end;
250
251 procedure TX3DFieldOrEvent.RemoveAlternativeName(const X3DMajorVersion: Integer);
252 begin
253 if X3DMajorVersion < Length(FAlternativeNames) then
254 FAlternativeNames[X3DMajorVersion] := '';
255 end;
256
257 function TX3DFieldOrEvent.IsName(const S: string): boolean;
258 var
259 I: Integer;
260 begin
261 { No field is ever named ''.
262 Actually, we sometimes use '' for special "unnamed fields",
263 in this case it's Ok that no name matches their name.
264 Besides, we don't want empty FAlternativeNames to match when
265 searching for S = ''. }
266
267 if S = '' then
268 Exit(false);
269
270 for I := Low(FAlternativeNames) to High(FAlternativeNames) do
271 if FAlternativeNames[I] = S then
272 Exit(true);
273
274 Result := X3DName = S;
275 end;
276
277 function TX3DFieldOrEvent.NameForVersion(Version: TX3DVersion): string;
278 begin
279 if Version.Major < Length(FAlternativeNames) then
280 Result := FAlternativeNames[Version.Major]
281 else
282 Result := '';
283
284 if Result = '' then
285 Result := X3DName;
286 end;
287
288 function TX3DFieldOrEvent.NameForVersion(
289 Writer: TX3DWriter): string;
290 begin
291 Result := NameForVersion(Writer.Version);
292 end;
293
294 procedure TX3DFieldOrEvent.FieldOrEventAssignCommon(Source: TX3DFieldOrEvent);
295 begin
296 FX3DName := Source.X3DName;
297 IsClauseNamesAssign(Source.FIsClauseNames);
298 FPositionInParent := Source.PositionInParent;
299 FAlternativeNames := Source.FAlternativeNames;
300 end;
301
302 function TX3DFieldOrEvent.NiceName: string;
303 begin
304 Result := '';
305
306 if ParentNode <> nil then
307 Result := Result + TX3DNode(ParentNode).NiceName + '.';
308
309 if X3DName <> '' then
310 Result := Result + X3DName
311 else
312 Result := Result + '<not named field>';
313 end;
314
315 procedure TX3DFieldOrEvent.SaveToStreamClassicIsClauses(Writer: TX3DWriter);
316 var
317 N: string;
318 I: Integer;
319 begin
320 N := NameForVersion(Writer);
321
322 { When N = '', we assume that field/event has only one "IS" clause.
323 Otherwise results don't make any sense. }
324 for I := 0 to IsClauseNamesCount - 1 do
325 begin
326 if N <> '' then
327 Writer.WriteIndent(N + ' ');
328 Writer.Writeln('IS ' + IsClauseNames[I]);
329 end;
330 end;
331
332 procedure TX3DFieldOrEvent.InternalAssignDeepCopy(const Source: TX3DFieldOrEvent;
333 const CopyState: TObject);
334 begin
335 Assign(Source);
336 end;
337
338 {$endif read_implementation}