1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-06 00:55:50 +00:00
OpenMW/components/lua_ui/adapter.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

64 lines
1.6 KiB
C++
Raw Normal View History

#include "adapter.hpp"
#include <MyGUI_Gui.h>
#include "container.hpp"
#include "element.hpp"
namespace LuaUi
{
namespace
{
sol::state luaState;
}
LuaAdapter::LuaAdapter()
: mElement(nullptr)
2022-01-29 22:43:08 +00:00
, mContainer(nullptr)
{
2022-01-29 22:43:08 +00:00
mContainer = MyGUI::Gui::getInstancePtr()->createWidget<LuaContainer>(
"", MyGUI::IntCoord(), MyGUI::Align::Default, "", "");
mContainer->initialize(luaState, mContainer, false);
2022-01-29 22:43:08 +00:00
mContainer->onCoordChange([this](WidgetExtension* ext, MyGUI::IntCoord coord) { setSize(coord.size()); });
mContainer->widget()->attachToWidget(this);
}
void LuaAdapter::attach(const std::shared_ptr<Element>& element)
{
detachElement();
mElement = element;
attachElement();
2022-01-29 22:43:08 +00:00
setSize(mContainer->widget()->getSize());
// workaround for MyGUI bug
// parent visibility doesn't affect added children
setVisible(!getVisible());
setVisible(!getVisible());
}
void LuaAdapter::detach()
{
detachElement();
setSize({ 0, 0 });
}
void LuaAdapter::attachElement()
{
if (!mElement.get())
return;
if (!mElement->mRoot)
throw std::logic_error("Attempting to use a destroyed UI Element");
mContainer->setChildren({ mElement->mRoot });
mElement->mRoot->updateCoord();
mContainer->updateCoord();
}
void LuaAdapter::detachElement()
{
mContainer->setChildren({});
if (mElement && mElement->mRoot)
mElement->mRoot->widget()->detachFromWidget();
mElement = nullptr;
}
}