| 1 |
{ |
| 2 |
Copyright 2008-2023 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ CastleScript utilities for usage as X3D scripts. } |
| 17 |
unit CastleInternalX3DScript; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses X3DFields, CastleScript, CastleUtils, CastleClassUtils, X3DTime; |
| 24 |
|
| 25 |
{$define read_interface} |
| 26 |
|
| 27 |
type |
| 28 |
TCasScriptX3DValueList = class(TCasScriptValueList) |
| 29 |
private |
| 30 |
FFieldOrEvents: TX3DFieldOrEventList; |
| 31 |
FLastEventTimes: TX3DTimeList; |
| 32 |
InsideAfterExecute: boolean; |
| 33 |
public |
| 34 |
constructor Create(AFreeObjects: boolean); |
| 35 |
destructor Destroy; override; |
| 36 |
|
| 37 |
{ List of field/events associated with this list's CasScript variables. |
| 38 |
This list is read-only (use @link(Add) to add here). |
| 39 |
It always has the same Count as our own count. } |
| 40 |
property FieldOrEvents: TX3DFieldOrEventList read FFieldOrEvents; |
| 41 |
|
| 42 |
{ Create TCasScriptValue descendant suitable to hold FieldOrEvent |
| 43 |
value, and add it to Items. |
| 44 |
FieldOrEvent is added to FieldOrEvents list, so we keep |
| 45 |
all information. } |
| 46 |
procedure Add(FieldOrEvent: TX3DFieldOrEvent); reintroduce; |
| 47 |
|
| 48 |
procedure BeforeExecute; |
| 49 |
procedure AfterExecute(const Time: TX3DTime); |
| 50 |
|
| 51 |
{ Clear the memory when the last events were generated from this script. |
| 52 |
For each script output-capable field (inputOutput or outputOnly) |
| 53 |
on this list, we keep track of when it last generated an event. |
| 54 |
|
| 55 |
This is needed to avoid loops. |
| 56 |
|
| 57 |
@italic(Some background info why this is needed:) |
| 58 |
|
| 59 |
X3D spec says (4.4.8.3 Execution model): |
| 60 |
Nodes that contain output events shall produce at most |
| 61 |
one event per field per timestamp. [...] This also applies to scripts. |
| 62 |
|
| 63 |
"[...]" above talks about ROUTE breaking rule, we handle this |
| 64 |
generally in TX3DRoute. The trouble with script is that it can |
| 65 |
make loops even without the help of ROUTEs: script receives it's inputs, |
| 66 |
and sends it's outputs, so when you send an output that causes your own |
| 67 |
input you made a loop... The most trivial example of this |
| 68 |
is when on receiving inputOutput you will set the very same inputOutput |
| 69 |
field, thus making a loop. |
| 70 |
|
| 71 |
Scripts with directOutput (although not implemented yet) can |
| 72 |
cause even more trouble, as you can generate input event on another node |
| 73 |
that will get back to you. If two scripts generate events on each |
| 74 |
other, this can again make a loop, without the help of ROUTEs. |
| 75 |
|
| 76 |
So we have to secure against this. This is done pretty much like |
| 77 |
with ROUTEs: we just remember the last timestamp, and ignore sending |
| 78 |
events again. } |
| 79 |
procedure ResetLastEventTimes; |
| 80 |
end; |
| 81 |
|
| 82 |
function X3DCasScriptCreateValue(FieldOrEvent: TX3DFieldOrEvent): TCasScriptValue; |
| 83 |
|
| 84 |
{ Do common things before VRML/X3D script with this variable is executed. |
| 85 |
This resets ValueAssigned (will be used in AfterExecute), |
| 86 |
and sets current variable's value from FieldOrEvent (if this is a field). } |
| 87 |
procedure X3DCasScriptBeforeExecute(Value: TCasScriptValue; |
| 88 |
FieldOrEvent: TX3DFieldOrEvent); |
| 89 |
|
| 90 |
{ Do common things after VRML/X3D script with this variable is executed. |
| 91 |
This checks ValueAssigned, and propagates value change to appropriate |
| 92 |
field/event, sending event/setting field. } |
| 93 |
procedure X3DCasScriptAfterExecute(Value: TCasScriptValue; |
| 94 |
FieldOrEvent: TX3DFieldOrEvent; var LastEventTime: TX3DTime; |
| 95 |
const Time: TX3DTime); |
| 96 |
|
| 97 |
{$undef read_interface} |
| 98 |
|
| 99 |
implementation |
| 100 |
|
| 101 |
uses SysUtils, X3DNodes, CastleLog, CastleScriptVectors, |
| 102 |
CastleVectors, CastleScriptImages, CastleScriptArrays; |
| 103 |
|
| 104 |
{$define read_implementation} |
| 105 |
|
| 106 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 107 |
type |
| 108 |
{ We use TCasScriptVec4f to represent VRML/X3D rotations. |
| 109 |
TCasScriptRotation is just for notation convenience. } |
| 110 |
TCasScriptRotation = TCasScriptVec4f; |
| 111 |
TCasScriptRotationArray = TCasScriptVec4fArray; |
| 112 |
{$endif CASTLE_SCRIPT_FPC} |
| 113 |
|
| 114 |
{ general utils -------------------------------------------------------- } |
| 115 |
|
| 116 |
function X3DCasScriptCreateValue(FieldOrEvent: TX3DFieldOrEvent): TCasScriptValue; |
| 117 |
var |
| 118 |
FieldClass: TX3DFieldClass; |
| 119 |
begin |
| 120 |
if FieldOrEvent is TX3DEvent then |
| 121 |
FieldClass := TX3DEvent(FieldOrEvent).FieldClass else |
| 122 |
FieldClass := TX3DFieldClass(FieldOrEvent.ClassType); |
| 123 |
|
| 124 |
if FieldClass.InheritsFrom(TSFEnum) or |
| 125 |
FieldClass.InheritsFrom(TSFLong) then |
| 126 |
Result := TCasScriptInteger.Create(true) else |
| 127 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 128 |
if FieldClass.InheritsFrom(TMFLong) then |
| 129 |
Result := TCasScriptInt32Array.Create(true) else |
| 130 |
{$endif CASTLE_SCRIPT_FPC} |
| 131 |
if FieldClass.InheritsFrom(TSFFloat) or |
| 132 |
FieldClass.InheritsFrom(TSFDouble) then |
| 133 |
Result := TCasScriptFloat.Create(true) else |
| 134 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 135 |
if FieldClass.InheritsFrom(TMFFloat) then |
| 136 |
Result := TCasScriptSingleArray.Create(true) else |
| 137 |
if FieldClass.InheritsFrom(TMFDouble) then |
| 138 |
Result := TCasScriptDoubleArray.Create(true) else |
| 139 |
{$endif CASTLE_SCRIPT_FPC} |
| 140 |
if FieldClass.InheritsFrom(TSFBool) then |
| 141 |
Result := TCasScriptBoolean.Create(true) else |
| 142 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 143 |
if FieldClass.InheritsFrom(TMFBool) then |
| 144 |
Result := TCasScriptBooleanArray.Create(true) else |
| 145 |
{$endif CASTLE_SCRIPT_FPC} |
| 146 |
if FieldClass.InheritsFrom(TSFString) then |
| 147 |
Result := TCasScriptString.Create(true) else |
| 148 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 149 |
if FieldClass.InheritsFrom(TMFString) then |
| 150 |
Result := TCasScriptStringArray.Create(true) else |
| 151 |
if FieldClass.InheritsFrom(TSFVec2f) then |
| 152 |
Result := TCasScriptVec2f.Create(true) else |
| 153 |
if FieldClass.InheritsFrom(TMFVec2f) then |
| 154 |
Result := TCasScriptVec2fArray.Create(true) else |
| 155 |
if FieldClass.InheritsFrom(TSFVec3f) then |
| 156 |
Result := TCasScriptVec3f.Create(true) else |
| 157 |
if FieldClass.InheritsFrom(TMFVec3f) then |
| 158 |
Result := TCasScriptVec3fArray.Create(true) else |
| 159 |
if FieldClass.InheritsFrom(TSFVec4f) then |
| 160 |
Result := TCasScriptVec4f.Create(true) else |
| 161 |
if FieldClass.InheritsFrom(TMFVec4f) then |
| 162 |
Result := TCasScriptVec4fArray.Create(true) else |
| 163 |
if FieldClass.InheritsFrom(TSFVec2d) then |
| 164 |
Result := TCasScriptVec2d.Create(true) else |
| 165 |
if FieldClass.InheritsFrom(TMFVec2d) then |
| 166 |
Result := TCasScriptVec2dArray.Create(true) else |
| 167 |
if FieldClass.InheritsFrom(TSFVec3d) then |
| 168 |
Result := TCasScriptVec3d.Create(true) else |
| 169 |
if FieldClass.InheritsFrom(TMFVec3d) then |
| 170 |
Result := TCasScriptVec3dArray.Create(true) else |
| 171 |
if FieldClass.InheritsFrom(TSFVec4d) then |
| 172 |
Result := TCasScriptVec4d.Create(true) else |
| 173 |
if FieldClass.InheritsFrom(TMFVec4d) then |
| 174 |
Result := TCasScriptVec4dArray.Create(true) else |
| 175 |
if FieldClass.InheritsFrom(TSFRotation) then |
| 176 |
Result := TCasScriptRotation.Create(true) else |
| 177 |
if FieldClass.InheritsFrom(TMFRotation) then |
| 178 |
Result := TCasScriptRotationArray.Create(true) else |
| 179 |
if FieldClass.InheritsFrom(TSFMatrix3f) then |
| 180 |
Result := TCasScriptMatrix3f.Create(true) else |
| 181 |
if FieldClass.InheritsFrom(TMFMatrix3f) then |
| 182 |
Result := TCasScriptMatrix3fArray.Create(true) else |
| 183 |
if FieldClass.InheritsFrom(TSFMatrix4f) then |
| 184 |
Result := TCasScriptMatrix4f.Create(true) else |
| 185 |
if FieldClass.InheritsFrom(TMFMatrix4f) then |
| 186 |
Result := TCasScriptMatrix4fArray.Create(true) else |
| 187 |
if FieldClass.InheritsFrom(TSFMatrix3d) then |
| 188 |
Result := TCasScriptMatrix3d.Create(true) else |
| 189 |
if FieldClass.InheritsFrom(TMFMatrix3d) then |
| 190 |
Result := TCasScriptMatrix3dArray.Create(true) else |
| 191 |
if FieldClass.InheritsFrom(TSFMatrix4d) then |
| 192 |
Result := TCasScriptMatrix4d.Create(true) else |
| 193 |
if FieldClass.InheritsFrom(TMFMatrix4d) then |
| 194 |
Result := TCasScriptMatrix4dArray.Create(true) else |
| 195 |
if FieldClass.InheritsFrom(TSFImage) {or |
| 196 |
FieldClass.InheritsFrom(TMFImage) }then |
| 197 |
Result := TCasScriptImage.Create(true) else |
| 198 |
{$endif CASTLE_SCRIPT_FPC} |
| 199 |
begin |
| 200 |
WritelnWarning('X3D', 'Note that CastleScript is not yet suitable to process values of type ' + FieldClass.X3DType); |
| 201 |
Result := TCasScriptFloat.Create(true); |
| 202 |
end; |
| 203 |
|
| 204 |
Result.Name := FieldOrEvent.X3DName; |
| 205 |
end; |
| 206 |
|
| 207 |
procedure X3DCasScriptBeforeExecute(Value: TCasScriptValue; |
| 208 |
FieldOrEvent: TX3DFieldOrEvent); |
| 209 |
|
| 210 |
procedure AssignX3DFieldValue(Field: TX3DField); |
| 211 |
begin |
| 212 |
if Field is TSFEnum then |
| 213 |
TCasScriptInteger(Value).Value := TSFEnum(Field).Value else |
| 214 |
if Field is TSFLong then |
| 215 |
TCasScriptInteger(Value).Value := TSFLong(Field).Value else |
| 216 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 217 |
if Field is TMFLong then |
| 218 |
TCasScriptInt32Array(Value).Value := TMFLong(Field).Items else |
| 219 |
{$endif CASTLE_SCRIPT_FPC} |
| 220 |
|
| 221 |
if Field is TSFFloat then |
| 222 |
TCasScriptFloat(Value).Value := TSFFloat(Field).Value else |
| 223 |
if Field is TSFDouble then |
| 224 |
TCasScriptFloat(Value).Value := TSFDouble(Field).Value else |
| 225 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 226 |
if Field is TMFFloat then |
| 227 |
TCasScriptSingleArray(Value).Value := TMFFloat(Field).Items else |
| 228 |
if Field is TMFDouble then |
| 229 |
TCasScriptDoubleArray(Value).Value := TMFDouble(Field).Items else |
| 230 |
{$endif CASTLE_SCRIPT_FPC} |
| 231 |
|
| 232 |
if Field is TSFBool then |
| 233 |
TCasScriptBoolean(Value).Value := TSFBool(Field).Value else |
| 234 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 235 |
if Field is TMFBool then |
| 236 |
TCasScriptBooleanArray(Value).Value := TMFBool(Field).Items else |
| 237 |
{$endif CASTLE_SCRIPT_FPC} |
| 238 |
|
| 239 |
if Field is TSFString then |
| 240 |
TCasScriptString(Value).Value := TSFString(Field).Value else |
| 241 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 242 |
if Field is TMFString then |
| 243 |
TCasScriptStringArray(Value).Value := TMFString(Field).Items else |
| 244 |
|
| 245 |
if Field is TSFVec2f then |
| 246 |
TCasScriptVec2f(Value).Value := TSFVec2f(Field).Value else |
| 247 |
if Field is TMFVec2f then |
| 248 |
TCasScriptVec2fArray(Value).Value := TMFVec2f(Field).Items else |
| 249 |
|
| 250 |
if Field is TSFVec3f then |
| 251 |
TCasScriptVec3f(Value).Value := TSFVec3f(Field).Value else |
| 252 |
if Field is TMFVec3f then |
| 253 |
TCasScriptVec3fArray(Value).Value := TMFVec3f(Field).Items else |
| 254 |
|
| 255 |
if Field is TSFVec4f then |
| 256 |
TCasScriptVec4f(Value).Value := TSFVec4f(Field).Value else |
| 257 |
if Field is TMFVec4f then |
| 258 |
TCasScriptVec4fArray(Value).Value := TMFVec4f(Field).Items else |
| 259 |
|
| 260 |
if Field is TSFVec2d then |
| 261 |
TCasScriptVec2d(Value).Value := TSFVec2d(Field).Value else |
| 262 |
if Field is TMFVec2d then |
| 263 |
TCasScriptVec2dArray(Value).Value := TMFVec2d(Field).Items else |
| 264 |
|
| 265 |
if Field is TSFVec3d then |
| 266 |
TCasScriptVec3d(Value).Value := TSFVec3d(Field).Value else |
| 267 |
if Field is TMFVec3d then |
| 268 |
TCasScriptVec3dArray(Value).Value := TMFVec3d(Field).Items else |
| 269 |
|
| 270 |
if Field is TSFVec4d then |
| 271 |
TCasScriptVec4d(Value).Value := TSFVec4d(Field).Value else |
| 272 |
if Field is TMFVec4d then |
| 273 |
TCasScriptVec4dArray(Value).Value := TMFVec4d(Field).Items else |
| 274 |
|
| 275 |
if Field is TSFRotation then |
| 276 |
TCasScriptRotation(Value).Value := TSFRotation(Field).Value else |
| 277 |
if Field is TMFRotation then |
| 278 |
TCasScriptRotationArray(Value).Value := TMFRotation(Field).Items else |
| 279 |
|
| 280 |
if Field is TSFMatrix3f then |
| 281 |
TCasScriptMatrix3f(Value).Value := TSFMatrix3f(Field).Value else |
| 282 |
if Field is TMFMatrix3f then |
| 283 |
TCasScriptMatrix3fArray(Value).Value := TMFMatrix3f(Field).Items else |
| 284 |
|
| 285 |
if Field is TSFMatrix4f then |
| 286 |
TCasScriptMatrix4f(Value).Value := TSFMatrix4f(Field).Value else |
| 287 |
if Field is TMFMatrix4f then |
| 288 |
TCasScriptMatrix4fArray(Value).Value := TMFMatrix4f(Field).Items else |
| 289 |
|
| 290 |
if Field is TSFMatrix3d then |
| 291 |
TCasScriptMatrix3d(Value).Value := TSFMatrix3d(Field).Value else |
| 292 |
if Field is TMFMatrix3d then |
| 293 |
TCasScriptMatrix3dArray(Value).Value := TMFMatrix3d(Field).Items else |
| 294 |
|
| 295 |
if Field is TSFMatrix4d then |
| 296 |
TCasScriptMatrix4d(Value).Value := TSFMatrix4d(Field).Value else |
| 297 |
if Field is TMFMatrix4d then |
| 298 |
TCasScriptMatrix4dArray(Value).Value := TMFMatrix4d(Field).Items else |
| 299 |
|
| 300 |
if Field is TSFImage then |
| 301 |
TCasScriptImage(Value).Value := TSFImage(Field).Value else |
| 302 |
{if Field is TMFImage then |
| 303 |
TCasScriptImageArray(Value).Value := TMFImage(Field).Items else} |
| 304 |
{$endif CASTLE_SCRIPT_FPC} |
| 305 |
|
| 306 |
{ No sensible way to convert, just fall back to predictable 0.0. } |
| 307 |
TCasScriptFloat(Value).Value := 0.0; |
| 308 |
end; |
| 309 |
|
| 310 |
begin |
| 311 |
if FieldOrEvent is TX3DField then |
| 312 |
AssignX3DFieldValue(TX3DField(FieldOrEvent)); |
| 313 |
|
| 314 |
Value.ValueAssigned := false; |
| 315 |
end; |
| 316 |
|
| 317 |
procedure X3DCasScriptAfterExecute(Value: TCasScriptValue; |
| 318 |
FieldOrEvent: TX3DFieldOrEvent; var LastEventTime: TX3DTime; |
| 319 |
const Time: TX3DTime); |
| 320 |
|
| 321 |
function CreateTempField(Event: TX3DEvent): TX3DField; |
| 322 |
begin |
| 323 |
{ We have to create temporary field to send. |
| 324 |
This is Ok, TX3DEvent.Send shortcuts would do the same thing |
| 325 |
after all. |
| 326 |
|
| 327 |
We use Event.ParentNode, Event.Name for this temporary field |
| 328 |
for the same reasons TX3DEvent.Send shortcuts do this: |
| 329 |
to get nice information in Logger node reports. } |
| 330 |
|
| 331 |
Result := Event.FieldClass.CreateUndefined(Event.ParentNode, false, Event.X3DName); |
| 332 |
end; |
| 333 |
|
| 334 |
var |
| 335 |
AbortSending: boolean; |
| 336 |
Field: TX3DField; |
| 337 |
SendToEvent: TX3DEvent; |
| 338 |
begin |
| 339 |
if Value.ValueAssigned then |
| 340 |
begin |
| 341 |
{ calculate Field, will be set based on our current Value } |
| 342 |
|
| 343 |
if FieldOrEvent is TX3DEvent then |
| 344 |
begin |
| 345 |
SendToEvent := TX3DEvent(FieldOrEvent); |
| 346 |
Field := CreateTempField(SendToEvent); |
| 347 |
end else |
| 348 |
if (FieldOrEvent as TX3DField).Exposed then |
| 349 |
begin |
| 350 |
SendToEvent := TX3DField(FieldOrEvent).EventIn; |
| 351 |
Field := CreateTempField(SendToEvent); |
| 352 |
end else |
| 353 |
begin |
| 354 |
SendToEvent := nil; |
| 355 |
Assert(FieldOrEvent is TX3DField); |
| 356 |
Assert(not TX3DField(FieldOrEvent).Exposed); |
| 357 |
{ For initializeOnly fields, we will set them directly. } |
| 358 |
Field := TX3DField(FieldOrEvent); |
| 359 |
end; |
| 360 |
|
| 361 |
AbortSending := false; |
| 362 |
|
| 363 |
{ set Field value (and leave AbortSending as false), |
| 364 |
or leave Field value (and set AbortSending as true). } |
| 365 |
|
| 366 |
if Field is TSFEnum then |
| 367 |
TSFEnum(Field).Value := TCasScriptInteger(Value).Value else |
| 368 |
if Field is TSFLong then |
| 369 |
TSFLong(Field).Value := TCasScriptInteger(Value).Value else |
| 370 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 371 |
if Field is TMFLong then |
| 372 |
TMFLong(Field).Items := TCasScriptInt32Array(Value).Value else |
| 373 |
{$endif CASTLE_SCRIPT_FPC} |
| 374 |
|
| 375 |
if Field is TSFFloat then |
| 376 |
TSFFloat(Field).Value := TCasScriptFloat(Value).Value else |
| 377 |
if Field is TSFDouble then |
| 378 |
TSFDouble(Field).Value := TCasScriptFloat(Value).Value else |
| 379 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 380 |
if Field is TMFFloat then |
| 381 |
TMFFloat(Field).Items := TCasScriptSingleArray(Value).Value else |
| 382 |
if Field is TMFDouble then |
| 383 |
TMFDouble(Field).Items := TCasScriptDoubleArray(Value).Value else |
| 384 |
{$endif CASTLE_SCRIPT_FPC} |
| 385 |
|
| 386 |
if Field is TSFBool then |
| 387 |
TSFBool(Field).Value := TCasScriptBoolean(Value).Value else |
| 388 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 389 |
if Field is TMFBool then |
| 390 |
TMFBool(Field).Items := TCasScriptBooleanArray(Value).Value else |
| 391 |
{$endif CASTLE_SCRIPT_FPC} |
| 392 |
|
| 393 |
if Field is TSFString then |
| 394 |
TSFString(Field).Value := TCasScriptString(Value).Value else |
| 395 |
{$ifdef CASTLE_SCRIPT_FPC} |
| 396 |
if Field is TMFString then |
| 397 |
TMFString(Field).Items := TCasScriptStringArray(Value).Value else |
| 398 |
|
| 399 |
if Field is TSFVec2f then |
| 400 |
TSFVec2f(Field).Value := TCasScriptVec2f(Value).Value else |
| 401 |
if Field is TMFVec2f then |
| 402 |
TMFVec2f(Field).Items := TCasScriptVec2fArray(Value).Value else |
| 403 |
|
| 404 |
if Field is TSFVec3f then |
| 405 |
TSFVec3f(Field).Value := TCasScriptVec3f(Value).Value else |
| 406 |
if Field is TMFVec3f then |
| 407 |
TMFVec3f(Field).Items := TCasScriptVec3fArray(Value).Value else |
| 408 |
|
| 409 |
if Field is TSFVec4f then |
| 410 |
TSFVec4f(Field).Value := TCasScriptVec4f(Value).Value else |
| 411 |
if Field is TMFVec4f then |
| 412 |
TMFVec4f(Field).Items := TCasScriptVec4fArray(Value).Value else |
| 413 |
|
| 414 |
if Field is TSFVec2d then |
| 415 |
TSFVec2d(Field).Value := TCasScriptVec2d(Value).Value else |
| 416 |
if Field is TMFVec2d then |
| 417 |
TMFVec2d(Field).Items := TCasScriptVec2dArray(Value).Value else |
| 418 |
|
| 419 |
if Field is TSFVec3d then |
| 420 |
TSFVec3d(Field).Value := TCasScriptVec3d(Value).Value else |
| 421 |
if Field is TMFVec3d then |
| 422 |
TMFVec3d(Field).Items := TCasScriptVec3dArray(Value).Value else |
| 423 |
|
| 424 |
if Field is TSFVec4d then |
| 425 |
TSFVec4d(Field).Value := TCasScriptVec4d(Value).Value else |
| 426 |
if Field is TMFVec4d then |
| 427 |
TMFVec4d(Field).Items := TCasScriptVec4dArray(Value).Value else |
| 428 |
|
| 429 |
if Field is TSFRotation then |
| 430 |
TSFRotation(Field).Value := TCasScriptRotation(Value).Value else |
| 431 |
if Field is TMFRotation then |
| 432 |
TMFRotation(Field).Items := TCasScriptRotationArray(Value).Value else |
| 433 |
|
| 434 |
if Field is TSFMatrix3f then |
| 435 |
TSFMatrix3f(Field).Value := TCasScriptMatrix3f(Value).Value else |
| 436 |
if Field is TMFMatrix3f then |
| 437 |
TMFMatrix3f(Field).Items := TCasScriptMatrix3fArray(Value).Value else |
| 438 |
|
| 439 |
if Field is TSFMatrix4f then |
| 440 |
TSFMatrix4f(Field).Value := TCasScriptMatrix4f(Value).Value else |
| 441 |
if Field is TMFMatrix4f then |
| 442 |
TMFMatrix4f(Field).Items := TCasScriptMatrix4fArray(Value).Value else |
| 443 |
|
| 444 |
if Field is TSFMatrix3d then |
| 445 |
TSFMatrix3d(Field).Value := TCasScriptMatrix3d(Value).Value else |
| 446 |
if Field is TMFMatrix3d then |
| 447 |
TMFMatrix3d(Field).Items := TCasScriptMatrix3dArray(Value).Value else |
| 448 |
|
| 449 |
if Field is TSFMatrix4d then |
| 450 |
TSFMatrix4d(Field).Value := TCasScriptMatrix4d(Value).Value else |
| 451 |
if Field is TMFMatrix4d then |
| 452 |
TMFMatrix4d(Field).Items := TCasScriptMatrix4dArray(Value).Value else |
| 453 |
|
| 454 |
if Field is TSFImage then |
| 455 |
TSFImage(Field).Value := TCasScriptImage(Value).Value.MakeCopy else |
| 456 |
{if Field is TMFImage then |
| 457 |
begin |
| 458 |
TMFImage(Field).Items := TCasScriptImageArray(Value).Value |
| 459 |
end else} |
| 460 |
{$endif CASTLE_SCRIPT_FPC} |
| 461 |
|
| 462 |
begin |
| 463 |
{ No sensible way to convert, just do nothing, don't set/send anything. } |
| 464 |
AbortSending := true; |
| 465 |
end; |
| 466 |
|
| 467 |
{ send and free Field, for output events or exposed fields. } |
| 468 |
|
| 469 |
if SendToEvent <> nil then |
| 470 |
begin |
| 471 |
if not AbortSending then |
| 472 |
begin |
| 473 |
if Time > LastEventTime then |
| 474 |
begin |
| 475 |
LastEventTime := Time; |
| 476 |
SendToEvent.Send(Field, Time); |
| 477 |
end else |
| 478 |
WritelnLog('X3D Script', Format( |
| 479 |
'Sending event %s from Script ignored at <= timestamp (%f, while last event was on %f). Potential loop avoided', |
| 480 |
[ SendToEvent.X3DName, Time.Seconds, LastEventTime.Seconds ])); |
| 481 |
end; |
| 482 |
FreeAndNil(Field); |
| 483 |
end; |
| 484 |
|
| 485 |
{ Theoretically, we should call Scene.InternalChangedField now |
| 486 |
if FieldOrEvent was initializeOnly field (and not AbortSending). |
| 487 |
But in this case, we know FieldOrEvent comes from a Script node, |
| 488 |
and in this situation InternalChangedField doesn't do anything anyway. } |
| 489 |
|
| 490 |
{ This is needed for TCasScriptX3DValueList.AfterExecute trick. |
| 491 |
We handled this change, so we mark it by ValueAssigned = false. } |
| 492 |
Value.ValueAssigned := false; |
| 493 |
end; |
| 494 |
end; |
| 495 |
|
| 496 |
{ TCasScriptX3DValueList -------------------------------------------------- } |
| 497 |
|
| 498 |
constructor TCasScriptX3DValueList.Create(AFreeObjects: boolean); |
| 499 |
begin |
| 500 |
inherited; |
| 501 |
FFieldOrEvents := TX3DFieldOrEventList.Create(false); |
| 502 |
FLastEventTimes := TX3DTimeList.Create; |
| 503 |
end; |
| 504 |
|
| 505 |
destructor TCasScriptX3DValueList.Destroy; |
| 506 |
begin |
| 507 |
SysUtils.FreeAndNil(FFieldOrEvents); |
| 508 |
SysUtils.FreeAndNil(FLastEventTimes); |
| 509 |
inherited; |
| 510 |
end; |
| 511 |
|
| 512 |
procedure TCasScriptX3DValueList.Add(FieldOrEvent: TX3DFieldOrEvent); |
| 513 |
begin |
| 514 |
inherited Add(X3DCasScriptCreateValue(FieldOrEvent)); |
| 515 |
FieldOrEvents.Add(FieldOrEvent); |
| 516 |
FLastEventTimes.Add(TX3DTime.Oldest); |
| 517 |
end; |
| 518 |
|
| 519 |
procedure TCasScriptX3DValueList.BeforeExecute; |
| 520 |
var |
| 521 |
I: Integer; |
| 522 |
begin |
| 523 |
if not InsideAfterExecute then |
| 524 |
begin |
| 525 |
for I := 0 to Count - 1 do |
| 526 |
X3DCasScriptBeforeExecute(Items[I], FieldOrEvents[I]); |
| 527 |
end; |
| 528 |
end; |
| 529 |
|
| 530 |
procedure TCasScriptX3DValueList.AfterExecute(const Time: TX3DTime); |
| 531 |
var |
| 532 |
I: Integer; |
| 533 |
WasSomeValueAssigned: boolean; |
| 534 |
begin |
| 535 |
if not InsideAfterExecute then |
| 536 |
begin |
| 537 |
{ Note that X3DCasScriptAfterExecute may send events when given |
| 538 |
fields changed. These events may cause yet another execution |
| 539 |
of the same script. For example, |
| 540 |
- setting inputOutput field of the script causes another execution |
| 541 |
of the same script |
| 542 |
- setting anything may cause route to another input event. |
| 543 |
It may be another field/event of the same script, |
| 544 |
or it may be field/event of different node (possibly, different |
| 545 |
script node) that will cause execution back to our script |
| 546 |
by another route... |
| 547 |
|
| 548 |
So we have to secure against this. We don't want to allow |
| 549 |
resetting of ValueAssigned at the beginning of recursive script calls. |
| 550 |
Actually, we also don't want to reinitialize the CastleScript field |
| 551 |
from VRML/X3D field value, to not lose new value. |
| 552 |
What to do about changed values? If recursive script execution |
| 553 |
will change again the same field, we lose the old value, but that's |
| 554 |
somewhat Ok. |
| 555 |
|
| 556 |
My approach: using InsideAfterExecute. |
| 557 |
If InsideAfterExecute is already true, then Before/AfterExecute |
| 558 |
do nothing. Actual script execution will possibly change ValueAssigned |
| 559 |
to true for some fields, and change their value, that's Ok. |
| 560 |
Only Before/AfterExecute with InsideAfterExecute = false will cause actual |
| 561 |
sending of fields/events. |
| 562 |
|
| 563 |
Note that execution of recursive script may cause setting ValueAssigned |
| 564 |
:= true on earlier value. So we have to reiterate over Items, |
| 565 |
until no ValueAssigned needs to be handled. Don't worry, this will |
| 566 |
not cause loops, FLastEventTimes prevents the loops. So eventually |
| 567 |
all fields will send their out values, and then WasSomeValueAssigned |
| 568 |
will have to be false and we will finish. } |
| 569 |
|
| 570 |
InsideAfterExecute := true; |
| 571 |
try |
| 572 |
repeat |
| 573 |
WasSomeValueAssigned := false; |
| 574 |
for I := 0 to Count - 1 do |
| 575 |
begin |
| 576 |
if Items[I].ValueAssigned then |
| 577 |
WasSomeValueAssigned := true; |
| 578 |
X3DCasScriptAfterExecute(Items[I], FieldOrEvents[I], FLastEventTimes.L[I], Time); |
| 579 |
end; |
| 580 |
until not WasSomeValueAssigned; |
| 581 |
finally InsideAfterExecute := false end; |
| 582 |
end; |
| 583 |
end; |
| 584 |
|
| 585 |
procedure TCasScriptX3DValueList.ResetLastEventTimes; |
| 586 |
var |
| 587 |
I: Integer; |
| 588 |
begin |
| 589 |
for I := 0 to Count - 1 do |
| 590 |
FLastEventTimes.L[I] := TX3DTime.Oldest; |
| 591 |
end; |
| 592 |
|
| 593 |
end. |