123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- # coding: utf-8
- import typing
-
- from synergine2_xyz.move import RequestMoveBehaviour
-
- from synergine2.config import Config
- from synergine2.log import SynergineLogger
- from synergine2.terminals import Terminal
- from synergine2.terminals import TerminalPackage
- from synergine2_cocos2d.exception import InteractionNotFound
- from synergine2_cocos2d.gl import draw_line
- from synergine2_cocos2d.layer import LayerManager
- from synergine2_cocos2d.user_action import UserAction
-
-
- class InteractionManager(object):
- def __init__(
- self,
- config: Config,
- logger: SynergineLogger,
- terminal: Terminal,
- ) -> None:
- self.config = config
- self.logger = logger
- self.terminal = terminal
- self.interactions = []
-
- def register(
- self,
- interaction_class: typing.Type['Interaction'],
- layer_manager: LayerManager,
- ) -> None:
- self.interactions.append(interaction_class(
- self.config,
- self.logger,
- terminal=self.terminal,
- layer_manager=layer_manager,
- ))
-
- def get_for_user_action(self, action: UserAction) -> 'Interaction':
- for interaction in self.interactions:
- if interaction.gui_action == action:
- return interaction
- raise InteractionNotFound('For action"{}"'.format(action))
-
-
- class Interaction(object):
- gui_action = None # type: UserAction
-
- def __init__(
- self,
- config: Config,
- logger: SynergineLogger,
- terminal: Terminal,
- layer_manager: LayerManager,
- ) -> None:
- self.config = config
- self.logger = logger
- self.terminal = terminal
- self.layer_manager = layer_manager
-
- def draw_pending(self) -> None:
- pass
-
- def execute(self) -> None:
- package = self.get_package_for_terminal()
- self.terminal.send(package)
-
- def get_package_for_terminal(self) -> TerminalPackage:
- raise NotImplementedError()
-
-
- class MoveActorInteraction(Interaction):
- gui_action = UserAction.ORDER_MOVE
- request_move_behaviour_class = RequestMoveBehaviour
-
- def draw_pending(self) -> None:
- for actor in self.layer_manager.edit_layer.selection:
- grid_position = self.layer_manager.grid_manager.get_grid_position(actor.position)
- pixel_position = self.layer_manager.grid_manager.get_pixel_position_of_grid_position(grid_position)
-
- draw_line(
- self.layer_manager.scrolling_manager.world_to_screen(*pixel_position),
- self.layer_manager.edit_layer.screen_mouse,
- (0, 0, 255),
- )
-
- def get_package_for_terminal(self) -> TerminalPackage:
- # TODO: MoveEvent ?
- actions = []
- mouse_grid_position = self.layer_manager.grid_manager.get_grid_position(
- self.layer_manager.scrolling_manager.screen_to_world(
- *self.layer_manager.edit_layer.screen_mouse,
- )
- )
-
- for actor in self.layer_manager.edit_layer.selection:
- actions.append((
- self.request_move_behaviour_class, {
- 'subject_id': actor.subject.id,
- 'move_to': mouse_grid_position,
- }
- ))
-
- return TerminalPackage(
- simulation_actions=actions,
- )
|