Debian Codegraph @

src/scene/x3d/castlefields_x3dsimplemultfield_descendants.inc

raw
1 {%MainUnit x3dfields.pas}
2 {
3 Copyright 2002-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 {$ifdef read_interface}
18
19 { Multiple value (MF) fields ----------------------------------------------- }
20
21 { }
22 TMFBool = class({$ifdef FPC}specialize{$endif} TX3DSimpleMultField<
23 boolean,
24 TSFBool,
25 TBooleanList>)
26 strict protected
27 function RawItemToString(ItemNum: Integer; const Encoding: TX3DEncoding): String; override;
28 procedure AddToList(const ItemList: TBooleanList; const Item: TSFBool); override;
29 function CreateItemBeforeParse: TSFBool; override;
30 public
31 class function X3DType: String; override;
32 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
33 end;
34
35 TMFLong = class({$ifdef FPC}specialize{$endif} TX3DSimpleMultField<
36 Int32,
37 TSFLong,
38 TInt32List>)
39 strict private
40 FSaveToStreamLineUptoNegative: boolean;
41 WrongVertexIndexWarnings: Integer;
42 strict protected
43 function RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String; override;
44 procedure AddToList(const ItemList: TInt32List; const Item: TSFLong); override;
45 function SaveToStreamDoNewLineAfterRawItem(ItemNum: integer): boolean; override;
46 function CreateItemBeforeParse: TSFLong; override;
47 public
48 { When saving to VRML/X3D classic encoding,
49 make newline after each negative value on the list.
50 This makes a nice output for fields like IndexedFaceSet.coordIndex. }
51 property SaveToStreamLineUptoNegative: boolean
52 read FSaveToStreamLineUptoNegative write FSaveToStreamLineUptoNegative
53 default false;
54
55 class function X3DType: String; override;
56 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
57
58 { Call WritelnWarning reporting that an invalid vertex index
59 is caused from this field. This simply calls WritelnWarning
60 formatting appropriate message.
61
62 Additionally this guards
63 us against producing too many warnings from the same field.
64 When a given threshold will be reached, further
65 WritelnWarning_WrongVertexIndex calls for this field instance
66 will be simply ignored. This is a good thing, as some invalid models
67 have really an incredible amount of invalid indexes, and the very
68 amount of lines printed on console makes viewing these invalid files
69 (at least, the valid parts of them) impossible.
70
71 Example test cases:
72 content/examples/Basic/HumanoidAnimation/AllenStandShootRifleM24.x3d
73 and
74 content/examples/Basic/HumanoidAnimation/NancyDiving.x3dv
75 from http://www.web3d.org/ example models. }
76 procedure WritelnWarning_WrongVertexIndex(
77 const GeometryX3DType: String;
78 const VertexNum: Integer; const CoordCount: Integer);
79 end;
80
81 TMFInt32 = class(TMFLong)
82 public
83 class function X3DType: String; override;
84 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
85 end;
86
87 TMFMatrix3f = class({$ifdef FPC}specialize{$endif} TX3DSimpleMultField<
88 TMatrix3,
89 TSFMatrix3f,
90 TMatrix3List>)
91 strict protected
92 function RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String; override;
93 procedure AddToList(const ItemList: TMatrix3List; const Item: TSFMatrix3f); override;
94 function CreateItemBeforeParse: TSFMatrix3f; override;
95 public
96 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
97 function CanAssignLerp: boolean; override;
98 class function X3DType: String; override;
99 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
100 end;
101
102 TMFMatrix3d = class({$ifdef FPC}specialize{$endif} TX3DSimpleMultField<
103 TMatrix3Double,
104 TSFMatrix3d,
105 TMatrix3DoubleList>)
106 strict protected
107 function RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String; override;
108 procedure AddToList(const ItemList: TMatrix3DoubleList; const Item: TSFMatrix3d); override;
109 function CreateItemBeforeParse: TSFMatrix3d; override;
110 public
111 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
112 function CanAssignLerp: boolean; override;
113 class function X3DType: String; override;
114 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
115 end;
116
117 TMFMatrix4f = class({$ifdef FPC}specialize{$endif} TX3DSimpleMultField<
118 TMatrix4,
119 TSFMatrix4f,
120 TMatrix4List>)
121 strict protected
122 function RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String; override;
123 procedure AddToList(const ItemList: TMatrix4List; const Item: TSFMatrix4f); override;
124 function CreateItemBeforeParse: TSFMatrix4f; override;
125 public
126 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
127 function CanAssignLerp: boolean; override;
128 class function X3DType: String; override;
129 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
130 end;
131
132 TMFMatrix4d = class({$ifdef FPC}specialize{$endif} TX3DSimpleMultField<
133 TMatrix4Double,
134 TSFMatrix4d,
135 TMatrix4DoubleList>)
136 strict protected
137 function RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String; override;
138 procedure AddToList(const ItemList: TMatrix4DoubleList; const Item: TSFMatrix4d); override;
139 function CreateItemBeforeParse: TSFMatrix4d; override;
140 public
141 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
142 function CanAssignLerp: boolean; override;
143 class function X3DType: String; override;
144 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
145 end;
146
147 TMFVec2f = class({$ifdef FPC}specialize{$endif} TX3DSimpleMultField<
148 TVector2,
149 TSFVec2f,
150 TVector2List>)
151 strict protected
152 function RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String; override;
153 procedure AddToList(const ItemList: TVector2List; const Item: TSFVec2f); override;
154 function CreateItemBeforeParse: TSFVec2f; override;
155 public
156 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
157 function CanAssignLerp: boolean; override;
158 class function X3DType: String; override;
159 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
160 end;
161
162 TMFVec3f = class({$ifdef FPC}specialize{$endif} TX3DSimpleMultField<
163 TVector3,
164 TSFVec3f,
165 TVector3List>)
166 strict protected
167 function RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String; override;
168 procedure AddToList(const ItemList: TVector3List; const Item: TSFVec3f); override;
169 function CreateItemBeforeParse: TSFVec3f; override;
170 public
171 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
172 function CanAssignLerp: boolean; override;
173 class function X3DType: String; override;
174 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
175 end;
176
177 TMFColor = class(TMFVec3f)
178 public
179 class function X3DType: String; override;
180 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
181 end;
182
183 TMFVec4f = class({$ifdef FPC}specialize{$endif} TX3DSimpleMultField<
184 TVector4,
185 TSFVec4f,
186 TVector4List>)
187 strict protected
188 function RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String; override;
189 procedure AddToList(const ItemList: TVector4List; const Item: TSFVec4f); override;
190 function CreateItemBeforeParse: TSFVec4f; override;
191 public
192 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
193 function CanAssignLerp: boolean; override;
194 class function X3DType: String; override;
195 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
196 end;
197
198 TMFColorRGBA = class(TMFVec4f)
199 public
200 class function X3DType: String; override;
201 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
202 end;
203
204 TMFVec2d = class({$ifdef FPC}specialize{$endif} TX3DSimpleMultField<
205 TVector2Double,
206 TSFVec2d,
207 TVector2DoubleList>)
208 strict protected
209 function RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String; override;
210 procedure AddToList(const ItemList: TVector2DoubleList; const Item: TSFVec2d); override;
211 function CreateItemBeforeParse: TSFVec2d; override;
212 public
213 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
214 function CanAssignLerp: boolean; override;
215 class function X3DType: String; override;
216 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
217 end;
218
219 TMFVec3d = class({$ifdef FPC}specialize{$endif} TX3DSimpleMultField<
220 TVector3Double,
221 TSFVec3d,
222 TVector3DoubleList>)
223 strict protected
224 function RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String; override;
225 procedure AddToList(const ItemList: TVector3DoubleList; const Item: TSFVec3d); override;
226 function CreateItemBeforeParse: TSFVec3d; override;
227 public
228 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
229 function CanAssignLerp: boolean; override;
230 class function X3DType: String; override;
231 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
232 end;
233
234 TMFVec4d = class({$ifdef FPC}specialize{$endif} TX3DSimpleMultField<
235 TVector4Double,
236 TSFVec4d,
237 TVector4DoubleList>)
238 strict protected
239 function RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String; override;
240 procedure AddToList(const ItemList: TVector4DoubleList; const Item: TSFVec4d); override;
241 function CreateItemBeforeParse: TSFVec4d; override;
242 public
243 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
244 function CanAssignLerp: boolean; override;
245 class function X3DType: String; override;
246 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
247 end;
248
249 TMFRotation = class({$ifdef FPC}specialize{$endif} TX3DSimpleMultField<
250 TVector4,
251 TSFRotation,
252 TVector4List>)
253 strict protected
254 function RawItemToString(ItemNum: Integer; const Encoding: TX3DEncoding): String; override;
255 procedure AddToList(const ItemList: TVector4List; const Item: TSFRotation); override;
256 function CreateItemBeforeParse: TSFRotation; override;
257 public
258 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
259 function CanAssignLerp: boolean; override;
260 class function X3DType: String; override;
261 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
262 end;
263
264 TMFFloat = class({$ifdef FPC}specialize{$endif} TX3DSimpleMultField<
265 Single,
266 TSFFloat,
267 TSingleList>)
268 strict private
269 FAngle: boolean;
270 strict protected
271 function RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String; override;
272 procedure AddToList(const ItemList: TSingleList; const Item: TSFFloat); override;
273 function SaveToStreamDoNewLineAfterRawItem(ItemNum: integer): boolean; override;
274 function CreateItemBeforeParse: TSFFloat; override;
275 public
276 { Value represents an angle. When reading from X3D 3.3 file, we will
277 make sure it's expressed in radians, honoring optional "UNIT angle ..."
278 declaration in X3D file. }
279 property Angle: boolean read FAngle write FAngle default false;
280
281 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
282 function CanAssignLerp: boolean; override;
283 class function X3DType: String; override;
284 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
285 end;
286
287 TMFDouble = class({$ifdef FPC}specialize{$endif} TX3DSimpleMultField<
288 Double,
289 TSFDouble,
290 TDoubleList>)
291 strict protected
292 function RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String; override;
293 procedure AddToList(const ItemList: TDoubleList; const Item: TSFDouble); override;
294 function SaveToStreamDoNewLineAfterRawItem(ItemNum: integer): boolean; override;
295 function CreateItemBeforeParse: TSFDouble; override;
296 public
297 procedure AssignLerp(const A: Double; Value1, Value2: TX3DField); override;
298 function CanAssignLerp: boolean; override;
299 class function X3DType: String; override;
300 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
301 end;
302
303 TMFTime = class(TMFDouble)
304 public
305 class function X3DType: String; override;
306 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
307 end;
308
309 TMFString = class(TX3DMultField)
310 strict private
311 WarningDoneInvalidIndex: boolean;
312
313 FItems: TCastleStringList;
314
315 { DefaultValuesCount may have three valid values (for now):
316 -1 (means "no default value for this field")
317 0 (means "default value of this field is empty")
318 1 (means "default value of this field is one-item array with DefaultValue").
319
320 So it's not possible to express default values with more
321 than one item. That's OK, because nodes from VRML 1.0 and 2.0 specifications
322 never have such field (and VRML 2.0 prototypes (that have user-defined
323 default field values) actually don't need it). So, for now, more flexible
324 DefaultValuesCount is not needed.
325
326 CreateUndefined sets DefaultValuesCount to -1. }
327 DefaultValuesCount: Integer;
328 DefaultValue: String;
329
330 procedure SetItems(const Value: TCastleStringList);
331 function GetItemsSafe(Index: Integer): String;
332 procedure SetItemsSafe(Index: Integer; const Value: String);
333 strict protected
334 procedure Clear;
335 function GetCount: TListSize; override;
336 procedure SetCount(const Value: TListSize); override;
337
338 function RawItemToString(ItemNum: Integer; const Encoding: TX3DEncoding): String;
339 procedure AddToList(const ItemList: TCastleStringList; const Item: TSFString);
340 procedure SaveToStreamValue(Writer: TX3DWriter); override;
341 function CreateItemBeforeParse: TSFString;
342
343 procedure WritelnWarning_InvalidIndex(const Index, ACount: Integer);
344 public
345 constructor Create(const AParentNode: TX3DFileItem;
346 const AExposed: boolean; const AName: String;
347 const InitialContent: array of string);
348
349 constructor CreateUndefined(const AParentNode: TX3DFileItem;
350 const AExposed: boolean; const AName: String); override;
351 destructor Destroy; override;
352 function FastEqualsValue(SecondValue: TX3DField): boolean; override;
353
354 { Parse MF field. This class handles parsing fully, usually no need to
355 override this more in descendants. It uses TSingleField.Parse method. }
356 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
357
358 class function X3DType: String; override;
359 class function CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent; override;
360 procedure ParseXMLAttribute(const AttributeValue: String; Reader: TX3DReader); override;
361 function SaveToXmlValue: TSaveToXmlMethod; override;
362
363 property Items: TCastleStringList read FItems write SetItems;
364 property ItemsSafe[Index: Integer]: String read GetItemsSafe write SetItemsSafe;
365
366 { Copy another field value, default value and other properties. }
367 procedure Assign(Source: TPersistent); override;
368 procedure AssignValue(Source: TX3DField); override;
369 procedure AssignDefaultValueFromValue; override;
370 procedure UnassignDefaultValue; override;
371
372 { Checks equality between this and SecondValue field.
373 In addition to inherited(Equals), this checks the actual contents
374 of the items.
375 Note that floating-point values are compared with some tolerance
376 for errors by this method. }
377 function Equals(SecondValue: TX3DField): boolean; override;
378 function EqualsDefaultValue: boolean; override;
379
380 { Set the field's value in a correct way (by sending X3D event, or at least notifying
381 the parent scene). }
382 procedure Send(const AValue: array of string); overload;
383 procedure Send(const AValue: TCastleStringList); overload;
384 end;
385
386 {$endif read_interface}
387
388 {$ifdef read_implementation}
389
390 { TMFBool ------------------------------------------------------------------ }
391
392 function TMFBool.RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String;
393 begin
394 Result := BoolKeywords[Encoding][Items[ItemNum]];
395 end;
396
397 procedure TMFBool.AddToList(const ItemList: TBooleanList; const Item: TSFBool);
398 begin
399 Items.Add(Item.Value);
400 end;
401
402 class function TMFBool.X3DType: String;
403 begin
404 Result := 'MFBool';
405 end;
406
407 class function TMFBool.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
408 begin
409 Result := TMFBoolEvent.Create(AParentNode, AName, AInEvent);
410 end;
411
412 function TMFBool.CreateItemBeforeParse: TSFBool;
413 begin
414 Result := TSFBool.CreateUndefined(ParentNode, false, '');
415 end;
416
417 { TMFLong -------------------------------------------------------------------- }
418
419 function TMFLong.SaveToStreamDoNewLineAfterRawItem(ItemNum: integer): boolean;
420 begin
421 Result := SaveToStreamLineUptoNegative and (Items[ItemNum] < 0);
422 end;
423
424 function TMFLong.RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String;
425 begin
426 Result := IntToStr(Items[ItemNum])
427 end;
428
429 procedure TMFLong.AddToList(const ItemList: TInt32List; const Item: TSFLong);
430 begin
431 Items.Add(Item.Value);
432 end;
433
434 class function TMFLong.X3DType: String;
435 begin
436 Result := 'MFLong';
437 end;
438
439 class function TMFLong.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
440 begin
441 Result := TMFLongEvent.Create(AParentNode, AName, AInEvent);
442 end;
443
444 function TMFLong.CreateItemBeforeParse: TSFLong;
445 begin
446 Result := TSFLong.CreateUndefined(ParentNode, false, '');
447 end;
448
449 procedure TMFLong.WritelnWarning_WrongVertexIndex(
450 const GeometryX3DType: String;
451 const VertexNum: Integer; const CoordCount: Integer);
452 const
453 MaxWrongVertexIndexWarnings = 10;
454 begin
455 Inc(WrongVertexIndexWarnings);
456 if WrongVertexIndexWarnings < MaxWrongVertexIndexWarnings then
457 WritelnWarning('X3D', Format('Wrong vertex index in indexed node %s (not enough points in Coordinate node defined: index is %d, we have only %d vertices)',
458 [GeometryX3DType, VertexNum, CoordCount]))
459 else
460 if WrongVertexIndexWarnings = MaxWrongVertexIndexWarnings then
461 WritelnWarning('X3D', Format('Wrong vertex index in indexed node %s reported for the %dth time. Further warnings regarding this field will not be reported (to avoid wasting time on printing countless warnings...)',
462 [GeometryX3DType, WrongVertexIndexWarnings]));
463 end;
464
465 { TMFInt32 ------------------------------------------------------------------- }
466
467 class function TMFInt32.X3DType: String;
468 begin
469 Result := 'MFInt32';
470 end;
471
472 class function TMFInt32.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
473 begin
474 Result := TMFInt32Event.Create(AParentNode, AName, AInEvent);
475 end;
476
477 { TMFMatrix3f ------------------------------------------------------------------- }
478
479 class function TMFMatrix3f.X3DType: String;
480 begin
481 Result := 'MFMatrix3f';
482 end;
483
484 class function TMFMatrix3f.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
485 begin
486 Result := TMFMatrix3fEvent.Create(AParentNode, AName, AInEvent);
487 end;
488
489 function TMFMatrix3f.CreateItemBeforeParse: TSFMatrix3f;
490 begin
491 Result := TSFMatrix3f.CreateUndefined(ParentNode, false, '');
492 end;
493
494 function TMFMatrix3f.RawItemToString(ItemNum: Integer; const Encoding: TX3DEncoding): String;
495 begin
496 Result := Items.L[ItemNum].ToRawString('', FloatOutputFormat);
497 end;
498
499 procedure TMFMatrix3f.AddToList(const ItemList: TMatrix3List; const Item: TSFMatrix3f);
500 begin
501 Items.Add(Item.Value);
502 end;
503
504 procedure TMFMatrix3f.AssignLerp(const A: Double; Value1, Value2: TX3DField);
505 begin
506 Items.AssignLerp(A, (Value1 as TMFMatrix3f).Items, (Value2 as TMFMatrix3f).Items);
507 end;
508
509 function TMFMatrix3f.CanAssignLerp: boolean;
510 begin
511 Result := true;
512 end;
513
514 { TMFMatrix3d ------------------------------------------------------------------- }
515
516 class function TMFMatrix3d.X3DType: String;
517 begin
518 Result := 'MFMatrix3d';
519 end;
520
521 class function TMFMatrix3d.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
522 begin
523 Result := TMFMatrix3dEvent.Create(AParentNode, AName, AInEvent);
524 end;
525
526 function TMFMatrix3d.CreateItemBeforeParse: TSFMatrix3d;
527 begin
528 Result := TSFMatrix3d.CreateUndefined(ParentNode, false, '');
529 end;
530
531 function TMFMatrix3d.RawItemToString(ItemNum: Integer; const Encoding: TX3DEncoding): String;
532 begin
533 Result := Items.L[ItemNum].ToRawString('', FloatOutputFormat);
534 end;
535
536 procedure TMFMatrix3d.AddToList(const ItemList: TMatrix3DoubleList; const Item: TSFMatrix3d);
537 begin
538 Items.Add(Item.Value);
539 end;
540
541 procedure TMFMatrix3d.AssignLerp(const A: Double; Value1, Value2: TX3DField);
542 begin
543 Items.AssignLerp(A, (Value1 as TMFMatrix3d).Items, (Value2 as TMFMatrix3d).Items);
544 end;
545
546 function TMFMatrix3d.CanAssignLerp: boolean;
547 begin
548 Result := true;
549 end;
550
551 { TMFMatrix4f ------------------------------------------------------------------- }
552
553 class function TMFMatrix4f.X3DType: String;
554 begin
555 Result := 'MFMatrix4f';
556 end;
557
558 class function TMFMatrix4f.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
559 begin
560 Result := TMFMatrix4fEvent.Create(AParentNode, AName, AInEvent);
561 end;
562
563 function TMFMatrix4f.CreateItemBeforeParse: TSFMatrix4f;
564 begin
565 Result := TSFMatrix4f.CreateUndefined(ParentNode, false, '');
566 end;
567
568 function TMFMatrix4f.RawItemToString(ItemNum: Integer; const Encoding: TX3DEncoding): String;
569 begin
570 Result := Items.L[ItemNum].ToRawString('', FloatOutputFormat);
571 end;
572
573 procedure TMFMatrix4f.AddToList(const ItemList: TMatrix4List; const Item: TSFMatrix4f);
574 begin
575 Items.Add(Item.Value);
576 end;
577
578 procedure TMFMatrix4f.AssignLerp(const A: Double; Value1, Value2: TX3DField);
579 begin
580 Items.AssignLerp(A, (Value1 as TMFMatrix4f).Items, (Value2 as TMFMatrix4f).Items);
581 end;
582
583 function TMFMatrix4f.CanAssignLerp: boolean;
584 begin
585 Result := true;
586 end;
587
588 { TMFMatrix4d ------------------------------------------------------------------- }
589
590 class function TMFMatrix4d.X3DType: String;
591 begin
592 Result := 'MFMatrix4d';
593 end;
594
595 class function TMFMatrix4d.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
596 begin
597 Result := TMFMatrix4dEvent.Create(AParentNode, AName, AInEvent);
598 end;
599
600 function TMFMatrix4d.CreateItemBeforeParse: TSFMatrix4d;
601 begin
602 Result := TSFMatrix4d.CreateUndefined(ParentNode, false, '');
603 end;
604
605 function TMFMatrix4d.RawItemToString(ItemNum: Integer; const Encoding: TX3DEncoding): String;
606 begin
607 Result := Items.L[ItemNum].ToRawString('', FloatOutputFormat);
608 end;
609
610 procedure TMFMatrix4d.AddToList(const ItemList: TMatrix4DoubleList; const Item: TSFMatrix4d);
611 begin
612 Items.Add(Item.Value);
613 end;
614
615 procedure TMFMatrix4d.AssignLerp(const A: Double; Value1, Value2: TX3DField);
616 begin
617 Items.AssignLerp(A, (Value1 as TMFMatrix4d).Items, (Value2 as TMFMatrix4d).Items);
618 end;
619
620 function TMFMatrix4d.CanAssignLerp: boolean;
621 begin
622 Result := true;
623 end;
624
625 { TMFVec2f ------------------------------------------------------------------- }
626
627 class function TMFVec2f.X3DType: String;
628 begin
629 Result := 'MFVec2f';
630 end;
631
632 class function TMFVec2f.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
633 begin
634 Result := TMFVec2fEvent.Create(AParentNode, AName, AInEvent);
635 end;
636
637 function TMFVec2f.CreateItemBeforeParse: TSFVec2f;
638 begin
639 Result := TSFVec2f.CreateUndefined(ParentNode, false, '');
640 end;
641
642 function TMFVec2f.RawItemToString(ItemNum: Integer; const Encoding: TX3DEncoding): String;
643 begin
644 Result := Items.L[ItemNum].ToRawString(FloatOutputFormat);
645 end;
646
647 procedure TMFVec2f.AddToList(const ItemList: TVector2List; const Item: TSFVec2f);
648 begin
649 Items.Add(Item.Value);
650 end;
651
652 procedure TMFVec2f.AssignLerp(const A: Double; Value1, Value2: TX3DField);
653 begin
654 Items.AssignLerp(A, (Value1 as TMFVec2f).Items, (Value2 as TMFVec2f).Items);
655 end;
656
657 function TMFVec2f.CanAssignLerp: boolean;
658 begin
659 Result := true;
660 end;
661
662 { TMFVec3f ------------------------------------------------------------------- }
663
664 class function TMFVec3f.X3DType: String;
665 begin
666 Result := 'MFVec3f';
667 end;
668
669 class function TMFVec3f.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
670 begin
671 Result := TMFVec3fEvent.Create(AParentNode, AName, AInEvent);
672 end;
673
674 function TMFVec3f.CreateItemBeforeParse: TSFVec3f;
675 begin
676 Result := TSFVec3f.CreateUndefined(ParentNode, false, '');
677 end;
678
679 function TMFVec3f.RawItemToString(ItemNum: Integer; const Encoding: TX3DEncoding): String;
680 begin
681 Result := Items.L[ItemNum].ToRawString(FloatOutputFormat);
682 end;
683
684 procedure TMFVec3f.AddToList(const ItemList: TVector3List; const Item: TSFVec3f);
685 begin
686 Items.Add(Item.Value);
687 end;
688
689 procedure TMFVec3f.AssignLerp(const A: Double; Value1, Value2: TX3DField);
690 begin
691 Items.AssignLerp(A, (Value1 as TMFVec3f).Items, (Value2 as TMFVec3f).Items);
692 end;
693
694 function TMFVec3f.CanAssignLerp: boolean;
695 begin
696 Result := true;
697 end;
698
699 { TMFColor ------------------------------------------------------------------- }
700
701 class function TMFColor.X3DType: String;
702 begin
703 Result := 'MFColor';
704 end;
705
706 class function TMFColor.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
707 begin
708 Result := TMFColorEvent.Create(AParentNode, AName, AInEvent);
709 end;
710
711 { TMFVec4f ------------------------------------------------------------------- }
712
713 class function TMFVec4f.X3DType: String;
714 begin
715 Result := 'MFVec4f';
716 end;
717
718 class function TMFVec4f.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
719 begin
720 Result := TMFVec4fEvent.Create(AParentNode, AName, AInEvent);
721 end;
722
723 function TMFVec4f.CreateItemBeforeParse: TSFVec4f;
724 begin
725 Result := TSFVec4f.CreateUndefined(ParentNode, false, '');
726 end;
727
728 function TMFVec4f.RawItemToString(ItemNum: Integer; const Encoding: TX3DEncoding): String;
729 begin
730 Result := Items.L[ItemNum].ToRawString(FloatOutputFormat);
731 end;
732
733 procedure TMFVec4f.AddToList(const ItemList: TVector4List; const Item: TSFVec4f);
734 begin
735 Items.Add(Item.Value);
736 end;
737
738 procedure TMFVec4f.AssignLerp(const A: Double; Value1, Value2: TX3DField);
739 begin
740 Items.AssignLerp(A, (Value1 as TMFVec4f).Items, (Value2 as TMFVec4f).Items);
741 end;
742
743 function TMFVec4f.CanAssignLerp: boolean;
744 begin
745 Result := true;
746 end;
747
748 { TMFColorRGBA --------------------------------------------------------------- }
749
750 class function TMFColorRGBA.X3DType: String;
751 begin
752 Result := 'MFColorRGBA';
753 end;
754
755 class function TMFColorRGBA.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
756 begin
757 Result := TMFColorRGBAEvent.Create(AParentNode, AName, AInEvent);
758 end;
759
760 { TMFVec2d ------------------------------------------------------------------- }
761
762 class function TMFVec2d.X3DType: String;
763 begin
764 Result := 'MFVec2d';
765 end;
766
767 class function TMFVec2d.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
768 begin
769 Result := TMFVec2dEvent.Create(AParentNode, AName, AInEvent);
770 end;
771
772 function TMFVec2d.CreateItemBeforeParse: TSFVec2d;
773 begin
774 Result := TSFVec2d.CreateUndefined(ParentNode, false, '');
775 end;
776
777 function TMFVec2d.RawItemToString(ItemNum: Integer; const Encoding: TX3DEncoding): String;
778 begin
779 Result := Items.L[ItemNum].ToRawString(FloatOutputFormat);
780 end;
781
782 procedure TMFVec2d.AddToList(const ItemList: TVector2DoubleList; const Item: TSFVec2d);
783 begin
784 Items.Add(Item.Value);
785 end;
786
787 procedure TMFVec2d.AssignLerp(const A: Double; Value1, Value2: TX3DField);
788 begin
789 Items.AssignLerp(A, (Value1 as TMFVec2d).Items, (Value2 as TMFVec2d).Items);
790 end;
791
792 function TMFVec2d.CanAssignLerp: boolean;
793 begin
794 Result := true;
795 end;
796
797 { TMFVec3d ------------------------------------------------------------------- }
798
799 class function TMFVec3d.X3DType: String;
800 begin
801 Result := 'MFVec3d';
802 end;
803
804 class function TMFVec3d.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
805 begin
806 Result := TMFVec3dEvent.Create(AParentNode, AName, AInEvent);
807 end;
808
809 function TMFVec3d.CreateItemBeforeParse: TSFVec3d;
810 begin
811 Result := TSFVec3d.CreateUndefined(ParentNode, false, '');
812 end;
813
814 function TMFVec3d.RawItemToString(ItemNum: Integer; const Encoding: TX3DEncoding): String;
815 begin
816 Result := Items.L[ItemNum].ToRawString(FloatOutputFormat);
817 end;
818
819 procedure TMFVec3d.AddToList(const ItemList: TVector3DoubleList; const Item: TSFVec3d);
820 begin
821 Items.Add(Item.Value);
822 end;
823
824 procedure TMFVec3d.AssignLerp(const A: Double; Value1, Value2: TX3DField);
825 begin
826 Items.AssignLerp(A, (Value1 as TMFVec3d).Items, (Value2 as TMFVec3d).Items);
827 end;
828
829 function TMFVec3d.CanAssignLerp: boolean;
830 begin
831 Result := true;
832 end;
833
834 { TMFVec4d ------------------------------------------------------------------- }
835
836 class function TMFVec4d.X3DType: String;
837 begin
838 Result := 'MFVec4d';
839 end;
840
841 class function TMFVec4d.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
842 begin
843 Result := TMFVec4dEvent.Create(AParentNode, AName, AInEvent);
844 end;
845
846 function TMFVec4d.CreateItemBeforeParse: TSFVec4d;
847 begin
848 Result := TSFVec4d.CreateUndefined(ParentNode, false, '');
849 end;
850
851 function TMFVec4d.RawItemToString(ItemNum: Integer; const Encoding: TX3DEncoding): String;
852 begin
853 Result := Items.L[ItemNum].ToRawString(FloatOutputFormat);
854 end;
855
856 procedure TMFVec4d.AddToList(const ItemList: TVector4DoubleList; const Item: TSFVec4d);
857 begin
858 Items.Add(Item.Value);
859 end;
860
861 procedure TMFVec4d.AssignLerp(const A: Double; Value1, Value2: TX3DField);
862 begin
863 Items.AssignLerp(A, (Value1 as TMFVec4d).Items, (Value2 as TMFVec4d).Items);
864 end;
865
866 function TMFVec4d.CanAssignLerp: boolean;
867 begin
868 Result := true;
869 end;
870
871 { TMFRotation ---------------------------------------------------------------- }
872
873 class function TMFRotation.X3DType: String;
874 begin
875 Result := 'MFRotation';
876 end;
877
878 class function TMFRotation.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
879 begin
880 Result := TMFRotationEvent.Create(AParentNode, AName, AInEvent);
881 end;
882
883 function TMFRotation.CreateItemBeforeParse: TSFRotation;
884 begin
885 Result := TSFRotation.CreateUndefined(ParentNode, false, '');
886 end;
887
888 function TMFRotation.RawItemToString(ItemNum: Integer; const Encoding: TX3DEncoding): String;
889 begin
890 Result := Items.L[ItemNum].ToRawString(FloatOutputFormat);
891 end;
892
893 procedure TMFRotation.AddToList(const ItemList: TVector4List; const Item: TSFRotation);
894 begin
895 Items.Add(Item.Value);
896 end;
897
898 procedure TMFRotation.AssignLerp(const A: Double; Value1, Value2: TX3DField);
899 begin
900 Items.AssignLerp(A, (Value1 as TMFRotation).Items, (Value2 as TMFRotation).Items);
901 end;
902
903 function TMFRotation.CanAssignLerp: boolean;
904 begin
905 Result := true;
906 end;
907
908 { TMFFloat ------------------------------------------------------------------- }
909
910 function TMFFloat.SaveToStreamDoNewLineAfterRawItem(ItemNum: integer): boolean;
911 begin
912 Result := false;
913 end;
914
915 function TMFFloat.RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String;
916 begin
917 Result := FormatDot(FloatOutputFormat, [Items[ItemNum]]);
918 end;
919
920 procedure TMFFloat.AddToList(const ItemList: TSingleList; const Item: TSFFloat);
921 begin
922 Items.Add(Item.Value);
923 end;
924
925 procedure TMFFloat.AssignLerp(const A: Double; Value1, Value2: TX3DField);
926 begin
927 Items.AssignLerp(A, (Value1 as TMFFloat).Items, (Value2 as TMFFloat).Items);
928 end;
929
930 function TMFFloat.CanAssignLerp: boolean;
931 begin
932 Result := true;
933 end;
934
935 class function TMFFloat.X3DType: String;
936 begin
937 Result := 'MFFloat';
938 end;
939
940 class function TMFFloat.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
941 begin
942 Result := TMFFloatEvent.Create(AParentNode, AName, AInEvent);
943 end;
944
945 function TMFFloat.CreateItemBeforeParse: TSFFloat;
946 begin
947 Result := TSFFloat.CreateUndefined(ParentNode, false, '');
948
949 { Assign our Angle to single item used for parsing.
950 This way float values on MFFloat fields will be correctly converted to radians,
951 important e.g. for Background.skyAngle,groundAngle. }
952 Result.Angle := Angle;
953 end;
954
955 { TMFDouble -------------------------------------------------------------------- }
956
957 function TMFDouble.SaveToStreamDoNewLineAfterRawItem(ItemNum: integer): boolean;
958 begin
959 Result := false;
960 end;
961
962 function TMFDouble.RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String;
963 begin
964 Result := FormatDot(FloatOutputFormat, [Items[ItemNum]]);
965 end;
966
967 procedure TMFDouble.AddToList(const ItemList: TDoubleList; const Item: TSFDouble);
968 begin
969 Items.Add(Item.Value);
970 end;
971
972 procedure TMFDouble.AssignLerp(const A: Double; Value1, Value2: TX3DField);
973 begin
974 Items.AssignLerp(A, (Value1 as TMFDouble).Items, (Value2 as TMFDouble).Items);
975 end;
976
977 function TMFDouble.CanAssignLerp: boolean;
978 begin
979 Result := true;
980 end;
981
982 class function TMFDouble.X3DType: String;
983 begin
984 Result := 'MFDouble';
985 end;
986
987 class function TMFDouble.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
988 begin
989 Result := TMFDoubleEvent.Create(AParentNode, AName, AInEvent);
990 end;
991
992 function TMFDouble.CreateItemBeforeParse: TSFDouble;
993 begin
994 Result := TSFDouble.CreateUndefined(ParentNode, false, '');
995 end;
996
997 { TMFTime -------------------------------------------------------------------- }
998
999 class function TMFTime.X3DType: String;
1000 begin
1001 Result := 'MFTime';
1002 end;
1003
1004 class function TMFTime.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
1005 begin
1006 Result := TMFTimeEvent.Create(AParentNode, AName, AInEvent);
1007 end;
1008
1009 { TMFString ------------------------------------------------------------------ }
1010
1011 function TMFString.FastEqualsValue(SecondValue: TX3DField): boolean;
1012
1013 function StringsEqual(const S1, S2: TStrings): Boolean;
1014 var
1015 I: Integer;
1016 begin
1017 Result := S1.Count = S2.Count;
1018 if Result then
1019 for I := 0 to S1.Count - 1 do
1020 if S1[I] <> S2[I] then
1021 Exit(false);
1022 end;
1023
1024 begin
1025 { It is important to implement useful FastEqualsValue for MFString,
1026 not just return false (as most multi-value fields do) because
1027 MFString change happens when you do SetUrl.
1028 It's useful that TImageTexture.SetUrl multiple times with the same URL
1029 does nothing. See:
1030
1031 https://github.com/castle-engine/castle-engine/issues/304
1032 https://gist.github.com/michaliskambi/876992f8fbc04ac869d8994d520383b4
1033 }
1034 Result := (SecondValue is TMFString) and
1035 StringsEqual(Items, TMFString(SecondValue).Items);
1036 end;
1037
1038 function TMFString.RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): String;
1039 begin
1040 case Encoding of
1041 xeClassic: Result := StringToX3DClassic(Items[ItemNum]);
1042 xeXML : Result := StringToX3DXmlMulti(Items[ItemNum]);
1043 {$ifndef COMPILER_CASE_ANALYSIS}
1044 else raise EInternalError.Create('TMFString.RawItemToString Encoding?');
1045 {$endif}
1046 end;
1047 end;
1048
1049 procedure TMFString.AddToList(const ItemList: TCastleStringList; const Item: TSFString);
1050 begin
1051 Items.Add(Item.Value);
1052 end;
1053
1054 class function TMFString.X3DType: String;
1055 begin
1056 Result := 'MFString';
1057 end;
1058
1059 class function TMFString.CreateEvent(const AParentNode: TX3DFileItem; const AName: String; const AInEvent: boolean): TX3DEvent;
1060 begin
1061 Result := TMFStringEvent.Create(AParentNode, AName, AInEvent);
1062 end;
1063
1064 function TMFString.CreateItemBeforeParse: TSFString;
1065 begin
1066 Result := TSFString.CreateUndefined(ParentNode, false, '');
1067 end;
1068
1069 procedure TMFString.ParseXMLAttribute(const AttributeValue: String; Reader: TX3DReader);
1070
1071 { TODO: Copy-paste of TX3DSimpleMultField.ParseXMLAttributeLexer }
1072 procedure ParseXMLAttributeLexer(Lexer: TX3DLexer; Reader: TX3DReader);
1073 var
1074 SingleItem: TSFString;
1075 begin
1076 { This is much easier and simpler in XML encoding than it was
1077 in classic encoding. We don't have to check for [ and ] tokens,
1078 comma is ignored (it was only for VRML 1.0 anyway), we just read
1079 single values up to the end of stream. }
1080
1081 Clear;
1082
1083 SingleItem := CreateItemBeforeParse;
1084 try
1085 { List size may be increased rapidly during parsing.
1086 Prepare for it by allocating some size in advance. }
1087 //Capacity := 64;
1088
1089 while Lexer.Token <> vtEnd do
1090 begin
1091 SingleItem.ParseValue(Lexer, Reader);
1092 AddToList(Items, SingleItem);
1093 end;
1094 finally FreeAndNil(SingleItem) end;
1095 end;
1096
1097 var
1098 Lexer: TX3DLexer;
1099 begin
1100 { For MFString, it's very common that normal parsing fails because
1101 of missing double quotes, even in models from
1102 http://www.web3d.org/x3d/content/examples/Basic/
1103 Although specification clearly says that MFString
1104 components should always be enclosed within double
1105 quotes. We just do what Xj3D seems to do, that is
1106 we handle this as a single string (producing a warning). }
1107
1108 try
1109 Lexer := TX3DLexer.CreateForPartialStream(AttributeValue, Reader.Version);
1110 try
1111 ParseXMLAttributeLexer(Lexer, Reader);
1112 finally FreeAndNil(Lexer) end;
1113
1114 { Surround in try..except both CreateForPartialStream and ParseXMLAttributeLexer,
1115 as CreateForPartialStream can already cause exception in case of
1116 demo-models/x3d/test_single_quotes_mfstring.x3d . }
1117 except
1118 on E: Exception do
1119 begin
1120 WritelnWarning('X3D', 'Error when reading MFString field "' + X3DName + '" value. Possibly missing double quotes (treating as a single string): ' + E.Message);
1121 Items.Count := 0;
1122 Items.Add(AttributeValue);
1123 end;
1124 end;
1125 end;
1126
1127 function TMFString.SaveToXmlValue: TSaveToXmlMethod;
1128 begin
1129 Result := sxAttributeCustomQuotes;
1130 end;
1131
1132 procedure TMFString.SaveToStreamValue(Writer: TX3DWriter);
1133
1134 function SaveToStreamDoNewLineAfterRawItem(ItemNum: integer): boolean;
1135 begin
1136 Result := true;
1137 end;
1138
1139 { TODO: Copy-paste of TX3DSimpleMultField.SaveToStreamValue }
1140 procedure SaveToStreamValueCore(Writer: TX3DWriter);
1141 var
1142 i: integer;
1143 WriteIndentNextTime: boolean;
1144 IndentMultiValueFields: boolean;
1145 begin
1146 case Writer.Encoding of
1147 xeClassic:
1148 { The general "for I := ..." code below can handle correctly any FItems.Count
1149 value. But for aesthetics, i.e. more clear output for humans,
1150 I handle the FItems.Count = 0 and 1 cases separately. }
1151 case Count of
1152 0: Writer.Write('[]');
1153 1: Writer.Write(RawItemToString(0, Writer.Encoding));
1154 else
1155 begin
1156 Writer.Writeln('[');
1157 Writer.IncIndent;
1158
1159 { For really long fields, writing indentation before each item
1160 can cost a significant disk space. So do not indent when
1161 there are many items. }
1162 IndentMultiValueFields := Count <= 10;
1163
1164 WriteIndentNextTime := IndentMultiValueFields;
1165 for i := 0 to Count-1 do
1166 begin
1167 if WriteIndentNextTime then Writer.WriteIndent('');
1168 Writer.Write(RawItemToString(i, Writer.Encoding) +',');
1169 { After the last item we always write newline,
1170 no matter what's SaveToStreamDoNewLineAfterRawItem }
1171 if (i = Count - 1) or
1172 SaveToStreamDoNewLineAfterRawItem(i) then
1173 begin Writer.Writeln; WriteIndentNextTime := IndentMultiValueFields end else
1174 begin Writer.Write(' '); WriteIndentNextTime := false; end;
1175 end;
1176
1177 Writer.DecIndent;
1178 Writer.WriteIndent(']');
1179 end;
1180 end;
1181 xeXML:
1182 for I := 0 to Count - 1 do
1183 begin
1184 Writer.Write(RawItemToString(I, Writer.Encoding));
1185 if I <> Count - 1 then
1186 Writer.Write(' ');
1187 end;
1188 {$ifndef COMPILER_CASE_ANALYSIS}
1189 else raise EInternalError.Create('TX3DSimpleMultField.SaveToStreamValue Encoding?');
1190 {$endif}
1191 end;
1192 end;
1193
1194 begin
1195 { MFString in XML encoding is surrounded by single quotes }
1196 if Writer.Encoding = xeXML then Writer.Write('''');
1197 SaveToStreamValueCore(Writer);
1198 if Writer.Encoding = xeXML then Writer.Write('''');
1199 end;
1200
1201 procedure TMFString.SetItems(const Value: TCastleStringList);
1202 begin
1203 FItems.Assign(Value);
1204 end;
1205
1206 procedure TMFString.Send(const AValue: array of string);
1207 var
1208 FieldValue: TMFString;
1209 begin
1210 if SendsByEvent then
1211 begin
1212 { We want to create an instance of ClassType (actual current class type),
1213 not just an instance of TMFString (which is only an ancestor
1214 of other MF field classes). }
1215 // This works in FPC (Delphi and ObjFpc mode), but doesn't in actual Delphi:
1216 // FieldValue := TX3DFieldClass(ClassType).Create(ParentNode, false, X3DName, AValue);
1217
1218 { AExposed = false below, because not needed otherwise. }
1219 FieldValue := TMFString(
1220 TX3DFieldClass(ClassType).CreateUndefined(ParentNode, false, X3DName));
1221 try
1222 FieldValue.Items.AddRange(AValue);
1223 Send(FieldValue);
1224 finally FreeAndNil(FieldValue) end;
1225 end else
1226 begin
1227 { When SendsByEvent = false, we can optimize this method.
1228 No need to create an intermediate FieldValue instance
1229 (and assign AValue to FieldValue, just to later assign FieldValue to Items). }
1230 ValueFromIsClause := false;
1231 Items.Assign(AValue);
1232 Changed;
1233 end;
1234 end;
1235
1236 procedure TMFString.Send(const AValue: TCastleStringList);
1237 var
1238 FieldValue: TMFString;
1239 begin
1240 { This is a copy-paste of implementation of Send(const AValue: array of TItem) }
1241 if SendsByEvent then
1242 begin
1243 FieldValue := TMFString(
1244 TX3DFieldClass(ClassType).CreateUndefined(ParentNode, false, X3DName));
1245 try
1246 FieldValue.Items.Assign(AValue);
1247 Send(FieldValue);
1248 finally FreeAndNil(FieldValue) end;
1249 end else
1250 begin
1251 ValueFromIsClause := false;
1252 Items.Assign(AValue);
1253 Changed;
1254 end;
1255 end;
1256
1257 constructor TMFString.CreateUndefined(const AParentNode: TX3DFileItem;
1258 const AExposed: boolean; const AName: String);
1259 begin
1260 inherited;
1261 FItems := TCastleStringList.Create;
1262 DefaultValuesCount := -1;
1263 end;
1264
1265 constructor TMFString.Create(const AParentNode: TX3DFileItem;
1266 const AExposed: boolean; const AName: String;
1267 const InitialContent: array of string);
1268 begin
1269 CreateUndefined(AParentNode, AExposed, AName);
1270 Items.AddRange(InitialContent);
1271 AssignDefaultValueFromValue;
1272 end;
1273
1274 destructor TMFString.Destroy;
1275 begin
1276 FreeAndNil(FItems);
1277 inherited;
1278 end;
1279
1280 procedure TMFString.Clear;
1281 begin
1282 FItems.Clear;
1283 end;
1284
1285 function TMFString.GetCount: TListSize;
1286 begin
1287 Result := FItems.Count;
1288 end;
1289
1290 procedure TMFString.SetCount(const Value: TListSize);
1291 begin
1292 FItems.Count := Value;
1293 end;
1294
1295 procedure TMFString.ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
1296 var
1297 SingleItem: TSFString;
1298 begin
1299 Clear;
1300
1301 SingleItem := nil;
1302 try
1303 SingleItem := CreateItemBeforeParse;
1304
1305 if Lexer.Token = vtOpenSqBracket then
1306 begin
1307 Lexer.NextToken;
1308
1309 { List size may be increased rapidly during parsing.
1310 Prepare for it by allocating some size in advance. }
1311 FItems.Capacity := 64;
1312
1313 while Lexer.Token <> vtCloseSqBracket do
1314 { we always look now at "]" or next single value }
1315 begin
1316 SingleItem.ParseValue(Lexer, Reader);
1317 AddToList(Items, SingleItem);
1318
1319 if Lexer.Token = vtCloseSqBracket then break;
1320
1321 if Lexer.Version.Major < 2 then
1322 begin
1323 Lexer.CheckTokenIs(vtComma);
1324 Lexer.NextToken;
1325 end;
1326 end;
1327
1328 { Our handling of commas is specified by VRML 1.0 spec:
1329 - When the list has no items, "[]" is allowed but "[,]" is not.
1330 - When there are some items on the list, the last item *may*
1331 be followed by a comma.
1332 For VRML >= 2.0 this all doesn't matter, comma is just a whitespace
1333 and Lexer will never return such token. }
1334
1335 Lexer.NextToken;
1336 end else
1337 begin
1338 { one single field - not enclosed in [] brackets }
1339 SingleItem.ParseValue(Lexer, Reader);
1340 AddToList(Items, SingleItem);
1341 end;
1342
1343 finally FreeAndNil(SingleItem) end;
1344 end;
1345
1346 procedure TMFString.Assign(Source: TPersistent);
1347 begin
1348 if Source is TMFString then
1349 begin
1350 DefaultValuesCount := TMFString(Source).DefaultValuesCount;
1351 DefaultValue := TMFString(Source).DefaultValue;
1352 Items.Assign(TMFString(Source).Items);
1353 VRMLFieldAssignCommon(TX3DField(Source));
1354 end else
1355 inherited;
1356 end;
1357
1358 procedure TMFString.AssignValue(Source: TX3DField);
1359 begin
1360 if Source is TMFString then
1361 begin
1362 inherited;
1363 Items.Assign(TMFString(Source).Items);
1364 end else
1365 AssignValueRaiseInvalidClass(Source);
1366 end;
1367
1368 procedure TMFString.AssignDefaultValueFromValue;
1369 begin
1370 inherited;
1371
1372 (* initialize default value of the field: DefaultValuesCount, and (only
1373 in case of DefaultValuesCount = 1 for now) initialize also DefaultValue *)
1374 case Count of
1375 0: DefaultValuesCount := 0;
1376 1: begin
1377 DefaultValuesCount := 1;
1378 DefaultValue := Items[0];
1379 end;
1380 else DefaultValuesCount := -1;
1381 end;
1382 end;
1383
1384 procedure TMFString.UnassignDefaultValue;
1385 begin
1386 DefaultValuesCount := -1;
1387 end;
1388
1389 function TMFString.Equals(SecondValue: TX3DField): boolean;
1390 begin
1391 Result := (inherited Equals(SecondValue)) and (SecondValue is TMFString) and
1392 Items.Equals(TMFString(SecondValue).Items);
1393 end;
1394
1395 function TMFString.EqualsDefaultValue: boolean;
1396 var
1397 Tmp: TCastleStringList;
1398 begin
1399 Result := Count = DefaultValuesCount;
1400
1401 if Result then
1402 begin
1403 if DefaultValuesCount = 1 then
1404 begin
1405 Tmp := TCastleStringList.Create;
1406 try
1407 Tmp.Add(DefaultValue);
1408 Result := Tmp.PerfectlyEquals(Items);
1409 finally
1410 FreeAndNil(Tmp)
1411 end;
1412 end else
1413 if DefaultValuesCount <> 0 then
1414 // we cannot check in case DefaultValuesCount >= 2
1415 Result := false;
1416 end;
1417 end;
1418
1419 function TMFString.GetItemsSafe(Index: Integer): String;
1420 begin
1421 if (Index >= 0) and (Index < Items.Count) then
1422 //Result := Items.{$ifdef FPC}L[Index]{$else}InternalGetItem(Index){$endif} else
1423 Result := Items.L[Index] else
1424 begin
1425 WritelnWarning_InvalidIndex(Index, Count);
1426 Result := Default(string);
1427 end;
1428 end;
1429
1430 procedure TMFString.SetItemsSafe(Index: Integer; const Value: String);
1431 begin
1432 if (Index >= 0) and (Index < Items.Count) then
1433 Items.L[Index] := Value else
1434 begin
1435 WritelnWarning_InvalidIndex(Index, Count);
1436 end;
1437 end;
1438
1439 procedure TMFString.WritelnWarning_InvalidIndex(const Index, ACount: Integer);
1440 begin
1441 if not WarningDoneInvalidIndex then
1442 begin
1443 WarningDoneInvalidIndex := true;
1444 WritelnWarning('X3D', Format('Invalid index %d (should be less than count %d) for field %s (type: %s)', [
1445 Index,
1446 ACount,
1447 NiceName,
1448 X3DType
1449 ]));
1450 end;
1451 end;
1452
1453 {$endif read_implementation}