| 1 |
{%MainUnit x3dnodes.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 |
{ Node types used with @link(TVRML1State). } |
| 20 |
TVRML1StateNode = |
| 21 |
( |
| 22 |
vsCoordinate3, |
| 23 |
vsShapeHints, |
| 24 |
vsFontStyle, |
| 25 |
vsMaterial, |
| 26 |
vsMaterialBinding, |
| 27 |
vsNormal, |
| 28 |
vsNormalBinding, |
| 29 |
vsTexture2, |
| 30 |
vsTextureCoordinate2 |
| 31 |
); |
| 32 |
|
| 33 |
{ Nodes that are saved during VRML/X3D traversing. |
| 34 |
Used for @link(TX3DGraphTraverseState.VRML1State). |
| 35 |
These nodes affect some following nodes in the graph, in VRML 1.0. |
| 36 |
|
| 37 |
They are never @nil after a @link(TX3DNode.Traverse) operation. |
| 38 |
the traversing code always takes care to initialize |
| 39 |
them to default nodes at the beginning. |
| 40 |
|
| 41 |
For all nodes enumerated here: it's guaranteed |
| 42 |
they don't affect the state (@link(TX3DGraphTraverseState)) during traversing |
| 43 |
(that is, they don't do anything special in |
| 44 |
TX3DNode.BeforeTraverse / TX3DNode.MiddleTraverse / TX3DNode.AfterTraverse). |
| 45 |
So it's guaranteed that changing some field's value of a node |
| 46 |
within TVRML1State affects @italic(only) |
| 47 |
the shapes that have given node inside State.VRML1State. |
| 48 |
TCastleSceneCore.InternalChangedField depends on that. } |
| 49 |
TVRML1State = class |
| 50 |
strict private |
| 51 |
Owned: array [TVRML1StateNode] of boolean; |
| 52 |
FNodes: array [TVRML1StateNode] of TX3DNode; |
| 53 |
function GetNode(const StateNode: TVRML1StateNode): TX3DNode; |
| 54 |
procedure SetNode(const StateNode: TVRML1StateNode; const Node: TX3DNode); |
| 55 |
public |
| 56 |
function Coordinate3: TCoordinate3Node_1; |
| 57 |
function ShapeHints: TShapeHintsNode_1; |
| 58 |
function FontStyle: TFontStyleNode_1; |
| 59 |
function Material: TMaterialNode_1; |
| 60 |
function MaterialBinding: TMaterialBindingNode_1; |
| 61 |
function Normal: TNormalNode; |
| 62 |
function NormalBinding: TNormalBindingNode_1; |
| 63 |
function Texture2: TTexture2Node_1; |
| 64 |
function TextureCoordinate2: TTextureCoordinate2Node_1; |
| 65 |
|
| 66 |
{ Destructor automatically frees and nils all @link(Nodes) that are owned. } |
| 67 |
destructor Destroy; override; |
| 68 |
|
| 69 |
{ Last nodes that occured when visiting this VRML 1.0 graph, |
| 70 |
indexed by TVRML1StateNode. } |
| 71 |
property Nodes [const StateNode: TVRML1StateNode]: TX3DNode read GetNode write SetNode; |
| 72 |
|
| 73 |
{ Create and assign all @link(Nodes). All nodes become owned. } |
| 74 |
procedure CreateNodes; |
| 75 |
|
| 76 |
{ Assign from Source. |
| 77 |
|
| 78 |
During doing this, old nodes are freed if they were owned. |
| 79 |
New nodes are not owned. |
| 80 |
|
| 81 |
This takes care of checking for each TVRML1StateNode |
| 82 |
if the old node is equal to new one. If yes, then the node |
| 83 |
if not freed (regardless of "owned" status), and the "owned" |
| 84 |
status is left as-is (not chaned to false). |
| 85 |
This way calling something like @code(MyState.Assign(MyState)) |
| 86 |
is a valid and harmless operation. } |
| 87 |
procedure Assign(const Source: TVRML1State); |
| 88 |
|
| 89 |
{ Set the node, and make it owned by this class. |
| 90 |
Contrast this with @code(Nodes[StateNode] := Node) that makes |
| 91 |
the node @italic(not) owned. This is just a shortcut for |
| 92 |
@longCode(# |
| 93 |
Nodes[StateNode] := Node; |
| 94 |
Owned[StateNode] := true; |
| 95 |
#) |
| 96 |
} |
| 97 |
procedure SetOwnNode(const StateNode: TVRML1StateNode; const Node: TX3DNode); |
| 98 |
end; |
| 99 |
|
| 100 |
{$endif read_interface} |
| 101 |
|
| 102 |
{$ifdef read_implementation} |
| 103 |
|
| 104 |
{ TVRML1State ---------------------------------------------------- } |
| 105 |
|
| 106 |
function TVRML1State.Coordinate3 : TCoordinate3Node_1 ; begin Result := TCoordinate3Node_1 (FNodes[vsCoordinate3 ]); end; |
| 107 |
function TVRML1State.ShapeHints : TShapeHintsNode_1 ; begin Result := TShapeHintsNode_1 (FNodes[vsShapeHints ]); end; |
| 108 |
function TVRML1State.FontStyle : TFontStyleNode_1 ; begin Result := TFontStyleNode_1 (FNodes[vsFontStyle ]); end; |
| 109 |
function TVRML1State.Material : TMaterialNode_1 ; begin Result := TMaterialNode_1 (FNodes[vsMaterial ]); end; |
| 110 |
function TVRML1State.MaterialBinding : TMaterialBindingNode_1 ; begin Result := TMaterialBindingNode_1 (FNodes[vsMaterialBinding ]); end; |
| 111 |
function TVRML1State.Normal : TNormalNode ; begin Result := TNormalNode (FNodes[vsNormal ]); end; |
| 112 |
function TVRML1State.NormalBinding : TNormalBindingNode_1 ; begin Result := TNormalBindingNode_1 (FNodes[vsNormalBinding ]); end; |
| 113 |
function TVRML1State.Texture2 : TTexture2Node_1 ; begin Result := TTexture2Node_1 (FNodes[vsTexture2 ]); end; |
| 114 |
function TVRML1State.TextureCoordinate2: TTextureCoordinate2Node_1; begin Result := TTextureCoordinate2Node_1(FNodes[vsTextureCoordinate2]); end; |
| 115 |
|
| 116 |
function TVRML1State.GetNode(const StateNode: TVRML1StateNode): TX3DNode; |
| 117 |
begin |
| 118 |
Result := FNodes[StateNode]; |
| 119 |
end; |
| 120 |
|
| 121 |
procedure TVRML1State.SetNode( |
| 122 |
const StateNode: TVRML1StateNode; const Node: TX3DNode); |
| 123 |
begin |
| 124 |
if FNodes[StateNode] <> Node then |
| 125 |
begin |
| 126 |
{$warnings off} // using deprecated to keep supporting VRML 1 |
| 127 |
Assert((Node = nil) or (Node is VRML1StateClasses[StateNode])); |
| 128 |
{$warnings on} |
| 129 |
|
| 130 |
if Owned[StateNode] then |
| 131 |
FNodes[StateNode].Free; |
| 132 |
FNodes[StateNode] := Node; |
| 133 |
Owned[StateNode] := false; |
| 134 |
end; |
| 135 |
end; |
| 136 |
|
| 137 |
procedure TVRML1State.CreateNodes; |
| 138 |
var |
| 139 |
SN: TVRML1StateNode; |
| 140 |
begin |
| 141 |
for SN := Low(SN) to High(SN) do |
| 142 |
begin |
| 143 |
{$warnings off} // using deprecated to keep supporting VRML 1 |
| 144 |
Nodes[SN] := VRML1StateClasses[SN].Create; |
| 145 |
{$warnings on} |
| 146 |
Owned[SN] := true; |
| 147 |
end; |
| 148 |
end; |
| 149 |
|
| 150 |
destructor TVRML1State.Destroy; |
| 151 |
|
| 152 |
procedure FreeAndNilNodes; |
| 153 |
var |
| 154 |
SN: TVRML1StateNode; |
| 155 |
begin |
| 156 |
for SN := Low(SN) to High(SN) do |
| 157 |
begin |
| 158 |
if Owned[SN] then |
| 159 |
FNodes[SN].Free; |
| 160 |
FNodes[SN] := nil; |
| 161 |
end; |
| 162 |
end; |
| 163 |
|
| 164 |
begin |
| 165 |
FreeAndNilNodes; |
| 166 |
inherited; |
| 167 |
end; |
| 168 |
|
| 169 |
procedure TVRML1State.Assign(const Source: TVRML1State); |
| 170 |
var |
| 171 |
SN: TVRML1StateNode; |
| 172 |
begin |
| 173 |
for SN := Low(SN) to High(SN) do |
| 174 |
Nodes[SN] := Source.FNodes[SN]; // this is called so often that even changing Nodes to FNodes makes a difference |
| 175 |
end; |
| 176 |
|
| 177 |
procedure TVRML1State.SetOwnNode(const StateNode: TVRML1StateNode; |
| 178 |
const Node: TX3DNode); |
| 179 |
begin |
| 180 |
Nodes[StateNode] := Node; |
| 181 |
Owned[StateNode] := true; |
| 182 |
end; |
| 183 |
|
| 184 |
{$endif read_implementation} |