fav_list_page.go 11 KB

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