Hello everyone!
I have a set of different shapes like rectangles, circles, etc., and I need to align the text associated with each shape perfectly in the center of the shape. I wrote the following algorithm to determine offsets for the x and y axes respectively:
sf::Text text = shape.getText();
float textOffsetX = (shape.getShape()->getGlobalBounds().width - text.getGlobalBounds().width) / 2;
float textOffsetY = (shape.getShape()->getGlobalBounds().height - text.getGlobalBounds().height) / 2;
text.setPosition(shapeOffsetX + textOffsetX, shapeOffsetY + textOffsetY);
As a result, the text inside each shape is perfectly aligned horizontally but wrong vertically. Please find the screenshot in attachments. I do not know how I can put it here :-[
My shapes are moving, so in addition to text offsets, there are also shape offsets. It does not have to matter. I have also tried two more ways, but the result is the same:
1.
sf::Text text = shape.getText();
float textOffsetX = (shape.getShape()->getGlobalBounds().width - text.getGlobalBounds().width) / 2;
auto glyph = text.getFont()->getGlyph(text.getString()[0], text.getCharacterSize(), false);
float textOffsetY = (shape.getShape()->getGlobalBounds().height - glyph.bounds.height) / 2;
text.setPosition(shapeOffsetX + textOffsetX, shapeOffsetY + textOffsetY);
2.
sf::Text text = shape.getText();
float textOffsetX = (shape.getShape()->getGlobalBounds().width - text.getGlobalBounds().width) / 2;
float textOffsetY = (shape.getShape()->getGlobalBounds().height - text.getCharacterSize()) / 2;
text.setPosition(shapeOffsetX + textOffsetX, shapeOffsetY + textOffsetY);
Could you please help me with this question?
To center text, you also need to consider its local bounds, for example like this:
#include <iostream>
#include <SFML/Graphics.hpp>
int main()
{
auto window = sf::RenderWindow{{800, 600, 32}, "SFML Window"};
window.setFramerateLimit(60);
auto font = sf::Font{};
if (!font.loadFromFile("OpenSans.ttf"))
{
std::cerr << "Could not load font\n";
return -1;
}
auto rectangle = sf::RectangleShape{ {300.f, 100.f} };
rectangle.setOutlineThickness(1.f);
rectangle.setOutlineColor(sf::Color::Green);
rectangle.setPosition({ 200.f, 200.f });
rectangle.setFillColor(sf::Color::Transparent);
auto text = sf::Text{ "Test 1234", font };
text.setOrigin(text.getGlobalBounds().getSize() / 2.f + text.getLocalBounds().getPosition());
text.setPosition(rectangle.getPosition() + (rectangle.getSize() / 2.f));
auto globalBounds = text.getGlobalBounds();
auto localBounds = text.getLocalBounds();
std::cout << "(" << globalBounds.left << ", " << globalBounds.top << ") (" << globalBounds.width << ", " << globalBounds.height << ")\n";
std::cout << "(" << localBounds.left << ", " << localBounds.top << ") (" << localBounds.width << ", " << localBounds.height << ")\n";
while (window.isOpen())
{
for (auto event = sf::Event{}; window.pollEvent(event);)
{
if (event.type == sf::Event::Closed)
{
window.close();
return 0;
}
}
window.clear();
window.draw(rectangle);
window.draw(text);
window.display();
}
}
text.setOrigin(text.getGlobalBounds().getSize() / 2.f + text.getLocalBounds().getPosition());
I think these should both be local bounds. i.e.:
text.setOrigin(text.getLocalBounds().getSize() / 2.f + text.getLocalBounds().getPosition());
;)