javascalingmousewheelpiccolo

How to inverse mouse wheel zoom handling in Piccolo2D?


In most applications rotating wheel down descreases the size of scaled objects, while rotating wheel up increases sizes.

In piccolo2d default behavior is reversed:

private static void showWorldNode() {
        new PFrame() {

            @Override
            public void initialize() {

                getCanvas().getLayer().addChild(worldNode);

                PMouseWheelZoomEventHandler mouseWheelZoomEventHandler = new PMouseWheelZoomEventHandler();
                mouseWheelZoomEventHandler.zoomAboutMouse();
                getCanvas().addInputEventListener(mouseWheelZoomEventHandler);
            }

        };
    }

How to reverse to default?


Solution

  • You can provide a negative scale factor to reverse the behavior, for example:

    mouseWheelZoomEventHandler.setScaleFactor(-0.1d);
    

    PMouseWheelZoomEventHandler calculates the zoom value based on the scale factor and the value of MouseWheelEvent.getWheelRotation() which returns:

    negative values if the mouse wheel was rotated up/away from the user, and positive values if the mouse wheel was rotated down/ towards the user

    Here is the relevant code from PMouseWheelZoomEventHandler

    double scale = 1.0d + event.getWheelRotation() * scaleFactor;