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!