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