Saving the entire mouse state in a data structure

My app needs too calculation for each wx.EVT_MOVE event, so I’d like to save the entire mouse state in some kind of data structure/class and then add it to a collections.deque for processing during wx.EVT_IDLE events. I feel like I’m “recreating the wheel” as it seems to me that someone has probably already done this.

I’d like to save all the mouse-related event.* attributes, too. Is the event object store-able as-is, or is it reused for every event ? Do I have to, instead, extract every event attribute individually and save them one-by-one ?

I have attached a class that outlines what I think needs to be done, but I am hoping this has already been implemented, which seems like it would save me a lot of work.

Ray Pasco

MouseState.py (2.68 KB)

My app needs too calculation for each wx.EVT_MOVE event, so I'd like to
save the entire mouse state in some kind of data structure/class and
then add it to a collections.deque for processing during wx.EVT_IDLE
events. I feel like I'm "recreating the wheel" as it seems to me that
someone has probably already done this.

I'd like to save all the mouse-related event.* attributes, too. Is the
event object store-able as-is, or is it reused for every event ? Do I
have to, instead, extract every event attribute individually and save
them one-by-one ?

The event object passed to the handler will be destroyed after control returns to where the event was sent from, so you will need to copy the values you are interested in from the event object.

I have attached a class that outlines what I think needs to be done, but
I am hoping this has already been implemented, which seems like it would
save me a lot of work.

Unless you really do need to be able to store both the current and previous positions in a single object, you can just use the wx.MouseState class instead. (Wouldn't it make more sense to have the previous position just be the previous state object in the list?)

In wxPython 2.9 the wx.MouseEvent derives from wx.MouseState, so they are *very* compatible :slight_smile: but it also exists in 2.8 and should work just fine. Here is what the C++/SWIG declaration of the class looks like in 2.8. As you can see there is a convenient property defined for each of the attributes, so writing a bit of code to transplant values from a wx.MouseEvent to an instance of wx.MouseState should be trivial:

class wxMouseState
{
public:
     wxMouseState();
     ~wxMouseState();

     wxCoord GetX();
     wxCoord GetY();

     bool LeftDown();
     bool MiddleDown();
     bool RightDown();

     bool ControlDown();
     bool ShiftDown();
     bool AltDown();
     bool MetaDown();
     bool CmdDown();

     void SetX(wxCoord x);
     void SetY(wxCoord y);

     void SetLeftDown(bool down);
     void SetMiddleDown(bool down);
     void SetRightDown(bool down);

     void SetControlDown(bool down);
     void SetShiftDown(bool down);
     void SetAltDown(bool down);
     void SetMetaDown(bool down);

     %pythoncode {
         x = property(GetX, SetX)
         y = property(GetY, SetY)
         leftDown = property(LeftDown, SetLeftDown)
         middleDown = property(MiddleDown, SetMiddleDown)
         rightDown = property(RightDown, SetRightDown)
         controlDown = property(ControlDown, SetControlDown)
         shiftDown = property(ShiftDown, SetShiftDown)
         altDown = property(AltDown, SetAltDown)
         metaDown = property(MetaDown, SetMetaDown)
         cmdDown = property(CmdDown)
     }
};

···

On 8/3/11 10:52 AM, Ray Pasco wrote:

--
Robin Dunn
Software Craftsman

My app needs too calculation for each wx.EVT_MOVE event, so I’d like to

save the entire mouse state in some kind of data structure/class and

then add it to a collections.deque for processing during wx.EVT_IDLE

events. I feel like I’m “recreating the wheel” as it seems to me that

someone has probably already done this.

I’d like to save all the mouse-related event.* attributes, too. Is the

event object store-able as-is, or is it reused for every event ? Do I

have to, instead, extract every event attribute individually and save

them one-by-one ?

The event object passed to the handler will be destroyed after control returns to where the event was sent from, so you will need to copy the values you are interested in from the event object.

Unless you really do need to be able to store both the current and previous positions in a single object, you can just use the wx.MouseState class instead. (Wouldn’t it make more sense to have the previous position just be the previous state object in the list?)

This would be very easy, but overkill for the very little previous state info I need.

In wxPython 2.9 the wx.MouseEvent derives from wx.MouseState, so they are very compatible :slight_smile: but it also exists in 2.8 and should work just fine. Here is what the C++/SWIG declaration of the class looks like in 2.8. As you can see there is a convenient property defined for each of the attributes, so writing a bit of code to transplant values from a wx.MouseEvent to an instance of wx.MouseState should be trivial:

class wxMouseState

{

public:

wxMouseState();

~wxMouseState();



wxCoord     GetX();

wxCoord     GetY();

Robin Dunn

Software Craftsman

http://wxPython.org

(This reply may or may not be a duplicate reply.)

Yup, this certainly seems to be exactly what I want.

At this point I only need to save a few attributes from the previous mouse state, so I’ll just add those “previous state” attributes to the current mouse state object. This will avoid unnecessarily doubling the size of the mouse state queue object and so, the queue itself. Besides, if later I decide that I really do need to queue the entire previous mouse state object, too, it will be very easy to change the queue design. So, just to be clear, what I need right now is :

from collections import deque

self.motionQueue = deque()      # Save the cursor state on each wx.EVT_MOTION

self.Bind( wx.EVT_IDLE, self.OnIdle )   # Necessary code to process EVT_MOTION is a long and winding road

...

currMouseState = GetMouseState()     # This doesn't even have to be inside a mouse event handler

# Add my own custom attributes:

currMouseState.numLeftClicks = numLeftClicks

currMouseState.prevPos = prevPos



self.motionQueue.append( currMouseState )

self.prevMouseState = currMouseState     # Save for possibly needed future reference

Thanks,

Ray

···

On Wed, Aug 3, 2011 at 3:01 PM, Robin Dunn robin@alldunn.com wrote:

On 8/3/11 10:52 AM, Ray Pasco wrote: