Files
OpenRA/OpenRA.Game/Widgets/ChatDisplayWidget.cs
Pavlos Touboulidis c099e6d09b Fix overlay chat lines expiration
It wasn't working right when the widget was hidden because it
wasn't receiving any Ticks. Instead of counting, we're now using
Game.LocalTick as the tick source.
2014-04-26 03:18:46 +03:00

110 lines
2.7 KiB
C#

#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace OpenRA.Widgets
{
public class ChatDisplayWidget : Widget
{
public readonly int RemoveTime = 0;
public readonly bool UseContrast = false;
public string Notification = "";
const int logLength = 9;
List<ChatLine> recentLines = new List<ChatLine>();
public override Rectangle EventBounds { get { return Rectangle.Empty; } }
public override void Draw()
{
var pos = RenderOrigin;
var chatLogArea = new Rectangle(pos.X, pos.Y, Bounds.Width, Bounds.Height);
var chatpos = new int2(chatLogArea.X + 5, chatLogArea.Bottom - 5);
var font = Game.Renderer.Fonts["Regular"];
Game.Renderer.EnableScissor(chatLogArea);
foreach (var line in recentLines.AsEnumerable().Reverse())
{
var inset = 0;
string owner = null;
if (!string.IsNullOrEmpty(line.Owner))
{
owner = line.Owner + ":";
inset = font.Measure(owner).X + 5;
}
var text = WidgetUtils.WrapText(line.Text, chatLogArea.Width - inset - 6, font);
chatpos.Y -= Math.Max(15, font.Measure(text).Y) + 5;
if (chatpos.Y < pos.Y)
break;
if (owner != null)
{
font.DrawTextWithContrast(owner, chatpos,
line.Color, Color.Black, UseContrast ? 1 : 0);
}
font.DrawTextWithContrast(text, chatpos + new int2(inset, 0),
Color.White, Color.Black, UseContrast ? 1 : 0);
}
Game.Renderer.DisableScissor();
}
public void AddLine(Color c, string from, string text)
{
recentLines.Add(new ChatLine(from, text, Game.LocalTick + RemoveTime, c));
if (Notification != null)
Sound.Play(Notification);
while (recentLines.Count > logLength)
recentLines.RemoveAt(0);
}
public void RemoveLine()
{
if (recentLines.Count > 0)
recentLines.RemoveAt(0);
}
public override void Tick()
{
if (RemoveTime == 0)
return;
// This takes advantage of the fact that recentLines is ordered by expiration, from sooner to later
while (recentLines.Count > 0 && Game.LocalTick >= recentLines[0].Expiration)
recentLines.RemoveAt(0);
}
}
class ChatLine
{
public readonly Color Color;
public readonly string Owner, Text;
public readonly int Expiration;
public ChatLine(string owner, string text, int expiration, Color color)
{
Owner = owner;
Text = text;
Expiration = expiration;
Color = color;
}
}
}