Creating a Class to generate ControlButtons
the goal is to create a class that anyone can use to create a set of controlbuttons from either a text file or using file names from a directory.
i will make everything adjustable: number of buttons per column, button sizes, column start x & y positions, etc.
this will make dynamic button creation much easier for people wanting to make scripts and require much less manual coding for everyone!
i have everything in-place an working just need help with debugging the navigation. if you can help, i'd appreciate it!
here's the code that creates the buttons from inputs...
def buildbuttons(self,dict,buttonset,blposx,blposy,blp osxdist,blposydist):
self.btnevent = []
totalnum = len(self.dictbuttons)
c = 0 # column counter
for n in range(totalnum): # loop to create buttons
item = dict[n]
bname = item['name']
bfile = item['file']
cp = (n %buttonspercolumn)+(buttonspercolumn-1)/buttonspercolumn # normalize position of buttons in column
self.posy = blposy + (blposydist * cp) # set vertical position
self.posx = blposx + (blposxdist * c)
btn = xbmcgui.controlbutton(int(self.posx * self.scalex),int(self.posy * self.scaley),int(140 * self.scalex),int(27 * self.scaley),bname,textxoffset=24)
self.addcontrol(btn)
self.btnevent.append(btn)
c = (n-(n %buttonspercolumn)+(buttonspercolumn-1))/buttonspercolumn # normalize to multiple of buttons per column
for n in range(totalnum): # loop to create button navigation
self.btnevent[(n+1)%totalnum].controlup(self.btnev ent[n])
self.btnevent[n].controldown(self.btnevent[(n+1)%t otalnum]) # (n+1)%totalnum --- wraps navigation
self.btnevent[(n+buttonspercolumn)%buttonspercolum n].controlleft(self.btnevent[n])
self.btnevent[n].controlright(self.btnevent[(n+but tonspercolumn)%buttonspercolumn])
(n+buttonspercolumn)%buttonspercolumn will return a value from 0...buttonspercolumn-1 which is not what you want...
buttonspercol = height
xxxxx totalnum% buttonspercolumn in last col
xxxxx
xxxxx
xxxx
xxxx
xxxx
xxxx
i think what makes most sense is this
nei = n + buttonspercolumn
if nei >= totalnum:
nei = nei % buttonspercolumn
calculate nei beforehand and replace the
(n+buttonspercolumn)%buttonspercolumn with nei.
ohh and to calculate the column
c = n/buttonspercolumn
will be fine since numbers are rounded in integer division.
as for the l and the r you would need to pass an optional set of controls and loop through the first btnspercolumn buttons and set their left control and the last len(btnevent)%btnspercolumn and set their controlright.
bxxxx
xxxxx
xxxx
cxxa
xxxx
xxxx
that will work but in the above going right from a will jump to b while with mine going right will jump to c.
im not entirely sure what that c and cp expression evaluates to (but i think you want row, col). it probably works fine but i would have just used
r = n % butspercol
c = n / butspercol
i think there is also
r,c = divmod(n,butspercol)
thanks for the help, works well except i get an index error after exiting the pop-up
errors on self.btnevent[i]:
for i in range(len(self.dictbuttons)):
if control == self.btnevent[i]:
here's the current button code... notice i limit the buttons that are created due ot the number of columns configured. i know this is causing the issue but not sure why. the range should be a subset of the entire dictionary.
def buildbuttons(self,dict,buttonset,blposx,blposy,blp osxdist,blposydist):
self.btnevent = []
c = 0 # column counter
btns = 0
totalnum = len(self.dictbuttons)
for n in range(totalnum): # loop to create buttons
c = n/buttonspercolumn
r = n%buttonspercolumn
if c == columns: break
item = dict[n]
bname = item['name']
bfile = item['file']
self.posy = blposy + (blposydist * r) # vertical position
self.posx = blposx + (blposxdist * c)
btn = xbmcgui.controlbutton(self.posx,self.posy,140,27,b name,textxoffset=24)
self.addcontrol(btn)
self.btnevent.append(btn)
btns = btns + 1
for n in range(btns): # loop to create button navigation
nud = (n+1)%btns
nei = n + buttonspercolumn
if nei >= btns:
nei = nei % buttonspercolumn
self.btnevent[nud].controlup(self.btnevent[n])
self.btnevent[n].controldown(self.btnevent[nud]) # (n+1)%totalnum --- wraps navigation
self.btnevent[nei].controlleft(self.btnevent[n])
self.btnevent[n].controlright(self.btnevent[nei])
now for the hard part... any idea how to get the "outer" buttons to have the properties to allow the focus to navigate to other controls?
example:
l c r
l c r
l c r
l's would have controlleft to a different control
r's would have controlright to different control
all top buttons have controlup to different control
... etc
trying to make an all-purpose class.
thanks!
i actually worked on the code a bit and come up with something before i saw your post. let me know what you think...
def buildbuttons(self,dict,buttonset,blposx,blposy,blp osxdist,blposydist):
self.btnevent = []
c = 0 # column counter
totalnum = 0
btnnum = len(self.dictbuttons)
for n in range(btnnum): # calc # of buttons given # columns requested
c = n/buttonspercolumn
if c == columns: break
totalnum = totalnum + 1
for n in range(totalnum): # loop to create buttons
item = dict[n]
bname = item['name']
bfile = item['file']
c = n/buttonspercolumn
cp = (n %buttonspercolumn)+(buttonspercolumn-1)/buttonspercolumn # normalize position of buttons in column
self.posy = blposy + (blposydist * cp) # set vertical position
self.posx = blposx + (blposxdist * c)
btn = xbmcgui.controlbutton(self.posx,self.posy,140,27,b name,textxoffset=24)
self.addcontrol(btn)
self.btnevent.append(btn)
for n in range(totalnum): # loop to create button navigation
self.btnevent[(n+1)%totalnum].controlup(self.btnev ent[n])
self.btnevent[n].controldown(self.btnevent[(n+1)%t otalnum]) # (n+1)%totalnum --- wraps navigation
self.btnevent[n].controlleft(self.btnevent[(n-buttonspercolumn)%totalnum])
self.btnevent[n].controlright(self.btnevent[(n+but tonspercolumn)%totalnum])
add the control with your screens resolution i.e.(0,0,720,480) and do not use the getresolution() for input to the setcoordinateresolution(), just use the resolution your developing in (i.e. ntsc_4x3).
#If you have any other info about this subject , Please add it free.# |