main_screen.go 8.4 KB

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