From bcb68e8e0ecc9a8ee902df8615f174d47e75de8e Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Thu, 5 Jul 2007 22:45:58 +0200 Subject: [PATCH] Add collisions detection --- sugar/graphics/spreadlayout.py | 33 +++++++++++++++++++++++++++------ tests/test-spread-layout.py | 2 +- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/sugar/graphics/spreadlayout.py b/sugar/graphics/spreadlayout.py index 36a4fe1c..5c6dc37a 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 d9911b7e..9ff964f7 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())