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 size = (int)((winding_t *)0)->points[MAX_POINTS_ON_WINDING];
255 g_pSelectedFaceWinding = (winding_t *)malloc( size );
256 memset( g_pSelectedFaceWinding, 0, size );
257 return "Texture tools for Radiant";
260 const char* QERPlug_GetName()
262 return (char*)PLUGIN_NAME;
265 const char* QERPlug_GetCommandList()
267 return PLUGIN_COMMANDS;
270 char *TranslateString (char *buf)
272 static char buf2[32768];
278 for (i=0 ; i<l ; i++)
293 // called by InitTexView to fit the view against the bounding box of control points
294 void FitView (IWindow* hwndDlg, int TexSize[2])
296 // apply a ratio to get the area we'll draw
297 g_2DView.m_Center[0] = 0.5f * ( g_2DView.m_Mins[0] + g_2DView.m_Maxs[0] );
298 g_2DView.m_Center[1] = 0.5f * ( g_2DView.m_Mins[1] + g_2DView.m_Maxs[1] );
299 g_2DView.m_Mins[0] = g_2DView.m_Center[0] + g_ViewportRatio*( g_2DView.m_Mins[0] - g_2DView.m_Center[0] );
300 g_2DView.m_Mins[1] = g_2DView.m_Center[1] + g_ViewportRatio*( g_2DView.m_Mins[1] - g_2DView.m_Center[1] );
301 g_2DView.m_Maxs[0] = g_2DView.m_Center[0] + g_ViewportRatio*( g_2DView.m_Maxs[0] - g_2DView.m_Center[0] );
302 g_2DView.m_Maxs[1] = g_2DView.m_Center[1] + g_ViewportRatio*( g_2DView.m_Maxs[1] - g_2DView.m_Center[1] );
304 g_2DView.m_rect.left = 0;
305 g_2DView.m_rect.top = 0;
306 g_2DView.m_rect.bottom = hwndDlg->getHeight();
307 g_2DView.m_rect.right = hwndDlg->getWidth();
309 // we need to draw this area, now compute a bigger area so the texture scale is the same along X and Y
310 // compute box shape in XY space, let's say X <-> S we'll get a ratio for Y:
313 g_SelectedFaceTable.m_pfnGetTextureSize( 0, TexSize );
317 TexSize[0] = g_pPatch->d_texture->width;
318 TexSize[1] = g_pPatch->d_texture->height;
320 // we want a texture with the same X / Y ratio
321 // compute XY space / window size ratio
322 float SSize = (float)fabs( g_2DView.m_Maxs[0] - g_2DView.m_Mins[0] );
323 float TSize = (float)fabs( g_2DView.m_Maxs[1] - g_2DView.m_Mins[1] );
324 float XSize = TexSize[0] * SSize;
325 float YSize = TexSize[1] * TSize;
326 float RatioX = XSize / (float)abs( g_2DView.m_rect.left - g_2DView.m_rect.right );
327 float RatioY = YSize / (float)abs( g_2DView.m_rect.top - g_2DView.m_rect.bottom );
328 if ( RatioX > RatioY )
330 YSize = (float)abs( g_2DView.m_rect.top - g_2DView.m_rect.bottom ) * RatioX;
331 TSize = YSize / (float)TexSize[1];
335 XSize = (float)abs( g_2DView.m_rect.left - g_2DView.m_rect.right ) * RatioY;
336 SSize = XSize / (float)TexSize[0];
338 g_2DView.m_Mins[0] = g_2DView.m_Center[0] - 0.5f * SSize;
339 g_2DView.m_Maxs[0] = g_2DView.m_Center[0] + 0.5f * SSize;
340 g_2DView.m_Mins[1] = g_2DView.m_Center[1] - 0.5f * TSize;
341 g_2DView.m_Maxs[1] = g_2DView.m_Center[1] + 0.5f * TSize;
344 // call this one each time we need to re-init
345 //++timo TODO: re-init objects state, g_2DView and g_ControlPointsManager
346 void InitTexView( IWindow* hwndDlg )
348 // size of the texture we are working on
350 g_bTexViewReady = false;
351 if (g_SelectedFaceTable.m_pfnGetSelectedFaceCount() != 0)
353 g_SelectedFaceTable.m_pfnGetFaceInfo( 0, &g_SelectedFaceData, g_pSelectedFaceWinding );
356 // we have something selected
357 // setup: compute BBox for the winding ( in ST space )
358 //++timo FIXME: move this in a C2DView member ? used as well for patches
359 g_2DView.m_Mins[0] = +9999.0f; g_2DView.m_Mins[1] = +9999.0f;
360 g_2DView.m_Maxs[0] = -9999.0f; g_2DView.m_Maxs[1] = -9999.0f;
361 for ( i=0; i<g_pSelectedFaceWinding->numpoints; i++ )
363 if ( g_pSelectedFaceWinding->points[i][3] < g_2DView.m_Mins[0] )
364 g_2DView.m_Mins[0] = g_pSelectedFaceWinding->points[i][3];
365 if ( g_pSelectedFaceWinding->points[i][3] > g_2DView.m_Maxs[0] )
366 g_2DView.m_Maxs[0] = g_pSelectedFaceWinding->points[i][3];
367 if ( g_pSelectedFaceWinding->points[i][4] < g_2DView.m_Mins[1] )
368 g_2DView.m_Mins[1] = g_pSelectedFaceWinding->points[i][4];
369 if ( g_pSelectedFaceWinding->points[i][4] > g_2DView.m_Maxs[1] )
370 g_2DView.m_Maxs[1] = g_pSelectedFaceWinding->points[i][4];
372 // NOTE: FitView will read and init TexSize
373 FitView( hwndDlg, TexSize );
374 // now init the work tables
375 g_NumPoints = g_pSelectedFaceWinding->numpoints;
376 for ( i=0; i<g_NumPoints; i++ )
378 g_WorkWinding.data[i][0] = g_pSelectedFaceWinding->points[i][3];
379 g_WorkWinding.data[i][1] = g_pSelectedFaceWinding->points[i][4];
381 g_ControlPointsBFace.Init( g_NumPoints, &g_WorkWinding, &g_2DView, TexSize, &g_SelectedFaceData, &g_QglTable );
384 fTexStep[0] = 1.0f / float(TexSize[0]);
385 fTexStep[1] = 1.0f / float(TexSize[1]);
386 g_2DView.SetGrid( fTexStep[0], fTexStep[1] );
387 g_pManager = &g_ControlPointsBFace;
388 // prepare the "Cancel" data
389 memcpy( &g_CancelFaceData, &g_SelectedFaceData, sizeof(_QERFaceData) );
391 g_bTexViewReady = true;
393 else if ( g_SurfaceTable.m_pfnAnyPatchesSelected())
395 g_pPatch = g_SurfaceTable.m_pfnGetSelectedPatch();
398 // compute BBox for all patch points
399 g_2DView.m_Mins[0] = +9999.0f; g_2DView.m_Mins[1] = +9999.0f;
400 g_2DView.m_Maxs[0] = -9999.0f; g_2DView.m_Maxs[1] = -9999.0f;
401 for ( i=0; i<g_pPatch->width; i++ )
403 for ( j=0; j<g_pPatch->height; j++ )
405 if ( g_pPatch->ctrl[i][j].st[0] < g_2DView.m_Mins[0] )
406 g_2DView.m_Mins[0] = g_pPatch->ctrl[i][j].st[0];
407 if ( g_pPatch->ctrl[i][j].st[0] > g_2DView.m_Maxs[0] )
408 g_2DView.m_Maxs[0] = g_pPatch->ctrl[i][j].st[0];
409 if ( g_pPatch->ctrl[i][j].st[1] < g_2DView.m_Mins[1] )
410 g_2DView.m_Mins[1] = g_pPatch->ctrl[i][j].st[1];
411 if ( g_pPatch->ctrl[i][j].st[1] > g_2DView.m_Maxs[1] )
412 g_2DView.m_Maxs[1] = g_pPatch->ctrl[i][j].st[1];
415 FitView( hwndDlg, TexSize);
416 // init the work tables
417 g_WorkPatch = *g_pPatch;
418 g_ControlPointsPatch.Init( &g_WorkPatch, &g_2DView, &g_QglTable, g_pPatch );
421 fTexStep[0] = 1.0f / float(TexSize[0]);
422 fTexStep[1] = 1.0f / float(TexSize[1]);
423 g_2DView.SetGrid( fTexStep[0], fTexStep[1] );
424 g_pManager = &g_ControlPointsPatch;
425 // prepare the "cancel" data
426 g_CancelPatch = *g_pPatch;
428 g_bTexViewReady = true;
432 void Textool_Validate()
434 // validate current situation into the main view
435 g_pManager->Commit( );
436 // for a brush face we have an aditionnal step
439 // tell Radiant to update (will also send update windows messages )
440 g_SelectedFaceTable.m_pfnSetFaceInfo( 0, &g_SelectedFaceData );
444 // ask to rebuild the patch display data
445 g_pPatch->bDirty = true;
446 // send a repaint to the camera window as well
447 g_FuncTable.m_pfnSysUpdateWindows( W_CAMERA );
449 // we'll need to update after that as well:
450 g_bTexViewReady = false;
451 // send a repaint message
452 g_pToolWnd->Redraw ();
455 void Textool_Cancel()
459 // tell Radiant to update (will also send update windows messages )
460 g_SelectedFaceTable.m_pfnSetFaceInfo( 0, &g_CancelFaceData );
464 *g_pPatch = g_CancelPatch;
465 g_pPatch->bDirty = true;
466 g_FuncTable.m_pfnSysUpdateWindows( W_CAMERA );
468 // do not call destroy, decref it
469 g_pToolWnd->DecRef();
473 static void DoExpose ()
477 g_2DView.PreparePaint();
478 g_QglTable.m_pfn_qglColor3f(1, 1, 1);
479 // draw the texture background
480 g_QglTable.m_pfn_qglPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
483 g_QglTable.m_pfn_qglBindTexture( GL_TEXTURE_2D, g_SelectedFaceTable.m_pfnGetTextureNumber(0) );
487 g_QglTable.m_pfn_qglBindTexture( GL_TEXTURE_2D, g_pPatch->d_texture->texture_number );
490 g_QglTable.m_pfn_qglEnable( GL_TEXTURE_2D );
491 g_QglTable.m_pfn_qglBegin( GL_QUADS );
492 g_QglTable.m_pfn_qglTexCoord2f( g_2DView.m_Mins[0], g_2DView.m_Mins[1] );
493 g_QglTable.m_pfn_qglVertex2f( g_2DView.m_Mins[0], g_2DView.m_Mins[1] );
494 g_QglTable.m_pfn_qglTexCoord2f( g_2DView.m_Maxs[0], g_2DView.m_Mins[1] );
495 g_QglTable.m_pfn_qglVertex2f( g_2DView.m_Maxs[0], g_2DView.m_Mins[1] );
496 g_QglTable.m_pfn_qglTexCoord2f( g_2DView.m_Maxs[0], g_2DView.m_Maxs[1] );
497 g_QglTable.m_pfn_qglVertex2f( g_2DView.m_Maxs[0], g_2DView.m_Maxs[1] );
498 g_QglTable.m_pfn_qglTexCoord2f( g_2DView.m_Mins[0], g_2DView.m_Maxs[1] );
499 g_QglTable.m_pfn_qglVertex2f( g_2DView.m_Mins[0], g_2DView.m_Maxs[1] );
500 g_QglTable.m_pfn_qglEnd();
501 g_QglTable.m_pfn_qglDisable( GL_TEXTURE_2D );
505 g_QglTable.m_pfn_qglBegin( GL_LINE_LOOP );
506 for ( i=0; i<g_NumPoints; i++ )
508 g_QglTable.m_pfn_qglVertex2f( g_WorkWinding.data[i][0], g_WorkWinding.data[i][1] );
510 g_QglTable.m_pfn_qglEnd();
514 g_QglTable.m_pfn_qglBegin( GL_LINES );
515 for ( i=0; i<g_pPatch->width; i++ )
516 for ( j=0; j<g_pPatch->height; j++ )
518 if ( i < g_pPatch->width-1 )
520 g_QglTable.m_pfn_qglVertex2f( g_WorkPatch.ctrl[i][j].st[0], g_WorkPatch.ctrl[i][j].st[1] );
521 g_QglTable.m_pfn_qglVertex2f( g_WorkPatch.ctrl[i+1][j].st[0], g_WorkPatch.ctrl[i+1][j].st[1] );
524 if ( j < g_pPatch->height-1 )
526 g_QglTable.m_pfn_qglVertex2f( g_WorkPatch.ctrl[i][j].st[0], g_WorkPatch.ctrl[i][j].st[1] );
527 g_QglTable.m_pfn_qglVertex2f( g_WorkPatch.ctrl[i][j+1].st[0], g_WorkPatch.ctrl[i][j+1].st[1] );
530 g_QglTable.m_pfn_qglEnd();
533 // let the control points manager render
534 g_pManager->Render( );
537 static bool CanProcess ()
539 if (!g_bTexViewReady && !g_bClosing)
541 InitTexView (g_pToolWnd);
543 if (!g_bTexViewReady)
546 DoMessageBox ("You must have brush primitives activated in your project settings and\n"
547 "have a patch or a single face selected to use the TexTool plugin.\n"
548 "See plugins/TexToolHelp for documentation.", "TexTool plugin", MB_ICONERROR | MB_OK);
549 // decref, this will destroy
550 g_pToolWnd->DecRef();
557 else if (!g_bTexViewReady && g_bClosing)
566 static void button_press (GtkWidget *widget, GdkEventButton *event, gpointer data)
570 switch (event->button)
573 g_pManager->OnLButtonDown (event->x, event->y); break;
575 g_2DView.OnRButtonDown (event->x, event->y); break;
580 static void button_release (GtkWidget *widget, GdkEventButton *event, gpointer data)
584 switch (event->button)
587 g_pManager->OnLButtonUp (event->x, event->y); break;
589 g_2DView.OnRButtonUp (event->x, event->y); break;
594 static void motion (GtkWidget *widget, GdkEventMotion *event, gpointer data)
598 if (g_2DView.OnMouseMove (event->x, event->y))
601 if (g_pManager->OnMouseMove (event->x, event->y))
606 static gint expose (GtkWidget *widget, GdkEventExpose *event, gpointer data)
608 if (event->count > 0)
616 g_2DView.m_rect.bottom = widget->allocation.height;
617 g_2DView.m_rect.right = widget->allocation.width;
619 if (!g_QglTable.m_pfn_glwidget_make_current (g_pToolWidget))
621 Sys_Printf("TexTool: glMakeCurrent failed\n");
627 g_QglTable.m_pfn_glwidget_swap_buffers (g_pToolWidget);
633 static gint keypress (GtkWidget* widget, GdkEventKey* event, gpointer data)
635 unsigned int code = gdk_keyval_to_upper(event->keyval);
637 if (code == GDK_Escape)
639 gtk_widget_destroy (g_pToolWnd);
646 if (g_2DView.OnKeyDown (code))
649 if (code == GDK_Return)
659 static gint close (GtkWidget *widget, GdkEvent* event, gpointer data)
661 gtk_widget_destroy (widget);
667 static GtkWidget* CreateOpenGLWidget ()
669 g_pToolWidget = g_QglTable.m_pfn_glwidget_new (FALSE, g_QglTable.m_pfn_GetQeglobalsGLWidget ());
671 gtk_widget_set_events (g_pToolWidget, GDK_DESTROY | GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
672 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK);
674 // Connect signal handlers
675 gtk_signal_connect (GTK_OBJECT (g_pToolWidget), "expose_event", GTK_SIGNAL_FUNC (expose), NULL);
676 gtk_signal_connect (GTK_OBJECT (g_pToolWidget), "motion_notify_event",
677 GTK_SIGNAL_FUNC (motion), NULL);
678 gtk_signal_connect (GTK_OBJECT (g_pToolWidget), "button_press_event",
679 GTK_SIGNAL_FUNC (button_press), NULL);
680 gtk_signal_connect (GTK_OBJECT (g_pToolWidget), "button_release_event",
681 GTK_SIGNAL_FUNC (button_release), NULL);
683 gtk_signal_connect (GTK_OBJECT (g_pToolWnd), "delete_event", GTK_SIGNAL_FUNC (close), NULL);
684 gtk_signal_connect (GTK_OBJECT (g_pToolWnd), "key_press_event",
685 GTK_SIGNAL_FUNC (keypress), NULL);
687 return g_pToolWidget;
692 static void DoPaint ()
699 g_2DView.m_rect.bottom = g_pToolWnd->getHeight();
700 g_2DView.m_rect.right = g_pToolWnd->getWidth();
703 g_2DView.PreparePaint();
704 // render the objects
705 // the master is not rendered the same way, draw over a unified texture background
706 g_2DView.TextureBackground(g_DrawObjects[0].pObject->getTextureNumber());
707 if (g_nDrawObjects >= 1)
710 for (i=1;i<g_nDrawObjects;i++)
712 // we use a first step to the GL_MODELVIEW for the master object
713 // GL_MODELVIEW will be altered in RenderAuxiliary too
714 g_DrawObjects[0].pObject->PrepareModelView(g_DrawObjects[i].pTopo);
715 g_DrawObjects[i].pObject->RenderAuxiliary();
718 // draw the polygon outline and control points
719 g_DrawObjects[0].pObject->PrepareModelView(NULL);
720 g_DrawObjects[0].pObject->RenderUI();
725 bool CWindowListener::OnLButtonDown(guint32 nFlags, double x, double y)
729 g_pManager->OnLButtonDown((int)x, (int)y);
735 bool CWindowListener::OnRButtonDown(guint32 nFlags, double x, double y)
739 g_2DView.OnRButtonDown ((int)x, (int)y);
745 bool CWindowListener::OnLButtonUp(guint32 nFlags, double x, double y)
749 g_pManager->OnLButtonUp((int)x, (int)y);
755 bool CWindowListener::OnRButtonUp(guint32 nFlags, double x, double y)
759 g_2DView.OnRButtonUp ((int)x, (int)y);
765 bool CWindowListener::OnMouseMove(guint32 nFlags, double x, double y)
769 if (g_2DView.OnMouseMove ((int)x, (int)y))
772 g_pManager->OnMouseMove((int)x, (int)y);
778 // the widget is closing
779 void CWindowListener::Close()
784 bool CWindowListener::Paint()
795 bool CWindowListener::OnKeyPressed(char *s)
797 if (!strcmp(s,"Escape"))
804 if (g_2DView.OnKeyDown (s))
807 if (!strcmp(s,"Return"))
816 extern "C" void QERPlug_Dispatch(const char* p, vec3_t vMin, vec3_t vMax, bool bSingleBrush)
819 // if it's the first call, perhaps we need some additional init steps
822 g_QglTable.m_nSize = sizeof(_QERQglTable);
823 if ( g_FuncTable.m_pfnRequestInterface( QERQglTable_GUID, static_cast<LPVOID>(&g_QglTable) ) )
825 g_bQglInitDone = true;
829 Sys_Printf("TexTool plugin: _QERQglTable interface request failed\n");
834 if (!g_bSelectedFaceInitDone)
836 g_SelectedFaceTable.m_nSize = sizeof(_QERSelectedFaceTable);
837 if (g_FuncTable.m_pfnRequestInterface (QERSelectedFaceTable_GUID,
838 static_cast<LPVOID>(&g_SelectedFaceTable)))
840 g_bSelectedFaceInitDone = true;
844 Sys_Printf("TexTool plugin: _QERSelectedFaceTable interface request failed\n");
849 if (!g_bSurfaceTableInitDone)
851 g_SurfaceTable.m_nSize = sizeof(_QERAppSurfaceTable);
852 if ( g_FuncTable.m_pfnRequestInterface( QERAppSurfaceTable_GUID, static_cast<LPVOID>(&g_SurfaceTable) ) )
854 g_bSurfaceTableInitDone = true;
858 Sys_Printf("TexTool plugin: _QERAppSurfaceTable interface request failed\n");
865 g_UITable.m_nSize = sizeof(_QERUITable);
866 if ( g_FuncTable.m_pfnRequestInterface( QERUI_GUID, static_cast<LPVOID>(&g_UITable) ) )
872 Sys_Printf("TexTool plugin: _QERUITable interface request failed\n");
878 if (!strcmp(p, "About..."))
880 DoMessageBox (PLUGIN_ABOUT, "About ...", MB_OK );
882 else if (!strcmp(p, "Go..."))
886 g_pToolWnd = g_UITable.m_pfnCreateGLWindow();
887 g_pToolWnd->setSizeParm(300,300);
888 g_pToolWnd->setName("TexTool");
889 // g_Listener is a static class, we need to bump the refCount to avoid premature release problems
891 // setListener will incRef on the listener too
892 g_pToolWnd->setListener(&g_Listen);
893 if (!g_pToolWnd->Show())
895 DoMessageBox ("Error creating texture tools window!", "TexTool plugin", MB_ICONERROR | MB_OK);
900 g_bTexViewReady = false;
903 else if (!strcmp(p, "Help..."))
906 DoMessageBox ("Select a brush face (ctrl+shift+left mouse) or a patch, and hit Go...\n"
907 "See tutorials for more", "TexTool plugin", MB_OK );
909 DoMessageBox ("Are you kidding me ?", "TexTool plugin", MB_OK );
914 // =============================================================================
917 CSynapseServer* g_pSynapseServer = NULL;
918 CSynapseClientTexTool g_SynapseClient;
921 #pragma GCC visibility push(default)
923 extern "C" CSynapseClient* SYNAPSE_DLL_EXPORT Synapse_EnumerateInterfaces( const char *version, CSynapseServer *pServer ) {
925 #pragma GCC visibility pop
927 if (strcmp(version, SYNAPSE_VERSION))
929 Syn_Printf("ERROR: synapse API version mismatch: should be '" SYNAPSE_VERSION "', got '%s'\n", version);
932 g_pSynapseServer = pServer;
933 g_pSynapseServer->IncRef();
934 Set_Syn_Printf(g_pSynapseServer->Get_Syn_Printf());
936 g_SynapseClient.AddAPI(PLUGIN_MAJOR, "textool", sizeof(_QERPluginTable));
937 g_SynapseClient.AddAPI(RADIANT_MAJOR, NULL, sizeof(g_FuncTable), SYN_REQUIRE, &g_FuncTable);
938 g_SynapseClient.AddAPI(QGL_MAJOR, NULL, sizeof(g_QglTable), SYN_REQUIRE, &g_QglTable);
939 g_SynapseClient.AddAPI(SELECTEDFACE_MAJOR, NULL, sizeof(g_SelectedFaceTable), SYN_REQUIRE, &g_SelectedFaceTable);
940 g_SynapseClient.AddAPI(UI_MAJOR, NULL, sizeof(_QERUITable), SYN_REQUIRE, &g_UITable);
941 g_SynapseClient.AddAPI(APPSURFACEDIALOG_MAJOR, NULL, sizeof(_QERAppSurfaceTable), SYN_REQUIRE, &g_SurfaceTable);
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;