~ubuntu-branches/ubuntu/utopic/castle-game-engine/utopic

« back to all changes in this revision

Viewing changes to src/x3d/x3dnodes_load.inc

  • Committer: Package Import Robot
  • Author(s): Abou Al Montacir
  • Date: 2013-04-27 18:06:40 UTC
  • Revision ID: package-import@ubuntu.com-20130427180640-eink4nmwzuivez1c
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{
 
2
  Copyright 2002-2013 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}
 
17
 
 
18
  { Container tracking VRML/X3D node and prototype names during parsing.
 
19
    Used by both classic and XML VRML/X3D readers. }
 
20
  TX3DReaderNames = class(TX3DReader)
 
21
  private
 
22
    FNodes: TX3DNodeNames;
 
23
    FPrototypes: TX3DPrototypeNames;
 
24
    FImported: TX3DNodeNames;
 
25
    FExported: TX3DNodeNames;
 
26
    FImportable: TX3DImportableNames;
 
27
    procedure CommonCreate(const AAutoRemoveNodes: boolean);
 
28
  public
 
29
    constructor Create(const AAutoRemoveNodes: boolean;
 
30
      const ABaseUrl: string;
 
31
      const AVersion: TX3DVersion);
 
32
    constructor CreateCopy(const AAutoRemoveNodes: boolean;
 
33
      Source: TX3DReader);
 
34
    destructor Destroy; override;
 
35
 
 
36
    { Extract names, before destructing this object.
 
37
      This method can be used only right before calling the destructor.
 
38
      It copies the prototype and exported names list (names visible
 
39
      from the outside), and sets them to @nil (to avoid releasing them
 
40
      at destruction). }
 
41
    procedure ExtractNames(out APrototypes: TX3DPrototypeNames;
 
42
      out AExported: TX3DNodeNames);
 
43
 
 
44
    { Current namespace for DEF/USE.
 
45
 
 
46
      This is a list without duplicates with all
 
47
      currently known node names. Objects[] of this list point to
 
48
      actual TX3DNode instances. If many instances had the same NodeName,
 
49
      only the last instance will be referenced here, following VRML spec
 
50
      (last DEF takes precedence).
 
51
 
 
52
      Internal notes: ParseNode doesn't modify this, only TX3DNode.Parse
 
53
      can do this. }
 
54
    property Nodes: TX3DNodeNames read FNodes;
 
55
 
 
56
    { Current namespace of PROTO names. }
 
57
    property Prototypes: TX3DPrototypeNames read FPrototypes;
 
58
 
 
59
    { Currently IMPORTed nodes.
 
60
 
 
61
      The nodes on this list are "bound" to their aliases,
 
62
      as this is the name under which they are visible in the current namespace.
 
63
      Alias is the identifier after the "AS" keyword in the "IMPORT" declaration
 
64
      (or, if no "AS xxx" clause was present, then alias is just the name
 
65
      under which node was exported). }
 
66
    property Imported: TX3DNodeNames read FImported;
 
67
 
 
68
    { Currently EXPORTed nodes from this scene.
 
69
 
 
70
      The nodes on this list are "bound" to their
 
71
      aliases, as this is the name under which they are visible for
 
72
      the outside VRML scenes (that can import these nodes).
 
73
      Alias is the identifier after the "AS" keyword in "EXPORT" declaration
 
74
      (or, if no "AS xxx" clause, then alias is just normal node name). }
 
75
    property Exported: TX3DNodeNames read FExported;
 
76
 
 
77
    { Currently loaded Inlines with importable nodes.
 
78
 
 
79
      The mechanism is that when you load an Inline node, the resulting
 
80
      "Exported" nodes (from the namespace within the Inline) get added
 
81
      to this "Importable" list. Then the "IMPORT" clause in this
 
82
      namespace can make "Importable" nodes into actually "Imported".
 
83
 
 
84
      This is a list with strings representing Inline node names
 
85
      (there's no way to IMPORT from unnamed Inline nodes).
 
86
      Objects[] of this list are instances of TX3DNodeNames
 
87
      corresponding to exported names within the inline. }
 
88
    property Importable: TX3DImportableNames read FImportable;
 
89
 
 
90
    procedure DoExport(E: TX3DExport);
 
91
    procedure DoImport(I: TX3DImport);
 
92
  end;
 
93
 
 
94
{$endif}
 
95
 
 
96
{$ifdef read_implementation}
 
97
 
 
98
{ TX3DReaderNames ----------------------------------------------------------------- }
 
99
 
 
100
constructor TX3DReaderNames.Create(const AAutoRemoveNodes: boolean;
 
101
  const ABaseUrl: string; const AVersion: TX3DVersion);
 
102
begin
 
103
  inherited Create(ABaseUrl, AVersion);
 
104
  CommonCreate(AAutoRemoveNodes);
 
105
end;
 
106
 
 
107
constructor TX3DReaderNames.CreateCopy(const AAutoRemoveNodes: boolean;
 
108
  Source: TX3DReader);
 
109
begin
 
110
  inherited CreateCopy(Source);
 
111
  CommonCreate(AAutoRemoveNodes);
 
112
end;
 
113
 
 
114
procedure TX3DReaderNames.CommonCreate(const AAutoRemoveNodes: boolean);
 
115
begin
 
116
  FNodes := TX3DNodeNames.Create(AAutoRemoveNodes);
 
117
  FPrototypes := TX3DPrototypeNames.Create;
 
118
  FImported := TX3DNodeNames.Create(AAutoRemoveNodes);
 
119
  FExported := TX3DNodeNames.Create(AAutoRemoveNodes);
 
120
  FImportable := TX3DImportableNames.Create;
 
121
end;
 
122
 
 
123
destructor TX3DReaderNames.Destroy;
 
124
begin
 
125
  FreeAndNil(FNodes);
 
126
  FreeAndNil(FPrototypes);
 
127
  FreeAndNil(FImported);
 
128
  FreeAndNil(FExported);
 
129
  FreeAndNil(FImportable);
 
130
  inherited;
 
131
end;
 
132
 
 
133
procedure TX3DReaderNames.ExtractNames(out APrototypes: TX3DPrototypeNames;
 
134
  out AExported: TX3DNodeNames);
 
135
begin
 
136
  APrototypes := FPrototypes;
 
137
  AExported := FExported;
 
138
 
 
139
  FPrototypes := nil;
 
140
  FExported := nil;
 
141
end;
 
142
 
 
143
procedure TX3DReaderNames.DoExport(E: TX3DExport);
 
144
var
 
145
  ExportedNode: TX3DNode;
 
146
  IgnoreNodeFinished: boolean;
 
147
begin
 
148
  ExportedNode := Nodes.Bound(E.ExportedNodeName, IgnoreNodeFinished);
 
149
  if ExportedNode = nil then
 
150
  begin
 
151
    OnWarning(wtMajor, 'VRML/X3D', Format('Exported node name "%s" not found', [E.ExportedNodeName]));
 
152
    Exit;
 
153
  end;
 
154
 
 
155
  Exported.Bind(ExportedNode, true, E.ExportedNodeAlias);
 
156
end;
 
157
 
 
158
procedure TX3DReaderNames.DoImport(I: TX3DImport);
 
159
var
 
160
  ImportedNames: TX3DNodeNames;
 
161
  ImportedNamesIndex: Integer;
 
162
  ImportedNode: TX3DNode;
 
163
  IgnoreNodeFinished: boolean;
 
164
begin
 
165
  ImportedNamesIndex := Importable.IndexOf(I.InlineNodeName);
 
166
  if ImportedNamesIndex = -1 then
 
167
  begin
 
168
    OnWarning(wtMajor, 'VRML/X3D', Format('Inline node name "%s" not found (or nothing was EXPORTed from it), cannot IMPORT', [I.InlineNodeName]));
 
169
    Exit;
 
170
  end;
 
171
 
 
172
  ImportedNames := Importable.Objects[ImportedNamesIndex] as TX3DNodeNames;
 
173
 
 
174
  ImportedNode := ImportedNames.Bound(I.ImportedNodeName, IgnoreNodeFinished);
 
175
  if ImportedNode = nil then
 
176
  begin
 
177
    OnWarning(wtMajor, 'VRML/X3D', Format('Imported node name "%s" not found in inline "%s"', [I.ImportedNodeName, I.InlineNodeName]));
 
178
    Exit;
 
179
  end;
 
180
 
 
181
  Imported.Bind(ImportedNode, true, I.ImportedNodeAlias);
 
182
end;
 
183
 
 
184
{$endif}