Problems with dynamic texture changing

Started by GetterSetter, 23 March 2024, 17:14:06

GetterSetter

Hello!
What I'm supposed to do: when button was clicked its texture should be changed.
I have textures in one single .png file and my code looks like:
bool linked = 1;
    tgui::Texture link_t[2];
    for (short i = 0; i < 2; ++i)
        link_t[i].load("lu.png", {unsigned(50 * i), 0, 50, 50});

    auto link_b = gui.get<tgui::Button>("link_b");

    auto lb_func = [&link_b, &linked, &link_t]()
    {
        linked = !linked;
        link_b->getRenderer()->setTexture(link_t[linked]);
    };

    gui.get<tgui::Button>("link_b")->onClick(lb_func);
It's expected the texture to be changed every time I click the button, but actually it changes only once.

So I've written the following code to "separate" textures:
bool linked = 1;
    tgui::Texture linked_t, unlinked_t;

    linked_t.load("linked.png");
    unlinked_t.load("unlinked.png");

    auto link_b = gui.get<tgui::Button>("link_b");

    if (linked)
        link_b->getRenderer()->setTexture(linked_t);
    else
        link_b->getRenderer()->setTexture(unlinked_t);

    auto lb_func = [&link_b, &linked, &linked_t, &unlinked_t]()
    {
        linked = !linked;
        if (linked)
            link_b->getRenderer()->setTexture(linked_t);
        else
            link_b->getRenderer()->setTexture(unlinked_t);
    };

    gui.get<tgui::Button>("link_b")->onClick(lb_func);
And it works pretty good! I think the problem that my first piece of code uses the textures with the same ID. Perhaps, that's the reason why setTexture() doesn't change the texture.

texus

Actually I think this issue was fixed already recently.
The "Texture::operator==" contained a bug where it considered two images with the same filename but a different part rect to be identical to each other.
So calling setTexture would do nothing because the code thought that you were just setting the same texture again.

I just uploaded TGUI 1.2.0 to the website (if it still shows 1.1 on the download page then refresh the page with ctrl+F5). If you use that version then this and the earlier reported bug should be fixed.

GetterSetter