Jump to content

How can i structure my UAK code?


Vida Marcell
 Share

Recommended Posts

You need to keep track of each variable you create, if you just do something like this:

auto window = CreateWindow("Window 1"....);
window = CreateWindow("Window 2"....);

then the first window is not referenced anymore and therefore deleted.

To better handle this, you should use classes to hold everything your windows need. (one class per window).

A very basic sample:

#include "UltraEngine.h"

using namespace UltraEngine;

class MyWindowBase : public Object
{
protected:
    shared_ptr<Window> _window;
    shared_ptr<Interface> _ui;
    string _title;
    bool _closed;

public:
    MyWindowBase(string title,int px,int py, int width,int height,  vector<shared_ptr<Display>> displays) : _title(title)
    {
        _window = CreateWindow(_title,px,py,width,height, displays[0], WINDOW_TITLEBAR | WINDOW_RESIZABLE | WINDOW_CENTER);
        _ui = CreateInterface(_window);
        _closed = false;
    }

    bool Closed()
    {
        return _closed;
    }

    void virtual Init() abstract;

    bool virtual Update(const Event ev)
    {
        if (ev.source != NULL && ev.source->As<Window>() == _window && !_closed)
        {
            switch (ev.id)
            {
            case EVENT_WINDOWCLOSE:
                _closed = true;
                _window = NULL;
                return true;
            }
        }

        return false;
    }
};

class MainWindow : public MyWindowBase
{
private:
    shared_ptr<Widget> _listbox;

public:
    
    MainWindow(string title, int px, int py, int width, int height, vector<shared_ptr<Display>> displays) : MyWindowBase(title, px, py, width, height, displays)
    {
        Init();
    }

    void Init() override
    {
        auto sz = _ui->root->GetSize();
        _listbox = CreateListBox(20, 20, sz.x - 40, sz.y - 40, _ui->root);
        _listbox->AddItem("Item 1", true);
        _listbox->AddItem("Item 2");
        _listbox->AddItem("Item 3");
        _listbox->AddItem("Item 4");
        _listbox->AddItem("Item 5");
        _listbox->AddItem("Item 6");
        _listbox->SetLayout(1, 1, 1, 1);
    }

    bool virtual Update(const Event ev) override
    {
        if (!MyWindowBase::Update(ev))
        {
            switch (ev.id)
            {
            case EVENT_WIDGETACTION:
                Print("Item " + String(ev.data) + " action");
                break;
            case EVENT_WIDGETSELECT:
                Print("Item " + String(ev.data) + " selected");
                break;
            }
        }

        return false;
    }

};

class SecondWindow : public MyWindowBase
{
private:
    shared_ptr<Widget> _listbox;

public:
    SecondWindow(string title, int px, int py, int width, int height, vector<shared_ptr<Display>> displays) : MyWindowBase(title, px, py, width, height, displays)
    {
        Init();
    }

    void Init() override
    {
        auto sz = _ui->root->GetSize();
        _listbox = CreateListBox(20, 20, sz.x - 40, sz.y - 40, _ui->root);
        _listbox->AddItem("Item 1.1", true);
        _listbox->AddItem("Item 1.2");
        _listbox->AddItem("Item 1.3");
        _listbox->AddItem("Item 1.4");
        _listbox->AddItem("Item 1.5");
        _listbox->AddItem("Item 1.6");
        _listbox->SetLayout(1, 1, 1, 1);
    }

    bool virtual Update(const Event ev) override
    {
        if (!MyWindowBase::Update(ev))
        {
            switch (ev.id)
            {
            case EVENT_WIDGETACTION:
                Print("Item " + String(ev.data) + " action");
                break;
            case EVENT_WIDGETSELECT:
                Print("Item " + String(ev.data) + " selected");
                break;
            }
        }

        return false;
    }

};

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

    vector<shared_ptr<MyWindowBase>> windows;
    windows.push_back(make_shared<MainWindow>("Main Window", 0, 0, 800, 600, displays));
    windows.push_back(make_shared<SecondWindow>("Second Window", 400, 300, 400, 300, displays));
    bool running = true;

    while (running)
    {
        const Event ev = WaitEvent();   
        running = false;

        for (auto w : windows) 
        {
            if (w->Update(ev))
                break;

            if (!w->Closed())
                running = true;
        }
    }
    return 0;
}

 

  • Thanks 1
  • Intel® Core™ i7-8550U @ 1.80 Ghz 
  • 16GB RAM 
  • INTEL UHD Graphics 620
  • Windows 10 Pro 64-Bit-Version
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...