Fixed item GUIMessages overlapping with each other

This commit is contained in:
Joonas Rikkonen
2017-12-19 18:11:47 +02:00
parent b7b747d674
commit d03dd40336
2 changed files with 18 additions and 9 deletions
@@ -546,6 +546,9 @@ namespace Barotrauma
} }
} }
/// <summary>
/// Displays a message at the center of the screen, automatically preventing overlapping with other centered messages
/// </summary>
public static void AddMessage(string message, Color color, float lifeTime = 3.0f, bool playSound = true) public static void AddMessage(string message, Color color, float lifeTime = 3.0f, bool playSound = true)
{ {
if (messages.Count > 0 && messages[messages.Count - 1].Text == message) if (messages.Count > 0 && messages[messages.Count - 1].Text == message)
@@ -555,12 +558,15 @@ namespace Barotrauma
} }
Vector2 pos = new Vector2(GameMain.GraphicsWidth / 2.0f, GameMain.GraphicsHeight * 0.7f); Vector2 pos = new Vector2(GameMain.GraphicsWidth / 2.0f, GameMain.GraphicsHeight * 0.7f);
pos.Y += messages.FindAll(m => m.Centered).Count * 30; pos.Y += messages.FindAll(m => m.AutoCenter).Count * 30;
messages.Add(new GUIMessage(message, color, pos, lifeTime, Alignment.Center, true)); messages.Add(new GUIMessage(message, color, pos, lifeTime, Alignment.Center, true));
if (playSound) PlayUISound(GUISoundType.Message); if (playSound) PlayUISound(GUISoundType.Message);
} }
/// <summary>
/// Display and automatically fade out a piece of text at an arbitrary position on the screen
/// </summary>
public static void AddMessage(string message, Vector2 position, Alignment alignment, Color color, float lifeTime = 3.0f, bool playSound = true) public static void AddMessage(string message, Vector2 position, Alignment alignment, Color color, float lifeTime = 3.0f, bool playSound = true)
{ {
if (messages.Count > 0 && messages[messages.Count - 1].Text == message) if (messages.Count > 0 && messages[messages.Count - 1].Text == message)
@@ -599,7 +605,7 @@ namespace Barotrauma
alpha -= 1.0f - msg.LifeTime; alpha -= 1.0f - msg.LifeTime;
} }
if (msg.Centered) if (msg.AutoCenter)
{ {
msg.Pos = MathUtils.SmoothStep(msg.Pos, currPos, deltaTime * 20.0f); msg.Pos = MathUtils.SmoothStep(msg.Pos, currPos, deltaTime * 20.0f);
currPos.Y += 30.0f; currPos.Y += 30.0f;
@@ -46,14 +46,21 @@ namespace Barotrauma
private set; private set;
} }
public bool Centered;
public GUIMessage(string text, Color color, Vector2 position, float lifeTime, Alignment textAlignment, bool centered) /// <summary>
/// Autocentered messages are automatically placed at the center of the screen and prevented from overlapping with each other
/// </summary>
public bool AutoCenter;
public GUIMessage(string text, Color color, Vector2 position, float lifeTime, Alignment textAlignment, bool autoCenter)
{ {
coloredText = new ColoredText(text, color); coloredText = new ColoredText(text, color);
pos = position; pos = position;
this.lifeTime = lifeTime; this.lifeTime = lifeTime;
this.Alignment = textAlignment; this.Alignment = textAlignment;
this.AutoCenter = autoCenter;
size = GUI.Font.MeasureString(text);
if (textAlignment.HasFlag(Alignment.Left)) if (textAlignment.HasFlag(Alignment.Left))
Origin.X += size.X * 0.5f; Origin.X += size.X * 0.5f;
@@ -67,14 +74,10 @@ namespace Barotrauma
if (textAlignment.HasFlag(Alignment.Bottom)) if (textAlignment.HasFlag(Alignment.Bottom))
Origin.Y -= size.Y * 0.5f; Origin.Y -= size.Y * 0.5f;
if (centered) if (autoCenter)
{ {
Origin = new Vector2((int)(0.5f * size.X), (int)(0.5f * size.Y)); Origin = new Vector2((int)(0.5f * size.X), (int)(0.5f * size.Y));
} }
else
{
size = GUI.Font.MeasureString(text);
}
} }
} }
} }