SFML community forums

Help => System => Topic started by: KokosNaPalmie on March 13, 2020, 04:04:06 pm

Title: Mouse wheel scroll detection not working
Post by: KokosNaPalmie on March 13, 2020, 04:04:06 pm
I'm trying to make settings state for my game and my mouse scroll detection is not working. I tried to find solution on sfml website, youtube and forums but my code still doesn't want to work
Here's the function I made to move a view:
void Options::CheckEvents()
{
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))   // quit check
        this->quit = true;

    while(window->pollEvent(this->evnt))
    {
        if (this->evnt.type == sf::Event::MouseWheelScrolled)
        {
            std::cout << "[DEBUG]: mouse wheel scrolled\n"; // this one prints, but not always
                                                            // (for example 4 prints per 10s of scrolling)

            if (evnt.mouseWheelScroll.delta < 0 && view->getCenter().y > WinHeight / 2) // if view center is not too far away
            {
                std::cout << "[DEBUG]: View moved up\n"; // not printing, even when I remove this ^ check
                view->move(sf::Vector2f(0, -view_speed * this->dt));
            }
            if (evnt.mouseWheelScroll.delta > 0 && view->getCenter().y < WinHeight * 2) // if view center is not too far away
            {
                std::cout << "[DEBUG]: View moved down\n"; // not printing, even when I remove this ^ check
                view->move(sf::Vector2f(0,  view_speed * this->dt));
            }
        }
    }
}

Additionally when I move the code out of pollEvent function the view moves only when I move my mouse
Title: Re: Mouse wheel scroll detection not working
Post by: G. on March 13, 2020, 08:42:52 pm
Flush your cout buffer if you want it to print something.
<< std::endl or << std::flush
Title: Re: Mouse wheel scroll detection not working
Post by: KokosNaPalmie on March 13, 2020, 11:49:49 pm
But how to scroll down the window? And for good news, the view is actually moving by 10 pixels onlywhen mouse position is on special screen position related to window
Title: Re: Mouse wheel scroll detection not working
Post by: KokosNaPalmie on March 15, 2020, 12:59:23 am
OK, I solved this by getting sf::Event evnt from the main program class, not using the local one

void Options::CheckEvents(sf::Event &evnt) // evnt is not from 'this' class, but from the main class that controls the whole program
{

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) // quit check
    {
        this->quit = true;
    }

    if (evnt.type == sf::Event::MouseWheelScrolled)
    {
        if (evnt.mouseWheelScroll.delta > 0) // moving up
        {
            view->move(sf::Vector2f(0, -this->view_speed * this->dt)); // dt is delta time
        }
        else if (evnt.mouseWheelScroll.delta < 0) // moving down
        {
            view->move(sf::Vector2f(0, this->view_speed * this->dt));  // dt is delta time
        }
    }
}
Title: Re: Mouse wheel scroll detection not working
Post by: Laurent on March 15, 2020, 09:26:02 am
Obviously you can't run two separate event loops, as one won't see events polled in the other.