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