Gcores Custom Modification

This commit is contained in:
cuu
2019-07-12 14:02:32 +08:00
parent f90b45059d
commit ae279bda94
25 changed files with 13 additions and 7 deletions

View File

@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
import pygame
## local UI import
import pages
import myvars
def Init(main_screen):
pages.InitPoller()
pages.InitListPage(main_screen)
pages.InitMusicLibPage(main_screen)
pages.InitSpectrumPage(main_screen)
def API(main_screen):
if main_screen !=None:
main_screen.PushCurPage()
main_screen.SetCurPage(myvars.PlayListPage)
main_screen.Draw()
main_screen.SwapAndShow()

View File

@@ -0,0 +1,134 @@
# -*- coding: utf-8 -*-
import pygame
from libs.roundrects import aa_round_rect
## local UI import
from UI.constants import ICON_TYPES
from UI.page import Page
from UI.label import Label
from UI.icon_item import IconItem
from UI.util_funcs import midRect
from UI.skin_manager import MySkinManager
# a item for List
# - - - - - - - - - - - --
# | Icon Text..... > |
# ------------------------
import myvars # icons_path
class ListItemIcon(IconItem):
_CanvasHWND = None
_Parent = None
_Width = 18
_Height = 18
def Draw(self):
self._CanvasHWND.blit(self._ImgSurf,(self._PosX,self._PosY+(self._Parent._Height-self._Height)/2,self._Width,self._Height))
class ListItemLabel(Label):
_ActiveColor = MySkinManager.GiveColor('Active')
_Active = False
def Draw(self):
self._FontObj.set_bold(self._Active)
"""
if self._Active == True:
my_text = self._FontObj.render( self._Text,True,self._ActiveColor)
else:
my_text = self._FontObj.render( self._Text,True,self._Color)
"""
my_text = self._FontObj.render( self._Text,True,self._Color)
self._CanvasHWND.blit(my_text,(self._PosX,self._PosY,self._Width,self._Height))
class ListItem(object):
_PosX = 0
_PosY = 0
_Width = 0
_Height = 30
_Labels = {}
_Icons = {}
_Fonts = {}
_MyType = ICON_TYPES["EXE"]
_LinkObj = None
_Path = ""
_Active = False
_Playing = False ## play or pause
_PlayingProcess = 0 # 0 - 100
_Parent = None
_Text = ""
def __init__(self):
self._Labels = {}
self._Icons = {}
self._Fonts = {}
def Init(self,text):
#self._Fonts["normal"] = fonts["veramono12"]
self._Text = text
l = ListItemLabel()
l._PosX = 22
l.SetCanvasHWND(self._Parent._CanvasHWND)
if self._MyType == ICON_TYPES["DIR"]:
l.Init(text,self._Fonts["normal"])
self._Path = text
else:
l.Init(text,self._Fonts["normal"])
self._Path = text
self._Labels["Text"] = l
def NewCoord(self,x,y):
self._PosX = x
self._PosY = y
def Draw(self):
if self._MyType == ICON_TYPES["DIR"] and self._Path != "[..]":
self._Parent._Icons["sys"]._IconIndex = 0
self._Parent._Icons["sys"].NewCoord(self._PosX+12,self._PosY+ (self._Height - self._Parent._Icons["sys"]._Height)/2+self._Parent._Icons["sys"]._Height/2)
self._Parent._Icons["sys"].Draw()
if self._MyType == ICON_TYPES["FILE"]:
self._Parent._Icons["sys"]._IconIndex = 1
self._Parent._Icons["sys"].NewCoord(self._PosX+12,self._PosY+ (self._Height - self._Parent._Icons["sys"]._Height)/2+self._Parent._Icons["sys"]._Height/2)
self._Parent._Icons["sys"].Draw()
if self._Active == True:
self._Labels["Text"]._Active = True
else:
self._Labels["Text"]._Active = False
self._Labels["Text"]._PosY = self._PosY + (self._Height - self._Labels["Text"]._Height)/2
pygame.draw.line(self._Parent._CanvasHWND,MySkinManager.GiveColor('Line'),(self._PosX,self._PosY+self._Height-1),(self._PosX+self._Width,self._PosY+self._Height-1),1)
if self._Playing == True:
self._Labels["Text"]._Active =True
self._Labels["Text"].Draw()
#_rect = midRect(10,self._PosY+15,10,10,self._Parent._Width,self._Parent._Height)
#aa_round_rect(self._Parent._CanvasHWND,_rect,(0,0,0),3,0,(0,0,0))
#pygame.draw.polygon(self._Parent._CanvasHWND, (0,0,0), [[6, self._PosY+7], [11, self._PosY+14],[6, self._PosY+21]], 2)
if self._PlayingProcess > 0:
seek_posx = int(self._Width * self._PlayingProcess/100.0)
pygame.draw.line(self._Parent._CanvasHWND,MySkinManager.GiveColor('Active'),(self._PosX,self._PosY+self._Height-2),(self._PosX+seek_posx,self._PosY+self._Height-2),2)
else:
self._Labels["Text"].Draw()

View File

@@ -0,0 +1,420 @@
# -*- coding: utf-8 -*-
import os
import time
import pygame
import numpy
import math
import gobject
from beeprint import pp
## local UI import
from UI.constants import Width,Height,ICON_TYPES
from UI.page import Page,PageSelector
from UI.label import Label
from UI.util_funcs import midRect
from UI.keys_def import CurKeys, IsKeyStartOrA, IsKeyMenuOrB
from UI.icon_item import IconItem
from UI.icon_pool import MyIconPool
from UI.skin_manager import MySkinManager
from UI.lang_manager import MyLangManager
from threading import Thread
from list_item import ListItem
import myvars
class PIFI(object):
_MPD_FIFO = "/tmp/mpd.fifo"
_SAMPLE_SIZE = 1024
_SAMPLING_RATE = 44100
_FIRST_SELECTED_BIN = 5
_NUMBER_OF_SELECTED_BINS = 1024
_samples_buffer = None
def __init__(self):
self.sampleSize = self._SAMPLE_SIZE
self.samplingRate = self._SAMPLING_RATE
def GetSpectrum(self,fifoFile,trim_by=4,log_scale=False,div_by=100):
try:
rawSamples = os.read(fifoFile,self.sampleSize) # will return empty lines (non-blocking)
if len(rawSamples) < 1:
# print("Read error")
pass
else:
self._samples_buffer = rawSamples
except Exception,e:
pass
if self._samples_buffer == None:
return ""
data = numpy.fromstring(self._samples_buffer, dtype=numpy.int16)
data = data * numpy.hanning(len(data))
left,right = numpy.split(numpy.abs(numpy.fft.fft(data)),2)
spec_y = numpy.add(left,right[::-1])
if log_scale:
spec_y=numpy.multiply(20,numpy.log10(spec_y))
if trim_by:
i=int((self.sampleSize/2)/trim_by)
spec_y=spec_y[:i]
if div_by:
spec_y=spec_y/float(div_by)
return spec_y
class MPDSpectrumPage(Page):
_Icons = {}
_Selector=None
_FootMsg = ["Nav","","","Back",""]
_MyList = []
_ListFont = MyLangManager.TrFont("veramono12")
_SongFont = MyLangManager.TrFont("notosanscjk12")
_PIFI = None
_FIFO = None
_Color = MySkinManager.GiveColor('Front')
_GobjectIntervalId = -1
_Queue = None
_KeepReading = True
_ReadingThread = None
_BGpng = None
_BGwidth = 320
_BGheight = 200
_SheepHead = None
_SheepHeadW = 69
_SheepHeadH = 66
_SheepBody = None
_SheepBodyW = 105
_SheepBodyH = 81
_RollCanvas = None
_RollW = 180
_RollH = 18
_freq_count = 0
_head_dir = 0
_Neighbor = None
_bby = []
_bbs = []
_capYPositionArray = []
_frames = 0
read_retry = 0
_queue_data = []
_vis_values = []
def __init__(self):
Page.__init__(self)
self._Icons = {}
self._CanvasHWND = None
self._MyList = []
self._PIFI = PIFI()
def Init(self):
self._PosX = self._Index * self._Screen._Width
self._Width = self._Screen._Width
self._Height = self._Screen._Height
self._CanvasHWND = self._Screen._CanvasHWND
self._RollCanvas = pygame.Surface(( self._RollW,self._RollH))
"""
self._BGpng = IconItem()
self._BGpng._ImgSurf = MyIconPool._Icons["sheep_bg"]
self._BGpng._MyType = ICON_TYPES["STAT"]
self._BGpng._Parent = self
self._BGpng.Adjust(0,0,self._BGwidth,self._BGheight,0)
self._SheepHead = IconItem()
self._SheepHead._ImgSurf = MyIconPool._Icons["sheep_head"]
self._SheepHead._MyType = ICON_TYPES["STAT"]
self._SheepHead._Parent = self
self._SheepHead.Adjust(0,0,self._SheepHeadW,self._SheepHeadH,0)
self._SheepBody = IconItem()
self._SheepBody._ImgSurf = MyIconPool._Icons["sheep_body"]
self._SheepBody._MyType = ICON_TYPES["STAT"]
self._SheepBody._Parent = self
self._SheepBody.Adjust(0,0,self._SheepBodyW,self._SheepBodyH,0)
"""
self._cwp_png = IconItem()
self._cwp_png._ImgSurf = MyIconPool._Icons["tape"]
self._cwp_png._MyType = ICON_TYPES["STAT"]
self._cwp_png._Parent = self
self._cwp_png.Adjust(0,0,79,79,0)
self._song_title = Label()
self._song_title.SetCanvasHWND(self._RollCanvas)
self._song_title.Init("Untitled",self._SongFont,MySkinManager.GiveColor('Text'))
self._title = Label()
self._title.SetCanvasHWND(self._CanvasHWND)
self._title.Init("Title:",self._ListFont,MySkinManager.GiveColor('Text'))
self._time = Label()
self._time.SetCanvasHWND(self._CanvasHWND)
self._time.Init("Time:",self._ListFont,MySkinManager.GiveColor('Text'))
self._time2 = Label()
self._time2.SetCanvasHWND(self._CanvasHWND)
self._time2.Init("00:00-00:00", self._ListFont,
MySkinManager.GiveColor('Text'))
self.Start()
def Start(self):
if self._Screen.CurPage() != self:
return
try:
self._FIFO = os.open(self._PIFI._MPD_FIFO, os.O_RDONLY | os.O_NONBLOCK)
t = Thread(target=self.GetSpectrum)
t.daemon = True # thread dies with the program
t.start()
self._ReadingThread = t
except IOError:
print("open %s failed"%self._PIFI._MPD_FIFO)
self._FIFO = None
return
def GetSpectrum(self):
while self._KeepReading and self._FIFO != None:
raw_samples = self._PIFI.GetSpectrum(self._FIFO)
if len(raw_samples) < 1:
#print("sleeping... 0.01")
time.sleep(0.01)
self.read_retry+=1
if self.read_retry > 20:
os.close(self._FIFO)
self._FIFO = os.open(self._PIFI._MPD_FIFO, os.O_RDONLY | os.O_NONBLOCK)
self.read_retry = 0
self.Playing()
else:
self.read_retry = 0
self._queue_data = raw_samples
self.Playing()
def Playing(self):
self._Screen.Draw()
self._Screen.SwapAndShow()
def ClearCanvas(self):
self._CanvasHWND.fill(MySkinManager.GiveColor('Black'))
def SgsSmooth(self):
passes = 1
points = 3
origs = self._bby[:]
for p in range(0,passes):
pivot = int(points/2.0)
for i in range(0,pivot):
self._bby[i] = origs[i]
self._bby[ len(origs) -i -1 ] = origs[ len(origs) -i -1 ]
smooth_constant = 1.0/(2.0*pivot+1.0)
for i in range(pivot, len(origs)-pivot):
_sum = 0.0
for j in range(0,(2*pivot)+1):
_sum += (smooth_constant * origs[i+j-pivot]) +j -pivot
self._bby[i] = _sum
if p < (passes - 1):
origs = self._bby[:]
def OnLoadCb(self):
if self._Neighbor != None:
pass
if self._KeepReading == False:
self._KeepReading = True
if self._FIFO == None:
self.Start()
def KeyDown(self,event):
if IsKeyMenuOrB(event.key):
try:
os.close(self._FIFO)
self._FIFO = None
except Exception, e:
print(e)
self._KeepReading = False
self._ReadingThread.join()
self._ReadingThread = None
self.ReturnToUpLevelPage()
self._Screen.Draw()
self._Screen.SwapAndShow()
def Draw(self):
self.ClearCanvas()
self._frames+=1
bw = 10
gap = 2
margin_bottom = 72
spects = None
meterNum = self._Width / float(bw +gap ) ## 320/12= 26
meter_left = meterNum - int(meterNum)
meter_left = meter_left*int(bw+gap)
margin_left = meter_left / 2 + gap
meterNum = int(meterNum)
self._cwp_png.NewCoord(43,159)
self._cwp_png.Draw()
if self._Neighbor != None:
if self._Neighbor._CurSongName != "":
self._song_title.SetText(self._Neighbor._CurSongName)
if self._Neighbor._CurSongTime != "":
times = self._Neighbor._CurSongTime
times_ = times.split(":")
if len(times_)> 1:
cur = int(times_[0])
end = int(times_[1])
if cur > 3600:
cur_text = time.strftime('%H:%M:%S', time.gmtime(cur))
else:
cur_text = time.strftime('%M:%S', time.gmtime(cur))
if end > 3600:
end_text = time.strftime('%H:%M:%S', time.gmtime(end))
else:
end_text = time.strftime('%M:%S', time.gmtime(end))
else:
cur_text = ""
end_text = times
self._time2.SetText(cur_text+"-"+end_text)
self._title.NewCoord(90,167)
self._title.Draw()
self._time.NewCoord(90,140)
self._time.Draw()
self._time2.NewCoord(135,140)
self._time2.Draw()
if self._RollCanvas != None:
# self._RollCanvas.fill((111,22,33))
self._RollCanvas.fill(MySkinManager.GiveColor('White'))
if self._song_title._Width > self._RollW:
if (self._song_title._PosX + self._song_title._Width) > self._RollW and self._frames % 30 == 0:
self._song_title._PosX -= 1
elif (self._song_title._PosX + self._song_title._Width) <= self._RollW and self._frames % 30 == 0:
self._song_title._PosX = 0
else:
self._song_title._PosX = 0
self._song_title.Draw()
self._CanvasHWND.blit(self._RollCanvas,(135,165,self._RollW,self._RollH))
try:
spects = self._queue_data
if len(spects) == 0:
return
# print("spects:",spects)
step = int( round( len( spects ) / meterNum) )
# print(len(spects))
self._bbs = []
a = numpy.logspace(0, 1, num=meterNum,endpoint=True)
for i in range(0,meterNum):
index = int(a[i] * step)
total = 0
value = spects[index]
self._bbs.append(value)
if len(self._bby) < len(self._bbs):
self._bby = self._bbs
elif len(self._bby) == len(self._bbs):
for i in range(0,len(self._bbs)):
self._bby[i] = (self._bby[i]+self._bbs[i])/2
self.SgsSmooth()
for i in range(0,meterNum):
value = self._bby[ i ]
if math.isnan(value) or math.isinf(value):
value = 0
value = value/32768.0
value = value * 123
value = value % (self._Height-gap-margin_bottom)
if len(self._vis_values) < len(self._bby):
self._vis_values.append(value)
elif len(self._vis_values) == len(self._bby):
if self._vis_values[i] < value:
self._vis_values[i] = value
except Exception,e:
print(e)
return
else: # got line
if len(self._vis_values) == 0:
return
for i in range(0,meterNum):
value = self._vis_values[i]
if len(self._capYPositionArray) < round(meterNum):
self._capYPositionArray.append(value)
if value < self._capYPositionArray[i]:
self._capYPositionArray[i]-=0.5
else:
self._capYPositionArray[i] = value
pygame.draw.rect(self._CanvasHWND,MySkinManager.GiveColor('Text'),(i*(bw+gap)+margin_left,self._Height-gap-self._capYPositionArray[i]-margin_bottom,bw,gap),0)
pygame.draw.rect(self._CanvasHWND,MySkinManager.GiveColor('Text'),(i*(bw+gap)+margin_left,self._Height-value-gap-margin_bottom,bw,value+gap),0)
self._vis_values[i] -= 2

View File

@@ -0,0 +1,327 @@
# -*- coding: utf-8 -*-
import os
import pygame
#from libs.roundrects import aa_round_rect
## local UI import
from UI.constants import Width,Height,ICON_TYPES
from UI.page import Page,PageSelector
from UI.label import Label
from UI.icon_item import IconItem
from UI.util_funcs import midRect
from UI.keys_def import CurKeys, IsKeyStartOrA, IsKeyMenuOrB
from UI.multi_icon_item import MultiIconItem
from UI.icon_pool import MyIconPool
from UI.scroller import ListScroller
from UI.skin_manager import MySkinManager
from UI.lang_manager import MyLangManager
from list_item import ListItem
import myvars
class MusicLibStack:
def __init__(self):
self.stack = list()
def Push(self,data):
if data not in self.stack:
self.stack.append(data)
return True
return False
def Pop(self):
if len(self.stack)<=0:
return None,False
return self.stack.pop(),True
def Last(self):
idx = len(self.stack) -1
if idx < 0:
return "/"
else:
return self.stack[ idx ]
def Length(self):
return len(self.stack)
class ListPageSelector(PageSelector):
_BackgroundColor = MySkinManager.GiveColor('Line')
def __init__(self):
self._PosX = 0
self._PosY = 0
self._Height = 0
self._Width = Width
def AnimateDraw(self,x2,y2):
pass
def Draw(self):
idx = self._Parent._PsIndex
if idx < len(self._Parent._MyList):
x = self._Parent._MyList[idx]._PosX+2
y = self._Parent._MyList[idx]._PosY+1
h = self._Parent._MyList[idx]._Height -3
self._PosX = x
self._PosY = y
self._Height = h
#aa_round_rect(self._Parent._CanvasHWND,
# (x,y,self._Width-4,h),self._BackgroundColor,4,0,self._BackgroundColor)
pygame.draw.rect(self._Parent._CanvasHWND,self._BackgroundColor,(x,y,self._Width-4,h),0)
class MusicLibListPage(Page):
_Icons = {}
_Selector=None
_FootMsg = ["Nav","","Scan","Back","Add to Playlist"]
_MyList = []
_SwapMyList = []
_ListFont = MyLangManager.TrFont("notosanscjk15")
_MyStack = None
_Scroller = None
_BGpng = None
_BGwidth = 56
_BGheight = 70
def __init__(self):
Page.__init__(self)
self._Icons = {}
self._CanvasHWND = None
self._MyList = []
self._SwapMyList = []
self._MyStack = MusicLibStack()
def SyncList(self,path):
if myvars.Poller == None:
return
alist = myvars.Poller.listfiles(path)
if alist == False:
print("listfiles return false")
return
self._MyList = []
self._SwapMyList = []
start_x = 0
start_y = 0
hasparent = 0
if self._MyStack.Length() > 0:
hasparent = 1
li = ListItem()
li._Parent = self
li._PosX = start_x
li._PosY = start_y
li._Width = Width
li._Fonts["normal"] = self._ListFont
li._MyType = ICON_TYPES["DIR"]
li._Parent = self
li.Init("[..]")
self._MyList.append(li)
for i,v in enumerate(sorted(alist)):
li = ListItem()
li._Parent = self
li._PosX = start_x
li._PosY = start_y + (i+hasparent)*ListItem._Height
li._Width = Width
li._Fonts["normal"] = self._ListFont
li._MyType = ICON_TYPES["FILE"]
li._Parent = self
if "directory" in v:
li._MyType = ICON_TYPES["DIR"]
dir_base_name = os.path.basename(v["directory"])
li.Init( dir_base_name )
li._Path = v["directory"]
elif "file" in v:
bname = os.path.basename(v["file"])
li.Init( bname )
li._Path = v["file"]
else:
li.Init("NoName")
self._MyList.append(li)
for i in self._MyList:
self._SwapMyList.append(i)
def Init(self):
self._PosX = self._Index * self._Screen._Width
self._Width = self._Screen._Width
self._Height = self._Screen._Height
self._CanvasHWND = self._Screen._CanvasHWND
ps = ListPageSelector()
ps._Parent = self
self._Ps = ps
self._PsIndex = 0
self.SyncList("/")
icon_for_list = MultiIconItem()
icon_for_list._ImgSurf = MyIconPool._Icons["sys"]
icon_for_list._MyType = ICON_TYPES["STAT"]
icon_for_list._Parent = self
icon_for_list.Adjust(0,0,18,18,0)
self._Icons["sys"] = icon_for_list
self._BGpng = IconItem()
self._BGpng._ImgSurf = MyIconPool._Icons["empty"]
self._BGpng._MyType = ICON_TYPES["STAT"]
self._BGpng._Parent = self
self._BGpng.AddLabel(MyLangManager.Tr("Please upload data over Wi-Fi"), MyLangManager.TrFont("varela18"))
self._BGpng.SetLableColor(MySkinManager.GiveColor('Text'))
self._BGpng.Adjust(0,0,self._BGwidth,self._BGheight,0)
self._Scroller = ListScroller()
self._Scroller._Parent = self
self._Scroller._PosX = self._Width - 10
self._Scroller._PosY = 2
self._Scroller.Init()
def Click(self):
if len(self._MyList) == 0:
return
cur_li = self._MyList[self._PsIndex]
if cur_li._MyType == ICON_TYPES["DIR"]:
if cur_li._Path == "[..]":
self._MyStack.Pop()
self.SyncList( self._MyStack.Last() )
self._PsIndex = 0
else:
self._MyStack.Push( self._MyList[self._PsIndex]._Path )
self.SyncList( self._MyStack.Last() )
self._PsIndex = 0
if cur_li._MyType == ICON_TYPES["FILE"]: ## add to playlist only
myvars.Poller.addfile(cur_li._Path)
myvars.PlayListPage.SyncList()
print("add" , cur_li._Path)
self._Screen.Draw()
self._Screen.SwapAndShow()
def Rescan(self):
self.SyncList("/")
self._PsIndex = 0
def KeyDown(self,event):
if IsKeyMenuOrB(event.key) or event.key == CurKeys["Left"]:
self.ReturnToUpLevelPage()
self._Screen.Draw()
self._Screen.SwapAndShow()
if event.key == CurKeys["Up"]:
self.ScrollUp()
self._Screen.Draw()
self._Screen.SwapAndShow()
if event.key == CurKeys["Down"]:
self.ScrollDown()
self._Screen.Draw()
self._Screen.SwapAndShow()
"""
if event.key == CurKeys["Right"]:
self.FScrollDown(Step=5)
self._Screen.Draw()
self._Screen.SwapAndShow()
if event.key == CurKeys["Left"]:
self.FScrollUp(Step=5)
self._Screen.Draw()
self._Screen.SwapAndShow()
"""
if event.key == CurKeys["X"]:
self.Rescan()
self._Screen.Draw()
self._Screen.SwapAndShow()
if IsKeyStartOrA(event.key):
self.Click()
def Draw(self):
self.ClearCanvas()
"""
start_x = 0
start_y = 0
counter = 0
self._MyList = []
for i,v in enumerate(self._SwapMyList):
if myvars.PlayListPage.InPlayList(v._Path):
v._Active = True
else:
v._Active = False
if v._Active == False:
v.NewCoord(start_x, start_y+counter* ListItem._Height)
counter+=1
self._MyList.append(v)
"""
if len(self._MyList) == 0:
self._BGpng.NewCoord(self._Width/2,self._Height/2)
self._BGpng.Draw()
return
else:
if len(self._MyList) * ListItem._Height > self._Height:
self._Ps._Width = self._Width - 11
self._Ps.Draw()
for i in self._MyList:
if myvars.PlayListPage.InPlayList(i._Path):
i._Active = True
else:
i._Active = False
if i._PosY > self._Height + self._Height/2:
break
if i._PosY < 0:
continue
i.Draw()
self._Scroller.UpdateSize( len(self._MyList)*ListItem._Height, self._PsIndex*ListItem._Height)
self._Scroller.Draw()
else:
self._Ps._Width = self._Width
self._Ps.Draw()
for i in self._MyList:
if myvars.PlayListPage.InPlayList(i._Path):
i._Active = True
else:
i._Active = False
if i._PosY > self._Height + self._Height/2:
break
if i._PosY < 0:
continue
i.Draw()

View File

@@ -0,0 +1,8 @@
Poller = None # MPD Poller
PlayListPage = None
MusicLibListPage = None
SpectrumPage = None

View File

@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
from libs.MPD import poller
from play_list_page import PlayListPage
from music_lib_list_page import MusicLibListPage
from mpd_spectrum_page import MPDSpectrumPage
import myvars
from config import MPD_socket
def InitPoller():
try:
myvars.Poller = poller.MPDPoller(host=MPD_socket)
myvars.Poller.connect()
except:
myvars.Poller = None
def InitMusicLibPage(main_screen):
myvars.MusicLibListPage = MusicLibListPage()
myvars.MusicLibListPage._Screen = main_screen
myvars.MusicLibListPage._Name = "Music Library"
myvars.MusicLibListPage.Init()
def InitListPage(main_screen):
myvars.PlayListPage = PlayListPage()
myvars.PlayListPage._Screen = main_screen
myvars.PlayListPage._Name = "Play List"
myvars.PlayListPage.Init()
def InitSpectrumPage(main_screen):
myvars.SpectrumPage = MPDSpectrumPage()
myvars.SpectrumPage._Screen = main_screen
myvars.SpectrumPage._Name = "GameShell RTA"
myvars.SpectrumPage.Init()

View File

@@ -0,0 +1,327 @@
# -*- coding: utf-8 -*-
import os
import pygame
import gobject
#from libs.roundrects import aa_round_rect
## local UI import
from UI.constants import Width,Height,ICON_TYPES
from UI.page import Page,PageSelector
from UI.icon_item import IconItem
from UI.label import Label
from UI.util_funcs import midRect
from UI.keys_def import CurKeys, IsKeyStartOrA, IsKeyMenuOrB
from UI.icon_pool import MyIconPool
from UI.skin_manager import MySkinManager
from UI.lang_manager import MyLangManager
from UI.scroller import ListScroller
from list_item import ListItem
import myvars
class ListPageSelector(PageSelector):
_BackgroundColor = MySkinManager.GiveColor('Line')
def __init__(self):
self._PosX = 0
self._PosY = 0
self._Height = 0
self._Width = Width
def AnimateDraw(self,x2,y2):
pass
def Draw(self):
idx = self._Parent._PsIndex
if idx > (len(self._Parent._MyList)-1):
idx = len(self._Parent._MyList)
if idx > 0:
idx -=1
elif idx == 0: #Nothing
return
x = self._Parent._MyList[idx]._PosX+2
y = self._Parent._MyList[idx]._PosY+1
h = self._Parent._MyList[idx]._Height -3
self._PosX = x
self._PosY = y
self._Height = h
#aa_round_rect(self._Parent._CanvasHWND,
# (x,y,self._Width-4,h),self._BackgroundColor,4,0,self._BackgroundColor)
pygame.draw.rect(self._Parent._CanvasHWND,self._BackgroundColor,(x,y,self._Width-4,h),0)
class PlayListPage(Page):
_Icons = {}
_Selector=None
_FootMsg = ["Nav","Remove","RTA","Back","Play/Pause"]
_MyList = []
_ListFont = MyLangManager.TrFont("notosanscjk15")
_Scroller = None
_CurSongTime="0:0"
_BGpng = None
_BGwidth = 75
_BGheight = 70
_Scrolled = 0
_CurSongName = ""
def __init__(self):
self._Icons = {}
Page.__init__(self)
self._CanvasHWND = None
self._MyList = []
def SyncList(self):
self._MyList = []
start_x = 0
start_y = 0
if myvars.Poller == None:
return
play_list = myvars.Poller.playlist()
for i,v in enumerate(play_list):
li = ListItem()
li._Parent = self
li._PosX = start_x
li._PosY = start_y + i*ListItem._Height
li._Width = Width
li._Fonts["normal"] = self._ListFont
if "title" in v:
if isinstance(v["title"], (list,)):
li.Init(" | ".join(v["title"]))
else:
li.Init( v["title"])
if "file" in v:
li._Path = v["file"]
elif "file" in v:
li.Init(os.path.basename(v["file"]))
li._Path = v["file"]
else:
li.Init("NoName")
li._Labels["Text"]._PosX = 7
self._MyList.append(li)
self.SyncPlaying()
def GObjectInterval(self): ## 250 ms
self.SyncPlaying()
if self._Screen.CurPage() == self:
self._Screen.Draw()
self._Screen.SwapAndShow()
return True
def SyncPlaying(self):
if myvars.Poller == None:
return
current_song = myvars.Poller.poll()
for i ,v in enumerate(self._MyList):
self._MyList[i]._Playing = False
self._MyList[i]._PlayingProcess = 0
if current_song != None:
if "song" in current_song:
posid = int(current_song["song"])
if posid < len(self._MyList): # out of index
self._CurSongName = self._MyList[posid]._Text
if "state" in current_song:
if current_song["state"] == "stop":
self._MyList[posid]._Playing = False
else:
self._MyList[posid]._Playing = True
if "time" in current_song:
self._CurSongTime = current_song["time"]
times_ = current_song["time"].split(":")
if len(times_)> 1:
cur = float(times_[0])
end = float(times_[1])
pros = int((cur/end)*100.0)
self._MyList[posid]._PlayingProcess = pros
def InPlayList(self,path):
for i,v in enumerate(self._MyList):
if v._Path == path:
return True
def Init(self):
self._PosX = self._Index * self._Screen._Width
self._Width = self._Screen._Width
self._Height = self._Screen._Height
self._CanvasHWND = self._Screen._CanvasHWND
ps = ListPageSelector()
ps._Parent = self
self._Ps = ps
self._PsIndex = 0
self.SyncList()
gobject.timeout_add(850,self.GObjectInterval)
self._BGpng = IconItem()
self._BGpng._ImgSurf = MyIconPool._Icons["heart"]
self._BGpng._MyType = ICON_TYPES["STAT"]
self._BGpng._Parent = self
self._BGpng.AddLabel(MyLangManager.Tr("my favorite music"), MyLangManager.TrFont("varela18"))
self._BGpng.SetLableColor(MySkinManager.GiveColor('Text'))
self._BGpng.Adjust(0,0,self._BGwidth,self._BGheight,0)
self._Scroller = ListScroller()
self._Scroller._Parent = self
self._Scroller._PosX = self._Width - 10
self._Scroller._PosY = 2
self._Scroller.Init()
def ScrollUp(self):
if len(self._MyList) == 0:
return
self._PsIndex -= 1
if self._PsIndex < 0:
self._PsIndex = 0
cur_li = self._MyList[self._PsIndex]
if cur_li._PosY < 0:
for i in range(0, len(self._MyList)):
self._MyList[i]._PosY += self._MyList[i]._Height
self._Scrolled +=1
def ScrollDown(self):
if len(self._MyList) == 0:
return
self._PsIndex +=1
if self._PsIndex >= len(self._MyList):
self._PsIndex = len(self._MyList) -1
cur_li = self._MyList[self._PsIndex]
if cur_li._PosY +cur_li._Height > self._Height:
for i in range(0,len(self._MyList)):
self._MyList[i]._PosY -= self._MyList[i]._Height
self._Scrolled -=1
def SyncScroll(self):# show where it left
if self._Scrolled == 0:
return
if self._PsIndex < len(self._MyList):
cur_li = self._MyList[self._PsIndex]
if self._Scrolled > 0:
if cur_li._PosY < 0:
for i in range(0, len(self._MyList)):
self._MyList[i]._PosY += self._Scrolled * self._MyList[i]._Height
elif self._Scrolled < 0:
if cur_li._PosY +cur_li._Height > self._Height:
for i in range(0,len(self._MyList)):
self._MyList[i]._PosY += self._Scrolled * self._MyList[i]._Height
def Click(self):
if len(self._MyList) == 0:
return
cur_li = self._MyList[self._PsIndex]
play_pos_id = myvars.Poller.play(self._PsIndex)
self.SyncPlaying()
self._Screen.Draw()
self._Screen.SwapAndShow()
def OnReturnBackCb(self): # return from music_lib_list_page
self.SyncList()
self.SyncScroll()
def KeyDown(self,event):
if IsKeyMenuOrB(event.key):
if myvars.Poller != None:
myvars.Poller.stop()
self._CurSongTime=""
self._CurSongName=""
self.ReturnToUpLevelPage()
self._Screen.Draw()
self._Screen.SwapAndShow()
if event.key == CurKeys["Up"]:
self.ScrollUp()
self._Screen.Draw()
self._Screen.SwapAndShow()
if event.key == CurKeys["Down"]:
self.ScrollDown()
self._Screen.Draw()
self._Screen.SwapAndShow()
if event.key == CurKeys["Right"]:#add
self._Screen.PushCurPage()
self._Screen.SetCurPage(myvars.MusicLibListPage)
self._Screen.Draw()
self._Screen.SwapAndShow()
if event.key == CurKeys["Y"]:# del selected songs
myvars.Poller.delete(self._PsIndex)
self.SyncList()
self._Screen.Draw()
self._Screen.SwapAndShow()
if IsKeyStartOrA(event.key):
self.Click()
if event.key == CurKeys["X"]: # start spectrum
myvars.SpectrumPage._Neighbor = self
self._Screen.PushPage(myvars.SpectrumPage)
self._Screen.Draw()
self._Screen.SwapAndShow()
def Draw(self):
self.ClearCanvas()
if len(self._MyList) == 0:
self._BGpng.NewCoord(self._Width/2,self._Height/2)
self._BGpng.Draw()
return
else:
if len(self._MyList) * ListItem._Height > self._Height:
self._Ps._Width = self._Width - 11
self._Ps.Draw()
for i in self._MyList:
if i._PosY > self._Height + self._Height/2:
break
if i._PosY < 0:
continue
i.Draw()
self._Scroller.UpdateSize( len(self._MyList)*ListItem._Height, self._PsIndex*ListItem._Height)
self._Scroller.Draw()
else:
self._Ps._Width = self._Width
self._Ps.Draw()
for i in self._MyList:
if i._PosY > self._Height + self._Height/2:
break
if i._PosY < 0:
continue
i.Draw()