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

Author Topic: Mouse Trouble  (Read 2726 times)

0 Members and 1 Guest are viewing this topic.

clampflower

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Mouse Trouble
« on: July 10, 2024, 11:50:38 am »
Hi All

Hope this is the right place. Trying to trap mouse and window resize events. C++ in MS Visual Studio 2022 (Windows).  I got the code working before but without warning I cant get this working now. The problem is when I click the left click, I get a scroll wheel down event.

Relevant code is.

Quote

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                windowHeight = 0;
                windowWidth = 0;
                window.close();
            }
            else if (event.type == sf::Event::Resized)
            {
                std::cout << "Resize " << std::endl;
                windowWidth = event.size.width;
                windowHeight = event.size.height;
                sf::FloatRect visibleArea(0, 0, windowWidth, windowHeight);
                window.setView(sf::View(visibleArea));
            }
            else if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel)
            {
                if (event.mouseWheelScroll.delta > 0) {
                    std::cout << "Wheel in " << std::endl;
                }
                else
                {
                   std::cout << "Wheel out " << std::endl;
                }
            }
            else if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
            {
                std::cout << "Left Mouse Button" << std::endl;
            }




        }

It's probably something stupid I have done but I just cant see it. Any help would be appreciated. Thanks.

Moon.


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Mouse Trouble
« Reply #1 on: July 10, 2024, 12:50:52 pm »
You need to always check the event type and you shouldn't mix events and real-time input (i.e. sf::Mouse::getPosition().

So something like this:


    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            // ...
            else if (event.type == sf::Event::MouseWheelScrolled && event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel)
            {
                if (event.mouseWheelScroll.delta > 0) {
                    std::cout << "Wheel in " << std::endl;
                }
                else
                {
                   std::cout << "Wheel out " << std::endl;
                }
            }
            else if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left)
            {
                std::cout << "Left Mouse Button" << std::endl;
            }
            // ...
        }
 
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

clampflower

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Mouse Trouble
« Reply #2 on: July 13, 2024, 07:50:30 am »

Thanks eXpl0it3r. Your reply is much appreciated. I have dug in to this some more and perhaps come across a better method. Continuously checking the events to get a status is a really bad idea but found that the switch command enables me to discretely handle each event in turn. Code is:

        sf::Event event;
        while (window.pollEvent(event))
        {
            switch (event.type)
            {
                case sf::Event::Closed:
                    window.close();
                    break;

                case sf::Event::MouseWheelScrolled:
                    if (event.mouseWheelScroll.delta > 0)
                    {
      \\ Mouse Scroll Up
                    }
                    else
                    {
      \\ Mouse Scroll Down
                    }
                    break;

                case sf::Event::MouseButtonPressed:
                    if (event.mouseButton.button == sf::Mouse::Left)
                    {
      \\ Left mouse button pressed.
                    }
                    else
                    {
      \\ Right mouse button pressed.
                    }

                    break;

                case sf::Event::MouseButtonReleased:

      \\ Either Mouse Button Released

                    break;


                case sf::Event::Resized:

      \\ Resize Event

                    windowWidth = event.size.width;
                    windowHeight = event.size.height;

                    visibleArea = sf::FloatRect(0, 0, windowWidth, windowHeight);

                    break;

                default:
                    break;
            }
        }

I think switch is a much better way to do this and don't know why SFML don't suggest this in their tutorials. Also probably worth noting that I am a terrible coder and not great with instructions. Having to learn patience and get the ability to read and understand the docs.

Thanks once again.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Mouse Trouble
« Reply #3 on: July 13, 2024, 09:35:40 am »
The switch won't really make a difference, as the compiler will likely optimize things, but it's indeed an option that many use  :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything