Skip to content

Commit

Permalink
Percent size code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Oen44 committed Nov 5, 2024
1 parent 22314af commit cf94a1d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 48 deletions.
13 changes: 1 addition & 12 deletions src/framework/ui/uianchorlayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,8 @@ bool UIAnchorLayout::updateWidget(const UIWidgetPtr& widget, const UIAnchorGroup
first = widget;

if (widget->isSizePercantage()) {
auto percentSize = widget->getPercentSize();
int width = widget->getWidth();
int height = widget->getHeight();
Rect paddingRect = parentWidget->getPaddingRect();
if (percentSize.width() > 0) {
width = static_cast<int>(paddingRect.width() * (percentSize.width() / 100.0));
}
if (percentSize.height() > 0) {
height = static_cast<int>(paddingRect.height() * (percentSize.height() / 100.0));
}

widget->setWidth(width);
widget->setHeight(height);
widget->updatePercentSize(paddingRect.size());
}

Rect newRect = widget->getRect();
Expand Down
13 changes: 1 addition & 12 deletions src/framework/ui/uiflexbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,7 @@ bool UIFlexBox::internalUpdate()
continue;

if (widget->isSizePercantage()) {
auto percentSize = widget->getPercentSize();
int width = widget->getWidth();
int height = widget->getHeight();
if (percentSize.width() > 0) {
width = static_cast<int>(clippingRect.width() * (percentSize.width() / 100.0));
}
if (percentSize.height() > 0) {
height = static_cast<int>(clippingRect.height() * (percentSize.height() / 100.0));
}

widget->setWidth(width);
widget->setHeight(height);
widget->updatePercentSize(clippingRect.size());
}

Size childSize = widget->getSize();
Expand Down
13 changes: 1 addition & 12 deletions src/framework/ui/uihorizontallayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,7 @@ bool UIHorizontalLayout::internalUpdate()
continue;

if (widget->isSizePercantage()) {
auto percentSize = widget->getPercentSize();
int width = widget->getWidth();
int height = widget->getHeight();
if (percentSize.width() > 0) {
width = static_cast<int>(paddingRect.width() * (percentSize.width() / 100.0));
}
if (percentSize.height() > 0) {
height = static_cast<int>(paddingRect.height() * (percentSize.height() / 100.0));
}

widget->setWidth(width);
widget->setHeight(height);
widget->updatePercentSize(paddingRect.size());
}

Size size = widget->getSize();
Expand Down
13 changes: 1 addition & 12 deletions src/framework/ui/uiverticallayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,7 @@ bool UIVerticalLayout::internalUpdate()
continue;

if (widget->isSizePercantage()) {
auto percentSize = widget->getPercentSize();
int width = widget->getWidth();
int height = widget->getHeight();
if (percentSize.width() > 0) {
width = static_cast<int>(paddingRect.width() * (percentSize.width() / 100.0));
}
if (percentSize.height() > 0) {
height = static_cast<int>(paddingRect.height() * (percentSize.height() / 100.0));
}

widget->setWidth(width);
widget->setHeight(height);
widget->updatePercentSize(paddingRect.size());
}

Size size = widget->getSize();
Expand Down
17 changes: 17 additions & 0 deletions src/framework/ui/uiwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2120,3 +2120,20 @@ void UIWidget::setHeight(int height, bool percentage)

resize(getWidth(), height);
}

void UIWidget::updatePercentSize(const Size& size)
{
auto percentSize = getPercentSize();
int width = getWidth();
int height = getHeight();

if (percentSize.width() > 0) {
width = static_cast<int>(size.width() * (percentSize.width() / 100.0));
}
if (percentSize.height() > 0) {
height = static_cast<int>(size.height() * (percentSize.height() / 100.0));
}

setWidth(width);
setHeight(height);
}
1 change: 1 addition & 0 deletions src/framework/ui/uiwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ class UIWidget : public LuaObject
void setRotation(float degrees) { m_rotation = degrees; }
void setChangeCursorImage(bool enable) { m_changeCursorImage = enable; }
void setCursor(const std::string& cursor);
void updatePercentSize(const Size& size);

int getX() { return m_rect.x(); }
int getY() { return m_rect.y(); }
Expand Down

0 comments on commit cf94a1d

Please sign in to comment.