interaction.py 3.2KB

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