Event dispatching
| Events | Touch events |
In this document
Introduction
Let's take a simple scheme to explain how event are working :

This is a classical tree, that can be created with the code below :
from pymt import *
window = MTWindow()
widA = MTWidget()
widB = MTWidget()
widC = MTWidget()
widD = MTWidget()
widB.addWidget(widC)
widB.addWidget(widD)
window.addWidget(widA)
window.addWidget(widB)
runTouchApp()
window = MTWindow()
widA = MTWidget()
widB = MTWidget()
widC = MTWidget()
widD = MTWidget()
widB.addWidget(widC)
widB.addWidget(widD)
window.addWidget(widA)
window.addWidget(widB)
runTouchApp()
Event are dispatched from the TOP to the BOTTOM of the graph : from window to leaf child. Many event are available by default :
- on_update - do some update in widget if necessary
- on_draw - draw the widget and his children
- on_touch_down - called when a touch is down
- on_touch_move - called when a touch is moved
- on_touch_up - called when a touch is up
- on_resize - the widget have been resized
- on_parent_resize - the parent widget have been resized
Update/Draw events
Every frame displayed on the screen, each widget in the tree will have 2 events called: on_update() and on_draw(). The on_draw() will call draw() function of widget, and dispatch on_draw() event in theses childrens.
If we try to represent it with a scheme, here is the call tree :
- window.on_update()
- widA.on_update()
- widB.on_update()
- widC.on_update()
- widD.on_update()
And, the drawing part :
- window.on_draw()
- window.draw()
- widA.on_draw()
- widA.draw()
- widB.on_draw()
- widB.draw()
- widC.on_draw()
- widC.draw()
- widD.on_draw()
- widD.draw()
Resize events

When a widget is resized, 2 events are fired :
- on_resize() on the widget itself
- on_parent_resize() on all the children
@widA.event
def on_resize(w, h):
print 'widA got a new size', w, h
# and test it
widA.size = (200, 500)
def on_resize(w, h):
print 'widA got a new size', w, h
# and test it
widA.size = (200, 500)
As soon as the size changed, event will be fired
