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

76 lines
2.4 KiB
C#
Raw 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;
public LocosGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public static int operator +(LocosGenericCollection<T, U> collect, T?
obj)
{
if (obj == null) return -1;
return collect?._collection.Insert(obj) ?? -1;
}
public static bool operator -(LocosGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
if (obj != null)
{
return collect._collection.Remove(pos);
}
return false;
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
}
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)
{
for (int i = 0; i < _collection.Count; i++) {
DrawingLocomotiv locomotiv = _collection.Get(i);
if (locomotiv != null) {
int inRow = _pictureWidth / _placeSizeWidth;
locomotiv.SetPosition(i % inRow * _placeSizeWidth, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight);
locomotiv.DrawLoco(g);
}
}
}
}