chat
This commit is contained in:
43
OpenRa.Game/Chat.cs
Normal file
43
OpenRa.Game/Chat.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using IjwFramework.Types;
|
||||
|
||||
namespace OpenRa.Game
|
||||
{
|
||||
class Chat
|
||||
{
|
||||
const int logLength = 10;
|
||||
|
||||
public List<Pair<string, string>> recentLines = new List<Pair<string, string>>();
|
||||
public string typing = "";
|
||||
public bool isChatting = false;
|
||||
|
||||
public void Toggle()
|
||||
{
|
||||
if (isChatting && typing.Length > 0)
|
||||
Game.controller.AddOrder(Order.Chat(Game.LocalPlayer, typing));
|
||||
|
||||
typing = "";
|
||||
isChatting ^= true;
|
||||
}
|
||||
|
||||
public void TypeChar(char c)
|
||||
{
|
||||
if (c == '\b')
|
||||
{
|
||||
if (typing.Length > 0)
|
||||
typing = typing.Remove(typing.Length - 1);
|
||||
}
|
||||
else
|
||||
typing += c;
|
||||
}
|
||||
|
||||
public void AddLine(Pair<string, string> line)
|
||||
{
|
||||
recentLines.Add(line);
|
||||
while (recentLines.Count > logLength) recentLines.RemoveAt(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,6 +125,24 @@ namespace OpenRa.Game
|
||||
|
||||
chromeRenderer.Flush();
|
||||
DrawBuildPalette(currentTab);
|
||||
|
||||
var chatpos = new int2( 400, Game.viewport.Height - 20 );
|
||||
|
||||
if (Game.chat.isChatting)
|
||||
RenderChatLine(Pair.New("Chat:", Game.chat.typing), chatpos);
|
||||
|
||||
foreach (var line in Game.chat.recentLines.AsEnumerable().Reverse())
|
||||
{
|
||||
chatpos.Y -= 20;
|
||||
RenderChatLine(line, chatpos);
|
||||
}
|
||||
}
|
||||
|
||||
void RenderChatLine(Pair<string, string> line, int2 p)
|
||||
{
|
||||
var size = renderer.MeasureText(line.First);
|
||||
renderer.DrawText(line.First, p, Color.Red);
|
||||
renderer.DrawText(line.Second, p + new int2(size.X + 10, 0), Color.White);
|
||||
}
|
||||
|
||||
string currentTab = "Building";
|
||||
|
||||
@@ -157,6 +157,8 @@ namespace OpenRa.Game
|
||||
static int oreTicks = oreFrequency;
|
||||
public static int RenderFrame = 0;
|
||||
|
||||
public static Chat chat = new Chat();
|
||||
|
||||
public static void Tick()
|
||||
{
|
||||
int t = Environment.TickCount;
|
||||
|
||||
@@ -128,6 +128,16 @@ namespace OpenRa.Game
|
||||
Location = new int2(e.Location)
|
||||
});
|
||||
}
|
||||
|
||||
protected override void OnKeyPress(KeyPressEventArgs e)
|
||||
{
|
||||
base.OnKeyPress(e);
|
||||
|
||||
if (e.KeyChar == '\r')
|
||||
Game.chat.Toggle();
|
||||
else if (Game.chat.isChatting)
|
||||
Game.chat.TypeChar(e.KeyChar);
|
||||
}
|
||||
}
|
||||
|
||||
struct MouseInput
|
||||
|
||||
@@ -75,6 +75,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Chat.cs" />
|
||||
<Compile Include="Chrome.cs" />
|
||||
<Compile Include="GameRules\GeneralInfo.cs" />
|
||||
<Compile Include="GameRules\TechTree.cs" />
|
||||
|
||||
@@ -87,6 +87,11 @@ namespace OpenRa.Game
|
||||
return Game.world.Actors.Where(x => x.ActorID == aID).First();
|
||||
}
|
||||
|
||||
public static Order Chat(Player subject, string text)
|
||||
{
|
||||
return new Order(subject, "Chat", null, null, int2.Zero, text, null);
|
||||
}
|
||||
|
||||
public static Order Attack(Actor subject, Actor target)
|
||||
{
|
||||
return new Order(subject.Owner, "Attack", subject, target, int2.Zero, null, Cursor.Attack);
|
||||
|
||||
@@ -155,7 +155,7 @@ namespace OpenRa.Game
|
||||
this.socket.NoDelay = true;
|
||||
var reader = new BinaryReader( socket.GetStream() );
|
||||
|
||||
var nextFrameId = System.BitConverter.GetBytes( nextLocalOrderFrame );
|
||||
var nextFrameId = BitConverter.GetBytes( nextLocalOrderFrame );
|
||||
socket.GetStream().Write( nextFrameId, 0, nextFrameId.Length );
|
||||
|
||||
new Thread( () =>
|
||||
@@ -208,14 +208,14 @@ namespace OpenRa.Game
|
||||
|
||||
var ms = new MemoryStream();
|
||||
|
||||
ms.Write( System.BitConverter.GetBytes( nextLocalOrderFrame ) );
|
||||
ms.Write( BitConverter.GetBytes( nextLocalOrderFrame ) );
|
||||
|
||||
foreach( var order in localOrders )
|
||||
ms.Write( order.Serialize() );
|
||||
|
||||
++nextLocalOrderFrame;
|
||||
|
||||
socket.GetStream().Write( System.BitConverter.GetBytes( (int)ms.Length ) );
|
||||
socket.GetStream().Write( BitConverter.GetBytes( (int)ms.Length ) );
|
||||
ms.WriteTo( socket.GetStream() );
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRa.Game.GameRules;
|
||||
using OpenRa.Game.Traits;
|
||||
using IjwFramework.Types;
|
||||
|
||||
namespace OpenRa.Game
|
||||
{
|
||||
@@ -143,6 +144,11 @@ namespace OpenRa.Game
|
||||
pt.rallyPoint = order.TargetLocation;
|
||||
break;
|
||||
}
|
||||
case "Chat":
|
||||
{
|
||||
Game.chat.AddLine(Pair.New(order.Player.PlayerName + ":", order.TargetString));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user