Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Mouse wheel scroll detection not working  (Read 7221 times)

0 Members and 2 Guests are viewing this topic.

KokosNaPalmie

  • Newbie
  • *
  • Posts: 10
    • View Profile
Mouse wheel scroll detection not working
« 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

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Mouse wheel scroll detection not working
« Reply #1 on: March 13, 2020, 08:42:52 pm »
Flush your cout buffer if you want it to print something.
<< std::endl or << std::flush

KokosNaPalmie

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Mouse wheel scroll detection not working
« Reply #2 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
« Last Edit: March 14, 2020, 12:25:26 am by KokosNaPalmie »

KokosNaPalmie

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Mouse wheel scroll detection not working
« Reply #3 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
        }
    }
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Mouse wheel scroll detection not working
« Reply #4 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.
Laurent Gomila - SFML developer

 

anything