| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
TAllowedChildren = (acAll, acClasses, acFunctionality); |
| 20 |
|
| 21 |
TNodeChangeEvent = procedure (const Sender: TSFNode; const NewNode: TX3DNode) of object; |
| 22 |
|
| 23 |
{ X3D field holding a reference to a single node. |
| 24 |
It's defined in this unit, not in X3DFields, since it uses |
| 25 |
TX3DNode definition. NULL value of the field is indicated by |
| 26 |
Value field = nil. |
| 27 |
|
| 28 |
Note that we store AllowedChildren list, which is a list of |
| 29 |
classes allowed as a Value (also nil is always allowed). |
| 30 |
But this is used only to produce warnings for a user. |
| 31 |
You should never assert that Value actually is one the requested |
| 32 |
classes. We want to keep here even not allowed items, |
| 33 |
because we want operation "read from VRML file + write to VRML file" |
| 34 |
to be as non-destructible as possible. So if user wrote |
| 35 |
invalid class hierarchy, we will output this invalid class hierarchy. } |
| 36 |
TSFNode = class(TX3DSingleField) |
| 37 |
strict private |
| 38 |
FParentNode: TX3DNode; |
| 39 |
FOnBeforeValueChange: TNodeChangeEvent; |
| 40 |
AllowedChildren: TAllowedChildren; |
| 41 |
AllowedChildrenClasses: TX3DNodeClassesList; |
| 42 |
AllowedChildrenFunctionality: TNodeFunctionalityClass; |
| 43 |
FDefaultValue: TX3DNode; |
| 44 |
FDefaultValueExists: boolean; |
| 45 |
FWeakLink: boolean; |
| 46 |
FAutomaticWeakLink: Boolean; |
| 47 |
procedure SetValue(const AValue: TX3DNode); |
| 48 |
procedure SetDefaultValue(const ADefaultValue: TX3DNode); |
| 49 |
procedure SetDefaultValueExists(const AValue: boolean); |
| 50 |
procedure SetWeakLink(const AValue: boolean); |
| 51 |
procedure WarningIfUnusedWeakLink(const Reader: TX3DReaderNames); |
| 52 |
procedure DestructionNotification(const Node: TX3DNode); |
| 53 |
private |
| 54 |
FValue: TX3DNode; |
| 55 |
strict protected |
| 56 |
procedure SaveToStreamValue(Writer: TX3DWriter); override; |
| 57 |
function SaveToXmlValue: TSaveToXmlMethod; override; |
| 58 |
public |
| 59 |
{ Construct a field allowing any children class. |
| 60 |
Suitable only for special cases. For example, in instantiated prototypes, |
| 61 |
we must initially just allow all children, otherwise valid prototypes |
| 62 |
with SFNode/MFNode would cause warnings when parsing. } |
| 63 |
constructor CreateUndefined(const AParentNode: TX3DFileItem; |
| 64 |
const AExposed: boolean; const AName: String); override; |
| 65 |
constructor Create(const AParentNode: TX3DNode; |
| 66 |
const AExposed: boolean; const AName: String; |
| 67 |
const AAllowedChildrenClasses: array of TX3DNodeClass; |
| 68 |
const AValue: TX3DNode = nil); overload; |
| 69 |
{ Constructor that takes a list of allowed children classes. |
| 70 |
Note that we copy the contents of AAllowedChildrenClasses, |
| 71 |
not the reference. } |
| 72 |
constructor Create(const AParentNode: TX3DNode; |
| 73 |
const AExposed: boolean; const AName: String; |
| 74 |
const AAllowedChildrenClasses: TX3DNodeClassesList; |
| 75 |
const AValue: TX3DNode = nil); overload; |
| 76 |
{ Constructor that allows as children any implementor of given functionality. } |
| 77 |
constructor Create(const AParentNode: TX3DNode; |
| 78 |
const AExposed: boolean; const AName: String; |
| 79 |
const AnAllowedChildrenFunctionality: TNodeFunctionalityClass; |
| 80 |
const AValue: TX3DNode = nil); overload; |
| 81 |
destructor Destroy; override; |
| 82 |
|
| 83 |
{ Default value of SFNode field. |
| 84 |
|
| 85 |
While X3D specification says for all SFNode fields that their |
| 86 |
default value is NULL, this is not necessarily true for PROTO |
| 87 |
SFNode fiels. So we have to take into account that any DefaultValue |
| 88 |
is possible. |
| 89 |
|
| 90 |
Note that this doesn't have to be @nil, but will be irrelevant |
| 91 |
if not DefaultValueExists. (Once I had an idea to automatically |
| 92 |
set DefaultValue to @nil when DefaultValueExists is set to @false, |
| 93 |
but this was uncomfortable (like "what to do when DefaultValue |
| 94 |
is assigned non-nil when DefaultValueExists is false?").) |
| 95 |
|
| 96 |
Freeing of this is automatically managed, just like the normal |
| 97 |
@link(Value) property. This means that you can simply set |
| 98 |
DefaultValue to @nil or some existing node, and eventual memory |
| 99 |
deallocation of previous DefaultValue node (if unused) will happen |
| 100 |
automatically. } |
| 101 |
property DefaultValue: TX3DNode |
| 102 |
read FDefaultValue write SetDefaultValue; |
| 103 |
property DefaultValueExists: boolean |
| 104 |
read FDefaultValueExists write SetDefaultValueExists default false; |
| 105 |
|
| 106 |
property Value: TX3DNode read FValue write SetValue; |
| 107 |
procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override; |
| 108 |
procedure ParseXMLAttribute(const AttributeValue: String; Reader: TX3DReader); override; |
| 109 |
procedure ParseXMLElement(Element: TDOMElement; Reader: TX3DReader); override; |
| 110 |
|
| 111 |
function EqualsDefaultValue: boolean; override; |
| 112 |
function Equals(SecondValue: TX3DField): boolean; override; |
| 113 |
|
| 114 |
procedure Assign(Source: TPersistent); override; |
| 115 |
procedure AssignValue(Source: TX3DField); override; |
| 116 |
procedure AssignDefaultValueFromValue; override; |
| 117 |
procedure UnassignDefaultValue; override; |
| 118 |
|
| 119 |
{ @exclude } |
| 120 |
procedure InternalAssignDeepCopy(const Source: TX3DFieldOrEvent; const CopyState: TObject); override; |
| 121 |
|
| 122 |
{ VRML node containing this field. May be @nil if unknown, in special |
| 123 |
cases. |
| 124 |
|
| 125 |
Note that this property is exactly the same as |
| 126 |
TX3DFieldOrEvent.ParentNode, |
| 127 |
contains always the same value. But this is declared as TX3DNode, |
| 128 |
so it's more comfortable. } |
| 129 |
property ParentNode: TX3DNode read FParentNode; |
| 130 |
|
| 131 |
class function X3DType: String; override; |
| 132 |
class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override; |
| 133 |
|
| 134 |
{ Checks is the Child allowed as a value of this SFNode, |
| 135 |
and makes WritelnWarning if not. |
| 136 |
|
| 137 |
Check is allowed is done looking at AllowedChildrenAll |
| 138 |
and AllowedChildren properties. |
| 139 |
|
| 140 |
Child must not be @nil. |
| 141 |
|
| 142 |
WritelnWarning message will suggest that this Child is used as value |
| 143 |
of this node. In other words, you should only pass as Child |
| 144 |
a node that you want to assign as Value to this field, |
| 145 |
otherwise WritelnWarning message will be a little unsensible. } |
| 146 |
procedure WarningIfChildNotAllowed(Child: TX3DNode); |
| 147 |
|
| 148 |
function ChildAllowed(Child: TX3DNode): boolean; |
| 149 |
function CurrentChildAllowed: boolean; |
| 150 |
|
| 151 |
{ Calls Func for our @link(Value), assuming it's set (non-nil). |
| 152 |
The main use for this is to simplify implementation of |
| 153 |
TX3DNode.DirectEnumerateActive overrides in TX3DNode descendants. } |
| 154 |
function Enumerate(Func: TEnumerateChildrenFunction): Pointer; |
| 155 |
|
| 156 |
procedure Send(const AValue: TX3DNode); overload; |
| 157 |
|
| 158 |
{ Use weak links to deal with cycles in the X3D graph. |
| 159 |
|
| 160 |
Marking a field as a @italic(weak link) can only be done |
| 161 |
when the field value is empty, right when the field is created, |
| 162 |
in @link(TX3DNode.CreateNode) descendant. |
| 163 |
|
| 164 |
Being a @italic(weak link) means two things: |
| 165 |
|
| 166 |
@orderedList( |
| 167 |
@item(The nodes inside a weak link are not enumerated |
| 168 |
when traversing the X3D graph in @italic(any) way. |
| 169 |
This includes @link(TX3DNode.EnumerateNodes), |
| 170 |
@link(TX3DNode.Traverse) and all others. |
| 171 |
Nodes implementing @link(TX3DNode.DirectEnumerateActive) |
| 172 |
should also omit these fields.) |
| 173 |
|
| 174 |
@item(A weak link does not create a reference count |
| 175 |
preventing the node from being freed (or freeing |
| 176 |
it automatically when ref count drops to zero). |
| 177 |
Instead, weak links merely observe the nodes, and automatically |
| 178 |
set their value to @nil when the node gets freed.) |
| 179 |
) |
| 180 |
|
| 181 |
If effect, this avoids loops when enumerating (and avoids |
| 182 |
recursive loops in reference counts, which would cause memory leaks), |
| 183 |
but use this only when you know that the node |
| 184 |
must occur somewhere else in the X3D graph anyway (or it's OK to |
| 185 |
ignore it). |
| 186 |
For example, this is useful for |
| 187 |
@link(TGeneratedShadowMapNode.Light), as we know that the light |
| 188 |
must occur somewhere else in the graph anyway to be useful. |
| 189 |
} |
| 190 |
property WeakLink: boolean |
| 191 |
read FWeakLink write SetWeakLink default false; |
| 192 |
|
| 193 |
(*Automatically use WeakLink if field would contain a node cycle. |
| 194 |
Using WeakLink=true allows to handle node cycles, like common in VRML/X3D |
| 195 |
|
| 196 |
@preformatted( |
| 197 |
DEF Xxx Transform { |
| 198 |
children [ |
| 199 |
... |
| 200 |
Script { |
| 201 |
... |
| 202 |
inputOutput SFNode someField USE Xxx |
| 203 |
} |
| 204 |
] |
| 205 |
} |
| 206 |
) |
| 207 |
|
| 208 |
When AutomaticWeakLink, the given field contents may, but don't have to, |
| 209 |
be a node cycle -- we only use WeakLink when necessary. |
| 210 |
|
| 211 |
Use this only for fields that are not enumerated by DirectEnumerateActive, |
| 212 |
Traverse etc., otherwise you create a cycle in nodes that we generally cannot handle. *) |
| 213 |
property AutomaticWeakLink: Boolean |
| 214 |
read FAutomaticWeakLink write FAutomaticWeakLink default false; |
| 215 |
|
| 216 |
{ Called when @link(Value) changes. |
| 217 |
Called before the change is actually done, |
| 218 |
right after we know that new value is different than old value. } |
| 219 |
property OnBeforeValueChange: TNodeChangeEvent |
| 220 |
read FOnBeforeValueChange write FOnBeforeValueChange; |
| 221 |
end; |
| 222 |
|
| 223 |
TSFNodeEventHelper = class helper for TSFNodeEvent |
| 224 |
procedure Send(const Value: TX3DNode; const Time: TX3DTime; |
| 225 |
const PartialSend: TPartialSend = nil); overload; |
| 226 |
procedure Send(const Value: TX3DNode); overload; |
| 227 |
end; |
| 228 |
|
| 229 |
{$endif read_interface} |
| 230 |
|
| 231 |
{$ifdef read_implementation} |
| 232 |
|
| 233 |
{ TSFNode --------------------------------------------------------------------- } |
| 234 |
|
| 235 |
constructor TSFNode.CreateUndefined(const AParentNode: TX3DFileItem; |
| 236 |
const AExposed: boolean; const AName: String); |
| 237 |
begin |
| 238 |
inherited; |
| 239 |
Value := nil; |
| 240 |
|
| 241 |
AllowedChildren := acAll; |
| 242 |
{ AllowedChildrenClasses may remain nil in this case } |
| 243 |
|
| 244 |
FDefaultValue := nil; |
| 245 |
FDefaultValueExists := false; |
| 246 |
end; |
| 247 |
|
| 248 |
constructor TSFNode.Create(const AParentNode: TX3DNode; |
| 249 |
const AExposed: boolean; const AName: String; |
| 250 |
const AAllowedChildrenClasses: array of TX3DNodeClass; |
| 251 |
const AValue: TX3DNode); |
| 252 |
begin |
| 253 |
CreateUndefined(AParentNode, AExposed, AName); |
| 254 |
|
| 255 |
{ FParentNode is just a copy of inherited (TX3DFieldOrEvent) FParentNode, |
| 256 |
but casted to TX3DNode } |
| 257 |
FParentNode := AParentNode; |
| 258 |
|
| 259 |
AllowedChildren := acClasses; |
| 260 |
if AllowedChildrenClasses = nil then |
| 261 |
AllowedChildrenClasses := TX3DNodeClassesList.Create; |
| 262 |
AllowedChildrenClasses.AssignArray(AAllowedChildrenClasses); |
| 263 |
|
| 264 |
Value := AValue; |
| 265 |
AssignDefaultValueFromValue; |
| 266 |
end; |
| 267 |
|
| 268 |
constructor TSFNode.Create(const AParentNode: TX3DNode; |
| 269 |
const AExposed: boolean; const AName: String; |
| 270 |
const AAllowedChildrenClasses: TX3DNodeClassesList; |
| 271 |
const AValue: TX3DNode); |
| 272 |
begin |
| 273 |
Create(AParentNode, AExposed, AName, [], AValue); |
| 274 |
|
| 275 |
Assert(AllowedChildren = acClasses); |
| 276 |
Assert(AllowedChildrenClasses <> nil); |
| 277 |
AllowedChildrenClasses.Assign(AAllowedChildrenClasses); |
| 278 |
end; |
| 279 |
|
| 280 |
constructor TSFNode.Create(const AParentNode: TX3DNode; |
| 281 |
const AExposed: boolean; const AName: String; |
| 282 |
const AnAllowedChildrenFunctionality: TNodeFunctionalityClass; |
| 283 |
const AValue: TX3DNode); |
| 284 |
begin |
| 285 |
CreateUndefined(AParentNode, AExposed, AName); |
| 286 |
|
| 287 |
{ FParentNode is just a copy of inherited (TX3DFieldOrEvent) FParentNode, |
| 288 |
but casted to TX3DNode } |
| 289 |
FParentNode := AParentNode; |
| 290 |
|
| 291 |
AllowedChildren := acFunctionality; |
| 292 |
AllowedChildrenFunctionality := AnAllowedChildrenFunctionality; |
| 293 |
|
| 294 |
Value := AValue; |
| 295 |
AssignDefaultValueFromValue; |
| 296 |
end; |
| 297 |
|
| 298 |
destructor TSFNode.Destroy; |
| 299 |
begin |
| 300 |
{ To delete Self from Value.FParentFields, and eventually free Value. } |
| 301 |
Value := nil; |
| 302 |
{ To delete Self from DefaultValue.FParentFields, and eventually free DefaultValue. } |
| 303 |
DefaultValue := nil; |
| 304 |
FreeAndNil(AllowedChildrenClasses); |
| 305 |
inherited; |
| 306 |
end; |
| 307 |
|
| 308 |
function TSFNode.ChildAllowed(Child: TX3DNode): boolean; |
| 309 |
begin |
| 310 |
case AllowedChildren of |
| 311 |
acAll : Result := true; |
| 312 |
acClasses : Result := (Child = nil) or (AllowedChildrenClasses.IndexOfAnyAncestor(Child) <> -1); |
| 313 |
acFunctionality: Result := (Child = nil) or (Child.Functionality(AllowedChildrenFunctionality) <> nil); |
| 314 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 315 |
else raise EInternalError.Create('AllowedChildren?'); |
| 316 |
{$endif} |
| 317 |
end; |
| 318 |
end; |
| 319 |
|
| 320 |
function TSFNode.CurrentChildAllowed: boolean; |
| 321 |
begin |
| 322 |
Result := ChildAllowed(Value); |
| 323 |
end; |
| 324 |
|
| 325 |
procedure TSFNode.WarningIfChildNotAllowed(Child: TX3DNode); |
| 326 |
|
| 327 |
procedure ChildNotAllowed; |
| 328 |
var |
| 329 |
S: String; |
| 330 |
begin |
| 331 |
S := Format('Node "%s" is not allowed in the field "%s"', |
| 332 |
[Child.X3DType, X3DName]); |
| 333 |
if ParentNode <> nil then |
| 334 |
S := S + Format(' of the node "%s"', [ParentNode.X3DType]); |
| 335 |
WritelnWarning('X3D', S); |
| 336 |
end; |
| 337 |
|
| 338 |
begin |
| 339 |
if not ChildAllowed(Child) then |
| 340 |
ChildNotAllowed; |
| 341 |
end; |
| 342 |
|
| 343 |
procedure TSFNode.WarningIfUnusedWeakLink(const Reader: TX3DReaderNames); |
| 344 |
begin |
| 345 |
if WeakLink and |
| 346 |
(Value <> nil) and |
| 347 |
(Value.VRML1ParentsCount = 0) and |
| 348 |
(Value.FParentFields.Count = 0) and |
| 349 |
(Value.KeepExisting = 0) and |
| 350 |
(not Reader.Nodes.NodeDuringReading(Value)) then |
| 351 |
begin |
| 352 |
FValue.FreeIfUnused; // we know it will be freed now |
| 353 |
FValue := nil; |
| 354 |
{ do a warning after freeing FValue, to avoid memory leaks in case OnWarning makes exception } |
| 355 |
WritelnWarning('X3D', Format('A node inside the field "%s" must be already used elsewhere (use USE clause, do not declare a new node here)', |
| 356 |
[NiceName])); |
| 357 |
end; |
| 358 |
end; |
| 359 |
|
| 360 |
procedure TSFNode.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); |
| 361 |
var |
| 362 |
UsingNodeCycle: Boolean; |
| 363 |
NewValue: TX3DNode; |
| 364 |
begin |
| 365 |
if (Lexer.Token = vtKeyword) and (Lexer.TokenKeyword = vkNULL) then |
| 366 |
begin |
| 367 |
Value := nil; |
| 368 |
Lexer.NextToken; |
| 369 |
end else |
| 370 |
begin |
| 371 |
UsingNodeCycle := false; |
| 372 |
{ This is one case when we can use NilIfUnresolvedUSE = @true } |
| 373 |
NewValue := ParseNode(Lexer, Reader as TX3DReaderNames, true, AutomaticWeakLink, UsingNodeCycle); |
| 374 |
if UsingNodeCycle then |
| 375 |
WeakLink := true; |
| 376 |
|
| 377 |
{ Copying NewValue -> Value only after WeakLink was adjusted by AutomaticWeakLink, |
| 378 |
as Value cannot change once WeakLink changed. } |
| 379 |
Value := NewValue; |
| 380 |
|
| 381 |
if Value <> nil then |
| 382 |
begin |
| 383 |
WarningIfChildNotAllowed(Value); |
| 384 |
WarningIfUnusedWeakLink(Reader as TX3DReaderNames); |
| 385 |
end; |
| 386 |
end; |
| 387 |
end; |
| 388 |
|
| 389 |
procedure TSFNode.ParseXMLAttribute(const AttributeValue: String; Reader: TX3DReader); |
| 390 |
const |
| 391 |
SNull = 'NULL'; |
| 392 |
var |
| 393 |
UsedNodeFinished: boolean; |
| 394 |
NewValue: TX3DNode; |
| 395 |
begin |
| 396 |
{ For SFNode and MFNode, X3D XML encoding has special handling: |
| 397 |
field value just indicates the node name, or NULL. |
| 398 |
(other values for SFNode / MFNode cannot be expressed inside |
| 399 |
the attribute). } |
| 400 |
|
| 401 |
NewValue := (Reader as TX3DReaderNames).Bound(AttributeValue, UsedNodeFinished, true); |
| 402 |
{ When AutomaticWeakLink, then UsedNodeFinished=false is not a problem. |
| 403 |
Just flip WeakLink to true, and we can handle a loop in node definition. } |
| 404 |
if (NewValue <> nil) and (not UsedNodeFinished) and AutomaticWeakLink then |
| 405 |
begin |
| 406 |
WeakLink := true; |
| 407 |
UsedNodeFinished := true; |
| 408 |
end; |
| 409 |
if (NewValue <> nil) and (not UsedNodeFinished) then |
| 410 |
begin |
| 411 |
WritelnWarning('X3D', Format('Cycles in X3D graph: SFNode value inside node "%s" refers to the same name', [AttributeValue])); |
| 412 |
Value := nil; |
| 413 |
Exit; |
| 414 |
end; |
| 415 |
|
| 416 |
Value := NewValue; |
| 417 |
if Value = nil then |
| 418 |
begin |
| 419 |
if AttributeValue <> SNull then |
| 420 |
WritelnWarning('X3D', Format('Invalid node name for SFNode field: "%s"', [AttributeValue])); |
| 421 |
end else |
| 422 |
begin |
| 423 |
WarningIfChildNotAllowed(Value); |
| 424 |
WarningIfUnusedWeakLink(Reader as TX3DReaderNames); |
| 425 |
end; |
| 426 |
end; |
| 427 |
|
| 428 |
procedure TSFNode.ParseXMLElement(Element: TDOMElement; Reader: TX3DReader); |
| 429 |
var |
| 430 |
Child: TX3DNode; |
| 431 |
I: TXMLElementIterator; |
| 432 |
ContainerFieldDummy: String; |
| 433 |
UsingNodeCycle: Boolean; |
| 434 |
begin |
| 435 |
I := Element.ChildrenIterator; |
| 436 |
try |
| 437 |
if I.GetNext then |
| 438 |
begin |
| 439 |
UsingNodeCycle := false; |
| 440 |
Child := ParseXMLNode(I.Current, |
| 441 |
ContainerFieldDummy { ignore containerField }, Reader as TX3DReaderNames, |
| 442 |
true, AutomaticWeakLink, UsingNodeCycle); |
| 443 |
if UsingNodeCycle then |
| 444 |
WeakLink := true; |
| 445 |
|
| 446 |
if Child <> nil then |
| 447 |
begin |
| 448 |
Value := Child; |
| 449 |
WarningIfChildNotAllowed(Child); |
| 450 |
end; |
| 451 |
|
| 452 |
if I.GetNext then |
| 453 |
WritelnWarning('X3D', Format('Field "%s" is SFNode, but it contains more than one XML element (2nd element is "%s")', |
| 454 |
[X3DName, I.Current.TagName])); |
| 455 |
end; |
| 456 |
finally FreeAndNil(I) end; |
| 457 |
end; |
| 458 |
|
| 459 |
procedure TSFNode.SaveToStreamValue(Writer: TX3DWriter); |
| 460 |
begin |
| 461 |
if Value = nil then |
| 462 |
{ For XML encoding, note that the NULL value can only be saved |
| 463 |
as an XML attribute (not child element). |
| 464 |
Also, there's no way to specify containerField for NULL value |
| 465 |
--- and that's Ok, since NULL is the default value of all SFNode fields, |
| 466 |
so it's never actually written in normal cases. } |
| 467 |
Writer.Write('NULL') else |
| 468 |
begin |
| 469 |
{ TX3DNode.SaveToStream normally starts from new line with an indent. |
| 470 |
In this case, we want it to start on the same line, so indent must |
| 471 |
be discarded. } |
| 472 |
if Writer.Encoding = xeClassic then |
| 473 |
Writer.DiscardNextIndent; |
| 474 |
|
| 475 |
Value.NodeSaveToStream(Writer, NameForVersion(Writer.Version)); |
| 476 |
end; |
| 477 |
end; |
| 478 |
|
| 479 |
function TSFNode.SaveToXmlValue: TSaveToXmlMethod; |
| 480 |
begin |
| 481 |
{ NULL can only be encoded as an attribute in XML encoding } |
| 482 |
if Value = nil then |
| 483 |
Result := sxAttribute else |
| 484 |
Result := sxChildElement; |
| 485 |
end; |
| 486 |
|
| 487 |
function TSFNode.EqualsDefaultValue: boolean; |
| 488 |
begin |
| 489 |
Result := DefaultValueExists and (Value = DefaultValue); |
| 490 |
end; |
| 491 |
|
| 492 |
function TSFNode.Equals(SecondValue: TX3DField): boolean; |
| 493 |
begin |
| 494 |
Result := (inherited Equals(SecondValue)) and |
| 495 |
(SecondValue is TSFNode) and |
| 496 |
(TSFNode(SecondValue).Value = Value); |
| 497 |
end; |
| 498 |
|
| 499 |
procedure TSFNode.Assign(Source: TPersistent); |
| 500 |
begin |
| 501 |
if Source is TSFNode then |
| 502 |
begin |
| 503 |
{ Assign using Value property, so that FParentFields will get |
| 504 |
correctly updated. } |
| 505 |
Value := TSFNode(Source).Value; |
| 506 |
DefaultValue := TSFNode(Source).DefaultValue; |
| 507 |
DefaultValueExists := TSFNode(Source).DefaultValueExists; |
| 508 |
VRMLFieldAssignCommon(TX3DField(Source)); |
| 509 |
{ When updating this, remember to also update TSFNode.InternalAssignDeepCopy } |
| 510 |
end else |
| 511 |
inherited; |
| 512 |
end; |
| 513 |
|
| 514 |
procedure TSFNode.InternalAssignDeepCopy(const Source: TX3DFieldOrEvent; const CopyState: TObject); |
| 515 |
var |
| 516 |
GoodCopyState: TX3DNodeDeepCopyState; |
| 517 |
|
| 518 |
function DeepCopyNode(const Node: TX3DNode): TX3DNode; |
| 519 |
begin |
| 520 |
if Node = nil then |
| 521 |
Result := nil |
| 522 |
else |
| 523 |
Result := GoodCopyState.DeepCopy(Node); |
| 524 |
end; |
| 525 |
|
| 526 |
begin |
| 527 |
Assert(Source is TSFNode); |
| 528 |
GoodCopyState := CopyState as TX3DNodeDeepCopyState; |
| 529 |
|
| 530 |
{ Similar to TSFNode.Assign } |
| 531 |
Value := DeepCopyNode(TSFNode(Source).Value); |
| 532 |
DefaultValue := DeepCopyNode(TSFNode(Source).DefaultValue); |
| 533 |
DefaultValueExists := TSFNode(Source).DefaultValueExists; |
| 534 |
VRMLFieldAssignCommon(TX3DField(Source)); |
| 535 |
end; |
| 536 |
|
| 537 |
procedure TSFNode.AssignValue(Source: TX3DField); |
| 538 |
begin |
| 539 |
if Source is TSFNode then |
| 540 |
begin |
| 541 |
inherited; |
| 542 |
Value := TSFNode(Source).Value; |
| 543 |
end else |
| 544 |
AssignValueRaiseInvalidClass(Source); |
| 545 |
end; |
| 546 |
|
| 547 |
procedure TSFNode.AssignDefaultValueFromValue; |
| 548 |
begin |
| 549 |
inherited; |
| 550 |
DefaultValue := Value; |
| 551 |
DefaultValueExists := true; |
| 552 |
end; |
| 553 |
|
| 554 |
procedure TSFNode.UnassignDefaultValue; |
| 555 |
begin |
| 556 |
DefaultValueExists := false; |
| 557 |
end; |
| 558 |
|
| 559 |
procedure TSFNode.SetValue(const AValue: TX3DNode); |
| 560 |
begin |
| 561 |
if FValue <> AValue then |
| 562 |
begin |
| 563 |
if Assigned(OnBeforeValueChange) then |
| 564 |
OnBeforeValueChange(Self, AValue); |
| 565 |
|
| 566 |
if FValue <> nil then |
| 567 |
begin |
| 568 |
if WeakLink then |
| 569 |
FValue.RemoveDestructionNotification({$ifdef FPC}@{$endif} DestructionNotification) |
| 570 |
else |
| 571 |
FValue.RemoveParentField(Self); |
| 572 |
end; |
| 573 |
|
| 574 |
FValue := AValue; |
| 575 |
|
| 576 |
if AValue <> nil then |
| 577 |
begin |
| 578 |
if WeakLink then |
| 579 |
FValue.AddDestructionNotification({$ifdef FPC}@{$endif} DestructionNotification) |
| 580 |
else |
| 581 |
FValue.AddParentField(Self); |
| 582 |
end; |
| 583 |
end; |
| 584 |
end; |
| 585 |
|
| 586 |
procedure TSFNode.SetDefaultValue(const ADefaultValue: TX3DNode); |
| 587 |
begin |
| 588 |
if FDefaultValue <> ADefaultValue then |
| 589 |
begin |
| 590 |
if FDefaultValue <> nil then |
| 591 |
begin |
| 592 |
if WeakLink then |
| 593 |
FDefaultValue.RemoveDestructionNotification({$ifdef FPC}@{$endif} DestructionNotification) |
| 594 |
else |
| 595 |
FDefaultValue.RemoveParentField(Self); |
| 596 |
end; |
| 597 |
|
| 598 |
FDefaultValue := ADefaultValue; |
| 599 |
|
| 600 |
if ADefaultValue <> nil then |
| 601 |
begin |
| 602 |
if WeakLink then |
| 603 |
FDefaultValue.AddDestructionNotification({$ifdef FPC}@{$endif} DestructionNotification) |
| 604 |
else |
| 605 |
FDefaultValue.AddParentField(Self); |
| 606 |
end; |
| 607 |
end; |
| 608 |
end; |
| 609 |
|
| 610 |
procedure TSFNode.DestructionNotification(const Node: TX3DNode); |
| 611 |
begin |
| 612 |
if WeakLink then |
| 613 |
begin |
| 614 |
if FValue = Node then |
| 615 |
FValue := nil; |
| 616 |
if FDefaultValue = Node then |
| 617 |
FDefaultValue := nil; |
| 618 |
end; |
| 619 |
end; |
| 620 |
|
| 621 |
procedure TSFNode.SetDefaultValueExists(const AValue: boolean); |
| 622 |
begin |
| 623 |
FDefaultValueExists := AValue; |
| 624 |
end; |
| 625 |
|
| 626 |
class function TSFNode.X3DType: String; |
| 627 |
begin |
| 628 |
Result := 'SFNode'; |
| 629 |
end; |
| 630 |
|
| 631 |
function TSFNode.Enumerate(Func: TEnumerateChildrenFunction): Pointer; |
| 632 |
begin |
| 633 |
{ checking CurrentChildAllowed is not really necessary here, |
| 634 |
and costs time, because it may do a slow Supports() call } |
| 635 |
//if (Value <> nil) and CurrentChildAllowed and not WeakLink then |
| 636 |
|
| 637 |
if (Value <> nil) and not WeakLink then |
| 638 |
Result := Func(ParentNode, Value) |
| 639 |
else |
| 640 |
Result := nil; |
| 641 |
end; |
| 642 |
|
| 643 |
class function TSFNode.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; |
| 644 |
begin |
| 645 |
Result := TSFNodeEvent.Create(AParentNode, AName, AInEvent); |
| 646 |
end; |
| 647 |
|
| 648 |
procedure TSFNode.Send(const AValue: TX3DNode); |
| 649 |
var |
| 650 |
FieldValue: TSFNode; |
| 651 |
begin |
| 652 |
{ We construct using CreateUndefined constructor,to have AllowedChildren = acAll } |
| 653 |
{ AExposed = false below, because not needed otherwise. } |
| 654 |
FieldValue := TSFNode.CreateUndefined(ParentNode, false, X3DName); |
| 655 |
try |
| 656 |
FieldValue.Value := AValue; |
| 657 |
Send(FieldValue); |
| 658 |
finally FreeAndNil(FieldValue) end; |
| 659 |
end; |
| 660 |
|
| 661 |
procedure TSFNode.SetWeakLink(const AValue: boolean); |
| 662 |
begin |
| 663 |
if FWeakLink <> AValue then |
| 664 |
begin |
| 665 |
if Value <> nil then |
| 666 |
raise EInternalError.Create('TSFNode.WeakLink cannot change when some node is already assigned'); |
| 667 |
FWeakLink := AValue; |
| 668 |
end; |
| 669 |
end; |
| 670 |
|
| 671 |
{ TSFNodeEventHelper --------------------------------------------------------- } |
| 672 |
|
| 673 |
procedure TSFNodeEventHelper.Send(const Value: TX3DNode; const Time: TX3DTime; |
| 674 |
const PartialSend: TPartialSend); |
| 675 |
var |
| 676 |
Field: TX3DField; |
| 677 |
begin |
| 678 |
Field := CreateTemp(PartialSend); |
| 679 |
(Field as TSFNode).Value := Value; |
| 680 |
try |
| 681 |
Self.Send(Field, Time); |
| 682 |
finally FreeTemp(Field) end; |
| 683 |
end; |
| 684 |
|
| 685 |
procedure TSFNodeEventHelper.Send(const Value: TX3DNode); |
| 686 |
begin |
| 687 |
if (ParentNode <> nil) and |
| 688 |
(TX3DNode(ParentNode).Scene <> nil) then |
| 689 |
Send(Value, TX3DNode(ParentNode).Scene.NextEventTime); |
| 690 |
end; |
| 691 |
|
| 692 |
{$endif read_implementation} |