]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/brushxml.h
fixed nudge-selection and build-monitoring-defaults
[xonotic/netradiant.git] / radiant / brushxml.h
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #if !defined(INCLUDED_BRUSHXML_H)
23 #define INCLUDED_BRUSHXML_H
24
25 #include "stream/stringstream.h"
26 #include "xml/xmlelement.h"
27
28 #include "brush.h"
29
30 inline void FaceTexdef_BP_importXML(FaceTexdef& texdef, const char* xmlContent)
31 {
32   StringTokeniser content(xmlContent);
33
34   texdef.m_projection.m_brushprimit_texdef.coords[0][0] = static_cast<float>(atof(content.getToken()));
35   texdef.m_projection.m_brushprimit_texdef.coords[0][1] = static_cast<float>(atof(content.getToken()));
36   texdef.m_projection.m_brushprimit_texdef.coords[0][2] = static_cast<float>(atof(content.getToken()));
37   texdef.m_projection.m_brushprimit_texdef.coords[1][0] = static_cast<float>(atof(content.getToken()));
38   texdef.m_projection.m_brushprimit_texdef.coords[1][1] = static_cast<float>(atof(content.getToken()));
39   texdef.m_projection.m_brushprimit_texdef.coords[1][2] = static_cast<float>(atof(content.getToken()));
40 }
41 inline void FaceTexdef_importXML(FaceTexdef& texdef, const char* xmlContent)
42 {
43   StringTokeniser content(xmlContent);
44
45   texdef.m_projection.m_texdef.shift[0] = static_cast<float>(atof(content.getToken()));
46   texdef.m_projection.m_texdef.shift[1] = static_cast<float>(atof(content.getToken()));
47   texdef.m_projection.m_texdef.rotate = static_cast<float>(atof(content.getToken()));
48   texdef.m_projection.m_texdef.scale[0] = static_cast<float>(atof(content.getToken()));
49   texdef.m_projection.m_texdef.scale[1] = static_cast<float>(atof(content.getToken()));
50
51   ASSERT_MESSAGE(texdef_sane(texdef.m_projection.m_texdef), "FaceTexdef_importXML: bad texdef");
52 }
53
54 inline void FacePlane_importXML(FacePlane& facePlane, const char* xmlContent)
55 {
56   StringTokeniser content(xmlContent);
57
58   for (int i = 0; i < 3; ++i)
59   {
60     for (int j = 0; j < 3; ++j)
61     {
62       facePlane.planePoints()[i][j] = atof(content.getToken());
63     }
64   }
65   facePlane.MakePlane();
66 }
67
68
69 class FaceXMLImporter
70 {
71   struct xml_state_t
72   {
73     enum EState
74     {
75       eDefault,
76       ePlanePts,
77       eTexdef,
78       eBPMatrix,
79       eFlags,
80       eShader,
81     };
82
83     EState m_state;
84     StringOutputStream m_content;
85
86     xml_state_t(EState state)
87       : m_state(state)
88     {}
89
90     EState state() const
91     {
92       return m_state;
93     }
94     const char* content() const
95     {
96       return m_content.c_str();
97     }
98     std::size_t write(const char* buffer, std::size_t length)
99     {
100       return m_content.write(buffer, length);
101     }
102   };
103
104   std::vector<xml_state_t> m_xml_state;
105   Face& m_face;
106 public:
107   FaceXMLImporter(Face& face) : m_face(face)
108   {
109     m_xml_state.push_back(xml_state_t::eDefault);
110   }
111   ~FaceXMLImporter()
112   {
113     m_face.planeChanged();
114   }
115
116   void pushElement(const XMLElement& element)
117   {
118     ASSERT_MESSAGE(m_xml_state.back().state() == xml_state_t::eDefault, "parse error");
119
120     if(strcmp(element.name(), "planepts") == 0)
121     {
122       m_xml_state.push_back(xml_state_t::ePlanePts);
123     }
124     else if(strcmp(element.name(), "texdef") == 0)
125     {
126       m_xml_state.push_back(xml_state_t::eTexdef);
127     }
128     else if(strcmp(element.name(), "bpmatrix") == 0)
129     {
130       m_xml_state.push_back(xml_state_t::eBPMatrix);
131     }
132     else if(strcmp(element.name(), "flags") == 0)
133     {
134       m_xml_state.push_back(xml_state_t::eFlags);
135     }
136     else if(strcmp(element.name(), "shader") == 0)
137     {
138       m_xml_state.push_back(xml_state_t::eShader);
139     }
140   }
141   void popElement(const char* name)
142   {
143     ASSERT_MESSAGE(m_xml_state.back().state() != xml_state_t::eDefault, "parse error");
144
145     switch(m_xml_state.back().state())
146     {
147     case xml_state_t::ePlanePts:
148       {
149         FacePlane_importXML(m_face.getPlane(), m_xml_state.back().content());
150       }
151       break;
152     case xml_state_t::eTexdef:
153       {
154         FaceTexdef_importXML(m_face.getTexdef(), m_xml_state.back().content());
155       }
156       break;
157     case xml_state_t::eBPMatrix:
158       {
159         FaceTexdef_BP_importXML(m_face.getTexdef(), m_xml_state.back().content());
160       }
161       break;
162    case xml_state_t::eFlags:
163      {
164         StringTokeniser content(m_xml_state.back().content());
165
166         m_face.getShader().m_flags.m_contentFlags = atoi(content.getToken());
167         m_face.getShader().m_flags.m_surfaceFlags = atoi(content.getToken());
168         m_face.getShader().m_flags.m_value = atoi(content.getToken());
169       }
170       break;
171     case xml_state_t::eShader:
172       {
173         m_face.getShader().setShader(m_xml_state.back().content());
174       }
175       break;
176     default:
177       break;
178     }
179
180     m_xml_state.pop_back();
181   }
182   std::size_t write(const char* data, std::size_t length)
183   {
184     ASSERT_MESSAGE(!m_xml_state.empty(), "parse error");
185     return m_xml_state.back().write(data, length);
186   }
187 };
188
189
190 inline void FaceTexdef_exportXML(const FaceTexdef& texdef, XMLImporter& importer)
191 {
192   StaticElement element("texdef");
193   importer.pushElement(element);
194
195   ASSERT_MESSAGE(texdef_sane(texdef.m_projection.m_texdef), "FaceTexdef_exportXML: bad texdef");
196
197   importer << texdef.m_projection.m_texdef.shift[0]
198     << ' ' << texdef.m_projection.m_texdef.shift[1]
199     << ' ' << texdef.m_projection.m_texdef.rotate
200     << ' ' << texdef.m_projection.m_texdef.scale[0]
201     << ' ' << texdef.m_projection.m_texdef.scale[1];
202
203   importer.popElement(element.name());
204 }
205 inline void FaceTexdef_BP_exportXML(const FaceTexdef& texdef, XMLImporter& importer)
206 {
207   StaticElement element("texdef");
208   importer.pushElement(element);
209
210   for(int i = 0; i < 2; ++i)
211   {
212     for(int j = 0; j < 3; ++j)
213     {
214       importer << texdef.m_projection.m_brushprimit_texdef.coords[i][j] << ' ';
215     }
216   }
217
218   importer.popElement(element.name());
219 }
220 inline void FaceShader_ContentsFlagsValue_exportXML(const FaceShader& faceShader, XMLImporter& importer)
221 {
222   StaticElement element("flags");
223   importer.pushElement(element);
224
225   {
226     importer << faceShader.m_flags.m_contentFlags
227       << ' ' << faceShader.m_flags.m_surfaceFlags
228       << ' ' << faceShader.m_flags.m_value;
229   }
230
231   importer.popElement(element.name());
232 }
233
234 inline void FacePlane_exportXML(const FacePlane& facePlane, XMLImporter& importer)
235 {
236   StaticElement element("planepts");
237   importer.pushElement(element);
238
239   {
240     // write planepts
241     for (int i=0 ; i<3 ; i++)
242     {
243       for (int j=0 ; j<3 ; j++)
244       {
245         importer << Face::m_quantise(facePlane.planePoints()[i][j]) << ' ';
246       }
247     }
248   }
249
250   importer.popElement(element.name());
251 }
252
253 class FaceXMLExporter
254 {
255   const Face& m_face;
256 public:
257   FaceXMLExporter(const Face& face) : m_face(face)
258   {
259   }
260   void exportXML(XMLImporter& importer)
261   {
262     bool bAlternateTexdef = (Face::m_type == eBrushTypeQuake3BP || Face::m_type == eBrushTypeDoom3 || Face::m_type == eBrushTypeQuake4);
263  
264     // write shader
265     {
266       StaticElement element("shader"); 
267       importer.pushElement(element);
268       importer << m_face.getShader().getShader();
269       importer.popElement(element.name());
270     }
271
272     FacePlane_exportXML(m_face.getPlane(), importer);
273
274     if(!bAlternateTexdef)
275     {
276       FaceTexdef_exportXML(m_face.getTexdef(), importer);
277     }
278     else
279     {
280       FaceTexdef_BP_exportXML(m_face.getTexdef(), importer);
281     }
282
283     FaceShader_ContentsFlagsValue_exportXML(m_face.getShader(), importer);
284   }
285 };
286
287
288 class BrushXMLImporter : public XMLImporter
289 {
290   class xml_state_t
291   {
292   public:
293     enum EState
294     {
295       eDefault,
296       eBrush,
297       eFace,
298     };
299
300   private:
301     EState m_state;
302
303   public:
304     xml_state_t(EState state)
305       : m_state(state)
306     {
307     }
308     EState state() const
309     {
310       return m_state;
311     }
312   };
313
314   std::vector<xml_state_t> m_xml_state;
315   char m_faceImporter[sizeof(FaceXMLImporter)];
316   Brush& m_brush;
317
318   FaceXMLImporter& faceImporter()
319   {
320     return *reinterpret_cast<FaceXMLImporter*>(m_faceImporter);
321   }
322
323 public:
324   BrushXMLImporter(Brush& brush) : m_brush(brush)
325   {
326     m_xml_state.push_back(xml_state_t::eDefault);
327   }
328   void pushElement(const XMLElement& element)
329   {
330     switch(m_xml_state.back().state())
331     {
332     case xml_state_t::eDefault:
333       ASSERT_MESSAGE(strcmp(element.name(), "brush") == 0, "parse error");
334       m_xml_state.push_back(xml_state_t::eBrush);
335       break;
336     case xml_state_t::eBrush:
337       ASSERT_MESSAGE(strcmp(element.name(), "plane") == 0, "parse error");
338       m_xml_state.push_back(xml_state_t::eFace);
339       m_brush.push_back(FaceSmartPointer(new Face(&m_brush)));
340       constructor(faceImporter(), makeReference(*m_brush.back()));
341       m_brush.planeChanged();
342       m_brush.shaderChanged();
343       break;
344     case xml_state_t::eFace:
345       m_xml_state.push_back(xml_state_t::eFace);
346       faceImporter().pushElement(element);
347       break;
348     }
349   }
350   void popElement(const char* name)
351   {
352     ASSERT_MESSAGE(!m_xml_state.empty(), "parse error");
353     m_xml_state.pop_back();
354
355     switch(m_xml_state.back().state())
356     {
357     case xml_state_t::eDefault:
358       break;
359     case xml_state_t::eBrush:
360       destructor(faceImporter());
361       break;
362     case xml_state_t::eFace:
363       faceImporter().popElement(name);
364       break;
365     }
366   }
367   std::size_t write(const char* data, std::size_t length)
368   {
369     switch(m_xml_state.back().state())
370     {
371     case xml_state_t::eFace:
372       return faceImporter().write(data, length);
373       break;
374     default:
375       break;
376     }
377     return length;
378   }
379 };
380
381 class BrushXMLExporter : public XMLExporter
382 {
383   const Brush& m_brush;
384
385 public:
386   BrushXMLExporter(const Brush& brush) : m_brush(brush)
387   {
388   }
389   void exportXML(XMLImporter& importer)
390   {
391     m_brush.evaluateBRep(); // ensure b-rep is up-to-date, so that non-contributing faces can be identified.
392     ASSERT_MESSAGE(m_brush.hasContributingFaces(), "exporting an empty brush");
393
394     const StaticElement brushElement("brush");
395     importer.pushElement(brushElement);
396
397     for(Brush::const_iterator i = m_brush.begin(); i != m_brush.end(); ++i)
398     {
399       if((*i)->contributes())
400       {
401         const StaticElement element("plane");
402         importer.pushElement(element);
403         FaceXMLExporter(*(*i)).exportXML(importer);
404         importer.popElement(element.name());
405       }
406     }
407
408     importer.popElement(brushElement.name());
409   }
410 };
411
412
413 #endif