mainscreen.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package main
  2. import (
  3. //"os"
  4. "fmt"
  5. "log"
  6. "io/ioutil"
  7. // "strconv"
  8. "strings"
  9. // "runtime"
  10. "path/filepath"
  11. //os/exec"
  12. "encoding/json"
  13. "sort"
  14. "github.com/go-ini/ini"
  15. "github.com/yookoala/realpath"
  16. "github.com/clockworkpi/LauncherGoDev/sysgo/UI"
  17. "github.com/clockworkpi/LauncherGoDev/sysgo/UI/Emulator"
  18. "github.com/clockworkpi/LauncherGoDev/Menu/GameShell/10_Settings"
  19. "github.com/clockworkpi/LauncherGoDev/Menu/GameShell/98_TinyCloud"
  20. "github.com/clockworkpi/LauncherGoDev/Menu/GameShell/99_PowerOFF"
  21. )
  22. var (
  23. UIPluginList = []*UI.UIPlugin {
  24. &UI.UIPlugin{1,"", "Menu/GameShell/10_Settings", "Settings", &Settings.APIOBJ},
  25. &UI.UIPlugin{1,"", "Menu/GameShell/98_TinyCloud", "TinyCloud", &TinyCloud.APIOBJ},
  26. &UI.UIPlugin{1,"", "Menu/GameShell/99_PowerOFF", "PowerOFF", &PowerOFF.APIOBJ},
  27. }
  28. )
  29. func ReunionPagesIcons(self *UI.MainScreen) {
  30. type Tup struct {
  31. FileName string
  32. OrigIdx int
  33. }
  34. var tmp []Tup
  35. for i,p := range self.Pages {
  36. p_icons := p.GetIcons()
  37. for i,x := range p_icons {
  38. var t Tup
  39. if x.GetFileName() != ""{
  40. if strings.Contains(x.GetFileName(),"_") == false {
  41. t = Tup{"98_"+x.GetFileName(),i}
  42. }else {
  43. t = Tup{x.GetFileName(),i}
  44. }
  45. }else{
  46. t = Tup{"",i}
  47. }
  48. tmp = append(tmp,t)
  49. }
  50. sort.Slice(tmp, func(i, j int) bool { return tmp[i].FileName < tmp[j].FileName })
  51. //fmt.Println(tmp)
  52. var retro_games_idx []int
  53. retro_games_dir := "20_Retro Games"
  54. for _,x := range tmp {
  55. if strings.HasPrefix(x.FileName, retro_games_dir) {
  56. retro_games_idx = append(retro_games_idx,x.OrigIdx)
  57. }
  58. }
  59. if len(retro_games_idx) > 1 {
  60. p_icons_0_link_page := p_icons[retro_games_idx[0]].GetLinkPage().(*UI.Page)
  61. for i:=1;i<len(retro_games_idx);i++ {
  62. icons_other_page := p_icons[retro_games_idx[i]].GetLinkPage().GetIcons()
  63. p_icons_0_link_page.Icons = append(p_icons_0_link_page.Icons, icons_other_page...)
  64. }
  65. var tmpswap []Tup
  66. for i,x := range tmp {
  67. if strings.HasPrefix(x.FileName,retro_games_dir) == false{
  68. tmpswap = append(tmpswap,x)
  69. }
  70. if strings.HasPrefix(x.FileName,retro_games_dir) == true && i==retro_games_idx[0] {
  71. tmpswap = append(tmpswap,x)
  72. }
  73. }
  74. tmp = tmpswap
  75. }
  76. var new_icons []UI.IconItemInterface
  77. for _,x := range tmp {
  78. new_icons = append(new_icons, p_icons[x.OrigIdx])
  79. }
  80. self.Pages[i].(*UI.Page).Icons = new_icons
  81. }
  82. }
  83. func ReadTheDirIntoPages(self *UI.MainScreen, _dir string, pglevel int, cur_page UI.PageInterface) {
  84. if UI.FileExists(_dir) == false && UI.IsDirectory(_dir) == false {
  85. return
  86. }
  87. files,err := ioutil.ReadDir(_dir)
  88. if err != nil {
  89. log.Fatal(err)
  90. return
  91. }
  92. for _,f := range files { // already sorted
  93. if UI.IsDirectory( _dir +"/"+f.Name()) {
  94. if pglevel == 0 {
  95. page := UI.NewPage()
  96. page.Name = self.ExtraName(f.Name())
  97. self.Pages = append(self.Pages, page)
  98. ReadTheDirIntoPages(self,_dir+"/"+f.Name(),pglevel+1, self.Pages[ len(self.Pages) - 1] )
  99. }else{ // on cur_page now
  100. i2:= self.ExtraName(f.Name())
  101. iconitem := UI.NewIconItem()
  102. iconitem.FileName = f.Name()
  103. iconitem.AddLabel(i2,self.IconFont)
  104. if UI.FileExists(filepath.Join(_dir,f.Name(),i2+".png")) { //eg: 20_Prog/Prog.png , cut 20_
  105. iconitem.ImageName = filepath.Join(_dir,f.Name(),i2+".png")
  106. }else if UI.FileExists( UI.SkinMap(_dir+"/"+i2+".png")) {
  107. iconitem.ImageName = UI.SkinMap(_dir+"/"+i2+".png")
  108. }else {
  109. //fmt.Println( UI.SkinMap(_dir+"/"+i2+".png") )
  110. untitled := UI.NewUntitledIcon()
  111. untitled.Init()
  112. if len(i2) > 1 {
  113. untitled.SetWords(string(i2[0]),string(i2[1]))
  114. }else if len(i2) == 1 {
  115. untitled.SetWords(string(i2[0]),string(i2[0]))
  116. }else {
  117. untitled.SetWords("G","s")
  118. }
  119. iconitem.ImgSurf = untitled.Surface()
  120. iconitem.ImageName = ""
  121. }
  122. if self.IsPluginPackage(_dir+"/"+f.Name()) {
  123. p_c := UI.PluginConfig{}
  124. dat, err := ioutil.ReadFile(_dir+"/"+f.Name()+"/" +UI.Plugin_flag)
  125. UI.ShowErr(err)
  126. err = json.Unmarshal(dat, &p_c)
  127. if err == nil {
  128. if p_c.NAME == "" {
  129. p_c.NAME = f.Name()
  130. }
  131. so_file := filepath.Join(_dir,f.Name(),p_c.SO_FILE)
  132. if UI.FileExists(so_file) && UI.IsAFile(so_file) {
  133. pi,err := UI.LoadPlugin(_dir+"/"+f.Name()+"/"+p_c.SO_FILE)
  134. UI.Assert(err)
  135. iconitem.CmdInvoke = UI.InitPlugin(pi,self)
  136. if iconitem.CmdInvoke != nil {
  137. iconitem.MyType = UI.ICON_TYPES["FUNC"]
  138. iconitem.CmdPath = f.Name()
  139. cur_page.AppendIcon(iconitem)
  140. }
  141. } else {
  142. for _,v := range UIPluginList {
  143. if v.LabelText == p_c.NAME {
  144. v.EmbInterface.Init(self)
  145. iconitem.CmdInvoke = v.EmbInterface
  146. if iconitem.CmdInvoke != nil {
  147. iconitem.MyType = UI.ICON_TYPES["FUNC"]
  148. iconitem.CmdPath = f.Name()
  149. cur_page.AppendIcon(iconitem)
  150. }
  151. }
  152. }
  153. }
  154. }
  155. //Init it
  156. }else if self.IsEmulatorPackage(_dir+"/"+f.Name()) {
  157. a_c := Emulator.ActionConfig{}
  158. a_c.FILETYPE="file"
  159. a_c.TITLE = "Game"
  160. cfg, err := ini.Load(_dir+"/"+f.Name()+"/" +UI.Emulator_flag)
  161. UI.ShowErr(err)
  162. err = cfg.MapTo(&a_c)
  163. if err == nil {
  164. //fmt.Println(a_c)
  165. if UI.FileExists(filepath.Join(_dir,f.Name(),"retroarch-local.cfg")) {
  166. a_c.RETRO_CONFIG = UI.CmdClean( filepath.Join(_dir,f.Name(),"retroarch-local.cfg") )
  167. fmt.Println("a local retroarch cfg: ",a_c.RETRO_CONFIG)
  168. }
  169. em := Emulator.NewMyEmulator()
  170. em.EmulatorConfig = &a_c
  171. em.Init(self)
  172. iconitem.CmdInvoke = em
  173. if iconitem.CmdInvoke != nil {
  174. iconitem.MyType = UI.ICON_TYPES["Emulator"]
  175. iconitem.CmdPath = f.Name()
  176. cur_page.AppendIcon(iconitem)
  177. }
  178. }else {
  179. fmt.Println("ReadTheDirIntoPages EmulatorConfig ",err)
  180. }
  181. }else if self.IsExecPackage(_dir+"/"+f.Name()) {
  182. iconitem.MyType = UI.ICON_TYPES["EXE"]
  183. rel_path,err := realpath.Realpath( filepath.Join(_dir,f.Name(),i2+".sh"))
  184. if err != nil {
  185. rel_path,_ = filepath.Abs(filepath.Join(_dir,f.Name(),i2+".sh"))
  186. }
  187. iconitem.CmdPath = rel_path
  188. UI.MakeExecutable( iconitem.CmdPath )
  189. cur_page.AppendIcon(iconitem)
  190. }else {
  191. iconitem.MyType = UI.ICON_TYPES["DIR"]
  192. linkpage := UI.NewPage()
  193. linkpage.Name = i2
  194. iconitem.LinkPage = linkpage
  195. cur_page.AppendIcon(iconitem)
  196. ReadTheDirIntoPages(self,_dir+"/"+f.Name(),pglevel+1, iconitem.LinkPage)
  197. }
  198. }
  199. } else if UI.IsAFile(_dir+"/"+f.Name()) && (pglevel > 0) {
  200. if strings.HasSuffix(strings.ToLower(f.Name()),UI.IconExt) {
  201. i2 := self.ExtraName(f.Name())
  202. iconitem := UI.NewIconItem()
  203. rel_path,err := realpath.Realpath( _dir+"/"+f.Name() )
  204. if err != nil {
  205. rel_path,_ = filepath.Abs(_dir+"/"+f.Name())
  206. }
  207. iconitem.CmdPath = rel_path
  208. iconitem.FileName = f.Name()
  209. UI.MakeExecutable( iconitem.CmdPath )
  210. iconitem.MyType = UI.ICON_TYPES["EXE"]
  211. if UI.FileExists( UI.SkinMap( _dir+"/"+ UI.ReplaceSuffix(i2,"png"))) {
  212. iconitem.ImageName = UI.SkinMap( _dir+"/"+ UI.ReplaceSuffix(i2,"png"))
  213. }else {
  214. untitled:= UI.NewUntitledIcon()
  215. untitled.Init()
  216. if len(i2) > 1 {
  217. untitled.SetWords(string(i2[0]),string(i2[1]))
  218. }else if len(i2) == 1 {
  219. untitled.SetWords(string(i2[0]),string(i2[0]))
  220. }else {
  221. untitled.SetWords("G","s")
  222. }
  223. iconitem.ImgSurf = untitled.Surface()
  224. iconitem.ImageName = ""
  225. }
  226. iconitem.AddLabel(strings.Split(i2,".")[0], self.IconFont)
  227. iconitem.LinkPage = nil
  228. cur_page.AppendIcon(iconitem)
  229. }
  230. }
  231. }
  232. }