ItemLabel optimization: textBlock is drawn using an offset relative to the position of the item instead of modifying textBlock.Rect each frame (causing the text position/wrapping to be recalculated each frame)

This commit is contained in:
Regalis
2016-04-02 15:54:00 +03:00
parent 5f05db7ca4
commit 2a2ba80f9c
2 changed files with 14 additions and 4 deletions

View File

@@ -231,6 +231,11 @@ namespace Barotrauma
}
public override void Draw(SpriteBatch spriteBatch)
{
Draw(spriteBatch, Vector2.Zero);
}
public void Draw(SpriteBatch spriteBatch, Vector2 offset)
{
if (!Visible) return;
@@ -238,6 +243,9 @@ namespace Barotrauma
if (state == ComponentState.Hover) currColor = hoverColor;
if (state == ComponentState.Selected) currColor = selectedColor;
Rectangle drawRect = rect;
if (offset != Vector2.Zero) drawRect.Location += offset.ToPoint();
if (currColor.A * currColor.A > 0.0f) GUI.DrawRectangle(spriteBatch, rect, currColor * (currColor.A / 255.0f), true);
base.Draw(spriteBatch);
@@ -248,7 +256,7 @@ namespace Barotrauma
{
spriteBatch.DrawString(Font,
text,
new Vector2(rect.X, rect.Y) + textPos,
new Vector2(rect.X, rect.Y) + textPos + offset,
textColor * (textColor.A / 255.0f),
0.0f, origin, 1.0f,
SpriteEffects.None, textDepth);
@@ -257,7 +265,6 @@ namespace Barotrauma
DrawChildren(spriteBatch);
if (OutlineColor.A * currColor.A > 0.0f) GUI.DrawRectangle(spriteBatch, rect, OutlineColor * (currColor.A / 255.0f), false);
}
}
}

View File

@@ -62,8 +62,11 @@ namespace Barotrauma.Items.Components
{
base.Draw(spriteBatch, editing);
textBlock.Rect = new Rectangle((int)item.DrawPosition.X - item.Rect.Width/2, -(int)(item.DrawPosition.Y + item.Rect.Height/2), item.Rect.Width, item.Rect.Height);
textBlock.Draw(spriteBatch);
var drawPos = new Vector2(
item.DrawPosition.X - item.Rect.Width/2.0f,
-(item.DrawPosition.Y + item.Rect.Height/2.0f));
textBlock.Draw(spriteBatch, drawPos - textBlock.Rect.Location.ToVector2());
}
}
}