| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- """Temporary-map checks for tool panel, erasers, and per-cell copy preview."""
- import copy
- import tempfile
- from pathlib import Path
- from verify_recovery import editor, pygame, ROOT, HERE
- from game.frame import obstacle
- from game.frame.frame import Frame
- from game.frame.obstacle_instance import ObstacleInstance
- def verify():
- with tempfile.TemporaryDirectory(prefix="editor_tools_") as tmp:
- target = Path(tmp) / "map.json"
- target.write_bytes((ROOT / "map.json").read_bytes())
- editor.MAP_JSON_FILE_NAME = str(target)
- update = editor.MapBuilder.update
- editor.MapBuilder.update = lambda self: None
- try:
- app = editor.MapBuilder()
- finally:
- editor.MapBuilder.update = update
- original = app.document_snapshot()
- tools = app.tools
- assert tools.sizes() == []
- assert tools.visible and app.screen.get_size() == (1776, 872)
- tools.toggle()
- assert not tools.visible and app.screen.get_size() == (1776, 872)
- tools.choose("floor")
- assert tools.visible
- for n, rect in tools.sizes():
- tools.handle_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN, button=1, pos=rect.center))
- assert tools.size == n
- app.map_size = (40, 40)
- app.map_json["basic"]["width"] = app.map_json["basic"]["height"] = 40
- app.map_obstacles_dict.clear()
- obs = dict(TYPE="tools_test", NAME="large", WIDTH=2, HEIGHT=2,
- STAND=[Frame(pygame.Surface((80, 80)), 0, 0)])
- obstacle.obstacles["tools_test"] = {"large": obs}
- ObstacleInstance(3, 3, app.map_obstacles_dict, obs)
- for size in range(1, 16):
- app.map_json["floor"] = [dict(type="test", name="tile", points=[dict(x=x, y=y) for x in range(20) for y in range(20)])]
- tools.size = size
- tools.erase((10, 10))
- bounds = tools.erase_bounds((10, 10))
- assert bounds == (10 - (size - 1) // 2, 10 - (size - 1) // 2,
- 10 - (size - 1) // 2 + size, 10 - (size - 1) // 2 + size)
- assert len(app.map_json["floor"][0]["points"]) == 400 - size * size
- assert (3, 3) in app.map_obstacles_dict
- tools.choose("obstacle")
- tools.size = 1
- before = app.document_snapshot()
- app.menu.edit(lambda: tools.erase((4, 4)))
- assert not app.map_obstacles_dict
- assert app.map_json["floor"] == before["floor"]
- app.menu.undo()
- assert app.document_snapshot() == before
- app.menu.redo()
- assert not app.map_obstacles_dict
- tools.choose("floor")
- tools.size = 15
- app.map_json["floor"] = [dict(type="test", name="tile", points=[dict(x=39, y=39), dict(x=38, y=39)])]
- tools.erase((39, 39))
- assert app.map_json["floor"][0]["points"] == []
- assert tools.erase_bounds((39, 39)) == (32, 32, 40, 40)
- assert tools.erase_bounds((0, 0)) == (0, 0, 8, 8)
- # A rapid drag interpolates cells and remains one undo step.
- app.map_json["floor"][0]["points"] = [dict(x=x, y=0) for x in range(8)]
- tools.size = 1
- app.scroll_grid_at = [0, 0]
- app.menu.block_pointer = False
- app.menu.undo_stack.clear()
- def event(kind, x):
- values = dict(pos=(editor.MAP_AT[0] + x * 40 + 4, editor.MAP_AT[1] + 4))
- if kind != pygame.MOUSEMOTION:
- values["button"] = 1
- return pygame.event.Event(kind, **values)
- down = event(pygame.MOUSEBUTTONDOWN, 0)
- app.menu.handle_event(down)
- tools.handle_event(down)
- tools.handle_event(event(pygame.MOUSEMOTION, 7))
- up = event(pygame.MOUSEBUTTONUP, 7)
- app.menu.handle_event(up)
- tools.handle_event(up)
- assert not app.map_json["floor"][0]["points"] and len(app.menu.undo_stack) == 1
- app.menu.undo()
- assert len(app.map_json["floor"][0]["points"]) == 8
- # Render conflicts red, other copied cells gray (not whole-group red).
- tools.choose("paint")
- assert tools.sizes() == []
- old_size = tools.size
- tools.handle_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN, button=1,
- pos=(editor.WINDOW_W + 30, 420)))
- assert tools.size == old_size
- app.selection.group = [dict(layer="obstacle", type="tools_test", name="large", x=0, y=0)]
- ObstacleInstance(5, 5, app.map_obstacles_dict, obs)
- valid, nodes, cells = app.selection.plan((4, 4))
- assert not valid and app.selection.conflicts == {(5, 5)}
- mouse = pygame.mouse.get_pos
- try:
- pygame.mouse.get_pos = lambda: (editor.MAP_AT[0] + 4 * 40 + 10, editor.MAP_AT[1] + 4 * 40 + 10)
- app.menu.block_pointer = False
- app.screen.fill((0, 0, 0))
- app.selection.draw()
- gray = app.screen.get_at(pygame.mouse.get_pos())
- red = app.screen.get_at((editor.MAP_AT[0] + 5 * 40 + 10, editor.MAP_AT[1] + 5 * 40 + 10))
- assert gray.r == gray.g == gray.b and red.r > red.g
- app.restore_document(original)
- tools.choose("floor")
- tools.size = 3
- app.screen.fill((0, 0, 0))
- app.update_obstacles()
- app.draw_coordinators()
- app.draw_floors_and_obstacles()
- app.draw_map_area()
- tools.draw_preview()
- tools.draw()
- app.menu.draw()
- pygame.image.save(app.screen, str(HERE / "tools_preview.png"))
- finally:
- pygame.mouse.get_pos = mouse
- pygame.quit()
- print("PASS: toolbar show/hide, all 15 sizes, floor/obstacle isolation, multi-cell removal,")
- print("edge clipping, interpolated drag undo/redo, red conflict cells and gray free cells")
- if __name__ == "__main__":
- verify()
|