Jump to content

[SOLVED] Sort Widgets by depth


photo

Recommended Posts

Hi, we have placed 2D Widgets over some 3D objects to display their info. We have implemented camera controls (zoom in/out, panning and orbit) and we realized that Widgets do overlap in some camera positions. Problem is that Widget painting is not done correctly in screen-space Z order. We tried using Widget::setOrder() and Widget::raise() but we could not come to a solution.

How can we sort Widgets to make them paint correctly (closer to camera in front and furthest at back)?

Regards, Javier

Edited by javier.serrano
Link to comment

Hi Javier,

Widget::setOrder() works only for ALIGN_OVERLAP and non-ALIGN_FIXED widgets when you use Widget::addChild() function.
Look at the example:

Unigine::Vector<Unigine::WidgetSpritePtr> sprites;

int AppWorldLogic::init()
{
	// create 3 squares with different colors
	for (int i = 0; i < 3; i++)
	{
		WidgetSpritePtr &sprite = sprites.append();
		sprite = WidgetSprite::create(Gui::get(), "white.dds");
		sprite->setPosition(i * 40 + 50, i * 40 + 50);
		sprite->setWidth(100);
		sprite->setHeight(100);
		Gui::get()->addChild(sprite->getWidget(), Gui::ALIGN_OVERLAP);
	}

	sprites[0]->setColor(vec4(1, 0.3f, 0.3f, 1));
	sprites[1]->setColor(vec4(0.3f, 1, 0.3f, 1));
	sprites[2]->setColor(vec4(0.3f, 0.3f, 1, 1));

	return 1;
}

int AppWorldLogic::update()
{
	// press space key to random reorder squares
	if (App::get()->clearKeyState(' '))
	{
		for (int i = 0; i < 3; i++)
		{
			sprites[i]->setOrder(Game::get()->getRandomInt(0, 10));
			
			Gui::get()->removeChild(sprites[i]->getWidget());
			Gui::get()->addChild(sprites[i]->getWidget());

			Log::message("%d ", sprites[i]->getOrder());
		}
		Log::message("\n");
	}
	return 1;
}

Best regards,
Alexander

Link to comment
  • morbid changed the title to [SOLVED] Sort Widgets by depth
×
×
  • Create New...