sfml how to detect if a mouse is within bounds of a play menu

  • kalifire
    Likes 0

    Problem Description
    Hey, I used your tutorial 51 for sfml on youtube to create menu for a game I am working on.
    In the video, you allowed user keyboard input to select play,options or exit with the keyboard.
    I am trying to take it a little further, and allow the user to click on each option with just a mouse.
    I made a new function in the menu class called "MouseClick."

    void Menu::MouseClick(sf::RenderWindow &window)
    {
    //sf::Mouse::setPosition(sf::Vector2i(0, 0));
    if (sf::Mouse::isButtonPressed(sf::Mouse::Left))&& sf::Mouse::getPosition().x == 0  && sf::Mouse::getPosition().y == 0)
        {
    cout<<"Mouse is within bounds" <<endl;
    }

    }
     
    I used the setPosition above just to make sure that my code ran properly, which it did as long as I had the sf::Mouse::getPosition().x==0 and same for the y.
    The issue is that my play button is not within that specific pixel (0,0).

    I even got a (x,y) that was within my play button ex (240,113) is a coord that is close enough to the play button that I want the user to click. so if i use 240,113 in my getPosition for x,y values what I found out was that it only sets that single pixel not a wide area to click on for my play button which is what I want.

     

    s. Inline image 1

    I even got a rectangle that looks like this.
    sf::RectangleShape Rectangle;

        //set size = to a size a bit bigger than the play button(height and width)
        Rectangle.setSize(sf::Vector2f(62, 54));

        //puts the rectangle over the play button
        Rectangle.setPosition(250, 120); //hardcoded the position of Rectangle (x,y) 250,120 is the origin of the very left corner of the rectangle
    with that code written in main.
     
    Sorry for the long message, I have spent a few days  trying to figure it out myself using youtube, and google.
    Thank you
     
     
  • Sonar Systems admin
    Likes 0

    How much is the position off by?

  • kalifire
    Likes 0

    well the problem is that the (240,113) coord sets to a single pixel on the screen, and so you have to click that specific pixel for the play button to run. Normally you can click anywhere on the “play” button for it to run not a specific pixel of play.

  • kalifire
    Likes 0

    Thank you, that fixed most of it for some reason only about half of the play button is registering a click.

  • Sonar Systems admin
    Likes 0

    Show us the code.

  • kalifire
    Likes 0

    if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && sf::Mouse::getPosition(window).x && sf::Mouse::getPosition(window).y)
        {
            sf::IntRect playButtonRect(menu[0].getPosition().x, menu[0].getPosition().y, menu[0].getGlobalBounds().width, menu[0].getGlobalBounds().height);

            if (playButtonRect.contains(sf::Mouse::getPosition(window)))
            {
                cout << "play button is clicked " << endl;
                return true;
            }
            return false;
        }

  • Sonar Systems admin
    Likes 0

    Can you show us a video demonstration of the problem.

Login to reply