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

Author Topic: Having an odd issue with centering text on a sf::RectangleShape  (Read 1697 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 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!
« Last Edit: March 01, 2014, 01:56:34 am by The Terminator »
Current Projects:
Technoport

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Re: Having an odd issue with centering text on a sf::RectangleShape
« Reply #1 on: March 01, 2014, 03:04:11 am »
Don't forget the 'left' and 'top' members of getGlobalBounds(). For text, they aren't 0 by default (or something) if I remember correctly.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Having an odd issue with centering text on a sf::RectangleShape
« Reply #2 on: March 01, 2014, 12:00:05 pm »
^ that

Also, make sure you're not updating the position as text is entered as it will affect the vertical based on characters entered.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: Having an odd issue with centering text on a sf::RectangleShape
« Reply #3 on: March 01, 2014, 04:49:24 pm »
Thanks guys, I resolved the issue.
Current Projects:
Technoport

 

anything