diff options
author | Marco 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) |
commit | bcb68e8e0ecc9a8ee902df8615f174d47e75de8e (patch) | |
tree | 554b2c1db8c4e3eaa54b4f8bfd73923d12e97d1a /sugar/graphics/spreadlayout.py | |
parent | 82694847c6ea2065c56bf2c82760507e4ccc47f6 (diff) |
Add collisions detection
Diffstat (limited to 'sugar/graphics/spreadlayout.py')
-rw-r--r-- | sugar/graphics/spreadlayout.py | 33 |
1 files changed, 27 insertions, 6 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): |