| 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 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
{ Route makes a connection between two X3D events, |
| 20 |
making the @italic(destination) event occur when the @italic(source) event happened. } |
| 21 |
TX3DRoute = class(TX3DFileItem) |
| 22 |
strict private |
| 23 |
FSourceNode: TX3DNode; |
| 24 |
FSourceEvent: TX3DEvent; |
| 25 |
|
| 26 |
FDestinationNode: TX3DNode; |
| 27 |
FDestinationEvent: TX3DEvent; |
| 28 |
|
| 29 |
LastEventTime: TX3DTime; |
| 30 |
FInternal: boolean; |
| 31 |
|
| 32 |
procedure DestructionNotification(const Node: TX3DNode); |
| 33 |
|
| 34 |
procedure UnsetEnding( |
| 35 |
var Node: TX3DNode; var Event: TX3DEvent; |
| 36 |
const DestEnding: boolean; |
| 37 |
const RemoveFromDestructionNotification: boolean = true); |
| 38 |
|
| 39 |
procedure SetEnding(const NodeName, FieldOrEventName: string; |
| 40 |
const Reader: TX3DReaderNames; |
| 41 |
var Node: TX3DNode; var Event: TX3DEvent; |
| 42 |
const DestEnding: boolean); |
| 43 |
|
| 44 |
{ Set Event, based on FieldOrEvent (may be actual event, |
| 45 |
or exposed field containing it) and DestEnding. |
| 46 |
Assumes that Event is clear on enter (cleared by UnsetEnding). } |
| 47 |
procedure SetEndingInternal( |
| 48 |
const Node: TX3DNode; const FieldOrEvent: TX3DFieldOrEvent; |
| 49 |
var Event: TX3DEvent; |
| 50 |
const DestEnding: boolean); |
| 51 |
|
| 52 |
procedure SetEndingDirectly( |
| 53 |
const NewNode: TX3DNode; const FieldOrEvent: TX3DFieldOrEvent; |
| 54 |
var Node: TX3DNode; var Event: TX3DEvent; |
| 55 |
const DestEnding: boolean); |
| 56 |
|
| 57 |
procedure EventReceive(const Event: TX3DEvent; const Value: TX3DField; |
| 58 |
const Time: TX3DTime); |
| 59 |
public |
| 60 |
constructor Create; |
| 61 |
destructor Destroy; override; |
| 62 |
|
| 63 |
{ Source event properties. Either all three are @nil, or: |
| 64 |
|
| 65 |
@unorderedList( |
| 66 |
@item(SourceEvent is assigned, meaning is self-explanatory. |
| 67 |
|
| 68 |
Note: if you want to get it's exposed field, remember this |
| 69 |
is available in SourceEvent.ParentExposedField.) |
| 70 |
|
| 71 |
@item(SourceNode must also be assigned and |
| 72 |
this must be the node enclosing SourceEvent. That is, the node that |
| 73 |
has SourceEvent as one of explicit (on TX3DNode.Events list) or |
| 74 |
implicit (exposed by some field) event.) |
| 75 |
) |
| 76 |
|
| 77 |
@groupBegin } |
| 78 |
property SourceNode: TX3DNode read FSourceNode; |
| 79 |
property SourceEvent: TX3DEvent read FSourceEvent; |
| 80 |
{ @groupEnd } |
| 81 |
|
| 82 |
{ Destination event properties. |
| 83 |
Analogous to SourceEvent, SourceNode. |
| 84 |
|
| 85 |
@groupBegin } |
| 86 |
property DestinationNode: TX3DNode read FDestinationNode; |
| 87 |
property DestinationEvent: TX3DEvent read FDestinationEvent; |
| 88 |
{ @groupEnd } |
| 89 |
|
| 90 |
{ Set source/destination of the route. |
| 91 |
|
| 92 |
This does everything that VRML parser should |
| 93 |
do when parsed VRML route. It looks for given node name |
| 94 |
(in Names.Nodes, then Names.Imported), |
| 95 |
then it looks for field/event within this node, |
| 96 |
and if everything is successful --- sets route properties. |
| 97 |
|
| 98 |
If something goes wrong, WritelnWarning is generated |
| 99 |
and route ending is left unset. |
| 100 |
|
| 101 |
@groupBegin } |
| 102 |
procedure SetSource( |
| 103 |
const SourceNodeName, SourceFieldOrEventName: string; |
| 104 |
Reader: TX3DReaderNames); |
| 105 |
|
| 106 |
procedure SetDestination( |
| 107 |
const DestinationNodeName, DestinationFieldOrEventName: string; |
| 108 |
Reader: TX3DReaderNames); |
| 109 |
{ @groupEnd } |
| 110 |
|
| 111 |
{ These set source/destination of the route in more direct way. |
| 112 |
|
| 113 |
FieldOrEvent is used to set SourceEvent (or DestinationEvent). |
| 114 |
FieldOrEvent may be the actual event to set, |
| 115 |
or exposed field containing this event. |
| 116 |
|
| 117 |
You specify explictly NewNode, which is not checked in any way. |
| 118 |
We don't check whether it exists, whether it contains given |
| 119 |
FieldOrEvent, etc. --- you have to guarantee this yourself. |
| 120 |
Also, remember that normal SetSource actually look for events |
| 121 |
inside PrototypeInstanceSourceNode, if exists --- if you want this, |
| 122 |
you have to do this yourself when using these SetXxxDirectly. |
| 123 |
It is used to set SourceNode (or DestinationNode). |
| 124 |
Overloaded versions that don't take NewNode parameter just assume |
| 125 |
that NewNode can be taken from FieldOrEvent.ParentNode. |
| 126 |
|
| 127 |
@groupBegin } |
| 128 |
procedure SetSourceDirectly( |
| 129 |
const NewNode: TX3DNode; const FieldOrEvent: TX3DFieldOrEvent); overload; |
| 130 |
|
| 131 |
procedure SetDestinationDirectly( |
| 132 |
const NewNode: TX3DNode; const FieldOrEvent: TX3DFieldOrEvent); overload; |
| 133 |
|
| 134 |
procedure SetSourceDirectly(const FieldOrEvent: TX3DFieldOrEvent); overload; |
| 135 |
procedure SetDestinationDirectly(const FieldOrEvent: TX3DFieldOrEvent); overload; |
| 136 |
{ @groupEnd } |
| 137 |
|
| 138 |
{ Parse the route (classic VRML encoding). |
| 139 |
Implementation should be able to safely assume that current token |
| 140 |
is ROUTE. } |
| 141 |
procedure Parse(Lexer: TX3DLexer; Reader: TX3DReaderNames); |
| 142 |
|
| 143 |
{ Parse the route (XML encoding). |
| 144 |
Given Element here must have TagName = 'ROUTE'. } |
| 145 |
procedure ParseXML(Element: TDOMElement; Reader: TX3DReaderNames); |
| 146 |
|
| 147 |
{ Save a ROUTE to VRML file. |
| 148 |
|
| 149 |
Will generate WritelnWarning when route cannot be saved. |
| 150 |
This can happen when SourceNode or SourceEvent |
| 151 |
or DestinationNode or DestinationEvent are @nil. |
| 152 |
Also, if SourceNode and DestinationNode are without a name, |
| 153 |
or the name is not currently bound in Writer.NodeNames. |
| 154 |
} |
| 155 |
procedure SaveToStream(Writer: TX3DWriter); override; |
| 156 |
|
| 157 |
{ Clear the memory when the last event passed through this route. |
| 158 |
Route must remember such thing, to avoid loops in routes. |
| 159 |
This is following VRML 2.0 / X3D specifications, that explicitly |
| 160 |
say that only one event per ROUTE per timestamp is allowed. |
| 161 |
|
| 162 |
Use ResetLastEventTime when you really want to reset this memory. |
| 163 |
In practice, this should be used only by TCastleSceneCore.ResetTime |
| 164 |
implementation. } |
| 165 |
procedure ResetLastEventTime; |
| 166 |
|
| 167 |
{ Internal routes are created by PROTO expansion code, which |
| 168 |
needs to create internal routes to implement "IS" clauses for events. |
| 169 |
|
| 170 |
These routes work exactly like normal routes, except: |
| 171 |
@unorderedList( |
| 172 |
@item(They are not saved to file (SaveToStream will ignore |
| 173 |
internal route).) |
| 174 |
@item(It's allowed (in fact, this will always happen for current |
| 175 |
internal routes) to route one input event to another input event.) |
| 176 |
) |
| 177 |
} |
| 178 |
property Internal: boolean read FInternal write FInternal default false; |
| 179 |
|
| 180 |
function DeepCopy(const CopyState: TX3DNodeDeepCopyState): TX3DRoute; |
| 181 |
end; |
| 182 |
|
| 183 |
TX3DRouteList = class({$ifdef FPC}specialize{$endif} TObjectList<TX3DRoute>); |
| 184 |
|
| 185 |
{$endif read_interface} |
| 186 |
|
| 187 |
{$ifdef read_implementation} |
| 188 |
|
| 189 |
{ TX3DRoute ----------------------------------------------------------------- } |
| 190 |
|
| 191 |
constructor TX3DRoute.Create; |
| 192 |
begin |
| 193 |
inherited; |
| 194 |
ResetLastEventTime; |
| 195 |
end; |
| 196 |
|
| 197 |
destructor TX3DRoute.Destroy; |
| 198 |
begin |
| 199 |
{ We have to unset, to call |
| 200 |
RemoveDestructionNotification(...) on our nodes before |
| 201 |
we get destroyed. Otherwise nodes would have invalid references |
| 202 |
on TX3DNode.FDestructionNotifications list. } |
| 203 |
|
| 204 |
UnsetEnding(FSourceNode , FSourceEvent , false); |
| 205 |
UnsetEnding(FDestinationNode, FDestinationEvent, true); |
| 206 |
inherited; |
| 207 |
end; |
| 208 |
|
| 209 |
procedure TX3DRoute.Parse(Lexer: TX3DLexer; Reader: TX3DReaderNames); |
| 210 |
var |
| 211 |
SourceNodeName, SourceEventName: string; |
| 212 |
DestinationNodeName, DestinationEventName: string; |
| 213 |
begin |
| 214 |
{ We don't use NextTokenForceVTName here, as then the dot "." |
| 215 |
is treated like part the VRML name. So this assumes that VRML node names |
| 216 |
are really correct VRML names. } |
| 217 |
|
| 218 |
Lexer.NextToken; |
| 219 |
Lexer.CheckTokenIs(vtName, 'VRML node name'); |
| 220 |
SourceNodeName := Lexer.TokenX3DName; |
| 221 |
|
| 222 |
Lexer.NextToken; |
| 223 |
Lexer.CheckTokenIs(vtPeriod); |
| 224 |
|
| 225 |
Lexer.NextToken; |
| 226 |
Lexer.CheckTokenIs(vtName, 'VRML field/event name'); |
| 227 |
SourceEventName := Lexer.TokenX3DName; |
| 228 |
|
| 229 |
Lexer.NextToken; |
| 230 |
Lexer.CheckTokenIsKeyword(vkTO); |
| 231 |
|
| 232 |
Lexer.NextToken; |
| 233 |
Lexer.CheckTokenIs(vtName, 'VRML node name'); |
| 234 |
DestinationNodeName := Lexer.TokenX3DName; |
| 235 |
|
| 236 |
Lexer.NextToken; |
| 237 |
Lexer.CheckTokenIs(vtPeriod); |
| 238 |
|
| 239 |
Lexer.NextToken; |
| 240 |
Lexer.CheckTokenIs(vtName, 'VRML field/event name'); |
| 241 |
DestinationEventName := Lexer.TokenX3DName; |
| 242 |
|
| 243 |
Lexer.NextToken; |
| 244 |
|
| 245 |
SetSource (SourceNodeName , SourceEventName , Reader); |
| 246 |
SetDestination(DestinationNodeName, DestinationEventName, Reader); |
| 247 |
end; |
| 248 |
|
| 249 |
procedure TX3DRoute.ParseXML(Element: TDOMElement; Reader: TX3DReaderNames); |
| 250 |
|
| 251 |
function RequiredX3DName(const AttrName: string): string; |
| 252 |
begin |
| 253 |
if not XMLAttributeX3DName(Element, AttrName, Result) then |
| 254 |
begin |
| 255 |
WritelnWarning('X3D', 'Missing ROUTE ' + AttrName + ' attribute'); |
| 256 |
Result := ''; |
| 257 |
end; |
| 258 |
end; |
| 259 |
|
| 260 |
var |
| 261 |
SourceNodeName, SourceEventName: string; |
| 262 |
DestinationNodeName, DestinationEventName: string; |
| 263 |
begin |
| 264 |
SourceNodeName := RequiredX3DName('fromNode'); |
| 265 |
SourceEventName := RequiredX3DName('fromField'); |
| 266 |
DestinationNodeName := RequiredX3DName('toNode'); |
| 267 |
DestinationEventName := RequiredX3DName('toField'); |
| 268 |
|
| 269 |
SetSource (SourceNodeName , SourceEventName , Reader); |
| 270 |
SetDestination(DestinationNodeName, DestinationEventName, Reader); |
| 271 |
end; |
| 272 |
|
| 273 |
procedure TX3DRoute.UnsetEnding( |
| 274 |
var Node: TX3DNode; var Event: TX3DEvent; |
| 275 |
const DestEnding: boolean; |
| 276 |
const RemoveFromDestructionNotification: boolean); |
| 277 |
begin |
| 278 |
if (Event <> nil) and (not DestEnding) then |
| 279 |
Event.RemoveNotification({$ifdef FPC}@{$endif} EventReceive); |
| 280 |
|
| 281 |
if Node <> nil then |
| 282 |
begin |
| 283 |
if RemoveFromDestructionNotification then |
| 284 |
Node.RemoveDestructionNotification({$ifdef FPC}@{$endif} DestructionNotification); |
| 285 |
Node := nil; |
| 286 |
end; |
| 287 |
|
| 288 |
Event := nil; |
| 289 |
end; |
| 290 |
|
| 291 |
procedure TX3DRoute.EventReceive( |
| 292 |
const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime); |
| 293 |
|
| 294 |
{ Like F.X3DName, but tolerates given F being nil, |
| 295 |
which is always possible for ROUTE endpoints. |
| 296 |
Testcase: https://github.com/castle-engine/castle-model-viewer/issues/89 } |
| 297 |
function X3DNameSafe(const F: TX3DFieldOrEvent): String; overload; |
| 298 |
begin |
| 299 |
if F <> nil then |
| 300 |
Result := F.X3DName |
| 301 |
else |
| 302 |
Result := '<none>'; |
| 303 |
end; |
| 304 |
|
| 305 |
function X3DNameSafe(const F: TX3DNode): String; overload; |
| 306 |
begin |
| 307 |
if F <> nil then |
| 308 |
Result := F.X3DName |
| 309 |
else |
| 310 |
Result := '<none>'; |
| 311 |
end; |
| 312 |
|
| 313 |
begin |
| 314 |
Assert(Event = SourceEvent); |
| 315 |
|
| 316 |
{ Follow ROUTE only when LastEventTime is older than current Time. |
| 317 |
This avoids loops in ROUTEs, following VRML / X3D specs. } |
| 318 |
if Time > LastEventTime then |
| 319 |
begin |
| 320 |
LastEventTime := Time; |
| 321 |
if DestinationEvent <> nil then |
| 322 |
DestinationEvent.Send(Value, Time); |
| 323 |
end else |
| 324 |
WritelnLog('X3D', Format( |
| 325 |
'Route %s.%s -> %s.%s ignored another event at <= timestamp (%f.%d, while last event was on %f.%d). Potential routes loop avoided', |
| 326 |
[ X3DNameSafe(SourceNode), |
| 327 |
X3DNameSafe(SourceEvent), |
| 328 |
X3DNameSafe(DestinationNode), |
| 329 |
X3DNameSafe(DestinationEvent), |
| 330 |
Time.Seconds, Time.PlusTicks, |
| 331 |
LastEventTime.Seconds, LastEventTime.PlusTicks ])); |
| 332 |
end; |
| 333 |
|
| 334 |
procedure TX3DRoute.ResetLastEventTime; |
| 335 |
begin |
| 336 |
LastEventTime := TX3DTime.Oldest; |
| 337 |
end; |
| 338 |
|
| 339 |
type |
| 340 |
ERouteSetEndingError = class(EX3DError); |
| 341 |
|
| 342 |
const |
| 343 |
DestEndingNames: array [boolean] of string = |
| 344 |
('source', 'destination'); |
| 345 |
|
| 346 |
procedure TX3DRoute.SetEndingInternal( |
| 347 |
const Node: TX3DNode; |
| 348 |
const FieldOrEvent: TX3DFieldOrEvent; |
| 349 |
var Event: TX3DEvent; |
| 350 |
const DestEnding: boolean); |
| 351 |
var |
| 352 |
ExposedField: TX3DField; |
| 353 |
begin |
| 354 |
if FieldOrEvent is TX3DField then |
| 355 |
begin |
| 356 |
ExposedField := TX3DField(FieldOrEvent); |
| 357 |
if not ExposedField.Exposed then |
| 358 |
raise ERouteSetEndingError.CreateFmt('Route %s specifies field "%s" (for node "%s"), but this is not an exposed field (cannot generate/receive events)', |
| 359 |
[ DestEndingNames[DestEnding], FieldOrEvent.X3DName, Node.X3DName ]); |
| 360 |
Event := ExposedField.ExposedEvents[DestEnding]; |
| 361 |
end else |
| 362 |
begin |
| 363 |
Assert(FieldOrEvent is TX3DEvent); |
| 364 |
Event := TX3DEvent(FieldOrEvent); |
| 365 |
end; |
| 366 |
|
| 367 |
if (not Internal) and (Event.InEvent <> DestEnding) then |
| 368 |
begin |
| 369 |
if DestEnding then |
| 370 |
raise ERouteSetEndingError.CreateFmt('Route uses wrong event: destination of the route (%s, type %s) can only be output event', |
| 371 |
[ Event.X3DName, Event.FieldClass.X3DType ]) else |
| 372 |
raise ERouteSetEndingError.CreateFmt('Route uses wrong event: source of the route (%s, type %s) can only be input event', |
| 373 |
[ Event.X3DName, Event.FieldClass.X3DType ]); |
| 374 |
end; |
| 375 |
|
| 376 |
if (SourceEvent <> nil) and |
| 377 |
(DestinationEvent <> nil) and |
| 378 |
(SourceEvent.FieldClass <> DestinationEvent.FieldClass) and |
| 379 |
{ destination field can be XFAny (for some InstantReality nodes) as an exception. } |
| 380 |
(not (DestinationEvent.FieldClass = TX3DField)) then |
| 381 |
raise ERouteSetEndingError.CreateFmt('Route has different event types for source (%s, type %s) and destination (%s, type %s)', |
| 382 |
[ SourceEvent .X3DName, SourceEvent .FieldClass.X3DType, |
| 383 |
DestinationEvent.X3DName, DestinationEvent.FieldClass.X3DType ]); |
| 384 |
|
| 385 |
if (Event <> nil) and (not DestEnding) then |
| 386 |
Event.AddNotification({$ifdef FPC}@{$endif} EventReceive); |
| 387 |
end; |
| 388 |
|
| 389 |
procedure TX3DRoute.SetEnding(const NodeName, FieldOrEventName: string; |
| 390 |
const Reader: TX3DReaderNames; |
| 391 |
var Node: TX3DNode; var Event: TX3DEvent; |
| 392 |
const DestEnding: boolean); |
| 393 |
var |
| 394 |
N: TX3DNode; |
| 395 |
FieldOrEvent: TX3DFieldOrEvent; |
| 396 |
IgnoreNodeFinished: boolean; |
| 397 |
begin |
| 398 |
UnsetEnding(Node, Event, DestEnding); |
| 399 |
|
| 400 |
try |
| 401 |
N := Reader.Bound(NodeName, IgnoreNodeFinished, false); |
| 402 |
if N = nil then |
| 403 |
raise ERouteSetEndingError.CreateFmt('Route %s node name "%s" not found', |
| 404 |
[ DestEndingNames[DestEnding], NodeName ]); |
| 405 |
|
| 406 |
Node := N; |
| 407 |
if Node.PrototypeInstanceSourceNode <> nil then |
| 408 |
begin |
| 409 |
Node := Node.PrototypeInstanceSourceNode; |
| 410 |
{ Actually, SearchNode.PrototypeInstanceSourceNode may also be non-nil, |
| 411 |
in case we have nested prototype. But it doesn't matter, |
| 412 |
that is we don't want to go all the way down to find the |
| 413 |
final PrototypeInstanceSourceNode --- we only want to see the first |
| 414 |
PrototypeInstanceSourceNode. } |
| 415 |
end; |
| 416 |
|
| 417 |
Node.AddDestructionNotification({$ifdef FPC}@{$endif} DestructionNotification); |
| 418 |
|
| 419 |
FieldOrEvent := Node.FieldOrEvent(FieldOrEventName); |
| 420 |
if FieldOrEvent = nil then |
| 421 |
raise ERouteSetEndingError.CreateFmt('Route %s field/event name "%s" (for node "%s", type "%s") not found', |
| 422 |
[ DestEndingNames[DestEnding], FieldOrEventName, NodeName, Node.X3DType ]); |
| 423 |
|
| 424 |
SetEndingInternal(Node, FieldOrEvent, Event, DestEnding); |
| 425 |
except |
| 426 |
on E: ERouteSetEndingError do |
| 427 |
begin |
| 428 |
UnsetEnding(Node, Event, DestEnding); |
| 429 |
WritelnWarning('X3D', E.Message); |
| 430 |
end; |
| 431 |
end; |
| 432 |
end; |
| 433 |
|
| 434 |
procedure TX3DRoute.SetSource( |
| 435 |
const SourceNodeName, SourceFieldOrEventName: string; |
| 436 |
Reader: TX3DReaderNames); |
| 437 |
begin |
| 438 |
SetEnding(SourceNodeName, SourceFieldOrEventName, |
| 439 |
Reader, FSourceNode, FSourceEvent, false); |
| 440 |
end; |
| 441 |
|
| 442 |
procedure TX3DRoute.SetDestination( |
| 443 |
const DestinationNodeName, DestinationFieldOrEventName: string; |
| 444 |
Reader: TX3DReaderNames); |
| 445 |
begin |
| 446 |
SetEnding(DestinationNodeName, DestinationFieldOrEventName, |
| 447 |
Reader, FDestinationNode, FDestinationEvent, true); |
| 448 |
end; |
| 449 |
|
| 450 |
procedure TX3DRoute.SetEndingDirectly( |
| 451 |
const NewNode: TX3DNode; const FieldOrEvent: TX3DFieldOrEvent; |
| 452 |
var Node: TX3DNode; var Event: TX3DEvent; |
| 453 |
const DestEnding: boolean); |
| 454 |
begin |
| 455 |
UnsetEnding(Node, Event, DestEnding); |
| 456 |
|
| 457 |
try |
| 458 |
Node := NewNode; |
| 459 |
Node.AddDestructionNotification({$ifdef FPC}@{$endif} DestructionNotification); |
| 460 |
|
| 461 |
SetEndingInternal(Node, FieldOrEvent, Event, DestEnding); |
| 462 |
except |
| 463 |
on E: ERouteSetEndingError do |
| 464 |
begin |
| 465 |
UnsetEnding(Node, Event, DestEnding); |
| 466 |
WritelnWarning('X3D', E.Message); |
| 467 |
end; |
| 468 |
end; |
| 469 |
end; |
| 470 |
|
| 471 |
procedure TX3DRoute.SetSourceDirectly( |
| 472 |
const NewNode: TX3DNode; const FieldOrEvent: TX3DFieldOrEvent); |
| 473 |
begin |
| 474 |
SetEndingDirectly(NewNode, FieldOrEvent, |
| 475 |
FSourceNode, FSourceEvent, |
| 476 |
false); |
| 477 |
end; |
| 478 |
|
| 479 |
procedure TX3DRoute.SetDestinationDirectly( |
| 480 |
const NewNode: TX3DNode; const FieldOrEvent: TX3DFieldOrEvent); |
| 481 |
begin |
| 482 |
SetEndingDirectly(NewNode, FieldOrEvent, |
| 483 |
FDestinationNode, FDestinationEvent, |
| 484 |
true); |
| 485 |
end; |
| 486 |
|
| 487 |
procedure TX3DRoute.SetSourceDirectly(const FieldOrEvent: TX3DFieldOrEvent); |
| 488 |
begin |
| 489 |
SetSourceDirectly(FieldOrEvent.ParentNode as TX3DNode, FieldOrEvent); |
| 490 |
end; |
| 491 |
|
| 492 |
procedure TX3DRoute.SetDestinationDirectly( |
| 493 |
const FieldOrEvent: TX3DFieldOrEvent); |
| 494 |
begin |
| 495 |
SetDestinationDirectly(FieldOrEvent.ParentNode as TX3DNode, FieldOrEvent); |
| 496 |
end; |
| 497 |
|
| 498 |
type |
| 499 |
EX3DRouteSaveError = class(EX3DError); |
| 500 |
|
| 501 |
procedure TX3DRoute.SaveToStream(Writer: TX3DWriter); |
| 502 |
|
| 503 |
procedure Ending(Node: TX3DNode; Event: TX3DEvent; const S: string; |
| 504 |
out NodeName, EventName: string); |
| 505 |
var |
| 506 |
BoundNode: TX3DNode; |
| 507 |
IgnoreNodeFinished: boolean; |
| 508 |
begin |
| 509 |
{ Check Node } |
| 510 |
if Node = nil then |
| 511 |
raise EX3DRouteSaveError.CreateFmt('Cannot save VRML route: %s node not assigned (look for warnings when reading this VRML file)', [S]); |
| 512 |
if Node.X3DName = '' then |
| 513 |
raise EX3DRouteSaveError.CreateFmt('Cannot save VRML route: %s node not named', [S]); |
| 514 |
|
| 515 |
BoundNode := (Writer as TX3DWriterNames).NodeNames.Bound(Node.X3DName, IgnoreNodeFinished); |
| 516 |
if BoundNode = nil then |
| 517 |
raise EX3DRouteSaveError.CreateFmt('Cannot save VRML route: %s node name "%s" not bound', |
| 518 |
[S, Node.X3DName]); |
| 519 |
|
| 520 |
{ Just like when setting node by TX3DRoute.SetEnding: |
| 521 |
we actually keep the Node that contains the route, which is |
| 522 |
sometimes TX3DPrototypeNode hidden inside PrototypeInstanceSourceNode. } |
| 523 |
if BoundNode.PrototypeInstanceSourceNode <> nil then |
| 524 |
BoundNode := BoundNode.PrototypeInstanceSourceNode; |
| 525 |
if BoundNode <> Node then |
| 526 |
raise EX3DRouteSaveError.CreateFmt('Cannot save VRML route: %s node name "%s" not bound (another node bound to the same name)', |
| 527 |
[S, Node.X3DName]); |
| 528 |
|
| 529 |
NodeName := Node.X3DName; |
| 530 |
|
| 531 |
{ Check Event } |
| 532 |
if Event = nil then |
| 533 |
raise EX3DRouteSaveError.CreateFmt('Cannot save VRML route: %s event not assigned', [S]); |
| 534 |
|
| 535 |
{ Check we have a name. } |
| 536 |
if Event.X3DName = '' then |
| 537 |
raise EX3DRouteSaveError.CreateFmt('Cannot save VRML route: %s event not named', [S]); |
| 538 |
EventName := Event.X3DName; |
| 539 |
end; |
| 540 |
|
| 541 |
var |
| 542 |
SourceNodeName, SourceEventName, DestinationNodeName, DestinationEventName: string; |
| 543 |
begin |
| 544 |
if Internal then Exit; |
| 545 |
|
| 546 |
try |
| 547 |
Ending(SourceNode , SourceEvent , 'source' , SourceNodeName , SourceEventName ); |
| 548 |
Ending(DestinationNode, DestinationEvent, 'destination', DestinationNodeName, DestinationEventName); |
| 549 |
|
| 550 |
case Writer.Encoding of |
| 551 |
xeClassic: |
| 552 |
Writer.WritelnIndent(Format('ROUTE %s.%s TO %s.%s', [ |
| 553 |
EncodeX3DName(SourceNodeName), |
| 554 |
EncodeX3DName(SourceEventName), |
| 555 |
EncodeX3DName(DestinationNodeName), |
| 556 |
EncodeX3DName(DestinationEventName) |
| 557 |
])); |
| 558 |
xeXML: |
| 559 |
Writer.WritelnIndent(Format('<ROUTE fromNode=%s fromField=%s toNode=%s toField=%s />', [ |
| 560 |
StringToX3DXml(EncodeX3DName(SourceNodeName)), |
| 561 |
StringToX3DXml(EncodeX3DName(SourceEventName)), |
| 562 |
StringToX3DXml(EncodeX3DName(DestinationNodeName)), |
| 563 |
StringToX3DXml(EncodeX3DName(DestinationEventName)) |
| 564 |
])); |
| 565 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 566 |
else raise EInternalError.Create('TX3DRoute.SaveToStream Encoding?'); |
| 567 |
{$endif} |
| 568 |
end; |
| 569 |
except |
| 570 |
on E: EX3DRouteSaveError do |
| 571 |
WritelnWarning('X3D', E.Message); |
| 572 |
end; |
| 573 |
end; |
| 574 |
|
| 575 |
procedure TX3DRoute.DestructionNotification(const Node: TX3DNode); |
| 576 |
begin |
| 577 |
{ UnsetEnding is called with RemoveFromDestructionNotification = false. |
| 578 |
Reason: |
| 579 |
1. Removing from DestructionNotification is not needed, |
| 580 |
since the Node is destroyed anyway, along with it's |
| 581 |
DestructionNotification list. |
| 582 |
2. Moreover, right now we probably iterate over DestructionNotification, |
| 583 |
so removing items from it is very bad idea (indexes shift, |
| 584 |
pointers change if reallocation occurs). } |
| 585 |
|
| 586 |
if Node = FSourceNode then |
| 587 |
UnsetEnding(FSourceNode , FSourceEvent , false, false); |
| 588 |
|
| 589 |
if Node = FDestinationNode then |
| 590 |
UnsetEnding(FDestinationNode, FDestinationEvent, true , false); |
| 591 |
end; |
| 592 |
|
| 593 |
function TX3DRoute.DeepCopy(const CopyState: TX3DNodeDeepCopyState): TX3DRoute; |
| 594 |
var |
| 595 |
NewSourceNode, NewDestinationNode: TX3DNode; |
| 596 |
NewSourceEvent, NewDestinationEvent: TX3DEvent; |
| 597 |
begin |
| 598 |
Result := TX3DRoute.Create; |
| 599 |
Result.Internal := Internal; |
| 600 |
|
| 601 |
if (SourceNode <> nil) and |
| 602 |
(SourceEvent <> nil) then |
| 603 |
begin |
| 604 |
NewSourceNode := CopyState.DeepCopy(SourceNode); |
| 605 |
NewSourceEvent := NewSourceNode.AnyEvent(SourceEvent.X3DName); |
| 606 |
if NewSourceEvent = nil then |
| 607 |
raise EInternalError.CreateFmt('Route source node "%s" (%s) has event "%s", which is not found in this node''s deep copy', |
| 608 |
[ NewSourceNode.X3DName, |
| 609 |
NewSourceNode.X3DType, |
| 610 |
NewSourceEvent.X3DName ]); |
| 611 |
Result.SetSourceDirectly(NewSourceNode, NewSourceEvent); |
| 612 |
end; |
| 613 |
|
| 614 |
if (DestinationNode <> nil) and |
| 615 |
(DestinationEvent <> nil) then |
| 616 |
begin |
| 617 |
NewDestinationNode := CopyState.DeepCopy(DestinationNode); |
| 618 |
NewDestinationEvent := NewDestinationNode.AnyEvent(DestinationEvent.X3DName); |
| 619 |
if NewDestinationEvent = nil then |
| 620 |
raise EInternalError.CreateFmt('Route destination node "%s" (%s) has event "%s", which is not found in this node''s deep copy', |
| 621 |
[ NewDestinationNode.X3DName, |
| 622 |
NewDestinationNode.X3DType, |
| 623 |
NewDestinationEvent.X3DName ]); |
| 624 |
Result.SetDestinationDirectly(NewDestinationNode, NewDestinationEvent); |
| 625 |
end; |
| 626 |
end; |
| 627 |
|
| 628 |
{$endif read_implementation} |