Debian Codegraph @

src/scene/x3d/castlefields_x3dsinglefield_descendants.inc

raw
1 {%MainUnit x3dfields.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 { Single-value X3D fields. }
18 {$ifdef read_interface}
19
20 { VRML 1.0 (deprecated) field representing a bitmask,
21 where a number of "flags" can be "on" or "off".
22 @deprecated This is deprecated, as VRML 1.0 is an ancient format now. }
23 TSFBitMask = class(TX3DSingleField)
24 strict private
25 fAllString, fNoneString: String;
26 fFlagNames: TStringList;
27
28 { Value of this field, as a bit mask.
29 VRML 1.0 specification guarantees that SFBitMask has 32 or less flags.
30 Actually, defined field values have no more than 3 fields, and
31 VRML > 1.0 dropped SFBitMask entirely. So 32 is always enough. }
32 fFlags: set of 0..31;
33 function GetFlags(i: integer): boolean;
34 procedure SetFlags(i: integer; value: boolean);
35 function GetFlagNames(i: integer): String;
36 strict protected
37 procedure SaveToStreamValue(Writer: TX3DWriter); override;
38 public
39 { Value of this field. You can use Index from the range 0 .. FlagsCount - 1. }
40 property Flags[i: integer]:boolean read GetFlags write SetFlags;
41 function FlagsCount: integer;
42
43 { Names of the flags that can be "on" or "off".
44 Note that this cannot be initialized by CreateUndefined,
45 so you always must initialize this field using the @link(Create)
46 overload that takes AFlagNames parameter. }
47 property FlagNames[i: integer]:string read GetFlagNames;
48
49 { Special strings that will be understood by parser as ALL or NONE
50 bit values. AllString selects all flags, NoneString selects none.
51 AllString may be '' is there's no such string, NoneString
52 should never be '' (otherwise, user could not be able to specify
53 some SFBitMask values --- NoneString is the only way to specify 0).
54
55 There is usually little sense in using them like "ALL | something"
56 (because it means just "ALL") or "NONE | something" (because it means
57 just "something"). But it's allowed syntactically.
58
59 @groupBegin }
60 property AllString: String read fAllString;
61 property NoneString: String read fNoneString;
62 { @groupEnd }
63
64 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
65
66 { Are all flag values set to @true currently. }
67 function AreAllFlags(value: boolean): boolean;
68
69 { Constructor.
70
71 Remember that arrays AFlagNames and AFlags
72 (AFlags is initial value of Flags) must have equal length.
73 Eventually, AFlags may be longer (excessive items will be ignored). }
74 constructor Create(const AParentNode: TX3DFileItem;
75 const AExposed: boolean; const AName: String; const AFlagNames: array of string;
76 const ANoneString, AAllString: String; const AFlags: array of boolean);
77
78 destructor Destroy; override;
79
80 function Equals(SecondValue: TX3DField): boolean; override;
81
82 procedure Assign(Source: TPersistent); override;
83 procedure AssignValue(Source: TX3DField); override;
84
85 class function X3DType: String; override;
86 end;
87
88 { X3D field containing a single boolean value. }
89 TSFBool = class(TX3DSingleField)
90 strict protected
91 procedure SaveToStreamValue(Writer: TX3DWriter); override;
92 public
93 Value: boolean;
94 DefaultValue: boolean;
95 DefaultValueExists: boolean;
96
97 constructor Create(const AParentNode: TX3DFileItem;
98 const AExposed: boolean; const AName: String; const AValue: boolean);
99
100 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
101 function EqualsDefaultValue: boolean; override;
102 function Equals(SecondValue: TX3DField): boolean; override;
103 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
104
105 procedure Assign(Source: TPersistent); override;
106 procedure AssignValue(Source: TX3DField); override;
107 procedure AssignDefaultValueFromValue; override;
108 procedure UnassignDefaultValue; override;
109
110 class function X3DType: String; override;
111 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
112
113 procedure Send(const AValue: boolean; const PartialSend: TPartialSend = nil); overload;
114 end;
115
116 { Array of strings used with TSFStringEnum and TSFEnum.
117 Passing around them as just pointers to a constant array makes them fast
118 and avoids copying and wasting memory for each TSFStringEnum / TSFEnum
119 to keep the names list in some separate object. }
120 TConstantStringArray = array [0..MaxInt div SizeOf(Pointer) - 1] of String;
121 PConstantStringArray = ^TConstantStringArray;
122
123 { VRML 1.0 (deprecated) field representing an enumerated value,
124 which means that you choose one (named) value from a set of possible values.
125 @deprecated This is deprecated, as VRML 1.0 is an ancient format now. }
126 TSFEnum = class(TX3DSingleField)
127 strict private
128 FEnumNames: PConstantStringArray;
129 FEnumNamesCount: Cardinal;
130 function GetEnumNames(i: integer): String;
131 strict protected
132 procedure SaveToStreamValue(Writer: TX3DWriter); override;
133 public
134 { Value between 0 .. EnumNamesCount - 1. By default 0. }
135 Value: integer;
136
137 DefaultValue: integer;
138 DefaultValueExists: boolean;
139
140 constructor Create(const AParentNode: TX3DFileItem;
141 const AExposed: boolean; const AName: String;
142 const AEnumNames: PConstantStringArray; const AEnumNamesCount: Cardinal;
143 const AValue: integer);
144
145 { Names of the enumerated values.
146 Note that this cannot be initialized by CreateUndefined,
147 so you always must initialize this field using the @link(Create)
148 overload that takes AEnumNames parameter. }
149 property EnumNames[i: integer]:string read GetEnumNames;
150 property EnumNamesCount: Cardinal read FEnumNamesCount;
151 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
152
153 function EqualsDefaultValue: boolean; override;
154 function Equals(SecondValue: TX3DField): boolean; override;
155
156 procedure Assign(Source: TPersistent); override;
157 procedure AssignValue(Source: TX3DField); override;
158 procedure AssignDefaultValueFromValue; override;
159 procedure UnassignDefaultValue; override;
160
161 class function X3DType: String; override;
162
163 procedure Send(const AValue: Int32); overload;
164 end;
165
166 { X3D field containing a floating-point value with Single precision. }
167 TSFFloat = class(TX3DSingleField)
168 strict private
169 FMustBeNonnegative: boolean;
170 FValue: Single;
171 FAngle: boolean;
172 procedure SetValue(const AValue: Single);
173 procedure SetMustBeNonnegative(const ANewMustBeNonnegative: Boolean);
174 strict protected
175 procedure SaveToStreamValue(Writer: TX3DWriter); override;
176 public
177 DefaultValue: Single;
178 DefaultValueExists: boolean;
179
180 constructor Create(const AParentNode: TX3DFileItem;
181 const AExposed: boolean; const AName: String; const AValue: Single);
182
183 { Current field value. }
184 property Value: Single read FValue write SetValue;
185
186 { Set this to @true to request that only values >= 0 are valid.
187 Trying to set Value to something < 0 will make a warning
188 and fix it (for now -- by negating it, but don't depend on this "fix" algorithm).
189 Also when changing this to @true while current Value is invalid,
190 it will also be fixed and make a warning. }
191 property MustBeNonnegative: Boolean
192 read FMustBeNonnegative write SetMustBeNonnegative default false;
193
194 { Value represents an angle. When reading from X3D 3.3 file, we will
195 make sure it's expressed in radians, honoring optional "UNIT angle ..."
196 declaration in X3D file. }
197 property Angle: boolean read FAngle write FAngle default false;
198
199 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
200
201 function EqualsDefaultValue: boolean; override;
202 function Equals(SecondValue: TX3DField): boolean; override;
203 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
204
205 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
206 function CanAssignLerp: boolean; override;
207 procedure Assign(Source: TPersistent); override;
208 procedure AssignValue(Source: TX3DField); override;
209 procedure AssignDefaultValueFromValue; override;
210 procedure UnassignDefaultValue; override;
211
212 class function X3DType: String; override;
213 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
214
215 procedure Send(const AValue: Single); overload;
216 end;
217
218 { X3D field containing a floating-point value with Double precision. }
219 TSFDouble = class(TX3DSingleField)
220 strict private
221 FValue: Double;
222 FAngle: boolean;
223 procedure SetValue(const AValue: Double);
224 strict protected
225 procedure SaveToStreamValue(Writer: TX3DWriter); override;
226 public
227 DefaultValue: Double;
228 DefaultValueExists: boolean;
229
230 constructor Create(const AParentNode: TX3DFileItem;
231 const AExposed: boolean; const AName: String; const AValue: Double);
232
233 property Value: Double read FValue write SetValue;
234
235 { Value represents an angle. When reading from X3D 3.3 file, we will
236 make sure it's expressed in radians, honoring optional "UNIT angle ..."
237 declaration in X3D file. }
238 property Angle: boolean read FAngle write FAngle default false;
239
240 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
241
242 function EqualsDefaultValue: boolean; override;
243 function Equals(SecondValue: TX3DField): boolean; override;
244 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
245
246 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
247 function CanAssignLerp: boolean; override;
248 procedure Assign(Source: TPersistent); override;
249 procedure AssignValue(Source: TX3DField); override;
250 procedure AssignDefaultValueFromValue; override;
251 procedure UnassignDefaultValue; override;
252
253 class function X3DType: String; override;
254 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
255
256 procedure Send(const AValue: Double); overload;
257 end;
258
259 { X3D field containing a time, expressed as seconds that passed since some moment. }
260 TSFTime = class(TSFDouble)
261 public
262 class function X3DType: String; override;
263 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
264 procedure Send(const AValue: Double); overload;
265 end;
266
267 { X3D field containing an image that is "embedded" in the X3D file. }
268 TSFImage = class(TX3DSingleField)
269 strict private
270 FValue: TCastleImage;
271 procedure SetValue(const AValue: TCastleImage);
272 strict protected
273 procedure SaveToStreamValue(Writer: TX3DWriter); override;
274 public
275 { Current image, expressed as the @link(TCastleImage) instance.
276
277 The image instance is by default owned by this object,
278 which means that we will free it in destructor or when setting
279 another value.
280
281 Value may be IsEmpty, and then we know that there is no image
282 recorded in this field. Value may never be @nil. }
283 property Value: TCastleImage read FValue write SetValue;
284
285 { @param(AValue is the initial value for Value.
286
287 Note - our constructor COPIES passed reference AValue, not it's contents
288 (I mean, we do Value := AValue, NOT Value := ImageCopy(AValue),
289 so don't Free image given to us (at least, don't do this without clearing
290 our Value field)).
291 You can pass AValue = nil, then Value will be initialized to null image
292 TRGBImage.Create.) }
293 constructor Create(const AParentNode: TX3DFileItem;
294 const AExposed: boolean; const AName: String; const AValue: TCastleImage);
295 constructor CreateUndefined(const AParentNode: TX3DFileItem;
296 const AExposed: boolean; const AName: String); override;
297
298 destructor Destroy; override;
299
300 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
301
302 function Equals(SecondValue: TX3DField): boolean; override;
303
304 procedure Assign(Source: TPersistent); override;
305 procedure AssignValue(Source: TX3DField); override;
306
307 class function X3DType: String; override;
308 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
309 end;
310
311 { X3D field containing an single integer 32-bit value. }
312 TSFLong = class(TX3DSingleField)
313 strict private
314 FMustBeNonnegative: boolean;
315 FValue: Int32;
316 procedure SetValue(const AValue: Int32);
317 procedure SetMustBeNonnegative(const ANewMustBeNonnegative: Boolean);
318 strict protected
319 procedure SaveToStreamValue(Writer: TX3DWriter); override;
320 public
321 DefaultValue: Int32;
322 DefaultValueExists: boolean;
323
324 constructor Create(const AParentNode: TX3DFileItem;
325 const AExposed: boolean; const AName: String; const AValue: Int32);
326
327 property Value: Int32 read FValue write SetValue;
328
329 { Set this to @true to request that only values >= 0 are valid.
330 Trying to set Value to something < 0 will make a warning
331 and fix it (for now -- by negating it, but don't depend on this "fix" algorithm).
332 Also when changing this to @true while current Value is invalid,
333 it will also be fixed and make a warning. }
334 property MustBeNonnegative: Boolean
335 read FMustBeNonnegative write SetMustBeNonnegative default false;
336
337 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
338
339 function EqualsDefaultValue: boolean; override;
340 function Equals(SecondValue: TX3DField): boolean; override;
341 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
342
343 procedure Assign(Source: TPersistent); override;
344 procedure AssignValue(Source: TX3DField); override;
345 procedure AssignDefaultValueFromValue; override;
346 procedure UnassignDefaultValue; override;
347
348 class function X3DType: String; override;
349 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
350
351 procedure Send(const AValue: Int32); overload; virtual;
352 end;
353
354 { X3D field containing an single integer 32-bit value.
355 The name SFInt32 is used instead of SFLong in new X3D versions. }
356 TSFInt32 = class(TSFLong)
357 public
358 class function X3DType: String; override;
359 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
360 procedure Send(const AValue: Int32); override;
361 end;
362
363 {$ifdef FPC}
364 { Generic class for an X3D field containing a single matrix value. }
365 {$ifdef FPC}generic{$endif}
366 TSFGenericMatrix<
367 TItem,
368 TItemColumn,
369 TEvent> = class(TX3DSingleField)
370 strict private
371 FValue: TItem;
372 DefaultValue: TItem;
373 DefaultValueExists: boolean;
374 class function MatrixSize: Integer;
375 strict protected
376 procedure SaveToStreamValue(Writer: TX3DWriter); override;
377 public
378 constructor Create(const AParentNode: TX3DFileItem;
379 const AExposed: boolean; const AName: String; const AValue: TItem);
380
381 property Value: TItem read FValue write FValue;
382
383 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
384
385 function EqualsDefaultValue: boolean; override;
386 function Equals(SecondValue: TX3DField): boolean; override;
387 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
388
389 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
390 function CanAssignLerp: boolean; override;
391 procedure Assign(Source: TPersistent); override;
392 procedure AssignValue(Source: TX3DField); override;
393 procedure AssignDefaultValueFromValue; override;
394 procedure UnassignDefaultValue; override;
395 procedure Send(const AValue: TItem); overload;
396
397 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
398 end;
399 {$endif}
400
401 { X3D field containing a 3x3 matrix with Single precision. }
402 TSFMatrix3f = class({$ifdef FPC}{$ifdef FPC}specialize{$endif} TSFGenericMatrix<
403 TMatrix3,
404 TVector3,
405 TSFMatrix3fEvent>{$else}TX3DSingleField{$endif})
406 {$ifndef FPC}
407 strict private
408 FValue: TMatrix3;
409 DefaultValue: TMatrix3;
410 DefaultValueExists: boolean;
411 class function MatrixSize: Integer;
412 strict protected
413 procedure SaveToStreamValue(Writer: TX3DWriter); override;
414 {$endif}
415 public
416 {$ifndef FPC}
417 constructor Create(const AParentNode: TX3DFileItem;
418 const AExposed: boolean; const AName: String; const AValue: TMatrix3);
419
420 property Value: TMatrix3 read FValue write FValue;
421
422 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
423
424 function EqualsDefaultValue: boolean; override;
425 function Equals(SecondValue: TX3DField): boolean; override;
426 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
427
428 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
429 function CanAssignLerp: boolean; override;
430 procedure Assign(Source: TPersistent); override;
431 procedure AssignValue(Source: TX3DField); override;
432 procedure AssignDefaultValueFromValue; override;
433 procedure UnassignDefaultValue; override;
434 procedure Send(const AValue: TMatrix3); overload;
435
436 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
437 {$endif}
438 class function X3DType: String; override;
439 end;
440
441 { X3D field containing a 3x3 matrix with Double precision. }
442 TSFMatrix3d = class({$ifdef FPC}{$ifdef FPC}specialize{$endif} TSFGenericMatrix<
443 TMatrix3Double,
444 TVector3Double,
445 TSFMatrix3dEvent>{$else}TX3DSingleField{$endif})
446 {$ifndef FPC}
447 strict private
448 FValue: TMatrix3Double;
449 DefaultValue: TMatrix3Double;
450 DefaultValueExists: boolean;
451 class function MatrixSize: Integer;
452 strict protected
453 procedure SaveToStreamValue(Writer: TX3DWriter); override;
454 {$endif}
455 public
456 {$ifndef FPC}
457 constructor Create(const AParentNode: TX3DFileItem;
458 const AExposed: boolean; const AName: String; const AValue: TMatrix3Double);
459
460 property Value: TMatrix3Double read FValue write FValue;
461
462 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
463
464 function EqualsDefaultValue: boolean; override;
465 function Equals(SecondValue: TX3DField): boolean; override;
466 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
467
468 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
469 function CanAssignLerp: boolean; override;
470 procedure Assign(Source: TPersistent); override;
471 procedure AssignValue(Source: TX3DField); override;
472 procedure AssignDefaultValueFromValue; override;
473 procedure UnassignDefaultValue; override;
474 procedure Send(const AValue: TMatrix3Double); overload;
475
476 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
477 {$endif}
478 class function X3DType: String; override;
479 end;
480
481 { X3D field containing a 4x4 matrix with Single precision. }
482 TSFMatrix4f = class({$ifdef FPC}{$ifdef FPC}specialize{$endif} TSFGenericMatrix<
483 TMatrix4,
484 TVector4,
485 TSFMatrix4fEvent>{$else}TX3DSingleField{$endif})
486 {$ifndef FPC}
487 strict private
488 FValue: TMatrix4;
489 DefaultValue: TMatrix4;
490 DefaultValueExists: boolean;
491 class function MatrixSize: Integer;
492 strict protected
493 procedure SaveToStreamValue(Writer: TX3DWriter); override;
494 {$endif}
495 public
496 {$ifndef FPC}
497 constructor Create(const AParentNode: TX3DFileItem;
498 const AExposed: boolean; const AName: String; const AValue: TMatrix4);
499
500 property Value: TMatrix4 read FValue write FValue;
501
502 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
503
504 function EqualsDefaultValue: boolean; override;
505 function Equals(SecondValue: TX3DField): boolean; override;
506 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
507
508 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
509 function CanAssignLerp: boolean; override;
510 procedure Assign(Source: TPersistent); override;
511 procedure AssignValue(Source: TX3DField); override;
512 procedure AssignDefaultValueFromValue; override;
513 procedure UnassignDefaultValue; override;
514 procedure Send(const AValue: TMatrix4); overload;
515
516 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
517 {$endif}
518 class function X3DType: String; override;
519 end;
520
521 { X3D field containing a 4x4 matrix with Double precision. }
522 TSFMatrix4d = class({$ifdef FPC}{$ifdef FPC}specialize{$endif} TSFGenericMatrix<
523 TMatrix4Double,
524 TVector4Double,
525 TSFMatrix4dEvent>{$else}TX3DSingleField{$endif})
526 {$ifndef FPC}
527 strict private
528 FValue: TMatrix4Double;
529 DefaultValue: TMatrix4Double;
530 DefaultValueExists: boolean;
531 class function MatrixSize: Integer;
532 strict protected
533 procedure SaveToStreamValue(Writer: TX3DWriter); override;
534 {$endif}
535 public
536 {$ifndef FPC}
537 constructor Create(const AParentNode: TX3DFileItem;
538 const AExposed: boolean; const AName: String; const AValue: TMatrix4Double);
539
540 property Value: TMatrix4Double read FValue write FValue;
541
542 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
543
544 function EqualsDefaultValue: boolean; override;
545 function Equals(SecondValue: TX3DField): boolean; override;
546 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
547
548 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
549 function CanAssignLerp: boolean; override;
550 procedure Assign(Source: TPersistent); override;
551 procedure AssignValue(Source: TX3DField); override;
552 procedure AssignDefaultValueFromValue; override;
553 procedure UnassignDefaultValue; override;
554 procedure Send(const AValue: TMatrix4Double); overload;
555
556 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
557 {$endif}
558 class function X3DType: String; override;
559 end;
560
561 { VRML 1.0 SFMatrix field.
562 @deprecated This is deprecated, as VRML 1.0 is an ancient format now. }
563 TSFMatrix = class(TSFMatrix4f)
564 public
565 class function X3DType: String; override;
566 end;
567
568 { X3D field representing a 3D rotation around an arbitrary axis. }
569 TSFRotation = class(TX3DSingleField)
570 strict private
571 DefaultAxis: TVector3;
572 DefaultRotationRad: Single;
573 DefaultValueExists: boolean;
574 strict protected
575 procedure SaveToStreamValue(Writer: TX3DWriter); override;
576 function GetValue: TVector4;
577 procedure SetValue(const AValue: TVector4);
578 function GetValueDeg: TVector4;
579 procedure SetValueDeg(const AValue: TVector4);
580 public
581 Axis: TVector3;
582 RotationRad: Single;
583
584 constructor Create(const AParentNode: TX3DFileItem;
585 const AExposed: boolean; const AName: String;
586 const AnAxis: TVector3; const ARotationRad: Single); overload;
587 constructor Create(const AParentNode: TX3DFileItem;
588 const AExposed: boolean; const AName: String;
589 const AValue: TVector4); overload;
590
591 { Current rotation value, with last component expressing rotation in radians.
592
593 This internally gets / sets values from @link(Axis), @link(RotationRad),
594 it only presents them to you differently. }
595 property Value: TVector4 read GetValue write SetValue;
596
597 { Current rotation value, with last component expressing rotation in degrees.
598
599 So this is just like @link(Value), but last component is in degrees.
600 This internally gets / sets values from @link(Axis), @link(RotationRad),
601 it only presents them to you differently. }
602 property ValueDeg: TVector4 read GetValueDeg write SetValueDeg;
603
604 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
605 { Rotate point Pt around Self. }
606 function RotatedPoint(const pt: TVector3): TVector3;
607
608 function Equals(SecondValue: TX3DField): boolean; override;
609 function EqualsDefaultValue: boolean; override;
610 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
611
612 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
613 function CanAssignLerp: boolean; override;
614 procedure Assign(Source: TPersistent); override;
615 procedure AssignValue(Source: TX3DField); override;
616 procedure AssignDefaultValueFromValue; override;
617 procedure UnassignDefaultValue; override;
618
619 class function X3DType: String; override;
620 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
621
622 procedure Send(const AValue: TVector4); overload;
623 end;
624
625 { X3D field containing a single string. }
626 TSFString = class(TX3DSingleField)
627 strict private
628 FValue: String;
629 FDefaultValue: String;
630 FDefaultValueExists: boolean;
631 strict protected
632 procedure SetValue(const NewValue: String); virtual;
633 procedure SetDefaultValue(const NewDefaultValue: String); virtual;
634 procedure SaveToStreamValue(Writer: TX3DWriter); override;
635 public
636 property DefaultValue: String read FDefaultValue write SetDefaultValue;
637 property DefaultValueExists: boolean
638 read FDefaultValueExists write FDefaultValueExists;
639 property Value: String read FValue write SetValue;
640
641 constructor Create(const AParentNode: TX3DFileItem;
642 const AExposed: boolean; const AName: String; const AValue: String);
643
644 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
645
646 function EqualsDefaultValue: boolean; override;
647 function Equals(SecondValue: TX3DField): boolean; override;
648 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
649
650 procedure Assign(Source: TPersistent); override;
651 procedure AssignValue(Source: TX3DField); override;
652 procedure AssignDefaultValueFromValue; override;
653 procedure UnassignDefaultValue; override;
654
655 class function X3DType: String; override;
656 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
657
658 procedure ParseXMLAttribute(const AttributeValue: String; Reader: TX3DReader); override;
659 function SaveToXmlValue: TSaveToXmlMethod; override;
660
661 procedure Send(const AValue: String); overload;
662 end;
663
664 { X3D field that contains a value from a specified set.
665 This represents a commonly used VRML/X3D construct where SFString field
666 is used to hold values from some limited set, thus emulating
667 an "enumerated" field.
668
669 Access the EnumValue to get / set the field value as an integer,
670 which is an index to ValueNames array. }
671 TSFStringEnum = class(TSFString)
672 strict private
673 FEnumNames: PConstantStringArray;
674 FEnumNamesCount: Cardinal;
675 FEnumValue: Integer;
676 FDefaultEnumValue: Integer;
677 AllNamesUpperCase: Boolean;
678 procedure SetEnumValue(const NewEnumValue: Integer);
679 procedure SetDefaultEnumValue(const NewDefaultEnumValue: Integer);
680 strict protected
681 function StringToEnumValue(const NewValue: String): Integer;
682 procedure SetValue(const NewValue: String); override;
683 procedure SetDefaultValue(const NewDefaultValue: String); override;
684 class function ExposedEventsFieldClass: TX3DFieldClass; override;
685 public
686 constructor Create(const AParentNode: TX3DFileItem;
687 const AExposed: boolean; const AName: String;
688 const AEnumNames: PConstantStringArray; const AEnumNamesCount: Cardinal;
689 const AValue: Integer);
690 property EnumValue: Integer
691 read FEnumValue write SetEnumValue;
692 property DefaultEnumValue: Integer
693 read FDefaultEnumValue write SetDefaultEnumValue;
694 procedure SendEnumValue(const NewValue: Integer);
695 end;
696
697 {$ifdef FPC}
698 { X3D field containing a vector. }
699 {$ifdef FPC}generic{$endif}
700 TSFGenericVector<
701 TItem,
702 TEvent> = class(TX3DSingleField)
703 strict protected
704 procedure SaveToStreamValue(Writer: TX3DWriter); override;
705 public
706 Value: TItem;
707
708 DefaultValue: TItem;
709 DefaultValueExists: boolean;
710
711 constructor Create(const AParentNode: TX3DFileItem;
712 const AExposed: boolean; const AName: String; const AValue: TItem);
713
714 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
715
716 function EqualsDefaultValue: boolean; override;
717 function Equals(SecondValue: TX3DField): boolean; override;
718 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
719
720 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
721 function CanAssignLerp: boolean; override;
722 procedure Assign(Source: TPersistent); override;
723 procedure AssignValue(Source: TX3DField); override;
724 procedure AssignDefaultValueFromValue; override;
725 procedure UnassignDefaultValue; override;
726 procedure Send(const AValue: TItem); overload;
727
728 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
729 end;
730 {$endif FPC}
731
732 { X3D field containing a 2D vector with Single precision. }
733 TSFVec2f = class({$ifdef FPC}{$ifdef FPC}specialize{$endif} TSFGenericVector<
734 TVector2,
735 TSFVec2fEvent>{$else}TX3DSingleField{$endif})
736 {$ifndef FPC}
737 strict protected
738 procedure SaveToStreamValue(Writer: TX3DWriter); override;
739 {$endif}
740 public
741 {$ifndef FPC}
742 Value: TVector2;
743
744 DefaultValue: TVector2;
745 DefaultValueExists: boolean;
746
747 constructor Create(const AParentNode: TX3DFileItem;
748 const AExposed: boolean; const AName: String; const AValue: TVector2);
749
750 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
751
752 function EqualsDefaultValue: boolean; override;
753 function Equals(SecondValue: TX3DField): boolean; override;
754 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
755
756 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
757 function CanAssignLerp: boolean; override;
758 procedure Assign(Source: TPersistent); override;
759 procedure AssignValue(Source: TX3DField); override;
760 procedure AssignDefaultValueFromValue; override;
761 procedure UnassignDefaultValue; override;
762 procedure Send(const AValue: TVector2); overload;
763
764 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
765 {$endif}
766 class function X3DType: String; override;
767 end;
768
769 { X3D field containing a 3D vector with Single precision. }
770 TSFVec3f = class({$ifdef FPC}{$ifdef FPC}specialize{$endif} TSFGenericVector<
771 TVector3,
772 TSFVec3fEvent>{$else}TX3DSingleField{$endif})
773 {$ifndef FPC}
774 strict protected
775 procedure SaveToStreamValue(Writer: TX3DWriter); override;
776 {$endif}
777 public
778 {$ifndef FPC}
779 Value: TVector3;
780
781 DefaultValue: TVector3;
782 DefaultValueExists: boolean;
783
784 constructor Create(const AParentNode: TX3DFileItem;
785 const AExposed: boolean; const AName: String; const AValue: TVector3);
786
787 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
788
789 function EqualsDefaultValue: boolean; override;
790 function Equals(SecondValue: TX3DField): boolean; override;
791 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
792
793 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
794 function CanAssignLerp: boolean; override;
795 procedure Assign(Source: TPersistent); override;
796 procedure AssignValue(Source: TX3DField); override;
797 procedure AssignDefaultValueFromValue; override;
798 procedure UnassignDefaultValue; override;
799 procedure Send(const AValue: TVector3); overload;
800
801 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
802 {$endif}
803 class function X3DType: String; override;
804 { Alternative version of @name, change only a given component of the vector. }
805 procedure Send(const Index: Integer; const ComponentValue: Single); overload;
806 end;
807
808 { X3D field containing an RGB color. }
809 TSFColor = class(TSFVec3f)
810 public
811 class function X3DType: String; override;
812 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
813 end;
814
815 { X3D field containing a 4D vector with Single precision. }
816 TSFVec4f = class({$ifdef FPC}{$ifdef FPC}specialize{$endif} TSFGenericVector<
817 TVector4,
818 TSFVec4fEvent>{$else}TX3DSingleField{$endif})
819 {$ifndef FPC}
820 strict protected
821 procedure SaveToStreamValue(Writer: TX3DWriter); override;
822 {$endif}
823 public
824 {$ifndef FPC}
825 Value: TVector4;
826
827 DefaultValue: TVector4;
828 DefaultValueExists: boolean;
829
830 constructor Create(const AParentNode: TX3DFileItem;
831 const AExposed: boolean; const AName: String; const AValue: TVector4);
832
833 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
834
835 function EqualsDefaultValue: boolean; override;
836 function Equals(SecondValue: TX3DField): boolean; override;
837 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
838
839 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
840 function CanAssignLerp: boolean; override;
841 procedure Assign(Source: TPersistent); override;
842 procedure AssignValue(Source: TX3DField); override;
843 procedure AssignDefaultValueFromValue; override;
844 procedure UnassignDefaultValue; override;
845 procedure Send(const AValue: TVector4); overload;
846
847 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
848 {$endif}
849 class function X3DType: String; override;
850 end;
851
852 { X3D field containing an RGBA color (with an alpha). }
853 TSFColorRGBA = class(TSFVec4f)
854 public
855 class function X3DType: String; override;
856 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
857 end;
858
859 { X3D field containing a 2D vector with Double precision. }
860 TSFVec2d = class({$ifdef FPC}{$ifdef FPC}specialize{$endif} TSFGenericVector<
861 TVector2Double,
862 TSFVec2dEvent>{$else}TX3DSingleField{$endif})
863 {$ifndef FPC}
864 strict protected
865 procedure SaveToStreamValue(Writer: TX3DWriter); override;
866 {$endif}
867 public
868 {$ifndef FPC}
869 Value: TVector2Double;
870
871 DefaultValue: TVector2Double;
872 DefaultValueExists: boolean;
873
874 constructor Create(const AParentNode: TX3DFileItem;
875 const AExposed: boolean; const AName: String; const AValue: TVector2Double);
876
877 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
878
879 function EqualsDefaultValue: boolean; override;
880 function Equals(SecondValue: TX3DField): boolean; override;
881 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
882
883 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
884 function CanAssignLerp: boolean; override;
885 procedure Assign(Source: TPersistent); override;
886 procedure AssignValue(Source: TX3DField); override;
887 procedure AssignDefaultValueFromValue; override;
888 procedure UnassignDefaultValue; override;
889 procedure Send(const AValue: TVector2Double); overload;
890
891 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
892 {$endif}
893 class function X3DType: String; override;
894 end;
895
896 { X3D field containing a 3D vector with Double precision. }
897 TSFVec3d = class({$ifdef FPC}{$ifdef FPC}specialize{$endif} TSFGenericVector<
898 TVector3Double,
899 TSFVec3dEvent>{$else}TX3DSingleField{$endif})
900 {$ifndef FPC}
901 strict protected
902 procedure SaveToStreamValue(Writer: TX3DWriter); override;
903 {$endif}
904 public
905 {$ifndef FPC}
906 Value: TVector3Double;
907
908 DefaultValue: TVector3Double;
909 DefaultValueExists: boolean;
910
911 constructor Create(const AParentNode: TX3DFileItem;
912 const AExposed: boolean; const AName: String; const AValue: TVector3Double);
913
914 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
915
916 function EqualsDefaultValue: boolean; override;
917 function Equals(SecondValue: TX3DField): boolean; override;
918 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
919
920 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
921 function CanAssignLerp: boolean; override;
922 procedure Assign(Source: TPersistent); override;
923 procedure AssignValue(Source: TX3DField); override;
924 procedure AssignDefaultValueFromValue; override;
925 procedure UnassignDefaultValue; override;
926 procedure Send(const AValue: TVector3Double); overload;
927
928 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
929 {$endif}
930 class function X3DType: String; override;
931 end;
932
933 { X3D field containing a 4D vector with Double precision. }
934 TSFVec4d = class({$ifdef FPC}{$ifdef FPC}specialize{$endif} TSFGenericVector<
935 TVector4Double,
936 TSFVec4dEvent>{$else}TX3DSingleField{$endif})
937 {$ifndef FPC}
938 strict protected
939 procedure SaveToStreamValue(Writer: TX3DWriter); override;
940 {$endif}
941 public
942 {$ifndef FPC}
943 Value: TVector4Double;
944
945 DefaultValue: TVector4Double;
946 DefaultValueExists: boolean;
947
948 constructor Create(const AParentNode: TX3DFileItem;
949 const AExposed: boolean; const AName: String; const AValue: TVector4Double);
950
951 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
952
953 function EqualsDefaultValue: boolean; override;
954 function Equals(SecondValue: TX3DField): boolean; override;
955 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
956
957 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
958 function CanAssignLerp: boolean; override;
959 procedure Assign(Source: TPersistent); override;
960 procedure AssignValue(Source: TX3DField); override;
961 procedure AssignDefaultValueFromValue; override;
962 procedure UnassignDefaultValue; override;
963 procedure Send(const AValue: TVector4Double); overload;
964
965 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
966 {$endif}
967 class function X3DType: String; override;
968 end;
969
970 {$endif read_interface}
971
972 {$ifdef read_implementation}
973
974 { TSFBool -------------------------------------------------------------------- }
975
976 constructor TSFBool.Create(const AParentNode: TX3DFileItem;
977 const AExposed: boolean; const AName: String; const AValue: boolean);
978 begin
979 CreateUndefined(AParentNode, AExposed, AName);
980
981 Value := AValue;
982 AssignDefaultValueFromValue;
983 end;
984
985 procedure TSFBool.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
986
987 procedure VRML2BooleanIntegerWarning;
988 begin
989 if Lexer.Version.Major >= 2 then
990 WritelnWarning('X3D', 'In X3D and VRML >= 2.0 you cannot express boolean values as 0 (instead of FALSE) or 1 (instead of TRUE)');
991 end;
992
993 const
994 SBoolExpected = 'boolean constant (TRUE, FALSE)';
995 begin
996 Lexer.CheckTokenIs([vtKeyword, vtInteger], SBoolExpected);
997 if Lexer.Token = vtKeyword then
998 begin
999 if Lexer.TokenKeyword = vkTrue then Value := true else
1000 if Lexer.TokenKeyword = vkFalse then Value := false else
1001 raise EX3DParserError.Create(Lexer,
1002 'Expected '+SBoolExpected+', got '+Lexer.DescribeToken);
1003 end else
1004 begin
1005 if Lexer.TokenInteger = 1 then
1006 begin
1007 Value := true;
1008 VRML2BooleanIntegerWarning;
1009 end else
1010 if Lexer.TokenInteger = 0 then
1011 begin
1012 Value := false;
1013 VRML2BooleanIntegerWarning;
1014 end else
1015 raise EX3DParserError.Create(Lexer,
1016 'Expected '+SBoolExpected+', got '+Lexer.DescribeToken);
1017 end;
1018 Lexer.NextToken;
1019 end;
1020
1021 const
1022 BoolKeywords: array [TX3DEncoding, boolean] of string =
1023 ( ('FALSE', 'TRUE'), ('false', 'true') );
1024
1025 procedure TSFBool.SaveToStreamValue(Writer: TX3DWriter);
1026 begin
1027 Writer.Write(BoolKeywords[Writer.Encoding, Value]);
1028 end;
1029
1030 function TSFBool.EqualsDefaultValue: boolean;
1031 begin
1032 result := DefaultValueExists and (DefaultValue = Value);
1033 end;
1034
1035 function TSFBool.Equals(SecondValue: TX3DField): boolean;
1036 begin
1037 Result := (inherited Equals(SecondValue)) and
1038 (SecondValue is TSFBool) and
1039 (TSFBool(SecondValue).Value = Value);
1040 end;
1041
1042 function TSFBool.FastEqualsValue(SecondValue: TX3DField): boolean;
1043 begin
1044 Result := (SecondValue is TSFBool) and
1045 (TSFBool(SecondValue).Value = Value);
1046 end;
1047
1048 procedure TSFBool.Assign(Source: TPersistent);
1049 begin
1050 if Source is TSFBool then
1051 begin
1052 DefaultValue := TSFBool(Source).DefaultValue;
1053 DefaultValueExists := TSFBool(Source).DefaultValueExists;
1054 Value := TSFBool(Source).Value;
1055 VRMLFieldAssignCommon(TX3DField(Source));
1056 end else
1057 inherited;
1058 end;
1059
1060 procedure TSFBool.AssignValue(Source: TX3DField);
1061 begin
1062 if Source is TSFBool then
1063 begin
1064 inherited;
1065 Value := TSFBool(Source).Value;
1066 end else
1067 AssignValueRaiseInvalidClass(Source);
1068 end;
1069
1070 procedure TSFBool.AssignDefaultValueFromValue;
1071 begin
1072 inherited;
1073 DefaultValue := Value;
1074 DefaultValueExists := true;
1075 end;
1076
1077 procedure TSFBool.UnassignDefaultValue;
1078 begin
1079 DefaultValueExists := false;
1080 end;
1081
1082 class function TSFBool.X3DType: String;
1083 begin
1084 Result := 'SFBool';
1085 end;
1086
1087 procedure TSFBool.Send(const AValue: Boolean; const PartialSend: TPartialSend);
1088 var
1089 FieldValue: TX3DField;
1090 begin
1091 FieldValue := TSFBool.Create(ParentNode, false, X3DName, AValue);
1092 try
1093 FieldValue.InternalPartialSend := PartialSend;
1094 Send(FieldValue);
1095 finally FreeAndNil(FieldValue) end;
1096 end;
1097
1098 class function TSFBool.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
1099 begin
1100 Result := TSFBoolEvent.Create(AParentNode, AName, AInEvent);
1101 end;
1102
1103 { TSFFloat ------------------------------------------------------------------- }
1104
1105 constructor TSFFloat.Create(const AParentNode: TX3DFileItem;
1106 const AExposed: boolean; const AName: String; const AValue: Single);
1107 begin
1108 CreateUndefined(AParentNode, AExposed, AName);
1109 FValue := AValue;
1110 AssignDefaultValueFromValue;
1111 end;
1112
1113 procedure TSFFloat.SetValue(const AValue: Single);
1114 begin
1115 if MustBeNonnegative and (AValue < 0) then
1116 begin
1117 WritelnWarning('Setting float field %s to value < 0 (given: %f) is invalid', [
1118 NiceName,
1119 AValue
1120 ]);
1121 FValue := -AValue;
1122 Assert(Value >= 0);
1123 end else
1124 FValue := AValue;
1125 end;
1126
1127 procedure TSFFloat.SetMustBeNonnegative(const ANewMustBeNonnegative: Boolean);
1128 begin
1129 if FMustBeNonnegative <> ANewMustBeNonnegative then
1130 begin
1131 FMustBeNonnegative := ANewMustBeNonnegative;
1132 if FMustBeNonnegative and (Value < 0) then
1133 begin
1134 WritelnWarning('Float field %s cannot have value < 0 (has: %f), fixing', [
1135 NiceName,
1136 Value
1137 ]);
1138 FValue := -FValue;
1139 Assert(Value >= 0);
1140 end;
1141 end;
1142 end;
1143
1144 procedure TSFFloat.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
1145 begin
1146 Value := ParseFloat(Lexer);
1147 if Angle then
1148 FValue := FValue * Reader.AngleConversionFactor;
1149 end;
1150
1151 procedure TSFFloat.SaveToStreamValue(Writer: TX3DWriter);
1152 begin
1153 Writer.Write(FormatDot(FloatOutputFormat, [Value]));
1154 end;
1155
1156 function TSFFloat.EqualsDefaultValue: boolean;
1157 begin
1158 result := DefaultValueExists and (DefaultValue = Value)
1159 end;
1160
1161 function TSFFloat.Equals(SecondValue: TX3DField): boolean;
1162 begin
1163 Result := (inherited Equals(SecondValue)) and
1164 (SecondValue is TSFFloat) and
1165 (TSFFloat(SecondValue).MustBeNonnegative = MustBeNonnegative) and
1166 SameValue(TSFFloat(SecondValue).Value, Value);
1167 end;
1168
1169 function TSFFloat.FastEqualsValue(SecondValue: TX3DField): boolean;
1170 begin
1171 Result := (SecondValue is TSFFloat) and
1172 (TSFFloat(SecondValue).Value = Value);
1173 end;
1174
1175 procedure TSFFloat.AssignLerp(const A: Double; Value1, Value2: TX3DField);
1176 begin
1177 Value := Lerp(A, (Value1 as TSFFloat).Value, (Value2 as TSFFloat).Value);
1178 end;
1179
1180 function TSFFloat.CanAssignLerp: boolean;
1181 begin
1182 Result := true;
1183 end;
1184
1185 procedure TSFFloat.Assign(Source: TPersistent);
1186 begin
1187 if Source is TSFFloat then
1188 begin
1189 DefaultValue := TSFFloat(Source).DefaultValue;
1190 DefaultValueExists := TSFFloat(Source).DefaultValueExists;
1191 FValue := TSFFloat(Source).Value;
1192 FMustBeNonnegative := TSFFloat(Source).MustBeNonnegative;
1193 VRMLFieldAssignCommon(TX3DField(Source));
1194 end else
1195 inherited;
1196 end;
1197
1198 procedure TSFFloat.AssignValue(Source: TX3DField);
1199 begin
1200 if Source is TSFFloat then
1201 begin
1202 inherited;
1203 Value := TSFFloat(Source).Value;
1204 end else
1205 AssignValueRaiseInvalidClass(Source);
1206 end;
1207
1208 procedure TSFFloat.AssignDefaultValueFromValue;
1209 begin
1210 inherited;
1211 DefaultValue := Value;
1212 DefaultValueExists := true;
1213 end;
1214
1215 procedure TSFFloat.UnassignDefaultValue;
1216 begin
1217 DefaultValueExists := false;
1218 end;
1219
1220 class function TSFFloat.X3DType: String;
1221 begin
1222 Result := 'SFFloat';
1223 end;
1224
1225 procedure TSFFloat.Send(const AValue: Single);
1226 var
1227 FieldValue: TX3DField;
1228 begin
1229 FieldValue := TSFFloat.Create(ParentNode, false, X3DName, AValue);
1230 try
1231 Send(FieldValue);
1232 finally FreeAndNil(FieldValue) end;
1233 end;
1234
1235 class function TSFFloat.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
1236 begin
1237 Result := TSFFloatEvent.Create(AParentNode, AName, AInEvent);
1238 end;
1239
1240 { TSFDouble -------------------------------------------------------------------- }
1241
1242 constructor TSFDouble.Create(const AParentNode: TX3DFileItem;
1243 const AExposed: boolean; const AName: String; const AValue: Double);
1244 begin
1245 CreateUndefined(AParentNode, AExposed, AName);
1246
1247 Value := AValue;
1248 AssignDefaultValueFromValue;
1249 end;
1250
1251 procedure TSFDouble.SetValue(const AValue: Double);
1252 begin
1253 FValue := AValue;
1254 end;
1255
1256 procedure TSFDouble.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
1257 begin
1258 Value := ParseFloat(Lexer);
1259 if Angle then
1260 FValue := FValue * Reader.AngleConversionFactor;
1261 end;
1262
1263 procedure TSFDouble.SaveToStreamValue(Writer: TX3DWriter);
1264 begin
1265 Writer.Write(FormatDot(FloatOutputFormat, [Value]));
1266 end;
1267
1268 function TSFDouble.EqualsDefaultValue: boolean;
1269 begin
1270 Result := DefaultValueExists and (DefaultValue = Value);
1271 end;
1272
1273 function TSFDouble.Equals(SecondValue: TX3DField): boolean;
1274 begin
1275 Result := (inherited Equals(SecondValue)) and
1276 (SecondValue is TSFDouble) and
1277 SameValue(TSFDouble(SecondValue).Value, Value);
1278 end;
1279
1280 function TSFDouble.FastEqualsValue(SecondValue: TX3DField): boolean;
1281 begin
1282 Result := (SecondValue is TSFDouble) and
1283 (TSFDouble(SecondValue).Value = Value);
1284 end;
1285
1286 procedure TSFDouble.AssignLerp(const A: Double; Value1, Value2: TX3DField);
1287 begin
1288 Value := Lerp(A, (Value1 as TSFDouble).Value, (Value2 as TSFDouble).Value);
1289 end;
1290
1291 function TSFDouble.CanAssignLerp: boolean;
1292 begin
1293 Result := true;
1294 end;
1295
1296 procedure TSFDouble.Assign(Source: TPersistent);
1297 begin
1298 if Source is TSFDouble then
1299 begin
1300 DefaultValue := TSFDouble(Source).DefaultValue;
1301 DefaultValueExists := TSFDouble(Source).DefaultValueExists;
1302 FValue := TSFDouble(Source).Value;
1303 VRMLFieldAssignCommon(TX3DField(Source));
1304 end else
1305 inherited;
1306 end;
1307
1308 procedure TSFDouble.AssignValue(Source: TX3DField);
1309 begin
1310 if Source is TSFDouble then
1311 begin
1312 inherited;
1313 Value := TSFDouble(Source).Value;
1314 end else
1315 AssignValueRaiseInvalidClass(Source);
1316 end;
1317
1318 procedure TSFDouble.AssignDefaultValueFromValue;
1319 begin
1320 inherited;
1321 DefaultValue := Value;
1322 DefaultValueExists := true;
1323 end;
1324
1325 procedure TSFDouble.UnassignDefaultValue;
1326 begin
1327 DefaultValueExists := false;
1328 end;
1329
1330 class function TSFDouble.X3DType: String;
1331 begin
1332 Result := 'SFDouble';
1333 end;
1334
1335 procedure TSFDouble.Send(const AValue: Double);
1336 var
1337 FieldValue: TX3DField;
1338 begin
1339 FieldValue := TSFDouble.Create(ParentNode, false, X3DName, AValue);
1340 try
1341 Send(FieldValue);
1342 finally FreeAndNil(FieldValue) end;
1343 end;
1344
1345 class function TSFDouble.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
1346 begin
1347 Result := TSFDoubleEvent.Create(AParentNode, AName, AInEvent);
1348 end;
1349
1350 { TSFTime -------------------------------------------------------------------- }
1351
1352 class function TSFTime.X3DType: String;
1353 begin
1354 Result := 'SFTime';
1355 end;
1356
1357 class function TSFTime.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
1358 begin
1359 Result := TSFTimeEvent.Create(AParentNode, AName, AInEvent);
1360 end;
1361
1362 procedure TSFTime.Send(const AValue: Double);
1363 var
1364 FieldValue: TX3DField;
1365 begin
1366 FieldValue := TSFTime.Create(ParentNode, false, X3DName, AValue);
1367 try
1368 Send(FieldValue);
1369 finally FreeAndNil(FieldValue) end;
1370 end;
1371
1372 { TSFImage ------------------------------------------------------------------- }
1373
1374 constructor TSFImage.Create(const AParentNode: TX3DFileItem;
1375 const AExposed: boolean; const AName: String; const AValue: TCastleImage);
1376 begin
1377 CreateUndefined(AParentNode, AExposed, AName);
1378
1379 if AValue <> nil then
1380 { use SetValue setter, to free previous value if needed }
1381 Value := AValue;
1382 end;
1383
1384 constructor TSFImage.CreateUndefined(const AParentNode: TX3DFileItem;
1385 const AExposed: boolean; const AName: String);
1386 begin
1387 inherited;
1388
1389 { Value must be initialized to non-nil. }
1390 FValue := TRGBImage.Create;
1391 end;
1392
1393 destructor TSFImage.Destroy;
1394 begin
1395 FreeAndNil(FValue);
1396 inherited;
1397 end;
1398
1399 procedure TSFImage.SetValue(const AValue: TCastleImage);
1400 begin
1401 if FValue <> AValue then
1402 begin
1403 FreeAndNil(FValue);
1404 FValue := AValue;
1405 end;
1406 end;
1407
1408 procedure TSFImage.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
1409 var
1410 w, h, comp: UInt32;
1411 i: Cardinal;
1412 RGBPixels: PVector3ByteArray;
1413 RGBAlphaPixels: PVector4ByteArray;
1414 GrayscalePixels: PByteArray;
1415 GrayscaleAlphaPixels: PVector2ByteArray;
1416 begin
1417 { Note that we should never let Value to be nil too long,
1418 because even if this method exits with exception, Value should
1419 always remain non-nil.
1420 That's why I'm doing below Value.Empty instead of FreeAndNil(Value).
1421 This way if e.g. TRGBImage.Create will raise "out of memory" exception,
1422 Value will still remain non-nil.
1423 }
1424
1425 Value.Empty;
1426
1427 w := ParseUInt32(Lexer);
1428 h := ParseUInt32(Lexer);
1429 comp := ParseUInt32(Lexer);
1430
1431 { If w or h =0 then w*h = 0 so we don't have to read anything more.
1432 We leave Value.IsEmpty in this case. }
1433 if (w <> 0) and (h <> 0) then
1434 begin
1435 case comp of
1436 1:begin
1437 Value := TGrayscaleImage.Create(w, h);
1438 GrayscalePixels := TGrayscaleImage(Value).PixelsArray;
1439 for i := 0 to W * H - 1 do
1440 DecodeX3DColor(ParseUInt32(Lexer), GrayscalePixels^[I]);
1441 end;
1442 2:begin
1443 Value := TGrayscaleAlphaImage.Create(w, h);
1444 GrayscaleAlphaPixels := TGrayscaleAlphaImage(Value).PixelsArray;
1445 for i := 0 to W * H - 1 do
1446 DecodeX3DColor(ParseUInt32(Lexer), GrayscaleAlphaPixels^[i]);
1447 end;
1448 3:begin
1449 Value := TRGBImage.Create(w, h);
1450 RGBPixels := TRGBImage(Value).PixelsArray;
1451 for i := 0 to W * H - 1 do
1452 DecodeX3DColor(ParseUInt32(Lexer), RGBPixels^[i]);
1453 end;
1454 4:begin
1455 Value := TRGBAlphaImage.Create(w, h);
1456 RGBAlphaPixels := TRGBAlphaImage(Value).PixelsArray;
1457 for i := 0 to W * H - 1 do
1458 DecodeX3DColor(ParseUInt32(Lexer), RGBAlphaPixels^[i]);
1459 end;
1460 else raise EX3DParserError.Create(Lexer, Format('Invalid components count'+
1461 ' for SFImage : is %d, should be 1, 2, 3 or 4.',[comp]));
1462 end;
1463 end;
1464 end;
1465
1466 procedure TSFImage.SaveToStreamValue(Writer: TX3DWriter);
1467 var
1468 ga: TVector2Byte;
1469 rgb: TVector3Byte;
1470 rgba: TVector4Byte;
1471 i: Cardinal;
1472 pixel: UInt32;
1473 begin
1474 if Value.IsEmpty then
1475 Writer.Write('0 0 1') else
1476 begin
1477 Writer.Writeln(FormatDot('%d %d %d', [
1478 Value.Width,
1479 Value.Height,
1480 Value.ColorComponentsCount]));
1481 Writer.IncIndent;
1482 Writer.WriteIndent('');
1483 {$I NoRQCheckBegin.inc}
1484 if Value is TGrayscaleImage then
1485 begin
1486 for i := 0 to Value.Width * Value.Height - 1 do
1487 begin
1488 pixel := TGrayscaleImage(Value).PixelsArray^[i];
1489 Writer.Write(FormatDot('0x%.2x ', [pixel]));
1490 end;
1491 end else
1492 if Value is TGrayscaleAlphaImage then
1493 begin
1494 for i := 0 to Value.Width * Value.Height - 1 do
1495 begin
1496 ga := TGrayscaleAlphaImage(Value).PixelsArray^[i];
1497 pixel := (ga.X shl 8) or ga.Y;
1498 Writer.Write(FormatDot('0x%.4x ', [pixel]));
1499 end;
1500 end else
1501 if Value is TRGBImage then
1502 begin
1503 for i := 0 to Value.Width * Value.Height - 1 do
1504 begin
1505 rgb := TRGBImage(Value).PixelsArray^[i];
1506 pixel := (rgb.X shl 16) or (rgb.Y shl 8) or rgb.Z;
1507 Writer.Write(FormatDot('0x%.6x ', [pixel]));
1508 end;
1509 end else
1510 if Value is TRGBAlphaImage then
1511 begin
1512 for i := 0 to Value.Width * Value.Height - 1 do
1513 begin
1514 rgba := TRGBAlphaImage(Value).PixelsArray^[i];
1515 pixel := (rgba.X shl 24) or (rgba.Y shl 16) or (rgba.Z shl 8) or rgba[3];
1516 Writer.Write(FormatDot('0x%.8x ', [pixel]));
1517 end;
1518 end else
1519 raise Exception.Create('TSFImage.SaveToStreamValue - not implemented TCastleImage descendant');
1520 {$I NoRQCheckEnd.inc}
1521 Writer.DecIndent;
1522 end;
1523 end;
1524
1525 function TSFImage.Equals(SecondValue: TX3DField): boolean;
1526 begin
1527 Result := (inherited Equals(SecondValue)) and
1528 (SecondValue is TSFImage) and
1529 { TODO: compare values
1530 (TSFImage(SecondValue).Value = Value) }true;
1531 end;
1532
1533 procedure TSFImage.Assign(Source: TPersistent);
1534 begin
1535 if Source is TSFImage then
1536 begin
1537 Value := TSFImage(Source).Value.MakeCopy;
1538 VRMLFieldAssignCommon(TX3DField(Source));
1539 end else
1540 inherited;
1541 end;
1542
1543 procedure TSFImage.AssignValue(Source: TX3DField);
1544 begin
1545 if Source is TSFImage then
1546 begin
1547 inherited;
1548 Value := TSFImage(Source).Value.MakeCopy;
1549 end else
1550 AssignValueRaiseInvalidClass(Source);
1551 end;
1552
1553 class function TSFImage.X3DType: String;
1554 begin
1555 Result := 'SFImage';
1556 end;
1557
1558 class function TSFImage.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
1559 begin
1560 Result := TSFImageEvent.Create(AParentNode, AName, AInEvent);
1561 end;
1562
1563 { TSFLong -------------------------------------------------------------------- }
1564
1565 constructor TSFLong.Create(const AParentNode: TX3DFileItem;
1566 const AExposed: boolean; const AName: String; const AValue: Int32);
1567 begin
1568 CreateUndefined(AParentNode, AExposed, AName);
1569 FValue := AValue;
1570 AssignDefaultValueFromValue;
1571 end;
1572
1573 procedure TSFLong.SetValue(const AValue: Int32);
1574 begin
1575 if MustBeNonnegative and (AValue < 0) then
1576 begin
1577 WritelnWarning('Setting int32 field %s to value < 0 (given: %f) is invalid', [
1578 NiceName,
1579 AValue
1580 ]);
1581 FValue := -AValue;
1582 Assert(Value >= 0);
1583 end else
1584 FValue := AValue;
1585 end;
1586
1587 procedure TSFLong.SetMustBeNonnegative(const ANewMustBeNonnegative: Boolean);
1588 begin
1589 if FMustBeNonnegative <> ANewMustBeNonnegative then
1590 begin
1591 FMustBeNonnegative := ANewMustBeNonnegative;
1592 if FMustBeNonnegative and (Value < 0) then
1593 begin
1594 WritelnWarning('Int32 field %s cannot have value < 0 (has: %f), fixing', [
1595 NiceName,
1596 Value
1597 ]);
1598 FValue := -FValue;
1599 Assert(Value >= 0);
1600 end;
1601 end;
1602 end;
1603
1604 procedure TSFLong.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
1605 begin
1606 Lexer.CheckTokenIs(vtInteger);
1607
1608 { Check is TokenInteger outside of 32-bit range. }
1609 if (Lexer.TokenInteger >= Low(Int32)) and
1610 (Lexer.TokenInteger <= High(Int32)) then
1611 begin
1612 Value := Lexer.TokenInteger;
1613 end else
1614 begin
1615 WritelnWarning('X3D', Format('Integer in the file is out of 32-bit range: %d',
1616 [Lexer.TokenInteger]));
1617 Value := -1;
1618 end;
1619
1620 Lexer.NextToken;
1621 end;
1622
1623 procedure TSFLong.SaveToStreamValue(Writer: TX3DWriter);
1624 begin
1625 Writer.Write(IntToStr(Value));
1626 end;
1627
1628 function TSFLong.EqualsDefaultValue: boolean;
1629 begin
1630 result := DefaultValueExists and (DefaultValue = Value)
1631 end;
1632
1633 function TSFLong.Equals(SecondValue: TX3DField): boolean;
1634 begin
1635 { Note that this means that SFInt32 and SFLong will actually be considered
1636 equal. That's Ok, we want this. }
1637 Result := (inherited Equals(SecondValue)) and
1638 (SecondValue is TSFLong) and
1639 (TSFLong(SecondValue).MustBeNonnegative = MustBeNonnegative) and
1640 (TSFLong(SecondValue).Value = Value);
1641 end;
1642
1643 function TSFLong.FastEqualsValue(SecondValue: TX3DField): boolean;
1644 begin
1645 Result := (SecondValue is TSFLong) and
1646 (TSFLong(SecondValue).Value = Value);
1647 end;
1648
1649 procedure TSFLong.Assign(Source: TPersistent);
1650 begin
1651 if Source is TSFLong then
1652 begin
1653 DefaultValue := TSFLong(Source).DefaultValue;
1654 DefaultValueExists := TSFLong(Source).DefaultValueExists;
1655 FValue := TSFLong(Source).Value;
1656 FMustBeNonnegative := TSFLong(Source).MustBeNonnegative;
1657 VRMLFieldAssignCommon(TX3DField(Source));
1658 end else
1659 inherited;
1660 end;
1661
1662 procedure TSFLong.AssignValue(Source: TX3DField);
1663 begin
1664 if Source is TSFLong then
1665 begin
1666 inherited;
1667 Value := TSFLong(Source).Value;
1668 end else
1669 AssignValueRaiseInvalidClass(Source);
1670 end;
1671
1672 procedure TSFLong.AssignDefaultValueFromValue;
1673 begin
1674 inherited;
1675 DefaultValue := Value;
1676 DefaultValueExists := true;
1677 end;
1678
1679 procedure TSFLong.UnassignDefaultValue;
1680 begin
1681 DefaultValueExists := false;
1682 end;
1683
1684 class function TSFLong.X3DType: String;
1685 begin
1686 Result := 'SFLong';
1687 end;
1688
1689 procedure TSFLong.Send(const AValue: Int32);
1690 var
1691 FieldValue: TX3DField;
1692 begin
1693 FieldValue := TSFLong.Create(ParentNode, false, X3DName, AValue);
1694 try
1695 Send(FieldValue);
1696 finally FreeAndNil(FieldValue) end;
1697 end;
1698
1699 class function TSFLong.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
1700 begin
1701 Result := TSFLongEvent.Create(AParentNode, AName, AInEvent);
1702 end;
1703
1704 { TSFInt32 ------------------------------------------------------------------- }
1705
1706 class function TSFInt32.X3DType: String;
1707 begin
1708 Result := 'SFInt32';
1709 end;
1710
1711 procedure TSFInt32.Send(const AValue: Int32);
1712 var
1713 FieldValue: TX3DField;
1714 begin
1715 FieldValue := TSFInt32.Create(ParentNode, false, X3DName, AValue);
1716 try
1717 Send(FieldValue);
1718 finally FreeAndNil(FieldValue) end;
1719 end;
1720
1721 class function TSFInt32.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
1722 begin
1723 Result := TSFInt32Event.Create(AParentNode, AName, AInEvent);
1724 end;
1725
1726 { TSFGenericMatrix ---------------------------------------------------------------------------- }
1727 {$ifdef FPC}
1728 constructor TSFGenericMatrix{$ifndef FPC}<
1729 TItem,
1730 TItemColumn,
1731 TEvent>{$endif} .
1732 Create(const AParentNode: TX3DFileItem;
1733 const AExposed: boolean; const AName: String; const AValue: TItem);
1734 begin
1735 CreateUndefined(AParentNode, AExposed, AName);
1736 FValue := AValue;
1737 AssignDefaultValueFromValue;
1738 end;
1739
1740 procedure TSFGenericMatrix{$ifndef FPC}<
1741 TItem,
1742 TItemColumn,
1743 TEvent>{$endif} .
1744 ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
1745 var
1746 Column, Row: integer;
1747 begin
1748 for Column := 0 to MatrixSize - 1 do
1749 begin
1750 for Row := 0 to MatrixSize - 1 do
1751 begin
1752 Lexer.CheckTokenIs(TokenNumbers, 'float number');
1753 FValue.Data[Column, Row] := Lexer.TokenFloat;
1754 Lexer.NextToken;
1755 end;
1756
1757 // Calling here global ParseVector or ParseFloat causes
1758 // Error: Global Generic template references static symtable
1759 // with FPC 3.0.2. TODO: test other FPC versions, potentially submit FPC bug.
1760 // ParseVector(, Lexer);
1761 end;
1762 end;
1763
1764 procedure TSFGenericMatrix.SaveToStreamValue(Writer: TX3DWriter);
1765 var
1766 V: TItemColumn;
1767 Column: integer;
1768 begin
1769 V := FValue.Columns[0];
1770 Writer.Writeln(V.ToRawString(FloatOutputFormat));
1771
1772 Writer.IncIndent;
1773 for Column := 1 to MatrixSize - 1 do
1774 begin
1775 V := FValue.Columns[Column];
1776 Writer.WritelnIndent(V.ToRawString(FloatOutputFormat));
1777 end;
1778 Writer.DecIndent;
1779 end;
1780
1781 function TSFGenericMatrix.Equals(SecondValue: TX3DField): boolean;
1782 begin
1783 Result := (inherited Equals(SecondValue)) and
1784 (SecondValue is TSFGenericMatrix) and
1785 TItem.Equals(TSFGenericMatrix(SecondValue).FValue, FValue);
1786 end;
1787
1788 function TSFGenericMatrix.FastEqualsValue(SecondValue: TX3DField): boolean;
1789 begin
1790 Result := (SecondValue is TSFGenericMatrix) and
1791 TItem.PerfectlyEquals(TSFGenericMatrix(SecondValue).Value, FValue);
1792 end;
1793
1794 procedure TSFGenericMatrix.AssignLerp(const A: Double; Value1, Value2: TX3DField);
1795 begin
1796 Value := TItem.Lerp(A, (Value1 as TSFGenericMatrix).Value, (Value2 as TSFGenericMatrix).Value);
1797 end;
1798
1799 function TSFGenericMatrix.CanAssignLerp: boolean;
1800 begin
1801 Result := true;
1802 end;
1803
1804 procedure TSFGenericMatrix.Assign(Source: TPersistent);
1805 begin
1806 if Source is TSFGenericMatrix then
1807 begin
1808 FValue := TSFGenericMatrix(Source).FValue;
1809 VRMLFieldAssignCommon(TX3DField(Source));
1810 end else
1811 inherited;
1812 end;
1813
1814 procedure TSFGenericMatrix.AssignValue(Source: TX3DField);
1815 begin
1816 if Source is TSFGenericMatrix then
1817 begin
1818 inherited;
1819 FValue := TSFGenericMatrix(Source).FValue;
1820 end else
1821 AssignValueRaiseInvalidClass(Source);
1822 end;
1823
1824 function TSFGenericMatrix.EqualsDefaultValue: boolean;
1825 begin
1826 Result := DefaultValueExists and
1827 TItem.PerfectlyEquals(DefaultValue, Value);
1828 end;
1829
1830 procedure TSFGenericMatrix.AssignDefaultValueFromValue;
1831 begin
1832 inherited;
1833 DefaultValue := Value;
1834 DefaultValueExists := true;
1835 end;
1836
1837 procedure TSFGenericMatrix.UnassignDefaultValue;
1838 begin
1839 DefaultValueExists := false;
1840 end;
1841
1842 class function TSFGenericMatrix.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
1843 begin
1844 Result := TEvent.Create(AParentNode, AName, AInEvent);
1845 end;
1846
1847 class function TSFGenericMatrix.MatrixSize: Integer;
1848 begin
1849 Result := High(TItemColumn.TIndex) + 1;
1850 end;
1851
1852 procedure TSFGenericMatrix.Send(const AValue: TItem);
1853 var
1854 FieldValue: TSFGenericMatrix;
1855 begin
1856 FieldValue := TSFGenericMatrix(
1857 TX3DFieldClass(ClassType).CreateUndefined(ParentNode, false, X3DName));
1858 try
1859 FieldValue.Value := AValue;
1860 Send(FieldValue);
1861 finally FreeAndNil(FieldValue) end;
1862 end;
1863 {$endif}
1864
1865 { TSFMatrix3f ------------------------------------------------------------------ }
1866
1867 class function TSFMatrix3f.X3DType: String;
1868 begin
1869 Result := 'SFMatrix3f';
1870 end;
1871
1872 {$ifndef FPC}
1873 constructor TSFMatrix3f.Create(const AParentNode: TX3DFileItem;
1874 const AExposed: boolean; const AName: String; const AValue: TMatrix3);
1875 begin
1876 CreateUndefined(AParentNode, AExposed, AName);
1877 FValue := AValue;
1878 AssignDefaultValueFromValue;
1879 end;
1880
1881 procedure TSFMatrix3f.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
1882 var
1883 Column, Row: integer;
1884 begin
1885 for Column := 0 to MatrixSize - 1 do
1886 begin
1887 for Row := 0 to MatrixSize - 1 do
1888 begin
1889 Lexer.CheckTokenIs(TokenNumbers, 'float number');
1890 FValue.Data[Column, Row] := Lexer.TokenFloat;
1891 Lexer.NextToken;
1892 end;
1893
1894 // Calling here global ParseVector or ParseFloat causes
1895 // Error: Global Generic template references static symtable
1896 // with FPC 3.0.2. TODO: test other FPC versions, potentially submit FPC bug.
1897 // ParseVector(, Lexer);
1898 end;
1899 end;
1900
1901 procedure TSFMatrix3f.SaveToStreamValue(Writer: TX3DWriter);
1902 var
1903 V: TVector3;
1904 Column: Integer;
1905 begin
1906 V := FValue.Columns[0];
1907 Writer.Writeln(V.ToRawString(FloatOutputFormat));
1908
1909 Writer.IncIndent;
1910 for Column := 1 to MatrixSize - 1 do
1911 begin
1912 V := FValue.Columns[Column];
1913 Writer.WritelnIndent(V.ToRawString(FloatOutputFormat));
1914 end;
1915 Writer.DecIndent;
1916 end;
1917
1918 function TSFMatrix3f.Equals(SecondValue: TX3DField): boolean;
1919 begin
1920 Result := (inherited Equals(SecondValue)) and
1921 (SecondValue is TSFMatrix3f) and
1922 TMatrix3.Equals(TSFMatrix3f(SecondValue).FValue, FValue);
1923 end;
1924
1925 function TSFMatrix3f.FastEqualsValue(SecondValue: TX3DField): boolean;
1926 begin
1927 Result := (SecondValue is TSFMatrix3f) and
1928 TMatrix3.PerfectlyEquals(TSFMatrix3f(SecondValue).Value, FValue);
1929 end;
1930
1931 procedure TSFMatrix3f.AssignLerp(const A: Double; Value1, Value2: TX3DField);
1932 begin
1933 Value := TMatrix3.Lerp(A, (Value1 as TSFMatrix3f).Value, (Value2 as TSFMatrix3f).Value);
1934 end;
1935
1936 function TSFMatrix3f.CanAssignLerp: boolean;
1937 begin
1938 Result := true;
1939 end;
1940
1941 procedure TSFMatrix3f.Assign(Source: TPersistent);
1942 begin
1943 if Source is TSFMatrix3f then
1944 begin
1945 FValue := TSFMatrix3f(Source).FValue;
1946 VRMLFieldAssignCommon(TX3DField(Source));
1947 end else
1948 inherited;
1949 end;
1950
1951 procedure TSFMatrix3f.AssignValue(Source: TX3DField);
1952 begin
1953 if Source is TSFMatrix3f then
1954 begin
1955 inherited;
1956 FValue := TSFMatrix3f(Source).FValue;
1957 end else
1958 AssignValueRaiseInvalidClass(Source);
1959 end;
1960
1961 function TSFMatrix3f.EqualsDefaultValue: boolean;
1962 begin
1963 Result := DefaultValueExists and
1964 TMatrix3.PerfectlyEquals(DefaultValue, Value);
1965 end;
1966
1967 procedure TSFMatrix3f.AssignDefaultValueFromValue;
1968 begin
1969 inherited;
1970 DefaultValue := Value;
1971 DefaultValueExists := true;
1972 end;
1973
1974 procedure TSFMatrix3f.UnassignDefaultValue;
1975 begin
1976 DefaultValueExists := false;
1977 end;
1978
1979 class function TSFMatrix3f.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
1980 begin
1981 Result := TSFMatrix3fEvent.Create(AParentNode, AName, AInEvent);
1982 end;
1983
1984 class function TSFMatrix3f.MatrixSize: Integer;
1985 begin
1986 Result := High(TVector3.TIndex) + 1;
1987 end;
1988
1989 procedure TSFMatrix3f.Send(const AValue: TMatrix3);
1990 var
1991 FieldValue: TSFMatrix3f;
1992 begin
1993 FieldValue := TSFMatrix3f(
1994 TX3DFieldClass(ClassType).CreateUndefined(ParentNode, false, X3DName));
1995 try
1996 FieldValue.Value := AValue;
1997 Send(FieldValue);
1998 finally FreeAndNil(FieldValue) end;
1999 end;
2000 {$endif}
2001
2002
2003 { TSFMatrix3d ------------------------------------------------------------------ }
2004
2005 class function TSFMatrix3d.X3DType: String;
2006 begin
2007 Result := 'SFMatrix3d';
2008 end;
2009
2010 {$ifndef FPC}
2011 constructor TSFMatrix3d.Create(const AParentNode: TX3DFileItem;
2012 const AExposed: boolean; const AName: String; const AValue: TMatrix3Double);
2013 begin
2014 CreateUndefined(AParentNode, AExposed, AName);
2015 FValue := AValue;
2016 AssignDefaultValueFromValue;
2017 end;
2018
2019 procedure TSFMatrix3d.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
2020 var
2021 Column, Row: integer;
2022 begin
2023 for Column := 0 to MatrixSize - 1 do
2024 begin
2025 for Row := 0 to MatrixSize - 1 do
2026 begin
2027 Lexer.CheckTokenIs(TokenNumbers, 'float number');
2028 FValue.Data[Column, Row] := Lexer.TokenFloat;
2029 Lexer.NextToken;
2030 end;
2031
2032 // Calling here global ParseVector or ParseFloat causes
2033 // Error: Global Generic template references static symtable
2034 // with FPC 3.0.2. TODO: test other FPC versions, potentially submit FPC bug.
2035 // ParseVector(, Lexer);
2036 end;
2037 end;
2038
2039 procedure TSFMatrix3d.SaveToStreamValue(Writer: TX3DWriter);
2040 var
2041 V: TVector3Double;
2042 Column: integer;
2043 begin
2044 V := FValue.Columns[0];
2045 Writer.Writeln(V.ToRawString(FloatOutputFormat));
2046
2047 Writer.IncIndent;
2048 for Column := 1 to MatrixSize - 1 do
2049 begin
2050 V := FValue.Columns[Column];
2051 Writer.WritelnIndent(V.ToRawString(FloatOutputFormat));
2052 end;
2053 Writer.DecIndent;
2054 end;
2055
2056 function TSFMatrix3d.Equals(SecondValue: TX3DField): boolean;
2057 begin
2058 Result := (inherited Equals(SecondValue)) and
2059 (SecondValue is TSFMatrix3d) and
2060 TMatrix3Double.Equals(TSFMatrix3d(SecondValue).FValue, FValue);
2061 end;
2062
2063 function TSFMatrix3d.FastEqualsValue(SecondValue: TX3DField): boolean;
2064 begin
2065 Result := (SecondValue is TSFMatrix3d) and
2066 TMatrix3Double.PerfectlyEquals(TSFMatrix3d(SecondValue).Value, FValue);
2067 end;
2068
2069 procedure TSFMatrix3d.AssignLerp(const A: Double; Value1, Value2: TX3DField);
2070 begin
2071 Value := TMatrix3Double.Lerp(A, (Value1 as TSFMatrix3d).Value, (Value2 as TSFMatrix3d).Value);
2072 end;
2073
2074 function TSFMatrix3d.CanAssignLerp: boolean;
2075 begin
2076 Result := true;
2077 end;
2078
2079 procedure TSFMatrix3d.Assign(Source: TPersistent);
2080 begin
2081 if Source is TSFMatrix3d then
2082 begin
2083 FValue := TSFMatrix3d(Source).FValue;
2084 VRMLFieldAssignCommon(TX3DField(Source));
2085 end else
2086 inherited;
2087 end;
2088
2089 procedure TSFMatrix3d.AssignValue(Source: TX3DField);
2090 begin
2091 if Source is TSFMatrix3d then
2092 begin
2093 inherited;
2094 FValue := TSFMatrix3d(Source).FValue;
2095 end else
2096 AssignValueRaiseInvalidClass(Source);
2097 end;
2098
2099 function TSFMatrix3d.EqualsDefaultValue: boolean;
2100 begin
2101 Result := DefaultValueExists and
2102 TMatrix3Double.PerfectlyEquals(DefaultValue, Value);
2103 end;
2104
2105 procedure TSFMatrix3d.AssignDefaultValueFromValue;
2106 begin
2107 inherited;
2108 DefaultValue := Value;
2109 DefaultValueExists := true;
2110 end;
2111
2112 procedure TSFMatrix3d.UnassignDefaultValue;
2113 begin
2114 DefaultValueExists := false;
2115 end;
2116
2117 class function TSFMatrix3d.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
2118 begin
2119 Result := TSFMatrix3dEvent.Create(AParentNode, AName, AInEvent);
2120 end;
2121
2122 class function TSFMatrix3d.MatrixSize: Integer;
2123 begin
2124 Result := High(TVector3Double.TIndex) + 1;
2125 end;
2126
2127 procedure TSFMatrix3d.Send(const AValue: TMatrix3Double);
2128 var
2129 FieldValue: TSFMatrix3d;
2130 begin
2131 FieldValue := TSFMatrix3d(
2132 TX3DFieldClass(ClassType).CreateUndefined(ParentNode, false, X3DName));
2133 try
2134 FieldValue.Value := AValue;
2135 Send(FieldValue);
2136 finally FreeAndNil(FieldValue) end;
2137 end;
2138 {$endif}
2139
2140 { TSFMatrix4f ------------------------------------------------------------------ }
2141
2142 class function TSFMatrix4f.X3DType: String;
2143 begin
2144 Result := 'SFMatrix4f';
2145 end;
2146
2147 {$ifndef FPC}
2148 constructor TSFMatrix4f.Create(const AParentNode: TX3DFileItem;
2149 const AExposed: boolean; const AName: String; const AValue: TMatrix4);
2150 begin
2151 CreateUndefined(AParentNode, AExposed, AName);
2152 FValue := AValue;
2153 AssignDefaultValueFromValue;
2154 end;
2155
2156 procedure TSFMatrix4f.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
2157 var
2158 Column, Row: integer;
2159 begin
2160 for Column := 0 to MatrixSize - 1 do
2161 begin
2162 for Row := 0 to MatrixSize - 1 do
2163 begin
2164 Lexer.CheckTokenIs(TokenNumbers, 'float number');
2165 FValue.Data[Column, Row] := Lexer.TokenFloat;
2166 Lexer.NextToken;
2167 end;
2168
2169 // Calling here global ParseVector or ParseFloat causes
2170 // Error: Global Generic template references static symtable
2171 // with FPC 3.0.2. TODO: test other FPC versions, potentially submit FPC bug.
2172 // ParseVector(, Lexer);
2173 end;
2174 end;
2175
2176 procedure TSFMatrix4f.SaveToStreamValue(Writer: TX3DWriter);
2177 var
2178 V: TVector4;
2179 Column: integer;
2180 begin
2181 V := FValue.Columns[0];
2182 Writer.Writeln(V.ToRawString(FloatOutputFormat));
2183
2184 Writer.IncIndent;
2185 for Column := 1 to MatrixSize - 1 do
2186 begin
2187 V := FValue.Columns[Column];
2188 Writer.WritelnIndent(V.ToRawString(FloatOutputFormat));
2189 end;
2190 Writer.DecIndent;
2191 end;
2192
2193 function TSFMatrix4f.Equals(SecondValue: TX3DField): boolean;
2194 begin
2195 Result := (inherited Equals(SecondValue)) and
2196 (SecondValue is TSFMatrix4f) and
2197 TMatrix4.Equals(TSFMatrix4f(SecondValue).FValue, FValue);
2198 end;
2199
2200 function TSFMatrix4f.FastEqualsValue(SecondValue: TX3DField): boolean;
2201 begin
2202 Result := (SecondValue is TSFMatrix4f) and
2203 TMatrix4.PerfectlyEquals(TSFMatrix4f(SecondValue).Value, FValue);
2204 end;
2205
2206 procedure TSFMatrix4f.AssignLerp(const A: Double; Value1, Value2: TX3DField);
2207 begin
2208 Value := TMatrix4.Lerp(A, (Value1 as TSFMatrix4f).Value, (Value2 as TSFMatrix4f).Value);
2209 end;
2210
2211 function TSFMatrix4f.CanAssignLerp: boolean;
2212 begin
2213 Result := true;
2214 end;
2215
2216 procedure TSFMatrix4f.Assign(Source: TPersistent);
2217 begin
2218 if Source is TSFMatrix4f then
2219 begin
2220 FValue := TSFMatrix4f(Source).FValue;
2221 VRMLFieldAssignCommon(TX3DField(Source));
2222 end else
2223 inherited;
2224 end;
2225
2226 procedure TSFMatrix4f.AssignValue(Source: TX3DField);
2227 begin
2228 if Source is TSFMatrix4f then
2229 begin
2230 inherited;
2231 FValue := TSFMatrix4f(Source).FValue;
2232 end else
2233 AssignValueRaiseInvalidClass(Source);
2234 end;
2235
2236 function TSFMatrix4f.EqualsDefaultValue: boolean;
2237 begin
2238 Result := DefaultValueExists and
2239 TMatrix4.PerfectlyEquals(DefaultValue, Value);
2240 end;
2241
2242 procedure TSFMatrix4f.AssignDefaultValueFromValue;
2243 begin
2244 inherited;
2245 DefaultValue := Value;
2246 DefaultValueExists := true;
2247 end;
2248
2249 procedure TSFMatrix4f.UnassignDefaultValue;
2250 begin
2251 DefaultValueExists := false;
2252 end;
2253
2254 class function TSFMatrix4f.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
2255 begin
2256 Result := TSFMatrix4fEvent.Create(AParentNode, AName, AInEvent);
2257 end;
2258
2259 class function TSFMatrix4f.MatrixSize: Integer;
2260 begin
2261 Result := High(TVector4.TIndex) + 1;
2262 end;
2263
2264 procedure TSFMatrix4f.Send(const AValue: TMatrix4);
2265 var
2266 FieldValue: TSFMatrix4f;
2267 begin
2268 FieldValue := TSFMatrix4f(
2269 TX3DFieldClass(ClassType).CreateUndefined(ParentNode, false, X3DName));
2270 try
2271 FieldValue.Value := AValue;
2272 Send(FieldValue);
2273 finally FreeAndNil(FieldValue) end;
2274 end;
2275 {$endif}
2276
2277
2278 { TSFMatrix4d ------------------------------------------------------------------ }
2279
2280 class function TSFMatrix4d.X3DType: String;
2281 begin
2282 Result := 'SFMatrix4d';
2283 end;
2284
2285 {$ifndef FPC}
2286 constructor TSFMatrix4d.Create(const AParentNode: TX3DFileItem;
2287 const AExposed: boolean; const AName: String; const AValue: TMatrix4Double);
2288 begin
2289 CreateUndefined(AParentNode, AExposed, AName);
2290 FValue := AValue;
2291 AssignDefaultValueFromValue;
2292 end;
2293
2294 procedure TSFMatrix4d.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
2295 var
2296 Column, Row: integer;
2297 begin
2298 for Column := 0 to MatrixSize - 1 do
2299 begin
2300 for Row := 0 to MatrixSize - 1 do
2301 begin
2302 Lexer.CheckTokenIs(TokenNumbers, 'float number');
2303 FValue.Data[Column, Row] := Lexer.TokenFloat;
2304 Lexer.NextToken;
2305 end;
2306
2307 // Calling here global ParseVector or ParseFloat causes
2308 // Error: Global Generic template references static symtable
2309 // with FPC 3.0.2. TODO: test other FPC versions, potentially submit FPC bug.
2310 // ParseVector(, Lexer);
2311 end;
2312 end;
2313
2314 procedure TSFMatrix4d.SaveToStreamValue(Writer: TX3DWriter);
2315 var
2316 V: TVector4Double;
2317 Column: integer;
2318 begin
2319 V := FValue.Columns[0];
2320 Writer.Writeln(V.ToRawString(FloatOutputFormat));
2321
2322 Writer.IncIndent;
2323 for Column := 1 to MatrixSize - 1 do
2324 begin
2325 V := FValue.Columns[Column];
2326 Writer.WritelnIndent(V.ToRawString(FloatOutputFormat));
2327 end;
2328 Writer.DecIndent;
2329 end;
2330
2331 function TSFMatrix4d.Equals(SecondValue: TX3DField): boolean;
2332 begin
2333 Result := (inherited Equals(SecondValue)) and
2334 (SecondValue is TSFMatrix4d) and
2335 TMatrix4Double.Equals(TSFMatrix4d(SecondValue).FValue, FValue);
2336 end;
2337
2338 function TSFMatrix4d.FastEqualsValue(SecondValue: TX3DField): boolean;
2339 begin
2340 Result := (SecondValue is TSFMatrix3d) and
2341 TMatrix4Double.PerfectlyEquals(TSFMatrix4d(SecondValue).Value, FValue);
2342 end;
2343
2344 procedure TSFMatrix4d.AssignLerp(const A: Double; Value1, Value2: TX3DField);
2345 begin
2346 Value := TMatrix4Double.Lerp(A, (Value1 as TSFMatrix4d).Value, (Value2 as TSFMatrix4d).Value);
2347 end;
2348
2349 function TSFMatrix4d.CanAssignLerp: boolean;
2350 begin
2351 Result := true;
2352 end;
2353
2354 procedure TSFMatrix4d.Assign(Source: TPersistent);
2355 begin
2356 if Source is TSFMatrix4d then
2357 begin
2358 FValue := TSFMatrix4d(Source).FValue;
2359 VRMLFieldAssignCommon(TX3DField(Source));
2360 end else
2361 inherited;
2362 end;
2363
2364 procedure TSFMatrix4d.AssignValue(Source: TX3DField);
2365 begin
2366 if Source is TSFMatrix4d then
2367 begin
2368 inherited;
2369 FValue := TSFMatrix4d(Source).FValue;
2370 end else
2371 AssignValueRaiseInvalidClass(Source);
2372 end;
2373
2374 function TSFMatrix4d.EqualsDefaultValue: boolean;
2375 begin
2376 Result := DefaultValueExists and
2377 TMatrix4Double.PerfectlyEquals(DefaultValue, Value);
2378 end;
2379
2380 procedure TSFMatrix4d.AssignDefaultValueFromValue;
2381 begin
2382 inherited;
2383 DefaultValue := Value;
2384 DefaultValueExists := true;
2385 end;
2386
2387 procedure TSFMatrix4d.UnassignDefaultValue;
2388 begin
2389 DefaultValueExists := false;
2390 end;
2391
2392 class function TSFMatrix4d.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
2393 begin
2394 Result := TSFMatrix4dEvent.Create(AParentNode, AName, AInEvent);
2395 end;
2396
2397 class function TSFMatrix4d.MatrixSize: Integer;
2398 begin
2399 Result := High(TVector4Double.TIndex) + 1;
2400 end;
2401
2402 procedure TSFMatrix4d.Send(const AValue: TMatrix4Double);
2403 var
2404 FieldValue: TSFMatrix4d;
2405 begin
2406 FieldValue := TSFMatrix4d(
2407 TX3DFieldClass(ClassType).CreateUndefined(ParentNode, false, X3DName));
2408 try
2409 FieldValue.Value := AValue;
2410 Send(FieldValue);
2411 finally FreeAndNil(FieldValue) end;
2412 end;
2413 {$endif}
2414
2415
2416 { TSFMatrix ------------------------------------------------------------------ }
2417
2418 class function TSFMatrix.X3DType: String;
2419 begin
2420 Result := 'SFMatrix';
2421 end;
2422
2423 { TSFRotation ---------------------------------------------------------------- }
2424
2425 constructor TSFRotation.Create(const AParentNode: TX3DFileItem;
2426 const AExposed: boolean; const AName: String;
2427 const AnAxis: TVector3; const ARotationRad: Single);
2428 begin
2429 CreateUndefined(AParentNode, AExposed, AName);
2430
2431 Axis := AnAxis;
2432 RotationRad := ARotationRad;
2433
2434 AssignDefaultValueFromValue;
2435 end;
2436
2437 constructor TSFRotation.Create(const AParentNode: TX3DFileItem;
2438 const AExposed: boolean; const AName: String;
2439 const AValue: TVector4);
2440 var
2441 AnAxis: TVector3 absolute AValue;
2442 begin
2443 CreateUndefined(AParentNode, AExposed, AName);
2444
2445 Axis := AnAxis;
2446 RotationRad := AValue[3];
2447
2448 AssignDefaultValueFromValue;
2449 end;
2450
2451 procedure TSFRotation.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
2452 begin
2453 Axis.X := ParseFloat(Lexer);
2454 Axis.Y := ParseFloat(Lexer);
2455 Axis.Z := ParseFloat(Lexer);
2456 RotationRad := ParseFloat(Lexer) * Reader.AngleConversionFactor;
2457 end;
2458
2459 function TSFRotation.GetValue: TVector4;
2460 begin
2461 Move(Axis, Result, SizeOf(Single) * 3);
2462 Result.W := RotationRad;
2463 end;
2464
2465 procedure TSFRotation.SetValue(const AValue: TVector4);
2466 begin
2467 Axis.X := AValue.X;
2468 Axis.Y := AValue.Y;
2469 Axis.Z := AValue.Z;
2470 RotationRad := AValue.W;
2471 end;
2472
2473 function TSFRotation.GetValueDeg: TVector4;
2474 begin
2475 Move(Axis, Result, SizeOf(Single) * 3);
2476 Result.W := RadToDeg(RotationRad);
2477 end;
2478
2479 procedure TSFRotation.SetValueDeg(const AValue: TVector4);
2480 begin
2481 Axis.X := AValue.X;
2482 Axis.Y := AValue.Y;
2483 Axis.Z := AValue.Z;
2484 RotationRad := DegToRad(AValue.W);
2485 end;
2486
2487 procedure TSFRotation.SaveToStreamValue(Writer: TX3DWriter);
2488 begin
2489 Writer.Write(Axis.ToRawString(FloatOutputFormat) +' ' +FormatDot(FloatOutputFormat, [RotationRad]));
2490 end;
2491
2492 function TSFRotation.RotatedPoint(const pt: TVector3): TVector3;
2493 begin
2494 if not Axis.IsZero then
2495 Result := RotatePointAroundAxisRad(RotationRad, pt, Axis) else
2496 begin
2497 { Safeguard against rotation around zero vector, which produces unpredictable
2498 results (actually, Result would be filled with Nan values).
2499 VRML spec says that SFRotation should always specify a normalized vector. }
2500 Result := Pt;
2501 WritelnWarning('X3D', FormatDot('SFRotation field (%s) specifies rotation around zero vector', [NiceName]));
2502 end;
2503 end;
2504
2505 function TSFRotation.Equals(SecondValue: TX3DField): boolean;
2506 begin
2507 Result := (inherited Equals(SecondValue)) and
2508 (SecondValue is TSFRotation) and
2509 TVector3.Equals(TSFRotation(SecondValue).Axis, Axis) and
2510 SameValue(TSFRotation(SecondValue).RotationRad, RotationRad);
2511 end;
2512
2513 function TSFRotation.FastEqualsValue(SecondValue: TX3DField): boolean;
2514 begin
2515 Result := (SecondValue is TSFRotation) and
2516 TVector3.PerfectlyEquals(TSFRotation(SecondValue).Axis, Axis) and
2517 (TSFRotation(SecondValue).RotationRad = RotationRad);
2518 end;
2519
2520 function TSFRotation.EqualsDefaultValue: boolean;
2521 begin
2522 Result := DefaultValueExists and
2523 TVector3.PerfectlyEquals(DefaultAxis, Axis) and
2524 (DefaultRotationRad = RotationRad);
2525 end;
2526
2527 procedure TSFRotation.AssignLerp(const A: Double; Value1, Value2: TX3DField);
2528 begin
2529 { interpolate using slerp (testcase when linear interpolation on axis/vector fails:
2530 god triangle in escape_universe) }
2531 Value := SLerp(A, (Value1 as TSFRotation).Value, (Value2 as TSFRotation).Value);
2532 end;
2533
2534 function TSFRotation.CanAssignLerp: boolean;
2535 begin
2536 Result := true;
2537 end;
2538
2539 procedure TSFRotation.Assign(Source: TPersistent);
2540 begin
2541 if Source is TSFRotation then
2542 begin
2543 Axis := TSFRotation(Source).Axis;
2544 RotationRad := TSFRotation(Source).RotationRad;
2545 VRMLFieldAssignCommon(TX3DField(Source));
2546 end else
2547 inherited;
2548 end;
2549
2550 procedure TSFRotation.AssignValue(Source: TX3DField);
2551 begin
2552 if Source is TSFRotation then
2553 begin
2554 inherited;
2555 Axis := TSFRotation(Source).Axis;
2556 RotationRad := TSFRotation(Source).RotationRad;
2557 end else
2558 AssignValueRaiseInvalidClass(Source);
2559 end;
2560
2561 procedure TSFRotation.AssignDefaultValueFromValue;
2562 begin
2563 inherited;
2564 DefaultAxis := Axis;
2565 DefaultRotationRad := RotationRad;
2566 DefaultValueExists := true;
2567 end;
2568
2569 procedure TSFRotation.UnassignDefaultValue;
2570 begin
2571 DefaultValueExists := false;
2572 end;
2573
2574 class function TSFRotation.X3DType: String;
2575 begin
2576 Result := 'SFRotation';
2577 end;
2578
2579 procedure TSFRotation.Send(const AValue: TVector4);
2580 var
2581 FieldValue: TX3DField;
2582 begin
2583 FieldValue := TSFRotation.Create(ParentNode, false, X3DName, AValue);
2584 try
2585 Send(FieldValue);
2586 finally FreeAndNil(FieldValue) end;
2587 end;
2588
2589 class function TSFRotation.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
2590 begin
2591 Result := TSFRotationEvent.Create(AParentNode, AName, AInEvent);
2592 end;
2593
2594 { TSFString ------------------------------------------------------------------ }
2595
2596 constructor TSFString.Create(const AParentNode: TX3DFileItem;
2597 const AExposed: boolean; const AName: String; const AValue: String);
2598 begin
2599 CreateUndefined(AParentNode, AExposed, AName);
2600
2601 Value := AValue;
2602 AssignDefaultValueFromValue;
2603 end;
2604
2605 procedure TSFString.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
2606 begin
2607 Lexer.CheckTokenIs(vtString);
2608 Value := Lexer.TokenString;
2609 Lexer.NextToken;
2610 end;
2611
2612 procedure TSFString.SaveToStreamValue(Writer: TX3DWriter);
2613 begin
2614 case Writer.Encoding of
2615 xeClassic: Writer.Write(StringToX3DClassic(Value));
2616 xeXML : Writer.Write(StringToX3DXml(Value));
2617 {$ifndef COMPILER_CASE_ANALYSIS}
2618 else raise EInternalError.Create('TSFString.SaveToStreamValue Encoding?');
2619 {$endif}
2620 end;
2621 end;
2622
2623 function TSFString.EqualsDefaultValue: boolean;
2624 begin
2625 Result := DefaultValueExists and (DefaultValue = Value);
2626 end;
2627
2628 function TSFString.Equals(SecondValue: TX3DField): boolean;
2629 begin
2630 Result := (inherited Equals(SecondValue)) and
2631 (SecondValue is TSFString) and
2632 (TSFString(SecondValue).Value = Value);
2633 end;
2634
2635 function TSFString.FastEqualsValue(SecondValue: TX3DField): boolean;
2636 begin
2637 Result := (SecondValue is TSFString) and
2638 (TSFString(SecondValue).Value = Value);
2639 end;
2640
2641 procedure TSFString.Assign(Source: TPersistent);
2642 begin
2643 if Source is TSFString then
2644 begin
2645 DefaultValue := TSFString(Source).DefaultValue;
2646 DefaultValueExists := TSFString(Source).DefaultValueExists;
2647 Value := TSFString(Source).Value;
2648 VRMLFieldAssignCommon(TX3DField(Source));
2649 end else
2650 inherited;
2651 end;
2652
2653 procedure TSFString.AssignValue(Source: TX3DField);
2654 begin
2655 if Source is TSFString then
2656 begin
2657 inherited;
2658 Value := TSFString(Source).Value;
2659 end else
2660 AssignValueRaiseInvalidClass(Source);
2661 end;
2662
2663 procedure TSFString.AssignDefaultValueFromValue;
2664 begin
2665 inherited;
2666 DefaultValue := Value;
2667 DefaultValueExists := true;
2668 end;
2669
2670 procedure TSFString.UnassignDefaultValue;
2671 begin
2672 DefaultValueExists := false;
2673 end;
2674
2675 class function TSFString.X3DType: String;
2676 begin
2677 Result := 'SFString';
2678 end;
2679
2680 procedure TSFString.ParseXMLAttribute(const AttributeValue: String; Reader: TX3DReader);
2681 begin
2682 { SFString has quite special interpretation, it's just attrib
2683 name. It would not be usefull trying to use TX3DLexer here,
2684 it's easier just to handle this as a special case.
2685
2686 Uhm... some X3D XML files commit the reverse mistake
2687 as for MFString: they *include* additional quotes around the string.
2688 Spec says that for SFString, such quotes are not needed.
2689 Example: openlibraries trunk/media files.
2690
2691 I detect this, warn and strip quotes. }
2692 if (Length(AttributeValue) >= 2) and
2693 (AttributeValue[1] = '"') and
2694 (AttributeValue[Length(AttributeValue)] = '"') then
2695 begin
2696 WritelnWarning('X3D', 'X3D XML: found extra quotes around SFString value. Assuming this is a mistake, and stripping quotes from ''' + AttributeValue + '''. Fix your model: SFString field values should not be enclosed in extra quotes!');
2697 Value := Copy(AttributeValue, 2, Length(AttributeValue) - 2);
2698 end else
2699 Value := AttributeValue;
2700 end;
2701
2702 procedure TSFString.Send(const AValue: String);
2703 var
2704 FieldValue: TX3DField;
2705 begin
2706 FieldValue := TSFString.Create(ParentNode, false, X3DName, AValue);
2707 try
2708 Send(FieldValue);
2709 finally FreeAndNil(FieldValue) end;
2710 end;
2711
2712 function TSFString.SaveToXmlValue: TSaveToXmlMethod;
2713 begin
2714 Result := sxAttributeCustomQuotes;
2715 end;
2716
2717 class function TSFString.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
2718 begin
2719 Result := TSFStringEvent.Create(AParentNode, AName, AInEvent);
2720 end;
2721
2722 procedure TSFString.SetValue(const NewValue: String);
2723 begin
2724 FValue := NewValue;
2725 end;
2726
2727 procedure TSFString.SetDefaultValue(const NewDefaultValue: String);
2728 begin
2729 FDefaultValue := NewDefaultValue;
2730 end;
2731
2732 { TSFStringEnum -------------------------------------------------------------- }
2733
2734 constructor TSFStringEnum.Create(const AParentNode: TX3DFileItem;
2735 const AExposed: boolean; const AName: String;
2736 const AEnumNames: PConstantStringArray; const AEnumNamesCount: Cardinal;
2737 const AValue: integer);
2738 var
2739 I: Integer;
2740 begin
2741 FEnumNames := AEnumNames;
2742 FEnumNamesCount := AEnumNamesCount;
2743 Assert(AEnumNamesCount > 0); // this makes also "FEnumNamesCount - 1" in for loops a valid Cardinal
2744
2745 AllNamesUpperCase := true;
2746 for I := 0 to FEnumNamesCount - 1 do
2747 if FEnumNames^[I] <> UpperCase(FEnumNames^[I]) then
2748 AllNamesUpperCase := false;
2749
2750 inherited Create(AParentNode, AExposed, AName, FEnumNames^[AValue]);
2751 { inherited Create will assign Value, and in SetValue should cause setting
2752 our FEnumValue }
2753 Assert(AValue = FEnumValue);
2754 end;
2755
2756 class function TSFStringEnum.ExposedEventsFieldClass: TX3DFieldClass;
2757 begin
2758 Result := TSFString;
2759 end;
2760
2761 function TSFStringEnum.StringToEnumValue(const NewValue: String): Integer;
2762
2763 function EnumNamesIndexOf(const S: String): Integer;
2764 begin
2765 for Result := 0 to FEnumNamesCount - 1 do
2766 if FEnumNames^[Result] = S then
2767 Exit;
2768 Result := -1;
2769 end;
2770
2771 var
2772 NewValueFixedCase: String;
2773 begin
2774 if AllNamesUpperCase then
2775 begin
2776 NewValueFixedCase := UpperCase(NewValue);
2777 if NewValueFixedCase <> NewValue then
2778 WritelnWarning('X3D', Format('Field "%s" value should be uppercase, but is not: "%s"',
2779 [X3DName, NewValue]));
2780 end else
2781 NewValueFixedCase := NewValue;
2782
2783 Result := EnumNamesIndexOf(NewValueFixedCase);
2784 if Result = -1 then
2785 begin
2786 Result := DefaultEnumValue;
2787 WritelnWarning('X3D', Format('Unknown "%s" field value: "%s"',
2788 [X3DName, NewValue]));
2789 end;
2790 end;
2791
2792 procedure TSFStringEnum.SetValue(const NewValue: String);
2793 begin
2794 inherited SetValue(NewValue);
2795 { calculate new FEnumValue, IOW convert string NewValue to integer }
2796 FEnumValue := StringToEnumValue(NewValue);
2797 end;
2798
2799 procedure TSFStringEnum.SetEnumValue(const NewEnumValue: Integer);
2800 begin
2801 inherited SetValue(FEnumNames^[NewEnumValue]);
2802 FEnumValue := NewEnumValue;
2803 end;
2804
2805 procedure TSFStringEnum.SendEnumValue(const NewValue: Integer);
2806 begin
2807 inherited Send(FEnumNames^[NewValue]);
2808 end;
2809
2810 procedure TSFStringEnum.SetDefaultValue(const NewDefaultValue: String);
2811 begin
2812 inherited SetDefaultValue(NewDefaultValue);
2813 FDefaultEnumValue := StringToEnumValue(NewDefaultValue);
2814 end;
2815
2816 procedure TSFStringEnum.SetDefaultEnumValue(const NewDefaultEnumValue: Integer);
2817 begin
2818 inherited SetDefaultValue(FEnumNames^[NewDefaultEnumValue]);
2819 FDefaultEnumValue := NewDefaultEnumValue;
2820 end;
2821
2822 { TSFGenericVector ----------------------------------------------------------- }
2823 {$ifdef FPC}
2824 constructor TSFGenericVector.Create(const AParentNode: TX3DFileItem;
2825 const AExposed: boolean; const AName: String; const AValue: TItem);
2826 begin
2827 CreateUndefined(AParentNode, AExposed, AName);
2828
2829 Value := AValue;
2830 AssignDefaultValueFromValue;
2831 end;
2832
2833 procedure TSFGenericVector.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
2834 var
2835 I: Integer;
2836 begin
2837 for I := 0 to High(Value.TIndex) do
2838 begin
2839 Lexer.CheckTokenIs(TokenNumbers, 'float number');
2840 Value.Data[I] := Lexer.TokenFloat;
2841 Lexer.NextToken;
2842 end;
2843
2844 // Calling ParseVector or ParseFloat here causes FPC 3.0.2 error
2845 // Error: Global Generic template references static symtable
2846 // TODO: check on other FPC versions and report.
2847 // ParseVector(Value.Data, Lexer);
2848 end;
2849
2850 procedure TSFGenericVector.SaveToStreamValue(Writer: TX3DWriter);
2851 begin
2852 Writer.Write(Value.ToRawString(FloatOutputFormat));
2853 end;
2854
2855 function TSFGenericVector.EqualsDefaultValue: boolean;
2856 begin
2857 Result := DefaultValueExists and TItem.PerfectlyEquals(DefaultValue, Value);
2858 end;
2859
2860 function TSFGenericVector.Equals(SecondValue: TX3DField): boolean;
2861 begin
2862 Result := (inherited Equals(SecondValue)) and
2863 (SecondValue is TSFGenericVector) and
2864 TItem.Equals(TSFGenericVector(SecondValue).Value, Value);
2865 end;
2866
2867 function TSFGenericVector.FastEqualsValue(SecondValue: TX3DField): boolean;
2868 begin
2869 Result := (SecondValue is TSFGenericVector) and
2870 TItem.PerfectlyEquals(TSFGenericVector(SecondValue).Value, Value);
2871 end;
2872
2873 procedure TSFGenericVector.AssignLerp(const A: Double; Value1, Value2: TX3DField);
2874 begin
2875 Value := TItem.Lerp(A, (Value1 as TSFGenericVector).Value, (Value2 as TSFGenericVector).Value);
2876 end;
2877
2878 function TSFGenericVector.CanAssignLerp: boolean;
2879 begin
2880 Result := true;
2881 end;
2882
2883 procedure TSFGenericVector.Assign(Source: TPersistent);
2884 begin
2885 if Source is TSFGenericVector then
2886 begin
2887 DefaultValue := TSFGenericVector(Source).DefaultValue;
2888 DefaultValueExists := TSFGenericVector(Source).DefaultValueExists;
2889 Value := TSFGenericVector(Source).Value;
2890 VRMLFieldAssignCommon(TX3DField(Source));
2891 end else
2892 inherited;
2893 end;
2894
2895 procedure TSFGenericVector.AssignValue(Source: TX3DField);
2896 begin
2897 if Source is TSFGenericVector then
2898 begin
2899 inherited;
2900 Value := TSFGenericVector(Source).Value;
2901 end else
2902 AssignValueRaiseInvalidClass(Source);
2903 end;
2904
2905 procedure TSFGenericVector.AssignDefaultValueFromValue;
2906 begin
2907 inherited;
2908 DefaultValue := Value;
2909 DefaultValueExists := true;
2910 end;
2911
2912 procedure TSFGenericVector.UnassignDefaultValue;
2913 begin
2914 DefaultValueExists := false;
2915 end;
2916
2917 class function TSFGenericVector.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
2918 begin
2919 Result := TEvent.Create(AParentNode, AName, AInEvent);
2920 end;
2921
2922 procedure TSFGenericVector.Send(const AValue: TItem);
2923 var
2924 FieldValue: TSFGenericVector;
2925 begin
2926 FieldValue := TSFGenericVector(
2927 TX3DFieldClass(ClassType).CreateUndefined(ParentNode, false, X3DName));
2928 try
2929 FieldValue.Value := AValue;
2930 Send(FieldValue);
2931 finally FreeAndNil(FieldValue) end;
2932 end;
2933 {$endif FPC}
2934
2935 { TSFVec2f ------------------------------------------------------------------- }
2936
2937 class function TSFVec2f.X3DType: String;
2938 begin
2939 Result := 'SFVec2f';
2940 end;
2941
2942 {$ifndef FPC}
2943 constructor TSFVec2f.Create(const AParentNode: TX3DFileItem;
2944 const AExposed: boolean; const AName: String; const AValue: TVector2);
2945 begin
2946 CreateUndefined(AParentNode, AExposed, AName);
2947
2948 Value := AValue;
2949 AssignDefaultValueFromValue;
2950 end;
2951
2952 procedure TSFVec2f.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
2953 var
2954 I: Integer;
2955 begin
2956 for I := 0 to Value.Count - 1 do
2957 begin
2958 Lexer.CheckTokenIs(TokenNumbers, 'float number');
2959 Value.Data[I] := Lexer.TokenFloat;
2960 Lexer.NextToken;
2961 end;
2962
2963 // Calling ParseVector or ParseFloat here causes FPC 3.0.2 error
2964 // Error: Global Generic template references static symtable
2965 // TODO: check on other FPC versions and report.
2966 // ParseVector(Value.Data, Lexer);
2967 end;
2968
2969 procedure TSFVec2f.SaveToStreamValue(Writer: TX3DWriter);
2970 begin
2971 Writer.Write(Value.ToRawString(FloatOutputFormat));
2972 end;
2973
2974 function TSFVec2f.EqualsDefaultValue: boolean;
2975 begin
2976 Result := DefaultValueExists and TVector2.PerfectlyEquals(DefaultValue, Value);
2977 end;
2978
2979 function TSFVec2f.Equals(SecondValue: TX3DField): boolean;
2980 begin
2981 Result := (inherited Equals(SecondValue)) and
2982 (SecondValue is TSFVec2f) and
2983 TVector2.Equals(TSFVec2f(SecondValue).Value, Value);
2984 end;
2985
2986 function TSFVec2f.FastEqualsValue(SecondValue: TX3DField): boolean;
2987 begin
2988 Result := (SecondValue is TSFVec2f) and
2989 TVector2.PerfectlyEquals(TSFVec2f(SecondValue).Value, Value);
2990 end;
2991
2992 procedure TSFVec2f.AssignLerp(const A: Double; Value1, Value2: TX3DField);
2993 begin
2994 Value := TVector2.Lerp(A, (Value1 as TSFVec2f).Value, (Value2 as TSFVec2f).Value);
2995 end;
2996
2997 function TSFVec2f.CanAssignLerp: boolean;
2998 begin
2999 Result := true;
3000 end;
3001
3002 procedure TSFVec2f.Assign(Source: TPersistent);
3003 begin
3004 if Source is TSFVec2f then
3005 begin
3006 DefaultValue := TSFVec2f(Source).DefaultValue;
3007 DefaultValueExists := TSFVec2f(Source).DefaultValueExists;
3008 Value := TSFVec2f(Source).Value;
3009 VRMLFieldAssignCommon(TX3DField(Source));
3010 end else
3011 inherited;
3012 end;
3013
3014 procedure TSFVec2f.AssignValue(Source: TX3DField);
3015 begin
3016 if Source is TSFVec2f then
3017 begin
3018 inherited;
3019 Value := TSFVec2f(Source).Value;
3020 end else
3021 AssignValueRaiseInvalidClass(Source);
3022 end;
3023
3024 procedure TSFVec2f.AssignDefaultValueFromValue;
3025 begin
3026 inherited;
3027 DefaultValue := Value;
3028 DefaultValueExists := true;
3029 end;
3030
3031 procedure TSFVec2f.UnassignDefaultValue;
3032 begin
3033 DefaultValueExists := false;
3034 end;
3035
3036 class function TSFVec2f.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
3037 begin
3038 Result := TSFVec2fEvent.Create(AParentNode, AName, AInEvent);
3039 end;
3040
3041 procedure TSFVec2f.Send(const AValue: TVector2);
3042 var
3043 FieldValue: TSFVec2f;
3044 begin
3045 FieldValue := TSFVec2f(
3046 TX3DFieldClass(ClassType).CreateUndefined(ParentNode, false, X3DName));
3047 try
3048 FieldValue.Value := AValue;
3049 Send(FieldValue);
3050 finally FreeAndNil(FieldValue) end;
3051 end;
3052 {$endif FPC}
3053
3054 { TSFVec3f ------------------------------------------------------------------- }
3055
3056 class function TSFVec3f.X3DType: String;
3057 begin
3058 Result := 'SFVec3f';
3059 end;
3060
3061 procedure TSFVec3f.Send(const Index: Integer; const ComponentValue: Single);
3062 var
3063 V: TVector3;
3064 begin
3065 V := Value;
3066 V.Data[Index] := ComponentValue;
3067 Send(V);
3068 end;
3069
3070 {$ifndef FPC}
3071 constructor TSFVec3f.Create(const AParentNode: TX3DFileItem;
3072 const AExposed: boolean; const AName: String; const AValue: TVector3);
3073 begin
3074 CreateUndefined(AParentNode, AExposed, AName);
3075
3076 Value := AValue;
3077 AssignDefaultValueFromValue;
3078 end;
3079
3080 procedure TSFVec3f.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
3081 var
3082 I: Integer;
3083 begin
3084 for I := 0 to Value.Count - 1 do
3085 begin
3086 Lexer.CheckTokenIs(TokenNumbers, 'float number');
3087 Value.Data[I] := Lexer.TokenFloat;
3088 Lexer.NextToken;
3089 end;
3090
3091 // Calling ParseVector or ParseFloat here causes FPC 3.0.2 error
3092 // Error: Global Generic template references static symtable
3093 // TODO: check on other FPC versions and report.
3094 // ParseVector(Value.Data, Lexer);
3095 end;
3096
3097 procedure TSFVec3f.SaveToStreamValue(Writer: TX3DWriter);
3098 begin
3099 Writer.Write(Value.ToRawString(FloatOutputFormat));
3100 end;
3101
3102 function TSFVec3f.EqualsDefaultValue: boolean;
3103 begin
3104 Result := DefaultValueExists and TVector3.PerfectlyEquals(DefaultValue, Value);
3105 end;
3106
3107 function TSFVec3f.Equals(SecondValue: TX3DField): boolean;
3108 begin
3109 Result := (inherited Equals(SecondValue)) and
3110 (SecondValue is TSFVec3f) and
3111 TVector3.Equals(TSFVec3f(SecondValue).Value, Value);
3112 end;
3113
3114 function TSFVec3f.FastEqualsValue(SecondValue: TX3DField): boolean;
3115 begin
3116 Result := (SecondValue is TSFVec3f) and
3117 TVector3.PerfectlyEquals(TSFVec3f(SecondValue).Value, Value);
3118 end;
3119
3120 procedure TSFVec3f.AssignLerp(const A: Double; Value1, Value2: TX3DField);
3121 begin
3122 Value := TVector3.Lerp(A, (Value1 as TSFVec3f).Value, (Value2 as TSFVec3f).Value);
3123 end;
3124
3125 function TSFVec3f.CanAssignLerp: boolean;
3126 begin
3127 Result := true;
3128 end;
3129
3130 procedure TSFVec3f.Assign(Source: TPersistent);
3131 begin
3132 if Source is TSFVec3f then
3133 begin
3134 DefaultValue := TSFVec3f(Source).DefaultValue;
3135 DefaultValueExists := TSFVec3f(Source).DefaultValueExists;
3136 Value := TSFVec3f(Source).Value;
3137 VRMLFieldAssignCommon(TX3DField(Source));
3138 end else
3139 inherited;
3140 end;
3141
3142 procedure TSFVec3f.AssignValue(Source: TX3DField);
3143 begin
3144 if Source is TSFVec3f then
3145 begin
3146 inherited;
3147 Value := TSFVec3f(Source).Value;
3148 end else
3149 AssignValueRaiseInvalidClass(Source);
3150 end;
3151
3152 procedure TSFVec3f.AssignDefaultValueFromValue;
3153 begin
3154 inherited;
3155 DefaultValue := Value;
3156 DefaultValueExists := true;
3157 end;
3158
3159 procedure TSFVec3f.UnassignDefaultValue;
3160 begin
3161 DefaultValueExists := false;
3162 end;
3163
3164 class function TSFVec3f.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
3165 begin
3166 Result := TSFVec3fEvent.Create(AParentNode, AName, AInEvent);
3167 end;
3168
3169 procedure TSFVec3f.Send(const AValue: TVector3);
3170 var
3171 FieldValue: TSFVec3f;
3172 begin
3173 FieldValue := TSFVec3f(
3174 TX3DFieldClass(ClassType).CreateUndefined(ParentNode, false, X3DName));
3175 try
3176 FieldValue.Value := AValue;
3177 Send(FieldValue);
3178 finally FreeAndNil(FieldValue) end;
3179 end;
3180 {$endif FPC}
3181
3182
3183 { TSFColor ------------------------------------------------------------------- }
3184
3185 class function TSFColor.X3DType: String;
3186 begin
3187 Result := 'SFColor';
3188 end;
3189
3190 class function TSFColor.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
3191 begin
3192 Result := TSFColorEvent.Create(AParentNode, AName, AInEvent);
3193 end;
3194
3195 { TSFVec4f ------------------------------------------------------------------- }
3196
3197 class function TSFVec4f.X3DType: String;
3198 begin
3199 Result := 'SFVec4f';
3200 end;
3201
3202 {$ifndef FPC}
3203 constructor TSFVec4f.Create(const AParentNode: TX3DFileItem;
3204 const AExposed: boolean; const AName: String; const AValue: TVector4);
3205 begin
3206 CreateUndefined(AParentNode, AExposed, AName);
3207
3208 Value := AValue;
3209 AssignDefaultValueFromValue;
3210 end;
3211
3212 procedure TSFVec4f.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
3213 var
3214 I: Integer;
3215 begin
3216 for I := 0 to Value.Count - 1 do
3217 begin
3218 Lexer.CheckTokenIs(TokenNumbers, 'float number');
3219 Value.Data[I] := Lexer.TokenFloat;
3220 Lexer.NextToken;
3221 end;
3222
3223 // Calling ParseVector or ParseFloat here causes FPC 3.0.2 error
3224 // Error: Global Generic template references static symtable
3225 // TODO: check on other FPC versions and report.
3226 // ParseVector(Value.Data, Lexer);
3227 end;
3228
3229 procedure TSFVec4f.SaveToStreamValue(Writer: TX3DWriter);
3230 begin
3231 Writer.Write(Value.ToRawString(FloatOutputFormat));
3232 end;
3233
3234 function TSFVec4f.EqualsDefaultValue: boolean;
3235 begin
3236 Result := DefaultValueExists and TVector4.PerfectlyEquals(DefaultValue, Value);
3237 end;
3238
3239 function TSFVec4f.Equals(SecondValue: TX3DField): boolean;
3240 begin
3241 Result := (inherited Equals(SecondValue)) and
3242 (SecondValue is TSFVec4f) and
3243 TVector4.Equals(TSFVec4f(SecondValue).Value, Value);
3244 end;
3245
3246 function TSFVec4f.FastEqualsValue(SecondValue: TX3DField): boolean;
3247 begin
3248 Result := (SecondValue is TSFVec4f) and
3249 TVector4.PerfectlyEquals(TSFVec4f(SecondValue).Value, Value);
3250 end;
3251
3252 procedure TSFVec4f.AssignLerp(const A: Double; Value1, Value2: TX3DField);
3253 begin
3254 Value := TVector4.Lerp(A, (Value1 as TSFVec4f).Value, (Value2 as TSFVec4f).Value);
3255 end;
3256
3257 function TSFVec4f.CanAssignLerp: boolean;
3258 begin
3259 Result := true;
3260 end;
3261
3262 procedure TSFVec4f.Assign(Source: TPersistent);
3263 begin
3264 if Source is TSFVec4f then
3265 begin
3266 DefaultValue := TSFVec4f(Source).DefaultValue;
3267 DefaultValueExists := TSFVec4f(Source).DefaultValueExists;
3268 Value := TSFVec4f(Source).Value;
3269 VRMLFieldAssignCommon(TX3DField(Source));
3270 end else
3271 inherited;
3272 end;
3273
3274 procedure TSFVec4f.AssignValue(Source: TX3DField);
3275 begin
3276 if Source is TSFVec4f then
3277 begin
3278 inherited;
3279 Value := TSFVec4f(Source).Value;
3280 end else
3281 AssignValueRaiseInvalidClass(Source);
3282 end;
3283
3284 procedure TSFVec4f.AssignDefaultValueFromValue;
3285 begin
3286 inherited;
3287 DefaultValue := Value;
3288 DefaultValueExists := true;
3289 end;
3290
3291 procedure TSFVec4f.UnassignDefaultValue;
3292 begin
3293 DefaultValueExists := false;
3294 end;
3295
3296 class function TSFVec4f.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
3297 begin
3298 Result := TSFVec4fEvent.Create(AParentNode, AName, AInEvent);
3299 end;
3300
3301 procedure TSFVec4f.Send(const AValue: TVector4);
3302 var
3303 FieldValue: TSFVec4f;
3304 begin
3305 FieldValue := TSFVec4f(
3306 TX3DFieldClass(ClassType).CreateUndefined(ParentNode, false, X3DName));
3307 try
3308 FieldValue.Value := AValue;
3309 Send(FieldValue);
3310 finally FreeAndNil(FieldValue) end;
3311 end;
3312 {$endif FPC}
3313
3314
3315 { TSFColorRGBA --------------------------------------------------------------- }
3316
3317 class function TSFColorRGBA.X3DType: String;
3318 begin
3319 Result := 'SFColorRGBA';
3320 end;
3321
3322 class function TSFColorRGBA.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
3323 begin
3324 Result := TSFColorRGBAEvent.Create(AParentNode, AName, AInEvent);
3325 end;
3326
3327 { TSFVec2d ------------------------------------------------------------------- }
3328
3329 class function TSFVec2d.X3DType: String;
3330 begin
3331 Result := 'SFVec2d';
3332 end;
3333
3334 {$ifndef FPC}
3335 constructor TSFVec2d.Create(const AParentNode: TX3DFileItem;
3336 const AExposed: boolean; const AName: String; const AValue: TVector2Double);
3337 begin
3338 CreateUndefined(AParentNode, AExposed, AName);
3339
3340 Value := AValue;
3341 AssignDefaultValueFromValue;
3342 end;
3343
3344 procedure TSFVec2d.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
3345 var
3346 I: Integer;
3347 begin
3348 for I := 0 to Value.Count - 1 do
3349 begin
3350 Lexer.CheckTokenIs(TokenNumbers, 'float number');
3351 Value.Data[I] := Lexer.TokenFloat;
3352 Lexer.NextToken;
3353 end;
3354
3355 // Calling ParseVector or ParseFloat here causes FPC 3.0.2 error
3356 // Error: Global Generic template references static symtable
3357 // TODO: check on other FPC versions and report.
3358 // ParseVector(Value.Data, Lexer);
3359 end;
3360
3361 procedure TSFVec2d.SaveToStreamValue(Writer: TX3DWriter);
3362 begin
3363 Writer.Write(Value.ToRawString(FloatOutputFormat));
3364 end;
3365
3366 function TSFVec2d.EqualsDefaultValue: boolean;
3367 begin
3368 Result := DefaultValueExists and TVector2Double.PerfectlyEquals(DefaultValue, Value);
3369 end;
3370
3371 function TSFVec2d.Equals(SecondValue: TX3DField): boolean;
3372 begin
3373 Result := (inherited Equals(SecondValue)) and
3374 (SecondValue is TSFVec2d) and
3375 TVector2Double.Equals(TSFVec2d(SecondValue).Value, Value);
3376 end;
3377
3378 function TSFVec2d.FastEqualsValue(SecondValue: TX3DField): boolean;
3379 begin
3380 Result := (SecondValue is TSFVec2d) and
3381 TVector2Double.PerfectlyEquals(TSFVec2d(SecondValue).Value, Value);
3382 end;
3383
3384 procedure TSFVec2d.AssignLerp(const A: Double; Value1, Value2: TX3DField);
3385 begin
3386 Value := TVector2Double.Lerp(A, (Value1 as TSFVec2d).Value, (Value2 as TSFVec2d).Value);
3387 end;
3388
3389 function TSFVec2d.CanAssignLerp: boolean;
3390 begin
3391 Result := true;
3392 end;
3393
3394 procedure TSFVec2d.Assign(Source: TPersistent);
3395 begin
3396 if Source is TSFVec2d then
3397 begin
3398 DefaultValue := TSFVec2d(Source).DefaultValue;
3399 DefaultValueExists := TSFVec2d(Source).DefaultValueExists;
3400 Value := TSFVec2d(Source).Value;
3401 VRMLFieldAssignCommon(TX3DField(Source));
3402 end else
3403 inherited;
3404 end;
3405
3406 procedure TSFVec2d.AssignValue(Source: TX3DField);
3407 begin
3408 if Source is TSFVec2d then
3409 begin
3410 inherited;
3411 Value := TSFVec2d(Source).Value;
3412 end else
3413 AssignValueRaiseInvalidClass(Source);
3414 end;
3415
3416 procedure TSFVec2d.AssignDefaultValueFromValue;
3417 begin
3418 inherited;
3419 DefaultValue := Value;
3420 DefaultValueExists := true;
3421 end;
3422
3423 procedure TSFVec2d.UnassignDefaultValue;
3424 begin
3425 DefaultValueExists := false;
3426 end;
3427
3428 class function TSFVec2d.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
3429 begin
3430 Result := TSFVec2dEvent.Create(AParentNode, AName, AInEvent);
3431 end;
3432
3433 procedure TSFVec2d.Send(const AValue: TVector2Double);
3434 var
3435 FieldValue: TSFVec2d;
3436 begin
3437 FieldValue := TSFVec2d(
3438 TX3DFieldClass(ClassType).CreateUndefined(ParentNode, false, X3DName));
3439 try
3440 FieldValue.Value := AValue;
3441 Send(FieldValue);
3442 finally FreeAndNil(FieldValue) end;
3443 end;
3444 {$endif FPC}
3445
3446
3447 { TSFVec3d ------------------------------------------------------------------- }
3448
3449 class function TSFVec3d.X3DType: String;
3450 begin
3451 Result := 'SFVec3d';
3452 end;
3453
3454 {$ifndef FPC}
3455 constructor TSFVec3d.Create(const AParentNode: TX3DFileItem;
3456 const AExposed: boolean; const AName: String; const AValue: TVector3Double);
3457 begin
3458 CreateUndefined(AParentNode, AExposed, AName);
3459
3460 Value := AValue;
3461 AssignDefaultValueFromValue;
3462 end;
3463
3464 procedure TSFVec3d.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
3465 var
3466 I: Integer;
3467 begin
3468 for I := 0 to Value.Count - 1 do
3469 begin
3470 Lexer.CheckTokenIs(TokenNumbers, 'float number');
3471 Value.Data[I] := Lexer.TokenFloat;
3472 Lexer.NextToken;
3473 end;
3474
3475 // Calling ParseVector or ParseFloat here causes FPC 3.0.2 error
3476 // Error: Global Generic template references static symtable
3477 // TODO: check on other FPC versions and report.
3478 // ParseVector(Value.Data, Lexer);
3479 end;
3480
3481 procedure TSFVec3d.SaveToStreamValue(Writer: TX3DWriter);
3482 begin
3483 Writer.Write(Value.ToRawString(FloatOutputFormat));
3484 end;
3485
3486 function TSFVec3d.EqualsDefaultValue: boolean;
3487 begin
3488 Result := DefaultValueExists and TVector3Double.PerfectlyEquals(DefaultValue, Value);
3489 end;
3490
3491 function TSFVec3d.Equals(SecondValue: TX3DField): boolean;
3492 begin
3493 Result := (inherited Equals(SecondValue)) and
3494 (SecondValue is TSFVec3d) and
3495 TVector3Double.Equals(TSFVec3d(SecondValue).Value, Value);
3496 end;
3497
3498 function TSFVec3d.FastEqualsValue(SecondValue: TX3DField): boolean;
3499 begin
3500 Result := (SecondValue is TSFVec3d) and
3501 TVector3Double.PerfectlyEquals(TSFVec3d(SecondValue).Value, Value);
3502 end;
3503
3504 procedure TSFVec3d.AssignLerp(const A: Double; Value1, Value2: TX3DField);
3505 begin
3506 Value := TVector3Double.Lerp(A, (Value1 as TSFVec3d).Value, (Value2 as TSFVec3d).Value);
3507 end;
3508
3509 function TSFVec3d.CanAssignLerp: boolean;
3510 begin
3511 Result := true;
3512 end;
3513
3514 procedure TSFVec3d.Assign(Source: TPersistent);
3515 begin
3516 if Source is TSFVec3d then
3517 begin
3518 DefaultValue := TSFVec3d(Source).DefaultValue;
3519 DefaultValueExists := TSFVec3d(Source).DefaultValueExists;
3520 Value := TSFVec3d(Source).Value;
3521 VRMLFieldAssignCommon(TX3DField(Source));
3522 end else
3523 inherited;
3524 end;
3525
3526 procedure TSFVec3d.AssignValue(Source: TX3DField);
3527 begin
3528 if Source is TSFVec3d then
3529 begin
3530 inherited;
3531 Value := TSFVec3d(Source).Value;
3532 end else
3533 AssignValueRaiseInvalidClass(Source);
3534 end;
3535
3536 procedure TSFVec3d.AssignDefaultValueFromValue;
3537 begin
3538 inherited;
3539 DefaultValue := Value;
3540 DefaultValueExists := true;
3541 end;
3542
3543 procedure TSFVec3d.UnassignDefaultValue;
3544 begin
3545 DefaultValueExists := false;
3546 end;
3547
3548 class function TSFVec3d.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
3549 begin
3550 Result := TSFVec3dEvent.Create(AParentNode, AName, AInEvent);
3551 end;
3552
3553 procedure TSFVec3d.Send(const AValue: TVector3Double);
3554 var
3555 FieldValue: TSFVec3d;
3556 begin
3557 FieldValue := TSFVec3d(
3558 TX3DFieldClass(ClassType).CreateUndefined(ParentNode, false, X3DName));
3559 try
3560 FieldValue.Value := AValue;
3561 Send(FieldValue);
3562 finally FreeAndNil(FieldValue) end;
3563 end;
3564 {$endif FPC}
3565
3566
3567 { TSFVec4d ------------------------------------------------------------------- }
3568
3569 class function TSFVec4d.X3DType: String;
3570 begin
3571 Result := 'SFVec4d';
3572 end;
3573
3574 {$ifndef FPC}
3575 constructor TSFVec4d.Create(const AParentNode: TX3DFileItem;
3576 const AExposed: boolean; const AName: String; const AValue: TVector4Double);
3577 begin
3578 CreateUndefined(AParentNode, AExposed, AName);
3579
3580 Value := AValue;
3581 AssignDefaultValueFromValue;
3582 end;
3583
3584 procedure TSFVec4d.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
3585 var
3586 I: Integer;
3587 begin
3588 for I := 0 to Value.Count - 1 do
3589 begin
3590 Lexer.CheckTokenIs(TokenNumbers, 'float number');
3591 Value.Data[I] := Lexer.TokenFloat;
3592 Lexer.NextToken;
3593 end;
3594
3595 // Calling ParseVector or ParseFloat here causes FPC 3.0.2 error
3596 // Error: Global Generic template references static symtable
3597 // TODO: check on other FPC versions and report.
3598 // ParseVector(Value.Data, Lexer);
3599 end;
3600
3601 procedure TSFVec4d.SaveToStreamValue(Writer: TX3DWriter);
3602 begin
3603 Writer.Write(Value.ToRawString(FloatOutputFormat));
3604 end;
3605
3606 function TSFVec4d.EqualsDefaultValue: boolean;
3607 begin
3608 Result := DefaultValueExists and TVector4Double.PerfectlyEquals(DefaultValue, Value);
3609 end;
3610
3611 function TSFVec4d.Equals(SecondValue: TX3DField): boolean;
3612 begin
3613 Result := (inherited Equals(SecondValue)) and
3614 (SecondValue is TSFVec3d) and
3615 TVector4Double.Equals(TSFVec4d(SecondValue).Value, Value);
3616 end;
3617
3618 function TSFVec4d.FastEqualsValue(SecondValue: TX3DField): boolean;
3619 begin
3620 Result := (SecondValue is TSFVec4d) and
3621 TVector4Double.PerfectlyEquals(TSFVec4d(SecondValue).Value, Value);
3622 end;
3623
3624 procedure TSFVec4d.AssignLerp(const A: Double; Value1, Value2: TX3DField);
3625 begin
3626 Value := TVector4Double.Lerp(A, (Value1 as TSFVec4d).Value, (Value2 as TSFVec4d).Value);
3627 end;
3628
3629 function TSFVec4d.CanAssignLerp: boolean;
3630 begin
3631 Result := true;
3632 end;
3633
3634 procedure TSFVec4d.Assign(Source: TPersistent);
3635 begin
3636 if Source is TSFVec4d then
3637 begin
3638 DefaultValue := TSFVec4d(Source).DefaultValue;
3639 DefaultValueExists := TSFVec4d(Source).DefaultValueExists;
3640 Value := TSFVec4d(Source).Value;
3641 VRMLFieldAssignCommon(TX3DField(Source));
3642 end else
3643 inherited;
3644 end;
3645
3646 procedure TSFVec4d.AssignValue(Source: TX3DField);
3647 begin
3648 if Source is TSFVec4d then
3649 begin
3650 inherited;
3651 Value := TSFVec4d(Source).Value;
3652 end else
3653 AssignValueRaiseInvalidClass(Source);
3654 end;
3655
3656 procedure TSFVec4d.UnassignDefaultValue;
3657 begin
3658 DefaultValueExists := false;
3659 end;
3660
3661 procedure TSFVec4d.AssignDefaultValueFromValue;
3662 begin
3663 inherited;
3664 DefaultValue := Value;
3665 DefaultValueExists := true;
3666 end;
3667
3668 class function TSFVec4d.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
3669 begin
3670 Result := TSFVec4dEvent.Create(AParentNode, AName, AInEvent);
3671 end;
3672
3673 procedure TSFVec4d.Send(const AValue: TVector4Double);
3674 var
3675 FieldValue: TSFVec4d;
3676 begin
3677 FieldValue := TSFVec4d(
3678 TX3DFieldClass(ClassType).CreateUndefined(ParentNode, false, X3DName));
3679 try
3680 FieldValue.Value := AValue;
3681 Send(FieldValue);
3682 finally FreeAndNil(FieldValue) end;
3683 end;
3684 {$endif FPC}
3685
3686
3687 { TSFBitMask ------------------------------------------------------------ }
3688
3689 constructor TSFBitMask.Create(const AParentNode: TX3DFileItem;
3690 const AExposed: boolean; const AName: String; const AFlagNames: array of string;
3691 const ANoneString, AAllString: String; const AFlags: array of boolean);
3692 var
3693 i: integer;
3694 begin
3695 CreateUndefined(AParentNode, AExposed, AName);
3696
3697 fFlagNames := TStringListCaseSens.Create;
3698 AddStrArrayToStrings(AFlagNames, fFlagNames);
3699 for i := 0 to FlagsCount-1 do Flags[i] := AFlags[i];
3700 fNoneString := ANoneString;
3701 fAllString := AAllString;
3702
3703 Assert(NoneString <> '', 'NoneString must be defined for SFBitMask');
3704 end;
3705
3706 destructor TSFBitMask.Destroy;
3707 begin
3708 fFlagNames.Free;
3709 inherited;
3710 end;
3711
3712 function TSFBitMask.GetFlags(i: integer): boolean;
3713 begin
3714 result := i in fFlags
3715 end;
3716
3717 procedure TSFBitMask.SetFlags(i: integer; value: boolean);
3718 begin
3719 if value then Include(fFlags, i) else Exclude(fFlags, i)
3720 end;
3721
3722 function TSFBitMask.FlagsCount: integer;
3723 begin
3724 result := fFlagNames.Count
3725 end;
3726
3727 function TSFBitMask.GetFlagNames(i: integer): String;
3728 begin
3729 result := fFlagNames[i]
3730 end;
3731
3732 procedure TSFBitMask.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
3733
3734 procedure InterpretTokenAsFlagName;
3735 var
3736 i: integer;
3737 begin
3738 Lexer.CheckTokenIs(vtName, 'bit mask constant');
3739 i := fFlagNames.IndexOf(Lexer.TokenName);
3740 if i >= 0 then
3741 Flags[i] := true else
3742 if Lexer.TokenName = fAllString then
3743 fFlags := [0..FlagsCount-1] else
3744 if Lexer.TokenName = fNoneString then
3745 { Don't set anything. Note that this doesn't clear other flags,
3746 so e.g. "( FLAG_1 | NONE )" equals just "FLAG_1". } else
3747 raise EX3DParserError.Create(Lexer,
3748 'Expected bit mask constant, got '+Lexer.DescribeToken);
3749 end;
3750
3751 begin
3752 fFlags:=[];
3753
3754 if Lexer.Token = vtOpenBracket then
3755 begin
3756 repeat
3757 Lexer.NextToken;
3758 InterpretTokenAsFlagName;
3759 Lexer.NextToken;
3760 until Lexer.Token <> vtBar;
3761 Lexer.CheckTokenIs(vtCloseBracket);
3762 Lexer.NextToken;
3763 end else
3764 begin
3765 InterpretTokenAsFlagName;
3766 Lexer.NextToken;
3767 end;
3768 end;
3769
3770 function TSFBitMask.AreAllFlags(value: boolean): boolean;
3771 var
3772 i: integer;
3773 begin
3774 for i := 0 to FlagsCount-1 do
3775 if Flags[i] <> value then exit(false);
3776 exit(true);
3777 end;
3778
3779 procedure TSFBitMask.SaveToStreamValue(Writer: TX3DWriter);
3780 var
3781 i: integer;
3782 PrecedeWithBar: boolean;
3783 begin
3784 { This is an VRML 1.0 (and Inventor) type. The existing specs only say
3785 how to encode it for classic encoding. For XML, we just use the same format. }
3786 if AreAllFlags(false) then
3787 Writer.Write(NoneString) else
3788 begin
3789 { We don't really need AllString to express that all bit are set
3790 (we could as well just name them all), but it looks nicer. }
3791 if (AllString <> '') and AreAllFlags(true) then
3792 Writer.Write(AllString) else
3793 begin
3794 PrecedeWithBar := false;
3795 Writer.Write('(');
3796 for i := 0 to FlagsCount-1 do
3797 if Flags[i] then
3798 begin
3799 if PrecedeWithBar then Writer.Write('|') else PrecedeWithBar := true;
3800 Writer.Write(FlagNames[i]);
3801 end;
3802 Writer.Write(')');
3803 end;
3804 end;
3805 end;
3806
3807 function TSFBitMask.Equals(SecondValue: TX3DField): boolean;
3808 begin
3809 Result := (inherited Equals(SecondValue)) and
3810 (SecondValue is TSFBitMask) and
3811 (TSFBitMask(SecondValue).FFlagNames.Equals(FFlagNames)) and
3812 (TSFBitMask(SecondValue).FFlags = FFlags) and
3813 (TSFBitMask(SecondValue).AllString = AllString) and
3814 (TSFBitMask(SecondValue).NoneString = NoneString);
3815 end;
3816
3817 procedure TSFBitMask.Assign(Source: TPersistent);
3818 begin
3819 if Source is TSFBitMask then
3820 begin
3821 FAllString := TSFBitMask(Source).AllString;
3822 FNoneString := TSFBitMask(Source).NoneString;
3823 FFlags := TSFBitMask(Source).FFlags;
3824 FFlagNames.Assign(TSFBitMask(Source).FFlagNames);
3825 VRMLFieldAssignCommon(TX3DField(Source));
3826 end else
3827 inherited;
3828 end;
3829
3830 procedure TSFBitMask.AssignValue(Source: TX3DField);
3831 begin
3832 if Source is TSFBitMask then
3833 begin
3834 inherited;
3835 FFlags := TSFBitMask(Source).FFlags;
3836 end else
3837 AssignValueRaiseInvalidClass(Source);
3838 end;
3839
3840 class function TSFBitMask.X3DType: String;
3841 begin
3842 Result := 'SFBitMask';
3843 end;
3844
3845 { TSFEnum ----------------------------------------------------------------- }
3846
3847 constructor TSFEnum.Create(const AParentNode: TX3DFileItem;
3848 const AExposed: boolean; const AName: String;
3849 const AEnumNames: PConstantStringArray; const AEnumNamesCount: Cardinal;
3850 const AValue: integer);
3851 begin
3852 CreateUndefined(AParentNode, AExposed, AName);
3853
3854 FEnumNames := AEnumNames;
3855 FEnumNamesCount := AEnumNamesCount;
3856 Assert(AEnumNamesCount > 0); // this makes also "FEnumNamesCount - 1" in for loops a valid Cardinal
3857
3858 Value := AValue;
3859 AssignDefaultValueFromValue;
3860 end;
3861
3862 function TSFEnum.GetEnumNames(i: integer): String;
3863 begin
3864 result := FEnumNames^[i];
3865 end;
3866
3867 procedure TSFEnum.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
3868
3869 function EnumNamesIndexOf(const S: String): Integer;
3870 begin
3871 for Result := 0 to FEnumNamesCount - 1 do
3872 if FEnumNames^[Result] = S then
3873 Exit;
3874 Result := -1;
3875 end;
3876
3877 var
3878 val: integer;
3879 begin
3880 Lexer.CheckTokenIs(vtName, 'enumerated type constant');
3881 val := EnumNamesIndexOf(Lexer.TokenName);
3882 if val = -1 then
3883 raise EX3DParserError.Create(Lexer,
3884 'Expected enumerated type constant, got '+Lexer.DescribeToken);
3885 Value := val;
3886 Lexer.NextToken;
3887 end;
3888
3889 procedure TSFEnum.SaveToStreamValue(Writer: TX3DWriter);
3890 begin
3891 Writer.Write(EnumNames[Value]);
3892 end;
3893
3894 function TSFEnum.EqualsDefaultValue: boolean;
3895 begin
3896 result := DefaultValueExists and (DefaultValue = Value);
3897 end;
3898
3899 function TSFEnum.Equals(SecondValue: TX3DField): boolean;
3900 begin
3901 Result := (inherited Equals(SecondValue)) and
3902 (SecondValue is TSFEnum) and
3903 (TSFEnum(SecondValue).Value = Value);
3904 end;
3905
3906 procedure TSFEnum.Assign(Source: TPersistent);
3907 begin
3908 if Source is TSFEnum then
3909 begin
3910 DefaultValue := TSFEnum(Source).DefaultValue;
3911 DefaultValueExists := TSFEnum(Source).DefaultValueExists;
3912 Value := TSFEnum(Source).Value;
3913 VRMLFieldAssignCommon(TX3DField(Source));
3914 end else
3915 inherited;
3916 end;
3917
3918 procedure TSFEnum.AssignValue(Source: TX3DField);
3919 begin
3920 if Source is TSFEnum then
3921 begin
3922 inherited;
3923 Value := TSFEnum(Source).Value;
3924 end else
3925 AssignValueRaiseInvalidClass(Source);
3926 end;
3927
3928 procedure TSFEnum.AssignDefaultValueFromValue;
3929 begin
3930 inherited;
3931 DefaultValue := Value;
3932 DefaultValueExists := true;
3933 end;
3934
3935 procedure TSFEnum.UnassignDefaultValue;
3936 begin
3937 DefaultValueExists := false;
3938 end;
3939
3940 class function TSFEnum.X3DType: String;
3941 begin
3942 Result := 'SFEnum';
3943 end;
3944
3945 const
3946 FakeEnumStrings: array [0..0] of String = ('');
3947
3948 procedure TSFEnum.Send(const AValue: Int32);
3949 var
3950 FieldValue: TX3DField;
3951 begin
3952 FieldValue := TSFEnum.Create(ParentNode, false, X3DName, @FakeEnumStrings, High(FakeEnumStrings) + 1, AValue);
3953 try
3954 Send(FieldValue);
3955 finally FreeAndNil(FieldValue) end;
3956 end;
3957
3958 {$endif read_implementation}