layer.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # coding: utf-8
  2. import typing
  3. from pyglet.window import key
  4. import cocos
  5. from synergine2.config import Config
  6. from synergine2.log import SynergineLogger
  7. from synergine2_cocos2d.middleware import MapMiddleware
  8. if False:
  9. from synergine2_cocos2d.actor import Actor
  10. from synergine2_cocos2d.gui import GridManager
  11. from synergine2_cocos2d.gui import Gui
  12. from synergine2_cocos2d.interaction import InteractionManager
  13. class ScrollingManager(cocos.layer.ScrollingManager):
  14. def world_to_screen_positions(
  15. self,
  16. positions: typing.List[typing.Tuple[int, int]],
  17. ) -> typing.List[typing.Tuple[int, int]]:
  18. screen_positions = []
  19. for position in positions:
  20. scx, scy = self.world_to_screen(
  21. position[0],
  22. position[1],
  23. )
  24. screen_positions.append((scx, scy))
  25. return screen_positions
  26. def screen_to_world_positions(
  27. self,
  28. positions: typing.List[typing.Tuple[int, int]],
  29. ) -> typing.List[typing.Tuple[int, int]]:
  30. world_positions = []
  31. for position in positions:
  32. wx, wy = self.screen_to_world(
  33. position[0],
  34. position[1],
  35. )
  36. world_positions.append((wx, wy))
  37. return world_positions
  38. class SubjectLayer(cocos.layer.ScrollableLayer):
  39. def __init__(self, parallax: int=1):
  40. super().__init__(parallax)
  41. self.subjects_index = {} # type: typing.Dict[int, Actor]
  42. def add_subject(self, actor: 'Actor') -> None:
  43. self.add(actor)
  44. self.subjects_index[actor.subject.id] = actor
  45. class LayerManager(object):
  46. edit_layer_class = None # type: typing.Type['EditLayer']
  47. def __init__(
  48. self,
  49. config: Config,
  50. logger: SynergineLogger,
  51. middleware: MapMiddleware,
  52. interaction_manager: 'InteractionManager',
  53. gui: 'Gui',
  54. ) -> None:
  55. self.config = config
  56. self.logger = logger
  57. self.middleware = middleware
  58. self.interaction_manager = interaction_manager
  59. self.gui = gui
  60. self.grid_manager = None # type: GridManager
  61. self.scrolling_manager = None # type: ScrollingManager
  62. self.main_scene = None # type: cocos.scene.Scene
  63. self.main_layer = None # type: cocos.layer.Layer
  64. self.edit_layer = None # TODO type
  65. self.background_sprite = None # type: cocos.sprite.Sprite
  66. self.ground_layer = None # type: cocos.tiles.RectMapLayer
  67. self.subject_layer = None # type: SubjectLayer
  68. self.top_layer = None # type: cocos.tiles.RectMapLayer
  69. from synergine2_cocos2d.gui import EditLayer
  70. self.edit_layer_class = self.edit_layer_class or EditLayer
  71. self.debug = self.config.resolve('global.debug', False)
  72. def init(self) -> None:
  73. # TODO: cyclic import
  74. from synergine2_cocos2d.gui import MainLayer
  75. from synergine2_cocos2d.gui import GridManager
  76. self.middleware.init()
  77. self.grid_manager = GridManager(
  78. self.middleware.get_cell_width(),
  79. self.middleware.get_cell_height(),
  80. self.middleware.get_world_width(),
  81. self.middleware.get_world_height(),
  82. )
  83. self.main_scene = cocos.scene.Scene()
  84. self.scrolling_manager = ScrollingManager()
  85. self.main_layer = MainLayer(
  86. self,
  87. self.grid_manager,
  88. **{
  89. 'width': 1200, # Note: world size
  90. 'height': 1000, # Note: world size
  91. }
  92. )
  93. self.edit_layer = self.edit_layer_class(
  94. self.config,
  95. self.logger,
  96. self,
  97. self.grid_manager,
  98. self.main_layer,
  99. **{
  100. 'bindings': {
  101. key.LEFT: 'left',
  102. key.RIGHT: 'right',
  103. key.UP: 'up',
  104. key.DOWN: 'down',
  105. key.NUM_ADD: 'zoomin',
  106. key.NUM_SUBTRACT: 'zoomout'
  107. },
  108. 'mod_modify_selection': key.MOD_SHIFT,
  109. 'mod_restricted_mov': key.MOD_ACCEL,
  110. 'fastness': 160.0,
  111. 'autoscroll_border': 20.0, # in pixels, float; None disables autoscroll
  112. 'autoscroll_fastness': 320.0,
  113. 'wheel_multiplier': 2.5,
  114. 'zoom_min': 0.1,
  115. 'zoom_max': 2.0,
  116. 'zoom_fastness': 1.0
  117. }
  118. )
  119. self.main_scene.add(self.scrolling_manager)
  120. self.scrolling_manager.add(self.main_layer, z=0)
  121. self.main_scene.add(self.edit_layer)
  122. self.background_sprite = self.middleware.get_background_sprite()
  123. self.ground_layer = self.middleware.get_ground_layer()
  124. self.subject_layer = SubjectLayer()
  125. self.top_layer = self.middleware.get_top_layer()
  126. self.main_layer.add(self.background_sprite)
  127. self.main_layer.add(self.ground_layer)
  128. self.main_layer.add(self.subject_layer)
  129. self.main_layer.add(self.top_layer)
  130. def center(self):
  131. self.background_sprite.position = 0 + (self.background_sprite.width/2), 0 + (self.background_sprite.height/2)
  132. self.ground_layer.set_view(0, 0, self.ground_layer.px_width, self.ground_layer.px_height)
  133. self.subject_layer.position = 0, 0
  134. self.top_layer.set_view(0, 0, self.top_layer.px_width, self.top_layer.px_height)
  135. def add_subject(self, subject: 'Actor') -> None:
  136. self.subject_layer.add_subject(subject)
  137. def remove_subject(self, subject: 'Actor') -> None:
  138. self.subject_layer.remove(subject)
  139. def set_selectable(self, subject: 'Actor') -> None:
  140. self.edit_layer.set_selectable(subject)
  141. def unset_selectable(self, subject: 'Actor') -> None:
  142. self.edit_layer.unset_selectable(subject)