PIbd-21_Yahontov_O.U._Troll.../Trolleybus/Trolleybus/MapWithSetTrolleybusGeneric.cs
2023-01-15 17:22:39 +04:00

210 lines
7.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace Trolleybus
{
internal class MapWithSetTrolleybusGeneric<T, U>
where T : class, IDrawningObject, IEquatable<T>
where U : AbstractMap
{
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private readonly int _placeSizeWidth = 210;
/// <summary>
/// Размер занимаемого объектом места (высота)
/// </summary>
private readonly int _placeSizeHeight = 90;
/// <summary>
/// Набор объектов
/// </summary>
private readonly SetTrolleybusGeneric<T> _setTrolleybus;
/// <summary>
/// Карта
/// </summary>
private readonly U _map;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
/// <param name="map"></param>
public MapWithSetTrolleybusGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setTrolleybus = new SetTrolleybusGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="map"></param>
/// <param name="trolleybus"></param>
/// <returns></returns>
public static int operator +(MapWithSetTrolleybusGeneric<T, U> map, T trolleybus)
{
return map._setTrolleybus.Insert(trolleybus);
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="map"></param>
/// <param name="position"></param>
/// <returns></returns>
public static T operator -(MapWithSetTrolleybusGeneric<T, U> map, int position)
{
return map._setTrolleybus.Remove(position);
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawTrolleybus(gr);
return bmp;
}
/// <summary>
/// Просмотр объекта на карте
/// </summary>
/// <returns></returns>
public Bitmap ShowOnMap()
{
Shaking();
foreach (var trolleybus in _setTrolleybus.GetTrolleybus())
{
return _map.CreateMap(_pictureWidth, _pictureHeight, trolleybus);
}
return new(_pictureWidth, _pictureHeight);
}
/// <summary>
/// Перемещение объекта по крате
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
public string GetData(char separatorType, char separatorData)
{
string data = $"{_map.GetType().Name}{separatorType}";
foreach (var trolleybus in _setTrolleybus.GetTrolleybus())
{
data += $"{trolleybus.GetInfo()}{separatorData}";
}
return data;
}
public void LoadData(string[] records)
{
foreach (var rec in records)
{
if (rec != "")
_setTrolleybus.Insert(DrawningObjectTrolleybus.Create(rec) as T);
}
}
public void Sort(IComparer<T> comparer)
{
_setTrolleybus.SortSet(comparer);
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>
private void Shaking()
{
int j = _setTrolleybus.Count - 1;
for (int i = 0; i < _setTrolleybus.Count; i++)
{
if (_setTrolleybus[j] == null)
{
for (; j > i; j--)
{
var car = _setTrolleybus[j];
if (car != null)
{
_setTrolleybus.Insert(car, i);
_setTrolleybus.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
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, j * _placeSizeHeight);
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight + 10, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + 10);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
/// <summary>
/// Метод прорисовки объектов
/// </summary>
/// <param name="g"></param>
private void DrawTrolleybus(Graphics g)
{
int xForTrolleybus = _pictureWidth - 2 * _placeSizeWidth + 60;
int yForTrolleybus = 10;
int countInRow = 0;
for (int i = 0; i < _setTrolleybus.Count; i++)
{
if (countInRow >= _pictureWidth / (_placeSizeWidth + 30))
{
xForTrolleybus = _pictureWidth - 2 * _placeSizeWidth + 60;
yForTrolleybus += _placeSizeHeight;
countInRow = 0;
}
if (_setTrolleybus[i] != null)
{
T trolleybus = _setTrolleybus[i];
trolleybus.SetObject(xForTrolleybus, yForTrolleybus, _pictureWidth, _pictureHeight);
trolleybus.DrawningObject(g);
}
xForTrolleybus -= _placeSizeWidth + 30;
countInRow++;
}
}
}
}