music_lib_list_page.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. if self.MyList == nil || len(self.MyList) == 0 {
  50. self.MyStack.Clear()
  51. self.SyncList("/")
  52. }
  53. }
  54. func (self *MusicLibListPage) SetCoords() {
  55. }
  56. func (self *MusicLibListPage) SyncList(path string) {
  57. fmt.Println("SyncList: ",path)
  58. fmt.Println(self.MyStack)
  59. conn := self.Parent.MpdClient
  60. self.MyList = nil
  61. start_x := 0
  62. start_y := 0
  63. hasparent :=0
  64. atts, err := conn.ListInfo(path)
  65. if err != nil {
  66. log.Println(err)
  67. return
  68. }
  69. if self.MyStack.Length() > 0 {
  70. hasparent = 1
  71. li := NewMusicLibListPageListItem()
  72. li.Parent = self
  73. li.PosX = start_x
  74. li.PosY = start_y
  75. li.Width = UI.Width
  76. li.Fonts["normal"] = self.ListFontObj
  77. li.Path = "[..]"
  78. li.Init("[..]")
  79. li.MyType = UI.ICON_TYPES["DIR"]
  80. self.MyList = append(self.MyList, li)
  81. }
  82. if len(atts) == 0 {
  83. log.Println("no songs")
  84. return
  85. }
  86. for i, m := range atts {
  87. li := NewMusicLibListPageListItem()
  88. li.Parent = self
  89. li.PosX = start_x
  90. li.PosY = start_y + (i+hasparent)*li.Height
  91. li.Width = UI.Width
  92. li.Fonts["normal"] = self.ListFontObj
  93. li.MyType = UI.ICON_TYPES["FILE"]
  94. init_val := "NoName"
  95. if val, ok := m["directory"] ; ok {
  96. li.MyType = UI.ICON_TYPES["DIR"]
  97. init_val = filepath.Base(val)
  98. li.Path = val
  99. }
  100. if val, ok := m["file"]; ok {
  101. li.MyType = UI.ICON_TYPES["FILE"]
  102. li.Path = val
  103. val2, ok2 := m["Title"]
  104. if ok2 && len(val2) > 4{
  105. init_val = val2
  106. }else{
  107. init_val = val
  108. }
  109. }
  110. li.Init(init_val)
  111. self.MyList = append(self.MyList, li)
  112. }
  113. }
  114. func (self *MusicLibListPage) Init() {
  115. if self.Screen == nil {
  116. panic("No Screen")
  117. }
  118. if self.Screen.CanvasHWND != nil && self.CanvasHWND == nil {
  119. self.HWND = self.Screen.CanvasHWND
  120. self.CanvasHWND = surface.Surface(self.Screen.Width, self.Screen.Height)
  121. }
  122. self.PosX = self.Index * self.Screen.Width
  123. self.Width = self.Screen.Width
  124. self.Height = self.Screen.Height
  125. ps := NewListPageSelector()
  126. ps.Parent = self
  127. ps.Width = UI.Width - 12
  128. ps.PosX = 2
  129. ps.Parent = self
  130. self.Ps = ps
  131. self.PsIndex = 0
  132. bgpng := UI.NewIconItem()
  133. bgpng.ImgSurf = UI.MyIconPool.GetImgSurf("empty")
  134. bgpng.MyType = UI.ICON_TYPES["STAT"]
  135. bgpng.Parent = self
  136. bgpng.AddLabel("Please upload data over Wi-Fi", UI.Fonts["varela22"])
  137. bgpng.SetLabelColor(&color.Color{204, 204, 204, 255})
  138. bgpng.Adjust(0, 0, self.BGwidth, self.BGheight, 0)
  139. self.Icons["bg"] = bgpng
  140. icon_for_list := UI.NewMultiIconItem()
  141. icon_for_list.ImgSurf = UI.MyIconPool.GetImgSurf("sys")
  142. icon_for_list.MyType = UI.ICON_TYPES["STAT"]
  143. icon_for_list.Parent = self
  144. icon_for_list.Adjust(0, 0, 18, 18, 0)
  145. self.Icons["sys"] = icon_for_list
  146. self.Scroller = UI.NewListScroller()
  147. self.Scroller.Parent = self
  148. self.Scroller.PosX = self.Width - 10
  149. self.Scroller.PosY = 2
  150. self.Scroller.Init()
  151. self.MyStack = UI.NewFolderStack()
  152. self.MyStack.SetRootPath("/")
  153. }
  154. func (self *MusicLibListPage) Click() {
  155. self.RefreshPsIndex()
  156. if len(self.MyList) == 0 {
  157. return
  158. }
  159. cur_li := self.MyList[self.PsIndex].(*MusicLibListPageListItem)
  160. if cur_li.MyType == UI.ICON_TYPES["DIR"] {
  161. if cur_li.Path == "[..]" {
  162. self.MyStack.Pop()
  163. self.SyncList( self.MyStack.Last() )
  164. self.PsIndex = 0
  165. } else {
  166. self.MyStack.Push( cur_li.Path )
  167. self.SyncList( self.MyStack.Last() )
  168. self.PsIndex = 0
  169. }
  170. }
  171. if cur_li.MyType == UI.ICON_TYPES["FILE"] {
  172. conn := self.Parent.MpdClient
  173. conn.Add(cur_li.Path)
  174. self.Parent.SyncList()
  175. fmt.Println("add" , cur_li.Path)
  176. }
  177. self.Screen.Refresh()
  178. }
  179. func (self *MusicLibListPage) KeyDown(ev *event.Event) {
  180. if UI.IsKeyMenuOrA(ev.Data["Key"]) || ev.Data["Key"] == UI.CurKeys["Left"] {
  181. self.ReturnToUpLevelPage()
  182. self.Screen.Refresh()
  183. }
  184. if ev.Data["Key"] == UI.CurKeys["Up"] {
  185. self.ScrollUp()
  186. self.Screen.Refresh()
  187. }
  188. if ev.Data["Key"] == UI.CurKeys["Down"] {
  189. self.ScrollDown()
  190. self.Screen.Refresh()
  191. }
  192. if ev.Data["Key"] == UI.CurKeys["B"] {
  193. self.Click()
  194. }
  195. if ev.Data["Key"] == UI.CurKeys["Y"] {
  196. self.Screen.ShowMsg("Scan...",300)
  197. self.OnLoadCb()
  198. }
  199. return
  200. }
  201. func (self *MusicLibListPage) Draw() {
  202. self.ClearCanvas()
  203. if len(self.MyList) == 0 {
  204. self.Icons["bg"].NewCoord(self.Width/2, self.Height/2)
  205. self.Icons["bg"].Draw()
  206. }else {
  207. if len(self.MyList)*UI.DefaultInfoPageListItemHeight > self.Height {
  208. self.Ps.(*ListPageSelector).Width = self.Width - 11
  209. self.Ps.Draw()
  210. for _, v := range self.MyList {
  211. v.(*MusicLibListPageListItem).Active = false
  212. if self.Parent.InPlayList( v.(*MusicLibListPageListItem).Path) {
  213. v.(*MusicLibListPageListItem).Active = true
  214. fmt.Println("in PlayList: ",v.(*MusicLibListPageListItem).Path)
  215. }
  216. if v.(*MusicLibListPageListItem).PosY > self.Height+self.Height/2 {
  217. break
  218. }
  219. if v.(*MusicLibListPageListItem).PosY < 0 {
  220. continue
  221. }
  222. v.Draw()
  223. }
  224. self.Scroller.UpdateSize( len(self.MyList)*UI.DefaultInfoPageListItemHeight, self.PsIndex*UI.DefaultInfoPageListItemHeight)
  225. self.Scroller.Draw()
  226. } else{
  227. self.Ps.(*ListPageSelector).Width = self.Width
  228. self.Ps.Draw()
  229. for _, v := range self.MyList {
  230. v.(*MusicLibListPageListItem).Active = false
  231. if self.Parent.InPlayList( v.(*MusicLibListPageListItem).Path) {
  232. v.(*MusicLibListPageListItem).Active = true
  233. fmt.Println("in PlayList: ",v.(*MusicLibListPageListItem).Path)
  234. }
  235. if v.(*MusicLibListPageListItem).PosY > self.Height+self.Height/2 {
  236. break
  237. }
  238. if v.(*MusicLibListPageListItem).PosY < 0 {
  239. continue
  240. }
  241. v.Draw()
  242. }
  243. }
  244. }
  245. if self.HWND != nil {
  246. surface.Fill(self.HWND, UI.MySkinManager.GiveColor("White"))
  247. rect_ := rect.Rect(self.PosX, self.PosY, self.Width, self.Height)
  248. surface.Blit(self.HWND, self.CanvasHWND, &rect_, nil)
  249. }
  250. }