]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bkgrnd2d/plugin.cpp
propagate from internal tree
[xonotic/netradiant.git] / contrib / bkgrnd2d / plugin.cpp
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
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 //
23 // 2d background Plugin
24 //
25 // Code by reyalP aka Reed Mideke
26 //
27 // Based on 
28 //
29
30 /*
31     Overview
32     ========
33         This little plugin allows you to display an image in the background of the
34         gtkradiant XY window.
35
36     Version History
37     ===============
38
39     v0.1
40       - Initial version.
41         v0.2
42           - three views, dialog box, toolbar
43     v0.25
44       - tooltips, follow gtkradiant coding conventions
45
46     Why ?
47     -----
48       http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=88
49
50
51     How ?
52     -----
53      - textures 'n widgets 'n stuff.
54 */
55
56 //#include "plugin.h"
57 //TODO we just poke the objects directly
58 #include "bkgrnd2d.h"
59 #include "dialog.h"
60
61 #define CMD_SEP "-" 
62 #define CMD_CONFIG "Configure..."
63 #define CMD_ABOUT "About..."
64 // =============================================================================
65 // Globals
66
67 // function tables
68 _QERFuncTable_1 g_FuncTable;
69 _QERQglTable g_QglTable;
70 _QERFileSystemTable g_FileSystemTable;
71 _QEREntityTable g_EntityTable;
72 _QERAppDataTable g_DataTable;
73
74 // for the file load dialog
75 void *g_pMainWidget;
76
77 // =============================================================================
78 // plugin implementation
79
80 static const char *PLUGIN_NAME = "2d window background plugin";
81
82 //backwards for some reason
83 static const char *PLUGIN_COMMANDS = CMD_ABOUT ";" 
84                                      CMD_SEP ";"
85                                                                                          CMD_CONFIG
86                                      ;
87
88 static const char *PLUGIN_ABOUT = "2d window background v0.25\n\n"
89                                   "By reyalP (hellsownpuppy@yahoo.com)";
90
91
92
93
94 void DoBkgrndToggleXY();
95 void DoBkgrndToggleXZ();
96 void DoBkgrndToggleYZ();
97
98 #define NUM_TOOLBAR_BUTTONS 4
99 struct toolbar_button_info_s
100 {
101         char *image;
102         char *text;
103         char *tip;
104         void (*func)();
105         IToolbarButton::EType type;
106 };
107
108 struct toolbar_button_info_s toolbar_buttons[NUM_TOOLBAR_BUTTONS] = 
109 {
110         {
111                 "bkgrnd2d_xy_toggle.bmp",
112                 "xy background",
113     "Toggle xy background image",
114                 DoBkgrndToggleXY,
115                 IToolbarButton::eToggleButton
116         },
117         {
118                 "bkgrnd2d_xz_toggle.bmp",
119                 "xz background",
120     "Toggle xz background image",
121                 DoBkgrndToggleXZ,
122                 IToolbarButton::eToggleButton
123         },
124         {
125                 "bkgrnd2d_yz_toggle.bmp",
126                 "yz background",
127     "Toggle yz background image",
128                 DoBkgrndToggleYZ,
129                 IToolbarButton::eToggleButton
130         },
131         {
132                 "bkgrnd2d_conf.bmp",
133                 "Configure",
134     "Configure background images",
135                 ShowBackgroundDialog,
136                 IToolbarButton::eButton
137         },
138 };
139
140 class Bkgrnd2dButton : public IToolbarButton
141 {
142 public:
143   const toolbar_button_info_s *bi;
144   virtual const char* getImage() const
145   {
146     return bi->image;
147   }
148   virtual const char* getText() const
149   {
150     return bi->text;
151   }
152   virtual const char* getTooltip() const
153   {
154     return bi->tip;
155   }
156   virtual void activate() const
157   {
158     bi->func();
159     return ;
160   }
161   virtual EType getType() const
162   {
163     return bi->type;
164   }
165 };
166
167 Bkgrnd2dButton g_bkgrnd2dbuttons[NUM_TOOLBAR_BUTTONS];
168
169 unsigned int ToolbarButtonCount()
170 {
171   return NUM_TOOLBAR_BUTTONS;
172 }
173
174 const IToolbarButton* GetToolbarButton(unsigned int index)
175 {
176   g_bkgrnd2dbuttons[index].bi = &toolbar_buttons[index];
177   return &g_bkgrnd2dbuttons[index];
178 }
179
180 extern "C" const char* QERPlug_Init (void *hApp, void* pMainWidget)
181 {
182   g_pMainWidget = pMainWidget;
183
184   InitBackgroundDialog();
185   render.Register();
186
187 //TODO is it right ? is it wrong ? it works
188 //TODO figure out supported image types
189   GetFileTypeRegistry()->addType(FILETYPE_KEY, filetype_t("all files", "*.*"));
190   GetFileTypeRegistry()->addType(FILETYPE_KEY, filetype_t("jpeg files", "*.jpg"));
191   GetFileTypeRegistry()->addType(FILETYPE_KEY, filetype_t("targa files", "*.tga"));
192   return (char *) PLUGIN_NAME;
193 }
194
195 extern "C" const char* QERPlug_GetName ()
196 {
197   return (char *) PLUGIN_NAME;
198 }
199
200 extern "C" const char* QERPlug_GetCommandList ()
201 {
202   return (char *) PLUGIN_COMMANDS;
203 }
204
205 extern "C" void QERPlug_Dispatch (const char *p, vec3_t vMin, vec3_t vMax, bool bSingleBrush)
206 {
207   Sys_Printf (MSG_PREFIX "Command \"%s\"\n",p); 
208   if(!strcmp(p, CMD_ABOUT)) {
209         g_FuncTable.m_pfnMessageBox(NULL, PLUGIN_ABOUT, "About", MB_OK, NULL);
210   } 
211   else if(!strcmp(p,CMD_CONFIG)) {
212         ShowBackgroundDialog();
213   }
214 }
215
216 //TODO these three suck
217 void DoBkgrndToggleXY()
218 {
219   Sys_Printf (MSG_PREFIX "DoBkgrndToggleXY\n"); 
220   // always toggle, since the buttons do
221   backgroundXY.m_bActive = (backgroundXY.m_bActive) ? false:true;
222   // if we don't have image or extents, and we activated,
223   // bring up the dialog with the corresponding page
224   // would be better to hide or grey out button, but we can't
225   if(backgroundXY.m_bActive && !backgroundXY.Valid())
226           ShowBackgroundDialogPG(0);
227   else
228           g_FuncTable.m_pfnSysUpdateWindows(W_XY);
229 }
230
231 void DoBkgrndToggleXZ()
232 {
233   Sys_Printf (MSG_PREFIX "DoBkgrndToggleXZ\n"); 
234   backgroundXZ.m_bActive = (backgroundXZ.m_bActive) ? false:true;
235   if(backgroundXZ.m_bActive && !backgroundXZ.Valid())
236           ShowBackgroundDialogPG(1);
237   else
238           g_FuncTable.m_pfnSysUpdateWindows(W_XY);
239 }
240
241 void DoBkgrndToggleYZ()
242 {
243   Sys_Printf (MSG_PREFIX "DoBkgrndToggleYZ\n"); 
244   backgroundYZ.m_bActive = (backgroundYZ.m_bActive) ? false:true;
245   if(backgroundYZ.m_bActive && !backgroundYZ.Valid())
246           ShowBackgroundDialogPG(2);
247   else
248           g_FuncTable.m_pfnSysUpdateWindows(W_XY);
249 }
250
251 // =============================================================================
252 // SYNAPSE
253
254 CSynapseServer* g_pSynapseServer = NULL;
255 CSynapseClientBkgrnd2d g_SynapseClient;
256     
257 #if __GNUC__ >= 4
258 #pragma GCC visibility push(default)
259 #endif
260 extern "C" CSynapseClient* SYNAPSE_DLL_EXPORT Synapse_EnumerateInterfaces( const char *version, CSynapseServer *pServer ) {
261 #if __GNUC__ >= 4
262 #pragma GCC visibility pop
263 #endif
264   if (strcmp(version, SYNAPSE_VERSION))
265   {
266     Syn_Printf("ERROR: synapse API version mismatch: should be '" SYNAPSE_VERSION "', got '%s'\n", version);
267     return NULL;
268   }
269   g_pSynapseServer = pServer;
270   g_pSynapseServer->IncRef();
271   Set_Syn_Printf(g_pSynapseServer->Get_Syn_Printf());
272
273   g_SynapseClient.AddAPI(TOOLBAR_MAJOR, BKGRND2D_MINOR, sizeof(_QERPlugToolbarTable));
274   g_SynapseClient.AddAPI(PLUGIN_MAJOR, BKGRND2D_MINOR, sizeof( _QERPluginTable ) );
275
276   g_SynapseClient.AddAPI( RADIANT_MAJOR, NULL, sizeof( g_FuncTable ), SYN_REQUIRE, &g_FuncTable );
277   g_SynapseClient.AddAPI( QGL_MAJOR, NULL, sizeof( g_QglTable ), SYN_REQUIRE, &g_QglTable );
278 // TODO is this the right way to ask for 'whichever VFS we have loaded' ? Seems to work
279 // for misc filename functions
280   g_SynapseClient.AddAPI( VFS_MAJOR, "*", sizeof( g_FileSystemTable ), SYN_REQUIRE, &g_FileSystemTable );
281 // get worldspawn
282   g_SynapseClient.AddAPI( ENTITY_MAJOR, NULL, sizeof( g_EntityTable ), SYN_REQUIRE, &g_EntityTable );
283 // selected brushes
284   g_SynapseClient.AddAPI( DATA_MAJOR, NULL, sizeof( g_DataTable ), SYN_REQUIRE, &g_DataTable );
285
286   return &g_SynapseClient;
287 }
288
289 bool CSynapseClientBkgrnd2d::RequestAPI(APIDescriptor_t *pAPI)
290 {
291   if (!strcmp(pAPI->major_name, PLUGIN_MAJOR))
292   {
293     _QERPluginTable* pTable= static_cast<_QERPluginTable*>(pAPI->mpTable);
294
295     pTable->m_pfnQERPlug_Init = QERPlug_Init;
296     pTable->m_pfnQERPlug_GetName = QERPlug_GetName;
297     pTable->m_pfnQERPlug_GetCommandList = QERPlug_GetCommandList;
298     pTable->m_pfnQERPlug_Dispatch = QERPlug_Dispatch;
299     return true;
300   }
301   if (!strcmp(pAPI->major_name, TOOLBAR_MAJOR))
302   {
303     _QERPlugToolbarTable* pTable= static_cast<_QERPlugToolbarTable*>(pAPI->mpTable);
304
305     pTable->m_pfnToolbarButtonCount = &ToolbarButtonCount;
306     pTable->m_pfnGetToolbarButton = &GetToolbarButton;
307     return true;
308   }
309
310   Syn_Printf("ERROR: RequestAPI( '%s' ) not found in '%s'\n", pAPI->major_name, GetInfo());
311   return false;
312 }
313
314 #include "version.h"
315
316 const char* CSynapseClientBkgrnd2d::GetInfo()
317 {
318   return "2d Background plugin built " __DATE__ " " RADIANT_VERSION;
319 }
320
321 const char* CSynapseClientBkgrnd2d::GetName()
322 {
323   return "bkgrnd2d";
324 }
325