Merge pull request #5191 from pavlos256/chat-window-improvements

Chat window improvements
This commit is contained in:
Paul Chote
2014-05-02 22:56:39 +12:00
5 changed files with 38 additions and 50 deletions

View File

@@ -149,7 +149,7 @@ namespace OpenRA
public static string JoinWith<T>(this IEnumerable<T> ts, string j) public static string JoinWith<T>(this IEnumerable<T> ts, string j)
{ {
return string.Join(j, ts.Select(t => t.ToString()).ToArray()); return string.Join(j, ts);
} }
public static IEnumerable<T> Append<T>(this IEnumerable<T> ts, params T[] moreTs) public static IEnumerable<T> Append<T>(this IEnumerable<T> ts, params T[] moreTs)

View File

@@ -22,9 +22,7 @@ namespace OpenRA.Widgets
public string Notification = ""; public string Notification = "";
const int logLength = 9; const int logLength = 9;
int ticksUntilRemove = 0; List<ChatLine> recentLines = new List<ChatLine>();
internal List<ChatLine> recentLines = new List<ChatLine>();
public override Rectangle EventBounds { get { return Rectangle.Empty; } } public override Rectangle EventBounds { get { return Rectangle.Empty; } }
@@ -69,8 +67,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, Game.LocalTick + RemoveTime, c));
ticksUntilRemove = RemoveTime;
if (Notification != null) if (Notification != null)
Sound.Play(Notification); Sound.Play(Notification);
@@ -85,27 +82,29 @@ namespace OpenRA.Widgets
recentLines.RemoveAt(0); recentLines.RemoveAt(0);
} }
public void ClearChat()
{
recentLines = new List<ChatLine>();
}
public override void Tick() public override void Tick()
{ {
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 && Game.LocalTick >= 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 int Expiration;
public ChatLine(string owner, string text, int expiration, Color color)
{
Owner = owner;
Text = text;
Expiration = expiration;
Color = color;
}
} }
} }

View File

@@ -151,7 +151,7 @@ namespace OpenRA.Widgets
public bool ScrolledToBottom public bool ScrolledToBottom
{ {
get { return ListOffset == Math.Min(0, Bounds.Height - ContentHeight); } get { return ListOffset == Math.Min(0, Bounds.Height - ContentHeight) || ContentHeight <= Bounds.Height; }
} }
public void ScrollToItem(string itemKey) public void ScrollToItem(string itemKey)

View File

@@ -170,52 +170,38 @@ namespace OpenRA.Widgets
var textSize = font.Measure(text); var textSize = font.Measure(text);
if (textSize.X > width) if (textSize.X > width)
{ {
var lines = text.Split('\n'); var lines = text.Split('\n').ToList();
var newLines = new List<string>();
var i = 0;
var line = lines[i++];
for(;;) for (var i=0; i<lines.Count; i++)
{ {
newLines.Add(line); var line = lines[i];
var m = font.Measure(line); var m = font.Measure(line);
var spaceIndex = 0;
var start = line.Length - 1;
if (m.X <= width) if (m.X <= width)
{
if (i < lines.Length - 1)
{
line = lines[i++];
continue; continue;
}
else var bestSpaceIndex = -1;
break; var start = line.Length - 1;
}
while (m.X > width) while (m.X > width)
{ {
if (-1 == (spaceIndex = line.LastIndexOf(' ', start))) var spaceIndex = line.LastIndexOf(' ', start);
if (spaceIndex == -1)
break; break;
bestSpaceIndex = spaceIndex;
start = spaceIndex - 1; start = spaceIndex - 1;
m = font.Measure(line.Substring(0, spaceIndex)); m = font.Measure(line.Substring(0, spaceIndex));
} }
if (spaceIndex != -1) if (bestSpaceIndex != -1)
{ {
newLines.RemoveAt(newLines.Count - 1); lines[i] = line.Substring(0, bestSpaceIndex);
newLines.Add(line.Substring(0, spaceIndex)); lines.Insert(i + 1, line.Substring(bestSpaceIndex + 1));
line = line.Substring(spaceIndex + 1);
} }
else if (i < lines.Length - 1)
{
line = lines[i++];
continue;
} }
else
break; return string.Join("\n", lines);
}
return newLines.JoinWith("\n");
} }
return text; return text;
} }

View File

@@ -146,8 +146,11 @@ namespace OpenRA.Mods.RA.Widgets.Logic
template.Bounds.Height += dh; template.Bounds.Height += dh;
} }
bool scrolledToBottom = chatScrollPanel.ScrolledToBottom;
chatScrollPanel.AddChild(template); chatScrollPanel.AddChild(template);
if (scrolledToBottom)
chatScrollPanel.ScrollToBottom(); chatScrollPanel.ScrollToBottom();
Sound.PlayNotification(null, "Sounds", "ChatLine", null); Sound.PlayNotification(null, "Sounds", "ChatLine", null);
} }
} }