layer.py 5.6KB

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