texus / TGUI

Cross-platform modern c++ GUI

Home Page:https://tgui.eu

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

setHorizontalAlignment not aligning Label

sandorex opened this issue · comments

Im using TGUI 0.7.4 with SFML 2.4.2

setHorizontalAlignment doesn't do anything i added 2 labels in same position so alignment is supposed to center them but they are in same position, am i doing something wrong ?

my code:

#include <cstdio>
#include <iostream>
#include <random>
#include <TGUI/TGUI.hpp>

int main(int argc, char *argv[])
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "");
    tgui::Gui gui(window);

    auto lbl = tgui::Label::create("Hello1");
    lbl->setTextColor(tgui::Color(sf::Color::White));
    lbl->setPosition({50, 60});
    lbl->setHorizontalAlignment(tgui::Label::HorizontalAlignment::Center);
    gui.add(lbl);

    auto lbl2 = tgui::Label::create("Hello2");
    lbl2->setTextColor(tgui::Color(sf::Color::White));
    lbl2->setPosition({50, 60});
    gui.add(lbl2);

    window.setFramerateLimit(80);
    
    while (window.isOpen())
    {
        sf::Event event;

        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            else if (event.type == sf::Event::Resized)
            {
                window.setView(sf::View(sf::FloatRect(0, 0, (float)event.size.width, (float)event.size.height)));
                gui.setView(window.getView());
            }
            gui.handleEvent(event);
        }

        window.clear();
        gui.draw();
        window.display();
    }

    return 0;
}

result:
screenshot 2018-04-20 14 07 18

The function exists to center the text of a label inside its given size, it only works when you call setSize on the label to tell it in which area it should center.

If you just want to center the label (or any other widget) inside its parent then you can use the following:

lbl->setPosition({"(parent.width - width) / 2", 60});

Oh im sorry i didn't find much of documentation but thanks anyways!