input_manager.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import logging
  2. import time
  3. import pygame
  4. logger = logging.getLogger(__name__)
  5. class InputManager():
  6. click = 1
  7. swipe = 2
  8. long_click = 3
  9. key = 4
  10. long_click_min_time = 0.3
  11. up = 0
  12. down = 1
  13. left = 2
  14. right = 3
  15. enter = 4
  16. def __init__(self, size):
  17. self.down_pos = (0, 0)
  18. self.up_pos = (0, 0)
  19. self.screen_size = size
  20. self.max_move_margin = self.screen_size[1] / 6
  21. self.min_swipe_move = self.screen_size[1] / 3
  22. self.down_button = -1
  23. self.down_time = -1
  24. self.last_key = -1
  25. def event(self, event):
  26. if event.type == pygame.MOUSEBUTTONUP:
  27. if event.button == 4:
  28. touch_event = InputEvent(InputManager.swipe, self.down_pos,
  29. self.up_pos, True, InputManager.up)
  30. return touch_event
  31. elif event.button == 5:
  32. touch_event = InputEvent(InputManager.swipe, self.down_pos,
  33. self.up_pos, True, InputManager.down)
  34. return touch_event
  35. elif event.button == 1 and self.down_button == 1:
  36. touch_event = self.mouse_up(event)
  37. return touch_event
  38. elif event.button == 3 and self.down_button == 3:
  39. touch_event = InputEvent(InputManager.long_click,
  40. self.down_pos, self.up_pos, None, None)
  41. return touch_event
  42. else:
  43. return None
  44. elif event.type == pygame.MOUSEBUTTONDOWN:
  45. self.mouse_down(event)
  46. return None
  47. elif event.type == pygame.KEYDOWN:
  48. self.key_down(event)
  49. return None
  50. elif event.type == pygame.KEYUP:
  51. return self.key_up(event)
  52. def key_down(self, event):
  53. self.last_key = event.key
  54. self.down_time = time.time()
  55. def key_up(self, event):
  56. if self.last_key == event.key:
  57. if self.last_key == pygame.K_DOWN:
  58. direction = InputManager.down
  59. elif self.last_key == pygame.K_UP:
  60. direction = InputManager.up
  61. elif self.last_key == pygame.K_LEFT:
  62. direction = InputManager.left
  63. elif self.last_key == pygame.K_RIGHT:
  64. direction = InputManager.right
  65. elif self.last_key == pygame.K_RETURN:
  66. direction = InputManager.enter
  67. else:
  68. return None
  69. if direction is not None:
  70. return InputEvent(InputManager.key, None, None, None, direction)
  71. def mouse_down(self, event):
  72. self.down_pos = event.pos
  73. self.down_button = event.button
  74. self.down_time = time.time()
  75. def mouse_up(self, event):
  76. self.up_pos = event.pos
  77. if abs(self.down_pos[0] - self.up_pos[0]) < self.max_move_margin:
  78. if abs(self.down_pos[1] - self.up_pos[1]) < self.max_move_margin:
  79. if time.time() - InputManager.long_click_min_time > \
  80. self.down_time:
  81. return InputEvent(InputManager.long_click, self.down_pos,
  82. self.up_pos, None, None)
  83. else:
  84. return InputEvent(InputManager.click, self.down_pos,
  85. self.up_pos, None, None)
  86. elif abs(self.down_pos[1] - self.up_pos[1]) > self.min_swipe_move:
  87. return InputEvent(InputManager.swipe, self.down_pos,
  88. self.up_pos, True, None)
  89. elif self.down_pos[1] - self.up_pos[1] < self.max_move_margin:
  90. if abs(self.down_pos[0] - self.up_pos[0]) > self.min_swipe_move:
  91. return InputEvent(InputManager.swipe, self.down_pos,
  92. self.up_pos, False, None)
  93. class InputEvent():
  94. def __init__(self, event_type, down_pos, current_pos, vertical, direction):
  95. self.type = event_type
  96. self.down_pos = down_pos
  97. self.current_pos = current_pos
  98. if event_type is InputManager.swipe and direction is None:
  99. if vertical:
  100. if self.down_pos[1] < self.current_pos[1]:
  101. self.direction = InputManager.down
  102. else:
  103. self.direction = InputManager.up
  104. else:
  105. if self.down_pos[0] < self.current_pos[0]:
  106. self.direction = InputManager.right
  107. else:
  108. self.direction = InputManager.left
  109. else:
  110. self.direction = direction