b4816b35738f29d1ce6a3a5137a7dcbb9f192c0b
[xonotic/netradiant.git] / radiant / plugin.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 "plugin.h"
23
24 #include "debugging/debugging.h"
25
26 #include "qerplugin.h"
27 #include "ifilesystem.h"
28 #include "ishaders.h"
29 #include "ientity.h"
30 #include "ieclass.h"
31 #include "irender.h"
32 #include "iscenegraph.h"
33 #include "iselection.h"
34 #include "ifilter.h"
35 #include "iscriplib.h"
36 #include "igl.h"
37 #include "iundo.h"
38 #include "itextures.h"
39 #include "ireference.h"
40 #include "ifiletypes.h"
41 #include "preferencesystem.h"
42 #include "ibrush.h"
43 #include "ipatch.h"
44 #include "iimage.h"
45 #include "itoolbar.h"
46 #include "iplugin.h"
47 #include "imap.h"
48 #include "namespace.h"
49
50 #include "gtkutil/messagebox.h"
51 #include "gtkutil/filechooser.h"
52 #include "maplib.h"
53
54 #include "error.h"
55 #include "map.h"
56 #include "qe3.h"
57 #include "entityinspector.h"
58 #include "entitylist.h"
59 #include "points.h"
60 #include "gtkmisc.h"
61 #include "texwindow.h"
62 #include "mainframe.h"
63 #include "build.h"
64 #include "mru.h"
65 #include "multimon.h"
66 #include "surfacedialog.h"
67 #include "groupdialog.h"
68 #include "patchdialog.h"
69 #include "camwindow.h"
70 #include "watchbsp.h"
71 #include "xywindow.h"
72 #include "entity.h"
73 #include "select.h"
74 #include "preferences.h"
75 #include "autosave.h"
76 #include "plugintoolbar.h"
77 #include "findtexturedialog.h"
78 #include "nullmodel.h"
79 #include "grid.h"
80
81 #include "modulesystem/modulesmap.h"
82 #include "modulesystem/singletonmodule.h"
83
84 #include "generic/callback.h"
85
86 const char* GameDescription_getKeyValue(const char* key)
87 {
88   return g_pGameDescription->getKeyValue(key);
89 }
90
91 const char* GameDescription_getRequiredKeyValue(const char* key)
92 {
93   return g_pGameDescription->getRequiredKeyValue(key);
94 }
95
96 const char* getMapName()
97 {
98   return Map_Name(g_map);
99 }
100
101 scene::Node& getMapWorldEntity()
102 {
103   return Map_FindOrInsertWorldspawn(g_map);
104 }
105
106 VIEWTYPE XYWindow_getViewType()
107 {
108   return g_pParentWnd->GetXYWnd()->GetViewType();
109 }
110
111 Vector3 XYWindow_windowToWorld(const WindowVector& position)
112 {
113   Vector3 result(0, 0, 0);
114   g_pParentWnd->GetXYWnd()->XY_ToPoint(static_cast<int>(position.x()), static_cast<int>(position.y()), result);
115   return result;
116 }
117
118 const char* TextureBrowser_getSelectedShader()
119 {
120   return TextureBrowser_GetSelectedShader(GlobalTextureBrowser());
121 }
122
123 class RadiantCoreAPI
124 {
125   _QERFuncTable_1 m_radiantcore;
126 public:
127   typedef _QERFuncTable_1 Type;
128   STRING_CONSTANT(Name, "*");
129
130   RadiantCoreAPI()
131   {
132     m_radiantcore.getEnginePath = &EnginePath_get;
133     m_radiantcore.getAppPath = &AppPath_get;
134     m_radiantcore.getGameToolsPath = &GameToolsPath_get;
135     m_radiantcore.getSettingsPath = &SettingsPath_get;
136     m_radiantcore.getMapsPath = &getMapsPath;
137
138     m_radiantcore.getGameName = &gamename_get;
139     m_radiantcore.getGameMode = &gamemode_get;
140
141     m_radiantcore.getMapName = &getMapName;
142     m_radiantcore.getMapWorldEntity = getMapWorldEntity;
143     m_radiantcore.getGridSize = GetGridSize;
144
145     m_radiantcore.getGameDescriptionKeyValue = &GameDescription_getKeyValue;
146     m_radiantcore.getRequiredGameDescriptionKeyValue = &GameDescription_getRequiredKeyValue;
147
148     m_radiantcore.attachGameToolsPathObserver = Radiant_attachGameToolsPathObserver;
149     m_radiantcore.detachGameToolsPathObserver = Radiant_detachGameToolsPathObserver;
150     m_radiantcore.attachEnginePathObserver = Radiant_attachEnginePathObserver;
151     m_radiantcore.detachEnginePathObserver = Radiant_detachEnginePathObserver;
152     m_radiantcore.attachGameNameObserver = Radiant_attachGameNameObserver;
153     m_radiantcore.detachGameNameObserver = Radiant_detachGameNameObserver;
154     m_radiantcore.attachGameModeObserver = Radiant_attachGameModeObserver;
155     m_radiantcore.detachGameModeObserver = Radiant_detachGameModeObserver;
156
157     m_radiantcore.XYWindowDestroyed_connect = XYWindowDestroyed_connect;
158     m_radiantcore.XYWindowDestroyed_disconnect = XYWindowDestroyed_disconnect;
159     m_radiantcore.XYWindowMouseDown_connect = XYWindowMouseDown_connect;
160     m_radiantcore.XYWindowMouseDown_disconnect = XYWindowMouseDown_disconnect;
161     m_radiantcore.XYWindow_getViewType = XYWindow_getViewType;
162     m_radiantcore.XYWindow_windowToWorld = XYWindow_windowToWorld;
163     m_radiantcore.TextureBrowser_getSelectedShader = TextureBrowser_getSelectedShader;
164
165     m_radiantcore.m_pfnMessageBox = &gtk_MessageBox;
166     m_radiantcore.m_pfnFileDialog = &file_dialog;
167     m_radiantcore.m_pfnColorDialog = &color_dialog;
168     m_radiantcore.m_pfnDirDialog = &dir_dialog;
169     m_radiantcore.m_pfnNewImage = &new_plugin_image;
170   }
171   _QERFuncTable_1* getTable()
172   {
173     return &m_radiantcore;
174   }
175 };
176
177 typedef SingletonModule<RadiantCoreAPI> RadiantCoreModule;
178 typedef Static<RadiantCoreModule> StaticRadiantCoreModule;
179 StaticRegisterModule staticRegisterRadiantCore(StaticRadiantCoreModule::instance());
180
181
182 class RadiantDependencies :
183   public GlobalRadiantModuleRef,
184   public GlobalFileSystemModuleRef,
185   public GlobalEntityModuleRef,
186   public GlobalShadersModuleRef,
187   public GlobalBrushModuleRef,
188   public GlobalSceneGraphModuleRef,
189   public GlobalShaderCacheModuleRef,
190   public GlobalFiletypesModuleRef,
191   public GlobalSelectionModuleRef,
192   public GlobalReferenceModuleRef,
193   public GlobalOpenGLModuleRef,
194   public GlobalEntityClassManagerModuleRef,
195   public GlobalUndoModuleRef,
196   public GlobalScripLibModuleRef,
197   public GlobalNamespaceModuleRef
198 {
199   ImageModulesRef m_image_modules;
200   MapModulesRef m_map_modules;
201   ToolbarModulesRef m_toolbar_modules;
202   PluginModulesRef m_plugin_modules;
203
204 public:
205   RadiantDependencies() :
206     GlobalEntityModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("entities")),
207     GlobalShadersModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("shaders")),
208     GlobalBrushModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("brushtypes")),
209     GlobalEntityClassManagerModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("entityclass")),
210     m_image_modules(GlobalRadiant().getRequiredGameDescriptionKeyValue("texturetypes")),
211     m_map_modules(GlobalRadiant().getRequiredGameDescriptionKeyValue("maptypes")),
212     m_toolbar_modules("*"),
213     m_plugin_modules("*")
214   {
215   }
216
217   ImageModules& getImageModules()
218   {
219     return m_image_modules.get();
220   }
221   MapModules& getMapModules()
222   {
223     return m_map_modules.get();
224   }
225   ToolbarModules& getToolbarModules()
226   {
227     return m_toolbar_modules.get();
228   }
229   PluginModules& getPluginModules()
230   {
231     return m_plugin_modules.get();
232   }
233 };
234
235 class Radiant : public TypeSystemRef
236 {
237 public:
238   Radiant()
239   {
240     Preferences_Init();
241
242     GlobalFiletypes().addType("sound", "wav", filetype_t("PCM sound files", "*.wav"));
243
244     Selection_construct();
245     HomePaths_Construct();
246     VFS_Construct();
247     Grid_construct();
248     MultiMon_Construct();
249     MRU_Construct();
250     Pointfile_Construct();
251     GLWindow_Construct();
252     BuildMenu_Construct();
253     Map_Construct();
254     EntityList_Construct();
255     MainFrame_Construct();
256     GroupDialog_Construct();
257     SurfaceInspector_Construct();
258     PatchInspector_Construct();
259     CamWnd_Construct();
260     XYWindow_Construct();
261     BuildMonitor_Construct();
262     TextureBrowser_Construct();
263     Entity_Construct();
264     Autosave_Construct();
265     EntityInspector_construct();
266     FindTextureDialog_Construct();
267     NullModel_construct();
268     MapRoot_construct();
269
270     EnginePath_verify();
271     EnginePath_Realise();
272   }
273   ~Radiant()
274   {
275     EnginePath_Unrealise();
276
277     MapRoot_destroy();
278     NullModel_destroy();
279     FindTextureDialog_Destroy();
280     EntityInspector_destroy();
281     Autosave_Destroy();
282     Entity_Destroy();
283     TextureBrowser_Destroy();
284     BuildMonitor_Destroy();
285     XYWindow_Destroy();
286     CamWnd_Destroy();
287     PatchInspector_Destroy();
288     SurfaceInspector_Destroy();
289     GroupDialog_Destroy();
290     MainFrame_Destroy();
291     EntityList_Destroy();
292     Map_Destroy();
293     BuildMenu_Destroy();
294     GLWindow_Destroy();
295     Pointfile_Destroy();
296     MRU_Destroy();
297     MultiMon_Destroy();
298     Grid_destroy();
299     VFS_Destroy();
300     HomePaths_Destroy();
301     Selection_destroy();
302   }
303 };
304
305 namespace
306 {
307   bool g_RadiantInitialised = false;
308   RadiantDependencies* g_RadiantDependencies;
309   Radiant* g_Radiant;
310 }
311
312
313
314 bool Radiant_Construct(ModuleServer& server)
315 {
316   GlobalModuleServer::instance().set(server);
317   StaticModuleRegistryList().instance().registerModules();
318
319   g_RadiantDependencies = new RadiantDependencies();
320
321   g_RadiantInitialised = !server.getError();
322
323   if(g_RadiantInitialised)
324   {
325     g_Radiant = new Radiant;
326   }
327
328   return g_RadiantInitialised;
329 }
330 void Radiant_Destroy()
331 {
332   if(g_RadiantInitialised)
333   {
334     delete g_Radiant;
335   }
336
337   delete g_RadiantDependencies;
338 }
339
340 ImageModules& Radiant_getImageModules()
341 {
342   return g_RadiantDependencies->getImageModules();
343 }
344 MapModules& Radiant_getMapModules()
345 {
346   return g_RadiantDependencies->getMapModules();
347 }
348 ToolbarModules& Radiant_getToolbarModules()
349 {
350   return g_RadiantDependencies->getToolbarModules();
351 }
352 PluginModules& Radiant_getPluginModules()
353 {
354   return g_RadiantDependencies->getPluginModules();
355 }
356
357