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

Author Topic: Having a little bit of trouble with my draggable GUI system  (Read 2311 times)

0 Members and 1 Guest are viewing this topic.

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Hi there, I'm having a little bit of trouble regarding my draggable GUI system. The part that I'm implementing now is the dragging of objects such as buttons, labels, radio buttons, etc. For the sake of simplicity, I'm only going to post my problems with buttons, as it's the same with all the others. My system detects holding the mouse, clicking it, and hovering it over fine. However, whenever I hold down the mouse and move the cursor, the sprite of the button does not move consistently. It often stops whenever I keep dragging, and moves whenever I just click once. Here's my code:

    const bool CButton::mouseOnObject(const float x, const float y)
    {
        if (m_sprite.getGlobalBounds().contains(x, y))
            return true;

        else
            return false;
    }

    const bool CButton::mouseHoldingObject(const float x, const float y)
    {
        if (mouseOnObject(x, y) && sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
            m_holdingObject = true;
            return true;
        }

        else
        {
            m_holdingObject = false;
            return false;
        }
    }

    const bool CButton::stoppedHoldingObject(const float x, const float y)
    {
        if (!m_holdingObject && !sf::Mouse::isButtonPressed(sf::Mouse::Left))
            return true;

        else
            return false;
    }

    void CButton::update(const float x, const float y)
    {
        if (!m_hovered)
            m_sprite.setTexture(m_backgroundTexture);

        else
            m_sprite.setTexture(m_hoveredTexture);

        if (mouseOnObject(x, y) && m_visible && m_enabled)
        {
            m_buttonHoveredSound.play();

            m_hovered = true;

            if (m_draggable && mouseHoldingObject(x, y))
            {
                std::cout << "emep" << std::endl;

                m_buttonHoveredSound.stop();

                m_sprite.setPosition(x, y);
            }
        }

        else
            m_hovered = false;
    }

    void CButton::draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        if (m_visible && m_enabled)
            target.draw(m_sprite, states);
    }

That's all the relevant parts to dragging in my button class. Thanks in advance to anybody that helps!
Current Projects:
Technoport

MrMuffins

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Having a little bit of trouble with my draggable GUI system
« Reply #1 on: June 16, 2013, 07:44:14 pm »
If I understand this correctly:

if (mouseOnObject(x, y) && m_visible && m_enabled)

Will only run if your mouse is over the object. Since you can move the mouse faster then the game can update, one cycle will have the mouse outside the object before it can move its position. Then your no longer hovering it and the button stops moving.

My way to fix it simply detect if you press the left click over a button, then set a bool to true and only set it to false when u let go of left click. And move the button based on the bool.

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: Having a little bit of trouble with my draggable GUI system
« Reply #2 on: June 16, 2013, 10:29:25 pm »
If I understand this correctly:

if (mouseOnObject(x, y) && m_visible && m_enabled)

Will only run if your mouse is over the object. Since you can move the mouse faster then the game can update, one cycle will have the mouse outside the object before it can move its position. Then your no longer hovering it and the button stops moving.

My way to fix it simply detect if you press the left click over a button, then set a bool to true and only set it to false when u let go of left click. And move the button based on the bool.

I did what you suggested, but with no avail. Any other ideas?
Current Projects:
Technoport

MrMuffins

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Having a little bit of trouble with my draggable GUI system
« Reply #3 on: June 16, 2013, 11:30:30 pm »

I did what you suggested, but with no avail. Any other ideas?

Are you sure? Something like this worked perfectly:

if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
    if (mouseOnObject(x, y)) m_holdingObject = true;
else
    m_holdingObject = false;

if (m_holdingObject)
    m_sprite.setPosition(x,y);
 

For any ideas I really don't know. Moving something with the mouse only works if your going slow. Or maybe if you run the game at like 2000fps.

 

anything