def execute(self): assert self.points != [] # Calc bounding rect rect = Rect(self.points[0].x, self.points[0].y, self.points[0].x + self.brush.width, self.points[0].y + self.brush.height) for point in self.points: rect.left = min(rect.left, point.x) rect.top = min(rect.top, point.y) rect.right = max(rect.right, point.x + self.brush.width) rect.bottom = max(rect.bottom, point.y + self.brush.height) self.pos.x = rect.left self.pos.y = rect.top self.redo_brush = TileBrush.from_field(self.tilemap.field.copy(), rect.width, rect.height, -self.pos.x, -self.pos.y) # FIXME: undo_field is unneeded, should just record the overwritten color self.undo_brush = TileBrush.from_field(self.undo_field, rect.width, rect.height, -self.pos.x, -self.pos.y) self.redo_brush.set_opaque() self.undo_brush.set_opaque() self.undo_field = None
def auto_crop(self): rect = Rect(0, 0, 0, 0) for y, x in itertools.product(range(0, self.field.height), range(0, self.field.width)): if self.field.at(x, y) != 0: rect.top = y break for y, x in itertools.product(reversed(range(0, self.field.height)), range(0, self.field.width)): if self.field.at(x, y) != 0: rect.bottom = y + 1 break for x, y in itertools.product(range(0, self.field.width), range(0, self.field.height)): if self.field.at(x, y) != 0: rect.left = x break for x, y in itertools.product(reversed(range(0, self.field.width)), range(0, self.field.height)): if self.field.at(x, y) != 0: rect.right = x + 1 break if rect.width != 0: self.resize(rect.width, rect.height, -rect.left, -rect.top) else: self.field = Field.from_size(1, 1) self.field.put(0, 0, 0) self.set_opaque()
def get_selection(self): selection = Rect(self.current_pos.x, self.current_pos.y, self.region_select_start.x, self.region_select_start.y) selection.normalize() selection.right += 1 selection.bottom += 1 selection.left = min(max(0, selection.left), self.columns) selection.right = min(max(0, selection.right), self.columns) selection.top = max(0, selection.top) return selection
def get_bounding_rect(self): if self._has_bounding_rect: return self.bounding_rect else: init = False rect = Rect(0, 0, 0, 0) for layer in self.layers: if layer.has_bounding_rect(): if not init: rect = layer.get_bounding_rect() init = True else: other = layer.get_bounding_rect() rect.top = min(rect.top, other.top) rect.bottom = max(rect.bottom, other.bottom) rect.left = min(rect.left, other.left) rect.right = max(rect.right, other.right) return rect