Rotating component issue

void SomeButton::mouseDrag(const MouseEvent& e) {

    Component* button = (Component*)e.eventComponent;
    int distance = e.getDistanceFromDragStart();

    float angle = (distance / (180.0 / double_Pi));

    float pivotX = button->getPosition().getX() + (button->getWidth()*0.5f);
    float pivotY = button->getPosition().getY() + (button->getHeight()*0.5f);

    button->setTransform(AffineTransform::rotation(angle, pivotX, pivotY));
}

Hello people!,
I have a problem, this code should rotate component while dragging,
this code works almost fine, the problem is that distance is always positive thus
when I drag up rotating right, and down rotating left but until I reach the component position,
suddenly rotation is switched to other side, like there is a mirror in the middle of the component,
I think if I just could get negative values for distance for below the component center,
What should I do to solve this issue?

Hopefully I’m clear enough.
tanks!

This is really just a convenience if you really only need the magnitude of the distance. You can get the mouse down position from the event as a point using MouseEvent::getMouseDownPosition(). Then you can do you’re own calculations using that.

I tried to do that

int distance = -e.getPosition().getY();

but apparently the getPosition() method, is a position relative (I guess) to the component transform,
so after few rotations (by dragging) the component go crazy because of negative and positive values (from the position) jump abruptly.
I tried to see the values of the position while the component is static, and they seem good.
Going from negative to positive values one by one without sudden changes.
the getPosition() behave differently only after transform is done.
What should I do next?

There is some missing consideration: what kind of drag rotation do you want to implement?
Some options like in the rotary slider:

  • Rotary: drag a point around the centre - use Point::getAngleToPoint()
  • RotaryHorizontalDrag - use only X-component
  • RotaryVerticalDrag - use only Y-component
  • RotaryHorizontalVerticalDrag - use the distance, but multiply with the sign of X+Y (of the vector, not the last point)

Make that decision first, before you poke around…

My favourite quote Edger Dijkstra: “If you don’t know what you program is supposed to do, don’t start writing it…” :wink:

1 Like