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

Author Topic: Main menu buttons  (Read 1315 times)

0 Members and 1 Guest are viewing this topic.

Suggs

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Main menu buttons
« on: February 09, 2013, 01:42:53 pm »
How exactly would I code a function that checks to see if the mouse is in a certain area, and then when it's clicked another function will start. SO basically, how can I make an invisible box around a sprite that checks to see if the mouse is in it?

I can't seem to find anything on google.

This is with SFML 2.0 by the way.

Thanks in advance :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10991
    • View Profile
    • development blog
    • Email
Re: Main menu buttons
« Reply #1 on: February 09, 2013, 02:26:37 pm »
You can either get the transformation (position & size) from the visible element or you can store them separately (e.g. std::vector<sf::FloatRect>).
Then you can iterate over those rects and do a simple 'collision' test. With SFML's sf::Rect<T> you can just call if(rect.contains(sf::Mouse::getPosition(window))) and then react the way you want to it.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

bumblecorn

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Main menu buttons
« Reply #2 on: February 09, 2013, 03:05:41 pm »
Indeed, this is the way I did it:

if (startBtn.getGlobalBounds().contains(mouseCoordX, mouseCoordY))
                {
                        //switch to a highlighted version of the button

                        if (startBtn.getGlobalBounds().contains(mouseCoordX, mouseCoordY) && sf::Mouse::isButtonPressed(sf::Mouse::Left))
                        {
                                //do things
                        }
                }