Saturday, March 20, 2010

Mouse support in TurtleArt

Here is how mouse support can be added to Turtle Art V84. In this example, mouse left click events are registered by setting a flag, the x and y at the mouse click can be read.


[note as of Turtle Blocks V87 no patching of tawindow.py is required and the python block code is in the sample myblock file]

It requires some modification of the program code, three new lines need to be added to the file tawindow.py near the beginning:

def __init__(self, win, path, parent=None, mycolors=None):
self.mouseflag=0
self.mouse_x=0
self.mouse_y=0
self.win = None

and three new lines about 1/4 way into the file:

def _buttonpress_cb(self, win, event):
""" Button press """
self.window.grab_focus()
x, y = xy(event)
self.mouseflag=1
self.mouse_x=x
self.mouse_y=y
self.button_press(event.get_state()&gtk.gdk.CONTROL_MASK, x, y)
return True
These changes are necessary so that the programmable block tamyblock.py can "see" the mouse event. Turtle Art's files can be modified with Browse and Pippy or with the Linux command line editor vi see Modifing_an_Activity

Then a bit of code in the programmable block tamyblock.py makes it all happen.

def myblock(lc, x):
if lc.tw.mouseflag==1:
lc.heap.append(lc.tw.mouse_y)
lc.heap.append(lc.tw.mouse_x)
lc.heap.append(1)
lc.tw.mouseflag=0
else:
lc.heap.append(0)
return

Help understanding the detail of Turtle Art's internal Object Oriented Python programming is at Python_code_block

File:Turtle_Art_Activity_mouse.ta


(note: blog does not preserve indents, correct Python indenting is required)

Labels: , , , ,

2 Comments:

Blogger Unknown said...

Nice!

Monday, March 22, 2010 3:48:00 AM  
Blogger Walter Bender said...

The modifications to tawindow.py have been incorporated into Version 87.

Wednesday, May 05, 2010 11:00:00 PM  

Post a Comment

<< Home