using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WarmlyLocomotive.Generics; using WarmlyLocomotive.DrawningObjects; using WarmlyLocomotive.Entities; using WarmlyLocomotive.MovementStrategy; using static System.Windows.Forms.VisualStyles.VisualStyleElement; using WarmlyLocomotive; namespace WarmlyLocomotive.Generics { internal class WarmlyLocomotiveGenericCollection where T : DrawningWarmlyLocomotive where U : IMoveableObject { private readonly int _pictureWidth; private readonly int _pictureHeight; private readonly int _placeSizeWidth = 270; private readonly int _placeSizeHeight = 100; private readonly SetGeneric _collection; public void Sort(IComparer comparer) => _collection.SortSet(comparer); public WarmlyLocomotiveGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; int height = picHeight / _placeSizeHeight; _pictureWidth = picWidth; _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } public static bool operator +(WarmlyLocomotiveGenericCollection collect, T? obj) { if (obj == null || collect == null) return false; collect._collection.Insert(obj, new DrawingWarmlyLocomotiveEqutables()); return true; } public static T? operator -(WarmlyLocomotiveGenericCollection collect, int pos) { T? obj = collect._collection[pos]; collect._collection.Remove(pos); return obj; } public U? GetU(int pos) { return (U?)_collection[pos]?.GetMoveableObject; } public Bitmap ShowWarmlyLocomotives() { Bitmap bmp = new(_pictureWidth, _pictureHeight); Graphics gr = Graphics.FromImage(bmp); DrawBackground(gr); DrawObjects(gr); return bmp; } private void DrawBackground(Graphics g) { Pen pen = new(Color.Black, 3); for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) {//линия разметки места g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); } g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); } } private void DrawObjects(Graphics g) { int i = 0; int numPlacesInRow = _pictureWidth / _placeSizeWidth; foreach (var warmlylocomotive in _collection.GetWarmlyLocomotive()) { if (warmlylocomotive != null) { warmlylocomotive.SetPosition((i % numPlacesInRow) * _placeSizeWidth + _placeSizeWidth / 20, _placeSizeHeight * (i / numPlacesInRow) + _placeSizeHeight / 10); warmlylocomotive.DrawTransport(g); } i++; } } public IEnumerable GetWarmlyLocomotive => _collection.GetWarmlyLocomotive(); } }