mirror of
https://github.com/luanti-org/luanti.git
synced 2025-10-13 08:35:20 +02:00
scrollcontainer: Add automatic scrollbar calculation (#14623)
New parameter 'content padding'. When specified, the scrollbar max value is calculated automatically. This aims to reduce manual calculation functions.
This commit is contained in:
@@ -67,6 +67,50 @@ void GUIScrollContainer::draw()
|
||||
}
|
||||
}
|
||||
|
||||
void GUIScrollContainer::setScrollBar(GUIScrollBar *scrollbar)
|
||||
{
|
||||
m_scrollbar = scrollbar;
|
||||
|
||||
if (m_scrollbar && m_content_padding_px.has_value() && m_scrollfactor != 0.0f) {
|
||||
// Set the scrollbar max value based on the content size.
|
||||
|
||||
// Get content size based on elements
|
||||
core::rect<s32> size;
|
||||
for (gui::IGUIElement *e : Children) {
|
||||
core::rect<s32> abs_rect = e->getAbsolutePosition();
|
||||
size.addInternalPoint(abs_rect.LowerRightCorner);
|
||||
}
|
||||
|
||||
s32 visible_content_px = (
|
||||
m_orientation == VERTICAL
|
||||
? AbsoluteClippingRect.getHeight()
|
||||
: AbsoluteClippingRect.getWidth()
|
||||
);
|
||||
|
||||
s32 total_content_px = *m_content_padding_px + (
|
||||
m_orientation == VERTICAL
|
||||
? (size.LowerRightCorner.Y - AbsoluteClippingRect.UpperLeftCorner.Y)
|
||||
: (size.LowerRightCorner.X - AbsoluteClippingRect.UpperLeftCorner.X)
|
||||
);
|
||||
|
||||
s32 hidden_content_px = std::max<s32>(0, total_content_px - visible_content_px);
|
||||
m_scrollbar->setMin(0);
|
||||
m_scrollbar->setMax(std::ceil(hidden_content_px / std::fabs(m_scrollfactor)));
|
||||
|
||||
// Note: generally, the scrollbar has the same size as the scroll container.
|
||||
// However, in case it isn't, proportional adjustments are needed.
|
||||
s32 scrollbar_px = (
|
||||
m_scrollbar->isHorizontal()
|
||||
? m_scrollbar->getRelativePosition().getWidth()
|
||||
: m_scrollbar->getRelativePosition().getHeight()
|
||||
);
|
||||
|
||||
m_scrollbar->setPageSize((total_content_px * scrollbar_px) / visible_content_px);
|
||||
}
|
||||
|
||||
updateScrolling();
|
||||
}
|
||||
|
||||
void GUIScrollContainer::updateScrolling()
|
||||
{
|
||||
s32 pos = m_scrollbar->getPos();
|
||||
|
Reference in New Issue
Block a user