pyglet's tile/sprite bits got event handling in the last couple of days. I reckon it's pretty cool and should greatly simplify development of sprite/tile based games (that use the mouse). It looks like (given a view):
# attach the following event handler to the view
# only highlight cells
def on_mouse_enter(cells, x, y):
' The mouse is hovering over the indicated cells. '
' The mouse has stopped hovering over these cells. '
# just for the car sprite
def on_mouse_press(sprites, button, x, y, modifiers):
' Car has been clicked '
def on_mouse_release(sprites_or_cells, button, x, y, modifiers):
def on_mouse_drag(sprites_or_cells, x, y, dx, dy, buttons, modifiers):
def on_view_enter(sprites_or_cells, x, y):
''' sprite or cell has entered the View (even if potentially
obscured by another cell / sprite) '''
def on_view_leave(sprites_or_cells, x, y):
' sprite or cell has left the View '
@event says what event dispatcher to hook the handler on to. The @for_cells() call tells the dispatcher to generate events for all map cells for all maps in the view. We could have passed a list of maps to interrogate as @for_cells([m1, m2, m3]). We also could have said @for_cells(is_base=True) to have it only generate events for cells that have properties['is_base'] == True. If we didn't say @for_cells then we'd get events for sprites too. For example:
@for_cells() # for any map cell
@for_cells([map, map]) # may have multiple maps
# event could be generated for every cell in the map(s)
@for_cells([map], name=value, name=value)
# looks up property by name on Cell, then Tile, then TileSet
# could be defined on Cell or Tile -- "id" special-cased as an attribute
# would be defined on Tiles by convention - but there's nothing
# stopping an exceptional Cell having a "type" property
# would be defined on TileSets by convention
@for_sprites() # all sprites are active
@for_sprites([list of sprites]) # limited list of sprites active
# filters may be chained:
def on_mouse_press(cell, ...):
There's a practical demonstration of this and another new feature -- adding drawing effects to Cells, Tiles, etc (any "Drawable") -- in tests/scene2d/RECT_FLAT_MOUSE.py