Czoko_Smieciarka/Trunk/Interface/CzokoŚmieciarka.WPF/Models/Board.cs

87 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using CzokoŚmieciarka.DataModels.GeneralModels.Models;
using CzokoŚmieciarka.DataModels.Models;
using CzokoŚmieciarka.WPF.Interfaces;
namespace CzokoŚmieciarka.WPF.Models
{
public class Board
{
static int _rows = 9;
static int _columns = 9;
List<Tile> _tiles = new List<Tile>();
public Board(List<AObject> objects, GarbageCollectorWPF garbageCollector)
{
foreach (var item in objects)
{
Tile tile = new Tile()
{
Data = item.Data,
Object = item
};
_tiles.Add(tile);
}
_tiles[garbageCollector.Location.X + garbageCollector.Location.Y].Object.Image = MergedBitmaps(
new Bitmap(_tiles[garbageCollector.Location.X + garbageCollector.Location.Y].Object.ImagePath),
new Bitmap(garbageCollector.ImagePath));
}
public void BoardRefresh(List<AObject> objects, GarbageCollectorWPF garbageCollector)
{
foreach (var item in objects)
{
_tiles[item.Location.X + item.Location.Y].Object = item;
_tiles[item.Location.X + item.Location.Y].Object.RefreshImage();
}
_tiles[garbageCollector.Location.X + garbageCollector.Location.Y].Object.Image = MergedBitmaps(
new Bitmap(_tiles[garbageCollector.Location.X + garbageCollector.Location.Y].Object.ImagePath),
new Bitmap(garbageCollector.ImagePath));
}
public int Rows
{
get { return _rows; }
set { _rows = value; }
}
public int Columns
{
get { return _columns; }
set { _columns = value; }
}
public List<Tile> Tiles
{
get { return _tiles; }
set { _tiles = value; }
}
private ImageBrush MergedBitmaps(Bitmap bmp1, Bitmap bmp2)
{
using (Graphics g = Graphics.FromImage(bmp1))
{
g.DrawImage(bmp2, new Point(0, 0));
}
MemoryStream ms = new MemoryStream();
((System.Drawing.Bitmap)bmp1).Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
BitmapImage image = new BitmapImage();
image.BeginInit();
ms.Seek(0, SeekOrigin.Begin);
image.StreamSource = ms;
image.EndInit();
ImageBrush result = new ImageBrush(image);
return result;
}
}
}