سؤال

Currently I'm working on a game editor that can zoom in and zoom out. There is issue that I can can do either scroll the mouse wheel up or down. I cannot achieve both scrolling i.e. if I call input->mouseWheelUp() in editor class, I only can scroll up. Scroll down does not work anymore. How can I fix this issue?

Get mouse wheel meesage

LRESULT Game::messageHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    if (initialized)    // do not process messages if not initialized
    {
        switch (msg)
        {
         case WM_MOUSEWHEEL:                            
                input->mouseWheelIn(wParam);
            return 0;
        }
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);    // let Windows handle it
}

Input class

void Input::mouseWheelIn(WPARAM wParam)
{
    mouseWheel = GET_WHEEL_DELTA_WPARAM(wParam);
}

bool mouseWheelUp()
{
    int wheel = mouseWheel;
    mouseWheel = 0;
    return wheel > 0;
}

bool mouseWheelDown()
{
    int wheel = mouseWheel;
    mouseWheel = 0;
    return wheel < 0;
}

Editor class

void Editor::update()
{
    if ( input->mouseWheelUp() )
    {
        zoom += 0.1f;
    }

    if (input->mouseWheelDown())
    {
        zoom -= 0.1f;
    }
}
هل كانت مفيدة؟

المحلول

Looking at your editor's update method, your first call is to mouseWheelUp, which resets the internally cached mouse wheel event value to 0. On your second call to mouseWheelDown the value will be 0 and therefore the return result of that method is always false.

You should approach that problem differently. You could for example introduce some kind of mapping to a constant or enum that signifies a mouse wheel up or down event. Or you could process in your update method the raw incoming event value instead. That's probably the better way since it's apparently a delta value that may be more or less big depending on how much you spin the mouse wheel. You should take the delta into account for making bigger or smaller changes to your zoom value.

نصائح أخرى

case WM_MOUSEWHEEL:
        if ((short)HIWORD(msg.wParam) < 0)
        {
            zoom-=3;
        }
        else
        {
            zoom+=3;
        }
        break;

Example of using mouse wheel scroll, simple and slick.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top