Merge pull request #125 from KaboPC/patch2

Update filter.py and tab_panel.py to allow tabbed filter inputs
This commit is contained in:
Tyler Kennedy 2012-09-18 02:10:52 -07:00
commit db44c6c437
2 changed files with 219 additions and 89 deletions

View File

@ -4,10 +4,16 @@
#
################################################################
from pygame import Rect
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from albow import *
from pygame import Rect, Surface, draw, image
from pygame.locals import SRCALPHA
from widget import Widget
from theme import ThemeProperty, FontProperty
from utils import brighten
from numpy import fromstring
class TabPanel(Widget):
@ -23,6 +29,7 @@ class TabPanel(Widget):
default_tab_bg_color = ThemeProperty('default_tab_bg_color')
tab_area_bg_color = ThemeProperty('tab_area_bg_color')
tab_dimming = ThemeProperty('tab_dimming')
tab_titles = None
#use_page_bg_color_for_tabs = ThemeProperty('use_page_bg_color_for_tabs')
def __init__(self, pages=None, **kwds):
@ -150,3 +157,63 @@ class TabPanel(Widget):
i = (x - m) * n // width
if 0 <= i < n:
return i
def gl_draw_self(self, root, offset):
self.gl_draw(root, offset)
def gl_draw(self, root, offset):
pages = self.pages
if len(pages) > 1:
tlcorner = (offset[0] + self.bottomleft[0], offset[1] + self.bottomleft[1])
pageTabContents = []
current_page = self.current_page
n = len(pages)
b = self.tab_border_width
s = self.tab_spacing
h = self.tab_height
m = self.tab_margin
tabWidth = (self.size[0]-(s*n)-(2*m))/n
width = self.width - 2 * m + s - b
x0 = m + tlcorner[0]
font = self.tab_font
fg = self.tab_fg_color
surface = Surface(self.size, SRCALPHA)
glEnable(GL_BLEND)
for i, page in enumerate(pages):
x1 = x0+tabWidth
selected = page is current_page
if selected:
glColor(1.0, 1.0, 1.0, 0.5)
else:
glColor(0.5, 0.5, 0.5, 0.5)
glRectf(x0, tlcorner[1]-(m+b), x1, tlcorner[1]-(h))
buf = font.render(self.pages[i].tab_title, True, self.fg_color or fg)
r = buf.get_rect()
offs = ((tabWidth - r.size[0])/2) + m +((s+tabWidth)*i)
surface.blit(buf, (offs, m))
x0 = x1 + s
data = image.tostring(surface, 'RGBA', 1)
rect = self.rect.move(offset)
w, h = root.size
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0, w, 0, h)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glRasterPos2i(rect.left, h - rect.bottom)
glPushAttrib(GL_COLOR_BUFFER_BIT)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glDrawPixels(self.width, self.height,
GL_RGBA, GL_UNSIGNED_BYTE, fromstring(data, dtype='uint8'))
glPopAttrib()
glFlush()
glDisable(GL_BLEND)

View File

@ -13,6 +13,7 @@ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE."""
from toolbasics import *
from albow.dialogs import wrapped_label
from albow import *
def alertFilterException(func):
@ -25,93 +26,7 @@ def alertFilterException(func):
return _func
class FilterModuleOptions(Widget):
is_gl_container = True
def __init__(self, tool, module, *args, **kw):
Widget.__init__(self, *args, **kw)
rows = []
self.optionDict = {}
self.tool = tool
cols = []
height = 0
max_height = 550
print "Creating options for ", module
if hasattr(module, "inputs"):
for optionName, optionType in module.inputs:
if isinstance(optionType, tuple):
if isinstance(optionType[0], (int, long, float)):
if len(optionType) > 2:
val, min, max = optionType
elif len(optionType) == 2:
min, max = optionType
val = min
rows.append(self.addNumberField(optionName, val, min, max))
if isinstance(optionType[0], (str, unicode)):
choiceButton = ChoiceButton(map(str, optionType))
self.optionDict[optionName] = AttrRef(choiceButton, 'selectedChoice')
rows.append(Row((Label(optionName), choiceButton)))
elif isinstance(optionType, bool):
cbox = CheckBox(value=optionType)
self.optionDict[optionName] = AttrRef(cbox, 'value')
row = Row((Label(optionName), cbox))
rows.append(row)
elif isinstance(optionType, (int, float)):
rows.append(self.addNumberField(optionName, optionType))
elif optionType == "blocktype" or isinstance(optionType, materials.Block):
blockButton = BlockButton(self.tool.editor.level.materials)
if isinstance(optionType, materials.Block):
blockButton.blockInfo = optionType
row = Column((Label(optionName), blockButton))
self.optionDict[optionName] = AttrRef(blockButton, 'blockInfo')
rows.append(row)
elif optionType == "label":
rows.append(wrapped_label(optionName, 50))
elif optionType == "string":
field = TextField(value="string")
self.optionDict[optionName] = AttrRef(field, 'value')
row = Row((Label(optionName), field))
rows.append(row)
else:
raise ValueError(("Unknown option type", optionType))
height = sum(r.height for r in rows)
if height > max_height:
h = 0
for i, r in enumerate(rows):
h += r.height
if h > height / 2:
break
cols.append(Column(rows[:i]))
rows = rows[i:]
#cols.append(Column(rows))
if len(rows):
cols.append(Column(rows))
if len(cols):
self.add(Row(cols))
self.shrink_wrap()
else:
self.size = (0, 0)
def addNumberField(self, optionName, val, min=None, max=None):
def addNumField(page, optionName, val, min=None, max=None):
if isinstance(val, float):
ftype = FloatField
else:
@ -122,11 +37,159 @@ class FilterModuleOptions(Widget):
max = None
field = ftype(value=val, width=100, min=min, max=max)
self.optionDict[optionName] = AttrRef(field, 'value')
page.optionDict[optionName] = AttrRef(field, 'value')
row = Row([Label(optionName), field])
return row
class FilterModuleOptions(Widget):
is_gl_container = True
def __init__(self, tool, module, *args, **kw):
Widget.__init__(self, *args, **kw)
self.tool = tool
pages = TabPanel()
pages.is_gl_container = True
self.pages = pages
self.optionDict = {}
pageTabContents = []
print "Creating options for ", module
if hasattr(module, "inputs"):
if isinstance(module.inputs, list):
for tabData in module.inputs:
title, page, pageRect = self.makeTabPage(self.tool, tabData)
pages.add_page(title, page)
pages.set_rect(pageRect.union(pages._rect))
elif isinstance(module.inputs, tuple):
title, page, pageRect = self.makeTabPage(self.tool, module.inputs)
pages.add_page(title, page)
pages.set_rect(pageRect)
else:
self.size = (0, 0)
pages.shrink_wrap()
self.add(pages)
self.shrink_wrap()
if len(pages.pages):
if(pages.current_page != None):
pages.show_page(pages.current_page)
else:
pages.show_page(pages.pages[0])
for eachPage in pages.pages:
self.optionDict = dict(self.optionDict.items() + eachPage.optionDict.items())
def makeTabPage(self, tool, inputs):
page = Widget()
page.is_gl_container = True
rows = []
cols = []
height = 0
max_height = 550
page.optionDict = {}
page.tool = tool
title = "Tab"
for optionName, optionType in inputs:
if isinstance(optionType, tuple):
if isinstance(optionType[0], (int, long, float)):
if len(optionType) > 2:
val, min, max = optionType
elif len(optionType) == 2:
min, max = optionType
val = min
rows.append(addNumField(page, optionName, val, min, max))
if isinstance(optionType[0], (str)):
if len(optionType) == 3:
a,b,c = optionType
if a == "strValSize":
field = TextField(value=b, width=c)
page.optionDict[optionName] = AttrRef(field, 'value')
row = Row((Label(optionName), field))
rows.append(row)
elif len(optionType) == 2:
a,b = optionType
if a == "strVal":
field = TextField(value=b, width=200)
page.optionDict[optionName] = AttrRef(field, 'value')
row = Row((Label(optionName), field))
rows.append(row)
elif a == "strSize":
field = TextField(value="Input String Here", width=b)
page.optionDict[optionName] = AttrRef(field, 'value')
row = Row((Label(optionName), field))
rows.append(row)
if isinstance(optionType[0], (str, unicode)):
choiceButton = ChoiceButton(map(str, optionType))
page.optionDict[optionName] = AttrRef(choiceButton, 'selectedChoice')
rows.append(Row((Label(optionName), choiceButton)))
elif isinstance(optionType, bool):
cbox = CheckBox(value=optionType)
page.optionDict[optionName] = AttrRef(cbox, 'value')
row = Row((Label(optionName), cbox))
rows.append(row)
elif isinstance(optionType, (int, float)):
rows.append(addNumField(self, optionName, optionType))
elif optionType == "blocktype" or isinstance(optionType, materials.Block):
blockButton = BlockButton(tool.editor.level.materials)
if isinstance(optionType, materials.Block):
blockButton.blockInfo = optionType
row = Column((Label(optionName), blockButton))
page.optionDict[optionName] = AttrRef(blockButton, 'blockInfo')
rows.append(row)
elif optionType == "label":
rows.append(wrapped_label(optionName, 50))
elif optionType == "string":
field = TextField(value="Input String Here", width=200)
page.optionDict[optionName] = AttrRef(field, 'value')
row = Row((Label(optionName), field))
rows.append(row)
elif optionType == "title":
title = optionName
else:
raise ValueError(("Unknown option type", optionType))
height = sum(r.height for r in rows)
if height > max_height:
h = 0
for i, r in enumerate(rows):
h += r.height
if h > height / 2:
break
cols.append(Column(rows[:i]))
rows = rows[i:]
#cols.append(Column(rows))
if len(rows):
cols.append(Column(rows))
if len(cols):
page.add(Row(cols))
page.shrink_wrap()
return (title, page, page._rect)
@property
def options(self):
return dict((k, v.get()) for k, v in self.optionDict.iteritems())