/usr/src/castle-game-engine-4.1.1/x3d/x3d_eventutilities.inc is in castle-game-engine-src 4.1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | {
Copyright 2008-2013 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{$ifdef read_interface}
{ }
TAbstractSequencerNode = class(TAbstractChildNode)
private
procedure EventSet_FractionReceive(Event: TX3DEvent; Value: TX3DField;
const Time: TX3DTime);
procedure EventNextReceive(Event: TX3DEvent; Value: TX3DField;
const Time: TX3DTime);
procedure EventPreviousReceive(Event: TX3DEvent; Value: TX3DField;
const Time: TX3DTime);
private
FCurrentKeyRange: Integer;
protected
{ Analogous to TAbstractInterpolatorNode.KeyRange. }
function KeyRange(const Fraction: Single): Integer;
{ In what key range we currently are. In other words, which
keyValue was last send (passed to SendValueChanged).
We have to keep track of this information, per each node,
as specification clearly says that value_changed should not be
generated continously on each set_fraction, but only when value
actually changes.
Valid values here are between -1 and FdKey.Count - 1.
Values between 0 and FdKey.Count - 1 indicate which keyValue was
last send. Value -1 means that SendValueChanged was not yet called,
this is the default value (after construction). }
property CurrentKeyRange: Integer read FCurrentKeyRange;
procedure SetCurrentKeyRange(const Value: Integer; const Time: TX3DTime);
{ Override this is descendants to actually send value_changed event
witk keyValue[ValueIndex]. You are guaranteed that ValueIndex
is between 0 and FdKey.Count - 1, but you have to check yourself
whether it's also between 0 and FdKeyValue.Count - 1
(although spec requires that key and keyValue fields have the same size,
we have to protect against invalid files). }
procedure SendValueChanged(const ValueIndex: Integer;
const Time: TX3DTime); virtual; abstract;
public
procedure CreateNode; override;
{ Event: SFBool, in } { }
private FEventNext: TX3DEvent;
public property EventNext: TX3DEvent read FEventNext;
{ Event: SFBool, in } { }
private FEventPrevious: TX3DEvent;
public property EventPrevious: TX3DEvent read FEventPrevious;
{ Event: SFFloat, in } { }
private FEventSet_fraction: TX3DEvent;
public property EventSet_fraction: TX3DEvent read FEventSet_fraction;
private FFdKey: TMFFloat;
public property FdKey: TMFFloat read FFdKey;
end;
TAbstractTriggerNode = class(TAbstractChildNode)
public
procedure CreateNode; override;
end;
TBooleanFilterNode = class(TAbstractChildNode)
private
procedure EventSet_BooleanReceive(Event: TX3DEvent; Value: TX3DField;
const Time: TX3DTime);
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
class function URNMatching(const URN: string): boolean; override;
{ Event: SFBool, in } { }
private FEventSet_boolean: TX3DEvent;
public property EventSet_boolean: TX3DEvent read FEventSet_boolean;
{ Event: SFBool, out } { }
private FEventInputFalse: TX3DEvent;
public property EventInputFalse: TX3DEvent read FEventInputFalse;
{ Event: SFBool, out } { }
private FEventInputNegate: TX3DEvent;
public property EventInputNegate: TX3DEvent read FEventInputNegate;
{ Event: SFBool, out } { }
private FEventInputTrue: TX3DEvent;
public property EventInputTrue: TX3DEvent read FEventInputTrue;
end;
TBooleanSequencerNode = class(TAbstractSequencerNode)
protected
procedure SendValueChanged(const ValueIndex: Integer;
const Time: TX3DTime); override;
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
class function URNMatching(const URN: string): boolean; override;
private FFdKeyValue: TMFBool;
public property FdKeyValue: TMFBool read FFdKeyValue;
{ Event: SFBool, out } { }
private FEventValue_changed: TX3DEvent;
public property EventValue_changed: TX3DEvent read FEventValue_changed;
end;
TBooleanToggleNode = class(TAbstractChildNode)
private
procedure EventSet_BooleanReceive(Event: TX3DEvent; Value: TX3DField;
const Time: TX3DTime);
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
class function URNMatching(const URN: string): boolean; override;
{ Event: SFBool, in } { }
private FEventSet_boolean: TX3DEvent;
public property EventSet_boolean: TX3DEvent read FEventSet_boolean;
private FFdToggle: TSFBool;
public property FdToggle: TSFBool read FFdToggle;
end;
TBooleanTriggerNode = class(TAbstractTriggerNode)
private
procedure EventSet_triggerTimeReceive(Event: TX3DEvent; Value: TX3DField;
const Time: TX3DTime);
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
class function URNMatching(const URN: string): boolean; override;
{ Event: SFTime, in } { }
private FEventSet_triggerTime: TX3DEvent;
public property EventSet_triggerTime: TX3DEvent read FEventSet_triggerTime;
{ Event: SFBool, out } { }
private FEventTriggerTrue: TX3DEvent;
public property EventTriggerTrue: TX3DEvent read FEventTriggerTrue;
end;
TIntegerSequencerNode = class(TAbstractSequencerNode)
protected
procedure SendValueChanged(const ValueIndex: Integer;
const Time: TX3DTime); override;
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
class function URNMatching(const URN: string): boolean; override;
private FFdKeyValue: TMFInt32;
public property FdKeyValue: TMFInt32 read FFdKeyValue;
{ Event: SFInt32, out } { }
private FEventValue_changed: TX3DEvent;
public property EventValue_changed: TX3DEvent read FEventValue_changed;
end;
TIntegerTriggerNode = class(TAbstractTriggerNode)
private
procedure EventSet_BooleanReceive(Event: TX3DEvent; Value: TX3DField;
const Time: TX3DTime);
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
class function URNMatching(const URN: string): boolean; override;
{ Event: SFBool, in } { }
private FEventSet_boolean: TX3DEvent;
public property EventSet_boolean: TX3DEvent read FEventSet_boolean;
private FFdIntegerKey: TSFInt32;
public property FdIntegerKey: TSFInt32 read FFdIntegerKey;
{ Event: SFInt32, out } { }
private FEventTriggerValue: TX3DEvent;
public property EventTriggerValue: TX3DEvent read FEventTriggerValue;
end;
TTimeTriggerNode = class(TAbstractTriggerNode)
private
procedure EventSet_BooleanReceive(Event: TX3DEvent; Value: TX3DField;
const Time: TX3DTime);
public
procedure CreateNode; override;
class function ClassNodeTypeName: string; override;
class function URNMatching(const URN: string): boolean; override;
{ Event: SFBool, in } { }
private FEventSet_boolean: TX3DEvent;
public property EventSet_boolean: TX3DEvent read FEventSet_boolean;
{ Event: SFTime, out } { }
private FEventTriggerTime: TX3DEvent;
public property EventTriggerTime: TX3DEvent read FEventTriggerTime;
end;
{$endif read_interface}
{$ifdef read_implementation}
procedure TAbstractSequencerNode.CreateNode;
begin
inherited;
FCurrentKeyRange := -1;
FEventNext := TX3DEvent.Create(Self, 'next', TSFBool, true);
Events.Add(FEventNext);
EventNext.OnReceive.Add(@EventNextReceive);
FEventPrevious := TX3DEvent.Create(Self, 'previous', TSFBool, true);
Events.Add(FEventPrevious);
EventPrevious.OnReceive.Add(@EventPreviousReceive);
FEventSet_fraction := TX3DEvent.Create(Self, 'set_fraction', TSFFloat, true);
Events.Add(FEventSet_fraction);
EventSet_Fraction.OnReceive.Add(@EventSet_FractionReceive);
FFdKey := TMFFloat.Create(Self, 'key', []);
Fields.Add(FFdKey);
{ X3D specification comment: (-Inf,Inf) }
DefaultContainerField := 'children';
end;
function TAbstractSequencerNode.KeyRange(
const Fraction: Single): Integer;
var
T: Single;
begin
{ T is simply ignored, as it's useless for sequences (since they generate
discrete events from float fraction, then they are not interested
in T value). }
Result := X3DNodes.KeyRange(FdKey.Items, Fraction, T);
end;
procedure TAbstractSequencerNode.SetCurrentKeyRange(const Value: Integer;
const Time: TX3DTime);
begin
if FCurrentKeyRange <> Value then
begin
Assert(Value >= -1);
Assert(Value < FdKey.Count);
FCurrentKeyRange := Value;
SendValueChanged(Value, Time);
end;
end;
procedure TAbstractSequencerNode.EventSet_FractionReceive(Event: TX3DEvent; Value: TX3DField;
const Time: TX3DTime);
var
KeyCount: Integer;
NewKeyRange: Integer;
begin
KeyCount := FdKey.Items.Count;
if KeyCount <> 0 then { for KeyCount = 0, do nothing }
begin
NewKeyRange := KeyRange((Value as TSFFloat).Value);
{ For NewKeyRange = 0 (before 1st key), leave it as 0.
For NewKeyRange = KeyCount, this is after last key, so we want
NewKeyRange = KeyCount - 1.
For other values, we're within some range, and NewKeyRange is index
of the right value --- we want to decrease it, so become index of the left
value. }
if NewKeyRange <> 0 then
Dec(NewKeyRange);
{ Set CurrentKeyRange and eventually send value_changed }
SetCurrentKeyRange(NewKeyRange, Time);
end;
end;
procedure TAbstractSequencerNode.EventNextReceive(Event: TX3DEvent; Value: TX3DField;
const Time: TX3DTime);
var
KeyCount, NewKeyRange: Integer;
begin
KeyCount := FdKey.Items.Count;
if (Value as TSFBool).Value and (KeyCount <> 0) then
begin
{ Note that CurrentKeyRange may be -1. Code below will still work then
(as CurrentKeyRange + 1 is always >= 0).
This also means that even when KeyCount = 1, calling "next" event
may be meaningfull: it will cause generation of the first keyvalue. }
NewKeyRange := CurrentKeyRange + 1;
if NewKeyRange = KeyCount then
NewKeyRange := 0;
{ Set CurrentKeyRange and eventually send value_changed }
SetCurrentKeyRange(NewKeyRange, Time);
end;
end;
procedure TAbstractSequencerNode.EventPreviousReceive(Event: TX3DEvent; Value: TX3DField;
const Time: TX3DTime);
var
KeyCount, NewKeyRange: Integer;
begin
KeyCount := FdKey.Items.Count;
if (Value as TSFBool).Value and (KeyCount <> 0) then
begin
{ Note that CurrentKeyRange may be -1. Code below will still work then
(as CurrentKeyRange - 1 is then -2, so we will jump to last item).
This also means that even when KeyCount = 1, calling "previous" event
may be meaningfull: it will cause generation of the last keyvalue. }
NewKeyRange := CurrentKeyRange - 1;
if NewKeyRange < 0 then
NewKeyRange := KeyCount - 1;
{ Set CurrentKeyRange and eventually send value_changed }
SetCurrentKeyRange(NewKeyRange, Time);
end;
end;
procedure TAbstractTriggerNode.CreateNode;
begin
inherited;
DefaultContainerField := 'children';
end;
procedure TBooleanFilterNode.CreateNode;
begin
inherited;
FEventSet_boolean := TX3DEvent.Create(Self, 'set_boolean', TSFBool, true);
Events.Add(FEventSet_boolean);
FEventSet_boolean.OnReceive.Add(@EventSet_BooleanReceive);
FEventInputFalse := TX3DEvent.Create(Self, 'inputFalse', TSFBool, false);
Events.Add(FEventInputFalse);
FEventInputNegate := TX3DEvent.Create(Self, 'inputNegate', TSFBool, false);
Events.Add(FEventInputNegate);
FEventInputTrue := TX3DEvent.Create(Self, 'inputTrue', TSFBool, false);
Events.Add(FEventInputTrue);
DefaultContainerField := 'children';
end;
procedure TBooleanFilterNode.EventSet_BooleanReceive(
Event: TX3DEvent; Value: TX3DField; const Time: TX3DTime);
var
B: boolean;
begin
B := (Value as TSFBool).Value;
if B then
EventInputTrue.Send(B, Time) else
EventInputFalse.Send(B, Time);
EventInputNegate.Send(not B, Time);
end;
class function TBooleanFilterNode.ClassNodeTypeName: string;
begin
Result := 'BooleanFilter';
end;
class function TBooleanFilterNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
procedure TBooleanSequencerNode.CreateNode;
begin
inherited;
FFdKeyValue := TMFBool.Create(Self, 'keyValue', []);
Fields.Add(FFdKeyValue);
FEventValue_changed := TX3DEvent.Create(Self, 'value_changed', TSFBool, false);
Events.Add(FEventValue_changed);
DefaultContainerField := 'children';
end;
class function TBooleanSequencerNode.ClassNodeTypeName: string;
begin
Result := 'BooleanSequencer';
end;
class function TBooleanSequencerNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
procedure TBooleanSequencerNode.SendValueChanged(const ValueIndex: Integer;
const Time: TX3DTime);
begin
if FdKeyValue.Count <> FdKey.Count then
OnWarning(wtMajor, 'VRML/X3D', 'BooleanSequencer node: key and keyValue fields should have the same size');
if ValueIndex < FdKeyValue.Count then
EventValue_Changed.Send(FdKeyValue.Items.Items[ValueIndex], Time);
end;
procedure TBooleanToggleNode.CreateNode;
begin
inherited;
FEventSet_boolean := TX3DEvent.Create(Self, 'set_boolean', TSFBool, true);
Events.Add(FEventSet_boolean);
FEventSet_boolean.OnReceive.Add(@EventSet_BooleanReceive);
FFdToggle := TSFBool.Create(Self, 'toggle', false);
Fields.Add(FFdToggle);
DefaultContainerField := 'children';
end;
procedure TBooleanToggleNode.EventSet_BooleanReceive(
Event: TX3DEvent; Value: TX3DField; const Time: TX3DTime);
var
B: boolean;
begin
B := (Value as TSFBool).Value;
if B then
FdToggle.EventIn.Send(not FdToggle.Value, Time);
end;
class function TBooleanToggleNode.ClassNodeTypeName: string;
begin
Result := 'BooleanToggle';
end;
class function TBooleanToggleNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
procedure TBooleanTriggerNode.CreateNode;
begin
inherited;
FEventSet_triggerTime := TX3DEvent.Create(Self, 'set_triggerTime', TSFTime, true);
Events.Add(FEventSet_triggerTime);
FEventSet_triggerTime.OnReceive.Add(@EventSet_triggerTimeReceive);
FEventTriggerTrue := TX3DEvent.Create(Self, 'triggerTrue', TSFBool, false);
Events.Add(FEventTriggerTrue);
DefaultContainerField := 'children';
end;
procedure TBooleanTriggerNode.EventSet_triggerTimeReceive(
Event: TX3DEvent; Value: TX3DField; const Time: TX3DTime);
begin
EventTriggerTrue.Send(true, Time);
end;
class function TBooleanTriggerNode.ClassNodeTypeName: string;
begin
Result := 'BooleanTrigger';
end;
class function TBooleanTriggerNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
procedure TIntegerSequencerNode.CreateNode;
begin
inherited;
FFdKeyValue := TMFInt32.Create(Self, 'keyValue', []);
Fields.Add(FFdKeyValue);
{ X3D specification comment: (-Inf,Inf) }
FEventValue_changed := TX3DEvent.Create(Self, 'value_changed', TSFInt32, false);
Events.Add(FEventValue_changed);
DefaultContainerField := 'children';
end;
class function TIntegerSequencerNode.ClassNodeTypeName: string;
begin
Result := 'IntegerSequencer';
end;
class function TIntegerSequencerNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
procedure TIntegerSequencerNode.SendValueChanged(const ValueIndex: Integer;
const Time: TX3DTime);
begin
if FdKeyValue.Count <> FdKey.Count then
OnWarning(wtMajor, 'VRML/X3D', 'IntegerSequencer node: key and keyValue fields should have the same size');
if ValueIndex < FdKeyValue.Count then
EventValue_Changed.Send(FdKeyValue.Items.Items[ValueIndex], Time);
end;
procedure TIntegerTriggerNode.CreateNode;
begin
inherited;
FEventSet_boolean := TX3DEvent.Create(Self, 'set_boolean', TSFBool, true);
Events.Add(FEventSet_boolean);
FEventSet_boolean.OnReceive.Add(@EventSet_booleanReceive);
{ X3D specification mistakenly omitted default value for this field...
I guess it's 0? }
FFdIntegerKey := TSFInt32.Create(Self, 'integerKey', 0);
Fields.Add(FFdIntegerKey);
{ X3D specification comment: (-Inf,Inf) }
FEventTriggerValue := TX3DEvent.Create(Self, 'triggerValue', TSFInt32, false);
Events.Add(FEventTriggerValue);
DefaultContainerField := 'children';
end;
procedure TIntegerTriggerNode.EventSet_BooleanReceive(
Event: TX3DEvent; Value: TX3DField; const Time: TX3DTime);
begin
EventtriggerValue.Send(FdintegerKey.Value, Time);
end;
class function TIntegerTriggerNode.ClassNodeTypeName: string;
begin
Result := 'IntegerTrigger';
end;
class function TIntegerTriggerNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
procedure TTimeTriggerNode.CreateNode;
begin
inherited;
FEventSet_boolean := TX3DEvent.Create(Self, 'set_boolean', TSFBool, true);
Events.Add(FEventSet_boolean);
FEventSet_boolean.OnReceive.Add(@EventSet_BooleanReceive);
FEventTriggerTime := TX3DEvent.Create(Self, 'triggerTime', TSFTime, false);
Events.Add(FEventTriggerTime);
DefaultContainerField := 'children';
end;
class function TTimeTriggerNode.ClassNodeTypeName: string;
begin
Result := 'TimeTrigger';
end;
class function TTimeTriggerNode.URNMatching(const URN: string): boolean;
begin
Result := (inherited URNMatching(URN)) or
(URN = URNX3DNodes + ClassNodeTypeName);
end;
procedure TTimeTriggerNode.EventSet_BooleanReceive(
Event: TX3DEvent; Value: TX3DField; const Time: TX3DTime);
begin
EventTriggerTime.Send(Time.Seconds, Time);
end;
procedure RegisterEventUtilitiesNodes;
begin
NodesManager.RegisterNodeClasses([
TBooleanFilterNode,
TBooleanSequencerNode,
TBooleanToggleNode,
TBooleanTriggerNode,
TIntegerSequencerNode,
TIntegerTriggerNode,
TTimeTriggerNode
]);
end;
{$endif read_implementation}
|