2 Copyright (C) 2006, Thomas Nitschke.
\r
5 This file is part of GtkRadiant.
\r
7 GtkRadiant is free software; you can redistribute it and/or modify
\r
8 it under the terms of the GNU General Public License as published by
\r
9 the Free Software Foundation; either version 2 of the License, or
\r
10 (at your option) any later version.
\r
12 GtkRadiant is distributed in the hope that it will be useful,
\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 GNU General Public License for more details.
\r
17 You should have received a copy of the GNU General Public License
\r
18 along with GtkRadiant; if not, write to the Free Software
\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\r
23 #include "iplugin.h"
\r
24 #include "qerplugin.h"
\r
26 #include <gtk/gtk.h>
\r
28 #include "debugging/debugging.h"
\r
29 #include "string/string.h"
\r
30 #include "modulesystem/singletonmodule.h"
\r
31 #include "stream/textfilestream.h"
\r
32 #include "stream/stringstream.h"
\r
33 #include "gtkutil/messagebox.h"
\r
34 #include "gtkutil/filechooser.h"
\r
37 #include "iscenegraph.h"
\r
38 #include "iselection.h"
\r
39 #include "ifilesystem.h"
\r
40 #include "ifiletypes.h"
\r
42 #include "../../radiant/brush.h"
\r
44 namespace BrushExport
\r
46 GtkWindow* g_mainwnd;
\r
48 class CExportFormatWavefront : public BrushVisitor
\r
50 TextFileOutputStream& m_file;
\r
52 StringOutputStream vertexbuffer;
\r
53 StringOutputStream texcoordbuffer;
\r
54 StringOutputStream facebuffer;
\r
61 CExportFormatWavefront(TextFileOutputStream& file)
\r
68 virtual ~CExportFormatWavefront(void) {}
\r
70 void visit(scene::Instance& instance)
\r
72 BrushInstance* bptr = InstanceTypeCast<BrushInstance>::cast(instance);
\r
75 Brush& brush(bptr->getBrush());
\r
77 m_file << "\ng " << brush.name() << exported << "\n";
\r
79 brush.forEachFace(*this);
\r
81 m_file << vertexbuffer.c_str() << "\n";
\r
82 m_file << texcoordbuffer.c_str();
\r
83 m_file << facebuffer.c_str() << "\n";
\r
85 vertexbuffer.clear();
\r
86 texcoordbuffer.clear();
\r
92 void visit(Face& face) const
\r
94 // cast the stupid const away
\r
95 const_cast<CExportFormatWavefront*>(this)->visit(face);
\r
98 void visit(Face& face)
\r
100 size_t v_start = vertices;
\r
101 const Winding& w(face.getWinding());
\r
102 for(size_t i = 0; i < w.numpoints; ++i)
\r
104 vertexbuffer << "v " << w[i].vertex.x() << " " << w[i].vertex.y() << " " << w[i].vertex.z() << "\n";
\r
105 texcoordbuffer << "vt " << w[i].texcoord.x() << " " << w[i].texcoord.y() << "\n";
\r
109 facebuffer << "\nf";
\r
110 for(size_t i = v_start; i < vertices; ++i)
\r
111 facebuffer << " " << i+1 << "/" << i+1;
\r
116 Exporterclass which will pass every visit-call
\r
117 to a special formatexporter.
\r
119 template<class TExporterFormat>
\r
120 class CExporter : public SelectionSystem::Visitor
\r
123 CExporter(TextFileOutputStream& file)
\r
127 virtual ~CExporter(void) {}
\r
129 void visit(scene::Instance& instance) const
\r
131 m_exporter.visit(instance);
\r
135 mutable TExporterFormat m_exporter;
\r
139 void export_selected(TextFileOutputStream& file)
\r
141 CExporter<T> exporter(file);
\r
142 GlobalSelectionSystem().foreachSelected(exporter);
\r
145 const char* init(void* hApp, void* pMainWidget)
\r
147 g_mainwnd = (GtkWindow*)pMainWidget;
\r
148 ASSERT_NOTNULL(g_mainwnd);
\r
151 const char* getName()
\r
153 return "Brush export Plugin";
\r
155 const char* getCommandList()
\r
157 return "Export selected as Wavefront Object;About";
\r
159 const char* getCommandTitleList()
\r
164 void dispatch(const char* command, float* vMin, float* vMax, bool bSingleBrush)
\r
166 if(string_equal(command, "About"))
\r
168 GlobalRadiant().m_pfnMessageBox(GTK_WIDGET(g_mainwnd), "Brushexport plugin v 1.0 by namespace (www.codecreator.net)\n"
\r
169 "Enjoy!\n\nSend feedback to spam@codecreator.net", "About me...",
\r
173 else if(string_equal(command, "Export selected as Wavefront Object"))
\r
175 if(const char* path = GlobalRadiant().m_pfnFileDialog(GTK_WIDGET(g_mainwnd), false, "Save as Obj", 0, 0))
\r
177 TextFileOutputStream file(path);
\r
180 GlobalRadiant().m_pfnMessageBox(GTK_WIDGET(g_mainwnd), "Unable to write to file", "Error",
\r
186 export_selected<CExportFormatWavefront>(file);
\r
194 class BrushExportDependencies :
\r
195 public GlobalRadiantModuleRef,
\r
196 public GlobalFiletypesModuleRef,
\r
197 public GlobalBrushModuleRef,
\r
198 public GlobalFileSystemModuleRef,
\r
199 public GlobalSceneGraphModuleRef,
\r
200 public GlobalSelectionModuleRef
\r
203 BrushExportDependencies(void)
\r
204 : GlobalBrushModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("brushtypes"))
\r
208 class BrushExportModule : public TypeSystemRef
\r
210 _QERPluginTable m_plugin;
\r
212 typedef _QERPluginTable Type;
\r
213 STRING_CONSTANT(Name, "brushexport");
\r
215 BrushExportModule()
\r
217 m_plugin.m_pfnQERPlug_Init = &BrushExport::init;
\r
218 m_plugin.m_pfnQERPlug_GetName = &BrushExport::getName;
\r
219 m_plugin.m_pfnQERPlug_GetCommandList = &BrushExport::getCommandList;
\r
220 m_plugin.m_pfnQERPlug_GetCommandTitleList = &BrushExport::getCommandTitleList;
\r
221 m_plugin.m_pfnQERPlug_Dispatch = &BrushExport::dispatch;
\r
223 _QERPluginTable* getTable()
\r
229 typedef SingletonModule<BrushExportModule, BrushExportDependencies> SingletonBrushExportModule;
\r
230 SingletonBrushExportModule g_BrushExportModule;
\r
232 extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules(ModuleServer& server)
\r
234 initialiseModule(server);
\r
235 g_BrushExportModule.selfRegister();
\r