Add collisions detection

This commit is contained in:
Marco Pesenti Gritti 2007-07-05 22:45:58 +02:00
parent 82694847c6
commit bcb68e8e0e
2 changed files with 28 additions and 7 deletions

View File

@ -30,18 +30,18 @@ class _Grid(object):
def __init__(self, width, height): def __init__(self, width, height):
self.width = width self.width = width
self.height = height self.height = height
self._children = []
self._collisions = []
self._collisions_sid = 0
self._array = array('B') self._array = array('B')
for i in range(width * height): for i in range(width * height):
self._array.append(0) self._array.append(0)
def add_locked(self, child, x, y, width, height): 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.locked = True
child.grid_rect = rect self._add_child(child)
self._add_weight(rect)
def add(self, child, width, height): def add(self, child, width, height):
trials = _PLACE_TRIALS trials = _PLACE_TRIALS
@ -60,12 +60,33 @@ class _Grid(object):
child.grid_rect = rect child.grid_rect = rect
child.locked = False child.locked = False
self._add_weight(rect) self._add_child(child)
if weight > 0:
self._detect_collisions(child)
def remove(self, child): def remove(self, child):
self._children.remove(child)
self._remove_weight(child.grid_rect) self._remove_weight(child.grid_rect)
child.grid_rect = None 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): def _add_weight(self, rect):
for i in range(rect.x, rect.x + rect.width): for i in range(rect.x, rect.x + rect.width):
for j in range(rect.y, rect.y + rect.height): for j in range(rect.y, rect.y + rect.height):

View File

@ -38,7 +38,7 @@ def _create_icon():
icon_name='theme:stock-buddy') icon_name='theme:stock-buddy')
layout.add(icon) layout.add(icon)
return (len(box.get_children()) < 70) return (len(box.get_children()) < 50)
window = gtk.Window() window = gtk.Window()
window.connect("destroy", lambda w: gtk.main_quit()) window.connect("destroy", lambda w: gtk.main_quit())