diff --git a/lainer/Lainer1/LainerGenericCollection.cs b/lainer/Lainer1/LainerGenericCollection.cs new file mode 100644 index 0000000..37a90d6 --- /dev/null +++ b/lainer/Lainer1/LainerGenericCollection.cs @@ -0,0 +1,105 @@ +using ProjectLainer.MovementStrategy; +using ProjectLainer.DrawningObjects; +using System.Runtime.CompilerServices; + +namespace ProjectLainer.Generics +{ + internal class LainersGenericCollection + where T : DrawingLainer + where U : IMoveableObject + { + + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + + private readonly int _placeSizeWidth = 210; + + private readonly int _placeSizeHeight = 90; + + public readonly SetGeneric _collection; + + public LainersGenericCollection(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 +(LainersGenericCollection collect, T? obj) + { + if (obj != null && collect != null) + { + var value = collect._collection.Insert(obj); + if(value <= collect._collection.Count && value >= 0) + { + return true; + } + else { return false; } + } + return false; + + } + public static bool? operator -(LainersGenericCollection collect, int + pos) + { + T? obj = collect?._collection.Get(pos); + if (obj != null && collect != null) + { + return collect._collection.Remove(pos); + } + else + { + return null; + } + } + + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + + public Bitmap ShowLainers() + { + 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++) + { + DrawingLainer? lainer = _collection.Get(i); + if (lainer != null) + { + int inRow = _pictureWidth / _placeSizeWidth; + + lainer.SetPosition((inRow - 1 - (i % inRow)) * _placeSizeWidth, i / inRow * _placeSizeHeight); + lainer.DrawTransport(g); + } + } + } + } +} diff --git a/lainer/Lainer1/SetGeneric.cs b/lainer/Lainer1/SetGeneric.cs new file mode 100644 index 0000000..6433123 --- /dev/null +++ b/lainer/Lainer1/SetGeneric.cs @@ -0,0 +1,61 @@ +namespace ProjectLainer.Generics +{ + internal class SetGeneric + where T : class + { + private readonly T?[] _places; + public int Count => _places.Length; + public SetGeneric(int count) + { + _places = new T?[count]; + } + + public int Insert(T lainer) + { + if (_places[Count - 1] != null) + return Count; + return Insert(lainer, 0); + } + public int Insert(T lainer, int position) + { + if (position < 0 || position >= Count) + { + return position; + } + if (_places[position] != null) + { + int ind = position; + while (ind < Count && _places[ind] != null) + ind++; + if (ind == Count) + return Count; + for (int i = ind - 1; i >= position; i--) + _places[i + 1] = _places[i]; + } + _places[position] = lainer; + return position; + } + public bool Remove(int position) + { + if (position < 0 ) + { + return false; + } + if(position >= Count) + { + return false; + } + _places[position] = null; + return true; + } + public T? Get(int position) + { + if(position < Count && position >= 0) + { + return _places[position]; + } + return null; + } + } +} +