music_player_page.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package MusicPlayer
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "log"
  6. "strconv"
  7. "strings"
  8. "github.com/cuu/gogame/event"
  9. "github.com/cuu/gogame/rect"
  10. "github.com/cuu/gogame/surface"
  11. "github.com/veandco/go-sdl2/ttf"
  12. "github.com/cuu/gogame/color"
  13. "github.com/clockworkpi/LauncherGoDev/sysgo"
  14. "github.com/clockworkpi/LauncherGoDev/sysgo/UI"
  15. "github.com/fhs/gompd/v2/mpd"
  16. )
  17. type MusicPlayerPage struct {
  18. UI.Page
  19. ListFontObj *ttf.Font
  20. URLColor *color.Color
  21. TextColor *color.Color
  22. Labels map[string]UI.LabelInterface
  23. Icons map[string]UI.IconItemInterface
  24. IP string
  25. MyMusicLibListPage *MusicLibListPage //also use the MpdClient *mpd.Client
  26. //MyList []UI.ListItemInterface
  27. MyStack *UI.FolderStack
  28. BGwidth int
  29. BGheight int //70
  30. Scroller *UI.ListScroller
  31. Scrolled int
  32. MpdClient *mpd.Client
  33. CurSongTime string
  34. CurSongName string
  35. }
  36. func NewMusicPlayerPage() *MusicPlayerPage {
  37. p := &MusicPlayerPage{}
  38. p.PageIconMargin = 20
  39. p.SelectedIconTopOffset = 20
  40. p.EasingDur = 10
  41. p.Align = UI.ALIGN["SLeft"]
  42. p.FootMsg = [5]string{"Nav","Remove","","Back","Play/Pause"}
  43. p.URLColor = UI.MySkinManager.GiveColor("URL")
  44. p.TextColor = UI.MySkinManager.GiveColor("Text")
  45. p.ListFontObj = UI.MyLangManager.TrFont("notosanscjk15")
  46. p.Labels = make(map[string]UI.LabelInterface)
  47. p.Icons = make(map[string]UI.IconItemInterface)
  48. p.BGwidth = 56
  49. p.BGheight = 70
  50. p.CurSongTime = "0:0"
  51. return p
  52. }
  53. func (self *MusicPlayerPage) OnLoadCb() {
  54. self.PosY = 0
  55. if self.MpdClient == nil {
  56. conn, err := mpd.Dial("unix", sysgo.MPD_socket)
  57. if err != nil {
  58. log.Fatalln(err)
  59. }
  60. self.MpdClient = conn
  61. fmt.Println("Start mpd client")
  62. }
  63. self.SyncList()
  64. }
  65. func (self *MusicPlayerPage) OnPopUpCb() {
  66. if self.MpdClient != nil {
  67. self.MpdClient.Close()
  68. self.MpdClient = nil
  69. fmt.Println("Close mpd client")
  70. }
  71. }
  72. func (self *MusicPlayerPage) OnReturnBackCb() {
  73. self.SyncList()
  74. }
  75. func (self *MusicPlayerPage) SetCoords() {
  76. }
  77. func (self *MusicPlayerPage) SetLabels() {
  78. }
  79. func (self *MusicPlayerPage) SyncList() {
  80. conn := self.MpdClient
  81. start_x := 0
  82. start_y := 0
  83. if conn == nil {
  84. return
  85. }
  86. self.MyList = nil
  87. play_list,_ := conn.PlaylistInfo(-1,-1)
  88. for i,v := range play_list {
  89. li := NewMusicPlayPageListItem()
  90. li.Parent = self
  91. li.PosX = start_x
  92. li.PosY = start_y + UI.DefaultInfoPageListItemHeight * i
  93. li.Width = UI.Width
  94. li.Fonts["normal"] = self.ListFontObj
  95. if val,ok:=v["Title"]; ok {
  96. li.Init(val)
  97. if val2,ok2 := v["file"]; ok2 {
  98. li.Path = val2
  99. }
  100. }else {
  101. if val2,ok2 := v["file"]; ok2 {
  102. li.Init(filepath.Base(val2))
  103. li.Path = val2
  104. }else{
  105. li.Init("NoName")
  106. }
  107. }
  108. x,_ := li.Labels["Text"].Coord()
  109. li.Labels["Text"].NewCoord(x,7)
  110. self.MyList = append(self.MyList, li)
  111. }
  112. self.SyncPlaying()
  113. }
  114. func (self *MusicPlayerPage) SyncPlaying() {
  115. conn := self.MpdClient
  116. for i,_ := range self.MyList {
  117. self.MyList[i].(*MusicPlayPageListItem).Active = false
  118. self.MyList[i].(*MusicPlayPageListItem).PlayingProcess = 0
  119. }
  120. current_song,_ := conn.CurrentSong()
  121. if len(current_song) > 0 {
  122. if val,ok := current_song["song"]; ok{
  123. posid, _ := strconv.Atoi(val)
  124. if posid < len(self.MyList) {
  125. if state,ok2 := current_song["state"]; ok2 {
  126. if state == "stop" {
  127. self.MyList[posid].(*MusicPlayPageListItem).Active = false
  128. }else{
  129. self.MyList[posid].(*MusicPlayPageListItem).Active = true
  130. }
  131. }
  132. if song_time,ok3 := current_song["time"]; ok3 {
  133. self.CurSongTime = song_time
  134. times := strings.Split(self.CurSongTime,":")
  135. if len(times) > 1{
  136. cur,_ := strconv.ParseFloat(times[0],64)
  137. end,_ := strconv.ParseFloat(times[1],64)
  138. pos := int( (cur/end)*100.0 )
  139. self.MyList[posid].(*MusicPlayPageListItem).PlayingProcess = pos
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. func (self *MusicPlayerPage) InPlayList(path string) bool {
  147. if self.MyList == nil || len(self.MyList) == 0 {
  148. return false
  149. }
  150. for _,v := range self.MyList {
  151. ///fmt.Println(v.(*MusicPlayPageListItem).Path, path)
  152. if v.(*MusicPlayPageListItem).Path == path {
  153. return true
  154. }
  155. }
  156. return false
  157. }
  158. func (self *MusicPlayerPage) Init() {
  159. if self.Screen == nil {
  160. panic("No Screen")
  161. }
  162. if self.Screen.CanvasHWND != nil && self.CanvasHWND == nil {
  163. self.HWND = self.Screen.CanvasHWND
  164. self.CanvasHWND = surface.Surface(self.Screen.Width, self.Screen.Height)
  165. }
  166. self.PosX = self.Index * self.Screen.Width
  167. self.Width = self.Screen.Width
  168. self.Height = self.Screen.Height
  169. //ps := UI.NewInfoPageSelector()
  170. ps := NewListPageSelector()
  171. //ps.Width = UI.Width - 12
  172. ps.Parent = self
  173. self.Ps = ps
  174. self.PsIndex = 0
  175. bgpng := UI.NewIconItem()
  176. bgpng.ImgSurf = UI.MyIconPool.GetImgSurf("empty")
  177. bgpng.MyType = UI.ICON_TYPES["STAT"]
  178. bgpng.Parent = self
  179. bgpng.AddLabel("Please upload data over Wi-Fi", UI.Fonts["varela22"])
  180. bgpng.SetLabelColor(&color.Color{204, 204, 204, 255})
  181. bgpng.Adjust(0, 0, self.BGwidth, self.BGheight, 0)
  182. self.Icons["bg"] = bgpng
  183. icon_for_list := UI.NewMultiIconItem()
  184. icon_for_list.ImgSurf = UI.MyIconPool.GetImgSurf("sys")
  185. icon_for_list.MyType = UI.ICON_TYPES["STAT"]
  186. icon_for_list.Parent = self
  187. icon_for_list.Adjust(0, 0, 18, 18, 0)
  188. self.Icons["sys"] = icon_for_list
  189. self.Scroller = UI.NewListScroller()
  190. self.Scroller.Parent = self
  191. self.Scroller.PosX = self.Width - 10
  192. self.Scroller.PosY = 2
  193. self.Scroller.Init()
  194. self.MyMusicLibListPage = NewMusicLibListPage()
  195. self.MyMusicLibListPage.Screen = self.Screen
  196. self.MyMusicLibListPage.Name = "Music library"
  197. self.MyMusicLibListPage.Parent = self
  198. self.MyMusicLibListPage.Init()
  199. self.MyStack = UI.NewFolderStack()
  200. self.MyStack.SetRootPath("/")
  201. }
  202. func (self *MusicPlayerPage) KeyDown(ev *event.Event) {
  203. if ev.Data["Key"] == UI.CurKeys["Right"] {
  204. self.Screen.PushPage(self.MyMusicLibListPage)
  205. self.Screen.Draw()
  206. self.Screen.SwapAndShow()
  207. }
  208. if ev.Data["Key"] == UI.CurKeys["A"] || ev.Data["Key"] == UI.CurKeys["Menu"] {
  209. self.ReturnToUpLevelPage()
  210. self.Screen.Draw()
  211. self.Screen.SwapAndShow()
  212. }
  213. if ev.Data["Key"] == UI.CurKeys["Up"] {
  214. self.ScrollUp()
  215. self.Screen.Draw()
  216. self.Screen.SwapAndShow()
  217. }
  218. if ev.Data["Key"] == UI.CurKeys["Down"] {
  219. self.ScrollDown()
  220. self.Screen.Draw()
  221. self.Screen.SwapAndShow()
  222. }
  223. if ev.Data["Key"] == UI.CurKeys["X"] {
  224. self.MpdClient.Delete(self.PsIndex,-1)
  225. self.SyncList()
  226. self.Screen.Draw()
  227. self.Screen.SwapAndShow()
  228. }
  229. return
  230. }
  231. func (self *MusicPlayerPage) Draw() {
  232. self.ClearCanvas()
  233. if len(self.MyList) == 0 {
  234. self.Icons["bg"].NewCoord(self.Width/2, self.Height/2)
  235. self.Icons["bg"].Draw()
  236. }else {
  237. if len(self.MyList)*UI.DefaultInfoPageListItemHeight > self.Height {
  238. self.Ps.(*ListPageSelector).Width = self.Width - 11
  239. self.Ps.Draw()
  240. for _, v := range self.MyList {
  241. if v.(*MusicPlayPageListItem).PosY > self.Height+self.Height/2 {
  242. break
  243. }
  244. if v.(*MusicPlayPageListItem).PosY < 0 {
  245. continue
  246. }
  247. v.Draw()
  248. }
  249. self.Scroller.UpdateSize( len(self.MyList)*UI.DefaultInfoPageListItemHeight, self.PsIndex*UI.DefaultInfoPageListItemHeight)
  250. self.Scroller.Draw()
  251. } else{
  252. self.Ps.(*ListPageSelector).Width = self.Width
  253. self.Ps.Draw()
  254. for _, v := range self.MyList {
  255. if v.(*MusicPlayPageListItem).PosY > self.Height+self.Height/2 {
  256. break
  257. }
  258. if v.(*MusicPlayPageListItem).PosY < 0 {
  259. continue
  260. }
  261. v.Draw()
  262. }
  263. }
  264. }
  265. if self.HWND != nil {
  266. surface.Fill(self.HWND, UI.MySkinManager.GiveColor("White"))
  267. rect_ := rect.Rect(self.PosX, self.PosY, self.Width, self.Height)
  268. surface.Blit(self.HWND, self.CanvasHWND, &rect_, nil)
  269. }
  270. }