]> git.rm.cloudns.org Git - xonotic/netradiant.git/commitdiff
Remove empty entities when regrouping primitives
authorMattia Basaglia <mattia.basaglia@gmail.com>
Thu, 23 Jul 2015 08:55:54 +0000 (10:55 +0200)
committerMattia Basaglia <mattia.basaglia@gmail.com>
Thu, 23 Jul 2015 08:55:54 +0000 (10:55 +0200)
radiant/map.cpp

index 44633afa11876ab1722cc4b1fa5acbe4e4c6879c..7493c088ded77e0d660e6a3f468187f6503f1059 100644 (file)
@@ -1598,38 +1598,65 @@ bool Map_SaveSelected( const char* filename ){
 
 class ParentSelectedBrushesToEntityWalker : public scene::Graph::Walker
 {
-scene::Node& m_parent;
+       scene::Node& m_parent;
+       mutable bool m_emptyOldParent {false};
+
 public:
-ParentSelectedBrushesToEntityWalker( scene::Node& parent ) : m_parent( parent ){
-}
-bool pre( const scene::Path& path, scene::Instance& instance ) const {
-       if ( path.top().get_pointer() != &m_parent
-                && Node_isPrimitive( path.top() ) ) {
-               Selectable* selectable = Instance_getSelectable( instance );
-               if ( selectable != 0
-                        && selectable->isSelected()
-                        && path.size() > 1 ) {
-                       return false;
+
+       ParentSelectedBrushesToEntityWalker( scene::Node& parent )
+       : m_parent( parent )
+       {}
+
+       bool pre( const scene::Path& path, scene::Instance& instance ) const override
+       {
+               if ( path.top().get_pointer() != &m_parent &&
+                               ( Node_isPrimitive( path.top() ) || m_emptyOldParent ) )
+               {
+                       Selectable* selectable = Instance_getSelectable( instance );
+                       if ( selectable && selectable->isSelected() && path.size() > 1 )
+                       {
+                               return false;
+                       }
                }
+               return true;
        }
-       return true;
-}
-void post( const scene::Path& path, scene::Instance& instance ) const {
-       if ( path.top().get_pointer() != &m_parent
-                && Node_isPrimitive( path.top() ) ) {
-               Selectable* selectable = Instance_getSelectable( instance );
-               if ( selectable != 0
-                        && selectable->isSelected()
-                        && path.size() > 1 ) {
-                       scene::Node& parent = path.parent();
-                       if ( &parent != &m_parent ) {
-                               NodeSmartReference node( path.top().get() );
-                               Node_getTraversable( parent )->erase( node );
-                               Node_getTraversable( m_parent )->insert( node );
+
+       void post( const scene::Path& path, scene::Instance& instance ) const override
+       {
+               if ( path.top().get_pointer() == &m_parent )
+                       return;
+
+               if (  Node_isPrimitive( path.top() ) )
+               {
+                       m_emptyOldParent = false;
+                       Selectable* selectable = Instance_getSelectable( instance );
+
+                       if ( selectable && selectable->isSelected() && path.size() > 1 )
+                       {
+                               scene::Node& parent = path.parent();
+                               if ( &parent != &m_parent )
+                               {
+                                       NodeSmartReference node( path.top().get() );
+                                       auto traversable_parent = Node_getTraversable( parent );
+                                       traversable_parent->erase( node );
+                                       Node_getTraversable( m_parent )->insert( node );
+                                       if ( traversable_parent->empty() )
+                                               m_emptyOldParent = true;
+                               }
+                       }
+               }
+               else if ( m_emptyOldParent )
+               {
+                       m_emptyOldParent = false;
+                       // delete empty entities
+                       Entity* entity = Node_getEntity( path.top() );
+                       if ( entity != 0
+                               && path.top().get_pointer() != Map_FindWorldspawn( g_map )
+                               && Node_getTraversable( path.top() )->empty() ) {
+                               Path_deleteTop( path );
                        }
                }
        }
-}
 };
 
 void Scene_parentSelectedBrushesToEntity( scene::Graph& graph, scene::Node& parent ){