The World

Every Slut Application is more or less structured around a "world class." This is a class that inherits methods and member variables from Slut's primal world: slut.world.World. Once this "world class" is instanced and run...

atlantis = MyWorld()
atlantis.run()    

...it starts firing events...

onSetup()
onDraw()
onLMouseMove()
onKeyDown()
...    

If such a method is defined in the "world class" the corresponding event will call it and execute the logic it holds. This is a common computer science pattern and generally known as event-based programming. Events are identified by a continuously running "main loop" and arbitrarily definable event-handler methods are called as the events occur. Writing a program with such an event-based model is usually a matter of structuring code around the events that the main loop provides. Nearly all event-based frameworks (true for Flash and most GUI Widgets libraries) have an event that is only called once at the beginning of the program (onSetup()), an event that is called on every frame to redraw the display (onDraw()) and a set of events that fire upon user input. Beyond that, Slut can also be set up to fire networking events and various other events that are useful for controlling OpenGL's state machine.

The World calls the following event handlers:

Event Upon Startup:
onSetup()

Display Redraw Event:
onDraw()
onDrawOrtho()

Mouse Events:
onMouseMove()
onMouseButtonUp()
onMouseButtonDown()

Keyboard Events:
onKeyDown()
onKeyUp()

Joystick Events:
onJoyAxisMotion()
onJoyHatMotion()
onJoyButtonDown()
onJoyButtonUp()

Relevant examples: mouse.py, keyboard.py, joystick.py, ALL

Slut supports keyboard, mouse, and joystick as possible input devices. With each of them comes a set of event handlers that can be defined as needed. Each of these methods get an event object passed that holds relevant information about the event:

onMouseMove(self, event):
    print 'The mouse is at'
    print 'x:', event.pos[0]
    print 'y:', event.pos[1]
    
onKeyDown(self, event):
    if event.key == ord('h'):
        print 'Key "h" was pressed.'

onJoyAxisMotion(self, event):
    if event.axis == 1:
        print 'Joystick's  axis 1 moved to value:'
        print event.value

When unsure about the event object it is always a good idea to take advantage of python's powerful introspection feature. Adding a print dir(event) to the event handler lists the content of the event object.

Initiated by Stephan Hechenberger
Thanks to CADRE's 103