Piccolo Snippets

From eqqon

Revision as of 07:12, 29 October 2007 by Henon (Talk | contribs)
Jump to: navigation, search

Useful Piccolo Extensions

Mouse Wheel Zoom

The class MouseWheelZoomController replaces Piccolo's not very intuitive default zoom event handler with a mouse wheel zoom event handler. Just instantiate it and mouse wheel zooming just works with your Piccolo canvas!

    public class MouseWheelZoomController
    {
        public static float MIN_SCALE = .0001f;
        public static float MAX_SCALE = 2500;
        PCamera camera;

        public MouseWheelZoomController(PCamera camera)
        {
            this.camera = camera;
            camera.Canvas.ZoomEventHandler = null;
            camera.MouseWheel += OnMouseWheel;
        }

        public void OnMouseWheel(object o, PInputEventArgs ea)
        {
            float currentScale = camera.ViewScale;
            float scaleDelta = (1.0f + (0.001f * ea.WheelDelta));
            float newScale = currentScale * scaleDelta;
            if (newScale < MIN_SCALE)
            {
                camera.ViewScale = MIN_SCALE;
                return;
            }
            if ((MAX_SCALE > 0) && (newScale > MAX_SCALE))
            {
                camera.ViewScale = MAX_SCALE;
                return;
            }
            PointF pos = ea.Position;
            camera.ScaleViewBy(scaleDelta, pos.X, pos.Y);
        }
    }