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 _tiles = new List(); private GarbageCollectorWPF garbageCollectorWPF = new GarbageCollectorWPF(_columns, new Coords(0,0), AppDomain.CurrentDomain.BaseDirectory + @"..\..\Images\garbageCollector.png"); public Board(List objects, GarbageCollector garbageCollector) { foreach(var item in objects) { Tile tile = new Tile() { Data = item.Data, Object = item }; _tiles.Add(tile); } _tiles[garbageCollectorWPF.Location.X + garbageCollectorWPF.Location.Y].Object.Image = MergedBitmaps( new Bitmap(_tiles[garbageCollectorWPF.Location.X + garbageCollectorWPF.Location.Y].Object.ImagePath), new Bitmap(garbageCollectorWPF.ImagePath)); } public void BoardRefresh(List objects, GarbageCollector garbageCollector) { foreach (var item in objects) { _tiles[item.Location.X + item.Location.Y].Object = item; _tiles[item.Location.X + item.Location.Y].Object.Image = _tiles[item.Location.X + item.Location.Y].Object.RefreshImage(); } garbageCollectorWPF.Move(_columns, garbageCollector); _tiles[garbageCollectorWPF.Location.X + garbageCollectorWPF.Location.Y].Object.Image = MergedBitmaps( new Bitmap(_tiles[garbageCollectorWPF.Location.X + garbageCollectorWPF.Location.Y].Object.ImagePath), new Bitmap(garbageCollectorWPF.ImagePath)); } public int Rows { get { return _rows; } set { _rows = value; } } public int Columns { get { return _columns; } set { _columns = value; } } public List 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; } } }