Hi there,
I'm doing a little test for my GUI system, where I have a sf::RectangleShape that is acting as an edit box. I'm trying to center the text directly on that rectangle shape, but whenever I call my centerText() method for the first time (whenever a person inputs a character for the first time) it changes the rectangle's position a little bit to the left and up, then works perfectly. Sorry if this is a little confusing but here's my code:
sf::RectangleShape m_box;
sf::Text m_text_drawn;
void EditBox::handleTextEvent(sf::Uint32 character)
{
if (!getInWidget() || !getVisible())
return;
if (character == 8)
{
if (!m_user_text.isEmpty())
m_user_text.erase(m_user_text.getSize() - 1);
return;
}
if (character < 127)
m_user_text += character;
centerText();
}
void EditBox::centerText()
{
m_box.setOrigin(m_box.getGlobalBounds().width / 2, m_box.getGlobalBounds().height / 2);
m_text_drawn.setOrigin(m_text_drawn.getGlobalBounds().width / 2, m_text_drawn.getGlobalBounds().height / 2);
m_text_drawn.setPosition(m_box.getPosition());
}
Thanks!