]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/camera/camera.cpp
reformat code! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / contrib / camera / camera.cpp
1 /*
2    Copyright (C) 1999-2006 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    Camera plugin for GtkRadiant
24    Copyright (C) 2002 Splash Damage Ltd.
25  */
26
27 #include "camera.h"
28
29 // Render view
30 CRenderer *Renderer = NULL;
31
32 // Interaction
33 CListener *Listener = NULL;
34
35 // plugin name
36 static const char *PLUGIN_NAME = "Camera";
37
38 // commands in the menu
39 static const char *PLUGIN_COMMANDS = "About...,-,Load Camera...,-,Preview Camera,-,Camera Inspector...,-,New Spline Camera,New Interpolated Camera,New Fixed Camera";
40
41 // globals
42 ui::Window g_pRadiantWnd{ui::null};
43 ui::Window g_pCameraInspectorWnd{ui::null};
44 CCamera *firstCam = NULL;       // double linked list
45 CCamera *firstFreeCam = NULL;   // single linked list
46 CCamera *currentCam = NULL;     // single item
47 bool g_bEditOn = false;
48 int g_iEditMode = 0;                    // 0: editting points 1: adding points
49 int g_iActiveTarget = -1;
50 int g_iPreviewRunning = 0;              // 0: no preview 1: start preview 2: preview in progress
51
52 static const char *PLUGIN_ABOUT = "Camera v1.0 for NetRadiant\n"
53         "by Arnout van Meer (rr2do2@splashdamage.com)\n\n"
54         "This product contains software technology\n"
55         "from id Software, Inc. ('id Technology').\n"
56         "id Technology (c) 2001, 2002 id Software, Inc.";
57
58
59 #include "iplugin.h"
60
61 const char *QERPlug_Init(void *hApp, void *pMainWidget)
62 {
63     g_pRadiantWnd = (GtkWidget *) pMainWidget;
64
65     // initialize cams
66     for (int i = 0; i < MAX_CAMERAS; i++) {
67         if (i == 0) {
68             firstFreeCam = new CCamera(i);
69             firstCam = firstFreeCam;
70         } else {
71             firstCam->SetNext(new CCamera(i));
72             firstCam = firstCam->GetNext();
73         }
74     }
75     firstCam = NULL;
76
77     if (!Renderer) {
78         Renderer = new CRenderer;
79     }
80
81     if (g_pCameraInspectorWnd == NULL) {
82         g_pCameraInspectorWnd = CreateCameraInspectorDialog();
83     }
84
85     GetFileTypeRegistry()->addType("camera", "", filetype_t("Camera file", "*.camera"));
86
87     return "Camera for NetRadiant";
88 }
89
90 const char *QERPlug_GetName()
91 {
92     return PLUGIN_NAME;
93 }
94
95 const char *QERPlug_GetCommandList()
96 {
97     return PLUGIN_COMMANDS;
98 }
99
100 void QERPlug_Dispatch(const char *p, float *vMin, float *vMax, bool bSingleBrush)
101 {
102     if (!strcmp(p, "New Fixed Camera")) {
103         DoNewFixedCamera();
104     } else if (!strcmp(p, "New Interpolated Camera")) {
105         DoNewInterpolatedCamera();
106     } else if (!strcmp(p, "New Spline Camera")) {
107         DoNewSplineCamera();
108     } else if (!strcmp(p, "Camera Inspector...")) {
109         DoCameraInspector();
110     } else if (!strcmp(p, "Preview Camera")) {
111         DoPreviewCamera();
112     } else if (!strcmp(p, "Load Camera...")) {
113         DoLoadCamera();
114     } else if (!strcmp(p, "About...")) {
115         g_FuncTable.m_pfnMessageBox((GtkWidget *) g_pRadiantWnd, PLUGIN_ABOUT, "About", eMB_OK);
116     }
117 }
118
119
120 // toolbar
121
122 #include "itoolbar.h"
123
124 unsigned int ToolbarButtonCount()
125 {
126     return 1;
127 }
128
129 class CameraInspectorButton : public IToolbarButton {
130 public:
131     virtual const char *getImage() const
132     {
133         return "camera_insp.jpg";
134     }
135
136     virtual const char *getText() const
137     {
138         return "Inspector";
139     }
140
141     virtual const char *getTooltip() const
142     {
143         return "Camera Inspector";
144     }
145
146     virtual void activate() const
147     {
148         DoCameraInspector();
149     }
150
151     virtual EType getType() const
152     {
153         return eButton;
154     }
155 };
156
157 CameraInspectorButton g_camerainspectorbutton;
158
159 const IToolbarButton *GetToolbarButton(unsigned int index)
160 {
161     return &g_camerainspectorbutton;
162 }
163
164
165 _QERFuncTable_1 g_FuncTable;
166 _QERQglTable g_QglTable;
167 _QERUITable g_UITable;
168 _QERCameraTable g_CameraTable;
169
170 // =============================================================================
171 // SYNAPSE
172
173 #include "synapse.h"
174
175 class CameraSynapseClient : public CSynapseClient {
176 public:
177 // CSynapseClient API
178     bool RequestAPI(APIDescriptor_t *pAPI);
179
180     const char *GetInfo();
181
182     CameraSynapseClient()
183     {}
184
185     virtual ~CameraSynapseClient()
186     {}
187 };
188
189 CSynapseServer *g_pSynapseServer = NULL;
190 CameraSynapseClient g_SynapseClient;
191
192 extern "C" CSynapseClient *SYNAPSE_DLL_EXPORT
193
194 Synapse_EnumerateInterfaces(const char *version, CSynapseServer *pServer)
195 {
196     if (strcmp(version, SYNAPSE_VERSION)) {
197         Syn_Printf("ERROR: synapse API version mismatch: should be '"
198         SYNAPSE_VERSION
199         "', got '%s'\n", version );
200         return NULL;
201     }
202     g_pSynapseServer = pServer;
203     g_pSynapseServer->IncRef();
204     Set_Syn_Printf(g_pSynapseServer->Get_Syn_Printf());
205
206     g_SynapseClient.AddAPI(TOOLBAR_MAJOR, "camera", sizeof(_QERPlugToolbarTable));
207     g_SynapseClient.AddAPI(PLUGIN_MAJOR, "camera", sizeof(_QERPluginTable));
208
209     g_SynapseClient.AddAPI(RADIANT_MAJOR, NULL, sizeof(_QERFuncTable_1), SYN_REQUIRE, &g_FuncTable);
210     g_SynapseClient.AddAPI(UI_MAJOR, NULL, sizeof(_QERUITable), SYN_REQUIRE, &g_UITable);
211     g_SynapseClient.AddAPI(QGL_MAJOR, NULL, sizeof(_QERQglTable), SYN_REQUIRE, &g_QglTable);
212     g_SynapseClient.AddAPI(CAMERA_MAJOR, NULL, sizeof(_QERCameraTable), SYN_REQUIRE, &g_CameraTable);
213
214     return &g_SynapseClient;
215 }
216
217 bool CameraSynapseClient::RequestAPI(APIDescriptor_t *pAPI)
218 {
219     if (!strcmp(pAPI->major_name, TOOLBAR_MAJOR)) {
220         _QERPlugToolbarTable *pTable = static_cast<_QERPlugToolbarTable *>( pAPI->mpTable );
221
222         pTable->m_pfnToolbarButtonCount = &ToolbarButtonCount;
223         pTable->m_pfnGetToolbarButton = &GetToolbarButton;
224         return true;
225     } else if (!strcmp(pAPI->major_name, PLUGIN_MAJOR)) {
226         _QERPluginTable *pTable = static_cast<_QERPluginTable *>( pAPI->mpTable );
227
228         pTable->m_pfnQERPlug_Init = QERPlug_Init;
229         pTable->m_pfnQERPlug_GetName = QERPlug_GetName;
230         pTable->m_pfnQERPlug_GetCommandList = QERPlug_GetCommandList;
231         pTable->m_pfnQERPlug_Dispatch = QERPlug_Dispatch;
232         return true;
233     }
234
235     Syn_Printf("ERROR: RequestAPI( '%s' ) not found in '%s'\n", pAPI->major_name, GetInfo());
236     return false;
237 }
238
239 #include "version.h"
240
241 const char *CameraSynapseClient::GetInfo()
242 {
243     return "Camera plugin v1.0 - Arnout van Meer - built " __DATE__ " "
244     RADIANT_VERSION;
245 }
246
247
248 //
249 // CCamera
250 //
251 CCamera *AllocCam()
252 {
253     if (!firstFreeCam) {
254         return (NULL);
255     }
256
257     CCamera *cam = firstFreeCam;
258     firstFreeCam = firstFreeCam->GetNext();
259     cam->Init();
260     if (firstCam) {
261         cam->SetNext(firstCam);
262         firstCam->SetPrev(cam);
263     }
264     firstCam = cam;
265
266     return (cam);
267 }
268
269 void FreeCam(CCamera *cam)
270 {
271     if (cam->GetPrev()) {
272         if (cam->GetNext()) {
273             cam->GetPrev()->SetNext(cam->GetNext());
274             cam->GetNext()->SetPrev(cam->GetPrev());
275         } else {
276             cam->GetPrev()->SetNext(NULL);
277         }
278     } else if (cam->GetNext()) {
279         cam->GetNext()->SetPrev(NULL);
280         firstCam = cam->GetNext();
281     } else {
282         firstCam = NULL;
283     }
284
285     cam->GetCam()->clear();
286     cam->Init();
287
288     if (firstFreeCam) {
289         cam->SetNext(firstFreeCam);
290     }
291     firstFreeCam = cam;
292 }
293
294 void SetCurrentCam(CCamera *cam)
295 {
296     currentCam = cam;
297 }
298
299 CCamera *GetCurrentCam()
300 {
301     return (currentCam);
302 }