]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/patchmanip.cpp
radiant: replace StringBuffer with std::string
[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( "NaturalizePatch", makeCallbackF(Patch_NaturalTexture), Accelerator( 'N', (GdkModifierType)GDK_CONTROL_MASK ) );
620         GlobalCommands_insert( "PatchCylinder", makeCallbackF(Patch_Cylinder) );
621         GlobalCommands_insert( "PatchDenseCylinder", makeCallbackF(Patch_DenseCylinder) );
622         GlobalCommands_insert( "PatchVeryDenseCylinder", makeCallbackF(Patch_VeryDenseCylinder) );
623         GlobalCommands_insert( "PatchSquareCylinder", makeCallbackF(Patch_SquareCylinder) );
624         GlobalCommands_insert( "PatchXactCylinder", makeCallbackF(Patch_XactCylinder) );
625         GlobalCommands_insert( "PatchXactSphere", makeCallbackF(Patch_XactSphere) );
626         GlobalCommands_insert( "PatchXactCone", makeCallbackF(Patch_XactCone) );
627         GlobalCommands_insert( "PatchEndCap", makeCallbackF(Patch_Endcap) );
628         GlobalCommands_insert( "PatchBevel", makeCallbackF(Patch_Bevel) );
629         GlobalCommands_insert( "PatchSquareBevel", makeCallbackF(Patch_SquareBevel) );
630         GlobalCommands_insert( "PatchSquareEndcap", makeCallbackF(Patch_SquareEndcap) );
631         GlobalCommands_insert( "PatchCone", makeCallbackF(Patch_Cone) );
632         GlobalCommands_insert( "PatchSphere", makeCallbackF(Patch_Sphere) );
633         GlobalCommands_insert( "SimplePatchMesh", makeCallbackF(Patch_Plane), Accelerator( 'P', (GdkModifierType)GDK_SHIFT_MASK ) );
634         GlobalCommands_insert( "PatchInsertInsertColumn", makeCallbackF(Patch_InsertInsertColumn), Accelerator( GDK_KEY_KP_Add, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
635         GlobalCommands_insert( "PatchInsertAddColumn", makeCallbackF(Patch_InsertAddColumn) );
636         GlobalCommands_insert( "PatchInsertInsertRow", makeCallbackF(Patch_InsertInsertRow), Accelerator( GDK_KEY_KP_Add, (GdkModifierType)GDK_CONTROL_MASK ) );
637         GlobalCommands_insert( "PatchInsertAddRow", makeCallbackF(Patch_InsertAddRow) );
638         GlobalCommands_insert( "PatchDeleteFirstColumn", makeCallbackF(Patch_DeleteFirstColumn) );
639         GlobalCommands_insert( "PatchDeleteLastColumn", makeCallbackF(Patch_DeleteLastColumn), Accelerator( GDK_KEY_KP_Subtract, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
640         GlobalCommands_insert( "PatchDeleteFirstRow", makeCallbackF(Patch_DeleteFirstRow), Accelerator( GDK_KEY_KP_Subtract, (GdkModifierType)GDK_CONTROL_MASK ) );
641         GlobalCommands_insert( "PatchDeleteLastRow", makeCallbackF(Patch_DeleteLastRow) );
642         GlobalCommands_insert( "InvertCurve", makeCallbackF(Patch_Invert), Accelerator( 'I', (GdkModifierType)GDK_CONTROL_MASK ) );
643         GlobalCommands_insert( "RedisperseRows", makeCallbackF(Patch_RedisperseRows), Accelerator( 'E', (GdkModifierType)GDK_CONTROL_MASK ) );
644         GlobalCommands_insert( "RedisperseCols", makeCallbackF(Patch_RedisperseCols), Accelerator( 'E', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
645         GlobalCommands_insert( "SmoothRows", makeCallbackF(Patch_SmoothRows), Accelerator( 'W', (GdkModifierType)GDK_CONTROL_MASK ) );
646         GlobalCommands_insert( "SmoothCols", makeCallbackF(Patch_SmoothCols), Accelerator( 'W', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
647         GlobalCommands_insert( "MatrixTranspose", makeCallbackF(Patch_Transpose), Accelerator( 'M', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
648         GlobalCommands_insert( "CapCurrentCurve", makeCallbackF(Patch_Cap), Accelerator( 'C', (GdkModifierType)GDK_SHIFT_MASK ) );
649         GlobalCommands_insert( "CycleCapTexturePatch", makeCallbackF(Patch_CycleProjection), Accelerator( 'N', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
650         GlobalCommands_insert( "MakeOverlayPatch", makeCallbackF(Patch_OverlayOn), Accelerator( 'Y' ) );
651         GlobalCommands_insert( "ClearPatchOverlays", makeCallbackF(Patch_OverlayOff), Accelerator( 'L', (GdkModifierType)GDK_CONTROL_MASK ) );
652 }
653
654 void Patch_constructToolbar( ui::Toolbar toolbar ){
655         toolbar_append_button( toolbar, "Put caps on the current patch (SHIFT + C)", "cap_curve.png", "CapCurrentCurve" );
656 }
657
658 void Patch_constructMenu( ui::Menu menu ){
659         create_menu_item_with_mnemonic( menu, "Cylinder", "PatchCylinder" );
660         {
661                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "More Cylinders" );
662                 if ( g_Layout_enableDetachableMenus.m_value ) {
663                         menu_tearoff( menu_in_menu );
664                 }
665                 create_menu_item_with_mnemonic( menu_in_menu, "Dense Cylinder", "PatchDenseCylinder" );
666                 create_menu_item_with_mnemonic( menu_in_menu, "Very Dense Cylinder", "PatchVeryDenseCylinder" );
667                 create_menu_item_with_mnemonic( menu_in_menu, "Square Cylinder", "PatchSquareCylinder" );
668                 create_menu_item_with_mnemonic( menu_in_menu, "Exact Cylinder...", "PatchXactCylinder" );
669         }
670         menu_separator( menu );
671         create_menu_item_with_mnemonic( menu, "End cap", "PatchEndCap" );
672         create_menu_item_with_mnemonic( menu, "Bevel", "PatchBevel" );
673         {
674                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "More End caps, Bevels" );
675                 if ( g_Layout_enableDetachableMenus.m_value ) {
676                         menu_tearoff( menu_in_menu );
677                 }
678                 create_menu_item_with_mnemonic( menu_in_menu, "Square Endcap", "PatchSquareBevel" );
679                 create_menu_item_with_mnemonic( menu_in_menu, "Square Bevel", "PatchSquareEndcap" );
680         }
681         menu_separator( menu );
682         create_menu_item_with_mnemonic( menu, "Cone", "PatchCone" );
683         create_menu_item_with_mnemonic( menu, "Exact Cone...", "PatchXactCone" );
684         menu_separator( menu );
685         create_menu_item_with_mnemonic( menu, "Sphere", "PatchSphere" );
686         create_menu_item_with_mnemonic( menu, "Exact Sphere...", "PatchXactSphere" );
687         menu_separator( menu );
688         create_menu_item_with_mnemonic( menu, "Simple Patch Mesh...", "SimplePatchMesh" );
689         menu_separator( menu );
690         {
691                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Insert" );
692                 if ( g_Layout_enableDetachableMenus.m_value ) {
693                         menu_tearoff( menu_in_menu );
694                 }
695                 create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Columns", "PatchInsertInsertColumn" );
696                 create_menu_item_with_mnemonic( menu_in_menu, "Add (2) Columns", "PatchInsertAddColumn" );
697                 menu_separator( menu_in_menu );
698                 create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Rows", "PatchInsertInsertRow" );
699                 create_menu_item_with_mnemonic( menu_in_menu, "Add (2) Rows", "PatchInsertAddRow" );
700         }
701         {
702                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Delete" );
703                 if ( g_Layout_enableDetachableMenus.m_value ) {
704                         menu_tearoff( menu_in_menu );
705                 }
706                 create_menu_item_with_mnemonic( menu_in_menu, "First (2) Columns", "PatchDeleteFirstColumn" );
707                 create_menu_item_with_mnemonic( menu_in_menu, "Last (2) Columns", "PatchDeleteLastColumn" );
708                 menu_separator( menu_in_menu );
709                 create_menu_item_with_mnemonic( menu_in_menu, "First (2) Rows", "PatchDeleteFirstRow" );
710                 create_menu_item_with_mnemonic( menu_in_menu, "Last (2) Rows", "PatchDeleteLastRow" );
711         }
712         menu_separator( menu );
713         {
714                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Matrix" );
715                 if ( g_Layout_enableDetachableMenus.m_value ) {
716                         menu_tearoff( menu_in_menu );
717                 }
718                 create_menu_item_with_mnemonic( menu_in_menu, "Invert", "InvertCurve" );
719                 auto menu_3 = create_sub_menu_with_mnemonic( menu_in_menu, "Re-disperse" );
720                 if ( g_Layout_enableDetachableMenus.m_value ) {
721                         menu_tearoff( menu_3 );
722                 }
723                 create_menu_item_with_mnemonic( menu_3, "Rows", "RedisperseRows" );
724                 create_menu_item_with_mnemonic( menu_3, "Columns", "RedisperseCols" );
725                 auto menu_4 = create_sub_menu_with_mnemonic( menu_in_menu, "Smooth" );
726                 if ( g_Layout_enableDetachableMenus.m_value ) {
727                         menu_tearoff( menu_4 );
728                 }
729                 create_menu_item_with_mnemonic( menu_4, "Rows", "SmoothRows" );
730                 create_menu_item_with_mnemonic( menu_4, "Columns", "SmoothCols" );
731                 create_menu_item_with_mnemonic( menu_in_menu, "Transpose", "MatrixTranspose" );
732         }
733         menu_separator( menu );
734         create_menu_item_with_mnemonic( menu, "Cap Selection", "CapCurrentCurve" );
735         create_menu_item_with_mnemonic( menu, "Cycle Cap Texture", "CycleCapTexturePatch" );
736         menu_separator( menu );
737         {
738                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Overlay" );
739                 if ( g_Layout_enableDetachableMenus.m_value ) {
740                         menu_tearoff( menu_in_menu );
741                 }
742                 create_menu_item_with_mnemonic( menu_in_menu, "Set", "MakeOverlayPatch" );
743                 create_menu_item_with_mnemonic( menu_in_menu, "Clear", "ClearPatchOverlays" );
744         }
745 }
746
747
748 #include "gtkutil/dialog.h"
749 #include "gtkutil/widget.h"
750
751 void DoNewPatchDlg( EPatchPrefab prefab, int minrows, int mincols, int defrows, int defcols, int maxrows, int maxcols ){
752         ModalDialog dialog;
753
754         ui::Window window = MainFrame_getWindow().create_dialog_window("Patch density", G_CALLBACK(dialog_delete_callback ), &dialog );
755
756         auto accel = ui::AccelGroup(ui::New);
757         window.add_accel_group( accel );
758         auto width = ui::ComboBoxText(ui::New);
759         auto height = ui::ComboBoxText(ui::New);
760         {
761                 auto hbox = create_dialog_hbox( 4, 4 );
762                 window.add(hbox);
763                 {
764                         auto table = create_dialog_table( 2, 2, 4, 4 );
765                         hbox.pack_start( table, TRUE, TRUE, 0 );
766                         {
767                                 auto label = ui::Label( "Width:" );
768                                 label.show();
769                 table.attach(label, {0, 1, 0, 1}, {GTK_FILL, 0});
770                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
771                         }
772                         {
773                                 auto label = ui::Label( "Height:" );
774                                 label.show();
775                 table.attach(label, {0, 1, 1, 2}, {GTK_FILL, 0});
776                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
777                         }
778
779                         {
780                                 auto combo = width;
781 #define D_ITEM( x ) if ( x >= mincols && ( !maxcols || x <= maxcols ) ) gtk_combo_box_text_append_text( combo, #x )
782                                 D_ITEM( 3 );
783                                 D_ITEM( 5 );
784                                 D_ITEM( 7 );
785                                 D_ITEM( 9 );
786                                 D_ITEM( 11 );
787                                 D_ITEM( 13 );
788                                 D_ITEM( 15 );
789                                 D_ITEM( 17 );
790                                 D_ITEM( 19 );
791                                 D_ITEM( 21 );
792                                 D_ITEM( 23 );
793                                 D_ITEM( 25 );
794                                 D_ITEM( 27 );
795                                 D_ITEM( 29 );
796                                 D_ITEM( 31 ); // MAX_PATCH_SIZE is 32, so we should be able to do 31...
797 #undef D_ITEM
798                                 combo.show();
799                 table.attach(combo, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
800                         }
801                         {
802                                 auto combo = height;
803 #define D_ITEM( x ) if ( x >= minrows && ( !maxrows || x <= maxrows ) ) gtk_combo_box_text_append_text( combo, #x )
804                                 D_ITEM( 3 );
805                                 D_ITEM( 5 );
806                                 D_ITEM( 7 );
807                                 D_ITEM( 9 );
808                                 D_ITEM( 11 );
809                                 D_ITEM( 13 );
810                                 D_ITEM( 15 );
811                                 D_ITEM( 17 );
812                                 D_ITEM( 19 );
813                                 D_ITEM( 21 );
814                                 D_ITEM( 23 );
815                                 D_ITEM( 25 );
816                                 D_ITEM( 27 );
817                                 D_ITEM( 29 );
818                                 D_ITEM( 31 ); // MAX_PATCH_SIZE is 32, so we should be able to do 31...
819 #undef D_ITEM
820                                 combo.show();
821                 table.attach(combo, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
822                         }
823                 }
824
825                 {
826                         auto vbox = create_dialog_vbox( 4 );
827                         hbox.pack_start( vbox, TRUE, TRUE, 0 );
828                         {
829                                 auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
830                                 vbox.pack_start( button, FALSE, FALSE, 0 );
831                                 widget_make_default( button );
832                                 gtk_widget_grab_focus( button  );
833                                 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
834                         }
835                         {
836                                 auto button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
837                                 vbox.pack_start( button, FALSE, FALSE, 0 );
838                                 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
839                         }
840                 }
841         }
842
843         // Initialize dialog
844         gtk_combo_box_set_active( width, ( defcols - mincols ) / 2 );
845         gtk_combo_box_set_active( height, ( defrows - minrows ) / 2 );
846
847         if ( modal_dialog_show( window, dialog ) == eIDOK ) {
848                 int w = gtk_combo_box_get_active( width ) * 2 + mincols;
849                 int h = gtk_combo_box_get_active( height ) * 2 + minrows;
850
851                 Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), prefab, GlobalXYWnd_getCurrentViewType(), w, h );
852         }
853
854         window.destroy();
855 }
856
857
858
859
860 EMessageBoxReturn DoCapDlg( ECapDialog* type ){
861         ModalDialog dialog;
862         ModalDialogButton ok_button( dialog, eIDOK );
863         ModalDialogButton cancel_button( dialog, eIDCANCEL );
864         ui::Widget bevel{ui::null};
865         ui::Widget ibevel{ui::null};
866         ui::Widget endcap{ui::null};
867         ui::Widget iendcap{ui::null};
868         ui::Widget cylinder{ui::null};
869
870         ui::Window window = MainFrame_getWindow().create_modal_dialog_window( "Cap", dialog );
871
872         auto accel_group = ui::AccelGroup(ui::New);
873         window.add_accel_group( accel_group );
874
875         {
876                 auto hbox = create_dialog_hbox( 4, 4 );
877                 window.add(hbox);
878
879                 {
880                         // Gef: Added a vbox to contain the toggle buttons
881                         auto radio_vbox = create_dialog_vbox( 4 );
882                         hbox.add(radio_vbox);
883
884                         {
885                                 auto table = ui::Table( 5, 2, FALSE );
886                                 table.show();
887                                 radio_vbox.pack_start( table, TRUE, TRUE, 0 );
888                                 gtk_table_set_row_spacings( table, 5 );
889                                 gtk_table_set_col_spacings( table, 5 );
890
891                                 {
892                                         auto image = new_local_image( "cap_bevel.png" );
893                                         image.show();
894                     table.attach(image, {0, 1, 0, 1}, {GTK_FILL, 0});
895                                 }
896                                 {
897                                         auto image = new_local_image( "cap_endcap.png" );
898                                         image.show();
899                     table.attach(image, {0, 1, 1, 2}, {GTK_FILL, 0});
900                                 }
901                                 {
902                                         auto image = new_local_image( "cap_ibevel.png" );
903                                         image.show();
904                     table.attach(image, {0, 1, 2, 3}, {GTK_FILL, 0});
905                                 }
906                                 {
907                                         auto image = new_local_image( "cap_iendcap.png" );
908                                         image.show();
909                     table.attach(image, {0, 1, 3, 4}, {GTK_FILL, 0});
910                                 }
911                                 {
912                                         auto image = new_local_image( "cap_cylinder.png" );
913                                         image.show();
914                     table.attach(image, {0, 1, 4, 5}, {GTK_FILL, 0});
915                                 }
916
917                                 GSList* group = 0;
918                                 {
919                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Bevel" ));
920                                         button.show();
921                     table.attach(button, {1, 2, 0, 1}, {GTK_FILL | GTK_EXPAND, 0});
922                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
923
924                                         bevel = button;
925                                 }
926                                 {
927                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Endcap" ));
928                                         button.show();
929                     table.attach(button, {1, 2, 1, 2}, {GTK_FILL | GTK_EXPAND, 0});
930                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
931
932                                         endcap = button;
933                                 }
934                                 {
935                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Inverted Bevel" ));
936                                         button.show();
937                     table.attach(button, {1, 2, 2, 3}, {GTK_FILL | GTK_EXPAND, 0});
938                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
939
940                                         ibevel = button;
941                                 }
942                                 {
943                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Inverted Endcap" ));
944                                         button.show();
945                     table.attach(button, {1, 2, 3, 4}, {GTK_FILL | GTK_EXPAND, 0});
946                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
947
948                                         iendcap = button;
949                                 }
950                                 {
951                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Cylinder" ));
952                                         button.show();
953                     table.attach(button, {1, 2, 4, 5}, {GTK_FILL | GTK_EXPAND, 0});
954                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
955
956                                         cylinder = button;
957                                 }
958                         }
959                 }
960
961                 {
962                         auto vbox = create_dialog_vbox( 4 );
963                         hbox.pack_start( vbox, FALSE, FALSE, 0 );
964                         {
965                                 auto button = create_modal_dialog_button( "OK", ok_button );
966                                 vbox.pack_start( button, FALSE, FALSE, 0 );
967                                 widget_make_default( button );
968                                 gtk_widget_add_accelerator( button , "clicked", accel_group, GDK_KEY_Return, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
969                         }
970                         {
971                                 auto button = create_modal_dialog_button( "Cancel", cancel_button );
972                                 vbox.pack_start( button, FALSE, FALSE, 0 );
973                                 gtk_widget_add_accelerator( button , "clicked", accel_group, GDK_KEY_Escape, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
974                         }
975                 }
976         }
977
978         // Initialize dialog
979         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( bevel ), TRUE );
980
981         EMessageBoxReturn ret = modal_dialog_show( window, dialog );
982         if ( ret == eIDOK ) {
983                 if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( bevel ) ) ) {
984                         *type = PATCHCAP_BEVEL;
985                 }
986                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( endcap ) ) ) {
987                         *type = PATCHCAP_ENDCAP;
988                 }
989                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( ibevel ) ) ) {
990                         *type = PATCHCAP_INVERTED_BEVEL;
991                 }
992                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( iendcap ) ) ) {
993                         *type = PATCHCAP_INVERTED_ENDCAP;
994                 }
995                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( cylinder ) ) ) {
996                         *type = PATCHCAP_CYLINDER;
997                 }
998         }
999
1000         window.destroy();
1001
1002         return ret;
1003 }