CodyK
CodyK

Reputation: 3657

Make an Image Scale with Mouse Scroll Wheel XNA

I have an image that I want to increase the size or decrease the size if the mouse wheel is scrolled up or down accordingly. If the image reaches a set max size it will not get ant larger and vice versa if making the image smaller. The problem I am having is once you reach the maximum size of the image and continue scrolling up then go to scroll down the image will not get smaller right away until you scrolled down the same number of times you scrolled up while having the image at the max size and again reverse for making the image smaller. ScrollWheelValue is an read only property so it cannot be reset. I was trying to add some logic where if the wheel is scrolled up and the image is max size subtract 120 because 120 is what the mouse increases per scroll. Can anyone help me with this issue? Thanks very much

Original Code:

        float scale = ms.ScrollWheelValue / 120;

        scaleFactor = scale * scaleChange;

        if (scaleFactor > MAX_SCALE)
        {
            scaleFactor = MAX_SCALE;


        }

        else if (scaleFactor < MIN_SCALE)
        {
            scaleFactor = MIN_SCALE;


        }            

New Code:

        if (scaleFactor > MAX_SCALE)
        {
            scaleFactor = MAX_SCALE;
            float newScale = ms.ScrollWheelValue / 120;
            if (newScale > scale)
            {
                scaleCount = scaleCount - 120;
            }
            if (newScale < scale)
            {
                scaleCount = scaleCount + 120;
            }

        }

        else if (scaleFactor < MIN_SCALE)
        {
            scaleFactor = MIN_SCALE;
            float newScale = ms.ScrollWheelValue / 120;
            if (newScale > scale)
            {
                scaleCount = scaleCount - 120;
            }
            if (newScale < scale)
            {
                scaleCount = scaleCount + 120;
            }

        }

        else
        {
            scale = ms.ScrollWheelValue / 120 + scaleCount;

            scaleFactor = scale * scaleChange;
        }

Upvotes: 2

Views: 3676

Answers (1)

Beanish
Beanish

Reputation: 1662

If you read: MSDN MouseState Scroll Wheel Value

You'll see that it keeps a running value from the beginning of the game. So what you want to do is check it for a change vs. the previous value and do something accordingly.

How you have it set up it seems you don't care about the actual value, just the difference since the last time they scrolled the wheel.

declare these outside of your update loop:

float prevWheelValue;
float currWheelValue;

Then in your update:

prevWheelValue = currWheelValue;
currWheelValue = ms.ScrollWheelValue;

now your checks can simply be if prevWheelValue > < or == to currWheelValue and clamp the value to the boundaries that you want.

Mathhelper.Clamp

Upvotes: 4

Related Questions