layer.py 5.2KB

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