]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/patchmanip.cpp
Merge commit 'eef39952025db1375e33b6e314692ac76badb93c' into garux-merge
[xonotic/netradiant.git] / radiant / patchmanip.cpp
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "patchmanip.h"
23
24 #include <gtk/gtk.h>
25 #include <gdk/gdkkeysyms.h>
26
27 #include "debugging/debugging.h"
28
29
30 #include "iselection.h"
31 #include "ipatch.h"
32
33 #include "math/vector.h"
34 #include "math/aabb.h"
35 #include "generic/callback.h"
36
37 #include "gtkutil/menu.h"
38 #include "gtkutil/image.h"
39 #include "map.h"
40 #include "mainframe.h"
41 #include "commands.h"
42 #include "gtkmisc.h"
43 #include "gtkdlgs.h"
44 #include "texwindow.h"
45 #include "xywindow.h"
46 #include "select.h"
47 #include "patch.h"
48 #include "grid.h"
49
50 PatchCreator* g_patchCreator = 0;
51
52 void Scene_PatchConstructPrefab( scene::Graph& graph, const AABB aabb, const char* shader, EPatchPrefab eType, int axis, std::size_t width = 3, std::size_t height = 3 ){
53         Select_Delete();
54         GlobalSelectionSystem().setSelectedAll( false );
55
56         NodeSmartReference node( g_patchCreator->createPatch() );
57         Node_getTraversable( Map_FindOrInsertWorldspawn( g_map ) )->insert( node );
58
59         Patch* patch = Node_getPatch( node );
60         patch->SetShader( shader );
61
62         patch->ConstructPrefab( aabb, eType, axis, width, height );
63         patch->controlPointsChanged();
64
65         {
66                 scene::Path patchpath( makeReference( GlobalSceneGraph().root() ) );
67                 patchpath.push( makeReference( *Map_GetWorldspawn( g_map ) ) );
68                 patchpath.push( makeReference( node.get() ) );
69                 Instance_getSelectable( *graph.find( patchpath ) )->setSelected( true );
70         }
71 }
72
73 void PatchAutoCapTexture( Patch& patch ) {
74
75         AABB box = patch.localAABB();
76         float x = box.extents.x();
77         float y = box.extents.y();
78         float z = box.extents.z();
79
80         int cap_direction = -1;
81         if ( x < y  && x < z )
82                 cap_direction = 0;
83         else if ( y < x  && y < z )
84                 cap_direction = 1;
85         else if ( z < x  && z < x )
86                 cap_direction = 2;
87
88         if ( cap_direction >= 0 )
89                 patch.ProjectTexture(cap_direction);
90         else
91                 patch.NaturalTexture();
92 }
93
94 void Patch_AutoCapTexture(){
95         UndoableCommand command( "patchAutoCapTexture" );
96         Scene_forEachVisibleSelectedPatch( &PatchAutoCapTexture );
97         SceneChangeNotify();
98 }
99
100 void Patch_makeCaps( Patch& patch, scene::Instance& instance, EPatchCap type, const char* shader ){
101         if ( ( type == eCapEndCap || type == eCapIEndCap )
102                  && patch.getWidth() != 5 ) {
103                 globalErrorStream() << "cannot create end-cap - patch width != 5\n";
104                 return;
105         }
106         if ( ( type == eCapBevel || type == eCapIBevel )
107                  && patch.getWidth() != 3 && patch.getWidth() != 5 ) {
108                 globalErrorStream() << "cannot create bevel-cap - patch width != 3\n";
109                 return;
110         }
111         if ( type == eCapCylinder
112                  && patch.getWidth() != 9 ) {
113                 globalErrorStream() << "cannot create cylinder-cap - patch width != 9\n";
114                 return;
115         }
116
117         {
118                 NodeSmartReference cap( g_patchCreator->createPatch() );
119                 Node_getTraversable( instance.path().parent() )->insert( cap );
120
121                 Patch* cap_patch = Node_getPatch( cap );
122                 patch.MakeCap( cap_patch, type, ROW, true );
123                 cap_patch->SetShader( shader );
124                 PatchAutoCapTexture(*cap_patch);
125
126                 scene::Path path( instance.path() );
127                 path.pop();
128                 path.push( makeReference( cap.get() ) );
129                 selectPath( path, true );
130         }
131
132         {
133                 NodeSmartReference cap( g_patchCreator->createPatch() );
134                 Node_getTraversable( instance.path().parent() )->insert( cap );
135
136                 Patch* cap_patch = Node_getPatch( cap );
137                 patch.MakeCap( cap_patch, type, ROW, false );
138                 cap_patch->SetShader( shader );
139                 PatchAutoCapTexture(*cap_patch);
140
141                 scene::Path path( instance.path() );
142                 path.pop();
143                 path.push( makeReference( cap.get() ) );
144                 selectPath( path, true );
145         }
146 }
147
148 typedef std::vector<scene::Instance*> InstanceVector;
149
150 enum ECapDialog {
151         PATCHCAP_BEVEL = 0,
152         PATCHCAP_ENDCAP,
153         PATCHCAP_INVERTED_BEVEL,
154         PATCHCAP_INVERTED_ENDCAP,
155         PATCHCAP_CYLINDER
156 };
157
158 EMessageBoxReturn DoCapDlg( ECapDialog *type );
159
160 void Scene_PatchDoCap_Selected( scene::Graph& graph, const char* shader ){
161         ECapDialog nType;
162
163         if ( DoCapDlg( &nType ) == eIDOK ) {
164                 EPatchCap eType;
165                 switch ( nType )
166                 {
167                 case PATCHCAP_INVERTED_BEVEL:
168                         eType = eCapIBevel;
169                         break;
170                 case PATCHCAP_BEVEL:
171                         eType = eCapBevel;
172                         break;
173                 case PATCHCAP_INVERTED_ENDCAP:
174                         eType = eCapIEndCap;
175                         break;
176                 case PATCHCAP_ENDCAP:
177                         eType = eCapEndCap;
178                         break;
179                 case PATCHCAP_CYLINDER:
180                         eType = eCapCylinder;
181                         break;
182                 default:
183                         ERROR_MESSAGE( "invalid patch cap type" );
184                         return;
185                 }
186
187                 InstanceVector instances;
188                 Scene_forEachVisibleSelectedPatchInstance([&](PatchInstance &patch) {
189                         instances.push_back(&patch);
190                 });
191                 for ( InstanceVector::const_iterator i = instances.begin(); i != instances.end(); ++i )
192                 {
193                         Patch_makeCaps( *Node_getPatch( ( *i )->path().top() ), *( *i ), eType, shader );
194                 }
195         }
196 }
197
198 Patch* Scene_GetUltimateSelectedVisiblePatch(){
199         if ( GlobalSelectionSystem().countSelected() != 0 ) {
200                 scene::Node& node = GlobalSelectionSystem().ultimateSelected().path().top();
201                 if ( node.visible() ) {
202                         return Node_getPatch( node );
203                 }
204         }
205         return 0;
206 }
207
208
209 void Scene_PatchCapTexture_Selected( scene::Graph& graph ){
210         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
211                 patch.ProjectTexture(Patch::m_CycleCapIndex);
212         });
213         Patch::m_CycleCapIndex = ( Patch::m_CycleCapIndex == 0 ) ? 1 : ( Patch::m_CycleCapIndex == 1 ) ? 2 : 0;
214         SceneChangeNotify();
215 }
216
217 void Scene_PatchFlipTexture_Selected( scene::Graph& graph, int axis ){
218         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
219                 patch.FlipTexture(axis);
220         });
221 }
222
223 void Scene_PatchNaturalTexture_Selected( scene::Graph& graph ){
224         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
225                 patch.NaturalTexture();
226         });
227         SceneChangeNotify();
228 }
229
230
231 void Scene_PatchInsertRemove_Selected( scene::Graph& graph, bool bInsert, bool bColumn, bool bFirst ){
232         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
233                 patch.InsertRemove(bInsert, bColumn, bFirst);
234         });
235 }
236
237 void Scene_PatchInvert_Selected( scene::Graph& graph ){
238         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
239                 patch.InvertMatrix();
240         });
241 }
242
243 void Scene_PatchRedisperse_Selected( scene::Graph& graph, EMatrixMajor major ){
244         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
245                 patch.Redisperse(major);
246         });
247 }
248
249 void Scene_PatchSmooth_Selected( scene::Graph& graph, EMatrixMajor major ){
250         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
251                 patch.Smooth(major);
252         });
253 }
254
255 void Scene_PatchTranspose_Selected( scene::Graph& graph ){
256         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
257                 patch.TransposeMatrix();
258         });
259 }
260
261 void Scene_PatchSetShader_Selected( scene::Graph& graph, const char* name ){
262         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
263                 patch.SetShader(name);
264         });
265         SceneChangeNotify();
266 }
267
268 void Scene_PatchGetShader_Selected( scene::Graph& graph, CopiedString& name ){
269         Patch* patch = Scene_GetUltimateSelectedVisiblePatch();
270         if ( patch != 0 ) {
271                 name = patch->GetShader();
272         }
273 }
274
275 void Scene_PatchSelectByShader( scene::Graph& graph, const char* name ){
276         Scene_forEachVisiblePatchInstance([&](PatchInstance &patch) {
277                 if (shader_equal(patch.getPatch().GetShader(), name)) {
278                         patch.setSelected(true);
279                 }
280         });
281 }
282
283
284 void Scene_PatchFindReplaceShader( scene::Graph& graph, const char* find, const char* replace ){
285         Scene_forEachVisiblePatch([&](Patch &patch) {
286                 if (shader_equal(patch.GetShader(), find)) {
287                         patch.SetShader(replace);
288                 }
289         });
290 }
291
292 void Scene_PatchFindReplaceShader_Selected( scene::Graph& graph, const char* find, const char* replace ){
293         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
294                 if (shader_equal(patch.GetShader(), find)) {
295                         patch.SetShader(replace);
296                 }
297         });
298 }
299
300
301 AABB PatchCreator_getBounds(){
302         AABB aabb( aabb_for_minmax( Select_getWorkZone().d_work_min, Select_getWorkZone().d_work_max ) );
303
304         float gridSize = GetGridSize();
305
306         if ( aabb.extents[0] == 0 ) {
307                 aabb.extents[0] = gridSize;
308         }
309         if ( aabb.extents[1] == 0 ) {
310                 aabb.extents[1] = gridSize;
311         }
312         if ( aabb.extents[2] == 0 ) {
313                 aabb.extents[2] = gridSize;
314         }
315
316         if ( aabb_valid( aabb ) ) {
317                 return aabb;
318         }
319         return AABB( Vector3( 0, 0, 0 ), Vector3( 64, 64, 64 ) );
320 }
321
322 void DoNewPatchDlg( EPatchPrefab prefab, int minrows, int mincols, int defrows, int defcols, int maxrows, int maxcols );
323
324 void Patch_XactCylinder(){
325         UndoableCommand undo( "patchCreateXactCylinder" );
326
327         DoNewPatchDlg( eXactCylinder, 3, 7, 3, 13, 0, 0 );
328 }
329
330 void Patch_XactSphere(){
331         UndoableCommand undo( "patchCreateXactSphere" );
332
333         DoNewPatchDlg( eXactSphere, 5, 7, 7, 13, 0, 0 );
334 }
335
336 void Patch_XactCone(){
337         UndoableCommand undo( "patchCreateXactCone" );
338
339         DoNewPatchDlg( eXactCone, 3, 7, 3, 13, 0, 0 );
340 }
341
342 void Patch_Cylinder(){
343         UndoableCommand undo( "patchCreateCylinder" );
344
345         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eCylinder, GlobalXYWnd_getCurrentViewType() );
346 }
347
348 void Patch_DenseCylinder(){
349         UndoableCommand undo( "patchCreateDenseCylinder" );
350
351         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eDenseCylinder, GlobalXYWnd_getCurrentViewType() );
352 }
353
354 void Patch_VeryDenseCylinder(){
355         UndoableCommand undo( "patchCreateVeryDenseCylinder" );
356
357         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eVeryDenseCylinder, GlobalXYWnd_getCurrentViewType() );
358 }
359
360 void Patch_SquareCylinder(){
361         UndoableCommand undo( "patchCreateSquareCylinder" );
362
363         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eSqCylinder, GlobalXYWnd_getCurrentViewType() );
364 }
365
366 void Patch_Endcap(){
367         UndoableCommand undo( "patchCreateCaps" );
368
369         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eEndCap, GlobalXYWnd_getCurrentViewType() );
370 }
371
372 void Patch_Bevel(){
373         UndoableCommand undo( "patchCreateBevel" );
374
375         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eBevel, GlobalXYWnd_getCurrentViewType() );
376 }
377
378 void Patch_Sphere(){
379         UndoableCommand undo( "patchCreateSphere" );
380
381         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eSphere, GlobalXYWnd_getCurrentViewType() );
382 }
383
384 void Patch_SquareBevel(){
385 }
386
387 void Patch_SquareEndcap(){
388 }
389
390 void Patch_Cone(){
391         UndoableCommand undo( "patchCreateCone" );
392
393         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eCone, GlobalXYWnd_getCurrentViewType() );
394 }
395
396 void Patch_Plane(){
397         UndoableCommand undo( "patchCreatePlane" );
398
399         DoNewPatchDlg( ePlane, 3, 3, 3, 3, 0, 0 );
400 }
401
402 void Patch_InsertInsertColumn(){
403         UndoableCommand undo( "patchInsertColumns" );
404
405         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), true, true, false );
406 }
407
408 void Patch_InsertAddColumn(){
409         UndoableCommand undo( "patchAddColumns" );
410
411         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), true, true, true );
412 }
413
414 void Patch_InsertInsertRow(){
415         UndoableCommand undo( "patchInsertRows" );
416
417         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), true, false, false );
418 }
419
420 void Patch_InsertAddRow(){
421         UndoableCommand undo( "patchAddRows" );
422
423         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), true, false, true );
424 }
425
426 void Patch_DeleteFirstColumn(){
427         UndoableCommand undo( "patchDeleteFirstColumns" );
428
429         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), false, true, true );
430 }
431
432 void Patch_DeleteLastColumn(){
433         UndoableCommand undo( "patchDeleteLastColumns" );
434
435         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), false, true, false );
436 }
437
438 void Patch_DeleteFirstRow(){
439         UndoableCommand undo( "patchDeleteFirstRows" );
440
441         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), false, false, true );
442 }
443
444 void Patch_DeleteLastRow(){
445         UndoableCommand undo( "patchDeleteLastRows" );
446
447         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), false, false, false );
448 }
449
450 void Patch_Invert(){
451         UndoableCommand undo( "patchInvert" );
452
453         Scene_PatchInvert_Selected( GlobalSceneGraph() );
454 }
455
456 void Patch_RedisperseRows(){
457         UndoableCommand undo( "patchRedisperseRows" );
458
459         Scene_PatchRedisperse_Selected( GlobalSceneGraph(), ROW );
460 }
461
462 void Patch_RedisperseCols(){
463         UndoableCommand undo( "patchRedisperseColumns" );
464
465         Scene_PatchRedisperse_Selected( GlobalSceneGraph(), COL );
466 }
467
468 void Patch_SmoothRows(){
469         UndoableCommand undo( "patchSmoothRows" );
470
471         Scene_PatchSmooth_Selected( GlobalSceneGraph(), ROW );
472 }
473
474 void Patch_SmoothCols(){
475         UndoableCommand undo( "patchSmoothColumns" );
476
477         Scene_PatchSmooth_Selected( GlobalSceneGraph(), COL );
478 }
479
480 void Patch_Transpose(){
481         UndoableCommand undo( "patchTranspose" );
482
483         Scene_PatchTranspose_Selected( GlobalSceneGraph() );
484 }
485
486 void Patch_Cap(){
487         // FIXME: add support for patch cap creation
488         // Patch_CapCurrent();
489         UndoableCommand undo( "patchCreateCaps" );
490
491         Scene_PatchDoCap_Selected( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
492 }
493
494 void Patch_CycleProjection(){
495         UndoableCommand undo( "patchCycleUVProjectionAxis" );
496
497         Scene_PatchCapTexture_Selected( GlobalSceneGraph() );
498 }
499
500 ///\todo Unfinished.
501 void Patch_OverlayOn(){
502 }
503
504 ///\todo Unfinished.
505 void Patch_OverlayOff(){
506 }
507
508 void Patch_FlipTextureX(){
509         UndoableCommand undo( "patchFlipTextureU" );
510
511         Scene_PatchFlipTexture_Selected( GlobalSceneGraph(), 0 );
512 }
513
514 void Patch_FlipTextureY(){
515         UndoableCommand undo( "patchFlipTextureV" );
516
517         Scene_PatchFlipTexture_Selected( GlobalSceneGraph(), 1 );
518 }
519
520 void Patch_NaturalTexture(){
521         UndoableCommand undo( "patchNaturalTexture" );
522
523         Scene_PatchNaturalTexture_Selected( GlobalSceneGraph() );
524 }
525
526 void Patch_CapTexture(){
527         UndoableCommand command( "patchCapTexture" );
528
529         Scene_PatchCapTexture_Selected( GlobalSceneGraph() );
530 }
531
532 void Patch_ResetTexture(){
533         float fx, fy;
534         if ( DoTextureLayout( &fx, &fy ) == eIDOK ) {
535                 UndoableCommand command( "patchTileTexture" );
536                 Scene_PatchTileTexture_Selected( GlobalSceneGraph(), fx, fy );
537         }
538 }
539
540 void Patch_FitTexture(){
541         UndoableCommand command( "patchFitTexture" );
542
543         Scene_PatchTileTexture_Selected( GlobalSceneGraph(), 1, 1 );
544 }
545
546 #include "ifilter.h"
547
548
549 class filter_patch_all : public PatchFilter
550 {
551 public:
552 bool filter( const Patch& patch ) const {
553         return true;
554 }
555 };
556
557 class filter_patch_shader : public PatchFilter
558 {
559 const char* m_shader;
560 public:
561 filter_patch_shader( const char* shader ) : m_shader( shader ){
562 }
563 bool filter( const Patch& patch ) const {
564         return shader_equal( patch.GetShader(), m_shader );
565 }
566 };
567
568 class filter_patch_flags : public PatchFilter
569 {
570 int m_flags;
571 public:
572 filter_patch_flags( int flags ) : m_flags( flags ){
573 }
574 bool filter( const Patch& patch ) const {
575         return ( patch.getShaderFlags() & m_flags ) != 0;
576 }
577 };
578
579
580 filter_patch_all g_filter_patch_all;
581 filter_patch_shader g_filter_patch_clip( "textures/common/clip" );
582 filter_patch_shader g_filter_patch_weapclip( "textures/common/weapclip" );
583 filter_patch_flags g_filter_patch_translucent( QER_TRANS );
584
585 void PatchFilters_construct(){
586         add_patch_filter( g_filter_patch_all, EXCLUDE_CURVES );
587         add_patch_filter( g_filter_patch_clip, EXCLUDE_CLIP );
588         add_patch_filter( g_filter_patch_weapclip, EXCLUDE_CLIP );
589         add_patch_filter( g_filter_patch_translucent, EXCLUDE_TRANSLUCENT );
590 }
591
592
593 #include "preferences.h"
594
595 void Patch_constructPreferences( PreferencesPage& page ){
596         page.appendEntry( "Patch Subdivide Threshold", g_PatchSubdivideThreshold );
597 }
598 void Patch_constructPage( PreferenceGroup& group ){
599         PreferencesPage page( group.createPage( "Patches", "Patch Display Preferences" ) );
600         Patch_constructPreferences( page );
601 }
602 void Patch_registerPreferencesPage(){
603         PreferencesDialog_addDisplayPage( makeCallbackF(Patch_constructPage) );
604 }
605
606
607 #include "preferencesystem.h"
608
609 void PatchPreferences_construct(){
610         GlobalPreferenceSystem().registerPreference( "Subdivisions", make_property_string( g_PatchSubdivideThreshold ) );
611 }
612
613
614 #include "generic/callback.h"
615
616 void Patch_registerCommands(){
617         GlobalCommands_insert( "InvertCurveTextureX", makeCallbackF(Patch_FlipTextureX), Accelerator( 'I', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
618         GlobalCommands_insert( "InvertCurveTextureY", makeCallbackF(Patch_FlipTextureY), Accelerator( 'I', (GdkModifierType)GDK_SHIFT_MASK ) );
619         GlobalCommands_insert( "IncPatchColumn", makeCallbackF(Patch_InsertInsertColumn), Accelerator( GDK_KP_Add, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
620         GlobalCommands_insert( "IncPatchRow", makeCallbackF(Patch_InsertInsertRow), Accelerator( GDK_KP_Add, (GdkModifierType)GDK_CONTROL_MASK ) );
621         GlobalCommands_insert( "DecPatchColumn", makeCallbackF(Patch_DeleteLastColumn), Accelerator( GDK_KP_Subtract, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
622         GlobalCommands_insert( "DecPatchRow", makeCallbackF(Patch_DeleteLastRow), Accelerator( GDK_KP_Subtract, (GdkModifierType)GDK_CONTROL_MASK ) );
623         GlobalCommands_insert( "NaturalizePatch", makeCallbackF(Patch_NaturalTexture), Accelerator( 'N', (GdkModifierType)GDK_CONTROL_MASK ) );
624         GlobalCommands_insert( "PatchCylinder", makeCallbackF(Patch_Cylinder) );
625         GlobalCommands_insert( "PatchDenseCylinder", makeCallbackF(Patch_DenseCylinder) );
626         GlobalCommands_insert( "PatchVeryDenseCylinder", makeCallbackF(Patch_VeryDenseCylinder) );
627         GlobalCommands_insert( "PatchSquareCylinder", makeCallbackF(Patch_SquareCylinder) );
628         GlobalCommands_insert( "PatchXactCylinder", makeCallbackF(Patch_XactCylinder) );
629         GlobalCommands_insert( "PatchXactSphere", makeCallbackF(Patch_XactSphere) );
630         GlobalCommands_insert( "PatchXactCone", makeCallbackF(Patch_XactCone) );
631         GlobalCommands_insert( "PatchEndCap", makeCallbackF(Patch_Endcap) );
632         GlobalCommands_insert( "PatchBevel", makeCallbackF(Patch_Bevel) );
633         GlobalCommands_insert( "PatchSquareBevel", makeCallbackF(Patch_SquareBevel) );
634         GlobalCommands_insert( "PatchSquareEndcap", makeCallbackF(Patch_SquareEndcap) );
635         GlobalCommands_insert( "PatchCone", makeCallbackF(Patch_Cone) );
636         GlobalCommands_insert( "PatchSphere", makeCallbackF(Patch_Sphere) );
637         GlobalCommands_insert( "SimplePatchMesh", makeCallbackF(Patch_Plane), Accelerator( 'P', (GdkModifierType)GDK_SHIFT_MASK ) );
638         GlobalCommands_insert( "PatchInsertInsertColumn", makeCallbackF(Patch_InsertInsertColumn), Accelerator( GDK_KEY_KP_Add, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
639         GlobalCommands_insert( "PatchInsertAddColumn", makeCallbackF(Patch_InsertAddColumn) );
640         GlobalCommands_insert( "PatchInsertInsertRow", makeCallbackF(Patch_InsertInsertRow), Accelerator( GDK_KEY_KP_Add, (GdkModifierType)GDK_CONTROL_MASK ) );
641         GlobalCommands_insert( "PatchInsertAddRow", makeCallbackF(Patch_InsertAddRow) );
642         GlobalCommands_insert( "PatchDeleteFirstColumn", makeCallbackF(Patch_DeleteFirstColumn) );
643         GlobalCommands_insert( "PatchDeleteLastColumn", makeCallbackF(Patch_DeleteLastColumn), Accelerator( GDK_KEY_KP_Subtract, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
644         GlobalCommands_insert( "PatchDeleteFirstRow", makeCallbackF(Patch_DeleteFirstRow), Accelerator( GDK_KEY_KP_Subtract, (GdkModifierType)GDK_CONTROL_MASK ) );
645         GlobalCommands_insert( "PatchDeleteLastRow", makeCallbackF(Patch_DeleteLastRow) );
646         GlobalCommands_insert( "InvertCurve", makeCallbackF(Patch_Invert), Accelerator( 'I', (GdkModifierType)GDK_CONTROL_MASK ) );
647         GlobalCommands_insert( "RedisperseRows", makeCallbackF(Patch_RedisperseRows), Accelerator( 'E', (GdkModifierType)GDK_CONTROL_MASK ) );
648         GlobalCommands_insert( "RedisperseCols", makeCallbackF(Patch_RedisperseCols), Accelerator( 'E', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
649         GlobalCommands_insert( "SmoothRows", makeCallbackF(Patch_SmoothRows), Accelerator( 'W', (GdkModifierType)GDK_CONTROL_MASK ) );
650         GlobalCommands_insert( "SmoothCols", makeCallbackF(Patch_SmoothCols), Accelerator( 'W', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
651         GlobalCommands_insert( "MatrixTranspose", makeCallbackF(Patch_Transpose), Accelerator( 'M', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
652         GlobalCommands_insert( "CapCurrentCurve", makeCallbackF(Patch_Cap), Accelerator( 'C', (GdkModifierType)GDK_SHIFT_MASK ) );
653         GlobalCommands_insert( "CycleCapTexturePatch", makeCallbackF(Patch_CycleProjection), Accelerator( 'N', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
654         GlobalCommands_insert( "MakeOverlayPatch", makeCallbackF(Patch_OverlayOn), Accelerator( 'Y' ) );
655         GlobalCommands_insert( "ClearPatchOverlays", makeCallbackF(Patch_OverlayOff), Accelerator( 'L', (GdkModifierType)GDK_CONTROL_MASK ) );
656 }
657
658 void Patch_constructToolbar( ui::Toolbar toolbar ){
659         toolbar_append_button( toolbar, "Put caps on the current patch (SHIFT + C)", "cap_curve.png", "CapCurrentCurve" );
660 }
661
662 void Patch_constructMenu( ui::Menu menu ){
663         create_menu_item_with_mnemonic( menu, "Cylinder", "PatchCylinder" );
664         {
665                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "More Cylinders" );
666                 if ( g_Layout_enableDetachableMenus.m_value ) {
667                         menu_tearoff( menu_in_menu );
668                 }
669                 create_menu_item_with_mnemonic( menu_in_menu, "Dense Cylinder", "PatchDenseCylinder" );
670                 create_menu_item_with_mnemonic( menu_in_menu, "Very Dense Cylinder", "PatchVeryDenseCylinder" );
671                 create_menu_item_with_mnemonic( menu_in_menu, "Square Cylinder", "PatchSquareCylinder" );
672                 create_menu_item_with_mnemonic( menu_in_menu, "Exact Cylinder...", "PatchXactCylinder" );
673         }
674         menu_separator( menu );
675         create_menu_item_with_mnemonic( menu, "End cap", "PatchEndCap" );
676         create_menu_item_with_mnemonic( menu, "Bevel", "PatchBevel" );
677         {
678 //              auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "More End caps, Bevels" );
679 //              if ( g_Layout_enableDetachableMenus.m_value ) {
680 //                      menu_tearoff( menu_in_menu );
681 //              }
682                 create_menu_item_with_mnemonic( menu, "Square Endcap", "PatchSquareBevel" );
683                 create_menu_item_with_mnemonic( menu, "Square Bevel", "PatchSquareEndcap" );
684         }
685         menu_separator( menu );
686         create_menu_item_with_mnemonic( menu, "Cone", "PatchCone" );
687         create_menu_item_with_mnemonic( menu, "Exact Cone...", "PatchXactCone" );
688         menu_separator( menu );
689         create_menu_item_with_mnemonic( menu, "Sphere", "PatchSphere" );
690         create_menu_item_with_mnemonic( menu, "Exact Sphere...", "PatchXactSphere" );
691         menu_separator( menu );
692         create_menu_item_with_mnemonic( menu, "Simple Patch Mesh...", "SimplePatchMesh" );
693         menu_separator( menu );
694         {
695                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Insert" );
696                 if ( g_Layout_enableDetachableMenus.m_value ) {
697                         menu_tearoff( menu_in_menu );
698                 }
699                 create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Columns", "PatchInsertInsertColumn" );
700                 create_menu_item_with_mnemonic( menu_in_menu, "Add (2) Columns", "PatchInsertAddColumn" );
701                 menu_separator( menu_in_menu );
702                 create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Rows", "PatchInsertInsertRow" );
703                 create_menu_item_with_mnemonic( menu_in_menu, "Add (2) Rows", "PatchInsertAddRow" );
704         }
705         {
706                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Delete" );
707                 if ( g_Layout_enableDetachableMenus.m_value ) {
708                         menu_tearoff( menu_in_menu );
709                 }
710                 create_menu_item_with_mnemonic( menu_in_menu, "First (2) Columns", "PatchDeleteFirstColumn" );
711                 create_menu_item_with_mnemonic( menu_in_menu, "Last (2) Columns", "PatchDeleteLastColumn" );
712                 menu_separator( menu_in_menu );
713                 create_menu_item_with_mnemonic( menu_in_menu, "First (2) Rows", "PatchDeleteFirstRow" );
714                 create_menu_item_with_mnemonic( menu_in_menu, "Last (2) Rows", "PatchDeleteLastRow" );
715         }
716         menu_separator( menu );
717         {
718                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Matrix" );
719                 if ( g_Layout_enableDetachableMenus.m_value ) {
720                         menu_tearoff( menu_in_menu );
721                 }
722                 create_menu_item_with_mnemonic( menu_in_menu, "Invert", "InvertCurve" );
723 //              auto menu_3 = create_sub_menu_with_mnemonic( menu_in_menu, "Re-disperse" );
724 //              if ( g_Layout_enableDetachableMenus.m_value ) {
725 //                      menu_tearoff( menu_3 );
726 //              }
727                 menu_separator( menu_in_menu );
728                 create_menu_item_with_mnemonic( menu, "Rows", "RedisperseRows" );
729                 create_menu_item_with_mnemonic( menu, "Columns", "RedisperseCols" );
730 //              auto menu_4 = create_sub_menu_with_mnemonic( menu_in_menu, "Smooth" );
731 //              if ( g_Layout_enableDetachableMenus.m_value ) {
732 //                      menu_tearoff( menu_4 );
733 //              }
734                 create_menu_item_with_mnemonic( menu, "Rows", "SmoothRows" );
735                 create_menu_item_with_mnemonic( menu, "Columns", "SmoothCols" );
736                 create_menu_item_with_mnemonic( menu_in_menu, "Transpose", "MatrixTranspose" );
737
738         }
739         menu_separator( menu );
740         create_menu_item_with_mnemonic( menu, "Cap Selection", "CapCurrentCurve" );
741         create_menu_item_with_mnemonic( menu, "Cycle Cap Texture", "CycleCapTexturePatch" );
742         menu_separator( menu );
743         {
744                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Texture" );
745                 if ( g_Layout_enableDetachableMenus.m_value ) {
746                         menu_tearoff( menu_in_menu );
747                 }
748                 create_menu_item_with_mnemonic( menu_in_menu, "Cycle Projection", "CycleCapTexturePatch" );
749                 create_menu_item_with_mnemonic( menu_in_menu, "Naturalize", "NaturalizePatch" );
750                 create_menu_item_with_mnemonic( menu_in_menu, "Invert X", "InvertCurveTextureX" );
751                 create_menu_item_with_mnemonic( menu_in_menu, "Invert Y", "InvertCurveTextureY" );
752
753         }
754         menu_separator( menu );
755         {
756                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Overlay" );
757                 if ( g_Layout_enableDetachableMenus.m_value ) {
758                         menu_tearoff( menu_in_menu );
759                 }
760                 create_menu_item_with_mnemonic( menu_in_menu, "Set", "MakeOverlayPatch" );
761                 create_menu_item_with_mnemonic( menu_in_menu, "Clear", "ClearPatchOverlays" );
762         }
763 }
764
765
766 #include "gtkutil/dialog.h"
767 #include "gtkutil/widget.h"
768
769 void DoNewPatchDlg( EPatchPrefab prefab, int minrows, int mincols, int defrows, int defcols, int maxrows, int maxcols ){
770         ModalDialog dialog;
771
772         ui::Window window = MainFrame_getWindow().create_dialog_window("Patch density", G_CALLBACK(dialog_delete_callback ), &dialog );
773
774         auto accel = ui::AccelGroup(ui::New);
775         window.add_accel_group( accel );
776         auto width = ui::ComboBoxText(ui::New);
777         auto height = ui::ComboBoxText(ui::New);
778         {
779                 auto hbox = create_dialog_hbox( 4, 4 );
780                 window.add(hbox);
781                 {
782                         auto table = create_dialog_table( 2, 2, 4, 4 );
783                         hbox.pack_start( table, TRUE, TRUE, 0 );
784                         {
785                                 auto label = ui::Label( "Width:" );
786                                 label.show();
787                 table.attach(label, {0, 1, 0, 1}, {GTK_FILL, 0});
788                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
789                         }
790                         {
791                                 auto label = ui::Label( "Height:" );
792                                 label.show();
793                 table.attach(label, {0, 1, 1, 2}, {GTK_FILL, 0});
794                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
795                         }
796
797                         {
798                                 auto combo = width;
799 #define D_ITEM( x ) if ( x >= mincols && ( !maxcols || x <= maxcols ) ) gtk_combo_box_text_append_text( combo, #x )
800                                 D_ITEM( 3 );
801                                 D_ITEM( 5 );
802                                 D_ITEM( 7 );
803                                 D_ITEM( 9 );
804                                 D_ITEM( 11 );
805                                 D_ITEM( 13 );
806                                 D_ITEM( 15 );
807                                 D_ITEM( 17 );
808                                 D_ITEM( 19 );
809                                 D_ITEM( 21 );
810                                 D_ITEM( 23 );
811                                 D_ITEM( 25 );
812                                 D_ITEM( 27 );
813                                 D_ITEM( 29 );
814                                 D_ITEM( 31 ); // MAX_PATCH_SIZE is 32, so we should be able to do 31...
815 #undef D_ITEM
816                                 combo.show();
817                 table.attach(combo, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
818                         }
819                         {
820                                 auto combo = height;
821 #define D_ITEM( x ) if ( x >= minrows && ( !maxrows || x <= maxrows ) ) gtk_combo_box_text_append_text( combo, #x )
822                                 D_ITEM( 3 );
823                                 D_ITEM( 5 );
824                                 D_ITEM( 7 );
825                                 D_ITEM( 9 );
826                                 D_ITEM( 11 );
827                                 D_ITEM( 13 );
828                                 D_ITEM( 15 );
829                                 D_ITEM( 17 );
830                                 D_ITEM( 19 );
831                                 D_ITEM( 21 );
832                                 D_ITEM( 23 );
833                                 D_ITEM( 25 );
834                                 D_ITEM( 27 );
835                                 D_ITEM( 29 );
836                                 D_ITEM( 31 ); // MAX_PATCH_SIZE is 32, so we should be able to do 31...
837 #undef D_ITEM
838                                 combo.show();
839                 table.attach(combo, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
840                         }
841                 }
842
843                 {
844                         auto vbox = create_dialog_vbox( 4 );
845                         hbox.pack_start( vbox, TRUE, TRUE, 0 );
846                         {
847                                 auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
848                                 vbox.pack_start( button, FALSE, FALSE, 0 );
849                                 widget_make_default( button );
850                                 gtk_widget_grab_focus( button  );
851                                 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
852                         }
853                         {
854                                 auto button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
855                                 vbox.pack_start( button, FALSE, FALSE, 0 );
856                                 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
857                         }
858                 }
859         }
860
861         // Initialize dialog
862         gtk_combo_box_set_active( width, ( defcols - mincols ) / 2 );
863         gtk_combo_box_set_active( height, ( defrows - minrows ) / 2 );
864
865         if ( modal_dialog_show( window, dialog ) == eIDOK ) {
866                 int w = gtk_combo_box_get_active( width ) * 2 + mincols;
867                 int h = gtk_combo_box_get_active( height ) * 2 + minrows;
868
869                 Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), prefab, GlobalXYWnd_getCurrentViewType(), w, h );
870         }
871
872         window.destroy();
873 }
874
875
876
877
878 EMessageBoxReturn DoCapDlg( ECapDialog* type ){
879         ModalDialog dialog;
880         ModalDialogButton ok_button( dialog, eIDOK );
881         ModalDialogButton cancel_button( dialog, eIDCANCEL );
882         ui::Widget bevel{ui::null};
883         ui::Widget ibevel{ui::null};
884         ui::Widget endcap{ui::null};
885         ui::Widget iendcap{ui::null};
886         ui::Widget cylinder{ui::null};
887
888         ui::Window window = MainFrame_getWindow().create_modal_dialog_window( "Cap", dialog );
889
890         auto accel_group = ui::AccelGroup(ui::New);
891         window.add_accel_group( accel_group );
892
893         {
894                 auto hbox = create_dialog_hbox( 4, 4 );
895                 window.add(hbox);
896
897                 {
898                         // Gef: Added a vbox to contain the toggle buttons
899                         auto radio_vbox = create_dialog_vbox( 4 );
900                         hbox.add(radio_vbox);
901
902                         {
903                                 auto table = ui::Table( 5, 2, FALSE );
904                                 table.show();
905                                 radio_vbox.pack_start( table, TRUE, TRUE, 0 );
906                                 gtk_table_set_row_spacings( table, 5 );
907                                 gtk_table_set_col_spacings( table, 5 );
908
909                                 {
910                                         auto image = new_local_image( "cap_bevel.png" );
911                                         image.show();
912                     table.attach(image, {0, 1, 0, 1}, {GTK_FILL, 0});
913                                 }
914                                 {
915                                         auto image = new_local_image( "cap_endcap.png" );
916                                         image.show();
917                     table.attach(image, {0, 1, 1, 2}, {GTK_FILL, 0});
918                                 }
919                                 {
920                                         auto image = new_local_image( "cap_ibevel.png" );
921                                         image.show();
922                     table.attach(image, {0, 1, 2, 3}, {GTK_FILL, 0});
923                                 }
924                                 {
925                                         auto image = new_local_image( "cap_iendcap.png" );
926                                         image.show();
927                     table.attach(image, {0, 1, 3, 4}, {GTK_FILL, 0});
928                                 }
929                                 {
930                                         auto image = new_local_image( "cap_cylinder.png" );
931                                         image.show();
932                     table.attach(image, {0, 1, 4, 5}, {GTK_FILL, 0});
933                                 }
934
935                                 GSList* group = 0;
936                                 {
937                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Bevel" ));
938                                         button.show();
939                     table.attach(button, {1, 2, 0, 1}, {GTK_FILL | GTK_EXPAND, 0});
940                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
941
942                                         bevel = button;
943                                 }
944                                 {
945                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Endcap" ));
946                                         button.show();
947                     table.attach(button, {1, 2, 1, 2}, {GTK_FILL | GTK_EXPAND, 0});
948                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
949
950                                         endcap = button;
951                                 }
952                                 {
953                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Inverted Bevel" ));
954                                         button.show();
955                     table.attach(button, {1, 2, 2, 3}, {GTK_FILL | GTK_EXPAND, 0});
956                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
957
958                                         ibevel = button;
959                                 }
960                                 {
961                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Inverted Endcap" ));
962                                         button.show();
963                     table.attach(button, {1, 2, 3, 4}, {GTK_FILL | GTK_EXPAND, 0});
964                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
965
966                                         iendcap = button;
967                                 }
968                                 {
969                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Cylinder" ));
970                                         button.show();
971                     table.attach(button, {1, 2, 4, 5}, {GTK_FILL | GTK_EXPAND, 0});
972                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
973
974                                         cylinder = button;
975                                 }
976                         }
977                 }
978
979                 {
980                         auto vbox = create_dialog_vbox( 4 );
981                         hbox.pack_start( vbox, FALSE, FALSE, 0 );
982                         {
983                                 auto button = create_modal_dialog_button( "OK", ok_button );
984                                 vbox.pack_start( button, FALSE, FALSE, 0 );
985                                 widget_make_default( button );
986                                 gtk_widget_add_accelerator( button , "clicked", accel_group, GDK_KEY_Return, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
987                         }
988                         {
989                                 auto button = create_modal_dialog_button( "Cancel", cancel_button );
990                                 vbox.pack_start( button, FALSE, FALSE, 0 );
991                                 gtk_widget_add_accelerator( button , "clicked", accel_group, GDK_KEY_Escape, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
992                         }
993                 }
994         }
995
996         // Initialize dialog
997         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( bevel ), TRUE );
998
999         EMessageBoxReturn ret = modal_dialog_show( window, dialog );
1000         if ( ret == eIDOK ) {
1001                 if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( bevel ) ) ) {
1002                         *type = PATCHCAP_BEVEL;
1003                 }
1004                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( endcap ) ) ) {
1005                         *type = PATCHCAP_ENDCAP;
1006                 }
1007                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( ibevel ) ) ) {
1008                         *type = PATCHCAP_INVERTED_BEVEL;
1009                 }
1010                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( iendcap ) ) ) {
1011                         *type = PATCHCAP_INVERTED_ENDCAP;
1012                 }
1013                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( cylinder ) ) ) {
1014                         *type = PATCHCAP_CYLINDER;
1015                 }
1016         }
1017
1018         window.destroy();
1019
1020         return ret;
1021 }