]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/patchmanip.cpp
Radiant:
[xonotic/netradiant.git] / radiant / patchmanip.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 #include "patchmanip.h"
23
24 #include "debugging/debugging.h"
25
26
27 #include "iselection.h"
28 #include "ipatch.h"
29
30 #include "math/vector.h"
31 #include "math/aabb.h"
32 #include "generic/callback.h"
33
34 #include "gtkutil/menu.h"
35 #include "gtkutil/image.h"
36 #include "map.h"
37 #include "mainframe.h"
38 #include "commands.h"
39 #include "gtkmisc.h"
40 #include "gtkdlgs.h"
41 #include "texwindow.h"
42 #include "xywindow.h"
43 #include "select.h"
44 #include "patch.h"
45 #include "grid.h"
46
47 PatchCreator* g_patchCreator = 0;
48
49 void Scene_PatchConstructPrefab( scene::Graph& graph, const AABB aabb, const char* shader, EPatchPrefab eType, int axis, std::size_t width = 3, std::size_t height = 3 ){
50         Select_Delete();
51         GlobalSelectionSystem().setSelectedAll( false );
52
53         NodeSmartReference node( g_patchCreator->createPatch() );
54         Node_getTraversable( Map_FindOrInsertWorldspawn( g_map ) )->insert( node );
55
56         Patch* patch = Node_getPatch( node );
57         patch->SetShader( shader );
58
59         patch->ConstructPrefab( aabb, eType, axis, width, height );
60         patch->controlPointsChanged();
61
62         {
63                 scene::Path patchpath( makeReference( GlobalSceneGraph().root() ) );
64                 patchpath.push( makeReference( *Map_GetWorldspawn( g_map ) ) );
65                 patchpath.push( makeReference( node.get() ) );
66                 Instance_getSelectable( *graph.find( patchpath ) )->setSelected( true );
67         }
68 }
69
70
71 void Patch_makeCaps( Patch& patch, scene::Instance& instance, EPatchCap type, const char* shader ){
72         if ( ( type == eCapEndCap || type == eCapIEndCap )
73                  && patch.getWidth() != 5 ) {
74                 globalErrorStream() << "cannot create end-cap - patch width != 5\n";
75                 return;
76         }
77         if ( ( type == eCapBevel || type == eCapIBevel )
78                  && patch.getWidth() != 3 && patch.getWidth() != 5 ) {
79                 globalErrorStream() << "cannot create bevel-cap - patch width != 3\n";
80                 return;
81         }
82         if ( type == eCapCylinder
83                  && patch.getWidth() != 9 ) {
84                 globalErrorStream() << "cannot create cylinder-cap - patch width != 9\n";
85                 return;
86         }
87
88         {
89                 NodeSmartReference cap( g_patchCreator->createPatch() );
90                 Node_getTraversable( instance.path().parent() )->insert( cap );
91
92                 patch.MakeCap( Node_getPatch( cap ), type, ROW, true );
93                 Node_getPatch( cap )->SetShader( shader );
94
95                 scene::Path path( instance.path() );
96                 path.pop();
97                 path.push( makeReference( cap.get() ) );
98                 selectPath( path, true );
99         }
100
101         {
102                 NodeSmartReference cap( g_patchCreator->createPatch() );
103                 Node_getTraversable( instance.path().parent() )->insert( cap );
104
105                 patch.MakeCap( Node_getPatch( cap ), type, ROW, false );
106                 Node_getPatch( cap )->SetShader( shader );
107
108                 scene::Path path( instance.path() );
109                 path.pop();
110                 path.push( makeReference( cap.get() ) );
111                 selectPath( path, true );
112         }
113 }
114
115
116 typedef std::vector<scene::Instance*> InstanceVector;
117
118 class PatchStoreInstance
119 {
120 InstanceVector& m_instances;
121 public:
122 PatchStoreInstance( InstanceVector& instances ) : m_instances( instances ){
123 }
124 void operator()( PatchInstance& patch ) const {
125         m_instances.push_back( &patch );
126 }
127 };
128
129 enum ECapDialog {
130         PATCHCAP_BEVEL = 0,
131         PATCHCAP_ENDCAP,
132         PATCHCAP_INVERTED_BEVEL,
133         PATCHCAP_INVERTED_ENDCAP,
134         PATCHCAP_CYLINDER
135 };
136
137 EMessageBoxReturn DoCapDlg( ECapDialog *type );
138
139 void Scene_PatchDoCap_Selected( scene::Graph& graph, const char* shader ){
140         ECapDialog nType;
141
142         if ( DoCapDlg( &nType ) == eIDOK ) {
143                 EPatchCap eType;
144                 switch ( nType )
145                 {
146                 case PATCHCAP_INVERTED_BEVEL:
147                         eType = eCapIBevel;
148                         break;
149                 case PATCHCAP_BEVEL:
150                         eType = eCapBevel;
151                         break;
152                 case PATCHCAP_INVERTED_ENDCAP:
153                         eType = eCapIEndCap;
154                         break;
155                 case PATCHCAP_ENDCAP:
156                         eType = eCapEndCap;
157                         break;
158                 case PATCHCAP_CYLINDER:
159                         eType = eCapCylinder;
160                         break;
161                 default:
162                         ERROR_MESSAGE( "invalid patch cap type" );
163                         return;
164                 }
165
166                 InstanceVector instances;
167                 Scene_forEachVisibleSelectedPatchInstance( PatchStoreInstance( instances ) );
168                 for ( InstanceVector::const_iterator i = instances.begin(); i != instances.end(); ++i )
169                 {
170                         Patch_makeCaps( *Node_getPatch( ( *i )->path().top() ), *( *i ), eType, shader );
171                 }
172         }
173 }
174
175 void Patch_deform( Patch& patch, scene::Instance& instance, const int deform ){
176         patch.undoSave();
177
178         for (PatchControlIter i = patch.begin(); i != patch.end(); ++i)
179         {
180                 PatchControl& control = *i;
181                 int randomNumber = int( deform * (float(std::rand()) / float(RAND_MAX)));
182                 control.m_vertex[2] += randomNumber;
183         }
184
185         patch.controlPointsChanged();
186 }
187
188 void Scene_PatchDeform( scene::Graph& graph, const int deform )
189 {
190         InstanceVector instances;
191         Scene_forEachVisibleSelectedPatchInstance( PatchStoreInstance( instances ) );
192         for ( InstanceVector::const_iterator i = instances.begin(); i != instances.end(); ++i )
193         {
194                 Patch_deform( *Node_getPatch( ( *i )->path().top() ), *( *i ), deform );
195         }
196
197 }
198
199 void Patch_thicken( Patch& patch, scene::Instance& instance, const float thickness, bool seams, const int axis ){
200
201                 // Create a new patch node
202                 NodeSmartReference node( g_patchCreator->createPatch() );
203                 // Insert the node into worldspawn
204                 Node_getTraversable( Map_FindOrInsertWorldspawn( g_map ) )->insert( node );
205
206                 // Retrieve the contained patch from the node
207                 Patch* targetPatch = Node_getPatch( node );
208
209                 // Create the opposite patch with the given thickness = distance
210                 bool no12 = true;
211                 bool no34 = true;
212                 targetPatch->createThickenedOpposite( patch, thickness, axis, no12, no34 );
213
214                 // Now select the newly created patches
215                 {
216                         scene::Path patchpath( makeReference( GlobalSceneGraph().root() ) );
217                         patchpath.push( makeReference( *Map_GetWorldspawn( g_map ) ) );
218                         patchpath.push( makeReference( node.get() ) );
219                         Instance_getSelectable( *GlobalSceneGraph().find( patchpath ) )->setSelected( true );
220                 }
221
222                 if( seams && thickness != 0.0f){
223                         int i = 0;
224                         if ( no12 ){
225                                 i = 2;
226                         }
227                         int iend = 4;
228                         if ( no34 ){
229                                 iend = 2;
230                         }
231                         // Now create the four walls
232                         for ( ; i < iend; i++ ){
233                                 // Allocate new patch
234                                 NodeSmartReference node = NodeSmartReference( g_patchCreator->createPatch() );
235                                 // Insert each node into worldspawn
236                                 Node_getTraversable( Map_FindOrInsertWorldspawn( g_map ) )->insert( node );
237
238                                 // Retrieve the contained patch from the node
239                                 Patch* wallPatch = Node_getPatch( node );
240
241                                 // Create the wall patch by passing i as wallIndex
242                                 wallPatch->createThickenedWall( patch, *targetPatch, i );
243
244                                 if( ( wallPatch->localAABB().extents[0] <= 0.00005 && wallPatch->localAABB().extents[1] <= 0.00005 ) ||
245                                         ( wallPatch->localAABB().extents[1] <= 0.00005 && wallPatch->localAABB().extents[2] <= 0.00005 ) ||
246                                         ( wallPatch->localAABB().extents[0] <= 0.00005 && wallPatch->localAABB().extents[2] <= 0.00005 ) ){
247                                         //globalOutputStream() << "Thicken: Discarding degenerate patch.\n";
248                                         Node_getTraversable( Map_FindOrInsertWorldspawn( g_map ) )->erase( node );
249                                 }
250                                 else
251                                 // Now select the newly created patches
252                                 {
253                                         scene::Path patchpath( makeReference( GlobalSceneGraph().root() ) );
254                                         patchpath.push( makeReference( *Map_GetWorldspawn(g_map) ) );
255                                         patchpath.push( makeReference( node.get() ) );
256                                         Instance_getSelectable( *GlobalSceneGraph().find( patchpath ) )->setSelected( true );
257                                 }
258                         }
259                 }
260
261                 // Invert the target patch so that it faces the opposite direction
262                 targetPatch->InvertMatrix();
263 }
264
265 void Scene_PatchThicken( scene::Graph& graph, const int thickness, bool seams, const int axis )
266 {
267         InstanceVector instances;
268         Scene_forEachVisibleSelectedPatchInstance( PatchStoreInstance( instances ) );
269         for ( InstanceVector::const_iterator i = instances.begin(); i != instances.end(); ++i )
270         {
271                 Patch_thicken( *Node_getPatch( ( *i )->path().top() ), *( *i ), thickness, seams, axis );
272         }
273
274 }
275
276 Patch* Scene_GetUltimateSelectedVisiblePatch(){
277         if ( GlobalSelectionSystem().countSelected() != 0 ) {
278                 scene::Node& node = GlobalSelectionSystem().ultimateSelected().path().top();
279                 if ( node.visible() ) {
280                         return Node_getPatch( node );
281                 }
282         }
283         return 0;
284 }
285
286
287 class PatchCapTexture
288 {
289 public:
290 void operator()( Patch& patch ) const {
291         patch.ProjectTexture( Patch::m_CycleCapIndex );
292 }
293 };
294
295 void Scene_PatchCapTexture_Selected( scene::Graph& graph ){
296         Scene_forEachVisibleSelectedPatch( PatchCapTexture() );
297         Patch::m_CycleCapIndex = ( Patch::m_CycleCapIndex == 0 ) ? 1 : ( Patch::m_CycleCapIndex == 1 ) ? 2 : 0;
298         SceneChangeNotify();
299 }
300
301 class PatchFlipTexture
302 {
303 int m_axis;
304 public:
305 PatchFlipTexture( int axis ) : m_axis( axis ){
306 }
307 void operator()( Patch& patch ) const {
308         patch.FlipTexture( m_axis );
309 }
310 };
311
312 void Scene_PatchFlipTexture_Selected( scene::Graph& graph, int axis ){
313         Scene_forEachVisibleSelectedPatch( PatchFlipTexture( axis ) );
314 }
315
316 class PatchNaturalTexture
317 {
318 public:
319 void operator()( Patch& patch ) const {
320         patch.NaturalTexture();
321 }
322 };
323
324 void Scene_PatchNaturalTexture_Selected( scene::Graph& graph ){
325         Scene_forEachVisibleSelectedPatch( PatchNaturalTexture() );
326         SceneChangeNotify();
327 }
328
329
330 class PatchInsertRemove
331 {
332 bool m_insert, m_column, m_first;
333 public:
334 PatchInsertRemove( bool insert, bool column, bool first ) : m_insert( insert ), m_column( column ), m_first( first ){
335 }
336 void operator()( Patch& patch ) const {
337         patch.InsertRemove( m_insert, m_column, m_first );
338 }
339 };
340
341 void Scene_PatchInsertRemove_Selected( scene::Graph& graph, bool bInsert, bool bColumn, bool bFirst ){
342         Scene_forEachVisibleSelectedPatch( PatchInsertRemove( bInsert, bColumn, bFirst ) );
343 }
344
345 class PatchInvertMatrix
346 {
347 public:
348 void operator()( Patch& patch ) const {
349         patch.InvertMatrix();
350 }
351 };
352
353 void Scene_PatchInvert_Selected( scene::Graph& graph ){
354         Scene_forEachVisibleSelectedPatch( PatchInvertMatrix() );
355 }
356
357 class PatchRedisperse
358 {
359 EMatrixMajor m_major;
360 public:
361 PatchRedisperse( EMatrixMajor major ) : m_major( major ){
362 }
363 void operator()( Patch& patch ) const {
364         patch.Redisperse( m_major );
365 }
366 };
367
368 void Scene_PatchRedisperse_Selected( scene::Graph& graph, EMatrixMajor major ){
369         Scene_forEachVisibleSelectedPatch( PatchRedisperse( major ) );
370 }
371
372 class PatchSmooth
373 {
374 EMatrixMajor m_major;
375 public:
376 PatchSmooth( EMatrixMajor major ) : m_major( major ){
377 }
378 void operator()( Patch& patch ) const {
379         patch.Smooth( m_major );
380 }
381 };
382
383 void Scene_PatchSmooth_Selected( scene::Graph& graph, EMatrixMajor major ){
384         Scene_forEachVisibleSelectedPatch( PatchSmooth( major ) );
385 }
386
387 class PatchTransposeMatrix
388 {
389 public:
390 void operator()( Patch& patch ) const {
391         patch.TransposeMatrix();
392 }
393 };
394
395 void Scene_PatchTranspose_Selected( scene::Graph& graph ){
396         Scene_forEachVisibleSelectedPatch( PatchTransposeMatrix() );
397 }
398
399 class PatchSetShader
400 {
401 const char* m_name;
402 public:
403 PatchSetShader( const char* name )
404         : m_name( name ){
405 }
406 void operator()( Patch& patch ) const {
407         patch.SetShader( m_name );
408 }
409 };
410
411 void Scene_PatchSetShader_Selected( scene::Graph& graph, const char* name ){
412         Scene_forEachVisibleSelectedPatch( PatchSetShader( name ) );
413         SceneChangeNotify();
414 }
415
416 void Scene_PatchGetShader_Selected( scene::Graph& graph, CopiedString& name ){
417         Patch* patch = Scene_GetUltimateSelectedVisiblePatch();
418         if ( patch != 0 ) {
419                 name = patch->GetShader();
420         }
421 }
422
423 class PatchSelectByShader
424 {
425 const char* m_name;
426 public:
427 inline PatchSelectByShader( const char* name )
428         : m_name( name ){
429 }
430 void operator()( PatchInstance& patch ) const {
431         if ( shader_equal( patch.getPatch().GetShader(), m_name ) ) {
432                 patch.setSelected( true );
433         }
434 }
435 };
436
437 void Scene_PatchSelectByShader( scene::Graph& graph, const char* name ){
438         Scene_forEachVisiblePatchInstance( PatchSelectByShader( name ) );
439 }
440
441
442 class PatchFindReplaceShader
443 {
444 const char* m_find;
445 const char* m_replace;
446 public:
447 PatchFindReplaceShader( const char* find, const char* replace ) : m_find( find ), m_replace( replace ){
448 }
449 void operator()( Patch& patch ) const {
450         if ( shader_equal( patch.GetShader(), m_find ) ) {
451                 patch.SetShader( m_replace );
452         }
453 }
454 };
455
456 namespace{
457 bool DoingSearch( const char *repl ){
458         return ( repl == NULL || ( strcmp( "textures/", repl ) == 0 ) );
459 }
460 }
461 void Scene_PatchFindReplaceShader( scene::Graph& graph, const char* find, const char* replace ){
462         if( DoingSearch( replace ) ){
463                 Scene_forEachVisiblePatchInstance( PatchSelectByShader( find ) );
464         }
465         else{
466                 Scene_forEachVisiblePatch( PatchFindReplaceShader( find, replace ) );
467         }
468 }
469
470 void Scene_PatchFindReplaceShader_Selected( scene::Graph& graph, const char* find, const char* replace ){
471         if( DoingSearch( replace ) ){
472                 //do nothing, because alternative is replacing to notex
473                 //perhaps deselect ones with not matching shaders here?
474         }
475         else{
476                 Scene_forEachVisibleSelectedPatch( PatchFindReplaceShader( find, replace ) );
477         }
478 }
479
480
481 AABB PatchCreator_getBounds(){
482         AABB aabb( aabb_for_minmax( Select_getWorkZone().d_work_min, Select_getWorkZone().d_work_max ) );
483
484         float gridSize = GetGridSize();
485
486         if ( aabb.extents[0] == 0 ) {
487                 aabb.extents[0] = gridSize;
488         }
489         if ( aabb.extents[1] == 0 ) {
490                 aabb.extents[1] = gridSize;
491         }
492         if ( aabb.extents[2] == 0 ) {
493                 aabb.extents[2] = gridSize;
494         }
495
496         if ( aabb_valid( aabb ) ) {
497                 return aabb;
498         }
499         return AABB( Vector3( 0, 0, 0 ), Vector3( 64, 64, 64 ) );
500 }
501
502 void DoNewPatchDlg( EPatchPrefab prefab, int minrows, int mincols, int defrows, int defcols, int maxrows, int maxcols );
503
504 void Patch_XactCylinder(){
505         UndoableCommand undo( "patchCreateXactCylinder" );
506
507         DoNewPatchDlg( eXactCylinder, 3, 7, 3, 13, 0, 0 );
508 }
509
510 void Patch_XactSphere(){
511         UndoableCommand undo( "patchCreateXactSphere" );
512
513         DoNewPatchDlg( eXactSphere, 5, 7, 7, 13, 0, 0 );
514 }
515
516 void Patch_XactCone(){
517         UndoableCommand undo( "patchCreateXactCone" );
518
519         DoNewPatchDlg( eXactCone, 3, 7, 3, 13, 0, 0 );
520 }
521
522 void Patch_Cylinder(){
523         UndoableCommand undo( "patchCreateCylinder" );
524
525         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eCylinder, GlobalXYWnd_getCurrentViewType() );
526 }
527
528 void Patch_DenseCylinder(){
529         UndoableCommand undo( "patchCreateDenseCylinder" );
530
531         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eDenseCylinder, GlobalXYWnd_getCurrentViewType() );
532 }
533
534 void Patch_VeryDenseCylinder(){
535         UndoableCommand undo( "patchCreateVeryDenseCylinder" );
536
537         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eVeryDenseCylinder, GlobalXYWnd_getCurrentViewType() );
538 }
539
540 void Patch_SquareCylinder(){
541         UndoableCommand undo( "patchCreateSquareCylinder" );
542
543         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eSqCylinder, GlobalXYWnd_getCurrentViewType() );
544 }
545
546 void Patch_Endcap(){
547         UndoableCommand undo( "patchCreateCaps" );
548
549         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eEndCap, GlobalXYWnd_getCurrentViewType() );
550 }
551
552 void Patch_Bevel(){
553         UndoableCommand undo( "patchCreateBevel" );
554
555         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eBevel, GlobalXYWnd_getCurrentViewType() );
556 }
557
558 void Patch_Sphere(){
559         UndoableCommand undo( "patchCreateSphere" );
560
561         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eSphere, GlobalXYWnd_getCurrentViewType() );
562 }
563
564 void Patch_SquareBevel(){
565 }
566
567 void Patch_SquareEndcap(){
568 }
569
570 void Patch_Cone(){
571         UndoableCommand undo( "patchCreateCone" );
572
573         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eCone, GlobalXYWnd_getCurrentViewType() );
574 }
575
576 void Patch_Plane(){
577         UndoableCommand undo( "patchCreatePlane" );
578
579         DoNewPatchDlg( ePlane, 3, 3, 3, 3, 0, 0 );
580 }
581
582 void Patch_InsertInsertColumn(){
583         UndoableCommand undo( "patchInsertColumns" );
584
585         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), true, true, false );
586 }
587
588 void Patch_InsertAddColumn(){
589         UndoableCommand undo( "patchAddColumns" );
590
591         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), true, true, true );
592 }
593
594 void Patch_InsertInsertRow(){
595         UndoableCommand undo( "patchInsertRows" );
596
597         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), true, false, false );
598 }
599
600 void Patch_InsertAddRow(){
601         UndoableCommand undo( "patchAddRows" );
602
603         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), true, false, true );
604 }
605
606 void Patch_DeleteFirstColumn(){
607         UndoableCommand undo( "patchDeleteFirstColumns" );
608
609         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), false, true, true );
610 }
611
612 void Patch_DeleteLastColumn(){
613         UndoableCommand undo( "patchDeleteLastColumns" );
614
615         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), false, true, false );
616 }
617
618 void Patch_DeleteFirstRow(){
619         UndoableCommand undo( "patchDeleteFirstRows" );
620
621         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), false, false, true );
622 }
623
624 void Patch_DeleteLastRow(){
625         UndoableCommand undo( "patchDeleteLastRows" );
626
627         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), false, false, false );
628 }
629
630 void Patch_Invert(){
631         UndoableCommand undo( "patchInvert" );
632
633         Scene_PatchInvert_Selected( GlobalSceneGraph() );
634 }
635
636 void Patch_RedisperseRows(){
637         UndoableCommand undo( "patchRedisperseRows" );
638
639         Scene_PatchRedisperse_Selected( GlobalSceneGraph(), ROW );
640 }
641
642 void Patch_RedisperseCols(){
643         UndoableCommand undo( "patchRedisperseColumns" );
644
645         Scene_PatchRedisperse_Selected( GlobalSceneGraph(), COL );
646 }
647
648 void Patch_SmoothRows(){
649         UndoableCommand undo( "patchSmoothRows" );
650
651         Scene_PatchSmooth_Selected( GlobalSceneGraph(), ROW );
652 }
653
654 void Patch_SmoothCols(){
655         UndoableCommand undo( "patchSmoothColumns" );
656
657         Scene_PatchSmooth_Selected( GlobalSceneGraph(), COL );
658 }
659
660 void Patch_Transpose(){
661         UndoableCommand undo( "patchTranspose" );
662
663         Scene_PatchTranspose_Selected( GlobalSceneGraph() );
664 }
665
666 void Patch_Cap(){
667         // FIXME: add support for patch cap creation
668         // Patch_CapCurrent();
669         UndoableCommand undo( "patchCreateCaps" );
670
671         Scene_PatchDoCap_Selected( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
672 }
673
674 void Patch_CycleProjection(){
675         UndoableCommand undo( "patchCycleUVProjectionAxis" );
676
677         Scene_PatchCapTexture_Selected( GlobalSceneGraph() );
678 }
679
680 ///\todo Unfinished.
681 void Patch_OverlayOn(){
682 }
683
684 ///\todo Unfinished.
685 void Patch_OverlayOff(){
686 }
687
688 void Patch_FlipTextureX(){
689         UndoableCommand undo( "patchFlipTextureU" );
690
691         Scene_PatchFlipTexture_Selected( GlobalSceneGraph(), 0 );
692 }
693
694 void Patch_FlipTextureY(){
695         UndoableCommand undo( "patchFlipTextureV" );
696
697         Scene_PatchFlipTexture_Selected( GlobalSceneGraph(), 1 );
698 }
699
700 void Patch_NaturalTexture(){
701         UndoableCommand undo( "patchNaturalTexture" );
702
703         Scene_PatchNaturalTexture_Selected( GlobalSceneGraph() );
704 }
705
706 void Patch_CapTexture(){
707         UndoableCommand command( "patchCapTexture" );
708         Scene_PatchCapTexture_Selected( GlobalSceneGraph() );
709 }
710
711 void Patch_ResetTexture(){
712         float fx, fy;
713         if ( DoTextureLayout( &fx, &fy ) == eIDOK ) {
714                 UndoableCommand command( "patchTileTexture" );
715                 Scene_PatchTileTexture_Selected( GlobalSceneGraph(), fx, fy );
716         }
717 }
718
719 void Patch_FitTexture(){
720         UndoableCommand command( "patchFitTexture" );
721         Scene_PatchTileTexture_Selected( GlobalSceneGraph(), 1, 1 );
722 }
723
724 void DoPatchDeformDlg();
725
726 void Patch_Deform(){
727         UndoableCommand undo( "patchDeform" );
728
729         DoPatchDeformDlg();
730 }
731
732 void DoPatchThickenDlg();
733
734 void Patch_Thicken(){
735         UndoableCommand undo( "patchThicken" );
736
737         DoPatchThickenDlg();
738 }
739
740
741
742 #include "ifilter.h"
743
744
745 class filter_patch_all : public PatchFilter
746 {
747 public:
748 bool filter( const Patch& patch ) const {
749         return true;
750 }
751 };
752
753 class filter_patch_shader : public PatchFilter
754 {
755 const char* m_shader;
756 public:
757 filter_patch_shader( const char* shader ) : m_shader( shader ){
758 }
759 bool filter( const Patch& patch ) const {
760         return shader_equal( patch.GetShader(), m_shader );
761 }
762 };
763
764 class filter_patch_flags : public PatchFilter
765 {
766 int m_flags;
767 public:
768 filter_patch_flags( int flags ) : m_flags( flags ){
769 }
770 bool filter( const Patch& patch ) const {
771         return ( patch.getShaderFlags() & m_flags ) != 0;
772 }
773 };
774
775
776 filter_patch_all g_filter_patch_all;
777 filter_patch_shader g_filter_patch_clip( "textures/common/clip" );
778 filter_patch_shader g_filter_patch_weapclip( "textures/common/weapclip" );
779 filter_patch_flags g_filter_patch_translucent( QER_TRANS );
780
781 void PatchFilters_construct(){
782         add_patch_filter( g_filter_patch_all, EXCLUDE_CURVES );
783         add_patch_filter( g_filter_patch_clip, EXCLUDE_CLIP );
784         add_patch_filter( g_filter_patch_weapclip, EXCLUDE_CLIP );
785         add_patch_filter( g_filter_patch_translucent, EXCLUDE_TRANSLUCENT );
786 }
787
788
789 #include "preferences.h"
790
791 void Patch_constructPreferences( PreferencesPage& page ){
792         page.appendEntry( "Patch Subdivide Threshold", g_PatchSubdivideThreshold );
793 }
794 void Patch_constructPage( PreferenceGroup& group ){
795         PreferencesPage page( group.createPage( "Patches", "Patch Display Preferences" ) );
796         Patch_constructPreferences( page );
797 }
798 void Patch_registerPreferencesPage(){
799         PreferencesDialog_addDisplayPage( FreeCaller1<PreferenceGroup&, Patch_constructPage>() );
800 }
801
802
803 #include "preferencesystem.h"
804
805 void PatchPreferences_construct(){
806         GlobalPreferenceSystem().registerPreference( "Subdivisions", IntImportStringCaller( g_PatchSubdivideThreshold ), IntExportStringCaller( g_PatchSubdivideThreshold ) );
807 }
808
809
810 #include "generic/callback.h"
811
812 void Patch_registerCommands(){
813         GlobalCommands_insert( "InvertCurveTextureX", FreeCaller<Patch_FlipTextureX>(), Accelerator( 'I', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
814         GlobalCommands_insert( "InvertCurveTextureY", FreeCaller<Patch_FlipTextureY>(), Accelerator( 'I', (GdkModifierType)GDK_SHIFT_MASK ) );
815         GlobalCommands_insert( "NaturalizePatch", FreeCaller<Patch_NaturalTexture>(), Accelerator( 'N', (GdkModifierType)GDK_CONTROL_MASK ) );
816         GlobalCommands_insert( "PatchCylinder", FreeCaller<Patch_Cylinder>() );
817         GlobalCommands_insert( "PatchDenseCylinder", FreeCaller<Patch_DenseCylinder>() );
818         GlobalCommands_insert( "PatchVeryDenseCylinder", FreeCaller<Patch_VeryDenseCylinder>() );
819         GlobalCommands_insert( "PatchSquareCylinder", FreeCaller<Patch_SquareCylinder>() );
820         GlobalCommands_insert( "PatchXactCylinder", FreeCaller<Patch_XactCylinder>() );
821         GlobalCommands_insert( "PatchXactSphere", FreeCaller<Patch_XactSphere>() );
822         GlobalCommands_insert( "PatchXactCone", FreeCaller<Patch_XactCone>() );
823         GlobalCommands_insert( "PatchEndCap", FreeCaller<Patch_Endcap>() );
824         GlobalCommands_insert( "PatchBevel", FreeCaller<Patch_Bevel>() );
825         GlobalCommands_insert( "PatchSquareBevel", FreeCaller<Patch_SquareBevel>() );
826         GlobalCommands_insert( "PatchSquareEndcap", FreeCaller<Patch_SquareEndcap>() );
827         GlobalCommands_insert( "PatchCone", FreeCaller<Patch_Cone>() );
828         GlobalCommands_insert( "PatchSphere", FreeCaller<Patch_Sphere>() );
829         GlobalCommands_insert( "SimplePatchMesh", FreeCaller<Patch_Plane>(), Accelerator( 'P', (GdkModifierType)GDK_SHIFT_MASK ) );
830         GlobalCommands_insert( "PatchInsertInsertColumn", FreeCaller<Patch_InsertInsertColumn>(), Accelerator( GDK_KP_Add, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
831         GlobalCommands_insert( "PatchInsertAddColumn", FreeCaller<Patch_InsertAddColumn>() );
832         GlobalCommands_insert( "PatchInsertInsertRow", FreeCaller<Patch_InsertInsertRow>(), Accelerator( GDK_KP_Add, (GdkModifierType)GDK_CONTROL_MASK ) );
833         GlobalCommands_insert( "PatchInsertAddRow", FreeCaller<Patch_InsertAddRow>() );
834         GlobalCommands_insert( "PatchDeleteFirstColumn", FreeCaller<Patch_DeleteFirstColumn>() );
835         GlobalCommands_insert( "PatchDeleteLastColumn", FreeCaller<Patch_DeleteLastColumn>(), Accelerator( GDK_KP_Subtract, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
836         GlobalCommands_insert( "PatchDeleteFirstRow", FreeCaller<Patch_DeleteFirstRow>() );
837         GlobalCommands_insert( "PatchDeleteLastRow", FreeCaller<Patch_DeleteLastRow>(), Accelerator( GDK_KP_Subtract, (GdkModifierType)GDK_CONTROL_MASK ) );
838         GlobalCommands_insert( "InvertCurve", FreeCaller<Patch_Invert>(), Accelerator( 'I', (GdkModifierType)GDK_CONTROL_MASK ) );
839         GlobalCommands_insert( "RedisperseRows", FreeCaller<Patch_RedisperseRows>(), Accelerator( 'E', (GdkModifierType)GDK_CONTROL_MASK ) );
840         GlobalCommands_insert( "RedisperseCols", FreeCaller<Patch_RedisperseCols>(), Accelerator( 'E', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
841         GlobalCommands_insert( "SmoothRows", FreeCaller<Patch_SmoothRows>(), Accelerator( 'W', (GdkModifierType)GDK_CONTROL_MASK ) );
842         GlobalCommands_insert( "SmoothCols", FreeCaller<Patch_SmoothCols>(), Accelerator( 'W', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
843         GlobalCommands_insert( "MatrixTranspose", FreeCaller<Patch_Transpose>(), Accelerator( 'M', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
844         GlobalCommands_insert( "CapCurrentCurve", FreeCaller<Patch_Cap>(), Accelerator( 'C', (GdkModifierType)GDK_SHIFT_MASK ) );
845         GlobalCommands_insert( "CycleCapTexturePatch", FreeCaller<Patch_CycleProjection>(), Accelerator( 'N', (GdkModifierType)GDK_SHIFT_MASK ) );
846         GlobalCommands_insert( "MakeOverlayPatch", FreeCaller<Patch_OverlayOn>(), Accelerator( 'Y' ) );
847         GlobalCommands_insert( "ClearPatchOverlays", FreeCaller<Patch_OverlayOff>(), Accelerator( 'L', (GdkModifierType)GDK_CONTROL_MASK ) );
848         GlobalCommands_insert( "PatchDeform", FreeCaller<Patch_Deform>() );
849         GlobalCommands_insert( "PatchThicken", FreeCaller<Patch_Thicken>() );
850 }
851
852 void Patch_constructToolbar( GtkToolbar* toolbar ){
853         toolbar_append_button( toolbar, "Put caps on the current patch (SHIFT + C)", "curve_cap.png", "CapCurrentCurve" );
854 }
855
856 void Patch_constructMenu( GtkMenu* menu ){
857         create_menu_item_with_mnemonic( menu, "Cylinder", "PatchCylinder" );
858         {
859                 GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "More Cylinders" );
860                 if ( g_Layout_enableDetachableMenus.m_value ) {
861                         menu_tearoff( menu_in_menu );
862                 }
863                 create_menu_item_with_mnemonic( menu_in_menu, "Dense Cylinder", "PatchDenseCylinder" );
864                 create_menu_item_with_mnemonic( menu_in_menu, "Very Dense Cylinder", "PatchVeryDenseCylinder" );
865                 create_menu_item_with_mnemonic( menu_in_menu, "Square Cylinder", "PatchSquareCylinder" );
866                 create_menu_item_with_mnemonic( menu_in_menu, "Exact Cylinder...", "PatchXactCylinder" );
867         }
868         menu_separator( menu );
869         create_menu_item_with_mnemonic( menu, "End cap", "PatchEndCap" );
870         create_menu_item_with_mnemonic( menu, "Bevel", "PatchBevel" );
871         {
872 //              GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "More End caps, Bevels" );
873 //              if ( g_Layout_enableDetachableMenus.m_value ) {
874 //                      menu_tearoff( menu_in_menu );
875 //              }
876                 create_menu_item_with_mnemonic( menu, "Square Endcap", "PatchSquareBevel" );
877                 create_menu_item_with_mnemonic( menu, "Square Bevel", "PatchSquareEndcap" );
878         }
879         menu_separator( menu );
880         create_menu_item_with_mnemonic( menu, "Cone", "PatchCone" );
881         create_menu_item_with_mnemonic( menu, "Exact Cone...", "PatchXactCone" );
882         menu_separator( menu );
883         create_menu_item_with_mnemonic( menu, "Sphere", "PatchSphere" );
884         create_menu_item_with_mnemonic( menu, "Exact Sphere...", "PatchXactSphere" );
885         menu_separator( menu );
886         create_menu_item_with_mnemonic( menu, "Simple Patch Mesh...", "SimplePatchMesh" );
887         menu_separator( menu );
888         {
889                 GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Insert" );
890                 if ( g_Layout_enableDetachableMenus.m_value ) {
891                         menu_tearoff( menu_in_menu );
892                 }
893                 create_menu_item_with_mnemonic( menu_in_menu, "Add (2) Columns", "PatchInsertAddColumn" );
894                 create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Columns", "PatchInsertInsertColumn" );
895                 menu_separator( menu_in_menu );
896                 create_menu_item_with_mnemonic( menu_in_menu, "Add (2) Rows", "PatchInsertAddRow" );
897                 create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Rows", "PatchInsertInsertRow" );
898         }
899         {
900                 GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Delete" );
901                 if ( g_Layout_enableDetachableMenus.m_value ) {
902                         menu_tearoff( menu_in_menu );
903                 }
904                 create_menu_item_with_mnemonic( menu_in_menu, "First (2) Columns", "PatchDeleteFirstColumn" );
905                 create_menu_item_with_mnemonic( menu_in_menu, "Last (2) Columns", "PatchDeleteLastColumn" );
906                 menu_separator( menu_in_menu );
907                 create_menu_item_with_mnemonic( menu_in_menu, "First (2) Rows", "PatchDeleteFirstRow" );
908                 create_menu_item_with_mnemonic( menu_in_menu, "Last (2) Rows", "PatchDeleteLastRow" );
909         }
910         menu_separator( menu );
911         {
912                 GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Matrix" );
913                 if ( g_Layout_enableDetachableMenus.m_value ) {
914                         menu_tearoff( menu_in_menu );
915                 }
916                 create_menu_item_with_mnemonic( menu_in_menu, "Invert", "InvertCurve" );
917                 create_menu_item_with_mnemonic( menu_in_menu, "Transpose", "MatrixTranspose" );
918 //              GtkMenu* menu_3 = create_sub_menu_with_mnemonic( menu_in_menu, "Re-disperse" );
919 //              if ( g_Layout_enableDetachableMenus.m_value ) {
920 //                      menu_tearoff( menu_3 );
921 //              }
922                 menu_separator( menu_in_menu );
923                 create_menu_item_with_mnemonic( menu_in_menu, "Re-disperse Rows", "RedisperseRows" );
924                 create_menu_item_with_mnemonic( menu_in_menu, "Re-disperse Columns", "RedisperseCols" );
925 //              GtkMenu* menu_4 = create_sub_menu_with_mnemonic( menu_in_menu, "Smooth" );
926 //              if ( g_Layout_enableDetachableMenus.m_value ) {
927 //                      menu_tearoff( menu_4 );
928 //              }
929                 menu_separator( menu_in_menu );
930                 create_menu_item_with_mnemonic( menu_in_menu, "Smooth Rows", "SmoothRows" );
931                 create_menu_item_with_mnemonic( menu_in_menu, "Smooth Columns", "SmoothCols" );
932         }
933         menu_separator( menu );
934         create_menu_item_with_mnemonic( menu, "Cap Selection", "CapCurrentCurve" );
935         menu_separator( menu );
936         {
937                 GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Texture" );
938                 if ( g_Layout_enableDetachableMenus.m_value ) {
939                         menu_tearoff( menu_in_menu );
940                 }
941                 create_menu_item_with_mnemonic( menu_in_menu, "Cycle Projection", "CycleCapTexturePatch" );
942                 create_menu_item_with_mnemonic( menu_in_menu, "Naturalize", "NaturalizePatch" );
943                 create_menu_item_with_mnemonic( menu_in_menu, "Invert X", "InvertCurveTextureX" );
944                 create_menu_item_with_mnemonic( menu_in_menu, "Invert Y", "InvertCurveTextureY" );
945
946         }
947         menu_separator( menu );
948         {
949                 GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Overlay" );
950                 if ( g_Layout_enableDetachableMenus.m_value ) {
951                         menu_tearoff( menu_in_menu );
952                 }
953                 create_menu_item_with_mnemonic( menu_in_menu, "Set", "MakeOverlayPatch" );
954                 create_menu_item_with_mnemonic( menu_in_menu, "Clear", "ClearPatchOverlays" );
955         }
956         menu_separator( menu );
957         create_menu_item_with_mnemonic( menu, "Deform...", "PatchDeform" );
958         create_menu_item_with_mnemonic( menu, "Thicken...", "PatchThicken" );
959 }
960
961
962 #include <gtk/gtkbox.h>
963 #include <gtk/gtktable.h>
964 #include <gtk/gtktogglebutton.h>
965 #include <gtk/gtkradiobutton.h>
966 #include <gtk/gtkcombobox.h>
967 #include <gtk/gtklabel.h>
968 #include "gtkutil/dialog.h"
969 #include "gtkutil/widget.h"
970
971 void DoNewPatchDlg( EPatchPrefab prefab, int minrows, int mincols, int defrows, int defcols, int maxrows, int maxcols ){
972         ModalDialog dialog;
973         GtkComboBox* width;
974         GtkComboBox* height;
975
976         GtkWindow* window = create_dialog_window( MainFrame_getWindow(), "Patch density", G_CALLBACK( dialog_delete_callback ), &dialog );
977
978         GtkAccelGroup* accel = gtk_accel_group_new();
979         gtk_window_add_accel_group( window, accel );
980
981         {
982                 GtkHBox* hbox = create_dialog_hbox( 4, 4 );
983                 gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( hbox ) );
984                 {
985                         GtkTable* table = create_dialog_table( 2, 2, 4, 4 );
986                         gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
987                         {
988                                 GtkLabel* label = GTK_LABEL( gtk_label_new( "Width:" ) );
989                                 gtk_widget_show( GTK_WIDGET( label ) );
990                                 gtk_table_attach( table, GTK_WIDGET( label ), 0, 1, 0, 1,
991                                                                   (GtkAttachOptions) ( GTK_FILL ),
992                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
993                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
994                         }
995                         {
996                                 GtkLabel* label = GTK_LABEL( gtk_label_new( "Height:" ) );
997                                 gtk_widget_show( GTK_WIDGET( label ) );
998                                 gtk_table_attach( table, GTK_WIDGET( label ), 0, 1, 1, 2,
999                                                                   (GtkAttachOptions) ( GTK_FILL ),
1000                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1001                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
1002                         }
1003
1004                         {
1005                                 GtkComboBox* combo = GTK_COMBO_BOX( gtk_combo_box_new_text() );
1006 #define D_ITEM( x ) if ( x >= mincols && ( !maxcols || x <= maxcols ) ) gtk_combo_box_append_text( combo, # x )
1007                                 D_ITEM( 3 );
1008                                 D_ITEM( 5 );
1009                                 D_ITEM( 7 );
1010                                 D_ITEM( 9 );
1011                                 D_ITEM( 11 );
1012                                 D_ITEM( 13 );
1013                                 D_ITEM( 15 );
1014                                 D_ITEM( 17 );
1015                                 D_ITEM( 19 );
1016                                 D_ITEM( 21 );
1017                                 D_ITEM( 23 );
1018                                 D_ITEM( 25 );
1019                                 D_ITEM( 27 );
1020                                 D_ITEM( 29 );
1021                                 D_ITEM( 31 ); // MAX_PATCH_SIZE is 32, so we should be able to do 31...
1022 #undef D_ITEM
1023                                 gtk_widget_show( GTK_WIDGET( combo ) );
1024                                 gtk_table_attach( table, GTK_WIDGET( combo ), 1, 2, 0, 1,
1025                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1026                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1027
1028                                 width = combo;
1029                         }
1030                         {
1031                                 GtkComboBox* combo = GTK_COMBO_BOX( gtk_combo_box_new_text() );
1032 #define D_ITEM( x ) if ( x >= minrows && ( !maxrows || x <= maxrows ) ) gtk_combo_box_append_text( combo, # x )
1033                                 D_ITEM( 3 );
1034                                 D_ITEM( 5 );
1035                                 D_ITEM( 7 );
1036                                 D_ITEM( 9 );
1037                                 D_ITEM( 11 );
1038                                 D_ITEM( 13 );
1039                                 D_ITEM( 15 );
1040                                 D_ITEM( 17 );
1041                                 D_ITEM( 19 );
1042                                 D_ITEM( 21 );
1043                                 D_ITEM( 23 );
1044                                 D_ITEM( 25 );
1045                                 D_ITEM( 27 );
1046                                 D_ITEM( 29 );
1047                                 D_ITEM( 31 ); // MAX_PATCH_SIZE is 32, so we should be able to do 31...
1048 #undef D_ITEM
1049                                 gtk_widget_show( GTK_WIDGET( combo ) );
1050                                 gtk_table_attach( table, GTK_WIDGET( combo ), 1, 2, 1, 2,
1051                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1052                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1053
1054                                 height = combo;
1055                         }
1056                 }
1057
1058                 {
1059                         GtkVBox* vbox = create_dialog_vbox( 4 );
1060                         gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), TRUE, TRUE, 0 );
1061                         {
1062                                 GtkButton* button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
1063                                 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1064                                 widget_make_default( GTK_WIDGET( button ) );
1065                                 gtk_widget_grab_focus( GTK_WIDGET( button ) );
1066                                 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
1067                         }
1068                         {
1069                                 GtkButton* button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
1070                                 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1071                                 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
1072                         }
1073                 }
1074         }
1075
1076         // Initialize dialog
1077         gtk_combo_box_set_active( width, ( defcols - mincols ) / 2 );
1078         gtk_combo_box_set_active( height, ( defrows - minrows ) / 2 );
1079
1080         if ( modal_dialog_show( window, dialog ) == eIDOK ) {
1081                 int w = gtk_combo_box_get_active( width ) * 2 + mincols;
1082                 int h = gtk_combo_box_get_active( height ) * 2 + minrows;
1083
1084                 Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), prefab, GlobalXYWnd_getCurrentViewType(), w, h );
1085         }
1086
1087         gtk_widget_destroy( GTK_WIDGET( window ) );
1088 }
1089
1090
1091 void DoPatchDeformDlg(){
1092         ModalDialog dialog;
1093         GtkWidget* deformW;
1094
1095         GtkWindow* window = create_dialog_window( MainFrame_getWindow(), "Patch deform", G_CALLBACK( dialog_delete_callback ), &dialog );
1096
1097         GtkAccelGroup* accel = gtk_accel_group_new();
1098         gtk_window_add_accel_group( window, accel );
1099
1100         {
1101                 GtkHBox* hbox = create_dialog_hbox( 4, 4 );
1102                 gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( hbox ) );
1103                 {
1104                         GtkTable* table = create_dialog_table( 2, 2, 4, 4 );
1105                         gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
1106                         {
1107                                 GtkLabel* label = GTK_LABEL( gtk_label_new( "Max deform:" ) );
1108                                 gtk_widget_show( GTK_WIDGET( label ) );
1109                                 gtk_table_attach( table, GTK_WIDGET( label ), 0, 1, 0, 1,
1110                                                                   (GtkAttachOptions) ( GTK_FILL ),
1111                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1112                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
1113                         }
1114                         {
1115                                 GtkWidget* entry = gtk_entry_new();
1116                                 gtk_entry_set_text( GTK_ENTRY( entry ), "16" );
1117                                 gtk_widget_show( entry );
1118                                 gtk_table_attach( table, entry, 1, 2, 0, 1,
1119                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1120                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1121
1122                                 deformW = entry;
1123                         }
1124                 }
1125                 {
1126                         GtkVBox* vbox = create_dialog_vbox( 4 );
1127                         gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), TRUE, TRUE, 0 );
1128                         {
1129                                 GtkButton* button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
1130                                 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1131                                 widget_make_default( GTK_WIDGET( button ) );
1132                                 gtk_widget_grab_focus( GTK_WIDGET( button ) );
1133                                 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
1134                         }
1135                         {
1136                                 GtkButton* button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
1137                                 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1138                                 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
1139                         }
1140                 }
1141         }
1142
1143         if ( modal_dialog_show( window, dialog ) == eIDOK ) {
1144                 int deform = static_cast<int>( atoi( gtk_entry_get_text( GTK_ENTRY( deformW ) ) ) );
1145                 Scene_PatchDeform( GlobalSceneGraph(), deform );
1146         }
1147
1148         gtk_widget_destroy( GTK_WIDGET( window ) );
1149 }
1150
1151
1152
1153 EMessageBoxReturn DoCapDlg( ECapDialog* type ){
1154         ModalDialog dialog;
1155         ModalDialogButton ok_button( dialog, eIDOK );
1156         ModalDialogButton cancel_button( dialog, eIDCANCEL );
1157         GtkWidget* bevel;
1158         GtkWidget* ibevel;
1159         GtkWidget* endcap;
1160         GtkWidget* iendcap;
1161         GtkWidget* cylinder;
1162
1163         GtkWindow* window = create_modal_dialog_window( MainFrame_getWindow(), "Cap", dialog );
1164
1165         GtkAccelGroup *accel_group = gtk_accel_group_new();
1166         gtk_window_add_accel_group( window, accel_group );
1167
1168         {
1169                 GtkHBox* hbox = create_dialog_hbox( 4, 4 );
1170                 gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( hbox ) );
1171
1172                 {
1173                         // Gef: Added a vbox to contain the toggle buttons
1174                         GtkVBox* radio_vbox = create_dialog_vbox( 4 );
1175                         gtk_container_add( GTK_CONTAINER( hbox ), GTK_WIDGET( radio_vbox ) );
1176
1177                         {
1178                                 GtkTable* table = GTK_TABLE( gtk_table_new( 5, 2, FALSE ) );
1179                                 gtk_widget_show( GTK_WIDGET( table ) );
1180                                 gtk_box_pack_start( GTK_BOX( radio_vbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
1181                                 gtk_table_set_row_spacings( table, 5 );
1182                                 gtk_table_set_col_spacings( table, 5 );
1183
1184                                 {
1185                                         GtkImage* image = new_local_image( "cap_bevel.png" );
1186                                         gtk_widget_show( GTK_WIDGET( image ) );
1187                                         gtk_table_attach( table, GTK_WIDGET( image ), 0, 1, 0, 1,
1188                                                                           (GtkAttachOptions) ( GTK_FILL ),
1189                                                                           (GtkAttachOptions) ( 0 ), 0, 0 );
1190                                 }
1191                                 {
1192                                         GtkImage* image = new_local_image( "cap_endcap.png" );
1193                                         gtk_widget_show( GTK_WIDGET( image ) );
1194                                         gtk_table_attach( table, GTK_WIDGET( image ), 0, 1, 1, 2,
1195                                                                           (GtkAttachOptions) ( GTK_FILL ),
1196                                                                           (GtkAttachOptions) ( 0 ), 0, 0 );
1197                                 }
1198                                 {
1199                                         GtkImage* image = new_local_image( "cap_ibevel.png" );
1200                                         gtk_widget_show( GTK_WIDGET( image ) );
1201                                         gtk_table_attach( table, GTK_WIDGET( image ), 0, 1, 2, 3,
1202                                                                           (GtkAttachOptions) ( GTK_FILL ),
1203                                                                           (GtkAttachOptions) ( 0 ), 0, 0 );
1204                                 }
1205                                 {
1206                                         GtkImage* image = new_local_image( "cap_iendcap.png" );
1207                                         gtk_widget_show( GTK_WIDGET( image ) );
1208                                         gtk_table_attach( table, GTK_WIDGET( image ), 0, 1, 3, 4,
1209                                                                           (GtkAttachOptions) ( GTK_FILL ),
1210                                                                           (GtkAttachOptions) ( 0 ), 0, 0 );
1211                                 }
1212                                 {
1213                                         GtkImage* image = new_local_image( "cap_cylinder.png" );
1214                                         gtk_widget_show( GTK_WIDGET( image ) );
1215                                         gtk_table_attach( table, GTK_WIDGET( image ), 0, 1, 4, 5,
1216                                                                           (GtkAttachOptions) ( GTK_FILL ),
1217                                                                           (GtkAttachOptions) ( 0 ), 0, 0 );
1218                                 }
1219
1220                                 GSList* group = 0;
1221                                 {
1222                                         GtkWidget* button = gtk_radio_button_new_with_label( group, "Bevel" );
1223                                         gtk_widget_show( button );
1224                                         gtk_table_attach( table, button, 1, 2, 0, 1,
1225                                                                           (GtkAttachOptions) ( GTK_FILL | GTK_EXPAND ),
1226                                                                           (GtkAttachOptions) ( 0 ), 0, 0 );
1227                                         group = gtk_radio_button_group( GTK_RADIO_BUTTON( button ) );
1228
1229                                         bevel = button;
1230                                 }
1231                                 {
1232                                         GtkWidget* button = gtk_radio_button_new_with_label( group, "Endcap" );
1233                                         gtk_widget_show( button );
1234                                         gtk_table_attach( table, button, 1, 2, 1, 2,
1235                                                                           (GtkAttachOptions) ( GTK_FILL | GTK_EXPAND ),
1236                                                                           (GtkAttachOptions) ( 0 ), 0, 0 );
1237                                         group = gtk_radio_button_group( GTK_RADIO_BUTTON( button ) );
1238
1239                                         endcap = button;
1240                                 }
1241                                 {
1242                                         GtkWidget* button = gtk_radio_button_new_with_label( group, "Inverted Bevel" );
1243                                         gtk_widget_show( button );
1244                                         gtk_table_attach( table, button, 1, 2, 2, 3,
1245                                                                           (GtkAttachOptions) ( GTK_FILL | GTK_EXPAND ),
1246                                                                           (GtkAttachOptions) ( 0 ), 0, 0 );
1247                                         group = gtk_radio_button_group( GTK_RADIO_BUTTON( button ) );
1248
1249                                         ibevel = button;
1250                                 }
1251                                 {
1252                                         GtkWidget* button = gtk_radio_button_new_with_label( group, "Inverted Endcap" );
1253                                         gtk_widget_show( button );
1254                                         gtk_table_attach( table, button, 1, 2, 3, 4,
1255                                                                           (GtkAttachOptions) ( GTK_FILL | GTK_EXPAND ),
1256                                                                           (GtkAttachOptions) ( 0 ), 0, 0 );
1257                                         group = gtk_radio_button_group( GTK_RADIO_BUTTON( button ) );
1258
1259                                         iendcap = button;
1260                                 }
1261                                 {
1262                                         GtkWidget* button = gtk_radio_button_new_with_label( group, "Cylinder" );
1263                                         gtk_widget_show( button );
1264                                         gtk_table_attach( table, button, 1, 2, 4, 5,
1265                                                                           (GtkAttachOptions) ( GTK_FILL | GTK_EXPAND ),
1266                                                                           (GtkAttachOptions) ( 0 ), 0, 0 );
1267                                         group = gtk_radio_button_group( GTK_RADIO_BUTTON( button ) );
1268
1269                                         cylinder = button;
1270                                 }
1271                         }
1272                 }
1273
1274                 {
1275                         GtkVBox* vbox = create_dialog_vbox( 4 );
1276                         gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), FALSE, FALSE, 0 );
1277                         {
1278                                 GtkButton* button = create_modal_dialog_button( "OK", ok_button );
1279                                 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1280                                 widget_make_default( GTK_WIDGET( button ) );
1281                                 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel_group, GDK_Return, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
1282                         }
1283                         {
1284                                 GtkButton* button = create_modal_dialog_button( "Cancel", cancel_button );
1285                                 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1286                                 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel_group, GDK_Escape, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
1287                         }
1288                 }
1289         }
1290
1291         // Initialize dialog
1292         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( bevel ), TRUE );
1293
1294         EMessageBoxReturn ret = modal_dialog_show( window, dialog );
1295         if ( ret == eIDOK ) {
1296                 if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( bevel ) ) ) {
1297                         *type = PATCHCAP_BEVEL;
1298                 }
1299                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( endcap ) ) ) {
1300                         *type = PATCHCAP_ENDCAP;
1301                 }
1302                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( ibevel ) ) ) {
1303                         *type = PATCHCAP_INVERTED_BEVEL;
1304                 }
1305                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( iendcap ) ) ) {
1306                         *type = PATCHCAP_INVERTED_ENDCAP;
1307                 }
1308                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( cylinder ) ) ) {
1309                         *type = PATCHCAP_CYLINDER;
1310                 }
1311         }
1312
1313         gtk_widget_destroy( GTK_WIDGET( window ) );
1314
1315         return ret;
1316 }
1317
1318
1319 void DoPatchThickenDlg(){
1320         ModalDialog dialog;
1321         GtkWidget* thicknessW;
1322         GtkWidget* seamsW;
1323         GtkWidget* radX;
1324         GtkWidget* radY;
1325         GtkWidget* radZ;
1326         GtkWidget* radNormals;
1327
1328         GtkWindow* window = create_dialog_window( MainFrame_getWindow(), "Patch thicken", G_CALLBACK( dialog_delete_callback ), &dialog );
1329
1330         GtkAccelGroup* accel = gtk_accel_group_new();
1331         gtk_window_add_accel_group( window, accel );
1332
1333         {
1334                 GtkHBox* hbox = create_dialog_hbox( 4, 4 );
1335                 gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( hbox ) );
1336                 {
1337                         GtkTable* table = create_dialog_table( 2, 4, 4, 4 );
1338                         gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
1339                         {
1340                                 GtkLabel* label = GTK_LABEL( gtk_label_new( "Thickness:" ) );
1341                                 gtk_widget_show( GTK_WIDGET( label ) );
1342                                 gtk_table_attach( table, GTK_WIDGET( label ), 0, 1, 0, 1,
1343                                                                   (GtkAttachOptions) ( GTK_FILL ),
1344                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1345                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
1346                         }
1347                         {
1348                                 GtkWidget* entry = gtk_entry_new();
1349                                 gtk_entry_set_text( GTK_ENTRY( entry ), "16" );
1350                                 gtk_widget_set_size_request( entry, 40, -1 );
1351                                 gtk_widget_show( entry );
1352                                 gtk_table_attach( table, entry, 1, 2, 0, 1,
1353                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1354                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1355
1356                                 thicknessW = entry;
1357                         }
1358                         {
1359                                 // Create the "create seams" label
1360                                 GtkWidget* _seamsCheckBox = gtk_check_button_new_with_label( "Side walls" );
1361                                 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( _seamsCheckBox ), TRUE );
1362                                 gtk_widget_show( _seamsCheckBox );
1363                                 gtk_table_attach( table, _seamsCheckBox, 3, 4, 0, 1,
1364                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1365                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1366                                 seamsW = _seamsCheckBox;
1367
1368                         }
1369                         {
1370                                 // Create the radio button group for choosing the extrude axis
1371                                 GtkWidget* _radNormals = gtk_radio_button_new_with_label( NULL, "Normal" );
1372                                 GtkWidget* _radX = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(_radNormals), "X" );
1373                                 GtkWidget* _radY = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(_radNormals), "Y" );
1374                                 GtkWidget* _radZ = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(_radNormals), "Z" );
1375                                 gtk_widget_show( _radNormals );
1376                                 gtk_widget_show( _radX );
1377                                 gtk_widget_show( _radY );
1378                                 gtk_widget_show( _radZ );
1379
1380
1381                                 // Pack the buttons into the table
1382                                 gtk_table_attach( table, _radNormals, 0, 1, 1, 2,
1383                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1384                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1385                                 gtk_table_attach( table, _radX, 1, 2, 1, 2,
1386                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1387                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1388                                 gtk_table_attach( table, _radY, 2, 3, 1, 2,
1389                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1390                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1391                                 gtk_table_attach( table, _radZ, 3, 4, 1, 2,
1392                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1393                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1394                                 radX = _radX;
1395                                 radY = _radY;
1396                                 radZ = _radZ;
1397                                 radNormals = _radNormals;
1398                         }
1399                 }
1400                 {
1401                         GtkVBox* vbox = create_dialog_vbox( 4 );
1402                         gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), TRUE, TRUE, 0 );
1403                         {
1404                                 GtkButton* button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
1405                                 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1406                                 widget_make_default( GTK_WIDGET( button ) );
1407                                 gtk_widget_grab_focus( GTK_WIDGET( button ) );
1408                                 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
1409                         }
1410                         {
1411                                 GtkButton* button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
1412                                 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
1413                                 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
1414                         }
1415                 }
1416         }
1417
1418         if ( modal_dialog_show( window, dialog ) == eIDOK ) {
1419                 int axis;
1420                 bool seams;
1421                 float thickness = static_cast<float>( atoi( gtk_entry_get_text( GTK_ENTRY( thicknessW ) ) ) );
1422                 seams = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON( seamsW )) ? true : false;
1423
1424                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radX))) {
1425                         axis = 0;
1426                 }
1427                 else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radY))) {
1428                         axis = 1;
1429                 }
1430                 else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radZ))) {
1431                         axis = 2;
1432                 }
1433                 else  {
1434                         // Extrude along normals
1435                         axis = 3;
1436                 }
1437                 Scene_PatchThicken( GlobalSceneGraph(), thickness, seams, axis );
1438         }
1439
1440         gtk_widget_destroy( GTK_WIDGET( window ) );
1441 }