]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/mapq3/write.cpp
reformat code! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / plugins / mapq3 / write.cpp
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 #include "write.h"
23
24 #include "ientity.h"
25 #include "iscriplib.h"
26 #include "scenelib.h"
27
28 inline MapExporter *Node_getMapExporter(scene::Node &node)
29 {
30     return NodeTypeCast<MapExporter>::cast(node);
31 }
32
33
34 static std::size_t g_count_entities;
35 static std::size_t g_count_brushes;
36
37
38 void Entity_ExportTokens(const Entity &entity, TokenWriter &writer)
39 {
40     g_count_brushes = 0;
41
42     class WriteKeyValue : public Entity::Visitor {
43         TokenWriter &m_writer;
44     public:
45         WriteKeyValue(TokenWriter &writer)
46                 : m_writer(writer)
47         {
48         }
49
50         void visit(const char *key, const char *value)
51         {
52             m_writer.writeString(key);
53             m_writer.writeString(value);
54             m_writer.nextLine();
55         }
56
57     } visitor(writer);
58
59     entity.forEachKeyValue(visitor);
60 }
61
62 class WriteTokensWalker : public scene::Traversable::Walker {
63     mutable Stack<bool> m_stack;
64     TokenWriter &m_writer;
65     bool m_ignorePatches;
66 public:
67     WriteTokensWalker(TokenWriter &writer, bool ignorePatches)
68             : m_writer(writer), m_ignorePatches(ignorePatches)
69     {
70     }
71
72     bool pre(scene::Node &node) const
73     {
74         m_stack.push(false);
75
76         Entity *entity = Node_getEntity(node);
77         if (entity != 0) {
78             m_writer.writeToken("//");
79             m_writer.writeToken("entity");
80             m_writer.writeUnsigned(g_count_entities++);
81             m_writer.nextLine();
82
83             m_writer.writeToken("{");
84             m_writer.nextLine();
85             m_stack.top() = true;
86
87             Entity_ExportTokens(*entity, m_writer);
88         } else {
89             MapExporter *exporter = Node_getMapExporter(node);
90             if (exporter != 0
91                 && !(m_ignorePatches && Node_isPatch(node))) {
92                 m_writer.writeToken("//");
93                 m_writer.writeToken("brush");
94                 m_writer.writeUnsigned(g_count_brushes++);
95                 m_writer.nextLine();
96
97                 exporter->exportTokens(m_writer);
98             }
99         }
100
101         return true;
102     }
103
104     void post(scene::Node &node) const
105     {
106         if (m_stack.top()) {
107             m_writer.writeToken("}");
108             m_writer.nextLine();
109         }
110         m_stack.pop();
111     }
112 };
113
114 void Map_Write(scene::Node &root, GraphTraversalFunc traverse, TokenWriter &writer, bool ignorePatches)
115 {
116     g_count_entities = 0;
117     traverse(root, WriteTokensWalker(writer, ignorePatches));
118 }