pingus-cvs
[Top][All Lists]
Advanced

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

[Pingus-CVS] [pingus] push by address@hidden - Switched LevelImpl::objec


From: pingus
Subject: [Pingus-CVS] [pingus] push by address@hidden - Switched LevelImpl::objects from std::vector<> to std::list<>, changed... on 2011-09-07 19:49 GMT
Date: Wed, 07 Sep 2011 20:05:55 +0000

Revision: 3f2fbfb153f7
Author:   Ingo Ruhnke <address@hidden>
Date:     Wed Sep  7 12:49:28 2011
Log: Switched LevelImpl::objects from std::vector<> to std::list<>, changed raise/lower to take overlapping objects into account

Fixes issue 30

http://code.google.com/p/pingus/source/detail?r=3f2fbfb153f7

Modified:
 /src/editor/editor_level.cpp
 /src/editor/editor_level.hpp
 /src/editor/viewport.cpp

=======================================
--- /src/editor/editor_level.cpp        Wed Sep  7 12:26:39 2011
+++ /src/editor/editor_level.cpp        Wed Sep  7 12:49:28 2011
@@ -17,6 +17,8 @@

 #include "editor/editor_level.hpp"

+#include <algorithm>
+
 #include "editor/level_objs.hpp"
 #include "editor/level_impl.hpp"
 #include "pingus/pingus_level.hpp"
@@ -159,8 +161,10 @@

   // Write the objects
   fw.begin_section("objects");
-  for (unsigned i = 0; i < impl->objects.size(); i++)
-    impl->objects[i]->write_properties(fw);
+  for (auto it = impl->objects.begin(); it != impl->objects.end(); ++it)
+  {
+    (*it)->write_properties(fw);
+  }
   fw.end_section();     // objects

   fw.end_section();     // pingus-level
@@ -311,7 +315,7 @@
 EditorLevel::sort()
 {
   // Sort by Z coordinate
- std::stable_sort(impl->objects.begin(), impl->objects.end(), LevelObjSort);
+  impl->objects.sort(LevelObjSort);
 }

 void
@@ -425,61 +429,60 @@
 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;
-    }
-  }
+ Objects::iterator it = std::find(impl->objects.begin(), impl->objects.end(), obj);
+  if (it != impl->objects.end())
+  {
+    impl->objects.erase(it);
+    impl->objects.push_back(obj);
+  }
 }

 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;
-    }
+ Objects::iterator it = std::find(impl->objects.begin(), impl->objects.end(), obj);
+  if (it != impl->objects.end())
+  {
+    impl->objects.erase(it);
+    impl->objects.push_front(obj);
   }
 }
+
+struct OverlapsWith
+{
+  Rect rect;
+
+  OverlapsWith(const Rect& rect_) :
+    rect(rect_)
+  {}
+
+  bool operator()(LevelObj* obj) {
+    return rect.is_overlapped(obj->get_rect());
+  }
+};

 void
 EditorLevel::raise_object(LevelObj* obj)
 {
-#if 0
-  Objects::iterator i = std::find(objects.begin(), objects.end(), object);
-  assert(i != objects.end());
-  Objects::iterator j = i;
-  ++j;
- j = std::find_if(j, objects.end(), OverlapsWith(object->get_bounding_box()));
-
-  if (j == objects.end())
-  {
-    // object overlaps with no other object, no point in raising it
+ Objects::iterator i = std::find(impl->objects.begin(), impl->objects.end(), obj);
+  if (i == impl->objects.end())
+  {
+    log_error("couldn't find object: " << obj);
   }
   else
   {
-    objects.erase(i);
-    objects.insert(++j, object);
-  }
-#endif
-
- 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;
+    Objects::iterator j = i;
+    ++j;
+ j = std::find_if(j, impl->objects.end(), OverlapsWith(obj->get_rect()));
+
+    if (j == impl->objects.end())
+    {
+      // object overlaps with no other object, no point in raising it
+    }
+    else
+    {
+      impl->objects.erase(i);
+      impl->objects.insert(++j, obj);
     }
   }
 }
@@ -487,13 +490,28 @@
 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;
+ Objects::reverse_iterator i = std::find(impl->objects.rbegin(), impl->objects.rend(), obj);
+  if (i == impl->objects.rend())
+  {
+    log_error("couldn't find object: " << obj);
+  }
+  else
+  {
+    Objects::reverse_iterator j = i;
+    ++j;
+ j = std::find_if(j, impl->objects.rend(), OverlapsWith(obj->get_rect()));
+
+    if (j == impl->objects.rend())
+    {
+      // object overlaps with no other object, no point in lowering it
+    }
+    else
+    {
+      // the base() of base in one further then where the reverse
+      // iterator was, so we have to move back to get the same
+      // position
+      impl->objects.erase(--(i.base()));
+      impl->objects.insert(--(j.base()), obj);
     }
   }
 }
=======================================
--- /src/editor/editor_level.hpp        Wed Sep  7 12:26:39 2011
+++ /src/editor/editor_level.hpp        Wed Sep  7 12:49:28 2011
@@ -18,9 +18,10 @@
 #ifndef HEADER_PINGUS_EDITOR_EDITOR_LEVEL_HPP
 #define HEADER_PINGUS_EDITOR_EDITOR_LEVEL_HPP

+#include <list>
+#include <map>
 #include <memory>
 #include <vector>
-#include <map>

 #include "math/size.hpp"

@@ -35,7 +36,7 @@
 class EditorLevel
 {
 public:
-  typedef std::vector<LevelObj*> Objects;
+  typedef std::list<LevelObj*> Objects;

   /** Construct new blank level */
   EditorLevel();
=======================================
--- /src/editor/viewport.cpp    Wed Sep  7 12:26:39 2011
+++ /src/editor/viewport.cpp    Wed Sep  7 12:49:28 2011
@@ -161,12 +161,12 @@
   if (current_action == HIGHLIGHTING)
   {
     highlighted_area.normalize();
-    for (unsigned i = 0; i < get_objects()->size(); i++)
-    {
- if (highlighted_area.contains(Vector2i(int((*get_objects())[i]->get_pos().x), - int((*get_objects())[i]->get_pos().y))))
-      {
-        selection.insert((*get_objects())[i]);
+ for (auto it = get_objects()->begin(); it != get_objects()->end(); ++it)
+    {
+      if (highlighted_area.contains(Vector2i(int((*it)->get_pos().x),
+                                             int((*it)->get_pos().y))))
+      {
+        selection.insert(*it);
       }
     }
     selection_changed(selection);
@@ -174,9 +174,9 @@
   else if (current_action == DRAGGING)
   {
     // Set the objects' positions for good
-    for (unsigned i = 0; i < (*get_objects()).size(); i++)
-    {
-      (*get_objects())[i]->set_orig_pos((*get_objects())[i]->get_pos());
+ for (auto it = get_objects()->begin(); it != get_objects()->end(); ++it)
+    {
+      (*it)->set_orig_pos((*it)->get_pos());
     }
   }
   current_action = NOTHING;



reply via email to

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