2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
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.
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.
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
25 #include "iscriplib.h"
28 inline MapExporter* Node_getMapExporter( scene::Node& node ){
29 return NodeTypeCast<MapExporter>::cast( node );
33 static std::size_t g_count_entities;
34 static std::size_t g_count_brushes;
37 void Entity_ExportTokens( const Entity& entity, TokenWriter& writer ){
40 class WriteKeyValue : public Entity::Visitor
42 TokenWriter& m_writer;
44 WriteKeyValue( TokenWriter& writer )
48 void visit( const char* key, const char* value ){
49 m_writer.writeString( key );
50 m_writer.writeString( value );
56 entity.forEachKeyValue( visitor );
59 class WriteTokensWalker : public scene::Traversable::Walker
61 mutable Stack<bool> m_stack;
62 TokenWriter& m_writer;
66 WriteTokensWalker( TokenWriter& writer, bool ignorePatches, bool writeComments )
67 : m_writer( writer ), m_ignorePatches( ignorePatches ), m_writeComments( writeComments ){
70 bool pre( scene::Node &node ) const {
71 m_stack.push( false );
73 Entity* entity = Node_getEntity( node );
75 if ( m_writeComments ) {
76 m_writer.writeToken( "//" );
77 m_writer.writeToken( "entity" );
78 m_writer.writeUnsigned( g_count_entities++ );
82 m_writer.writeToken( "{" );
86 Entity_ExportTokens( *entity, m_writer );
90 MapExporter* exporter = Node_getMapExporter( node );
92 && !( m_ignorePatches && Node_isPatch( node ) ) ) {
93 if ( m_writeComments ) {
94 m_writer.writeToken( "//" );
95 m_writer.writeToken( "brush" );
96 m_writer.writeUnsigned( g_count_brushes++ );
100 exporter->exportTokens( m_writer );
107 void post( scene::Node& node ) const {
108 if ( m_stack.top() ) {
109 m_writer.writeToken( "}" );
116 void Map_Write( scene::Node& root, GraphTraversalFunc traverse, TokenWriter& writer, bool ignorePatches, bool writeComments ){
117 g_count_entities = 0;
118 traverse( root, WriteTokensWalker( writer, ignorePatches, writeComments ) );