keyboard_screen.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. scroll_no_fit=False)
  48. self.keyboards[self.current_keyboard].\
  49. set_touch_object(key, button)
  50. pos += self.base_width
  51. line += self.base_height
  52. self.current_keyboard = 0
  53. # Symbol button
  54. button = TouchAndTextItem(self.font, "123",
  55. (0, self.base_height*4),
  56. (self.base_width*2, self.base_height),
  57. center=True, background=(150, 150, 150),
  58. scroll_no_fit=False)
  59. self.other_objects.set_touch_object("symbols", button)
  60. # remove button
  61. button = TouchAndTextItem(self.font, "<-",
  62. (self.base_width*2, self.base_height*4),
  63. (self.base_width*2, self.base_height),
  64. center=True, background=(150, 150, 150),
  65. scroll_no_fit=False)
  66. self.other_objects.set_touch_object("remove", button)
  67. # Space button
  68. button = TouchAndTextItem(self.font, " ",
  69. (self.base_width*4, self.base_height*4),
  70. (self.base_width*4, self.base_height),
  71. center=True, background=(150, 150, 150),
  72. scroll_no_fit=False)
  73. self.other_objects.set_touch_object("space", button)
  74. # OK button
  75. button = TouchAndTextItem(self.font, "->",
  76. (self.base_width*8, self.base_height*4),
  77. (self.base_width*2, self.base_height),
  78. center=True, background=(150, 150, 150),
  79. scroll_no_fit=False)
  80. self.other_objects.set_touch_object("ok", button)
  81. # EditText button
  82. button = TouchAndTextItem(self.font, "",
  83. (0, 0),
  84. (self.size[0], self.base_height),
  85. center=False, scroll_no_fit=False)
  86. self.other_objects.set_object("text", button)
  87. self.change_selected(0, 0)
  88. def update(self, screen):
  89. screen.fill((0, 0, 0))
  90. self.keyboards[self.current_keyboard].render(screen)
  91. self.other_objects.render(screen)
  92. def touch_event(self, touch_event):
  93. if touch_event.type == InputManager.click:
  94. keys = self.keyboards[self.current_keyboard]\
  95. .get_touch_objects_in_pos(touch_event.current_pos)
  96. for key in keys:
  97. self.other_objects.get_object("text").add_text(key, False)
  98. keys = self.other_objects.get_touch_objects_in_pos(
  99. touch_event.current_pos)
  100. for key in keys:
  101. if key == 'symbols':
  102. self.change_keyboard()
  103. elif key == "remove":
  104. self.other_objects.get_object("text").remove_text(1, False)
  105. elif key == "space":
  106. self.other_objects.get_object("text").add_text(" ", False)
  107. elif key == "ok":
  108. text = self.other_objects.get_object("text").text
  109. self.listener.text_input(text)
  110. self.manager.close_keyboard()
  111. elif touch_event.type == InputManager.key:
  112. if not isinstance(touch_event.unicode, int):
  113. if touch_event.unicode == u'\x08':
  114. self.other_objects.get_object("text").remove_text(1, False)
  115. else:
  116. self.other_objects.get_object("text").add_text(
  117. touch_event.unicode, False)
  118. elif touch_event.direction is not None:
  119. x = 0
  120. y = 0
  121. if touch_event.direction == InputManager.left:
  122. x = -1
  123. elif touch_event.direction == InputManager.right:
  124. x = 1
  125. elif touch_event.direction == InputManager.up:
  126. y = -1
  127. elif touch_event.direction == InputManager.down:
  128. y = 1
  129. if touch_event.direction == InputManager.enter:
  130. self.selected_click()
  131. else:
  132. self.change_selected(x, y)
  133. def change_keyboard(self):
  134. if self.current_keyboard == 0:
  135. self.current_keyboard = 1
  136. else:
  137. self.current_keyboard = 0
  138. if self.selected_others < 0:
  139. self.change_selected(0, 0)
  140. def change_selected(self, x, y):
  141. if self.selected_others < 0:
  142. # We are on the keyboard
  143. # Update col
  144. self.selected_col += x
  145. if self.selected_col < 0:
  146. self.selected_col = 0
  147. elif self.selected_col > 9:
  148. self.selected_col = 9
  149. # Update row
  150. self.selected_row += y
  151. if self.selected_row < 0:
  152. self.selected_row = 0
  153. elif self.selected_row > 2:
  154. # Change to the bottom part
  155. if self.selected_col < 2:
  156. self.selected_others = 0
  157. elif self.selected_col < 4:
  158. self.selected_others = 1
  159. elif self.selected_col < 8:
  160. self.selected_others = 2
  161. else:
  162. self.selected_others = 3
  163. # Update selected
  164. if self.selected_others < 0:
  165. key = self.keys[self.current_keyboard][
  166. self.selected_row][self.selected_col]
  167. self.keyboards[self.current_keyboard].set_selected(key)
  168. else:
  169. self.keyboards[0].set_selected(None)
  170. self.keyboards[1].set_selected(None)
  171. self.set_selected_other()
  172. else:
  173. # We are on the bottom part
  174. if y < 0:
  175. # we are returning to the keyboard
  176. # Select col
  177. if self.selected_others == 0:
  178. self.selected_col = 0
  179. elif self.selected_others == 1:
  180. self.selected_col = 2
  181. elif self.selected_others == 2:
  182. self.selected_col = 4
  183. else:
  184. self.selected_col = 8
  185. self.selected_others = -1
  186. self.set_selected_other()
  187. self.selected_row = 2
  188. key = self.keys[self.current_keyboard][
  189. self.selected_row][self.selected_col]
  190. self.keyboards[self.current_keyboard].set_selected(key)
  191. elif x != 0:
  192. # We change in horizontal
  193. self.selected_others += x
  194. if self.selected_others < 0:
  195. self.selected_others = 0
  196. elif self.selected_others > 3:
  197. self.selected_others = 3
  198. self.set_selected_other()
  199. def selected_click(self):
  200. if self.selected_others >= 0:
  201. if self.selected_others == 0:
  202. self.change_keyboard()
  203. elif self.selected_others == 1:
  204. self.other_objects.get_object("text").remove_text(1, False)
  205. elif self.selected_others == 2:
  206. self.other_objects.get_object("text").add_text(" ", False)
  207. elif self.selected_others == 3:
  208. text = self.other_objects.get_object("text").text
  209. self.listener.text_input(text)
  210. self.manager.close_keyboard()
  211. else:
  212. key = self.keys[self.current_keyboard][
  213. self.selected_row][self.selected_col]
  214. self.other_objects.get_object("text").add_text(
  215. key, False)
  216. def set_selected_other(self):
  217. key = None
  218. if self.selected_others == 0:
  219. key = "symbols"
  220. elif self.selected_others == 1:
  221. key = "remove"
  222. elif self.selected_others == 2:
  223. key = "space"
  224. elif self.selected_others == 3:
  225. key = "ok"
  226. self.other_objects.set_selected(key)