Ticket #3944: collision.patch

File collision.patch, 2.5 KB (added by dsd, 11 years ago)

improved collision detection behaviour

  • src/jarabe/desktop/grid.py

    diff --git a/src/jarabe/desktop/grid.py b/src/jarabe/desktop/grid.py
    index 9d6d820..942d9f3 100644
    a b class Grid(SugarExt.Grid): 
    197197    def _detect_collisions(self, child):
    198198        collision_found = False
    199199        child_rect = self._child_rects[child]
     200
     201        # Look at all of the other grid children, to see if child conflicts
     202        # with them. If so, try to move one of the children involved in the
     203        # collision.
    200204        for c in self._children:
     205            if c == child:
     206                continue
     207
    201208            intersects_, intersection = Gdk.rectangle_intersect(
    202209                child_rect, self._child_rects[c])
    203             if c != child and intersection.width > 0:
    204                 if (c not in self._locked_children and
    205                     c not in self._collisions):
    206                     collision_found = True
     210            if intersection.width == 0:
     211                # No collision
     212                continue
     213
     214            # We found a collision - now we try to act on it
     215            child_locked = child in self._locked_children
     216            c_locked = c in self._locked_children
     217
     218            # If both children involved in the collision are locked, lets
     219            # just let them collide.
     220            if c_locked and child_locked:
     221                continue
     222
     223            # If the child is locked, lets move the unlocked colliding c
     224            # somewhere else
     225            if child_locked:
     226                if c not in self._collisions:
    207227                    self._collisions.append(c)
    208 
    209         if collision_found:
    210             if child not in self._collisions:
    211                 self._collisions.append(child)
     228                continue
     229
     230            # We are dealing with a collision where child is not locked.
     231            # If it collides with a locked c, we try to reposition child,
     232            # as the locked one should "win" for obvious reasons.
     233            # If c is not locked, we similarly decide to move child to solve,
     234            # the collision, on the basis that c got there first so child
     235            # should be the one that moves around, reducing UI disruption.
     236            self._collisions.append(child)
     237
     238            # Now we break out early; as we're about to re-position child,
     239            # it doesn't make any sense to continue looking for more
     240            # collisions with it.
     241            break
    212242
    213243        if self._collisions and not self._collisions_sid:
    214244            self._collisions_sid = GObject.timeout_add(_REFRESH_RATE,