]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/model/plugin.cpp
eol
[xonotic/netradiant.git] / plugins / model / plugin.cpp
1
2 /*
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6 This file is part of GtkRadiant.
7
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 */
22
23 #include "plugin.h"
24
25 #if 0 // stop using windowing systems in plugins - put the text in SynapseClient::GetInfo
26 // =============================================================================
27 // Utility functions
28 static void dialog_button_callback (GtkWidget *widget, gpointer data)
29 {
30         GtkWidget *parent;
31         int *loop, *ret;
32  
33         parent = gtk_widget_get_toplevel (widget);
34         loop = (int*)g_object_get_data (G_OBJECT (parent), "loop");
35         ret = (int*)g_object_get_data (G_OBJECT (parent), "ret");
36  
37         *loop = 0;
38         *ret = (int)data;
39 }
40
41 static gint dialog_delete_callback (GtkWidget *widget, GdkEvent* event, gpointer data)
42 {
43         int *loop;
44  
45         gtk_widget_hide (widget);
46         loop = (int*)g_object_get_data (G_OBJECT (widget), "loop");
47         *loop = 0;
48
49         return TRUE;
50 }
51
52 int DoAboutBox( GtkWidget *parent )
53 {
54         GtkWidget *window, *w, *text, *vbox, *hbox, *hbox2, *frame;
55         GdkPixmap *pixmap;
56         GdkBitmap *mask;
57         GtkStyle *style;
58         int ret, loop = 1;
59         char buf[2048];
60   const picoModule_t **modules, *pm;
61  
62         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
63         gtk_signal_connect (GTK_OBJECT (window), "delete_event",
64                       GTK_SIGNAL_FUNC (dialog_delete_callback), NULL);
65         gtk_signal_connect (GTK_OBJECT (window), "destroy",
66                       GTK_SIGNAL_FUNC (gtk_widget_destroy), NULL);
67         gtk_window_set_title (GTK_WINDOW (window), "About...");
68         gtk_container_border_width (GTK_CONTAINER (window), 10);
69         g_object_set_data (G_OBJECT (window), "loop", &loop);
70         g_object_set_data (G_OBJECT (window), "ret", &ret);
71         gtk_widget_realize (window);
72
73         if (parent != NULL)
74                 gtk_window_set_transient_for (GTK_WINDOW (window), GTK_WINDOW (parent));
75
76   vbox = gtk_vbox_new (FALSE, 10);
77   gtk_container_add (GTK_CONTAINER (window), vbox);
78         gtk_widget_show (vbox);
79
80         style = gtk_widget_get_style(window);
81
82   hbox2 = gtk_hbox_new (FALSE, 10);
83   gtk_box_pack_start (GTK_BOX (vbox), hbox2, FALSE, FALSE, 2);
84         gtk_widget_show (hbox2);
85
86   frame = gtk_frame_new (NULL);
87   gtk_box_pack_start (GTK_BOX (hbox2), frame, FALSE, FALSE, 2);
88   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
89   gtk_widget_show (frame);
90
91         if( g_FuncTable.m_pfnLoadBitmap( "picomodel.bmp", (void **)&pixmap, (void **)&mask ) ) {
92                 w = gtk_pixmap_new (pixmap, mask);
93     gtk_container_add (GTK_CONTAINER (frame), w);
94                 gtk_widget_show (w);
95         }
96
97         w = gtk_label_new ("Model Module v1.0 for GtkRadiant\nby Arnout van Meer (rr2do2@splashdamage.com)\n\nBased on the MD3Model Module by SPoG\nPicoModel Library Copyright (c) 2002, Randy Reddig & seaw0lf" );
98         gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 2);
99         gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_LEFT);
100         gtk_widget_show (w);
101
102   w = gtk_scrolled_window_new(NULL, NULL);
103   gtk_box_pack_start(GTK_BOX(vbox), w, TRUE, TRUE, 2);
104   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
105   gtk_widget_show(w);
106
107   text = gtk_text_new(NULL, NULL);
108   gtk_text_set_editable(GTK_TEXT(text), FALSE);
109   gtk_container_add(GTK_CONTAINER(w), text);
110
111   strcpy( buf, "#Supported Model Formats:\n" );
112   gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL, buf, -1);
113
114   for( modules = PicoModuleList( NULL ); *modules != NULL; modules++ )
115   {
116     pm = *modules;
117
118     if( pm == NULL)
119                         break;
120
121     sprintf( buf, "\n%s, version %s, (c) %s", pm->displayName, pm->version, pm->copyright );
122     gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL, buf, -1);
123   }
124
125   gtk_text_set_word_wrap(GTK_TEXT(text), FALSE);
126   gtk_widget_show(text);
127
128   gtk_text_set_point(GTK_TEXT(text), 0);
129   gtk_text_forward_delete(GTK_TEXT(text), 1);
130
131         w = gtk_hseparator_new ();
132         gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 2);
133         gtk_widget_show (w);
134  
135         hbox = gtk_hbox_new (FALSE, 10);
136         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 2);
137         gtk_widget_show (hbox);
138  
139         w = gtk_button_new_with_label ("Ok");
140         gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
141         gtk_signal_connect (GTK_OBJECT (w), "clicked",
142                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDOK));
143         GTK_WIDGET_SET_FLAGS (w, GTK_CAN_DEFAULT);
144         gtk_widget_grab_default (w);
145         gtk_widget_show (w);
146         ret = IDOK;
147  
148         gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
149         gtk_widget_show (window);
150         gtk_grab_add (window);
151  
152         while (loop)
153                 gtk_main_iteration ();
154  
155         gtk_grab_remove (window);
156         gtk_widget_destroy (window);
157  
158         return ret;
159 }
160 #endif
161
162 // toolbar implementation
163
164 class CFlushReloadSelectedToolbarButton : public IToolbarButton
165 {
166 public:
167   virtual const char* getImage() const
168   {
169     return "model_reload_entity.bmp";
170   }
171   virtual const char* getText() const
172   {
173     return "Reload";
174   }
175   virtual const char* getTooltip() const
176   {
177     return "Flush & Reload Selected Model";
178   }
179   virtual void activate() const
180   {
181     DoFlushReloadSelected();
182   }
183   virtual EType getType() const
184   {
185     return eButton;
186   }
187 };
188
189 CFlushReloadSelectedToolbarButton g_flushreloadselected;
190
191 unsigned int ToolbarButtonCount()
192 {
193   return 1;
194 }
195
196 const IToolbarButton* GetToolbarButton(unsigned int index)
197 {
198   return &g_flushreloadselected;
199 }
200
201 // =============================================================================
202 // Pico utility functions
203
204 #include "picomodel.h"
205
206 void PicoPrintFunc( int level, const char *str )
207 {
208         if( str == NULL )
209                 return;
210         switch( level )
211         {
212                 case PICO_NORMAL:
213                         Sys_Printf( "%s\n", str );
214                         break;
215                 
216                 case PICO_VERBOSE:
217                         Sys_FPrintf( SYS_VRB, "%s\n", str );
218                         break;
219                 
220                 case PICO_WARNING:
221                         Sys_Printf( "WARNING: %s\n", str );
222                         break;
223                 
224                 case PICO_ERROR:
225                         Sys_FPrintf( SYS_VRB, "ERROR: %s\n", str );
226                         break;
227                 
228                 case PICO_FATAL:
229       Sys_Printf( "ERROR: %s\n", str );
230                         break;
231         }
232 }
233
234 void PicoLoadFileFunc( char *name, byte **buffer, int *bufSize )
235 {
236         *bufSize = vfsLoadFile( (const char*) name, (void**) buffer, 0 );
237 }
238
239 void PicoFreeFileFunc( void* file )
240 {
241         vfsFreeFile(file);
242 }
243
244 static void initialise()
245 {
246         PicoInit();
247         PicoSetMallocFunc( malloc );
248         PicoSetFreeFunc( free );
249         PicoSetPrintFunc( PicoPrintFunc );
250         PicoSetLoadFileFunc( PicoLoadFileFunc );
251         PicoSetFreeFileFunc( PicoFreeFileFunc );
252 }
253
254 static void add_model_apis(CSynapseClient& client)
255 {
256   const picoModule_t** modules = PicoModuleList( NULL );
257   while(*modules != NULL)
258   {
259     const picoModule_t* module = *modules++;
260     if(module->canload && module->load)
261       for(unsigned int j = 0; module->defaultExts[j] != NULL; j++)
262         client.AddAPI(MODEL_MAJOR, module->defaultExts[j], sizeof(_QERPlugModelTable));
263   }   
264 }
265
266 static bool model_is_supported(const char* extension)
267 {
268   const picoModule_t** modules = PicoModuleList( NULL );
269   while(*modules != NULL)
270   {
271     const picoModule_t* module = *modules++;
272     if(module->canload && module->load)
273       for(unsigned int j = 0; module->defaultExts[j] != NULL; j++)
274         if(strcmp(extension, module->defaultExts[j]) == 0)
275           return true;
276   }
277   return false;
278 }
279
280 void init_filetypes()
281 {
282   const picoModule_t **modules = PicoModuleList(NULL);
283   while(*modules != NULL)
284   {
285     const picoModule_t* module = *modules++;
286     if(module->canload && module->load)
287     {
288       for(char*const* ext = module->defaultExts; *ext != NULL; ++ext)
289       {
290         char buf[16];
291         buf[0] = '*';
292         buf[1] = '.';
293         strcpy(buf+2, *ext);
294         GetFileTypeRegistry()->addType(MODEL_MAJOR, filetype_t(module->displayName, buf));
295       }
296     }
297   }
298 }
299
300 // plugin implementation
301
302 static const char *PLUGIN_NAME = "Model loading module";
303 static const char *PLUGIN_COMMANDS = "Flush & Reload Models,Flush & Reload Selected";
304 static const char *PLUGIN_ABOUT = "Model loading module";
305
306 extern "C" const char* QERPlug_Init (void *hApp, void* pMainWidget)
307 {
308   init_filetypes();
309   return (char *) PLUGIN_NAME;
310 }
311
312 extern "C" const char* QERPlug_GetName ()
313 {
314   return (char *) PLUGIN_NAME;
315 }
316
317 extern "C" const char* QERPlug_GetCommandList ()
318 {
319   return (char *) PLUGIN_COMMANDS;
320 }
321
322 extern "C" void QERPlug_Dispatch (const char *p, vec3_t vMin, vec3_t vMax, bool bSingleBrush)
323 {
324   if( !strcmp( p, "Flush & Reload Selected" ) )
325     DoFlushReloadSelected();
326   else if( !strcmp( p, "Flush & Reload Models" ) )
327     DoFlushReloadAll();
328 }
329
330
331 void DoFlushReloadSelected() {
332 }
333
334 void DoFlushReloadAll() {
335   GetModelCache()->RefreshAll();
336 }
337
338 // =============================================================================
339
340 // function tables
341 _QERFuncTable_1 g_FuncTable;
342 _QERQglTable g_QglTable;
343 _QERShadersTable g_ShadersTable;
344 _QERFileSystemTable g_FileSystemTable;
345
346 // =============================================================================
347 // SYNAPSE
348
349 CSynapseServer* g_pSynapseServer = NULL;
350 CSynapseClientModel g_SynapseClient;
351
352 static const XMLConfigEntry_t entries[] = 
353   { { SHADERS_MAJOR, SYN_REQUIRE, sizeof(g_ShadersTable), &g_ShadersTable }, 
354     { VFS_MAJOR, SYN_REQUIRE, sizeof(g_FileSystemTable), &g_FileSystemTable },
355     { NULL, SYN_UNKNOWN, 0, NULL } };
356
357 #if __GNUC__ >= 4
358 #pragma GCC visibility push(default)
359 #endif
360 extern "C" CSynapseClient* SYNAPSE_DLL_EXPORT Synapse_EnumerateInterfaces( const char *version, CSynapseServer *pServer ) {
361 #if __GNUC__ >= 4
362 #pragma GCC visibility pop
363 #endif
364   if (strcmp(version, SYNAPSE_VERSION))
365   {
366     Syn_Printf("ERROR: synapse API version mismatch: should be '" SYNAPSE_VERSION "', got '%s'\n", version);
367     return NULL;
368   }
369   g_pSynapseServer = pServer;
370   g_pSynapseServer->IncRef();
371   Set_Syn_Printf( g_pSynapseServer->Get_Syn_Printf() );
372
373   initialise();
374   
375   add_model_apis(g_SynapseClient);
376   g_SynapseClient.AddAPI(TOOLBAR_MAJOR, "model", sizeof(_QERPlugToolbarTable));
377   g_SynapseClient.AddAPI(PLUGIN_MAJOR, "model", sizeof(_QERPluginTable));
378
379   g_SynapseClient.AddAPI(RADIANT_MAJOR, NULL, sizeof(g_FuncTable), SYN_REQUIRE, &g_FuncTable);
380   g_SynapseClient.AddAPI(QGL_MAJOR, NULL, sizeof(g_QglTable), SYN_REQUIRE, &g_QglTable);
381
382   if ( !g_SynapseClient.ConfigXML( pServer, NULL, entries ) ) {
383     return NULL;
384   }
385   
386   return &g_SynapseClient;
387 }
388
389 bool CSynapseClientModel::RequestAPI(APIDescriptor_t *pAPI)
390 {
391   if (!strcmp(pAPI->major_name, MODEL_MAJOR))
392   {
393     _QERPlugModelTable* pTable= static_cast<_QERPlugModelTable*>(pAPI->mpTable);
394
395     if (model_is_supported(pAPI->minor_name))
396     {
397       pTable->m_pfnLoadModel = &LoadModel;
398       return true;
399     }
400   }
401   else if (!strcmp(pAPI->major_name, TOOLBAR_MAJOR))
402   {
403     _QERPlugToolbarTable* pTable= static_cast<_QERPlugToolbarTable*>(pAPI->mpTable);
404
405     pTable->m_pfnToolbarButtonCount = &ToolbarButtonCount;
406     pTable->m_pfnGetToolbarButton = &GetToolbarButton;
407     return true;
408   }
409   else if (!strcmp(pAPI->major_name, PLUGIN_MAJOR))
410   {
411     _QERPluginTable* pTable= static_cast<_QERPluginTable*>(pAPI->mpTable);
412
413     pTable->m_pfnQERPlug_Init = QERPlug_Init;
414     pTable->m_pfnQERPlug_GetName = QERPlug_GetName;
415     pTable->m_pfnQERPlug_GetCommandList = QERPlug_GetCommandList;
416     pTable->m_pfnQERPlug_Dispatch = QERPlug_Dispatch;
417     return true;
418   }
419
420   Syn_Printf("ERROR: RequestAPI( '%s' ) not found in '%s'\n", pAPI->major_name, GetInfo());
421   return false;
422 }
423
424 #include "version.h"
425
426 const char* CSynapseClientModel::GetInfo()
427 {
428   return "picomodel loader module built " __DATE__ " " RADIANT_VERSION;
429 }
430
431 const char* CSynapseClientModel::GetName()
432 {
433   return "model";
434 }