Add widget state support.

This commit is contained in:
jmsgrogan 2023-01-18 13:29:31 +00:00
parent 19091a0e80
commit 8536908eab
19 changed files with 385 additions and 46 deletions

View file

@ -64,8 +64,6 @@ TransformNode* Widget::getRootNode() const
return mRootNode.get();
}
void Widget::setBackground(Theme::Sys::Color token)
{
if (mBackground != token)
@ -75,6 +73,42 @@ void Widget::setBackground(Theme::Sys::Color token)
}
}
void Widget::setBackgroundOpacity(float opacity)
{
if (mBackgroundOpacity != opacity)
{
mBackgroundOpacity = opacity;
mMaterialDirty = true;
}
}
void Widget::setOutlineThickness(double thickness)
{
if (mBorderThickness != thickness)
{
mBorderThickness = thickness;
mMaterialDirty = true;
}
}
void Widget::setOutline(Theme::Sys::Color token)
{
if (mBorder != token)
{
mBorder = token;
mMaterialDirty = true;
}
}
void Widget::setRadius(double radius)
{
if (mRadius != radius)
{
mRadius = radius;
mGeometryDirty = true;
}
}
void Widget::setVisible(bool visible)
{
if (mVisible != visible)
@ -92,7 +126,7 @@ void Widget::setVisible(bool visible)
bool Widget::isDirty() const
{
return mTransformDirty || mMaterialDirty || mVisibilityDirty || mPendingChildNodes.size() > 0;
return mStateDirty || mGeometryDirty || mTransformDirty || mMaterialDirty || mVisibilityDirty || mPendingChildNodes.size() > 0;
}
bool Widget::needsUpdate() const
@ -111,6 +145,11 @@ bool Widget::needsUpdate() const
return false;
}
void Widget::updateState()
{
}
void Widget::doPaint(const PaintEvent* event)
{
updateBackground(event);
@ -127,16 +166,19 @@ void Widget::onPaintEvent(const PaintEvent* event)
{
mRootNode->setName(mName + "_RootNode");
if (mStateDirty)
{
updateState();
mStateDirty = false;
}
doPaint(event);
if (mVisibilityDirty)
{
mRootNode->setIsVisible(mVisible);
}
mTransformDirty = false;
mMaterialDirty = false;
mVisibilityDirty = false;
mVisibilityDirty = false;
}
}
for (auto node : mPendingChildNodes)
@ -212,9 +254,96 @@ bool Widget::onMouseEvent(const MouseEvent* event)
return true;
}
bool Widget::onFocusInEvent(const FocusEvent* event)
{
bool inChild = false;
for (const auto& child : mChildren)
{
if (child->onFocusInEvent(event))
{
inChild = true;
break;
}
}
if (inChild)
{
return true;
}
else if (mAcceptsFocus)
{
setFocus(true);
return true;
}
return false;
}
void Widget::setFocus(bool hasFocus)
{
if (mHasFocus != hasFocus)
{
mHasFocus = hasFocus;
mStateDirty = true;
}
}
void Widget::onMyMouseEvent(const MouseEvent* event)
{
MLOG_INFO("Widget mouse event");
}
void Widget::createOrUpdateGeometry()
{
if (!mBackgroundNode)
{
unsigned locX = mLocation.getX() + mMargin.mLeft;
unsigned locY = mLocation.getY() + mMargin.mTop;
unsigned deltaX = mSize.mWidth - mMargin.mLeft - mMargin.mRight;
unsigned deltaY = mSize.mHeight - mMargin.mTop - mMargin.mBottom;
mBackgroundNode = std::make_unique<RectangleNode>(DiscretePoint(locX, locY), deltaX, deltaY);
mBackgroundNode->setName(mName + "_BackgroundNode");
mRootNode->addChild(mBackgroundNode.get());
}
else
{
mBackgroundNode->setRadius(mRadius);
}
}
void Widget::updateTransform()
{
unsigned locX = mLocation.getX() + mMargin.mLeft;
unsigned locY = mLocation.getY() + mMargin.mTop;
unsigned deltaX = mSize.mWidth - mMargin.mLeft - mMargin.mRight;
unsigned deltaY = mSize.mHeight - mMargin.mTop - mMargin.mBottom;
mBackgroundNode->setWidth(deltaX);
mBackgroundNode->setHeight(deltaY);
mBackgroundNode->setLocation(DiscretePoint(locX, locY));
}
void Widget::updateMaterial(const PaintEvent* event)
{
if (mBackground != Theme::Sys::Color::None)
{
auto background_color = event->getThemesManager()->getColor(mBackground);
background_color.setAlpha(mBackgroundOpacity);
mBackgroundNode->setFillColor(background_color);
}
else
{
mBackgroundNode->setHasFillColor(false);
}
if (mBorder != Theme::Sys::Color::None)
{
mBackgroundNode->setStrokeColor(event->getThemesManager()->getColor(mBorder));
mBackgroundNode->setStrokeThickness(mBorderThickness);
}
else
{
mBackgroundNode->setHasStrokeColor(false);
}
}
void Widget::updateBackground(const PaintEvent* event)
@ -233,16 +362,13 @@ void Widget::updateBackground(const PaintEvent* event)
if (mTransformDirty)
{
mBackgroundNode->setWidth(deltaX);
mBackgroundNode->setHeight(deltaY);
mBackgroundNode->setLocation(DiscretePoint(locX, locY));
updateTransform();
mTransformDirty = false;
}
if (mMaterialDirty)
{
mBackgroundNode->setFillColor(event->getThemesManager()->getColor(mBackground));
mBackgroundNode->setStrokeColor(event->getThemesManager()->getColor(mBorder));
mBackgroundNode->setStrokeThickness(mBorderThickness);
updateMaterial(event);
}
if (mVisibilityDirty)