Advertisement
Guest User

Untitled

a guest
Jan 6th, 2020
128
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. # -*- coding: utf-8 -*-
  3.  
  4. import os
  5. import pygame
  6. import glob
  7. import time
  8. from libs.roundrects import aa_round_rect
  9.  
  10. ## local UI import
  11. from UI.constants import Width,Height,ICON_TYPES,RUNEVT
  12. from UI.page import Page,PageSelector
  13. from UI.label import Label
  14. from UI.icon_item import IconItem
  15. from UI.util_funcs import midRect,CmdClean,FileExists
  16. from UI.keys_def import CurKeys, IsKeyStartOrA, IsKeyMenuOrB
  17. from UI.multi_icon_item import MultiIconItem
  18. from UI.icon_pool import MyIconPool
  19. from UI.scroller import ListScroller
  20. from UI.skin_manager import MySkinManager
  21. from UI.lang_manager import MyLangManager
  22.  
  23. from rom_so_confirm_page import RomSoConfirmPage
  24.  
  25. from list_item import ListItem
  26.  
  27.  
  28. class FavStack:
  29. _Emulator = None
  30.  
  31. def __init__(self):
  32. self.stack = list()
  33.  
  34. def Push(self,data):
  35. if data not in self.stack:
  36. self.stack.append(data)
  37. return True
  38. return False
  39.  
  40. def Pop(self):
  41. if len(self.stack)<=0:
  42. return None,False
  43. return self.stack.pop(),True
  44.  
  45. def Last(self):
  46. idx = len(self.stack) -1
  47. if idx < 0:## empty stack,return root path
  48. return self._Emulator["ROM"]
  49. else:
  50. return self.stack[ idx ]
  51.  
  52. def Length(self):
  53. return len(self.stack)
  54.  
  55. class ListPageSelector(PageSelector):
  56. _BackgroundColor = MySkinManager.GiveColor('Front')
  57.  
  58. def __init__(self):
  59. self._PosX = 0
  60. self._PosY = 0
  61. self._Height = 0
  62. self._Width = Width-12
  63.  
  64. def AnimateDraw(self,x2,y2):
  65. pass
  66.  
  67. def Draw(self):
  68. idx = self._Parent._PsIndex
  69. if idx > (len(self._Parent._MyList)-1):
  70. idx = len(self._Parent._MyList)
  71. if idx > 0:
  72. idx -=1
  73. elif idx == 0:##nothing in _MyList
  74. return
  75.  
  76. self._Parent._PsIndex = idx #sync PsIndex
  77.  
  78. x = self._Parent._MyList[idx]._PosX+2
  79. y = self._Parent._MyList[idx]._PosY+1
  80. h = self._Parent._MyList[idx]._Height -3
  81.  
  82. self._PosX = x
  83. self._PosY = y
  84. self._Height = h
  85.  
  86. aa_round_rect(self._Parent._CanvasHWND,
  87. (x,y,self._Width-4,h),self._BackgroundColor,4,0,self._BackgroundColor)
  88.  
  89.  
  90.  
  91. class FavListPage(Page):
  92.  
  93. _Icons = {}
  94. _Selector=None
  95. _FootMsg = ["Nav","Remove","Scan","","Run"]
  96. _MyList = []
  97. _ListFont = MyLangManager.TrFont("notosanscjk15")
  98. _MyStack = None
  99. _Emulator = None
  100. _Parent = None
  101. _Scroller = None
  102. _Scrolled = 0
  103. _BGwidth = 75
  104. _BGheight = 73
  105. _RomSoConfirmDownloadPage = None
  106.  
  107.  
  108. def __init__(self):
  109. Page.__init__(self)
  110. self._Icons = {}
  111. self._CanvasHWND = None
  112. self._MyList = []
  113. self._MyStack = FavStack()
  114. self._Emulator = {}
  115.  
  116. def GeneratePathList(self,path):
  117. if os.path.isdir(path) == False:
  118. return False
  119.  
  120.  
  121. files_path = glob.glob(path+"/*")
  122.  
  123. ret = []
  124.  
  125. for i ,v in enumerate(files_path):
  126. dirmap = {}
  127. if os.path.isdir(v) and self._Emulator["FILETYPE"] == "dir": ## like DOSBOX
  128. gameshell_bat = self._Emulator["EXT"][0]
  129. if FileExists(v+"/"+gameshell_bat):
  130. stats = os.stat(v)
  131. if stats.st_gid != self._Parent._FavGID: ## only favs
  132. continue
  133. try:
  134. dirmap["gamedir"] = v.decode("utf8","ignore")
  135. except:
  136. dirmap["gamedir"] = v.decode("ascii","ignore")
  137.  
  138. ret.append(dirmap)
  139. if os.path.isfile(v) and self._Emulator["FILETYPE"] == "file":
  140. stats = os.stat(v)
  141. if stats.st_gid != self._Parent._FavGID: ## only favs
  142. continue
  143. bname = os.path.basename(v) ### filter extension
  144. if len(bname)> 1:
  145. pieces = bname.split(".")
  146. if len(pieces) > 1:
  147. if pieces[ len(pieces)-1 ].lower() in self._Emulator["EXT"]:
  148. try:
  149. dirmap["file"] = v.decode("utf8","ignore")
  150. except:
  151. dirmap["file"] = v.decode("ascii","ignore")
  152.  
  153. ret.append(dirmap)
  154.  
  155. # else:
  156. # print("not file or dir")
  157.  
  158.  
  159. return ret
  160.  
  161. def SyncList(self,path):
  162.  
  163. alist = self.GeneratePathList(path)
  164.  
  165. if alist == False:
  166. print("listfiles return false")
  167. return
  168. # print("fav list alist: ")
  169. # print(alist)
  170.  
  171. self._MyList = []
  172. start_x = 0
  173. start_y = 0
  174. hasparent = 0
  175. if self._MyStack.Length() > 0:
  176. hasparent = 1
  177. li = ListItem()
  178. li._Parent = self
  179. li._PosX = start_x
  180. li._PosY = start_y
  181. li._Width = Width
  182. li._Fonts["normal"] = self._ListFont
  183. li._MyType = ICON_TYPES["DIR"]
  184. li._Parent = self
  185. li.Init("[..]")
  186. self._MyList.append(li)
  187.  
  188. for i,v in enumerate(sorted(alist)):
  189. li = ListItem()
  190. li._Parent = self
  191. li._PosX = start_x
  192. li._PosY = start_y + (i+hasparent)*ListItem._Height
  193. li._Width = Width
  194. li._Fonts["normal"] = self._ListFont
  195. li._MyType = ICON_TYPES["FILE"]
  196. li._Parent = self
  197. if "directory" in v:
  198. li._MyType = ICON_TYPES["DIR"]
  199. li.Init(v["directory"])
  200. elif "file" in v:
  201. li.Init(v["file"])
  202.  
  203. elif "gamedir" in v:
  204. li.Init(v["gamedir"])
  205. else:
  206. li.Init("NoName")
  207.  
  208. self._MyList.append(li)
  209.  
  210.  
  211.  
  212. def Init(self):
  213. self._PosX = self._Index * self._Screen._Width
  214. self._Width = self._Screen._Width
  215. self._Height = self._Screen._Height
  216.  
  217. self._CanvasHWND = self._Screen._CanvasHWND
  218.  
  219. ps = ListPageSelector()
  220. ps._Parent = self
  221. self._Ps = ps
  222. self._PsIndex = 0
  223.  
  224. self.SyncList(self._Emulator["ROM"])
  225.  
  226. self._MyStack._Emulator = self._Emulator
  227.  
  228. icon_for_list = MultiIconItem()
  229. icon_for_list._ImgSurf = self._Parent._Icons["sys"]
  230. icon_for_list._MyType = ICON_TYPES["STAT"]
  231. icon_for_list._Parent = self
  232. icon_for_list.Adjust(0,0,18,18,0)
  233.  
  234. self._Icons["sys"] = icon_for_list
  235.  
  236.  
  237. bgpng = IconItem()
  238. bgpng._ImgSurf = MyIconPool.GiveIconSurface("star")
  239. bgpng._MyType = ICON_TYPES["STAT"]
  240. bgpng._Parent = self
  241. bgpng.AddLabel(MyLangManager.Tr("MyFavGames"), MyLangManager.TrFont("varela18"))
  242. bgpng.SetLableColor(MySkinManager.GiveColor('Disabled'))
  243. bgpng.Adjust(0,0,self._BGwidth,self._BGheight,0)
  244.  
  245. self._Icons["bg"] = bgpng
  246.  
  247. self._Scroller = ListScroller()
  248. self._Scroller._Parent = self
  249. self._Scroller._PosX = self._Width - 10
  250. self._Scroller._PosY = 2
  251. self._Scroller.Init()
  252.  
  253. rom_so_confirm_page = RomSoConfirmPage()
  254. rom_so_confirm_page._Screen = self._Screen
  255. rom_so_confirm_page._Name = "Download Confirm"
  256. rom_so_confirm_page._Parent = self
  257. rom_so_confirm_page.Init()
  258.  
  259. self._RomSoConfirmDownloadPage = rom_so_confirm_page
  260.  
  261. def ScrollUp(self):
  262. if len(self._MyList) == 0:
  263. return
  264.  
  265. tmp = self._PsIndex
  266. self._PsIndex -= self._ScrollStep
  267.  
  268. if self._PsIndex < 0:
  269. self._PsIndex = 0
  270. dy = tmp - self._PsIndex
  271. cur_li = self._MyList[self._PsIndex]
  272. if cur_li._PosY < 0:
  273. for i in range(0, len(self._MyList)):
  274. self._MyList[i]._PosY += self._MyList[i]._Height*dy
  275. self._Scrolled +=dy
  276.  
  277. def ScrollDown(self):
  278. if len(self._MyList) == 0:
  279. return
  280. tmp = self._PsIndex
  281. self._PsIndex +=self._ScrollStep
  282. if self._PsIndex >= len(self._MyList):
  283. self._PsIndex = len(self._MyList) -1
  284.  
  285. dy = self._PsIndex - tmp
  286. cur_li = self._MyList[self._PsIndex]
  287. if cur_li._PosY +cur_li._Height > self._Height:
  288. for i in range(0,len(self._MyList)):
  289. self._MyList[i]._PosY -= self._MyList[i]._Height*dy
  290. self._Scrolled -= dy
  291.  
  292. def SyncScroll(self):
  293. ##
  294. if self._Scrolled == 0:
  295. return
  296.  
  297. if self._PsIndex < len(self._MyList):
  298. cur_li = self._MyList[self._PsIndex]
  299. if self._Scrolled > 0:
  300. if cur_li._PosY < 0:
  301. for i in range(0, len(self._MyList)):
  302. self._MyList[i]._PosY += self._Scrolled * self._MyList[i]._Height
  303. elif self._Scrolled < 0:
  304. if cur_li._PosY +cur_li._Height > self._Height:
  305. for i in range(0,len(self._MyList)):
  306. self._MyList[i]._PosY += self._Scrolled * self._MyList[i]._Height
  307.  
  308. def Click(self):
  309.  
  310. if len(self._MyList) == 0:
  311. return
  312.  
  313. if self._PsIndex > len(self._MyList) - 1:
  314. return
  315.  
  316. cur_li = self._MyList[self._PsIndex]
  317.  
  318. if cur_li._MyType == ICON_TYPES["DIR"]:
  319. return
  320.  
  321. if cur_li._MyType == ICON_TYPES["FILE"]:
  322. self._Screen._MsgBox.SetText("Launching")
  323. self._Screen._MsgBox.Draw()
  324. self._Screen.SwapAndShow()
  325. if self._Emulator["FILETYPE"] == "dir":
  326. path = cur_li._Path +"/"+self._Emulator["EXT"][0]
  327. else:
  328. path = cur_li._Path
  329.  
  330. print("Run ",path)
  331.  
  332. if self._Emulator["FILETYPE"] == "dir":
  333. escaped_path = CmdClean(path)
  334. else:
  335. escaped_path = CmdClean(path)
  336.  
  337. custom_config = ""
  338. if self._Emulator["RETRO_CONFIG"] != "" and len(self._Emulator["RETRO_CONFIG"]) > 5:
  339. custom_config = " -c " + self._Emulator["RETRO_CONFIG"]
  340.  
  341. cmdpath = " ".join( (self._Emulator["LAUNCHER"],self._Emulator["ROM_SO"], custom_config, escaped_path))
  342.  
  343. if self._Emulator["ROM_SO"] =="": #empty means No needs for rom so
  344. pygame.event.post( pygame.event.Event(RUNEVT, message=cmdpath))
  345. else:
  346. if FileExists(self._Emulator["ROM_SO"].split(" ")[0]):
  347. pygame.event.post( pygame.event.Event(RUNEVT, message=cmdpath))
  348. else:
  349. self._Screen.PushPage(self._RomSoConfirmDownloadPage)
  350. self._Screen.Draw()
  351. self._Screen.SwapAndShow()
  352.  
  353. return
  354.  
  355. self._Screen.Draw()
  356. self._Screen.SwapAndShow()
  357.  
  358. def ReScan(self):
  359. if self._MyStack.Length() == 0:
  360. self.SyncList(self._Emulator["ROM"])
  361. else:
  362. self.SyncList( self._MyStack.Last() )
  363.  
  364. idx = self._PsIndex
  365. if idx > (len(self._MyList)-1):
  366. idx = len(self._MyList)
  367. if idx > 0:
  368. idx -=1
  369. elif idx == 0:##nothing in _MyList
  370. pass
  371.  
  372. self._PsIndex = idx ## sync PsIndex
  373.  
  374. self.SyncScroll()
  375.  
  376.  
  377. def OnReturnBackCb(self):
  378. self.ReScan()
  379. self._Screen.Draw()
  380. self._Screen.SwapAndShow()
  381.  
  382. def OnLoadCb(self):
  383. self.ReScan()
  384. self._Screen.Draw()
  385. self._Screen.SwapAndShow()
  386.  
  387. def SpeedScroll(self, thekey):
  388. if self._Screen._LastKey == thekey:
  389. self._ScrollStep+=1
  390. if self._ScrollStep >=5:
  391. self._ScrollStep = 5
  392. else:
  393. self._ScrollStep = 1
  394.  
  395. cur_time = time.time()
  396.  
  397. if cur_time - self._Screen._LastKeyDown > 0.3:
  398. self._ScrollStep = 1
  399.  
  400. def KeyDown(self,event):
  401.  
  402. if IsKeyMenuOrB(event.key) or event.key == CurKeys["Left"]:
  403. self.ReturnToUpLevelPage()
  404. self._Screen.Draw()
  405. self._Screen.SwapAndShow()
  406.  
  407.  
  408. if event.key == CurKeys["Up"]:
  409. self.SpeedScroll(event.key)
  410. self.ScrollUp()
  411. self._Screen.Draw()
  412. self._Screen.SwapAndShow()
  413. if event.key == CurKeys["Down"]:
  414. self.SpeedScroll(event.key)
  415. self.ScrollDown()
  416. self._Screen.Draw()
  417. self._Screen.SwapAndShow()
  418.  
  419.  
  420. if IsKeyStartOrA(event.key):
  421. self.Click()
  422.  
  423.  
  424. if event.key == CurKeys["X"]: #Scan current
  425. self.ReScan()
  426. self._Screen.Draw()
  427. self._Screen.SwapAndShow()
  428.  
  429. if event.key == CurKeys["Y"]: #del
  430. if len(self._MyList) == 0:
  431. return
  432.  
  433. cur_li = self._MyList[self._PsIndex]
  434. if cur_li.IsFile():
  435. """
  436. #self._Parent.FavDeleteConfirmPage.SetFileName(cur_li._Path)
  437. #self._Parent.FavDeleteConfirmPage.SetTrashDir(self._Emulator["ROM"])## Fav delete,return to ROM dir,not .Trash
  438. #self._Screen.PushCurPage()
  439. #self._Screen.SetCurPage(self._Parent.FavDeleteConfirmPage)
  440. #self._Screen.Draw()
  441. #self._Screen.SwapAndShow()
  442. """
  443.  
  444. #delete directly without confirm dialog
  445. stats = os.stat(cur_li._Path)
  446. os.chown(cur_li._Path, stats.st_uid,stats.st_uid) ## normally uid and gid should be the same
  447. self._Screen._MsgBox.SetText("Deleting")
  448. self._Screen._MsgBox.Draw()
  449. self._Screen.SwapAndShow()
  450. pygame.time.delay(600)
  451. self.ReScan()
  452. self._Screen.Draw()
  453. self._Screen.SwapAndShow()
  454.  
  455. def Draw(self):
  456. self.ClearCanvas()
  457.  
  458. if len(self._MyList) == 0:
  459. self._Icons["bg"].NewCoord(self._Width/2,self._Height/2)
  460. self._Icons["bg"].Draw()
  461. else:
  462. if len(self._MyList) * ListItem._Height > self._Height:
  463. self._Ps._Width = self._Width - 10
  464. self._Ps.Draw()
  465.  
  466. for i in self._MyList:
  467. if i._PosY > self._Height + self._Height/2:
  468. break
  469.  
  470. if i._PosY < 0:
  471. continue
  472.  
  473. i.Draw()
  474.  
  475. self._Scroller.UpdateSize( len(self._MyList)*ListItem._Height, self._PsIndex*ListItem._Height)
  476. self._Scroller.Draw()
  477.  
  478. else:
  479. self._Ps._Width = self._Width
  480. self._Ps.Draw()
  481. for i in self._MyList:
  482. i.Draw()
Advertisement
RAW Paste Data Copied
Advertisement