]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - radiant/patchmanip.cpp
Merge commit '01a950c3de3ef7f7da23360f925404e2bd385d5d' into master-merge
[xonotic/netradiant.git] / radiant / patchmanip.cpp
index 7ae19fb4af6d6b65a8fc133f8ac398014b2bee87..ca984ef6601089c00ceeb357bb58fde0df56c64a 100644 (file)
@@ -145,6 +145,7 @@ void Patch_makeCaps( Patch& patch, scene::Instance& instance, EPatchCap type, co
        }
 }
 
+
 typedef std::vector<scene::Instance*> InstanceVector;
 
 enum ECapDialog {
@@ -195,6 +196,111 @@ void Scene_PatchDoCap_Selected( scene::Graph& graph, const char* shader ){
        }
 }
 
+void Patch_deform( Patch& patch, scene::Instance& instance, const int deform ){
+       patch.undoSave();
+
+       for (PatchControlIter i = patch.begin(); i != patch.end(); ++i)
+       {
+               PatchControl& control = *i;
+               int randomNumber = int( deform * (float(std::rand()) / float(RAND_MAX)));
+               control.m_vertex[2] += randomNumber;
+       }
+
+       patch.controlPointsChanged();
+}
+
+void Scene_PatchDeform( scene::Graph& graph, const int deform )
+{
+       InstanceVector instances;
+       Scene_forEachVisibleSelectedPatchInstance([&](PatchInstance &patch) {
+                       instances.push_back(&patch);
+       });
+       for ( InstanceVector::const_iterator i = instances.begin(); i != instances.end(); ++i )
+       {
+               Patch_deform( *Node_getPatch( ( *i )->path().top() ), *( *i ), deform );
+       }
+
+}
+
+void Patch_thicken( Patch& patch, scene::Instance& instance, const float thickness, bool seams, const int axis ){
+
+               // Create a new patch node
+               NodeSmartReference node(g_patchCreator->createPatch());
+               // Insert the node into worldspawn
+               Node_getTraversable(Map_FindOrInsertWorldspawn(g_map))->insert(node);
+
+               // Retrieve the contained patch from the node
+               Patch* targetPatch = Node_getPatch(node);
+
+               // Create the opposite patch with the given thickness = distance
+               bool no12 = true;
+               bool no34 = true;
+               targetPatch->createThickenedOpposite(patch, thickness, axis, no12, no34);
+
+               // Now select the newly created patches
+               {
+                       scene::Path patchpath(makeReference(GlobalSceneGraph().root()));
+                       patchpath.push(makeReference(*Map_GetWorldspawn(g_map)));
+                       patchpath.push(makeReference(node.get()));
+                       Instance_getSelectable(*GlobalSceneGraph().find(patchpath))->setSelected(true);
+               }
+
+               if (seams && thickness != 0.0f) {
+                       int i = 0;
+                       if ( no12 ){
+                               i = 2;
+                       }
+                       int iend = 4;
+                       if ( no34 ){
+                               iend = 2;
+                       }
+                       // Now create the four walls
+                       for ( ; i < iend; i++ ) {
+                               // Allocate new patch
+                               NodeSmartReference node = NodeSmartReference(g_patchCreator->createPatch());
+                               // Insert each node into worldspawn
+                               Node_getTraversable(Map_FindOrInsertWorldspawn(g_map))->insert(node);
+
+                               // Retrieve the contained patch from the node
+                               Patch* wallPatch = Node_getPatch(node);
+
+                               // Create the wall patch by passing i as wallIndex
+                               wallPatch->createThickenedWall( patch, *targetPatch, i);
+
+                               if( ( wallPatch->localAABB().extents[0] <= 0.00005 && wallPatch->localAABB().extents[1] <= 0.00005 ) ||
+                                       ( wallPatch->localAABB().extents[1] <= 0.00005 && wallPatch->localAABB().extents[2] <= 0.00005 ) ||
+                                       ( wallPatch->localAABB().extents[0] <= 0.00005 && wallPatch->localAABB().extents[2] <= 0.00005 ) ){
+                                       //globalOutputStream() << "Thicken: Discarding degenerate patch.\n";
+                                       Node_getTraversable( Map_FindOrInsertWorldspawn(g_map) )->erase( node );
+                               }
+                               else
+                               // Now select the newly created patches
+                               {
+                                       scene::Path patchpath(makeReference(GlobalSceneGraph().root()));
+                                       patchpath.push(makeReference(*Map_GetWorldspawn(g_map)));
+                                       patchpath.push(makeReference(node.get()));
+                                       Instance_getSelectable(*GlobalSceneGraph().find(patchpath))->setSelected(true);
+                               }
+                       }
+               }
+
+               // Invert the target patch so that it faces the opposite direction
+               targetPatch->InvertMatrix();
+}
+
+void Scene_PatchThicken( scene::Graph& graph, const int thickness, bool seams, const int axis )
+{
+       InstanceVector instances;
+       Scene_forEachVisibleSelectedPatchInstance([&](PatchInstance &patch) {
+               instances.push_back(&patch);
+       });
+       for ( InstanceVector::const_iterator i = instances.begin(); i != instances.end(); ++i )
+       {
+               Patch_thicken( *Node_getPatch( ( *i )->path().top() ), *( *i ), thickness, seams, axis );
+       }
+
+}
+
 Patch* Scene_GetUltimateSelectedVisiblePatch(){
        if ( GlobalSelectionSystem().countSelected() != 0 ) {
                scene::Node& node = GlobalSelectionSystem().ultimateSelected().path().top();
@@ -543,6 +649,23 @@ void Patch_FitTexture(){
        Scene_PatchTileTexture_Selected( GlobalSceneGraph(), 1, 1 );
 }
 
+void DoPatchDeformDlg();
+
+void Patch_Deform(){
+       UndoableCommand undo( "patchDeform" );
+
+       DoPatchDeformDlg();
+}
+
+void DoPatchThickenDlg();
+
+void Patch_Thicken(){
+       UndoableCommand undo( "patchThicken" );
+
+       DoPatchThickenDlg();
+}
+
+
 #include "ifilter.h"
 
 
@@ -653,6 +776,8 @@ void Patch_registerCommands(){
        GlobalCommands_insert( "CycleCapTexturePatch", makeCallbackF(Patch_CycleProjection), Accelerator( 'N', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
        GlobalCommands_insert( "MakeOverlayPatch", makeCallbackF(Patch_OverlayOn), Accelerator( 'Y' ) );
        GlobalCommands_insert( "ClearPatchOverlays", makeCallbackF(Patch_OverlayOff), Accelerator( 'L', (GdkModifierType)GDK_CONTROL_MASK ) );
+       GlobalCommands_insert( "PatchDeform", makeCallbackF(Patch_Deform) );
+       GlobalCommands_insert( "PatchThicken", makeCallbackF(Patch_Thicken) );
 }
 
 void Patch_constructToolbar( ui::Toolbar toolbar ){
@@ -696,11 +821,11 @@ void Patch_constructMenu( ui::Menu menu ){
                if ( g_Layout_enableDetachableMenus.m_value ) {
                        menu_tearoff( menu_in_menu );
                }
-               create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Columns", "PatchInsertInsertColumn" );
                create_menu_item_with_mnemonic( menu_in_menu, "Add (2) Columns", "PatchInsertAddColumn" );
+               create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Columns", "PatchInsertInsertColumn" );
                menu_separator( menu_in_menu );
-               create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Rows", "PatchInsertInsertRow" );
                create_menu_item_with_mnemonic( menu_in_menu, "Add (2) Rows", "PatchInsertAddRow" );
+               create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Rows", "PatchInsertInsertRow" );
        }
        {
                auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Delete" );
@@ -760,6 +885,9 @@ void Patch_constructMenu( ui::Menu menu ){
                create_menu_item_with_mnemonic( menu_in_menu, "Set", "MakeOverlayPatch" );
                create_menu_item_with_mnemonic( menu_in_menu, "Clear", "ClearPatchOverlays" );
        }
+       menu_separator( menu );
+       create_menu_item_with_mnemonic( menu, "Deform...", "PatchDeform" );
+       create_menu_item_with_mnemonic( menu, "Thicken...", "PatchThicken" );
 }
 
 
@@ -873,6 +1001,66 @@ void DoNewPatchDlg( EPatchPrefab prefab, int minrows, int mincols, int defrows,
 }
 
 
+void DoPatchDeformDlg(){
+       ModalDialog dialog;
+       GtkWidget* deformW;
+
+       ui::Window window = create_dialog_window( MainFrame_getWindow(), "Patch deform", G_CALLBACK( dialog_delete_callback ), &dialog );
+
+       GtkAccelGroup* accel = gtk_accel_group_new();
+       gtk_window_add_accel_group( window, accel );
+
+       {
+               auto hbox = create_dialog_hbox( 4, 4 );
+               gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( hbox ) );
+               {
+                       auto table = create_dialog_table( 2, 2, 4, 4 );
+                       gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
+                       {
+                               GtkLabel* label = GTK_LABEL( gtk_label_new( "Max deform:" ) );
+                               gtk_widget_show( GTK_WIDGET( label ) );
+                               gtk_table_attach( table, GTK_WIDGET( label ), 0, 1, 0, 1,
+                                                                 (GtkAttachOptions) ( GTK_FILL ),
+                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+                               gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
+                       }
+                       {
+                               GtkWidget* entry = gtk_entry_new();
+                               gtk_entry_set_text( GTK_ENTRY( entry ), "16" );
+                               gtk_widget_show( entry );
+                               gtk_table_attach( table, entry, 1, 2, 0, 1,
+                                                                 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
+                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+
+                               deformW = entry;
+                       }
+               }
+               {
+                       auto vbox = create_dialog_vbox( 4 );
+                       hbox.pack_start( vbox, FALSE, FALSE, 0 );
+                       {
+                               auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
+                               vbox.pack_start( button, FALSE, FALSE, 0 );
+                               widget_make_default( button );
+                               gtk_widget_grab_focus( button  );
+                               gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
+                       }
+                       {
+                               auto button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
+                               vbox.pack_start( button, FALSE, FALSE, 0 );
+                               gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
+                       }
+               }
+       }
+
+       if ( modal_dialog_show( window, dialog ) == eIDOK ) {
+               int deform = static_cast<int>( atoi( gtk_entry_get_text( GTK_ENTRY( deformW ) ) ) );
+               Scene_PatchDeform( GlobalSceneGraph(), deform );
+       }
+
+       gtk_widget_destroy( GTK_WIDGET( window ) );
+}
+
 
 
 EMessageBoxReturn DoCapDlg( ECapDialog* type ){
@@ -1019,3 +1207,128 @@ EMessageBoxReturn DoCapDlg( ECapDialog* type ){
 
        return ret;
 }
+
+
+void DoPatchThickenDlg(){
+       ModalDialog dialog;
+       GtkWidget* thicknessW;
+       GtkWidget* seamsW;
+       GtkWidget* radX;
+       GtkWidget* radY;
+       GtkWidget* radZ;
+       GtkWidget* radNormals;
+
+       ui::Window window = create_dialog_window( MainFrame_getWindow(), "Patch thicken", G_CALLBACK( dialog_delete_callback ), &dialog );
+
+       GtkAccelGroup* accel = gtk_accel_group_new();
+       gtk_window_add_accel_group( window, accel );
+
+       {
+               auto hbox = create_dialog_hbox( 4, 4 );
+               gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( hbox ) );
+               {
+                       auto table = create_dialog_table( 2, 4, 4, 4 );
+                       gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
+                       {
+                               GtkLabel* label = GTK_LABEL( gtk_label_new( "Thickness:" ) );
+                               gtk_widget_show( GTK_WIDGET( label ) );
+                               gtk_table_attach( table, GTK_WIDGET( label ), 0, 1, 0, 1,
+                                                                 (GtkAttachOptions) ( GTK_FILL ),
+                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+                               gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
+                       }
+                       {
+                               GtkWidget* entry = gtk_entry_new();
+                               gtk_entry_set_text( GTK_ENTRY( entry ), "16" );
+                               gtk_widget_set_size_request( entry, 40, -1 );
+                               gtk_widget_show( entry );
+                               gtk_table_attach( table, entry, 1, 2, 0, 1,
+                                                                 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
+                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+
+                               thicknessW = entry;
+                       }
+                       {
+                               // Create the "create seams" label
+                               GtkWidget* _seamsCheckBox = gtk_check_button_new_with_label( "Side walls" );
+                               gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( _seamsCheckBox ), TRUE );
+                               gtk_widget_show( _seamsCheckBox );
+                               gtk_table_attach( table, _seamsCheckBox, 3, 4, 0, 1,
+                                                                 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
+                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+                               seamsW = _seamsCheckBox;
+
+                       }
+                       {
+                               // Create the radio button group for choosing the extrude axis
+                               GtkWidget* _radNormals = gtk_radio_button_new_with_label( NULL, "Normal" );
+                               GtkWidget* _radX = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(_radNormals), "X" );
+                               GtkWidget* _radY = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(_radNormals), "Y" );
+                               GtkWidget* _radZ = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(_radNormals), "Z" );
+                               gtk_widget_show( _radNormals );
+                               gtk_widget_show( _radX );
+                               gtk_widget_show( _radY );
+                               gtk_widget_show( _radZ );
+
+
+                               // Pack the buttons into the table
+                               gtk_table_attach( table, _radNormals, 0, 1, 1, 2,
+                                                                 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
+                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+                               gtk_table_attach( table, _radX, 1, 2, 1, 2,
+                                                                 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
+                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+                               gtk_table_attach( table, _radY, 2, 3, 1, 2,
+                                                                 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
+                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+                               gtk_table_attach( table, _radZ, 3, 4, 1, 2,
+                                                                 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
+                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+                               radX = _radX;
+                               radY = _radY;
+                               radZ = _radZ;
+                               radNormals = _radNormals;
+                       }
+               }
+               {
+                       auto vbox = create_dialog_vbox( 4 );
+                       hbox.pack_start( vbox, FALSE, FALSE, 0 );
+                       {
+                               auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
+                               vbox.pack_start( button, FALSE, FALSE, 0 );
+                               widget_make_default( button );
+                               gtk_widget_grab_focus( button  );
+                               gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
+                       }
+                       {
+                               auto button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
+                               vbox.pack_start( button, FALSE, FALSE, 0 );
+                               gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
+                       }
+               }
+       }
+
+       if ( modal_dialog_show( window, dialog ) == eIDOK ) {
+               int axis;
+               bool seams;
+               float thickness = static_cast<float>( atoi( gtk_entry_get_text( GTK_ENTRY( thicknessW ) ) ) );
+               seams = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON( seamsW )) ? true : false;
+
+               if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radX))) {
+                       axis = 0;
+               }
+               else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radY))) {
+                       axis = 1;
+               }
+               else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radZ))) {
+                       axis = 2;
+               }
+               else  {
+                       // Extrude along normals
+                       axis = 3;
+               }
+               Scene_PatchThicken( GlobalSceneGraph(), thickness, seams, axis );
+       }
+
+       gtk_widget_destroy( GTK_WIDGET( window ) );
+}