interaction.py 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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
  6. from synergine2.terminals import TerminalPackage
  7. from synergine2_cocos2d.exception import InteractionNotFound
  8. from synergine2_cocos2d.layer import LayerManager
  9. from synergine2_cocos2d.user_action import UserAction
  10. class InteractionManager(object):
  11. def __init__(
  12. self,
  13. config: Config,
  14. logger: SynergineLogger,
  15. terminal: Terminal,
  16. ) -> None:
  17. self.config = config
  18. self.logger = logger
  19. self.terminal = terminal
  20. self.interactions = []
  21. def register(
  22. self,
  23. interaction_class: typing.Type['Interaction'],
  24. layer_manager: LayerManager,
  25. ) -> None:
  26. self.interactions.append(interaction_class(
  27. self.config,
  28. self.logger,
  29. terminal=self.terminal,
  30. layer_manager=layer_manager,
  31. ))
  32. def get_for_user_action(self, action: UserAction) -> 'Interaction':
  33. for interaction in self.interactions:
  34. if interaction.gui_action == action:
  35. return interaction
  36. raise InteractionNotFound('For action"{}"'.format(action))
  37. class Interaction(object):
  38. gui_action = None # type: UserAction
  39. def __init__(
  40. self,
  41. config: Config,
  42. logger: SynergineLogger,
  43. terminal: Terminal,
  44. layer_manager: LayerManager,
  45. ) -> None:
  46. self.config = config
  47. self.logger = logger
  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()