keyboard.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. from libs import easing
  4. ## local UI import
  5. from UI.constants import Width,Height,ICON_TYPES
  6. from UI.page import Page,PageSelector
  7. from UI.label import Label
  8. from UI.fonts import fonts
  9. from UI.util_funcs import midRect
  10. from UI.keys_def import CurKeys
  11. from UI.icon_item import IconItem
  12. from textarea import Textarea
  13. from text_item import TextItem
  14. from icons import preload
  15. import myvars
  16. class KeyboardIcon(IconItem):
  17. _PosX = 0
  18. _PosY = 0
  19. _Width = 0
  20. _Height = 0
  21. _Color = pygame.Color(83,83,83)
  22. _MyType = ICON_TYPES["NAV"]
  23. _Parent = None
  24. _Str = ""
  25. def Draw(self):
  26. self._Parent._CanvasHWND.blit(self._ImgSurf, \
  27. midRect(self._PosX,self._PosY,self._Width,self._Height,Width,Height))
  28. class KeyboardSelector(PageSelector):
  29. def Draw(self):
  30. sec_idx = self._Parent._SectionIndex
  31. row_idx = self._Parent._RowIndex
  32. idx = self._Parent._PsIndex
  33. x = self._Parent._SecsKeys[sec_idx][row_idx][idx]._PosX
  34. y = self._Parent._SecsKeys[sec_idx][row_idx][idx]._PosY
  35. w = self._Parent._SecsKeys[sec_idx][row_idx][idx]._Width
  36. h = self._Parent._SecsKeys[sec_idx][row_idx][idx]._Height
  37. rect = midRect(x,y,w,h,self._Parent._Width,self._Parent._Height)
  38. if rect.width <=0 or rect.height <= 0 :
  39. return
  40. pygame.draw.rect(self._Parent._CanvasHWND,(0,0,0),rect,1)
  41. class Keyboard(Page):
  42. _PosX = 0
  43. _PosY = 0
  44. _Width = 0
  45. _Height = 0
  46. _SectionNumbers = 3
  47. _SectionIndex = 1
  48. _Icons = {}
  49. _Secs = {}
  50. _SecsKeys = {}
  51. _KeyboardLayoutFile = "UI/keyboard_keys.layout"
  52. _Textarea = None
  53. _Selector = None
  54. _LeftOrRight = 1
  55. _FootMsg = ["Nav.","ABC","Done","Backspace","Enter"]
  56. _RowIndex = 0
  57. def __init__(self):
  58. self._Secs = {}
  59. self._SecsKeys = {}
  60. self._Icons = {}
  61. def ReadLayoutFile(self,fname):
  62. LayoutIndex = 0
  63. with open(fname) as f:
  64. content = f.readlines()
  65. content = [ x.strip() for x in content]
  66. content = [ x.split(" ") for x in content]
  67. for i in content:
  68. i = [ x.strip() for x in i]
  69. if len(i) > 2:
  70. if LayoutIndex in self._Secs:
  71. self._Secs[LayoutIndex].append(i)
  72. else:
  73. self._Secs[LayoutIndex] = []
  74. self._Secs[LayoutIndex].append(i)
  75. else:
  76. LayoutIndex+=1
  77. def SetPassword(self,pwd):
  78. pwd_list = list(pwd)
  79. self._Textarea.ResetMyWords()
  80. for i in pwd_list:
  81. self._Textarea.AppendText(i)
  82. #self._Textarea.BlitText()
  83. def Init(self):
  84. self._CanvasHWND = self._Screen._CanvasHWND
  85. self.ReadLayoutFile(self._KeyboardLayoutFile) ## assign to _Secs
  86. self._SectionNumbers = len(self._Secs)
  87. self._PosX = self._Index*self._Screen._Width
  88. self._Width = self._Screen._Width
  89. self._Height = self._Screen._Height
  90. fontobj = fonts["veramono24"]
  91. word_margin = 15
  92. start_x = (self._Width - fontobj.size( "".join(self._Secs[0][0]))[0]-len(self._Secs[0][0])*word_margin)/2+word_margin/2
  93. start_y = 0
  94. cnt = 0
  95. for i in range(0,self._SectionNumbers):
  96. self._SecsKeys[i] = []
  97. for j in range(0,len(self._Secs[i])):
  98. self._SecsKeys[i].append( [] )
  99. start_x = (self._Width - fontobj.size( "".join(self._Secs[i][j]))[0]-len(self._Secs[i][j])*word_margin)/2+word_margin/2
  100. start_x = start_x + i*Width
  101. start_y = 84+j*(word_margin+14)
  102. for idx,val in enumerate(self._Secs[i][j]):
  103. ti = TextItem()
  104. ti._FontObj = fontobj
  105. ti._Parent = self
  106. if val == "_L" or val == "_R":
  107. it = KeyboardIcon()
  108. it._ImgSurf = preload.ICONS_PRELOAD[val]
  109. it._Parent = self
  110. it._Str = val
  111. it.Init(start_x+it._ImgSurf.get_width()/2 ,start_y,it._ImgSurf.get_width(),it._ImgSurf.get_height(),0)
  112. #self._Icons[val] = it
  113. self._SecsKeys[i][j].append(it)
  114. self._IconNumbers+=1
  115. start_x = start_x + it._ImgSurf.get_width()+word_margin
  116. else:
  117. if val == "_S":
  118. val = "Space"
  119. ti._FontObj = fonts["veramono15"]
  120. ti._Bold = True
  121. cur_alpha_size = ti._FontObj.size( val)
  122. ti.Init(start_x + cur_alpha_size[0]/2,start_y,cur_alpha_size[0],cur_alpha_size[1],0)
  123. ti._Str = val
  124. start_x = start_x + cur_alpha_size[0]+word_margin # prepare for next alpha
  125. self._SecsKeys[i][j].append(ti)
  126. self._SectionIndex = 0
  127. self._Textarea = Textarea()
  128. self._Textarea._PosX = 4
  129. self._Textarea._PosY = 4
  130. self._Textarea._Width= self._Width - 4*2
  131. self._Textarea._Height = 60
  132. self._Textarea._CanvasHWND = self._CanvasHWND
  133. self._Textarea.Init()
  134. ps = KeyboardSelector()
  135. ps._Parent = self
  136. ps.Init(start_x,start_y,25,25,128)
  137. self._Ps = ps
  138. self._PsIndex = 0
  139. self._Ps._OnShow = True
  140. def SelectUpChar(self):
  141. sec_idx = self._SectionIndex
  142. self._RowIndex-=1
  143. if self._RowIndex <0:
  144. self._RowIndex = len(self._SecsKeys[sec_idx])-1
  145. if self._PsIndex >=len(self._SecsKeys[sec_idx][self._RowIndex]):
  146. self._PsIndex = len(self._SecsKeys[sec_idx][self._RowIndex])-1
  147. self.ClearCanvas()
  148. self.Draw()
  149. self._Screen.SwapAndShow()
  150. def SelectDownChar(self):
  151. sec_idx = self._SectionIndex
  152. self._RowIndex+=1
  153. if self._RowIndex >= len(self._SecsKeys[sec_idx]):
  154. self._RowIndex = 0
  155. if self._PsIndex >=len(self._SecsKeys[sec_idx][self._RowIndex]):
  156. self._PsIndex = len(self._SecsKeys[sec_idx][self._RowIndex])-1
  157. self.ClearCanvas()
  158. self.Draw()
  159. self._Screen.SwapAndShow()
  160. def SelectNextChar(self):
  161. sec_idx = self._SectionIndex
  162. row_idx = self._RowIndex
  163. self._PsIndex+=1
  164. if self._PsIndex >= len(self._SecsKeys[sec_idx][row_idx]):
  165. self._PsIndex = 0
  166. self._RowIndex+=1
  167. if self._RowIndex >= len(self._SecsKeys[sec_idx]):
  168. self._RowIndex = 0
  169. self.ClearCanvas()
  170. self.Draw()
  171. self._Screen.SwapAndShow()
  172. def SelectPrevChar(self):
  173. sec_idx = self._SectionIndex
  174. self._PsIndex-=1
  175. if self._PsIndex < 0:
  176. self._RowIndex-=1
  177. if self._RowIndex <=0:
  178. self._RowIndex = len(self._SecsKeys[sec_idx])-1
  179. self._PsIndex = len(self._SecsKeys[sec_idx][self._RowIndex]) -1
  180. self.ClearCanvas()
  181. self.Draw()
  182. self._Screen.SwapAndShow()
  183. def ClickOnChar(self):
  184. sec_idx = self._SectionIndex
  185. alphabet = self._SecsKeys[sec_idx][self._RowIndex][self._PsIndex]._Str
  186. if alphabet == "Space":
  187. alphabet = " "
  188. if alphabet == "_L" or alphabet == "_R":
  189. if alphabet == "_L":
  190. self._Textarea.SubTextIndex()
  191. elif alphabet == "_R":
  192. self._Textarea.AddTextIndex()
  193. else:
  194. self._Textarea.AppendText(alphabet)
  195. self._Textarea.Draw()
  196. self._Screen.SwapAndShow()
  197. def KeyboardShift(self):
  198. current_time = 0.0
  199. start_posx = 0.0
  200. current_posx = start_posx
  201. final_posx = 320.0
  202. posx_init = 0.0
  203. dur = 30
  204. last_posx = 0.0
  205. all_last_posx = []
  206. for i in range(0,Width*dur):
  207. current_posx = easing.SineIn(current_time,start_posx,final_posx-start_posx,float(dur))
  208. if current_posx >= final_posx:
  209. current_posx = final_posx
  210. dx = current_posx - last_posx
  211. all_last_posx.append(int(dx))
  212. current_time +=1
  213. last_posx = current_posx
  214. if current_posx >= final_posx:
  215. break
  216. c = 0
  217. for i in all_last_posx:
  218. c+=i
  219. if c < final_posx - start_posx:
  220. all_last_posx.append(final_posx - c)
  221. for i in all_last_posx:
  222. for j in range(0,self._SectionNumbers):
  223. for u in self._SecsKeys[j]:
  224. for x in u:
  225. x._PosX += self._LeftOrRight*i
  226. self.ResetPageSelector()
  227. self.ClearCanvas()
  228. self.Draw()
  229. self._Screen.SwapAndShow()
  230. def KeyDown(self,event):# event from pygame.event.get()
  231. if event.key == CurKeys["Up"]:
  232. self.SelectUpChar()
  233. if event.key == CurKeys["Down"]:
  234. self.SelectDownChar()
  235. if event.key == CurKeys["Right"]:
  236. self.SelectNextChar()
  237. if event.key == CurKeys["Left"]:
  238. self.SelectPrevChar()
  239. if event.key == CurKeys["B"] or event.key == CurKeys["Enter"]:
  240. self.ClickOnChar()
  241. if event.key == CurKeys["X"]:
  242. if self._SectionIndex <= 0:
  243. self._LeftOrRight = -1
  244. if self._SectionIndex >= (self._SectionNumbers -1):
  245. self._LeftOrRight = 1
  246. self.KeyboardShift()
  247. self._SectionIndex -= self._LeftOrRight
  248. #print(self._SectionIndex) # on which keyboard section now
  249. self.Draw()
  250. self._Screen.SwapAndShow()
  251. if event.key == CurKeys["Menu"]: # we assume keyboard always be child page
  252. self.ReturnToUpLevelPage()
  253. self._Screen.Draw()
  254. self._Screen.SwapAndShow()
  255. if event.key == CurKeys["Y"]: #done
  256. print("".join(self._Textarea._MyWords))
  257. self.ReturnToUpLevelPage()
  258. self._Screen.SwapAndShow()
  259. ## config and connect now
  260. myvars.ScanPage.ConfigWireless( "".join(self._Textarea._MyWords))
  261. if event.key == CurKeys["A"]:
  262. self._Textarea.RemoveFromLastText()
  263. self._Textarea.Draw()
  264. self._Screen.SwapAndShow()
  265. def Draw(self):
  266. self.ClearCanvas()
  267. for i in range(0,self._SectionNumbers):
  268. for j in self._SecsKeys[i]:
  269. for u in j:
  270. u.Draw()
  271. self._Textarea.Draw()
  272. self._Ps.Draw()