pingus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pingus-CVS] r3443 - trunk/pingus/src/editor


From: grumbel at BerliOS
Subject: [Pingus-CVS] r3443 - trunk/pingus/src/editor
Date: Thu, 1 Nov 2007 09:04:23 +0100

Author: grumbel
Date: 2007-11-01 09:04:23 +0100 (Thu, 01 Nov 2007)
New Revision: 3443

Modified:
   trunk/pingus/src/editor/editor_level.cpp
   trunk/pingus/src/editor/editor_level.hpp
   trunk/pingus/src/editor/editor_viewport.cpp
   trunk/pingus/src/editor/editor_viewport.hpp
   trunk/pingus/src/editor/level_impl.hpp
Log:
- moved objects from Viewport to EditorLevel

Modified: trunk/pingus/src/editor/editor_level.cpp
===================================================================
--- trunk/pingus/src/editor/editor_level.cpp    2007-11-01 07:28:09 UTC (rev 
3442)
+++ trunk/pingus/src/editor/editor_level.cpp    2007-11-01 08:04:23 UTC (rev 
3443)
@@ -103,7 +103,7 @@
 */
 bool EditorLevel::is_valid()
 {
-  std::cout << "EditorLevel::is_valid() - Not yet implemented" << std::endl;
+  //std::cout << "EditorLevel::is_valid() - Not yet implemented" << std::endl;
   if (impl)
     return true;
   else
@@ -113,6 +113,10 @@
 // Save the level to a file.  Returns true if successful
 bool EditorLevel::save_level(const std::string& filename)
 {
+  // Sort the level before saving, so that object order doesn't change
+  // after a save/load cycle (load sort() too)
+  sort();
+
   // Make sure level is valid
   if (!is_valid())
     return false;
@@ -307,6 +311,12 @@
       editor->get_viewport()->add_object(obj);
     }
 
+  sort();
+}
+
+void
+EditorLevel::sort()
+{
   // Sort by Z coordinate
   std::stable_sort(editor->get_viewport()->get_objects()->begin(),
                    editor->get_viewport()->get_objects()->end(),
@@ -444,6 +454,70 @@
   if (impl->size.height <= 0)
     impl->size.height = 1;
 }
+
+void
+EditorLevel::raise_object_to_top(LevelObj* obj)
+{
+  for(std::vector<LevelObj*>::size_type i = 0; i < impl->objects.size(); ++i)
+    {
+      if (impl->objects[i] == obj)
+        {
+          for(int j = i; j < int(impl->objects.size()-1); ++j)
+            std::swap(impl->objects[j], impl->objects[j+1]);
+
+          break;
+        }      
+    } 
+}
+
+void
+EditorLevel::lower_object_to_bottom(LevelObj* obj)
+{
+  for(std::vector<LevelObj*>::size_type i = 0; i < impl->objects.size(); ++i)
+    {
+      if (impl->objects[i] == obj)
+        {
+          for(int j = i; j >= 1; --j)
+            std::swap(impl->objects[j], impl->objects[j-1]);
+          
+          break;
+        }      
+    }
+}
+
+void
+EditorLevel::raise_object(LevelObj* obj)
+{
+  for(std::vector<LevelObj*>::size_type i = 0; i < impl->objects.size(); ++i)
+    {
+      if (impl->objects[i] == obj)
+        {
+          if (i != impl->objects.size()-1)
+            std::swap(impl->objects[i], impl->objects[i+1]);
+          break;
+        }
+    }
+}
+
+void
+EditorLevel::lower_object(LevelObj* obj)
+{
+  for(std::vector<LevelObj*>::size_type i = 0; i < impl->objects.size(); ++i)
+    {
+      if (impl->objects[i] == obj)
+        {
+          if (i != 0)
+            std::swap(impl->objects[i], impl->objects[i-1]);
+          break;
+        }
+    }
+}  
+
+std::vector<LevelObj*>*
+EditorLevel::get_objects()
+{
+  return &(impl->objects);
+}
 
 } // namespace Editor
 

Modified: trunk/pingus/src/editor/editor_level.hpp
===================================================================
--- trunk/pingus/src/editor/editor_level.hpp    2007-11-01 07:28:09 UTC (rev 
3442)
+++ trunk/pingus/src/editor/editor_level.hpp    2007-11-01 08:04:23 UTC (rev 
3443)
@@ -85,9 +85,20 @@
   void set_comment(const std::string&);
   void set_music(const std::string&);
 
+  /** Sorts the level according to the objects z-pos */
+  void sort();
+
   void set_action(const std::string& actionname, int count); 
   std::map<std::string, int> get_actions() const;
 
+  void raise_object(LevelObj* obj);
+  void lower_object(LevelObj* obj);
+
+  void raise_object_to_top(LevelObj* obj);
+  void lower_object_to_bottom(LevelObj* obj);
+
+  std::vector<LevelObj*>* get_objects();
+
 private:
   LevelImpl* impl;
 

Modified: trunk/pingus/src/editor/editor_viewport.cpp
===================================================================
--- trunk/pingus/src/editor/editor_viewport.cpp 2007-11-01 07:28:09 UTC (rev 
3442)
+++ trunk/pingus/src/editor/editor_viewport.cpp 2007-11-01 08:04:23 UTC (rev 
3443)
@@ -50,11 +50,6 @@
 EditorViewport::~EditorViewport ()
 {
   delete drawing_context;
-
-  for(std::vector<LevelObj*>::iterator i = objs.begin(); i != objs.end(); ++i)
-    {
-      delete *i;
-    }
 }
 
 void
@@ -140,16 +135,16 @@
   if (current_action == HIGHLIGHTING)
     {
       highlighted_area.normalize();
-      for (unsigned i = 0; i < objs.size(); i++)
+      for (unsigned i = 0; i < get_objects()->size(); i++)
         {
-          if (highlighted_area.is_inside(Vector2i(int(objs[i]->get_pos().x),
-                                                  int(objs[i]->get_pos().y))))
+          if 
(highlighted_area.is_inside(Vector2i(int((*get_objects())[i]->get_pos().x),
+                                                  
int((*get_objects())[i]->get_pos().y))))
             {
-              selected_objs.push_back(objs[i]);
-              objs[i]->select();
+              selected_objs.push_back((*get_objects())[i]);
+              (*get_objects())[i]->select();
             }
           else
-            objs[i]->unselect();
+            (*get_objects())[i]->unselect();
         }
 
       selection_changed(selected_objs);
@@ -157,8 +152,8 @@
   else if (current_action == DRAGGING)
     {
       // Set the objects' positions for good
-      for (unsigned i = 0; i < objs.size(); i++)
-        objs[i]->set_orig_pos(objs[i]->get_pos());
+      for (unsigned i = 0; i < (*get_objects()).size(); i++)
+        (*get_objects())[i]->set_orig_pos((*get_objects())[i]->get_pos());
     }
   current_action = NOTHING;
 }
@@ -223,14 +218,14 @@
             break;
 
           case 'a':
-            if (selected_objs == objs)
+            if (selected_objs == (*get_objects()))
               {
                 clear_selection();
               }
             else 
               {
                 clear_selection();
-                selected_objs = objs;
+                selected_objs = (*get_objects());
                 for (unsigned i = 0; i < selected_objs.size(); i++)
                   selected_objs[i]->select();
               }
@@ -327,8 +322,8 @@
   drawing_context->draw_rect(Rect(Vector2i(0,0), 
editor->get_level()->get_size()).grow(-100), Color(155,155,155), 5000.0f);
        
   // Draw the level objects
-  for (unsigned i = 0; i < objs.size(); i++)
-    objs[i]->draw(*drawing_context);
+  for (unsigned i = 0; i < (*get_objects()).size(); i++)
+    (*get_objects())[i]->draw(*drawing_context);
 
   if (current_action == HIGHLIGHTING)
     {
@@ -378,8 +373,8 @@
 {
   // we travel reversly through the object list, so that we get the
   // top-most object
-  for (std::vector<LevelObj*>::reverse_iterator i = objs.rbegin ();
-       i != objs.rend (); ++i)
+  for (std::vector<LevelObj*>::reverse_iterator i = (*get_objects()).rbegin ();
+       i != (*get_objects()).rend (); ++i)
     {
       if ((*i)->is_at(x, y))
         return *i;
@@ -399,7 +394,7 @@
 void 
 EditorViewport::add_object(LevelObj* obj)
 {
-  objs.push_back(obj);
+  (*get_objects()).push_back(obj);
 }
 
 void
@@ -412,7 +407,7 @@
       if (clone)
         {
           new_objs.push_back(clone);
-          objs.push_back(clone);
+          (*get_objects()).push_back(clone);
           clone->select();
         }
     }
@@ -428,8 +423,8 @@
   for(std::vector<LevelObj*>::iterator i = selected_objs.begin(); i != 
selected_objs.end(); ++i)
     (*i)->remove();
   
-  objs.erase(std::remove_if(objs.begin(), objs.end(), 
boost::mem_fn(&LevelObj::is_removed)),
-             objs.end());
+  (*get_objects()).erase(std::remove_if((*get_objects()).begin(), 
(*get_objects()).end(), boost::mem_fn(&LevelObj::is_removed)),
+             (*get_objects()).end());
 
   for(std::vector<LevelObj*>::iterator i = selected_objs.begin(); i != 
selected_objs.end(); ++i)
     delete (*i);
@@ -483,59 +478,25 @@
 void
 EditorViewport::raise_object(LevelObj* obj)
 {
-  for(std::vector<LevelObj*>::size_type i = 0; i < objs.size(); ++i)
-    {
-      if (objs[i] == obj)
-        {
-          if (i != objs.size()-1)
-            std::swap(objs[i], objs[i+1]);
-          break;
-        }
-    }
+  editor->get_level()->raise_object(obj);
 }
 
 void
 EditorViewport::lower_object(LevelObj* obj)
 {
-  for(std::vector<LevelObj*>::size_type i = 0; i < objs.size(); ++i)
-    {
-      if (objs[i] == obj)
-        {
-          if (i != 0)
-            std::swap(objs[i], objs[i-1]);
-          break;
-        }
-    }
+  editor->get_level()->lower_object(obj);
 }
 
 void
 EditorViewport::raise_object_to_top(LevelObj* obj)
 {
-  for(std::vector<LevelObj*>::size_type i = 0; i < objs.size(); ++i)
-    {
-      if (objs[i] == obj)
-        {
-          for(int j = i; j < int(objs.size()-1); ++j)
-            std::swap(objs[j], objs[j+1]);
-
-          break;
-        }      
-    }
+  editor->get_level()->raise_object_to_top(obj);
 }
 
 void
 EditorViewport::lower_object_to_bottom(LevelObj* obj)
 {
-  for(std::vector<LevelObj*>::size_type i = 0; i < objs.size(); ++i)
-    {
-      if (objs[i] == obj)
-        {
-          for(int j = i; j >= 1; --j)
-            std::swap(objs[j], objs[j-1]);
-          
-          break;
-        }      
-    }
+  editor->get_level()->lower_object_to_bottom(obj);
 }
 
 void
@@ -600,9 +561,9 @@
 EditorViewport::clear()
 {
   selected_objs.clear();
-  for(std::vector<LevelObj*>::iterator i = objs.begin(); i != objs.end(); ++i)
+  for(std::vector<LevelObj*>::iterator i = (*get_objects()).begin(); i != 
(*get_objects()).end(); ++i)
     delete *i;
-  objs.clear();
+  (*get_objects()).clear();
   selection_changed(selected_objs);
 }
 
@@ -617,6 +578,12 @@
 {
   state.set_pos(pos);
 }
+
+std::vector<LevelObj*>*
+EditorViewport::get_objects()
+{
+  return editor->get_level()->get_objects();
+}
 
 } // namespace Editor
 

Modified: trunk/pingus/src/editor/editor_viewport.hpp
===================================================================
--- trunk/pingus/src/editor/editor_viewport.hpp 2007-11-01 07:28:09 UTC (rev 
3442)
+++ trunk/pingus/src/editor/editor_viewport.hpp 2007-11-01 08:04:23 UTC (rev 
3443)
@@ -49,6 +49,7 @@
   /** The EditorScreen to which this viewport belongs */
   EditorScreen* editor;
 
+
   /** Whether or not Autoscrolling is turned on */
   bool autoscroll;
 
@@ -59,10 +60,7 @@
   /** Where the mouse started dragging from */
   Vector2i drag_world_pos;
   Vector2i drag_screen_pos;
-
-  /** All objects in the level */
-  std::vector<LevelObj*> objs;
-
+  
   /** The currently selected LevelObjs */
   std::vector<LevelObj*> selected_objs;
 
@@ -155,7 +153,7 @@
 
   void update_layout();
 
-  std::vector<LevelObj*>* get_objects() { return &objs; }
+  std::vector<LevelObj*>* get_objects();
 
   void clear_selection();
   void clear();

Modified: trunk/pingus/src/editor/level_impl.hpp
===================================================================
--- trunk/pingus/src/editor/level_impl.hpp      2007-11-01 07:28:09 UTC (rev 
3442)
+++ trunk/pingus/src/editor/level_impl.hpp      2007-11-01 08:04:23 UTC (rev 
3443)
@@ -44,6 +44,8 @@
   /** Destructor */
   ~LevelImpl()
   {
+    for(std::vector<LevelObj*>::iterator i = objects.begin(); i != 
objects.end(); ++i)
+      delete *i;
   }
                    
   std::string resname;
@@ -66,6 +68,8 @@
   std::string comment;
   std::string music;
 
+  std::vector<LevelObj*> objects;
+
 private:
   LevelImpl (const LevelImpl&);
   LevelImpl& operator= (const LevelImpl&);





reply via email to

[Prev in Thread] Current Thread [Next in Thread]