page.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. from pygame.locals import *
  4. from sys import exit
  5. import os
  6. import sys
  7. import math
  8. from libs import easing
  9. #import base64
  10. #from beeprint import pp
  11. ### local import
  12. from constants import ALIGN,icon_width,icon_height,Width,Height,ICON_TYPES
  13. from util_funcs import midRect
  14. from keys_def import CurKeys
  15. from icon_pool import MyIconPool
  16. from lang_manager import MyLangManager
  17. class PageStack:
  18. def __init__(self):
  19. self.stack = list()
  20. def Push(self,data):
  21. if data not in self.stack:
  22. self.stack.append(data)
  23. return True
  24. return False
  25. def Pop(self):
  26. if len(self.stack)<=0:
  27. return None,False
  28. return self.stack.pop(),True
  29. def Length(self):
  30. return len(self.stack)
  31. class PageSelector:
  32. _PosX = 0
  33. _PosY = 0
  34. _Width = 0
  35. _Height = 0
  36. _Parent = None
  37. _Alpha = 0
  38. _OnShow = True
  39. _IconSurf = None
  40. def __init__(self):
  41. pass
  42. def Init(self,x,y,w,h,alpha):
  43. self._PosX = x
  44. self._PosY = y
  45. self._Width = w
  46. self._Height = h
  47. self._Alpha = alpha
  48. def Adjust(self,x,y,w,h,alpha):
  49. self._PosX = x
  50. self._PosY = y
  51. self._Width = w
  52. self._Height = h
  53. self._Alpha = alpha
  54. def Draw(self):
  55. canvas = self._Parent._CanvasHWND
  56. idx = self._Parent._PsIndex
  57. iconidx = self._Parent._IconIndex
  58. if idx < len(self._Parent._Icons):
  59. x = self._Parent._Icons[idx]._PosX+self._Parent._PosX
  60. y = self._Parent._Icons[iconidx]._PosY ## only use current icon's PosY
  61. rect = midRect(x,y,self._Width,self._Height,self._Parent._Width,self._Parent._Height)
  62. if rect.width <=0 or rect.height <= 0 :
  63. return
  64. #color = (244,197,66,50)
  65. #pygame.draw.rect(canvas,color,rect,1)
  66. if self._IconSurf != None:
  67. self._Parent._CanvasHWND.blit(self._IconSurf,rect)
  68. class Page(object):
  69. _PosX=0
  70. _PosY=0
  71. _Width=0
  72. _Height=0
  73. _Icons = []
  74. _Ps = None
  75. _PsIndex = 0
  76. _IconNumbers = 0
  77. _IconIndex = 0 ## shows which icon current selected, the Selector can not move here
  78. _PrevIconIndex = 0 ## for remember the Highlighted Icon ,restore it's PosY to average
  79. _Index = 0
  80. _Align = ALIGN["SLeft"]
  81. _CanvasHWND = None #
  82. _HWND = None #
  83. _OnShow = False
  84. _Name = ""
  85. _Screen = None ## Should be the Screen Class
  86. _PageIconMargin = 20
  87. _FootMsg = ["Nav","","","","Enter"] ## Default Page Foot info
  88. _SelectedIconTopOffset=20
  89. _EasingDur = 30
  90. def __init__(self):
  91. self._Icons = []
  92. def AdjustHLeftAlign(self): ## adjust coordinator and append the PageSelector
  93. self._PosX = self._Index*self._Screen._Width
  94. self._Width = self._Screen._Width
  95. self._Height = self._Screen._Height
  96. cols = int(Width /icon_width)
  97. rows = int( (self._IconNumbers * icon_width)/Width + 1)
  98. if rows < 1:
  99. rows = 1
  100. cnt = 0
  101. for i in range(0,rows):
  102. for j in range(0,cols):
  103. start_x = icon_width/2 + j*icon_width
  104. start_y = icon_height/2 + i*icon_height
  105. icon = self._Icons[cnt]
  106. icon.Adjust(start_x,start_y,icon_width-4,icon_height-4,0)
  107. icon._Index = cnt
  108. icon._Parent = self
  109. if cnt >= (self._IconNumbers -1):
  110. break
  111. cnt+=1
  112. ps = PageSelector()
  113. ps._IconSurf = MyIconPool._Icons["blueselector"]
  114. ps._Parent = self
  115. ps.Init(icon_width/2, TitleBar._BarHeight+icon_height/2,92,92,128)
  116. self._Ps = ps
  117. self._PsIndex = 0
  118. self._OnShow = False
  119. def AdjustSLeftAlign(self): ## adjust coordinator and append the PageSelector
  120. self._PosX = self._Index*self._Screen._Width
  121. self._Width = self._Screen._Width
  122. self._Height = self._Screen._Height
  123. start_x = (self._PageIconMargin + icon_width+self._PageIconMargin) /2
  124. start_y = self._Height/2
  125. for i in range(0,self._IconNumbers):
  126. it = self._Icons[i]
  127. it._Parent = self
  128. it._Index = i
  129. it.Adjust(start_x+i*self._PageIconMargin+i*icon_width,start_y,icon_width-6,icon_height-6,0)
  130. it._ImgSurf = pygame.transform.smoothscale(it._ImgSurf,(it._Width,it._Height))
  131. ps = PageSelector()
  132. ps._IconSurf = MyIconPool._Icons["blueselector"]
  133. ps._Parent = self
  134. ps.Init(start_x,start_y,92,92,128)
  135. self._Ps = ps
  136. self._PsIndex = 0
  137. self._OnShow = False
  138. if self._IconNumbers > 1:
  139. self._PsIndex = 1
  140. self._IconIndex = self._PsIndex
  141. self._PrevIconIndex = self._IconIndex
  142. self._Icons[self._IconIndex]._PosY -= self._SelectedIconTopOffset
  143. def AdjustSAutoLeftAlign(self): ## adjust coordinator and append the PageSelector
  144. self._PosX = self._Index*self._Screen._Width
  145. self._Width = self._Screen._Width
  146. self._Height = self._Screen._Height
  147. start_x = (self._PageIconMargin + icon_width+self._PageIconMargin) /2
  148. start_y = self._Height/2
  149. if self._IconNumbers == 1:
  150. start_x = self._Width / 2
  151. start_y = self._Height/2
  152. it = self._Icons[0]
  153. it._Parent = self
  154. it._Index = 0
  155. it.Adjust(start_x,start_y,icon_width,icon_height,0)
  156. #it._ImgSurf = pygame.transform.smoothscale(it._ImgSurf,(it._Width,it._Height))
  157. elif self._IconNumbers == 2:
  158. start_x = (self._Width - self._PageIconMargin - self._IconNumbers*icon_width) / 2 + icon_width/2
  159. start_y = self._Height /2
  160. for i in range(0,self._IconNumbers):
  161. it = self._Icons[i]
  162. it._Parent = self
  163. it._Index = i
  164. it.Adjust(start_x+i*self._PageIconMargin + i*icon_width,start_y, icon_width, icon_height,0)
  165. #it._ImgSurf = pygame.transform.smoothscale(it._ImgSurf,(it._Width,it._Height))
  166. elif self._IconNumbers > 2:
  167. for i in range(0,self._IconNumbers):
  168. it = self._Icons[i]
  169. it._Parent = self
  170. it._Index = i
  171. it.Adjust(start_x+i*self._PageIconMargin + i*icon_width,start_y,icon_width,icon_height,0)
  172. #it._ImgSurf = pygame.transform.smoothscale(it._ImgSurf,(it._Width,it._Height))
  173. ps = PageSelector()
  174. ps._IconSurf = MyIconPool._Icons["blueselector"]
  175. ps._Parent = self
  176. ps.Init(start_x,start_y,92,92,128)
  177. self._Ps = ps
  178. self._PsIndex = 0
  179. self._OnShow = False
  180. if self._IconNumbers > 1:
  181. self._PsIndex = 1
  182. self._IconIndex = self._PsIndex
  183. self._PrevIconIndex = self._IconIndex
  184. self._Icons[self._IconIndex]._PosY -= self._SelectedIconTopOffset
  185. def InitLeftAlign(self):
  186. self._PosX = self._Index*Width
  187. self._Width = self._Screen._Width
  188. self._Height = self._Screen._Height
  189. cols = int(self._Width /icon_width)
  190. rows = int((self._IconNumbers * icon_width)/self._Width + 1)
  191. if rows < 1:
  192. rows = 1
  193. cnt = 0
  194. for i in range(0,rows):
  195. for j in range(0,cols):
  196. start_x = icon_width/2 + j*icon_width
  197. start_y = TitleBar._BarHeight + icon_height/2 + i*icon_height
  198. icon = IconItem()
  199. icon.Init(start_x,start_y,icon_width-4,icon_height-4,0)
  200. icon._Index = cnt
  201. icon._Parent = self
  202. self._Icons.append(icon)
  203. if cnt >= (self._IconNumbers -1):
  204. break
  205. cnt+=1
  206. ps = PageSelector()
  207. ps._IconSurf = MyIconPool._Icons["blueselector"]
  208. ps._Parent = self
  209. ps.Init(icon_width/2,icon_height/2,92,92,128)
  210. self._Ps = ps
  211. self._PsIndex = 0
  212. self._OnShow = False
  213. def Adjust(self): ## default init way,
  214. self._PosX = self._Index*self._Screen._Width
  215. self._Width = self._Screen._Width ## equal to screen width
  216. self._Height = self._Screen._Height
  217. if self._Align == ALIGN["HLeft"]:
  218. start_x = (self._Width - self._IconNumbers*icon_width)/2 + icon_width/2
  219. start_y = self._Height/2
  220. for i in range(0,self._IconNumbers):
  221. it = self._Icons[i]
  222. it._Parent = self
  223. it._Index = i
  224. it.Adjust(start_x+i*icon_width,start_y,icon_width,icon_height,0)
  225. ps = PageSelector()
  226. ps._IconSurf = MyIconPool._Icons["blueselector"]
  227. ps._Parent = self
  228. ps.Init(start_x,start_y,92,92,128)
  229. self._Ps = ps
  230. self._PsIndex = 0
  231. self._OnShow = False
  232. elif self._Align == ALIGN["SLeft"]:
  233. start_x = (self._PageIconMargin + icon_width+self._PageIconMargin) /2
  234. start_y = self._Height/2
  235. for i in range(0,self._IconNumbers):
  236. it = self._Icons[i]
  237. it._Parent = self
  238. it._Index = i
  239. it.Adjust(start_x+i*self._PageIconMargin+i*icon_width,start_y,icon_width,icon_height,0)
  240. ps = PageSelector()
  241. ps._IconSurf = MyIconPool._Icons["blueselector"]
  242. ps._Parent = self
  243. ps.Init(start_x,start_y-self._SelectedIconTopOffset,92,92,128)
  244. self._Ps = ps
  245. self._PsIndex = 0
  246. self._OnShow = False
  247. if self._IconNumbers > 1:
  248. self._PsIndex = 1
  249. self._IconIndex = self._PsIndex
  250. self._PrevIconIndex = self._IconIndex
  251. self._Icons[self._IconIndex]._PosY -= self._SelectedIconTopOffset
  252. def Init(self): ## default init way,
  253. if self._Screen != None:
  254. if self._Screen._CanvasHWND != None and self._CanvasHWND == None:
  255. self._CanvasHWND = self._Screen._CanvasHWND
  256. self._PosX = self._Index*self._Screen._Width
  257. self._Width = self._Screen._Width ## equal to screen width
  258. self._Height = self._Screen._Height
  259. start_x = (self._Width - self._IconNumbers*icon_width)/2 + icon_width/2
  260. start_y = self._Height/2
  261. for i in range(0,self._IconNumbers):
  262. it = IconItem()
  263. it._Parent = self
  264. it._Index = i
  265. it.Init(start_x+i*icon_width,start_y,icon_width,icon_height,0)
  266. self._Icons.append(it)
  267. if self._IconNumbers > 0:
  268. ps = PageSelector()
  269. ps._IconSurf = MyIconPool._Icons["blueselector"]
  270. ps._Parent = self
  271. ps.Init(start_x,start_y,icon_width+4,icon_height+4,128)
  272. self._Ps = ps
  273. self._PsIndex = 0
  274. self._OnShow = False
  275. def IconStepMoveData(self,icon_eh,cuts):## no Sine,No curve,plain movement steps data
  276. all_pieces = []
  277. piece = icon_eh / cuts
  278. c = 0.0
  279. prev = 0.0
  280. for i in range(0,cuts):
  281. c+=piece
  282. dx = c-prev
  283. if dx < 0.5:
  284. dx = 1.0
  285. all_pieces.append( math.ceil(dx) )
  286. if c >= icon_eh:
  287. break
  288. c = 0
  289. bidx = 0
  290. for i in all_pieces:
  291. c+=i
  292. bidx+=1
  293. if c >= icon_eh:
  294. break
  295. all_pieces = all_pieces[0:bidx]
  296. if len(all_pieces) < cuts:
  297. dff = cuts - len(all_pieces)
  298. diffa = []
  299. for i in range(0,dff):
  300. diffa.append(0)
  301. all_pieces.extend( diffa)
  302. return all_pieces
  303. def EasingData(self,start,distance):##generate easing steps data
  304. current_time = 0.0
  305. start_posx = 0.0
  306. current_posx = start_posx
  307. final_posx = float(distance)
  308. posx_init = start
  309. dur = self._EasingDur
  310. last_posx = 0.0
  311. all_last_posx = []
  312. for i in range(0,distance*dur):
  313. current_posx = easing.SineIn(current_time,start_posx,final_posx-start_posx,float(dur))
  314. if current_posx >= final_posx:
  315. current_posx = final_posx
  316. dx = current_posx - last_posx
  317. all_last_posx.append(int(dx))
  318. current_time+=1
  319. last_posx = current_posx
  320. if current_posx >= final_posx:
  321. break
  322. c = 0
  323. for i in all_last_posx:
  324. c+=i
  325. if c < final_posx -start_posx:
  326. all_last_posx.append(final_posx - c)
  327. return all_last_posx
  328. def IconSmoothUp(self,icon_ew):
  329. data = self.EasingData(self._PosX,icon_ew)
  330. data2 = self.IconStepMoveData(self._SelectedIconTopOffset,len(data))
  331. for i,v in enumerate(data):
  332. self.ClearCanvas()
  333. self._Icons[self._IconIndex]._PosY -= data2[i]
  334. if self._Icons[self._PrevIconIndex]._PosY < self._Height/2:
  335. self._Icons[self._PrevIconIndex]._PosY+=data2[i]
  336. self.DrawIcons()
  337. self._Screen.SwapAndShow()
  338. def IconsEasingLeft(self,icon_ew):
  339. data = self.EasingData(self._PosX,icon_ew)
  340. data2 = self.IconStepMoveData(self._SelectedIconTopOffset,len(data))
  341. for i,v in enumerate(data):
  342. self.ClearCanvas()
  343. self._PosX -= v
  344. self._Icons[self._IconIndex]._PosY -= data2[i]
  345. if self._Icons[self._PrevIconIndex]._PosY < self._Height/2:
  346. self._Icons[self._PrevIconIndex]._PosY+=data2[i]
  347. self.DrawIcons()
  348. self._Screen.SwapAndShow()
  349. def IconsEasingRight(self,icon_ew):
  350. data = self.EasingData(self._PosX,icon_ew)
  351. data2 = self.IconStepMoveData(self._SelectedIconTopOffset,len(data))
  352. for i,v in enumerate(data):
  353. self.ClearCanvas()
  354. self._PosX += v
  355. self._Icons[self._IconIndex]._PosY-=data2[i]
  356. if self._Icons[self._PrevIconIndex]._PosY < self._Height/2:
  357. self._Icons[self._PrevIconIndex]._PosY+=data2[i]
  358. self.DrawIcons()
  359. self._Screen.SwapAndShow()
  360. def EasingLeft(self,ew): #ew int
  361. data = self.EasingData(self._PosX,ew)
  362. for i in data:
  363. self._PosX -=i
  364. self.Draw()
  365. self._Screen.SwapAndShow()
  366. def EasingRight(self,ew):
  367. data = self.EasingData(self._PosX,ew)
  368. for i in data:
  369. self._PosX += i
  370. self.Draw()
  371. self._Screen.SwapAndShow()
  372. def MoveLeft(self,ew):
  373. self._PosX -= ew
  374. def MoveRight(self,ew):
  375. self._PosX += ew
  376. def ResetPageSelector(self):
  377. self._PsIndex = 0
  378. self._IconIndex = 0
  379. self._Ps._OnShow = True
  380. def DrawPageSelector(self):
  381. if self._Ps._OnShow == True:
  382. self._Ps.Draw()
  383. def MoveIconIndexPrev(self):
  384. self._IconIndex-=1
  385. if self._IconIndex < 0:
  386. self._IconIndex = 0
  387. self._PrevIconIndex = self._IconIndex
  388. return False
  389. self._PrevIconIndex = self._IconIndex+1
  390. return True
  391. def MoveIconIndexNext(self):
  392. #True for Moved,False is boundary
  393. self._IconIndex+=1
  394. if self._IconIndex > (self._IconNumbers - 1):
  395. self._IconIndex = self._IconNumbers -1
  396. self._PrevIconIndex = self._IconIndex
  397. return False
  398. self._PrevIconIndex = self._IconIndex-1
  399. return True
  400. def IconClick(self):
  401. if self._IconIndex > (len(self._Icons) -1):
  402. return
  403. cur_icon = self._Icons[self._IconIndex]
  404. if self._Ps._OnShow == False:
  405. return
  406. if cur_icon._MyType == ICON_TYPES["EXE"]:
  407. print("IconClick: %s %d"%(cur_icon._CmdPath,cur_icon._Index))
  408. self._Screen.RunEXE(cur_icon._CmdPath)
  409. elif cur_icon._MyType == ICON_TYPES["DIR"]:
  410. child_page = self._Icons[self._IconIndex]._LinkPage
  411. if child_page != None:
  412. child_page.Draw()
  413. self._Screen._MyPageStack.Push(self._Screen._CurrentPage)
  414. self._Screen._CurrentPage = child_page
  415. elif cur_icon._MyType == ICON_TYPES["FUNC"]:
  416. print("IconClick API: %d"%(cur_icon._Index))
  417. #print("%s"% cur_icon._CmdPath)
  418. api_cb = getattr(cur_icon._CmdPath,"API",None)
  419. if api_cb != None:
  420. if callable(api_cb):
  421. cur_icon._CmdPath.API(self._Screen)
  422. elif cur_icon._MyType == ICON_TYPES["Emulator"]:
  423. cur_icon._CmdPath.API(self._Screen)
  424. def ReturnToUpLevelPage(self):
  425. pop_page,ok = self._Screen._MyPageStack.Pop()
  426. if ok == True:
  427. #self._Screen._CurrentPage.ResetPageSelector()
  428. pop_page.Draw()
  429. self._Screen._CurrentPage = pop_page
  430. on_return_back_cb = getattr(self._Screen._CurrentPage,"OnReturnBackCb",None)
  431. if on_return_back_cb != None:
  432. if callable(on_return_back_cb):
  433. self._Screen._CurrentPage.OnReturnBackCb()
  434. else:
  435. if self._Screen._MyPageStack.Length() == 0:
  436. if len(self._Screen._Pages) > 0:
  437. self._Screen._CurrentPage = self._Screen._Pages[self._Screen._PageIndex]
  438. self._Screen._CurrentPage.Draw()
  439. print("OnTopLevel ",self._Screen._PageIndex)
  440. def ClearCanvas(self):
  441. self._CanvasHWND.fill(self._Screen._SkinManager.GiveColor("White"))
  442. def ClearIcons(self):
  443. for i in range(0,self._IconNumbers):
  444. self._Icons[i].Clear()
  445. def DrawIcons(self):
  446. for i in range(0,self._IconNumbers):
  447. self._Icons[i].Draw()
  448. def KeyDown(self,event):##default keydown,every inherited page class should have it's own KeyDown
  449. if event.key == CurKeys["A"]:
  450. if self._FootMsg[3] == "Back":
  451. self.ReturnToUpLevelPage()
  452. self._Screen.Draw()
  453. self._Screen.SwapAndShow()
  454. return
  455. if event.key == CurKeys["Menu"]:
  456. self.ReturnToUpLevelPage()
  457. self._Screen.Draw()
  458. self._Screen.SwapAndShow()
  459. if event.key == CurKeys["Right"]:
  460. if self.MoveIconIndexNext() == True:
  461. if self._IconIndex == (self._IconNumbers -1) or self._PrevIconIndex == 0:
  462. self.IconSmoothUp(icon_width+ self._PageIconMargin) # only move up selected icon,no horizontal translation
  463. else:
  464. self.IconsEasingLeft(icon_width + self._PageIconMargin)
  465. self._PsIndex = self._IconIndex
  466. self._Screen.Draw()
  467. self._Screen.SwapAndShow()
  468. if event.key == CurKeys["Left"]:
  469. if self.MoveIconIndexPrev() == True:
  470. if self._IconIndex == 0 or self._PrevIconIndex == (self._IconNumbers -1):
  471. self.IconSmoothUp(icon_width+ self._PageIconMargin)
  472. else:
  473. self.IconsEasingRight(icon_width + self._PageIconMargin)
  474. self._PsIndex = self._IconIndex
  475. self._Screen.Draw()
  476. self._Screen.SwapAndShow()
  477. if event.key == CurKeys["Enter"]:
  478. self.IconClick()
  479. self._Screen.Draw()
  480. self._Screen.SwapAndShow()
  481. def Draw(self):
  482. self.ClearCanvas()
  483. self.DrawIcons()
  484. self.DrawPageSelector()