interaction.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # coding: utf-8
  2. import typing
  3. from synergine2_xyz.move import RequestMoveBehaviour
  4. from synergine2.config import Config
  5. from synergine2.log import SynergineLogger
  6. from synergine2.terminals import Terminal
  7. from synergine2.terminals import TerminalPackage
  8. from synergine2_cocos2d.exception import InteractionNotFound
  9. from synergine2_cocos2d.gl import draw_line
  10. from synergine2_cocos2d.layer import LayerManager
  11. from synergine2_cocos2d.user_action import UserAction
  12. class InteractionManager(object):
  13. def __init__(
  14. self,
  15. config: Config,
  16. logger: SynergineLogger,
  17. terminal: Terminal,
  18. ) -> None:
  19. self.config = config
  20. self.logger = logger
  21. self.terminal = terminal
  22. self.interactions = []
  23. def register(
  24. self,
  25. interaction_class: typing.Type['Interaction'],
  26. layer_manager: LayerManager,
  27. ) -> None:
  28. self.interactions.append(interaction_class(
  29. self.config,
  30. self.logger,
  31. terminal=self.terminal,
  32. layer_manager=layer_manager,
  33. ))
  34. def get_for_user_action(self, action: UserAction) -> 'Interaction':
  35. for interaction in self.interactions:
  36. if interaction.gui_action == action:
  37. return interaction
  38. raise InteractionNotFound('For action"{}"'.format(action))
  39. class Interaction(object):
  40. gui_action = None # type: UserAction
  41. def __init__(
  42. self,
  43. config: Config,
  44. logger: SynergineLogger,
  45. terminal: Terminal,
  46. layer_manager: LayerManager,
  47. ) -> None:
  48. self.config = config
  49. self.logger = logger
  50. self.terminal = terminal
  51. self.layer_manager = layer_manager
  52. def draw_pending(self) -> None:
  53. pass
  54. def execute(self) -> None:
  55. package = self.get_package_for_terminal()
  56. self.terminal.send(package)
  57. def get_package_for_terminal(self) -> TerminalPackage:
  58. raise NotImplementedError()
  59. class MoveActorInteraction(Interaction):
  60. gui_action = UserAction.ORDER_MOVE
  61. request_move_behaviour_class = RequestMoveBehaviour
  62. def draw_pending(self) -> None:
  63. for actor in self.layer_manager.edit_layer.selection:
  64. grid_position = self.layer_manager.grid_manager.get_grid_position(actor.position)
  65. pixel_position = self.layer_manager.grid_manager.get_pixel_position_of_grid_position(grid_position)
  66. draw_line(
  67. self.layer_manager.scrolling_manager.world_to_screen(*pixel_position),
  68. self.layer_manager.edit_layer.screen_mouse,
  69. (0, 0, 255),
  70. )
  71. def get_package_for_terminal(self) -> TerminalPackage:
  72. # TODO: MoveEvent ?
  73. actions = []
  74. mouse_grid_position = self.layer_manager.grid_manager.get_grid_position(
  75. self.layer_manager.scrolling_manager.screen_to_world(
  76. *self.layer_manager.edit_layer.screen_mouse,
  77. )
  78. )
  79. for actor in self.layer_manager.edit_layer.selection:
  80. actions.append((
  81. self.request_move_behaviour_class, {
  82. 'subject_id': actor.subject.id,
  83. 'move_to': mouse_grid_position,
  84. }
  85. ))
  86. return TerminalPackage(
  87. simulation_actions=actions,
  88. )