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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Chopa17

Pages: [1]
1
Window / Checking the position of the cursor in relation to buttons
« on: September 15, 2018, 05:18:57 pm »
I'm trying to setup a changing texture based upon hovering over a button but having an issue that the hovering isn't matching up with the buttons rectangle, the image below demonstrates the problem for two different sized buttons (which have been cropped out from a maximised window with a titlebar).


This is my relevant piece of code:
void TR_GUI_Button::Draw()
{
        if (!m_bIsVisible) return;

        sf::Sprite xSprite;
        sf::Vector2i xPos = sf::Mouse::getPosition();

        if (!m_xRect.contains(xPos))
        {
                xSprite.setTexture(m_xDefaultTexture);
        }
        else
        {
                xSprite.setTexture(m_xHoverTexture);
        }

        float fScaleX = m_xRect.width / xSprite.getGlobalBounds().width;
        float fScaleY = m_xRect.height / xSprite.getGlobalBounds().height;
        xSprite.setScale(sf::Vector2f(fScaleX, fScaleY));
        xSprite.setPosition(static_cast<float>(m_xRect.left), static_cast<float>(m_xRect.top));

        TR_GameManager::GetGameWindow().draw(xSprite);
}
 

I thought it might be because the window's title bar wasn't being included so I added this code to the draw function:
xPos.y -= ::GetSystemMetrics(SM_CYCAPTION);

This still doesn't match up as it now overcompensates for the larger button.


Any help would be appreciated.

Pages: [1]