"""Rectangle brushes and continuous, replacing large-obstacle strokes.""" import pygame from const.window import MAP_AT, MAP_SIZE, GAME_SQUARE from game.frame import obstacle class Placement: def __init__(self, app): self.app = app self.active = False def stamp(self, cell): previous = self.app.current_elem self.app.current_elem = self.brush try: self.app.add_elem(cell) finally: self.app.current_elem = previous def move(self, cell): if self.rectangle: self.end = cell return dx, dy = cell[0] - self.end[0], cell[1] - self.end[1] steps = max(abs(dx), abs(dy)) origin = self.end for i in range(1, steps + 1): self.stamp((origin[0] + round(dx * i / steps), origin[1] + round(dy * i / steps))) self.end = cell def finish(self, cancel=False): if not self.active: return self.active = False if cancel: self.app.restore_document(self.before) else: if self.rectangle: for x in range(min(self.start[0], self.end[0]), max(self.start[0], self.end[0]) + 1): for y in range(min(self.start[1], self.end[1]), max(self.start[1], self.end[1]) + 1): self.stamp((x, y)) self.app.menu.record(self.before) self.app.menu.block_pointer = True def handle_event(self, event): app = self.app if self.active: if event.type == pygame.MOUSEMOTION: self.move(app.selection.cell(event.pos, clamp=True)) elif event.type == pygame.MOUSEBUTTONUP and event.button == 1: self.move(app.selection.cell(event.pos, clamp=True)) self.finish() elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: self.finish(cancel=True) return True if event.type != pygame.MOUSEBUTTONDOWN or event.button != 1: return False if (app.tools.tool != "paint" or app.tools.regions.interacting() or app.settings.focus is not None or app.menu.blocks_editing() or pygame.key.get_mods() & (pygame.KMOD_SHIFT | pygame.KMOD_CTRL) or len(app.selection.group) > 1): return False cell = app.selection.cell(event.pos) if cell is None: return False if app.selection.group: node = app.selection.group[0] brush = dict(elem_type=node["layer"], type=node["type"], name=node["name"]) else: brush = dict(app.current_elem) if not brush.get("elem_type"): return False if brush["elem_type"] == "obstacle" and not app.show_obstacle: return True obs = obstacle.get_obstacle(brush["type"], brush["name"]) if brush["elem_type"] == "obstacle" else None self.rectangle = obs is None or obs["WIDTH"] == obs["HEIGHT"] == 1 app.menu.finish_stroke() self.before = app.document_snapshot() self.brush = brush self.start = self.end = cell self.active = True app.blank_map_hint_area() if not self.rectangle: self.stamp(cell) return True def draw(self): if not self.active or not self.rectangle: return app = self.app x0, x1 = sorted((self.start[0], self.end[0])) y0, y1 = sorted((self.start[1], self.end[1])) old_clip = app.screen.get_clip() app.screen.set_clip(pygame.Rect(MAP_AT, MAP_SIZE)) tile = pygame.Surface((GAME_SQUARE, GAME_SQUARE), pygame.SRCALPHA) for x in range(x0, x1 + 1): for y in range(y0, y1 + 1): blocked = self.brush["elem_type"] == "obstacle" and (x, y) in app.map_obstacles_dict tile.fill((230, 65, 65, 100) if blocked else (115, 160, 210, 100)) pos = (MAP_AT[0] + (x - app.scroll_grid_at[0]) * GAME_SQUARE, MAP_AT[1] + (y - app.scroll_grid_at[1]) * GAME_SQUARE) app.screen.blit(tile, pos) pygame.draw.rect(app.screen, (200, 215, 235), (*pos, GAME_SQUARE, GAME_SQUARE), 1) app.screen.set_clip(old_clip)