interaction.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # coding: utf-8
  2. import typing
  3. from synergine2.config import Config
  4. from synergine2.log import SynergineLogger
  5. from synergine2.simulation import SimulationBehaviour
  6. from synergine2.terminals import Terminal
  7. from synergine2.terminals import TerminalPackage
  8. from synergine2_cocos2d.actor import Actor
  9. from synergine2_cocos2d.exception import InteractionNotFound
  10. from synergine2_cocos2d.gl import draw_line
  11. from synergine2_cocos2d.layer import LayerManager
  12. from synergine2_cocos2d.user_action import UserAction
  13. class InteractionManager(object):
  14. def __init__(
  15. self,
  16. config: Config,
  17. logger: SynergineLogger,
  18. terminal: Terminal,
  19. ) -> None:
  20. self.config = config
  21. self.logger = logger
  22. self.terminal = terminal
  23. self.interactions = []
  24. def register(
  25. self,
  26. interaction_class: typing.Type['Interaction'],
  27. layer_manager: LayerManager,
  28. ) -> None:
  29. self.interactions.append(interaction_class(
  30. self.config,
  31. self.logger,
  32. terminal=self.terminal,
  33. layer_manager=layer_manager,
  34. ))
  35. def get_for_user_action(self, action: UserAction) -> 'Interaction':
  36. for interaction in self.interactions:
  37. if interaction.gui_action == action:
  38. return interaction
  39. raise InteractionNotFound('For action"{}"'.format(action))
  40. class Interaction(object):
  41. gui_action = None # type: UserAction
  42. def __init__(
  43. self,
  44. config: Config,
  45. logger: SynergineLogger,
  46. terminal: Terminal,
  47. layer_manager: LayerManager,
  48. ) -> None:
  49. self.config = config
  50. self.logger = logger
  51. self.terminal = terminal
  52. self.layer_manager = layer_manager
  53. def draw_pending(self) -> None:
  54. pass
  55. def execute(self) -> None:
  56. package = self.get_package_for_terminal()
  57. self.terminal.send(package)
  58. def get_package_for_terminal(self) -> TerminalPackage:
  59. raise NotImplementedError()
  60. class BaseActorInteraction(Interaction):
  61. gui_action = None
  62. color = None
  63. def draw_pending(self) -> None:
  64. for actor in self.layer_manager.edit_layer.selection:
  65. grid_position = self.layer_manager.grid_manager.get_grid_position(actor.position)
  66. pixel_position = self.layer_manager.grid_manager.get_world_position_of_grid_position(grid_position)
  67. draw_line(
  68. self.layer_manager.scrolling_manager.world_to_screen(*pixel_position),
  69. self.layer_manager.edit_layer.screen_mouse,
  70. self.color,
  71. )
  72. def get_package_for_terminal(self) -> TerminalPackage:
  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. behaviour_class, behaviour_data = self.get_behaviour(actor, mouse_grid_position)
  81. actions.append((behaviour_class, behaviour_data))
  82. return TerminalPackage(
  83. simulation_actions=actions,
  84. )
  85. def get_behaviour(self, actor: Actor, mouse_grid_position) -> typing.Tuple[typing.Type[SimulationBehaviour], dict]:
  86. raise NotImplementedError()