title_bar.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package UI
  2. import (
  3. "fmt"
  4. "os"
  5. "log"
  6. "strconv"
  7. "bufio"
  8. "strings"
  9. "time"
  10. "github.com/veandco/go-sdl2/sdl"
  11. "github.com/veandco/go-sdl2/ttf"
  12. "github.com/cuu/gogame/surface"
  13. "github.com/cuu/gogame/rect"
  14. "github.com/cuu/gogame/font"
  15. "github.com/cuu/gogame/draw"
  16. "github.com/itchyny/volume-go"
  17. "github.com/vjeantet/jodaTime"
  18. "../DBUS"
  19. "../../sysgo"
  20. )
  21. var TitleBar_BarHeight = 24
  22. type TitleBarIconItem struct {
  23. MultiIconItem
  24. Parent *TitleBar
  25. }
  26. func NewTitleBarIconItem() *TitleBarIconItem {
  27. m := &TitleBarIconItem{}
  28. m.IconIndex = 0
  29. m.IconWidth = 18
  30. m.IconHeight = 18
  31. m.Align = ALIGN["VCenter"]
  32. return m
  33. }
  34. func (self *TitleBarIconItem) Adjust(x,y,w,h,at int) {
  35. self.PosX = x
  36. self.PosY = y
  37. self.Width = w
  38. self.Height = h
  39. self.AnimationTime = at
  40. if self.Label != nil {
  41. self.Label.SetCanvasHWND(self.Parent.CanvasHWND)
  42. }
  43. self.CreateImgSurf()
  44. // self.AdjustLinkPage()
  45. }
  46. func (self *TitleBarIconItem) Draw() {
  47. parent_x,parent_y := self.Parent.PosX,self.Parent.PosY
  48. if self.Label != nil {
  49. // lab_x,lab_y := self.Label.Coord()
  50. lab_w,lab_h:= self.Label.Size()
  51. if self.Align == ALIGN["VCenter"] {
  52. self.Label.NewCoord( self.PosX - lab_w/2 + parent_x, self.PosY + self.Height/2+6 + parent_y)
  53. }else if self.Align == ALIGN["HLeft"] {
  54. self.Label.NewCoord( self.PosX + self.Width/2+3 + parent_x, self.PosY - lab_h/2 + parent_y )
  55. }
  56. self.Label.Draw()
  57. }
  58. if self.ImgSurf != nil {
  59. portion := rect.Rect(0,self.IconIndex*self.IconHeight,self.IconWidth,self.IconHeight)
  60. surface.Blit(self.Parent.CanvasHWND,
  61. self.ImgSurf,draw.MidRect(self.PosX + parent_x, self.PosY + parent_y,
  62. self.Width,self.Height, Width, Height),&portion)
  63. }
  64. }
  65. type TitleBar struct {
  66. PosX int
  67. PosY int
  68. Width int
  69. Height int
  70. BarHeight int
  71. LOffset int
  72. ROffset int
  73. Icons map[string]IconItemInterface
  74. IconWidth int
  75. IconHeight int
  76. BorderWidth int
  77. CanvasHWND *sdl.Surface
  78. HWND *sdl.Surface
  79. Title string
  80. InLowBackLight int
  81. SkinManager *SkinManager //set by MainScreen
  82. DBusManager DBUS.DBusInterface
  83. icon_base_path string /// SkinMap("gameshell/titlebar_icons/")
  84. TitleFont *ttf.Font
  85. TimeFont *ttf.Font
  86. }
  87. func NewTitleBar() *TitleBar {
  88. t := &TitleBar{}
  89. t.BorderWidth = 1
  90. t.BarHeight = TitleBar_BarHeight
  91. t.Height = t.BarHeight + t.BorderWidth
  92. t.Width = Width
  93. t.IconWidth = 18
  94. t.IconHeight = 18
  95. t.LOffset = 3
  96. t.ROffset = 3
  97. t.Icons = make(map[string]IconItemInterface)
  98. t.icon_base_path = SkinMap("sysgo/gameshell/titlebar_icons/")
  99. t.TitleFont = Fonts["varela16"]
  100. t.TimeFont = Fonts["varela12"]
  101. return t
  102. }
  103. func (t *TitleBar) RoundRobinCheck() {
  104. }
  105. func (t *TitleBar) UpdateWifiStrength() {
  106. }
  107. func (t *TitleBar) GetWifiStrength(stren int) int {
  108. segs := [][]int{ []int{-2,-1}, []int{0,25}, []int{25,50}, []int{50,75},[]int{75,100}}
  109. stren_number := stren
  110. ge := 0
  111. if stren_number == 0 {
  112. return ge
  113. }
  114. for i,v := range segs {
  115. if stren_number >= v[0] && stren_number <= v[1] {
  116. ge = i
  117. break
  118. }
  119. }
  120. return ge
  121. }
  122. func (self *TitleBar) SyncSoundVolume() {
  123. vol, err := volume.GetVolume()
  124. if err != nil {
  125. log.Fatalf("get volume failed: %+v", err)
  126. vol = 0
  127. }
  128. fmt.Printf("current volume: %d\n", vol)
  129. snd_segs := [][]int{ []int{0,10}, []int{10,30}, []int{30,70},[]int{70,100} }
  130. ge := 0
  131. for i,v := range snd_segs {
  132. if vol >= v[0] && vol <= v[1] {
  133. ge = i
  134. break
  135. }
  136. }
  137. self.Icons["soundvolume"].SetIconIndex(ge)
  138. self.Icons["sound"] = self.Icons["soundvolume"]
  139. //
  140. }
  141. func (t *TitleBar) SetSoundVolume(vol int) {
  142. //pass
  143. }
  144. func (self *TitleBar) CheckBatteryStat() {
  145. bat_segs:= [][]int{[]int{0,6},[]int{7,15},[]int{16,20},[]int{21,30},[]int{31,50},[]int{51,60},[]int{61,80},[]int{81,90},[]int{91,100}}
  146. file, err := os.Open( sysgo.Battery )
  147. if err != nil {
  148. fmt.Println("Could not open file ", sysgo.Battery)
  149. self.Icons["battery"] = self.Icons["battery_unknown"]
  150. return
  151. }
  152. defer file.Close()
  153. bat_uevent := make(map[string]string)
  154. scanner := bufio.NewScanner(file)
  155. scanner.Split(bufio.ScanLines)
  156. for scanner.Scan() {
  157. line := scanner.Text()
  158. line = strings.Trim(line," ")
  159. pis := strings.Split(line,"=")
  160. if len(pis) > 1 {
  161. bat_uevent[ pis[0] ] = pis[1]
  162. }
  163. }
  164. cur_cap := 0
  165. if val, ok := bat_uevent["POWER_SUPPLY_CAPACITY"]; ok {
  166. cur_cap,_ = strconv.Atoi(val)
  167. }else {
  168. cur_cap = 0
  169. }
  170. cap_ge := 0
  171. for i,v := range bat_segs {
  172. if cur_cap >= v[0] && cur_cap <= v[1] {
  173. cap_ge = i
  174. break
  175. }
  176. }
  177. if val, ok := bat_uevent["POWER_SUPPLY_STATUS"]; ok {
  178. if val == "Charging" {
  179. self.Icons["battery_charging"].SetIconIndex(cap_ge)
  180. self.Icons["battery"] = self.Icons["battery_charging"]
  181. }else {
  182. self.Icons["battery_charging"].SetIconIndex(cap_ge)
  183. self.Icons["battery"] = self.Icons["battery_discharging"]
  184. }
  185. }
  186. }
  187. func (self *TitleBar) SetBatteryStat( bat int) {
  188. }
  189. func (self *TitleBar) Init(main_screen *MainScreen) {
  190. start_x := 0
  191. self.CanvasHWND = surface.Surface(self.Width,self.Height)
  192. self.HWND = main_screen.HWND
  193. self.SkinManager = main_screen.SkinManager
  194. self.DBusManager = main_screen.DBusManager
  195. icon_wifi_status := NewTitleBarIconItem()
  196. icon_wifi_status.MyType = ICON_TYPES["STAT"]
  197. icon_wifi_status.ImageName = self.icon_base_path+"wifi.png"
  198. icon_wifi_status.Parent = self
  199. icon_wifi_status.Adjust(start_x+self.IconWidth+5,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
  200. self.Icons["wifistatus"] = icon_wifi_status
  201. battery_charging := NewTitleBarIconItem()
  202. battery_charging.MyType = ICON_TYPES["STAT"]
  203. battery_charging.Parent = self
  204. battery_charging.ImageName = self.icon_base_path+"withcharging.png"
  205. battery_charging.Adjust(start_x+self.IconWidth+self.IconWidth+8,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
  206. self.Icons["battery_charging"] = battery_charging
  207. battery_discharging := NewTitleBarIconItem()
  208. battery_discharging.MyType = ICON_TYPES["STAT"]
  209. battery_discharging.Parent = self
  210. battery_discharging.ImageName = self.icon_base_path+"without_charging.png"
  211. battery_discharging.Adjust(start_x+self.IconWidth+self.IconWidth+8,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
  212. self.Icons["battery_discharging"] = battery_discharging
  213. battery_unknown := NewTitleBarIconItem()
  214. battery_unknown.MyType = ICON_TYPES["STAT"]
  215. battery_unknown.Parent = self
  216. battery_unknown.ImageName = self.icon_base_path+"battery_unknown.png"
  217. battery_unknown.Adjust(start_x+self.IconWidth+self.IconWidth+8,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
  218. self.Icons["battery_unknown"] = battery_unknown
  219. self.CheckBatteryStat()
  220. sound_volume := NewTitleBarIconItem()
  221. sound_volume.MyType = ICON_TYPES["STAT"]
  222. sound_volume.Parent = self
  223. sound_volume.ImageName = self.icon_base_path+"soundvolume.png"
  224. sound_volume.Adjust(start_x+self.IconWidth+self.IconWidth+8,self.IconHeight/2+(self.BarHeight-self.IconHeight)/2,self.IconWidth,self.IconHeight,0)
  225. self.Icons["soundvolume"] = sound_volume
  226. self.SyncSoundVolume()
  227. round_corners := NewTitleBarIconItem()
  228. round_corners.IconWidth = 10
  229. round_corners.IconHeight = 10
  230. round_corners.MyType = ICON_TYPES["STAT"]
  231. round_corners.Parent = self
  232. round_corners.ImgSurf = MyIconPool.GetImgSurf("roundcorners")
  233. round_corners.Adjust(0,0,10,10,0)
  234. self.Icons["round_corners"] = round_corners
  235. if self.DBusManager.IsWifiConnectedNow() {
  236. print("wifi is connected")
  237. print( self.DBusManager.WifiStrength())
  238. }
  239. }
  240. func (self *TitleBar) ClearCanvas() {
  241. surface.Fill(self.CanvasHWND, self.SkinManager.GiveColor("TitleBg"))
  242. self.Icons["round_corners"].NewCoord(5,5)
  243. self.Icons["round_corners"].SetIconIndex(0)
  244. self.Icons["round_corners"].Draw()
  245. self.Icons["round_corners"].NewCoord(self.Width-5, 5)
  246. self.Icons["round_corners"].SetIconIndex(1)
  247. self.Icons["round_corners"].Draw()
  248. }
  249. func (self *TitleBar) Draw(title string) {
  250. self.ClearCanvas()
  251. self.Title = title
  252. cur_time := jodaTime.Format("HH:mm", time.Now())
  253. time_text_w, time_text_h := font.Size(self.TimeFont, cur_time)
  254. title_text_w, title_text_h := font.Size(self.TitleFont, self.Title)
  255. title_text_surf := font.Render(self.TitleFont, self.Title, true, self.SkinManager.GiveColor("Text"),nil)
  256. surface.Blit(self.CanvasHWND,title_text_surf, draw.MidRect(title_text_w/2+self.LOffset,title_text_h/2+(self.BarHeight-title_text_h)/2,title_text_w,title_text_h,Width,Height),nil)
  257. time_text_surf := font.Render(self.TimeFont, cur_time,true,self.SkinManager.GiveColor("Text"),nil)
  258. surface.Blit(self.CanvasHWND, time_text_surf, draw.MidRect(Width-time_text_w/2-self.ROffset, time_text_h/2+(self.BarHeight-time_text_h)/2, time_text_w,time_text_h,Width,Height),nil)
  259. start_x := Width - time_text_w - self.ROffset - self.IconWidth*3 // close to the time_text
  260. self.Icons["sound"].NewCoord( start_x, self.IconHeight/2+ (self.BarHeight-self.IconHeight)/2)
  261. self.Icons["battery"].NewCoord(start_x+self.IconWidth+self.IconWidth+8, self.IconHeight/2+(self.BarHeight-self.IconHeight)/2)
  262. if self.DBusManager.IsWifiConnectedNow() == true {
  263. ge := self.GetWifiStrength( self.DBusManager.WifiStrength() )
  264. fmt.Println("wifi ge: ",ge)
  265. if ge > 0 {
  266. self.Icons["wifistatus"].SetIconIndex(ge)
  267. self.Icons["wifistatus"].NewCoord(start_x+self.IconWidth+5, self.IconHeight/2+(self.BarHeight-self.IconHeight)/2 )
  268. self.Icons["wifistatus"].Draw()
  269. }else {
  270. self.Icons["wifistatus"].SetIconIndex(0)
  271. self.Icons["wifistatus"].Draw()
  272. }
  273. }else {
  274. self.Icons["wifistatus"].SetIconIndex(0)
  275. self.Icons["wifistatus"].NewCoord(start_x+self.IconWidth+5, self.IconHeight/2+(self.BarHeight-self.IconHeight)/2)
  276. self.Icons["wifistatus"].Draw()
  277. }
  278. self.Icons["sound"].Draw()
  279. self.Icons["battery"].Draw()
  280. draw.Line(self.CanvasHWND,self.SkinManager.GiveColor("Line"), 0,self.BarHeight,self.Width,self.BarHeight, self.BorderWidth)
  281. if self.HWND != nil {
  282. rect_ := rect.Rect(self.PosX,self.PosY, self.Width,self.Height)
  283. surface.Blit(self.HWND, self.CanvasHWND, &rect_, nil)
  284. }
  285. }