main_screen.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. package UI
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "strings"
  7. //"encoding/json"
  8. "github.com/veandco/go-sdl2/sdl"
  9. "github.com/veandco/go-sdl2/ttf"
  10. "path/filepath"
  11. gotime "time"
  12. "github.com/cuu/gogame/color"
  13. "github.com/cuu/gogame/display"
  14. "github.com/cuu/gogame/draw"
  15. "github.com/cuu/gogame/font"
  16. "github.com/cuu/gogame/rect"
  17. "github.com/cuu/gogame/surface"
  18. "github.com/cuu/gogame/time"
  19. "github.com/cuu/gogame/event"
  20. "github.com/clockworkpi/LauncherGoDev/sysgo"
  21. )
  22. //eg: MainScreen
  23. type ScreenInterface interface {
  24. AppendPage(pg PageInterface)
  25. ClearCanvas()
  26. CurPage() PageInterface
  27. Draw()
  28. ExtraName(name string) string
  29. FartherPages()
  30. Init()
  31. IsEmulatorPackage(dirname string) bool
  32. IsExecPackage(dirname string) bool
  33. IsPluginPackage(dirname string) bool
  34. KeyDown(ev *event.Event)
  35. OnExitCb()
  36. HookExitCb()
  37. PushCurPage()
  38. PushPage(pg PageInterface)
  39. RunEXE(cmdpath string)
  40. SetCurPage(pg PageInterface)
  41. SwapAndShow()
  42. IsWifiConnectedNow()
  43. Refresh()
  44. }
  45. type PluginConfig struct {
  46. NAME string `json:"NAME"` // plugin name,default could be the same as Plugin Folder's name
  47. SO_FILE string `json:"SO_FILE"`
  48. }
  49. type MessageBox struct {
  50. Label
  51. Parent *MainScreen
  52. HWND *sdl.Surface
  53. }
  54. func NewMessageBox() *MessageBox {
  55. m := &MessageBox{}
  56. m.Color = &color.Color{83, 83, 83, 255}
  57. return m
  58. }
  59. func (self *MessageBox) Init(text string, font_obj *ttf.Font, col *color.Color) {
  60. if col != nil {
  61. self.Color = col
  62. }
  63. self.Text = text
  64. self.FontObj = font_obj
  65. self.Width = 0
  66. self.Height = 0
  67. self.CanvasHWND = surface.Surface(self.Parent.Width, self.Parent.Height)
  68. self.HWND = self.Parent.CanvasHWND
  69. }
  70. func (self *MessageBox) SetText(text string) {
  71. self.Text = text
  72. }
  73. func (self *MessageBox) Draw() {
  74. self.Width = 0
  75. self.Height = 0
  76. surface.Fill(self.CanvasHWND, &color.Color{255, 255, 255, 255})
  77. words := strings.Split(self.Text, " ")
  78. space, _ := font.Size(self.FontObj, " ")
  79. max_width := self.Parent.Width - 40
  80. x := 0
  81. y := 0
  82. row_total_width := 0
  83. lines := 0
  84. for _, word := range words {
  85. word_surface := font.Render(self.FontObj, word, true, self.Color, nil)
  86. word_width := int(word_surface.W)
  87. word_height := int(word_surface.H)
  88. row_total_width += word_width
  89. if lines == 0 {
  90. lines += word_height
  91. }
  92. if (row_total_width + space) >= max_width {
  93. x = 0
  94. y += word_height
  95. row_total_width = word_width
  96. lines += word_height
  97. }
  98. dest_rect := rect.Rect(x, y, word_width, word_height)
  99. surface.Blit(self.CanvasHWND, word_surface, &dest_rect, nil)
  100. word_surface.Free()
  101. if len(words) == 1 {
  102. x += word_width
  103. } else {
  104. x += word_width + space
  105. }
  106. if x > self.Width {
  107. self.Width = x
  108. }
  109. if lines >= self.Parent.Height-40 {
  110. break
  111. }
  112. }
  113. self.Height = lines
  114. padding := 5
  115. x = (self.Parent.Width - self.Width) / 2
  116. y = (self.Parent.Height - self.Height) / 2
  117. rect_ := rect.Rect(x-padding, y-padding, self.Width+padding*2, self.Height+padding*2)
  118. if self.HWND != nil {
  119. draw.Rect(self.HWND, &color.Color{255, 255, 255, 255}, &rect_, 0)
  120. rect__ := draw.MidRect(self.Parent.Width/2, self.Parent.Height/2, self.Width, self.Height, Width, Height)
  121. dest_rect := rect.Rect(0, 0, self.Width, self.Height)
  122. surface.Blit(self.HWND, self.CanvasHWND, rect__, &dest_rect)
  123. draw.Rect(self.HWND, &color.Color{0, 0, 0, 255}, &rect_, 1)
  124. }
  125. }
  126. type MainScreen struct {
  127. Widget
  128. Pages []PageInterface
  129. Child []PageInterface
  130. PageMax int
  131. PageIndex int
  132. MyPageStack *PageStack
  133. CurrentPage PageInterface
  134. CanvasHWND *sdl.Surface
  135. HWND *sdl.Surface
  136. TitleBar *TitleBar
  137. FootBar *FootBar
  138. MsgBox *MessageBox
  139. MsgBoxFont *ttf.Font
  140. IconFont *ttf.Font
  141. SkinManager *SkinManager
  142. CounterScreen *CounterScreen
  143. Closed bool
  144. UIPluginList []*UIPlugin
  145. LastKey string
  146. LastKeyDown gotime.Time
  147. updateScreen chan bool
  148. }
  149. func NewMainScreen() *MainScreen {
  150. m := &MainScreen{}
  151. m.PosY = TitleBar_BarHeight + 1
  152. m.Width = Width
  153. m.Height = Height - FootBar_BarHeight - TitleBar_BarHeight - 1
  154. m.MyPageStack = NewPageStack()
  155. m.MsgBoxFont = Fonts["veramono20"]
  156. m.IconFont = Fonts["varela15"]
  157. m.Closed = false
  158. m.updateScreen = make(chan bool,1)
  159. return m
  160. }
  161. func (self *MainScreen) Init() {
  162. self.CanvasHWND = surface.Surface(self.Width, self.Height)
  163. self.MsgBox = NewMessageBox()
  164. self.MsgBox.Parent = self
  165. self.MsgBox.Init(" ", self.MsgBoxFont, &color.Color{83, 83, 83, 255})
  166. self.SkinManager = NewSkinManager()
  167. self.SkinManager.Init()
  168. self.CounterScreen = NewCounterScreen()
  169. self.CounterScreen.HWND = self.HWND
  170. self.CounterScreen.Init()
  171. //self.GenList() // load predefined plugin list,ready to be injected ,or ,as a .so for dynamic loading
  172. go func() {
  173. self.RefreshLoop()
  174. }()
  175. }
  176. func (self *MainScreen) FartherPages() { // right after ReadTheDirIntoPages
  177. self.PageMax = len(self.Pages)
  178. for i := 0; i < self.PageMax; i++ {
  179. self.Pages[i].SetIndex(i)
  180. self.Pages[i].SetCanvasHWND(self.CanvasHWND)
  181. self.Pages[i].UpdateIconNumbers() // IconNumbers always == len(Pages[i].Icons)
  182. self.Pages[i].SetScreen(self)
  183. self.Pages[i].Adjust()
  184. if self.Pages[i].GetIconNumbers() > 1 {
  185. self.Pages[i].SetPsIndex(1)
  186. self.Pages[i].SetIconIndex(1)
  187. }
  188. }
  189. self.CurrentPage = self.Pages[self.PageIndex]
  190. self.CurrentPage.SetOnShow(true)
  191. }
  192. func (self *MainScreen) CurPage() PageInterface {
  193. return self.CurrentPage
  194. }
  195. func (self *MainScreen) PushCurPage() {
  196. self.MyPageStack.Push(self.CurrentPage)
  197. }
  198. func (self *MainScreen) SetCurPage(pg PageInterface) {
  199. self.CurrentPage = pg
  200. pg.OnLoadCb()
  201. }
  202. func (self *MainScreen) PushPage(pg PageInterface) {
  203. self.PushCurPage()
  204. self.SetCurPage(pg)
  205. }
  206. func (self *MainScreen) AppendPage(pg PageInterface) {
  207. self.Pages = append(self.Pages, pg)
  208. }
  209. func (self *MainScreen) ClearCanvas() {
  210. surface.Fill(self.CanvasHWND, &color.Color{255, 255, 255, 255})
  211. }
  212. func (self *MainScreen) SwapAndShow() {
  213. if self.HWND != nil {
  214. rect_ := rect.Rect(self.PosX, self.PosY, self.Width, self.Height)
  215. surface.Blit(self.HWND, self.CanvasHWND, &rect_, nil)
  216. }
  217. display.Flip()
  218. }
  219. func (self *MainScreen) ExtraName(name string) string {
  220. parts := strings.Split(name, "_")
  221. if len(parts) > 1 {
  222. return parts[1]
  223. } else if len(parts) == 1 {
  224. return parts[0]
  225. } else {
  226. return name
  227. }
  228. }
  229. //ExecPackage is all-in-one folder ,Name.sh,Name.png,etc
  230. func (self *MainScreen) IsExecPackage(dirname string) bool {
  231. files, err := ioutil.ReadDir(dirname)
  232. if err != nil {
  233. log.Println(err)
  234. return false
  235. }
  236. bname := filepath.Base(dirname)
  237. bname = self.ExtraName(bname)
  238. for _, v := range files {
  239. if v.Name() == bname+".sh" {
  240. return true
  241. }
  242. }
  243. return false
  244. }
  245. func (self *MainScreen) IsPluginPackage(dirname string) bool {
  246. ret := false
  247. files, err := ioutil.ReadDir(dirname)
  248. if err != nil {
  249. log.Println(err)
  250. return false
  251. }
  252. for _, f := range files {
  253. if f.IsDir() {
  254. //pass
  255. } else {
  256. if strings.HasSuffix(f.Name(), Plugin_flag) == true {
  257. ret = true
  258. break
  259. }
  260. }
  261. }
  262. return ret
  263. }
  264. func (self *MainScreen) IsEmulatorPackage(dirname string) bool {
  265. ret := false
  266. files, err := ioutil.ReadDir(dirname)
  267. if err != nil {
  268. log.Println(err)
  269. return false
  270. }
  271. for _, f := range files {
  272. if f.IsDir() {
  273. //pass
  274. } else {
  275. if strings.HasSuffix(f.Name(), Emulator_flag) == true {
  276. ret = true
  277. break
  278. }
  279. }
  280. }
  281. return ret
  282. }
  283. func (self *MainScreen) IsWifiConnectedNow() bool {
  284. cli := fmt.Sprintf("ip -4 addr show %s | grep -oP '(?<=inet\\s)\\d+(\\.\\d+){3}'", sysgo.WifiDev)
  285. out := System(cli)
  286. if len(out) < 6 {
  287. return false
  288. }
  289. return true
  290. }
  291. func (self *MainScreen) GetWirelessIP() string {
  292. cli := fmt.Sprintf("ip -4 addr show %s | grep -oP '(?<=inet\\s)\\d+(\\.\\d+){3}'", sysgo.WifiDev)
  293. out := SystemTrim(cli)
  294. return out
  295. }
  296. func (self *MainScreen) RunEXE(cmdpath string) {
  297. self.DrawRun()
  298. self.SwapAndShow()
  299. time.BlockDelay(1000)
  300. cmdpath = strings.Trim(cmdpath, " ")
  301. cmdpath = CmdClean(cmdpath)
  302. event.Post(RUNEVT, cmdpath)
  303. }
  304. func (self *MainScreen) HookExitCb( page PageInterface) {
  305. self.Child = append(self.Child,page)
  306. }
  307. func (self *MainScreen) OnExitCb() {
  308. PageLen := len(self.Child)
  309. for i := 0; i < PageLen; i++ {
  310. self.Child[i].OnExitCb()
  311. }
  312. }
  313. func (self *MainScreen) KeyDown(ev *event.Event) {
  314. if ev.Data["Key"] == "T" {
  315. self.DrawRun()
  316. self.SwapAndShow()
  317. return
  318. }
  319. if ev.Data["Key"] == "Space" {
  320. self.Draw()
  321. self.SwapAndShow()
  322. }
  323. self.CurrentPage.KeyDown(ev)
  324. self.LastKey = ev.Data["Key"]
  325. }
  326. func (self *MainScreen) ShowMsg(content string, blocktime int) {
  327. self.MsgBox.SetText(content)
  328. self.MsgBox.Draw()
  329. self.SwapAndShow()
  330. if blocktime > 0 {
  331. time.BlockDelay(blocktime)
  332. self.CurrentPage.Draw()
  333. self.SwapAndShow()
  334. }
  335. }
  336. func (self *MainScreen) DrawRun() {
  337. self.MsgBox.SetText("Launching....")
  338. self.MsgBox.Draw()
  339. }
  340. func (self *MainScreen) Draw() {
  341. if self.CurrentPage != nil {
  342. self.CurrentPage.Draw()
  343. }
  344. if self.TitleBar != nil {
  345. //every plugin_init should not do any Draw actions since CurrentPage might be nil at that time
  346. self.TitleBar.Draw(self.CurrentPage.GetName())
  347. }
  348. if self.FootBar != nil {
  349. self.FootBar.SetLabelTexts(self.CurrentPage.GetFootMsg())
  350. self.FootBar.Draw()
  351. }
  352. }
  353. func (self *MainScreen) Refresh() {
  354. self.updateScreen <- true
  355. }
  356. func (self *MainScreen) RefreshLoop() {
  357. L:
  358. for {
  359. select {
  360. case v:= <- self.updateScreen:
  361. if v == true {
  362. self.Draw()
  363. self.SwapAndShow()
  364. }
  365. if v== false {
  366. break L
  367. }
  368. }
  369. }
  370. }