123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- package UI
- import (
- "fmt"
- "io/ioutil"
- "log"
- "strings"
- //"encoding/json"
- "github.com/veandco/go-sdl2/sdl"
- "github.com/veandco/go-sdl2/ttf"
- "path/filepath"
- gotime "time"
- "github.com/cuu/gogame/color"
- "github.com/cuu/gogame/display"
- "github.com/cuu/gogame/draw"
- "github.com/cuu/gogame/font"
- "github.com/cuu/gogame/rect"
- "github.com/cuu/gogame/surface"
- "github.com/cuu/gogame/time"
- "github.com/cuu/gogame/event"
- "github.com/clockworkpi/LauncherGoDev/sysgo"
- )
- //eg: MainScreen
- type ScreenInterface interface {
- AppendPage(pg PageInterface)
- ClearCanvas()
- CurPage() PageInterface
- Draw()
- ExtraName(name string) string
- FartherPages()
- Init()
- IsEmulatorPackage(dirname string) bool
- IsExecPackage(dirname string) bool
- IsPluginPackage(dirname string) bool
- KeyDown(ev *event.Event)
- OnExitCb()
- HookExitCb()
- PushCurPage()
- PushPage(pg PageInterface)
- RunEXE(cmdpath string)
- SetCurPage(pg PageInterface)
- SwapAndShow()
- IsWifiConnectedNow()
- Refresh()
- }
- type PluginConfig struct {
- NAME string `json:"NAME"` // plugin name,default could be the same as Plugin Folder's name
- SO_FILE string `json:"SO_FILE"`
- }
- type MessageBox struct {
- Label
- Parent *MainScreen
- HWND *sdl.Surface
- }
- func NewMessageBox() *MessageBox {
- m := &MessageBox{}
- m.Color = &color.Color{83, 83, 83, 255}
- return m
- }
- func (self *MessageBox) Init(text string, font_obj *ttf.Font, col *color.Color) {
- if col != nil {
- self.Color = col
- }
- self.Text = text
- self.FontObj = font_obj
- self.Width = 0
- self.Height = 0
- self.CanvasHWND = surface.Surface(self.Parent.Width, self.Parent.Height)
- self.HWND = self.Parent.CanvasHWND
- }
- func (self *MessageBox) SetText(text string) {
- self.Text = text
- }
- func (self *MessageBox) Draw() {
- self.Width = 0
- self.Height = 0
- surface.Fill(self.CanvasHWND, &color.Color{255, 255, 255, 255})
- words := strings.Split(self.Text, " ")
- space, _ := font.Size(self.FontObj, " ")
- max_width := self.Parent.Width - 40
- x := 0
- y := 0
- row_total_width := 0
- lines := 0
- for _, word := range words {
- word_surface := font.Render(self.FontObj, word, true, self.Color, nil)
- word_width := int(word_surface.W)
- word_height := int(word_surface.H)
- row_total_width += word_width
- if lines == 0 {
- lines += word_height
- }
- if (row_total_width + space) >= max_width {
- x = 0
- y += word_height
- row_total_width = word_width
- lines += word_height
- }
- dest_rect := rect.Rect(x, y, word_width, word_height)
- surface.Blit(self.CanvasHWND, word_surface, &dest_rect, nil)
- word_surface.Free()
- if len(words) == 1 {
- x += word_width
- } else {
- x += word_width + space
- }
- if x > self.Width {
- self.Width = x
- }
- if lines >= self.Parent.Height-40 {
- break
- }
- }
- self.Height = lines
- padding := 5
- x = (self.Parent.Width - self.Width) / 2
- y = (self.Parent.Height - self.Height) / 2
- rect_ := rect.Rect(x-padding, y-padding, self.Width+padding*2, self.Height+padding*2)
- if self.HWND != nil {
- draw.Rect(self.HWND, &color.Color{255, 255, 255, 255}, &rect_, 0)
- rect__ := draw.MidRect(self.Parent.Width/2, self.Parent.Height/2, self.Width, self.Height, Width, Height)
- dest_rect := rect.Rect(0, 0, self.Width, self.Height)
- surface.Blit(self.HWND, self.CanvasHWND, rect__, &dest_rect)
- draw.Rect(self.HWND, &color.Color{0, 0, 0, 255}, &rect_, 1)
- }
- }
- type MainScreen struct {
- Widget
- Pages []PageInterface
- Child []PageInterface
- PageMax int
- PageIndex int
- MyPageStack *PageStack
- CurrentPage PageInterface
- CanvasHWND *sdl.Surface
- HWND *sdl.Surface
- TitleBar *TitleBar
- FootBar *FootBar
- MsgBox *MessageBox
- MsgBoxFont *ttf.Font
- IconFont *ttf.Font
- SkinManager *SkinManager
- CounterScreen *CounterScreen
- Closed bool
- UIPluginList []*UIPlugin
- LastKey string
- LastKeyDown gotime.Time
-
- updateScreen chan bool
- }
- func NewMainScreen() *MainScreen {
- m := &MainScreen{}
- m.PosY = TitleBar_BarHeight + 1
- m.Width = Width
- m.Height = Height - FootBar_BarHeight - TitleBar_BarHeight - 1
- m.MyPageStack = NewPageStack()
- m.MsgBoxFont = Fonts["veramono20"]
- m.IconFont = Fonts["varela15"]
- m.Closed = false
-
- m.updateScreen = make(chan bool,1)
- return m
- }
- func (self *MainScreen) Init() {
- self.CanvasHWND = surface.Surface(self.Width, self.Height)
- self.MsgBox = NewMessageBox()
- self.MsgBox.Parent = self
- self.MsgBox.Init(" ", self.MsgBoxFont, &color.Color{83, 83, 83, 255})
- self.SkinManager = NewSkinManager()
- self.SkinManager.Init()
- self.CounterScreen = NewCounterScreen()
- self.CounterScreen.HWND = self.HWND
- self.CounterScreen.Init()
- //self.GenList() // load predefined plugin list,ready to be injected ,or ,as a .so for dynamic loading
- go func() {
- sdl.Do(func() {
- self.RefreshLoop()
- })
- }()
- }
- func (self *MainScreen) FartherPages() { // right after ReadTheDirIntoPages
- self.PageMax = len(self.Pages)
- for i := 0; i < self.PageMax; i++ {
- self.Pages[i].SetIndex(i)
- self.Pages[i].SetCanvasHWND(self.CanvasHWND)
- self.Pages[i].UpdateIconNumbers() // IconNumbers always == len(Pages[i].Icons)
- self.Pages[i].SetScreen(self)
- self.Pages[i].Adjust()
- if self.Pages[i].GetIconNumbers() > 1 {
- self.Pages[i].SetPsIndex(1)
- self.Pages[i].SetIconIndex(1)
- }
- }
- self.CurrentPage = self.Pages[self.PageIndex]
- self.CurrentPage.SetOnShow(true)
- }
- func (self *MainScreen) CurPage() PageInterface {
- return self.CurrentPage
- }
- func (self *MainScreen) PushCurPage() {
- self.MyPageStack.Push(self.CurrentPage)
- }
- func (self *MainScreen) SetCurPage(pg PageInterface) {
- self.CurrentPage = pg
- pg.OnLoadCb()
- }
- func (self *MainScreen) PushPage(pg PageInterface) {
- self.PushCurPage()
- self.SetCurPage(pg)
- }
- func (self *MainScreen) AppendPage(pg PageInterface) {
- self.Pages = append(self.Pages, pg)
- }
- func (self *MainScreen) ClearCanvas() {
- surface.Fill(self.CanvasHWND, &color.Color{255, 255, 255, 255})
- }
- func (self *MainScreen) SwapAndShow() {
- if self.HWND != nil {
- rect_ := rect.Rect(self.PosX, self.PosY, self.Width, self.Height)
- surface.Blit(self.HWND, self.CanvasHWND, &rect_, nil)
- }
- display.Flip()
- }
- func (self *MainScreen) ExtraName(name string) string {
- parts := strings.Split(name, "_")
- if len(parts) > 1 {
- return parts[1]
- } else if len(parts) == 1 {
- return parts[0]
- } else {
- return name
- }
- }
- //ExecPackage is all-in-one folder ,Name.sh,Name.png,etc
- func (self *MainScreen) IsExecPackage(dirname string) bool {
- files, err := ioutil.ReadDir(dirname)
- if err != nil {
- log.Println(err)
- return false
- }
- bname := filepath.Base(dirname)
- bname = self.ExtraName(bname)
- for _, v := range files {
- if v.Name() == bname+".sh" {
- return true
- }
- }
- return false
- }
- func (self *MainScreen) IsPluginPackage(dirname string) bool {
- ret := false
- files, err := ioutil.ReadDir(dirname)
- if err != nil {
- log.Println(err)
- return false
- }
- for _, f := range files {
- if f.IsDir() {
- //pass
- } else {
- if strings.HasSuffix(f.Name(), Plugin_flag) == true {
- ret = true
- break
- }
- }
- }
- return ret
- }
- func (self *MainScreen) IsEmulatorPackage(dirname string) bool {
- ret := false
- files, err := ioutil.ReadDir(dirname)
- if err != nil {
- log.Println(err)
- return false
- }
- for _, f := range files {
- if f.IsDir() {
- //pass
- } else {
- if strings.HasSuffix(f.Name(), Emulator_flag) == true {
- ret = true
- break
- }
- }
- }
- return ret
- }
- func (self *MainScreen) IsWifiConnectedNow() bool {
- cli := fmt.Sprintf("ip -4 addr show %s | grep -oP '(?<=inet\\s)\\d+(\\.\\d+){3}'", sysgo.WifiDev)
- out := System(cli)
- if len(out) < 6 {
- return false
- }
- return true
- }
- func (self *MainScreen) GetWirelessIP() string {
- cli := fmt.Sprintf("ip -4 addr show %s | grep -oP '(?<=inet\\s)\\d+(\\.\\d+){3}'", sysgo.WifiDev)
- out := SystemTrim(cli)
- return out
- }
- func (self *MainScreen) RunEXE(cmdpath string) {
- self.DrawRun()
- self.SwapAndShow()
- time.BlockDelay(1000)
- cmdpath = strings.Trim(cmdpath, " ")
- cmdpath = CmdClean(cmdpath)
- event.Post(RUNEVT, cmdpath)
- }
- func (self *MainScreen) HookExitCb( page PageInterface) {
- self.Child = append(self.Child,page)
- }
- func (self *MainScreen) OnExitCb() {
- PageLen := len(self.Child)
- for i := 0; i < PageLen; i++ {
- self.Child[i].OnExitCb()
- }
- }
- func (self *MainScreen) KeyDown(ev *event.Event) {
- if ev.Data["Key"] == "T" {
- self.DrawRun()
- self.SwapAndShow()
- return
- }
- if ev.Data["Key"] == "Space" {
- self.Draw()
- self.SwapAndShow()
- }
- self.CurrentPage.KeyDown(ev)
- self.LastKey = ev.Data["Key"]
- }
- func (self *MainScreen) ShowMsg(content string, blocktime int) {
- self.MsgBox.SetText(content)
- self.MsgBox.Draw()
- self.SwapAndShow()
- if blocktime > 0 {
- time.BlockDelay(blocktime)
- self.CurrentPage.Draw()
- self.SwapAndShow()
- }
- }
- func (self *MainScreen) DrawRun() {
- self.MsgBox.SetText("Launching....")
- self.MsgBox.Draw()
- }
- func (self *MainScreen) Draw() {
- if self.CurrentPage != nil {
- self.CurrentPage.Draw()
- }
- if self.TitleBar != nil {
- //every plugin_init should not do any Draw actions since CurrentPage might be nil at that time
- self.TitleBar.Draw(self.CurrentPage.GetName())
- }
- if self.FootBar != nil {
- self.FootBar.SetLabelTexts(self.CurrentPage.GetFootMsg())
- self.FootBar.Draw()
- }
- }
- func (self *MainScreen) Refresh() {
- self.updateScreen <- true
- }
- func (self *MainScreen) RefreshLoop() {
- L:
- for {
- select {
- case v:= <- self.updateScreen:
- if v == true {
- self.Draw()
- self.SwapAndShow()
- }
- if v== false {
- break L
- }
- }
- }
- }
|