Add Container widget type, use it to make Adapter code less hacky

This commit is contained in:
uramer 2022-01-29 23:40:31 +01:00
parent 086a7d9bc5
commit f07f05ddd3
9 changed files with 107 additions and 35 deletions

View file

@ -14,10 +14,11 @@ namespace LuaUi
, mAbsoluteCoord()
, mRelativeCoord()
, mAnchor()
, mLua{ nullptr }
, mWidget{ nullptr }
, mLua(nullptr)
, mWidget(nullptr)
, mSlot(this)
, mLayout{ sol::nil }
, mParent(nullptr)
, mLayout(sol::nil)
{}
void WidgetExtension::initialize(lua_State* lua, MyGUI::Widget* self)
@ -64,6 +65,8 @@ namespace LuaUi
mWidget->eventKeySetFocus.clear();
mWidget->eventKeyLostFocus.clear();
mOnSizeChange.reset();
for (WidgetExtension* w : mChildren)
w->deinitialize();
for (WidgetExtension* w : mTemplateChildren)
@ -72,6 +75,7 @@ namespace LuaUi
void WidgetExtension::attach(WidgetExtension* ext)
{
ext->mParent = this;
ext->widget()->attachToWidget(mSlot->widget());
ext->updateCoord();
}
@ -150,6 +154,7 @@ namespace LuaUi
mChildren[i] = children[i];
attach(mChildren[i]);
}
updateChildren();
}
void WidgetExtension::setTemplateChildren(const std::vector<WidgetExtension*>& children)
@ -212,6 +217,8 @@ namespace LuaUi
if (mOnSizeChange.has_value())
mOnSizeChange.value()(newCoord.size());
}
if (oldCoord != newCoord && mOnCoordChange.has_value())
mOnCoordChange.value()(this, newCoord);
}
void WidgetExtension::setProperties(sol::object props)
@ -240,23 +247,31 @@ namespace LuaUi
w->updateCoord();
}
MyGUI::IntSize WidgetExtension::parentSize()
{
if (mParent)
return mParent->childScalingSize();
else
return widget()->getParentSize();
}
MyGUI::IntSize WidgetExtension::calculateSize()
{
const MyGUI::IntSize& parentSize = mWidget->getParentSize();
MyGUI::IntSize pSize = parentSize();
MyGUI::IntSize newSize;
newSize = mAbsoluteCoord.size() + mForcedCoord.size();
newSize.width += mRelativeCoord.width * parentSize.width;
newSize.height += mRelativeCoord.height * parentSize.height;
newSize.width += mRelativeCoord.width * pSize.width;
newSize.height += mRelativeCoord.height * pSize.height;
return newSize;
}
MyGUI::IntPoint WidgetExtension::calculatePosition(const MyGUI::IntSize& size)
{
const MyGUI::IntSize& parentSize = mWidget->getParentSize();
MyGUI::IntSize pSize = parentSize();
MyGUI::IntPoint newPosition;
newPosition = mAbsoluteCoord.point() + mForcedCoord.point();
newPosition.left += mRelativeCoord.left * parentSize.width - mAnchor.width * size.width;
newPosition.top += mRelativeCoord.top * parentSize.height - mAnchor.height * size.height;
newPosition.left += mRelativeCoord.left * pSize.width - mAnchor.width * size.width;
newPosition.top += mRelativeCoord.top * pSize.height - mAnchor.height * size.height;
return newPosition;
}
@ -268,6 +283,11 @@ namespace LuaUi
return newCoord;
}
MyGUI::IntSize WidgetExtension::childScalingSize()
{
return widget()->getSize();
}
void WidgetExtension::triggerEvent(std::string_view name, const sol::object& argument = sol::nil) const
{
auto it = mCallbacks.find(name);