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