main_screen.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. sdl.Do(func() {
  174. self.RefreshLoop()
  175. })
  176. }()
  177. }
  178. func (self *MainScreen) FartherPages() { // right after ReadTheDirIntoPages
  179. self.PageMax = len(self.Pages)
  180. for i := 0; i < self.PageMax; i++ {
  181. self.Pages[i].SetIndex(i)
  182. self.Pages[i].SetCanvasHWND(self.CanvasHWND)
  183. self.Pages[i].UpdateIconNumbers() // IconNumbers always == len(Pages[i].Icons)
  184. self.Pages[i].SetScreen(self)
  185. self.Pages[i].Adjust()
  186. if self.Pages[i].GetIconNumbers() > 1 {
  187. self.Pages[i].SetPsIndex(1)
  188. self.Pages[i].SetIconIndex(1)
  189. }
  190. }
  191. self.CurrentPage = self.Pages[self.PageIndex]
  192. self.CurrentPage.SetOnShow(true)
  193. }
  194. func (self *MainScreen) CurPage() PageInterface {
  195. return self.CurrentPage
  196. }
  197. func (self *MainScreen) PushCurPage() {
  198. self.MyPageStack.Push(self.CurrentPage)
  199. }
  200. func (self *MainScreen) SetCurPage(pg PageInterface) {
  201. self.CurrentPage = pg
  202. pg.OnLoadCb()
  203. }
  204. func (self *MainScreen) PushPage(pg PageInterface) {
  205. self.PushCurPage()
  206. self.SetCurPage(pg)
  207. }
  208. func (self *MainScreen) AppendPage(pg PageInterface) {
  209. self.Pages = append(self.Pages, pg)
  210. }
  211. func (self *MainScreen) ClearCanvas() {
  212. surface.Fill(self.CanvasHWND, &color.Color{255, 255, 255, 255})
  213. }
  214. func (self *MainScreen) SwapAndShow() {
  215. if self.HWND != nil {
  216. rect_ := rect.Rect(self.PosX, self.PosY, self.Width, self.Height)
  217. surface.Blit(self.HWND, self.CanvasHWND, &rect_, nil)
  218. }
  219. display.Flip()
  220. }
  221. func (self *MainScreen) ExtraName(name string) string {
  222. parts := strings.Split(name, "_")
  223. if len(parts) > 1 {
  224. return parts[1]
  225. } else if len(parts) == 1 {
  226. return parts[0]
  227. } else {
  228. return name
  229. }
  230. }
  231. //ExecPackage is all-in-one folder ,Name.sh,Name.png,etc
  232. func (self *MainScreen) IsExecPackage(dirname string) bool {
  233. files, err := ioutil.ReadDir(dirname)
  234. if err != nil {
  235. log.Println(err)
  236. return false
  237. }
  238. bname := filepath.Base(dirname)
  239. bname = self.ExtraName(bname)
  240. for _, v := range files {
  241. if v.Name() == bname+".sh" {
  242. return true
  243. }
  244. }
  245. return false
  246. }
  247. func (self *MainScreen) IsPluginPackage(dirname string) bool {
  248. ret := false
  249. files, err := ioutil.ReadDir(dirname)
  250. if err != nil {
  251. log.Println(err)
  252. return false
  253. }
  254. for _, f := range files {
  255. if f.IsDir() {
  256. //pass
  257. } else {
  258. if strings.HasSuffix(f.Name(), Plugin_flag) == true {
  259. ret = true
  260. break
  261. }
  262. }
  263. }
  264. return ret
  265. }
  266. func (self *MainScreen) IsEmulatorPackage(dirname string) bool {
  267. ret := false
  268. files, err := ioutil.ReadDir(dirname)
  269. if err != nil {
  270. log.Println(err)
  271. return false
  272. }
  273. for _, f := range files {
  274. if f.IsDir() {
  275. //pass
  276. } else {
  277. if strings.HasSuffix(f.Name(), Emulator_flag) == true {
  278. ret = true
  279. break
  280. }
  281. }
  282. }
  283. return ret
  284. }
  285. func (self *MainScreen) IsWifiConnectedNow() bool {
  286. cli := fmt.Sprintf("ip -4 addr show %s | grep -oP '(?<=inet\\s)\\d+(\\.\\d+){3}'", sysgo.WifiDev)
  287. out := System(cli)
  288. if len(out) < 6 {
  289. return false
  290. }
  291. return true
  292. }
  293. func (self *MainScreen) GetWirelessIP() string {
  294. cli := fmt.Sprintf("ip -4 addr show %s | grep -oP '(?<=inet\\s)\\d+(\\.\\d+){3}'", sysgo.WifiDev)
  295. out := SystemTrim(cli)
  296. return out
  297. }
  298. func (self *MainScreen) RunEXE(cmdpath string) {
  299. self.DrawRun()
  300. self.SwapAndShow()
  301. time.BlockDelay(1000)
  302. cmdpath = strings.Trim(cmdpath, " ")
  303. cmdpath = CmdClean(cmdpath)
  304. event.Post(RUNEVT, cmdpath)
  305. }
  306. func (self *MainScreen) HookExitCb( page PageInterface) {
  307. self.Child = append(self.Child,page)
  308. }
  309. func (self *MainScreen) OnExitCb() {
  310. PageLen := len(self.Child)
  311. for i := 0; i < PageLen; i++ {
  312. self.Child[i].OnExitCb()
  313. }
  314. }
  315. func (self *MainScreen) KeyDown(ev *event.Event) {
  316. if ev.Data["Key"] == "T" {
  317. self.DrawRun()
  318. self.SwapAndShow()
  319. return
  320. }
  321. if ev.Data["Key"] == "Space" {
  322. self.Draw()
  323. self.SwapAndShow()
  324. }
  325. self.CurrentPage.KeyDown(ev)
  326. self.LastKey = ev.Data["Key"]
  327. }
  328. func (self *MainScreen) ShowMsg(content string, blocktime int) {
  329. self.MsgBox.SetText(content)
  330. self.MsgBox.Draw()
  331. self.SwapAndShow()
  332. if blocktime > 0 {
  333. time.BlockDelay(blocktime)
  334. self.CurrentPage.Draw()
  335. self.SwapAndShow()
  336. }
  337. }
  338. func (self *MainScreen) DrawRun() {
  339. self.MsgBox.SetText("Launching....")
  340. self.MsgBox.Draw()
  341. }
  342. func (self *MainScreen) Draw() {
  343. if self.CurrentPage != nil {
  344. self.CurrentPage.Draw()
  345. }
  346. if self.TitleBar != nil {
  347. //every plugin_init should not do any Draw actions since CurrentPage might be nil at that time
  348. self.TitleBar.Draw(self.CurrentPage.GetName())
  349. }
  350. if self.FootBar != nil {
  351. self.FootBar.SetLabelTexts(self.CurrentPage.GetFootMsg())
  352. self.FootBar.Draw()
  353. }
  354. }
  355. func (self *MainScreen) Refresh() {
  356. self.updateScreen <- true
  357. }
  358. func (self *MainScreen) RefreshLoop() {
  359. L:
  360. for {
  361. select {
  362. case v:= <- self.updateScreen:
  363. if v == true {
  364. self.Draw()
  365. self.SwapAndShow()
  366. }
  367. if v== false {
  368. break L
  369. }
  370. }
  371. }
  372. }