]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/patchmanip.cpp
Merge branch 'NateEag-master-patch-12920' into 'master'
[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) );
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( "RedisperseRows", makeCallbackF(Patch_RedisperseRows) );
809         // GlobalCommands_insert( "RedisperseCols", makeCallbackF(Patch_RedisperseCols), Accelerator( 'E', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
810         GlobalCommands_insert( "RedisperseCols", makeCallbackF(Patch_RedisperseCols) );
811         GlobalCommands_insert( "SmoothRows", makeCallbackF(Patch_SmoothRows), Accelerator( 'W', (GdkModifierType)GDK_CONTROL_MASK ) );
812         GlobalCommands_insert( "SmoothCols", makeCallbackF(Patch_SmoothCols), Accelerator( 'W', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
813         GlobalCommands_insert( "MatrixTranspose", makeCallbackF(Patch_Transpose), Accelerator( 'M', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
814         GlobalCommands_insert( "CapCurrentCurve", makeCallbackF(Patch_Cap), Accelerator( 'C', (GdkModifierType)GDK_SHIFT_MASK ) );
815         GlobalCommands_insert( "CycleCapTexturePatch", makeCallbackF(Patch_CycleProjection), Accelerator( 'N', (GdkModifierType)GDK_SHIFT_MASK ) );
816 //      GlobalCommands_insert( "MakeOverlayPatch", makeCallbackF(Patch_OverlayOn), Accelerator( 'Y' ) );
817 //      GlobalCommands_insert( "ClearPatchOverlays", makeCallbackF(Patch_OverlayOff), Accelerator( 'L', (GdkModifierType)GDK_CONTROL_MASK ) );
818         GlobalCommands_insert( "PatchDeform", makeCallbackF(Patch_Deform) );
819         GlobalCommands_insert( "PatchThicken", makeCallbackF(Patch_Thicken), Accelerator( 'T', (GdkModifierType)GDK_CONTROL_MASK ) );
820 }
821
822 void Patch_constructToolbar( ui::Toolbar toolbar ){
823         toolbar_append_button( toolbar, "Put caps on the current patch (SHIFT + C)", "cap_curve.png", "CapCurrentCurve" );
824 }
825
826 void Patch_constructMenu( ui::Menu menu ){
827         create_menu_item_with_mnemonic( menu, "Simple Patch Mesh...", "SimplePatchMesh" );
828         create_menu_item_with_mnemonic( menu, "Bevel", "PatchBevel" );
829         create_menu_item_with_mnemonic( menu, "End cap", "PatchEndCap" );
830         create_menu_item_with_mnemonic( menu, "Cylinder (9x3)", "PatchCylinder" );
831         create_menu_item_with_mnemonic( menu, "Square Cylinder (9x3)", "PatchSquareCylinder" );
832         create_menu_item_with_mnemonic( menu, "Exact Cylinder...", "PatchXactCylinder" );
833         create_menu_item_with_mnemonic( menu, "Cone (9x3)", "PatchCone" );
834         create_menu_item_with_mnemonic( menu, "Exact Cone...", "PatchXactCone" );
835         create_menu_item_with_mnemonic( menu, "Sphere (9x5)", "PatchSphere" );
836         create_menu_item_with_mnemonic( menu, "Exact Sphere...", "PatchXactSphere" );
837 //      {
838 //              ui::Menu menu_in_menu = create_sub_menu_with_mnemonic( menu, "More Cylinders" );
839 //              if ( g_Layout_enableDetachableMenus.m_value ) {
840 //                      menu_tearoff( menu_in_menu );
841 //              }
842 //              create_menu_item_with_mnemonic( menu_in_menu, "Dense Cylinder", "PatchDenseCylinder" );
843 //              create_menu_item_with_mnemonic( menu_in_menu, "Very Dense Cylinder", "PatchVeryDenseCylinder" );
844 //              create_menu_item_with_mnemonic( menu_in_menu, "Square Cylinder", "PatchSquareCylinder" );
845 //      }
846 //      {
847 //              //not implemented
848 //              create_menu_item_with_mnemonic( menu, "Square Endcap", "PatchSquareBevel" );
849 //              create_menu_item_with_mnemonic( menu, "Square Bevel", "PatchSquareEndcap" );
850 //      }
851         menu_separator( menu );
852         create_menu_item_with_mnemonic( menu, "Cap Selection", "CapCurrentCurve" );
853         menu_separator( menu );
854         {
855                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Insert/Delete" );
856                 if ( g_Layout_enableDetachableMenus.m_value ) {
857                         menu_tearoff( menu_in_menu );
858                 }
859                 create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) First Columns", "PatchInsertFirstColumn" );
860                 create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Last Columns", "PatchInsertLastColumn" );
861                 menu_separator( menu_in_menu );
862                 create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) First Rows", "PatchInsertFirstRow" );
863                 create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Last Rows", "PatchInsertLastRow" );
864                 menu_separator( menu_in_menu );
865                 create_menu_item_with_mnemonic( menu_in_menu, "Del First (2) Columns", "PatchDeleteFirstColumn" );
866                 create_menu_item_with_mnemonic( menu_in_menu, "Del Last (2) Columns", "PatchDeleteLastColumn" );
867                 menu_separator( menu_in_menu );
868                 create_menu_item_with_mnemonic( menu_in_menu, "Del First (2) Rows", "PatchDeleteFirstRow" );
869                 create_menu_item_with_mnemonic( menu_in_menu, "Del Last (2) Rows", "PatchDeleteLastRow" );
870         }
871         menu_separator( menu );
872         {
873                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Matrix" );
874                 if ( g_Layout_enableDetachableMenus.m_value ) {
875                         menu_tearoff( menu_in_menu );
876                 }
877                 create_menu_item_with_mnemonic( menu_in_menu, "Invert", "InvertCurve" );
878                 create_menu_item_with_mnemonic( menu_in_menu, "Transpose", "MatrixTranspose" );
879
880                 menu_separator( menu_in_menu );
881                 create_menu_item_with_mnemonic( menu_in_menu, "Re-disperse Rows", "RedisperseRows" );
882                 create_menu_item_with_mnemonic( menu_in_menu, "Re-disperse Columns", "RedisperseCols" );
883
884                 menu_separator( menu_in_menu );
885                 create_menu_item_with_mnemonic( menu_in_menu, "Smooth Rows", "SmoothRows" );
886                 create_menu_item_with_mnemonic( menu_in_menu, "Smooth Columns", "SmoothCols" );
887         }
888         menu_separator( menu );
889         {
890                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Texture" );
891                 if ( g_Layout_enableDetachableMenus.m_value ) {
892                         menu_tearoff( menu_in_menu );
893                 }
894                 create_menu_item_with_mnemonic( menu_in_menu, "Cycle Projection", "CycleCapTexturePatch" );
895                 create_menu_item_with_mnemonic( menu_in_menu, "Naturalize", "NaturalizePatch" );
896                 create_menu_item_with_mnemonic( menu_in_menu, "Invert X", "InvertCurveTextureX" );
897                 create_menu_item_with_mnemonic( menu_in_menu, "Invert Y", "InvertCurveTextureY" );
898
899         }
900 //      menu_separator( menu );
901 //      {
902 //              auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Overlay" );
903 //              if ( g_Layout_enableDetachableMenus.m_value ) {
904 //                      menu_tearoff( menu_in_menu );
905 //              }
906 //              create_menu_item_with_mnemonic( menu_in_menu, "Set", "MakeOverlayPatch" );
907 //              create_menu_item_with_mnemonic( menu_in_menu, "Clear", "ClearPatchOverlays" );
908 //      }
909         menu_separator( menu );
910         create_menu_item_with_mnemonic( menu, "Deform...", "PatchDeform" );
911         create_menu_item_with_mnemonic( menu, "Thicken...", "PatchThicken" );
912 }
913
914
915 #include "gtkutil/dialog.h"
916 #include "gtkutil/widget.h"
917
918 void DoNewPatchDlg( EPatchPrefab prefab, int minrows, int mincols, int defrows, int defcols, int maxrows, int maxcols ){
919         ModalDialog dialog;
920         GtkWidget* redisperseCheckBox;
921
922         ui::Window window = MainFrame_getWindow().create_dialog_window("Patch density", G_CALLBACK(dialog_delete_callback ), &dialog );
923
924         auto accel = ui::AccelGroup(ui::New);
925         window.add_accel_group( accel );
926         auto width = ui::ComboBoxText(ui::New);
927         auto height = ui::ComboBoxText(ui::New);
928         {
929                 auto hbox = create_dialog_hbox( 4, 4 );
930                 window.add(hbox);
931                 {
932                         auto table = create_dialog_table( 3, 2, 4, 4 );
933                         hbox.pack_start( table, TRUE, TRUE, 0 );
934                         {
935                                 auto label = ui::Label( "Width:" );
936                                 label.show();
937                 table.attach(label, {0, 1, 0, 1}, {GTK_FILL, 0});
938                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
939                         }
940                         {
941                                 auto label = ui::Label( "Height:" );
942                                 label.show();
943                 table.attach(label, {0, 1, 1, 2}, {GTK_FILL, 0});
944                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
945                         }
946
947                         {
948                                 auto combo = width;
949 #define D_ITEM( x ) if ( x >= mincols && ( !maxcols || x <= maxcols ) ) gtk_combo_box_text_append_text( combo, #x )
950                                 D_ITEM( 3 );
951                                 D_ITEM( 5 );
952                                 D_ITEM( 7 );
953                                 D_ITEM( 9 );
954                                 D_ITEM( 11 );
955                                 D_ITEM( 13 );
956                                 D_ITEM( 15 );
957                                 D_ITEM( 17 );
958                                 D_ITEM( 19 );
959                                 D_ITEM( 21 );
960                                 D_ITEM( 23 );
961                                 D_ITEM( 25 );
962                                 D_ITEM( 27 );
963                                 D_ITEM( 29 );
964                                 D_ITEM( 31 ); // MAX_PATCH_SIZE is 32, so we should be able to do 31...
965 #undef D_ITEM
966                                 combo.show();
967                 table.attach(combo, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
968                         }
969                         {
970                                 auto combo = height;
971 #define D_ITEM( x ) if ( x >= minrows && ( !maxrows || x <= maxrows ) ) gtk_combo_box_text_append_text( combo, #x )
972                                 D_ITEM( 3 );
973                                 D_ITEM( 5 );
974                                 D_ITEM( 7 );
975                                 D_ITEM( 9 );
976                                 D_ITEM( 11 );
977                                 D_ITEM( 13 );
978                                 D_ITEM( 15 );
979                                 D_ITEM( 17 );
980                                 D_ITEM( 19 );
981                                 D_ITEM( 21 );
982                                 D_ITEM( 23 );
983                                 D_ITEM( 25 );
984                                 D_ITEM( 27 );
985                                 D_ITEM( 29 );
986                                 D_ITEM( 31 ); // MAX_PATCH_SIZE is 32, so we should be able to do 31...
987 #undef D_ITEM
988                                 combo.show();
989                 table.attach(combo, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
990                         }
991
992                         if( prefab != ePlane ){
993                                 GtkWidget* _redisperseCheckBox = gtk_check_button_new_with_label( "Square" );
994                                 gtk_widget_set_tooltip_text( _redisperseCheckBox, "Redisperse columns & rows" );
995                                 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( _redisperseCheckBox ), FALSE );
996                                 gtk_widget_show( _redisperseCheckBox );
997                                 gtk_table_attach( table, _redisperseCheckBox, 0, 2, 2, 3,
998                                                                 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
999                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
1000                                 redisperseCheckBox = _redisperseCheckBox;
1001                         }
1002
1003                 }
1004
1005                 {
1006                         auto vbox = create_dialog_vbox( 4 );
1007                         hbox.pack_start( vbox, TRUE, TRUE, 0 );
1008                         {
1009                                 auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
1010                                 vbox.pack_start( button, FALSE, FALSE, 0 );
1011                                 widget_make_default( button );
1012                                 gtk_widget_grab_focus( button  );
1013                                 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
1014                         }
1015                         {
1016                                 auto button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
1017                                 vbox.pack_start( button, FALSE, FALSE, 0 );
1018                                 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
1019                         }
1020                 }
1021         }
1022
1023         // Initialize dialog
1024         gtk_combo_box_set_active( width, ( defcols - mincols ) / 2 );
1025         gtk_combo_box_set_active( height, ( defrows - minrows ) / 2 );
1026
1027         if ( modal_dialog_show( window, dialog ) == eIDOK ) {
1028                 int w = gtk_combo_box_get_active( width ) * 2 + mincols;
1029                 int h = gtk_combo_box_get_active( height ) * 2 + minrows;
1030                 bool redisperse = false;
1031                 if( prefab != ePlane ){
1032                         redisperse = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( redisperseCheckBox ) ) ? true : false;
1033                 }
1034                 Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), prefab, GlobalXYWnd_getCurrentViewType(), w, h, redisperse );
1035         }
1036
1037         window.destroy();
1038 }
1039
1040
1041 void DoPatchDeformDlg(){
1042         ModalDialog dialog;
1043         GtkWidget* deformW;
1044
1045     GtkWidget* rndY;
1046         GtkWidget* rndX;
1047
1048         ui::Window window = create_dialog_window( MainFrame_getWindow(), "Patch deform", G_CALLBACK( dialog_delete_callback ), &dialog );
1049
1050         GtkAccelGroup* accel = gtk_accel_group_new();
1051         gtk_window_add_accel_group( window, accel );
1052
1053         {
1054                 auto hbox = create_dialog_hbox( 4, 4 );
1055                 gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( hbox ) );
1056                 {
1057                         auto table = create_dialog_table( 2, 2, 4, 4 );
1058                         gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
1059                         {
1060                                 GtkLabel* label = GTK_LABEL( gtk_label_new( "Max deform:" ) );
1061                                 gtk_widget_show( GTK_WIDGET( label ) );
1062                                 gtk_table_attach( table, GTK_WIDGET( label ), 0, 1, 0, 1,
1063                                                                   (GtkAttachOptions) ( GTK_FILL ),
1064                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1065                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
1066                         }
1067 //                      {
1068 //                              GtkWidget* entry = gtk_entry_new();
1069 //                              gtk_entry_set_text( GTK_ENTRY( entry ), "64" );
1070 //                              gtk_widget_show( entry );
1071 //                              gtk_table_attach( table, entry, 1, 2, 0, 1,
1072 //                                                                (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1073 //                                                                (GtkAttachOptions) ( 0 ), 0, 0 );
1074 //
1075 //                              deformW = entry;
1076 //                      }
1077                         {
1078                                 GtkAdjustment* adj = GTK_ADJUSTMENT( gtk_adjustment_new( 64, -9999, 9999, 1, 10, 0 ) );
1079                                 //GtkSpinButton* spin = GTK_SPIN_BUTTON( gtk_spin_button_new( adj, 1, 0 ) );
1080                                 GtkWidget* spin = gtk_spin_button_new( adj, 1, 0 );
1081                                 gtk_widget_show( spin );
1082                                 gtk_table_attach( table, spin, 1, 2, 0, 1,
1083                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1084                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1085                                 gtk_widget_set_size_request( spin, 64, -1 );
1086                                 gtk_spin_button_set_numeric( GTK_SPIN_BUTTON( spin ), TRUE );
1087
1088                                 deformW = spin;
1089                         }
1090                         {
1091                                 // Create the radio button group for choosing the axis
1092                                 GtkWidget* _rndZ = gtk_radio_button_new_with_label_from_widget( NULL, "Z" );
1093                                 GtkWidget* _rndY = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(_rndZ), "Y" );
1094                                 GtkWidget* _rndX = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(_rndZ), "X" );
1095                                 gtk_widget_show( _rndZ );
1096                                 gtk_widget_show( _rndY );
1097                                 gtk_widget_show( _rndX );
1098
1099
1100                                 GtkHBox* _hbox = create_dialog_hbox( 4, 4 );
1101                                 gtk_table_attach( table, GTK_WIDGET( _hbox ), 0, 2, 1, 2,
1102                                                                   (GtkAttachOptions) ( GTK_FILL ),
1103                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1104                                 gtk_box_pack_start( GTK_BOX( _hbox ), GTK_WIDGET( _rndX ), TRUE, TRUE, 0 );
1105                                 gtk_box_pack_start( GTK_BOX( _hbox ), GTK_WIDGET( _rndY ), TRUE, TRUE, 0 );
1106                                 gtk_box_pack_start( GTK_BOX( _hbox ), GTK_WIDGET( _rndZ ), TRUE, TRUE, 0 );
1107
1108                                 rndX = _rndX;
1109                                 rndY = _rndY;
1110                         }
1111                 }
1112                 {
1113                         auto vbox = create_dialog_vbox( 4 );
1114                         hbox.pack_start( vbox, FALSE, FALSE, 0 );
1115                         {
1116                                 auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
1117                                 vbox.pack_start( button, FALSE, FALSE, 0 );
1118                                 widget_make_default( button );
1119                                 gtk_widget_grab_focus( button  );
1120                                 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
1121                         }
1122                         {
1123                                 auto button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
1124                                 vbox.pack_start( button, FALSE, FALSE, 0 );
1125                                 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
1126                         }
1127                 }
1128         }
1129
1130         if ( modal_dialog_show( window, dialog ) == eIDOK ) {
1131                 //int deform = static_cast<int>( atoi( gtk_entry_get_text( GTK_ENTRY( deformW ) ) ) );
1132                 gtk_spin_button_update ( GTK_SPIN_BUTTON( deformW ) );
1133                 int deform = static_cast<int>( gtk_spin_button_get_value( GTK_SPIN_BUTTON( deformW ) ) );
1134                 int axis = 2; //Z
1135                 if( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( rndX ) ) ){
1136                         axis = 0;
1137                 }
1138                 else if( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( rndY ) ) ){
1139                         axis = 1;
1140                 }
1141                 Scene_PatchDeform( GlobalSceneGraph(), deform, axis );
1142         }
1143         gtk_widget_destroy( GTK_WIDGET( window ) );
1144 }
1145
1146
1147
1148 EMessageBoxReturn DoCapDlg( ECapDialog* type ){
1149         ModalDialog dialog;
1150         ModalDialogButton ok_button( dialog, eIDOK );
1151         ModalDialogButton cancel_button( dialog, eIDCANCEL );
1152         ui::Widget bevel{ui::null};
1153         ui::Widget ibevel{ui::null};
1154         ui::Widget endcap{ui::null};
1155         ui::Widget iendcap{ui::null};
1156         ui::Widget cylinder{ui::null};
1157
1158         ui::Window window = MainFrame_getWindow().create_modal_dialog_window( "Cap", dialog );
1159
1160         auto accel_group = ui::AccelGroup(ui::New);
1161         window.add_accel_group( accel_group );
1162
1163         {
1164                 auto hbox = create_dialog_hbox( 4, 4 );
1165                 window.add(hbox);
1166
1167                 {
1168                         // Gef: Added a vbox to contain the toggle buttons
1169                         auto radio_vbox = create_dialog_vbox( 4 );
1170                         hbox.add(radio_vbox);
1171
1172                         {
1173                                 auto table = ui::Table( 5, 2, FALSE );
1174                                 table.show();
1175                                 radio_vbox.pack_start( table, TRUE, TRUE, 0 );
1176                                 gtk_table_set_row_spacings( table, 5 );
1177                                 gtk_table_set_col_spacings( table, 5 );
1178
1179                                 {
1180                                         auto image = new_local_image( "cap_bevel.png" );
1181                                         image.show();
1182                     table.attach(image, {0, 1, 0, 1}, {GTK_FILL, 0});
1183                                 }
1184                                 {
1185                                         auto image = new_local_image( "cap_endcap.png" );
1186                                         image.show();
1187                     table.attach(image, {0, 1, 1, 2}, {GTK_FILL, 0});
1188                                 }
1189                                 {
1190                                         auto image = new_local_image( "cap_ibevel.png" );
1191                                         image.show();
1192                     table.attach(image, {0, 1, 2, 3}, {GTK_FILL, 0});
1193                                 }
1194                                 {
1195                                         auto image = new_local_image( "cap_iendcap.png" );
1196                                         image.show();
1197                     table.attach(image, {0, 1, 3, 4}, {GTK_FILL, 0});
1198                                 }
1199                                 {
1200                                         auto image = new_local_image( "cap_cylinder.png" );
1201                                         image.show();
1202                     table.attach(image, {0, 1, 4, 5}, {GTK_FILL, 0});
1203                                 }
1204
1205                                 GSList* group = 0;
1206                                 {
1207                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Bevel" ));
1208                                         button.show();
1209                     table.attach(button, {1, 2, 0, 1}, {GTK_FILL | GTK_EXPAND, 0});
1210                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
1211
1212                                         bevel = button;
1213                                 }
1214                                 {
1215                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Endcap" ));
1216                                         button.show();
1217                     table.attach(button, {1, 2, 1, 2}, {GTK_FILL | GTK_EXPAND, 0});
1218                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
1219
1220                                         endcap = button;
1221                                 }
1222                                 {
1223                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Inverted Bevel" ));
1224                                         button.show();
1225                     table.attach(button, {1, 2, 2, 3}, {GTK_FILL | GTK_EXPAND, 0});
1226                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
1227
1228                                         ibevel = button;
1229                                 }
1230                                 {
1231                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Inverted Endcap" ));
1232                                         button.show();
1233                     table.attach(button, {1, 2, 3, 4}, {GTK_FILL | GTK_EXPAND, 0});
1234                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
1235
1236                                         iendcap = button;
1237                                 }
1238                                 {
1239                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Cylinder" ));
1240                                         button.show();
1241                     table.attach(button, {1, 2, 4, 5}, {GTK_FILL | GTK_EXPAND, 0});
1242                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
1243
1244                                         cylinder = button;
1245                                 }
1246                         }
1247                 }
1248
1249                 {
1250                         auto vbox = create_dialog_vbox( 4 );
1251                         hbox.pack_start( vbox, FALSE, FALSE, 0 );
1252                         {
1253                                 auto button = create_modal_dialog_button( "OK", ok_button );
1254                                 vbox.pack_start( button, FALSE, FALSE, 0 );
1255                                 widget_make_default( button );
1256                                 gtk_widget_add_accelerator( button , "clicked", accel_group, GDK_KEY_Return, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
1257                         }
1258                         {
1259                                 auto button = create_modal_dialog_button( "Cancel", cancel_button );
1260                                 vbox.pack_start( button, FALSE, FALSE, 0 );
1261                                 gtk_widget_add_accelerator( button , "clicked", accel_group, GDK_KEY_Escape, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
1262                         }
1263                 }
1264         }
1265
1266         // Initialize dialog
1267         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( bevel ), TRUE );
1268
1269         EMessageBoxReturn ret = modal_dialog_show( window, dialog );
1270         if ( ret == eIDOK ) {
1271                 if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( bevel ) ) ) {
1272                         *type = PATCHCAP_BEVEL;
1273                 }
1274                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( endcap ) ) ) {
1275                         *type = PATCHCAP_ENDCAP;
1276                 }
1277                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( ibevel ) ) ) {
1278                         *type = PATCHCAP_INVERTED_BEVEL;
1279                 }
1280                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( iendcap ) ) ) {
1281                         *type = PATCHCAP_INVERTED_ENDCAP;
1282                 }
1283                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( cylinder ) ) ) {
1284                         *type = PATCHCAP_CYLINDER;
1285                 }
1286         }
1287
1288         window.destroy();
1289
1290         return ret;
1291 }
1292
1293
1294 void DoPatchThickenDlg(){
1295         ModalDialog dialog;
1296         GtkWidget* thicknessW;
1297         GtkWidget* seamsW;
1298         GtkWidget* radX;
1299         GtkWidget* radY;
1300         GtkWidget* radZ;
1301
1302         ui::Window window = create_dialog_window( MainFrame_getWindow(), "Patch thicken", G_CALLBACK( dialog_delete_callback ), &dialog );
1303
1304         GtkAccelGroup* accel = gtk_accel_group_new();
1305         gtk_window_add_accel_group( window, accel );
1306
1307         {
1308                 auto hbox = create_dialog_hbox( 4, 4 );
1309                 gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( hbox ) );
1310                 {
1311                         auto table = create_dialog_table( 2, 4, 4, 4 );
1312                         gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
1313                         {
1314                                 GtkLabel* label = GTK_LABEL( gtk_label_new( "Thickness:" ) );
1315                                 gtk_widget_show( GTK_WIDGET( label ) );
1316                                 gtk_table_attach( table, GTK_WIDGET( label ), 0, 1, 0, 1,
1317                                                                   (GtkAttachOptions) ( GTK_FILL ),
1318                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1319                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
1320                         }
1321 //                      {
1322 //                              GtkWidget* entry = gtk_entry_new();
1323 //                              gtk_entry_set_text( GTK_ENTRY( entry ), "16" );
1324 //                              gtk_widget_set_size_request( entry, 40, -1 );
1325 //                              gtk_widget_show( entry );
1326 //                              gtk_table_attach( table, entry, 1, 2, 0, 1,
1327 //                                                                (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1328 //                                                                (GtkAttachOptions) ( 0 ), 0, 0 );
1329 //
1330 //                              thicknessW = entry;
1331 //                      }
1332                         {
1333                                 GtkAdjustment* adj = GTK_ADJUSTMENT( gtk_adjustment_new( 16, -9999, 9999, 1, 10, 0 ) );
1334                                 GtkWidget* spin = gtk_spin_button_new( adj, 1, 0 );
1335                                 gtk_widget_show( spin );
1336                                 gtk_table_attach( table, spin, 1, 2, 0, 1,
1337                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1338                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1339                                 gtk_widget_set_size_request( spin, 48, -1 );
1340                                 gtk_spin_button_set_numeric( GTK_SPIN_BUTTON( spin ), TRUE );
1341
1342                                 thicknessW = spin;
1343                         }
1344                         {
1345                                 // Create the "create seams" label
1346                                 GtkWidget* _seamsCheckBox = gtk_check_button_new_with_label( "Side walls" );
1347                                 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( _seamsCheckBox ), TRUE );
1348                                 gtk_widget_show( _seamsCheckBox );
1349                                 gtk_table_attach( table, _seamsCheckBox, 2, 4, 0, 1,
1350                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1351                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1352                                 seamsW = _seamsCheckBox;
1353
1354                         }
1355                         {
1356                                 // Create the radio button group for choosing the extrude axis
1357                                 GtkWidget* _radNormals = gtk_radio_button_new_with_label( NULL, "Normal" );
1358                                 GtkWidget* _radX = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(_radNormals), "X" );
1359                                 GtkWidget* _radY = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(_radNormals), "Y" );
1360                                 GtkWidget* _radZ = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(_radNormals), "Z" );
1361                                 gtk_widget_show( _radNormals );
1362                                 gtk_widget_show( _radX );
1363                                 gtk_widget_show( _radY );
1364                                 gtk_widget_show( _radZ );
1365
1366
1367                                 // Pack the buttons into the table
1368                                 gtk_table_attach( table, _radNormals, 0, 1, 1, 2,
1369                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1370                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1371                                 gtk_table_attach( table, _radX, 1, 2, 1, 2,
1372                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1373                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1374                                 gtk_table_attach( table, _radY, 2, 3, 1, 2,
1375                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1376                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1377                                 gtk_table_attach( table, _radZ, 3, 4, 1, 2,
1378                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1379                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
1380                                 radX = _radX;
1381                                 radY = _radY;
1382                                 radZ = _radZ;
1383                         }
1384                 }
1385                 {
1386                         auto vbox = create_dialog_vbox( 4 );
1387                         hbox.pack_start( vbox, FALSE, FALSE, 0 );
1388                         {
1389                                 auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
1390                                 vbox.pack_start( button, FALSE, FALSE, 0 );
1391                                 widget_make_default( button );
1392                                 gtk_widget_grab_focus( button  );
1393                                 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
1394                         }
1395                         {
1396                                 auto button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
1397                                 vbox.pack_start( button, FALSE, FALSE, 0 );
1398                                 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
1399                         }
1400                 }
1401         }
1402
1403         if ( modal_dialog_show( window, dialog ) == eIDOK ) {
1404                 int axis = 3; // Extrude along normals
1405                 bool seams;
1406                 //float thickness = static_cast<float>( atoi( gtk_entry_get_text( GTK_ENTRY( thicknessW ) ) ) );
1407                 gtk_spin_button_update ( GTK_SPIN_BUTTON( thicknessW ) );
1408                 float thickness = static_cast<float>( gtk_spin_button_get_value( GTK_SPIN_BUTTON( thicknessW ) ) );
1409                 seams = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON( seamsW )) ? true : false;
1410
1411                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radX))) {
1412                         axis = 0;
1413                 }
1414                 else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radY))) {
1415                         axis = 1;
1416                 }
1417                 else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radZ))) {
1418                         axis = 2;
1419                 }
1420
1421                 Scene_PatchThicken( GlobalSceneGraph(), thickness, seams, axis );
1422         }
1423
1424         gtk_widget_destroy( GTK_WIDGET( window ) );
1425 }