main_screen.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. from pygame.locals import *
  4. from sys import exit
  5. import os
  6. import sys
  7. from libs import easing
  8. from datetime import datetime
  9. from beeprint import pp
  10. ## local package import
  11. from constants import ICON_TYPES,icon_ext,icon_width,icon_height,RUNEVT
  12. from icon_item import IconItem
  13. from page import Page,PageStack
  14. from title_bar import TitleBar
  15. from foot_bar import FootBar
  16. from constants import Width,Height,bg_color
  17. from util_funcs import midRect,FileExists,ReplaceSuffix,ReadTheFileContent,CmdClean,MakeExecutable,SkinMap
  18. from fonts import fonts
  19. from keys_def import CurKeys
  20. from label import Label
  21. from untitled_icon import UntitledIcon
  22. from Emulator import MyEmulator
  23. from skin_manager import SkinManager
  24. class MessageBox(Label):
  25. _Parent = None
  26. def __init__(self):
  27. pass
  28. def Init(self,text,font_obj,color=pygame.Color(83,83,83)):
  29. self._Color = color
  30. self._FontObj = font_obj
  31. self._Text = text
  32. self._Width = 0
  33. self._Height = 0
  34. self._CanvasHWND = pygame.Surface( ( int(self._Parent._Width),int(self._Parent._Height)))
  35. self._HWND = self._Parent._CanvasHWND
  36. def SetText(self,text):
  37. self._Text = text
  38. def PreDraw(self):
  39. self._Width = 0
  40. self._Height = 0
  41. self._CanvasHWND.fill( (255,255,255))
  42. words = self._Text.split(' ')
  43. space = self._FontObj.size(' ')[0]
  44. max_width = self._Parent._Width-40
  45. x ,y = (0,0)
  46. row_total_width = 0
  47. lines = 0
  48. for word in words:
  49. word_surface = self._FontObj.render(word, True, self._Color)
  50. word_width = word_surface.get_width()
  51. word_height = word_surface.get_height()
  52. row_total_width += word_width
  53. if lines == 0:
  54. lines += word_height
  55. if row_total_width+space >= max_width:
  56. x = 0 # Reset the x.
  57. y += word_height # Start on new row.
  58. row_total_width = word_width
  59. lines += word_height
  60. self._CanvasHWND.blit(word_surface, (x, y))
  61. if len(words) == 1: # single line
  62. x += word_width
  63. else:
  64. x += word_width + space
  65. if x > self._Width:
  66. self._Width = x
  67. if lines >= (self._Parent._Height - 40):
  68. break
  69. self._Height = lines
  70. def DrawWith(self, x_,y_, withborder):
  71. self.PreDraw()
  72. x_ = x_ - self._Width/2
  73. y_ = y_ - self._Height/2
  74. padding = 5
  75. pygame.draw.rect(self._HWND,(255,255,255),(x_-padding,y_-padding, self._Width+padding*2,self._Height+padding*2))
  76. if self._HWND != None:
  77. rect = pygame.Rect(x_,y_,self._Width,self._Height)
  78. self._HWND.blit(self._CanvasHWND,rect,(0,0,self._Width,self._Height))
  79. #self._HWND.blit(self._CanvasHWND,rect)
  80. if withborder == True:
  81. pygame.draw.rect(self._HWND,(0,0,0),(x_-padding,y_-padding, self._Width+padding*2,self._Height+padding*2),1)
  82. def Draw(self):
  83. x = (self._Parent._Width)/2
  84. y = (self._Parent._Height)/2
  85. self.DrawWith(x,y,True)
  86. python_package_flag = "__init__.py"
  87. emulator_flag = "action.config"
  88. ##Abstract object for manage Pages ,not the pygame's physic screen
  89. class MainScreen(object):
  90. _Pages = []
  91. _PageMax = 0
  92. _PageIndex = 0
  93. _PosX = 0
  94. _PosY = TitleBar._BarHeight+1
  95. _Width = Width
  96. _Height = Height -FootBar._BarHeight -TitleBar._BarHeight-1
  97. _MyPageStack = None
  98. _CurrentPage = None # pointer to the current displaying Page Class
  99. _CanvasHWND = None
  100. _HWND = None
  101. _TitleBar = None
  102. _FootBar = None
  103. _MsgBox = None
  104. _MsgBoxFont = fonts["veramono20"]
  105. _IconFont = fonts["varela15"]
  106. _SkinManager = None
  107. def __init__(self):
  108. self._Pages = []
  109. self._MyPageStack = PageStack()
  110. def Init(self):
  111. self._CanvasHWND = pygame.Surface((self._Width,self._Height))
  112. self._MsgBox= MessageBox()
  113. self._MsgBox._Parent= self
  114. self._MsgBox.Init(" ", self._MsgBoxFont)
  115. self._SkinManager = SkinManager()
  116. self._SkinManager.Init()
  117. def FartherPages(self):
  118. self._PageMax = len(self._Pages)
  119. for i in range(0,self._PageMax):
  120. self._Pages[i]._Index = i
  121. self._Pages[i]._CanvasHWND = self._CanvasHWND
  122. self._Pages[i]._IconNumbers = len(self._Pages[i]._Icons)
  123. self._Pages[i]._Screen = self
  124. self._Pages[i].Adjust()
  125. if self._Pages[i]._IconNumbers > 1:
  126. self._Pages[i]._PsIndex = 1
  127. self._Pages[i]._IconIndex = self._Pages[i]._PsIndex
  128. self._CurrentPage = self._Pages[self._PageIndex]
  129. self._CurrentPage._OnShow = True
  130. def GetMyRightSidePage(self):
  131. ret = self._PageIndex +1
  132. if ret > (self._PageMax -1):
  133. ret = self._PageMax -1
  134. return ret
  135. def PageMoveLeft(self):
  136. self._Pages[self._PageIndex]._OnShow = False
  137. if self._PageIndex < (self._PageMax - 1):
  138. my_right_side_page = self.GetMyRightSidePage()
  139. for i in range(0,self._PageMax):
  140. if i!= self._PageIndex and i != my_right_side_page:
  141. self._Pages[i].MoveLeft(Width)
  142. self._Pages[self._PageIndex].EasingLeft(Width)
  143. if self._PageIndex != my_right_side_page:
  144. self._Pages[my_right_side_page].EasingLeft(Width)
  145. self._Pages[self._PageIndex].ResetPageSelector()
  146. self._PageIndex+=1
  147. if self._PageIndex > (self._PageMax -1):
  148. self._PageIndex = (self._PageMax -1)
  149. self._Pages[self._PageIndex]._OnShow = True
  150. self._CurrentPage = self._Pages[self._PageIndex]
  151. def GetMyLeftSidePage(self):
  152. ret = self._PageIndex -1
  153. if ret < 0:
  154. ret = 0
  155. return ret
  156. def PageMoveRight(self):
  157. self._Pages[self._PageIndex]._OnShow = False
  158. if self._PageIndex > 0:
  159. my_left_side_page = self.GetMyLeftSidePage()
  160. for i in range(0,self._PageMax):
  161. if i!= self._PageIndex and i!= my_left_side_page:
  162. pass
  163. #self._Pages[i].MoveRight(Width)
  164. self._Pages[self._PageIndex].EasingRight(Width)
  165. if self._PageIndex != my_left_side_page:
  166. self._Pages[my_left_side_page].EasingRight(Width)
  167. self._Pages[self._PageIndex].ResetPageSelector()
  168. self._PageIndex-=1
  169. if self._PageIndex < 0:
  170. self._PageIndex = 0
  171. self._Pages[self._PageIndex]._OnShow = True
  172. self._CurrentPage = self._Pages[self._PageIndex]
  173. def EasingAllPageLeft(self):
  174. current_time = 0.0
  175. start_posx = 0.0
  176. current_posx = start_posx
  177. final_posx = float(Width)
  178. posx_init = 0
  179. dur = 30
  180. last_posx = 0.0
  181. all_last_posx = []
  182. if self._PageIndex >= (self._PageMax - 1):
  183. return
  184. for i in range(0,Width*dur):
  185. current_posx = easing.SineIn(current_time,start_posx,final_posx-start_posx,float(dur))
  186. if current_posx >= final_posx:
  187. current_posx = final_posx
  188. dx = current_posx - last_posx
  189. all_last_posx.append(int(dx))
  190. current_time+=1
  191. last_posx = current_posx
  192. if current_posx >= final_posx:
  193. break
  194. c = 0
  195. for i in all_last_posx:
  196. c+=i
  197. if c < final_posx - start_posx:
  198. all_last_posx.append( final_posx - c )
  199. for i in all_last_posx:
  200. self.ClearCanvas()
  201. for j in self._Pages:
  202. j._PosX -= i
  203. j.DrawIcons()
  204. j._Screen.SwapAndShow()
  205. self._Pages[self._PageIndex]._OnShow = False
  206. self._PageIndex+=1
  207. if self._PageIndex > (self._PageMax -1):
  208. self._PageIndex = (self._PageMax -1)
  209. self._Pages[self._PageIndex]._OnShow = True
  210. self._CurrentPage = self._Pages[self._PageIndex]
  211. def EasingAllPageRight(self):
  212. current_time = 0.0
  213. start_posx = 0.0
  214. current_posx = start_posx
  215. final_posx = float(Width)
  216. posx_init = 0
  217. dur = 30
  218. last_posx = 0.0
  219. all_last_posx = []
  220. if self._PageIndex <= 0:
  221. return
  222. for i in range(0,Width*dur):
  223. current_posx = easing.SineIn(current_time,start_posx,final_posx-start_posx,float(dur))
  224. if current_posx >= final_posx:
  225. current_posx = final_posx
  226. dx = current_posx - last_posx
  227. all_last_posx.append(int(dx))
  228. current_time+=1
  229. last_posx = current_posx
  230. if current_posx >= final_posx:
  231. break
  232. c = 0
  233. for i in all_last_posx:
  234. c+=i
  235. if c < final_posx - start_posx:
  236. all_last_posx.append( final_posx - c )
  237. for i in all_last_posx:
  238. self.ClearCanvas()
  239. for j in reversed(self._Pages):
  240. j._PosX += i
  241. j.DrawIcons()
  242. j._Screen.SwapAndShow()
  243. self._Pages[self._PageIndex]._OnShow = False
  244. self._PageIndex-=1
  245. if self._PageIndex < 0:
  246. self._PageIndex = 0
  247. self._Pages[self._PageIndex]._OnShow = True
  248. self._CurrentPage = self._Pages[self._PageIndex]
  249. def CurPage(self):
  250. return self._CurrentPage
  251. def PushCurPage(self):
  252. self._MyPageStack.Push(self._CurrentPage)
  253. def SetCurPage(self,page):
  254. self._CurrentPage = page
  255. on_load_cb = getattr(self._CurrentPage,"OnLoadCb",None)
  256. if on_load_cb != None:
  257. if callable( on_load_cb ):
  258. self._CurrentPage.OnLoadCb()
  259. def PushPage(self,page):
  260. self.PushCurPage()
  261. self.SetCurPage(page)
  262. def AppendPage(self,Page):
  263. self._Pages.append(Page)
  264. def ClearCanvas(self):
  265. self._CanvasHWND.fill((255,255,255))
  266. def SwapAndShow(self):
  267. if self._HWND != None:
  268. self._HWND.blit(self._CanvasHWND,(self._PosX,self._PosY,self._Width,self._Height))
  269. pygame.display.update()
  270. def ExtraName(self,name):
  271. ## extra name like 1_xxx to be => xxx,
  272. parts = name.split("_")
  273. if len(parts) > 1:
  274. return parts[1]
  275. elif len(parts) == 1:
  276. return parts[0]
  277. else:
  278. return name
  279. def IsEmulatorPackage(self,dirname):
  280. files = os.listdir(dirname)
  281. for i in sorted(files):
  282. if i.endswith(emulator_flag):
  283. return True
  284. return False
  285. def IsPythonPackage(self,dirname):
  286. files = os.listdir(dirname)
  287. for i in sorted(files):
  288. if i.endswith(python_package_flag):
  289. return True
  290. return False
  291. def ReadTheDirIntoPages(self,_dir,pglevel,cur_page):
  292. if FileExists(_dir) == False and os.path.isdir(_dir) == False:
  293. return
  294. files = os.listdir(_dir)
  295. for i in sorted(files):
  296. if os.path.isdir(_dir+"/"+i): # TOPLEVEL only is dir
  297. if pglevel == 0:
  298. page = Page()
  299. page._Name = self.ExtraName(i)
  300. page._Icons = []
  301. self._Pages.append(page)
  302. self.ReadTheDirIntoPages(_dir+"/"+i, pglevel+1 ,self._Pages[ len(self._Pages) -1])
  303. else: ## On CurPage now
  304. i2 = self.ExtraName(i)
  305. iconitem = IconItem()
  306. iconitem._CmdPath = ""
  307. iconitem.AddLabel(i2,self._IconFont)
  308. if FileExists( SkinMap(_dir+"/"+i2+".png") ):
  309. iconitem._ImageName = SkinMap(_dir+"/"+i2+".png")
  310. else:
  311. untitled = UntitledIcon()
  312. untitled.Init()
  313. if len(i2) > 1:
  314. untitled.SetWords(i2[:2])
  315. elif len(i2) == 1:
  316. untitled.SetWords([i2[0],i2[0]])
  317. else:
  318. untitled.SetWords(["G","s"])
  319. iconitem._ImgSurf = untitled.Surface()
  320. iconitem._ImageName = ""
  321. if self.IsPythonPackage(_dir+"/"+i):
  322. iconitem._MyType = ICON_TYPES["FUNC"]
  323. sys.path.append(_dir)
  324. iconitem._CmdPath = __import__(i)
  325. init_cb = getattr(iconitem._CmdPath,"Init",None)
  326. if init_cb != None:
  327. if callable(init_cb):
  328. iconitem._CmdPath.Init(self)
  329. cur_page._Icons.append(iconitem)
  330. elif self.IsEmulatorPackage(_dir+"/"+i):
  331. obj = {}
  332. obj["ROM"] = ""
  333. obj["ROM_SO"] =""
  334. obj["EXT"] = []
  335. obj["FILETYPE"] = "file"
  336. obj["LAUNCHER"] = ""
  337. obj["TITLE"] = "Game"
  338. obj["SO_URL"] = ""
  339. obj["RETRO_CONFIG"] = "" ##
  340. try:
  341. f = open(_dir+"/"+i+"/"+emulator_flag)
  342. except IOError:
  343. print("action config open failed")
  344. return
  345. else:
  346. with f:
  347. content = f.readlines()
  348. content = [x.strip() for x in content]
  349. for i in content:
  350. pis = i.split("=")
  351. if len(pis) > 1:
  352. if "EXT" in pis[0]:
  353. obj[pis[0]] = pis[1].split(",")
  354. else:
  355. obj[pis[0]] = pis[1]
  356. em = MyEmulator()
  357. em._Emulator = obj
  358. em.Init(self)
  359. iconitem._CmdPath = em
  360. iconitem._MyType = ICON_TYPES["Emulator"]
  361. cur_page._Icons.append(iconitem)
  362. else:
  363. iconitem._MyType = ICON_TYPES["DIR"]
  364. iconitem._LinkPage = Page()
  365. iconitem._LinkPage._Name = i2
  366. cur_page._Icons.append(iconitem)
  367. self.ReadTheDirIntoPages(_dir+"/"+i,pglevel+1,iconitem._LinkPage)
  368. elif os.path.isfile(_dir+"/"+i) and pglevel > 0:
  369. if i.lower().endswith(icon_ext):
  370. i2 = self.ExtraName(i)
  371. #cmd = ReadTheFileContent(_dir+"/"+i)
  372. iconitem = IconItem()
  373. iconitem._CmdPath = _dir+"/"+i
  374. MakeExecutable(iconitem._CmdPath)
  375. iconitem._MyType = ICON_TYPES["EXE"]
  376. if FileExists( SkinMap( _dir+"/"+ReplaceSuffix(i2,"png"))):
  377. iconitem._ImageName = SkinMap(_dir+"/"+ReplaceSuffix(i2,"png"))
  378. else:
  379. untitled = UntitledIcon()
  380. untitled.Init()
  381. if len(i2) > 1:
  382. untitled.SetWords(i2[:2])
  383. elif len(i2) == 1:
  384. untitled.SetWords([i2[0],i2[0]])
  385. else:
  386. untitled.SetWords(["G","s"])
  387. iconitem._ImgSurf = untitled.Surface()
  388. iconitem._ImageName = ""
  389. iconitem.AddLabel(i2.split(".")[0],self._IconFont)
  390. iconitem._LinkPage = None
  391. cur_page._Icons.append(iconitem)
  392. def RunEXE(self,cmdpath):
  393. self.DrawRun()
  394. self.SwapAndShow()
  395. pygame.time.delay(1000)
  396. cmdpath = cmdpath.strip()
  397. cmdpath = CmdClean(cmdpath)
  398. pygame.event.post( pygame.event.Event(RUNEVT, message=cmdpath))
  399. def OnExitCb(self,event):
  400. ## leave rest to Pages
  401. on_exit_cb = getattr(self._CurrentPage,"OnExitCb",None)
  402. if on_exit_cb != None:
  403. if callable( on_exit_cb ):
  404. self._CurrentPage.OnExitCb(event)
  405. return
  406. def KeyDown(self,event):
  407. """
  408. if event.key == pygame.K_PAGEUP:
  409. self.EasingAllPageLeft()
  410. #self.SwapAndShow()
  411. if event.key == pygame.K_PAGEDOWN:
  412. self.EasingAllPageRight()
  413. #self.SwapAndShow()
  414. """
  415. if event.key == pygame.K_t:
  416. self.DrawRun()
  417. self.SwapAndShow()
  418. if event.key == CurKeys["Space"]:
  419. self.Draw()
  420. self.SwapAndShow()
  421. ## leave rest to Pages
  422. current_page_key_down_cb = getattr(self._CurrentPage,"KeyDown",None)
  423. if current_page_key_down_cb != None:
  424. if callable( current_page_key_down_cb ):
  425. self._CurrentPage.KeyDown(event)
  426. def DrawRun(self):
  427. self._MsgBox.SetText("Launching....")
  428. self._MsgBox.Draw()
  429. def Draw(self):
  430. self._CurrentPage.Draw()
  431. #if self._HWND != None:
  432. # self._HWND.blit(self._CanvasHWND,(self._PosX,self._PosY,self._Width,self._Height))
  433. if self._TitleBar != None:
  434. self._TitleBar.Draw(self._CurrentPage._Name)
  435. if self._FootBar != None:
  436. if hasattr(self._CurrentPage,"_FootMsg"):
  437. self._FootBar.SetLabelTexts(self._CurrentPage._FootMsg)
  438. self._FootBar.Draw()