main_screen.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. package UI
  2. import (
  3. //"fmt"
  4. "io/ioutil"
  5. "strings"
  6. "log"
  7. //"encoding/json"
  8. "path/filepath"
  9. "github.com/veandco/go-sdl2/sdl"
  10. "github.com/veandco/go-sdl2/ttf"
  11. "github.com/cuu/gogame/display"
  12. "github.com/cuu/gogame/surface"
  13. "github.com/cuu/gogame/draw"
  14. "github.com/cuu/gogame/color"
  15. "github.com/cuu/gogame/rect"
  16. "github.com/cuu/gogame/font"
  17. "github.com/cuu/gogame/time"
  18. "github.com/cuu/gogame/event"
  19. "github.com/clockworkpi/LauncherGoDev/sysgo/DBUS"
  20. )
  21. type PluginConfig struct {
  22. NAME string `json:"NAME"` // plugin name,default could be the same as Plugin Folder's name
  23. SO_FILE string `json:"SO_FILE"`
  24. }
  25. type MessageBox struct {
  26. Label
  27. Parent *MainScreen
  28. HWND *sdl.Surface
  29. }
  30. func NewMessageBox() *MessageBox {
  31. m := &MessageBox{}
  32. m.Color = &color.Color{83,83,83,255}
  33. return m
  34. }
  35. func (self *MessageBox) Init( text string, font_obj *ttf.Font, col *color.Color) {
  36. if col != nil {
  37. self.Color = col
  38. }
  39. self.Text = text
  40. self.FontObj = font_obj
  41. self.Width = 0
  42. self.Height = 0
  43. self.CanvasHWND = surface.Surface(self.Parent.Width, self.Parent.Height)
  44. self.HWND = self.Parent.CanvasHWND
  45. }
  46. func (self *MessageBox) SetText( text string) {
  47. self.Text = text
  48. }
  49. func (self *MessageBox) Draw() {
  50. self.Width = 0
  51. self.Height = 0
  52. surface.Fill(self.CanvasHWND, &color.Color{255,255,255,255} )
  53. words := strings.Split(self.Text," ")
  54. space,_ := font.Size(self.FontObj," ")
  55. max_width := self.Parent.Width - 40
  56. x := 0
  57. y := 0
  58. row_total_width := 0
  59. lines := 0
  60. for _,word := range words {
  61. word_surface := font.Render( self.FontObj, word, true, self.Color,nil)
  62. word_width := int(word_surface.W)
  63. word_height := int(word_surface.H)
  64. row_total_width += word_width
  65. if lines == 0 {
  66. lines += word_height
  67. }
  68. if (row_total_width + space ) >= max_width {
  69. x = 0
  70. y += word_height
  71. row_total_width = word_width
  72. lines+=word_height
  73. }
  74. dest_rect := rect.Rect(x,y, word_width,word_height)
  75. surface.Blit(self.CanvasHWND, word_surface, &dest_rect,nil)
  76. if len(words) == 1 {
  77. x+=word_width
  78. } else {
  79. x += word_width+space
  80. }
  81. if x > self.Width {
  82. self.Width = x
  83. }
  84. if lines >= self.Parent.Height - 40 {
  85. break
  86. }
  87. }
  88. self.Height = lines
  89. padding := 5
  90. x = (self.Parent.Width - self.Width) / 2
  91. y = (self.Parent.Height - self.Height) /2
  92. rect_ := rect.Rect(x-padding,y-padding, self.Width+padding*2, self.Height+padding*2)
  93. if self.HWND != nil {
  94. draw.Rect(self.HWND , &color.Color{255,255,255,255},&rect_,0)
  95. rect__ := draw.MidRect(self.Parent.Width/2, self.Parent.Height/2,self.Width,self.Height,Width,Height)
  96. dest_rect := rect.Rect(0,0,self.Width,self.Height)
  97. surface.Blit(self.HWND, self.CanvasHWND, rect__, &dest_rect)
  98. draw.Rect(self.HWND , &color.Color{0,0,0,255},&rect_,1)
  99. }
  100. }
  101. type MainScreen struct {
  102. Widget
  103. Pages []PageInterface
  104. PageMax int
  105. PageIndex int
  106. MyPageStack *PageStack
  107. CurrentPage PageInterface
  108. CanvasHWND *sdl.Surface
  109. HWND *sdl.Surface
  110. TitleBar *TitleBar
  111. FootBar *FootBar
  112. MsgBox *MessageBox
  113. MsgBoxFont *ttf.Font
  114. IconFont *ttf.Font
  115. SkinManager *SkinManager
  116. DBusManager *DBUS.DBus
  117. CounterScreen *CounterScreen
  118. Closed bool
  119. UIPluginList []*UIPlugin
  120. }
  121. func NewMainScreen() *MainScreen {
  122. m := &MainScreen{}
  123. m.PosY = TitleBar_BarHeight+1
  124. m.Width = Width
  125. m.Height = Height - FootBar_BarHeight - TitleBar_BarHeight - 1
  126. m.MyPageStack = NewPageStack()
  127. m.MsgBoxFont = Fonts["veramono20"]
  128. m.IconFont = Fonts["varela15"]
  129. m.Closed = false
  130. return m
  131. }
  132. func (self *MainScreen) Init() {
  133. self.CanvasHWND = surface.Surface(self.Width,self.Height)
  134. self.MsgBox = NewMessageBox()
  135. self.MsgBox.Parent = self
  136. self.MsgBox.Init(" ", self.MsgBoxFont, &color.Color{83,83,83,255})
  137. self.SkinManager = NewSkinManager()
  138. self.SkinManager.Init()
  139. self.DBusManager = DBUS.DBusHandler
  140. self.CounterScreen = NewCounterScreen()
  141. self.CounterScreen.HWND = self.HWND
  142. self.CounterScreen.Init()
  143. //self.GenList() // load predefined plugin list,ready to be injected ,or ,as a .so for dynamic loading
  144. }
  145. func (self *MainScreen) FartherPages() { // right after ReadTheDirIntoPages
  146. self.PageMax = len(self.Pages)
  147. for i:=0;i< self.PageMax; i++ {
  148. self.Pages[i].SetIndex(i)
  149. self.Pages[i].SetCanvasHWND(self.CanvasHWND)
  150. self.Pages[i].UpdateIconNumbers() // IconNumbers always == len(Pages[i].Icons)
  151. self.Pages[i].SetScreen(self)
  152. self.Pages[i].Adjust()
  153. if self.Pages[i].GetIconNumbers() > 1 {
  154. self.Pages[i].SetPsIndex(1)
  155. self.Pages[i].SetIconIndex( 1 )
  156. }
  157. }
  158. self.CurrentPage = self.Pages[ self.PageIndex ]
  159. self.CurrentPage.SetOnShow(true)
  160. }
  161. func (self *MainScreen) CurPage() PageInterface {
  162. return self.CurrentPage
  163. }
  164. func (self *MainScreen) PushCurPage() {
  165. self.MyPageStack.Push(self.CurrentPage)
  166. }
  167. func (self *MainScreen) SetCurPage( pg PageInterface) {
  168. self.CurrentPage = pg
  169. pg.OnLoadCb()
  170. }
  171. func (self *MainScreen) PushPage( pg PageInterface) {
  172. self.PushCurPage()
  173. self.SetCurPage(pg)
  174. }
  175. func (self *MainScreen) AppendPage( pg PageInterface ) {
  176. self.Pages = append(self.Pages,pg)
  177. }
  178. func (self *MainScreen) ClearCanvas() {
  179. surface.Fill(self.CanvasHWND, &color.Color{255,255,255,255} )
  180. }
  181. func (self *MainScreen) SwapAndShow() {
  182. if self.HWND != nil {
  183. rect_ := rect.Rect( self.PosX,self.PosY,self.Width,self.Height)
  184. surface.Blit(self.HWND,self.CanvasHWND,&rect_, nil)
  185. }
  186. display.Flip()
  187. }
  188. func (self *MainScreen) ExtraName(name string) string {
  189. parts := strings.Split(name,"_")
  190. if len(parts) > 1 {
  191. return parts[1]
  192. }else if len(parts) == 1 {
  193. return parts[0]
  194. }else {
  195. return name
  196. }
  197. }
  198. //ExecPackage is all-in-one folder ,Name.sh,Name.png,etc
  199. func (self *MainScreen) IsExecPackage(dirname string ) bool {
  200. files,err := ioutil.ReadDir(dirname)
  201. if err != nil {
  202. log.Fatal(err)
  203. return false
  204. }
  205. bname := filepath.Base(dirname)
  206. bname = self.ExtraName(bname)
  207. for _,v := range files {
  208. if v.Name() == bname+".sh" {
  209. return true
  210. }
  211. }
  212. return false
  213. }
  214. func (self *MainScreen) IsPluginPackage(dirname string ) bool {
  215. ret := false
  216. files,err := ioutil.ReadDir(dirname)
  217. if err != nil {
  218. log.Fatal(err)
  219. return false
  220. }
  221. for _,f := range files {
  222. if f.IsDir() {
  223. //pass
  224. }else {
  225. if strings.HasSuffix(f.Name(),Plugin_flag) == true {
  226. ret = true
  227. break
  228. }
  229. }
  230. }
  231. return ret
  232. }
  233. func (self *MainScreen) IsEmulatorPackage(dirname string ) bool {
  234. ret := false
  235. files,err := ioutil.ReadDir(dirname)
  236. if err != nil {
  237. log.Fatal(err)
  238. return false
  239. }
  240. for _,f := range files {
  241. if f.IsDir() {
  242. //pass
  243. }else {
  244. if strings.HasSuffix(f.Name(),Emulator_flag) == true {
  245. ret = true
  246. break
  247. }
  248. }
  249. }
  250. return ret
  251. }
  252. func (self *MainScreen) RunEXE( cmdpath string) {
  253. self.DrawRun()
  254. self.SwapAndShow()
  255. time.BlockDelay(1000)
  256. cmdpath = strings.Trim(cmdpath," ")
  257. cmdpath = CmdClean(cmdpath)
  258. event.Post(RUNEVT,cmdpath)
  259. }
  260. func (self *MainScreen) OnExitCb() {
  261. self.CurrentPage.OnExitCb()
  262. }
  263. func (self *MainScreen) KeyDown(ev *event.Event) {
  264. if ev.Data["Key"] == "T" {
  265. self.DrawRun()
  266. self.SwapAndShow()
  267. return
  268. }
  269. if ev.Data["Key"] == "Space" {
  270. self.Draw()
  271. self.SwapAndShow()
  272. }
  273. self.CurrentPage.KeyDown(ev)
  274. }
  275. func (self *MainScreen) DrawRun() {
  276. self.MsgBox.SetText("Launching....")
  277. self.MsgBox.Draw()
  278. }
  279. func (self *MainScreen) Draw() {
  280. if self.CurrentPage != nil {
  281. self.CurrentPage.Draw()
  282. }
  283. if self.TitleBar != nil {
  284. self.TitleBar.Draw( self.CurrentPage.GetName())
  285. }
  286. if self.FootBar != nil {
  287. self.FootBar.SetLabelTexts( self.CurrentPage.GetFootMsg())
  288. self.FootBar.Draw()
  289. }
  290. }