selection.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. """Rectangular copy and collision-checked group placement."""
  2. import pygame
  3. from const.window import MAP_AT, MAP_W, MAP_H, GAME_SQUARE
  4. from game.frame import obstacle
  5. from game.frame.obstacle_instance import ObstacleInstance
  6. class Selection:
  7. def __init__(self, app, read_clipboard):
  8. self.app = app
  9. self.read_clipboard = read_clipboard
  10. self.start = None
  11. self.end = None
  12. self.layer = None
  13. self.group = []
  14. self.clipboard_text = None
  15. def cancel(self):
  16. self.start = self.end = None
  17. self.group = []
  18. self.app.blank_map_hint_area()
  19. def cell(self, pos, clamp=False):
  20. x, y = pos[0] - MAP_AT[0], pos[1] - MAP_AT[1]
  21. if not clamp and not (0 <= x < MAP_W and 0 <= y < MAP_H):
  22. return None
  23. x = max(0, min(MAP_W - 1, x)) // GAME_SQUARE + self.app.scroll_grid_at[0]
  24. y = max(0, min(MAP_H - 1, y)) // GAME_SQUARE + self.app.scroll_grid_at[1]
  25. if not clamp and (x >= self.app.map_size[0] or y >= self.app.map_size[1]):
  26. return None
  27. return (min(x, self.app.map_size[0] - 1), min(y, self.app.map_size[1] - 1))
  28. def handle_event(self, event):
  29. if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
  30. cell = self.cell(event.pos)
  31. if cell is None:
  32. return False
  33. if pygame.key.get_mods() & pygame.KMOD_SHIFT:
  34. self.start = self.end = cell
  35. self.layer = "obstacle" if self.app.show_obstacle else "floor"
  36. return True
  37. if self.group:
  38. self.app.menu.edit(lambda: self.place(cell))
  39. self.app.menu.block_pointer = True
  40. return True
  41. if self.start is not None:
  42. if event.type == pygame.MOUSEMOTION:
  43. self.end = self.cell(event.pos, clamp=True)
  44. elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
  45. self.end = self.cell(event.pos, clamp=True)
  46. self.copy_rectangle(self.start, self.end, self.layer)
  47. self.start = self.end = None
  48. self.app.menu.block_pointer = True
  49. return True
  50. return False
  51. def copy_rectangle(self, start, end, layer):
  52. x0, x1 = sorted((start[0], end[0]))
  53. y0, y1 = sorted((start[1], end[1]))
  54. nodes = []
  55. if layer == "obstacle":
  56. seen = set()
  57. for (x, y), instance in self.app.map_obstacles_dict.items():
  58. if x0 <= x <= x1 and y0 <= y <= y1 and id(instance) not in seen:
  59. seen.add(id(instance))
  60. nodes.append(dict(type=instance.obstacle["TYPE"], name=instance.obstacle["NAME"],
  61. x=instance.x, y=instance.y, layer=layer))
  62. else:
  63. for node in self.app.map_json["floor"]:
  64. for point in node["points"]:
  65. if x0 <= point["x"] <= x1 and y0 <= point["y"] <= y1:
  66. nodes.append(dict(type=node["type"], name=node["name"],
  67. x=point["x"], y=point["y"], layer=layer))
  68. self.group = nodes
  69. self.clipboard_text = self.read_clipboard()
  70. self.app.blank_map_hint_area()
  71. if nodes:
  72. anchor_x, anchor_y = min(n["x"] for n in nodes), min(n["y"] for n in nodes)
  73. for node in nodes:
  74. node["x"] -= anchor_x
  75. node["y"] -= anchor_y
  76. self.app.menu.notify("已复制 %d 个元素;左键放置,Esc 取消" % len(nodes))
  77. def plan(self, position):
  78. result, cells = [], set()
  79. self.conflicts = set()
  80. valid = bool(self.group)
  81. moving = getattr(self.app.tools.extra, "sources", []) if self.app.tools.tool == "move" else []
  82. for node in self.group:
  83. x, y = position[0] + node["x"], position[1] + node["y"]
  84. obs = obstacle.get_obstacle(node["type"], node["name"]) if node["layer"] == "obstacle" else None
  85. width, height = (obs["WIDTH"], obs["HEIGHT"]) if obs else (1, 1)
  86. if obs and not self.app.show_obstacle:
  87. valid = False
  88. for dx in range(width):
  89. for dy in range(height):
  90. cell = (x + dx, y + dy)
  91. if (cell in cells or (obs is not None and cell in self.app.map_obstacles_dict and self.app.map_obstacles_dict[cell] not in moving) or
  92. not (0 <= cell[0] < self.app.map_size[0] and 0 <= cell[1] < self.app.map_size[1])):
  93. valid = False
  94. self.conflicts.add(cell)
  95. cells.add(cell)
  96. result.append((node, x, y, obs))
  97. return valid, result, cells
  98. def place(self, position):
  99. valid, nodes, cells = self.plan(position)
  100. if not valid:
  101. self.app.menu.notify("无法放置:有障碍物、超出地图或障碍物已隐藏")
  102. return False
  103. # The entire footprint is checked before making any change.
  104. for node, x, y, obs in nodes:
  105. if obs:
  106. ObstacleInstance(x, y, self.app.map_obstacles_dict, obs)
  107. else:
  108. self.app.remove_elem((x, y), "floor")
  109. target = next((n for n in self.app.map_json["floor"]
  110. if n["type"] == node["type"] and n["name"] == node["name"]), None)
  111. if target is None:
  112. target = dict(type=node["type"], name=node["name"], points=[])
  113. self.app.map_json["floor"].append(target)
  114. target["points"].append(dict(x=x, y=y))
  115. self.app.redraw_floor_obstacle_trigger = True
  116. return True
  117. def draw(self):
  118. overlay = pygame.Surface((MAP_W, MAP_H), pygame.SRCALPHA)
  119. scroll_x, scroll_y = self.app.scroll_grid_at
  120. if self.start is not None:
  121. x0, x1 = sorted((self.start[0], self.end[0]))
  122. y0, y1 = sorted((self.start[1], self.end[1]))
  123. rect = pygame.Rect((x0 - scroll_x) * GAME_SQUARE, (y0 - scroll_y) * GAME_SQUARE,
  124. (x1 - x0 + 1) * GAME_SQUARE, (y1 - y0 + 1) * GAME_SQUARE)
  125. pygame.draw.rect(overlay, (70, 155, 255, 55), rect)
  126. pygame.draw.rect(overlay, (100, 195, 255, 240), rect, 2)
  127. elif self.group and not self.app.menu.blocks_editing():
  128. position = self.cell(pygame.mouse.get_pos())
  129. if position is not None:
  130. valid, nodes, cells = self.plan(position)
  131. for x, y in cells:
  132. color = (255, 65, 65, 130) if (x, y) in self.conflicts else (170, 170, 170, 105)
  133. rect = pygame.Rect((x - scroll_x) * GAME_SQUARE, (y - scroll_y) * GAME_SQUARE,
  134. GAME_SQUARE, GAME_SQUARE)
  135. pygame.draw.rect(overlay, color, rect)
  136. pygame.draw.rect(overlay, color[:3] + (220,), rect, 1)
  137. self.app.screen.blit(overlay, MAP_AT)