* updated ufoai plugin
[xonotic/netradiant.git] / contrib / ufoai / 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 #include "plugin.h"
23 #include "ufoai_filters.h"
24
25 #define CMD_SEP "-"
26 #define CMD_ABOUT "About..."
27 // =============================================================================
28 // Globals
29
30 // function tables
31 _QERFuncTable_1 g_FuncTable;
32 _QERQglTable g_QglTable;
33 _QERFileSystemTable g_FileSystemTable;
34 _QEREntityTable g_EntityTable;
35 _QERAppDataTable g_DataTable;
36
37 // the gtk widget
38 void *g_pMainWidget;
39
40 // =============================================================================
41 // plugin implementation
42
43 #define PLUGIN_NAME "UFO:AI plugin"
44 #define PLUGIN_VERSION "0.1"
45
46 //backwards for some reason
47 static const char *PLUGIN_COMMANDS = CMD_ABOUT ";" CMD_SEP;
48 static const char *PLUGIN_ABOUT = "UFO: Alien Invasion plugin " PLUGIN_VERSION "\nby Martin Gerhardy";
49
50 #define NUM_TOOLBAR_BUTTONS FILTER_MAX
51 typedef struct toolbar_button_info_s
52 {
53         char *image;
54         char *text;
55         char *tip;
56         void (*func)();
57         IToolbarButton::EType type;
58 } toolbar_button_info_t;
59
60 static const toolbar_button_info_t toolbar_buttons[NUM_TOOLBAR_BUTTONS] =
61 {
62         {
63                 "ufoai_actorclip.bmp",
64                 "Filter actorclip",
65                 "Actorclip",
66                 DoActorClipFiltering,
67                 IToolbarButton::eToggleButton
68         },
69         {
70                 "ufoai_weaponclip.bmp",
71                 "Filter weaponclip",
72                 "Weaponclip",
73                 DoWeaponClipFiltering,
74                 IToolbarButton::eToggleButton
75         },
76         {
77                 "ufoai_nodraw.bmp",
78                 "Filter nodraw",
79                 "NoDraw",
80                 DoNoDrawFiltering,
81                 IToolbarButton::eToggleButton
82         },
83         {
84                 "ufoai_stepon.bmp",
85                 "Filter stepon",
86                 "Stepon",
87                 DoSteponFiltering,
88                 IToolbarButton::eToggleButton
89         },
90         {
91                 "ufoai_level1.bmp",
92                 "Filter level1",
93                 "Level 1",
94                 DoLevel1Filtering,
95                 IToolbarButton::eToggleButton
96         },
97         {
98                 "ufoai_level2.bmp",
99                 "Filter level2",
100                 "Level 2",
101                 DoLevel2Filtering,
102                 IToolbarButton::eToggleButton
103         },
104         {
105                 "ufoai_level3.bmp",
106                 "Filter level3",
107                 "Level 3",
108                 DoLevel3Filtering,
109                 IToolbarButton::eToggleButton
110         },
111         {
112                 "ufoai_level4.bmp",
113                 "Filter level4",
114                 "Level 4",
115                 DoLevel4Filtering,
116                 IToolbarButton::eToggleButton
117         },
118         {
119                 "ufoai_level5.bmp",
120                 "Filter level5",
121                 "Level 5",
122                 DoLevel5Filtering,
123                 IToolbarButton::eToggleButton
124         },
125         {
126                 "ufoai_level6.bmp",
127                 "Filter level6",
128                 "Level 6",
129                 DoLevel6Filtering,
130                 IToolbarButton::eToggleButton
131         },
132         {
133                 "ufoai_level7.bmp",
134                 "Filter level7",
135                 "Level 7",
136                 DoLevel7Filtering,
137                 IToolbarButton::eToggleButton
138         },
139         {
140                 "ufoai_level8.bmp",
141                 "Filter level8",
142                 "Level 8",
143                 DoLevel8Filtering,
144                 IToolbarButton::eToggleButton
145         },
146 };
147
148 class UFOAIButton : public IToolbarButton
149 {
150 public:
151         const toolbar_button_info_s *bi;
152         virtual const char* getImage() const
153         {
154                 return bi->image;
155         }
156         virtual const char* getText() const
157         {
158                 return bi->text;
159         }
160         virtual const char* getTooltip() const
161         {
162                 return bi->tip;
163         }
164         virtual void activate() const
165         {
166                 bi->func();
167                 return ;
168         }
169         virtual EType getType() const
170         {
171                 return bi->type;
172         }
173 };
174
175 UFOAIButton g_ufoaibuttons[NUM_TOOLBAR_BUTTONS];
176
177 unsigned int ToolbarButtonCount (void)
178 {
179         return NUM_TOOLBAR_BUTTONS;
180 }
181
182 const IToolbarButton* GetToolbarButton (unsigned int index)
183 {
184         g_ufoaibuttons[index].bi = &toolbar_buttons[index];
185         return &g_ufoaibuttons[index];
186 }
187
188 extern "C" const char* QERPlug_Init (void *hApp, void* pMainWidget)
189 {
190         g_pMainWidget = pMainWidget;
191
192         UFOAIFilterInit();
193
194         return PLUGIN_NAME;
195 }
196
197 extern "C" const char* QERPlug_GetName (void)
198 {
199         return (char *) PLUGIN_NAME;
200 }
201
202 extern "C" const char* QERPlug_GetCommandList (void)
203 {
204         return (char *) PLUGIN_COMMANDS;
205 }
206
207 extern "C" void QERPlug_Dispatch (const char *p, vec3_t vMin, vec3_t vMax, bool bSingleBrush)
208 {
209         if (!strcmp(p, CMD_ABOUT)) {
210                 g_FuncTable.m_pfnMessageBox(NULL, PLUGIN_ABOUT, "About", MB_OK, NULL);
211         } else {
212                 Sys_Printf("Message: %s\n", p);
213         }
214 }
215
216 // =============================================================================
217 // SYNAPSE
218
219 CSynapseServer* g_pSynapseServer = NULL;
220 CSynapseClientUFOAI g_SynapseClient;
221
222 #if __GNUC__ >= 4
223 #pragma GCC visibility push(default)
224 #endif
225 extern "C" CSynapseClient* SYNAPSE_DLL_EXPORT Synapse_EnumerateInterfaces (const char *version, CSynapseServer *pServer)
226 {
227 #if __GNUC__ >= 4
228 #pragma GCC visibility pop
229 #endif
230         if (strcmp(version, SYNAPSE_VERSION)) {
231                 Syn_Printf("ERROR: synapse API version mismatch: should be '" SYNAPSE_VERSION "', got '%s'\n", version);
232                 return NULL;
233         }
234         g_pSynapseServer = pServer;
235         g_pSynapseServer->IncRef();
236         Set_Syn_Printf(g_pSynapseServer->Get_Syn_Printf());
237
238         g_SynapseClient.AddAPI(TOOLBAR_MAJOR, UFOAI_MINOR, sizeof(_QERPlugToolbarTable));
239         g_SynapseClient.AddAPI(PLUGIN_MAJOR, UFOAI_MINOR, sizeof(_QERPluginTable));
240
241         g_SynapseClient.AddAPI(RADIANT_MAJOR, NULL, sizeof(g_FuncTable), SYN_REQUIRE, &g_FuncTable);
242         g_SynapseClient.AddAPI(QGL_MAJOR, NULL, sizeof(g_QglTable), SYN_REQUIRE, &g_QglTable);
243         g_SynapseClient.AddAPI(VFS_MAJOR, "*", sizeof(g_FileSystemTable), SYN_REQUIRE, &g_FileSystemTable);
244         // get worldspawn
245         g_SynapseClient.AddAPI(ENTITY_MAJOR, NULL, sizeof(g_EntityTable), SYN_REQUIRE, &g_EntityTable);
246         // selected brushes
247         g_SynapseClient.AddAPI(DATA_MAJOR, NULL, sizeof(g_DataTable), SYN_REQUIRE, &g_DataTable);
248
249         return &g_SynapseClient;
250 }
251
252 bool CSynapseClientUFOAI::RequestAPI (APIDescriptor_t *pAPI)
253 {
254         if (!strcmp(pAPI->major_name, PLUGIN_MAJOR)) {
255                 _QERPluginTable* pTable= static_cast<_QERPluginTable*>(pAPI->mpTable);
256
257                 pTable->m_pfnQERPlug_Init = QERPlug_Init;
258                 pTable->m_pfnQERPlug_GetName = QERPlug_GetName;
259                 pTable->m_pfnQERPlug_GetCommandList = QERPlug_GetCommandList;
260                 pTable->m_pfnQERPlug_Dispatch = QERPlug_Dispatch;
261                 return true;
262         } else if (!strcmp(pAPI->major_name, TOOLBAR_MAJOR)) {
263                 _QERPlugToolbarTable* pTable= static_cast<_QERPlugToolbarTable*>(pAPI->mpTable);
264
265                 pTable->m_pfnToolbarButtonCount = &ToolbarButtonCount;
266                 pTable->m_pfnGetToolbarButton = &GetToolbarButton;
267                 return true;
268         }
269
270         Syn_Printf("ERROR: RequestAPI( '%s' ) not found in '%s'\n", pAPI->major_name, GetInfo());
271         return false;
272 }
273
274 #include "version.h"
275
276 const char* CSynapseClientUFOAI::GetInfo()
277 {
278         return PLUGIN_NAME " plugin built " __DATE__ " " RADIANT_VERSION;
279 }
280
281 const char* CSynapseClientUFOAI::GetName()
282 {
283         return PLUGIN_NAME;
284 }
285