rom_list_page.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. package Emulator
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "regexp"
  7. "path/filepath"
  8. "os/exec"
  9. "errors"
  10. "github.com/veandco/go-sdl2/ttf"
  11. "github.com/cuu/gogame/time"
  12. "github.com/cuu/gogame/color"
  13. "github.com/cuu/gogame/event"
  14. "github.com/clockworkpi/LauncherGoDev/sysgo/UI"
  15. )
  16. type RomListPage struct {
  17. UI.Page
  18. Icons map[string]UI.IconItemInterface
  19. ListFont *ttf.Font
  20. MyStack *UI.FolderStack
  21. EmulatorConfig *ActionConfig
  22. RomSoConfirmDownloadPage *RomSoConfirmPage
  23. MyList []UI.ListItemInterface
  24. BGwidth int
  25. BGheight int //70
  26. Scroller *UI.ListScroller
  27. Scrolled int
  28. Leader *MyEmulator
  29. }
  30. func NewRomListPage() *RomListPage {
  31. p := &RomListPage{}
  32. p.PageIconMargin = 20
  33. p.SelectedIconTopOffset = 20
  34. p.EasingDur = 10
  35. p.Align = UI.ALIGN["SLeft"]
  36. p.FootMsg = [5]string{ "Nav","Scan","Del","AddFav","Run" }
  37. p.Icons=make(map[string]UI.IconItemInterface)
  38. p.ListFont = UI.Fonts["notosanscjk15"]
  39. p.MyStack = UI.NewFolderStack()
  40. p.BGwidth = 56
  41. p.BGheight = 70
  42. return p
  43. }
  44. func (self *RomListPage) GetMyList() []UI.ListItemInterface {
  45. return self.MyList
  46. }
  47. func (self *RomListPage) GetMapIcons() map[string]UI.IconItemInterface {
  48. return self.Icons
  49. }
  50. func (self *RomListPage) GetEmulatorConfig() *ActionConfig {
  51. return self.EmulatorConfig
  52. }
  53. func (self *RomListPage) GeneratePathList(path string) ([]map[string]string,error) {
  54. if UI.IsDirectory(path) == false {
  55. return nil,errors.New("Path is not a folder")
  56. }
  57. var ret []map[string]string
  58. file_paths,err := filepath.Glob(path+"/*")//sorted
  59. if err != nil {
  60. fmt.Println(err)
  61. return ret,err
  62. }
  63. for _,v := range file_paths {
  64. dirmap := make(map[string]string)
  65. if UI.IsDirectory(v) && self.EmulatorConfig.FILETYPE == "dir" { // like DOSBOX
  66. gameshell_bat := self.EmulatorConfig.EXT[0]
  67. if UI.GetGid(v) == FavGID { // skip fav roms
  68. continue
  69. }
  70. if UI.FileExists( filepath.Join(v,gameshell_bat)) == true {
  71. dirmap["gamedir"] = v
  72. ret = append(ret,dirmap)
  73. }
  74. }
  75. if UI.IsAFile(v) && self.EmulatorConfig.FILETYPE == "file" {
  76. if UI.GetGid(v) == FavGID {
  77. continue
  78. }
  79. bname := filepath.Base(v)
  80. if len(bname) > 1 {
  81. is_excluded := false
  82. for _,exclude_pattern := range self.EmulatorConfig.EXCLUDE {
  83. if matched, err := regexp.MatchString(exclude_pattern,bname); err == nil {
  84. if matched == true {
  85. is_excluded = true
  86. break
  87. }
  88. }
  89. }
  90. if is_excluded == false {
  91. pieces := strings.Split(bname,".")
  92. if len(pieces) > 1 {
  93. pieces_ext := strings.ToLower( pieces[len(pieces)-1])
  94. for _,u := range self.EmulatorConfig.EXT {
  95. if pieces_ext == u {
  96. dirmap["file"] = v
  97. ret = append(ret,dirmap)
  98. break
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. return ret,nil
  107. }
  108. func (self *RomListPage) SyncList( path string ) {
  109. alist,err := self.GeneratePathList(path)
  110. if err != nil {
  111. fmt.Println(err)
  112. return
  113. }
  114. self.MyList = nil
  115. start_x := 0
  116. start_y := 0
  117. hasparent := 0
  118. if self.MyStack.Length() > 0 {
  119. hasparent = 1
  120. li := NewEmulatorListItem()
  121. li.Parent = self
  122. li.PosX = start_x
  123. li.PosY = start_y
  124. li.Width = UI.Width
  125. li.Fonts["normal"] = self.ListFont
  126. li.MyType = UI.ICON_TYPES["DIR"]
  127. li.Init("[..]")
  128. self.MyList = append(self.MyList,li)
  129. }
  130. for i,v := range alist {
  131. li := NewEmulatorListItem()
  132. li.Parent = self
  133. li.PosX = start_x
  134. li.PosY = start_y + (i+hasparent)*li.Height
  135. li.Width = UI.Width
  136. li.Fonts["normal"] = self.ListFont
  137. li.MyType = UI.ICON_TYPES["FILE"]
  138. init_val := "NoName"
  139. if val, ok := v["directory"]; ok {
  140. li.MyType = UI.ICON_TYPES["DIR"]
  141. init_val = val
  142. }
  143. if val, ok := v["file"]; ok {
  144. init_val = val
  145. }
  146. if val, ok := v["gamedir"]; ok {
  147. init_val = val
  148. }
  149. li.Init(init_val)
  150. self.MyList = append(self.MyList,li)
  151. }
  152. }
  153. func (self *RomListPage) Init() {
  154. self.PosX = self.Index *self.Screen.Width
  155. self.Width = self.Screen.Width
  156. self.Height = self.Screen.Height
  157. self.CanvasHWND = self.Screen.CanvasHWND
  158. ps := UI.NewInfoPageSelector()
  159. ps.Width = UI.Width - 12
  160. ps.PosX = 2
  161. ps.Parent = self
  162. self.Ps = ps
  163. self.PsIndex = 0
  164. self.MyStack.SetRootPath(self.EmulatorConfig.ROM)
  165. self.SyncList( self.EmulatorConfig.ROM )
  166. err := os.MkdirAll( self.EmulatorConfig.ROM+"/.Trash", 0700)
  167. if err != nil {
  168. panic(err)
  169. }
  170. err = os.MkdirAll( self.EmulatorConfig.ROM+"/.Fav", 0700)
  171. if err != nil {
  172. panic(err)
  173. }
  174. icon_for_list := UI.NewMultiIconItem()
  175. icon_for_list.ImgSurf = UI.MyIconPool.GetImgSurf("sys")
  176. icon_for_list.MyType = UI.ICON_TYPES["STAT"]
  177. icon_for_list.Parent = self
  178. icon_for_list.Adjust(0,0,18,18,0)
  179. self.Icons["sys"] = icon_for_list
  180. bgpng := UI.NewIconItem()
  181. bgpng.ImgSurf = UI.MyIconPool.GetImgSurf("empty")
  182. bgpng.MyType = UI.ICON_TYPES["STAT"]
  183. bgpng.Parent = self
  184. bgpng.AddLabel("Please upload data over Wi-Fi",UI.Fonts["varela22"])
  185. bgpng.SetLabelColor(&color.Color{204,204,204,255} )
  186. bgpng.Adjust(0,0,self.BGwidth,self.BGheight,0)
  187. self.Icons["bg"] = bgpng
  188. self.Scroller = UI.NewListScroller()
  189. self.Scroller.Parent = self
  190. self.Scroller.PosX = self.Width - 10
  191. self.Scroller.PosY = 2
  192. self.Scroller.Init()
  193. rom_so_confirm_page := NewRomSoConfirmPage()
  194. rom_so_confirm_page.Screen = self.Screen
  195. rom_so_confirm_page.Name = "Download Confirm"
  196. rom_so_confirm_page.Parent = self
  197. rom_so_confirm_page.Init()
  198. self.RomSoConfirmDownloadPage = rom_so_confirm_page
  199. }
  200. func (self *RomListPage) ScrollUp() {
  201. if len(self.MyList) == 0 {
  202. return
  203. }
  204. self.PsIndex -=1
  205. if self.PsIndex < 0 {
  206. self.PsIndex = 0
  207. }
  208. cur_li := self.MyList[self.PsIndex]
  209. x,y := cur_li.Coord()
  210. _,h := cur_li.Size()
  211. if y < 0 {
  212. for i,_ := range self.MyList{
  213. x,y = self.MyList[i].Coord()
  214. _, h = self.MyList[i].Size()
  215. self.MyList[i].NewCoord(x,y + h)
  216. }
  217. self.Scrolled +=1
  218. }
  219. }
  220. func (self *RomListPage) ScrollDown(){
  221. if len(self.MyList) == 0 {
  222. return
  223. }
  224. self.PsIndex +=1
  225. if self.PsIndex >= len(self.MyList) {
  226. self.PsIndex = len(self.MyList) - 1
  227. }
  228. cur_li := self.MyList[self.PsIndex]
  229. x,y := cur_li.Coord()
  230. _,h := cur_li.Size()
  231. if y+ h > self.Height {
  232. for i,_ := range self.MyList{
  233. x,y = self.MyList[i].Coord()
  234. _, h = self.MyList[i].Size()
  235. self.MyList[i].NewCoord(x,y - h)
  236. }
  237. self.Scrolled -=1
  238. }
  239. }
  240. func (self *RomListPage) SyncScroll() {
  241. if self.Scrolled == 0 {
  242. return
  243. }
  244. if self.PsIndex < len(self.MyList) {
  245. cur_li := self.MyList[self.PsIndex]
  246. x, y := cur_li.Coord()
  247. _, h := cur_li.Size()
  248. if self.Scrolled > 0 {
  249. if y < 0 {
  250. for i,_ := range self.MyList{
  251. x,y = self.MyList[i].Coord()
  252. _, h = self.MyList[i].Size()
  253. self.MyList[i].NewCoord(x, y + self.Scrolled*h)
  254. }
  255. }
  256. }else if self.Scrolled < 0 {
  257. if y + h > self.Height{
  258. for i,_ := range self.MyList{
  259. x,y = self.MyList[i].Coord()
  260. _, h = self.MyList[i].Size()
  261. self.MyList[i].NewCoord(x, y + self.Scrolled*h)
  262. }
  263. }
  264. }
  265. }
  266. }
  267. func (self *RomListPage) Click() {
  268. if len(self.MyList) == 0 {
  269. return
  270. }
  271. if self.PsIndex > len(self.MyList) - 1 {
  272. return
  273. }
  274. cur_li := self.MyList[self.PsIndex]
  275. if cur_li.(*EmulatorListItem).MyType == UI.ICON_TYPES["DIR"] {
  276. if cur_li.(*EmulatorListItem).Path == "[..]"{
  277. self.MyStack.Pop()
  278. self.SyncList(self.MyStack.Last())
  279. self.PsIndex = 0
  280. }else{
  281. self.MyStack.Push(self.MyList[self.PsIndex].(*EmulatorListItem).Path)
  282. self.SyncList(self.MyStack.Last())
  283. self.PsIndex = 0
  284. }
  285. }
  286. if cur_li.(*EmulatorListItem).MyType == UI.ICON_TYPES["FILE"] {
  287. self.Screen.MsgBox.SetText("Launching")
  288. self.Screen.MsgBox.Draw()
  289. self.Screen.SwapAndShow()
  290. path := ""
  291. if self.EmulatorConfig.FILETYPE == "dir" {
  292. path = filepath.Join(cur_li.(*EmulatorListItem).Path,self.EmulatorConfig.EXT[0])
  293. }else{
  294. path = cur_li.(*EmulatorListItem).Path
  295. }
  296. fmt.Println("Run ",path)
  297. escaped_path := UI.CmdClean(path)
  298. if self.EmulatorConfig.FILETYPE == "dir" {
  299. escaped_path = UI.CmdClean(path)
  300. }
  301. custom_config := ""
  302. if self.EmulatorConfig.RETRO_CONFIG != "" && len(self.EmulatorConfig.RETRO_CONFIG) > 5 {
  303. custom_config = " -c " + self.EmulatorConfig.RETRO_CONFIG
  304. }
  305. partsofpath := []string{self.EmulatorConfig.LAUNCHER,self.EmulatorConfig.ROM_SO,custom_config,escaped_path}
  306. cmdpath := strings.Join( partsofpath," ")
  307. if self.EmulatorConfig.ROM_SO =="" { //empty means No needs for rom so
  308. event.Post(UI.RUNEVT,cmdpath)
  309. }else{
  310. if UI.FileExists(self.EmulatorConfig.ROM_SO) == true {
  311. event.Post(UI.RUNEVT,cmdpath)
  312. } else {
  313. self.Screen.PushCurPage()
  314. self.Screen.SetCurPage( self.RomSoConfirmDownloadPage)
  315. self.Screen.Draw()
  316. self.Screen.SwapAndShow()
  317. }
  318. }
  319. return
  320. }
  321. self.Screen.Draw()
  322. self.Screen.SwapAndShow()
  323. }
  324. func (self *RomListPage) ReScan() {
  325. //fmt.Println("RomListPage ReScan ",self.EmulatorConfig.ROM)
  326. if self.MyStack.Length() == 0 {
  327. self.SyncList(self.EmulatorConfig.ROM)
  328. }else{
  329. self.SyncList(self.MyStack.Last())
  330. }
  331. idx := self.PsIndex
  332. if idx > len(self.MyList) - 1 {
  333. idx = len(self.MyList)
  334. if idx > 0 {
  335. idx -= 1
  336. }else if idx == 0 {
  337. //nothing in MyList
  338. }
  339. }
  340. self.PsIndex = idx //sync PsIndex
  341. self.SyncScroll()
  342. }
  343. func (self *RomListPage) OnReturnBackCb() {
  344. self.ReScan()
  345. self.Screen.Draw()
  346. self.Screen.SwapAndShow()
  347. }
  348. func (self *RomListPage) KeyDown(ev *event.Event) {
  349. if ev.Data["Key"] == UI.CurKeys["Menu"]{
  350. self.ReturnToUpLevelPage()
  351. self.Screen.Draw()
  352. self.Screen.SwapAndShow()
  353. }
  354. if ev.Data["Key"] == UI.CurKeys["Right"]{
  355. self.Screen.PushCurPage()
  356. self.Screen.SetCurPage(self.Leader.FavPage)
  357. self.Screen.Draw()
  358. self.Screen.SwapAndShow()
  359. }
  360. if ev.Data["Key"] == UI.CurKeys["Up"]{
  361. self.ScrollUp()
  362. self.Screen.Draw()
  363. self.Screen.SwapAndShow()
  364. }
  365. if ev.Data["Key"] == UI.CurKeys["Down"] {
  366. self.ScrollDown()
  367. self.Screen.Draw()
  368. self.Screen.SwapAndShow()
  369. }
  370. if ev.Data["Key"] == UI.CurKeys["Enter"] {
  371. self.Click()
  372. }
  373. if ev.Data["Key"] == UI.CurKeys["A"] {
  374. if len(self.MyList) == 0{
  375. return
  376. }
  377. cur_li := self.MyList[self.PsIndex]
  378. if cur_li.(*EmulatorListItem).IsFile() {
  379. cmd := exec.Command("chgrp", FavGname, UI.CmdClean(cur_li.(*EmulatorListItem).Path))
  380. err := cmd.Run()
  381. if err != nil {
  382. fmt.Println(err)
  383. }
  384. self.Screen.MsgBox.SetText("Add to favourite list")
  385. self.Screen.MsgBox.Draw()
  386. self.Screen.SwapAndShow()
  387. time.BlockDelay(600)
  388. self.ReScan()
  389. self.Screen.Draw()
  390. self.Screen.SwapAndShow()
  391. }
  392. }
  393. if ev.Data["Key"] == UI.CurKeys["X"] { //Scan current
  394. self.ReScan()
  395. self.Screen.Draw()
  396. self.Screen.SwapAndShow()
  397. }
  398. if ev.Data["Key"] == UI.CurKeys["Y"] {// del
  399. if len(self.MyList) == 0 {
  400. return
  401. }
  402. cur_li := self.MyList[self.PsIndex]
  403. if cur_li.(*EmulatorListItem).IsFile() {
  404. self.Leader.DeleteConfirmPage.SetFileName(cur_li.(*EmulatorListItem).Path)
  405. self.Leader.DeleteConfirmPage.SetTrashDir(filepath.Join(self.EmulatorConfig.ROM,"/.Trash") )
  406. self.Screen.PushCurPage()
  407. self.Screen.SetCurPage(self.Leader.DeleteConfirmPage)
  408. self.Screen.Draw()
  409. self.Screen.SwapAndShow()
  410. }
  411. }
  412. }
  413. func (self *RomListPage) Draw() {
  414. self.ClearCanvas()
  415. if len(self.MyList) == 0 {
  416. self.Icons["bg"].NewCoord(self.Width/2,self.Height/2)
  417. self.Icons["bg"].Draw()
  418. }else{
  419. _,h := self.Ps.Size()
  420. if len(self.MyList) * UI.HierListItemDefaultHeight > self.Height {
  421. self.Ps.NewSize(self.Width - 10,h)
  422. self.Ps.Draw()
  423. for _,v := range self.MyList {
  424. _,y := v.Coord()
  425. if y > (self.Height + self.Height/2) {
  426. break
  427. }
  428. v.Draw()
  429. }
  430. self.Scroller.UpdateSize( len(self.MyList)*UI.HierListItemDefaultHeight,
  431. self.PsIndex*UI.HierListItemDefaultHeight)
  432. self.Scroller.Draw()
  433. }else {
  434. self.Ps.NewSize(self.Width,h)
  435. self.Ps.Draw()
  436. for _,v := range self.MyList {
  437. v.Draw()
  438. }
  439. }
  440. }
  441. }