Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize RichTextBox rendering with long text #2451

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions Source/Engine/UI/GUI/Common/RichTextBoxBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,27 @@ public override void DrawSelf()
var selection = new TextRange(SelectionLeft, SelectionRight);
var viewRect = new Rectangle(_viewOffset, Size).MakeExpanded(10.0f);
var firstTextBlock = textBlocksCount;
for (int i = 0; i < textBlocksCount; i++)
if (textBlocksCount > 0)
{
ref TextBlock textBlock = ref textBlocks[i];
if (textBlock.Bounds.Intersects(ref viewRect))
// Try to estimate the rough location of the first line
float lineHeight = textBlocks[0].Bounds.Height;
firstTextBlock = Math.Clamp((int)Math.Floor(viewRect.Y / lineHeight) + 1, 0, textBlocksCount - 1);
if (textBlocks[firstTextBlock].Bounds.Top > viewRect.Top)
{
firstTextBlock = i;
break;
// Overshoot...
for (; firstTextBlock > 0; firstTextBlock--)
{
ref TextBlock textBlock = ref textBlocks[firstTextBlock];
if (textBlocks[firstTextBlock].Bounds.Top < viewRect.Top)
break;
}
}

for (; firstTextBlock < textBlocksCount; firstTextBlock++)
{
ref TextBlock textBlock = ref textBlocks[firstTextBlock];
if (textBlock.Bounds.Intersects(ref viewRect))
break;
}
}
var endTextBlock = Mathf.Min(firstTextBlock + 1, textBlocksCount);
Expand Down Expand Up @@ -309,21 +323,24 @@ public override void DrawSelf()
if (!font)
continue;

TextRange textBlockRange = new TextRange(0, textBlock.Range.EndIndex - textBlock.Range.StartIndex);
string textBlockText = _text.Substring(textBlock.Range.StartIndex, textBlockRange.Length);

// Shadow
Color color;
if (!textBlock.Style.ShadowOffset.IsZero && textBlock.Style.ShadowColor != Color.Transparent)
{
color = textBlock.Style.ShadowColor;
if (!enabled)
color *= 0.6f;
Render2D.DrawText(font, _text, ref textBlock.Range, color, textBlock.Bounds.Location + textBlock.Style.ShadowOffset, textBlock.Style.CustomMaterial);
Render2D.DrawText(font, textBlockText, ref textBlockRange, color, textBlock.Bounds.Location + textBlock.Style.ShadowOffset, textBlock.Style.CustomMaterial);
}

// Text
color = textBlock.Style.Color;
if (!enabled)
color *= 0.6f;
Render2D.DrawText(font, _text, ref textBlock.Range, color, textBlock.Bounds.Location, textBlock.Style.CustomMaterial);
Render2D.DrawText(font, textBlockText, ref textBlockRange, color, textBlock.Bounds.Location, textBlock.Style.CustomMaterial);
}

// Draw underline
Expand Down
Loading