keyboard_screen.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.selected_others = 3
  88. self.set_selected_other()
  89. def update(self, screen):
  90. screen.fill((0, 0, 0))
  91. self.keyboards[self.current_keyboard].render(screen)
  92. self.other_objects.render(screen)
  93. def touch_event(self, touch_event):
  94. if touch_event.type == InputManager.click:
  95. keys = self.keyboards[self.current_keyboard]\
  96. .get_touch_objects_in_pos(touch_event.current_pos)
  97. for key in keys:
  98. self.other_objects.get_object("text").add_text(key, False)
  99. keys = self.other_objects.get_touch_objects_in_pos(
  100. touch_event.current_pos)
  101. for key in keys:
  102. if key == 'symbols':
  103. self.change_keyboard()
  104. elif key == "remove":
  105. self.other_objects.get_object("text").remove_text(1, False)
  106. elif key == "space":
  107. self.other_objects.get_object("text").add_text(" ", False)
  108. elif key == "ok":
  109. text = self.other_objects.get_object("text").text
  110. self.listener.text_input(text)
  111. self.manager.close_keyboard()
  112. elif touch_event.type == InputManager.key:
  113. if not isinstance(touch_event.unicode, int):
  114. if touch_event.unicode == u'\x08':
  115. self.other_objects.get_object("text").remove_text(1, False)
  116. else:
  117. self.other_objects.get_object("text").add_text(
  118. touch_event.unicode, False)
  119. elif touch_event.direction is not None:
  120. x = 0
  121. y = 0
  122. if touch_event.direction == InputManager.left:
  123. x = -1
  124. elif touch_event.direction == InputManager.right:
  125. x = 1
  126. elif touch_event.direction == InputManager.up:
  127. y = -1
  128. elif touch_event.direction == InputManager.down:
  129. y = 1
  130. if touch_event.direction == InputManager.enter:
  131. self.selected_click()
  132. else:
  133. self.change_selected(x, y)
  134. def change_keyboard(self):
  135. if self.current_keyboard == 0:
  136. self.current_keyboard = 1
  137. else:
  138. self.current_keyboard = 0
  139. if self.selected_others < 0:
  140. self.change_selected(0, 0)
  141. def change_selected(self, x, y):
  142. if self.selected_others < 0:
  143. # We are on the keyboard
  144. # Update col
  145. self.selected_col += x
  146. if self.selected_col < 0:
  147. self.selected_col = 0
  148. elif self.selected_col > 9:
  149. self.selected_col = 9
  150. # Update row
  151. self.selected_row += y
  152. if self.selected_row < 0:
  153. self.selected_row = 0
  154. elif self.selected_row > 2:
  155. # Change to the bottom part
  156. if self.selected_col < 2:
  157. self.selected_others = 0
  158. elif self.selected_col < 4:
  159. self.selected_others = 1
  160. elif self.selected_col < 8:
  161. self.selected_others = 2
  162. else:
  163. self.selected_others = 3
  164. # Update selected
  165. if self.selected_others < 0:
  166. key = self.keys[self.current_keyboard][
  167. self.selected_row][self.selected_col]
  168. self.keyboards[self.current_keyboard].set_selected(key)
  169. else:
  170. self.keyboards[0].set_selected(None)
  171. self.keyboards[1].set_selected(None)
  172. self.set_selected_other()
  173. else:
  174. # We are on the bottom part
  175. if y < 0:
  176. # we are returning to the keyboard
  177. # Select col
  178. if self.selected_others == 0:
  179. self.selected_col = 0
  180. elif self.selected_others == 1:
  181. self.selected_col = 2
  182. elif self.selected_others == 2:
  183. self.selected_col = 4
  184. else:
  185. self.selected_col = 8
  186. self.selected_others = -1
  187. self.set_selected_other()
  188. self.selected_row = 2
  189. key = self.keys[self.current_keyboard][
  190. self.selected_row][self.selected_col]
  191. self.keyboards[self.current_keyboard].set_selected(key)
  192. elif x != 0:
  193. # We change in horizontal
  194. self.selected_others += x
  195. if self.selected_others < 0:
  196. self.selected_others = 0
  197. elif self.selected_others > 3:
  198. self.selected_others = 3
  199. self.set_selected_other()
  200. def selected_click(self):
  201. if self.selected_others >= 0:
  202. if self.selected_others == 0:
  203. self.change_keyboard()
  204. elif self.selected_others == 1:
  205. self.other_objects.get_object("text").remove_text(1, False)
  206. elif self.selected_others == 2:
  207. self.other_objects.get_object("text").add_text(" ", False)
  208. elif self.selected_others == 3:
  209. text = self.other_objects.get_object("text").text
  210. self.listener.text_input(text)
  211. self.manager.close_keyboard()
  212. else:
  213. key = self.keys[self.current_keyboard][
  214. self.selected_row][self.selected_col]
  215. self.other_objects.get_object("text").add_text(
  216. key, False)
  217. def set_selected_other(self):
  218. key = None
  219. if self.selected_others == 0:
  220. key = "symbols"
  221. elif self.selected_others == 1:
  222. key = "remove"
  223. elif self.selected_others == 2:
  224. key = "space"
  225. elif self.selected_others == 3:
  226. key = "ok"
  227. self.other_objects.set_selected(key)