| 1 |
{%MainUnit x3dfields.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-2022 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 |
{ Base class for any item within X3D file: a node, a field, a route, |
| 20 |
a prototype etc. We need a common base class for all such things |
| 21 |
to store PositionInParent. } |
| 22 |
TX3DFileItem = class(TPersistent) |
| 23 |
{$ifndef FPC} // TODO: these things are used as interfaces for Delphi generics, so they have to implement _AddRef etc. |
| 24 |
public |
| 25 |
function _AddRef: Integer; stdcall; |
| 26 |
function _Release: Integer; stdcall; |
| 27 |
function QueryInterface(const IID: TGUID; out Obj): Hresult; virtual; stdcall; |
| 28 |
{$endif} |
| 29 |
private |
| 30 |
FPositionInParent: Integer; |
| 31 |
|
| 32 |
{ Secondary order for saving items to VRML/X3D file. |
| 33 |
When PositionInParent are equal, this decides which item is first. |
| 34 |
It may be useful, since SortPositionInParent is not a stable sort |
| 35 |
(because QuickSort is not stable), so using this to preserve order |
| 36 |
may be helpful. |
| 37 |
|
| 38 |
TX3DFileItemList.Add sets this, which allows to preserve |
| 39 |
order when saving. } |
| 40 |
PositionOnList: Integer; |
| 41 |
public |
| 42 |
constructor Create; |
| 43 |
|
| 44 |
{ Position of this item within parent VRML/X3D node, used for saving |
| 45 |
the VRML/X3D graph to file. Default value -1 means "undefined". |
| 46 |
|
| 47 |
For normal usage and processing of VRML graph, this is totally not needed. |
| 48 |
This position doesn't dictate actual meaning of VRML graph. |
| 49 |
If you're looking to change order of nodes, you probably want |
| 50 |
to rather look at something like ReplaceItems within TMFNode or such. |
| 51 |
|
| 52 |
This field is purely a hint when encoding VRML file how to order |
| 53 |
VRML items (nodes, fields, routes, protos) within parent node |
| 54 |
or the VRML file. Reason: VRML allows non-unique node names. |
| 55 |
Each DEF XXX overrides all previous ("previous" in lexical sense, |
| 56 |
i.e. normal order of tokens in the file) DEF XXX with the same XXX, |
| 57 |
thus hiding previous node name "XXX". |
| 58 |
This means that when saving VRML file we have to be very careful |
| 59 |
about the order of items, such that e.g. all routes are specified |
| 60 |
when appropriate node names are bound. |
| 61 |
|
| 62 |
This is a relative position, relative to other PositionInParent |
| 63 |
value of other TX3DFileItem items. So it's not necessary |
| 64 |
to keep all PositionInParent different or successive within some |
| 65 |
parent. When saving, we will sort everything according to |
| 66 |
PositionInParent. |
| 67 |
|
| 68 |
See e.g. ../../../demo_models/x3d/tricky_def_use.x3dv |
| 69 |
for tests of some tricky layout. When reading such file we have |
| 70 |
to record PositionInParent to be able to save such file correctly. } |
| 71 |
property PositionInParent: Integer |
| 72 |
read FPositionInParent write FPositionInParent default -1; |
| 73 |
|
| 74 |
{ Save to stream. } |
| 75 |
procedure SaveToStream(Writer: TX3DWriter); virtual; abstract; |
| 76 |
|
| 77 |
{ How is this saved to X3D XML encoding. This determines when |
| 78 |
SaveToStream is called. It also cooperates with some SaveToStream |
| 79 |
implementations, guiding how the item is actually saved. |
| 80 |
By default it is sxChildElement. } |
| 81 |
function SaveToXml: TSaveToXmlMethod; virtual; |
| 82 |
end; |
| 83 |
|
| 84 |
TX3DFileItemList = class({$ifdef FPC}specialize{$endif} TObjectList<TX3DFileItem>) |
| 85 |
public |
| 86 |
procedure SortPositionInParent; |
| 87 |
{ Sort all items by PositionInParent and then save them all to stream. } |
| 88 |
procedure SaveToStream(Writer: TX3DWriter); |
| 89 |
procedure Add(Item: TX3DFileItem); reintroduce; |
| 90 |
end; |
| 91 |
|
| 92 |
{$endif read_interface} |
| 93 |
|
| 94 |
{$ifdef read_implementation} |
| 95 |
|
| 96 |
{ TX3DFileItem -------------------------------------------------------------- } |
| 97 |
|
| 98 |
constructor TX3DFileItem.Create; |
| 99 |
begin |
| 100 |
inherited; |
| 101 |
FPositionInParent := -1; |
| 102 |
end; |
| 103 |
|
| 104 |
function TX3DFileItem.SaveToXml: TSaveToXmlMethod; |
| 105 |
begin |
| 106 |
Result := sxChildElement; |
| 107 |
end; |
| 108 |
|
| 109 |
{$ifndef FPC} |
| 110 |
function TX3DFileItem._AddRef: Integer; |
| 111 |
begin |
| 112 |
Result := -1; |
| 113 |
end; |
| 114 |
|
| 115 |
function TX3DFileItem._Release: Integer; |
| 116 |
begin |
| 117 |
Result := -1; |
| 118 |
end; |
| 119 |
|
| 120 |
function TX3DFileItem.QueryInterface(const IID: TGUID; out Obj): Hresult; |
| 121 |
begin |
| 122 |
if GetInterface(IID, Obj) then |
| 123 |
Result := S_OK |
| 124 |
else |
| 125 |
Result := E_NOINTERFACE; |
| 126 |
end; |
| 127 |
{$endif} |
| 128 |
|
| 129 |
{ TX3DFileItemList --------------------------------------------------------- } |
| 130 |
|
| 131 |
function IsSmallerPositionInParent( |
| 132 |
{$ifdef GENERICS_CONSTREF}constref{$else}const{$endif} A, B: TX3DFileItem): Integer; |
| 133 |
begin |
| 134 |
Result := A.PositionInParent - B.PositionInParent; |
| 135 |
if Result = 0 then |
| 136 |
Result := A.PositionOnList - B.PositionOnList; |
| 137 |
end; |
| 138 |
|
| 139 |
procedure TX3DFileItemList.SortPositionInParent; |
| 140 |
type |
| 141 |
TX3DFileItemComparer = {$ifdef FPC}specialize{$endif} TComparer<TX3DFileItem>; |
| 142 |
begin |
| 143 |
Sort(TX3DFileItemComparer.Construct( |
| 144 |
{$ifdef FPC}@{$endif}IsSmallerPositionInParent)); |
| 145 |
end; |
| 146 |
|
| 147 |
procedure TX3DFileItemList.SaveToStream(Writer: TX3DWriter); |
| 148 |
var |
| 149 |
I: Integer; |
| 150 |
begin |
| 151 |
SortPositionInParent; |
| 152 |
for I := 0 to Count - 1 do |
| 153 |
Items[I].SaveToStream(Writer); |
| 154 |
end; |
| 155 |
|
| 156 |
procedure TX3DFileItemList.Add(Item: TX3DFileItem); |
| 157 |
begin |
| 158 |
Item.PositionOnList := Count; |
| 159 |
inherited Add(Item); |
| 160 |
end; |
| 161 |
|
| 162 |
{$endif read_implementation} |