views:

28

answers:

1

I'm having some issue and am hoping someone can answer the question. I have a Deep Zoom project that I used the standard (deep zoom composer) project which places a DeepZommInitializer behavior on the MultiScaleImage control. I'm trying to constrain dragging to make sure that the user doesn't drag the image off screen (and therefore is unable to find the image). I did add a Home button which will bring the image back to the starting location at a zoom of 1. Anyway here's the code I currently have (have scoured the internet looking for an answer).

        // msi is the multiscale image
        msi.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e)
        {
            lastMouseDownPos = e.GetPosition(msi); // class level var
            lastMouseViewPort = msi.ViewportOrigin; // class level var

            mouseDown = true; // class level var

            msi.CaptureMouse();
        };

        msi.MouseMove += delegate(object sender, MouseEventArgs e)
        {
            lastMousePos = e.GetPosition(msi);

            if (duringDrag) 
            {
                Point newPoint = lastMouseViewPort;

                newPoint.X += (lastMouseDownPos.X - lastMousePos.X) / msi.ActualWidth * msi.ViewportWidth;
                newPoint.Y += (lastMouseDownPos.Y - lastMousePos.Y) / msi.ActualWidth * msi.ViewportWidth;

                var limits = new Rect(new Point(1, 1 / msi.AspectRatio), new Point(-1, -1 / msi.AspectRatio));

                if (newPoint.X > limits.Right * .999)
                {
                    newPoint.X += (-2 * (lastMouseDownPos.X - lastMousePos.X)) / msi.ActualWidth * msi.ViewportWidth; // Reverses direction when going off left

                }

                if (newPoint.Y > limits.Bottom * .999)
                {
                    newPoint.Y += (-2 * (lastMouseDownPos.Y - lastMousePos.Y)) / msi.ActualWidth * msi.ViewportWidth; // Reverses direction when going off top of screen
                }

                msi.ViewportOrigin = lastMouseViewPort = newPoint;
                lastMouseDownPos = lastMousePos;
            }
        };

I really need a solution that works for right and bottom but the moment that I zoom at all the values all change. My limit code works if zoom level is 1. I can't believe this is nowhere to be found on the internet! But the moment the zoom changes then everything goes out the window (values for newPoint aren't in the range I expect). Any help would be fantastic!

A: 

And just like that I figured it out myself (finally after several weeks).

Here's the answer (simple change the mouse move event as follows):

        msi.MouseMove += delegate(object sender, MouseEventArgs e)
        {
            lastMousePos = e.GetPosition(msi);

            if (duringDrag) 
            {
                Point newPoint = lastMouseViewPort;

                newPoint.X += (lastMouseDownPos.X - lastMousePos.X) / msi.ActualWidth * msi.ViewportWidth;
                newPoint.Y += (lastMouseDownPos.Y - lastMousePos.Y) / msi.ActualWidth * msi.ViewportWidth;
                var limits = new Rect(new Point(1, 1 / msi.AspectRatio), new Point(-1 / Settings.ZoomLevel, -1 / msi.AspectRatio / Settings.ZoomLevel));

                if (newPoint.X < limits.Left * .999)
                {
                    newPoint.X += (-2*(lastMouseDownPos.X - lastMousePos.X)) / msi.ActualWidth * msi.ViewportWidth;  // we went off right so reverse X direction

                }
                if (newPoint.X > limits.Right * .999)
                {
                    newPoint.X += (-2 * (lastMouseDownPos.X - lastMousePos.X)) / msi.ActualWidth * msi.ViewportWidth; // we went off left so reverse X direction
                }
                if (newPoint.Y < limits.Top * .999)
                {
                    newPoint.Y += (-2*(lastMouseDownPos.Y - lastMousePos.Y)) / msi.ActualWidth * msi.ViewportWidth;  // we went off Bottom so reverse direction
                }
                if (newPoint.Y > limits.Bottom * .999)
                {
                    newPoint.Y += (-2 * (lastMouseDownPos.Y - lastMousePos.Y)) / msi.ActualWidth * msi.ViewportWidth; // we went off Top so reverse direction
                }
                msi.ViewportOrigin = lastMouseViewPort = newPoint;
                lastMouseDownPos = lastMousePos;

                msi.SendMovedZoomMsg(Settings.ZoomLevel, newPoint, myClassName);
            }
        };

This probably doesn't completely constrain the image, but it keeps it on screen and more than likely the user will stop trying to move in the wrong direction

DevTheo
@DevTheo grt that u find out solution. You can mark u r own answer :)
Malcolm