layer.py 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. def __init__(
  46. self,
  47. config: Config,
  48. logger: SynergineLogger,
  49. middleware: MapMiddleware,
  50. interaction_manager: 'InteractionManager',
  51. ) -> None:
  52. self.config = config
  53. self.logger = logger
  54. self.middleware = middleware
  55. self.interaction_manager = interaction_manager
  56. self.grid_manager = None # type: GridManager
  57. self.scrolling_manager = None # type: ScrollingManager
  58. self.main_scene = None # type: cocos.scene.Scene
  59. self.main_layer = None # type: cocos.layer.Layer
  60. self.edit_layer = None # TODO type
  61. self.background_sprite = None # type: cocos.sprite.Sprite
  62. self.ground_layer = None # type: cocos.tiles.RectMapLayer
  63. self.subject_layer = None # type: SubjectLayer
  64. self.top_layer = None # type: cocos.tiles.RectMapLayer
  65. def init(self) -> None:
  66. # TODO: cyclic import
  67. from synergine2_cocos2d.gui import MainLayer
  68. from synergine2_cocos2d.gui import EditLayer
  69. from synergine2_cocos2d.gui import GridManager
  70. self.middleware.init()
  71. self.grid_manager = GridManager(
  72. self.middleware.get_cell_width(),
  73. self.middleware.get_cell_height(),
  74. self.middleware.get_world_width(),
  75. self.middleware.get_world_height(),
  76. )
  77. self.main_scene = cocos.scene.Scene()
  78. self.scrolling_manager = ScrollingManager()
  79. self.main_layer = MainLayer(
  80. self,
  81. self.grid_manager,
  82. **{
  83. 'width': 1200, # Note: world size
  84. 'height': 1000, # Note: world size
  85. }
  86. )
  87. self.edit_layer = EditLayer(
  88. self.config,
  89. self.logger,
  90. self,
  91. self.grid_manager,
  92. self.main_layer,
  93. **{
  94. 'bindings': {
  95. key.LEFT: 'left',
  96. key.RIGHT: 'right',
  97. key.UP: 'up',
  98. key.DOWN: 'down',
  99. key.NUM_ADD: 'zoomin',
  100. key.NUM_SUBTRACT: 'zoomout'
  101. },
  102. 'mod_modify_selection': key.MOD_SHIFT,
  103. 'mod_restricted_mov': key.MOD_ACCEL,
  104. 'fastness': 160.0,
  105. 'autoscroll_border': 20.0, # in pixels, float; None disables autoscroll
  106. 'autoscroll_fastness': 320.0,
  107. 'wheel_multiplier': 2.5,
  108. 'zoom_min': 0.1,
  109. 'zoom_max': 2.0,
  110. 'zoom_fastness': 1.0
  111. }
  112. )
  113. self.main_scene.add(self.scrolling_manager)
  114. self.scrolling_manager.add(self.main_layer, z=0)
  115. self.main_scene.add(self.edit_layer)
  116. self.background_sprite = self.middleware.get_background_sprite()
  117. self.ground_layer = self.middleware.get_ground_layer()
  118. self.subject_layer = SubjectLayer()
  119. self.top_layer = self.middleware.get_top_layer()
  120. self.main_layer.add(self.background_sprite)
  121. self.main_layer.add(self.ground_layer)
  122. self.main_layer.add(self.subject_layer)
  123. self.main_layer.add(self.top_layer)
  124. def center(self):
  125. self.background_sprite.position = 0 + (self.background_sprite.width/2), 0 + (self.background_sprite.height/2)
  126. self.ground_layer.set_view(0, 0, self.ground_layer.px_width, self.ground_layer.px_height)
  127. self.subject_layer.position = 0, 0
  128. self.top_layer.set_view(0, 0, self.top_layer.px_width, self.top_layer.px_height)
  129. def add_subject(self, subject: 'Actor') -> None:
  130. self.subject_layer.add_subject(subject)
  131. def remove_subject(self, subject: 'Actor') -> None:
  132. self.subject_layer.remove(subject)
  133. def set_selectable(self, subject: 'Actor') -> None:
  134. self.edit_layer.set_selectable(subject)
  135. def unset_selectable(self, subject: 'Actor') -> None:
  136. self.edit_layer.unset_selectable(subject)