Fix chat line expiration

The chat display overlay would remove one chat line every X ticks.
It will now keep track of the time each chat line has to be removed
and act accordingly.

For example, if 3 chat lines are added with 1 second difference
from each other, they will be removed one after the other, with the
same 1 second difference.
This commit is contained in:
Pavlos Touboulidis
2014-04-25 17:34:00 +03:00
parent a7c77d4155
commit fa5c8d083e

View File

@@ -22,7 +22,7 @@ namespace OpenRA.Widgets
public string Notification = ""; public string Notification = "";
const int logLength = 9; const int logLength = 9;
int ticksUntilRemove = 0; uint totalTicks;
internal List<ChatLine> recentLines = new List<ChatLine>(); internal List<ChatLine> recentLines = new List<ChatLine>();
@@ -69,8 +69,7 @@ namespace OpenRA.Widgets
public void AddLine(Color c, string from, string text) public void AddLine(Color c, string from, string text)
{ {
recentLines.Add(new ChatLine { Color = c, Owner = from, Text = text }); recentLines.Add(new ChatLine(from, text, totalTicks + (uint)RemoveTime, c));
ticksUntilRemove = RemoveTime;
if (Notification != null) if (Notification != null)
Sound.Play(Notification); Sound.Play(Notification);
@@ -92,20 +91,29 @@ namespace OpenRA.Widgets
public override void Tick() public override void Tick()
{ {
totalTicks++;
if (RemoveTime == 0) if (RemoveTime == 0)
return; return;
if (--ticksUntilRemove > 0) // This takes advantage of the fact that recentLines is ordered by expiration, from sooner to later
return; while (recentLines.Count > 0 && totalTicks >= recentLines [0].Expiration)
recentLines.RemoveAt(0);
ticksUntilRemove = RemoveTime;
RemoveLine();
} }
} }
class ChatLine class ChatLine
{ {
public Color Color = Color.White; public readonly Color Color;
public string Owner, Text; public readonly string Owner, Text;
public readonly uint Expiration;
public ChatLine(string owner, string text, uint expiration, Color color)
{
this.Owner = owner;
this.Text = text;
this.Expiration = expiration;
this.Color = color;
}
} }
} }