Skip to content

Commit

Permalink
fixed recently created bug with paragraphs background size (undocumen…
Browse files Browse the repository at this point in the history
…ted issue)
  • Loading branch information
RonenNess committed Jul 6, 2018
1 parent 0944ecd commit 8d0d572
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions GeonBit.UI/Source/Entities/Paragraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public Paragraph(string text, Anchor anchor = Anchor.Auto, Vector2? size = null,
Text = text;
UpdateStyle(DefaultStyle);
if (scale != null) { SetStyleProperty(StylePropertyIds.Scale, new StyleProperty((float)scale)); }
UpdateFontPropertiesIfNeeded();
}


Expand All @@ -180,6 +181,7 @@ public Paragraph(string text, Anchor anchor, Color color, float? scale = null, V
{
SetStyleProperty(StylePropertyIds.FillColor, new StyleProperty(color));
if (scale != null) { SetStyleProperty(StylePropertyIds.Scale, new StyleProperty((float)scale)); }
UpdateFontPropertiesIfNeeded();
}

/// <summary>
Expand Down Expand Up @@ -365,11 +367,10 @@ override public void UpdateDestinationRects()
}

/// <summary>
/// Calculate the paragraph actual destination rect with word-wrap and other factors taken into consideration.
/// Update font-related properties, if needed.
/// </summary>
public void CalcTextActualRectWithWrap()
private void UpdateFontPropertiesIfNeeded()
{
// get font
SpriteFont font = GetCurrFont();
if (font != _currFont)
{
Expand All @@ -384,6 +385,15 @@ public void CalcTextActualRectWithWrap()
if ((SingleCharacterSize.X * 2) != _currFont.MeasureString("!.").X)
throw new Exceptions.InvalidValueException("Cannot use non-monospace fonts!");
}
}

/// <summary>
/// Calculate the paragraph actual destination rect with word-wrap and other factors taken into consideration.
/// </summary>
public void CalcTextActualRectWithWrap()
{
// update font properties
UpdateFontPropertiesIfNeeded();

// calc actual scale
float actualScale = Scale * BaseSize * GlobalScale;
Expand All @@ -397,7 +407,7 @@ public void CalcTextActualRectWithWrap()
string newProcessedText = Text;
if (WrapWords)
{
newProcessedText = WrapText(font, newProcessedText, _destRect.Width, _actualScale);
newProcessedText = WrapText(_currFont, newProcessedText, _destRect.Width, _actualScale);
}

// if processed text changed
Expand All @@ -412,7 +422,7 @@ public void CalcTextActualRectWithWrap()
// so we just update _size every frame and the text alignemtn (left, right, center..) fix itself by the destination rect.
_fontOrigin = Vector2.Zero;
_position = new Vector2(_destRect.X, _destRect.Y);
Vector2 size = font.MeasureString(_processedText);
Vector2 size = _currFont.MeasureString(_processedText);

// set position and origin based on anchor.
// note: no top-left here because thats the default set above.
Expand Down

0 comments on commit 8d0d572

Please sign in to comment.