Creating a Class to generate ControlButtons
#1
i need help from a guru. the code is almost complete.

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...

Quote: def buildbuttons(self,dict,buttonset,blposx,blposy,blposxdist,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.btnevent[n])
self.btnevent[n].controldown(self.btnevent[(n+1)%totalnum]) # (n+1)%totalnum --- wraps navigation
self.btnevent[(n+buttonspercolumn)%buttonspercolumn].controlleft(self.btnevent[n])
self.btnevent[n].controlright(self.btnevent[(n+buttonspercolumn)%buttonspercolumn])
I'm not an expert but I play one at work.
Reply
#2
since we have setcoordinateresolution which automatically handles scaling the self.scalex self.scaley is unnecessary and not recommended.

(n+buttonspercolumn)%buttonspercolumn will return a value from 0...buttonspercolumn-1 which is not what you want...

Quote:buttonspercol = height
xxxxx totalnum% buttonspercolumn in last col
xxxxx
xxxxx
xxxx
xxxx
xxxx
xxxx
i think what makes most sense is this
Quote: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.



Reply
#3
i have eliminated the scaling in the past and the full screen images i'm trying to display show up as blank screens... i added the scaling, and bingo, it worked properly.

i actually worked on the code a bit and come up with something before i saw your post. let me know what you think...

Quote: def buildbuttons(self,dict,buttonset,blposx,blposy,blposxdist,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,bname,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.btnevent[n])
self.btnevent[n].controldown(self.btnevent[(n+1)%totalnum]) # (n+1)%totalnum --- wraps navigation
self.btnevent[n].controlleft(self.btnevent[(n-buttonspercolumn)%totalnum])
self.btnevent[n].controlright(self.btnevent[(n+buttonspercolumn)%totalnum])



I'm not an expert but I play one at work.
Reply
#4
Quote:xxxxx totalnum% buttonspercolumn in last col
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)
Reply
#5
about your blank bg. i noticed in your one script, you're using the setcoordinateresolution() wrong.

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).



For python coding questions first see http://mirrors.xbmc.org/docs/python-docs/
Reply
#6
asteron,
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.

Quote: def buildbuttons(self,dict,buttonset,blposx,blposy,blposxdist,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,bname,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'm not an expert but I play one at work.
Reply
#7
len(self.dictbuttons) <=> len(self.btnevent)

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.
Reply

Logout Mark Read Team Forum Stats Members Help
Creating a Class to generate ControlButtons0