main_screen.py 19 KB

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