Move chat display into a widget

This commit is contained in:
Paul Chote
2010-07-10 20:43:29 +12:00
parent fdf570398a
commit 50f3aecf53
10 changed files with 21 additions and 125 deletions

View File

@@ -1,71 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Collections.Generic;
using System.Drawing;
using OpenRA.FileFormats;
using OpenRA.Traits;
using System.Text;
namespace OpenRA
{
class Chat
{
const int logLength = 10;
public List<ChatLine> recentLines = new List<ChatLine>();
public void AddLine(Session.Client p, string text)
{
AddLine(Game.world.PlayerColors()[p.PaletteIndex].Color, p.Name, text);
}
public void AddLine(Color c, string from, string text)
{
var sizeOwner = Game.chrome.renderer.RegularFont.Measure(from);
var sizeText = Game.chrome.renderer.RegularFont.Measure(text);
if (sizeOwner.X + sizeText.X + 10 <= Chrome.ChatWidth)
recentLines.Add(new ChatLine { Color = c, Owner = from, Text = text });
else
{
StringBuilder sb = new StringBuilder();
foreach (var w in text.Split(' '))
{
if ( Game.chrome.renderer.RegularFont.Measure(sb.ToString() + ' ' + w).X > Chrome.ChatWidth )
{
recentLines.Add(new ChatLine { Color = c, Owner = from, Text = sb.ToString() } );
sb = new StringBuilder();
sb.Append(w);
}
else
sb.Append( ' ' + w);
}
if (sb.Length != 0)
recentLines.Add(new ChatLine { Color = c, Owner = from, Text = sb.ToString() } );
}
var eva = Rules.Info["world"].Traits.Get<EvaAlertsInfo>();
Sound.Play(eva.ChatBeep);
while (recentLines.Count > logLength) recentLines.RemoveAt(0);
}
}
class ChatLine { public Color Color = Color.White; public string Owner, Text; public bool wrapped = false; }
}

View File

@@ -61,6 +61,7 @@ namespace OpenRA
public static Widget rootWidget = null; public static Widget rootWidget = null;
public static Widget selectedWidget; public static Widget selectedWidget;
public static ChatDisplayWidget chatWidget;
public void Tick(World world) public void Tick(World world)
{ {
@@ -75,10 +76,6 @@ namespace OpenRA
{ {
buttons.Clear(); buttons.Clear();
renderer.Device.DisableScissor(); renderer.Device.DisableScissor();
var typingArea = new Rectangle(240, Game.viewport.Height - 30, Game.viewport.Width - 420, 30);
var chatLogArea = new Rectangle(240, Game.viewport.Height - 500, Game.viewport.Width - 420, 500 - 40);
DrawChat(typingArea, chatLogArea);
} }
void AddUiButton(int2 pos, string text, Action<bool> a) void AddUiButton(int2 pos, string text, Action<bool> a)
@@ -165,17 +162,6 @@ namespace OpenRA
currentMap = null; currentMap = null;
else else
currentMap = Game.AvailableMaps[ Game.LobbyInfo.GlobalSettings.Map ]; currentMap = Game.AvailableMaps[ Game.LobbyInfo.GlobalSettings.Map ];
var w = 800;
var h = 600;
var r = new Rectangle( (Game.viewport.Width - w) / 2, (Game.viewport.Height - h) / 2, w, h );
var typingBox = new Rectangle(r.Left + 20, r.Bottom - 77, r.Width - 40, 27);
var chatBox = new Rectangle(r.Left + 20, r.Bottom - 269, r.Width - 40, 190);
DrawDialogBackground(chatBox, "dialog3");
DrawChat(typingBox, chatBox);
} }
void AddButton(RectangleF r, Action<bool> b) { buttons.Add(Pair.New(r, b)); } void AddButton(RectangleF r, Action<bool> b) { buttons.Add(Pair.New(r, b)); }
@@ -185,30 +171,6 @@ namespace OpenRA
WidgetUtils.DrawPanel(collection, r); WidgetUtils.DrawPanel(collection, r);
} }
void DrawChat(Rectangle typingArea, Rectangle chatLogArea)
{
var chatpos = new int2(chatLogArea.X + 10, chatLogArea.Bottom - 6);
ChatWidth = chatLogArea.Width - 10;
renderer.Device.EnableScissor(chatLogArea.Left, chatLogArea.Top, chatLogArea.Width, chatLogArea.Height);
foreach (var line in Game.chat.recentLines.AsEnumerable().Reverse())
{
chatpos.Y -= 20;
RenderChatLine(line, chatpos);
}
rgbaRenderer.Flush();
renderer.Device.DisableScissor();
}
void RenderChatLine(ChatLine line, int2 p)
{
var size = renderer.RegularFont.Measure(line.Owner);
renderer.RegularFont.DrawText(line.Owner, p, line.Color);
renderer.RegularFont.DrawText(line.Text, p + new int2(size.X + 10, 0), Color.White);
}
public int ticksSinceLastMove = 0; public int ticksSinceLastMove = 0;
public int2 lastMousePos; public int2 lastMousePos;
public bool HandleInput(World world, MouseInput mi) public bool HandleInput(World world, MouseInput mi)

View File

@@ -201,9 +201,6 @@ namespace OpenRA
} }
internal static int RenderFrame = 0; internal static int RenderFrame = 0;
internal static Chat chat = new Chat();
internal static int LocalTick = 0; internal static int LocalTick = 0;
const int NetTickScale = 3; // 120ms net tick for 40ms local tick const int NetTickScale = 3; // 120ms net tick for 40ms local tick
@@ -618,7 +615,7 @@ namespace OpenRA
public static void Exit() { quit = true; } public static void Exit() { quit = true; }
public static void Debug(string s) { chat.AddLine(Color.White, "Debug", s); } public static void Debug(string s) { Chrome.chatWidget.AddLine(Color.White, "Debug", s); }
public static void Disconnect() public static void Disconnect()
{ {

View File

@@ -33,27 +33,29 @@ namespace OpenRA.Network
case "Chat": case "Chat":
{ {
var client = Game.LobbyInfo.Clients.FirstOrDefault(c => c.Index == clientId); var client = Game.LobbyInfo.Clients.FirstOrDefault(c => c.Index == clientId);
if (client != null) if (client != null && Chrome.chatWidget != null)
Game.chat.AddLine(client, order.TargetString); Chrome.chatWidget.AddLine(client, order.TargetString);
break; break;
} }
case "TeamChat": case "TeamChat":
{ {
var client = Game.LobbyInfo.Clients.FirstOrDefault(c => c.Index == clientId); var client = Game.LobbyInfo.Clients.FirstOrDefault(c => c.Index == clientId);
if (client != null) if (client != null && Chrome.chatWidget != null)
{ {
var player = Game.world.players.Values.FirstOrDefault(p => p.Index == client.Index); var player = Game.world.players.Values.FirstOrDefault(p => p.Index == client.Index);
var isAlly = player != null && Game.world.LocalPlayer != null var isAlly = player != null && Game.world.LocalPlayer != null
&& player.Stances[Game.world.LocalPlayer] == Stance.Ally; && player.Stances[Game.world.LocalPlayer] == Stance.Ally;
if (isAlly) if (isAlly)
Game.chat.AddLine(client, "(Team) " + order.TargetString); Chrome.chatWidget.AddLine(client, "(Team) " + order.TargetString);
} }
break; break;
} }
case "StartGame": case "StartGame":
{ {
Game.chat.AddLine(Color.White, "Server", "The game has started."); if (Chrome.chatWidget != null)
Chrome.chatWidget.AddLine(Color.White, "Server", "The game has started.");
Game.StartGame(); Game.StartGame();
break; break;
} }

View File

@@ -75,7 +75,6 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Chat.cs" />
<Compile Include="Chrome.cs" /> <Compile Include="Chrome.cs" />
<Compile Include="GameRules\WeaponInfo.cs" /> <Compile Include="GameRules\WeaponInfo.cs" />
<Compile Include="Group.cs" /> <Compile Include="Group.cs" />
@@ -226,6 +225,7 @@
<Compile Include="Traits\World\AircraftInfluence.cs" /> <Compile Include="Traits\World\AircraftInfluence.cs" />
<Compile Include="Traits\World\HazardLayer.cs" /> <Compile Include="Traits\World\HazardLayer.cs" />
<Compile Include="Widgets\TextFieldWidget.cs" /> <Compile Include="Widgets\TextFieldWidget.cs" />
<Compile Include="Widgets\ChatDisplayWidget.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj"> <ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -25,8 +25,6 @@ namespace OpenRA.Traits
{ {
// Sound effects // Sound effects
public readonly string TabClick = "ramenu1.aud"; public readonly string TabClick = "ramenu1.aud";
public readonly string ChatBeep = "rabeep1.aud";
public readonly string RadarUp = "radaron2.aud"; public readonly string RadarUp = "radaron2.aud";
public readonly string RadarDown = "radardn1.aud"; public readonly string RadarDown = "radardn1.aud";

View File

@@ -68,6 +68,7 @@ namespace OpenRA.Widgets.Delegates
}; };
Game.LobbyInfoChanged += UpdatePlayerList; Game.LobbyInfoChanged += UpdatePlayerList;
Chrome.chatWidget = lobby.GetWidget("CHAT_DISPLAY") as ChatDisplayWidget;
} }
void UpdatePlayerList() void UpdatePlayerList()

View File

@@ -102,7 +102,6 @@ namespace OpenRA.Widgets
TextBuffer += c; TextBuffer += c;
} }
int blinkCycle = 10; int blinkCycle = 10;
bool showCursor = true; bool showCursor = true;
public override void Tick(World world) public override void Tick(World world)
@@ -133,7 +132,7 @@ namespace OpenRA.Widgets
// Right align and scissor when the text overflows // Right align and scissor when the text overflows
if (textSize.X > Bounds.Width - 2*margin) if (textSize.X > Bounds.Width - 2*margin)
{ {
textPos += new int2(Bounds.Width - 2*margin - textSize.X,0); textPos += new int2(Bounds.Width - 2*margin - textSize.X,0);
Game.chrome.renderer.Device.EnableScissor(pos.X + margin, pos.Y, Bounds.Width - 2*margin, Bounds.Bottom); Game.chrome.renderer.Device.EnableScissor(pos.X + margin, pos.Y, Bounds.Width - 2*margin, Bounds.Bottom);
} }

View File

@@ -491,6 +491,15 @@ Container:
Width:120 Width:120
Height:25 Height:25
Text:Change Map Text:Change Map
ChatDisplay@CHAT_DISPLAY:
Id:CHAT_DISPLAY
Visible:true
X:20
Y:PARENT_BOTTOM - 269
Width:PARENT_RIGHT - 40
Height:190
Notification: beepy2.aud
#rabeep1.aud for ra
Label@LABEL_LOBBY_TEAM: Label@LABEL_LOBBY_TEAM:
Id:LABEL_LOBBY_TEAM Id:LABEL_LOBBY_TEAM
Width:70 Width:70

View File

@@ -205,7 +205,6 @@ World:
SpawnDefaultUnits: SpawnDefaultUnits:
EvaAlerts: EvaAlerts:
TabClick: button.aud TabClick: button.aud
ChatBeep: beepy2.aud
RadarUp: comcntr1.aud RadarUp: comcntr1.aud
RadarDown: powrdn1.aud RadarDown: powrdn1.aud
BuildPaletteOpen: appear1.aud BuildPaletteOpen: appear1.aud