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)
{
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)

View File

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

View File

@@ -151,7 +151,7 @@ namespace OpenRA.Widgets
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)

View File

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

View File

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