MouseEvent movement since last event


#1

Is there a way to get the movement of the mouse in a Component::mouseDrag event? I only found the getDistanceFromDragStartX() and getDistanceFromDragStartY(), but not the distance from the last (move) event.

Sure, I can keep track on my own in a member variable, but the design would be nicer, if the information was stored in the event just like the drag start. And probably this information is there, I only didn't see it.

Thanks for hints.


#2

No, don't think that's there.

As you say, it's very easy to calculate yourself, but it's not something I'd want to add to the event class, as it's actually not a very useful value.. I've found that it's almost always a bad idea to move things based on relative distances, as thsse will drift out of sync with the absolutel mouse position when it hits the sides of the screen.


#3

You are right, most times I also prefer the absolute position. I implement the panning of a scene, where little differences wouldn't be noticed at all, and I want the mouse movement to be added to the translation. So the move information would have been handy. But it is no problem at all to track it in the component.

Thanks again.


#4

I was writing a reply to this, but as so often happens with JUCE I discovered a handy method to do what I thought was quite finnicky. What I was going to write:

Just to clarify: what then is the recommended method? I have a component, whose parent is registered as a mouse listener so that the sub-component can be moved around in the parent. To accomplish this currently I take the position on mouseDown, then reposition relative to that in mouseDrag. Should I instead take the absolute screen position of the mouse, figure out what that equates to inside the parent component, and move based on that? That seems a bit complicated...

Actually, no it isn't. Component::getLocalPoint (nullptr, mouseEvent.getScreenPosition()) in the parent component's mouseDrag callback solves it neatly. Just posting for anyone else doing it the 'wrong' way like I was.