gui.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # coding: utf-8
  2. import cocos
  3. import pyglet
  4. from pyglet.window import key as wkey
  5. from cocos.director import director
  6. from cocos.layer import ScrollableLayer, Layer
  7. from cocos.sprite import Sprite
  8. from synergine2.config import Config
  9. from synergine2.log import SynergineLogger
  10. from synergine2.terminals import Terminal
  11. from synergine2.terminals import TerminalPackage
  12. class GridManager(object):
  13. def __init__(
  14. self,
  15. layer: Layer,
  16. square_width: int,
  17. border: int=0,
  18. ):
  19. self.layer = layer
  20. self.square_width = square_width
  21. self.border = border
  22. @property
  23. def final_width(self):
  24. return self.square_width + self.border
  25. def scale_sprite(self, sprite: Sprite):
  26. sprite.scale_x = self.final_width / sprite.image.width
  27. sprite.scale_y = self.final_width / sprite.image.height
  28. def position_sprite(self, sprite: Sprite, grid_position):
  29. grid_x = grid_position[0]
  30. grid_y = grid_position[1]
  31. sprite.position = grid_x * self.final_width, grid_y * self.final_width
  32. def get_window_position(self, grid_position_x, grid_position_y):
  33. grid_x = grid_position_x
  34. grid_y = grid_position_y
  35. return grid_x * self.final_width, grid_y * self.final_width
  36. def get_grid_position(self, window_x, window_y, z=0) -> tuple:
  37. window_size = director.get_window_size()
  38. window_center_x = window_size[0] // 2
  39. window_center_y = window_size[1] // 2
  40. window_relative_x = window_x - window_center_x
  41. window_relative_y = window_y - window_center_y
  42. real_width = self.final_width * self.layer.scale
  43. return int(window_relative_x // real_width),\
  44. int(window_relative_y // real_width),\
  45. z
  46. class GridLayerMixin(object):
  47. def __init__(self, *args, **kwargs):
  48. square_width = kwargs.pop('square_width', 32)
  49. square_border = kwargs.pop('square_border', 2)
  50. self.grid_manager = GridManager(
  51. self,
  52. square_width=square_width,
  53. border=square_border,
  54. )
  55. super().__init__(*args, **kwargs)
  56. class MainLayer(ScrollableLayer):
  57. is_event_handler = True
  58. def __init__(self, terminal: Terminal, scroll_step: int=100):
  59. super().__init__()
  60. self.terminal = terminal
  61. self.scroll_step = scroll_step
  62. self.grid_manager = GridManager(self, 32, border=2)
  63. # Set scene center on center of screen
  64. window_size = director.get_window_size()
  65. self.position = window_size[0] // 2, window_size[1] // 2
  66. def on_key_press(self, key, modifiers):
  67. if key == wkey.LEFT:
  68. self.position = (self.position[0] + self.scroll_step, self.position[1])
  69. if key == wkey.RIGHT:
  70. self.position = (self.position[0] - self.scroll_step, self.position[1])
  71. if key == wkey.UP:
  72. self.position = (self.position[0], self.position[1] - self.scroll_step)
  73. if key == wkey.DOWN:
  74. self.position = (self.position[0], self.position[1] + self.scroll_step)
  75. if key == wkey.A:
  76. if self.scale >= 0.3:
  77. self.scale -= 0.2
  78. if key == wkey.Z:
  79. if self.scale <= 4:
  80. self.scale += 0.2
  81. class Gui(object):
  82. def __init__(
  83. self,
  84. config: Config,
  85. logger: SynergineLogger,
  86. terminal: Terminal,
  87. read_queue_interval: float= 1/60.0,
  88. ):
  89. self.config = config
  90. self.logger = logger,
  91. self._read_queue_interval = read_queue_interval
  92. self.terminal = terminal
  93. self.cycle_duration = self.config.core.cycle_duration
  94. cocos.director.director.init()
  95. def run(self):
  96. pyglet.clock.schedule_interval(
  97. lambda *_, **__: self.terminal.read(),
  98. self._read_queue_interval,
  99. )
  100. cocos.director.director.run(self.get_main_scene())
  101. def get_main_scene(self) -> cocos.cocosnode.CocosNode:
  102. raise NotImplementedError()
  103. def before_received(self, package: TerminalPackage):
  104. pass
  105. def after_received(self, package: TerminalPackage):
  106. pass