fav_list_page.go 11 KB

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