Handling events

There are seven different types of mouse events that you may listen to in a panel.

enter the mouse enters the panel
exit the mouse exits the panel
click the mouse is quickly pressed and released
press the mouse is pressed
release the mouse is released after being pressed
drag the mouse is moved after being pressed
move the mouse is moved without being pressed

To listen to these events in a JyPanel, define a function to be called when the event occurs.

def click(x, y):
    print 'Click at ' + str(x) + ', ' + str(y)

The arguments x, y are the pixel coordinates at which the event occurs.

This function needs to be registered with the panel with its onclick function.

panel = JyPanel([300, 300, draw])
panel.onclick(click)

You should now see the coordinates printed out when you click in the panel.

Sometimes you would like to have the coordinates x, y be relative to a coordinate system used during the drawing function. To do this, use a Mark as follows:

def move(x, y):
    print 'Move to ' + str(x) + ', ' + str(y)

movemark = Mark()
panel.onmove(enter, movemark)

Now in the drawing function, use placemark to set the coordinate system.

def draw(p):
    p.beginpage()

    p.center()
    p.scale(50)
    p.setdeg()
    p.gsave()
    p.rotate(angle)
    p.placemark(movemark)   # <-----
    p.grestore()

The coordinates supplied to move will now be in the current coordinate system when the mark was placed.

Here is a complete example. As you'll see, Moveablepoints should not be affected.

from JyScript import *

s = ''
angle = 0
px, py = 0, 0
def draw(p):
    p.beginpage()

    p.center()
    p.scale(50)
    p.setdeg()
    p.gsave()
    p.rotate(angle)
    p.box(-1.5, -1.5, 3, 3)
    p.fill(1, 0, 0)
    p.placemark(mark)
    p.grestore()

    p.newpath()
    p.setcolor(0, 0, 1, 0.5)
    p.setfont(36)
    p.moveto(-1.5, 0)
    p.show(s)

    if len(s) > 0:
        p.newpath()
        p.moveto(-1.5, -1)
        pt = '(%.1f, %.1f)' % (px, py)
        p.show(pt)

    p.placemoveable(point)

    p.endpage()

def step():
    global angle
    angle += 1
    refresh()
settimer(25, step)
def click(x, y):
    global s, px, py
    px, py = x, y
    s = 'click'
def enter(x, y):
    global s, px, py
    px, py = x, y
    s = 'enter'
    starttimer()
def exit(x, y):
    global s, px, py
    px, py = x, y
    s = 'exit'
    stoptimer()
def press(x, y):
    global s, px, py
    px, py = x, y
    s = 'press'
def release(x, y):
    global s, px, py
    px, py = x, y
    s = 'release'
def drag(x, y):
    global s, px, py
    px, py = x, y
    s = 'drag'
def move(x, y):
    global s, px, py
    px, py = x, y
    s = 'move'

mark = Mark()
panel = JyPanel([300, 300, draw])
panel.onclick(click)
panel.onenter(enter, mark)
panel.onexit(exit, mark)
panel.onpress(press, mark)
panel.onrelease(release, mark)
panel.ondrag(drag, mark)
panel.onmove(move, mark)

def movepoint(point, x, y):
    point.setpoint(x,y)
point = Moveablepoint(-1, -2, movepoint)
point.setfillcolor(0, 1, 0)
panel.addmoveable(point)
openframe(panel)