ComponentDragger and AffineTransforms

I’ve been playing a bit with the new Affine Transform code, and it’s really cool.
I created a simple component and a slider to apply the rotate transform like in the demo and everything is fine.
Now I want to drag the component around with the mouse,
so I just add a ComponentDragger in it and I call its proper functions like explained in the comments.

The component becomes draggable, but when mouseDrag() is called it jumps to a wrong position.
So what’s the best way to drag around a transformed component?

It sounds like that’s just an error in the ComponentDragger - I’ll test that and make sure it works.

There are likely to be a few places in the code where components make assumptions about coordinates that are no longer true when transforms are used, so let me know if you spot any other weirdnesses!

Just checked the last tip.
Excellent, now it works fine.
One question:
for the rotate transform that I’m using I set the pivot coordinates to be the center of the component:

float pivotX = (float) transformedComponent->getX() + (transformedComponent->getWidth() / 2.0f); float pivotY = (float) transformedComponent->getY() + (transformedComponent->getHeight() / 2.0f);

But obviously this leads to a wrong point.
So what should I write to get the central point of the transformed component?
I tried also with calling getBoundsInParent()->getX() and so on, but the point is still wrong…

Yeah, that would be correct, I think. Something like

setTransform (AffineTransform::rotation (angle, getBounds().getCentre().getX(), getBounds().getCentre().getY()));

…should work.

It works, but if I:

  • drag the component around
  • apply the transformation
  • drag the component somewhere else
  • apply the transformation again

the component jumps around.
So there should be something missing…

You’ll probably have problems if you try to transform a component while it’s being dragged, just like you would do if you tried to change its position while it’s being dragged. I guess if you transformed it so that the drag’s anchor point remains stationary, then you might be ok, but otherwise, the mouse event’s location will stop being correct as soon as the component is transformed, and that’ll make all the deltas go wrong.

Right, but the transformation never happens during a drag event.
The component jumps around only when I re-apply the transformation, not when I drag it.

How can I set a stationary drag’s anchor point?

Well, it’s up to you to do the maths to figure out your transforms - I never said it was easy - transforms make my head hurt too!

If you’re doing something complicated, it may be easiest to keep your component’s bounds at 0, 0 and move it around entirely by changing the transform, so that the bounds are no longer part of the equation. (You’d have to write your own dragging code for that though)