| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-2024 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 |
{ TX3DNode class. Huge class, the basic building block of X3D nodes. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
{ Private stuff for TX3DNode.DeepCopy and friends implementation. } |
| 22 |
TX3DNodeDeepCopyState = class |
| 23 |
private |
| 24 |
type |
| 25 |
TNodeNodeMap = {$ifdef FPC}specialize{$endif} TDictionary<TX3DNode, TX3DNode>; |
| 26 |
TExportExportMap = {$ifdef FPC}specialize{$endif} TDictionary<TX3DExport, TX3DExport>; |
| 27 |
var |
| 28 |
NodeOriginalToCopy: TNodeNodeMap; |
| 29 |
ExportOriginalToCopy: TExportExportMap; |
| 30 |
public |
| 31 |
constructor Create; |
| 32 |
destructor Destroy; override; |
| 33 |
{ Return a copy or OriginalNode. |
| 34 |
|
| 35 |
To keep sharing of nodes (DEF/USE mechanism) within the newly |
| 36 |
created copy, we need a list of already duplicated children. |
| 37 |
This method uses and updates such list. When called for the |
| 38 |
first time with OriginalNode, it actually creates a duplicate |
| 39 |
(by OriginalNode.DeepCopy). Next time, it will just return |
| 40 |
this copy. } |
| 41 |
function DeepCopy(const OriginalNode: TX3DNode): TX3DNode; |
| 42 |
end; |
| 43 |
|
| 44 |
{ Simple component system inside TX3DNode. |
| 45 |
See @link(TX3DNode.Functionality). } |
| 46 |
TNodeFunctionality = class |
| 47 |
strict private |
| 48 |
FParent: TX3DNode; |
| 49 |
public |
| 50 |
constructor Create(const AParent: TX3DNode); |
| 51 |
property Parent: TX3DNode read FParent; |
| 52 |
end; |
| 53 |
|
| 54 |
TNodeFunctionalityList = {$ifdef FPC}specialize{$endif} TObjectList<TNodeFunctionality>; |
| 55 |
|
| 56 |
TNodeFunctionalityClass = class of TNodeFunctionality; |
| 57 |
|
| 58 |
TFindNodeOption = (fnNilOnMissing, fnOnlyActive); |
| 59 |
TFindNodeOptions = set of TFindNodeOption; |
| 60 |
|
| 61 |
{ X3D node. Every X3D node class descends from this. } |
| 62 |
TX3DNode = class(TX3DFileItem) |
| 63 |
strict private |
| 64 |
FFunctionalityList: TNodeFunctionalityList; //< may be @nil if none |
| 65 |
FTransformFunctionality: TTransformFunctionality; |
| 66 |
FTimeFunctionality: TTimeDependentFunctionality; |
| 67 |
FGenTexFunctionality: TGeneratedTextureFunctionality; |
| 68 |
private |
| 69 |
FX3DName: string; |
| 70 |
FBaseUrl: String; |
| 71 |
FVRML1Children, FVRML1Parents: TX3DNodeList; |
| 72 |
FKeepExisting: Cardinal; |
| 73 |
FParentFields: TX3DFieldList; |
| 74 |
FPrototypes: TX3DPrototypeBaseList; |
| 75 |
FRoutes: TX3DRouteList; |
| 76 |
FImports, FExports: TX3DFileItemList; |
| 77 |
FFields, FFieldsSFNode, FFieldsMFNode: TX3DFieldList; |
| 78 |
FEvents: TX3DEventList; |
| 79 |
FPrototypeInstance: boolean; |
| 80 |
FPrototypeInstanceSourceNode: TX3DPrototypeNode; |
| 81 |
FPrototypeInstanceHelpers: TX3DNode; |
| 82 |
FDefaultContainerField: string; |
| 83 |
FHasInterfaceDeclarations: TX3DAccessTypes; |
| 84 |
FInterfaceDeclarations: TX3DInterfaceDeclarationList; |
| 85 |
FCDataField: TMFString; |
| 86 |
FDestructionNotifications: TNodeDestructionNotificationList; |
| 87 |
FScene: TX3DEventsEngine; |
| 88 |
FInternalSceneShape: TObject; |
| 89 |
|
| 90 |
{ Internally used by PROTO instantiating mechanism, |
| 91 |
in TX3DPrototypeNode.InstantiateIsClauses |
| 92 |
and TX3DPrototypeNode.Instantiate. |
| 93 |
|
| 94 |
This is always @false outside of TX3DPrototypeNode.Instantiate |
| 95 |
implementation (as TX3DPrototypeNode.Instantiate sets it to @true |
| 96 |
and changes back to @false for appropriate nodes). } |
| 97 |
NeedsInstantiateIsClause: boolean; |
| 98 |
|
| 99 |
function GetVRML1Child(const I: Integer): TX3DNode; |
| 100 |
function GetVRML1Parent(const I: Integer): TX3DNode; |
| 101 |
procedure SetVRML1Child(const I: Integer; const Value: TX3DNode); |
| 102 |
function GetParentFieldsItem(Index: Integer): TX3DField; |
| 103 |
function GetParentFieldsNodeItem(Index: Integer): TX3DNode; |
| 104 |
procedure RemoveParentField(const Field: TX3DField); |
| 105 |
procedure AddParentField(const Field: TX3DField); |
| 106 |
procedure SetHasInterfaceDeclarations(const Value: TX3DAccessTypes); |
| 107 |
{ Internal save to stream utility. |
| 108 |
When CurrentContainerField is not empty, it means we know our containing field |
| 109 |
(and should adjust containerField= for XML encoding output). } |
| 110 |
procedure NodeSaveToStream(Writer: TX3DWriter; const CurrentContainerField: string = ''); |
| 111 |
function TryFindNodeStateTraverse( |
| 112 |
ANode: TX3DNode; StateStack: TX3DGraphTraverseStateStack; |
| 113 |
ParentInfo: PTraversingInfo; var TraverseIntoChildren: boolean): Pointer; |
| 114 |
function TryFindNodeTransformTraverse( |
| 115 |
ANode: TX3DNode; StateStack: TX3DGraphTraverseStateStack; |
| 116 |
ParentInfo: PTraversingInfo; var TraverseIntoChildren: boolean): Pointer; |
| 117 |
function GetPrototypes(const Index: Integer): TX3DPrototypeBase; |
| 118 |
function GetRoutes(const Index: Integer): TX3DRoute; |
| 119 |
function GetImports(const Index: Integer): TX3DImport; |
| 120 |
function GetExports(const Index: Integer): TX3DExport; |
| 121 |
function GetFields(const Index: Integer): TX3DField; |
| 122 |
function GetEvents(const Index: Integer): TX3DEvent; |
| 123 |
procedure UnregisterSceneCallback(Node: TX3DNode); |
| 124 |
|
| 125 |
{ Move shape associations (created using TShapeTree.AssociateNode) |
| 126 |
before TSFNode.Value changes (this is compatible with TSFNode.OnBeforeValueChanged). |
| 127 |
|
| 128 |
Assumes that current node has shape associations, and Sender is a field of this node, |
| 129 |
and it is also associated with the same shapes. |
| 130 |
Our InternalSceneShape will be transferred from old to new value of this |
| 131 |
TSFNode field. |
| 132 |
|
| 133 |
Does not move associations in some children nodes. |
| 134 |
For this, assign specialized OnBeforeValueChanged implementation |
| 135 |
instead of this, like TAbstractShapeNode.SetAppearanceBefore. } |
| 136 |
procedure MoveShapeAssociations(const Sender: TSFNode; const NewNode: TX3DNode); |
| 137 |
protected |
| 138 |
{ Make a specified warning, but only once for this node instance. |
| 139 |
Uses WarningDone to track if warning was already done. |
| 140 |
Warning will contain automatically NiceName, so X3D node name and type. } |
| 141 |
procedure WarningOnce(var WarningDone: boolean; const Message: string); |
| 142 |
|
| 143 |
{ Does actual DeepCopy work. You can override this to copy some |
| 144 |
more properties for descendants. } |
| 145 |
function DeepCopyCore(CopyState: TX3DNodeDeepCopyState): TX3DNode; virtual; |
| 146 |
|
| 147 |
{ This should be a mere call to constructor of your own class. |
| 148 |
|
| 149 |
In TX3DNode, this simply calls default virtual constructor, |
| 150 |
which is Ok for all normal nodes. But we have some special nodes, |
| 151 |
like TX3DPrototypeNode or TX3DUnknownNode, that simply cannot |
| 152 |
be created by default constructor. They need to override this. } |
| 153 |
function DeepCopyCreate(CopyState: TX3DNodeDeepCopyState): TX3DNode; virtual; |
| 154 |
|
| 155 |
{ Register new TNodeFunctionality instance to be returned by @link(Functionality), |
| 156 |
and automatically freed when this node is freed. } |
| 157 |
procedure AddFunctionality(const F: TNodeFunctionality); |
| 158 |
|
| 159 |
{ Use this instead of DefaultContainerField to account that containerField |
| 160 |
may change depending on X3D version (like containerField of metadata nodes, |
| 161 |
changed between X3D 3 and 4) or parent node (like containerField of |
| 162 |
HAnimMotion). ParentNode may be @nil when it is at root. } |
| 163 |
function DefaultContainerFieldInContext( |
| 164 |
const Version: TX3DVersion; const ParentNode: TX3DNode): String; virtual; |
| 165 |
protected |
| 166 |
{ Are VRML 1.0 children allowed. |
| 167 |
This condition is checked in VRML1ChildAdd, so it's strictly |
| 168 |
impossible to add a node that is not allowed. |
| 169 |
|
| 170 |
Note that in some special cases VRML1ChildrenAllowed and |
| 171 |
VRML1ChildrenParsingAllowed values may be changed during object lifetime. |
| 172 |
Currently, this may concern TX3DUnknownNode. |
| 173 |
|
| 174 |
Default @false. } |
| 175 |
VRML1ChildrenAllowed: boolean; |
| 176 |
|
| 177 |
{ VRML 1.0 children allowed to be added during parsing. |
| 178 |
This is used only by *Inline nodes for now, that do not allow |
| 179 |
reading children during parsing but may get new children |
| 180 |
in memory. So their VRML1ChildrenParsingAllowed must be empty, |
| 181 |
but VRML1ChildrenAllowed must allow all. |
| 182 |
|
| 183 |
When VRML1ChildrenAllowed is @false then VRML1ChildrenParsingAllowed should |
| 184 |
also be @false. |
| 185 |
|
| 186 |
Default @false. } |
| 187 |
VRML1ChildrenParsingAllowed: boolean; |
| 188 |
|
| 189 |
{ Should SaveToStream save our VRML1Children. |
| 190 |
In this class default implementation returns @true, |
| 191 |
this is what you will want in 99% of cases. |
| 192 |
It's useful to set this to false if you use |
| 193 |
VRML1Children internally, e.g. *Inline nodes. } |
| 194 |
class function VRML1ChildrenSaveToStream: boolean; virtual; |
| 195 |
|
| 196 |
{ Enumerate all active child nodes of given node. |
| 197 |
|
| 198 |
"Active nodes" are the ones affecting current look or collisions, |
| 199 |
e.g. from Switch node only one child will be enumerated. |
| 200 |
See @link(Traverse) for more precise definition. |
| 201 |
|
| 202 |
"Direct" means that this enumerates only direct descendants, |
| 203 |
i.e. this is not recursive. See methods like Traverse or EnumerateNodes |
| 204 |
if you want recursive behavior. |
| 205 |
|
| 206 |
This can enumerate both @link(VRML1Children) nodes |
| 207 |
and nodes within TSFNode and TMFNode fields. |
| 208 |
|
| 209 |
Default implementation in this class enumerates all Children |
| 210 |
nodes of VRML 1.0. If you need to remove some children |
| 211 |
for VRML 1.0 (e.g. for Switch or LOD nodes) |
| 212 |
or add some children for VRML 2.0 you |
| 213 |
have to override this. @bold(You do not need to call inherited |
| 214 |
when overriding this --- in fact, you should not, if you |
| 215 |
want to omit some nodes.) |
| 216 |
|
| 217 |
Stops and returns immediately if Func returns non-nil for some child. } |
| 218 |
function DirectEnumerateActive( |
| 219 |
Func: TEnumerateChildrenFunction): Pointer; virtual; |
| 220 |
|
| 221 |
{ Enumerate all active child nodes of given node, |
| 222 |
and may additionally modify StateStack. It's used by Traverse. |
| 223 |
|
| 224 |
Default implementation in this class simply calls |
| 225 |
DirectEnumerateActive, ignoring StateStack, and this is suitable |
| 226 |
for 99% of nodes. However, for some special nodes (only Collision |
| 227 |
node for now), they have to modify state during traversing into |
| 228 |
various children, and then they can override this. |
| 229 |
|
| 230 |
Stops and returns immediately in Func returns non-nil for some child. } |
| 231 |
function DirectEnumerateActiveForTraverse( |
| 232 |
Func: TEnumerateChildrenFunction; |
| 233 |
StateStack: TX3DGraphTraverseStateStack): Pointer; virtual; |
| 234 |
|
| 235 |
{ Simply enumerate all direct descendant nodes. |
| 236 |
That is, all children in VRML 1.0 style and |
| 237 |
all nodes in SFNode and MFNode fields. |
| 238 |
This includes prototype stuff, if this node is expanded |
| 239 |
from prototype: PrototypeInstanceSourceNode and PrototypeInstanceHelpers. } |
| 240 |
function DirectEnumerateAll( |
| 241 |
Func: TEnumerateChildrenFunction): Pointer; |
| 242 |
|
| 243 |
{ This enumerates direct descendant nodes of this node. |
| 244 |
This is equivalent to DirectEnumerateActive or |
| 245 |
DirectEnumerateAll, depending on value of OnlyActive param. } |
| 246 |
function DirectEnumerate( |
| 247 |
Func: TEnumerateChildrenFunction; |
| 248 |
OnlyActive: boolean): Pointer; |
| 249 |
|
| 250 |
{ Override these methods to determine what happens when |
| 251 |
given node is traversed during Traverse call. |
| 252 |
The main use of this is to operate on TX3DGraphTraverseStateStack. |
| 253 |
|
| 254 |
Remember to always call inherited when overriding. |
| 255 |
In BeforeTraverse and MiddleTraverse you should call inherited |
| 256 |
at the beginning, in AfterTraverse inherited should be called at the end. |
| 257 |
|
| 258 |
Besides changing StateStack.Top fields, you can do push/pop |
| 259 |
on the stack. Remember that if you do StateStack.Push in BeforeTraverse, |
| 260 |
and then you @italic(must call StateStack.Pop in AfterTraverse). |
| 261 |
|
| 262 |
@groupBegin } |
| 263 |
procedure BeforeTraverse(StateStack: TX3DGraphTraverseStateStack); virtual; |
| 264 |
procedure MiddleTraverse(StateStack: TX3DGraphTraverseStateStack); virtual; |
| 265 |
procedure AfterTraverse(StateStack: TX3DGraphTraverseStateStack); virtual; |
| 266 |
{ @groupEnd } |
| 267 |
|
| 268 |
{ Parse VRML node body element. Usually, this is a field. |
| 269 |
May also be VRML 1.0 style child node. |
| 270 |
May also be VRML 2.0 Script node interface declaration, etc. |
| 271 |
--- see VRML 2.0 grammar spec. |
| 272 |
|
| 273 |
This should be overriden to parse special features within particular |
| 274 |
nodes. While generally VRML is very clean and there's no need to |
| 275 |
override this, there's one use for this currently: |
| 276 |
|
| 277 |
@orderedList( |
| 278 |
@item(Since we handle a couple of VRML flavors (at least |
| 279 |
Inventor, VRML 1.0 and VRML 97), sometimes the same node has |
| 280 |
different fields to express the same things in various VRML flavors. |
| 281 |
So it may be useful to parse a field and copy it's value into |
| 282 |
other fields. |
| 283 |
|
| 284 |
Example: TShapeHintsNode_1 in Inventor parses "hints" field, |
| 285 |
and copies it's value to other fields as appropriate. |
| 286 |
"hints" field is not exposed in TShapeHintsNode_1 interface, |
| 287 |
so everything is clean in the interface, and under the hood |
| 288 |
TShapeHintsNode_1 can "magically" handle "hints" field for Inventor.) |
| 289 |
) |
| 290 |
|
| 291 |
When overriding, always check inherited result first, and exit if |
| 292 |
inherited handled successfully. |
| 293 |
Otherwise either read your stuff and return @true |
| 294 |
(Lexer should advance to the position of next "nodeBodyElement"). |
| 295 |
Or return @false without changing Lexer position. } |
| 296 |
function ParseNodeBodyElement(Lexer: TX3DLexer; Reader: TX3DReaderNames; |
| 297 |
const APositionInParent: Integer): boolean; virtual; |
| 298 |
|
| 299 |
{ Called at the end of parsing this node (including children) in any encoding. } |
| 300 |
procedure ParseAfter(Reader: TX3DReaderNames); virtual; |
| 301 |
|
| 302 |
procedure CreateNode; virtual; |
| 303 |
|
| 304 |
{ The field where CDATA section from XML is added. |
| 305 |
Used when loading X3D in XML encoding: XML elements may contain |
| 306 |
CDATA sections, that are added to "url" field. |
| 307 |
See X3D XML encoding specification about |
| 308 |
"Encapsulating Script node code" |
| 309 |
[http://www.web3d.org/x3d/specifications/ISO-IEC-19776-1.2-X3DEncodings-XML/Part01/concepts.html#EncapsulatingScriptNodeCode]. |
| 310 |
We also allow this for shader nodes (sensible (follows the intention |
| 311 |
of the spec) and compatible with InstantReality). |
| 312 |
|
| 313 |
When not assigned, then CDATA section for this node is not allowed. |
| 314 |
|
| 315 |
This should be set in descendants constructor. } |
| 316 |
property CDataField: TMFString read FCDataField write FCDataField; |
| 317 |
|
| 318 |
{ React when this node is processed as an immediate child of a grouping node, |
| 319 |
within grouping node's BeforeTraverse. } |
| 320 |
procedure GroupBeforeTraverse(const State: TX3DGraphTraverseState; var WasPointingDeviceSensor: Boolean); virtual; |
| 321 |
public |
| 322 |
{ Node fields. |
| 323 |
|
| 324 |
For normal nodes, all fields are created and added |
| 325 |
using AddField from the constructor. Fields default values are set, |
| 326 |
and current field values are set to these defaults. |
| 327 |
Later, we only modify these fields current values (e.g. when parsing). |
| 328 |
|
| 329 |
However, there are special node classes that set their fields differently. |
| 330 |
TX3DPrototypeNode has their fields set according to it's VRML 2.0 prototype. |
| 331 |
TX3DUnknownNode may have it's fields set by VRML 1.0 "fields" feature |
| 332 |
(so it's Fields are initialized by parsing it). |
| 333 |
|
| 334 |
Nodes with HasInterfaceDeclarations have some Fields and Events |
| 335 |
added when reading node. |
| 336 |
|
| 337 |
All fields on this list are owned by this instance. |
| 338 |
@groupBegin } |
| 339 |
property Fields [const Index: Integer]: TX3DField read GetFields; |
| 340 |
function FieldsCount: Integer; |
| 341 |
procedure AddField(const Value: TX3DField); |
| 342 |
function IndexOfField(const AName: string): Integer; |
| 343 |
{ @groupEnd } |
| 344 |
|
| 345 |
{ Explicit events (that is, not exposed by some field) of this node. |
| 346 |
For exposed events, see each field's property ExposedEvents. |
| 347 |
@groupBegin } |
| 348 |
property Events [const Index: Integer]: TX3DEvent read GetEvents; |
| 349 |
function EventsCount: Integer; |
| 350 |
procedure AddEvent(const Value: TX3DEvent); |
| 351 |
function IndexOfEvent(const AName: string): Integer; |
| 352 |
{ @groupEnd } |
| 353 |
|
| 354 |
{ Search by name for given field. |
| 355 |
When RaiseOnError=false (default) returns @nil when not found. |
| 356 |
When RaiseOnError=true raises EX3DNotFound when not found. |
| 357 |
@raises EX3DNotFound } |
| 358 |
function Field(const AName: string; const RaiseOnError: Boolean = false): TX3DField; |
| 359 |
|
| 360 |
{ Search by name for given field or event (exposed by some field or not). |
| 361 |
When RaiseOnError=false (default) returns @nil when not found. |
| 362 |
When RaiseOnError=true raises EX3DNotFound when not found. |
| 363 |
@raises EX3DNotFound } |
| 364 |
function FieldOrEvent(const AName: string; const RaiseOnError: Boolean = false): TX3DFieldOrEvent; |
| 365 |
|
| 366 |
{ Search by name for given event (exposed by some field or not). |
| 367 |
When RaiseOnError=false (default) returns @nil when not found. |
| 368 |
When RaiseOnError=true raises EX3DNotFound when not found. |
| 369 |
@raises EX3DNotFound } |
| 370 |
function AnyEvent(const AName: string; const RaiseOnError: Boolean = false): TX3DEvent; |
| 371 |
|
| 372 |
{ VRML 1.0 children nodes. These are nodes directly specified inside |
| 373 |
a VRML 1.0 node, they don't belong to any node field. (In VRML 1.0, |
| 374 |
there was no SFNode / MFNode fields.) |
| 375 |
|
| 376 |
In VRML 2.0, this is always empty. |
| 377 |
|
| 378 |
VRML 1.0 nodes may have any number of children. |
| 379 |
The children nodes refer back to it's parent nodes in VRML1Parents list. |
| 380 |
When travelling over VRML/X3D graph, remember that cycles |
| 381 |
are possible, because of DEF/USE. |
| 382 |
Obviously, they possible when travelling along the VRML1Parents list. |
| 383 |
We currently assume that there are no cycles when we treat the graph |
| 384 |
as directed, but it may change one day (some VRML/X3D models |
| 385 |
create real cycles). |
| 386 |
|
| 387 |
Adding/removing stuff from the VRML1Children list keeps track |
| 388 |
of how many times a node is used. If the child node has no parents, |
| 389 |
it will be freed. |
| 390 |
Actually, nodes can be children of both nodes (VRML 1.0 style, |
| 391 |
then VRML1Children and VRML1Parents is used) or fields (TMFNode or TSFNode, |
| 392 |
in VRML 2.0 style; then ParentFields is used). So the node is freed |
| 393 |
only when it's not referenced by any node and not referenced by any |
| 394 |
field. Generally, it's the parent that takes care of reference-counting |
| 395 |
and freeing the children, not the other way around. |
| 396 |
|
| 397 |
Note that given node instance may be a children of a single node |
| 398 |
multiple times, through DEF/USE mechanism. The order of children |
| 399 |
is important and preserved. (On the other hand, the order of |
| 400 |
VRML1Parents is not important, as that list is mostly for reference-counting.) |
| 401 |
|
| 402 |
You can replace one children with another like |
| 403 |
@code(VRML1Children[I] := NewChildren;). |
| 404 |
This works like a shortcut for |
| 405 |
@code(VRML1ChildRemove(I); VRML1ChildAdd(I, NewChildren);). |
| 406 |
But 1. it's more efficient; 2. it's safer --- if Children[I] |
| 407 |
is already equal to NewChildren, it does nothing. |
| 408 |
|
| 409 |
@groupBegin } |
| 410 |
property VRML1Children [const I: Integer]: TX3DNode read GetVRML1Child write SetVRML1Child; |
| 411 |
function VRML1ChildrenCount: integer; |
| 412 |
{ @groupEnd } |
| 413 |
|
| 414 |
{ Add a VRML 1.0 child node at given position. |
| 415 |
Index (position) must be in [0..VRML1ChildrenCount]. |
| 416 |
Items at and above Index position are moved to the right, |
| 417 |
to insert new child at Index position. } |
| 418 |
procedure VRML1ChildAdd(const Index: Integer; const Child: TX3DNode); overload; |
| 419 |
|
| 420 |
{ Add a VRML 1.0 child node at the end of VRML1Children list. } |
| 421 |
procedure VRML1ChildAdd(const Child: TX3DNode); overload; |
| 422 |
|
| 423 |
procedure VRML1ChildRemove(const I: Integer); |
| 424 |
procedure VRML1ChildrenClear; |
| 425 |
|
| 426 |
{ Remove a VRML 1.0 children at index I and return it (do not free it). |
| 427 |
Compare this with VRML1Remove, that removes children I and |
| 428 |
frees it if it's no longer used by anything. |
| 429 |
|
| 430 |
This removes children I, appropriately adjusting |
| 431 |
all parent / children links, but even if a node is unused after removing, |
| 432 |
it is not freed. It's always returned. } |
| 433 |
function VRML1ChildExtract(const I: Integer): TX3DNode; |
| 434 |
|
| 435 |
{ All VRML 1.0 parent nodes. VRML1Parents is a reverse of |
| 436 |
VRML1Children --- it lists all the nodes where we are on VRML1Children |
| 437 |
list. |
| 438 |
|
| 439 |
@groupBegin } |
| 440 |
property VRML1Parents [const I: Integer] :TX3DNode read GetVRML1Parent; |
| 441 |
function VRML1ParentsCount: integer; |
| 442 |
{ @groupEnd } |
| 443 |
|
| 444 |
{ All SFNode and MFNode fields where this node is referenced. |
| 445 |
This is somewhat analogous for VRML1Parents, but for VRML 2.0. |
| 446 |
|
| 447 |
ParentFieldsNode is just for your comfort, it returns always |
| 448 |
appropriate field's ParentNode property value |
| 449 |
(i.e. @code((ParentField[Index] as TSFNode).ParentNode) |
| 450 |
or @code((ParentField[Index] as TMFNode).ParentNode)). |
| 451 |
|
| 452 |
@groupBegin } |
| 453 |
property ParentFields[Index: Integer]: TX3DField read GetParentFieldsItem; |
| 454 |
property ParentFieldsNode[Index: Integer]: TX3DNode |
| 455 |
read GetParentFieldsNodeItem; |
| 456 |
function ParentFieldsCount: Integer; |
| 457 |
{ @groupEnd } |
| 458 |
|
| 459 |
{ Free this object (if it's not @nil) @italic(also removing |
| 460 |
it from @bold(all) parent nodes and fields). |
| 461 |
|
| 462 |
By design, normal destructor (Destroy called by Free) |
| 463 |
doesn't care about removing references to this object from |
| 464 |
it's parents. That's because it's the parents that usually |
| 465 |
initialize freeing of their children, and they free child |
| 466 |
when it's reference count is 0. So this freeing method |
| 467 |
is special in this regard. |
| 468 |
|
| 469 |
Use this if you really want to remove all node occurrences from the middle |
| 470 |
of VRML hierarchy. } |
| 471 |
procedure FreeRemovingFromAllParents; |
| 472 |
|
| 473 |
{ Free this node if it is not referenced by any parent fields or nodes. |
| 474 |
Takes into account that node may have VRML 1.0 parent nodes |
| 475 |
and VRML 2.0 / X3D parent fields (SFNode or MFNode). |
| 476 |
This is a safe way of removing |
| 477 |
a node that may, but doesn't have to, be part of some VRML/X3D graph. |
| 478 |
The idea is that if a node is a part of some graph, |
| 479 |
we don't need to do anything (since you should have |
| 480 |
a reference to the entine graph somewhere anyway), otherwise node is |
| 481 |
considered unused and freed. |
| 482 |
|
| 483 |
Analogous to standard TObject.Free, this also works when called on |
| 484 |
a @nil value (does nothing in this case). |
| 485 |
|
| 486 |
For safety, it's advised to set reference to @nil after calling FreeIfUnused. |
| 487 |
You can use FreeIfUnusedAndNil utility for this (that employs a trick |
| 488 |
to @nil the visible reference even before freeing, which is even safer). |
| 489 |
This is analogous to standard FreeAndNil. } |
| 490 |
procedure FreeIfUnused; |
| 491 |
|
| 492 |
{ When KeepExisting is non-zero, the node will not be automatically freed |
| 493 |
when it will become unused (when reference count changes from non-zero to zero). |
| 494 |
This is useful when you want to control the node lifetime explicitly. |
| 495 |
|
| 496 |
For example, you may want to remove the node from X3D graph |
| 497 |
hierarchy, but keep a valid reference to it anyway |
| 498 |
(for example, maybe you'll free it later by hand or add it somewhere |
| 499 |
else). |
| 500 |
|
| 501 |
Methods KeepExistingBegin and KeepExistingEnd simply increase / |
| 502 |
decrease KeepExisting value by 1. This is the most usual usage. } |
| 503 |
property KeepExisting: Cardinal read FKeepExisting write FKeepExisting; |
| 504 |
|
| 505 |
{ Increase @link(KeepExisting) property by 1, preventing the node from being |
| 506 |
automatically freed when the reference count changes from non-zero to zero. } |
| 507 |
procedure KeepExistingBegin; |
| 508 |
|
| 509 |
{ Decrease @link(KeepExisting) property by 1. This should match previous |
| 510 |
@link(KeepExistingBegin) call. When @link(KeepExisting) is zero, |
| 511 |
the node will be automatically freed when the reference count |
| 512 |
changes from non-zero to zero. |
| 513 |
|
| 514 |
Note that merely calling KeepExistingEnd, or changing @link(KeepExisting) |
| 515 |
property, @italic(will not) free the node. Follow this with @link(FreeIfUnused) |
| 516 |
or @link(FreeIfUnusedAndNil) to actually free unused node. } |
| 517 |
procedure KeepExistingEnd; |
| 518 |
|
| 519 |
{ Name of this node. When saving/loading, this comes from VRML/X3D |
| 520 |
"DEF" construct. Empty value means that the name is not defined. |
| 521 |
Do not change this during loading / saving of the X3D graph, |
| 522 |
or searching for nodes e.g. by @link(EnumerateNodes). |
| 523 |
|
| 524 |
Note that this property is deliberately not called @code(Name). |
| 525 |
In the future, this class may descend from the standard TComponent |
| 526 |
class, that defines a @code(Name) field with a special restrictions |
| 527 |
(it must be a valid Pascal identifier), which cannot apply to X3D node names |
| 528 |
(that can have quite free names, see |
| 529 |
http://www.web3d.org/documents/specifications/19776-2/V3.3/Part02/grammar.html ). |
| 530 |
We don't want to confuse these two properties. } |
| 531 |
property X3DName: string read FX3DName write FX3DName; |
| 532 |
{$ifdef FPC} |
| 533 |
property NodeName: string read FX3DName write FX3DName; deprecated 'use X3DName'; |
| 534 |
{$endif FPC} |
| 535 |
|
| 536 |
{ Base URL for all URLs inside our fields. |
| 537 |
Used everywhere as a base for relative URLs, to handle fields that refer |
| 538 |
to external files like textures, other 3D models (ImageTexture, |
| 539 |
Inline nodes, many others). |
| 540 |
|
| 541 |
It must be an absolute URL. Currently, it doesn't have to contain |
| 542 |
a protocol, and is then interpreted as a path on local filesystem |
| 543 |
(just like it had file:// prefix). Still, it must be an absolute path. |
| 544 |
|
| 545 |
This is set in constructor, and eventually adjusted by |
| 546 |
various parsing routines. |
| 547 |
This way we could, if only we would like to, resolve nodes |
| 548 |
like Inline or ImageTexture immediately after parsing them. } |
| 549 |
property BaseUrl: String read FBaseUrl write FBaseUrl; |
| 550 |
|
| 551 |
{$ifdef FPC} |
| 552 |
{ Old deprecated name for BaseUrl. @deprecated } |
| 553 |
property WWWBasePath: string read FBaseUrl write FBaseUrl; deprecated; |
| 554 |
{$endif} |
| 555 |
|
| 556 |
{ Returns an absolute path, assuming that RelativePath is relative |
| 557 |
path from BaseUrl or that RelativePath is already absolute. } |
| 558 |
function PathFromBaseUrl(const RelativePath: string): string; |
| 559 |
|
| 560 |
{ Parse node. This should set values of your fields, VRML 1.0 Children |
| 561 |
list, BaseUrl. |
| 562 |
|
| 563 |
In special cases like TX3DUnknownNode this may |
| 564 |
actually initialize whole Fields list (by VRML 1.0 "fields" extensibility |
| 565 |
feature). } |
| 566 |
procedure Parse(Lexer: TX3DLexer; Reader: TX3DReaderNames); virtual; |
| 567 |
|
| 568 |
{ Parse node body, i.e. mainly node's fields. } |
| 569 |
procedure ParseXML(Element: TDOMElement; Reader: TX3DReaderNames); |
| 570 |
|
| 571 |
{ Constructor. Initializes various properties: |
| 572 |
|
| 573 |
@unorderedList( |
| 574 |
@item(Name, BaseUrl are initialized from given parameters.) |
| 575 |
@item(The @link(Fields), @link(Events) lists are filled |
| 576 |
in every descendant, to have all the fields/events defined |
| 577 |
by the specification.) |
| 578 |
@item(DefaultContainerField, and other node-specific |
| 579 |
stuff, is filled in descendants. This is actually implemented |
| 580 |
in CreateNode, that is called at the end of this constructor.) |
| 581 |
) |
| 582 |
} |
| 583 |
constructor Create(const AX3DName: string = ''; |
| 584 |
const ABaseUrl: String = ''); virtual; |
| 585 |
|
| 586 |
destructor Destroy; override; |
| 587 |
|
| 588 |
{ Type of the node in X3D, like @code('Group') or @code('Shape'). Never empty. |
| 589 |
Constant for the class lifetime (even for special |
| 590 |
TX3DUnknownNode and TX3DPrototypeNode, where this is calculated |
| 591 |
at runtime). |
| 592 |
|
| 593 |
Note that VRML/X3D is generally case-sensitive, so this property is too. |
| 594 |
|
| 595 |
In TX3DNode, this returns ClassX3DType, which is suitable |
| 596 |
for most nodes. See ClassX3DType. } |
| 597 |
function X3DType: string; virtual; |
| 598 |
function NodeTypeName: string; deprecated 'use X3DType'; |
| 599 |
|
| 600 |
{ Node type name in VRML/X3D, for this class. |
| 601 |
Normal VRML/X3D node classes should override this to return something |
| 602 |
non-empty, and then X3DType automatically will return the same value. |
| 603 |
|
| 604 |
Empty for classes that don't have a hardcoded VRML/X3D node name, |
| 605 |
like a special TX3DUnknownNode. Such special classes should override |
| 606 |
then X3DType to return actual non-empty name there. |
| 607 |
|
| 608 |
You usually should call X3DType. The only use of this method |
| 609 |
is that it works on classes (it's "class function"), without needing |
| 610 |
at actual instance. } |
| 611 |
class function ClassX3DType: string; virtual; |
| 612 |
|
| 613 |
{ Traverse all the nodes of VRML graph that are active. |
| 614 |
|
| 615 |
An "active" part of the VRML graph are the nodes that actually |
| 616 |
change what the VRML file represents, in terms of geometry, |
| 617 |
collision detection etc. For example, the Switch node has only |
| 618 |
one child usually active. Nodes that merely influence |
| 619 |
the active graph by some events and routes do not have to be |
| 620 |
active (these nodes may change what the VRML file actually represents, |
| 621 |
but only by changing other nodes). |
| 622 |
|
| 623 |
For all nodes of NodeClass TraversingFunc |
| 624 |
will be called. If if returns non-nil for some node, the processing |
| 625 |
of following nodes (children and siblings) will be aborted and result |
| 626 |
will be returned. |
| 627 |
|
| 628 |
Traverse not only enumerates these |
| 629 |
nodes, it also collects all state (transformation, etc --- |
| 630 |
see TX3DGraphTraverseState) that affects how given node should |
| 631 |
be presented. |
| 632 |
|
| 633 |
Also, TraversingInfo is passed to each TraversingFunc call. |
| 634 |
This allows you to investigate, during TraversingFunc call, the parents |
| 635 |
hierarchy (you can't use VRML1Parents / ParentFields of the current node, |
| 636 |
since a node may have many parents). Traverse calls are |
| 637 |
naturally recursive, and so the stack of TraversingInfo |
| 638 |
structures is naturally build and destroyed by recursive calls. |
| 639 |
For the root node (the one where you called Traverse without |
| 640 |
specifying initial TraversingInfo), ParentInfo is simply @nil. |
| 641 |
|
| 642 |
The scheme of how Traverse works: |
| 643 |
|
| 644 |
@preformatted( |
| 645 |
BeforeTraverse; |
| 646 |
|
| 647 |
TraverseIntoChildren := true; |
| 648 |
if Self is NodeClass then TraversingFunc (Self, State, TraverseIntoChildren); |
| 649 |
|
| 650 |
MiddleTraverse; |
| 651 |
|
| 652 |
if TraverseIntoChildren is still true then |
| 653 |
for all children returned by DirectEnumerateActive |
| 654 |
call their Traverse(State); |
| 655 |
|
| 656 |
AfterTraverse; |
| 657 |
|
| 658 |
Add Self to State.VRML1State (for VRML 1.0 state nodes); |
| 659 |
) |
| 660 |
|
| 661 |
Note: setting TraverseIntoChildren to false means that some |
| 662 |
part of the tree is explicitly omitted from traversing. |
| 663 |
Use this only if you know what you're doing, as for some |
| 664 |
nodes they actually affect their parents too (for example, |
| 665 |
chilren within VRML 1.0 Group affect other nodes too; |
| 666 |
global lights within VRML >= 2.0 affect all nodes; and so on...). |
| 667 |
Usually, you will use this only for separator-like nodes |
| 668 |
(for VRML >= 2.0, all Group, Transform, Switch etc. act like separators), |
| 669 |
and only when you will somehow traverse into these nodes anyway. |
| 670 |
|
| 671 |
We guarantee that AfterTraverse will be called if BeforeTraverse |
| 672 |
was called and finished (that is, AfterTraverse is called in the |
| 673 |
@code(finally..end)). |
| 674 |
|
| 675 |
During traversing, you can only modify the children (direct or not) |
| 676 |
nodes of the current node. } |
| 677 |
function Traverse( |
| 678 |
NodeClass: TX3DNodeClass; |
| 679 |
TraversingFunc: TTraversingFunc): Pointer; |
| 680 |
|
| 681 |
{ Like @link(Traverse), but it takes explicit starting state stack |
| 682 |
and starting ParentInfo. Not generally useful, use only for special |
| 683 |
purposes. } |
| 684 |
function TraverseInternal(StateStack: TX3DGraphTraverseStateStack; |
| 685 |
NodeClass: TX3DNodeClass; |
| 686 |
TraversingFunc: TTraversingFunc; |
| 687 |
ParentInfo: PTraversingInfo): Pointer; |
| 688 |
|
| 689 |
{ Like @link(Traverse), but only enters children. |
| 690 |
This does the job of Traverse normally omitted if your @link(Traverse) |
| 691 |
callback returns TraverseIntoChildren = @false. |
| 692 |
Using this method, you can traverse into children explicitly |
| 693 |
from your callback. } |
| 694 |
function TraverseIntoChildren(StateStack: TX3DGraphTraverseStateStack; |
| 695 |
NodeClass: TX3DNodeClass; |
| 696 |
TraversingFunc: TTraversingFunc; |
| 697 |
ParentInfo: PTraversingInfo): Pointer; |
| 698 |
|
| 699 |
{ Enumerate all our children of some class. Recursively. |
| 700 |
Enumerates also this instance (Self), if it satisfies the |
| 701 |
NodeClass and SeekNodeName conditions. |
| 702 |
|
| 703 |
This enumerates both VRML 1.0 children (@link(VRML1Children)) as well as |
| 704 |
VRML >= 2.0 (including X3D) children (nodes in TSFNode and TMFNode fields). |
| 705 |
|
| 706 |
If OnlyActive then it will enumerate only active parts |
| 707 |
of the graph ("active" as defined by @link(Traverse)), |
| 708 |
so it will work as a simpler version of Traverse |
| 709 |
(simpler, because it doesn't track any state). |
| 710 |
|
| 711 |
If not OnlyActive then it will simply enumerate all nodes. |
| 712 |
This will include then also prototype helpers, if this node |
| 713 |
was expanded from prototype: see PrototypeInstanceSourceNode |
| 714 |
and PrototypeInstanceHelpers. |
| 715 |
|
| 716 |
If you give SeekNodeName parameter, we'll only look for nodes |
| 717 |
with X3DName = SeekNodeName. When SeekNodeName = '' it, |
| 718 |
consistently, looks for unnamed nodes (where X3DName = ''). |
| 719 |
|
| 720 |
Enumerates children only after enumerating Self. |
| 721 |
So you can modify children before enumerating them in the Proc callback. |
| 722 |
You can only modify your children in the Proc callback. |
| 723 |
|
| 724 |
@groupBegin } |
| 725 |
procedure EnumerateNodes( |
| 726 |
proc: TX3DNodeProc; OnlyActive: boolean); overload; |
| 727 |
procedure EnumerateNodes(nodeClass: TX3DNodeClass; |
| 728 |
proc: TX3DNodeProc; OnlyActive: boolean); overload; |
| 729 |
procedure EnumerateNodes(nodeClass: TX3DNodeClass; |
| 730 |
const SeekNodeName: string; |
| 731 |
proc: TX3DNodeProc; OnlyActive: boolean); overload; |
| 732 |
{ @groupEnd } |
| 733 |
|
| 734 |
{ Search nodes, like enumerating nodes but allows early exit during |
| 735 |
the enumeration. |
| 736 |
We search for a node for which Proc(Node) returns something non-nil. |
| 737 |
|
| 738 |
For every node, the given Proc callback is run. |
| 739 |
If the Proc callback returns something non-nil, |
| 740 |
the enumeration of the following nodes stops, |
| 741 |
and we return the callback result. } |
| 742 |
function SearchNodes(const Proc: TX3DNodeSearchProc; |
| 743 |
const OnlyActive: boolean): Pointer; |
| 744 |
|
| 745 |
{ TryFindNodeByName and TryFindNode seek for a node with |
| 746 |
given class (and node name, in case of TryFindNodeByName). |
| 747 |
If OnlyActive then they seek among only active nodes |
| 748 |
("active" as defined by @link(Traverse)), otherwise all nodes. |
| 749 |
|
| 750 |
These functions are quite like EnumerateNodes, except |
| 751 |
they stop at the first occurrence and return it. |
| 752 |
|
| 753 |
TryFindNodeByName and TryFindNode return @nil if such node |
| 754 |
is not found. FindNodeByName and FindNode raise exception EX3DNotFound |
| 755 |
in this case. |
| 756 |
@raises(EX3DNotFound FindNodeByName and FindNode raise |
| 757 |
this exception when node is not found.) |
| 758 |
@groupBegin } |
| 759 |
function TryFindNodeByName(FindClass: TX3DNodeClass; |
| 760 |
const FindName: string; |
| 761 |
OnlyActive: boolean): TX3DNode; deprecated 'use FindNode(NodeClass: TX3DNodeClass, FindName: String, Options: TFindNodeOptions = [])'; |
| 762 |
function FindNodeByName(FindClass: TX3DNodeClass; |
| 763 |
const FindName: string; |
| 764 |
OnlyActive: boolean): TX3DNode; deprecated 'use FindNode(NodeClass: TX3DNodeClass, FindName: String, Options: TFindNodeOptions = [])'; |
| 765 |
function TryFindNode(FindClass: TX3DNodeClass; |
| 766 |
OnlyActive: boolean): TX3DNode; deprecated 'use FindNode(NodeClass: TX3DNodeClass, FindName: String, Options: TFindNodeOptions = [])'; |
| 767 |
function FindNode(FindClass: TX3DNodeClass; |
| 768 |
OnlyActive: boolean): TX3DNode; overload; deprecated 'use FindNode(NodeClass: TX3DNodeClass, FindName: String, Options: TFindNodeOptions = [])'; |
| 769 |
{ @groupEnd } |
| 770 |
|
| 771 |
{ Find a node by name and class, in this node and children (recursively). |
| 772 |
|
| 773 |
By default it searches both active and inactive graph parts. |
| 774 |
Add fnOnlyActive to search only in active parts. |
| 775 |
|
| 776 |
@raises(EX3DNotFound When node is not found. |
| 777 |
Unless fnNilOnMissing in Options, then it returns @nil on missing node, |
| 778 |
and EX3DNotFound is never raised.) |
| 779 |
} |
| 780 |
function FindNode(const NodeClass: TX3DNodeClass; const FindName: String; |
| 781 |
const Options: TFindNodeOptions = []): TX3DNode; overload; |
| 782 |
|
| 783 |
function FindNode(const FindName: string): TX3DNode; overload; deprecated 'use FindNode(NodeClass: TX3DNodeClass, FindName: String, Options: TFindNodeOptions = [])'; |
| 784 |
|
| 785 |
{$ifdef GENERIC_METHODS} |
| 786 |
|
| 787 |
{ Find a node of given type and given name, in this node and children (recursively). |
| 788 |
See FindNode for description of Options. } |
| 789 |
{$ifdef FPC}generic{$endif} function Find<T: TX3DNode>(const FindName: string; |
| 790 |
const Options: TFindNodeOptions = []): T; |
| 791 |
|
| 792 |
{$endif} |
| 793 |
|
| 794 |
{ Find the first node with given class (NodeClass), |
| 795 |
return it's state or just transformation. |
| 796 |
Similar to @link(Traverse), but stops as soon as the given node is found. |
| 797 |
|
| 798 |
Returns @false when not found ("out" parametrs |
| 799 |
Node, State and Transform are undefined then). |
| 800 |
@groupBegin } |
| 801 |
function TryFindNodeState( |
| 802 |
NodeClass: TX3DNodeClass; |
| 803 |
out Node: TX3DNode; |
| 804 |
out State: TX3DGraphTraverseState): boolean; deprecated 'use FindNode to look for nodes easier; or iterate over TCastleScene.Shapes.TraverseList if you really need to know nodes with their TX3DGraphTraverseState'; |
| 805 |
function TryFindNodeTransform( |
| 806 |
NodeClass: TX3DNodeClass; |
| 807 |
out Node: TX3DNode; |
| 808 |
out Transform: TMatrix4; |
| 809 |
out TransformScale: Single): boolean; deprecated 'use FindNode to look for nodes easier; or iterate over TCastleScene.Shapes.TraverseList if you really need to know nodes with their transformations'; |
| 810 |
{ @groupEnd } |
| 811 |
|
| 812 |
{ Seeks Self and parent nodes (from VRML1Parents and ParentFields, |
| 813 |
recursively) for given node name. |
| 814 |
|
| 815 |
In other words, this is similar to TryNodeByName or NodeByName, |
| 816 |
but it goes "upward" in graph hierarchy. Note that this |
| 817 |
never restricts itself only to "active" graph part |
| 818 |
("active" as defined by @link(Traverse)) |
| 819 |
because you really can't detect what is the "active" |
| 820 |
part of the graph when going upward. |
| 821 |
|
| 822 |
@raises(EX3DNotFound |
| 823 |
FindParentByName raises this exception when node is not found.) |
| 824 |
|
| 825 |
@groupBegin } |
| 826 |
function TryFindParentByName(const FindName: string): TX3DNode; |
| 827 |
function FindParentByName(const FindName: string): TX3DNode; |
| 828 |
{ @groupEnd } |
| 829 |
|
| 830 |
{ Looks for a given Node in parents (and self), recursively. |
| 831 |
Similar to TryFindParentByName. Returns @true only if found. } |
| 832 |
function HasParentNode(Node: TX3DNode): boolean; |
| 833 |
|
| 834 |
{ Search immediate parents of this node for a node with given FindName. |
| 835 |
Returns @nil if not found. } |
| 836 |
function TryFindDirectParentByName(const FindName: string): TX3DNode; |
| 837 |
|
| 838 |
{ Looks for a given node in our children (direct and not, including self). |
| 839 |
If OnlyActive, then only active parts are searched |
| 840 |
("active" as defined by @link(Traverse)). } |
| 841 |
function IsNodePresent(Node: TX3DNode; OnlyActive: boolean): boolean; |
| 842 |
|
| 843 |
{ Count the occurrences of given node class in our children. |
| 844 |
For example, you can pass NodeClass = TAbstractLightNode to count |
| 845 |
the light sources in the scene. |
| 846 |
|
| 847 |
If CountOnlyActiveNodes, then only active parts are searched |
| 848 |
("active" as defined by @link(Traverse)). |
| 849 |
|
| 850 |
This traverses both VRML 1.0 children nodes and VRML 2.0 nodes |
| 851 |
inside SFNode and MFNode fields. } |
| 852 |
function NodesCount(NodeClass: TX3DNodeClass; |
| 853 |
CountOnlyActiveNodes: boolean): integer; |
| 854 |
|
| 855 |
{ Save node to a stream. Saves everything, including node name, |
| 856 |
node type, and node contents. } |
| 857 |
procedure SaveToStream(Writer: TX3DWriter); override; |
| 858 |
|
| 859 |
{ Do the nodes of (exactly) this class should be included |
| 860 |
in VRML 1.0 state (see TX3DGraphTraverseState.VRML1State). } |
| 861 |
class function VRML1StateNode(out StateNode: TVRML1StateNode): boolean; |
| 862 |
|
| 863 |
{ Some nodes are present only in specific VRML/X3D version. |
| 864 |
This functions decides it. |
| 865 |
|
| 866 |
For example some nodes can only work in VRML < 2.0, |
| 867 |
some others only in VRML >= 2.0. There are even some pairs |
| 868 |
of nodes: for example TConeNode_1 works with VRML < 2.0, |
| 869 |
TConeNode works with VRML >= 2.0. |
| 870 |
|
| 871 |
NodesManager will use this. |
| 872 |
|
| 873 |
Default implementation of this function returns always @true. |
| 874 |
Generally, I don't try to set this too aggresively --- |
| 875 |
in other words, for all cases when it's sensible, I allow |
| 876 |
nodes to be used in every VRML/X3D version, even when official |
| 877 |
specification doesn't. This means that when reading VRML 1.0 |
| 878 |
files actually a large part of VRML 2.0 is allowed too, |
| 879 |
and also while reading VRML 2.0 many constructs from VRML 1.0 |
| 880 |
(officially no longer present in VRML 2.0) are allowed too. |
| 881 |
I'm trying to support what I call a "sum of VRML 1.0 and 2.0". |
| 882 |
|
| 883 |
In practice I only use this function when various VRML/X3D versions |
| 884 |
specify the same node name but |
| 885 |
|
| 886 |
@unorderedList( |
| 887 |
@item(With different fields. |
| 888 |
|
| 889 |
For example Cone and Cylinder have slightly different fields, |
| 890 |
due to the fact that VRML 2.0 resigned from using TSFBitMask fields.) |
| 891 |
|
| 892 |
@item(With different behavior. |
| 893 |
|
| 894 |
For example definitions of Sphere for VRML 1.0 |
| 895 |
and 2.0 are practically equal. However, the behavior from where |
| 896 |
to take texture and material info is different --- in VRML 1.0 |
| 897 |
we take last Texture2, Material etc. nodes, while in VRML 2.0 |
| 898 |
we look in parent Shape's "appearance" field. So once again |
| 899 |
two different Sphere classes are needed.) |
| 900 |
) } |
| 901 |
class function ForVRMLVersion(const Version: TX3DVersion): boolean; |
| 902 |
virtual; |
| 903 |
|
| 904 |
{ Enumerates all children nodes (recursively), |
| 905 |
allowing you to decide for each node to replace or remove it. |
| 906 |
|
| 907 |
So this is something like EnumerateNodes, |
| 908 |
except that it allows you to remove the nodes. It always |
| 909 |
enumerates all nodes, not only active (e.g. it enumerates all |
| 910 |
Switch node children, not only the chosen one). |
| 911 |
|
| 912 |
Note that (unlike regular EnumerateNodes) @bold(this doesn't |
| 913 |
report Self to Func !). Which is natural, since this may remove |
| 914 |
nodes by normal VRML1ChildRemove calls, so it needs to know ParentNode |
| 915 |
of the removed node. |
| 916 |
|
| 917 |
For each node Func will be called, with ParentNode and Node set. |
| 918 |
If you change the Node to something else, then the old node will |
| 919 |
be removed and new Node inserted in the same place. |
| 920 |
If new Node is @nil, then only the old node will be removed. |
| 921 |
|
| 922 |
Nodes are traversed in depth-first search. Node is first reported |
| 923 |
to Func, and then (if it's not replaced) we descend into this Node. |
| 924 |
|
| 925 |
@returns The number of removed (and possibly replaced) nodes. } |
| 926 |
function EnumerateReplaceChildren( |
| 927 |
Func: TEnumerateReplaceNodesFunction): Cardinal; |
| 928 |
|
| 929 |
{ Removes all children (and their children, recursively) with |
| 930 |
node names matchig Wildcard. You can use * and ? special chars |
| 931 |
in the Wildcard. |
| 932 |
@returns The number of removed nodes. } |
| 933 |
function RemoveChildrenWithMatchingName( |
| 934 |
const Wildcard: string; IgnoreCase: Boolean): Cardinal; |
| 935 |
|
| 936 |
property Prototypes [const Index: Integer]: TX3DPrototypeBase read GetPrototypes; |
| 937 |
function PrototypesCount: Integer; |
| 938 |
procedure AddPrototype(const Value: TX3DPrototypeBase); |
| 939 |
|
| 940 |
property Routes [const Index: Integer]: TX3DRoute read GetRoutes; |
| 941 |
function RoutesCount: Integer; |
| 942 |
procedure AddRoute(const Value: TX3DRoute); overload; |
| 943 |
procedure AddRoute(const Source, Destination: TX3DFieldOrEvent); overload; |
| 944 |
procedure RemoveRoute(const Value: TX3DRoute); overload; |
| 945 |
procedure RemoveRoute(const Index: Integer); overload; |
| 946 |
|
| 947 |
property ImportsList [const Index: Integer]: TX3DImport read GetImports; |
| 948 |
function ImportsCount: Integer; |
| 949 |
procedure AddImport(const Value: TX3DImport); |
| 950 |
|
| 951 |
property ExportsList [const Index: Integer]: TX3DExport read GetExports; |
| 952 |
function ExportsCount: Integer; |
| 953 |
procedure AddExport(const Value: TX3DExport); |
| 954 |
|
| 955 |
{ Create a deep copy of this node and all it's children. |
| 956 |
|
| 957 |
New copy is completely independent from original, |
| 958 |
having all children nodes (in both VRML 1.0 sense (Children) |
| 959 |
and VRML >= 2.0 (inside SFNode and MFNode fields)) also |
| 960 |
duplicated. New copy has protypes, routes, interface declarations |
| 961 |
and generally everything established like in the original, using copied |
| 962 |
nodes. |
| 963 |
|
| 964 |
Doesn't copy things which are dependent on container hierarchy. |
| 965 |
(So copying them would be more dangerous than useful.) |
| 966 |
This means: DestructionNotifications, Scene, VRML1Parents, |
| 967 |
ParentFields. VRML1Parents and ParentFields will be set for children |
| 968 |
anyway (to appropriate copies). |
| 969 |
|
| 970 |
Caller owns this newly created copy --- as returned by this method, |
| 971 |
it's not linked anywhere. } |
| 972 |
function DeepCopy: TX3DNode; |
| 973 |
|
| 974 |
{ Internal notes: |
| 975 |
|
| 976 |
TODO: memory leaks are known to be possible in some difficult cases |
| 977 |
with PrototypeInstanceHelpers. See e.g. |
| 978 |
../../../demo-models/prototypes/warnings/errors/proto_leak.wrl and |
| 979 |
../../../demo-models/prototypes/warnings/errors/proto_leak_2.wrl |
| 980 |
for simple testcases. Reason: PrototypeInstanceHelpers may contain, |
| 981 |
by DEF statements, links to Self. |
| 982 |
This causes circular dependency (Self is child of some node on |
| 983 |
PrototypeInstanceHelpers, but PrototypeInstanceHelpers will |
| 984 |
be freed only if Self is freed) causing some memory to be left |
| 985 |
always allocated. |
| 986 |
|
| 987 |
PrototypeInstanceHelpers are actually always TX3DRootNode, |
| 988 |
may be declared as such in the future if needed. |
| 989 |
} |
| 990 |
|
| 991 |
{ PrototypeInstance = @true indicates that this node was created |
| 992 |
from a non-external prototype instantiation. |
| 993 |
|
| 994 |
Then PrototypeInstanceSourceNode is non-nil and indicates |
| 995 |
parsed prototype node (and PrototypeInstanceSourceNode.Prototype |
| 996 |
gives you even a link to the actual prototype specification). |
| 997 |
|
| 998 |
PrototypeInstanceSourceNode is used for events: any ROUTEs |
| 999 |
specified @italic(outside) of prototype and |
| 1000 |
leading to/from instantiated prototype should actually lead |
| 1001 |
to PrototypeInstanceSourceNode events (not to events of Self). |
| 1002 |
Reason: prototype events may be different than actual expanded |
| 1003 |
node events, and ROUTEs want to lead to prototype events. |
| 1004 |
This is implemented when expanding prototype |
| 1005 |
(@link(TX3DPrototypeNode.Instantiate)) |
| 1006 |
and when linking ROUTE (TX3DRoute.SetSource, TX3DRoute.SetDestination). |
| 1007 |
|
| 1008 |
PrototypeInstanceHelpers may be @nil if empty, or may contain |
| 1009 |
a list of other nodes duplicated along with the main prototype node. |
| 1010 |
From VRML spec: |
| 1011 |
|
| 1012 |
@italic(Any other nodes and accompanying scene graphs |
| 1013 |
are not part of the transformation hierarchy, but may be referenced |
| 1014 |
by ROUTE statements or Script nodes in the prototype definition.) |
| 1015 |
|
| 1016 |
Note that for TX3DPrototypeNode (within PrototypeInstanceSourceNode) |
| 1017 |
these have a little different meaning: they describe the |
| 1018 |
@italic(nested prototype), if any, that was used to create this node. |
| 1019 |
This may happen if the node was expanded from one prototype within |
| 1020 |
another. (Usually, you shouldn't be concerned about this; |
| 1021 |
see TX3DPrototypeNode.Instantiate implementation comments for |
| 1022 |
gory details about this.) |
| 1023 |
|
| 1024 |
@groupBegin } |
| 1025 |
property PrototypeInstance: boolean read FPrototypeInstance; |
| 1026 |
property PrototypeInstanceSourceNode: TX3DPrototypeNode |
| 1027 |
read FPrototypeInstanceSourceNode; |
| 1028 |
property PrototypeInstanceHelpers: TX3DNode read FPrototypeInstanceHelpers; |
| 1029 |
{ @groupEnd } |
| 1030 |
|
| 1031 |
{ Default value of "containerField" attribute for this node in X3D XML |
| 1032 |
encoding. } |
| 1033 |
property DefaultContainerField: string |
| 1034 |
read FDefaultContainerField write FDefaultContainerField; |
| 1035 |
|
| 1036 |
{ For some special VRML / X3D nodes (like Script, ComposedShader) |
| 1037 |
that allow the definition of additional fields/events within. |
| 1038 |
|
| 1039 |
In X3D specification this is marked like |
| 1040 |
|
| 1041 |
@preformatted( |
| 1042 |
# And any number of: |
| 1043 |
fieldType [in] fieldName |
| 1044 |
fieldType [in,out] fieldName initialValue |
| 1045 |
fieldType [out] fieldName |
| 1046 |
fieldType [] fieldName initialValue |
| 1047 |
) |
| 1048 |
|
| 1049 |
If HasInterfaceDeclarations is not [], then InterfaceDeclarations |
| 1050 |
will be non-nil and parser (classic VRML parser in this unit, |
| 1051 |
X3D XML reader too) will read this from VRML files. |
| 1052 |
Moreover, for each interface declaration, also appropriate field/event |
| 1053 |
will be added to the list of @link(Fields) or @link(Events), |
| 1054 |
so fields/events created by interface declarations will function |
| 1055 |
just like other standard fields everywhere. |
| 1056 |
|
| 1057 |
@groupBegin } |
| 1058 |
property HasInterfaceDeclarations: TX3DAccessTypes |
| 1059 |
read FHasInterfaceDeclarations |
| 1060 |
write SetHasInterfaceDeclarations default []; |
| 1061 |
|
| 1062 |
property InterfaceDeclarations: TX3DInterfaceDeclarationList |
| 1063 |
read FInterfaceDeclarations; |
| 1064 |
{ @groupEnd } |
| 1065 |
|
| 1066 |
{ Callbacks registered here will be called when this node is destroyed. |
| 1067 |
@groupBegin } |
| 1068 |
procedure AddDestructionNotification(const Event: TNodeDestructionNotification); |
| 1069 |
procedure RemoveDestructionNotification(const Event: TNodeDestructionNotification); |
| 1070 |
{ @groupEnd } |
| 1071 |
|
| 1072 |
{ Scene that will be notified about changes to this node. |
| 1073 |
This is necessary to allow the scene to properly process VRML/X3D |
| 1074 |
events, and also to react properly to any other changes to the nodes |
| 1075 |
(like when you change some field directly by ObjectPascal code, |
| 1076 |
e.g. by TX3DField.Send). |
| 1077 |
|
| 1078 |
May be @nil if none. |
| 1079 |
|
| 1080 |
A given TX3DNode may be renderable only by a single renderer. |
| 1081 |
This means it can be placed only within one TCastleScene. } |
| 1082 |
property Scene: TX3DEventsEngine read FScene write FScene; |
| 1083 |
|
| 1084 |
{ Internally used by @link(Scene) to store some information at node. |
| 1085 |
@exclude } |
| 1086 |
property InternalSceneShape: TObject |
| 1087 |
read FInternalSceneShape write FInternalSceneShape; |
| 1088 |
|
| 1089 |
{ This will be always called by VRML parsers after adding new item |
| 1090 |
to our InterfaceDeclarations. |
| 1091 |
|
| 1092 |
In this class, this simply adds |
| 1093 |
IDecl.FieldOrEvent to our normal fields/events by IDecl.AddFieldOrEvent. |
| 1094 |
You may override this in subclasses to react in some special way |
| 1095 |
to new fields/events, for example Script node may register here |
| 1096 |
to receive notification when input event is received. } |
| 1097 |
procedure PostAddInterfaceDeclaration(IDecl: TX3DInterfaceDeclaration); virtual; |
| 1098 |
|
| 1099 |
{ Add to node InterfaceDeclarations given field. |
| 1100 |
This should only be used with nodes having |
| 1101 |
HasInterfaceDeclarations = @true, like Script or ComposedShader. } |
| 1102 |
procedure AddCustomField(AField: TX3DField); |
| 1103 |
|
| 1104 |
{ Add to node InterfaceDeclarations given field or event. |
| 1105 |
This should only be used with nodes having |
| 1106 |
HasInterfaceDeclarations = @true, like Script or ComposedShader. } |
| 1107 |
procedure AddCustomFieldOrEvent(AFieldOrEvent: TX3DFieldOrEvent); |
| 1108 |
|
| 1109 |
{ What happens when transformation of the node changes. |
| 1110 |
This is internally used by TCastleSceneCore when Transform (or similar) |
| 1111 |
node affecting this node changes. It's an important optimization, |
| 1112 |
to avoid large if clause with many "is" checks that would be quite slow. |
| 1113 |
@exclude } |
| 1114 |
function TransformationChange: TNodeTransformationChange; virtual; |
| 1115 |
|
| 1116 |
{ Nice and concise node description for user. |
| 1117 |
Shows node type and name. } |
| 1118 |
function NiceName: string; |
| 1119 |
|
| 1120 |
{ Detach this node from parent scene, recursively. |
| 1121 |
Useful if you take a node and want to put it in another TCastleSceneCore. |
| 1122 |
|
| 1123 |
@italic(You almost never need to call this method from your application). |
| 1124 |
This is done automatically for you when TCastleSceneCore is destroyed. |
| 1125 |
And a node can only be part of one TCastleSceneCore instance |
| 1126 |
at one time. |
| 1127 |
|
| 1128 |
Notable exceptions when you need to call this: |
| 1129 |
|
| 1130 |
@orderedList( |
| 1131 |
@item( |
| 1132 |
If you process RootNode graph and extract some node from it |
| 1133 |
(that is, delete node from one RootNode graph, |
| 1134 |
but instead of freeing it you insert it into some other VRML/X3D graph) |
| 1135 |
you must call it to manually |
| 1136 |
detach this node (and all it's children) from a previous TCastleSceneCore |
| 1137 |
instance. |
| 1138 |
) |
| 1139 |
|
| 1140 |
@item( |
| 1141 |
Right now the TCastleSceneCore doesn't call RootNode.UnregisterScene |
| 1142 |
when TCastleSceneCore.OwnsRootNode. |
| 1143 |
This is done for speed, because it's not needed in 99% cases. |
| 1144 |
|
| 1145 |
However, if your scene contains some node that isn't freed |
| 1146 |
because of @link(KeepExistingBegin), |
| 1147 |
then such node will remain attached to non-existing scene. |
| 1148 |
You should call UnregisterScene on such node manually, for now. |
| 1149 |
) |
| 1150 |
) |
| 1151 |
} |
| 1152 |
procedure UnregisterScene; |
| 1153 |
|
| 1154 |
{ Find field set by given event of this node. |
| 1155 |
@nil if not found (including when this Event doesn't actually belong to this node). } |
| 1156 |
function FieldSetByEvent(const Event: TX3DEvent): TX3DField; virtual; |
| 1157 |
|
| 1158 |
{ Find field that sends given event of this node. |
| 1159 |
@nil if not found (including when this Event doesn't actually belong to this node). } |
| 1160 |
function FieldSendingEvent(const Event: TX3DEvent): TX3DField; virtual; |
| 1161 |
|
| 1162 |
{ Return an instance performing given functionality of this class |
| 1163 |
(like a fog, transformation, material...), or @nil if none. |
| 1164 |
This implements a simple component system inside TX3DNode, |
| 1165 |
allowing some functionality to have common interface and implementation |
| 1166 |
across a few nodes that are not otherwise related in the ancestor hierarchy. |
| 1167 |
|
| 1168 |
Descendants should register TNodeFunctionality classes using |
| 1169 |
@link(AddFunctionality). } |
| 1170 |
function Functionality( |
| 1171 |
const FunctionalityClass: TNodeFunctionalityClass): TNodeFunctionality; |
| 1172 |
|
| 1173 |
{ Fast shortcut to Functionality(TTransformFunctionality). } |
| 1174 |
property TransformFunctionality: TTransformFunctionality read FTransformFunctionality; |
| 1175 |
{ Fast shortcut to Functionality(TTimeDependentFunctionality). } |
| 1176 |
property TimeFunctionality: TTimeDependentFunctionality read FTimeFunctionality; |
| 1177 |
{ Fast shortcut to Functionality(TGeneratedTextureFunctionality). } |
| 1178 |
property GenTexFunctionality: TGeneratedTextureFunctionality read FGenTexFunctionality; |
| 1179 |
end; |
| 1180 |
|
| 1181 |
TX3DNodeList = class({$ifdef FPC}specialize{$endif} TObjectList<TX3DNode>) |
| 1182 |
{ Find node by name, @nil if not found. |
| 1183 |
|
| 1184 |
For empty node name, always returns @nil. This follows the definition |
| 1185 |
of @link(TX3DNode.X3DName): |
| 1186 |
empty means that node has no name, so it should not be found |
| 1187 |
by searches by name, ever. |
| 1188 |
|
| 1189 |
@seealso(IndexOfName Returns the node index. This is sometimes more |
| 1190 |
flexible (you can always quickly get actual node knowing the index, |
| 1191 |
by Items[Index], but not the other way around). It is also sometimes |
| 1192 |
less comfortable (often you're not interested in node index).) } |
| 1193 |
function FindName(const Name: string): TX3DNode; |
| 1194 |
|
| 1195 |
{ Find index of a node with given name, -1 if not found. } |
| 1196 |
function IndexOfName(const Name: string): Integer; |
| 1197 |
|
| 1198 |
procedure AddIfNotExists(const Node: TX3DNode); |
| 1199 |
function Equals(SecondValue: TObject): boolean; {$ifdef TOBJECT_HAS_EQUALS} override; {$endif} |
| 1200 |
procedure Assign(const Source: TX3DNodeList); overload; |
| 1201 |
procedure Assign(const Source: array of TX3DNode); overload; |
| 1202 |
end; |
| 1203 |
|
| 1204 |
{$endif read_interface} |
| 1205 |
|
| 1206 |
{$ifdef read_implementation} |
| 1207 |
|
| 1208 |
{ TX3DNodeDeepCopyState ----------------------------------------------------- } |
| 1209 |
|
| 1210 |
constructor TX3DNodeDeepCopyState.Create; |
| 1211 |
begin |
| 1212 |
inherited; |
| 1213 |
NodeOriginalToCopy := TNodeNodeMap.Create; |
| 1214 |
ExportOriginalToCopy := TExportExportMap.Create; |
| 1215 |
end; |
| 1216 |
|
| 1217 |
destructor TX3DNodeDeepCopyState.Destroy; |
| 1218 |
begin |
| 1219 |
FreeAndNil(NodeOriginalToCopy); |
| 1220 |
FreeAndNil(ExportOriginalToCopy); |
| 1221 |
inherited; |
| 1222 |
end; |
| 1223 |
|
| 1224 |
function TX3DNodeDeepCopyState.DeepCopy(const OriginalNode: TX3DNode): TX3DNode; |
| 1225 |
begin |
| 1226 |
if not NodeOriginalToCopy.TryGetValue(OriginalNode, Result) then |
| 1227 |
begin |
| 1228 |
{ DeepCopyCore will expand NodeOriginalToCopy at the beginning. } |
| 1229 |
Result := OriginalNode.DeepCopyCore(Self); |
| 1230 |
end; |
| 1231 |
end; |
| 1232 |
|
| 1233 |
{ TNodeFunctionality --------------------------------------------------------- } |
| 1234 |
|
| 1235 |
constructor TNodeFunctionality.Create(const AParent: TX3DNode); |
| 1236 |
begin |
| 1237 |
inherited Create; |
| 1238 |
FParent := AParent; |
| 1239 |
end; |
| 1240 |
|
| 1241 |
{ TX3DNode ------------------------------------------------------------------ } |
| 1242 |
|
| 1243 |
constructor TX3DNode.Create(const AX3DName, ABaseUrl: String); |
| 1244 |
begin |
| 1245 |
inherited Create; |
| 1246 |
VRML1ChildrenAllowed := false; |
| 1247 |
VRML1ChildrenParsingAllowed := false; |
| 1248 |
|
| 1249 |
FX3DName := AX3DName; |
| 1250 |
FBaseUrl := ABaseUrl; |
| 1251 |
|
| 1252 |
FParentFields := TX3DFieldList.Create(false); |
| 1253 |
FFields := TX3DFieldList.Create(false); |
| 1254 |
FEvents := TX3DEventList.Create(false); |
| 1255 |
|
| 1256 |
FHasInterfaceDeclarations := []; |
| 1257 |
FInterfaceDeclarations := nil; |
| 1258 |
|
| 1259 |
CreateNode; |
| 1260 |
end; |
| 1261 |
|
| 1262 |
destructor TX3DNode.Destroy; |
| 1263 |
var |
| 1264 |
I: Integer; |
| 1265 |
begin |
| 1266 |
{ This whole unit, including AnyNodeDestructionNotifications, |
| 1267 |
may be already finalized when calling this. } |
| 1268 |
if AnyNodeDestructionNotifications <> nil then |
| 1269 |
AnyNodeDestructionNotifications.ExecuteAll(Self); |
| 1270 |
|
| 1271 |
{ Note that we call AnyNodeDestructionNotifications and FDestructionNotifications |
| 1272 |
early in the destructor, to allow callbacks to work on still properly initialized |
| 1273 |
instances. } |
| 1274 |
if FDestructionNotifications <> nil then |
| 1275 |
begin |
| 1276 |
FDestructionNotifications.ExecuteAll(Self); |
| 1277 |
FreeAndNil(FDestructionNotifications); |
| 1278 |
end; |
| 1279 |
|
| 1280 |
if FVRML1Children <> nil then |
| 1281 |
VRML1ChildrenClear; |
| 1282 |
|
| 1283 |
if PrototypeInstance then |
| 1284 |
begin |
| 1285 |
FreeAndNil(FPrototypeInstanceSourceNode); |
| 1286 |
FreeAndNil(FPrototypeInstanceHelpers); |
| 1287 |
FPrototypeInstance := false; |
| 1288 |
end; |
| 1289 |
|
| 1290 |
FreeAndNil(FPrototypes); |
| 1291 |
FreeAndNil(FRoutes); |
| 1292 |
FreeAndNil(FImports); |
| 1293 |
FreeAndNil(FExports); |
| 1294 |
FreeAndNil(FFieldsSFNode); |
| 1295 |
FreeAndNil(FFieldsMFNode); |
| 1296 |
FreeAndNil(FFunctionalityList); |
| 1297 |
|
| 1298 |
{ First free Fields and Events, before freeing InterfaceDeclarations. |
| 1299 |
Reason: Fields and Events may contains references to InterfaceDeclarations |
| 1300 |
items (since parsing added them there by PostAddInterfaceDeclaration(IDecl)). |
| 1301 |
So these references have to be valid, and omitted by checking |
| 1302 |
ParentInterfaceDeclaration <> nil. } |
| 1303 |
|
| 1304 |
if FEvents <> nil then |
| 1305 |
begin |
| 1306 |
for I := 0 to FEvents.Count - 1 do |
| 1307 |
if FEvents[I].ParentInterfaceDeclaration = nil then |
| 1308 |
begin |
| 1309 |
FEvents[I].Free; |
| 1310 |
FEvents[I] := nil; |
| 1311 |
end; |
| 1312 |
FreeAndNil(FEvents); |
| 1313 |
end; |
| 1314 |
|
| 1315 |
if FFields <> nil then |
| 1316 |
begin |
| 1317 |
for I := 0 to FFields.Count - 1 do |
| 1318 |
if FFields[I].ParentInterfaceDeclaration = nil then |
| 1319 |
begin |
| 1320 |
FFields[I].Free; |
| 1321 |
FFields[I] := nil; |
| 1322 |
end; |
| 1323 |
FreeAndNil(FFields); |
| 1324 |
end; |
| 1325 |
|
| 1326 |
FreeAndNil(FInterfaceDeclarations); |
| 1327 |
|
| 1328 |
FreeAndNil(FVRML1Children); |
| 1329 |
FreeAndNil(FVRML1Parents); |
| 1330 |
FreeAndNil(FParentFields); |
| 1331 |
|
| 1332 |
inherited; |
| 1333 |
end; |
| 1334 |
|
| 1335 |
procedure TX3DNode.AddDestructionNotification(const Event: TNodeDestructionNotification); |
| 1336 |
begin |
| 1337 |
{ create FDestructionNotifications on demand } |
| 1338 |
if FDestructionNotifications = nil then |
| 1339 |
FDestructionNotifications := TNodeDestructionNotificationList.Create; |
| 1340 |
FDestructionNotifications.Add(Event); |
| 1341 |
end; |
| 1342 |
|
| 1343 |
procedure TX3DNode.RemoveDestructionNotification(const Event: TNodeDestructionNotification); |
| 1344 |
begin |
| 1345 |
if FDestructionNotifications <> nil then |
| 1346 |
FDestructionNotifications.Remove(Event); |
| 1347 |
end; |
| 1348 |
|
| 1349 |
procedure TX3DNode.FreeIfUnused; |
| 1350 |
begin |
| 1351 |
if (Self <> nil) and |
| 1352 |
(VRML1ParentsCount = 0) and |
| 1353 |
(FParentFields.Count = 0) and |
| 1354 |
(KeepExisting = 0) then |
| 1355 |
begin |
| 1356 |
{ For FPC <= 2.2.2: |
| 1357 |
This is written as "Self.Destroy" to actually do the desctruction, |
| 1358 |
freeing memory etc. If I would just call it "Destroy", it would |
| 1359 |
perform what destructor does but leaving object instance unfreed. |
| 1360 |
|
| 1361 |
In FPC 2.2.4, 2.4.0, 2.4.2, 2.4.4, 2.6.0, calling Destroy always |
| 1362 |
does the actual destruction, there seems to be no difference |
| 1363 |
between "Self.Destroy" and "Destroy" below. } |
| 1364 |
Self.Destroy; |
| 1365 |
end; |
| 1366 |
end; |
| 1367 |
|
| 1368 |
procedure TX3DNode.KeepExistingBegin; |
| 1369 |
begin |
| 1370 |
Inc(FKeepExisting); |
| 1371 |
end; |
| 1372 |
|
| 1373 |
procedure TX3DNode.KeepExistingEnd; |
| 1374 |
begin |
| 1375 |
Dec(FKeepExisting); |
| 1376 |
end; |
| 1377 |
|
| 1378 |
procedure TX3DNode.VRML1ChildAdd(const Index: Integer; const Child: TX3DNode); |
| 1379 |
begin |
| 1380 |
Check(VRML1ChildrenAllowed, 'Node "' + X3DType + '" is not allowed to have child node of type ' + Child.X3DType); |
| 1381 |
|
| 1382 |
if Child.FVRML1Parents = nil then |
| 1383 |
Child.FVRML1Parents := TX3DNodeList.Create(false); |
| 1384 |
Child.FVRML1Parents.Add(Self); |
| 1385 |
|
| 1386 |
if FVRML1Children = nil then |
| 1387 |
FVRML1Children := TX3DNodeList.Create(false); |
| 1388 |
FVRML1Children.Insert(Index, Child); |
| 1389 |
end; |
| 1390 |
|
| 1391 |
procedure TX3DNode.VRML1ChildAdd(const Child: TX3DNode); |
| 1392 |
begin |
| 1393 |
VRML1ChildAdd(VRML1ChildrenCount, Child); |
| 1394 |
end; |
| 1395 |
|
| 1396 |
procedure TX3DNode.VRML1ChildRemove(const I: Integer); |
| 1397 |
var |
| 1398 |
OldChild: TX3DNode; |
| 1399 |
begin |
| 1400 |
Assert(FVRML1Children <> nil); |
| 1401 |
Assert(Between(I, 0, FVRML1Children.Count - 1)); |
| 1402 |
|
| 1403 |
OldChild := FVRML1Children[I]; |
| 1404 |
FVRML1Children.Delete(i); |
| 1405 |
Assert(OldChild.FVRML1Parents <> nil); |
| 1406 |
OldChild.FVRML1Parents.Remove(Self); |
| 1407 |
OldChild.FreeIfUnused; |
| 1408 |
end; |
| 1409 |
|
| 1410 |
function TX3DNode.VRML1ChildExtract(const I: Integer): TX3DNode; |
| 1411 |
begin |
| 1412 |
Assert(FVRML1Children <> nil); |
| 1413 |
Assert(Between(I, 0, FVRML1Children.Count - 1)); |
| 1414 |
|
| 1415 |
Result := FVRML1Children[I]; |
| 1416 |
FVRML1Children.Delete(i); |
| 1417 |
Assert(Result.FVRML1Parents <> nil); |
| 1418 |
Result.FVRML1Parents.Remove(Self); |
| 1419 |
|
| 1420 |
{ VRML1ChildRemove now does |
| 1421 |
OldChild.FreeIfUnused; |
| 1422 |
but ExtractChild doesn't do it. } |
| 1423 |
end; |
| 1424 |
|
| 1425 |
procedure TX3DNode.SetVRML1Child(const I: Integer; const Value: TX3DNode); |
| 1426 |
var |
| 1427 |
OldChild: TX3DNode; |
| 1428 |
begin |
| 1429 |
Assert(FVRML1Children <> nil); |
| 1430 |
Assert(Between(I, 0, FVRML1Children.Count - 1)); |
| 1431 |
|
| 1432 |
{ Inefficient implementation: VRML1ChildRemove(I); VRML1ChildAdd(I, Value); } |
| 1433 |
|
| 1434 |
if Value <> FVRML1Children[I] then |
| 1435 |
begin |
| 1436 |
Check(VRML1ChildrenAllowed, |
| 1437 |
'Node '+X3DType+' is not allowed to have child node of type '+ |
| 1438 |
Value.X3DType); |
| 1439 |
|
| 1440 |
OldChild := FVRML1Children[I]; |
| 1441 |
FVRML1Children[I] := Value; |
| 1442 |
|
| 1443 |
Assert(OldChild.FVRML1Parents <> nil); |
| 1444 |
OldChild.FVRML1Parents.Remove(Self); |
| 1445 |
OldChild.FreeIfUnused; |
| 1446 |
|
| 1447 |
if Value.FVRML1Parents = nil then |
| 1448 |
Value.FVRML1Parents := TX3DNodeList.Create(false); |
| 1449 |
Value.FVRML1Parents.Add(Self); |
| 1450 |
end; |
| 1451 |
end; |
| 1452 |
|
| 1453 |
procedure TX3DNode.VRML1ChildrenClear; |
| 1454 |
begin |
| 1455 |
while VRML1ChildrenCount > 0 do |
| 1456 |
VRML1ChildRemove(0); |
| 1457 |
end; |
| 1458 |
|
| 1459 |
function TX3DNode.GetVRML1Child(const I: Integer): TX3DNode; |
| 1460 |
begin |
| 1461 |
Assert(FVRML1Children <> nil); |
| 1462 |
Assert(Between(I, 0, FVRML1Children.Count - 1)); |
| 1463 |
|
| 1464 |
Result := FVRML1Children[I]; |
| 1465 |
end; |
| 1466 |
|
| 1467 |
function TX3DNode.GetVRML1Parent(const I: Integer): TX3DNode; |
| 1468 |
begin |
| 1469 |
Assert(FVRML1Parents <> nil); |
| 1470 |
Assert(Between(I, 0, FVRML1Parents.Count - 1)); |
| 1471 |
|
| 1472 |
Result := FVRML1Parents[I]; |
| 1473 |
end; |
| 1474 |
|
| 1475 |
function TX3DNode.VRML1ChildrenCount: Integer; |
| 1476 |
begin |
| 1477 |
if FVRML1Children <> nil then |
| 1478 |
Result := FVRML1Children.Count |
| 1479 |
else |
| 1480 |
Result := 0; |
| 1481 |
end; |
| 1482 |
|
| 1483 |
function TX3DNode.VRML1ParentsCount: Integer; |
| 1484 |
begin |
| 1485 |
if FVRML1Parents <> nil then |
| 1486 |
Result := FVRML1Parents.Count |
| 1487 |
else |
| 1488 |
Result := 0; |
| 1489 |
end; |
| 1490 |
|
| 1491 |
procedure TX3DNode.FreeRemovingFromAllParents; |
| 1492 |
var |
| 1493 |
i, j: integer; |
| 1494 |
SF: TSFNode; |
| 1495 |
MF: TMFNode; |
| 1496 |
begin |
| 1497 |
if Self = nil then exit; |
| 1498 |
|
| 1499 |
for i := 0 to VRML1ParentsCount - 1 do |
| 1500 |
begin |
| 1501 |
Assert(FVRML1Parents[i].FVRML1Children <> nil); |
| 1502 |
j := FVRML1Parents[i].FVRML1Children.IndexOf(Self); |
| 1503 |
FVRML1Parents[i].FVRML1Children.Delete(j); |
| 1504 |
{ nie musimy sie tu martwic usuwaniem naszego Parenta z listy |
| 1505 |
FVRML1Parents ktora |
| 1506 |
wlasnie przegladamy bo przeciez i tak zaraz zrobimy sobie Destroy; } |
| 1507 |
end; |
| 1508 |
|
| 1509 |
for I := 0 to FParentFields.Count - 1 do |
| 1510 |
begin |
| 1511 |
if FParentFields[I] is TSFNode then |
| 1512 |
begin |
| 1513 |
SF := TSFNode(FParentFields[I]); |
| 1514 |
{ We remove accessing private SF.FValue, |
| 1515 |
not SF.Value property setter, |
| 1516 |
to avoid checking our reference count (and possibly calling |
| 1517 |
our destructor) by this setter. } |
| 1518 |
SF.FValue := nil; |
| 1519 |
end else |
| 1520 |
if FParentFields[I] is TMFNode then |
| 1521 |
begin |
| 1522 |
MF := TMFNode(FParentFields[I]); |
| 1523 |
{ Again we remove using internal methods, that shouldn't be used |
| 1524 |
by normal usage from outside: we call directly FItems methods |
| 1525 |
(instead of calling MFNode.RemoveItem method that would call our |
| 1526 |
RemoveParentField that possibly calls our destructor). } |
| 1527 |
J := MF.FItems.IndexOf(Self); |
| 1528 |
Assert(J <> -1, 'Node must be present on Items list of parent MFNode'); |
| 1529 |
MF.FItems.Delete(J); |
| 1530 |
end else |
| 1531 |
raise EInternalError.Create('TX3DNode.ParentFields not SF or MF Node class'); |
| 1532 |
end; |
| 1533 |
|
| 1534 |
Self.Destroy; |
| 1535 |
end; |
| 1536 |
|
| 1537 |
function TX3DNode.GetParentFieldsItem(Index: Integer): TX3DField; |
| 1538 |
begin |
| 1539 |
Result := FParentFields[Index]; |
| 1540 |
end; |
| 1541 |
|
| 1542 |
function TX3DNode.GetParentFieldsNodeItem(Index: Integer): TX3DNode; |
| 1543 |
var |
| 1544 |
F: TX3DField; |
| 1545 |
begin |
| 1546 |
F := ParentFields[Index]; |
| 1547 |
if F is TSFNode then |
| 1548 |
Result := TSFNode(F).ParentNode else |
| 1549 |
Result := (F as TMFNode).ParentNode; |
| 1550 |
end; |
| 1551 |
|
| 1552 |
function TX3DNode.ParentFieldsCount: Integer; |
| 1553 |
begin |
| 1554 |
Result := FParentFields.Count; |
| 1555 |
end; |
| 1556 |
|
| 1557 |
function TX3DNode.DirectEnumerateActive(Func: TEnumerateChildrenFunction): Pointer; |
| 1558 |
var |
| 1559 |
I: Integer; |
| 1560 |
begin |
| 1561 |
Result := nil; |
| 1562 |
for I := 0 to VRML1ChildrenCount - 1 do |
| 1563 |
begin |
| 1564 |
Result := Func(Self, VRML1Children[I]); |
| 1565 |
if Result <> nil then Exit; |
| 1566 |
end; |
| 1567 |
end; |
| 1568 |
|
| 1569 |
function TX3DNode.DirectEnumerateActiveForTraverse( |
| 1570 |
Func: TEnumerateChildrenFunction; |
| 1571 |
StateStack: TX3DGraphTraverseStateStack): Pointer; |
| 1572 |
begin |
| 1573 |
Result := DirectEnumerateActive(Func); |
| 1574 |
end; |
| 1575 |
|
| 1576 |
function TX3DNode.DirectEnumerateAll( |
| 1577 |
Func: TEnumerateChildrenFunction): Pointer; |
| 1578 |
var |
| 1579 |
I, J: Integer; |
| 1580 |
SF: TSFNode; |
| 1581 |
MF: TMFNode; |
| 1582 |
begin |
| 1583 |
Result := nil; |
| 1584 |
|
| 1585 |
for I := 0 to VRML1ChildrenCount - 1 do |
| 1586 |
begin |
| 1587 |
Result := Func(Self, VRML1Children[I]); |
| 1588 |
if Result <> nil then Exit; |
| 1589 |
end; |
| 1590 |
|
| 1591 |
if FFieldsSFNode <> nil then |
| 1592 |
for I := 0 to FFieldsSFNode.Count - 1 do |
| 1593 |
begin |
| 1594 |
SF := TSFNode(FFieldsSFNode[I]); |
| 1595 |
if (SF.Value <> nil) and not SF.WeakLink then |
| 1596 |
begin |
| 1597 |
Result := Func(Self, SF.Value); |
| 1598 |
if Result <> nil then Exit; |
| 1599 |
end; |
| 1600 |
end; |
| 1601 |
|
| 1602 |
{ Use InternalItems for an extra epsilon of speed. } |
| 1603 |
|
| 1604 |
if FFieldsMFNode <> nil then |
| 1605 |
for I := 0 to FFieldsMFNode.Count - 1 do |
| 1606 |
begin |
| 1607 |
MF := TMFNode(FFieldsMFNode[I]); |
| 1608 |
for J := 0 to MF.InternalItems.Count - 1 do |
| 1609 |
begin |
| 1610 |
Result := Func(Self, MF.InternalItems[J]); |
| 1611 |
if Result <> nil then Exit; |
| 1612 |
end; |
| 1613 |
end; |
| 1614 |
|
| 1615 |
if PrototypeInstance then |
| 1616 |
begin |
| 1617 |
Assert(PrototypeInstanceSourceNode <> nil); |
| 1618 |
Result := Func(Self, PrototypeInstanceSourceNode); |
| 1619 |
if Result <> nil then Exit; |
| 1620 |
|
| 1621 |
if PrototypeInstanceHelpers <> nil then |
| 1622 |
begin |
| 1623 |
Result := Func(Self, PrototypeInstanceHelpers); |
| 1624 |
if Result <> nil then Exit; |
| 1625 |
end; |
| 1626 |
end; |
| 1627 |
end; |
| 1628 |
|
| 1629 |
function TX3DNode.DirectEnumerate( |
| 1630 |
Func: TEnumerateChildrenFunction; |
| 1631 |
OnlyActive: boolean): Pointer; |
| 1632 |
begin |
| 1633 |
if OnlyActive then |
| 1634 |
Result := DirectEnumerateActive(Func) else |
| 1635 |
Result := DirectEnumerateAll(Func); |
| 1636 |
end; |
| 1637 |
|
| 1638 |
procedure TX3DNode.BeforeTraverse(StateStack: TX3DGraphTraverseStateStack); |
| 1639 |
begin |
| 1640 |
if PrototypeInstance then |
| 1641 |
Inc(StateStack.Top.InsidePrototype); |
| 1642 |
end; |
| 1643 |
|
| 1644 |
procedure TX3DNode.MiddleTraverse(StateStack: TX3DGraphTraverseStateStack); |
| 1645 |
begin |
| 1646 |
end; |
| 1647 |
|
| 1648 |
procedure TX3DNode.AfterTraverse(StateStack: TX3DGraphTraverseStateStack); |
| 1649 |
begin |
| 1650 |
if PrototypeInstance then |
| 1651 |
Dec(StateStack.Top.InsidePrototype); |
| 1652 |
end; |
| 1653 |
|
| 1654 |
type |
| 1655 |
TTraverseEnumerator = class |
| 1656 |
StateStack: TX3DGraphTraverseStateStack; |
| 1657 |
NodeClass: TX3DNodeClass; |
| 1658 |
TraversingFunc: TTraversingFunc; |
| 1659 |
ParentInfo: PTraversingInfo; |
| 1660 |
function DoTraverseIntoChildren(Child: TX3DNode): Pointer; |
| 1661 |
function EnumerateChildrenFunction(Node, Child: TX3DNode): Pointer; |
| 1662 |
end; |
| 1663 |
|
| 1664 |
function TTraverseEnumerator.DoTraverseIntoChildren(Child: TX3DNode): Pointer; |
| 1665 |
var |
| 1666 |
ParentInfoForChildren: TTraversingInfo; |
| 1667 |
begin |
| 1668 |
ParentInfoForChildren.Node := Child; |
| 1669 |
ParentInfoForChildren.ParentInfo := ParentInfo; |
| 1670 |
ParentInfo := @ParentInfoForChildren; |
| 1671 |
try |
| 1672 |
Result := Child.DirectEnumerateActiveForTraverse( |
| 1673 |
{$ifdef FPC}@{$endif} EnumerateChildrenFunction, StateStack); |
| 1674 |
finally |
| 1675 |
ParentInfo := ParentInfoForChildren.ParentInfo; |
| 1676 |
end; |
| 1677 |
end; |
| 1678 |
|
| 1679 |
function TTraverseEnumerator.EnumerateChildrenFunction( |
| 1680 |
Node, Child: TX3DNode): Pointer; |
| 1681 |
var |
| 1682 |
StateNode: TVRML1StateNode; |
| 1683 |
IsTraverseIntoChildren: boolean; |
| 1684 |
begin |
| 1685 |
Result := nil; |
| 1686 |
|
| 1687 |
Child.BeforeTraverse(StateStack); |
| 1688 |
try |
| 1689 |
IsTraverseIntoChildren := true; |
| 1690 |
if Child is NodeClass then |
| 1691 |
begin |
| 1692 |
Result := TraversingFunc(Child, StateStack, ParentInfo, IsTraverseIntoChildren); |
| 1693 |
if Result <> nil then Exit; |
| 1694 |
end; |
| 1695 |
|
| 1696 |
Child.MiddleTraverse(StateStack); |
| 1697 |
|
| 1698 |
if IsTraverseIntoChildren then |
| 1699 |
begin |
| 1700 |
Result := DoTraverseIntoChildren(Child); |
| 1701 |
if Result <> nil then Exit; |
| 1702 |
end; |
| 1703 |
finally Child.AfterTraverse(StateStack) end; |
| 1704 |
|
| 1705 |
if Child.VRML1StateNode(StateNode) then |
| 1706 |
StateStack.Top.VRML1State.Nodes[StateNode] := Child; |
| 1707 |
end; |
| 1708 |
|
| 1709 |
function TX3DNode.TraverseInternal(StateStack: TX3DGraphTraverseStateStack; |
| 1710 |
NodeClass: TX3DNodeClass; |
| 1711 |
TraversingFunc: TTraversingFunc; |
| 1712 |
ParentInfo: PTraversingInfo): Pointer; |
| 1713 |
var |
| 1714 |
Enumerator: TTraverseEnumerator; |
| 1715 |
begin |
| 1716 |
Enumerator := TTraverseEnumerator.Create; |
| 1717 |
try |
| 1718 |
Enumerator.StateStack := StateStack; |
| 1719 |
Enumerator.NodeClass := NodeClass; |
| 1720 |
Enumerator.TraversingFunc := TraversingFunc; |
| 1721 |
Enumerator.ParentInfo := ParentInfo; |
| 1722 |
Result := Enumerator.EnumerateChildrenFunction(nil, Self); |
| 1723 |
|
| 1724 |
{ Check that, if all went without exception, Enumerator.ParentInfo |
| 1725 |
returned to original state. } |
| 1726 |
Assert(Enumerator.ParentInfo = ParentInfo); |
| 1727 |
finally FreeAndNil(Enumerator) end; |
| 1728 |
end; |
| 1729 |
|
| 1730 |
function TX3DNode.TraverseIntoChildren(StateStack: TX3DGraphTraverseStateStack; |
| 1731 |
NodeClass: TX3DNodeClass; |
| 1732 |
TraversingFunc: TTraversingFunc; |
| 1733 |
ParentInfo: PTraversingInfo): Pointer; |
| 1734 |
var |
| 1735 |
Enumerator: TTraverseEnumerator; |
| 1736 |
begin |
| 1737 |
Enumerator := TTraverseEnumerator.Create; |
| 1738 |
try |
| 1739 |
Enumerator.StateStack := StateStack; |
| 1740 |
Enumerator.NodeClass := NodeClass; |
| 1741 |
Enumerator.TraversingFunc := TraversingFunc; |
| 1742 |
Enumerator.ParentInfo := ParentInfo; |
| 1743 |
Result := Enumerator.DoTraverseIntoChildren(Self); |
| 1744 |
|
| 1745 |
{ Check that, if all went without exception, Enumerator.ParentInfo |
| 1746 |
returned to original state. } |
| 1747 |
Assert(Enumerator.ParentInfo = ParentInfo); |
| 1748 |
finally FreeAndNil(Enumerator) end; |
| 1749 |
end; |
| 1750 |
|
| 1751 |
var |
| 1752 |
TraverseSingleStack: TX3DGraphTraverseStateStack; |
| 1753 |
|
| 1754 |
function TX3DNode.Traverse(NodeClass: TX3DNodeClass; |
| 1755 |
TraversingFunc: TTraversingFunc): Pointer; |
| 1756 |
begin |
| 1757 |
TraverseSingleStack.PushClear; |
| 1758 |
try |
| 1759 |
Result := TraverseInternal(TraverseSingleStack, NodeClass, TraversingFunc, nil); |
| 1760 |
finally TraverseSingleStack.Pop end; |
| 1761 |
end; |
| 1762 |
|
| 1763 |
function TX3DNode.X3DType: string; |
| 1764 |
begin |
| 1765 |
result := ClassX3DType; |
| 1766 |
end; |
| 1767 |
|
| 1768 |
function TX3DNode.NodeTypeName: string; |
| 1769 |
begin |
| 1770 |
result := X3DType; |
| 1771 |
end; |
| 1772 |
|
| 1773 |
class function TX3DNode.ClassX3DType: string; |
| 1774 |
begin |
| 1775 |
result := ''; |
| 1776 |
end; |
| 1777 |
|
| 1778 |
function BlenderRelativePath(const RelativePath: string): boolean; |
| 1779 |
begin |
| 1780 |
Result := IsPrefix('//', RelativePath, false); |
| 1781 |
end; |
| 1782 |
|
| 1783 |
function TX3DNode.PathFromBaseUrl(const RelativePath: string): string; |
| 1784 |
begin |
| 1785 |
{ This is a workaround for Blender errorneous VRML 1.0 export. |
| 1786 |
Blender exports relative paths by prefixing them by "//" |
| 1787 |
(that's a general convention used internally by Blender, AFAIK). |
| 1788 |
Here we simply remove this "//". } |
| 1789 |
if BlenderRelativePath(RelativePath) then |
| 1790 |
Result := CombineURI(BaseUrl, SEnding(RelativePath, 3)) |
| 1791 |
else |
| 1792 |
Result := CombineURI(BaseUrl, RelativePath); |
| 1793 |
end; |
| 1794 |
|
| 1795 |
procedure TX3DNode.Parse(Lexer: TX3DLexer; Reader: TX3DReaderNames); |
| 1796 |
var |
| 1797 |
Handled: boolean; |
| 1798 |
Position: Integer; |
| 1799 |
ChildNode: TX3DNode; |
| 1800 |
SavedParentNode: TX3DNode; |
| 1801 |
begin |
| 1802 |
VRML1ChildrenClear; |
| 1803 |
|
| 1804 |
// modify Reader.ParentNode to let DefaultContainerFieldInContext have this knowledge |
| 1805 |
SavedParentNode := Reader.ParentNode as TX3DNode; |
| 1806 |
Reader.ParentNode := Self; |
| 1807 |
|
| 1808 |
Position := 0; |
| 1809 |
|
| 1810 |
Lexer.CheckTokenIs(vtOpenCurlyBracket); |
| 1811 |
Lexer.NextToken; |
| 1812 |
while Lexer.Token <> vtCloseCurlyBracket do |
| 1813 |
begin |
| 1814 |
Handled := ParseNodeBodyElement(Lexer, Reader, Position); |
| 1815 |
|
| 1816 |
{ VRML 1.0 children nodes are handled as a last resort here |
| 1817 |
(that's also why they can't be inside our ParseNodeBodyElement). |
| 1818 |
That's because ParseNode just raises exception in case of unknown |
| 1819 |
node, so I have to catch first everything else (like "hints" field |
| 1820 |
of TShapeHintsNode_1). } |
| 1821 |
if not Handled then |
| 1822 |
begin |
| 1823 |
if VRML1ChildrenParsingAllowed then |
| 1824 |
begin |
| 1825 |
ChildNode := ParseNode(Lexer, Reader, false); |
| 1826 |
ChildNode.PositionInParent := Position; |
| 1827 |
VRML1ChildAdd(ChildNode); |
| 1828 |
end else |
| 1829 |
begin |
| 1830 |
raise EX3DParserError.Create(Lexer, |
| 1831 |
Format('Invalid X3D node content (probably unknown or not allowed field, prototype or VRML 1.0-style children) inside "%s": got %s', |
| 1832 |
[X3DType, Lexer.DescribeToken])); |
| 1833 |
end; |
| 1834 |
end; |
| 1835 |
|
| 1836 |
Inc(Position); |
| 1837 |
end; |
| 1838 |
Lexer.NextToken; |
| 1839 |
|
| 1840 |
FBaseUrl := Reader.BaseUrl; |
| 1841 |
|
| 1842 |
ParseAfter(Reader); |
| 1843 |
|
| 1844 |
Reader.ParentNode := SavedParentNode; |
| 1845 |
end; |
| 1846 |
|
| 1847 |
function TX3DNode.ParseNodeBodyElement(Lexer: TX3DLexer; Reader: TX3DReaderNames; |
| 1848 |
const APositionInParent: Integer): boolean; |
| 1849 |
|
| 1850 |
procedure ParseExtensibilityFields; |
| 1851 |
|
| 1852 |
procedure ReadOneField; |
| 1853 |
var |
| 1854 |
X3DType: string; |
| 1855 |
//FieldName: string; |
| 1856 |
FieldType: TX3DFieldClass; |
| 1857 |
begin |
| 1858 |
Lexer.CheckTokenIs(vtName, 'Field type name'); |
| 1859 |
X3DType := Lexer.TokenName; |
| 1860 |
FieldType := X3DFieldsManager.X3DTypeToClass(X3DType); |
| 1861 |
if FieldType = nil then |
| 1862 |
raise EX3DParserError.Create( |
| 1863 |
Lexer, Format(SExpectedFieldType, [Lexer.DescribeToken])); |
| 1864 |
|
| 1865 |
Lexer.NextToken; |
| 1866 |
|
| 1867 |
Lexer.CheckTokenIs(vtName, 'Field name'); |
| 1868 |
//FieldName := Lexer.TokenName; |
| 1869 |
|
| 1870 |
Lexer.NextToken; |
| 1871 |
|
| 1872 |
{ TODO: we should actually do something with obtained here |
| 1873 |
FieldName, FieldType } |
| 1874 |
end; |
| 1875 |
|
| 1876 |
begin |
| 1877 |
{ We parse VRML 1.0 "fields" extensibility feature in a way similar to |
| 1878 |
MF fields syntax, this was the intention (although not clarified precisely) |
| 1879 |
of VRML 1.0 spec. } |
| 1880 |
if Lexer.Token = vtOpenSqBracket then |
| 1881 |
begin |
| 1882 |
Lexer.NextToken; |
| 1883 |
|
| 1884 |
while Lexer.Token <> vtCloseSqBracket do |
| 1885 |
begin |
| 1886 |
ReadOneField; |
| 1887 |
|
| 1888 |
if Lexer.Token = vtCloseSqBracket then break; |
| 1889 |
|
| 1890 |
{ In VRML >= 2.0 the comma is simply a whitespace and will be ignored |
| 1891 |
by the lexer. } |
| 1892 |
if Lexer.Version.Major < 2 then |
| 1893 |
begin |
| 1894 |
Lexer.CheckTokenIs(vtComma); |
| 1895 |
Lexer.NextToken; |
| 1896 |
end; |
| 1897 |
end; |
| 1898 |
|
| 1899 |
{ consume final "]" } |
| 1900 |
Lexer.NextToken; |
| 1901 |
end else |
| 1902 |
begin |
| 1903 |
{ one single field - not enclosed in [] brackets } |
| 1904 |
ReadOneField; |
| 1905 |
end; |
| 1906 |
end; |
| 1907 |
|
| 1908 |
procedure ParseExtensibilityIsA; |
| 1909 |
var |
| 1910 |
IsAField: TMFString; |
| 1911 |
begin |
| 1912 |
IsAField := TMFString.Create(Self, false, '', []); |
| 1913 |
try |
| 1914 |
IsAField.Parse(Lexer, Reader, false); |
| 1915 |
|
| 1916 |
{ TODO: we should actually do something with obtained here |
| 1917 |
isA value } |
| 1918 |
finally FreeAndNil(IsAField) end; |
| 1919 |
end; |
| 1920 |
|
| 1921 |
var |
| 1922 |
I: Integer; |
| 1923 |
Route: TX3DRoute; |
| 1924 |
Proto: TX3DPrototypeBase; |
| 1925 |
Event: TX3DEvent; |
| 1926 |
IDecl: TX3DInterfaceDeclaration; |
| 1927 |
Import: TX3DImport; |
| 1928 |
ExportItem: TX3DExport; { "export" is a keyword in Pascal } |
| 1929 |
begin |
| 1930 |
Result := false; |
| 1931 |
|
| 1932 |
{ If I would know that all fields used are standard, I could |
| 1933 |
check first for if Lexer.TokenName[0] in ['a'..'z'], since all |
| 1934 |
standard field names start lowercase. But of course I can't, |
| 1935 |
all VRML versions allow to define your own nodes and fields. } |
| 1936 |
if Lexer.Token = vtName then |
| 1937 |
begin |
| 1938 |
I := IndexOfField(Lexer.TokenName); |
| 1939 |
if I >= 0 then |
| 1940 |
begin |
| 1941 |
Result := true; |
| 1942 |
|
| 1943 |
{ Advance to the next token. Usually, it should be just "Lexer.NextToken;" |
| 1944 |
But I have to add here some dirty hack to allow SFString fields |
| 1945 |
to contain strings not enclosed in double quotes in VRML 1.0. |
| 1946 |
So I call here NextTokenForceVTString before SFString field. |
| 1947 |
|
| 1948 |
For VRML >= 2.0, this nonsense feature was fortunately removed, |
| 1949 |
and that's good because in VRML >= 2.0 you must be able to use |
| 1950 |
keyword "IS" here, so calling NextTokenForceVTString would be bad. } |
| 1951 |
if (Fields[I] is TSFString) and (Lexer.Version.Major <= 1) then |
| 1952 |
Lexer.NextTokenForceVTString else |
| 1953 |
Lexer.NextToken; |
| 1954 |
|
| 1955 |
Fields[I].Parse(Lexer, Reader, true); |
| 1956 |
Fields[I].PositionInParent := APositionInParent; |
| 1957 |
end else |
| 1958 |
begin |
| 1959 |
Event := AnyEvent(Lexer.TokenName); |
| 1960 |
if Event <> nil then |
| 1961 |
begin |
| 1962 |
Result := true; |
| 1963 |
Lexer.NextToken; |
| 1964 |
Event.Parse(Lexer); |
| 1965 |
Event.PositionInParent := APositionInParent; |
| 1966 |
end else |
| 1967 |
if Lexer.TokenName = 'fields' then |
| 1968 |
begin |
| 1969 |
Result := true; |
| 1970 |
Lexer.NextToken; |
| 1971 |
ParseExtensibilityFields; |
| 1972 |
end else |
| 1973 |
if Lexer.TokenName = 'isA' then |
| 1974 |
begin |
| 1975 |
Result := true; |
| 1976 |
Lexer.NextToken; |
| 1977 |
ParseExtensibilityIsA; |
| 1978 |
end; |
| 1979 |
end; |
| 1980 |
end else |
| 1981 |
if Lexer.TokenIsKeyword(InterfaceDeclarationKeywords(HasInterfaceDeclarations)) then |
| 1982 |
begin |
| 1983 |
Result := true; |
| 1984 |
|
| 1985 |
{ since we're here, HasInterfaceDeclarations is <> [] } |
| 1986 |
Assert(InterfaceDeclarations <> nil); |
| 1987 |
|
| 1988 |
IDecl := TX3DInterfaceDeclaration.Create(Self); |
| 1989 |
InterfaceDeclarations.Add(IDecl); |
| 1990 |
IDecl.Parse(Lexer, Reader, true, true); |
| 1991 |
IDecl.PositionInParent := APositionInParent; |
| 1992 |
PostAddInterfaceDeclaration(IDecl); |
| 1993 |
end else |
| 1994 |
if Lexer.TokenIsKeyword(vkPROTO) then |
| 1995 |
begin |
| 1996 |
Result := true; |
| 1997 |
|
| 1998 |
Proto := TX3DPrototype.Create; |
| 1999 |
AddPrototype(Proto); |
| 2000 |
Proto.Parse(Lexer, Reader); |
| 2001 |
Proto.PositionInParent := APositionInParent; |
| 2002 |
end else |
| 2003 |
if Lexer.TokenIsKeyword(vkEXTERNPROTO) then |
| 2004 |
begin |
| 2005 |
Result := true; |
| 2006 |
|
| 2007 |
Proto := TX3DExternalPrototype.Create; |
| 2008 |
AddPrototype(Proto); |
| 2009 |
Proto.Parse(Lexer, Reader); |
| 2010 |
Proto.PositionInParent := APositionInParent; |
| 2011 |
end else |
| 2012 |
if Lexer.TokenIsKeyword(vkROUTE) then |
| 2013 |
begin |
| 2014 |
Result := true; |
| 2015 |
|
| 2016 |
Route := TX3DRoute.Create; |
| 2017 |
AddRoute(Route); |
| 2018 |
Route.Parse(Lexer, Reader); |
| 2019 |
Route.PositionInParent := APositionInParent; |
| 2020 |
end else |
| 2021 |
if Lexer.TokenIsKeyword(vkIMPORT) then |
| 2022 |
begin |
| 2023 |
Result := true; |
| 2024 |
|
| 2025 |
Import := TX3DImport.Create; |
| 2026 |
AddImport(Import); |
| 2027 |
Import.Parse(Lexer, Reader); |
| 2028 |
Import.PositionInParent := APositionInParent; |
| 2029 |
end else |
| 2030 |
if Lexer.TokenIsKeyword(vkEXPORT) then |
| 2031 |
begin |
| 2032 |
Result := true; |
| 2033 |
|
| 2034 |
ExportItem := TX3DExport.Create; |
| 2035 |
AddExport(ExportItem); |
| 2036 |
ExportItem.Parse(Lexer, Reader); |
| 2037 |
ExportItem.PositionInParent := APositionInParent; |
| 2038 |
end; |
| 2039 |
end; |
| 2040 |
|
| 2041 |
procedure TX3DNode.ParseXML(Element: TDOMElement; Reader: TX3DReaderNames); |
| 2042 |
var |
| 2043 |
Position: Integer; |
| 2044 |
|
| 2045 |
procedure ParseXMLAttributes; |
| 2046 |
var |
| 2047 |
Attr: TDOMAttr; |
| 2048 |
AttrNode: TDOMNode; |
| 2049 |
AttrIndex, Index: Integer; |
| 2050 |
begin |
| 2051 |
{ enumerate over all attributes } |
| 2052 |
for AttrIndex := 0 to Integer(Element.Attributes.Length) - 1 do |
| 2053 |
begin |
| 2054 |
AttrNode := Element.Attributes[AttrIndex]; |
| 2055 |
Assert(AttrNode.NodeType = ATTRIBUTE_NODE); |
| 2056 |
Attr := AttrNode as TDOMAttr; |
| 2057 |
|
| 2058 |
{ containerField and DEF attributes are handled in ParseNode, |
| 2059 |
we can safely ignore them now. } |
| 2060 |
if (Attr.Name = SAttrContainerField) or |
| 2061 |
(Attr.Name = SAttrDEF) then |
| 2062 |
Continue; |
| 2063 |
|
| 2064 |
Index := IndexOfField(Attr.NodeName8); |
| 2065 |
if Index >= 0 then |
| 2066 |
begin |
| 2067 |
Fields[Index].ParseXMLAttribute(Attr.NodeValue8, Reader); |
| 2068 |
{ Note that saving PositionInParent from XML attributes order |
| 2069 |
isn't really useful, as FPC DOM unit already sorted |
| 2070 |
Element.Attributes (TDOMNamedNodeMap) by name. } |
| 2071 |
Fields[Index].PositionInParent := Position; |
| 2072 |
Inc(Position); |
| 2073 |
end else |
| 2074 |
WritelnWarning('X3D', 'Unknown X3D field name (unhandled X3D XML attribute) "' + Attr.NodeName8 + '" in node "' + X3DType + '"'); |
| 2075 |
end; |
| 2076 |
end; |
| 2077 |
|
| 2078 |
procedure ParseXMLChildrenNodes; |
| 2079 |
var |
| 2080 |
FieldIndex: Integer; |
| 2081 |
Child: TX3DNode; |
| 2082 |
ContainerField: string; |
| 2083 |
SF: TSFNode; |
| 2084 |
MF: TMFNode; |
| 2085 |
Route: TX3DRoute; |
| 2086 |
I: TXMLElementIterator; |
| 2087 |
Proto: TX3DPrototype; |
| 2088 |
ExternProto: TX3DExternalPrototype; |
| 2089 |
IDecl: TX3DInterfaceDeclaration; |
| 2090 |
Import: TX3DImport; |
| 2091 |
ExportItem: TX3DExport; |
| 2092 |
begin |
| 2093 |
I := Element.ChildrenIterator; |
| 2094 |
try |
| 2095 |
while I.GetNext do |
| 2096 |
begin |
| 2097 |
if I.Current.TagName = 'ROUTE' then |
| 2098 |
begin |
| 2099 |
Route := TX3DRoute.Create; |
| 2100 |
Route.PositionInParent := Position; |
| 2101 |
AddRoute(Route); |
| 2102 |
Route.ParseXML(I.Current, Reader); |
| 2103 |
end else |
| 2104 |
if I.Current.TagName = 'IS' then |
| 2105 |
begin |
| 2106 |
ParseISStatement(Self, I.Current, Position); |
| 2107 |
end else |
| 2108 |
if I.Current.TagName = 'IMPORT' then |
| 2109 |
begin |
| 2110 |
Import := TX3DImport.Create; |
| 2111 |
Import.PositionInParent := Position; |
| 2112 |
AddImport(Import); |
| 2113 |
Import.ParseXML(I.Current, Reader); |
| 2114 |
end else |
| 2115 |
if I.Current.TagName = 'EXPORT' then |
| 2116 |
begin |
| 2117 |
ExportItem := TX3DExport.Create; |
| 2118 |
ExportItem.PositionInParent := Position; |
| 2119 |
AddExport(ExportItem); |
| 2120 |
ExportItem.ParseXML(I.Current, Reader); |
| 2121 |
end else |
| 2122 |
if I.Current.TagName = 'ProtoDeclare' then |
| 2123 |
begin |
| 2124 |
Proto := TX3DPrototype.Create; |
| 2125 |
Proto.PositionInParent := Position; |
| 2126 |
AddPrototype(Proto); |
| 2127 |
Proto.ParseXML(I.Current, Reader); |
| 2128 |
end else |
| 2129 |
if I.Current.TagName = 'ExternProtoDeclare' then |
| 2130 |
begin |
| 2131 |
ExternProto := TX3DExternalPrototype.Create; |
| 2132 |
ExternProto.PositionInParent := Position; |
| 2133 |
AddPrototype(ExternProto); |
| 2134 |
ExternProto.ParseXML(I.Current, Reader); |
| 2135 |
end else |
| 2136 |
if I.Current.TagName = 'field' then |
| 2137 |
begin |
| 2138 |
IDecl := TX3DInterfaceDeclaration.Create(Self); |
| 2139 |
try |
| 2140 |
IDecl.ParseXML(I.Current, Reader, true); |
| 2141 |
IDecl.PositionInParent := Position; |
| 2142 |
if IDecl.AccessType in HasInterfaceDeclarations then |
| 2143 |
begin |
| 2144 |
InterfaceDeclarations.Add(IDecl); |
| 2145 |
PostAddInterfaceDeclaration(IDecl); |
| 2146 |
end else |
| 2147 |
begin |
| 2148 |
FreeAndNil(IDecl); |
| 2149 |
WritelnWarning('X3D', 'X3D XML: specified <field> inside node, but this node doesn''t allow interface declaration with such accessType'); |
| 2150 |
end; |
| 2151 |
except |
| 2152 |
FreeAndNil(IDecl); |
| 2153 |
raise; |
| 2154 |
end; |
| 2155 |
end else |
| 2156 |
begin |
| 2157 |
Child := ParseXMLNode(I.Current, ContainerField, Reader, true); |
| 2158 |
if Child <> nil then |
| 2159 |
try |
| 2160 |
Child.PositionInParent := Position; |
| 2161 |
FieldIndex := IndexOfField(ContainerField); |
| 2162 |
|
| 2163 |
if (FieldIndex = -1) and |
| 2164 |
(ContainerField <> Child.DefaultContainerField) and |
| 2165 |
(Child.DefaultContainerField <> '') then |
| 2166 |
begin |
| 2167 |
{ Retry with DefaultContainerField value, since it exists |
| 2168 |
and is different than current ContainerField. } |
| 2169 |
FieldIndex := IndexOfField(Child.DefaultContainerField); |
| 2170 |
if FieldIndex >= 0 then |
| 2171 |
WritelnWarning('X3D', 'X3D XML: containerField indicated unknown field name ("' + ContainerField + '" by node "' + Child.X3DType + '" inside node "' + X3DType + '"), using the default containerField value "' + Child.DefaultContainerField + '" succeeded'); |
| 2172 |
end; |
| 2173 |
|
| 2174 |
if FieldIndex >= 0 then |
| 2175 |
begin |
| 2176 |
if Fields[FieldIndex] is TSFNode then |
| 2177 |
begin |
| 2178 |
SF := Fields[FieldIndex] as TSFNode; |
| 2179 |
|
| 2180 |
if SF.Value <> nil then |
| 2181 |
WritelnWarning('X3D', 'X3D XML: More than one value specified for SFNode field ' + SF.NiceName); |
| 2182 |
|
| 2183 |
{ Although field doesn't have a set position in XML X3D |
| 2184 |
encoding, when saving later in classic encoding we |
| 2185 |
need some order of fields. This is yet another problem |
| 2186 |
with non-unique names, something defined in XML X3D |
| 2187 |
may be not possible to save in other encoding: |
| 2188 |
|
| 2189 |
<Group> |
| 2190 |
<Shape> ... <Appearance DEF="XXX" ....> </Shape> |
| 2191 |
<ROUTE ... using "XXX" name ...> |
| 2192 |
<Shape> ... <Appearance DEF="XXX" ....> </Shape> |
| 2193 |
<ROUTE ... using "XXX" name ...> |
| 2194 |
</Group> |
| 2195 |
|
| 2196 |
This is uneasy to save in classic encoding, since |
| 2197 |
you cannot insert ROUTE in the middle of "children" |
| 2198 |
field of Group node in classic encoding. |
| 2199 |
} |
| 2200 |
Fields[FieldIndex].PositionInParent := Position; |
| 2201 |
SF.Value := Child; |
| 2202 |
SF.WarningIfChildNotAllowed(Child); |
| 2203 |
end else |
| 2204 |
if Fields[FieldIndex] is TMFNode then |
| 2205 |
begin |
| 2206 |
MF := Fields[FieldIndex] as TMFNode; |
| 2207 |
Fields[FieldIndex].PositionInParent := Position; |
| 2208 |
MF.Add(Child); |
| 2209 |
MF.WarningIfChildNotAllowed(Child); |
| 2210 |
end else |
| 2211 |
WritelnWarning('X3D', 'X3D field "' + ContainerField + '" is not SFNode or MFNode, but a node value (XML element) is specified'); |
| 2212 |
end else |
| 2213 |
WritelnWarning('X3D', 'Unknown X3D field name (indicated by containerField value) "' + ContainerField + '" by node "' + Child.NiceName + '" inside node "' + NiceName + '"'); |
| 2214 |
finally |
| 2215 |
{ Any WritelnWarning above makes the Child unused. |
| 2216 |
Note that WritelnWarning may, but doesn't have, raise an exception. } |
| 2217 |
Child.FreeIfUnused; |
| 2218 |
Child := nil; |
| 2219 |
end; |
| 2220 |
end; |
| 2221 |
Inc(Position); |
| 2222 |
end; |
| 2223 |
finally FreeAndNil(I) end; |
| 2224 |
end; |
| 2225 |
|
| 2226 |
procedure ParseXMLCdata; |
| 2227 |
var |
| 2228 |
I: TXMLCDataIterator; |
| 2229 |
CDataExists: boolean; |
| 2230 |
CData: string; |
| 2231 |
begin |
| 2232 |
CDataExists := false; |
| 2233 |
CData := ''; |
| 2234 |
|
| 2235 |
I := TXMLCDataIterator.Create(Element); |
| 2236 |
try |
| 2237 |
if I.GetNext then |
| 2238 |
begin |
| 2239 |
CDataExists := true; |
| 2240 |
repeat |
| 2241 |
CData := CData + I.Current; |
| 2242 |
until not I.GetNext; |
| 2243 |
end; |
| 2244 |
finally FreeAndNil(I) end; |
| 2245 |
|
| 2246 |
if CDataExists then |
| 2247 |
if CDataField <> nil then |
| 2248 |
CDataField.Items.Add(CData) else |
| 2249 |
WritelnWarning('X3D', Format('X3D node %s doesn''t allow CDATA section, but it''s specified', |
| 2250 |
[X3DType])); |
| 2251 |
end; |
| 2252 |
|
| 2253 |
var |
| 2254 |
SavedParentNode: TX3DNode; |
| 2255 |
begin |
| 2256 |
// modify Reader.ParentNode to let DefaultContainerFieldInContext have this knowledge |
| 2257 |
SavedParentNode := Reader.ParentNode as TX3DNode; |
| 2258 |
Reader.ParentNode := Self; |
| 2259 |
|
| 2260 |
Position := 0; |
| 2261 |
{ The order below is important: first parse XML attributes, |
| 2262 |
then elements, since VRML DEF mechanism says that DEF order |
| 2263 |
is significant. } |
| 2264 |
ParseXMLAttributes; |
| 2265 |
ParseXMLChildrenNodes; |
| 2266 |
{ This must be called *after* parsing fields, to add CDATA at the *end* |
| 2267 |
of "url" fields, like spec requires. } |
| 2268 |
ParseXMLCdata; |
| 2269 |
|
| 2270 |
ParseAfter(Reader); |
| 2271 |
|
| 2272 |
Reader.ParentNode := SavedParentNode; |
| 2273 |
end; |
| 2274 |
|
| 2275 |
procedure TX3DNode.ParseAfter(Reader: TX3DReaderNames); |
| 2276 |
begin |
| 2277 |
end; |
| 2278 |
|
| 2279 |
procedure TX3DNode.CreateNode; |
| 2280 |
begin |
| 2281 |
end; |
| 2282 |
|
| 2283 |
type |
| 2284 |
TEnumerateNodes0Enumerator = class |
| 2285 |
Proc: TX3DNodeProc; |
| 2286 |
OnlyActive: boolean; |
| 2287 |
function EnumerateChildrenFunction(Node, Child: TX3DNode): Pointer; |
| 2288 |
end; |
| 2289 |
|
| 2290 |
function TEnumerateNodes0Enumerator.EnumerateChildrenFunction( |
| 2291 |
Node, Child: TX3DNode): Pointer; |
| 2292 |
begin |
| 2293 |
Result := nil; |
| 2294 |
Proc(Child); |
| 2295 |
Child.DirectEnumerate({$ifdef FPC}@{$endif} Self.EnumerateChildrenFunction, OnlyActive); |
| 2296 |
end; |
| 2297 |
|
| 2298 |
procedure TX3DNode.EnumerateNodes( |
| 2299 |
Proc: TX3DNodeProc; OnlyActive: boolean); |
| 2300 |
var |
| 2301 |
Enumerator: TEnumerateNodes0Enumerator; |
| 2302 |
begin |
| 2303 |
Enumerator := TEnumerateNodes0Enumerator.Create; |
| 2304 |
try |
| 2305 |
Enumerator.Proc := Proc; |
| 2306 |
Enumerator.OnlyActive := OnlyActive; |
| 2307 |
Enumerator.EnumerateChildrenFunction(nil, Self); |
| 2308 |
finally FreeAndNil(Enumerator) end; |
| 2309 |
end; |
| 2310 |
|
| 2311 |
type |
| 2312 |
TEnumerateNodes1Enumerator = class |
| 2313 |
NodeClass: TX3DNodeClass; |
| 2314 |
Proc: TX3DNodeProc; |
| 2315 |
OnlyActive: boolean; |
| 2316 |
function EnumerateChildrenFunction(Node, Child: TX3DNode): Pointer; |
| 2317 |
end; |
| 2318 |
|
| 2319 |
function TEnumerateNodes1Enumerator.EnumerateChildrenFunction( |
| 2320 |
Node, Child: TX3DNode): Pointer; |
| 2321 |
begin |
| 2322 |
Result := nil; |
| 2323 |
if Child is NodeClass then Proc(Child); |
| 2324 |
Child.DirectEnumerate({$ifdef FPC}@{$endif} Self.EnumerateChildrenFunction, OnlyActive); |
| 2325 |
end; |
| 2326 |
|
| 2327 |
procedure TX3DNode.EnumerateNodes(nodeClass: TX3DNodeClass; |
| 2328 |
Proc: TX3DNodeProc; OnlyActive: boolean); |
| 2329 |
var |
| 2330 |
Enumerator: TEnumerateNodes1Enumerator; |
| 2331 |
begin |
| 2332 |
Enumerator := TEnumerateNodes1Enumerator.Create; |
| 2333 |
try |
| 2334 |
Enumerator.NodeClass := NodeClass; |
| 2335 |
Enumerator.Proc := Proc; |
| 2336 |
Enumerator.OnlyActive := OnlyActive; |
| 2337 |
Enumerator.EnumerateChildrenFunction(nil, Self); |
| 2338 |
finally FreeAndNil(Enumerator) end; |
| 2339 |
end; |
| 2340 |
|
| 2341 |
type |
| 2342 |
TEnumerateNodes2Enumerator = class |
| 2343 |
NodeClass: TX3DNodeClass; |
| 2344 |
SeekNodeName: string; |
| 2345 |
Proc: TX3DNodeProc; |
| 2346 |
OnlyActive: boolean; |
| 2347 |
function EnumerateChildrenFunction(Node, Child: TX3DNode): Pointer; |
| 2348 |
end; |
| 2349 |
|
| 2350 |
function TEnumerateNodes2Enumerator.EnumerateChildrenFunction( |
| 2351 |
Node, Child: TX3DNode): Pointer; |
| 2352 |
begin |
| 2353 |
Result := nil; |
| 2354 |
if (Child is NodeClass) and |
| 2355 |
(Child.X3DName = SeekNodeName) then |
| 2356 |
Proc(Child); |
| 2357 |
Child.DirectEnumerate({$ifdef FPC}@{$endif} Self.EnumerateChildrenFunction, OnlyActive); |
| 2358 |
end; |
| 2359 |
|
| 2360 |
procedure TX3DNode.EnumerateNodes(NodeClass: TX3DNodeClass; |
| 2361 |
const SeekNodeName: string; |
| 2362 |
Proc: TX3DNodeProc; OnlyActive: boolean); |
| 2363 |
var |
| 2364 |
Enumerator: TEnumerateNodes2Enumerator; |
| 2365 |
begin |
| 2366 |
Enumerator := TEnumerateNodes2Enumerator.Create; |
| 2367 |
try |
| 2368 |
Enumerator.NodeClass := NodeClass; |
| 2369 |
Enumerator.SeekNodeName := SeekNodeName; |
| 2370 |
Enumerator.Proc := Proc; |
| 2371 |
Enumerator.OnlyActive := OnlyActive; |
| 2372 |
Enumerator.EnumerateChildrenFunction(nil, Self); |
| 2373 |
finally FreeAndNil(Enumerator) end; |
| 2374 |
end; |
| 2375 |
|
| 2376 |
type |
| 2377 |
TSearchNodes0Helper = class |
| 2378 |
Proc: TX3DNodeSearchProc; |
| 2379 |
OnlyActive: boolean; |
| 2380 |
function Callback(Node, Child: TX3DNode): Pointer; |
| 2381 |
end; |
| 2382 |
|
| 2383 |
function TSearchNodes0Helper.Callback( |
| 2384 |
Node, Child: TX3DNode): Pointer; |
| 2385 |
begin |
| 2386 |
Result := Proc(Child); |
| 2387 |
if Result <> nil then Exit; |
| 2388 |
|
| 2389 |
Result := Child.DirectEnumerate({$ifdef FPC}@{$endif} Self.Callback, OnlyActive); |
| 2390 |
end; |
| 2391 |
|
| 2392 |
function TX3DNode.SearchNodes( |
| 2393 |
const Proc: TX3DNodeSearchProc; const OnlyActive: boolean): Pointer; |
| 2394 |
var |
| 2395 |
Helper: TSearchNodes0Helper; |
| 2396 |
begin |
| 2397 |
Helper := TSearchNodes0Helper.Create; |
| 2398 |
try |
| 2399 |
Helper.Proc := Proc; |
| 2400 |
Helper.OnlyActive := OnlyActive; |
| 2401 |
Result := Helper.Callback(nil, Self); |
| 2402 |
finally FreeAndNil(Helper) end; |
| 2403 |
end; |
| 2404 |
|
| 2405 |
type |
| 2406 |
TFindNodeHelper = class |
| 2407 |
NodeClass: TX3DNodeClass; |
| 2408 |
function Callback(Node: TX3DNode): Pointer; |
| 2409 |
end; |
| 2410 |
|
| 2411 |
function TFindNodeHelper.Callback(Node: TX3DNode): Pointer; |
| 2412 |
begin |
| 2413 |
if Node is NodeClass then |
| 2414 |
Result := Node else |
| 2415 |
Result := nil; |
| 2416 |
end; |
| 2417 |
|
| 2418 |
function TX3DNode.TryFindNode(FindClass: TX3DNodeClass; OnlyActive: boolean): TX3DNode; |
| 2419 |
var |
| 2420 |
Helper: TFindNodeHelper; |
| 2421 |
begin |
| 2422 |
Helper := TFindNodeHelper.Create; |
| 2423 |
try |
| 2424 |
Helper.NodeClass := FindClass; |
| 2425 |
Result := TX3DNode(SearchNodes({$ifdef FPC}@{$endif} Helper.Callback, OnlyActive)); |
| 2426 |
finally FreeAndNil(Helper) end; |
| 2427 |
end; |
| 2428 |
|
| 2429 |
function TX3DNode.FindNode(FindClass: TX3DNodeClass; OnlyActive: boolean): TX3DNode; |
| 2430 |
begin |
| 2431 |
{$warnings off} // usind deprecated in deprecated |
| 2432 |
result := TryFindNode(FindClass, OnlyActive); |
| 2433 |
{$warnings on} |
| 2434 |
if Result = nil then |
| 2435 |
raise EX3DNotFound.CreateFmt('Node class "%s" not found', [FindClass.ClassName]); |
| 2436 |
end; |
| 2437 |
|
| 2438 |
type |
| 2439 |
TFindNodeByNameHelper = class |
| 2440 |
NodeClass: TX3DNodeClass; |
| 2441 |
NodeName: string; |
| 2442 |
function Callback(Node: TX3DNode): Pointer; |
| 2443 |
end; |
| 2444 |
|
| 2445 |
function TFindNodeByNameHelper.Callback(Node: TX3DNode): Pointer; |
| 2446 |
begin |
| 2447 |
if (Node is NodeClass) and (Node.X3DName = NodeName) then |
| 2448 |
Result := Node else |
| 2449 |
Result := nil; |
| 2450 |
end; |
| 2451 |
|
| 2452 |
function TX3DNode.TryFindNodeByName( |
| 2453 |
FindClass: TX3DNodeClass; const FindName: string; OnlyActive: boolean): TX3DNode; |
| 2454 |
var |
| 2455 |
Helper: TFindNodeByNameHelper; |
| 2456 |
begin |
| 2457 |
Helper := TFindNodeByNameHelper.Create; |
| 2458 |
try |
| 2459 |
Helper.NodeClass := FindClass; |
| 2460 |
Helper.NodeName := FindName; |
| 2461 |
Result := TX3DNode(SearchNodes({$ifdef FPC}@{$endif} Helper.Callback, OnlyActive)); |
| 2462 |
finally FreeAndNil(Helper) end; |
| 2463 |
end; |
| 2464 |
|
| 2465 |
function TX3DNode.FindNodeByName( |
| 2466 |
FindClass: TX3DNodeClass; const FindName: string; |
| 2467 |
OnlyActive: boolean): TX3DNode; |
| 2468 |
begin |
| 2469 |
{$warnings off} // usind deprecated in deprecated |
| 2470 |
Result := TryFindNodeByName(FindClass, FindName, OnlyActive); |
| 2471 |
{$warnings on} |
| 2472 |
if Result = nil then |
| 2473 |
raise EX3DNotFound.CreateFmt('Node name "%s" of class "%s" not found', [ |
| 2474 |
FindName, |
| 2475 |
FindClass.ClassName |
| 2476 |
]); |
| 2477 |
end; |
| 2478 |
|
| 2479 |
function TX3DNode.FindNode(const FindName: string): TX3DNode; |
| 2480 |
begin |
| 2481 |
Result := FindNode(TX3DNode, FindName); |
| 2482 |
end; |
| 2483 |
|
| 2484 |
function TX3DNode.FindNode(const NodeClass: TX3DNodeClass; const FindName: string; |
| 2485 |
const Options: TFindNodeOptions): TX3DNode; |
| 2486 |
begin |
| 2487 |
{$warnings off} // usind deprecated, but in the long run this all should be collapses into non-deprecated method |
| 2488 |
Result := TryFindNodeByName(NodeClass, FindName, fnOnlyActive in Options); |
| 2489 |
{$warnings on} |
| 2490 |
if (Result = nil) and (not (fnNilOnMissing in Options)) then |
| 2491 |
raise EX3DNotFound.CreateFmt('Node name "%s" of class "%s" not found', [ |
| 2492 |
FindName, |
| 2493 |
NodeClass.ClassName |
| 2494 |
]); |
| 2495 |
end; |
| 2496 |
|
| 2497 |
{$ifdef GENERIC_METHODS} |
| 2498 |
|
| 2499 |
{$ifdef FPC}generic{$endif} function TX3DNode.Find<T>(const FindName: string; |
| 2500 |
const Options: TFindNodeOptions): T; |
| 2501 |
begin |
| 2502 |
{$warnings off} // usind deprecated, but in the long run this all should be collapses into non-deprecated method |
| 2503 |
Result := TryFindNodeByName(T, FindName, fnOnlyActive in Options) as T; |
| 2504 |
{$warnings on} |
| 2505 |
if (Result = nil) and (not (fnNilOnMissing in Options)) then |
| 2506 |
raise EX3DNotFound.CreateFmt('Node name "%s" of class "%s" not found', [ |
| 2507 |
FindName, |
| 2508 |
T.ClassName |
| 2509 |
]); |
| 2510 |
end; |
| 2511 |
|
| 2512 |
{$endif} |
| 2513 |
|
| 2514 |
{ TX3DNode.TryFindNodeState/Transform ----------------------------------------- } |
| 2515 |
|
| 2516 |
type |
| 2517 |
TTryFindNodeStateResult = class |
| 2518 |
Node: TX3DNode; |
| 2519 |
State: TX3DGraphTraverseState; |
| 2520 |
end; |
| 2521 |
|
| 2522 |
function TX3DNode.TryFindNodeStateTraverse( |
| 2523 |
ANode: TX3DNode; StateStack: TX3DGraphTraverseStateStack; |
| 2524 |
ParentInfo: PTraversingInfo; var TraverseIntoChildren: boolean): Pointer; |
| 2525 |
var |
| 2526 |
Res: TTryFindNodeStateResult; |
| 2527 |
begin |
| 2528 |
Res := TTryFindNodeStateResult.Create; |
| 2529 |
Res.Node := ANode; |
| 2530 |
Res.State := TX3DGraphTraverseState.CreateCopy(StateStack.Top); |
| 2531 |
Result := Res; |
| 2532 |
end; |
| 2533 |
|
| 2534 |
function TX3DNode.TryFindNodeState( |
| 2535 |
NodeClass: TX3DNodeClass; |
| 2536 |
out Node: TX3DNode; out State: TX3DGraphTraverseState): boolean; |
| 2537 |
var |
| 2538 |
Res: TTryFindNodeStateResult; |
| 2539 |
begin |
| 2540 |
Res := TTryFindNodeStateResult(Traverse(NodeClass, {$ifdef FPC}@{$endif} TryFindNodeStateTraverse)); |
| 2541 |
Result := Res <> nil; |
| 2542 |
if Result then |
| 2543 |
begin |
| 2544 |
Node := Res.Node; |
| 2545 |
State := Res.State; |
| 2546 |
end; |
| 2547 |
end; |
| 2548 |
|
| 2549 |
type |
| 2550 |
TTryFindNodeTransformResult = class |
| 2551 |
Node: TX3DNode; |
| 2552 |
Transform: TMatrix4; |
| 2553 |
TransformScale: Single; |
| 2554 |
end; |
| 2555 |
|
| 2556 |
function TX3DNode.TryFindNodeTransformTraverse( |
| 2557 |
ANode: TX3DNode; StateStack: TX3DGraphTraverseStateStack; |
| 2558 |
ParentInfo: PTraversingInfo; var TraverseIntoChildren: boolean): Pointer; |
| 2559 |
var |
| 2560 |
Res: TTryFindNodeTransformResult; |
| 2561 |
begin |
| 2562 |
Res := TTryFindNodeTransformResult.Create; |
| 2563 |
Res.Node := ANode; |
| 2564 |
Res.Transform := StateStack.Top.Transformation.Transform; |
| 2565 |
Res.TransformScale := StateStack.Top.Transformation.Scale; |
| 2566 |
Result := Res; |
| 2567 |
end; |
| 2568 |
|
| 2569 |
function TX3DNode.TryFindNodeTransform( |
| 2570 |
NodeClass: TX3DNodeClass; |
| 2571 |
out Node: TX3DNode; |
| 2572 |
out Transform: TMatrix4; |
| 2573 |
out TransformScale: Single): boolean; |
| 2574 |
var |
| 2575 |
Res: TTryFindNodeTransformResult; |
| 2576 |
begin |
| 2577 |
Res := TTryFindNodeTransformResult(Traverse(NodeClass, |
| 2578 |
{$ifdef FPC}@{$endif} TryFindNodeTransformTraverse)); |
| 2579 |
Result := Res <> nil; |
| 2580 |
if Result then |
| 2581 |
begin |
| 2582 |
Node := Res.Node; |
| 2583 |
Transform := Res.Transform; |
| 2584 |
TransformScale := Res.TransformScale; |
| 2585 |
end; |
| 2586 |
end; |
| 2587 |
|
| 2588 |
function TX3DNode.TryFindParentByName(const FindName: string): TX3DNode; |
| 2589 |
var |
| 2590 |
I: Integer; |
| 2591 |
begin |
| 2592 |
if X3DName = FindName then |
| 2593 |
result := Self else |
| 2594 |
begin |
| 2595 |
result := nil; |
| 2596 |
|
| 2597 |
for I := 0 to VRML1ParentsCount - 1 do |
| 2598 |
begin |
| 2599 |
result := VRML1Parents[I].TryFindParentByName(FindName); |
| 2600 |
if result <> nil then exit; |
| 2601 |
end; |
| 2602 |
|
| 2603 |
for I := 0 to ParentFieldsCount - 1 do |
| 2604 |
begin |
| 2605 |
result := ParentFieldsNode[I].TryFindParentByName(FindName); |
| 2606 |
if result <> nil then exit; |
| 2607 |
end; |
| 2608 |
end; |
| 2609 |
end; |
| 2610 |
|
| 2611 |
function TX3DNode.FindParentByName(const FindName: string): TX3DNode; |
| 2612 |
begin |
| 2613 |
result := TryFindParentByName(FindName); |
| 2614 |
if Result = nil then |
| 2615 |
raise EX3DNotFound.CreateFmt('Node name "%s" not found in parents', [FindName]); |
| 2616 |
end; |
| 2617 |
|
| 2618 |
function TX3DNode.TryFindDirectParentByName(const FindName: string): TX3DNode; |
| 2619 |
var |
| 2620 |
I: Integer; |
| 2621 |
begin |
| 2622 |
for I := 0 to VRML1ParentsCount - 1 do |
| 2623 |
begin |
| 2624 |
Result := VRML1Parents[I]; |
| 2625 |
if Result.X3DName = FindName then Exit; |
| 2626 |
end; |
| 2627 |
|
| 2628 |
for I := 0 to ParentFieldsCount - 1 do |
| 2629 |
begin |
| 2630 |
Result := ParentFieldsNode[I]; |
| 2631 |
if Result.X3DName = FindName then Exit; |
| 2632 |
end; |
| 2633 |
|
| 2634 |
Result := nil; |
| 2635 |
end; |
| 2636 |
|
| 2637 |
function TX3DNode.HasParentNode(Node: TX3DNode): boolean; |
| 2638 |
var |
| 2639 |
I: Integer; |
| 2640 |
begin |
| 2641 |
if Self = Node then |
| 2642 |
result := true else |
| 2643 |
begin |
| 2644 |
for i := 0 to VRML1ParentsCount - 1 do |
| 2645 |
if VRML1Parents[i].HasParentNode(Node) then Exit(true); |
| 2646 |
|
| 2647 |
for i := 0 to ParentFieldsCount - 1 do |
| 2648 |
if ParentFieldsNode[i].HasParentNode(Node) then Exit(true); |
| 2649 |
|
| 2650 |
result := False; |
| 2651 |
end; |
| 2652 |
end; |
| 2653 |
|
| 2654 |
type |
| 2655 |
TIsNodePresentHelper = class |
| 2656 |
SeekNode: TX3DNode; |
| 2657 |
function Callback(Node: TX3DNode): Pointer; |
| 2658 |
end; |
| 2659 |
|
| 2660 |
function TIsNodePresentHelper.Callback(Node: TX3DNode): Pointer; |
| 2661 |
begin |
| 2662 |
if Node = SeekNode then |
| 2663 |
Result := Node { doesn't matter what, anything non-nil } else |
| 2664 |
Result := nil; |
| 2665 |
end; |
| 2666 |
|
| 2667 |
function TX3DNode.IsNodePresent(Node: TX3DNode; OnlyActive: boolean): boolean; |
| 2668 |
var |
| 2669 |
Helper: TIsNodePresentHelper; |
| 2670 |
begin |
| 2671 |
Helper := TIsNodePresentHelper.Create; |
| 2672 |
try |
| 2673 |
Helper.SeekNode := Node; |
| 2674 |
Result := SearchNodes({$ifdef FPC}@{$endif} Helper.Callback, OnlyActive) <> nil; |
| 2675 |
finally FreeAndNil(Helper) end; |
| 2676 |
end; |
| 2677 |
|
| 2678 |
type |
| 2679 |
TNodesCountHelper = class |
| 2680 |
Counter: integer; |
| 2681 |
procedure CountNode(node: TX3DNode); |
| 2682 |
end; |
| 2683 |
|
| 2684 |
procedure TNodesCountHelper.CountNode(node: TX3DNode); |
| 2685 |
begin Inc(Counter) end; |
| 2686 |
|
| 2687 |
function TX3DNode.NodesCount(NodeClass: TX3DNodeClass; |
| 2688 |
CountOnlyActiveNodes: boolean): integer; |
| 2689 |
var |
| 2690 |
C: TNodesCountHelper; |
| 2691 |
begin |
| 2692 |
C := TNodesCountHelper.Create; |
| 2693 |
try |
| 2694 |
EnumerateNodes(NodeClass, {$ifdef FPC}@{$endif} C.CountNode, CountOnlyActiveNodes); |
| 2695 |
result := C.Counter; |
| 2696 |
finally C.Free end; |
| 2697 |
end; |
| 2698 |
|
| 2699 |
procedure TX3DNode.SaveToStream(Writer: TX3DWriter); |
| 2700 |
begin |
| 2701 |
NodeSaveToStream(Writer); |
| 2702 |
if Writer.Encoding = xeClassic then Writer.Writeln; |
| 2703 |
end; |
| 2704 |
|
| 2705 |
procedure TX3DNode.NodeSaveToStream(Writer: TX3DWriter; const CurrentContainerField: string); |
| 2706 |
|
| 2707 |
{ Collect node contents to save. |
| 2708 |
You want to save here exactly what you read in ParseNodeBodyElement. } |
| 2709 |
function ContentsToSave: TX3DFileItemList; |
| 2710 |
var |
| 2711 |
I: Integer; |
| 2712 |
begin |
| 2713 |
Result := TX3DFileItemList.Create(false); |
| 2714 |
try |
| 2715 |
if HasInterfaceDeclarations <> [] then |
| 2716 |
begin |
| 2717 |
for I := 0 to InterfaceDeclarations.Count - 1 do |
| 2718 |
begin |
| 2719 |
{ Saving interface declaration within X3D node (like custom Effect or ComposedShader field) |
| 2720 |
should always save the value, as there's no intrinsic "default". |
| 2721 |
|
| 2722 |
Testcase: terrain demo saving fields, they have defaults often equal to current value, |
| 2723 |
and could result in incorrect X3D XML like: |
| 2724 |
|
| 2725 |
<field accessType="inputOutput" type="SFFloat" name="h0">5</field> |
| 2726 |
|
| 2727 |
instead of |
| 2728 |
|
| 2729 |
<field accessType="inputOutput" type="SFFloat" name="h0" value="5" /> |
| 2730 |
} |
| 2731 |
if InterfaceDeclarations[I].Field <> nil then |
| 2732 |
InterfaceDeclarations[I].Field.UnassignDefaultValue; |
| 2733 |
Result.Add(InterfaceDeclarations[I]); |
| 2734 |
end; |
| 2735 |
end; |
| 2736 |
for I := 0 to PrototypesCount - 1 do |
| 2737 |
Result.Add(Prototypes[I]); |
| 2738 |
|
| 2739 |
for I := 0 to FieldsCount - 1 do |
| 2740 |
begin |
| 2741 |
{ Saving InterfaceDeclarations already handled saving fields |
| 2742 |
with ParentInterfaceDeclaration <> nil, so no need to save them again. } |
| 2743 |
if Fields[I].ParentInterfaceDeclaration = nil then |
| 2744 |
Result.Add(Fields[I]); |
| 2745 |
|
| 2746 |
if Fields[I].Exposed then |
| 2747 |
begin |
| 2748 |
{ exposed events may have their own IS clauses, save them } |
| 2749 |
Result.Add(Fields[I].EventIn); |
| 2750 |
Result.Add(Fields[I].EventOut); |
| 2751 |
end; |
| 2752 |
end; |
| 2753 |
|
| 2754 |
if VRML1ChildrenSaveToStream then |
| 2755 |
for I := 0 to VRML1ChildrenCount - 1 do |
| 2756 |
Result.Add(VRML1Children[I]); |
| 2757 |
|
| 2758 |
for I := 0 to EventsCount - 1 do |
| 2759 |
if { Saving InterfaceDeclarations already handled saving events |
| 2760 |
with ParentInterfaceDeclaration <> nil, so no need to save them again. } |
| 2761 |
(Events[I].ParentInterfaceDeclaration = nil) then |
| 2762 |
Result.Add(Events[I]); |
| 2763 |
|
| 2764 |
for I := 0 to RoutesCount - 1 do |
| 2765 |
Result.Add(Routes[I]); |
| 2766 |
|
| 2767 |
for I := 0 to ImportsCount - 1 do |
| 2768 |
Result.Add(ImportsList[I]); |
| 2769 |
|
| 2770 |
for I := 0 to ExportsCount - 1 do |
| 2771 |
Result.Add(ExportsList[I]); |
| 2772 |
except FreeAndNil(Result) end; |
| 2773 |
end; |
| 2774 |
|
| 2775 |
{ Gather IS clauses of fields/events. |
| 2776 |
For XML encoding, also gather from fields/events inside interface |
| 2777 |
declarations (for classic encoding, |
| 2778 |
they are saved by TX3DInterfaceDeclaration.SaveToStream). } |
| 2779 |
procedure CollectIsClauses(out NodeFields, ProtoFields: TCastleStringList); |
| 2780 |
|
| 2781 |
procedure AddIsClauses(Item: TX3DFieldOrEvent); |
| 2782 |
var |
| 2783 |
N: string; |
| 2784 |
I: Integer; |
| 2785 |
begin |
| 2786 |
N := Item.NameForVersion(Writer.Version); |
| 2787 |
if N = '' then Exit; { unnamed fields/events cannot be saved here } |
| 2788 |
for I := 0 to Item.IsClauseNamesCount - 1 do |
| 2789 |
begin |
| 2790 |
NodeFields.Add(N); |
| 2791 |
ProtoFields.Add(Item.IsClauseNames[I]); |
| 2792 |
end; |
| 2793 |
end; |
| 2794 |
|
| 2795 |
var |
| 2796 |
I: Integer; |
| 2797 |
begin |
| 2798 |
NodeFields := TCastleStringList.Create; |
| 2799 |
ProtoFields := TCastleStringList.Create; |
| 2800 |
|
| 2801 |
for I := 0 to FieldsCount - 1 do |
| 2802 |
begin |
| 2803 |
if (Writer.Encoding <> xeClassic) or |
| 2804 |
(Fields[I].ParentInterfaceDeclaration = nil) then |
| 2805 |
AddIsClauses(Fields[I]); |
| 2806 |
|
| 2807 |
if Fields[I].Exposed then |
| 2808 |
begin |
| 2809 |
{ exposed events may have their own IS clauses, save them } |
| 2810 |
AddIsClauses(Fields[I].EventIn); |
| 2811 |
AddIsClauses(Fields[I].EventOut); |
| 2812 |
end; |
| 2813 |
end; |
| 2814 |
|
| 2815 |
for I := 0 to EventsCount - 1 do |
| 2816 |
if (Writer.Encoding <> xeClassic) or |
| 2817 |
(Events[I].ParentInterfaceDeclaration = nil) then |
| 2818 |
AddIsClauses(Events[I]); |
| 2819 |
end; |
| 2820 |
|
| 2821 |
procedure Classic; |
| 2822 |
var |
| 2823 |
FileItems: TX3DFileItemList; |
| 2824 |
NodeFields, ProtoFields: TCastleStringList; |
| 2825 |
I: Integer; |
| 2826 |
begin |
| 2827 |
if (Writer as TX3DWriterNames).NodeNames.Bound(Self) then |
| 2828 |
begin |
| 2829 |
Writer.WriteIndent('USE ' + EncodeX3DName(X3DName)); |
| 2830 |
end else |
| 2831 |
begin |
| 2832 |
(Writer as TX3DWriterNames).NodeNames.Bind(Self, false); |
| 2833 |
|
| 2834 |
CollectIsClauses(NodeFields, ProtoFields); |
| 2835 |
try |
| 2836 |
Writer.WriteIndent(''); |
| 2837 |
if X3DName <> '' then Writer.Write('DEF ' + EncodeX3DName(X3DName) + ' '); |
| 2838 |
Writer.Writeln(X3DType +' {'); |
| 2839 |
|
| 2840 |
Writer.IncIndent; |
| 2841 |
FileItems := ContentsToSave; |
| 2842 |
try |
| 2843 |
FileItems.SaveToStream(Writer); |
| 2844 |
finally FreeAndNil(FileItems) end; |
| 2845 |
|
| 2846 |
{ save IS clauses } |
| 2847 |
for I := 0 to NodeFields.Count - 1 do |
| 2848 |
Writer.WritelnIndent(Format('%s IS %s', [NodeFields[I], ProtoFields[I]])); |
| 2849 |
Writer.DecIndent; |
| 2850 |
|
| 2851 |
Writer.WriteIndent('}'); |
| 2852 |
finally |
| 2853 |
FreeAndNil(NodeFields); |
| 2854 |
FreeAndNil(ProtoFields); |
| 2855 |
end; |
| 2856 |
|
| 2857 |
(Writer as TX3DWriterNames).NodeNames.Bind(Self, true); |
| 2858 |
end; |
| 2859 |
end; |
| 2860 |
|
| 2861 |
procedure Xml; |
| 2862 |
var |
| 2863 |
FileItems: TX3DFileItemList; |
| 2864 |
HasContent: boolean; |
| 2865 |
I: Integer; |
| 2866 |
WriteContainerField: string; |
| 2867 |
ElementName: string; |
| 2868 |
ProtoInstance: boolean; |
| 2869 |
NodeFields, ProtoFields: TCastleStringList; |
| 2870 |
SavedParentNode: TX3DNode; |
| 2871 |
begin |
| 2872 |
{ Knowing CurrentContainerField and DefaultContainerField, |
| 2873 |
we can always correctly set containerField= ourselves. } |
| 2874 |
if (CurrentContainerField <> '') and |
| 2875 |
(CurrentContainerField <> |
| 2876 |
DefaultContainerFieldInContext(Writer.Version, Writer.ParentNode as TX3DNode)) then |
| 2877 |
WriteContainerField := ' containerField=' + StringToX3DXml(CurrentContainerField) |
| 2878 |
else |
| 2879 |
WriteContainerField := ''; |
| 2880 |
|
| 2881 |
// modify Reader.ParentNode to let DefaultContainerFieldInContext have this knowledge |
| 2882 |
SavedParentNode := Writer.ParentNode as TX3DNode; |
| 2883 |
Writer.ParentNode := Self; |
| 2884 |
|
| 2885 |
{ For XML encoding, TX3DPrototypeNode is saved a little specially. } |
| 2886 |
ProtoInstance := Self is TX3DPrototypeNode; |
| 2887 |
|
| 2888 |
if ProtoInstance then |
| 2889 |
ElementName := 'ProtoInstance' else |
| 2890 |
ElementName := X3DType; |
| 2891 |
|
| 2892 |
if (Writer as TX3DWriterNames).NodeNames.Bound(Self) then |
| 2893 |
begin |
| 2894 |
Writer.WritelnIndent(Format('<%s USE=%s%s />', |
| 2895 |
[ElementName, StringToX3DXml(EncodeX3DName(X3DName)), WriteContainerField])); |
| 2896 |
end else |
| 2897 |
begin |
| 2898 |
(Writer as TX3DWriterNames).NodeNames.Bind(Self, false); |
| 2899 |
|
| 2900 |
CollectIsClauses(NodeFields, ProtoFields); |
| 2901 |
try |
| 2902 |
Writer.WriteIndent('<' + ElementName); |
| 2903 |
if ProtoInstance then |
| 2904 |
Writer.Write(' name=' + StringToX3DXml(X3DType)); |
| 2905 |
if X3DName <> '' then Writer.Write(' DEF=' +StringToX3DXml(EncodeX3DName(X3DName))); |
| 2906 |
Writer.Write(WriteContainerField); |
| 2907 |
|
| 2908 |
HasContent := false; |
| 2909 |
FileItems := ContentsToSave; |
| 2910 |
try |
| 2911 |
FileItems.SortPositionInParent; |
| 2912 |
|
| 2913 |
Writer.IncIndent; |
| 2914 |
for I := 0 to FileItems.Count - 1 do |
| 2915 |
case FileItems[I].SaveToXml of |
| 2916 |
sxAttribute, sxAttributeCustomQuotes: |
| 2917 |
if ProtoInstance then |
| 2918 |
{ for a prototype, every field must be wrapped in <fieldValue> } |
| 2919 |
HasContent := true else |
| 2920 |
FileItems[I].SaveToStream(Writer); |
| 2921 |
sxChildElement: HasContent := true; |
| 2922 |
else ; |
| 2923 |
end; |
| 2924 |
Writer.DecIndent; |
| 2925 |
|
| 2926 |
if NodeFields.Count <> 0 then |
| 2927 |
HasContent := true; |
| 2928 |
|
| 2929 |
if HasContent then |
| 2930 |
begin |
| 2931 |
Writer.Writeln('>'); |
| 2932 |
Writer.IncIndent; |
| 2933 |
|
| 2934 |
for I := 0 to FileItems.Count - 1 do |
| 2935 |
if ProtoInstance and |
| 2936 |
(FileItems[I] is TX3DField) and |
| 2937 |
(FileItems[I].SaveToXml <> sxNone) then |
| 2938 |
begin |
| 2939 |
{ prototype instance fields must be wrapped in <fieldValue> } |
| 2940 |
Writer.WriteIndent('<fieldValue name=' + |
| 2941 |
StringToX3DXml((FileItems[I] as TX3DField).X3DName)); |
| 2942 |
if FileItems[I].SaveToXml = sxChildElement then |
| 2943 |
begin |
| 2944 |
Writer.Writeln('>'); |
| 2945 |
Writer.IncIndent; |
| 2946 |
FileItems[I].SaveToStream(Writer); |
| 2947 |
Writer.DecIndent; |
| 2948 |
Writer.WritelnIndent('</fieldValue>'); |
| 2949 |
end else |
| 2950 |
begin |
| 2951 |
Assert(FileItems[I].SaveToXml in [sxAttribute, sxAttributeCustomQuotes]); |
| 2952 |
Writer.IncIndent; |
| 2953 |
Writer.Writeln; |
| 2954 |
Writer.WriteIndent('value='); |
| 2955 |
(FileItems[I] as TX3DField).FieldSaveToStream(Writer, true, true); |
| 2956 |
Writer.DecIndent; |
| 2957 |
Writer.Writeln(' />'); |
| 2958 |
end; |
| 2959 |
end else |
| 2960 |
if FileItems[I].SaveToXml = sxChildElement then |
| 2961 |
FileItems[I].SaveToStream(Writer); |
| 2962 |
|
| 2963 |
{ save IS clauses } |
| 2964 |
{ TODO: should be before all, but *after* interface declarations and fieldValue } |
| 2965 |
if NodeFields.Count <> 0 then |
| 2966 |
begin |
| 2967 |
Writer.WritelnIndent('<IS>'); |
| 2968 |
Writer.IncIndent; |
| 2969 |
for I := 0 to NodeFields.Count - 1 do |
| 2970 |
Writer.WritelnIndent(Format('<connect nodeField=%s protoField=%s />', |
| 2971 |
[ StringToX3DXml(NodeFields[I]), |
| 2972 |
StringToX3DXml(ProtoFields[I])])); |
| 2973 |
Writer.DecIndent; |
| 2974 |
Writer.WritelnIndent('</IS>'); |
| 2975 |
end; |
| 2976 |
|
| 2977 |
Writer.DecIndent; |
| 2978 |
Writer.WritelnIndent('</' + ElementName + '>'); |
| 2979 |
end else |
| 2980 |
Writer.Writeln(' />'); |
| 2981 |
finally FreeAndNil(FileItems) end; |
| 2982 |
finally |
| 2983 |
FreeAndNil(NodeFields); |
| 2984 |
FreeAndNil(ProtoFields); |
| 2985 |
end; |
| 2986 |
|
| 2987 |
(Writer as TX3DWriterNames).NodeNames.Bind(Self, true); |
| 2988 |
end; |
| 2989 |
|
| 2990 |
Writer.ParentNode := SavedParentNode; |
| 2991 |
end; |
| 2992 |
|
| 2993 |
begin |
| 2994 |
if PrototypeInstance and |
| 2995 |
{ TX3DPrototypeNode has somewhat different meaning of PrototypeInstance, |
| 2996 |
we want to save it directly (otherwise |
| 2997 |
PrototypeInstanceSourceNode.SaveToStream could cause another |
| 2998 |
recursive PrototypeInstanceSourceNode.SaveToStream with |
| 2999 |
nested proto). For example test read + save |
| 3000 |
demo_models/x3d/key_sensor.x3dv, to see that check below is needed. } |
| 3001 |
not (Self is TX3DPrototypeNode) then |
| 3002 |
begin |
| 3003 |
{ If this is an expanded prototype, then delegate writing to the |
| 3004 |
PrototypeInstanceSourceNode. } |
| 3005 |
PrototypeInstanceSourceNode.NodeSaveToStream(Writer, CurrentContainerField); |
| 3006 |
|
| 3007 |
{ What to do about |
| 3008 |
NodeNames.Bind(Self) |
| 3009 |
called from PrototypeInstanceSourceNode.SaveToStream ? |
| 3010 |
This means that PrototypeInstanceSourceNode (TX3DPrototypeNode) |
| 3011 |
is bound to given name. |
| 3012 |
But when reading, we bound Self node (the actual expanded proto) |
| 3013 |
to the same name. |
| 3014 |
Routes when saving check this (to make sure correct names are bound). |
| 3015 |
So we bind again Self, instead of PrototypeInstanceSourceNode, |
| 3016 |
to this name. } |
| 3017 |
|
| 3018 |
(Writer as TX3DWriterNames).NodeNames.Bind(Self, true); |
| 3019 |
end else |
| 3020 |
case Writer.Encoding of |
| 3021 |
xeClassic: Classic; |
| 3022 |
xeXML : Xml; |
| 3023 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 3024 |
else raise EInternalError.Create('TX3DNode.SaveToStream Encoding?'); |
| 3025 |
{$endif} |
| 3026 |
end; |
| 3027 |
end; |
| 3028 |
|
| 3029 |
{$warnings off} // using deprecated to keep supporting VRML 1 |
| 3030 |
class function TX3DNode.VRML1StateNode(out StateNode: TVRML1StateNode): boolean; |
| 3031 |
var |
| 3032 |
SN: TVRML1StateNode; |
| 3033 |
begin |
| 3034 |
{ We're in "class function", so Self = reference to our class. |
| 3035 |
So simple comparison "Self = ..." is what we want. } |
| 3036 |
for SN := Low(SN) to High(SN) do |
| 3037 |
if VRML1StateClasses[SN] = Self then |
| 3038 |
begin |
| 3039 |
StateNode := SN; |
| 3040 |
Result := true; |
| 3041 |
Exit; |
| 3042 |
end; |
| 3043 |
Result := false; |
| 3044 |
end; |
| 3045 |
{$warnings on} |
| 3046 |
|
| 3047 |
class function TX3DNode.ForVRMLVersion(const Version: TX3DVersion): boolean; |
| 3048 |
begin |
| 3049 |
Result := true; |
| 3050 |
end; |
| 3051 |
|
| 3052 |
procedure TX3DNode.RemoveParentField(const Field: TX3DField); |
| 3053 |
begin |
| 3054 |
if FParentFields.Remove(Field) = -1 then |
| 3055 |
raise EInternalError.CreateFmt('RemoveParentField cannot find indicated parent, node %s, parent field %s', [ |
| 3056 |
NiceName, |
| 3057 |
Field.NiceName |
| 3058 |
]); |
| 3059 |
FreeIfUnused; |
| 3060 |
end; |
| 3061 |
|
| 3062 |
procedure TX3DNode.AddParentField(const Field: TX3DField); |
| 3063 |
begin |
| 3064 |
FParentFields.Add(Field); |
| 3065 |
end; |
| 3066 |
|
| 3067 |
class function TX3DNode.VRML1ChildrenSaveToStream: boolean; |
| 3068 |
begin |
| 3069 |
Result := true; |
| 3070 |
end; |
| 3071 |
|
| 3072 |
function TX3DNode.EnumerateReplaceChildren( |
| 3073 |
Func: TEnumerateReplaceNodesFunction): Cardinal; |
| 3074 |
var |
| 3075 |
I, J: Integer; |
| 3076 |
SF: TSFNode; |
| 3077 |
MF: TMFNode; |
| 3078 |
NewNode: TX3DNode; |
| 3079 |
begin |
| 3080 |
{ I don't use EnumerateNodes since I have to enumerate them myself, |
| 3081 |
since they may be removed during enumeration. |
| 3082 |
The code below mimics TX3DNode.DirectEnumerateAll implementation, |
| 3083 |
but it takes into account that nodes may be removed. } |
| 3084 |
|
| 3085 |
Result := 0; |
| 3086 |
|
| 3087 |
I := 0; |
| 3088 |
while I < VRML1ChildrenCount do |
| 3089 |
begin |
| 3090 |
NewNode := VRML1Children[I]; |
| 3091 |
Func(Self, NewNode); |
| 3092 |
if NewNode <> VRML1Children[I] then |
| 3093 |
begin |
| 3094 |
VRML1ChildRemove(I); |
| 3095 |
Inc(Result); |
| 3096 |
if NewNode <> nil then |
| 3097 |
begin |
| 3098 |
VRML1ChildAdd(I, NewNode); |
| 3099 |
Inc(I); |
| 3100 |
end; |
| 3101 |
end else |
| 3102 |
begin |
| 3103 |
Result := Result + VRML1Children[I].EnumerateReplaceChildren(Func); |
| 3104 |
Inc(I); |
| 3105 |
end; |
| 3106 |
end; |
| 3107 |
|
| 3108 |
if FFieldsSFNode <> nil then |
| 3109 |
for I := 0 to FFieldsSFNode.Count - 1 do |
| 3110 |
begin |
| 3111 |
SF := TSFNode(FFieldsSFNode[I]); |
| 3112 |
if (SF.Value <> nil) and not SF.WeakLink then |
| 3113 |
begin |
| 3114 |
NewNode := SF.Value; |
| 3115 |
Func(Self, NewNode); |
| 3116 |
if NewNode <> SF.Value then |
| 3117 |
begin |
| 3118 |
SF.Value := NewNode; |
| 3119 |
Inc(Result); |
| 3120 |
end else |
| 3121 |
begin |
| 3122 |
Result := Result + SF.Value.EnumerateReplaceChildren(Func); |
| 3123 |
end; |
| 3124 |
end; |
| 3125 |
end; |
| 3126 |
|
| 3127 |
if FFieldsMFNode <> nil then |
| 3128 |
for I := 0 to FFieldsMFNode.Count - 1 do |
| 3129 |
begin |
| 3130 |
MF := TMFNode(FFieldsMFNode[I]); |
| 3131 |
J := 0; |
| 3132 |
while J < MF.InternalItems.Count do |
| 3133 |
begin |
| 3134 |
NewNode := MF.InternalItems[J]; |
| 3135 |
Func(Self, NewNode); |
| 3136 |
if NewNode <> MF.InternalItems[J] then |
| 3137 |
begin |
| 3138 |
MF.Delete(J); |
| 3139 |
Inc(Result); |
| 3140 |
if NewNode <> nil then |
| 3141 |
begin |
| 3142 |
MF.Add(J, NewNode); |
| 3143 |
Inc(J); |
| 3144 |
end; |
| 3145 |
end else |
| 3146 |
begin |
| 3147 |
Result := Result + MF.InternalItems[J].EnumerateReplaceChildren(Func); |
| 3148 |
Inc(J); |
| 3149 |
end; |
| 3150 |
end; |
| 3151 |
end; |
| 3152 |
end; |
| 3153 |
|
| 3154 |
type |
| 3155 |
TRemoveChildrenWithMatchingNameHelper = class |
| 3156 |
Wildcard: string; |
| 3157 |
IgnoreCase: boolean; |
| 3158 |
procedure DoIt(ParentNode: TX3DNode; var Node: TX3DNode); |
| 3159 |
end; |
| 3160 |
|
| 3161 |
procedure TRemoveChildrenWithMatchingNameHelper.DoIt( |
| 3162 |
ParentNode: TX3DNode; var Node: TX3DNode); |
| 3163 |
begin |
| 3164 |
if IsWild(Node.X3DName, Wildcard, IgnoreCase) then |
| 3165 |
Node := nil; |
| 3166 |
end; |
| 3167 |
|
| 3168 |
function TX3DNode.RemoveChildrenWithMatchingName( |
| 3169 |
const Wildcard: string; IgnoreCase: Boolean): Cardinal; |
| 3170 |
var |
| 3171 |
Helper: TRemoveChildrenWithMatchingNameHelper; |
| 3172 |
begin |
| 3173 |
Helper := TRemoveChildrenWithMatchingNameHelper.Create; |
| 3174 |
try |
| 3175 |
Helper.Wildcard := Wildcard; |
| 3176 |
Helper.IgnoreCase := IgnoreCase; |
| 3177 |
Result := EnumerateReplaceChildren({$ifdef FPC}@{$endif} Helper.DoIt); |
| 3178 |
finally FreeAndNil(Helper) end; |
| 3179 |
end; |
| 3180 |
|
| 3181 |
function TX3DNode.Field(const AName: string; const RaiseOnError: Boolean): TX3DField; |
| 3182 |
var |
| 3183 |
I: Integer; |
| 3184 |
begin |
| 3185 |
I := FFields.IndexOfName(AName); |
| 3186 |
if I <> -1 then |
| 3187 |
Result := FFields[I] |
| 3188 |
else |
| 3189 |
{ not found } |
| 3190 |
if RaiseOnError then |
| 3191 |
raise EX3DNotFound.CreateFmt('Cannot find field "%s" on node "%s"', [AName, NiceName]) |
| 3192 |
else |
| 3193 |
Result := nil; |
| 3194 |
end; |
| 3195 |
|
| 3196 |
function TX3DNode.FieldOrEvent(const AName: string; const RaiseOnError: Boolean): TX3DFieldOrEvent; |
| 3197 |
var |
| 3198 |
I: Integer; |
| 3199 |
ResultEvent: TX3DEvent; |
| 3200 |
begin |
| 3201 |
I := FFields.IndexOfName(AName); |
| 3202 |
if I <> -1 then |
| 3203 |
Exit(FFields[I]); |
| 3204 |
|
| 3205 |
{ I use helper ResultEvent below, instead of passing |
| 3206 |
"TX3DEvent(Result)" as last param: don't know why, |
| 3207 |
but with FPC 2.2.0 this cast may fail (even though it shouldn't |
| 3208 |
be checked at all?), testcase: |
| 3209 |
castle-model-viewer www.web3d.org/x3d/content/examples/Basic/CAD/CADGeometryPrototypes.x3d |
| 3210 |
} |
| 3211 |
|
| 3212 |
I := FFields.IndexOfExposedEvent(AName, ResultEvent); |
| 3213 |
if I <> -1 then |
| 3214 |
Exit(ResultEvent); |
| 3215 |
|
| 3216 |
I := FEvents.IndexOfName(AName); |
| 3217 |
if I <> -1 then |
| 3218 |
Exit(Events[I]); |
| 3219 |
|
| 3220 |
{ not found } |
| 3221 |
if RaiseOnError then |
| 3222 |
raise EX3DNotFound.CreateFmt('Cannot find field or event "%s" on node "%s"', [AName, NiceName]) |
| 3223 |
else |
| 3224 |
Result := nil; |
| 3225 |
end; |
| 3226 |
|
| 3227 |
function TX3DNode.AnyEvent(const AName: string; const RaiseOnError: Boolean): TX3DEvent; |
| 3228 |
var |
| 3229 |
I: Integer; |
| 3230 |
begin |
| 3231 |
I := FFields.IndexOfExposedEvent(AName, Result); |
| 3232 |
if I <> -1 then |
| 3233 |
Exit; { Result is already set } |
| 3234 |
|
| 3235 |
I := FEvents.IndexOfName(AName); |
| 3236 |
if I <> -1 then |
| 3237 |
Exit(Events[I]); |
| 3238 |
|
| 3239 |
{ not found } |
| 3240 |
if RaiseOnError then |
| 3241 |
raise EX3DNotFound.CreateFmt('Cannot find event "%s" on node "%s"', [AName, NiceName]) |
| 3242 |
else |
| 3243 |
Result := nil; |
| 3244 |
end; |
| 3245 |
|
| 3246 |
procedure TX3DNode.SetHasInterfaceDeclarations(const Value: TX3DAccessTypes); |
| 3247 |
begin |
| 3248 |
if Value <> HasInterfaceDeclarations then |
| 3249 |
begin |
| 3250 |
FHasInterfaceDeclarations := Value; |
| 3251 |
if HasInterfaceDeclarations <> [] then |
| 3252 |
begin |
| 3253 |
{ make sure InterfaceDeclarations is non-nil } |
| 3254 |
if FInterfaceDeclarations = nil then |
| 3255 |
FInterfaceDeclarations := TX3DInterfaceDeclarationList.Create(true); |
| 3256 |
end else |
| 3257 |
begin |
| 3258 |
{ make sure InterfaceDeclarations is nil } |
| 3259 |
FreeAndNil(FInterfaceDeclarations); |
| 3260 |
end; |
| 3261 |
end; |
| 3262 |
end; |
| 3263 |
|
| 3264 |
function TX3DNode.DeepCopyCreate(CopyState: TX3DNodeDeepCopyState): TX3DNode; |
| 3265 |
begin |
| 3266 |
Result := TX3DNodeClass(ClassType).Create(X3DName, BaseUrl); |
| 3267 |
end; |
| 3268 |
|
| 3269 |
function TX3DNode.DeepCopyCore(CopyState: TX3DNodeDeepCopyState): TX3DNode; |
| 3270 |
var |
| 3271 |
I: Integer; |
| 3272 |
IDecl: TX3DInterfaceDeclaration; |
| 3273 |
begin |
| 3274 |
Result := DeepCopyCreate(CopyState); |
| 3275 |
|
| 3276 |
try |
| 3277 |
|
| 3278 |
{ We expand CopyState.NodeOriginalToCopy now, right after DeepCopyCreate. |
| 3279 |
This is needed, as later during DeepCopyCore we may need this node |
| 3280 |
in case of loops within hierarchy. |
| 3281 |
|
| 3282 |
For example, internal |
| 3283 |
routes from TX3DPrototypeNode are established to handle "IS" clauses |
| 3284 |
for events. There routes are to/from TX3DPrototypeNode, and are |
| 3285 |
also placed within this TX3DPrototypeNode instance. So when copying |
| 3286 |
node routes, this node must already be present in CopyState arrays. |
| 3287 |
|
| 3288 |
Also, in the future we will have to allow loops in Script nodes |
| 3289 |
(USE within Script node may refer to the same node). So again loop |
| 3290 |
will be created. } |
| 3291 |
CopyState.NodeOriginalToCopy.Add(Self, Result); |
| 3292 |
|
| 3293 |
for I := 0 to VRML1ChildrenCount - 1 do |
| 3294 |
Result.VRML1ChildAdd(CopyState.DeepCopy(VRML1Children[I])); |
| 3295 |
|
| 3296 |
{ Copy InterfaceDeclarations first, before copying Fields and Events |
| 3297 |
(as some Fields and Events come from InterfaceDeclarations). } |
| 3298 |
Result.HasInterfaceDeclarations := HasInterfaceDeclarations; |
| 3299 |
|
| 3300 |
if InterfaceDeclarations <> nil then |
| 3301 |
begin |
| 3302 |
for I := 0 to InterfaceDeclarations.Count - 1 do |
| 3303 |
begin |
| 3304 |
IDecl := InterfaceDeclarations[I].DeepCopy(Result, CopyState); |
| 3305 |
Result.InterfaceDeclarations.Add(IDecl); |
| 3306 |
Result.PostAddInterfaceDeclaration(IDecl); |
| 3307 |
end; |
| 3308 |
end; |
| 3309 |
|
| 3310 |
{ TODO: No need to copy prototypes for now? |
| 3311 |
|
| 3312 |
This DeepCopy is used for now by protos expanding. |
| 3313 |
It does not need prototype links (as protos are already expanded when copying, |
| 3314 |
and they don't need anything more). |
| 3315 |
|
| 3316 |
for I := 0 to Prototypes.Count - 1 do |
| 3317 |
...(Prototypes[I]); |
| 3318 |
} |
| 3319 |
|
| 3320 |
Assert(FieldsCount = Result.FieldsCount); |
| 3321 |
Assert(EventsCount = Result.EventsCount); |
| 3322 |
|
| 3323 |
for I := 0 to FieldsCount - 1 do |
| 3324 |
{ Copying InterfaceDeclarations field/event already handled. } |
| 3325 |
if Fields[I].ParentInterfaceDeclaration = nil then |
| 3326 |
begin |
| 3327 |
Result.Fields[I].InternalAssignDeepCopy(Fields[I], CopyState); |
| 3328 |
|
| 3329 |
if Result.Fields[I].Exposed then |
| 3330 |
begin |
| 3331 |
Result.Fields[I].EventIn .InternalAssignDeepCopy(Fields[I].EventIn , CopyState); |
| 3332 |
Result.Fields[I].EventOut.InternalAssignDeepCopy(Fields[I].EventOut, CopyState); |
| 3333 |
end; |
| 3334 |
end; |
| 3335 |
|
| 3336 |
for I := 0 to EventsCount - 1 do |
| 3337 |
{ Copying InterfaceDeclarations field/event already handled. } |
| 3338 |
if Events[I].ParentInterfaceDeclaration = nil then |
| 3339 |
Result.Events[I].InternalAssignDeepCopy(Events[I], CopyState); |
| 3340 |
|
| 3341 |
for I := 0 to RoutesCount - 1 do |
| 3342 |
Result.AddRoute(Routes[I].DeepCopy(CopyState)); |
| 3343 |
|
| 3344 |
for I := 0 to ImportsCount - 1 do |
| 3345 |
Result.AddImport((ImportsList[I] as TX3DImport).DeepCopy(CopyState)); |
| 3346 |
|
| 3347 |
for I := 0 to ExportsCount - 1 do |
| 3348 |
Result.AddExport((ExportsList[I] as TX3DExport).DeepCopy(CopyState)); |
| 3349 |
|
| 3350 |
if PrototypeInstance then |
| 3351 |
begin |
| 3352 |
Result.FPrototypeInstance := PrototypeInstance; |
| 3353 |
Result.FPrototypeInstanceSourceNode := |
| 3354 |
CopyState.DeepCopy(PrototypeInstanceSourceNode) as TX3DPrototypeNode; |
| 3355 |
if PrototypeInstanceHelpers <> nil then |
| 3356 |
Result.FPrototypeInstanceHelpers := CopyState.DeepCopy(PrototypeInstanceHelpers); |
| 3357 |
end; |
| 3358 |
|
| 3359 |
Result.DefaultContainerField := DefaultContainerField; |
| 3360 |
except |
| 3361 |
FreeAndNil(Result); |
| 3362 |
raise; |
| 3363 |
end; |
| 3364 |
end; |
| 3365 |
|
| 3366 |
function TX3DNode.DeepCopy: TX3DNode; |
| 3367 |
var |
| 3368 |
CopyState: TX3DNodeDeepCopyState; |
| 3369 |
begin |
| 3370 |
CopyState := TX3DNodeDeepCopyState.Create; |
| 3371 |
try |
| 3372 |
Result := CopyState.DeepCopy(Self); |
| 3373 |
finally FreeAndNil(CopyState); end; |
| 3374 |
end; |
| 3375 |
|
| 3376 |
procedure TX3DNode.PostAddInterfaceDeclaration(IDecl: TX3DInterfaceDeclaration); |
| 3377 |
begin |
| 3378 |
IDecl.AddFieldOrEvent(Self); |
| 3379 |
end; |
| 3380 |
|
| 3381 |
procedure TX3DNode.AddCustomFieldOrEvent(AFieldOrEvent: TX3DFieldOrEvent); |
| 3382 |
var |
| 3383 |
IDecl: TX3DInterfaceDeclaration; |
| 3384 |
begin |
| 3385 |
IDecl := TX3DInterfaceDeclaration.Create(Self); |
| 3386 |
IDecl.FieldOrEvent := AFieldOrEvent; |
| 3387 |
AFieldOrEvent.ParentInterfaceDeclaration := IDecl; |
| 3388 |
AFieldOrEvent.ParentNode := Self; |
| 3389 |
InterfaceDeclarations.Add(IDecl); |
| 3390 |
PostAddInterfaceDeclaration(IDecl); |
| 3391 |
end; |
| 3392 |
|
| 3393 |
procedure TX3DNode.AddCustomField(AField: TX3DField); |
| 3394 |
begin |
| 3395 |
AddCustomFieldOrEvent(AField); |
| 3396 |
end; |
| 3397 |
|
| 3398 |
function TX3DNode.TransformationChange: TNodeTransformationChange; |
| 3399 |
begin |
| 3400 |
Result := ntcNone; |
| 3401 |
end; |
| 3402 |
|
| 3403 |
function TX3DNode.NiceName: string; |
| 3404 |
begin |
| 3405 |
Result := X3DType; |
| 3406 |
|
| 3407 |
// show ClassName *and* X3DType e.g. for Group:TX3DRootNode |
| 3408 |
if not SameText(ClassName, 'T' + X3DType + 'Node') then |
| 3409 |
Result := SAppendPart(Result, ':', ClassName); |
| 3410 |
|
| 3411 |
if X3DName <> '' then |
| 3412 |
Result := Result + '(' + X3DName + ')'; |
| 3413 |
end; |
| 3414 |
|
| 3415 |
function TX3DNode.GetFields(const Index: Integer): TX3DField; |
| 3416 |
begin |
| 3417 |
Result := FFields[Index]; |
| 3418 |
end; |
| 3419 |
|
| 3420 |
function TX3DNode.FieldsCount: Integer; |
| 3421 |
begin |
| 3422 |
Result := FFields.Count; |
| 3423 |
end; |
| 3424 |
|
| 3425 |
procedure TX3DNode.AddField(const Value: TX3DField); |
| 3426 |
begin |
| 3427 |
FFields.Add(Value); |
| 3428 |
|
| 3429 |
{ add the field also to FFieldsSFNode or FFieldsMFNode } |
| 3430 |
if Value is TSFNode then |
| 3431 |
begin |
| 3432 |
if FFieldsSFNode = nil then |
| 3433 |
FFieldsSFNode := TX3DFieldList.Create(false); |
| 3434 |
FFieldsSFNode.Add(Value); |
| 3435 |
end else |
| 3436 |
if Value is TMFNode then |
| 3437 |
begin |
| 3438 |
if FFieldsMFNode = nil then |
| 3439 |
FFieldsMFNode := TX3DFieldList.Create(false); |
| 3440 |
FFieldsMFNode.Add(Value); |
| 3441 |
end; |
| 3442 |
end; |
| 3443 |
|
| 3444 |
function TX3DNode.IndexOfField(const AName: string): Integer; |
| 3445 |
begin |
| 3446 |
Result := FFields.IndexOfName(AName); |
| 3447 |
end; |
| 3448 |
|
| 3449 |
function TX3DNode.GetEvents(const Index: Integer): TX3DEvent; |
| 3450 |
begin |
| 3451 |
Result := FEvents[Index]; |
| 3452 |
end; |
| 3453 |
|
| 3454 |
function TX3DNode.EventsCount: Integer; |
| 3455 |
begin |
| 3456 |
Result := FEvents.Count; |
| 3457 |
end; |
| 3458 |
|
| 3459 |
procedure TX3DNode.AddEvent(const Value: TX3DEvent); |
| 3460 |
begin |
| 3461 |
FEvents.Add(Value); |
| 3462 |
end; |
| 3463 |
|
| 3464 |
function TX3DNode.IndexOfEvent(const AName: string): Integer; |
| 3465 |
begin |
| 3466 |
Result := FEvents.IndexOfName(AName); |
| 3467 |
end; |
| 3468 |
|
| 3469 |
function TX3DNode.GetPrototypes(const Index: Integer): TX3DPrototypeBase; |
| 3470 |
begin |
| 3471 |
if FPrototypes = nil then |
| 3472 |
raise ERangeError.CreateFmt('Prototypes list is empty, but tried to access index %d', [Index]); |
| 3473 |
Result := FPrototypes[Index]; |
| 3474 |
end; |
| 3475 |
|
| 3476 |
function TX3DNode.PrototypesCount: Integer; |
| 3477 |
begin |
| 3478 |
if FPrototypes <> nil then |
| 3479 |
Result := FPrototypes.Count else |
| 3480 |
Result := 0; |
| 3481 |
end; |
| 3482 |
|
| 3483 |
procedure TX3DNode.AddPrototype(const Value: TX3DPrototypeBase); |
| 3484 |
begin |
| 3485 |
{ create FPrototypes at demand } |
| 3486 |
if FPrototypes = nil then |
| 3487 |
FPrototypes := TX3DPrototypeBaseList.Create(true); |
| 3488 |
FPrototypes.Add(Value); |
| 3489 |
end; |
| 3490 |
|
| 3491 |
function TX3DNode.GetRoutes(const Index: Integer): TX3DRoute; |
| 3492 |
begin |
| 3493 |
if FRoutes = nil then |
| 3494 |
raise ERangeError.CreateFmt('Routes list is empty, but tried to access index %d', [Index]); |
| 3495 |
Result := FRoutes[Index]; |
| 3496 |
end; |
| 3497 |
|
| 3498 |
function TX3DNode.RoutesCount: Integer; |
| 3499 |
begin |
| 3500 |
if FRoutes <> nil then |
| 3501 |
Result := FRoutes.Count else |
| 3502 |
Result := 0; |
| 3503 |
end; |
| 3504 |
|
| 3505 |
procedure TX3DNode.AddRoute(const Value: TX3DRoute); |
| 3506 |
begin |
| 3507 |
{ create FRoutes at demand } |
| 3508 |
if FRoutes = nil then |
| 3509 |
FRoutes := TX3DRouteList.Create(true); |
| 3510 |
FRoutes.Add(Value); |
| 3511 |
end; |
| 3512 |
|
| 3513 |
procedure TX3DNode.AddRoute(const Source, Destination: TX3DFieldOrEvent); |
| 3514 |
var |
| 3515 |
Route: TX3DRoute; |
| 3516 |
begin |
| 3517 |
Route := TX3DRoute.Create; |
| 3518 |
Route.SetSourceDirectly(Source); |
| 3519 |
Route.SetDestinationDirectly(Destination); |
| 3520 |
AddRoute(Route); |
| 3521 |
end; |
| 3522 |
|
| 3523 |
procedure TX3DNode.RemoveRoute(const Value: TX3DRoute); |
| 3524 |
begin |
| 3525 |
Assert(FRoutes <> nil); |
| 3526 |
|
| 3527 |
RemoveRoute(FRoutes.IndexOf(Value)); |
| 3528 |
end; |
| 3529 |
|
| 3530 |
procedure TX3DNode.RemoveRoute(const Index: Integer); |
| 3531 |
begin |
| 3532 |
Assert(FRoutes <> nil); |
| 3533 |
Assert(Between(Index, 0, FRoutes.Count - 1)); |
| 3534 |
|
| 3535 |
FRoutes.Delete(Index); |
| 3536 |
end; |
| 3537 |
|
| 3538 |
function TX3DNode.GetImports(const Index: Integer): TX3DImport; |
| 3539 |
begin |
| 3540 |
if FImports = nil then |
| 3541 |
raise ERangeError.CreateFmt('Imports list is empty, but tried to access index %d', [Index]); |
| 3542 |
Result := FImports[Index] as TX3DImport; |
| 3543 |
end; |
| 3544 |
|
| 3545 |
function TX3DNode.ImportsCount: Integer; |
| 3546 |
begin |
| 3547 |
if FImports <> nil then |
| 3548 |
Result := FImports.Count else |
| 3549 |
Result := 0; |
| 3550 |
end; |
| 3551 |
|
| 3552 |
procedure TX3DNode.AddImport(const Value: TX3DImport); |
| 3553 |
begin |
| 3554 |
{ create FImports at demand } |
| 3555 |
if FImports = nil then |
| 3556 |
FImports := TX3DFileItemList.Create(true); |
| 3557 |
FImports.Add(Value); |
| 3558 |
end; |
| 3559 |
|
| 3560 |
function TX3DNode.GetExports(const Index: Integer): TX3DExport; |
| 3561 |
begin |
| 3562 |
if FExports = nil then |
| 3563 |
raise ERangeError.CreateFmt('Exports list is empty, but tried to access index %d', [Index]); |
| 3564 |
Result := FExports[Index] as TX3DExport; |
| 3565 |
end; |
| 3566 |
|
| 3567 |
function TX3DNode.ExportsCount: Integer; |
| 3568 |
begin |
| 3569 |
if FExports <> nil then |
| 3570 |
Result := FExports.Count else |
| 3571 |
Result := 0; |
| 3572 |
end; |
| 3573 |
|
| 3574 |
procedure TX3DNode.AddExport(const Value: TX3DExport); |
| 3575 |
begin |
| 3576 |
{ create FExports at demand } |
| 3577 |
if FExports = nil then |
| 3578 |
FExports := TX3DFileItemList.Create(true); |
| 3579 |
FExports.Add(Value); |
| 3580 |
end; |
| 3581 |
|
| 3582 |
procedure TX3DNode.UnregisterSceneCallback(Node: TX3DNode); |
| 3583 |
begin |
| 3584 |
Node.Scene := nil; |
| 3585 |
end; |
| 3586 |
|
| 3587 |
procedure TX3DNode.UnregisterScene; |
| 3588 |
begin |
| 3589 |
EnumerateNodes(TX3DNode, {$ifdef FPC}@{$endif} UnregisterSceneCallback, false); |
| 3590 |
end; |
| 3591 |
|
| 3592 |
function TX3DNode.FieldSetByEvent(const Event: TX3DEvent): TX3DField; |
| 3593 |
var |
| 3594 |
F: TX3DField; |
| 3595 |
I: Integer; |
| 3596 |
begin |
| 3597 |
for I := 0 to FieldsCount - 1 do |
| 3598 |
begin |
| 3599 |
F := Fields[I]; |
| 3600 |
if F.EventIn = Event then |
| 3601 |
Exit(F); |
| 3602 |
end; |
| 3603 |
Result := nil; |
| 3604 |
end; |
| 3605 |
|
| 3606 |
function TX3DNode.FieldSendingEvent(const Event: TX3DEvent): TX3DField; |
| 3607 |
var |
| 3608 |
F: TX3DField; |
| 3609 |
I: Integer; |
| 3610 |
begin |
| 3611 |
for I := 0 to FieldsCount - 1 do |
| 3612 |
begin |
| 3613 |
F := Fields[I]; |
| 3614 |
if F.EventOut = Event then |
| 3615 |
Exit(F); |
| 3616 |
end; |
| 3617 |
Result := nil; |
| 3618 |
end; |
| 3619 |
|
| 3620 |
procedure TX3DNode.AddFunctionality(const F: TNodeFunctionality); |
| 3621 |
|
| 3622 |
procedure RefreshShortcuts; |
| 3623 |
begin |
| 3624 |
FTransformFunctionality := Functionality(TTransformFunctionality) as TTransformFunctionality; |
| 3625 |
FTimeFunctionality := Functionality(TTimeDependentFunctionality) as TTimeDependentFunctionality; |
| 3626 |
FGenTexFunctionality := Functionality(TGeneratedTextureFunctionality) as TGeneratedTextureFunctionality; |
| 3627 |
end; |
| 3628 |
|
| 3629 |
begin |
| 3630 |
Assert(not ( (F is TTransformFunctionality) and (FTransformFunctionality <> nil) ), 'Do not add 2 functionalities implementing TTransformFunctionality'); |
| 3631 |
Assert(not ( (F is TTimeDependentFunctionality) and (FTimeFunctionality <> nil) ), 'Do not add 2 functionalities implementing TTimeDependentFunctionality'); |
| 3632 |
Assert(not ( (F is TGeneratedTextureFunctionality) and (FGenTexFunctionality <> nil) ), 'Do not add 2 functionalities implementing TGeneratedTextureFunctionality'); |
| 3633 |
|
| 3634 |
if FFunctionalityList = nil then |
| 3635 |
FFunctionalityList := TNodeFunctionalityList.Create(true); |
| 3636 |
FFunctionalityList.Add(F); |
| 3637 |
|
| 3638 |
RefreshShortcuts; |
| 3639 |
end; |
| 3640 |
|
| 3641 |
function TX3DNode.Functionality( |
| 3642 |
const FunctionalityClass: TNodeFunctionalityClass): TNodeFunctionality; |
| 3643 |
var |
| 3644 |
I: Integer; |
| 3645 |
MaybeResult: TNodeFunctionality; |
| 3646 |
begin |
| 3647 |
Result := nil; |
| 3648 |
if FFunctionalityList <> nil then |
| 3649 |
for I := 0 to FFunctionalityList.Count - 1 do |
| 3650 |
begin |
| 3651 |
MaybeResult := FFunctionalityList[I]; |
| 3652 |
if MaybeResult.InheritsFrom(FunctionalityClass) then |
| 3653 |
Exit(MaybeResult); |
| 3654 |
end; |
| 3655 |
end; |
| 3656 |
|
| 3657 |
procedure TX3DNode.GroupBeforeTraverse(const State: TX3DGraphTraverseState; var WasPointingDeviceSensor: Boolean); |
| 3658 |
begin |
| 3659 |
end; |
| 3660 |
|
| 3661 |
procedure TX3DNode.MoveShapeAssociations(const Sender: TSFNode; const NewNode: TX3DNode); |
| 3662 |
begin |
| 3663 |
if Scene <> nil then |
| 3664 |
Scene.InternalMoveShapeAssociations(Sender.Value, NewNode, InternalSceneShape); |
| 3665 |
end; |
| 3666 |
|
| 3667 |
procedure TX3DNode.WarningOnce(var WarningDone: boolean; const Message: string); |
| 3668 |
begin |
| 3669 |
if not WarningDone then |
| 3670 |
begin |
| 3671 |
WritelnWarning(NiceName, Message); |
| 3672 |
WarningDone := true; |
| 3673 |
end; |
| 3674 |
end; |
| 3675 |
|
| 3676 |
function TX3DNode.DefaultContainerFieldInContext( |
| 3677 |
const Version: TX3DVersion; const ParentNode: TX3DNode): String; |
| 3678 |
begin |
| 3679 |
Result := DefaultContainerField; |
| 3680 |
end; |
| 3681 |
|
| 3682 |
{ TX3DNodeList ------------------------------------------------------------- } |
| 3683 |
|
| 3684 |
function TX3DNodeList.FindName(const Name: string): TX3DNode; |
| 3685 |
var |
| 3686 |
I: Integer; |
| 3687 |
begin |
| 3688 |
if Name = '' then |
| 3689 |
Exit(nil); |
| 3690 |
|
| 3691 |
for I := 0 to Count - 1 do |
| 3692 |
begin |
| 3693 |
Result := Items[I]; |
| 3694 |
if Result.X3DName = Name then |
| 3695 |
Exit; |
| 3696 |
end; |
| 3697 |
Result := nil; |
| 3698 |
end; |
| 3699 |
|
| 3700 |
function TX3DNodeList.IndexOfName(const Name: string): Integer; |
| 3701 |
begin |
| 3702 |
if Name = '' then |
| 3703 |
Exit(-1); |
| 3704 |
|
| 3705 |
for Result := 0 to Count - 1 do |
| 3706 |
if Items[Result].X3DName = Name then |
| 3707 |
Exit; |
| 3708 |
Result := -1; |
| 3709 |
end; |
| 3710 |
|
| 3711 |
procedure TX3DNodeList.AddIfNotExists(const Node: TX3DNode); |
| 3712 |
begin |
| 3713 |
if IndexOf(Node) = -1 then |
| 3714 |
Add(Node); |
| 3715 |
end; |
| 3716 |
|
| 3717 |
function TX3DNodeList.Equals(SecondValue: TObject): boolean; |
| 3718 |
var |
| 3719 |
I: Integer; |
| 3720 |
begin |
| 3721 |
Result := |
| 3722 |
(SecondValue <> nil) and |
| 3723 |
(SecondValue is TX3DNodeList) and |
| 3724 |
(TX3DNodeList(SecondValue).Count = Count); |
| 3725 |
|
| 3726 |
if Result then |
| 3727 |
for I := 0 to Count - 1 do |
| 3728 |
if Items[I] <> TX3DNodeList(SecondValue)[I] then |
| 3729 |
Exit(false); |
| 3730 |
end; |
| 3731 |
|
| 3732 |
procedure TX3DNodeList.Assign(const Source: TX3DNodeList); |
| 3733 |
begin |
| 3734 |
Clear; |
| 3735 |
AddRange(Source); |
| 3736 |
end; |
| 3737 |
|
| 3738 |
procedure TX3DNodeList.Assign(const Source: array of TX3DNode); |
| 3739 |
begin |
| 3740 |
Clear; |
| 3741 |
AddRange(Source); |
| 3742 |
end; |
| 3743 |
|
| 3744 |
{$endif read_implementation} |