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