Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Pesenti Gritti <marco@localhost.localdomain>2007-07-05 20:45:58 (GMT)
committer Marco Pesenti Gritti <marco@localhost.localdomain>2007-07-05 20:45:58 (GMT)
commitbcb68e8e0ecc9a8ee902df8615f174d47e75de8e (patch)
tree554b2c1db8c4e3eaa54b4f8bfd73923d12e97d1a
parent82694847c6ea2065c56bf2c82760507e4ccc47f6 (diff)
Add collisions detection
-rw-r--r--sugar/graphics/spreadlayout.py33
-rwxr-xr-xtests/test-spread-layout.py2
2 files changed, 28 insertions, 7 deletions
diff --git a/sugar/graphics/spreadlayout.py b/sugar/graphics/spreadlayout.py
index 36a4fe1..5c6dc37 100644
--- a/sugar/graphics/spreadlayout.py
+++ b/sugar/graphics/spreadlayout.py
@@ -30,18 +30,18 @@ class _Grid(object):
def __init__(self, width, height):
self.width = width
self.height = height
+ self._children = []
+ self._collisions = []
+ self._collisions_sid = 0
self._array = array('B')
for i in range(width * height):
self._array.append(0)
def add_locked(self, child, x, y, width, height):
- rect = gtk.gdk.Rectangle(x, y, width, height)
-
+ child.grid_rect = gtk.gdk.Rectangle(x, y, width, height)
child.locked = True
- child.grid_rect = rect
-
- self._add_weight(rect)
+ self._add_child(child)
def add(self, child, width, height):
trials = _PLACE_TRIALS
@@ -60,12 +60,33 @@ class _Grid(object):
child.grid_rect = rect
child.locked = False
- self._add_weight(rect)
+ self._add_child(child)
+
+ if weight > 0:
+ self._detect_collisions(child)
def remove(self, child):
+ self._children.remove(child)
self._remove_weight(child.grid_rect)
child.grid_rect = None
+ def _add_child(self, child):
+ self._children.append(child)
+ self._add_weight(child.grid_rect)
+
+ def _solve_collisions(self):
+ return False
+
+ def _detect_collisions(self, child):
+ for c in self._children:
+ intersection = child.grid_rect.intersect(c.grid_rect)
+ if c != child and intersection.width > 0:
+ if c not in self._collisions:
+ self._collisions.append(c)
+ if not self._collisions_sid:
+ self._collisions_sid = \
+ gobject.idle_add(self._solve_collisions)
+
def _add_weight(self, rect):
for i in range(rect.x, rect.x + rect.width):
for j in range(rect.y, rect.y + rect.height):
diff --git a/tests/test-spread-layout.py b/tests/test-spread-layout.py
index d9911b7..9ff964f 100755
--- a/tests/test-spread-layout.py
+++ b/tests/test-spread-layout.py
@@ -38,7 +38,7 @@ def _create_icon():
icon_name='theme:stock-buddy')
layout.add(icon)
- return (len(box.get_children()) < 70)
+ return (len(box.get_children()) < 50)
window = gtk.Window()
window.connect("destroy", lambda w: gtk.main_quit())