2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
22 //-----------------------------------------------------------------------------
25 // main plugin implementation
26 // texturing tools for Radiant
31 static void dialog_button_callback (GtkWidget *widget, gpointer data)
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");
44 static gint dialog_delete_callback (GtkWidget *widget, GdkEvent* event, gpointer data)
48 gtk_widget_hide (widget);
49 loop = (int*)g_object_get_data (G_OBJECT (widget), "loop");
55 int DoMessageBox (const char* lpText, const char* lpCaption, guint32 uType)
57 GtkWidget *window, *w, *vbox, *hbox;
58 int mode = (uType & MB_TYPEMASK), ret, loop = 1;
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);
71 vbox = gtk_vbox_new (FALSE, 10);
72 gtk_container_add (GTK_CONTAINER (window), vbox);
73 gtk_widget_show (vbox);
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);
80 w = gtk_hseparator_new ();
81 gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 2);
84 hbox = gtk_hbox_new (FALSE, 10);
85 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 2);
86 gtk_widget_show (hbox);
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);
99 else if (mode == MB_OKCANCEL)
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);
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));
116 else if (mode == MB_YESNOCANCEL)
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);
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));
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));
139 else /* if (mode == MB_YESNO) */
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);
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));
157 gtk_widget_show (window);
158 gtk_grab_add (window);
161 gtk_main_iteration ();
163 gtk_grab_remove (window);
164 gtk_widget_destroy (window);
169 // Radiant function table
170 _QERFuncTable_1 g_FuncTable;
173 const char *PLUGIN_NAME = "Q3 Texture Tools";
175 // commands in the menu
176 const char *PLUGIN_COMMANDS = "About...;Go...";
178 // cast to GtkWidget*
180 IWindow *g_pToolWnd = NULL; // handle to the window
181 CWindowListener g_Listen;
183 // plugin interfaces ---------------------------
184 bool g_bQglInitDone = false;
185 _QERQglTable g_QglTable;
186 bool g_bSelectedFaceInitDone = false;
187 _QERSelectedFaceTable g_SelectedFaceTable;
188 bool g_bUITable = false;
189 _QERUITable g_UITable;
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
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
205 CtrlPts_t g_WorkWinding;
206 // reference _QERFaceData we use on Cancel, and for Commit
207 _QERFaceData g_CancelFaceData;
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;
223 // ---------------------------------------------
224 // holds the manager we are currently using
225 CControlPointsManager *g_pManager = NULL;
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;
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;
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)";
243 extern "C" void* WINAPI QERPlug_GetFuncTable ()
248 const char* QERPlug_Init (void* hApp, void *pWidget)
251 GtkWidget* pMainWidget = static_cast<GtkWidget*>(pWidget);
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";
262 const char* QERPlug_GetName()
264 return (char*)PLUGIN_NAME;
267 const char* QERPlug_GetCommandList()
269 return PLUGIN_COMMANDS;
272 char *TranslateString (char *buf)
274 static char buf2[32768];
280 for (i=0 ; i<l ; i++)
295 // called by InitTexView to fit the view against the bounding box of control points
296 void FitView (IWindow* hwndDlg, int TexSize[2])
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] );
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();
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:
315 g_SelectedFaceTable.m_pfnGetTextureSize( 0, TexSize );
319 TexSize[0] = g_pPatch->d_texture->width;
320 TexSize[1] = g_pPatch->d_texture->height;
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 )
332 YSize = (float)abs( g_2DView.m_rect.top - g_2DView.m_rect.bottom ) * RatioX;
333 TSize = YSize / (float)TexSize[1];
337 XSize = (float)abs( g_2DView.m_rect.left - g_2DView.m_rect.right ) * RatioY;
338 SSize = XSize / (float)TexSize[0];
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;
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 )
350 // size of the texture we are working on
352 g_bTexViewReady = false;
353 if (g_SelectedFaceTable.m_pfnGetSelectedFaceCount() != 0)
355 g_SelectedFaceTable.m_pfnGetFaceInfo( 0, &g_SelectedFaceData, g_pSelectedFaceWinding );
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++ )
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];
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++ )
380 g_WorkWinding.data[i][0] = g_pSelectedFaceWinding->points[i][3];
381 g_WorkWinding.data[i][1] = g_pSelectedFaceWinding->points[i][4];
383 g_ControlPointsBFace.Init( g_NumPoints, &g_WorkWinding, &g_2DView, TexSize, &g_SelectedFaceData, &g_QglTable );
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) );
393 g_bTexViewReady = true;
395 else if ( g_SurfaceTable.m_pfnAnyPatchesSelected())
397 g_pPatch = g_SurfaceTable.m_pfnGetSelectedPatch();
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++ )
405 for ( j=0; j<g_pPatch->height; j++ )
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];
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 );
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;
430 g_bTexViewReady = true;
434 void Textool_Validate()
436 // validate current situation into the main view
437 g_pManager->Commit( );
438 // for a brush face we have an aditionnal step
441 // tell Radiant to update (will also send update windows messages )
442 g_SelectedFaceTable.m_pfnSetFaceInfo( 0, &g_SelectedFaceData );
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 );
451 // we'll need to update after that as well:
452 g_bTexViewReady = false;
453 // send a repaint message
454 g_pToolWnd->Redraw ();
457 void Textool_Cancel()
461 // tell Radiant to update (will also send update windows messages )
462 g_SelectedFaceTable.m_pfnSetFaceInfo( 0, &g_CancelFaceData );
466 *g_pPatch = g_CancelPatch;
467 g_pPatch->bDirty = true;
468 g_FuncTable.m_pfnSysUpdateWindows( W_CAMERA );
470 // do not call destroy, decref it
471 g_pToolWnd->DecRef();
475 static void DoExpose ()
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 );
485 g_QglTable.m_pfn_qglBindTexture( GL_TEXTURE_2D, g_SelectedFaceTable.m_pfnGetTextureNumber(0) );
489 g_QglTable.m_pfn_qglBindTexture( GL_TEXTURE_2D, g_pPatch->d_texture->texture_number );
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 );
507 g_QglTable.m_pfn_qglBegin( GL_LINE_LOOP );
508 for ( i=0; i<g_NumPoints; i++ )
510 g_QglTable.m_pfn_qglVertex2f( g_WorkWinding.data[i][0], g_WorkWinding.data[i][1] );
512 g_QglTable.m_pfn_qglEnd();
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++ )
520 if ( i < g_pPatch->width-1 )
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] );
526 if ( j < g_pPatch->height-1 )
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] );
532 g_QglTable.m_pfn_qglEnd();
535 // let the control points manager render
536 g_pManager->Render( );
539 static bool CanProcess ()
541 if (!g_bTexViewReady && !g_bClosing)
543 InitTexView (g_pToolWnd);
545 if (!g_bTexViewReady)
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();
559 else if (!g_bTexViewReady && g_bClosing)
568 static void button_press (GtkWidget *widget, GdkEventButton *event, gpointer data)
572 switch (event->button)
575 g_pManager->OnLButtonDown (event->x, event->y); break;
577 g_2DView.OnRButtonDown (event->x, event->y); break;
582 static void button_release (GtkWidget *widget, GdkEventButton *event, gpointer data)
586 switch (event->button)
589 g_pManager->OnLButtonUp (event->x, event->y); break;
591 g_2DView.OnRButtonUp (event->x, event->y); break;
596 static void motion (GtkWidget *widget, GdkEventMotion *event, gpointer data)
600 if (g_2DView.OnMouseMove (event->x, event->y))
603 if (g_pManager->OnMouseMove (event->x, event->y))
608 static gint expose (GtkWidget *widget, GdkEventExpose *event, gpointer data)
610 if (event->count > 0)
618 g_2DView.m_rect.bottom = widget->allocation.height;
619 g_2DView.m_rect.right = widget->allocation.width;
621 if (!g_QglTable.m_pfn_glwidget_make_current (g_pToolWidget))
623 Sys_Printf("TexTool: glMakeCurrent failed\n");
629 g_QglTable.m_pfn_glwidget_swap_buffers (g_pToolWidget);
635 static gint keypress (GtkWidget* widget, GdkEventKey* event, gpointer data)
637 unsigned int code = gdk_keyval_to_upper(event->keyval);
639 if (code == GDK_Escape)
641 gtk_widget_destroy (g_pToolWnd);
648 if (g_2DView.OnKeyDown (code))
651 if (code == GDK_Return)
661 static gint close (GtkWidget *widget, GdkEvent* event, gpointer data)
663 gtk_widget_destroy (widget);
669 static GtkWidget* CreateOpenGLWidget ()
671 g_pToolWidget = g_QglTable.m_pfn_glwidget_new (FALSE, g_QglTable.m_pfn_GetQeglobalsGLWidget ());
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);
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);
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);
689 return g_pToolWidget;
694 static void DoPaint ()
701 g_2DView.m_rect.bottom = g_pToolWnd->getHeight();
702 g_2DView.m_rect.right = g_pToolWnd->getWidth();
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)
712 for (i=1;i<g_nDrawObjects;i++)
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();
720 // draw the polygon outline and control points
721 g_DrawObjects[0].pObject->PrepareModelView(NULL);
722 g_DrawObjects[0].pObject->RenderUI();
727 bool CWindowListener::OnLButtonDown(guint32 nFlags, double x, double y)
731 g_pManager->OnLButtonDown((int)x, (int)y);
737 bool CWindowListener::OnRButtonDown(guint32 nFlags, double x, double y)
741 g_2DView.OnRButtonDown ((int)x, (int)y);
747 bool CWindowListener::OnLButtonUp(guint32 nFlags, double x, double y)
751 g_pManager->OnLButtonUp((int)x, (int)y);
757 bool CWindowListener::OnRButtonUp(guint32 nFlags, double x, double y)
761 g_2DView.OnRButtonUp ((int)x, (int)y);
767 bool CWindowListener::OnMouseMove(guint32 nFlags, double x, double y)
771 if (g_2DView.OnMouseMove ((int)x, (int)y))
774 g_pManager->OnMouseMove((int)x, (int)y);
780 // the widget is closing
781 void CWindowListener::Close()
786 bool CWindowListener::Paint()
797 bool CWindowListener::OnKeyPressed(char *s)
799 if (!strcmp(s,"Escape"))
806 if (g_2DView.OnKeyDown (s))
809 if (!strcmp(s,"Return"))
818 extern "C" void QERPlug_Dispatch(const char* p, vec3_t vMin, vec3_t vMax, bool bSingleBrush)
821 // if it's the first call, perhaps we need some additional init steps
824 g_QglTable.m_nSize = sizeof(_QERQglTable);
825 if ( g_FuncTable.m_pfnRequestInterface( QERQglTable_GUID, static_cast<LPVOID>(&g_QglTable) ) )
827 g_bQglInitDone = true;
831 Sys_Printf("TexTool plugin: _QERQglTable interface request failed\n");
836 if (!g_bSelectedFaceInitDone)
838 g_SelectedFaceTable.m_nSize = sizeof(_QERSelectedFaceTable);
839 if (g_FuncTable.m_pfnRequestInterface (QERSelectedFaceTable_GUID,
840 static_cast<LPVOID>(&g_SelectedFaceTable)))
842 g_bSelectedFaceInitDone = true;
846 Sys_Printf("TexTool plugin: _QERSelectedFaceTable interface request failed\n");
851 if (!g_bSurfaceTableInitDone)
853 g_SurfaceTable.m_nSize = sizeof(_QERAppSurfaceTable);
854 if ( g_FuncTable.m_pfnRequestInterface( QERAppSurfaceTable_GUID, static_cast<LPVOID>(&g_SurfaceTable) ) )
856 g_bSurfaceTableInitDone = true;
860 Sys_Printf("TexTool plugin: _QERAppSurfaceTable interface request failed\n");
867 g_UITable.m_nSize = sizeof(_QERUITable);
868 if ( g_FuncTable.m_pfnRequestInterface( QERUI_GUID, static_cast<LPVOID>(&g_UITable) ) )
874 Sys_Printf("TexTool plugin: _QERUITable interface request failed\n");
880 if (!strcmp(p, "About..."))
882 DoMessageBox (PLUGIN_ABOUT, "About ...", MB_OK );
884 else if (!strcmp(p, "Go..."))
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
893 // setListener will incRef on the listener too
894 g_pToolWnd->setListener(&g_Listen);
895 if (!g_pToolWnd->Show())
897 DoMessageBox ("Error creating texture tools window!", "TexTool plugin", MB_ICONERROR | MB_OK);
902 g_bTexViewReady = false;
905 else if (!strcmp(p, "Help..."))
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 );
911 DoMessageBox ("Are you kidding me ?", "TexTool plugin", MB_OK );
916 // =============================================================================
919 CSynapseServer* g_pSynapseServer = NULL;
920 CSynapseClientTexTool g_SynapseClient;
923 #pragma GCC visibility push(default)
925 extern "C" CSynapseClient* SYNAPSE_DLL_EXPORT Synapse_EnumerateInterfaces( const char *version, CSynapseServer *pServer ) {
927 #pragma GCC visibility pop
929 if (strcmp(version, SYNAPSE_VERSION))
931 Syn_Printf("ERROR: synapse API version mismatch: should be '" SYNAPSE_VERSION "', got '%s'\n", version);
934 g_pSynapseServer = pServer;
935 g_pSynapseServer->IncRef();
936 Set_Syn_Printf(g_pSynapseServer->Get_Syn_Printf());
938 g_SynapseClient.AddAPI(PLUGIN_MAJOR, "textool", sizeof(_QERPluginTable));
939 g_SynapseClient.AddAPI(RADIANT_MAJOR, NULL, sizeof(g_FuncTable), SYN_REQUIRE, &g_FuncTable);
940 g_SynapseClient.AddAPI(QGL_MAJOR, NULL, sizeof(g_QglTable), SYN_REQUIRE, &g_QglTable);
941 g_SynapseClient.AddAPI(SELECTEDFACE_MAJOR, NULL, sizeof(g_SelectedFaceTable), SYN_REQUIRE, &g_SelectedFaceTable);
943 return &g_SynapseClient;
946 bool CSynapseClientTexTool::RequestAPI(APIDescriptor_t *pAPI)
948 if (!strcmp(pAPI->major_name, PLUGIN_MAJOR))
950 _QERPluginTable *pTable = static_cast<_QERPluginTable*>(pAPI->mpTable);
951 pTable->m_pfnQERPlug_Init = QERPlug_Init;
952 pTable->m_pfnQERPlug_GetName = QERPlug_GetName;
953 pTable->m_pfnQERPlug_GetCommandList = QERPlug_GetCommandList;
954 pTable->m_pfnQERPlug_Dispatch = QERPlug_Dispatch;
958 Syn_Printf("ERROR: RequestAPI( '%s' ) not found in '%s'\n", pAPI->major_name, GetInfo());
964 const char* CSynapseClientTexTool::GetInfo()
966 return "Texture Tools plugin built " __DATE__ " " RADIANT_VERSION;