Poriferous
Poriferous

Reputation: 1582

SDL mousewheel and mousemotion

I made a previous question regarding mouse input which helped me understand mouse events worked for a start. I created a class that handles all kinds of input, and suffice it to say there are a few bugs which I've tried correcting with booleans but to no avail.

Mouse wheel and mousemotion events execute continuously even when the mouse wheel or mouse is not scrolling or moving.

I test for mouse scrolls with this simple call:

bool isMouseWheelScrolled() const
{
    return pMouseWheelScrolled;
}

Then I listen for mouse events using a switch statement:

while(SDL_PollEvent(&event))
{
    switch (event.type)
    {
        case SDL_MOUSEMOTION:
            onMouseMove(event);
            break;

        case SDL_MOUSEWHEEL:
            onMouseWheelScroll(event);
            break;

        default:
            break;
    }
}

void onMouseWheelScroll(SDL_Event &event)
{
    switch(event.wheel.type)
    {
    case SDL_MOUSEWHEEL:
        pMouseWheelScrolled = true;
        pMouseWheel.x = event.wheel.x;
        pMouseWheel.y = event.wheel.y;
        break;

    default:
        break;
    }
}

The same thing applies in mouse movement, but:

void onMouseMove(SDL_Event &event)
{
    pMouseMoved = true;
    pMousePosition.x = event.motion.x;
    pMousePosition.y = event.motion.y;
}

It does have a problem with the booleans; the conditions for setting them to true are tested, but I don't know how I can analyse a condition for these booleans to become false. My solution would be to dwell into listening for mouse states, however I do not know how to do this and tutorials are insufficient in explaining them. The documentation isn't that intuitive either. The reason I need booleans is so I can test whether the mouse has moved or scrolled OUTSIDE the class. I can already test for key presses and mouse button presses; but mouse wheel and mouse moved are another issue; primarily because I need to obtain

event.motion.x; 
event.motion.y;
event.wheel.x;
event.wheel.y;

for my camera. And I am not prepared to pass camera into my class because that breaks OO.

tldr; mouse wheel and mouse moved are set to true, but how to disable them when the mouse wheel isn't scrolling or when the mouse isn't moving?

Upvotes: 0

Views: 8304

Answers (1)

rodrigo
rodrigo

Reputation: 98436

I think you misunderstand how mouse events work. You do not get a SDL_MOUSEMOTION event while the mouse is moving. Instead you get a SDL_MOUSEMOTION event when the mouse is moved. That is, the event represents a single instant in time.

If you want to do something when the mouse is moved, your best option is to do it as direct response to the mouse event. If that goes against your OO design, then your design is wrong.‎‎‎‎

If you insist in using booleans, then you should set the boolean variable to false as soon as your consumer class sees it as true, so that each event is processed only once.

Anyway, my advice is to use some kind of interface, such as this one:

class IMouseListener
{
    virtual OnMouseMove(int x, int y) =0;
    virtual OnMouseWheel(int x, int y) =0;
};

Then make your main class implement this interface, and make your mouse handling code receive a pointer to such interface.

Upvotes: 1

Related Questions