Die Gui hat nun ein Chatfenster. Bisher können lediglich nachrichten
versandt, jedoch nicht empfangen werden.
This commit is contained in:
82
src/Gui/ChatInputBox.cs
Normal file
82
src/Gui/ChatInputBox.cs
Normal 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
46
src/Gui/ChatPanel.cs
Normal 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?
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -10,6 +10,10 @@ namespace WorldOfPeacecraft
|
||||
|
||||
IEnumerable<IPositionable> GetDragons();
|
||||
|
||||
void SendChatMessage (string message);
|
||||
|
||||
void SendCommand (string command);
|
||||
|
||||
void StartThreads();
|
||||
|
||||
void Stop();
|
||||
|
||||
Reference in New Issue
Block a user