Index: code/branches/gui/src/core/GameState.cc
===================================================================
--- code/branches/gui/src/core/GameState.cc	(revision 1660)
+++ code/branches/gui/src/core/GameState.cc	(revision 1661)
@@ -56,4 +56,19 @@
     /**
     @brief
+        Destructor only checks that we don't delete an active state.
+    */
+    GameState::~GameState()
+    {
+        if (this->bActive_)
+        {
+            if (this->parent_)
+                this->requestState(this->parent_->getName());
+            else
+                this->requestState("");
+        }
+    }
+
+    /**
+    @brief
         Adds a child to the current tree. The Child can contain children of its own.
         But you cannot a state tree that already has an active state.
@@ -104,4 +119,64 @@
         // mark us as parent
         state->parent_ = this;
+    }
+
+    /**
+    @brief
+        Removes a child by instance. This splits the tree in two parts,
+        each of them functional on its own.
+    @param state
+        GameState by instance pointer
+    */
+    void GameState::removeChild(GameState* state)
+    {
+        std::map<GameState*, GameState*>::iterator it = this->grandchildrenToChildren_.find(state);
+        if (it != this->grandchildrenToChildren_.end())
+        {
+            if (state->isActive())
+            {
+                ThrowException(GameState, "Cannot remove active game state child '"
+                    + state->getName() + "' from '" + name_ + "'.");
+                //COUT(2) << "Warning: Cannot remove active game state child '" << state->getName()
+                //    << "' from '" << name_ << "'." << std::endl;
+            }
+            else
+            {
+                for (std::map<GameState*, GameState*>::const_iterator it = state->grandchildrenToChildren_.begin();
+                    it != state->grandchildrenToChildren_.end(); ++it)
+                {
+                    this->grandchildRemoved(it->first);
+                }
+                this->grandchildRemoved(state);
+            }
+        }
+        else
+        {
+            ThrowException(GameState, "Game state '" + name_ + "' doesn't have a child named '"
+                + state->getName() + "'. Removal skipped.");
+            //COUT(2) << "Warning: Game state '" << name_ << "' doesn't have a child named '"
+            //    << state->getName() << "'. Removal skipped." << std::endl;
+        }
+    }
+
+    /**
+    @brief
+        Removes a child by name. This splits the tree in two parts,
+        each of them functional on its own.
+    @param state
+        GameState by name
+    */
+
+    void GameState::removeChild(const std::string& name)
+    {
+        GameState* state = checkState(name);
+        if (state)
+        {
+            removeChild(state);
+        }
+        else
+        {
+            ThrowException(GameState, "GameState '" + name + "' doesn't exist.");
+            //COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl;
+        }
     }
 
@@ -126,4 +201,22 @@
     /**
     @brief
+        Tells a state that one of its children has removed a child. This is necessary
+        to fill the internal maps correctly.
+    @param child
+        The child who notices this state.
+    @param grandchild
+        The child that has been removed.
+    */
+    void GameState::grandchildRemoved(GameState* grandchild)
+    {
+        // adjust the two maps correctly.
+        this->allChildren_.erase(grandchild->getName());
+        this->grandchildrenToChildren_.erase(grandchild);
+        if (this->parent_)
+            this->parent_->grandchildRemoved(grandchild);
+    }
+
+    /**
+    @brief
         Checks whether a specific game states exists in the hierarchy.
     @remarks
@@ -191,24 +284,40 @@
     void GameState::requestState(const std::string& name)
     {
-        GameState* request = checkState(name);
-        GameState* current = getCurrentState();
-        if (request)
-        {
+        if (name == "")
+        {
+            // user would like to leave every state.
+            GameState* current = getCurrentState();
             if (current)
             {
-                // There is already an active state
-                current->makeTransition(request);
-            }
-            else
-            {
-                // no active state --> we have to activate the root node first.
+                // Deactivate all states but root
                 GameState* root = getRootNode();
-                root->activate();
-                root->makeTransition(request);
-            }
-        }
-        else
-        {
-            COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl;
+                current->makeTransition(root);
+                // Kick root too
+                root->deactivate();
+            }
+        }
+        else
+        {
+            GameState* request = checkState(name);
+            GameState* current = getCurrentState();
+            if (request)
+            {
+                if (current)
+                {
+                    // There is already an active state
+                    current->makeTransition(request);
+                }
+                else
+                {
+                    // no active state --> we have to activate the root node first.
+                    GameState* root = getRootNode();
+                    root->activate();
+                    root->makeTransition(request);
+                }
+            }
+            else
+            {
+                COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl;
+            }
         }
     }
Index: code/branches/gui/src/core/GameState.h
===================================================================
--- code/branches/gui/src/core/GameState.h	(revision 1660)
+++ code/branches/gui/src/core/GameState.h	(revision 1661)
@@ -62,9 +62,11 @@
     public:
         GameState(const std::string& name);
-        virtual ~GameState() { }
+        virtual ~GameState();
 
         const std::string& getName() const { return name_; }
 
         void addChild(GameState* state);
+        void removeChild(GameState* state);
+        void removeChild(const std::string& name);
         void requestState(const std::string& name);
 
@@ -76,8 +78,14 @@
         bool isCurrentState() { return this->bActive_ && !this->activeChild_; }
 
+        virtual bool tick(float dt) { return true; }
+
     protected:
+        //virtual void enter() = 0;
+        //virtual void leave() = 0;
+        //virtual void tick(float dt) = 0;
         virtual void enter() { }
         virtual void leave() { }
-        virtual void tick(float dt) { }
+
+        GameState* getActiveChild() { return this->activeChild_; }
 
     private:
@@ -86,4 +94,5 @@
         GameState* getRootNode();
         void grandchildAdded(GameState* child, GameState* grandchild);
+        void grandchildRemoved(GameState* grandchild);
         void makeTransition(GameState* state);
         void activate();
