parrotcode: base class for application-specific SDL event handlers | |
Contents | Libraries |
SDL::EventHandler - base class for application-specific SDL event handlers
# load the event class and this library
load_bytecode 'library/SDL/Event.pir'
load_bytecode 'library/SDL/EventHandler.pir'
# subclass this class
.local pmc parent_class
.local pmc class_type
getclass parent_class, 'SDL::EventHandler'
subclass class_type, parent_class, 'My::Event::Handler'
# define your overridden methods
.sub key_down_down :method
.param pmc event
.param pmc event_args
# ...
.end
# create an event handler object
.local pmc event_handler
.local int handler_type
find_type handler_type, 'My::Event::Handler'
event_handler = new handler_type
# create and populate some event_arguments
.local pmc event_args
new event_args, .Hash
event_args[ 'main_surface' ] = main_surface
event_args[ 'sprite_list' ] = sprites
# create a new event object
.local pmc event
.local int event_type
find_type event_type, 'SDL::Event'
event = new event_type
# ... and process events
event.'process_events'( event_handler, handler_args )
SDL::EventHandler is a parent class for all event handlers in SDL Parrot programs. Subclass this class and override the methods that correspond to the events you want to handle.
SDL::EventHandler provides the following methods:
key_down_keyname
method, if one exists. Otherwise, does nothing.key_down_*
methods instead. $ perldoc SDL::Event
.sub key_down_q # 'q' key
.sub key_down_down # <down> arrow key
.sub key_down_kp_plus # <keypad-plus> key
...
key_upkeyname
method, if one exists. Otherwise, does nothing.key_up_*
methods instead.Key event methods have names of the form key_eventtype_keyname
. That is, to handle a key down event for the Escape key, override the method key_down_escape
. Key names follow the SDL naming convention, except that key names are in all lowercase.
Unless you override key_down
or key_up
and do something different, all of these methods will receive one argument, the event_args
hash passed to SDL::Event::wait_event()
. Use this hash to store and to retrieve data between events, particularly your main surface and any sprites or other surfaces.
In addition, you can override the following methods to handle their event types.
Synopsis for mouse event handler:
.sub mouse_button_up :method
.param pmc event
.param pmc args
.local int b, x, y
event = event.'event'( 'MouseButton' )
b = event['state'] # 1 = left, 2 = middle, 3 = right
x = event['x']
y = event['y']
...
By default, these methods do nothing. They all take two arguments, the SDL::Event object representing the incoming event and the event_args
hash storing data between events.
At the very least, you should override quit()
.
Written and maintained by chromatic, <chromatic at wgz dot org>. Designed by Allison Randal. Please send patches, feedback, and suggestions to the Perl 6 Internals mailing list.
Copyright (C) 2004-2006, The Perl Foundation.
|