Jump to content

[SOLVED] Runtime Unigine::Widgets C++


photo

Recommended Posts

Posted

hi, i need to create some widgets in my Unigine-based program.

somewhere in runtime, after unigine init, in some qt event handler.

 

Now working only this variant:

(here copy-paste from WidgetWindow.cpp sample)

using namespace Unigine;
GuiPtr gui = Gui::get();

// create window
WidgetWindowPtr window = WidgetWindow::create(gui,"Hello from C++ world",4,4);
window->setWidth(320);
window->setSizeable(1);

// create editline
WidgetEditLinePtr editline = WidgetEditLine::create(gui,"Edit me");
window->addChild(editline->getWidget(),Gui::ALIGN_EXPAND);
editline->setFontSize(16);

// create button
WidgetButtonPtr button = WidgetButton::create(gui,"Press me");
window->addChild(button->getWidget(),Gui::ALIGN_EXPAND);
button->setFontSize(18);

// arrange window
window->arrange();
gui->addChild(window->getWidget(),Gui::ALIGN_OVERLAP | Gui::ALIGN_CENTER);
			
while(engine->isDone() == 0)
{
    engine->update();
    my_update();
	
    engine->render();
    my_render();
		
    engine->swap();

    my_swap();
}

but i need create widget in runtime on some event handler,

this something like event :D :

    int count = 0;
    while(engine->isDone() == 0)
    {
        if(count == 10000)
        {
                using namespace Unigine;
	        GuiPtr gui = Gui::get();

		// create window
		WidgetWindowPtr window = WidgetWindow::create(gui,"Hello from C++ world",4,4);
		window->setWidth(320);
		window->setSizeable(1);

		// create editline
		WidgetEditLinePtr editline = WidgetEditLine::create(gui,"Edit me");
		window->addChild(editline->getWidget(),Gui::ALIGN_EXPAND);
		//editline->setCallback1(Gui::CHANGED,MakeCallback(editline_changed));
		editline->setFontSize(16);

		// create button
		WidgetButtonPtr button = WidgetButton::create(gui,"Press me");
		window->addChild(button->getWidget(),Gui::ALIGN_EXPAND);
		button->setFontSize(18);

		// arrange window
		window->arrange();
		gui->addChild(window->getWidget(),Gui::ALIGN_OVERLAP | Gui::ALIGN_CENTER);
        }
        engine->update();
        my_update();
	
        engine->render();
        my_render();
		
        engine->swap();

        my_swap();
        count ++;
    }

in this case my screen is empty. why? and how fix it?

Posted

Hi there!

 

I did simple calculations and if you're going to wait 10000 frames with, let's say, 60 fps fixed framerate then it'll be about 2.77777 minutes before this code get actually executed. That may be the reason. :)

Posted

we`ve used Log::message to catch this moment  :D, and vertical sync is off. i was about 30 sec, but widget was not appear

Posted

I've found the problem.

 

It's lifetime and pointers, you know, C++ stuff. :)

 

When you have smart pointer inside some scope it'll automatically destroy its contents after program reach the end of that scope. So in that code:

{
    WidgetWindowPtr window = WidgetWindow::create(gui,"Hello from C++ world",4,4);
} // the window widget will be automatically killed just because smart pointer will die here and kill its contents (because there no more references left)

So consider moving that smart pointers out of scope or use raw pointers.

×
×
  • Create New...