Debian Codegraph @

src/scene/x3d/x3dnodes_mfnode.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2002-2023 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 { VRML/X3D field holding a list of nodes.
20
21 Just like SFNode, it's defined in this unit, as it uses TX3DNode.
22 Note that items of MFNode @italic(cannot) be nil (i.e. VRML/X3D doesn't
23 allow to use NULL inside MFNode), contrary to SFNode.
24
25 Note that TMFNode implementation doesn't use TX3DSimpleMultField.
26 One reason is that we don't want to parse MFNode items
27 by SFNode parser, because MFNode doesn't allow NULL items.
28 (In the past, another argument was that we want to use TX3DNodeList
29 and it wasn't compatible with TX3DSimpleMultField.
30 But now TX3DNodeList descends from TFPSList, so it isn't a problem.)
31
32 Just like for TSFNode:
33 Note that we store AllowedChildren list, which is a list of
34 classes allowed as Items.
35 But this is used only to produce warnings for a user.
36 You should never assert that every item actually is one the requested
37 classes. }
38 TMFNode = class(TX3DMultField)
39 strict private
40 type
41 TEnumerator = record
42 strict private
43 FList: TX3DNodeList;
44 FPosition: Integer;
45 function GetCurrent: TX3DNode; inline;
46 public
47 constructor Create(const AList: TX3DNodeList);
48 function MoveNext: Boolean; inline;
49 property Current: TX3DNode read GetCurrent;
50 end;
51 var
52 FDefaultItems: TX3DNodeList;
53 FDefaultValueExists: boolean;
54 FParentNode: TX3DNode;
55 AllowedChildren: TAllowedChildren;
56 AllowedChildrenClasses: TX3DNodeClassesList;
57 AllowedChildrenFunctionality: TNodeFunctionalityClass;
58 function GetItems(const Index: Integer): TX3DNode;
59 procedure AddSelfParentField(const AItems: TX3DNodeList);
60 procedure AssignItemsDeepCopy(const SourceItems: TX3DNodeList;
61 const CopyState: TX3DNodeDeepCopyState);
62 procedure AssignDefaultItemsDeepCopy(const SourceDefaultItems: TX3DNodeList;
63 const CopyState: TX3DNodeDeepCopyState);
64 private
65 FItems: TX3DNodeList;
66 strict protected
67 procedure SaveToStreamValue(Writer: TX3DWriter); override;
68 function SaveToXmlValue: TSaveToXmlMethod; override;
69 { Get or set the number of items.
70
71 When increasing this, remember that new items of TMFNode
72 will be @nil. You @bold(must immediately initialize them to
73 something else then @nil) by the @link(Replace) method.
74 Other TMFNode methods, and outside code working with MFNodes,
75 usually assumes that all MFNode children are non-nil.
76 (As VRML/X3D spec don't really allow NULL items inside MFNode fields.)
77 @groupBegin }
78 function GetCount: TListSize; override;
79 procedure SetCount(const Value: TListSize); override;
80 { @groupEnd }
81 public
82 { Construct a field allowing any children class.
83 Suitable only for special cases. For example, in instantiated prototypes,
84 we must initially just allow all children, otherwise valid prototypes
85 with SFNode/MFNode would cause warnings when parsing. }
86 constructor CreateUndefined(const AParentNode: TX3DFileItem;
87 const AExposed: boolean; const AName: String); override;
88 constructor Create(const AParentNode: TX3DNode;
89 const AExposed: boolean; const AName: String;
90 const AAllowedChildrenClasses: array of TX3DNodeClass); overload;
91 { Constructor that takes a list of allowed children classes.
92 Note that we copy the contents of AAllowedChildrenClasses,
93 not the reference. }
94 constructor Create(const AParentNode: TX3DNode;
95 const AExposed: boolean; const AName: String;
96 const AAllowedChildrenClasses: TX3DNodeClassesList); overload;
97 { Constructor that allows as children any implementor of given functionality. }
98 constructor Create(const AParentNode: TX3DNode;
99 const AExposed: boolean; const AName: String;
100 const AnAllowedChildrenFunctionality: TNodeFunctionalityClass); overload;
101 destructor Destroy; override;
102
103 function GetEnumerator: TEnumerator;
104
105 procedure Replace(const Index: Integer; Node: TX3DNode);
106
107 { Items of this field. }
108 property Items[const Index: Integer]: TX3DNode read GetItems write Replace; default;
109 {$ifdef FPC}
110 property ItemsArray[Index: Integer]: TX3DNode read GetItems; deprecated 'use Items (default array property)';
111 {$endif}
112
113 { Lists of items in this field.
114 Do not modify this list explicitly. Use only methods in this class
115 like @link(Add). They take care of calling appropriate
116 AddParentField / RemoveParentField, otherwise you
117 can break reference-counting of nodes by ParentFields.
118 @exclude }
119 property InternalItems: TX3DNodeList read FItems;
120
121 procedure Add(Node: TX3DNode); overload;
122 procedure Add(Position: Integer; Node: TX3DNode); overload;
123
124 procedure Delete(Index: Integer);
125 { Search list for given node, and, if found, remove it.
126 Returns the index of removed item, or -1 if not found. }
127 function Remove(const Node: TX3DNode): Integer;
128 { Remove child with given Index, and return it, @italic(never freeing it).
129 This is analogous to TX3DNode.ExtractChild, see there for more
130 explanation. }
131 function Extract(Index: Integer): TX3DNode;
132 procedure Clear;
133 procedure AssignItems(const SourceItems: TX3DNodeList); overload;
134 procedure AssignItems(const SourceItems: array of TX3DNode); overload;
135
136 { @exclude }
137 procedure InternalAssignDeepCopy(const Source: TX3DFieldOrEvent; const CopyState: TObject); override;
138
139 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
140 procedure ParseXMLAttribute(const AttributeValue: String; Reader: TX3DReader); override;
141 procedure ParseXMLElement(Element: TDOMElement; Reader: TX3DReader); override;
142
143 function EqualsDefaultValue: boolean; override;
144 function Equals(SecondValue: TX3DField): boolean; override;
145
146 procedure Assign(Source: TPersistent); override;
147 procedure AssignValue(Source: TX3DField); override;
148 procedure AssignDefaultValueFromValue; override;
149 procedure UnassignDefaultValue; override;
150
151 property ParentNode: TX3DNode read FParentNode;
152
153 class function X3DType: String; override;
154 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
155
156 { Checks is Child allowed on the list of nodes of this MFNode,
157 and makes WritelnWarning if not.
158
159 Check is allowed is done looking at AllowedChildrenAll
160 and AllowedChildren properties.
161
162 Child must not be @nil.
163
164 WritelnWarning message will suggest that this Child is added to
165 this node. In other words, you should only pass as Child
166 a node that you want to add (e.g. by @link(Add)) to this field,
167 otherwise WritelnWarning message will be a little unsensible. }
168 procedure WarningIfChildNotAllowed(Child: TX3DNode);
169
170 function ChildAllowed(Child: TX3DNode): boolean;
171
172 { Lists default items of this field.
173
174 Do not modify this list explicitly. Use only methods in this class
175 like AssignDefaultItems (they take care of calling appropriate
176 AddParentField / RemoveParentField, otherwise you
177 could break reference-counting of nodes by ParentFields). }
178 property DefaultItems: TX3DNodeList read FDefaultItems;
179
180 { Operate on DefaultItems, just like analogous AssignItems and @link(Clear).
181 @groupBegin }
182 procedure AssignDefaultItems(const SourceDefaultItems: TX3DNodeList);
183 procedure ClearDefault;
184 { @groupEnd }
185
186 property DefaultValueExists: boolean
187 read FDefaultValueExists write FDefaultValueExists default false;
188
189 { Calls Func for all current children.
190 Stops if Func returns something non-nil.
191 The main use for this is to simplify implementation of
192 TX3DNode.DirectEnumerateActive overrides in TX3DNode descendants. }
193 function Enumerate(Func: TEnumerateChildrenFunction): Pointer;
194
195 { Set the field's value in a correct way (by sending X3D event, or at least notifying
196 the parent scene). }
197 procedure Send(const AValue: array of TX3DNode); overload;
198
199 // Not needed now
200 //procedure Send(const AValue: TX3DNodeList); overload;
201
202 { Find node by name, @nil if not found.
203
204 For empty node name, always returns @nil. This follows the definition
205 of @link(TX3DNode.X3DName):
206 empty means that node has no name, so it should not be found
207 by searches by name, ever.
208
209 @seealso(IndexOfName Returns the node index. This is sometimes more
210 flexible (you can always quickly get actual node knowing the index,
211 by Items[Index], but not the other way around). It is also sometimes
212 less comfortable (often you're not interested in node index).) }
213 function FindName(const AName: String): TX3DNode;
214
215 { Find index of a node with given name, -1 if not found. }
216 function IndexOfName(const AName: String): Integer;
217 { Find index of a node , -1 if not found. }
218 function IndexOf(const ANode: TX3DNode): Integer;
219 end;
220
221 {$endif read_interface}
222
223 {$ifdef read_implementation}
224
225 { TMFNode -------------------------------------------------------------------- }
226
227 constructor TMFNode.CreateUndefined(const AParentNode: TX3DFileItem;
228 const AExposed: boolean; const AName: String);
229 begin
230 inherited;
231 FItems := TX3DNodeList.Create(false);
232
233 AllowedChildren := acAll;
234 { AllowedChildrenClasses may remain nil in this case }
235
236 FDefaultItems := TX3DNodeList.Create(false);
237 FDefaultValueExists := false;
238 end;
239
240 constructor TMFNode.Create(const AParentNode: TX3DNode;
241 const AExposed: boolean; const AName: String;
242 const AAllowedChildrenClasses: array of TX3DNodeClass);
243 begin
244 CreateUndefined(AParentNode, AExposed, AName);
245 FParentNode := AParentNode;
246
247 AllowedChildren := acClasses;
248 if AllowedChildrenClasses = nil then
249 AllowedChildrenClasses := TX3DNodeClassesList.Create;
250 AllowedChildrenClasses.AssignArray(AAllowedChildrenClasses);
251
252 { In the future, this constructor may also allow setting DefaultItems
253 from parameters. For now, this is not needed anywhere.
254 We assume DefaultItems = [] if you used this constructor. }
255 DefaultValueExists := true;
256 end;
257
258 constructor TMFNode.Create(const AParentNode: TX3DNode;
259 const AExposed: boolean; const AName: String;
260 const AAllowedChildrenClasses: TX3DNodeClassesList);
261 begin
262 Create(AParentNode, AExposed, AName, []);
263
264 Assert(AllowedChildren = acClasses);
265 Assert(AllowedChildrenClasses <> nil);
266 AllowedChildrenClasses.Assign(AAllowedChildrenClasses);
267 end;
268
269 constructor TMFNode.Create(const AParentNode: TX3DNode;
270 const AExposed: boolean; const AName: String;
271 const AnAllowedChildrenFunctionality: TNodeFunctionalityClass);
272 begin
273 CreateUndefined(AParentNode, AExposed, AName);
274 FParentNode := AParentNode;
275
276 AllowedChildren := acFunctionality;
277 AllowedChildrenFunctionality := AnAllowedChildrenFunctionality;
278
279 { In the future, this constructor may also allow setting DefaultItems
280 from parameters. For now, this is not needed anywhere.
281 We assume DefaultItems = [] if you used this constructor. }
282 DefaultValueExists := true;
283 end;
284
285 destructor TMFNode.Destroy;
286 begin
287 Clear;
288 ClearDefault;
289 FreeAndNil(FItems);
290 FreeAndNil(FDefaultItems);
291 FreeAndNil(AllowedChildrenClasses);
292 inherited;
293 end;
294
295 procedure TMFNode.SaveToStreamValue(Writer: TX3DWriter);
296 var
297 I: Integer;
298 N: String;
299 begin
300 N := NameForVersion(Writer.Version);
301 case Writer.Encoding of
302 xeClassic:
303 { We code Count = 0 and Count = 1 cases separately just to get a more
304 compact look in these common situations. }
305 if Count = 0 then
306 Writer.Write('[]') else
307 if Count = 1 then
308 begin
309 { TX3DNode.SaveToStream normally starts from new line with an indent...
310 In this case, we want it to start on the same line, so indent must
311 be discarded. }
312 Writer.DiscardNextIndent;
313 FItems[0].NodeSaveToStream(Writer, N);
314 end else
315 begin
316 Writer.Writeln('[');
317 Writer.IncIndent;
318 for I := 0 to Count - 1 do
319 begin
320 FItems[I].NodeSaveToStream(Writer, N);
321 Writer.Writeln;
322 end;
323 Writer.DecIndent;
324 Writer.WriteIndent(']');
325 end;
326 xeXML:
327 for I := 0 to Count - 1 do
328 FItems[I].NodeSaveToStream(Writer, N);
329 {$ifndef COMPILER_CASE_ANALYSIS}
330 else raise EInternalError.Create('TMFNode.SaveToStreamValue Encoding?');
331 {$endif}
332 end;
333 end;
334
335 function TMFNode.SaveToXmlValue: TSaveToXmlMethod;
336 begin
337 Result := sxChildElement;
338 end;
339
340 function TMFNode.GetCount: TListSize;
341 begin
342 Result := FItems.Count;
343 end;
344
345 procedure TMFNode.SetCount(const Value: TListSize);
346 var
347 I: Integer;
348 begin
349 if Value < FItems.Count then
350 begin
351 for I := Value to FItems.Count - 1 do
352 FItems[I].RemoveParentField(Self);
353 FItems.Count := Value;
354 end else
355 if Value > FItems.Count then
356 begin
357 { TObjectList makes sure that increasing count sets new items to nil }
358 FItems.Count := Value;
359 end;
360 end;
361
362 procedure TMFNode.Add(Node: TX3DNode);
363 begin
364 if Node = nil then
365 raise EX3DError.Create('Cannot add nil node to an MFNode field ' + NiceName);
366 FItems.Add(Node);
367 Node.AddParentField(Self);
368 end;
369
370 procedure TMFNode.Add(Position: Integer; Node: TX3DNode);
371 begin
372 if Node = nil then
373 raise EX3DError.Create('Cannot add nil node to an MFNode field ' + NiceName);
374 FItems.Insert(Position, Node);
375 Node.AddParentField(Self);
376 end;
377
378 procedure TMFNode.Delete(Index: Integer);
379 begin
380 FItems[Index].RemoveParentField(Self);
381 FItems.Delete(Index);
382 end;
383
384 function TMFNode.Remove(const Node: TX3DNode): Integer;
385 begin
386 Result := FItems.IndexOf(Node);
387 if Result <> -1 then
388 Delete(Result);
389 end;
390
391 function TMFNode.Extract(Index: Integer): TX3DNode;
392 begin
393 Result := FItems[Index];
394
395 { Instead of calling Result.RemoveParentField(Self), which would possibly
396 free Result, we manually call FParentFields.Delete. }
397 Result.FParentFields.Remove(Self);
398
399 FItems.Delete(Index);
400 end;
401
402 procedure TMFNode.Replace(const Index: Integer; Node: TX3DNode);
403 begin
404 if FItems[Index] <> Node then
405 begin
406 { Replace is the only method that must work even when items are nil }
407 if FItems[Index] <> nil then
408 FItems[Index].RemoveParentField(Self);
409 FItems[Index] := Node;
410 FItems[Index].AddParentField(Self);
411 end;
412 end;
413
414 procedure TMFNode.Clear;
415 var
416 I: Integer;
417 begin
418 for I := 0 to FItems.Count - 1 do
419 FItems[I].RemoveParentField(Self);
420 FItems.Count := 0;
421 end;
422
423 procedure TMFNode.ClearDefault;
424 var
425 I: Integer;
426 begin
427 for I := 0 to FDefaultItems.Count - 1 do
428 FDefaultItems[I].RemoveParentField(Self);
429 FDefaultItems.Count := 0;
430 end;
431
432 procedure TMFNode.AddSelfParentField(const AItems: TX3DNodeList);
433 var
434 I: Integer;
435 begin
436 for I := 0 to AItems.Count - 1 do
437 AItems[I].AddParentField(Self);
438 end;
439
440 procedure TMFNode.AssignItems(const SourceItems: array of TX3DNode);
441 begin
442 Clear;
443 FItems.Assign(SourceItems);
444 AddSelfParentField(FItems);
445 end;
446
447 procedure TMFNode.AssignItems(const SourceItems: TX3DNodeList);
448 begin
449 Clear;
450 FItems.Assign(SourceItems);
451 AddSelfParentField(FItems);
452 end;
453
454 procedure TMFNode.AssignDefaultItems(const SourceDefaultItems: TX3DNodeList);
455 begin
456 ClearDefault;
457 DefaultItems.Assign(SourceDefaultItems);
458 AddSelfParentField(DefaultItems);
459 end;
460
461 procedure TMFNode.AssignItemsDeepCopy(const SourceItems: TX3DNodeList;
462 const CopyState: TX3DNodeDeepCopyState);
463 var
464 I: Integer;
465 begin
466 Clear;
467 FItems.Assign(SourceItems);
468 for I := 0 to FItems.Count - 1 do
469 FItems[I] := CopyState.DeepCopy(FItems[I]);
470 AddSelfParentField(FItems);
471 end;
472
473 procedure TMFNode.AssignDefaultItemsDeepCopy(const SourceDefaultItems: TX3DNodeList;
474 const CopyState: TX3DNodeDeepCopyState);
475 var
476 I: Integer;
477 begin
478 ClearDefault;
479 DefaultItems.Assign(SourceDefaultItems);
480 for I := 0 to DefaultItems.Count - 1 do
481 DefaultItems[I] := CopyState.DeepCopy(DefaultItems[I]);
482 AddSelfParentField(DefaultItems);
483 end;
484
485 function TMFNode.ChildAllowed(Child: TX3DNode): boolean;
486 begin
487 case AllowedChildren of
488 acAll : Result := true;
489 acClasses : Result := (Child = nil) or (AllowedChildrenClasses.IndexOfAnyAncestor(Child) <> -1);
490 acFunctionality: Result := (Child = nil) or (Child.Functionality(AllowedChildrenFunctionality) <> nil);
491 {$ifndef COMPILER_CASE_ANALYSIS}
492 else raise EInternalError.Create('AllowedChildren?');
493 {$endif}
494 end;
495 end;
496
497 procedure TMFNode.WarningIfChildNotAllowed(Child: TX3DNode);
498
499 procedure ChildNotAllowed;
500 var
501 S: String;
502 begin
503 S := Format('Node "%s" is not allowed in the field "%s"',
504 [Child.X3DType, X3DName]);
505 if ParentNode <> nil then
506 S := S + Format(' of the node "%s"', [ParentNode.X3DType]);
507 WritelnWarning('X3D', S);
508 end;
509
510 begin
511 if not ChildAllowed(Child) then
512 ChildNotAllowed;
513 end;
514
515 procedure TMFNode.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
516
517 procedure ParseOneItem;
518 var
519 Node: TX3DNode;
520 ModifiedPositionInParent: Integer;
521 NewFileItem: TX3DFileItem;
522 begin
523 if ParentNode = nil then
524 begin
525 { Allow MFNode to only contain nodes.
526
527 This was universally good for X3D < 4.0.
528 For X3D 4.0, it still has to be used when MFNode is a default value
529 inside a prototype, we cannot allow other things there because we don't
530 have a "parent node" to associate them with (and it's unclear what would it
531 even be -- parent node of proto definition or instance?).
532
533 Testcase: demo-models\x3d\routes_protos_within_mfnode.x3dv }
534 Node := ParseNode(Lexer, Reader as TX3DReaderNames, false);
535 Add(Node);
536 WarningIfChildNotAllowed(Node);
537
538 //raise EX3DError.Create('Cannot parse MFNode contents in classic encoding without ParentNode, since in X3D 4.0 the MFNode may contain also prototypes, routes');
539 end else
540 begin
541 { We cannot set PositionInParent on children to anything useful,
542 as we don't store the children directly in MFNode anyway.
543 We'd need to extend MFNode to hold more things (like protos) to have useful
544 PositionInParent within.
545 For now, we just store -1 always.
546
547 We will modify it later to something big for things that can safely
548 go at the end - ROUTE, EXPORT, since these only reference existing nodes. }
549 ModifiedPositionInParent := -1;
550
551 NewFileItem := ParseStatement(Lexer, Reader as TX3DReaderNames,
552 ParentNode, Self, false, ModifiedPositionInParent);
553
554 if (NewFileItem is TX3DRoute) or
555 (NewFileItem is TX3DExport) then
556 begin
557 { Make sure to put ROUTE and EXPORT at the end of containing node.
558 Testcase: round-trip (save as X3D classic and read it back)
559 of demo-models\x3d\routes_protos_within_mfnode.x3dv }
560 NewFileItem.PositionInParent := 1000 * 1000 * 1000;
561 end;
562
563 // Generate warning on incorrect child.
564 // Testcase: demo-models/texturing_advanced/warnings/tex3d_composed_warnings.x3dv
565 if NewFileItem is TX3DNode then
566 WarningIfChildNotAllowed(TX3DNode(NewFileItem));
567 end;
568 end;
569
570 begin
571 Clear;
572
573 { Note that we ignore commas when parsing.
574 Commas are ignored (like whitespace) in VRML 2.0 and X3D,
575 and MFNode is only in these versions.
576 So we just parse until "]". }
577 if Lexer.Token = vtOpenSqBracket then
578 begin
579 Lexer.NextToken;
580
581 while Lexer.Token <> vtCloseSqBracket do
582 ParseOneItem;
583
584 Lexer.NextToken;
585 end else
586 begin
587 { one single item - not enclosed in [] brackets }
588 ParseOneItem;
589 end;
590 end;
591
592 procedure TMFNode.ParseXMLAttribute(const AttributeValue: String; Reader: TX3DReader);
593 var
594 Node: TX3DNode;
595 UsedNodeFinished: boolean;
596 begin
597 Node := (Reader as TX3DReaderNames).Bound(AttributeValue, UsedNodeFinished, true);
598 if Node = nil then
599 begin
600 { NULL not allowed for MFNode, unlike the SFNode }
601 WritelnWarning('X3D', Format('Invalid node name for MFNode field: "%s"', [AttributeValue]));
602 end else
603 if not UsedNodeFinished then
604 begin
605 WritelnWarning('X3D', Format('Cycles in X3D graph: MFNode value inside node "%s" refers to the same name', [AttributeValue]));
606 end else
607 begin
608 Add(Node);
609 WarningIfChildNotAllowed(Node);
610 end;
611 end;
612
613 procedure TMFNode.ParseXMLElement(Element: TDOMElement; Reader: TX3DReader);
614 var
615 Child: TX3DNode;
616 I: TXMLElementIterator;
617 ContainerFieldDummy: String;
618 begin
619 { Clear is necessary because MFNode default value may be non-empty,
620 when it's defined by a prototype.
621 See http://web3d.org/pipermail/x3d-public_web3d.org/2016-May/004771.html }
622 Clear;
623
624 I := Element.ChildrenIterator;
625 try
626 while I.GetNext do
627 begin
628 Child := ParseXMLNode(I.Current,
629 ContainerFieldDummy { ignore containerField }, Reader as TX3DReaderNames, true);
630 if Child <> nil then
631 begin
632 Add(Child);
633 WarningIfChildNotAllowed(Child);
634 end;
635 end;
636 finally FreeAndNil(I) end;
637 end;
638
639 function TMFNode.EqualsDefaultValue: boolean;
640 begin
641 Result := DefaultValueExists and DefaultItems.Equals(FItems);
642 end;
643
644 function TMFNode.Equals(SecondValue: TX3DField): boolean;
645 begin
646 Result := (inherited Equals(SecondValue)) and
647 (SecondValue is TMFNode) and
648 (TMFNode(SecondValue).FItems.Equals(FItems));
649 end;
650
651 procedure TMFNode.Assign(Source: TPersistent);
652 begin
653 if Source is TMFNode then
654 begin
655 AssignItems(TMFNode(Source).FItems);
656 AssignDefaultItems(TMFNode(Source).DefaultItems);
657 DefaultValueExists := TMFNode(Source).DefaultValueExists;
658 VRMLFieldAssignCommon(TX3DField(Source));
659 { When updating this, remember to also update TMFNode.InternalAssignDeepCopy }
660 end else
661 inherited;
662 end;
663
664 procedure TMFNode.InternalAssignDeepCopy(const Source: TX3DFieldOrEvent; const CopyState: TObject);
665 var
666 GoodCopyState: TX3DNodeDeepCopyState;
667 begin
668 Assert(Source is TMFNode);
669 GoodCopyState := CopyState as TX3DNodeDeepCopyState;
670
671 { Similar to TMFNode.Assign }
672 AssignItemsDeepCopy (TMFNode(Source).FItems , GoodCopyState);
673 AssignDefaultItemsDeepCopy(TMFNode(Source).DefaultItems, GoodCopyState);
674 DefaultValueExists := TMFNode(Source).DefaultValueExists;
675 VRMLFieldAssignCommon(TX3DField(Source));
676 end;
677
678 procedure TMFNode.AssignValue(Source: TX3DField);
679 begin
680 if Source is TMFNode then
681 begin
682 inherited;
683 AssignItems(TMFNode(Source).FItems);
684 end else
685 AssignValueRaiseInvalidClass(Source);
686 end;
687
688 procedure TMFNode.AssignDefaultValueFromValue;
689 begin
690 inherited;
691 AssignDefaultItems(FItems);
692 DefaultValueExists := true;
693 end;
694
695 procedure TMFNode.UnassignDefaultValue;
696 begin
697 DefaultValueExists := false;
698 end;
699
700 class function TMFNode.X3DType: String;
701 begin
702 Result := 'MFNode';
703 end;
704
705 function TMFNode.Enumerate(Func: TEnumerateChildrenFunction): Pointer;
706 var
707 I: Integer;
708 begin
709 Result := nil;
710 for I := 0 to Count - 1 do
711 { Checking ChildAllowed is not really necessary here,
712 and costs time, because it may do a slow Supports / Functionality call }
713 //if ChildAllowed(Items[I]) then
714 begin
715 Result := Func(ParentNode, FItems[I]);
716 if Result <> nil then Exit;
717 end;
718 end;
719
720 function TMFNode.GetItems(const Index: Integer): TX3DNode;
721 begin
722 Result := FItems[Index];
723 end;
724
725 class function TMFNode.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
726 begin
727 Result := TMFNodeEvent.Create(AParentNode, AName, AInEvent);
728 end;
729
730 procedure TMFNode.Send(const AValue: array of TX3DNode);
731 var
732 FieldValue: TMFNode;
733 begin
734 { implementation just like TX3DSimpleMultField.Send }
735 if SendsByEvent then
736 begin
737 { AExposed = false below, because not needed otherwise. }
738 FieldValue := TMFNode.CreateUndefined(ParentNode, false, X3DName);
739 try
740 FieldValue.AssignItems(AValue);
741 Send(FieldValue);
742 finally FreeAndNil(FieldValue) end;
743 end else
744 begin
745 { When SendsByEvent = false, we can optimize this method.
746 No need to create an intermediate FieldValue instance
747 (and assign AValue to FieldValue, just to later assign FieldValue to Items). }
748 ValueFromIsClause := false;
749 AssignItems(AValue);
750 Changed;
751 end;
752 end;
753
754 function TMFNode.FindName(const AName: String): TX3DNode;
755 begin
756 Result := FItems.FindName(AName);
757 end;
758
759 function TMFNode.IndexOfName(const AName: String): Integer;
760 begin
761 Result := FItems.IndexOfName(AName);
762 end;
763
764 function TMFNode.IndexOf(const ANode: TX3DNode): Integer;
765 begin
766 Result := FItems.IndexOf(ANode);
767 end;
768
769
770 // Not needed now
771 // procedure TMFNode.Send(const AValue: TX3DNodeList);
772 // var
773 // FieldValue: TMFNode;
774 // begin
775 // { This is a copy-paste of implementation of Send(const AValue: array of TItem) }
776 // if SendsByEvent then
777 // begin
778 // FieldValue := TMFNode.CreateUndefined(ParentNode, false, X3DName);
779 // try
780 // FieldValue.Items.AddRange(AValue);
781 // Send(FieldValue);
782 // finally FreeAndNil(FieldValue) end;
783 // end else
784 // begin
785 // ValueFromIsClause := false;
786 // AssignItems(AValue);
787 // Changed;
788 // end;
789 // end;
790
791 function TMFNode.GetEnumerator: TEnumerator;
792 begin
793 Result := TEnumerator.Create(FItems);
794 end;
795
796 { TMFNode.TEnumerator ------------------------------------------------- }
797
798 function TMFNode.TEnumerator.GetCurrent: TX3DNode;
799 begin
800 Result := FList[FPosition];
801 end;
802
803 constructor TMFNode.TEnumerator.Create(const AList: TX3DNodeList);
804 begin
805 // inherited Create;
806 FList := AList;
807 FPosition := -1;
808 end;
809
810 function TMFNode.TEnumerator.MoveNext: Boolean;
811 begin
812 Inc(FPosition);
813 Result := FPosition < FList.Count;
814 end;
815
816 {$endif read_implementation}