From 2a2ba80f9c2ec02a85ddf02aa63e90ca03cc08f0 Mon Sep 17 00:00:00 2001 From: Regalis Date: Sat, 2 Apr 2016 15:54:00 +0300 Subject: [PATCH] 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) --- Subsurface/Source/GUI/GUITextBlock.cs | 11 +++++++++-- Subsurface/Source/Items/Components/ItemLabel.cs | 7 +++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Subsurface/Source/GUI/GUITextBlock.cs b/Subsurface/Source/GUI/GUITextBlock.cs index c04912ad3..4b4dda5dc 100644 --- a/Subsurface/Source/GUI/GUITextBlock.cs +++ b/Subsurface/Source/GUI/GUITextBlock.cs @@ -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); - } } } diff --git a/Subsurface/Source/Items/Components/ItemLabel.cs b/Subsurface/Source/Items/Components/ItemLabel.cs index c9e51fe14..7837ad5c0 100644 --- a/Subsurface/Source/Items/Components/ItemLabel.cs +++ b/Subsurface/Source/Items/Components/ItemLabel.cs @@ -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()); } } } \ No newline at end of file