Debian Codegraph @

src/scene/x3d/castlefields_x3dsimplemultfield.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
20 { X3D field with a list of values.
21 This is an ancestor of most multiple-value X3D fields. }
22 {$ifdef FPC}generic{$endif}
23 TX3DSimpleMultField<
24 TItem;
25 TSingleField{$ifndef FPC}: TX3DSingleField{$endif};
26 TItemList{$ifndef FPC}: TStructList<TItem>, constructor{$endif}> = class(TX3DMultField)
27 strict private
28 WarningDoneInvalidIndex: boolean;
29
30 { Items are kept in a TItemList, that may be an efficient
31 Generics.Collections.TList<T>.
32 This allows speed (the TList<T> has trivial cache-friendly layout).
33 That's why TX3DSimpleMultField is not a descendant of TX3DSingleFieldsList,
34 as then we would have a list of references, which could be quite memory-consuming
35 (imagine a milion vertexes in MFVec3f, which is absolutely possible)
36 and not cache friendly. }
37 FItems: TItemList;
38
39 { DefaultValuesCount may have three valid values (for now):
40 -1 (means "no default value for this field")
41 0 (means "default value of this field is empty")
42 1 (means "default value of this field is one-item array with DefaultValue").
43
44 So it's not possible to express default values with more
45 than one item. That's OK, because nodes from VRML 1.0 and 2.0 specifications
46 never have such field (and VRML 2.0 prototypes (that have user-defined
47 default field values) actually don't need it). So, for now, more flexible
48 DefaultValuesCount is not needed.
49
50 CreateUndefined sets DefaultValuesCount to -1. }
51 DefaultValuesCount: Integer;
52 DefaultValue: TItem;
53 function GetItems: TItemList;
54 procedure SetItems(const Value: TItemList);
55 function GetItemsSafe(Index: Integer): TItem;
56 procedure SetItemsSafe(Index: Integer; const Value: TItem);
57 strict protected
58 { Create new instance of a corresponding single-value field. It doesn't need to
59 have initialized value (so it can be created by
60 CreateUndefined), since we'll call his Parse method immediately.
61 Default implementation in this class uses simply
62 calls TSingleField.CreateUndefined. }
63 function CreateItemBeforeParse: TSingleField; virtual; abstract;
64 procedure AddToList(const ItemList: TItemList; const Item: TSingleField); virtual; abstract;
65
66 procedure WritelnWarning_InvalidIndex(const Index, ACount: Integer);
67
68 function GetCount: TListSize; override;
69 procedure SetCount(const Value: TListSize); override;
70
71 { SaveToStreamValue overriden for MF fields. This class handles
72 SaveToStreamValue fully, no need to override it again in
73 descendants. }
74 procedure SaveToStreamValue(Writer: TX3DWriter); override;
75
76 { RawItemToString(i) must change FItems[i] into a string that can be used to
77 store this is text stream. In descendants, you have to override this. }
78 function RawItemToString(ItemNum: integer; const Encoding: TX3DEncoding): string; virtual; abstract;
79
80 { This says when we should do newline when writing this field into a stream.
81 It's has purely aesthetical meaning, nothing more.
82 In this class SaveToStreamDoNewLineAfterRawItem always returns @true
83 (although SaveToStreamValue may sometimes ignore it, if it knows better). }
84 function SaveToStreamDoNewLineAfterRawItem(ItemNum: integer): boolean; virtual;
85
86 procedure Clear;
87 function GetCapacity: TListSize;
88 procedure SetCapacity(const Value: TListSize);
89 property Capacity: TListSize read GetCapacity write SetCapacity;
90 public
91 constructor Create(const AParentNode: TX3DFileItem;
92 const AExposed: boolean; const AName: string;
93 const InitialContent: array of TItem);
94 constructor CreateUndefined(const AParentNode: TX3DFileItem;
95 const AExposed: boolean; const AName: string); override;
96 destructor Destroy; override;
97
98 property Items: TItemList read GetItems write SetItems;
99
100 { Copy another field value, default value and other properties. }
101 procedure Assign(Source: TPersistent); override;
102 procedure AssignValue(Source: TX3DField); override;
103 procedure AssignDefaultValueFromValue; override;
104 procedure UnassignDefaultValue; override;
105
106 { Parse MF field. This class handles parsing fully, usually no need to
107 override this more in descendants. It uses TSingleField.Parse method. }
108 procedure ParseValue(Lexer: TX3DLexer; Reader: TX3DReader); override;
109
110 { Checks equality between this and SecondValue field.
111 In addition to inherited(Equals), this checks the actual contents
112 of the items.
113 Note that floating-point values are compared with some tolerance
114 for errors by this method. }
115 function Equals(SecondValue: TX3DField): boolean; override;
116 function EqualsDefaultValue: boolean; override;
117
118 procedure ParseXMLAttributeLexer(Lexer: TX3DLexer; Reader: TX3DReader); override;
119
120 { Access items, making nice warning in case of an invalid index.
121 In case of an invalid index,
122 getting will return default value (filled with zeroes),
123 setting will do nothing,
124 and both will emit user-friendly WritelnWarning. }
125 property ItemsSafe[Index: Integer]: TItem read GetItemsSafe write SetItemsSafe;
126
127 { Set the field's value in a correct way (by sending X3D event, or at least notifying
128 the parent scene). }
129 procedure Send(const AValue: array of TItem); overload;
130 procedure Send(const AValue: TItemList); overload;
131 end;
132
133 {$endif read_interface}
134
135 {$ifdef read_implementation}
136
137 { TX3DSimpleMultField ------------------------------------------------------- }
138
139 procedure TX3DSimpleMultField{$ifndef FPC} <
140 TItem;
141 TSingleField;
142 TItemList
143 > {$endif} .
144 ParseValue(Lexer: TX3DLexer; Reader: TX3DReader);
145 var
146 SingleItem: TSingleField;
147 begin
148 Clear;
149
150 SingleItem := nil;
151 try
152 SingleItem := CreateItemBeforeParse;
153
154 if Lexer.Token = vtOpenSqBracket then
155 begin
156 Lexer.NextToken;
157
158 { List size may be increased rapidly during parsing.
159 Prepare for it by allocating some size in advance. }
160 Capacity := 64;
161
162 while Lexer.Token <> vtCloseSqBracket do
163 { we always look now at "]" or next single value }
164 begin
165 SingleItem.ParseValue(Lexer, Reader);
166 AddToList(Items, SingleItem);
167
168 if Lexer.Token = vtCloseSqBracket then break;
169
170 if Lexer.Version.Major < 2 then
171 begin
172 Lexer.CheckTokenIs(vtComma);
173 Lexer.NextToken;
174 end;
175 end;
176
177 { Our handling of commas is specified by VRML 1.0 spec:
178 - When the list has no items, "[]" is allowed but "[,]" is not.
179 - When there are some items on the list, the last item *may*
180 be followed by a comma.
181 For VRML >= 2.0 this all doesn't matter, comma is just a whitespace
182 and Lexer will never return such token. }
183
184 Lexer.NextToken;
185 end else
186 begin
187 { one single field - not enclosed in [] brackets }
188 SingleItem.ParseValue(Lexer, Reader);
189 AddToList(Items, SingleItem);
190 end;
191
192 finally FreeAndNil(SingleItem) end;
193 end;
194
195 procedure TX3DSimpleMultField{$ifndef FPC} <
196 TItem;
197 TSingleField;
198 TItemList
199 > {$endif} .
200 ParseXMLAttributeLexer(Lexer: TX3DLexer; Reader: TX3DReader);
201 var
202 SingleItem: TSingleField;
203 begin
204 { This is much easier and simpler in XML encoding than it was
205 in classic encoding. We don't have to check for [ and ] tokens,
206 comma is ignored (it was only for VRML 1.0 anyway), we just read
207 single values up to the end of stream. }
208
209 Clear;
210
211 SingleItem := CreateItemBeforeParse;
212 try
213 { List size may be increased rapidly during parsing.
214 Prepare for it by allocating some size in advance. }
215 Capacity := 64;
216
217 while Lexer.Token <> vtEnd do
218 begin
219 SingleItem.ParseValue(Lexer, Reader);
220 AddToList(Items, SingleItem);
221 end;
222 finally FreeAndNil(SingleItem) end;
223 end;
224
225 procedure TX3DSimpleMultField{$ifndef FPC} <
226 TItem;
227 TSingleField;
228 TItemList
229 > {$endif} .
230 SaveToStreamValue(Writer: TX3DWriter);
231 var
232 i: integer;
233 WriteIndentNextTime: boolean;
234 IndentMultiValueFields: boolean;
235 begin
236 case Writer.Encoding of
237 xeClassic:
238 { The general "for I := ..." code below can handle correctly any FItems.Count
239 value. But for aesthetics, i.e. more clear output for humans,
240 I handle the FItems.Count = 0 and 1 cases separately. }
241 case Count of
242 0: Writer.Write('[]');
243 1: Writer.Write(RawItemToString(0, Writer.Encoding));
244 else
245 begin
246 Writer.Writeln('[');
247 Writer.IncIndent;
248
249 { For really long fields, writing indentation before each item
250 can cost a significant disk space. So do not indent when
251 there are many items. }
252 IndentMultiValueFields := Count <= 10;
253
254 WriteIndentNextTime := IndentMultiValueFields;
255 for i := 0 to Count-1 do
256 begin
257 if WriteIndentNextTime then Writer.WriteIndent('');
258 Writer.Write(RawItemToString(i, Writer.Encoding) +',');
259 { After the last item we always write newline,
260 no matter what's SaveToStreamDoNewLineAfterRawItem }
261 if (i = Count - 1) or
262 SaveToStreamDoNewLineAfterRawItem(i) then
263 begin Writer.Writeln; WriteIndentNextTime := IndentMultiValueFields end else
264 begin Writer.Write(' '); WriteIndentNextTime := false; end;
265 end;
266
267 Writer.DecIndent;
268 Writer.WriteIndent(']');
269 end;
270 end;
271 xeXML:
272 for I := 0 to Count - 1 do
273 begin
274 Writer.Write(RawItemToString(I, Writer.Encoding));
275 if I <> Count - 1 then
276 Writer.Write(' ');
277 end;
278 {$ifndef COMPILER_CASE_ANALYSIS}
279 else raise EInternalError.Create('TX3DSimpleMultField.SaveToStreamValue Encoding?');
280 {$endif}
281 end;
282 end;
283
284 function TX3DSimpleMultField{$ifndef FPC} <
285 TItem;
286 TSingleField;
287 TItemList
288 > {$endif} .
289 SaveToStreamDoNewLineAfterRawItem(ItemNum: integer): boolean;
290 begin
291 Result := true;
292 end;
293
294 function TX3DSimpleMultField{$ifndef FPC} <
295 TItem;
296 TSingleField;
297 TItemList
298 > {$endif} .
299 Equals(SecondValue: TX3DField): boolean;
300 begin
301 Result :=
302 (inherited Equals(SecondValue)) and
303 (SecondValue is TX3DSimpleMultField{$ifndef FPC}<TItem, TSingleField, TItemList>{$endif}) and
304 Items.Equals(TX3DSimpleMultField{$ifndef FPC}<TItem, TSingleField, TItemList>{$endif}(SecondValue).Items);
305 end;
306
307 function TX3DSimpleMultField{$ifndef FPC} <
308 TItem;
309 TSingleField;
310 TItemList
311 > {$endif} .
312 EqualsDefaultValue: boolean;
313 var
314 Tmp: TItemList;
315 begin
316 Result := Count = DefaultValuesCount;
317
318 if Result then
319 begin
320 if DefaultValuesCount = 1 then
321 begin
322 Tmp := TItemList.Create;
323 try
324 Tmp.Add(DefaultValue);
325 Result := Tmp.PerfectlyEquals(Items);
326 finally
327 FreeAndNil(Tmp)
328 end;
329 end else
330 if DefaultValuesCount <> 0 then
331 // we cannot check in case DefaultValuesCount >= 2
332 Result := false;
333 end;
334 end;
335
336 procedure TX3DSimpleMultField{$ifndef FPC} <
337 TItem;
338 TSingleField;
339 TItemList
340 > {$endif} .
341 WritelnWarning_InvalidIndex(
342 const Index, ACount: Integer);
343 begin
344 if not WarningDoneInvalidIndex then
345 begin
346 WarningDoneInvalidIndex := true;
347 WritelnWarning('X3D', Format('Invalid index %d (should be less than count %d) for field %s (type: %s)', [
348 Index,
349 ACount,
350 NiceName,
351 X3DType
352 ]));
353 end;
354 end;
355
356 constructor TX3DSimpleMultField{$ifndef FPC} <
357 TItem;
358 TSingleField;
359 TItemList
360 > {$endif} .
361 CreateUndefined(const AParentNode: TX3DFileItem;
362 const AExposed: boolean; const AName: string);
363 begin
364 inherited;
365 FItems := TItemList.Create;
366 DefaultValuesCount := -1;
367 end;
368
369 constructor TX3DSimpleMultField{$ifndef FPC} <
370 TItem;
371 TSingleField;
372 TItemList
373 > {$endif} .
374 Create(
375 const AParentNode: TX3DFileItem; const AExposed: boolean; const AName: string;
376 const InitialContent: array of TItem);
377 begin
378 CreateUndefined(AParentNode, AExposed, AName);
379 Items.AddRange(InitialContent);
380 AssignDefaultValueFromValue;
381 end;
382
383 destructor TX3DSimpleMultField{$ifndef FPC} <
384 TItem;
385 TSingleField;
386 TItemList
387 > {$endif} .
388 Destroy;
389 begin
390 FreeAndNil(FItems);
391 inherited;
392 end;
393
394 function TX3DSimpleMultField{$ifndef FPC} <
395 TItem;
396 TSingleField;
397 TItemList
398 > {$endif} .
399 GetCount: TListSize;
400 begin
401 Result := FItems.Count;
402 end;
403
404 procedure TX3DSimpleMultField{$ifndef FPC} <
405 TItem;
406 TSingleField;
407 TItemList
408 > {$endif} .
409 SetCount(const Value: TListSize);
410 begin
411 FItems.Count := Value;
412 end;
413
414 function TX3DSimpleMultField{$ifndef FPC} <
415 TItem;
416 TSingleField;
417 TItemList
418 > {$endif} .
419 GetCapacity: TListSize;
420 begin
421 Result := FItems.Capacity;
422 end;
423
424 procedure TX3DSimpleMultField{$ifndef FPC} <
425 TItem;
426 TSingleField;
427 TItemList
428 > {$endif} .
429 SetCapacity(const Value: TListSize);
430 begin
431 FItems.Capacity := Value;
432 end;
433
434 procedure TX3DSimpleMultField{$ifndef FPC} <
435 TItem;
436 TSingleField;
437 TItemList
438 > {$endif} .
439 Clear;
440 begin
441 FItems.Clear;
442 end;
443
444 function TX3DSimpleMultField{$ifndef FPC} <
445 TItem;
446 TSingleField;
447 TItemList
448 > {$endif} .
449 GetItems: TItemList;
450 begin
451 Result := TItemList(FItems);
452 end;
453
454 procedure TX3DSimpleMultField{$ifndef FPC} <
455 TItem;
456 TSingleField;
457 TItemList
458 > {$endif} .
459 SetItems(const Value: TItemList);
460 begin
461 TItemList(FItems).Assign(Value);
462 end;
463
464 procedure TX3DSimpleMultField{$ifndef FPC} <
465 TItem;
466 TSingleField;
467 TItemList
468 > {$endif} .
469 Assign(Source: TPersistent);
470 begin
471 if Source is TX3DSimpleMultField{$ifndef FPC}<TItem, TSingleField,
472 TItemList>{$endif} then
473 begin
474 DefaultValuesCount := TX3DSimpleMultField{$ifndef FPC}<TItem, TSingleField,
475 TItemList>{$endif}(Source).DefaultValuesCount;
476 DefaultValue := TX3DSimpleMultField{$ifndef FPC}<TItem, TSingleField,
477 TItemList>{$endif}(Source).DefaultValue;
478 Items.Assign(TX3DSimpleMultField{$ifndef FPC}<TItem, TSingleField,
479 TItemList>{$endif}(Source).Items);
480 VRMLFieldAssignCommon(TX3DField(Source));
481 end else
482 inherited;
483 end;
484
485 procedure TX3DSimpleMultField{$ifndef FPC} <
486 TItem;
487 TSingleField;
488 TItemList
489 > {$endif} .
490 AssignValue(Source: TX3DField);
491 begin
492 if Source is TX3DSimpleMultField{$ifndef FPC}<TItem, TSingleField,
493 TItemList>{$endif} then
494 begin
495 inherited;
496 Items.Assign(TX3DSimpleMultField{$ifndef FPC}<TItem, TSingleField,
497 TItemList>{$endif}(Source).Items);
498 end else
499 AssignValueRaiseInvalidClass(Source);
500 end;
501
502 procedure TX3DSimpleMultField{$ifndef FPC} <
503 TItem;
504 TSingleField;
505 TItemList
506 > {$endif} .
507 AssignDefaultValueFromValue;
508 begin
509 inherited;
510
511 (* initialize default value of the field: DefaultValuesCount, and (only
512 in case of DefaultValuesCount = 1 for now) initialize also DefaultValue *)
513 case Count of
514 0: DefaultValuesCount := 0;
515 1: begin
516 DefaultValuesCount := 1;
517 DefaultValue := Items{$ifdef FPC}[0]{$else}.InternalGetItem(0){$endif};
518 end;
519 else DefaultValuesCount := -1;
520 end;
521 end;
522
523 procedure TX3DSimpleMultField{$ifndef FPC} <
524 TItem;
525 TSingleField;
526 TItemList
527 > {$endif} .
528 UnassignDefaultValue;
529 begin
530 inherited;
531 DefaultValuesCount := -1;
532 end;
533
534 function TX3DSimpleMultField{$ifndef FPC} <
535 TItem;
536 TSingleField;
537 TItemList
538 > {$endif} .
539 GetItemsSafe(Index: Integer): TItem;
540 begin
541 if (Index >= 0) and (Index < Items.Count) then
542 Result := Items.{$ifdef FPC}L[Index]{$else}InternalGetItem(Index){$endif} else
543 begin
544 WritelnWarning_InvalidIndex(Index, Count);
545 Result := Default(TItem);
546 end;
547 end;
548
549 procedure TX3DSimpleMultField{$ifndef FPC} <
550 TItem;
551 TSingleField;
552 TItemList
553 > {$endif} .
554 SetItemsSafe(Index: Integer; const Value: TItem);
555 begin
556 if (Index >= 0) and (Index < Items.Count) then
557 {$ifdef FPC}
558 Items.L[Index] := Value else
559 {$else}
560 Items.InternalSetItem(Index, Value) else
561 {$endif}
562 begin
563 WritelnWarning_InvalidIndex(Index, Count);
564 end;
565 end;
566
567 procedure TX3DSimpleMultField{$ifndef FPC} <
568 TItem;
569 TSingleField;
570 TItemList
571 > {$endif} .
572 Send(const AValue: array of TItem);
573 // This works in FPC (Delphi and ObjFpc mode), but doesn't in actual Delphi:
574 // type
575 // TMyClass = TX3DSimpleMultField {$ifndef FPC}<...>{$endif};
576 // TMyClassClass = class of TMyClass;
577 var
578 FieldValue: TX3DSimpleMultField{$ifndef FPC}<TItem, TSingleField,
579 TItemList>{$endif};
580 begin
581 if SendsByEvent then
582 begin
583 { We want to create an instance of ClassType (actual current class type),
584 not just an instance of TX3DSimpleMultField<T> (which is only an ancestor
585 of other MF field classes). }
586 // This works in FPC (Delphi and ObjFpc mode), but doesn't in actual Delphi:
587 // FieldValue := TX3DFieldClass(ClassType).Create(ParentNode, false, X3DName, AValue);
588
589 { AExposed = false below, because not needed otherwise. }
590 FieldValue := TX3DSimpleMultField{$ifndef FPC}<TItem, TSingleField,
591 TItemList>{$endif}(
592 TX3DFieldClass(ClassType).CreateUndefined(ParentNode, false, X3DName));
593 try
594 FieldValue.Items.AddRange(AValue);
595 Send(FieldValue);
596 finally FreeAndNil(FieldValue) end;
597 end else
598 begin
599 { When SendsByEvent = false, we can optimize this method.
600 No need to create an intermediate FieldValue instance
601 (and assign AValue to FieldValue, just to later assign FieldValue to Items). }
602 ValueFromIsClause := false;
603 Items.Assign(AValue);
604 Changed;
605 end;
606 end;
607
608 procedure TX3DSimpleMultField{$ifndef FPC} <
609 TItem;
610 TSingleField;
611 TItemList
612 > {$endif} .
613 Send(const AValue: TItemList);
614 var
615 FieldValue: TX3DSimpleMultField{$ifndef FPC}<TItem, TSingleField,
616 TItemList>{$endif};
617 begin
618 { This is a copy-paste of implementation of Send(const AValue: array of TItem) }
619 if SendsByEvent then
620 begin
621 FieldValue := TX3DSimpleMultField{$ifndef FPC}<TItem, TSingleField,
622 TItemList>{$endif}(
623 TX3DFieldClass(ClassType).CreateUndefined(ParentNode, false, X3DName));
624 try
625 FieldValue.Items.Assign(AValue);
626 Send(FieldValue);
627 finally FreeAndNil(FieldValue) end;
628 end else
629 begin
630 ValueFromIsClause := false;
631 Items.Assign(AValue);
632 Changed;
633 end;
634 end;
635
636 {$endif read_implementation}