Jump to content

After recreation with clearing a widgets thats stores in a vector of custom widgets are now visible


Dreikblack
 Share

Go to solution Solved by Josh,

Recommended Posts

Steam beta branch.

When i first time create widgets it's works. But when i destroy them and create new ones in a same vector they are invisible.

It worked several months ago. Need that for an inventory gui.

image.png.15e6d527c2b602e89d3ec7c3994af9d9.pngimage.png.ef5393a41bd06f3638e38a8ef077ec5c.png

#include "UltraEngine.h"

using namespace UltraEngine;

class ContainerWidget : public Panel
{
    vector<shared_ptr<Widget>> btns;

protected:

    virtual bool Initialize(const int x, const int y, const int width, const int height, shared_ptr<Widget> parent)
    {
        btns.resize(4);
        return Widget::Initialize(text, x, y, width, height, parent, style);
    }

public:

    void initBtns() {
        clearBtns();
        for (int i = 0; i < btns.size(); i++)
        {
            btns[i] = CreateButton("Btn", i * 50 + 10, 0, 20, 20, Self()->As<Widget>());
        }
    }

    void clearBtns() {
        for (int i = 0; i < btns.size(); i++)
        {
            if (btns[i])
            {
                btns[i]->SetParent(nullptr);
                btns[i] = nullptr;
            }
        }
    }

    static shared_ptr<ContainerWidget> create(const int x, const int y, const int width, const int height, shared_ptr<Widget> parent)
    {
        struct Struct : public ContainerWidget {
        };
        auto instance = std::make_shared<Struct>();
        instance->Initialize(x, y, width, height, parent);
        instance->SetColor(0.5, 0.5, 0.5, 1, WIDGETCOLOR_BACKGROUND);
        instance->initBtns();
        return instance;
    }

};

shared_ptr<Window> window;
shared_ptr<Framebuffer> framebuffer;
shared_ptr<World> menuWold;
shared_ptr<Interface> ui;
shared_ptr<Camera> uiCamera;

shared_ptr<Widget> btn;
shared_ptr<ContainerWidget> container;

void initGui()
{
    auto default_font = LoadFont("Fonts\\arial.ttf");
    ui = CreateInterface(menuWold, default_font, framebuffer->GetSize());
    ui->SetRenderLayers(2);
    ui->root->SetColor(0.0f, 0.0f, 0.0f, 0.0f);
    uiCamera = CreateCamera(menuWold, PROJECTION_ORTHOGRAPHIC);
    uiCamera->SetPosition((float)framebuffer->GetSize().x * 0.5f, (float)framebuffer->GetSize().y * 0.5f, 0);
    uiCamera->SetRenderLayers(2);
    uiCamera->SetClearMode(CLEAR_DEPTH);
    container = ContainerWidget::create(10, 50, 200, 50, ui->root);
    btn = CreateButton("Reinit buttons", 10, 10, 150, 20, ui->root);
}


int main(int argc, const char* argv[])
{
    //Get the displays
    auto displays = GetDisplays();

    //Create a window
    window = CreateWindow("Ultra Engine", 0, 0, 300, 300, displays[0], WINDOW_DEFAULT);

    //Create a world
    menuWold = CreateWorld();

    //Create a framebuffer
    framebuffer = CreateFramebuffer(window);

    //Create light
    auto light = CreateBoxLight(menuWold);
    light->SetRange(-10, 10);
    light->SetRotation(15, 15, 0);
    light->SetColor(2);

    //Create camera
    auto camera = CreateCamera(menuWold);
    camera->SetClearColor(0.125);
    camera->SetPosition(0, 0, -3);
    camera->SetFov(70);

    initGui();

    //Main loop
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        while (PeekEvent())
        {
            const Event ev = WaitEvent();
            if (ev.source == btn && ev.id == EVENT_WIDGETACTION)
            {
                container->initBtns();
            }
            ui->ProcessEvent(ev);
        }
        menuWold->Update();
        menuWold->Render(framebuffer);
    }
    return 0;
}

 

  • Thanks 1
Link to comment
Share on other sites

Okay, I have simplified this and it appears to be some sort of clipping problem. Space key recreates the button:

#include "UltraEngine.h"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{
    //Get the displays
    auto displays = GetDisplays();

    //Create a window
    auto window = CreateWindow("Ultra Engine", 0, 0, 300, 300, displays[0], WINDOW_DEFAULT);

    //Create a world
    auto menuWold = CreateWorld();

    //Create a framebuffer
    auto framebuffer = CreateFramebuffer(window);

    //Create camera
    auto camera = CreateCamera(menuWold);

    auto uiCamera = CreateCamera(menuWold, PROJECTION_ORTHOGRAPHIC);
    uiCamera->SetRenderLayers(2);
    uiCamera->SetPosition(framebuffer->size.x / 2.0f, framebuffer->size.y / 2.0f);

    auto ui = CreateInterface(menuWold, LoadFont("Fonts\\arial.ttf"), iVec2(300));
    ui->SetRenderLayers(2);

    //This works:
    //auto panel = CreatePanel(10, 19, 200, 50, ui->background);

    //This does not work:
    auto panel = CreatePanel(10, 20, 200, 50, ui->background);

    panel->SetColor(1, 0, 0, 1);

    auto btn = CreateButton("Btn", 10, 0, 20, 20, panel);

    //Main loop
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        if (window->KeyHit(KEY_SPACE))
        {
            btn->SetParent(nullptr);
            btn = CreateButton(String(Random(100)), 10, 0, 20, 20, panel);
        }
        while (PeekEvent())
        {
            const Event ev = WaitEvent();
            ui->ProcessEvent(ev);
        }
        menuWold->Update();
        menuWold->Render(framebuffer);
    }
    return 0;
}

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...