tool_panel.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. """Optional right-hand tools and square layer-specific erasers."""
  2. import pygame
  3. from district_overlay import DistrictOverlay
  4. from extra_tools import ExtraTools
  5. from const.window import WINDOW_W, WINDOW_H, MENU_HEIGHT, MAP_AT, MAP_W, MAP_H, GAME_SQUARE, TOOL_PANEL_WIDTH
  6. class ToolPanel:
  7. WIDTH = TOOL_PANEL_WIDTH
  8. def __init__(self, app):
  9. self.app = app
  10. self.visible = True
  11. self.tool = "paint"
  12. self.size = 1
  13. self.dragging = False
  14. self.last = None
  15. self.regions = DistrictOverlay(app)
  16. self.extra = ExtraTools(app)
  17. def toggle(self):
  18. self.visible = not self.visible
  19. def choose(self, tool):
  20. if tool != "paint":
  21. self.regions.finish_drag()
  22. self.regions.enabled = False
  23. self.regions.editing = self.regions.moving_npc = None
  24. self.regions.create_mode = self.regions.create_start = self.regions.create_end = None
  25. self.app.menu.finish_stroke()
  26. self.dragging = False
  27. self.last = None
  28. self.tool = tool
  29. self.extra.reset()
  30. if tool != "spray":
  31. self.app.selection.cancel()
  32. if tool != "paint" and not self.visible:
  33. self.toggle()
  34. self.app.menu.notify({"paint": "放置工具", "floor": "地板橡皮擦", "obstacle": "障碍物橡皮擦", "move": "拖拽框选障碍物,再点击目标位置移动", "spray": "喷溅笔", "expand": "左 / 上方拓展", "export": "选择下方按钮导出图片"}[tool])
  35. def buttons(self):
  36. return [(name, label, pygame.Rect(WINDOW_W + 12 + i % 4 * 100, 72 + i // 4 * 72, 92, 64))
  37. for i, (name, label) in enumerate([
  38. ("grid", "显示网格"), ("obstacles", "显示障碍"), ("regions", "显示区域"), ("models", "怪物渲染"),
  39. ("floor", "地板擦除"), ("obstacle", "障碍擦除"), ("paint", "放置工具"), ("origin", "回到原点"),
  40. ("move", "批量移动"), ("spray", "喷溅笔"), ("expand", "左/上拓展"), ("export", "图片导出")])]
  41. def sizes(self):
  42. if self.tool not in ("floor", "obstacle"):
  43. return []
  44. return [(n, pygame.Rect(WINDOW_W + 12 + (n - 1) % 3 * 64, 338 + (n - 1) // 3 * 36, 58, 30))
  45. for n in range(1, 16)]
  46. def erase_bounds(self, cell):
  47. # Odd sizes center on the cursor; even sizes put it in the upper-left
  48. # cell of the central 2x2. Clip without shifting the footprint inward.
  49. offset = (self.size - 1) // 2
  50. x0, y0 = cell[0] - offset, cell[1] - offset
  51. x1, y1 = min(x0 + self.size, self.app.map_size[0]), min(y0 + self.size, self.app.map_size[1])
  52. return max(0, x0), max(0, y0), x1, y1
  53. def erase(self, cell):
  54. x0, y0, x1, y1 = self.erase_bounds(cell)
  55. if self.tool == "floor":
  56. for node in self.app.map_json["floor"]:
  57. node["points"] = [p for p in node["points"] if not (x0 <= p["x"] < x1 and y0 <= p["y"] < y1)]
  58. elif self.tool == "obstacle":
  59. objects = {self.app.map_obstacles_dict[(x, y)] for x in range(x0, x1) for y in range(y0, y1)
  60. if (x, y) in self.app.map_obstacles_dict}
  61. for obj in objects:
  62. obj.uninstall()
  63. self.app.redraw_floor_obstacle_trigger = True
  64. def erase_to(self, cell):
  65. # Interpolate fast mouse motion, so a drag cannot skip cells.
  66. start = self.last or cell
  67. steps = max(abs(cell[0] - start[0]), abs(cell[1] - start[1]))
  68. def action():
  69. for i in range(steps + 1):
  70. t = i / steps if steps else 0
  71. self.erase((round(start[0] + (cell[0] - start[0]) * t),
  72. round(start[1] + (cell[1] - start[1]) * t)))
  73. self.app.menu.edit(action)
  74. self.last = cell
  75. def handle_event(self, event):
  76. if self.extra.handle_event(event):
  77. return True
  78. if self.visible and self.regions.handle_event(event):
  79. self.app.menu.block_pointer = True
  80. return True
  81. if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
  82. self.dragging = False
  83. self.last = None
  84. if self.visible and hasattr(event, "pos") and event.pos[0] >= WINDOW_W:
  85. if event.type == pygame.MOUSEBUTTONDOWN:
  86. self.app.menu.block_pointer = True
  87. if event.button == 1:
  88. for name, label, rect in self.buttons():
  89. if rect.collidepoint(event.pos):
  90. actions = {"grid": self.app.menu.toggle_grid, "obstacles": self.app.menu.toggle_obstacles,
  91. "origin": self.app.menu.go_origin, "regions": self.regions.toggle, "models": self.regions.toggle_models}
  92. self.app.menu.invoke(actions[name] if name in actions else lambda n=name: self.choose(n))
  93. for n, rect in self.sizes():
  94. if rect.collidepoint(event.pos):
  95. self.size = n
  96. return True
  97. if self.tool not in ("floor", "obstacle"):
  98. return False
  99. if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
  100. cell = self.app.selection.cell(event.pos)
  101. if cell is not None:
  102. self.dragging = True
  103. self.last = None
  104. self.erase_to(cell)
  105. return True
  106. if event.type == pygame.MOUSEMOTION and self.dragging:
  107. cell = self.app.selection.cell(event.pos)
  108. if cell is not None:
  109. self.erase_to(cell)
  110. else:
  111. self.last = None
  112. return True
  113. return False
  114. def draw_preview(self):
  115. self.extra.draw_preview()
  116. if self.tool not in ("floor", "obstacle") or self.app.menu.blocks_editing():
  117. return
  118. cell = self.app.selection.cell(pygame.mouse.get_pos())
  119. if cell is None:
  120. return
  121. overlay = pygame.Surface((MAP_W, MAP_H), pygame.SRCALPHA)
  122. x, y, x1, y1 = self.erase_bounds(cell)
  123. sx, sy = self.app.scroll_grid_at
  124. rect = pygame.Rect((x - sx) * GAME_SQUARE, (y - sy) * GAME_SQUARE,
  125. (x1 - x) * GAME_SQUARE, (y1 - y) * GAME_SQUARE)
  126. color = (100, 180, 255) if self.tool == "floor" else (255, 160, 85)
  127. pygame.draw.rect(overlay, color + (60,), rect)
  128. pygame.draw.rect(overlay, color + (240,), rect, 2)
  129. self.app.screen.blit(overlay, MAP_AT)
  130. def draw(self):
  131. if not self.visible:
  132. return
  133. screen, font = self.app.screen, self.app.text_font
  134. pygame.draw.rect(screen, (35, 39, 46), (WINDOW_W, MENU_HEIGHT, self.WIDTH, WINDOW_H - MENU_HEIGHT))
  135. pygame.draw.line(screen, (144, 155, 173), (WINDOW_W, MENU_HEIGHT), (WINDOW_W, WINDOW_H))
  136. def text(label, center, color=(235, 238, 243)):
  137. image = font.render(label, True, color)
  138. screen.blit(image, image.get_rect(center=center))
  139. self.app.settings.draw_fields()
  140. mouse = pygame.mouse.get_pos()
  141. for name, label, rect in self.buttons():
  142. active = (name == self.tool or name == "grid" and self.app.menu.show_grid or
  143. name == "obstacles" and self.app.show_obstacle or name == "regions" and self.regions.enabled or name == "models" and self.regions.render_models)
  144. bg = (22, 26, 33) if active else (72, 82, 98) if rect.collidepoint(mouse) else (53, 61, 74)
  145. pygame.draw.rect(screen, bg, rect)
  146. pygame.draw.rect(screen, (144, 155, 173), rect, 1)
  147. cx, cy = rect.centerx, rect.y + 22
  148. color = (235, 238, 243)
  149. if name == "grid":
  150. for d in (-12, 0, 12):
  151. pygame.draw.line(screen, color, (cx + d, cy - 12), (cx + d, cy + 12))
  152. pygame.draw.line(screen, color, (cx - 12, cy + d), (cx + 12, cy + d))
  153. elif name == "models":
  154. self.regions.monster_icon(screen, pygame.Rect(cx - 12, cy - 12, 24, 24), (255, 190, 85))
  155. elif name == "regions":
  156. pygame.draw.rect(screen, (100, 200, 255), (cx - 15, cy - 12, 22, 18), 2)
  157. pygame.draw.rect(screen, (255, 190, 85), (cx - 5, cy - 3, 22, 18), 2)
  158. elif name in ("floor", "obstacle"):
  159. color = (100, 180, 255) if name == "floor" else (255, 160, 85)
  160. pygame.draw.polygon(screen, color, [(cx - 13, cy + 3), (cx, cy - 10), (cx + 13, cy), (cx, cy + 13)], 2)
  161. pygame.draw.line(screen, color, (cx - 7, cy - 3), (cx + 6, cy + 7), 2)
  162. elif name == "obstacles":
  163. pygame.draw.ellipse(screen, color, (cx - 15, cy - 9, 30, 18), 2)
  164. pygame.draw.circle(screen, color, (cx, cy), 5)
  165. elif name == "move":
  166. pygame.draw.line(screen, color, (cx-15, cy), (cx+15, cy), 2)
  167. pygame.draw.line(screen, color, (cx, cy-15), (cx, cy+15), 2)
  168. for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)):
  169. 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)])
  170. elif name == "spray":
  171. for dx, dy in ((0, 0), (-11, -7), (8, -10), (-6, 11), (12, 7), (1, -15)):
  172. pygame.draw.circle(screen, color, (cx+dx, cy+dy), 2)
  173. elif name == "export":
  174. pygame.draw.rect(screen, color, (cx-15, cy-12, 30, 24), 2)
  175. pygame.draw.lines(screen, color, False, [(cx-12, cy+8), (cx-3, cy-2), (cx+4, cy+5), (cx+10, cy-3)], 2)
  176. elif name == "expand":
  177. pygame.draw.rect(screen, color, (cx-4, cy-2, 16, 14), 2)
  178. pygame.draw.lines(screen, color, False, [(cx-15, cy+4), (cx-15, cy-13), (cx+5, cy-13)], 2)
  179. elif name == "paint":
  180. pygame.draw.line(screen, color, (cx - 9, cy + 9), (cx + 8, cy - 8), 5)
  181. pygame.draw.polygon(screen, color, [(cx - 13, cy + 13), (cx - 10, cy + 5), (cx - 5, cy + 10)])
  182. else:
  183. pygame.draw.lines(screen, color, False, [(cx - 12, cy + 10), (cx - 12, cy - 10), (cx + 12, cy - 10)], 2)
  184. pygame.draw.line(screen, color, (cx - 12, cy - 10), (cx + 7, cy + 8), 2)
  185. text(label, (rect.centerx, rect.bottom - 12))
  186. self.extra.draw()
  187. self.regions.draw_panel()
  188. if self.tool not in ("floor", "obstacle"):
  189. return
  190. text("橡皮擦大小", (WINDOW_W + 104, 310))
  191. for n, rect in self.sizes():
  192. pygame.draw.rect(screen, (22, 26, 33) if n == self.size else (53, 61, 74), rect)
  193. pygame.draw.rect(screen, (144, 155, 173), rect, 1)
  194. text("%dx%d" % (n, n), rect.center)
  195. for i, label in enumerate(["左键拖动擦除", "触及多格障碍会整块删除", "Esc 返回放置工具"]):
  196. text(label, (WINDOW_W + 104, 546 + i * 24), (175, 187, 203))