| 1 |
{ |
| 2 |
Copyright 2008-2022 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{$ifdef read_interface_nested} |
| 17 |
|
| 18 |
function ReadChildText(Element: TDOMElement; const ChildTagName: string): string; |
| 19 |
forward; |
| 20 |
|
| 21 |
{$endif read_interface_nested} |
| 22 |
|
| 23 |
{$ifdef read_implementation_nested} |
| 24 |
|
| 25 |
{ Read the contents of the text data inside single child ChildTagName. |
| 26 |
Useful to handle things like <init_from> and <source> elements in Collada. |
| 27 |
Returns empty string if ChildTagName not present (or present present more |
| 28 |
than once) in Element. } |
| 29 |
function ReadChildText(Element: TDOMElement; const ChildTagName: string): string; |
| 30 |
var |
| 31 |
Child: TDOMElement; |
| 32 |
begin |
| 33 |
Child := Element.ChildElement(ChildTagName, false); |
| 34 |
if Child <> nil then |
| 35 |
Result := Child.TextData |
| 36 |
else |
| 37 |
Result := ''; |
| 38 |
end; |
| 39 |
|
| 40 |
{ Read the contents of the text data inside single child ChildTagName, |
| 41 |
interpret them as Float. |
| 42 |
|
| 43 |
Returns false when such child not found (or occurs more than once), |
| 44 |
or when text cannot be converted to float. |
| 45 |
In this case, Value is guaranteed not to be modified. } |
| 46 |
function ReadChildFloat(Element: TDOMElement; const ChildTagName: string; |
| 47 |
var Value: Float): boolean; overload; |
| 48 |
var |
| 49 |
Child: TDOMElement; |
| 50 |
begin |
| 51 |
Child := Element.ChildElement(ChildTagName, false); |
| 52 |
Result := Child <> nil; |
| 53 |
if Result then |
| 54 |
try |
| 55 |
Value := StrToFloatDot(Child.TextData); |
| 56 |
except on EConvertError do Result := false; end; |
| 57 |
end; |
| 58 |
|
| 59 |
function ReadChildFloat(Element: TDOMElement; const ChildTagName: string; |
| 60 |
var Value: Single): boolean; overload; |
| 61 |
var |
| 62 |
ValueFloat: Float; |
| 63 |
begin |
| 64 |
ValueFloat := 0; { avoid uninitialized warnings } |
| 65 |
Result := ReadChildFloat(Element, ChildTagName, ValueFloat); |
| 66 |
if Result then |
| 67 |
Value := ValueFloat; |
| 68 |
end; |
| 69 |
|
| 70 |
{ Read the contents of the text data inside single child ChildTagName, |
| 71 |
interpret them as TVector3. |
| 72 |
|
| 73 |
Returns false when such child not found (or occurs more than once), |
| 74 |
or when cannot be converted to float. |
| 75 |
In this case, Value is guaranteed not to be modified. } |
| 76 |
function ReadChildVector(Element: TDOMElement; const ChildTagName: string; |
| 77 |
var Value: TVector3): boolean; |
| 78 |
var |
| 79 |
Child: TDOMElement; |
| 80 |
begin |
| 81 |
Child := Element.ChildElement(ChildTagName, false); |
| 82 |
Result := Child <> nil; |
| 83 |
if Result then |
| 84 |
try |
| 85 |
Value := Vector3FromStr(Child.TextData); |
| 86 |
except on EConvertError do Result := false; end; |
| 87 |
end; |
| 88 |
|
| 89 |
{ Read <library> element. |
| 90 |
Only for Collada < 1.4.x (Collada >= 1.4.x has <library_xxx> elements). } |
| 91 |
procedure ReadLibrary(LibraryElement: TDOMElement); |
| 92 |
var |
| 93 |
LibraryType: string; |
| 94 |
begin |
| 95 |
if LibraryElement.AttributeString('type', LibraryType) then |
| 96 |
begin |
| 97 |
if LibraryType = 'MATERIAL' then |
| 98 |
ReadLibraryMaterials(LibraryElement) else |
| 99 |
if LibraryType = 'GEOMETRY' then |
| 100 |
ReadLibraryGeometries(LibraryElement); |
| 101 |
{ other LibraryType not supported for now } |
| 102 |
end; |
| 103 |
end; |
| 104 |
|
| 105 |
{ Read <instance_*>, adding resulting X3D nodes into ParentGroup. } |
| 106 |
procedure ReadInstance(ParentGroup: TAbstractGroupingNode; |
| 107 |
InstantiatingElement: TDOMElement; List: TX3DChildrenList); |
| 108 |
var |
| 109 |
Id: string; |
| 110 |
Node: TAbstractChildNode; |
| 111 |
begin |
| 112 |
if InstantiatingElement.AttributeString('url', Id) and |
| 113 |
SCharIs(Id, 1, '#') then |
| 114 |
begin |
| 115 |
Delete(Id, 1, 1); |
| 116 |
Node := List.FindName(Id); |
| 117 |
if Node = nil then |
| 118 |
WritelnWarning('Collada', Format('<node> instantiates non-existing element "%s"', |
| 119 |
[Id])) else |
| 120 |
ParentGroup.AddChildren(Node); |
| 121 |
end else |
| 122 |
WritelnWarning('Collada', Format('Element <%s> missing url attribute (that has to start with #)', |
| 123 |
[InstantiatingElement.TagName])); |
| 124 |
end; |
| 125 |
|
| 126 |
{$endif read_implementation_nested} |