]> git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/camera/funchandlers.cpp
reformat code! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / contrib / camera / funchandlers.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 #include "globaldefs.h"
29
30 extern GtkWidget *g_pEditModeAddRadioButton;
31
32 char *Q_realpath(const char *path, char *resolved_path, size_t size)
33 {
34 #if GDEF_OS_POSIX
35     return realpath( path, resolved_path );
36 #elif GDEF_OS_WINDOWS
37     return _fullpath( resolved_path, path, size );
38 #else
39 #error "unsupported platform"
40 #endif
41 }
42
43 static void DoNewCamera(idCameraPosition::positionType type)
44 {
45     CCamera *cam = AllocCam();
46
47     if (cam) {
48         char buf[128];
49         sprintf(buf, "camera%i", cam->GetCamNum());
50
51         cam->GetCam()->startNewCamera(type);
52         cam->GetCam()->setName(buf);
53         cam->GetCam()->buildCamera();
54
55         sprintf(buf, "Unsaved Camera %i", cam->GetCamNum());
56         cam->SetFileName(buf, false);
57
58         SetCurrentCam(cam);
59         RefreshCamListCombo();
60
61         // Go to editmode Add
62         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(g_pEditModeAddRadioButton), TRUE);
63
64         // Show the camera inspector
65         DoCameraInspector();
66
67         // Start edit mode (if not initiated by DoCameraInspector)
68         if (!g_bEditOn) {
69             DoStartEdit(GetCurrentCam());
70         }
71     } else {
72         g_FuncTable.m_pfnMessageBox((GtkWidget *) g_pRadiantWnd, "No free cameras available.", "Create Camera Error",
73                                     eMB_OK);
74     }
75 }
76
77 void DoNewFixedCamera()
78 {
79     DoNewCamera(idCameraPosition::FIXED);
80 }
81
82 void DoNewInterpolatedCamera()
83 {
84     DoNewCamera(idCameraPosition::INTERPOLATED);
85 }
86
87 void DoNewSplineCamera()
88 {
89     DoNewCamera(idCameraPosition::SPLINE);
90 }
91
92 void DoCameraInspector()
93 {
94     g_pCameraInspectorWnd.show();
95 }
96
97 void DoPreviewCamera()
98 {
99     if (GetCurrentCam()) {
100         g_iPreviewRunning = 1;
101         g_FuncTable.m_pfnSysUpdateWindows(W_XY_OVERLAY | W_CAMERA);
102     }
103 }
104
105 void DoLoadCamera()
106 {
107     char basepath[PATH_MAX];
108
109     if (firstCam && firstCam->HasBeenSaved()) {
110         ExtractFilePath(firstCam->GetFileName(), basepath);
111     } else {
112         strcpy(basepath, g_FuncTable.m_pfnGetGamePath());
113     }
114
115     const gchar *filename = g_FuncTable.m_pfnFileDialog((GtkWidget *) g_pRadiantWnd, TRUE, "Open Camera File", basepath,
116                                                         "camera");
117
118     if (filename) {
119         CCamera *cam = AllocCam();
120         char fullpathtofile[PATH_MAX];
121
122         if (cam) {
123             Q_realpath(filename, fullpathtofile, PATH_MAX);
124
125             // see if this camera file was already loaded
126             CCamera *checkCam = firstCam->GetNext(); // not the first one as we just allocated it
127             while (checkCam) {
128                 if (!strcmp(fullpathtofile, checkCam->GetFileName())) {
129                     char error[PATH_MAX + 64];
130                     FreeCam(cam);
131                     sprintf(error, "Camera file \'%s\' is already loaded", fullpathtofile);
132                     g_FuncTable.m_pfnMessageBox((GtkWidget *) g_pRadiantWnd, error, "Load error", eMB_OK);
133                     //g_free( filename );
134                     return;
135                 }
136                 checkCam = checkCam->GetNext();
137             }
138
139             if (loadCamera(cam->GetCamNum(), fullpathtofile)) {
140                 cam->GetCam()->buildCamera();
141                 cam->SetFileName(filename, true);
142                 SetCurrentCam(cam);
143                 RefreshCamListCombo();
144                 g_FuncTable.m_pfnSysUpdateWindows(W_XY_OVERLAY | W_CAMERA);
145             } else {
146                 char error[PATH_MAX + 64];
147                 FreeCam(cam);
148                 sprintf(error, "An error occured during the loading of \'%s\'", fullpathtofile);
149                 g_FuncTable.m_pfnMessageBox((GtkWidget *) g_pRadiantWnd, error, "Load error", eMB_OK);
150             }
151
152             //g_free( filename );
153         } else {
154             g_FuncTable.m_pfnMessageBox((GtkWidget *) g_pRadiantWnd, "No free camera slots available", "Load error",
155                                         eMB_OK);
156         }
157     }
158 }
159
160 void DoSaveCamera()
161 {
162     char basepath[PATH_MAX];
163
164     if (!GetCurrentCam()) {
165         return;
166     }
167
168     if (GetCurrentCam()->GetFileName()[0]) {
169         ExtractFilePath(GetCurrentCam()->GetFileName(), basepath);
170     } else {
171         strcpy(basepath, g_FuncTable.m_pfnGetGamePath());
172     }
173
174     const gchar *filename = g_FuncTable.m_pfnFileDialog(g_pRadiantWnd, FALSE, "Save Camera File", basepath, "camera");
175
176     if (filename) {
177         char fullpathtofile[PATH_MAX + 8];
178
179         Q_realpath(filename, fullpathtofile, PATH_MAX);
180
181         // File dialog from windows (and maybe the gtk one from radiant) doesn't handle default extensions properly.
182         // Add extension and check again if file exists
183         if (strcmp(fullpathtofile + (strlen(fullpathtofile) - 7), ".camera")) {
184             strcat(fullpathtofile, ".camera");
185
186             if (FileExists(fullpathtofile)) {
187                 if (g_FuncTable.m_pfnMessageBox((GtkWidget *) g_pRadiantWnd, "File already exists.\nOverwrite?",
188                                                 "Save Camera File", eMB_YESNO) == eIDNO) {
189                     return;
190                 }
191             }
192         }
193
194         // see if this camera file was already loaded
195         CCamera *checkCam = firstCam;
196         while (checkCam) {
197             if (checkCam == GetCurrentCam()) {
198                 checkCam = checkCam->GetNext();
199                 if (!checkCam) { // we only have one camera file opened so no need to check further
200                     break;
201                 }
202             } else if (!strcmp(fullpathtofile, checkCam->GetFileName())) {
203                 char error[PATH_MAX + 64];
204                 sprintf(error,
205                         "Camera file \'%s\' is currently loaded by NetRadiant.\nPlease select a different filename.",
206                         fullpathtofile);
207                 g_FuncTable.m_pfnMessageBox((GtkWidget *) g_pRadiantWnd, error, "Save error", eMB_OK);
208                 return;
209             }
210             checkCam = checkCam->GetNext();
211         }
212
213         // FIXME: check for existing directory
214
215         GetCurrentCam()->GetCam()->save(fullpathtofile);
216         GetCurrentCam()->SetFileName(fullpathtofile, true);
217         RefreshCamListCombo();
218     }
219 }
220
221 void DoUnloadCamera()
222 {
223     if (!GetCurrentCam()) {
224         return;
225     }
226
227     if (!GetCurrentCam()->HasBeenSaved()) {
228         char buf[PATH_MAX + 64];
229         sprintf(buf, "Do you want to save the changes for camera '%s'?", GetCurrentCam()->GetCam()->getName());
230         if (g_FuncTable.m_pfnMessageBox((GtkWidget *) g_pRadiantWnd, buf, "Warning", eMB_YESNO) == eIDYES) {
231             DoSaveCamera();
232         }
233     } else if (GetCurrentCam()->HasBeenSaved() == 2) {
234         char buf[PATH_MAX + 64];
235         sprintf(buf, "Do you want to save the changes made to camera file '%s'?", GetCurrentCam()->GetFileName());
236         if (g_FuncTable.m_pfnMessageBox((GtkWidget *) g_pRadiantWnd, buf, "Warning", eMB_YESNO) == eIDYES) {
237             DoSaveCamera();
238         }
239     }
240
241     if (g_pCurrentEditCam) {
242         DoStopEdit();
243         g_pCurrentEditCam = NULL;
244     }
245
246     FreeCam(GetCurrentCam());
247     SetCurrentCam(NULL);
248     RefreshCamListCombo();
249 }
250
251 CCamera *g_pCurrentEditCam = NULL;
252
253 void DoStartEdit(CCamera *cam)
254 {
255     if (g_pCurrentEditCam) {
256         DoStopEdit();
257         g_pCurrentEditCam = NULL;
258     }
259
260     if (cam) {
261         g_bEditOn = true;
262
263         if (!Listener) {
264             Listener = new CListener;
265         }
266
267         cam->GetCam()->startEdit(g_iActiveTarget < 0 ? true : false);
268
269         g_pCurrentEditCam = cam;
270
271         g_FuncTable.m_pfnSysUpdateWindows(W_XY_OVERLAY | W_CAMERA);
272     }
273 }
274
275 void DoStopEdit(void)
276 {
277     g_bEditOn = false;
278
279     if (Listener) {
280         delete Listener;
281         Listener = NULL;
282     }
283
284     if (g_pCurrentEditCam) {
285         // stop editing on the current cam
286         g_pCurrentEditCam->GetCam()->stopEdit();
287         g_pCurrentEditCam = NULL;
288
289         g_FuncTable.m_pfnSysUpdateWindows(W_XY_OVERLAY | W_CAMERA);
290     }
291 }