Text in ScrollableContainer now mostly stays within the initial area.

This commit is contained in:
Leon Styhre 2021-10-14 21:59:09 +02:00
parent fe5e3ad5d4
commit 10e284c929
2 changed files with 12 additions and 0 deletions

View file

@ -18,6 +18,7 @@ ScrollableContainer::ScrollableContainer(Window* window)
: GuiComponent{window}
, mScrollPos{0.0f, 0.0f}
, mScrollDir{0.0f, 0.0f}
, mClipSpacing{0.0f}
, mAutoScrollDelay{0}
, mAutoScrollSpeed{0}
, mAutoScrollAccumulator{0}
@ -81,6 +82,11 @@ void ScrollableContainer::update(int deltaTime)
float lineSpacing{mChildren.front()->getLineSpacing()};
float combinedHeight{mChildren.front()->getFont()->getHeight(lineSpacing)};
// Calculate the line spacing which will be used to clip the container.
if (mClipSpacing == 0.0f)
mClipSpacing =
std::round((combinedHeight - mChildren.front()->getFont()->getLetterHeight()) / 2.0f);
// Resize container to font height boundary to avoid rendering a fraction of the last line.
if (!mUpdatedSize && contentSize.y > mSize.y) {
float numLines{mSize.y / combinedHeight};
@ -173,6 +179,11 @@ void ScrollableContainer::render(const glm::mat4& parentTrans)
glm::ivec2 clipDim{static_cast<int>(dimScaled.x - trans[3].x),
static_cast<int>(dimScaled.y - trans[3].y)};
// By effectively clipping the upper and lower boundaries of the container we mostly avoid
// scrolling outside the vertical starting and ending positions.
clipPos.y += mClipSpacing;
clipDim.y -= mClipSpacing * 0.9f;
Renderer::pushClipRect(clipPos, clipDim);
trans = glm::translate(trans, -glm::vec3{mScrollPos.x, mScrollPos.y, 0.0f});

View file

@ -44,6 +44,7 @@ private:
float mAutoScrollDelayConstant;
float mAutoScrollSpeedConstant;
float mResolutionModifier;
float mClipSpacing;
int mAutoScrollDelay;
int mAutoScrollSpeed;