PDA

View Full Version : onMouseMove events not firing?


tspratt
11-28-2003, 02:29 PM
In the onIdle example shown elsewhere, a stream of events is produced when the mouse is moved and the mouse positions are displayed in the debugger . I would have expected onMouseMove event to do the same, as in the code here:

<method event="onMouseMove">
debug.write (this.immediateParent.getMouse('x'))
</method>

However, I am not seeing the events. Any ideas where my thinking or code is wrong?

I notice that the onMouseMove event attribute is in the "setter" category and not the "event handler" category. Is this significant?

Tracy

antun
12-01-2003, 09:18 AM
Hi Tracy

onmousemove is actually an event that no longer exists (there was a bug filed against this). You can achieve the same result as follows:


<canvas debug="true">
<view>
<attribute name="lastxpos" value="0" type="number" />
<attribute name="lastypos" value="0" type="number" />

<method event="onidle" reference="LzIdle">
<![CDATA[
var xpos = Math.round( canvas.getMouse("x") );
var ypos = Math.round( canvas.getMouse("y") );
if ( (this.lastxpos != xpos)
|| (this.lastypos != ypos) ) {
debug.write( "onmousemove: " + xpos + ", " + ypos );
this.lastxpos = xpos;
this.lastypos = ypos;
}
]]>
</method>
</view>

</canvas>


... you can also tweak the logic above to adjust the sensitivity - for example you could insist that only when the mouse moved 5 pixels does the event fire.

-Antun

adam
12-02-2003, 08:42 AM
This is the only way to do this, but note that this method will be called everytime the screen redraws, so you should try to make the method efficient, and you should probably use a delegate to make it so that you're only tracking the mouse when you care (e.g. when the mouse is over a certain view, or when the mouse button is down.)

Here's a little program that tracks the mouse only when the button goes down over the red box

<canvas debug="true">
<debug y="300"/>
<view onmousedown="this.startTrackMouseMove()"
onmouseup="this.stopTrackMouseMove()"
width="100" height="100" bgcolor="red">


<attribute name="lastxpos" value="0" type="number" />
<attribute name="lastypos" value="0" type="number" />

<attribute name="trackDel"
value="$once{new LzDelegate( this , 'trackMouseMove' ) }" />

<method name="startTrackMouseMove">
this.trackDel.register( LzIdle, "onidle" );
</method>

<method name="stopTrackMouseMove">
this.trackDel.unregisterAll();
</method>

<method name="trackMouseMove">
<![CDATA[
var xpos = Math.round( canvas.getMouse("x") );
var ypos = Math.round( canvas.getMouse("y") );
if ( (this.lastxpos != xpos)
|| (this.lastypos != ypos) ) {
debug.write( "onmousemove: " + xpos + ", " + ypos );
this.lastxpos = xpos;
this.lastypos = ypos;
}
]]>
</method>
</view>

</canvas>