verify_selection.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """Group placement regression checks using an entirely temporary map."""
  2. import json
  3. import tempfile
  4. from pathlib import Path
  5. from verify_recovery import editor, pygame
  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_selection_") as tmp:
  11. target = Path(tmp) / "map.json"
  12. data = dict(basic=dict(name="test", width=40, height=40, scroll=[0, 0], begin=[0, 0], finish=[39, 39]),
  13. floor=[], obstacle=[], districts=[])
  14. target.write_text(json.dumps(data))
  15. editor.MAP_JSON_FILE_NAME = str(target)
  16. update = editor.MapBuilder.update
  17. editor.MapBuilder.update = lambda self: None
  18. try:
  19. app = editor.MapBuilder()
  20. finally:
  21. editor.MapBuilder.update = update
  22. for name, width, height in [("large", 2, 2), ("small", 1, 1)]:
  23. obstacle.obstacles.setdefault("selection_test", {})[name] = dict(
  24. TYPE="selection_test", NAME=name, WIDTH=width, HEIGHT=height,
  25. STAND=[Frame(pygame.Surface((width * 40, height * 40)), 0, 0)])
  26. large = obstacle.get_obstacle("selection_test", "large")
  27. small = obstacle.get_obstacle("selection_test", "small")
  28. ObstacleInstance(2, 2, app.map_obstacles_dict, large)
  29. ObstacleInstance(6, 2, app.map_obstacles_dict, large)
  30. selection = app.selection
  31. selection.copy_rectangle((7, 3), (3, 2), "obstacle")
  32. assert len(selection.group) == 2 # Partial selection includes the full object, once.
  33. before = app.document_snapshot()
  34. ObstacleInstance(15, 11, app.map_obstacles_dict, small)
  35. blocked = app.document_snapshot()
  36. assert not selection.place((10, 10)) # Collision in the second object's far corner.
  37. assert app.document_snapshot() == blocked and (10, 10) not in app.map_obstacles_dict
  38. app.remove_elem((15, 11), "obstacle")
  39. assert not selection.place((39, 39))
  40. assert app.document_snapshot() == before
  41. app.menu.edit(lambda: selection.place((10, 10)))
  42. placed = app.document_snapshot()
  43. assert (11, 11) in app.map_obstacles_dict and (15, 11) in app.map_obstacles_dict
  44. app.menu.undo()
  45. assert app.document_snapshot() == before
  46. app.menu.redo()
  47. assert app.document_snapshot() == placed
  48. assert not selection.place((10, 10)) # Existing copies may not be replaced.
  49. app.current_elem = dict(elem_type="obstacle", type="selection_test", name="large")
  50. app.add_elem((9, 9))
  51. assert app.map_obstacles_dict[(9, 9)] is app.map_obstacles_dict[(10, 10)]
  52. assert (11, 11) not in app.map_obstacles_dict # Overlapping object removed entirely.
  53. app.restore_document(placed)
  54. app.map_json["floor"] = [dict(type="test", name="tile", points=[dict(x=0, y=0), dict(x=1, y=0)])]
  55. selection.copy_rectangle((0, 0), (1, 0), "floor")
  56. floor_before = app.document_snapshot()
  57. assert selection.place((1, 2)) # Floor placement ignores obstacles without modifying them.
  58. assert app.document_snapshot()["obstacle"] == floor_before["obstacle"]
  59. assert selection.plan((1, 2))[0] and not selection.conflicts
  60. assert {"x": 2, "y": 2} in app.map_json["floor"][0]["points"]
  61. app.current_elem = dict(elem_type="floor", type="test", name="tile")
  62. app.add_elem((3, 3))
  63. assert {"x": 3, "y": 3} in app.map_json["floor"][0]["points"]
  64. assert app.document_snapshot()["obstacle"] == floor_before["obstacle"]
  65. before_bounds = app.document_snapshot()
  66. assert not selection.place((39, 39))
  67. assert app.document_snapshot() == before_bounds
  68. assert selection.place((20, 20))
  69. selection.cancel()
  70. assert not selection.group
  71. # Exercise Shift-drag events, reverse direction, and release outside the viewport.
  72. pygame.key.set_mods(pygame.KMOD_SHIFT)
  73. def pos(x, y):
  74. return (editor.MAP_AT[0] + x * 40 + 5, editor.MAP_AT[1] + y * 40 + 5)
  75. app.show_obstacle = True
  76. selection.handle_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN, button=1, pos=pos(7, 3)))
  77. selection.handle_event(pygame.event.Event(pygame.MOUSEMOTION, pos=pos(2, 2)))
  78. selection.handle_event(pygame.event.Event(pygame.MOUSEBUTTONUP, button=1, pos=pos(2, 2)))
  79. assert selection.start is None and len(selection.group) == 2
  80. pygame.key.set_mods(0)
  81. app.menu.begin_frame()
  82. app.menu.undo_stack.clear()
  83. app.menu.redo_stack.clear()
  84. event = pygame.event.Event(pygame.MOUSEBUTTONDOWN, button=1, pos=pos(20, 10))
  85. app.menu.handle_event(event)
  86. assert selection.handle_event(event)
  87. app.menu.handle_event(pygame.event.Event(pygame.MOUSEBUTTONUP, button=1, pos=event.pos))
  88. assert len(app.menu.undo_stack) == 1 and (24, 10) in app.map_obstacles_dict
  89. app.menu.undo()
  90. assert (24, 10) not in app.map_obstacles_dict
  91. app.save_map_json()
  92. assert target.exists()
  93. pygame.quit()
  94. print("PASS: Shift-drag copy, full-object deduplication, full-footprint collision, atomic rejection,")
  95. print("bounds, floors ignore obstacles, direct group placement, undo/redo, large-obstacle replacement")
  96. if __name__ == "__main__":
  97. verify()