PIbd-23_Vrazhkin_S_A_Electr.../lab1/Generics/LocosGenericCollection.cs

77 lines
2.5 KiB
C#
Raw Permalink Normal View History

2023-10-17 13:49:31 +04:00
namespace ElectricLocomotive;
public class LocosGenericCollection <T, U> where T : DrawingLocomotiv where U : IMoveableObject
{
private readonly int _pictureWidth;
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 170;
private readonly int _placeSizeHeight = 110;
private readonly SetGeneric<T> _collection;
2023-11-28 12:26:51 +04:00
public IEnumerable<T?> GetLocos => _collection.GetElectricLocos();
2023-10-17 13:49:31 +04:00
public LocosGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
2023-10-31 13:25:13 +04:00
public static bool operator +(LocosGenericCollection<T, U> collect, T?
2023-10-17 13:49:31 +04:00
obj)
{
2023-10-31 13:25:13 +04:00
if (obj == null) return false;
2023-10-17 13:49:31 +04:00
2023-10-31 13:25:13 +04:00
return collect?._collection.Insert(obj) ?? false;
2023-10-17 13:49:31 +04:00
}
2023-10-31 13:25:13 +04:00
public static T? operator -(LocosGenericCollection<T, U> collect, int
2023-10-17 13:49:31 +04:00
pos)
{
2023-10-31 13:25:13 +04:00
T? obj = collect._collection[pos];
2023-11-28 13:43:14 +04:00
collect._collection.Remove(pos);
2023-10-31 13:25:13 +04:00
return obj;
2023-10-17 13:49:31 +04:00
}
public U? GetU(int pos)
{
2023-10-31 13:25:13 +04:00
return (U?)_collection[pos]?.GetMoveableObject;
2023-10-17 13:49:31 +04:00
}
public Bitmap ShowLocos()
{
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)
{
2023-10-31 13:25:13 +04:00
int i = 0;
foreach(var electricLoco in _collection.GetElectricLocos())
{
if (electricLoco != null)
{
2023-10-17 13:49:31 +04:00
int inRow = _pictureWidth / _placeSizeWidth;
2023-10-31 13:25:13 +04:00
electricLoco.SetPosition(i % inRow * _placeSizeWidth, _pictureHeight - _pictureHeight % _placeSizeHeight - (i / inRow + 1) * _placeSizeHeight);
electricLoco.DrawLoco(g);
2023-10-17 13:49:31 +04:00
}
2023-10-31 13:25:13 +04:00
i++;
2023-10-17 13:49:31 +04:00
}
}
}