Vizard 8 » Tutorials & Examples » Example scripts » Events » Event handling
8.0

Event handling

\examples\eventHandlingClasses\tutorial_EventHandingClass.py

This script demonstrates event handling.

"""
Move the mouse over the blue texture quads.
They change color due to a class member function
that is registered as a callback handler.
"""
import viz
import vizact
import vizinfo

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

vizinfo.InfoPanel()

#Generate a unique event ID for our custom event
MOUSE_OVER_QUAD_EVENT = viz.getEventID( 'MouseOverQuad' )

#Add texture quads to the screen
quadLeft = viz.addTexQuad( viz.SCREEN )
quadLeft.setPosition( [.2, .5, 0] )
quadLeft.color( viz.BLUE )
quadLeft.isRed = 0

quadRight = viz.addTexQuad( viz.SCREEN )
quadRight.setPosition( [.7, .5, 0] )
quadRight.color( viz.BLUE )
quadRight.isRed = 0

#Our custom event class that acts on MOUSE_OVER_QUAD_EVENT events
class QuadColorChanger(viz.EventClass):
    def __init__(self):
        #Call super class constructor
        viz.EventClass.__init__(self)

        #When MOUSE_OVER_QUAD_EVENT occurs, call the member function onMouseOver
        self.callback( MOUSE_OVER_QUAD_EVENT, self.onMouseOver )

        #Setup event count displayer
        self.__eventCount = 0
        self.__countDisplay = viz.addText( '0', parent=viz.ORTHO, fontSize=40, pos=(10,10,0) )

    def onMouseOver( self, quad ):
        print('Changing quad color')
        #Increment and display event counter
        self.__eventCount += 1
        self.__countDisplay.message( str(self.__eventCount) )
        #Change quad's color
        if quad.isRed == 1:
            quad.color( viz.BLUE )
            quad.isRed = 0
        else:
            quad.color( viz.RED )
            quad.isRed = 1
        #Stop this event from being passed to any other handlers
        return True

#Create instance of our custom event so that it can receive events
colorChanger = QuadColorChanger()


#Create a class that never gets the event because <QuadColorChanger>.onMouseOver returns true
class NeverGetsQuadEvent(viz.EventClass):
    def __init__(self):
        viz.EventClass.__init__(self)

        #When MOUSE_OVER_QUAD_EVENT occurs, call the member function neverGetsCalled
        self.callback( MOUSE_OVER_QUAD_EVENT, self.neverGetsCalled )

    def neverGetsCalled( self, quad ):
        print('I am not printed')

soSad = NeverGetsQuadEvent()


class HighPriority(viz.EventClass):
    def __init__(self):
        viz.EventClass.__init__(self)

        #When MOUSE_OVER_QUAD_EVENT occurs, call the member function calledFirst
        self.callback( MOUSE_OVER_QUAD_EVENT, self.calledFirst, priority = -1 )

    def calledFirst( self, quad ):
        print('Called first')

firstToGetEvent = HighPriority()

#Used so that event is not called when mouse rests on a quad
lastPickedObject = None

#Check if the mouse is over a quad and send event
def checkMouse():
    global lastPickedObject

    #Check if the mouse is over one of the boxes
    quad = viz.pick( False, viz.SCREEN )

    if quad == quadLeft or quad == quadRight:
        #If quad is an viz object and has not just been picked
        if quad.valid() and quad != lastPickedObject:
            #Trigger our custom event
            viz.sendEvent( MOUSE_OVER_QUAD_EVENT, quad )

    #Set the picked object as our lastPickedObject
    lastPickedObject = quad

#Create and start a timer that checks every frame if the mouse is over a box
vizact.ontimer(0,checkMouse)