music_lib_list_page.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package MusicPlayer
  2. import (
  3. "fmt"
  4. "log"
  5. "path/filepath"
  6. "github.com/cuu/gogame/event"
  7. "github.com/cuu/gogame/rect"
  8. "github.com/cuu/gogame/surface"
  9. "github.com/veandco/go-sdl2/ttf"
  10. "github.com/cuu/gogame/color"
  11. // "github.com/clockworkpi/LauncherGoDev/sysgo"
  12. "github.com/clockworkpi/LauncherGoDev/sysgo/UI"
  13. // "github.com/fhs/gompd/v2/mpd"
  14. )
  15. type MusicLibListPage struct {
  16. UI.Page
  17. ListFontObj *ttf.Font
  18. URLColor *color.Color
  19. TextColor *color.Color
  20. Labels map[string]UI.LabelInterface
  21. Icons map[string]UI.IconItemInterface
  22. IP string
  23. //MyList []UI.ListItemInterface
  24. MyStack *UI.FolderStack
  25. BGwidth int
  26. BGheight int //70
  27. Scroller *UI.ListScroller
  28. Scrolled int
  29. Parent *MusicPlayerPage// also use the MpdClient from
  30. }
  31. func NewMusicLibListPage() *MusicLibListPage {
  32. p := &MusicLibListPage{}
  33. p.PageIconMargin = 20
  34. p.SelectedIconTopOffset = 20
  35. p.EasingDur = 10
  36. p.Align = UI.ALIGN["SLeft"]
  37. p.FootMsg = [5]string{"Nav.", "", "Scan","Back","Add to Playlist"}
  38. p.URLColor = UI.MySkinManager.GiveColor("URL")
  39. p.TextColor = UI.MySkinManager.GiveColor("Text")
  40. p.ListFontObj = UI.MyLangManager.TrFont("notosanscjk15")
  41. p.Labels = make(map[string]UI.LabelInterface)
  42. p.Icons = make(map[string]UI.IconItemInterface)
  43. p.BGwidth = 56
  44. p.BGheight = 70
  45. return p
  46. }
  47. func (self *MusicLibListPage) OnLoadCb() {
  48. self.PosY = 0
  49. self.SyncList("/")
  50. }
  51. func (self *MusicLibListPage) SetCoords() {
  52. }
  53. func (self *MusicLibListPage) SyncList(path string) {
  54. conn := self.Parent.MpdClient
  55. self.MyList = nil
  56. start_x := 0
  57. start_y := 0
  58. hasparent :=0
  59. atts, err := conn.ListInfo(path)
  60. if err != nil {
  61. log.Println(err)
  62. return
  63. }
  64. if self.MyStack.Length() > 0 {
  65. hasparent = 1
  66. li := NewMusicLibListPageListItem()
  67. li.Parent = self
  68. li.PosX = start_x
  69. li.PosY = start_y
  70. li.Width = UI.Width
  71. li.Fonts["normal"] = self.ListFontObj
  72. li.Init("[..]")
  73. li.MyType = UI.ICON_TYPES["DIR"]
  74. self.MyList = append(self.MyList, li)
  75. }
  76. if len(atts) == 0 {
  77. log.Println("no songs")
  78. return
  79. }
  80. for i, m := range atts {
  81. li := NewMusicLibListPageListItem()
  82. li.Parent = self
  83. li.PosX = start_x
  84. li.PosY = start_y + (i+hasparent)*li.Height
  85. li.Width = UI.Width
  86. li.Fonts["normal"] = self.ListFontObj
  87. li.MyType = UI.ICON_TYPES["FILE"]
  88. init_val := "NoName"
  89. if val, ok := m["directory"] ; ok {
  90. li.MyType = UI.ICON_TYPES["DIR"]
  91. init_val = filepath.Base(val)
  92. li.Path = val
  93. }
  94. if val, ok := m["file"]; ok {
  95. li.MyType = UI.ICON_TYPES["FILE"]
  96. li.Path = val
  97. val2, ok2 := m["Title"]
  98. if ok2 && len(val2) > 4{
  99. init_val = val2
  100. }else{
  101. init_val = val
  102. }
  103. }
  104. li.Init(init_val)
  105. self.MyList = append(self.MyList, li)
  106. }
  107. }
  108. func (self *MusicLibListPage) Init() {
  109. if self.Screen == nil {
  110. panic("No Screen")
  111. }
  112. if self.Screen.CanvasHWND != nil && self.CanvasHWND == nil {
  113. self.HWND = self.Screen.CanvasHWND
  114. self.CanvasHWND = surface.Surface(self.Screen.Width, self.Screen.Height)
  115. }
  116. self.PosX = self.Index * self.Screen.Width
  117. self.Width = self.Screen.Width
  118. self.Height = self.Screen.Height
  119. ps := NewListPageSelector()
  120. ps.Parent = self
  121. ps.Width = UI.Width - 12
  122. ps.PosX = 2
  123. ps.Parent = self
  124. self.Ps = ps
  125. self.PsIndex = 0
  126. bgpng := UI.NewIconItem()
  127. bgpng.ImgSurf = UI.MyIconPool.GetImgSurf("empty")
  128. bgpng.MyType = UI.ICON_TYPES["STAT"]
  129. bgpng.Parent = self
  130. bgpng.AddLabel("Please upload data over Wi-Fi", UI.Fonts["varela22"])
  131. bgpng.SetLabelColor(&color.Color{204, 204, 204, 255})
  132. bgpng.Adjust(0, 0, self.BGwidth, self.BGheight, 0)
  133. self.Icons["bg"] = bgpng
  134. icon_for_list := UI.NewMultiIconItem()
  135. icon_for_list.ImgSurf = UI.MyIconPool.GetImgSurf("sys")
  136. icon_for_list.MyType = UI.ICON_TYPES["STAT"]
  137. icon_for_list.Parent = self
  138. icon_for_list.Adjust(0, 0, 18, 18, 0)
  139. self.Icons["sys"] = icon_for_list
  140. self.Scroller = UI.NewListScroller()
  141. self.Scroller.Parent = self
  142. self.Scroller.PosX = self.Width - 10
  143. self.Scroller.PosY = 2
  144. self.Scroller.Init()
  145. self.MyStack = UI.NewFolderStack()
  146. self.MyStack.SetRootPath("/")
  147. }
  148. func (self *MusicLibListPage) Click() {
  149. self.RefreshPsIndex()
  150. if len(self.MyList) == 0 {
  151. return
  152. }
  153. cur_li := self.MyList[self.PsIndex].(*MusicLibListPageListItem)
  154. if cur_li.MyType == UI.ICON_TYPES["DIR"] {
  155. if cur_li.Path == "[..]" {
  156. self.MyStack.Pop()
  157. self.SyncList( self.MyStack.Last() )
  158. self.PsIndex = 0
  159. } else {
  160. self.MyStack.Push( cur_li.Path )
  161. self.SyncList( self.MyStack.Last() )
  162. self.PsIndex = 0
  163. }
  164. }
  165. if cur_li.MyType == UI.ICON_TYPES["FILE"] {
  166. conn := self.Parent.MpdClient
  167. conn.Add(cur_li.Path)
  168. self.Parent.SyncList()
  169. fmt.Println("add" , cur_li.Path)
  170. }
  171. self.Screen.Draw()
  172. self.Screen.SwapAndShow()
  173. }
  174. func (self *MusicLibListPage) KeyDown(ev *event.Event) {
  175. if UI.IsKeyMenuOrB(ev.Data["Key"]) || ev.Data["Key"] == UI.CurKeys["Left"] {
  176. self.ReturnToUpLevelPage()
  177. self.Screen.Draw()
  178. self.Screen.SwapAndShow()
  179. }
  180. if ev.Data["Key"] == UI.CurKeys["Up"] {
  181. self.ScrollUp()
  182. self.Screen.Draw()
  183. self.Screen.SwapAndShow()
  184. }
  185. if ev.Data["Key"] == UI.CurKeys["Down"] {
  186. self.ScrollDown()
  187. self.Screen.Draw()
  188. self.Screen.SwapAndShow()
  189. }
  190. if UI.IsKeyStartOrA(ev.Data["Key"]) {
  191. self.Click()
  192. }
  193. return
  194. }
  195. func (self *MusicLibListPage) Draw() {
  196. self.ClearCanvas()
  197. if len(self.MyList) == 0 {
  198. self.Icons["bg"].NewCoord(self.Width/2, self.Height/2)
  199. self.Icons["bg"].Draw()
  200. }else {
  201. if len(self.MyList)*UI.DefaultInfoPageListItemHeight > self.Height {
  202. self.Ps.(*ListPageSelector).Width = self.Width - 11
  203. self.Ps.Draw()
  204. for _, v := range self.MyList {
  205. if v.(*MusicLibListPageListItem).PosY > self.Height+self.Height/2 {
  206. break
  207. }
  208. if v.(*MusicLibListPageListItem).PosY < 0 {
  209. continue
  210. }
  211. v.Draw()
  212. }
  213. self.Scroller.UpdateSize( len(self.MyList)*UI.DefaultInfoPageListItemHeight, self.PsIndex*UI.DefaultInfoPageListItemHeight)
  214. self.Scroller.Draw()
  215. } else{
  216. self.Ps.(*ListPageSelector).Width = self.Width
  217. self.Ps.Draw()
  218. for _, v := range self.MyList {
  219. if v.(*MusicLibListPageListItem).PosY > self.Height+self.Height/2 {
  220. break
  221. }
  222. if v.(*MusicLibListPageListItem).PosY < 0 {
  223. continue
  224. }
  225. v.Draw()
  226. }
  227. }
  228. }
  229. if self.HWND != nil {
  230. surface.Fill(self.HWND, UI.MySkinManager.GiveColor("White"))
  231. rect_ := rect.Rect(self.PosX, self.PosY, self.Width, self.Height)
  232. surface.Blit(self.HWND, self.CanvasHWND, &rect_, nil)
  233. }
  234. }