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