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

Author Topic: Can't manage to have a clickable button.  (Read 2662 times)

0 Members and 1 Guest are viewing this topic.

Pvt.Derpy

  • Newbie
  • *
  • Posts: 7
    • View Profile
Can't manage to have a clickable button.
« on: November 08, 2018, 04:10:39 pm »
I know there are dozens of threads already, and I have read all of them. That's why I have no idea what I'm doing wrong.

I have a Button class:

class RectButton : public Widget
        {
        public:
                  virtual InputEventState ProcessInput(const InputEvent& event) override;
//more stuff

        private:
                float m_sizeX;
                float m_sizeY;
                sf::RectangleShape m_rect;
 

InputEventState RectButton::ProcessInput(const InputEvent & event)
{
        if (event.type == sf::Event::MouseButtonPressed)
        {
                if (event.mouseButton.button == sf::Mouse::Right)
                {
                        if (// what do I put here?)
                        {
                                std::cout << "the right button was pressed" << std::endl;
                                std::cout << "mouse x: " << event.mouseButton.x << std::endl;
                                std::cout << "mouse y: " << event.mouseButton.y << std::endl;
                        }
                }
        }
return InputEventState();
}

I know for sure I should put something like:

if (m_rect.getGlobalBound().contains(static_cast<sf::FloatRect>(sf::Mouse::getPosition())))

But, even there, I cannot convert FloatRect to Vector2f. Sorry, I'm 100% lost.

Thanks in advance.

EDIT: formatting
« Last Edit: November 08, 2018, 04:12:13 pm by Pvt.Derpy »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Can't manage to have a clickable button.
« Reply #1 on: November 08, 2018, 04:27:01 pm »
Why are you trying to cast the mouse's position to a FloatRect? The contains function accepts a point, not a rectangle.

Pvt.Derpy

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Can't manage to have a clickable button.
« Reply #2 on: November 08, 2018, 04:34:14 pm »
Why are you trying to cast the mouse's position to a FloatRect? The contains function accepts a point, not a rectangle.

Isn't the mouse's position a X/Y coordinate on the screen? If it isn't how do I get a point out of its position?

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Can't manage to have a clickable button.
« Reply #3 on: November 08, 2018, 04:53:39 pm »
Yes the mouse's position is a X/Y coordinate on the screen. That was my point. Why are you trying to cast it to a rectangle?

if (m_rect.getGlobalBound().contains(static_cast<sf::FloatRect>(sf::Mouse::getPosition())))

Also, the MouseButtonPressed event already contains the mouse's coordinates (as shown by your cout prints), so you don't really need to use sf::Mouse::getPosition(). Just use the coordinates that are given to you in the event.

Pvt.Derpy

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Can't manage to have a clickable button.
« Reply #4 on: November 08, 2018, 05:14:56 pm »
Thanks, that's just the input I was looking for!

I came up with another solution, doesn't look clean. But it works:

if (event.mouseButton.button == sf::Mouse::Right)
                        {
                                if (event.mouseButton.x >= m_rect.getPosition().x
                                        && event.mouseButton.x <= m_rect.getPosition().x + m_rect.getSize().x
                                        && event.mouseButton.y >= m_rect.getPosition().y
                                        && event.mouseButton.y <= m_rect.getPosition().y + m_rect.getSize().y)
                                {
                                        std::cout << "the right button was pressed" << std::endl;
                                        std::cout << "mouse x: " << event.mouseButton.x << std::endl;
                                        std::cout << "mouse y: " << event.mouseButton.y << std::endl;
                                }
                        }

I dont know if there is a cleaner way, but at least I can proceed for now. Thanks again for your kind help!