Die Gui hat nun ein Chatfenster. Bisher können lediglich nachrichten

versandt, jedoch nicht empfangen werden.
This commit is contained in:
2014-05-01 19:06:58 +02:00
parent 7ffba85903
commit 8cada1d5ce
6 changed files with 167 additions and 21 deletions

View File

@@ -9,26 +9,27 @@
<OutputType>Exe</OutputType>
<RootNamespace>inf3</RootNamespace>
<AssemblyName>inf3</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<DebugSymbols>True</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<Optimize>False</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<ConsolePause>false</ConsolePause>
<ConsolePause>False</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<Optimize>True</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<ConsolePause>false</ConsolePause>
<ConsolePause>False</ConsolePause>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
@@ -75,6 +76,8 @@
<Compile Include="src\Gui\IPositionable.cs" />
<Compile Include="src\Buffer.cs" />
<Compile Include="src\Gui\IGui.cs" />
<Compile Include="src\Gui\ChatPanel.cs" />
<Compile Include="src\Gui\ChatInputBox.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>

View File

@@ -99,12 +99,14 @@ namespace WorldOfPeacecraft
return Map.GetTiles ();
}
public void sendCommand (string command)
public void SendCommand (string command)
{
SenderBuffer.AddLine (command);
}
public void sendChat (string message)
public void SendChatMessage (string message)
{
SenderBuffer.AddLine ("ask:say:" + message);
}
public void RefreshGui()

82
src/Gui/ChatInputBox.cs Normal file
View File

@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WorldOfPeacecraft
{
public class ChatInputBox : TextBox
{
private LinkedList<string> InputHistory = new LinkedList<string>();
private LinkedListNode<string> CurrentHistoryEntry;
public delegate void ChatMessageSubmittedEventHandler (string message);
public event ChatMessageSubmittedEventHandler ChatMessageSubmitted;
public ChatInputBox ()
{
this.KeyDown += KeyDownEvent;
this.KeyPress += KeyPressEvent;
}
private void KeyPressEvent (object o, KeyPressEventArgs args)
{
// Supress the "ding"-sound
if (args.KeyChar == (char)Keys.Return) {
args.Handled = true;
}
}
private void KeyDownEvent (object o, KeyEventArgs args)
{
if (args.KeyCode == Keys.Return) {
if (this.Text != "")
{
string message = this.Text;
this.Text = "";
if (CurrentHistoryEntry == null) {
InputHistory.AddLast (message);
}
else {
CurrentHistoryEntry.Value = message;
CurrentHistoryEntry = null;
}
if (ChatMessageSubmitted != null) {
this.ChatMessageSubmitted (message);
}
}
args.Handled = true;
} else if (args.KeyCode == Keys.Up) {
if (InputHistory.Count > 0) {
if (CurrentHistoryEntry == null) {
CurrentHistoryEntry = InputHistory.Last;
if (this.Text != "")
{
InputHistory.AddLast(this.Text);
}
this.Text = CurrentHistoryEntry.Value;
args.Handled = true;
} else {
if (CurrentHistoryEntry != InputHistory.First) {
CurrentHistoryEntry.Value = this.Text;
CurrentHistoryEntry = CurrentHistoryEntry.Previous;
this.Text = CurrentHistoryEntry.Value;
args.Handled = true;
}
}
}
} else if (args.KeyCode == Keys.Down) {
if (CurrentHistoryEntry != null)
{
CurrentHistoryEntry.Value = this.Text;
CurrentHistoryEntry = CurrentHistoryEntry.Next;
if (CurrentHistoryEntry == null) {
this.Text = "";
} else {
this.Text = CurrentHistoryEntry.Value;
}
}
}
}
}
}

46
src/Gui/ChatPanel.cs Normal file
View File

@@ -0,0 +1,46 @@
using System.Drawing;
using System.Windows.Forms;
namespace WorldOfPeacecraft
{
public class ChatPanel : Panel
{
private ChatInputBox ChatInput;
private TextBox ChatOutput;
private IBackend Backend;
public ChatPanel (IBackend backend)
{
Backend = backend;
ChatInput = new ChatInputBox ();
ChatInput.ChatMessageSubmitted += OnChatMessageSent;
ChatOutput = new TextBox ();
ChatOutput.ReadOnly = true;
ChatOutput.Multiline = true;
this.Controls.Add (ChatInput);
this.Controls.Add (ChatOutput);
}
protected override void OnLayout (LayoutEventArgs levent)
{
int outputHeight = this.ClientSize.Height - ChatInput.Height;
ChatOutput.Location = new Point (0, 0);
ChatOutput.Size = new Size (this.ClientSize.Width, outputHeight);
ChatInput.Location = new Point (0, outputHeight);
ChatInput.Size = new Size (this.ClientSize.Width, ChatInput.ClientSize.Width);
base.OnLayout (levent);
}
private void OnChatMessageSent (string message)
{
if (message.StartsWith ("/")) {
string command = message.Substring (1);
Backend.SendCommand(command);
} else {
Backend.SendChatMessage(message);
}
// TODO Move focus to board?
}
}
}

View File

@@ -8,11 +8,13 @@ namespace WorldOfPeacecraft
{
class Gui : Form, IGui
{
private const int tileSize = 32;
private const int entitySize = 10;
private const int TileSize = 32;
private const int EntitySize = 10;
private const int ChatWidth = 300;
private IBackend Backend;
private Panel Board = new Panel();
private ChatPanel ChatPanel;
public Gui ()
{
@@ -33,18 +35,23 @@ namespace WorldOfPeacecraft
public void InitializeComponents ()
{
ChatPanel = new ChatPanel (Backend);
this.SuspendLayout();
this.Size = new Size(400, 400);
this.Size = new Size(400 + ChatWidth, 400);
Board.Location = new Point(0,0);
Board.Size = new Size(400, 400);
Board.Paint += DoPaint;
ChatPanel.Location = new Point (400, 0);
ChatPanel.Size = new Size (300, 400);
this.DoubleBuffered = true;
this.MaximizeBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "WorldOfPeacecraft";
this.ShowIcon = false;
this.Controls.Add(Board);
this.Controls.Add (Board);
this.Controls.Add (ChatPanel);
this.ResumeLayout();
}
@@ -82,8 +89,8 @@ namespace WorldOfPeacecraft
public void PaintTile (Graphics g, ITile tile, int x, int y)
{
int posx = x * tileSize;
int posy = y * tileSize;
int posx = x * TileSize;
int posy = y * TileSize;
Color color;
if (tile.IsForest ()) {
color = Color.Green;
@@ -99,7 +106,7 @@ namespace WorldOfPeacecraft
} else {
color = Color.Black;
}
g.FillRectangle(new SolidBrush(color), posx, posy, tileSize, tileSize);
g.FillRectangle(new SolidBrush(color), posx, posy, TileSize, TileSize);
}
public void PaintEntities (Graphics g)
@@ -116,10 +123,10 @@ namespace WorldOfPeacecraft
public void PaintEntity (Graphics g, IPositionable entity)
{
int x = entity.GetX () * tileSize + tileSize / 2 - entitySize / 2;
int y = entity.GetY () * tileSize + tileSize / 2 - entitySize / 2;
g.FillRectangle (new SolidBrush (Color.Red), x, y, entitySize, entitySize);
g.DrawRectangle (new Pen( new SolidBrush (Color.Black)), x, y, entitySize, entitySize);
int x = entity.GetX () * TileSize + TileSize / 2 - EntitySize / 2;
int y = entity.GetY () * TileSize + TileSize / 2 - EntitySize / 2;
g.FillRectangle (new SolidBrush (Color.Red), x, y, EntitySize, EntitySize);
g.DrawRectangle (new Pen( new SolidBrush (Color.Black)), x, y, EntitySize, EntitySize);
}
public void PerformRefresh ()
@@ -130,11 +137,13 @@ namespace WorldOfPeacecraft
if (map == null) {
return;
}
int mapWidth = (map.GetLength(0)) * tileSize;
int mapHeight = (map.GetLength(1)) * tileSize;
int mapWidth = (map.GetLength(0)) * TileSize;
int mapHeight = (map.GetLength(1)) * TileSize;
this.SuspendLayout();
this.SetClientSizeCore(mapWidth, mapHeight);
this.SetClientSizeCore(mapWidth + ChatWidth, mapHeight);
Board.Size = new Size(mapWidth, mapHeight);
ChatPanel.Location = new Point (mapWidth, 0);
ChatPanel.Size = new Size (ChatWidth, mapHeight);
this.ResumeLayout();
this.PerformLayout();
this.Refresh();

View File

@@ -10,6 +10,10 @@ namespace WorldOfPeacecraft
IEnumerable<IPositionable> GetDragons();
void SendChatMessage (string message);
void SendCommand (string command);
void StartThreads();
void Stop();