mainscreen.go 7.1 KB

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