]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/textool/TexTool.cpp
Merge commit '515673c08f8718a237e90c2130a1f5294f966d6a'
[xonotic/netradiant.git] / plugins / textool / TexTool.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 //
24 // DESCRIPTION:
25 // main plugin implementation
26 // texturing tools for Radiant
27 //
28
29 #include "StdAfx.h"
30
31 static void dialog_button_callback (GtkWidget *widget, gpointer data)
32 {
33   GtkWidget *parent;
34   int *loop, *ret;
35  
36   parent = gtk_widget_get_toplevel (widget);
37   loop = (int*)g_object_get_data (G_OBJECT (parent), "loop");
38   ret = (int*)g_object_get_data (G_OBJECT (parent), "ret");
39  
40   *loop = 0;
41   *ret = gpointer_to_int (data);
42 }
43  
44 static gint dialog_delete_callback (GtkWidget *widget, GdkEvent* event, gpointer data)
45 {
46   int *loop;
47  
48   gtk_widget_hide (widget);
49   loop = (int*)g_object_get_data (G_OBJECT (widget), "loop");
50   *loop = 0;
51
52   return TRUE;
53 }
54
55 int DoMessageBox (const char* lpText, const char* lpCaption, guint32 uType)
56 {
57   GtkWidget *window, *w, *vbox, *hbox;
58   int mode = (uType & MB_TYPEMASK), ret, loop = 1;
59  
60   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
61   gtk_signal_connect (GTK_OBJECT (window), "delete_event",
62                       GTK_SIGNAL_FUNC (dialog_delete_callback), NULL);
63   gtk_signal_connect (GTK_OBJECT (window), "destroy",
64                       GTK_SIGNAL_FUNC (gtk_widget_destroy), NULL);
65   gtk_window_set_title (GTK_WINDOW (window), lpCaption);
66   gtk_container_border_width (GTK_CONTAINER (window), 10);
67   g_object_set_data (G_OBJECT (window), "loop", &loop);
68   g_object_set_data (G_OBJECT (window), "ret", &ret);
69   gtk_widget_realize (window);
70  
71   vbox = gtk_vbox_new (FALSE, 10);
72   gtk_container_add (GTK_CONTAINER (window), vbox);
73   gtk_widget_show (vbox);
74  
75   w = gtk_label_new (lpText);
76   gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 2);
77   gtk_label_set_justify (GTK_LABEL (w), GTK_JUSTIFY_LEFT);
78   gtk_widget_show (w);
79  
80   w = gtk_hseparator_new ();
81   gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 2);
82   gtk_widget_show (w);
83  
84   hbox = gtk_hbox_new (FALSE, 10);
85   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 2);
86   gtk_widget_show (hbox);
87  
88   if (mode == MB_OK)
89   {
90     w = gtk_button_new_with_label ("Ok");
91     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
92     gtk_signal_connect (GTK_OBJECT (w), "clicked",
93                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDOK));
94     GTK_WIDGET_SET_FLAGS (w, GTK_CAN_DEFAULT);
95     gtk_widget_grab_default (w);
96     gtk_widget_show (w);
97     ret = IDOK;
98   }
99   else if (mode ==  MB_OKCANCEL)
100   {
101     w = gtk_button_new_with_label ("Ok");
102     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
103     gtk_signal_connect (GTK_OBJECT (w), "clicked",
104                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDOK));
105     GTK_WIDGET_SET_FLAGS (w, GTK_CAN_DEFAULT);
106     gtk_widget_grab_default (w);
107     gtk_widget_show (w);
108  
109     w = gtk_button_new_with_label ("Cancel");
110     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
111     gtk_signal_connect (GTK_OBJECT (w), "clicked",
112                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDCANCEL));
113     gtk_widget_show (w);
114     ret = IDCANCEL;
115   }
116   else if (mode == MB_YESNOCANCEL)
117   {
118     w = gtk_button_new_with_label ("Yes");
119     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
120     gtk_signal_connect (GTK_OBJECT (w), "clicked",
121                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDYES));
122     GTK_WIDGET_SET_FLAGS (w, GTK_CAN_DEFAULT);
123     gtk_widget_grab_default (w);
124     gtk_widget_show (w);
125  
126     w = gtk_button_new_with_label ("No");
127     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
128     gtk_signal_connect (GTK_OBJECT (w), "clicked",
129                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDNO));
130     gtk_widget_show (w);
131  
132     w = gtk_button_new_with_label ("Cancel");
133     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
134     gtk_signal_connect (GTK_OBJECT (w), "clicked",
135                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDCANCEL));
136     gtk_widget_show (w);
137     ret = IDCANCEL;
138   }
139   else /* if (mode == MB_YESNO) */
140   {
141     w = gtk_button_new_with_label ("Yes");
142     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
143     gtk_signal_connect (GTK_OBJECT (w), "clicked",
144                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDYES));
145     GTK_WIDGET_SET_FLAGS (w, GTK_CAN_DEFAULT);
146     gtk_widget_grab_default (w);
147     gtk_widget_show (w);
148  
149     w = gtk_button_new_with_label ("No");
150     gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
151     gtk_signal_connect (GTK_OBJECT (w), "clicked",
152                         GTK_SIGNAL_FUNC (dialog_button_callback), GINT_TO_POINTER (IDNO));
153     gtk_widget_show (w);
154     ret = IDNO;
155   }
156  
157   gtk_widget_show (window);
158   gtk_grab_add (window);
159  
160   while (loop)
161     gtk_main_iteration ();
162  
163   gtk_grab_remove (window);
164   gtk_widget_destroy (window);
165  
166   return ret;
167 }
168
169 // Radiant function table
170 _QERFuncTable_1 g_FuncTable;
171
172 // plugin name
173 const char *PLUGIN_NAME = "Q3 Texture Tools";
174
175 // commands in the menu
176 const char *PLUGIN_COMMANDS = "About...;Go...";
177
178 // cast to GtkWidget*
179 void *g_pMainWnd;
180 IWindow *g_pToolWnd = NULL; // handle to the window
181 CWindowListener g_Listen;
182
183 // plugin interfaces ---------------------------
184 bool                    g_bQglInitDone = false;
185 OpenGLBinding           g_QglTable;
186 bool                    g_bSelectedFaceInitDone = false;
187 _QERSelectedFaceTable   g_SelectedFaceTable;
188 bool            g_bUITable = false;
189 _QERUITable     g_UITable;
190
191 // selected face -------------------------------
192 // we use this one to commit / read with Radiant
193 _QERFaceData                    g_SelectedFaceData;
194 // g_pSelectedFaceWindings gets allocated with MAX_POINTS_ON_WINDING at plugin startup ( QERPlug_Init )
195 winding_t                               *g_pSelectedFaceWinding = NULL;
196 const float                             g_ViewportRatio = 1.2f;
197 // usefull class to manage the 2D view
198 C2DView                                 g_2DView;
199 // control points to move the polygon
200 CControlPointsManagerBFace      g_ControlPointsBFace;
201 // tells if a face is selected and we have something to render in the TexWindow
202 bool                                    g_bTexViewReady = false;
203 // data for texture work
204 int                                             g_NumPoints;
205 CtrlPts_t                               g_WorkWinding;
206 // reference _QERFaceData we use on Cancel, and for Commit
207 _QERFaceData                    g_CancelFaceData;
208
209 // patches -------------------------------------
210 bool                                    g_bPatch = false;
211 //++timo we use this one to grab selected patchMesh_t
212 // FIXME: update when there's a real interface to read/write patches
213 bool                                    g_bSurfaceTableInitDone = false;
214 _QERAppSurfaceTable             g_SurfaceTable;
215 CControlPointsManagerPatch      g_ControlPointsPatch;
216 // data for texture work
217 patchMesh_t*                    g_pPatch;
218 // we only use ctrl[][].st in this one
219 patchMesh_t                             g_WorkPatch;
220 // copy of initial g_pPatch for Cancel situation
221 patchMesh_t                             g_CancelPatch;
222
223 // ---------------------------------------------
224 // holds the manager we are currently using
225 CControlPointsManager   *g_pManager = NULL;
226
227 // ---------------------------------------------
228 // globals flags for user preferences
229 //++timo TODO: this should be retrieved from the Editor's .INI prefs in a dedicated interface
230 // update camera view during manipulation ?
231 bool                                    g_bPrefsUpdateCameraView = true;
232
233 // misc ----------------------------------------
234 bool                                    g_bHelp = false;
235 //++timo FIXME: used to close the plugin window if InitTexView fails
236 // it's dirty, only use is to prevent infinite loop in DialogProc
237 bool                                    g_bClosing = false;
238
239 const char *PLUGIN_ABOUT = "Texture Tools for Radiant\n\n"
240                            "Gtk port by Leonardo Zide (leo@lokigames.com)\n"
241                            "Original version by Timothee \"TTimo\" Besset (timo@qeradiant.com)";
242
243 extern "C" void* WINAPI QERPlug_GetFuncTable ()
244 {
245   return &g_FuncTable;
246 }
247
248 const char* QERPlug_Init (void* hApp, void *pWidget)
249 {
250   int size;
251   GtkWidget* pMainWidget = static_cast<GtkWidget*>(pWidget);
252
253   g_pMainWnd = pMainWidget;
254   memset(&g_FuncTable, 0, sizeof(_QERFuncTable_1));
255   g_FuncTable.m_nSize = sizeof(_QERFuncTable_1);
256   size = (int)((winding_t *)0)->points[MAX_POINTS_ON_WINDING];
257   g_pSelectedFaceWinding = (winding_t *)malloc( size );
258   memset( g_pSelectedFaceWinding, 0, size );
259   return "Texture tools for Radiant";
260 }
261
262 const char* QERPlug_GetName()
263 {
264   return (char*)PLUGIN_NAME;
265 }
266
267 const char* QERPlug_GetCommandList()
268 {
269   return PLUGIN_COMMANDS;
270 }
271
272 char *TranslateString (char *buf)
273 {
274   static        char    buf2[32768];
275   int           i, l;
276   char  *out;
277
278   l = strlen(buf);
279   out = buf2;
280   for (i=0 ; i<l ; i++)
281   {
282     if (buf[i] == '\n')
283     {
284       *out++ = '\r';
285       *out++ = '\n';
286     }
287     else
288       *out++ = buf[i];
289   }
290   *out++ = 0;
291
292   return buf2;
293 }
294
295 // called by InitTexView to fit the view against the bounding box of control points
296 void FitView (IWindow* hwndDlg, int TexSize[2])
297 {
298   // apply a ratio to get the area we'll draw
299   g_2DView.m_Center[0] = 0.5f * ( g_2DView.m_Mins[0] + g_2DView.m_Maxs[0] );
300   g_2DView.m_Center[1] = 0.5f * ( g_2DView.m_Mins[1] + g_2DView.m_Maxs[1] );
301   g_2DView.m_Mins[0] = g_2DView.m_Center[0] + g_ViewportRatio*( g_2DView.m_Mins[0] - g_2DView.m_Center[0] );
302   g_2DView.m_Mins[1] = g_2DView.m_Center[1] + g_ViewportRatio*( g_2DView.m_Mins[1] - g_2DView.m_Center[1] );
303   g_2DView.m_Maxs[0] = g_2DView.m_Center[0] + g_ViewportRatio*( g_2DView.m_Maxs[0] - g_2DView.m_Center[0] );
304   g_2DView.m_Maxs[1] = g_2DView.m_Center[1] + g_ViewportRatio*( g_2DView.m_Maxs[1] - g_2DView.m_Center[1] );
305
306   g_2DView.m_rect.left = 0;
307   g_2DView.m_rect.top = 0;
308   g_2DView.m_rect.bottom = hwndDlg->getHeight();
309   g_2DView.m_rect.right = hwndDlg->getWidth();
310
311   // we need to draw this area, now compute a bigger area so the texture scale is the same along X and Y
312   // compute box shape in XY space, let's say X <-> S we'll get a ratio for Y: 
313   if (!g_bPatch)
314   {
315     g_SelectedFaceTable.m_pfnGetTextureSize( 0, TexSize );
316   }
317   else
318   {
319     TexSize[0] = g_pPatch->d_texture->width;
320     TexSize[1] = g_pPatch->d_texture->height;
321   }
322   // we want a texture with the same X / Y ratio
323   // compute XY space / window size ratio
324   float SSize = (float)fabs( g_2DView.m_Maxs[0] - g_2DView.m_Mins[0] );
325   float TSize = (float)fabs( g_2DView.m_Maxs[1] - g_2DView.m_Mins[1] );
326   float XSize = TexSize[0] * SSize;
327   float YSize = TexSize[1] * TSize;
328   float RatioX = XSize / (float)abs( g_2DView.m_rect.left - g_2DView.m_rect.right );
329   float RatioY = YSize / (float)abs( g_2DView.m_rect.top - g_2DView.m_rect.bottom );
330   if ( RatioX > RatioY )
331   {
332     YSize = (float)abs( g_2DView.m_rect.top - g_2DView.m_rect.bottom ) * RatioX;
333     TSize = YSize / (float)TexSize[1];
334   }
335   else
336   {
337     XSize = (float)abs( g_2DView.m_rect.left - g_2DView.m_rect.right ) * RatioY;
338     SSize = XSize / (float)TexSize[0];
339   }
340   g_2DView.m_Mins[0] = g_2DView.m_Center[0] - 0.5f * SSize;
341   g_2DView.m_Maxs[0] = g_2DView.m_Center[0] + 0.5f * SSize;
342   g_2DView.m_Mins[1] = g_2DView.m_Center[1] - 0.5f * TSize;
343   g_2DView.m_Maxs[1] = g_2DView.m_Center[1] + 0.5f * TSize;
344 }
345
346 // call this one each time we need to re-init
347 //++timo TODO: re-init objects state, g_2DView and g_ControlPointsManager
348 void InitTexView( IWindow* hwndDlg )
349 {
350   // size of the texture we are working on
351   int TexSize[2];
352   g_bTexViewReady = false;
353   if (g_SelectedFaceTable.m_pfnGetSelectedFaceCount() != 0)
354   {
355     g_SelectedFaceTable.m_pfnGetFaceInfo( 0, &g_SelectedFaceData, g_pSelectedFaceWinding );
356     g_bPatch = false;
357     int i;
358     // we have something selected
359     // setup: compute BBox for the winding ( in ST space )
360     //++timo FIXME: move this in a C2DView member ? used as well for patches
361     g_2DView.m_Mins[0] = +9999.0f; g_2DView.m_Mins[1] = +9999.0f;
362     g_2DView.m_Maxs[0] = -9999.0f; g_2DView.m_Maxs[1] = -9999.0f;
363     for ( i=0; i<g_pSelectedFaceWinding->numpoints; i++ )
364     {
365       if ( g_pSelectedFaceWinding->points[i][3] < g_2DView.m_Mins[0] )
366         g_2DView.m_Mins[0] = g_pSelectedFaceWinding->points[i][3];
367       if ( g_pSelectedFaceWinding->points[i][3] > g_2DView.m_Maxs[0] )
368         g_2DView.m_Maxs[0] = g_pSelectedFaceWinding->points[i][3];
369       if ( g_pSelectedFaceWinding->points[i][4] < g_2DView.m_Mins[1] )
370         g_2DView.m_Mins[1] = g_pSelectedFaceWinding->points[i][4];
371       if ( g_pSelectedFaceWinding->points[i][4] > g_2DView.m_Maxs[1] )
372         g_2DView.m_Maxs[1] = g_pSelectedFaceWinding->points[i][4];
373     }
374     // NOTE: FitView will read and init TexSize
375     FitView( hwndDlg, TexSize );
376     // now init the work tables
377     g_NumPoints = g_pSelectedFaceWinding->numpoints;
378     for ( i=0; i<g_NumPoints; i++ )
379     {
380       g_WorkWinding.data[i][0] = g_pSelectedFaceWinding->points[i][3];
381       g_WorkWinding.data[i][1] = g_pSelectedFaceWinding->points[i][4];
382     }
383     g_ControlPointsBFace.Init( g_NumPoints, &g_WorkWinding, &g_2DView, TexSize, &g_SelectedFaceData, &g_QglTable );
384     // init snap-to-grid
385     float fTexStep[2];
386     fTexStep[0] = 1.0f / float(TexSize[0]);
387     fTexStep[1] = 1.0f / float(TexSize[1]);
388     g_2DView.SetGrid( fTexStep[0], fTexStep[1] );
389     g_pManager = &g_ControlPointsBFace;
390     // prepare the "Cancel" data
391     memcpy( &g_CancelFaceData, &g_SelectedFaceData, sizeof(_QERFaceData) );
392     // we are done
393     g_bTexViewReady = true;
394   }
395   else if ( g_SurfaceTable.m_pfnAnyPatchesSelected())
396   {
397     g_pPatch = g_SurfaceTable.m_pfnGetSelectedPatch();
398     g_bPatch = true;
399     int i,j;
400     // compute BBox for all patch points
401     g_2DView.m_Mins[0] = +9999.0f; g_2DView.m_Mins[1] = +9999.0f;
402     g_2DView.m_Maxs[0] = -9999.0f; g_2DView.m_Maxs[1] = -9999.0f;
403     for ( i=0; i<g_pPatch->width; i++ )
404     {
405       for ( j=0; j<g_pPatch->height; j++ )
406       {
407         if ( g_pPatch->ctrl[i][j].st[0] < g_2DView.m_Mins[0] )
408           g_2DView.m_Mins[0] = g_pPatch->ctrl[i][j].st[0];
409         if ( g_pPatch->ctrl[i][j].st[0] > g_2DView.m_Maxs[0] )
410           g_2DView.m_Maxs[0] = g_pPatch->ctrl[i][j].st[0];
411         if ( g_pPatch->ctrl[i][j].st[1] < g_2DView.m_Mins[1] )
412           g_2DView.m_Mins[1] = g_pPatch->ctrl[i][j].st[1];
413         if ( g_pPatch->ctrl[i][j].st[1] > g_2DView.m_Maxs[1] )
414           g_2DView.m_Maxs[1] = g_pPatch->ctrl[i][j].st[1];
415       }
416     }
417     FitView( hwndDlg, TexSize);
418     // init the work tables
419     g_WorkPatch = *g_pPatch;
420     g_ControlPointsPatch.Init( &g_WorkPatch, &g_2DView, &g_QglTable, g_pPatch );
421     // init snap-to-grid
422     float fTexStep[2];
423     fTexStep[0] = 1.0f / float(TexSize[0]);
424     fTexStep[1] = 1.0f / float(TexSize[1]);
425     g_2DView.SetGrid( fTexStep[0], fTexStep[1] );
426     g_pManager = &g_ControlPointsPatch;
427     // prepare the "cancel" data
428     g_CancelPatch = *g_pPatch;
429     // we are done
430     g_bTexViewReady = true;
431   }
432 }
433
434 void Textool_Validate()
435 {
436   // validate current situation into the main view
437   g_pManager->Commit( );
438   // for a brush face we have an aditionnal step
439   if (!g_bPatch)
440   {
441     // tell Radiant to update (will also send update windows messages )
442     g_SelectedFaceTable.m_pfnSetFaceInfo( 0, &g_SelectedFaceData );
443   }
444   else
445   {
446     // ask to rebuild the patch display data
447     g_pPatch->bDirty = true;
448     // send a repaint to the camera window as well
449     g_FuncTable.m_pfnSysUpdateWindows( W_CAMERA );
450   }
451   // we'll need to update after that as well:
452   g_bTexViewReady = false;
453   // send a repaint message
454   g_pToolWnd->Redraw ();
455 }
456
457 void Textool_Cancel()
458 {
459   if (!g_bPatch)
460   {
461     // tell Radiant to update (will also send update windows messages )
462     g_SelectedFaceTable.m_pfnSetFaceInfo( 0, &g_CancelFaceData );
463   }
464   else
465   {
466     *g_pPatch = g_CancelPatch;
467     g_pPatch->bDirty = true;
468     g_FuncTable.m_pfnSysUpdateWindows( W_CAMERA );
469   }
470   // do not call destroy, decref it
471   g_pToolWnd->DecRef();
472   g_pToolWnd = NULL;
473 }
474
475 static void DoExpose ()
476 {
477   int i,j;
478
479   g_2DView.PreparePaint();
480   g_QglTable.m_pfn_qglColor3f(1, 1, 1);
481   // draw the texture background
482   g_QglTable.m_pfn_qglPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
483   if (!g_bPatch)
484   {
485     g_QglTable.m_pfn_qglBindTexture( GL_TEXTURE_2D, g_SelectedFaceTable.m_pfnGetTextureNumber(0) );
486   }
487   else
488   {
489     g_QglTable.m_pfn_qglBindTexture( GL_TEXTURE_2D, g_pPatch->d_texture->texture_number );
490   }
491
492   g_QglTable.m_pfn_qglEnable( GL_TEXTURE_2D );
493   g_QglTable.m_pfn_qglBegin( GL_QUADS );
494   g_QglTable.m_pfn_qglTexCoord2f( g_2DView.m_Mins[0], g_2DView.m_Mins[1] );
495   g_QglTable.m_pfn_qglVertex2f( g_2DView.m_Mins[0], g_2DView.m_Mins[1] );
496   g_QglTable.m_pfn_qglTexCoord2f( g_2DView.m_Maxs[0], g_2DView.m_Mins[1] );
497   g_QglTable.m_pfn_qglVertex2f( g_2DView.m_Maxs[0], g_2DView.m_Mins[1] );
498   g_QglTable.m_pfn_qglTexCoord2f( g_2DView.m_Maxs[0], g_2DView.m_Maxs[1] );
499   g_QglTable.m_pfn_qglVertex2f( g_2DView.m_Maxs[0], g_2DView.m_Maxs[1] );
500   g_QglTable.m_pfn_qglTexCoord2f( g_2DView.m_Mins[0], g_2DView.m_Maxs[1] );
501   g_QglTable.m_pfn_qglVertex2f( g_2DView.m_Mins[0], g_2DView.m_Maxs[1] );
502   g_QglTable.m_pfn_qglEnd();
503   g_QglTable.m_pfn_qglDisable( GL_TEXTURE_2D );
504
505   if (!g_bPatch)
506   {
507     g_QglTable.m_pfn_qglBegin( GL_LINE_LOOP );
508     for ( i=0; i<g_NumPoints; i++ )
509     {
510       g_QglTable.m_pfn_qglVertex2f( g_WorkWinding.data[i][0], g_WorkWinding.data[i][1] );
511     }
512     g_QglTable.m_pfn_qglEnd();
513   }
514   else
515   {
516     g_QglTable.m_pfn_qglBegin( GL_LINES );
517     for ( i=0; i<g_pPatch->width; i++ )
518       for ( j=0; j<g_pPatch->height; j++ )
519       {
520         if ( i < g_pPatch->width-1 )
521         {
522           g_QglTable.m_pfn_qglVertex2f( g_WorkPatch.ctrl[i][j].st[0], g_WorkPatch.ctrl[i][j].st[1] );
523           g_QglTable.m_pfn_qglVertex2f( g_WorkPatch.ctrl[i+1][j].st[0], g_WorkPatch.ctrl[i+1][j].st[1] );
524         }
525
526         if ( j < g_pPatch->height-1 )
527         {
528           g_QglTable.m_pfn_qglVertex2f( g_WorkPatch.ctrl[i][j].st[0], g_WorkPatch.ctrl[i][j].st[1] );
529           g_QglTable.m_pfn_qglVertex2f( g_WorkPatch.ctrl[i][j+1].st[0], g_WorkPatch.ctrl[i][j+1].st[1] );
530         }
531       }
532     g_QglTable.m_pfn_qglEnd();
533   }
534
535   // let the control points manager render
536   g_pManager->render( );
537 }
538
539 static bool CanProcess ()
540 {
541   if (!g_bTexViewReady && !g_bClosing)
542   {
543     InitTexView (g_pToolWnd);
544
545     if (!g_bTexViewReady)
546     {
547       g_bClosing = true;
548       DoMessageBox ("You must have brush primitives activated in your project settings and\n"
549                     "have a patch or a single face selected to use the TexTool plugin.\n"
550                     "See plugins/TexToolHelp for documentation.", "TexTool plugin", MB_ICONERROR | MB_OK);
551       // decref, this will destroy
552       g_pToolWnd->DecRef();
553       g_pToolWnd = NULL;
554       return 0;
555     }
556     else
557       g_bClosing = false;
558   }
559   else if (!g_bTexViewReady && g_bClosing)
560   {
561     return 0;
562   }
563
564   return 1;
565 }
566
567 #if 0
568 static void button_press (GtkWidget *widget, GdkEventButton *event, gpointer data)
569 {
570   if (CanProcess ())
571   {
572     switch (event->button)
573     {
574     case 1:
575       g_pManager->OnLButtonDown (event->x, event->y); break;
576     case 3:
577       g_2DView.OnRButtonDown (event->x, event->y); break;
578     }
579   }
580 }
581
582 static void button_release (GtkWidget *widget, GdkEventButton *event, gpointer data)
583 {
584   if (CanProcess ())
585   {
586     switch (event->button)
587     {
588     case 1:
589       g_pManager->OnLButtonUp (event->x, event->y); break;
590     case 3:
591       g_2DView.OnRButtonUp (event->x, event->y); break;
592     }
593   }
594 }
595
596 static void motion (GtkWidget *widget, GdkEventMotion *event, gpointer data)
597 {
598   if (CanProcess ())
599   {
600     if (g_2DView.OnMouseMove (event->x, event->y))
601       return;
602
603     if (g_pManager->OnMouseMove (event->x, event->y))
604       return;
605   }
606 }
607
608 static gint expose (GtkWidget *widget, GdkEventExpose *event, gpointer data)
609 {
610   if (event->count > 0)
611     return TRUE;
612
613   if (!CanProcess ())
614     return TRUE;
615
616   if (g_bTexViewReady)
617   {
618     g_2DView.m_rect.bottom = widget->allocation.height;
619     g_2DView.m_rect.right = widget->allocation.width;
620
621     if (!g_QglTable.m_pfn_glwidget_make_current (g_pToolWidget))
622     {
623       Sys_Printf("TexTool: glMakeCurrent failed\n");
624       return TRUE;
625     }
626
627     DoExpose ();
628
629     g_QglTable.m_pfn_glwidget_swap_buffers (g_pToolWidget);
630   }
631
632   return TRUE;
633 }
634
635 static gint keypress (GtkWidget* widget, GdkEventKey* event, gpointer data)
636 {
637   unsigned int code = gdk_keyval_to_upper(event->keyval);
638
639   if (code == GDK_Escape)
640   {
641     gtk_widget_destroy (g_pToolWnd);
642     g_pToolWnd = NULL;
643     return TRUE;
644   }
645
646   if (CanProcess ())
647   {
648     if (g_2DView.OnKeyDown (code))
649       return FALSE;
650
651     if (code == GDK_Return)
652     {
653       Textool_Validate();
654       return FALSE;
655     }
656   }
657
658   return TRUE;
659 }
660
661 static gint close (GtkWidget *widget, GdkEvent* event, gpointer data)
662 {
663   gtk_widget_destroy (widget);
664   g_pToolWnd = NULL;
665
666   return TRUE;
667 }
668
669 static GtkWidget* CreateOpenGLWidget ()
670 {
671   g_pToolWidget = g_QglTable.m_pfn_glwidget_new (FALSE, g_QglTable.m_pfn_GetQeglobalsGLWidget ());
672
673   gtk_widget_set_events (g_pToolWidget, GDK_DESTROY | GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
674                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK);
675
676   // Connect signal handlers
677   gtk_signal_connect (GTK_OBJECT (g_pToolWidget), "expose_event", GTK_SIGNAL_FUNC (expose), NULL);
678   gtk_signal_connect (GTK_OBJECT (g_pToolWidget), "motion_notify_event",
679                       GTK_SIGNAL_FUNC (motion), NULL);
680   gtk_signal_connect (GTK_OBJECT (g_pToolWidget), "button_press_event",
681                       GTK_SIGNAL_FUNC (button_press), NULL);
682   gtk_signal_connect (GTK_OBJECT (g_pToolWidget), "button_release_event",
683                       GTK_SIGNAL_FUNC (button_release), NULL);
684
685   gtk_signal_connect (GTK_OBJECT (g_pToolWnd), "delete_event", GTK_SIGNAL_FUNC (close), NULL);
686   gtk_signal_connect (GTK_OBJECT (g_pToolWnd), "key_press_event",
687                       GTK_SIGNAL_FUNC (keypress), NULL);
688
689   return g_pToolWidget;
690 }
691 #endif
692
693 #if 0
694 static void DoPaint ()
695 {
696   if (!CanProcess ())
697     return;
698   
699   if (g_bTexViewReady)
700   {
701     g_2DView.m_rect.bottom = g_pToolWnd->getHeight();
702     g_2DView.m_rect.right = g_pToolWnd->getWidth();
703     
704     // set GL_PROJECTION
705     g_2DView.PreparePaint();
706     // render the objects
707     // the master is not rendered the same way, draw over a unified texture background
708     g_2DView.TextureBackground(g_DrawObjects[0].pObject->getTextureNumber());
709     if (g_nDrawObjects >= 1)
710     {
711       int i;
712       for (i=1;i<g_nDrawObjects;i++)
713       {
714         // we use a first step to the GL_MODELVIEW for the master object
715         // GL_MODELVIEW will be altered in RenderAuxiliary too
716         g_DrawObjects[0].pObject->PrepareModelView(g_DrawObjects[i].pTopo);
717         g_DrawObjects[i].pObject->RenderAuxiliary();
718       }
719     }
720     // draw the polygon outline and control points
721     g_DrawObjects[0].pObject->PrepareModelView(NULL);
722     g_DrawObjects[0].pObject->RenderUI();
723   }
724 }
725 #endif
726
727 bool CWindowListener::OnLButtonDown(guint32 nFlags, double x, double y)
728 {
729   if (CanProcess())
730   {
731     g_pManager->OnLButtonDown((int)x, (int)y);
732     return true;
733   }
734   return false;
735 }
736
737 bool CWindowListener::OnRButtonDown(guint32 nFlags, double x, double y)
738 {
739   if (CanProcess())
740   {
741     g_2DView.OnRButtonDown ((int)x, (int)y);
742     return true;
743   }
744   return false;
745 }
746
747 bool CWindowListener::OnLButtonUp(guint32 nFlags, double x, double y)
748 {
749   if (CanProcess())
750   {
751     g_pManager->OnLButtonUp((int)x, (int)y);
752     return true;
753   }
754   return false;
755 }
756
757 bool CWindowListener::OnRButtonUp(guint32 nFlags, double x, double y)
758 {
759   if (CanProcess())
760   {
761     g_2DView.OnRButtonUp ((int)x, (int)y);
762     return true;
763   }
764   return false;
765 }
766
767 bool CWindowListener::OnMouseMove(guint32 nFlags, double x, double y)
768 {
769   if (CanProcess ())
770   {
771     if (g_2DView.OnMouseMove ((int)x, (int)y))
772       return true;
773
774     g_pManager->OnMouseMove((int)x, (int)y);
775     return true;
776   }
777   return false;
778 }
779
780 // the widget is closing
781 void CWindowListener::Close()
782 {
783   g_pToolWnd = NULL;
784 }
785
786 bool CWindowListener::Paint()
787 {
788   if (!CanProcess ())
789     return false;
790
791   if (g_bTexViewReady)
792     DoExpose();
793
794   return true;
795 }
796
797 bool CWindowListener::OnKeyPressed(char *s)
798 {
799   if (!strcmp(s,"Escape"))
800   {
801     Textool_Cancel();
802     return TRUE;
803   }
804   if (CanProcess ())
805   {
806     if (g_2DView.OnKeyDown (s))
807       return TRUE;
808
809     if (!strcmp(s,"Return"))
810     {
811       Textool_Validate();
812       return TRUE;
813     }
814   }
815   return FALSE;
816 }
817
818 extern "C" void QERPlug_Dispatch(const char* p, vec3_t vMin, vec3_t vMax, bool bSingleBrush)
819 {
820   #if 0
821   // if it's the first call, perhaps we need some additional init steps
822   if (!g_bQglInitDone)
823   {
824     g_QglTable.m_nSize = sizeof(OpenGLBinding);
825     if ( g_FuncTable.m_pfnRequestInterface( QERQglTable_GUID, static_cast<LPVOID>(&g_QglTable) ) )
826     {
827       g_bQglInitDone = true;
828     }
829     else
830     {
831       Sys_Printf("TexTool plugin: OpenGLBinding interface request failed\n");
832       return;
833     }
834   }
835
836   if (!g_bSelectedFaceInitDone)
837   {
838     g_SelectedFaceTable.m_nSize = sizeof(_QERSelectedFaceTable);
839     if (g_FuncTable.m_pfnRequestInterface (QERSelectedFaceTable_GUID,
840                                            static_cast<LPVOID>(&g_SelectedFaceTable)))
841     {
842       g_bSelectedFaceInitDone = true;
843     }
844     else
845     {
846       Sys_Printf("TexTool plugin: _QERSelectedFaceTable interface request failed\n");
847       return;
848     }
849   }
850
851   if (!g_bSurfaceTableInitDone)
852   {
853     g_SurfaceTable.m_nSize = sizeof(_QERAppSurfaceTable);
854     if ( g_FuncTable.m_pfnRequestInterface( QERAppSurfaceTable_GUID, static_cast<LPVOID>(&g_SurfaceTable) ) )
855     {
856       g_bSurfaceTableInitDone = true;
857     }
858     else
859     {
860       Sys_Printf("TexTool plugin: _QERAppSurfaceTable interface request failed\n");
861       return;
862     }
863   }
864
865   if (!g_bUITable)
866   {
867     g_UITable.m_nSize = sizeof(_QERUITable);
868     if ( g_FuncTable.m_pfnRequestInterface( QERUI_GUID, static_cast<LPVOID>(&g_UITable) ) )
869     {
870       g_bUITable = true;
871     }
872     else
873     {
874       Sys_Printf("TexTool plugin: _QERUITable interface request failed\n");
875       return;
876     }
877   }
878   #endif
879
880   if (!strcmp(p, "About..."))
881   {
882     DoMessageBox (PLUGIN_ABOUT, "About ...", MB_OK );
883   }
884   else if (!strcmp(p, "Go..."))
885   {
886     if (!g_pToolWnd)
887     {
888       g_pToolWnd = g_UITable.m_pfnCreateGLWindow();
889       g_pToolWnd->setSizeParm(300,300);
890       g_pToolWnd->setName("TexTool");
891       // g_Listener is a static class, we need to bump the refCount to avoid premature release problems
892       g_Listen.IncRef();
893       // setListener will incRef on the listener too
894       g_pToolWnd->setListener(&g_Listen);
895       if (!g_pToolWnd->Show())
896       {
897         DoMessageBox ("Error creating texture tools window!", "TexTool plugin", MB_ICONERROR | MB_OK);
898         return;
899       }
900     }
901     
902     g_bTexViewReady = false;
903     g_bClosing = false;
904   }
905   else if (!strcmp(p, "Help..."))
906   {
907     if (!g_bHelp)
908       DoMessageBox ("Select a brush face (ctrl+shift+left mouse) or a patch, and hit Go...\n"
909                     "See tutorials for more", "TexTool plugin", MB_OK );
910     else
911       DoMessageBox ("Are you kidding me ?", "TexTool plugin", MB_OK );
912     g_bHelp = true;
913   }
914 }
915
916 // =============================================================================
917 // SYNAPSE
918
919 CSynapseServer* g_pSynapseServer = NULL;
920 CSynapseClientTexTool g_SynapseClient;
921
922 extern "C" CSynapseClient* SYNAPSE_DLL_EXPORT Synapse_EnumerateInterfaces (const char *version, CSynapseServer *pServer)
923 {
924   if (strcmp(version, SYNAPSE_VERSION))
925   {
926     Syn_Printf("ERROR: synapse API version mismatch: should be '" SYNAPSE_VERSION "', got '%s'\n", version);
927     return NULL;
928   }
929   g_pSynapseServer = pServer;
930   g_pSynapseServer->IncRef();
931   Set_Syn_Printf(g_pSynapseServer->Get_Syn_Printf());
932   
933   g_SynapseClient.AddAPI(PLUGIN_MAJOR, "textool", sizeof(_QERPluginTable));  
934   g_SynapseClient.AddAPI(RADIANT_MAJOR, NULL, sizeof(g_FuncTable), SYN_REQUIRE, &g_FuncTable);
935   g_SynapseClient.AddAPI(QGL_MAJOR, NULL, sizeof(g_QglTable), SYN_REQUIRE, &g_QglTable);
936   g_SynapseClient.AddAPI(SELECTEDFACE_MAJOR, NULL, sizeof(g_SelectedFaceTable), SYN_REQUIRE, &g_SelectedFaceTable);
937   
938   return &g_SynapseClient;
939 }
940
941 bool CSynapseClientTexTool::RequestAPI(APIDescriptor_t *pAPI)
942 {
943   if (!strcmp(pAPI->major_name, PLUGIN_MAJOR))
944   {
945     _QERPluginTable *pTable = static_cast<_QERPluginTable*>(pAPI->mpTable);
946     pTable->m_pfnQERPlug_Init = QERPlug_Init;
947     pTable->m_pfnQERPlug_GetName = QERPlug_GetName;
948     pTable->m_pfnQERPlug_GetCommandList = QERPlug_GetCommandList;
949     pTable->m_pfnQERPlug_Dispatch = QERPlug_Dispatch;
950     return true;
951   }
952
953   Syn_Printf("ERROR: RequestAPI( '%s' ) not found in '%s'\n", pAPI->major_name, GetInfo());
954   return false;
955 }
956
957 #include "version.h"
958
959 const char* CSynapseClientTexTool::GetInfo()
960 {
961   return "Texture Tools plugin built " __DATE__ " " RADIANT_VERSION;
962 }