Exemple #1
0
def checkBorderSnap(mouseLoc, sideA, sideB, checkedTiles, isDragging,
        isSnapped, sidesToSnap, snapdTile, isConflict, selectedTiles):
    if common.isInSquare(sideA.corner.getAbsPosition(),
            sideB.corner.getAbsPosition(), 1):
        if not isConflict:
            if (sideA.color == sideB.color and not
                    sideA.isSnapped and not sideB.isSnapped):
                sideA.adjSide = sideB
                sideB.adjSide = sideA
                # If colors match and both sides aren't snapped, they
                # are added to a list of sides to snap if there is an
                # unclick
                sidesToSnap.append(sideA)
            else:
                if isDragging:
                    movers.noSnapMove(mouseLoc, selectedTiles)
                    isSnapped, sidesToSnap, snapdTile, isConflict = \
                            misc.unSnapAll(sidesToSnap)
                isConflict = True
        # If there has been a conflict with another tile in the group of
        # the tile who's side is being checked, then the tile is just
        # added to a list of tiles which have already been checked
        checkedTiles.append(sideB.tile)
    return isSnapped, sidesToSnap, snapdTile, isConflict
Exemple #2
0
def moveSnappable(myCanvas, mouseLoc, checkedTiles, selectedTiles,
        selectedTile, palletBack, isSnapped, sidesToSnap, snapdTile, snapdSide,
        adjSide, grid, gridRes):
    # minDistance will end up storing the distance to the nearest tile/
    # tile group
    minDistance = objects.Canvas.SNAP_DISTANCE * objects.Canvas.scale
    # isMatch keeps track of whether or not there are two tiles who's
    # facing sides match, and which are close enough together to
    # potentially snap
    isMatch = False
    # This next block basically looks at all the moving tiles, and
    # determines whether or not any of them are close enough, and the
    # right kind of side, to potentially snap together, and if so,
    # sets those to sides to snapdSide and adjSide
    for tileA in selectedTile.tileGroup:
        # This block only runs if there are sides to the tile which are
        # not already snapped to other tiles
        if tileA.isFree():
            # This gets areas near each tile to reduce the number of
            # tiles which need to be checked for potential snapping
            areasToCheck = misc.getAreasToCheck(tileA.position, grid, gridRes)
            for sideA in tileA.sides:
                if not sideA.isSnapped:
                    for area in areasToCheck:
                        for tileB in area:
                            # If a side of a moving tile is not snapped,
                            # and a nearby tile has unsnapped sides,
                            # and the nearby tile has not already been
                            # found to be unsnappable (not in
                            # checkedTiles) then this block evaluates
                            if tileB.isFree() and tileB not in checkedTiles:
                                # This sets sideB to the side of the
                                # nearby tile adjacent to the moving
                                # tile
                                sideB = tileB.getSide(sideA.adjType)
                                # This determines if the two sides
                                # could potentially snap, and if so
                                # sets snapdSide and adjSide to these
                                # two sides, and sets isMatch to True.
                                # In the end it only applies to a
                                # nearest snappable tile.
                                minDist, isMatch, snapdSide, adjSide = \
                                        misc.findMatch(sideA, sideB,
                                        minDistance, isMatch, snapdSide,
                                        adjSide)
    if isMatch:
        checkedTiles.append(adjSide.tile)
        # If two matching sides were found above in this function, then
        # the below function shifts the moving tiles into a snapped
        # position with the nearby tiles
        isSnapped, snapdTile, snapdSide = misc.snapTiles(selectedTile,
                snapdSide, adjSide)
        # If, by shifting towards other tiles to snap, a tile group
        # would overlap with the tile pallet, the tile group is then
        # moved back to where it would have been before shifting, and
        # isConflict is set to true.  This is to prevent tiles from
        # snapping to other tiles when they would also be moving into
        # a position over the tile pallet which usually means the tiles
        # would be set to be deleted
        if selectedTile.getRect().colliderect(palletBack):
            isSnapped, sidesToSnap, snapdTile, isConflict = \
                    misc.unSnapAll(sidesToSnap)
        # All that is known at this point is that two tiles match and
        # could potentially snap.  Below, the code checks whether or not
        # shifting tiles to a snapped position would cause OTHER tiles
        # to not match with each other, in which case these tiles are
        # moved back to where they were before they were snapped.  Any
        # tiles which were found to not be able to snap are added to
        # checkedTiles.  This moveSnappable function is then called
        # recursively to check for possible matches which are not the
        # NEAREST potential matches, until all nearby tiles have been
        # checked for possible snapping.  If all the sides match, the
        # sides are set up to be snapped whenever the user unclicks.
        else:
            isSnapped, sidesToSnap, snapdTile, isConflict = \
                    checkers.checkBordersSnap(myCanvas, mouseLoc,
                    selectedTiles, checkedTiles, grid, gridRes, True,
                    isSnapped, sidesToSnap, snapdTile, selectedTiles)
        if isConflict:
            isSnapped, sidesToSnap, snapdTile, snapdSide, adjSide = \
                    moveSnappable(myCanvas, mouseLoc, checkedTiles,
                    selectedTiles, selectedTile, palletBack, isSnapped,
                    sidesToSnap, snapdTile, snapdSide, adjSide, grid, gridRes)
    # If there were no sets of tiles nearby in which all sides matched,
    # the dragged tiles simply move to where they are dragged
    else:
        noSnapMove(mouseLoc, selectedTiles)
    return isSnapped, sidesToSnap, snapdTile, snapdSide, adjSide