interaction.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # coding: utf-8
  2. import typing
  3. from synergine2.config import Config
  4. from synergine2.log import get_logger
  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. terminal: Terminal,
  18. ) -> None:
  19. self.config = config
  20. self.logger = get_logger('InteractionManager', config)
  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. 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. terminal: Terminal,
  44. layer_manager: LayerManager,
  45. ) -> None:
  46. self.config = config
  47. self.logger = get_logger(self.__class__.__name__, config)
  48. self.terminal = terminal
  49. self.layer_manager = layer_manager
  50. def draw_pending(self) -> None:
  51. pass
  52. def execute(self) -> None:
  53. package = self.get_package_for_terminal()
  54. self.terminal.send(package)
  55. def get_package_for_terminal(self) -> TerminalPackage:
  56. raise NotImplementedError()
  57. class BaseActorInteraction(Interaction):
  58. gui_action = None
  59. color = None
  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_world_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. self.color,
  68. )
  69. def get_package_for_terminal(self) -> TerminalPackage:
  70. actions = []
  71. mouse_grid_position = self.layer_manager.grid_manager.get_grid_position(
  72. self.layer_manager.scrolling_manager.screen_to_world(
  73. *self.layer_manager.edit_layer.screen_mouse,
  74. )
  75. )
  76. for actor in self.layer_manager.edit_layer.selection:
  77. behaviour_class, behaviour_data = self.get_behaviour(actor, mouse_grid_position)
  78. actions.append((behaviour_class, behaviour_data))
  79. return TerminalPackage(
  80. simulation_actions=actions,
  81. )
  82. def get_behaviour(self, actor: Actor, mouse_grid_position) -> typing.Tuple[typing.Type[SimulationBehaviour], dict]:
  83. raise NotImplementedError()