keyboard.py 11 KB

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