keyboard_screen.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import pygame
  2. from .base_screen import BaseScreen
  3. from ..graphic_utils import ScreenObjectsManager, TouchAndTextItem
  4. from ..input import InputManager
  5. class Keyboard(BaseScreen):
  6. def __init__(self, size, base_size, manager, fonts, listener):
  7. BaseScreen.__init__(self, size, base_size, manager, fonts)
  8. self.base_width = size[0]/10
  9. self.base_height = size[1]/5
  10. self.listener = listener
  11. self.manager = manager
  12. self.selected_row = 0
  13. self.selected_col = 0
  14. self.selected_others = -1
  15. self.font = pygame.font.SysFont("arial", size[1]/6)
  16. self.keyboards = [ScreenObjectsManager(), ScreenObjectsManager()]
  17. self.other_objects = ScreenObjectsManager()
  18. self.current_keyboard = 0
  19. self.keys = [[['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
  20. ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', '-'],
  21. [',', 'z', 'x', 'c', 'v', 'b', 'n', 'm', '.', '_']],
  22. [['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
  23. ['!', '@', '#', '$', '%', '&', '/', '(', ')', '='],
  24. ['?', '{', '}', '_', '[', ']', '+', '<', '>', '*']]]
  25. line = self.base_height
  26. for row in self.keys[self.current_keyboard]:
  27. pos = 0
  28. for key in row:
  29. button = \
  30. TouchAndTextItem(self.font, key,
  31. (pos, line),
  32. (self.base_width, self.base_height),
  33. center=True, background=(150, 150, 150))
  34. self.keyboards[self.current_keyboard].\
  35. set_touch_object(key, button)
  36. pos += self.base_width
  37. line += self.base_height
  38. self.current_keyboard = 1
  39. line = self.base_height
  40. for row in self.keys[self.current_keyboard]:
  41. pos = 0
  42. for key in row:
  43. button = \
  44. TouchAndTextItem(self.font, key, (pos, line),
  45. (self.base_width, self.base_height),
  46. center=True, background=(150, 150, 150))
  47. self.keyboards[self.current_keyboard].\
  48. set_touch_object(key, button)
  49. pos += self.base_width
  50. line += self.base_height
  51. self.current_keyboard = 0
  52. # Symbol button
  53. button = TouchAndTextItem(self.font, "123",
  54. (0, self.base_height*4),
  55. (self.base_width*2, self.base_height),
  56. center=True, background=(150, 150, 150))
  57. self.other_objects.set_touch_object("symbols", button)
  58. # remove button
  59. button = TouchAndTextItem(self.font, "<-",
  60. (self.base_width*2, self.base_height*4),
  61. (self.base_width*2, self.base_height),
  62. center=True, background=(150, 150, 150))
  63. self.other_objects.set_touch_object("remove", button)
  64. # Space button
  65. button = TouchAndTextItem(self.font, " ",
  66. (self.base_width*4, self.base_height*4),
  67. (self.base_width*4, self.base_height),
  68. center=True, background=(150, 150, 150))
  69. self.other_objects.set_touch_object("space", button)
  70. # OK button
  71. button = TouchAndTextItem(self.font, "->",
  72. (self.base_width*8, self.base_height*4),
  73. (self.base_width*2, self.base_height),
  74. center=True, background=(150, 150, 150))
  75. self.other_objects.set_touch_object("ok", button)
  76. # EditText button
  77. button = TouchAndTextItem(self.font, "",
  78. (0, 0),
  79. (self.base_width*10, self.base_height),
  80. center=True)
  81. self.other_objects.set_object("text", button)
  82. self.change_selected(0, 0)
  83. def update(self, screen):
  84. screen.fill((0, 0, 0))
  85. self.keyboards[self.current_keyboard].render(screen)
  86. self.other_objects.render(screen)
  87. def touch_event(self, touch_event):
  88. if touch_event.type == InputManager.click:
  89. keys = self.keyboards[self.current_keyboard]\
  90. .get_touch_objects_in_pos(touch_event.current_pos)
  91. for key in keys:
  92. self.other_objects.get_object("text").add_text(key, False)
  93. keys = self.other_objects.get_touch_objects_in_pos(
  94. touch_event.current_pos)
  95. for key in keys:
  96. if key == 'symbols':
  97. self.change_keyboard()
  98. elif key == "remove":
  99. self.other_objects.get_object("text").remove_text(1, False)
  100. elif key == "space":
  101. self.other_objects.get_object("text").add_text(" ", False)
  102. elif key == "ok":
  103. text = self.other_objects.get_object("text").text
  104. self.listener.text_input(text)
  105. self.manager.close_keyboard()
  106. elif touch_event.type == InputManager.key:
  107. if isinstance(touch_event.unicode, unicode):
  108. if touch_event.unicode == u'\x08':
  109. self.other_objects.get_object("text").remove_text(1, False)
  110. else:
  111. self.other_objects.get_object("text").add_text(
  112. touch_event.unicode, False)
  113. elif touch_event.direction is not None:
  114. x = 0
  115. y = 0
  116. if touch_event.direction == InputManager.left:
  117. x = -1
  118. elif touch_event.direction == InputManager.right:
  119. x = 1
  120. elif touch_event.direction == InputManager.up:
  121. y = -1
  122. elif touch_event.direction == InputManager.down:
  123. y = 1
  124. self.change_selected(x, y)
  125. def change_keyboard(self):
  126. if self.current_keyboard == 0:
  127. self.current_keyboard = 1
  128. else:
  129. self.current_keyboard = 0
  130. if self.selected_others < 0:
  131. self.change_selected(0, 0)
  132. def change_selected(self, x, y):
  133. if self.selected_others < 0:
  134. self.selected_row += x
  135. if self.selected_row < 0:
  136. self.selected_row = 0
  137. elif self.selected_row > 9:
  138. self.selected_row = 9
  139. self.selected_col += y
  140. if self.selected_col < 0:
  141. self.selected_col = 0
  142. elif self.selected_col > 2:
  143. if self.selected_col < 2:
  144. self.selected_others = 0
  145. elif self.selected_col < 8:
  146. self.selected_others = 1
  147. else:
  148. self.selected_others = 2
  149. self.set_selected_other()
  150. if self.selected_others < 0:
  151. key = self.keys[self.current_keyboard]
  152. [self.selected_col][self.selected_row]
  153. self.keyboards[self.current_keyboard].set_selected(key)
  154. else:
  155. self.keyboards[0].set_selected(None)
  156. self.keyboards[0].set_selected(None)
  157. else:
  158. if y < 0:
  159. self.selected_others = -1
  160. self.set_selected_other()
  161. self.selected_col = 2
  162. self.selected_row = 0
  163. key = self.keys[self.current_keyboard]
  164. [self.selected_col][self.selected_row]
  165. self.keyboards[self.current_keyboard].set_selected(key)
  166. else:
  167. self.selected_others += x
  168. if self.selected_others < 0:
  169. self.selected_others = 0
  170. elif self.selected_others > 3:
  171. self.selected_others = 3
  172. self.set_selected_other()
  173. def set_selected_other(self):
  174. key = None
  175. if self.selected_others == 0:
  176. key = "symbols"
  177. elif self.selected_others == 1:
  178. key = "remove"
  179. elif self.selected_others == 2:
  180. key = "space"
  181. elif self.selected_others == 3:
  182. key = "ok"
  183. self.other_objects.set_selected(key)