TextBoxes align text to the right and hide any overflowing text at the left if the width of the box is exceeded

This commit is contained in:
Regalis
2017-04-30 18:14:25 +03:00
parent eddc33dc89
commit f92c2df9f7
7 changed files with 56 additions and 54 deletions
+9 -9
View File
@@ -26,8 +26,8 @@ namespace Barotrauma
public bool Wrap;
private bool overflowScrollActive;
public bool OverflowScroll;
private bool overflowClipActive;
public bool OverflowClip;
private float textDepth;
@@ -195,7 +195,7 @@ namespace Barotrauma
{
if (text == null) return;
overflowScrollActive = false;
overflowClipActive = false;
wrappedText = text;
@@ -206,18 +206,18 @@ namespace Barotrauma
wrappedText = ToolBox.WrapText(text, rect.Width - padding.X - padding.Z, Font, textScale);
size = MeasureText(wrappedText);
}
else if (OverflowScroll)
else if (OverflowClip)
{
overflowScrollActive = size.X > rect.Width;
overflowClipActive = size.X > rect.Width;
}
textPos = new Vector2(rect.Width / 2.0f, rect.Height / 2.0f);
origin = size * 0.5f;
if (textAlignment.HasFlag(Alignment.Left) && !overflowScrollActive)
if (textAlignment.HasFlag(Alignment.Left) && !overflowClipActive)
origin.X += (rect.Width / 2.0f - padding.X) - size.X / 2;
if (textAlignment.HasFlag(Alignment.Right) || overflowScrollActive)
if (textAlignment.HasFlag(Alignment.Right) || overflowClipActive)
origin.X -= (rect.Width / 2.0f - padding.Z) - size.X / 2;
if (textAlignment.HasFlag(Alignment.Top))
@@ -279,7 +279,7 @@ namespace Barotrauma
if (TextGetter != null) text = TextGetter();
if (overflowScrollActive) GameMain.CurrGraphicsDevice.ScissorRectangle = rect;
if (overflowClipActive) GameMain.CurrGraphicsDevice.ScissorRectangle = rect;
if (!string.IsNullOrEmpty(text))
{
@@ -291,7 +291,7 @@ namespace Barotrauma
SpriteEffects.None, textDepth);
}
if (overflowScrollActive) GameMain.CurrGraphicsDevice.ScissorRectangle = new Rectangle(0, 0, GameMain.GraphicsWidth, GameMain.GraphicsHeight);
if (overflowClipActive) GameMain.CurrGraphicsDevice.ScissorRectangle = new Rectangle(0, 0, GameMain.GraphicsWidth, GameMain.GraphicsHeight);
DrawChildren(spriteBatch);
+22 -20
View File
@@ -27,6 +27,8 @@ namespace Barotrauma
public OnTextChangedHandler OnTextChanged;
public bool CaretEnabled;
private int? maxTextWidth;
public GUITextBlock.TextGetterHandler TextGetter
{
@@ -40,12 +42,17 @@ namespace Barotrauma
set { textBlock.Wrap = value; }
}
public bool LimitText
public int? MaxTextWidth
{
get { return textBlock.LimitText; }
set { textBlock.LimitText = value; }
get { return maxTextWidth; }
set
{
textBlock.OverflowClip = value != null && (int)value > textBlock.Rect.Width - textBlock.Padding.X - textBlock.Padding.Z;
maxTextWidth = value;
}
}
public bool Enabled
{
get;
@@ -118,7 +125,7 @@ namespace Barotrauma
}
}
public String Text
public string Text
{
get
{
@@ -126,28 +133,22 @@ namespace Barotrauma
}
set
{
if (textBlock.Text == value) return;
textBlock.Text = value;
if (textBlock.Text == null) textBlock.Text = "";
if (textBlock.Text != "")
{
/*//if you attempt to display a Character that is not in your font
//you will get an exception, so we filter the characters
//remove the filtering if you're using a default Character in your spritefont
String filtered = "";
foreach (char c in value)
if (!Wrap)
{
if (Font.Characters.Contains(c)) filtered += c;
}
int maxWidth = MaxTextWidth == null ? (int)(textBlock.Rect.Width - textBlock.Padding.X - textBlock.Padding.Z) : (int)MaxTextWidth;
textBlock.Text = filtered;*/
if (!Wrap && Font.MeasureString(textBlock.Text).X > rect.Width)
{
//ensure that text cannot be larger than the box
Text = textBlock.Text.Substring(0, textBlock.Text.Length - 1);
if (Font.MeasureString(textBlock.Text).X > maxWidth)
{
Text = textBlock.Text.Substring(0, textBlock.Text.Length - 1);
}
}
}
}
}
@@ -178,8 +179,9 @@ namespace Barotrauma
if (parent != null)
parent.AddChild(this);
textBlock = new GUITextBlock(new Rectangle(0,0,0,0), "", color, textColor, textAlignment, style, this);
textBlock = new GUITextBlock(new Rectangle(0,0,0,0), "", color, textColor, textAlignment, style, this);
Font = GUI.Font;
GUI.Style.Apply(textBlock, style == "" ? "GUITextBox" : style);