verify_tools.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. """Temporary-map checks for tool panel, erasers, and per-cell copy preview."""
  2. import copy
  3. import tempfile
  4. from pathlib import Path
  5. from verify_recovery import editor, pygame, ROOT, HERE
  6. from game.frame import obstacle
  7. from game.frame.frame import Frame
  8. from game.frame.obstacle_instance import ObstacleInstance
  9. def verify():
  10. with tempfile.TemporaryDirectory(prefix="editor_tools_") as tmp:
  11. target = Path(tmp) / "map.json"
  12. target.write_bytes((ROOT / "map.json").read_bytes())
  13. editor.MAP_JSON_FILE_NAME = str(target)
  14. update = editor.MapBuilder.update
  15. editor.MapBuilder.update = lambda self: None
  16. try:
  17. app = editor.MapBuilder()
  18. finally:
  19. editor.MapBuilder.update = update
  20. original = app.document_snapshot()
  21. tools = app.tools
  22. assert tools.sizes() == []
  23. assert tools.visible and app.screen.get_size() == (1776, 872)
  24. tools.toggle()
  25. assert not tools.visible and app.screen.get_size() == (1776, 872)
  26. tools.choose("floor")
  27. assert tools.visible
  28. for n, rect in tools.sizes():
  29. tools.handle_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN, button=1, pos=rect.center))
  30. assert tools.size == n
  31. app.map_size = (40, 40)
  32. app.map_json["basic"]["width"] = app.map_json["basic"]["height"] = 40
  33. app.map_obstacles_dict.clear()
  34. obs = dict(TYPE="tools_test", NAME="large", WIDTH=2, HEIGHT=2,
  35. STAND=[Frame(pygame.Surface((80, 80)), 0, 0)])
  36. obstacle.obstacles["tools_test"] = {"large": obs}
  37. ObstacleInstance(3, 3, app.map_obstacles_dict, obs)
  38. for size in range(1, 16):
  39. 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)])]
  40. tools.size = size
  41. tools.erase((10, 10))
  42. bounds = tools.erase_bounds((10, 10))
  43. assert bounds == (10 - (size - 1) // 2, 10 - (size - 1) // 2,
  44. 10 - (size - 1) // 2 + size, 10 - (size - 1) // 2 + size)
  45. assert len(app.map_json["floor"][0]["points"]) == 400 - size * size
  46. assert (3, 3) in app.map_obstacles_dict
  47. tools.choose("obstacle")
  48. tools.size = 1
  49. before = app.document_snapshot()
  50. app.menu.edit(lambda: tools.erase((4, 4)))
  51. assert not app.map_obstacles_dict
  52. assert app.map_json["floor"] == before["floor"]
  53. app.menu.undo()
  54. assert app.document_snapshot() == before
  55. app.menu.redo()
  56. assert not app.map_obstacles_dict
  57. tools.choose("floor")
  58. tools.size = 15
  59. app.map_json["floor"] = [dict(type="test", name="tile", points=[dict(x=39, y=39), dict(x=38, y=39)])]
  60. tools.erase((39, 39))
  61. assert app.map_json["floor"][0]["points"] == []
  62. assert tools.erase_bounds((39, 39)) == (32, 32, 40, 40)
  63. assert tools.erase_bounds((0, 0)) == (0, 0, 8, 8)
  64. # A rapid drag interpolates cells and remains one undo step.
  65. app.map_json["floor"][0]["points"] = [dict(x=x, y=0) for x in range(8)]
  66. tools.size = 1
  67. app.scroll_grid_at = [0, 0]
  68. app.menu.block_pointer = False
  69. app.menu.undo_stack.clear()
  70. def event(kind, x):
  71. values = dict(pos=(editor.MAP_AT[0] + x * 40 + 4, editor.MAP_AT[1] + 4))
  72. if kind != pygame.MOUSEMOTION:
  73. values["button"] = 1
  74. return pygame.event.Event(kind, **values)
  75. down = event(pygame.MOUSEBUTTONDOWN, 0)
  76. app.menu.handle_event(down)
  77. tools.handle_event(down)
  78. tools.handle_event(event(pygame.MOUSEMOTION, 7))
  79. up = event(pygame.MOUSEBUTTONUP, 7)
  80. app.menu.handle_event(up)
  81. tools.handle_event(up)
  82. assert not app.map_json["floor"][0]["points"] and len(app.menu.undo_stack) == 1
  83. app.menu.undo()
  84. assert len(app.map_json["floor"][0]["points"]) == 8
  85. # Render conflicts red, other copied cells gray (not whole-group red).
  86. tools.choose("paint")
  87. assert tools.sizes() == []
  88. old_size = tools.size
  89. tools.handle_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN, button=1,
  90. pos=(editor.WINDOW_W + 30, 420)))
  91. assert tools.size == old_size
  92. app.selection.group = [dict(layer="obstacle", type="tools_test", name="large", x=0, y=0)]
  93. ObstacleInstance(5, 5, app.map_obstacles_dict, obs)
  94. valid, nodes, cells = app.selection.plan((4, 4))
  95. assert not valid and app.selection.conflicts == {(5, 5)}
  96. mouse = pygame.mouse.get_pos
  97. try:
  98. pygame.mouse.get_pos = lambda: (editor.MAP_AT[0] + 4 * 40 + 10, editor.MAP_AT[1] + 4 * 40 + 10)
  99. app.menu.block_pointer = False
  100. app.screen.fill((0, 0, 0))
  101. app.selection.draw()
  102. gray = app.screen.get_at(pygame.mouse.get_pos())
  103. red = app.screen.get_at((editor.MAP_AT[0] + 5 * 40 + 10, editor.MAP_AT[1] + 5 * 40 + 10))
  104. assert gray.r == gray.g == gray.b and red.r > red.g
  105. app.restore_document(original)
  106. tools.choose("floor")
  107. tools.size = 3
  108. app.screen.fill((0, 0, 0))
  109. app.update_obstacles()
  110. app.draw_coordinators()
  111. app.draw_floors_and_obstacles()
  112. app.draw_map_area()
  113. tools.draw_preview()
  114. tools.draw()
  115. app.menu.draw()
  116. pygame.image.save(app.screen, str(HERE / "tools_preview.png"))
  117. finally:
  118. pygame.mouse.get_pos = mouse
  119. pygame.quit()
  120. print("PASS: toolbar show/hide, all 15 sizes, floor/obstacle isolation, multi-cell removal,")
  121. print("edge clipping, interpolated drag undo/redo, red conflict cells and gray free cells")
  122. if __name__ == "__main__":
  123. verify()