| 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 |
{ Raised when there's a problem instantiating X3D prototype, |
| 20 |
see @link(TX3DPrototypeNode.Instantiate). |
| 21 |
|
| 22 |
User code usually doesn't see this exception, unless you call |
| 23 |
@link(TX3DPrototypeNode.Instantiate) explicitly (but you usually don't). |
| 24 |
In normal circumstances, the engine calls @link(TX3DPrototypeNode.Instantiate) |
| 25 |
when necessary, and converts eventual EX3DPrototypeInstantiateError |
| 26 |
exceptions into warnings (reported by |
| 27 |
@link(TCastleApplicationProperties.OnWarning ApplicationProperties.OnWarning)). } |
| 28 |
EX3DPrototypeInstantiateError = class(EX3DError); |
| 29 |
|
| 30 |
{ Node with information about X3D prototype. |
| 31 |
|
| 32 |
This node will have fields |
| 33 |
initialized according to associated Prototype.InterfaceDeclarations. |
| 34 |
This way you can simply parse this node (just like any other node) |
| 35 |
to parse prototype instance. |
| 36 |
|
| 37 |
The prototype may be instantiated. After parsing you can |
| 38 |
do it by @link(Instantiate) method. In case of non-external prototype, |
| 39 |
this should always be possible (for a valid X3D files, that is), |
| 40 |
in case of external prototype this may requite loading the external |
| 41 |
prototype file. |
| 42 |
|
| 43 |
This node cannot be created by standard Create method, |
| 44 |
always use CreatePrototypeNode. } |
| 45 |
TX3DPrototypeNode = class(TX3DNode) |
| 46 |
strict private |
| 47 |
FPrototype: TX3DPrototypeBase; |
| 48 |
|
| 49 |
function PrepareInstantiateIsClause(Node, Child: TX3DNode): Pointer; |
| 50 |
|
| 51 |
(*This searches Node for fields/events with "IS" clauses, and handles them: |
| 52 |
for fields, this means copying field value from Self to Child |
| 53 |
(and setting ValueFromIsClause). |
| 54 |
for events, this means adding internal routes to route event |
| 55 |
from/to Self to/from Child. |
| 56 |
|
| 57 |
Handled IS clauses are removed (to not be seen by next calls |
| 58 |
of InstantiateIsClauses on the same node, or on it's copy, |
| 59 |
see below). |
| 60 |
|
| 61 |
It also descends recursively into all children nodes. |
| 62 |
Note that it descends into all |
| 63 |
nodes, even the ones that came from another prototype |
| 64 |
(PrototypeInstance is @true). It doesn't try to omit them, |
| 65 |
as they may have "IS" clauses that refer to our fields. |
| 66 |
Consider part of key_sensor.x3dv test: |
| 67 |
|
| 68 |
@preformatted( |
| 69 |
PROTO SimpleText [ |
| 70 |
inputOutput MFString onestring "" |
| 71 |
] { Shape { geometry Text { string IS onestring } } } |
| 72 |
|
| 73 |
PROTO PressedText [ |
| 74 |
inputOutput MFString againstring "" |
| 75 |
] { SimpleText { onestring IS againstring } } |
| 76 |
|
| 77 |
PressedText { againstring "zero" } |
| 78 |
) |
| 79 |
|
| 80 |
After expanding SimpleText within PressedText, we have |
| 81 |
@code(Shape { geometry Text { string IS againstring } }), |
| 82 |
that is we resolved "IS onestring" to yet another IS clause: |
| 83 |
"IS againstring". Which means that when expanding PressedText, |
| 84 |
we have to process everything again, to eventually fill "againstring" |
| 85 |
value. *) |
| 86 |
function InstantiateIsClauses(Node, Child: TX3DNode): Pointer; |
| 87 |
|
| 88 |
{ Handle "IS" clause on Destination field/event, by copying it's value |
| 89 |
from Source. |
| 90 |
|
| 91 |
For fields basically does Destination.AssignValue(Source). |
| 92 |
In case of EX3DFieldAssign, make WritelnWarning with clear message. |
| 93 |
|
| 94 |
For events establishes internal route. |
| 95 |
|
| 96 |
Assumes that all Source "IS" clauses are not expanded yet. |
| 97 |
In fact, Source field/event always comes from this node, |
| 98 |
that is TX3DPrototypeNode. So we just expand prototype within |
| 99 |
TX3DPrototypeNode. If this TX3DPrototypeNode is itself within |
| 100 |
another prototype, there's no way Source.IsClauseNames could be expanded |
| 101 |
already. |
| 102 |
|
| 103 |
Above paragraph means that when Source.IsClauseNames exist, |
| 104 |
we know what to do. If this is about fields, then we have to copy |
| 105 |
field's value (in case in Source field value was specified explicitly) |
| 106 |
and also assign Source.IsClauseNames as new Destination.IsClauseNames |
| 107 |
(eventually, higher IS clauses will override field's value). |
| 108 |
For events, we also copy IsClauseNames, in addition to creating |
| 109 |
internal route (is internal route still necessary? for exposedFields? |
| 110 |
seems so, I'm not sure...). } |
| 111 |
procedure FieldOrEventHandleIsClause( |
| 112 |
Destination, Source: TX3DFieldOrEvent; |
| 113 |
NewIsClauseNames: TCastleStringList); |
| 114 |
protected |
| 115 |
function DeepCopyCreate(CopyState: TX3DNodeDeepCopyState): TX3DNode; override; |
| 116 |
public |
| 117 |
{ This constructor will raise exception for TX3DPrototypeNode. |
| 118 |
Always use CreatePrototypeNode for this node class. } |
| 119 |
constructor Create(const AName, ABaseUrl: String); override; |
| 120 |
constructor CreatePrototypeNode(const AName, ABaseUrl: String; |
| 121 |
APrototype: TX3DPrototypeBase); |
| 122 |
function X3DType: String; override; |
| 123 |
|
| 124 |
property Prototype: TX3DPrototypeBase read FPrototype; |
| 125 |
|
| 126 |
{ Instantiate the prototype, that is create new X3D node |
| 127 |
(of "normal" classs, not TX3DPrototypeNode) using prototype description. |
| 128 |
|
| 129 |
For non-external prototype, in essense it just takes Prototype.Node |
| 130 |
and returns it's copy. For external prototype it first loads external file, |
| 131 |
and then uses non-external prototype there. Eventually, |
| 132 |
for external prototype we may also use build-in node (if URN will |
| 133 |
indicate so). |
| 134 |
|
| 135 |
Actually, the process is a little more involved (see below for |
| 136 |
details), but the idea is that returned node can be simply inserted |
| 137 |
into VRML hierarchy and works just like a normal node. |
| 138 |
The important feature is that returned instance class is the same |
| 139 |
that was specified as a first prototype node. For example, if the |
| 140 |
prototype should expand to Material node, then this returns |
| 141 |
TMaterialNode. Just like Material node would be normally specified, |
| 142 |
not created by some prototype. |
| 143 |
|
| 144 |
Note that this TX3DPrototypeNode becomes "owned" by returned |
| 145 |
node instance, in PrototypeInstanceSourceNode. |
| 146 |
(that's needed for returned node's SaveToStream to work correctly). |
| 147 |
|
| 148 |
Details: |
| 149 |
@unorderedList( |
| 150 |
@item( |
| 151 |
Prototype.Node may be just a wrapper, i.e. TX3DRootNode. |
| 152 |
|
| 153 |
In this case the first children of Prototype.Node is used |
| 154 |
to create instance. The rest of the wrapper (with this first children |
| 155 |
removed, to not cause cycles) is also duplicated and set |
| 156 |
as new node's PrototypeInstanceHelpers.) |
| 157 |
|
| 158 |
@item( |
| 159 |
Returned Node (with all it's helpers in PrototypeInstanceHelpers) |
| 160 |
has "IS" clauses everywhere filled, according to our field values.) |
| 161 |
|
| 162 |
@item(Name of returned node is copied from our Name.) |
| 163 |
|
| 164 |
@item( |
| 165 |
For SaveToStream to work, returned Node has PrototypeInstance = @true, |
| 166 |
and PrototypeInstanceSourceNode set to Self. This allows SaveToStream |
| 167 |
to correctly save using PrototypeInstanceSourceNode, instead |
| 168 |
of writing actual node contents.)) |
| 169 |
|
| 170 |
@raises(EX3DPrototypeInstantiateError If for some reason |
| 171 |
the prototype cannot be instantiated. |
| 172 |
Outside code should catch this and replace with |
| 173 |
ApplicationProperties.OnWarning, if possible.) |
| 174 |
} |
| 175 |
function Instantiate: TX3DNode; |
| 176 |
end; |
| 177 |
|
| 178 |
{ Common class to represent X3D external prototypes (referencing another file) |
| 179 |
or non-external prototypes (describing how to expand the prototype into other nodes). } |
| 180 |
TX3DPrototypeBase = class(TX3DFileItem) |
| 181 |
private |
| 182 |
FX3DName: String; |
| 183 |
FInterfaceDeclarations: TX3DInterfaceDeclarationList; |
| 184 |
|
| 185 |
FBaseUrl: String; |
| 186 |
|
| 187 |
{ Parses InterfaceDeclarations. Also inits BaseUrl from |
| 188 |
Names.BaseUrl, by the way. } |
| 189 |
procedure ParseInterfaceDeclarations(ExternalProto: boolean; |
| 190 |
Lexer: TX3DLexer; Reader: TX3DReaderNames); |
| 191 |
|
| 192 |
{ Parse interface declarations in XML encoding. |
| 193 |
Handle sequence of <field> elements. |
| 194 |
|
| 195 |
Note: unlike classic ParseInterfaceDeclarations, |
| 196 |
this doesn't set BaseUrl, do it yourself (because often |
| 197 |
you do not have 'ProtoInterface', so you would have to do it yourself |
| 198 |
anyway). } |
| 199 |
procedure ParseInterfaceDeclarationsXML(ExternalProto: boolean; |
| 200 |
Element: TDOMElement; Reader: TX3DReaderNames); |
| 201 |
|
| 202 |
{ Saves interface declarations of the prototype. |
| 203 |
For classic encoding, they are already enclosed in [ ]. } |
| 204 |
procedure SaveInterfaceDeclarationsToStream( |
| 205 |
Writer: TX3DWriter; ExternalProto: boolean); |
| 206 |
public |
| 207 |
constructor Create; |
| 208 |
destructor Destroy; override; |
| 209 |
property X3DName: String read FX3DName write FX3DName; |
| 210 |
{$ifdef FPC} |
| 211 |
property Name: String read FX3DName write FX3DName; deprecated 'use X3DName'; |
| 212 |
{$endif} |
| 213 |
property InterfaceDeclarations: TX3DInterfaceDeclarationList |
| 214 |
read FInterfaceDeclarations; |
| 215 |
|
| 216 |
{ Parse prototype, and add it to Names.Prototypes. |
| 217 |
Adds to @code(Names) by @code(Names.Prototypes.Bind(Self)). |
| 218 |
@groupBegin } |
| 219 |
procedure Parse(Lexer: TX3DLexer; Reader: TX3DReaderNames); virtual; abstract; |
| 220 |
procedure ParseXML(Element: TDOMElement; Reader: TX3DReaderNames); virtual; abstract; |
| 221 |
{ @groupEnd } |
| 222 |
|
| 223 |
{ The base URL path used to resolve urls inside. |
| 224 |
For now, used by EXTERNPROTO urls. |
| 225 |
See TX3DNode.BaseUrl for more comments. } |
| 226 |
property BaseUrl: String read FBaseUrl write FBaseUrl; |
| 227 |
end; |
| 228 |
|
| 229 |
TX3DPrototypeBaseList = class({$ifdef FPC}specialize{$endif} TObjectList<TX3DPrototypeBase>); |
| 230 |
|
| 231 |
{ X3D non-external prototype (describing how to expand the prototype into other nodes). } |
| 232 |
TX3DPrototype = class(TX3DPrototypeBase) |
| 233 |
strict private |
| 234 |
FNode: TX3DRootNode; |
| 235 |
public |
| 236 |
destructor Destroy; override; |
| 237 |
|
| 238 |
procedure Parse(Lexer: TX3DLexer; Reader: TX3DReaderNames); override; |
| 239 |
procedure ParseXML(Element: TDOMElement; Reader: TX3DReaderNames); override; |
| 240 |
procedure SaveToStream(Writer: TX3DWriter); override; |
| 241 |
|
| 242 |
{ Prototype contents: all nodes, prototypes, routes defined inside. } |
| 243 |
property Node: TX3DRootNode read FNode; |
| 244 |
end; |
| 245 |
|
| 246 |
{ X3D external prototype (referencing another file). } |
| 247 |
TX3DExternalPrototype = class(TX3DPrototypeBase) |
| 248 |
strict private |
| 249 |
FUrlList: TMFString; |
| 250 |
|
| 251 |
{ FReferencedPrototype has links to other parts of the VRML graph. |
| 252 |
Not only FReferencedPrototype.Node, but also |
| 253 |
FReferencedPrototype.InterfaceDeclaration may have links to it: |
| 254 |
if referenced node has SFNode or MFNode fields and their default |
| 255 |
values have "USE ..." clauses. |
| 256 |
So it's best to keep whole ReferencedPrototypeNode (whole VRML file |
| 257 |
that contained this prototype) loaded. } |
| 258 |
ReferencedPrototypeNode: TX3DRootNode; |
| 259 |
|
| 260 |
FReferencedPrototype: TX3DPrototype; |
| 261 |
|
| 262 |
FReferencedClass: TX3DNodeClass; |
| 263 |
public |
| 264 |
constructor Create; |
| 265 |
destructor Destroy; override; |
| 266 |
property UrlList: TMFString read FUrlList; |
| 267 |
|
| 268 |
procedure Parse(Lexer: TX3DLexer; Reader: TX3DReaderNames); override; |
| 269 |
procedure ParseXML(Element: TDOMElement; Reader: TX3DReaderNames); override; |
| 270 |
procedure SaveToStream(Writer: TX3DWriter); override; |
| 271 |
|
| 272 |
property ReferencedPrototype: TX3DPrototype read FReferencedPrototype; |
| 273 |
property ReferencedClass: TX3DNodeClass read FReferencedClass; |
| 274 |
|
| 275 |
{ Loads URL, until the first success. Sets either ReferencedClass to non-nil |
| 276 |
(if it's built-in node) or ReferencedPrototype (if prototype expansion |
| 277 |
found in external file). } |
| 278 |
procedure LoadReferenced(const Version: TX3DVersion); |
| 279 |
procedure UnloadReferenced; |
| 280 |
end; |
| 281 |
|
| 282 |
{$endif read_interface} |
| 283 |
|
| 284 |
{$ifdef read_implementation} |
| 285 |
|
| 286 |
{ TX3DPrototypeNode --------------------------------------------------------- } |
| 287 |
|
| 288 |
constructor TX3DPrototypeNode.Create(const AName, ABaseUrl: String); |
| 289 |
begin |
| 290 |
raise EInternalError.Create('TX3DPrototypeNode node must be created' + |
| 291 |
' using CreatePrototypeNode, never default constructor'); |
| 292 |
end; |
| 293 |
|
| 294 |
constructor TX3DPrototypeNode.CreatePrototypeNode( |
| 295 |
const AName, ABaseUrl: String; |
| 296 |
APrototype: TX3DPrototypeBase); |
| 297 |
var |
| 298 |
I: TX3DInterfaceDeclaration; |
| 299 |
Index: Integer; |
| 300 |
ProtoInitial: TX3DPrototypeBase; |
| 301 |
begin |
| 302 |
inherited Create(AName, ABaseUrl); |
| 303 |
FPrototype := APrototype; |
| 304 |
|
| 305 |
ProtoInitial := Prototype; |
| 306 |
|
| 307 |
if (ProtoInitial is TX3DExternalPrototype) and |
| 308 |
(TX3DExternalPrototype(ProtoInitial).ReferencedPrototype <> nil) then |
| 309 |
{ It's important to use ReferencedPrototype, not just current |
| 310 |
Prototype, in this case. That's because field's default values |
| 311 |
should be set by constructor (before parsing the specified |
| 312 |
fields), and TX3DExternalPrototype doesn't have default values |
| 313 |
available. |
| 314 |
|
| 315 |
If ReferencedPrototype = nil (e.g. because couldn't |
| 316 |
be loaded) then Instantiate will not be able to instantiate |
| 317 |
it anyway (and will produce appropriate WritelnWarning). } |
| 318 |
ProtoInitial := TX3DExternalPrototype(ProtoInitial).ReferencedPrototype; |
| 319 |
|
| 320 |
for Index := 0 to ProtoInitial.InterfaceDeclarations.Count - 1 do |
| 321 |
begin |
| 322 |
I := ProtoInitial.InterfaceDeclarations.Items[Index]; |
| 323 |
I.CopyAndAddFieldOrEvent(Self); |
| 324 |
end; |
| 325 |
end; |
| 326 |
|
| 327 |
function TX3DPrototypeNode.DeepCopyCreate(CopyState: TX3DNodeDeepCopyState): TX3DNode; |
| 328 |
begin |
| 329 |
Result := TX3DPrototypeNode.CreatePrototypeNode(X3DName, BaseUrl, |
| 330 |
{ TODO: for now, we don't copy proto, instead simply passing the same |
| 331 |
proto reference. } |
| 332 |
Prototype); |
| 333 |
end; |
| 334 |
|
| 335 |
function TX3DPrototypeNode.X3DType: String; |
| 336 |
begin |
| 337 |
Result := Prototype.X3DName; |
| 338 |
end; |
| 339 |
|
| 340 |
procedure TX3DPrototypeNode.FieldOrEventHandleIsClause( |
| 341 |
Destination, Source: TX3DFieldOrEvent; |
| 342 |
NewIsClauseNames: TCastleStringList); |
| 343 |
var |
| 344 |
DestinationField, SourceField: TX3DField; |
| 345 |
DestinationEvent, SourceEvent: TX3DEvent; |
| 346 |
Route: TX3DRoute; |
| 347 |
I: Integer; |
| 348 |
const |
| 349 |
InEventName: array [boolean] of string = ( 'output', 'input' ); |
| 350 |
begin |
| 351 |
{ When Source.IsClauseNamesCount <> 0, then we're expanded |
| 352 |
within the definition of another prototype. |
| 353 |
So Destination.IsClauseNames referers to Source |
| 354 |
(from current Prototype), but Source.IsClauseNames refers to yet |
| 355 |
another, enclosing, prototype (that will be expanded later --- for |
| 356 |
now we're within enclosing prototype definition). |
| 357 |
|
| 358 |
See comments in the interface for more. } |
| 359 |
|
| 360 |
{ Intuitively: NewIsClauseNames.AddStrings(Source.IsClauseNames); } |
| 361 |
for I := 0 to Source.IsClauseNamesCount - 1 do |
| 362 |
NewIsClauseNames.Add(Source.IsClauseNames[I]); |
| 363 |
|
| 364 |
if Source is TX3DField then |
| 365 |
begin |
| 366 |
Assert(Destination is TX3DField); |
| 367 |
SourceField := Source as TX3DField; |
| 368 |
DestinationField := Destination as TX3DField; |
| 369 |
|
| 370 |
try |
| 371 |
DestinationField.AssignValue(SourceField); |
| 372 |
DestinationField.ValueFromIsClause := true; |
| 373 |
except |
| 374 |
on E: EX3DFieldAssignInvalidClass do |
| 375 |
begin |
| 376 |
WritelnWarning('VRML/X3D', Format('Within prototype "%s", ' + |
| 377 |
'field of type %s (named "%s") references ' + |
| 378 |
'(by "IS" clause) field of different type %s (named "%s")', |
| 379 |
[Prototype.X3DName, |
| 380 |
DestinationField.X3DType, |
| 381 |
Destination.X3DName, |
| 382 |
SourceField.X3DType, |
| 383 |
Source.X3DName])); |
| 384 |
end; |
| 385 |
on E: EX3DFieldAssign do |
| 386 |
begin |
| 387 |
WritelnWarning('VRML/X3D', Format('Error when expanding prototype "%s": ', |
| 388 |
[Prototype.X3DName]) + E.Message); |
| 389 |
end; |
| 390 |
end; |
| 391 |
end else |
| 392 |
if Source is TX3DEvent then |
| 393 |
begin |
| 394 |
Assert(Destination is TX3DEvent); |
| 395 |
SourceEvent := Source as TX3DEvent; |
| 396 |
DestinationEvent := Destination as TX3DEvent; |
| 397 |
|
| 398 |
if SourceEvent.InEvent <> DestinationEvent.InEvent then |
| 399 |
begin |
| 400 |
WritelnWarning('VRML/X3D', Format('When expanding prototype "%s": "%s" event references (by "IS" clause) "%s" event', |
| 401 |
[ Prototype.X3DName, |
| 402 |
InEventName[DestinationEvent.InEvent], |
| 403 |
InEventName[SourceEvent.InEvent] ])); |
| 404 |
Exit; |
| 405 |
end; |
| 406 |
|
| 407 |
if SourceEvent.FieldClass <> DestinationEvent.FieldClass then |
| 408 |
begin |
| 409 |
WritelnWarning('VRML/X3D', Format('When expanding prototype "%s": "%s" event references (by "IS" clause) "%s" event', |
| 410 |
[ Prototype.X3DName, |
| 411 |
DestinationEvent.FieldClass.X3DType, |
| 412 |
SourceEvent.FieldClass.X3DType ])); |
| 413 |
Exit; |
| 414 |
end; |
| 415 |
|
| 416 |
Route := TX3DRoute.Create; |
| 417 |
Route.Internal := true; |
| 418 |
|
| 419 |
try |
| 420 |
if SourceEvent.InEvent then |
| 421 |
begin |
| 422 |
Route.SetSourceDirectly(SourceEvent); |
| 423 |
Route.SetDestinationDirectly(DestinationEvent); |
| 424 |
end else |
| 425 |
begin |
| 426 |
Route.SetSourceDirectly(DestinationEvent); |
| 427 |
Route.SetDestinationDirectly(SourceEvent); |
| 428 |
end; |
| 429 |
|
| 430 |
AddRoute(Route); |
| 431 |
except |
| 432 |
FreeAndNil(Route); |
| 433 |
raise; |
| 434 |
end; |
| 435 |
end; |
| 436 |
end; |
| 437 |
|
| 438 |
function TX3DPrototypeNode.InstantiateIsClauses(Node, Child: TX3DNode): Pointer; |
| 439 |
|
| 440 |
{ In terminology of VRML/X3D specs, |
| 441 |
InstanceField/Event is the one in "prototype definition" |
| 442 |
(that is, inside prototype content) and |
| 443 |
OutField/Event is the one in "prototype declaration". |
| 444 |
|
| 445 |
This is where we implement table "Rules for mapping PROTOTYPE |
| 446 |
declarations to node instances" in specifications about |
| 447 |
"PROTO definition semantics". } |
| 448 |
|
| 449 |
procedure ExpandEvent(InstanceEvent: TX3DEvent); forward; |
| 450 |
|
| 451 |
procedure ExpandField(InstanceField: TX3DField); |
| 452 |
var |
| 453 |
OurField: TX3DField; |
| 454 |
OurEvent: TX3DEvent; |
| 455 |
OurFieldIndex: Integer; |
| 456 |
I: Integer; |
| 457 |
IsClauseName: String; |
| 458 |
NewIsClauseNames: TCastleStringList; |
| 459 |
begin |
| 460 |
if InstanceField.IsClauseNamesCount <> 0 then |
| 461 |
begin |
| 462 |
NewIsClauseNames := TCastleStringList.Create; |
| 463 |
try |
| 464 |
for I := 0 to InstanceField.IsClauseNamesCount - 1 do |
| 465 |
begin |
| 466 |
IsClauseName := InstanceField.IsClauseNames[I]; |
| 467 |
OurFieldIndex := IndexOfField(IsClauseName); |
| 468 |
if OurFieldIndex <> -1 then |
| 469 |
begin |
| 470 |
OurField := Fields[OurFieldIndex]; |
| 471 |
FieldOrEventHandleIsClause(InstanceField, OurField, NewIsClauseNames); |
| 472 |
|
| 473 |
if InstanceField.Exposed and OurField.Exposed then |
| 474 |
begin |
| 475 |
{ We have to "break" exposed fields inside TX3DPrototypeNode, |
| 476 |
that is they will not automatically get changes and forwarding |
| 477 |
in event to out event. Reason: |
| 478 |
|
| 479 |
- This causes cycles in routes (remember that |
| 480 |
FieldOrEventHandleIsClause creates internal routes |
| 481 |
for "IS" between events.) When something sets event, |
| 482 |
by sending to InstanceField.EventIn, then this will |
| 483 |
be forwarded to InstanceField.EventOut. But also |
| 484 |
will be forwarded to OurField.EventIn (this must be done, |
| 485 |
in case other "IS" clauses interact with these exposed |
| 486 |
events). Without breaking ExposedEventsLinked, |
| 487 |
OurField.EventIn would forward this once again to |
| 488 |
InstanceField.EventOut, causing all routes from |
| 489 |
InstanceField.EventOut to the outside be detected as loops, |
| 490 |
as another event travels through them. |
| 491 |
|
| 492 |
- Lastly, this is not needed. Copying field's value |
| 493 |
is useless, as field's value in TX3DPrototypeNode |
| 494 |
is useless (field value must get to actual field in expanded |
| 495 |
hierarchy). |
| 496 |
} |
| 497 |
OurField.ExposedEventsLinked := false; |
| 498 |
|
| 499 |
{ Note that I pass here NewIsClauseNames, that is |
| 500 |
is our in/out event will have specialized IS clauses, |
| 501 |
they will be assigned to whole exposed field. |
| 502 |
This is Ok, as event may only point to another event |
| 503 |
higher in proto nesting. And exposed field may also refer |
| 504 |
to another event (inputOnly or outputOnly) higher in |
| 505 |
proto nesting. } |
| 506 |
|
| 507 |
FieldOrEventHandleIsClause(InstanceField.EventIn , OurField.EventIn , NewIsClauseNames); |
| 508 |
FieldOrEventHandleIsClause(InstanceField.EventOut, OurField.EventOut, NewIsClauseNames); |
| 509 |
end; |
| 510 |
end else |
| 511 |
if InstanceField.Exposed then |
| 512 |
begin |
| 513 |
{ exposed field may also reference by IS clause the simple |
| 514 |
"only input" or "only output" event. } |
| 515 |
{ TODO: untested case } |
| 516 |
OurEvent := AnyEvent(IsClauseName); |
| 517 |
if OurEvent <> nil then |
| 518 |
begin |
| 519 |
if OurEvent.InEvent then |
| 520 |
FieldOrEventHandleIsClause(InstanceField.EventIn , OurEvent, NewIsClauseNames) else |
| 521 |
FieldOrEventHandleIsClause(InstanceField.EventOut, OurEvent, NewIsClauseNames); |
| 522 |
end else |
| 523 |
WritelnWarning('VRML/X3D', Format('Within prototype "%s", exposed field "%s" references (by "IS" clause) non-existing field/event name "%s"', |
| 524 |
[Prototype.X3DName, InstanceField.X3DName, IsClauseName])); |
| 525 |
end else |
| 526 |
WritelnWarning('VRML/X3D', Format('Within prototype "%s", field "%s" references (by "IS" clause) non-existing field "%s"', |
| 527 |
[Prototype.X3DName, InstanceField.X3DName, IsClauseName])); |
| 528 |
end; |
| 529 |
|
| 530 |
InstanceField.IsClauseNamesAssign(NewIsClauseNames); |
| 531 |
finally FreeAndNil(NewIsClauseNames) end; |
| 532 |
end; |
| 533 |
|
| 534 |
if InstanceField.Exposed then |
| 535 |
begin |
| 536 |
(*Completely independent from the fact whether InstanceField.IsClause, |
| 537 |
it's exposed events may also have their own IS clauses, for example |
| 538 |
|
| 539 |
DirectionalLight { |
| 540 |
on IS light_on_initial_value |
| 541 |
set_on IS light_on_setter |
| 542 |
on_changed IS light_on_getter |
| 543 |
} |
| 544 |
*) |
| 545 |
|
| 546 |
ExpandEvent(InstanceField.EventIn); |
| 547 |
ExpandEvent(InstanceField.EventOut); |
| 548 |
end; |
| 549 |
end; |
| 550 |
|
| 551 |
procedure ExpandEvent(InstanceEvent: TX3DEvent); |
| 552 |
var |
| 553 |
OurEvent: TX3DEvent; |
| 554 |
OurEventIndex: Integer; |
| 555 |
I: Integer; |
| 556 |
IsClauseName: String; |
| 557 |
NewIsClauseNames: TCastleStringList; |
| 558 |
begin |
| 559 |
if InstanceEvent.IsClauseNamesCount <> 0 then |
| 560 |
begin |
| 561 |
NewIsClauseNames := TCastleStringList.Create; |
| 562 |
try |
| 563 |
for I := 0 to InstanceEvent.IsClauseNamesCount - 1 do |
| 564 |
begin |
| 565 |
IsClauseName := InstanceEvent.IsClauseNames[I]; |
| 566 |
|
| 567 |
{ Event from prototype definition can only correspond to the |
| 568 |
same event type of prototype declaration. It cannot reference |
| 569 |
implicit event (within exposed field) of prototype declaration. |
| 570 |
Which is good, since otherwise it would be difficult to implement |
| 571 |
(Self (TX3DPrototypeNode) is used to keep events, but it doesn't |
| 572 |
keep actual field values (there are kept within actual expanded nodes). |
| 573 |
|
| 574 |
This also means that searching below by Events.IndexOf is Ok, |
| 575 |
no need to use AnyEvent to search. } |
| 576 |
|
| 577 |
OurEventIndex := IndexOfEvent(IsClauseName); |
| 578 |
if OurEventIndex <> -1 then |
| 579 |
begin |
| 580 |
OurEvent := Events[OurEventIndex]; |
| 581 |
FieldOrEventHandleIsClause(InstanceEvent, OurEvent, NewIsClauseNames); |
| 582 |
end else |
| 583 |
WritelnWarning('VRML/X3D', Format('Within prototype "%s", event "%s" references (by "IS" clause) non-existing event "%s"', |
| 584 |
[Prototype.X3DName, InstanceEvent.X3DName, IsClauseName])); |
| 585 |
end; |
| 586 |
|
| 587 |
InstanceEvent.IsClauseNamesAssign(NewIsClauseNames); |
| 588 |
finally FreeAndNil(NewIsClauseNames) end; |
| 589 |
end; |
| 590 |
end; |
| 591 |
|
| 592 |
var |
| 593 |
I: Integer; |
| 594 |
begin |
| 595 |
Result := nil; |
| 596 |
|
| 597 |
{ The NeedsInstantiateIsClause flag is needed for InstantiateIsClauses. |
| 598 |
By checking this flag (and setting back to @false after handling): |
| 599 |
|
| 600 |
1. We ensure that every node is expanded only once (otherwise, |
| 601 |
in case of funny DEF/USE usage, we could process the same node twice |
| 602 |
by InstantiateIsClauses). |
| 603 |
|
| 604 |
2. We avoid instantiating "IS" clauses in nodes outside the NodeCopy. |
| 605 |
Such nodes with "IS" clauses may be added by InstantiateIsClauses, |
| 606 |
when our own PROTO has fields with SFNode / MFNode type, |
| 607 |
and they contain "IS" clauses that should be handled by other |
| 608 |
(outer) proto. See e.g. demo_models/vrml_2/proto_nested_expand.wrl |
| 609 |
testcase. |
| 610 |
|
| 611 |
(The 2nd point could also be fixed by simply moving recursive call to |
| 612 |
Child.DirectEnumerateAll(@InstantiateIsClauses) to the beginning, |
| 613 |
not the end, of InstantiateIsClauses. But this would leave 1st point |
| 614 |
unfixed.) } |
| 615 |
|
| 616 |
if not Child.NeedsInstantiateIsClause then Exit; |
| 617 |
Child.NeedsInstantiateIsClause := false; |
| 618 |
|
| 619 |
if RebaseRelativeUrlsInPrototypes then |
| 620 |
Child.BaseUrl := BaseUrl; |
| 621 |
|
| 622 |
for I := 0 to Child.FieldsCount - 1 do |
| 623 |
ExpandField(Child.Fields[I]); |
| 624 |
|
| 625 |
for I := 0 to Child.EventsCount - 1 do |
| 626 |
ExpandEvent(Child.Events[I]); |
| 627 |
|
| 628 |
Child.DirectEnumerateAll({$ifdef FPC}@{$endif} Self.InstantiateIsClauses); |
| 629 |
end; |
| 630 |
|
| 631 |
function TX3DPrototypeNode.PrepareInstantiateIsClause( |
| 632 |
Node, Child: TX3DNode): Pointer; |
| 633 |
begin |
| 634 |
Result := nil; |
| 635 |
if not (Child.NeedsInstantiateIsClause or (Child is TInlineNode)) then |
| 636 |
begin |
| 637 |
Child.NeedsInstantiateIsClause := true; |
| 638 |
Child.DirectEnumerateAll({$ifdef FPC}@{$endif} Self.PrepareInstantiateIsClause); |
| 639 |
end; |
| 640 |
end; |
| 641 |
|
| 642 |
function TX3DPrototypeNode.Instantiate: TX3DNode; |
| 643 |
|
| 644 |
procedure InstantiateNonExternalPrototype(Proto: TX3DPrototype); |
| 645 |
var |
| 646 |
NodeCopy, NewPrototypeInstanceHelpers: TX3DRootNode; |
| 647 |
begin |
| 648 |
{ We want to copy the whole Proto.Node, instead of copying separately |
| 649 |
Proto.Node.FdChildren[0], Proto.Node.FdChildren[1] etc. |
| 650 |
This way, DEF / USE links, routes links (internal, for nested |
| 651 |
protos "IS" clauses, and non-internal) are preserved as they should. } |
| 652 |
|
| 653 |
NodeCopy := Proto.Node.DeepCopy as TX3DRootNode; |
| 654 |
|
| 655 |
Assert(NodeCopy.PrototypeInstance = |
| 656 |
(NodeCopy.PrototypeInstanceSourceNode <> nil)); |
| 657 |
Assert(NodeCopy.PrototypeInstance or |
| 658 |
(NodeCopy.PrototypeInstanceHelpers = nil)); |
| 659 |
|
| 660 |
try |
| 661 |
{ First, set NeedsInstantiateIsClause := true everywhere |
| 662 |
inside NodeCopy. (We can assume that NeedsInstantiateIsClause |
| 663 |
was @false everywhere before this.) } |
| 664 |
PrepareInstantiateIsClause(nil, NodeCopy); |
| 665 |
|
| 666 |
InstantiateIsClauses(nil, NodeCopy); |
| 667 |
except |
| 668 |
FreeAndNil(NodeCopy); |
| 669 |
raise; |
| 670 |
end; |
| 671 |
|
| 672 |
if NodeCopy.FdChildren.Count = 0 then |
| 673 |
begin |
| 674 |
{ If exception occurs before NodeCopy is connected to Result, |
| 675 |
NodeCopy should be simply freed. } |
| 676 |
FreeAndNil(NodeCopy); |
| 677 |
raise EX3DPrototypeInstantiateError.CreateFmt( |
| 678 |
'Prototype "%s" has no nodes, cannot instantiate', |
| 679 |
[Proto.X3DName]); |
| 680 |
end; |
| 681 |
|
| 682 |
{ ExtractChild/Item methods were really invented specially for this case. |
| 683 |
|
| 684 |
We have to remove Result from NodeCopy, to avoid cycles |
| 685 |
(that can cause mem leaks) because Result.FPrototypeInstanceHelpers |
| 686 |
has to keep pointer to NodeCopy. |
| 687 |
|
| 688 |
At the same time, Result must not be freed here because of ref count = 0... } |
| 689 |
Result := NodeCopy.FdChildren.Extract(0); |
| 690 |
|
| 691 |
Assert(Result.PrototypeInstance = |
| 692 |
(Result.PrototypeInstanceSourceNode <> nil)); |
| 693 |
Assert(Result.PrototypeInstance or |
| 694 |
(Result.PrototypeInstanceHelpers = nil)); |
| 695 |
|
| 696 |
{ NewPrototypeInstanceHelpers is used to keep the rest of |
| 697 |
NodeCopy.FdChildren[1...] that should accompany this node. } |
| 698 |
NewPrototypeInstanceHelpers := NodeCopy; |
| 699 |
|
| 700 |
Result.X3DName := X3DName; |
| 701 |
|
| 702 |
(* Result and NodeCopy may come from another prototype. |
| 703 |
For example, |
| 704 |
|
| 705 |
PROTO SimpleText [ |
| 706 |
inputOutput MFString string "" |
| 707 |
] { Shape { geometry Text { string IS string } } } |
| 708 |
|
| 709 |
PROTO PressedText [ |
| 710 |
inputOutput MFString string "" |
| 711 |
] { SimpleText { string IS string } } |
| 712 |
|
| 713 |
PressedText { string "zero" } |
| 714 |
|
| 715 |
(this is a simplified part of ../../../demo_models/x3d/key_sensor.x3dv |
| 716 |
file). In such case, when PressedText, you may get Shape that already |
| 717 |
was expanded from SimpleText. So NodeCopy will have PrototypeInstance |
| 718 |
= true. Or NodeCopy may be TX3DRootNode wrapper, then it's |
| 719 |
first item (that is assigned to Result) will |
| 720 |
have PrototypeInstance = true. |
| 721 |
|
| 722 |
We have assertions above to check that their PrototypeInstance* |
| 723 |
properties are in valid state. |
| 724 |
|
| 725 |
Note that we take proper actions to not leak memory (so do |
| 726 |
not blindly overwrite Result.PrototypeInstanceSourceNode and ...Helpers). |
| 727 |
Also, existing Result.PrototypeInstanceSourceNode and ...Helpers |
| 728 |
must be retained, as they may contain routes for proper functioning |
| 729 |
of this nested prototype. |
| 730 |
|
| 731 |
What can we do? We have to keep them somewhere, but move out |
| 732 |
of the way. Current approach is to overuse |
| 733 |
TX3DPrototypeNode.PrototypeInstanceSourceNode and ...Helpers |
| 734 |
fiels for this. (They should be empty right now, as one |
| 735 |
TX3DPrototypeNode is expanded only once.) |
| 736 |
We store there information about nested prototype. |
| 737 |
*) |
| 738 |
if Result.PrototypeInstance then |
| 739 |
begin |
| 740 |
Assert(not PrototypeInstance); |
| 741 |
Assert(PrototypeInstanceSourceNode = nil); |
| 742 |
Assert(PrototypeInstanceHelpers = nil); |
| 743 |
|
| 744 |
FPrototypeInstance := Result.PrototypeInstance; |
| 745 |
FPrototypeInstanceSourceNode := Result.PrototypeInstanceSourceNode; |
| 746 |
FPrototypeInstanceHelpers := Result.PrototypeInstanceHelpers; |
| 747 |
end; |
| 748 |
|
| 749 |
{ Note: set PrototypeInstance to @true *after* InstantiateIsClauses, |
| 750 |
otherwise InstantiateIsClauses would not enter this node. |
| 751 |
TODO: bad comment above? } |
| 752 |
Result.FPrototypeInstance := true; |
| 753 |
Result.FPrototypeInstanceSourceNode := Self; |
| 754 |
Result.FPrototypeInstanceHelpers := NewPrototypeInstanceHelpers; |
| 755 |
end; |
| 756 |
|
| 757 |
procedure InstantiateExternalPrototype(Proto: TX3DExternalPrototype); |
| 758 |
begin |
| 759 |
if Proto.ReferencedPrototype = nil then |
| 760 |
raise EX3DPrototypeInstantiateError.CreateFmt( |
| 761 |
'External prototype "%s" cannot be loaded, so cannot instantiate nodes using it', |
| 762 |
[Proto.X3DName]); |
| 763 |
|
| 764 |
{ Note that we do not check whether ReferencedPrototype actually |
| 765 |
has the same fields/events as declared for externproto. |
| 766 |
Although when expanding IS clauses, missing declarations |
| 767 |
or incorrect types or field/event will be caught, so the necessary |
| 768 |
things will be checked when expanding. } |
| 769 |
|
| 770 |
InstantiateNonExternalPrototype(Proto.ReferencedPrototype); |
| 771 |
end; |
| 772 |
|
| 773 |
begin |
| 774 |
if Prototype is TX3DPrototype then |
| 775 |
InstantiateNonExternalPrototype(Prototype as TX3DPrototype) else |
| 776 |
if Prototype is TX3DExternalPrototype then |
| 777 |
InstantiateExternalPrototype(Prototype as TX3DExternalPrototype) else |
| 778 |
raise EX3DPrototypeInstantiateError.CreateFmt( |
| 779 |
'Cannot instantiate prototype "%s": '+ |
| 780 |
'unknown prototype class %s', [Prototype.X3DName, Prototype.ClassName]); |
| 781 |
end; |
| 782 |
|
| 783 |
{ TX3DPrototypeBase --------------------------------------------------------- } |
| 784 |
|
| 785 |
constructor TX3DPrototypeBase.Create; |
| 786 |
begin |
| 787 |
inherited; |
| 788 |
FInterfaceDeclarations := TX3DInterfaceDeclarationList.Create(true); |
| 789 |
end; |
| 790 |
|
| 791 |
destructor TX3DPrototypeBase.Destroy; |
| 792 |
begin |
| 793 |
FreeAndNil(FInterfaceDeclarations); |
| 794 |
inherited; |
| 795 |
end; |
| 796 |
|
| 797 |
procedure TX3DPrototypeBase.ParseInterfaceDeclarations(ExternalProto: boolean; |
| 798 |
Lexer: TX3DLexer; Reader: TX3DReaderNames); |
| 799 |
var |
| 800 |
I: TX3DInterfaceDeclaration; |
| 801 |
begin |
| 802 |
while Lexer.Token <> vtCloseSqBracket do |
| 803 |
begin |
| 804 |
I := TX3DInterfaceDeclaration.Create(nil); |
| 805 |
InterfaceDeclarations.Add(I); |
| 806 |
|
| 807 |
if Lexer.TokenIsKeyword(InterfaceDeclarationKeywords(AllAccessTypes)) then |
| 808 |
begin |
| 809 |
I.Parse(Lexer, Reader, not ExternalProto, false); |
| 810 |
end else |
| 811 |
raise EX3DParserError.Create( |
| 812 |
Lexer, Format(SExpectedInterfaceDeclaration, [Lexer.DescribeToken])); |
| 813 |
end; |
| 814 |
|
| 815 |
{ eat "]" token } |
| 816 |
Lexer.NextToken; |
| 817 |
|
| 818 |
FBaseUrl := Reader.BaseUrl; |
| 819 |
end; |
| 820 |
|
| 821 |
procedure TX3DPrototypeBase.ParseInterfaceDeclarationsXML(ExternalProto: boolean; |
| 822 |
Element: TDOMElement; Reader: TX3DReaderNames); |
| 823 |
var |
| 824 |
I: TX3DInterfaceDeclaration; |
| 825 |
Iter: TXMLElementIterator; |
| 826 |
begin |
| 827 |
Iter := Element.ChildrenIterator; |
| 828 |
try |
| 829 |
while Iter.GetNext do |
| 830 |
begin |
| 831 |
if Iter.Current.TagName = 'field' then |
| 832 |
begin |
| 833 |
I := TX3DInterfaceDeclaration.Create(nil); |
| 834 |
InterfaceDeclarations.Add(I); |
| 835 |
I.ParseXML(Iter.Current, Reader, not ExternalProto); |
| 836 |
end else |
| 837 |
WritelnWarning('VRML/X3D', 'X3D XML: only <field> elements expected in prototype interface'); |
| 838 |
end; |
| 839 |
finally FreeAndNil(Iter) end; |
| 840 |
end; |
| 841 |
|
| 842 |
procedure TX3DPrototypeBase.SaveInterfaceDeclarationsToStream( |
| 843 |
Writer: TX3DWriter; ExternalProto: boolean); |
| 844 |
var |
| 845 |
I: Integer; |
| 846 |
begin |
| 847 |
if Writer.Encoding = xeClassic then Writer.Writeln('['); |
| 848 |
Writer.IncIndent; |
| 849 |
for I := 0 to InterfaceDeclarations.Count - 1 do |
| 850 |
InterfaceDeclarations.Items[I].IDeclSaveToStream(Writer, not ExternalProto); |
| 851 |
Writer.DecIndent; |
| 852 |
if Writer.Encoding = xeClassic then Writer.WritelnIndent(']'); |
| 853 |
end; |
| 854 |
|
| 855 |
{ TX3DPrototype ------------------------------------------------------------- } |
| 856 |
|
| 857 |
destructor TX3DPrototype.Destroy; |
| 858 |
begin |
| 859 |
FreeAndNil(FNode); |
| 860 |
inherited; |
| 861 |
end; |
| 862 |
|
| 863 |
procedure TX3DPrototype.Parse(Lexer: TX3DLexer; Reader: TX3DReaderNames); |
| 864 |
var |
| 865 |
OldReader: TX3DReaderNames; |
| 866 |
begin |
| 867 |
Lexer.NextToken; |
| 868 |
Lexer.CheckTokenIs(vtName); |
| 869 |
FX3DName := Lexer.TokenName; |
| 870 |
|
| 871 |
Lexer.NextToken; |
| 872 |
Lexer.CheckTokenIs(vtOpenSqBracket); |
| 873 |
|
| 874 |
Lexer.NextToken; |
| 875 |
ParseInterfaceDeclarations(false, Lexer, Reader); |
| 876 |
|
| 877 |
Lexer.CheckTokenIs(vtOpenCurlyBracket); |
| 878 |
|
| 879 |
Lexer.NextToken; |
| 880 |
FreeAndNil(FNode); |
| 881 |
|
| 882 |
{ VRML 2.0 spec explicitly says that inside prototype has it's own DEF/USE |
| 883 |
scope, completely independent from the outside. |
| 884 |
|
| 885 |
Also prototype name scope is local within the prototype, |
| 886 |
however it starts from current prototype name scope (not empty, |
| 887 |
like in case of Reader.Nodes). So prototypes defined outside |
| 888 |
are available inside, but nested prototypes inside are not |
| 889 |
available outside. } |
| 890 |
OldReader := Reader; |
| 891 |
Reader := TX3DReaderNames.CreateCopy(true, OldReader); |
| 892 |
try |
| 893 |
Reader.Prototypes.Assign(OldReader.Prototypes); |
| 894 |
FNode := ParseStatements(Lexer, Reader, vtCloseCurlyBracket, false); |
| 895 |
finally |
| 896 |
FreeAndNil(Reader); |
| 897 |
Reader := OldReader; |
| 898 |
end; |
| 899 |
|
| 900 |
{ consume last vtCloseCurlyBracket, ParseStatements doesn't do it } |
| 901 |
Lexer.NextToken; |
| 902 |
|
| 903 |
Reader.Prototypes.Bind(Self); |
| 904 |
end; |
| 905 |
|
| 906 |
procedure TX3DPrototype.ParseXML(Element: TDOMElement; Reader: TX3DReaderNames); |
| 907 |
var |
| 908 |
OldReader: TX3DReaderNames; |
| 909 |
NewName: String; |
| 910 |
E: TDOMElement; |
| 911 |
begin |
| 912 |
BaseUrl := Reader.BaseUrl; |
| 913 |
|
| 914 |
if Element.AttributeString('name', NewName) then |
| 915 |
X3DName := NewName |
| 916 |
else |
| 917 |
raise EX3DXmlError.Create('Missing "name" for <ProtoDeclare> element'); |
| 918 |
|
| 919 |
E := Element.ChildElement('ProtoInterface', false); |
| 920 |
if E <> nil then |
| 921 |
ParseInterfaceDeclarationsXML(false, E, Reader); |
| 922 |
|
| 923 |
E := Element.ChildElement('ProtoBody', false); |
| 924 |
if E = nil then |
| 925 |
raise EX3DXmlError.CreateFmt('Missing <ProtoBody> inside <ProtoDeclare> element of prototype "%s"', [X3DName]); |
| 926 |
|
| 927 |
FreeAndNil(FNode); |
| 928 |
|
| 929 |
{ VRML 2.0 spec explicitly says that inside prototype has it's own DEF/USE |
| 930 |
scope, completely independent from the outside. |
| 931 |
|
| 932 |
Also prototype name scope is local within the prototype, |
| 933 |
however it starts from current prototype name scope (not empty, |
| 934 |
like in case of Reader.Nodes). So prototypes defined outside |
| 935 |
are available inside, but nested prototypes inside are not |
| 936 |
available outside. } |
| 937 |
OldReader := Reader; |
| 938 |
Reader := TX3DReaderNames.CreateCopy(true, OldReader); |
| 939 |
try |
| 940 |
Reader.Prototypes.Assign(OldReader.Prototypes); |
| 941 |
FNode := ParseStatements(E, false, nil, Reader); |
| 942 |
finally |
| 943 |
FreeAndNil(Reader); |
| 944 |
Reader := OldReader; |
| 945 |
end; |
| 946 |
|
| 947 |
Reader.Prototypes.Bind(Self); |
| 948 |
end; |
| 949 |
|
| 950 |
procedure TX3DPrototype.SaveToStream(Writer: TX3DWriter); |
| 951 |
var |
| 952 |
OldNodeNames: TX3DNodeNames; |
| 953 |
WriterNames: TX3DWriterNames; |
| 954 |
begin |
| 955 |
// TODO: We should save/load prototype names using EncodeX3DName/DecodeX3DName too |
| 956 |
case Writer.Encoding of |
| 957 |
xeClassic: Writer.WriteIndent('PROTO ' + X3DName + ' '); |
| 958 |
xeXML : Writer.WritelnIndent('<ProtoDeclare name=' + StringToX3DXml(X3DName) + '>'); |
| 959 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 960 |
else raise EInternalError.Create('TX3DPrototype.SaveToStream Encoding?'); |
| 961 |
{$endif} |
| 962 |
end; |
| 963 |
|
| 964 |
if Writer.Encoding = xeXML then |
| 965 |
begin |
| 966 |
Writer.IncIndent; |
| 967 |
Writer.WritelnIndent('<ProtoInterface>'); |
| 968 |
Writer.IncIndent; |
| 969 |
end; |
| 970 |
|
| 971 |
SaveInterfaceDeclarationsToStream(Writer, false); |
| 972 |
|
| 973 |
if Writer.Encoding = xeXML then |
| 974 |
begin |
| 975 |
Writer.DecIndent; |
| 976 |
Writer.WritelnIndent('</ProtoInterface>'); |
| 977 |
end; |
| 978 |
|
| 979 |
WriterNames := Writer as TX3DWriterNames; |
| 980 |
|
| 981 |
{ Inside prototype has it's own DEF/USE scope. } |
| 982 |
OldNodeNames := WriterNames.NodeNames; |
| 983 |
WriterNames.NodeNames := TX3DNodeNames.Create(false); |
| 984 |
try |
| 985 |
case Writer.Encoding of |
| 986 |
xeClassic: Writer.WritelnIndent('{'); |
| 987 |
xeXML : Writer.WritelnIndent('<ProtoBody>'); |
| 988 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 989 |
else raise EInternalError.Create('TX3DPrototype.SaveToStream 2 Encoding?'); |
| 990 |
{$endif} |
| 991 |
end; |
| 992 |
{ Node may be TX3DRootNode here, that's OK, |
| 993 |
TX3DRootNode.SaveToStream will magically handle this right. } |
| 994 |
Writer.IncIndent; |
| 995 |
Node.SaveToStream(Writer); |
| 996 |
Writer.DecIndent; |
| 997 |
case Writer.Encoding of |
| 998 |
xeClassic: Writer.WritelnIndent('}'); |
| 999 |
xeXML : Writer.WritelnIndent('</ProtoBody>'); |
| 1000 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 1001 |
else raise EInternalError.Create('TX3DPrototype.SaveToStream 3 Encoding?'); |
| 1002 |
{$endif} |
| 1003 |
end; |
| 1004 |
finally |
| 1005 |
FreeAndNil(WriterNames.NodeNames); |
| 1006 |
WriterNames.NodeNames := OldNodeNames; |
| 1007 |
end; |
| 1008 |
|
| 1009 |
if Writer.Encoding = xeXML then |
| 1010 |
begin |
| 1011 |
Writer.DecIndent; |
| 1012 |
Writer.WritelnIndent('</ProtoDeclare>'); |
| 1013 |
end; |
| 1014 |
end; |
| 1015 |
|
| 1016 |
{ TX3DExternalPrototype ----------------------------------------------------- } |
| 1017 |
|
| 1018 |
constructor TX3DExternalPrototype.Create; |
| 1019 |
begin |
| 1020 |
inherited; |
| 1021 |
FUrlList := TMFString.Create(nil, false, '', []); |
| 1022 |
end; |
| 1023 |
|
| 1024 |
destructor TX3DExternalPrototype.Destroy; |
| 1025 |
begin |
| 1026 |
UnloadReferenced; |
| 1027 |
FreeAndNil(FUrlList); |
| 1028 |
inherited; |
| 1029 |
end; |
| 1030 |
|
| 1031 |
procedure TX3DExternalPrototype.Parse(Lexer: TX3DLexer; Reader: TX3DReaderNames); |
| 1032 |
begin |
| 1033 |
Lexer.NextToken; |
| 1034 |
Lexer.CheckTokenIs(vtName); |
| 1035 |
FX3DName := Lexer.TokenName; |
| 1036 |
|
| 1037 |
Lexer.NextToken; |
| 1038 |
Lexer.CheckTokenIs(vtOpenSqBracket); |
| 1039 |
|
| 1040 |
Lexer.NextToken; |
| 1041 |
ParseInterfaceDeclarations(true, Lexer, Reader); |
| 1042 |
|
| 1043 |
UrlList.Parse(Lexer, Reader, false); |
| 1044 |
|
| 1045 |
Reader.Prototypes.Bind(Self); |
| 1046 |
|
| 1047 |
LoadReferenced(Reader.Version); |
| 1048 |
end; |
| 1049 |
|
| 1050 |
procedure TX3DExternalPrototype.ParseXML(Element: TDOMElement; Reader: TX3DReaderNames); |
| 1051 |
var |
| 1052 |
NewName, UrlListValue: String; |
| 1053 |
begin |
| 1054 |
BaseUrl := Reader.BaseUrl; |
| 1055 |
|
| 1056 |
if Element.AttributeString('name', NewName) then |
| 1057 |
X3DName := NewName |
| 1058 |
else |
| 1059 |
raise EX3DXmlError.Create('Missing "name" for <ExternProtoDeclare> element'); |
| 1060 |
|
| 1061 |
ParseInterfaceDeclarationsXML(true, Element, Reader); |
| 1062 |
|
| 1063 |
if Element.AttributeString('url', UrlListValue) then |
| 1064 |
UrlList.ParseXMLAttribute(UrlListValue, Reader) else |
| 1065 |
raise EX3DXmlError.Create('Missing "url" for <ExternProtoDeclare> element'); |
| 1066 |
|
| 1067 |
Reader.Prototypes.Bind(Self); |
| 1068 |
|
| 1069 |
LoadReferenced(Reader.Version); |
| 1070 |
end; |
| 1071 |
|
| 1072 |
procedure TX3DExternalPrototype.SaveToStream(Writer: TX3DWriter); |
| 1073 |
begin |
| 1074 |
case Writer.Encoding of |
| 1075 |
xeClassic: |
| 1076 |
begin |
| 1077 |
Writer.WriteIndent('EXTERNPROTO ' + X3DName + ' '); |
| 1078 |
|
| 1079 |
SaveInterfaceDeclarationsToStream(Writer, true); |
| 1080 |
|
| 1081 |
{ Writer.NodeNames will be ignored by UrlList |
| 1082 |
(TMFString.SaveToStream), don't worry about it. } |
| 1083 |
UrlList.SaveToStream(Writer); |
| 1084 |
end; |
| 1085 |
xeXML: |
| 1086 |
begin |
| 1087 |
Writer.WriteIndent('<ExternProtoDeclare name=' + StringToX3DXml(X3DName) + ' url='); |
| 1088 |
UrlList.FieldSaveToStream(Writer, true, true); |
| 1089 |
Writer.Writeln('>'); |
| 1090 |
|
| 1091 |
Writer.IncIndent; |
| 1092 |
SaveInterfaceDeclarationsToStream(Writer, true); |
| 1093 |
Writer.DecIndent; |
| 1094 |
|
| 1095 |
Writer.WritelnIndent('</ExternProtoDeclare>'); |
| 1096 |
end; |
| 1097 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 1098 |
else raise EInternalError.Create('TX3DExternalPrototype.SaveToStream Encoding?'); |
| 1099 |
{$endif} |
| 1100 |
end; |
| 1101 |
end; |
| 1102 |
|
| 1103 |
procedure TX3DExternalPrototype.LoadReferenced(const Version: TX3DVersion); |
| 1104 |
|
| 1105 |
procedure LoadInterfaceDeclarationsValues; |
| 1106 |
var |
| 1107 |
IIndex: Integer; |
| 1108 |
I: TX3DInterfaceDeclaration; |
| 1109 |
ReferencedField: TX3DField; |
| 1110 |
begin |
| 1111 |
{ We should load default values for our fields now, |
| 1112 |
since InterfaceDeclaration of external prototype doesn't |
| 1113 |
specify default values. } |
| 1114 |
for IIndex := 0 to InterfaceDeclarations.Count - 1 do |
| 1115 |
begin |
| 1116 |
I := InterfaceDeclarations[IIndex]; |
| 1117 |
if I.Field <> nil then |
| 1118 |
begin |
| 1119 |
ReferencedField := ReferencedPrototype.InterfaceDeclarations. |
| 1120 |
TryFindFieldName(I.Field.X3DName); |
| 1121 |
if ReferencedField <> nil then |
| 1122 |
begin |
| 1123 |
try |
| 1124 |
I.Field.AssignValue(ReferencedField); |
| 1125 |
except |
| 1126 |
on E: EX3DFieldAssign do |
| 1127 |
begin |
| 1128 |
WritelnWarning('VRML/X3D', Format( |
| 1129 |
'Error when linking external prototype "%s" with prototype "%s": ', |
| 1130 |
[X3DName, ReferencedPrototype.X3DName]) + E.Message); |
| 1131 |
end; |
| 1132 |
end; |
| 1133 |
end else |
| 1134 |
WritelnWarning('VRML/X3D', Format('Prototype "%s" referenced by external ' + |
| 1135 |
'prototype "%s" doesn''t have field "%s"', |
| 1136 |
[ReferencedPrototype.X3DName, X3DName, I.Field.X3DName])); |
| 1137 |
end; |
| 1138 |
end; |
| 1139 |
end; |
| 1140 |
|
| 1141 |
function LoadFromExternalVRML(const RelativeUrl: String): boolean; |
| 1142 |
var |
| 1143 |
Url: String; |
| 1144 |
PrototypeNames: TX3DPrototypeNames; |
| 1145 |
|
| 1146 |
procedure ProtoWarning(const S: String); |
| 1147 |
begin |
| 1148 |
WritelnWarning('VRML/X3D', Format('Cannot load external prototype from URL "%s": ', |
| 1149 |
[Url]) + S); |
| 1150 |
end; |
| 1151 |
|
| 1152 |
{ Find PROTO (but not EXTERNPROTO) with matching Name. |
| 1153 |
Name is ignored if ''. |
| 1154 |
@nil if not found. } |
| 1155 |
function TryFindProtoNonExternal(const Name: String): TX3DPrototype; |
| 1156 |
var |
| 1157 |
I: Integer; |
| 1158 |
begin |
| 1159 |
if PrototypeNames <> nil then |
| 1160 |
for I := 0 to PrototypeNames.Count - 1 do |
| 1161 |
if PrototypeNames.Objects[I] is TX3DPrototype then |
| 1162 |
begin |
| 1163 |
Result := TX3DPrototype(PrototypeNames.Objects[I]); |
| 1164 |
if (Name = '') or (Result.X3DName = Name) then |
| 1165 |
Exit; |
| 1166 |
end; |
| 1167 |
Result := nil; |
| 1168 |
end; |
| 1169 |
|
| 1170 |
var |
| 1171 |
Anchor: String; |
| 1172 |
begin |
| 1173 |
Result := false; |
| 1174 |
|
| 1175 |
Url := CombineURI(BaseUrl, RelativeUrl); |
| 1176 |
URIExtractAnchor(Url, Anchor); |
| 1177 |
try |
| 1178 |
ReferencedPrototypeNode := X3DCache.LoadNode(Url); |
| 1179 |
PrototypeNames := ReferencedPrototypeNode.PrototypeNames; |
| 1180 |
except |
| 1181 |
on E: Exception do |
| 1182 |
begin |
| 1183 |
ProtoWarning(E.Message); |
| 1184 |
Exit; |
| 1185 |
end; |
| 1186 |
end; |
| 1187 |
|
| 1188 |
FReferencedPrototype := TryFindProtoNonExternal(Anchor); |
| 1189 |
if FReferencedPrototype = nil then |
| 1190 |
begin |
| 1191 |
X3DCache.FreeNode(ReferencedPrototypeNode); |
| 1192 |
if Anchor = '' then |
| 1193 |
ProtoWarning('No PROTO found') else |
| 1194 |
ProtoWarning(Format('No PROTO named "%s" found', [Anchor])); |
| 1195 |
Exit; |
| 1196 |
end; |
| 1197 |
|
| 1198 |
Result := true; |
| 1199 |
|
| 1200 |
LoadInterfaceDeclarationsValues; |
| 1201 |
end; |
| 1202 |
|
| 1203 |
function LoadFromURN(const URN: String): boolean; |
| 1204 |
begin |
| 1205 |
FReferencedClass := NodesManager.URNToClass(URN, Version); |
| 1206 |
Result := ReferencedClass <> nil; |
| 1207 |
if not Result then |
| 1208 |
WritelnWarning('VRML/X3D', Format('Unknown node URN "%s"', [URN])); |
| 1209 |
end; |
| 1210 |
|
| 1211 |
var |
| 1212 |
I: Integer; |
| 1213 |
S: String; |
| 1214 |
ProtoLoaded: boolean; |
| 1215 |
begin |
| 1216 |
UnloadReferenced; |
| 1217 |
|
| 1218 |
for I := 0 to UrlList.Count - 1 do |
| 1219 |
begin |
| 1220 |
S := UrlList.Items[I]; |
| 1221 |
if IsPrefix('urn:', S) then |
| 1222 |
ProtoLoaded := LoadFromURN(S) |
| 1223 |
else |
| 1224 |
ProtoLoaded := LoadFromExternalVRML(S); |
| 1225 |
if ProtoLoaded then |
| 1226 |
Break; |
| 1227 |
end; |
| 1228 |
end; |
| 1229 |
|
| 1230 |
procedure TX3DExternalPrototype.UnloadReferenced; |
| 1231 |
begin |
| 1232 |
{ FReferencedPrototype will be freed as part of ReferencedPrototypeNode } |
| 1233 |
FReferencedPrototype := nil; |
| 1234 |
|
| 1235 |
X3DCache.FreeNode(ReferencedPrototypeNode); |
| 1236 |
|
| 1237 |
FReferencedClass := nil; |
| 1238 |
end; |
| 1239 |
|
| 1240 |
{$endif read_implementation} |