page.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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. import fnmatch
  9. import random
  10. import time
  11. from libs import easing
  12. # import base64
  13. # from beeprint import pp
  14. # local import
  15. from constants import ALIGN, icon_width, icon_height, Width, Height, ICON_TYPES
  16. from util_funcs import midRect,FileExists
  17. from keys_def import CurKeys, IsKeyStartOrA, IsKeyMenuOrB
  18. from icon_pool import MyIconPool
  19. from lang_manager import MyLangManager
  20. from skin_manager import MySkinManager
  21. from widget import Widget
  22. import config
  23. class PageStack:
  24. def __init__(self):
  25. self.stack = list()
  26. def Push(self, data):
  27. if data not in self.stack:
  28. self.stack.append(data)
  29. return True
  30. return False
  31. def Pop(self):
  32. if len(self.stack) <= 0:
  33. return None, False
  34. return self.stack.pop(), True
  35. def Length(self):
  36. return len(self.stack)
  37. class PageSelector(Widget):
  38. _Parent = None
  39. _Alpha = 0
  40. _OnShow = True
  41. _IconSurf = None
  42. def __init__(self):
  43. pass
  44. def Init(self, x, y, w, h, alpha):
  45. self._PosX = x
  46. self._PosY = y
  47. self._Width = w
  48. self._Height = h
  49. self._Alpha = alpha
  50. def Adjust(self, x, y, w, h, alpha):
  51. self._PosX = x
  52. self._PosY = y
  53. self._Width = w
  54. self._Height = h
  55. self._Alpha = alpha
  56. def Draw(self):
  57. canvas = self._Parent._CanvasHWND
  58. idx = self._Parent._PsIndex
  59. iconidx = self._Parent._IconIndex
  60. if idx < len(self._Parent._Icons):
  61. x = self._Parent._Icons[idx]._PosX+self._Parent._PosX
  62. # only use current icon's PosY
  63. y = self._Parent._Icons[iconidx]._PosY
  64. rect = midRect(x, y, self._Width, self._Height,
  65. self._Parent._Width, self._Parent._Height)
  66. if rect.width <= 0 or rect.height <= 0:
  67. return
  68. # color = (244,197,66,50)
  69. # pygame.draw.rect(canvas,color,rect,1)
  70. if self._IconSurf != None:
  71. self._Parent._CanvasHWND.blit(self._IconSurf, rect)
  72. class Page(Widget):
  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. _Wallpaper = None
  89. _SelectedIconTopOffset = 20
  90. _EasingDur = 30
  91. _Padding = pygame.Rect(0, 0, 0, 0) # x,y,w,h
  92. _Margin = pygame.Rect(0, 0, 0, 0)
  93. _ScrollStep = 1
  94. _Scrolled = 0
  95. _ItemsPerPage = 6
  96. def __init__(self):
  97. self._Icons = []
  98. ## so every theme can have a background.png for displaying as the background of the launcher,except the topbar and footbar
  99. ## https://forum.clockworkpi.com/t/give-your-gs-a-custom-wallpaper/3724
  100. bg_img_path = config.SKIN+"/background.png"
  101. if FileExists(bg_img_path):
  102. self._Wallpaper = pygame.transform.scale(pygame.image.load(bg_img_path).convert(), (320,240))
  103. def AdjustHLeftAlign(self): ## adjust coordinator and append the PageSelector
  104. self._PosX = self._Index*self._Screen._Width
  105. self._Width = self._Screen._Width
  106. self._Height = self._Screen._Height
  107. cols = int(Width /icon_width)
  108. rows = int( (self._IconNumbers * icon_width)/Width + 1)
  109. if rows < 1:
  110. rows = 1
  111. cnt = 0
  112. for i in range(0,rows):
  113. for j in range(0,cols):
  114. start_x = icon_width/2 + j*icon_width
  115. start_y = icon_height/2 + i*icon_height
  116. icon = self._Icons[cnt]
  117. icon.Adjust(start_x,start_y,icon_width-4,icon_height-4,0)
  118. icon._Index = cnt
  119. icon._Parent = self
  120. if cnt >= (self._IconNumbers -1):
  121. break
  122. cnt+=1
  123. ps = PageSelector()
  124. ps._IconSurf = MyIconPool.GiveIconSurface("blueselector")
  125. ps._Parent = self
  126. ps.Init(icon_width/2, TitleBar._BarHeight+icon_height/2,92,92,128)
  127. self._Ps = ps
  128. self._PsIndex = 0
  129. self._OnShow = False
  130. def AdjustSLeftAlign(self): ## adjust coordinator and append the PageSelector
  131. self._PosX = self._Index*self._Screen._Width
  132. self._Width = self._Screen._Width
  133. self._Height = self._Screen._Height
  134. start_x = (self._PageIconMargin + icon_width+self._PageIconMargin) /2
  135. start_y = self._Height/2
  136. for i in range(0,self._IconNumbers):
  137. it = self._Icons[i]
  138. it._Parent = self
  139. it._Index = i
  140. it.Adjust(start_x+i*self._PageIconMargin+i*icon_width,start_y,icon_width-6,icon_height-6,0)
  141. it._ImgSurf = pygame.transform.smoothscale(it._ImgSurf,(it._Width,it._Height))
  142. ps = PageSelector()
  143. ps._IconSurf = MyIconPool.GiveIconSurface("blueselector")
  144. ps._Parent = self
  145. ps.Init(start_x,start_y,92,92,128)
  146. self._Ps = ps
  147. self._PsIndex = 0
  148. self._OnShow = False
  149. if self._IconNumbers > 1:
  150. self._PsIndex = 1
  151. self._IconIndex = self._PsIndex
  152. self._PrevIconIndex = self._IconIndex
  153. self._Icons[self._IconIndex]._PosY -= self._SelectedIconTopOffset
  154. def AdjustSAutoLeftAlign(self): ## adjust coordinator and append the PageSelector
  155. self._PosX = self._Index*self._Screen._Width
  156. self._Width = self._Screen._Width
  157. self._Height = self._Screen._Height
  158. start_x = (self._PageIconMargin + icon_width+self._PageIconMargin) /2
  159. start_y = self._Height/2
  160. if self._IconNumbers == 1:
  161. start_x = self._Width / 2
  162. start_y = self._Height/2
  163. it = self._Icons[0]
  164. it._Parent = self
  165. it._Index = 0
  166. it.Adjust(start_x,start_y,icon_width,icon_height,0)
  167. # it._ImgSurf = pygame.transform.smoothscale(it._ImgSurf,(it._Width,it._Height))
  168. elif self._IconNumbers == 2:
  169. start_x = (self._Width - self._PageIconMargin - self._IconNumbers*icon_width) / 2 + icon_width/2
  170. start_y = self._Height /2
  171. for i in range(0,self._IconNumbers):
  172. it = self._Icons[i]
  173. it._Parent = self
  174. it._Index = i
  175. it.Adjust(start_x+i*self._PageIconMargin + i*icon_width,start_y, icon_width, icon_height,0)
  176. # it._ImgSurf = pygame.transform.smoothscale(it._ImgSurf,(it._Width,it._Height))
  177. elif self._IconNumbers > 2:
  178. for i in range(0,self._IconNumbers):
  179. it = self._Icons[i]
  180. it._Parent = self
  181. it._Index = i
  182. it.Adjust(start_x+i*self._PageIconMargin + i*icon_width,start_y,icon_width,icon_height,0)
  183. # it._ImgSurf = pygame.transform.smoothscale(it._ImgSurf,(it._Width,it._Height))
  184. ps = PageSelector()
  185. ps._IconSurf = MyIconPool.GiveIconSurface("blueselector")
  186. ps._Parent = self
  187. ps.Init(start_x,start_y,92,92,128)
  188. self._Ps = ps
  189. self._PsIndex = 0
  190. self._OnShow = False
  191. if self._IconNumbers > 1:
  192. self._PsIndex = 1
  193. self._IconIndex = self._PsIndex
  194. self._PrevIconIndex = self._IconIndex
  195. self._Icons[self._IconIndex]._PosY -= self._SelectedIconTopOffset
  196. def InitLeftAlign(self):
  197. self._PosX = self._Index*Width
  198. self._Width = self._Screen._Width
  199. self._Height = self._Screen._Height
  200. cols = int(self._Width /icon_width)
  201. rows = int((self._IconNumbers * icon_width)/self._Width + 1)
  202. if rows < 1:
  203. rows = 1
  204. cnt = 0
  205. for i in range(0,rows):
  206. for j in range(0,cols):
  207. start_x = icon_width/2 + j*icon_width
  208. start_y = TitleBar._BarHeight + icon_height/2 + i*icon_height
  209. icon = IconItem()
  210. icon.Init(start_x,start_y,icon_width-4,icon_height-4,0)
  211. icon._Index = cnt
  212. icon._Parent = self
  213. self._Icons.append(icon)
  214. if cnt >= (self._IconNumbers -1):
  215. break
  216. cnt+=1
  217. ps = PageSelector()
  218. ps._IconSurf = MyIconPool.GiveIconSurface("blueselector")
  219. ps._Parent = self
  220. ps.Init(icon_width/2,icon_height/2,92,92,128)
  221. self._Ps = ps
  222. self._PsIndex = 0
  223. self._OnShow = False
  224. def Adjust(self): ## default init way,
  225. self._PosX = self._Index*self._Screen._Width
  226. self._Width = self._Screen._Width ## equal to screen width
  227. self._Height = self._Screen._Height
  228. if self._Align == ALIGN["HLeft"]:
  229. start_x = (self._Width - self._IconNumbers*icon_width)/2 + icon_width/2
  230. start_y = self._Height/2
  231. for i in range(0,self._IconNumbers):
  232. it = self._Icons[i]
  233. it._Parent = self
  234. it._Index = i
  235. it.Adjust(start_x+i*icon_width,start_y,icon_width,icon_height,0)
  236. ps = PageSelector()
  237. ps._IconSurf = MyIconPool.GiveIconSurface("blueselector")
  238. ps._Parent = self
  239. ps.Init(start_x,start_y,92,92,128)
  240. self._Ps = ps
  241. self._PsIndex = 0
  242. self._OnShow = False
  243. elif self._Align == ALIGN["SLeft"]:
  244. start_x = (self._PageIconMargin + icon_width+self._PageIconMargin) /2
  245. start_y = self._Height/2
  246. for i in range(0,self._IconNumbers):
  247. it = self._Icons[i]
  248. it._Parent = self
  249. it._Index = i
  250. it.Adjust(start_x+i*self._PageIconMargin+i*icon_width,start_y,icon_width,icon_height,0)
  251. ps = PageSelector()
  252. ps._IconSurf = MyIconPool.GiveIconSurface("blueselector")
  253. ps._Parent = self
  254. ps.Init(start_x,start_y-self._SelectedIconTopOffset,92,92,128)
  255. self._Ps = ps
  256. self._PsIndex = 0
  257. self._OnShow = False
  258. if self._IconNumbers > 1:
  259. self._PsIndex = 1
  260. self._IconIndex = self._PsIndex
  261. self._PrevIconIndex = self._IconIndex
  262. self._Icons[self._IconIndex]._PosY -= self._SelectedIconTopOffset
  263. def Init(self): ## default init way,
  264. if self._Screen != None:
  265. if self._Screen._CanvasHWND != None and self._CanvasHWND == None:
  266. self._CanvasHWND = self._Screen._CanvasHWND
  267. self._PosX = self._Index*self._Screen._Width
  268. self._Width = self._Screen._Width ## equal to screen width
  269. self._Height = self._Screen._Height
  270. start_x = (self._Width - self._IconNumbers*icon_width)/2 + icon_width/2
  271. start_y = self._Height/2
  272. for i in range(0,self._IconNumbers):
  273. it = IconItem()
  274. it._Parent = self
  275. it._Index = i
  276. it.Init(start_x+i*icon_width,start_y,icon_width,icon_height,0)
  277. self._Icons.append(it)
  278. if self._IconNumbers > 0:
  279. ps = PageSelector()
  280. ps._IconSurf = MyIconPool.GiveIconSurface("blueselector")
  281. ps._Parent = self
  282. ps.Init(start_x,start_y,icon_width+4,icon_height+4,128)
  283. self._Ps = ps
  284. self._PsIndex = 0
  285. self._OnShow = False
  286. def IconStepMoveData(self,icon_eh,cuts):## no Sine,No curve,plain movement steps data
  287. all_pieces = []
  288. piece = icon_eh / cuts
  289. c = 0.0
  290. prev = 0.0
  291. for i in range(0,cuts):
  292. c+=piece
  293. dx = c-prev
  294. if dx < 0.5:
  295. dx = 1.0
  296. all_pieces.append( math.ceil(dx) )
  297. if c >= icon_eh:
  298. break
  299. c = 0
  300. bidx = 0
  301. for i in all_pieces:
  302. c+=i
  303. bidx+=1
  304. if c >= icon_eh:
  305. break
  306. all_pieces = all_pieces[0:bidx]
  307. if len(all_pieces) < cuts:
  308. dff = cuts - len(all_pieces)
  309. diffa = []
  310. for i in range(0,dff):
  311. diffa.append(0)
  312. all_pieces.extend( diffa)
  313. return all_pieces
  314. def EasingData(self,start,distance):##generate easing steps data
  315. current_time = 0.0
  316. start_posx = 0.0
  317. current_posx = start_posx
  318. final_posx = float(distance)
  319. posx_init = start
  320. dur = self._EasingDur
  321. last_posx = 0.0
  322. all_last_posx = []
  323. for i in range(0,distance*dur):
  324. current_posx = easing.SineIn(current_time,start_posx,final_posx-start_posx,float(dur))
  325. if current_posx >= final_posx:
  326. current_posx = final_posx
  327. dx = current_posx - last_posx
  328. all_last_posx.append(int(dx))
  329. current_time+=1
  330. last_posx = current_posx
  331. if current_posx >= final_posx:
  332. break
  333. c = 0
  334. for i in all_last_posx:
  335. c+=i
  336. if c < final_posx -start_posx:
  337. all_last_posx.append(final_posx - c)
  338. return all_last_posx
  339. # def IconSmoothUp(self,icon_ew):
  340. def IconSmoothUp(self, icon_ew, fast = False):
  341. data = self.EasingData(self._PosX,icon_ew)
  342. data2 = self.IconStepMoveData(self._SelectedIconTopOffset,len(data))
  343. for i,v in enumerate(data):
  344. self.ClearCanvas()
  345. self._Icons[self._IconIndex]._PosY -= data2[i]
  346. if self._Icons[self._PrevIconIndex]._PosY < self._Height/2:
  347. self._Icons[self._PrevIconIndex]._PosY+=data2[i]
  348. # self.DrawIcons()
  349. # self._Screen.SwapAndShow()
  350. if not fast:
  351. self.DrawIcons()
  352. self._Screen.SwapAndShow()
  353. # def IconsEasingLeft(self,icon_ew):
  354. def IconsEasingLeft(self, icon_ew, fast = False):
  355. data = self.EasingData(self._PosX,icon_ew)
  356. data2 = self.IconStepMoveData(self._SelectedIconTopOffset,len(data))
  357. for i,v in enumerate(data):
  358. self.ClearCanvas()
  359. self._PosX -= v
  360. self._Icons[self._IconIndex]._PosY -= data2[i]
  361. if self._Icons[self._PrevIconIndex]._PosY < self._Height/2:
  362. self._Icons[self._PrevIconIndex]._PosY+=data2[i]
  363. # self.DrawIcons()
  364. # self._Screen.SwapAndShow()
  365. if not fast:
  366. self.DrawIcons()
  367. self._Screen.SwapAndShow()
  368. # def IconsEasingRight(self,icon_ew):
  369. def IconsEasingRight(self, icon_ew, fast = False):
  370. data = self.EasingData(self._PosX,icon_ew)
  371. data2 = self.IconStepMoveData(self._SelectedIconTopOffset,len(data))
  372. for i,v in enumerate(data):
  373. self.ClearCanvas()
  374. self._PosX += v
  375. self._Icons[self._IconIndex]._PosY-=data2[i]
  376. if self._Icons[self._PrevIconIndex]._PosY < self._Height/2:
  377. self._Icons[self._PrevIconIndex]._PosY+=data2[i]
  378. # self.DrawIcons()
  379. # self._Screen.SwapAndShow()
  380. if not fast:
  381. self.DrawIcons()
  382. self._Screen.SwapAndShow()
  383. def EasingLeft(self,ew): #ew int
  384. data = self.EasingData(self._PosX,ew)
  385. for i in data:
  386. self._PosX -=i
  387. self.Draw()
  388. self._Screen.SwapAndShow()
  389. def EasingRight(self,ew):
  390. data = self.EasingData(self._PosX,ew)
  391. for i in data:
  392. self._PosX += i
  393. self.Draw()
  394. self._Screen.SwapAndShow()
  395. def MoveLeft(self,ew):
  396. self._PosX -= ew
  397. def MoveRight(self,ew):
  398. self._PosX += ew
  399. def ResetPageSelector(self):
  400. self._PsIndex = 0
  401. self._IconIndex = 0
  402. self._Ps._OnShow = True
  403. def DrawPageSelector(self):
  404. if self._Ps._OnShow == True:
  405. self._Ps.Draw()
  406. def MoveIconIndexPrev(self):
  407. self._PrevIconIndex = self._IconIndex
  408. self._IconIndex-=1
  409. if self._IconIndex < 0:
  410. self._IconIndex = max(0, self._IconNumbers - 1) # Wrap Icon Index
  411. return False
  412. return True
  413. def MoveIconIndexNext(self):
  414. self._PrevIconIndex = self._IconIndex
  415. self._IconIndex+=1
  416. if self._IconIndex > (self._IconNumbers - 1):
  417. self._IconIndex = 0 # Wrap Icon Index
  418. return False
  419. return True
  420. def IconClick(self):
  421. if self._IconIndex > (len(self._Icons) -1):
  422. return
  423. cur_icon = self._Icons[self._IconIndex]
  424. if self._Ps._OnShow == False:
  425. return
  426. if cur_icon._MyType == ICON_TYPES["EXE"]:
  427. print("IconClick: %s %d"%(cur_icon._CmdPath,cur_icon._Index))
  428. self._Screen.RunEXE(cur_icon._CmdPath)
  429. elif cur_icon._MyType == ICON_TYPES["DIR"]:
  430. child_page = self._Icons[self._IconIndex]._LinkPage
  431. if child_page != None:
  432. child_page.Draw()
  433. self._Screen._MyPageStack.Push(self._Screen._CurrentPage)
  434. self._Screen._CurrentPage = child_page
  435. elif cur_icon._MyType == ICON_TYPES["FUNC"]:
  436. print("IconClick API: %d"%(cur_icon._Index))
  437. # print("%s"% cur_icon._CmdPath)
  438. api_cb = getattr(cur_icon._CmdPath,"API",None)
  439. if api_cb != None:
  440. if callable(api_cb):
  441. cur_icon._CmdPath.API(self._Screen)
  442. elif cur_icon._MyType == ICON_TYPES["Emulator"] or cur_icon._MyType == ICON_TYPES["Commercial"]:
  443. cur_icon._CmdPath.API(self._Screen)
  444. def ReturnToUpLevelPage(self):
  445. pop_page,ok = self._Screen._MyPageStack.Pop()
  446. if ok == True:
  447. # self._Screen._CurrentPage.ResetPageSelector()
  448. pop_page.Draw()
  449. self._Screen._CurrentPage = pop_page
  450. on_return_back_cb = getattr(self._Screen._CurrentPage,"OnReturnBackCb",None)
  451. if on_return_back_cb != None:
  452. if callable(on_return_back_cb):
  453. self._Screen._CurrentPage.OnReturnBackCb()
  454. else:
  455. if self._Screen._MyPageStack.Length() == 0:
  456. if len(self._Screen._Pages) > 0:
  457. self._Screen._CurrentPage = self._Screen._Pages[self._Screen._PageIndex]
  458. self._Screen._CurrentPage.Draw()
  459. print("OnTopLevel ",self._Screen._PageIndex)
  460. def ClearCanvas(self):
  461. if self._Wallpaper:
  462. self._CanvasHWND.blit(self._Wallpaper,(0,0))
  463. else:
  464. self._CanvasHWND.fill(self._Screen._SkinManager.GiveColor("White"))
  465. def ClearIcons(self):
  466. for i in range(0,self._IconNumbers):
  467. self._Icons[i].Clear()
  468. def DrawIcons(self):
  469. for i in range(0,self._IconNumbers):
  470. self._Icons[i].Draw()
  471. # make sure the Class has the _MyList
  472. # def ScrollDown(self):
  473. # if len(self._MyList) == 0:
  474. # return
  475. # self._PsIndex +=1
  476. # if self._PsIndex >= len(self._MyList):
  477. # self._PsIndex = len(self._MyList) -1
  478. # cur_li = self._MyList[self._PsIndex]
  479. # if cur_li._PosY +cur_li._Height > self._Height:
  480. # for i in range(0,len(self._MyList)):
  481. # self._MyList[i]._PosY -= self._MyList[i]._Height
  482. # def ScrollUp(self):
  483. # if len(self._MyList) == 0:
  484. # return
  485. # self._PsIndex -= 1
  486. # if self._PsIndex < 0:
  487. # self._PsIndex = 0
  488. # cur_li = self._MyList[self._PsIndex]
  489. # if cur_li._PosY < 0:
  490. # for i in range(0, len(self._MyList)):
  491. # self._MyList[i]._PosY += self._MyList[i]._Height
  492. def ScrollUp(self, step = 1):
  493. if len(self._MyList) <= 1:
  494. return
  495. # check step
  496. if step > self._ItemsPerPage:
  497. step = self._ItemsPerPage - 1
  498. if step > len(self._MyList) - 1:
  499. step = 1
  500. # first to end
  501. if self._PsIndex - step < 0 and step == 1:
  502. # index of the last item on current screen
  503. self._PsIndex = 0 + self._ItemsPerPage - 1
  504. # loop scroll, to end
  505. if len(self._MyList) > self._ItemsPerPage:
  506. self.FScrollDown(len(self._MyList) - self._ItemsPerPage, True)
  507. self._PsIndex = len(self._MyList) - 1
  508. self._Scrolled = self._PsIndex
  509. return
  510. else:
  511. self.FScrollUp(step)
  512. def ScrollDown(self, step = 1):
  513. if len(self._MyList) <= 1:
  514. return
  515. # check step
  516. if step > self._ItemsPerPage:
  517. step = self._ItemsPerPage - 1
  518. if step > len(self._MyList) - 1:
  519. step = 1
  520. # end to first
  521. if self._PsIndex + step >= len(self._MyList) and step == 1:
  522. # index of the first item on current screen
  523. self._PsIndex = (len(self._MyList) - 1) - (self._ItemsPerPage - 1)
  524. # loop scroll, to first
  525. if len(self._MyList) > self._ItemsPerPage:
  526. self.FScrollUp(len(self._MyList) - self._ItemsPerPage, True)
  527. self._PsIndex = 0
  528. self._Scrolled = self._PsIndex
  529. return
  530. else:
  531. self.FScrollDown(step)
  532. # do not directly call this function, please use "ScrollUp(step)"
  533. def FScrollUp(self, step = 1, loop_scroll = False):
  534. # if len(self._MyList) == 0:
  535. if len(self._MyList) <= 1:
  536. return
  537. if step < self._ItemsPerPage and not loop_scroll:
  538. if (self._PsIndex - step + 1) - 1 < step:
  539. step = 1
  540. tmp = self._PsIndex
  541. self._PsIndex -= step
  542. if self._PsIndex < 0:
  543. self._PsIndex = 0
  544. # dy = tmp-self._PsIndex
  545. dy = abs(tmp - self._PsIndex)
  546. cur_li = self._MyList[self._PsIndex]
  547. if cur_li._PosY < 0:
  548. for i in range(0, len(self._MyList)):
  549. self._MyList[i]._PosY += self._MyList[i]._Height * dy
  550. self._Scrolled += dy
  551. # do not directly call this function, please use "ScrollDown(step)"
  552. def FScrollDown(self, step = 1, loop_scroll = False):
  553. # if len(self._MyList) == 0:
  554. if len(self._MyList) <= 1:
  555. return
  556. if step < self._ItemsPerPage and not loop_scroll:
  557. if len(self._MyList) - (self._PsIndex + step + 1) < step:
  558. step = 1
  559. tmp = self._PsIndex
  560. self._PsIndex += step
  561. if self._PsIndex >= len(self._MyList):
  562. self._PsIndex = len(self._MyList) - 1
  563. # dy = self._PsIndex - tmp
  564. dy = abs(self._PsIndex - tmp)
  565. cur_li = self._MyList[self._PsIndex]
  566. if cur_li._PosY + cur_li._Height > self._Height:
  567. for i in range(0, len(self._MyList)):
  568. self._MyList[i]._PosY -= self._MyList[i]._Height * dy
  569. self._Scrolled -= dy
  570. def SyncScroll(self):
  571. if self._Scrolled == 0:
  572. return
  573. if self._PsIndex < len(self._MyList):
  574. cur_li = self._MyList[self._PsIndex]
  575. if self._Scrolled > 0:
  576. if cur_li._PosY < 0:
  577. for i in range(0, len(self._MyList)):
  578. self._MyList[i]._PosY += self._Scrolled * self._MyList[i]._Height
  579. elif self._Scrolled < 0:
  580. if cur_li._PosY +cur_li._Height > self._Height:
  581. for i in range(0,len(self._MyList)):
  582. self._MyList[i]._PosY += self._Scrolled * self._MyList[i]._Height
  583. def SpeedScroll(self, thekey):
  584. if self._Screen._LastKey == thekey:
  585. self._ScrollStep += 1
  586. if self._ScrollStep >= self._ItemsPerPage:
  587. self._ScrollStep = self._ItemsPerPage - 1
  588. else:
  589. self._ScrollStep = 1
  590. cur_time = time.time()
  591. if cur_time - self._Screen._LastKeyDown > 0.3:
  592. self._ScrollStep = 1
  593. if len(self._MyList) < self._ItemsPerPage:
  594. self._ScrollStep = 1
  595. def RefreshPsIndex(self):
  596. if len(self._MyList) == 0:
  597. self._PsIndex = 0
  598. if self._PsIndex > (len(self._MyList) -1):
  599. self._PsIndex = len(self._MyList) -1
  600. # def KeyDown(self,event):##default keydown, every inherited page class should have it's own KeyDown
  601. # fast: fast display mode
  602. def KeyDown(self, event, fast = False):##default keydown, every inherited page class should have it's own KeyDown
  603. if IsKeyMenuOrB(event.key):
  604. self.ReturnToUpLevelPage()
  605. self._Screen.Draw()
  606. self._Screen.SwapAndShow()
  607. if event.key == CurKeys["Right"]:
  608. if self.MoveIconIndexNext() == True:
  609. if self._IconIndex == (self._IconNumbers -1) or self._PrevIconIndex == 0:
  610. # self.IconSmoothUp(icon_width + self._PageIconMargin)
  611. self.IconSmoothUp(icon_width + self._PageIconMargin, fast)
  612. else:
  613. # self.IconsEasingLeft(icon_width + self._PageIconMargin)
  614. self.IconsEasingLeft(icon_width + self._PageIconMargin, fast)
  615. else:
  616. screen_icons = int(math.floor(self._Screen._Width / (icon_width + self._PageIconMargin)))
  617. if self._IconNumbers > screen_icons:
  618. # self.IconsEasingRight((icon_width + self._PageIconMargin) * (self._IconNumbers - screen_icons))
  619. self.IconsEasingRight((icon_width + self._PageIconMargin) * (self._IconNumbers - screen_icons), fast)
  620. elif self._IconNumbers > 0:
  621. # self.IconSmoothUp(icon_width+ self._PageIconMargin)
  622. self.IconSmoothUp(icon_width+ self._PageIconMargin, fast)
  623. self._PsIndex = self._IconIndex
  624. # self._Screen.Draw()
  625. # self._Screen.SwapAndShow()
  626. if not fast:
  627. self._Screen.Draw()
  628. self._Screen.SwapAndShow()
  629. if event.key == CurKeys["Left"]:
  630. if self.MoveIconIndexPrev() == True:
  631. if self._IconIndex == 0 or self._PrevIconIndex == (self._IconNumbers -1):
  632. # self.IconSmoothUp(icon_width + self._PageIconMargin)
  633. self.IconSmoothUp(icon_width + self._PageIconMargin, fast)
  634. else:
  635. # self.IconsEasingRight(icon_width + self._PageIconMargin)
  636. self.IconsEasingRight(icon_width + self._PageIconMargin, fast)
  637. else:
  638. screen_icons = int(math.floor(self._Screen._Width / (icon_width + self._PageIconMargin)))
  639. if self._IconNumbers > screen_icons:
  640. # self.IconsEasingLeft((icon_width + self._PageIconMargin) * (self._IconNumbers - screen_icons))
  641. self.IconsEasingLeft((icon_width + self._PageIconMargin) * (self._IconNumbers - screen_icons), fast)
  642. elif self._IconNumbers > 0:
  643. # self.IconSmoothUp(icon_width+ self._PageIconMargin)
  644. self.IconSmoothUp(icon_width+ self._PageIconMargin, fast)
  645. self._PsIndex = self._IconIndex
  646. # self._Screen.Draw()
  647. # self._Screen.SwapAndShow()
  648. if not fast:
  649. self._Screen.Draw()
  650. self._Screen.SwapAndShow()
  651. if event.key == CurKeys["Up"]:
  652. move = 3
  653. pageup = pygame.event.Event(pygame.KEYDOWN, key = CurKeys["Left"])
  654. for i in range(move):
  655. self.KeyDown(pageup, True)
  656. self.DrawIcons() # redraw icons
  657. self._Screen.Draw() # show selected icon
  658. self._Screen.SwapAndShow()
  659. if event.key == CurKeys["Down"]:
  660. move = 3
  661. pagedown = pygame.event.Event(pygame.KEYDOWN, key = CurKeys["Right"])
  662. for i in range(move):
  663. self.KeyDown(pagedown, True)
  664. self.DrawIcons() # redraw icons
  665. self._Screen.Draw() # show selected icon
  666. self._Screen.SwapAndShow()
  667. if IsKeyStartOrA(event.key):
  668. self.IconClick()
  669. self._Screen.Draw()
  670. self._Screen.SwapAndShow()
  671. ##for gcores
  672. def DrawCross(self,topleft,top):
  673. start_x = topleft
  674. start_y = top
  675. width = 2
  676. height = 10
  677. padding = 4
  678. rect1 = pygame.Rect(start_x+padding,start_y,width,height)
  679. rect2 = pygame.Rect(start_x,start_y+padding,height,width)
  680. pygame.draw.rect(self._CanvasHWND,MySkinManager.GiveColor('Text'),rect1, 0)
  681. pygame.draw.rect(self._CanvasHWND,MySkinManager.GiveColor('Text'),rect2, 0)
  682. def Draw(self):
  683. self.ClearCanvas()
  684. self.DrawIcons()
  685. self.DrawPageSelector()