简体   繁体   English

使用鼠标滚轮XNA进行图像缩放

[英]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. ScrollWheelValue是只读属性,因此无法重置。 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. 我试图添加一些逻辑,如果滚轮向上滚动且图像最大尺寸减去120,因为每次滚动鼠标增加120。 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;
        }

If you read: MSDN MouseState Scroll Wheel Value 如果您阅读: MSDN MouseState滚轮值

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. 现在,您只需检查prevWheelValue> <或==以获取currWheelValue并将值限制在所需的边界即可。

Mathhelper.Clamp Mathhelper.Clamp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM