def checkBordersSnap(myCanvas, mouseLoc, tilesToSnap, checkedTiles, grid, gridRes, isDragging, isSnapped, sidesToSnap, snapdTile, selectedTiles): isConflict = False for tileA in tilesToSnap: if tileA.isFree(): 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 tileA is not tileB: sideB = tileB.getSide(sideA.adjType) isSnapped, sidesToSnap, snapdTile, \ isConflict = checkBorderSnap(mouseLoc, sideA, sideB, checkedTiles, isDragging, isSnapped, sidesToSnap, snapdTile, isConflict, selectedTiles) return isSnapped, sidesToSnap, snapdTile, isConflict
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