on_mouse_scroll acts differently than on_mouse_motion/drag?

74 views
Skip to first unread message

Timothy Lillicrap

unread,
Mar 2, 2012, 6:38:38 AM3/2/12
to glumpy-users
Hey,

I'm having trouble getting the glumpy on_mouse_scroll event to work
for me. The other events (such as on_mouse_move and on_mouse_drag) in
my class work fine, but it seems like no matter what I use for a
function signature it complains that I don't have the right one, i.e.:

TypeError: on_mouse_scroll event was dispatched with 4 arguments, but
handler on_mouse_scroll at demos/simple5.py:97 has an incompatible
function signature.

At the moment I have these in my class (the first two work fine):

def on_mouse_motion(self, x, y, dx, dy):
pass

def on_mouse_drag(self, x, y, dx, dy, button):
print 'Mouse drag (x=%.1f, y=%.1f, dx=%.1f, dy=%.1f,
button=%d)' % (x,y,dx,dy,button)

def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
pass

I've tried several other things for function signatures for
on_mouse_scroll (like removing the "self" argument, which just seems
wrong, and trying different numbers of arguments). Am I doing
something silly?

Btw, thanks so much for the help in he last couple of days. Hopefully
I'll settle into the codebase soon and won't have quite so many
annoying questions.

Tim.

Nicolas Rougier

unread,
Mar 2, 2012, 7:16:36 AM3/2/12
to glumpy-users

Could you post your simple5.py sources ?

Nicolas



On Mar 2, 12:38 pm, Timothy Lillicrap <timothy.lillic...@gmail.com>
wrote:

Timothy Lillicrap

unread,
Mar 2, 2012, 8:15:58 AM3/2/12
to glumpy-users
Hi Nicolas,

hopefully this will run for you:

import glumpy
import numpy as np
import OpenGL
import OpenGL.GL as gl
import OpenGL.GLU as glu
import OpenGL.GLUT as glut
import loadtiff as loadtiff

#import pickle
#f = open('refimg.pickle','r')
#refimg = pickle.load(f)
#f.close()
#f = open('img.pickle','r')
#img = pickle.load(f)
#f.close()

refimg = np.random.uniform(0., 1., (512, 512)).astype(np.float32)
img = np.random.uniform(0,1,(200,100)).astype(np.float32)

#refimg = loadtiff.loadtiff('/media/SPINDRIVE/local_data/stimtests/
120118/12011802/zdepth.tif')
#imgs = loadtiff.loadtiffsequence('/media/SPINDRIVE/local_data/
stimtests/120118/12011802/bar_6.tif')
#imgs = np.asarray(imgs,np.float32)
#img = imgs[0:50,:,:].mean(0)/4096.
#refimg = refimg/refimg.max()
#img = img/img.max()

#import pickle
#f = open('refimg.pickle','w')
#pickle.dump(refimg,f)
#f.close()
#f = open('img.pickle','w')
#pickle.dump(img,f)
#f.close()

A = np.random.uniform(0,1, (512,512,2)).astype(np.float32)
A[:,:,1] = np.ones_like(refimg).astype(np.float32)
A[:,:,0] = refimg

B = np.ones((img.shape[0],img.shape[1],4)).astype(np.float32)
B *= (0,0,0,.7)
#B[:,:,2] = img
#B[:,:,1] = img
B[:,:,0] = img

fig = glumpy.figure( (512,512) )
A_img = glumpy.Image(A)
A_img.x,A_img.y = 0,0
#A_img.theta = 0

B_img = glumpy.Image(B)
B_img.x,B_img.y = 0,0
#B_img.theta = 0

class simple(object):
def __init__(self):

self.tx, self.ty = 0,0; self.tt = 0.

# @fig.event
def on_init(self):
print 'Inititalization'
gl.glEnable( gl.GL_BLEND )
gl.glBlendFunc( gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA )
gl.glEnable(gl.GL_LINE_SMOOTH);
gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_DONT_CARE);

# @fig.event
def on_draw(self):
tx = self.tx; ty = self.ty; tt = self.tt

fig.clear()

gl.glLoadIdentity()
A_img.update()
A_img.draw(x=A_img.x, y=A_img.y, z=0.0, width=fig.width,
height=fig.height)

gl.glLoadIdentity()
gl.glPushMatrix()
gl.glTranslate(B_img.x+B_img.width/2.0, B_img.y+B_img.height/2.0,0)
gl.glRotate(B_img.theta,0,0,1)
B_img.draw(x=-B_img.width/2., y=-B_img.height/2.,
z=1.0 ,width=img.shape[1], height=img.shape[0])
gl.glPopMatrix()


def on_mouse_motion(self, x, y, dx, dy):
pass

def on_mouse_drag(self, x, y, dx, dy, button):
print 'Mouse drag (x=%.1f, y=%.1f, dx=%.1f, dy=%.1f, button=
%d)' % (x,y,dx,dy,button)

if (button == 2):
B_img.x = B_img.x + dx
B_img.y = B_img.y + dy

if (button == 8):
B_img.theta = B_img.theta + dy/10.

fig.redraw()

def on_key_press(self, symbol, modifiers):
print 'Key pressed (symbol=%s, modifiers=%s)'% (symbol,modifiers)

def on_key_release(self, symbol, modifiers):
print 'Key released (symbol=%s, modifiers=%s)'% (symbol,modifiers)
if(symbol == 102):
print 'f'

def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
pass

s = simple()
fig.push(s)
fig.show()

Timothy Lillicrap

unread,
Mar 2, 2012, 8:21:40 AM3/2/12
to glumpy-users
Sorry, you'll need to remove the "import loadtiff as loadtiff" line.
I think I got rid of all the instances of it's use anyway.

Tim.

On Mar 2, 1:15 pm, Timothy Lillicrap <timothy.lillic...@gmail.com>
wrote:

Nicolas Rougier

unread,
Mar 2, 2012, 2:08:02 PM3/2/12
to glumpy-users
Thanks. I found the reason (bad handler in figure.py). I correct it
and committed.

You can also correct your sources easily:

replace
def on_mouse_scroll(self, dx, dy):
by
def on_mouse_scroll(self, x, y, dx, dy):

in glumpy/figure.py



On Mar 2, 2:21 pm, Timothy Lillicrap <timothy.lillic...@gmail.com>

Will

unread,
Mar 8, 2012, 9:27:57 PM3/8/12
to glumpy-users
Hi Nicolas,

First, I wanted to thank you for putting together the GlumPy library
-- it has been incredibly useful. I have followed your revision to
the figure.py file, but unfortunately, still unable to get the
on_mouse_scroll function to work. I have added the following lines to
the events.py file in the demo folder:

@fig.event
def on_mouse_scroll(x, y, dx, dy):
print 'Mouse scroll (x=%.1f, y=%.1f, dx=%.1f, dy=%.1f)' %
(x,y,dx,dy)

The function only returns when I push down on the scroll wheel, but
not when scrolling the wheel. The print output is as follows:

Mouse scroll (x=45.0, y=205.0, dx=0.0, dy=-1.0)

I have tested a few different mice (on Mac OS 10.7.3), but have not
yet arrived at a solution. Any advice on how/where I should start
poking around to resolve this issue or am I just missing something
obvious?

As a side note on interaction, I wanted to share some changes I made
to the Trackball class in the trackball.py file in order enable
panning in a 3D scene. Perhaps you have advice for a better way to
this, but my solution is as follows:

def pan_to(self, x, y, dx, dy):
''' Move trackball position from x, y to x+dx, y+dz, distance '''
self.push()
self.x += dx*0.1
self.y += dy*0.1
self.pop()

I have also added a self.x and self.y to the Trackball def __init__
with self.x and self.y = 0.0 to initialize and in def push(self): I
replaced:

gl.glTranslate (0.0, 0.0, -self._distance)

with:

gl.glTranslate (self.x, self.y, -self._distance)


Best Regards,
Will

Nicolas Rougier

unread,
Mar 12, 2012, 3:41:03 AM3/12/12
to glumpy...@googlegroups.com

If you're using osx, this might be GLUT/osx related problem:

http://iihm.imag.fr/blanch/howtos/MacOSXGLUTMouseWheel.html


I'm alos using osx and I did not get mouse wheel event (I need to patch glut as explained in the link but did not find time to do it yet).


Thanks for the addition on the trackball, I'll add.


Nicolas

Reply all
Reply to author
Forward
0 new messages