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