Debian Codegraph @

src/scene/x3d/castlefields_x3dwriter.inc

raw
1 {%MainUnit x3dfields.pas}
2 {
3 Copyright 2002-2018 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 TX3DEvent = class;
20
21 { Writer of VRML/X3D to stream. }
22 TX3DWriter = class abstract
23 strict private
24 Indent: string;
25 DoDiscardNextIndent: boolean;
26 FEncoding: TX3DEncoding;
27 FStream: TStream;
28 public
29 { Which VRML/X3D version are we writing. Read-only after creation. }
30 Version: TX3DVersion;
31
32 { Parent node, always TX3DNode, @nil if this is top-level.
33 Fow now only used to enhance DefaultContainerFieldInContext knowledge,
34 for XML encoding writing, though we maintain it for all encodings. }
35 ParentNode: TObject;
36
37 constructor Create(AStream: TStream;
38 const AVersion: TX3DVersion; const AEncoding: TX3DEncoding);
39 destructor Destroy; override;
40
41 property Encoding: TX3DEncoding read FEncoding;
42
43 procedure IncIndent;
44 procedure DecIndent;
45
46 { Comfortable routines that simply write given string to a stream.
47 @groupBegin }
48 procedure Write(const S: string);
49 procedure Writeln(const S: string); overload;
50 procedure Writeln; overload;
51 procedure WriteIndent(const S: string);
52 procedure WritelnIndent(const S: string);
53 { @groupEnd }
54
55 { Causes next WriteIndent or WritelnIndent too not write the Indent.
56 Useful in some cases to improve readability of generated VRML file. }
57 procedure DiscardNextIndent;
58 end;
59
60 {$endif read_interface}
61
62 {$ifdef read_implementation}
63
64 { TX3DWriter ----------------------------------------------------------------- }
65
66 const
67 { IndentIncrement is string or char. It's used by SaveToStream }
68 IndentIncrement = CharTab;
69
70 constructor TX3DWriter.Create(AStream: TStream; const AVersion: TX3DVersion;
71 const AEncoding: TX3DEncoding);
72 begin
73 inherited Create;
74 Version := AVersion;
75 FStream := AStream;
76 FEncoding := AEncoding;
77 end;
78
79 destructor TX3DWriter.Destroy;
80 begin
81 inherited;
82 end;
83
84 procedure TX3DWriter.IncIndent;
85 var
86 L: Integer;
87 begin
88 L := Length(Indent) + 1;
89 SetLength(Indent, L);
90 Indent[L] := IndentIncrement;
91 end;
92
93 procedure TX3DWriter.DecIndent;
94 begin
95 SetLength(Indent, Length(Indent) - 1);
96 end;
97
98 procedure TX3DWriter.Write(const S: string);
99 begin
100 WriteStr(FStream, S);
101 end;
102
103 procedure TX3DWriter.Writeln(const S: string);
104 begin
105 WriteStr(FStream, S);
106 WriteStr(FStream, NL);
107 end;
108
109 procedure TX3DWriter.Writeln;
110 begin
111 WriteStr(FStream, NL);
112 end;
113
114 procedure TX3DWriter.WriteIndent(const S: string);
115 begin
116 if DoDiscardNextIndent then
117 DoDiscardNextIndent := false else
118 WriteStr(FStream, Indent);
119 WriteStr(FStream, S);
120 end;
121
122 procedure TX3DWriter.WritelnIndent(const S: string);
123 begin
124 WriteIndent(S);
125 WriteStr(FStream, NL);
126 end;
127
128 procedure TX3DWriter.DiscardNextIndent;
129 begin
130 DoDiscardNextIndent := true;
131 end;
132
133 {$endif read_implementation}