mainscreen.go 8.3 KB

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