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; public bool Wrap;
private bool overflowScrollActive; private bool overflowClipActive;
public bool OverflowScroll; public bool OverflowClip;
private float textDepth; private float textDepth;
@@ -195,7 +195,7 @@ namespace Barotrauma
{ {
if (text == null) return; if (text == null) return;
overflowScrollActive = false; overflowClipActive = false;
wrappedText = text; wrappedText = text;
@@ -206,18 +206,18 @@ namespace Barotrauma
wrappedText = ToolBox.WrapText(text, rect.Width - padding.X - padding.Z, Font, textScale); wrappedText = ToolBox.WrapText(text, rect.Width - padding.X - padding.Z, Font, textScale);
size = MeasureText(wrappedText); 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); textPos = new Vector2(rect.Width / 2.0f, rect.Height / 2.0f);
origin = size * 0.5f; 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; 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; origin.X -= (rect.Width / 2.0f - padding.Z) - size.X / 2;
if (textAlignment.HasFlag(Alignment.Top)) if (textAlignment.HasFlag(Alignment.Top))
@@ -279,7 +279,7 @@ namespace Barotrauma
if (TextGetter != null) text = TextGetter(); if (TextGetter != null) text = TextGetter();
if (overflowScrollActive) GameMain.CurrGraphicsDevice.ScissorRectangle = rect; if (overflowClipActive) GameMain.CurrGraphicsDevice.ScissorRectangle = rect;
if (!string.IsNullOrEmpty(text)) if (!string.IsNullOrEmpty(text))
{ {
@@ -291,7 +291,7 @@ namespace Barotrauma
SpriteEffects.None, textDepth); 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); DrawChildren(spriteBatch);
+21 -19
View File
@@ -28,6 +28,8 @@ namespace Barotrauma
public bool CaretEnabled; public bool CaretEnabled;
private int? maxTextWidth;
public GUITextBlock.TextGetterHandler TextGetter public GUITextBlock.TextGetterHandler TextGetter
{ {
get { return textBlock.TextGetter; } get { return textBlock.TextGetter; }
@@ -40,10 +42,15 @@ namespace Barotrauma
set { textBlock.Wrap = value; } set { textBlock.Wrap = value; }
} }
public bool LimitText public int? MaxTextWidth
{ {
get { return textBlock.LimitText; } get { return maxTextWidth; }
set { textBlock.LimitText = value; } set
{
textBlock.OverflowClip = value != null && (int)value > textBlock.Rect.Width - textBlock.Padding.X - textBlock.Padding.Z;
maxTextWidth = value;
}
} }
public bool Enabled public bool Enabled
@@ -118,7 +125,7 @@ namespace Barotrauma
} }
} }
public String Text public string Text
{ {
get get
{ {
@@ -126,28 +133,22 @@ namespace Barotrauma
} }
set set
{ {
if (textBlock.Text == value) return;
textBlock.Text = value; textBlock.Text = value;
if (textBlock.Text == null) textBlock.Text = ""; if (textBlock.Text == null) textBlock.Text = "";
if (textBlock.Text != "") if (textBlock.Text != "")
{ {
/*//if you attempt to display a Character that is not in your font if (!Wrap)
//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 (Font.Characters.Contains(c)) filtered += c; int maxWidth = MaxTextWidth == null ? (int)(textBlock.Rect.Width - textBlock.Padding.X - textBlock.Padding.Z) : (int)MaxTextWidth;
if (Font.MeasureString(textBlock.Text).X > maxWidth)
{
Text = textBlock.Text.Substring(0, textBlock.Text.Length - 1);
}
} }
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);
}
} }
} }
} }
@@ -178,6 +179,7 @@ namespace Barotrauma
if (parent != null) if (parent != null)
parent.AddChild(this); 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; Font = GUI.Font;
+9 -6
View File
@@ -1,10 +1,6 @@
using Barotrauma.Items.Components; using Lidgren.Network;
using Barotrauma.Networking.ReliableMessages;
using Lidgren.Network;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
namespace Barotrauma.Networking namespace Barotrauma.Networking
@@ -19,7 +15,14 @@ namespace Barotrauma.Networking
{ {
public const float SpeakRange = 2000.0f; public const float SpeakRange = 2000.0f;
public static Color[] MessageColor = { Color.White, Color.Red, new Color(63, 72, 204), Color.LightGreen, Color.Yellow }; public static Color[] MessageColor =
{
new Color(125, 140, 153),
new Color(204, 74, 78),
new Color(63, 72, 204),
new Color(157, 225, 160),
new Color(228, 199, 27)
};
public readonly string Text; public readonly string Text;
@@ -147,6 +147,7 @@ namespace Barotrauma.Networking
new Rectangle(chatBox.Rect.X, chatBox.Rect.Y + chatBox.Rect.Height + 20, chatBox.Rect.Width, 25), new Rectangle(chatBox.Rect.X, chatBox.Rect.Y + chatBox.Rect.Height + 20, chatBox.Rect.Width, 25),
Color.White * 0.5f, Color.Black, Alignment.TopLeft, Alignment.Left, "", inGameHUD); Color.White * 0.5f, Color.Black, Alignment.TopLeft, Alignment.Left, "", inGameHUD);
chatMsgBox.Font = GUI.SmallFont; chatMsgBox.Font = GUI.SmallFont;
chatMsgBox.MaxTextWidth = chatBox.Rect.Width * 2;
chatMsgBox.Padding = Vector4.Zero; chatMsgBox.Padding = Vector4.Zero;
chatMsgBox.OnEnterPressed = EnterChatMessage; chatMsgBox.OnEnterPressed = EnterChatMessage;
chatMsgBox.OnTextChanged = TypingChatMessage; chatMsgBox.OnTextChanged = TypingChatMessage;
@@ -309,10 +310,9 @@ namespace Barotrauma.Networking
displayedText = message.SenderName + ": " + displayedText; displayedText = message.SenderName + ": " + displayedText;
} }
GUITextBlock msg = new GUITextBlock(new Rectangle(0, 0, 0, 20), displayedText, GUITextBlock msg = new GUITextBlock(new Rectangle(0, 0, chatBox.Rect.Width - 40, 0), displayedText,
((chatBox.CountChildren % 2) == 0) ? Color.Transparent : Color.Black * 0.1f, message.Color, ((chatBox.CountChildren % 2) == 0) ? Color.Transparent : Color.Black * 0.1f, message.Color,
Alignment.Left, null, null, true); Alignment.Left, Alignment.TopLeft, "", null, true, GUI.SmallFont);
msg.Font = GUI.SmallFont;
msg.UserData = message.SenderName; msg.UserData = message.SenderName;
msg.Padding = new Vector4(20.0f, 0, 0, 0); msg.Padding = new Vector4(20.0f, 0, 0, 0);
+6 -6
View File
@@ -59,21 +59,21 @@ namespace Barotrauma.Networking
{ {
LogFrame = new GUIFrame(new Rectangle(0, 0, GameMain.GraphicsWidth, GameMain.GraphicsHeight), Color.Black * 0.5f); LogFrame = new GUIFrame(new Rectangle(0, 0, GameMain.GraphicsWidth, GameMain.GraphicsHeight), Color.Black * 0.5f);
GUIFrame innerFrame = new GUIFrame(new Rectangle(0,0,400, 400), null, Alignment.Center, "", LogFrame); GUIFrame innerFrame = new GUIFrame(new Rectangle(0, 0, 500, 400), null, Alignment.Center, "", LogFrame);
innerFrame.Padding = new Vector4(10.0f, 10.0f, 10.0f, 10.0f); innerFrame.Padding = new Vector4(10.0f, 20.0f, 10.0f, 20.0f);
new GUITextBlock(new Rectangle(-200,0,100,15), "Filter", "", Alignment.TopRight, Alignment.TopRight, innerFrame, false, GUI.SmallFont); new GUITextBlock(new Rectangle(-200, 0, 100, 15), "Filter", "", Alignment.TopRight, Alignment.TopRight, innerFrame, false, GUI.SmallFont);
GUITextBox searchBox = new GUITextBox(new Rectangle(-20,0,180,15), Alignment.TopRight, "", innerFrame); GUITextBox searchBox = new GUITextBox(new Rectangle(-20, 0, 180, 15), Alignment.TopRight, "", innerFrame);
searchBox.Font = GUI.SmallFont; searchBox.Font = GUI.SmallFont;
searchBox.OnTextChanged = FilterMessages; searchBox.OnTextChanged = FilterMessages;
GUIComponent.KeyboardDispatcher.Subscriber = searchBox; GUIComponent.KeyboardDispatcher.Subscriber = searchBox;
var clearButton = new GUIButton(new Rectangle(0,0,15,15), "x", Alignment.TopRight, "", innerFrame); var clearButton = new GUIButton(new Rectangle(0, 0, 15, 15), "x", Alignment.TopRight, "", innerFrame);
clearButton.OnClicked = ClearFilter; clearButton.OnClicked = ClearFilter;
clearButton.UserData = searchBox; clearButton.UserData = searchBox;
listBox = new GUIListBox(new Rectangle(0,20,0,335), "", innerFrame); listBox = new GUIListBox(new Rectangle(0, 30, 0, 310), "", innerFrame);
var currLines = lines.ToList(); var currLines = lines.ToList();
+2 -2
View File
@@ -193,10 +193,10 @@ namespace Barotrauma
var catButton = new GUIButton(new Rectangle(0, y, 0, 20), category.ToString(), Alignment.Left, "", leftPanel); var catButton = new GUIButton(new Rectangle(0, y, 0, 20), category.ToString(), Alignment.Left, "", leftPanel);
catButton.UserData = i; catButton.UserData = i;
catButton.OnClicked = SelectTab; catButton.OnClicked = SelectTab;
y+=25; y += 25;
GUItabs[i] = new GUIFrame(new Rectangle(GameMain.GraphicsWidth / 2 - width / 2, GameMain.GraphicsHeight / 2 - height / 2, width, height), ""); GUItabs[i] = new GUIFrame(new Rectangle(GameMain.GraphicsWidth / 2 - width / 2, GameMain.GraphicsHeight / 2 - height / 2, width, height), "");
GUItabs[i].Padding = new Vector4(10.0f, 30.0f, 10.0f, 10.0f); GUItabs[i].Padding = new Vector4(10.0f, 30.0f, 10.0f, 20.0f);
new GUITextBlock(new Rectangle(-200, 0, 100, 15), "Filter", "", Alignment.TopRight, Alignment.CenterRight, GUItabs[i], false, GUI.SmallFont); new GUITextBlock(new Rectangle(-200, 0, 100, 15), "Filter", "", Alignment.TopRight, Alignment.CenterRight, GUItabs[i], false, GUI.SmallFont);
+4 -7
View File
@@ -3,9 +3,6 @@ using Lidgren.Network;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Barotrauma.Networking; using Barotrauma.Networking;
using FarseerPhysics;
using FarseerPhysics.Factories;
using FarseerPhysics.Dynamics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
@@ -188,6 +185,7 @@ namespace Barotrauma
chatBox = new GUIListBox(new Rectangle(0,0,0,chatFrame.Rect.Height-80), Color.White, "", chatFrame); chatBox = new GUIListBox(new Rectangle(0,0,0,chatFrame.Rect.Height-80), Color.White, "", chatFrame);
textBox = new GUITextBox(new Rectangle(0, 25, 0, 25), Alignment.Bottom, "", chatFrame); textBox = new GUITextBox(new Rectangle(0, 25, 0, 25), Alignment.Bottom, "", chatFrame);
textBox.MaxTextWidth = textBox.Rect.Width * 2;
textBox.Font = GUI.SmallFont; textBox.Font = GUI.SmallFont;
//player info panel ------------------------------------------------------------ //player info panel ------------------------------------------------------------
@@ -1031,16 +1029,15 @@ namespace Barotrauma
{ {
float prevSize = chatBox.BarSize; float prevSize = chatBox.BarSize;
while (chatBox.CountChildren>20) while (chatBox.CountChildren > 20)
{ {
chatBox.RemoveChild(chatBox.children[1]); chatBox.RemoveChild(chatBox.children[1]);
} }
GUITextBlock msg = new GUITextBlock(new Rectangle(0, 0, 0, 20), GUITextBlock msg = new GUITextBlock(new Rectangle(0, 0, chatBox.Rect.Width-20, 0),
message.TextWithSender, message.TextWithSender,
((chatBox.CountChildren % 2) == 0) ? Color.Transparent : Color.Black*0.1f, message.Color, ((chatBox.CountChildren % 2) == 0) ? Color.Transparent : Color.Black*0.1f, message.Color,
Alignment.Left, "", null, true); Alignment.Left, Alignment.TopLeft, "", null, true, GUI.SmallFont);
msg.Font = GUI.SmallFont;
msg.UserData = message.SenderName; msg.UserData = message.SenderName;
msg.CanBeFocused = false; msg.CanBeFocused = false;