Vizard 8 » Tutorials & Examples » Example scripts » GUIs » vizMenu
8.0

VizMenu

\examples\graphicalUserInterfaces\Vizmenu\tutorial_vizmenu.py

This example script demonstrates the use of vizmenu.

"""
This example demonstrates the use of vizmenu.
Move the mouse to the gray at the top of the screen to unhide the menu.
Press 'c' to put the menu in the center of the screen.
Press 'l' to put the menu on the left.
"""
import viz
import vizact

viz.setMultiSample(4)
viz.fov(60)
viz.go()

import vizinfo
vizinfo.InfoPanel(align=viz.ALIGN_RIGHT_BOTTOM)

viz.clearcolor( viz.SLATE )
viz.addChild( 'ground.osgb' )

#Setup color schemes for menu
defaultTheme = viz.getTheme()

#Set in colors for dark theme
darkTheme = viz.Theme()
darkTheme.borderColor = (0.1,0.1,0.1,1)
darkTheme.backColor = (0.4,0.4,0.4,1)
darkTheme.lightBackColor = (0.6,0.6,0.6,1)
darkTheme.darkBackColor = (0.2,0.2,0.2,1)
darkTheme.highBackColor = (0.2,0.2,0.2,1)

#Set in colors for green theme
greenTheme = viz.Theme()
greenTheme.borderColor = (0.1,0.2,0.05,1)
greenTheme.backColor = (0.5,0.7,0.3,1)
greenTheme.lightBackColor = (0.6,0.8,0.4,1)
greenTheme.darkBackColor = (0.2,0.4,0.1,1)
greenTheme.highBackColor = (0.2,0.4,0.1,1)
greenTheme.highTextColor = (0.95,0.88,0.33,1)

themes = ( defaultTheme, darkTheme, greenTheme )


#Add balls for the menu options to affect
import vizshape
ball1 = vizshape.addSphere(radius=0.1)
ball2 = ball1.copy()

ball1.setPosition([-.5, 1.6, 2.5])
ball2.setPosition([.5, 1.6, 2.5])

#Create main menu object
import vizmenu
menu = vizmenu.add()

#Place the menu in the center or on the left
vizact.onkeydown( 'c', menu.setAlignment, vizmenu.CENTER )
vizact.onkeydown( 'l', menu.setAlignment, vizmenu.LEFT )

#Create three menu subjects
AppearanceMenu = menu.add( 'Appearance' )
Ball1Menu = menu.add( 'Ball 1' )
Ball2Menu = menu.add( 'Ball 2' )


#Appearance Menu

#Add dropdown list of theme options to Appearance menu
themeDropDown = AppearanceMenu.add( viz.DROPLIST, 'Theme' )
themeDropDown.addItems( ['Default','Dark','Green'] )

def onTheme( e ):
    #Change menu theme when new item in drop-down list is selected
    viz.setTheme( themes[e.newSel] )

#Call onTheme when themeDropDown is changed
vizact.onlist( themeDropDown, onTheme )

#Create a number of sub-menus with labels
fooMenu = AppearanceMenu.add( vizmenu.MENU, 'foo' )
barMenu = fooMenu.add( vizmenu.MENU, 'bar' )

#Add textbox to sub-sub-menu
messageBox = barMenu.add( viz.TEXTBOX, 'Textbox' )

#Add object to display what is in the textbox
onScreenText = viz.addText( '', viz.SCREEN )

def textChanged(e):
    #The GUI textbox e.object has been modified
    #e.newText is the new text
    #e.oldText is the previous text
    #e.key is the key that has just been pressed
    onScreenText.message( e.newText )

vizact.ontextbox(messageBox, textChanged)

#Ball1 menu

#Create sub-menu to house ball color radio buttons
Ball1ColorMenu = Ball1Menu.add( vizmenu.MENU, 'Color' )

#Add ball color modifying radio buttons
Ball1Red = Ball1ColorMenu.add( viz.RADIO, 0, 'Red' )
Ball1White = Ball1ColorMenu.add( viz.RADIO, 0, 'White' )
Ball1Blue = Ball1ColorMenu.add( viz.RADIO, 0, 'Blue' )

#Set inital selected radio button
Ball1White.set( viz.DOWN )

#Set callbacks for changing ball color
vizact.onbuttondown( Ball1Red, ball1.color, viz.RED )
vizact.onbuttondown( Ball1White, ball1.color, viz.WHITE )
vizact.onbuttondown( Ball1Blue, ball1.color, viz.BLUE )


#Ball2 menu

#Setup menu in ball
progressBar = Ball2Menu.add( viz.PROGRESS_BAR, '1.00', 'Alpha' )
progressBar.set( 1.0 )

def updateAlpha( sliderPOS ):
    #Set transparency of ball
    progressBar.message( str('%.2f'%(sliderPOS)) )
    ball2.alpha( sliderPOS )

vizact.onslider( progressBar, updateAlpha )