"""Optional right-hand tools and square layer-specific erasers.""" import pygame from district_overlay import DistrictOverlay from extra_tools import ExtraTools from const.window import WINDOW_W, WINDOW_H, MENU_HEIGHT, MAP_AT, MAP_W, MAP_H, GAME_SQUARE, TOOL_PANEL_WIDTH class ToolPanel: WIDTH = TOOL_PANEL_WIDTH def __init__(self, app): self.app = app self.visible = True self.tool = "paint" self.size = 1 self.dragging = False self.last = None self.regions = DistrictOverlay(app) self.extra = ExtraTools(app) def toggle(self): self.visible = not self.visible def choose(self, tool): if tool != "paint": self.regions.finish_drag() self.regions.enabled = False self.regions.editing = self.regions.moving_npc = None self.regions.create_mode = self.regions.create_start = self.regions.create_end = None self.app.menu.finish_stroke() self.dragging = False self.last = None self.tool = tool self.extra.reset() if tool != "spray": self.app.selection.cancel() if tool != "paint" and not self.visible: self.toggle() self.app.menu.notify({"paint": "放置工具", "floor": "地板橡皮擦", "obstacle": "障碍物橡皮擦", "move": "拖拽框选障碍物,再点击目标位置移动", "spray": "喷溅笔", "expand": "左 / 上方拓展", "export": "选择下方按钮导出图片"}[tool]) def buttons(self): return [(name, label, pygame.Rect(WINDOW_W + 12 + i % 4 * 100, 72 + i // 4 * 72, 92, 64)) for i, (name, label) in enumerate([ ("grid", "显示网格"), ("obstacles", "显示障碍"), ("regions", "显示区域"), ("models", "怪物渲染"), ("floor", "地板擦除"), ("obstacle", "障碍擦除"), ("paint", "放置工具"), ("origin", "回到原点"), ("move", "批量移动"), ("spray", "喷溅笔"), ("expand", "左/上拓展"), ("export", "图片导出")])] def sizes(self): if self.tool not in ("floor", "obstacle"): return [] return [(n, pygame.Rect(WINDOW_W + 12 + (n - 1) % 3 * 64, 338 + (n - 1) // 3 * 36, 58, 30)) for n in range(1, 16)] def erase_bounds(self, cell): # Odd sizes center on the cursor; even sizes put it in the upper-left # cell of the central 2x2. Clip without shifting the footprint inward. offset = (self.size - 1) // 2 x0, y0 = cell[0] - offset, cell[1] - offset x1, y1 = min(x0 + self.size, self.app.map_size[0]), min(y0 + self.size, self.app.map_size[1]) return max(0, x0), max(0, y0), x1, y1 def erase(self, cell): x0, y0, x1, y1 = self.erase_bounds(cell) if self.tool == "floor": for node in self.app.map_json["floor"]: node["points"] = [p for p in node["points"] if not (x0 <= p["x"] < x1 and y0 <= p["y"] < y1)] elif self.tool == "obstacle": objects = {self.app.map_obstacles_dict[(x, y)] for x in range(x0, x1) for y in range(y0, y1) if (x, y) in self.app.map_obstacles_dict} for obj in objects: obj.uninstall() self.app.redraw_floor_obstacle_trigger = True def erase_to(self, cell): # Interpolate fast mouse motion, so a drag cannot skip cells. start = self.last or cell steps = max(abs(cell[0] - start[0]), abs(cell[1] - start[1])) def action(): for i in range(steps + 1): t = i / steps if steps else 0 self.erase((round(start[0] + (cell[0] - start[0]) * t), round(start[1] + (cell[1] - start[1]) * t))) self.app.menu.edit(action) self.last = cell def handle_event(self, event): if self.extra.handle_event(event): return True if self.visible and self.regions.handle_event(event): self.app.menu.block_pointer = True return True if event.type == pygame.MOUSEBUTTONUP and event.button == 1: self.dragging = False self.last = None if self.visible and hasattr(event, "pos") and event.pos[0] >= WINDOW_W: if event.type == pygame.MOUSEBUTTONDOWN: self.app.menu.block_pointer = True if event.button == 1: for name, label, rect in self.buttons(): if rect.collidepoint(event.pos): actions = {"grid": self.app.menu.toggle_grid, "obstacles": self.app.menu.toggle_obstacles, "origin": self.app.menu.go_origin, "regions": self.regions.toggle, "models": self.regions.toggle_models} self.app.menu.invoke(actions[name] if name in actions else lambda n=name: self.choose(n)) for n, rect in self.sizes(): if rect.collidepoint(event.pos): self.size = n return True if self.tool not in ("floor", "obstacle"): return False if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: cell = self.app.selection.cell(event.pos) if cell is not None: self.dragging = True self.last = None self.erase_to(cell) return True if event.type == pygame.MOUSEMOTION and self.dragging: cell = self.app.selection.cell(event.pos) if cell is not None: self.erase_to(cell) else: self.last = None return True return False def draw_preview(self): self.extra.draw_preview() if self.tool not in ("floor", "obstacle") or self.app.menu.blocks_editing(): return cell = self.app.selection.cell(pygame.mouse.get_pos()) if cell is None: return overlay = pygame.Surface((MAP_W, MAP_H), pygame.SRCALPHA) x, y, x1, y1 = self.erase_bounds(cell) sx, sy = self.app.scroll_grid_at rect = pygame.Rect((x - sx) * GAME_SQUARE, (y - sy) * GAME_SQUARE, (x1 - x) * GAME_SQUARE, (y1 - y) * GAME_SQUARE) color = (100, 180, 255) if self.tool == "floor" else (255, 160, 85) pygame.draw.rect(overlay, color + (60,), rect) pygame.draw.rect(overlay, color + (240,), rect, 2) self.app.screen.blit(overlay, MAP_AT) def draw(self): if not self.visible: return screen, font = self.app.screen, self.app.text_font pygame.draw.rect(screen, (35, 39, 46), (WINDOW_W, MENU_HEIGHT, self.WIDTH, WINDOW_H - MENU_HEIGHT)) pygame.draw.line(screen, (144, 155, 173), (WINDOW_W, MENU_HEIGHT), (WINDOW_W, WINDOW_H)) def text(label, center, color=(235, 238, 243)): image = font.render(label, True, color) screen.blit(image, image.get_rect(center=center)) self.app.settings.draw_fields() mouse = pygame.mouse.get_pos() for name, label, rect in self.buttons(): active = (name == self.tool or name == "grid" and self.app.menu.show_grid or name == "obstacles" and self.app.show_obstacle or name == "regions" and self.regions.enabled or name == "models" and self.regions.render_models) bg = (22, 26, 33) if active else (72, 82, 98) if rect.collidepoint(mouse) else (53, 61, 74) pygame.draw.rect(screen, bg, rect) pygame.draw.rect(screen, (144, 155, 173), rect, 1) cx, cy = rect.centerx, rect.y + 22 color = (235, 238, 243) if name == "grid": for d in (-12, 0, 12): pygame.draw.line(screen, color, (cx + d, cy - 12), (cx + d, cy + 12)) pygame.draw.line(screen, color, (cx - 12, cy + d), (cx + 12, cy + d)) elif name == "models": self.regions.monster_icon(screen, pygame.Rect(cx - 12, cy - 12, 24, 24), (255, 190, 85)) elif name == "regions": pygame.draw.rect(screen, (100, 200, 255), (cx - 15, cy - 12, 22, 18), 2) pygame.draw.rect(screen, (255, 190, 85), (cx - 5, cy - 3, 22, 18), 2) elif name in ("floor", "obstacle"): color = (100, 180, 255) if name == "floor" else (255, 160, 85) pygame.draw.polygon(screen, color, [(cx - 13, cy + 3), (cx, cy - 10), (cx + 13, cy), (cx, cy + 13)], 2) pygame.draw.line(screen, color, (cx - 7, cy - 3), (cx + 6, cy + 7), 2) elif name == "obstacles": pygame.draw.ellipse(screen, color, (cx - 15, cy - 9, 30, 18), 2) pygame.draw.circle(screen, color, (cx, cy), 5) elif name == "move": pygame.draw.line(screen, color, (cx-15, cy), (cx+15, cy), 2) pygame.draw.line(screen, color, (cx, cy-15), (cx, cy+15), 2) for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)): pygame.draw.polygon(screen, color, [(cx+dx*16, cy+dy*16), (cx+dx*9-dy*4, cy+dy*9+dx*4), (cx+dx*9+dy*4, cy+dy*9-dx*4)]) elif name == "spray": for dx, dy in ((0, 0), (-11, -7), (8, -10), (-6, 11), (12, 7), (1, -15)): pygame.draw.circle(screen, color, (cx+dx, cy+dy), 2) elif name == "export": pygame.draw.rect(screen, color, (cx-15, cy-12, 30, 24), 2) pygame.draw.lines(screen, color, False, [(cx-12, cy+8), (cx-3, cy-2), (cx+4, cy+5), (cx+10, cy-3)], 2) elif name == "expand": pygame.draw.rect(screen, color, (cx-4, cy-2, 16, 14), 2) pygame.draw.lines(screen, color, False, [(cx-15, cy+4), (cx-15, cy-13), (cx+5, cy-13)], 2) elif name == "paint": pygame.draw.line(screen, color, (cx - 9, cy + 9), (cx + 8, cy - 8), 5) pygame.draw.polygon(screen, color, [(cx - 13, cy + 13), (cx - 10, cy + 5), (cx - 5, cy + 10)]) else: pygame.draw.lines(screen, color, False, [(cx - 12, cy + 10), (cx - 12, cy - 10), (cx + 12, cy - 10)], 2) pygame.draw.line(screen, color, (cx - 12, cy - 10), (cx + 7, cy + 8), 2) text(label, (rect.centerx, rect.bottom - 12)) self.extra.draw() self.regions.draw_panel() if self.tool not in ("floor", "obstacle"): return text("橡皮擦大小", (WINDOW_W + 104, 310)) for n, rect in self.sizes(): pygame.draw.rect(screen, (22, 26, 33) if n == self.size else (53, 61, 74), rect) pygame.draw.rect(screen, (144, 155, 173), rect, 1) text("%dx%d" % (n, n), rect.center) for i, label in enumerate(["左键拖动擦除", "触及多格障碍会整块删除", "Esc 返回放置工具"]): text(label, (WINDOW_W + 104, 546 + i * 24), (175, 187, 203))