Erste Gui Version. Es wird lediglich die Map gezeichnet. Das Backend ist angeschlossen

This commit is contained in:
2014-04-30 17:28:58 +02:00
parent 7a8df2e147
commit ba4eb0d2d9
19 changed files with 363 additions and 513 deletions

107
src/Gui/Gui.cs Normal file
View File

@@ -0,0 +1,107 @@
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WorldOfPeacecraft
{
class Gui : Form, IGui
{
private const int tileSize = 16;
private const int entitySize = 10;
private IBackend Backend;
private Panel Board = new Panel();
public Gui ()
{
AllocConsole();
InitializeComponents();
}
public void SetBackend (IBackend backend)
{
Backend = backend;
}
public void InitializeComponents ()
{
this.SuspendLayout();
this.Size = new Size(400, 400);
Board.Location = new Point(0,0);
Board.Size = new Size(400, 400);
Board.Paint += DoPaint;
this.DoubleBuffered = true;
this.MaximizeBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "WorldOfPeacecraft";
this.ShowIcon = false;
this.Controls.Add(Board);
this.ResumeLayout();
this.PerformLayout();
}
public void DoPaint (object source, PaintEventArgs args)
{
BufferedGraphics buffer = BufferedGraphicsManager.Current.Allocate (Board.CreateGraphics (), Board.DisplayRectangle);
Graphics g = buffer.Graphics;
PaintMap(g);
buffer.Render();
}
public void PaintMap (Graphics g)
{
ITile[,] Map = Backend.GetMap ();
if (Map != null) {
for (int y = 0; y < Map.GetLength(1); y++) {
for (int x = 0; x < Map.GetLength(0); x++) {
PaintTile (g, Map [x, y], x, y);
}
}
}
else {
g.FillRectangle(new SolidBrush(Color.White), Board.DisplayRectangle);
}
}
public void PaintTile (Graphics g, ITile tile, int x, int y)
{
int posx = x * tileSize;
int posy = y * tileSize;
Color color;
if (tile.IsForest ()) {
color = Color.Green;
} // Stupid parenthesis
else if (tile.IsHuntable ()) {
color = Color.BurlyWood;
} else if (tile.IsWalkable ()) {
color = Color.Yellow;
} else if (tile.IsWall ()) {
color = Color.DarkGray;
} else if (tile.IsWater ()) {
color = Color.Blue;
} else {
color = Color.Black;
}
g.FillRectangle(new SolidBrush(color), posx, posy, tileSize, tileSize);
}
public void PerformRefresh ()
{
ITile[,] map = Backend.GetMap();
int mapWidth = (map.GetLength(0)) * tileSize;
int mapHeight = (map.GetLength(1)) * tileSize;
this.SuspendLayout();
this.ClientSize = new Size(mapWidth, mapHeight);
Board.Size = new Size(mapWidth, mapHeight);
this.ResumeLayout();
this.PerformLayout();
this.Refresh();
}
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
}
}

8
src/Gui/IBackend.cs Normal file
View File

@@ -0,0 +1,8 @@
namespace WorldOfPeacecraft
{
public interface IBackend
{
ITile[,] GetMap();
}
}

8
src/Gui/IGui.cs Normal file
View File

@@ -0,0 +1,8 @@
namespace WorldOfPeacecraft
{
public interface IGui
{
void PerformRefresh();
void SetBackend(IBackend backend);
}
}

26
src/Gui/IPositionable.cs Normal file
View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorldOfPeacecraft
{
/// <summary>
/// Interface your positionable objects should implement. Such as the dragons and players.
/// This enables the frontend to determine the current position of said objects to render them at the correct space.
/// </summary>
public interface IPositionable
{
/// <summary>
/// Getter for the x-position
/// </summary>
/// <returns>the x-position</returns>
int GetX();
/// <summary>
/// Getter for the y-position
/// </summary>
/// <returns>the y-position</returns>
int GetY();
}
}

17
src/Gui/ITile.cs Normal file
View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorldOfPeacecraft
{
public interface ITile : IPositionable
{
bool IsWalkable();
bool IsWall();
bool IsForest();
bool IsHuntable();
bool IsWater();
}
}

20
src/Gui/Program.cs Normal file
View File

@@ -0,0 +1,20 @@
using System;
using System.Windows.Forms;
namespace WorldOfPeacecraft
{
public class Program
{
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Gui gui = new Gui();
Backend backend = new Backend(gui);
gui.SetBackend(backend);
Application.Run (gui);
}
}
}