Compare commits

..

No commits in common. "85704d9e33cf6aa3768406a1400d8adea44ad18c" and "7583b8b681b0868a9f533c373fc80fe879f3e682" have entirely different histories.

7 changed files with 103 additions and 408 deletions

View File

@ -56,7 +56,7 @@ namespace ProjectElectricLocomotive.DrawningObjects
public int GetHeight => _locomotiveHeight;
/// <summary>
/// Получение объекта IMoveableObject из объекта DrawningLocomotive
/// Получение объекта IMoveableObject из объекта DrawningCar
/// </summary>
public IMoveableObject GetMoveableObject => new DrawningObjectLocomotive(this);

View File

@ -31,11 +31,6 @@
tableLayoutPanel1 = new TableLayoutPanel();
pictureBoxCollection = new PictureBox();
groupBox1 = new GroupBox();
groupBox2 = new GroupBox();
buttonDelObject = new Button();
listBoxStorages = new ListBox();
buttonAddObject = new Button();
textBoxStorageName = new TextBox();
maskedTextBoxNumber = new MaskedTextBox();
buttonRefreshCollection = new Button();
buttonRemoveLocomotive = new Button();
@ -43,7 +38,6 @@
tableLayoutPanel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
groupBox1.SuspendLayout();
groupBox2.SuspendLayout();
SuspendLayout();
//
// tableLayoutPanel1
@ -75,7 +69,7 @@
// groupBox1
//
groupBox1.Anchor = AnchorStyles.Top | AnchorStyles.Right;
groupBox1.Controls.Add(groupBox2);
groupBox1.AutoSize = true;
groupBox1.Controls.Add(maskedTextBoxNumber);
groupBox1.Controls.Add(buttonRefreshCollection);
groupBox1.Controls.Add(buttonRemoveLocomotive);
@ -84,64 +78,14 @@
groupBox1.MaximumSize = new Size(200, 1000000);
groupBox1.MinimumSize = new Size(200, 200);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(200, 444);
groupBox1.Size = new Size(200, 220);
groupBox1.TabIndex = 0;
groupBox1.TabStop = false;
groupBox1.Text = "Инструменты";
//
// groupBox2
//
groupBox2.Controls.Add(buttonDelObject);
groupBox2.Controls.Add(listBoxStorages);
groupBox2.Controls.Add(buttonAddObject);
groupBox2.Controls.Add(textBoxStorageName);
groupBox2.Location = new Point(6, 26);
groupBox2.Name = "groupBox2";
groupBox2.Size = new Size(188, 244);
groupBox2.TabIndex = 5;
groupBox2.TabStop = false;
groupBox2.Text = "Наборы";
//
// buttonDelObject
//
buttonDelObject.Location = new Point(4, 204);
buttonDelObject.Name = "buttonDelObject";
buttonDelObject.Size = new Size(176, 29);
buttonDelObject.TabIndex = 3;
buttonDelObject.Text = "Удалить набор";
buttonDelObject.UseVisualStyleBackColor = true;
buttonDelObject.Click += buttonDelObject_Click;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 20;
listBoxStorages.Location = new Point(6, 94);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(174, 104);
listBoxStorages.TabIndex = 2;
listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged;
//
// buttonAddObject
//
buttonAddObject.Location = new Point(4, 59);
buttonAddObject.Name = "buttonAddObject";
buttonAddObject.Size = new Size(176, 29);
buttonAddObject.TabIndex = 1;
buttonAddObject.Text = "Добавить набор";
buttonAddObject.UseVisualStyleBackColor = true;
buttonAddObject.Click += buttonAddObject_Click;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(6, 26);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(176, 27);
textBoxStorageName.TabIndex = 0;
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(10, 328);
maskedTextBoxNumber.Location = new Point(6, 97);
maskedTextBoxNumber.Mask = "00000";
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(184, 27);
@ -150,7 +94,7 @@
//
// buttonRefreshCollection
//
buttonRefreshCollection.Location = new Point(10, 396);
buttonRefreshCollection.Location = new Point(6, 165);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(184, 29);
buttonRefreshCollection.TabIndex = 3;
@ -160,7 +104,7 @@
//
// buttonRemoveLocomotive
//
buttonRemoveLocomotive.Location = new Point(10, 361);
buttonRemoveLocomotive.Location = new Point(6, 130);
buttonRemoveLocomotive.Name = "buttonRemoveLocomotive";
buttonRemoveLocomotive.Size = new Size(184, 29);
buttonRemoveLocomotive.TabIndex = 2;
@ -170,12 +114,13 @@
//
// buttonAddLocomotive
//
buttonAddLocomotive.Location = new Point(10, 293);
buttonAddLocomotive.Location = new Point(6, 26);
buttonAddLocomotive.Name = "buttonAddLocomotive";
buttonAddLocomotive.Size = new Size(184, 29);
buttonAddLocomotive.TabIndex = 0;
buttonAddLocomotive.Text = "Добавить локомотив";
buttonAddLocomotive.UseVisualStyleBackColor = true;
buttonAddLocomotive.Click += buttonAddLocomotive_Click;
//
// FormLocomotiveCollection
//
@ -187,11 +132,10 @@
Name = "FormLocomotiveCollection";
Text = "FormLocomotiveCollection";
tableLayoutPanel1.ResumeLayout(false);
tableLayoutPanel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
groupBox2.ResumeLayout(false);
groupBox2.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
@ -205,10 +149,5 @@
private Button buttonRemoveLocomotive;
private Button buttonRefreshCollection;
private MaskedTextBox maskedTextBoxNumber;
private GroupBox groupBox2;
private TextBox textBoxStorageName;
private Button buttonAddObject;
private ListBox listBoxStorages;
private Button buttonDelObject;
}
}

View File

@ -12,7 +12,10 @@ namespace ProjectElectricLocomotive
{
public partial class FormLocomotiveCollection
{
private void buttonAddLocomotive_Click(object sender, EventArgs e)
{
ButtonAddLocomotive_Click(sender, e);
}
private void buttonRemoveLocomotive_Click(object sender, EventArgs e)
{
@ -23,20 +26,5 @@ namespace ProjectElectricLocomotive
{
ButtonRefreshCollection_Click(sender, e);
}
private void buttonAddObject_Click(object sender, EventArgs e)
{
ButtonAddObject_Click(sender, e);
}
private void buttonDelObject_Click(object sender, EventArgs e)
{
ButtonDelObject_Click(sender, e);
}
private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
{
ListBoxObjects_SelectedIndexChanged(sender, e);
}
}
}

View File

@ -56,17 +56,14 @@ namespace ProjectElectricLocomotive.Generics
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static int operator +(LocomotivesGenericCollection<T, U> collect, T? obj)
public static bool operator +(LocomotivesGenericCollection<T, U> collect, T?
obj)
{
if (obj == null)
{
return 0;
return false;
}
if (collect?._collection.Insert(obj) ?? false)
{
return collect._collection.Count;
}
return 0;
return collect?._collection.Insert(obj) ?? false;
}
/// <summary>
/// Перегрузка оператора вычитания
@ -74,15 +71,15 @@ namespace ProjectElectricLocomotive.Generics
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static int operator -(LocomotivesGenericCollection<T, U> collect, int
public static T? operator -(LocomotivesGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection[pos];
if (obj != null && collect._collection.Remove(pos))
T? obj = collect._collection.Get(pos);
if (obj != null)
{
return collect._collection.Count;
collect._collection.Remove(pos);
}
return -1;
return obj;
}
/// <summary>
/// Получение объекта IMoveableObject
@ -91,14 +88,14 @@ namespace ProjectElectricLocomotive.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection[pos]?.GetMoveableObject;
return (U?)_collection.Get(pos)?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowLocomotives()
public Bitmap ShowCars()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
@ -133,19 +130,17 @@ namespace ProjectElectricLocomotive.Generics
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
int ColumnCount = _pictureWidth / _placeSizeWidth;
int RowsCount = _pictureHeight / _placeSizeHeight;
int pos = 0;
foreach (var locomotive in _collection.GetLocomotives())
for (int i = 0; i < _collection.Count; i++)
{
if (locomotive != null)
DrawningLocomotive drawningLocomotive = _collection.Get(i);
if (drawningLocomotive == null)
{
locomotive.SetPosition((pos % ColumnCount) * _placeSizeWidth, ((RowsCount - pos / ColumnCount - 1) * _placeSizeHeight));
locomotive.DrawTransport(g);
continue;
}
int widthCount = _pictureHeight / _placeSizeHeight;
drawningLocomotive.SetPosition((i / widthCount) * _placeSizeWidth, (i % widthCount) * _placeSizeHeight);
drawningLocomotive.DrawTransport(g);
}
}
}

View File

@ -1,83 +0,0 @@
using ProjectElectricLocomotive.DrawningObjects;
using ProjectElectricLocomotive.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.Generics
{
/// <summary>
/// Класс для хранения коллекции
/// </summary>
internal class LocomotivesGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, LocomotivesGenericCollection<
DrawningLocomotive, DrawningObjectLocomotive>
> _locomotiveStorage;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _locomotiveStorage.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public LocomotivesGenericStorage(int pictureWidth, int pictureHeight)
{
_locomotiveStorage = new Dictionary<string,
LocomotivesGenericCollection<DrawningLocomotive,
DrawningObjectLocomotive>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
// TODO Прописать логику для удаления
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public LocomotivesGenericCollection<DrawningLocomotive,
DrawningObjectLocomotive>? this[string ind]
{
get
{
// TODO Продумать логику получения набора
return null;
}
}
}
}

View File

@ -14,25 +14,33 @@ namespace ProjectElectricLocomotive.Generics
where T : class
{
/// <summary>
/// Список объектов, которые храним
/// Массив объектов, которые храним
/// </summary>
private readonly List<T?> _places;
private readonly T?[] _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
public int Count => _places.Length;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{
_maxCount = count;
_places = new List<T?>(count);
_places = new T?[count];
}
/// <summary>
/// обмен двух элементов
/// </summary>
/// <param name="first">Первый элемент</param>
/// <param name="second">Второй элемент</param>
/// <returns></returns>
private void swap(ref T first, ref T second)
{
T temp = first;
first = second;
second = temp;
}
/// <summary>
@ -42,10 +50,24 @@ namespace ProjectElectricLocomotive.Generics
/// <returns></returns>
public bool Insert(T locomotive)
{
if (_places.Count >= _maxCount) {
int end = _places.Length;
for (int i = 0; i < _places.Length; ++i)
{
if (_places[i] == null)
{
end = i;
break;
}
}
if (end == _places.Length)
{
return false;
}
_places.Insert(0, locomotive);
for (int i = end; i >= 1; --i)
{
_places[i] = _places[i - 1];
}
_places[0] = locomotive;
return true;
}
/// <summary>
@ -56,11 +78,33 @@ namespace ProjectElectricLocomotive.Generics
/// <returns></returns>
public bool Insert(T locomotive, int position)
{
if (_places.Count >= _maxCount || position >= 0 || position <= _places.Count)
if (position >= _places.Length || position < 0)
{
return false;
}
_places.Insert(position, locomotive);
if (_places[position] == null)
{
_places[position] = locomotive;
return true;
}
int end = _places.Length;
for (int i = position; i < _places.Length; ++i)
{
if (_places[i] == null)
{
end = i;
break;
}
}
if (end == _places.Length)
{
return false;
}
_places[end] = locomotive;
for (int i = position; i < end; ++i)
{
swap(ref _places[i], ref _places[end]);
}
return true;
}
/// <summary>
@ -70,12 +114,12 @@ namespace ProjectElectricLocomotive.Generics
/// <returns></returns>
public bool Remove(int position)
{
if (position >= Count || position < 0)
if (position >= _places.Length || position < 0)
{
return false;
}
_places.RemoveAt(position);
_places[position] = null;
return true;
}
@ -84,39 +128,13 @@ namespace ProjectElectricLocomotive.Generics
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? this[int position]
public T? Get(int position)
{
get
if (position >= _places.Length || position < 0)
{
if (position >= Count || position < 0)
{
return null;
}
return _places[position];
}
set
{
if (position < Count || position >= 0)
{
_places[position] = value;
}
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetLocomotives(int? maxLocomotives = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxLocomotives.HasValue && i == maxLocomotives.Value)
{
yield break;
}
return null;
}
return _places[position];
}
}

View File

@ -18,153 +18,19 @@ namespace ProjectElectricLocomotive
/// <summary>
/// Набор объектов
/// </summary>
private readonly LocomotivesGenericStorage _storage;
private readonly LocomotivesGenericCollection<
DrawningLocomotive, DrawningObjectLocomotive> _locomotives;
/// <summary>
/// Конструктор
/// </summary>
public FormLocomotiveCollection()
{
InitializeComponent();
_storage = new LocomotivesGenericStorage(
pictureBoxCollection.Width,
pictureBoxCollection.Height
_locomotives = new LocomotivesGenericCollection<DrawningLocomotive, DrawningObjectLocomotive>(
pictureBoxCollection.Width, pictureBoxCollection.Height
);
}
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
private void ReloadObjects()
{
int index = listBoxStorages.SelectedIndex;
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(_storage.Keys[i]);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count))
{
listBoxStorages.SelectedIndex = 0;
}
else if (listBoxStorages.Items.Count > 0 && index > -1 && index < listBoxStorages.Items.Count)
{
listBoxStorages.SelectedIndex = index;
}
}
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowLocomotives();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?",
"Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
}
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
private void ReloadObjects()
{
int index = listBoxStorages.SelectedIndex;
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(_storage.Keys[i]);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count))
{
listBoxStorages.SelectedIndex = 0;
}
else if (listBoxStorages.Items.Count > 0 && index > -1 &&
index < listBoxStorages.Items.Count)
{
listBoxStorages.SelectedIndex = index;
}
}
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowLocomotives();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?",
"Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
@ -172,23 +38,13 @@ namespace ProjectElectricLocomotive
/// <param name="e"></param>
private void ButtonAddLocomotive_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormLocomotive form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (obj + form.SelectedLocomotive > 0)
if (_locomotives + form.SelectedLocomotive)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = obj.ShowLocomotives();
pictureBoxCollection.Image = _locomotives.ShowCars();
}
else
{
@ -203,15 +59,6 @@ namespace ProjectElectricLocomotive
/// <param name="e"></param>
private void ButtonRemoveLocomotive_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show(
"Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
@ -219,10 +66,10 @@ namespace ProjectElectricLocomotive
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (obj - pos != -1)
if (_locomotives - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = obj.ShowLocomotives();
pictureBoxCollection.Image = _locomotives.ShowCars();
}
else
{
@ -236,16 +83,7 @@ namespace ProjectElectricLocomotive
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowLocomotives();
pictureBoxCollection.Image = _locomotives.ShowCars();
}
}
}