]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/brushexport/plugin.cpp
8954473dc3de68ea4b5f283d16c2e2fd8c82c982
[xonotic/netradiant.git] / contrib / brushexport / plugin.cpp
1 /*\r
2 Copyright (C) 2006, Thomas Nitschke.\r
3 All Rights Reserved.\r
4 \r
5 This file is part of GtkRadiant.\r
6 \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
11 \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
16 \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
20 */\r
21 #include "plugin.h"\r
22 \r
23 #include "iplugin.h"\r
24 #include "qerplugin.h"\r
25 \r
26 #include <gtk/gtk.h>\r
27 \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
35 \r
36 #include "ibrush.h"\r
37 #include "iscenegraph.h"\r
38 #include "iselection.h"\r
39 #include "ifilesystem.h"\r
40 #include "ifiletypes.h"\r
41 \r
42 #include "../../radiant/brush.h"\r
43 \r
44 namespace BrushExport\r
45\r
46   GtkWindow* g_mainwnd;\r
47   \r
48   class CExportFormatWavefront : public BrushVisitor\r
49   {\r
50     TextFileOutputStream& m_file;\r
51 \r
52     StringOutputStream vertexbuffer;\r
53     StringOutputStream texcoordbuffer;\r
54     StringOutputStream facebuffer;\r
55     \r
56     size_t vertices;\r
57     size_t exported;\r
58     \r
59   public:\r
60     \r
61     CExportFormatWavefront(TextFileOutputStream& file)\r
62         : m_file(file)\r
63     {\r
64       exported = 0;\r
65       vertices = 0;\r
66     }\r
67     \r
68     virtual ~CExportFormatWavefront(void) {}\r
69     \r
70     void visit(scene::Instance& instance)\r
71     {\r
72       BrushInstance* bptr = InstanceTypeCast<BrushInstance>::cast(instance);\r
73       if(bptr)\r
74       {\r
75         Brush& brush(bptr->getBrush());\r
76         \r
77         m_file << "\ng " << brush.name() << exported << "\n";\r
78         \r
79         brush.forEachFace(*this);\r
80     \r
81         m_file << vertexbuffer.c_str() << "\n";\r
82         m_file << texcoordbuffer.c_str();\r
83         m_file << facebuffer.c_str() << "\n";\r
84         \r
85         vertexbuffer.clear();\r
86         texcoordbuffer.clear();\r
87         facebuffer.clear();\r
88         ++exported;\r
89       }\r
90     }\r
91    \r
92     void visit(Face& face) const\r
93     {\r
94       // cast the stupid const away\r
95       const_cast<CExportFormatWavefront*>(this)->visit(face);\r
96     }\r
97     \r
98     void visit(Face& face)\r
99     {\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
103       {\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
106         ++vertices;\r
107       }\r
108       \r
109       facebuffer << "\nf";\r
110       for(size_t i = v_start; i < vertices; ++i)\r
111         facebuffer << " " << i+1 << "/" << i+1;\r
112     }\r
113   };\r
114   \r
115   /**\r
116     Exporterclass which will pass every visit-call\r
117     to a special formatexporter.\r
118   */\r
119   template<class TExporterFormat>\r
120   class CExporter : public SelectionSystem::Visitor\r
121   {\r
122   public:\r
123     CExporter(TextFileOutputStream& file)\r
124       : m_exporter(file)\r
125     {}\r
126     \r
127     virtual ~CExporter(void) {}\r
128     \r
129     void visit(scene::Instance& instance) const\r
130     {\r
131       m_exporter.visit(instance);\r
132     }\r
133     \r
134   private:\r
135     mutable TExporterFormat m_exporter;\r
136   };\r
137   \r
138   template<class T>\r
139   void export_selected(TextFileOutputStream& file)\r
140   {\r
141     CExporter<T> exporter(file);\r
142     GlobalSelectionSystem().foreachSelected(exporter);\r
143   }\r
144 \r
145   const char* init(void* hApp, void* pMainWidget)\r
146   {\r
147     g_mainwnd = (GtkWindow*)pMainWidget;\r
148     ASSERT_NOTNULL(g_mainwnd);\r
149     return "";\r
150   }\r
151   const char* getName()\r
152   {\r
153     return "Brush export Plugin";\r
154   }\r
155   const char* getCommandList()\r
156   {\r
157     return "Export selected as Wavefront Object;About";\r
158   }\r
159   const char* getCommandTitleList()\r
160   {\r
161     return "";\r
162   }\r
163   \r
164   void dispatch(const char* command, float* vMin, float* vMax, bool bSingleBrush)\r
165   {\r
166     if(string_equal(command, "About"))\r
167     {\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
170                                         eMB_OK,\r
171                                         eMB_ICONDEFAULT);\r
172     }\r
173     else if(string_equal(command, "Export selected as Wavefront Object"))\r
174     {\r
175       if(const char* path = GlobalRadiant().m_pfnFileDialog(GTK_WIDGET(g_mainwnd), false, "Save as Obj", 0, 0))\r
176       {\r
177         TextFileOutputStream file(path); \r
178         if(file.failed())\r
179         {\r
180           GlobalRadiant().m_pfnMessageBox(GTK_WIDGET(g_mainwnd), "Unable to write to file", "Error",\r
181                                         eMB_OK,\r
182                                         eMB_ICONERROR);\r
183         }\r
184         else\r
185         {\r
186           export_selected<CExportFormatWavefront>(file);\r
187         }\r
188       }\r
189     }\r
190   }\r
191   \r
192 } // namespace\r
193 \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
201 {\r
202 public:\r
203   BrushExportDependencies(void)\r
204     : GlobalBrushModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("brushtypes"))\r
205   {}\r
206 };\r
207 \r
208 class BrushExportModule : public TypeSystemRef\r
209 {\r
210   _QERPluginTable m_plugin;\r
211 public:\r
212   typedef _QERPluginTable Type;\r
213   STRING_CONSTANT(Name, "brushexport");\r
214 \r
215   BrushExportModule()\r
216   {\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
222   }\r
223   _QERPluginTable* getTable()\r
224   {\r
225     return &m_plugin;\r
226   }\r
227 };\r
228 \r
229 typedef SingletonModule<BrushExportModule, BrushExportDependencies> SingletonBrushExportModule;\r
230 SingletonBrushExportModule g_BrushExportModule;\r
231 \r
232 extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules(ModuleServer& server)\r
233 {\r
234   initialiseModule(server);\r
235   g_BrushExportModule.selfRegister();\r
236 }\r