промежуточный результат

This commit is contained in:
bekodeg 2023-10-25 10:25:50 +04:00
parent ddcceff03f
commit 74ad587062
4 changed files with 224 additions and 41 deletions

View File

@ -31,13 +31,19 @@
tableLayoutPanel1 = new TableLayoutPanel();
pictureBoxCollection = new PictureBox();
groupBox1 = new GroupBox();
groupBox2 = new GroupBox();
listBoxStorages = new ListBox();
button1 = new Button();
textBoxStorageName = new TextBox();
maskedTextBoxNumber = new MaskedTextBox();
buttonRefreshCollection = new Button();
buttonRemoveLocomotive = new Button();
buttonAddLocomotive = new Button();
button2 = new Button();
tableLayoutPanel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
groupBox1.SuspendLayout();
groupBox2.SuspendLayout();
SuspendLayout();
//
// tableLayoutPanel1
@ -69,7 +75,7 @@
// groupBox1
//
groupBox1.Anchor = AnchorStyles.Top | AnchorStyles.Right;
groupBox1.AutoSize = true;
groupBox1.Controls.Add(groupBox2);
groupBox1.Controls.Add(maskedTextBoxNumber);
groupBox1.Controls.Add(buttonRefreshCollection);
groupBox1.Controls.Add(buttonRemoveLocomotive);
@ -78,14 +84,52 @@
groupBox1.MaximumSize = new Size(200, 1000000);
groupBox1.MinimumSize = new Size(200, 200);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(200, 220);
groupBox1.Size = new Size(200, 444);
groupBox1.TabIndex = 0;
groupBox1.TabStop = false;
groupBox1.Text = "Инструменты";
//
// groupBox2
//
groupBox2.Controls.Add(button2);
groupBox2.Controls.Add(listBoxStorages);
groupBox2.Controls.Add(button1);
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 = "Наборы";
//
// 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;
//
// button1
//
button1.Location = new Point(4, 59);
button1.Name = "button1";
button1.Size = new Size(176, 29);
button1.TabIndex = 1;
button1.Text = "Добавить набор";
button1.UseVisualStyleBackColor = true;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(6, 26);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(176, 27);
textBoxStorageName.TabIndex = 0;
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(6, 97);
maskedTextBoxNumber.Location = new Point(10, 328);
maskedTextBoxNumber.Mask = "00000";
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(184, 27);
@ -94,7 +138,7 @@
//
// buttonRefreshCollection
//
buttonRefreshCollection.Location = new Point(6, 165);
buttonRefreshCollection.Location = new Point(10, 396);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(184, 29);
buttonRefreshCollection.TabIndex = 3;
@ -104,7 +148,7 @@
//
// buttonRemoveLocomotive
//
buttonRemoveLocomotive.Location = new Point(6, 130);
buttonRemoveLocomotive.Location = new Point(10, 361);
buttonRemoveLocomotive.Name = "buttonRemoveLocomotive";
buttonRemoveLocomotive.Size = new Size(184, 29);
buttonRemoveLocomotive.TabIndex = 2;
@ -114,13 +158,21 @@
//
// buttonAddLocomotive
//
buttonAddLocomotive.Location = new Point(6, 26);
buttonAddLocomotive.Location = new Point(10, 293);
buttonAddLocomotive.Name = "buttonAddLocomotive";
buttonAddLocomotive.Size = new Size(184, 29);
buttonAddLocomotive.TabIndex = 0;
buttonAddLocomotive.Text = "Добавить локомотив";
buttonAddLocomotive.UseVisualStyleBackColor = true;
buttonAddLocomotive.Click += buttonAddLocomotive_Click;
//
// button2
//
button2.Location = new Point(4, 204);
button2.Name = "button2";
button2.Size = new Size(176, 29);
button2.TabIndex = 3;
button2.Text = "Добавить набор";
button2.UseVisualStyleBackColor = true;
//
// FormLocomotiveCollection
//
@ -132,10 +184,11 @@
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();
}
@ -149,5 +202,10 @@
private Button buttonRemoveLocomotive;
private Button buttonRefreshCollection;
private MaskedTextBox maskedTextBoxNumber;
private GroupBox groupBox2;
private TextBox textBoxStorageName;
private Button button1;
private ListBox listBoxStorages;
private Button button2;
}
}

View File

@ -91,7 +91,7 @@ namespace ProjectElectricLocomotive.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
@ -133,12 +133,13 @@ namespace ProjectElectricLocomotive.Generics
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
foreach (var locomotive in _collection.GetLocomotives())
{
DrawningLocomotive drawningLocomotive = _collection.Get(i);
if (drawningLocomotive == null)
if (locomotive != null)
{
continue;
//int widthCount = _pictureHeight / _placeSizeHeight;
//locomotive.SetPosition((i / widthCount) * _placeSizeWidth, (i % widthCount) * _placeSizeHeight);
locomotive.DrawTransport(g);
}
int ColumnCount = _pictureWidth / _placeSizeWidth;

View File

@ -14,20 +14,25 @@ namespace ProjectElectricLocomotive.Generics
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// Список объектов, которые храним
/// </summary>
private readonly T?[] _places;
private readonly List<T?> _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(count);
}
/// <summary>
@ -36,11 +41,11 @@ namespace ProjectElectricLocomotive.Generics
/// <param name="first">Первый элемент</param>
/// <param name="second">Второй элемент</param>
/// <returns></returns>
private void swap(ref T first, ref T second)
private void swap(int index1, int index2)
{
T temp = first;
first = second;
second = temp;
T temp = _places[index1];
_places[index1] = _places[index2];
_places[index2] = temp;
}
/// <summary>
@ -50,8 +55,8 @@ namespace ProjectElectricLocomotive.Generics
/// <returns></returns>
public bool Insert(T locomotive)
{
int end = _places.Length;
for (int i = 0; i < _places.Length; ++i)
int end = Count;
for (int i = 0; i < Count; ++i)
{
if (_places[i] == null)
{
@ -59,7 +64,7 @@ namespace ProjectElectricLocomotive.Generics
break;
}
}
if (end == _places.Length)
if (end == Count)
{
return false;
}
@ -78,7 +83,7 @@ namespace ProjectElectricLocomotive.Generics
/// <returns></returns>
public bool Insert(T locomotive, int position)
{
if (position >= _places.Length || position < 0)
if (position >= Count || position < 0)
{
return false;
}
@ -87,8 +92,8 @@ namespace ProjectElectricLocomotive.Generics
_places[position] = locomotive;
return true;
}
int end = _places.Length;
for (int i = position; i < _places.Length; ++i)
int end = Count;
for (int i = position; i < Count; ++i)
{
if (_places[i] == null)
{
@ -96,14 +101,14 @@ namespace ProjectElectricLocomotive.Generics
break;
}
}
if (end == _places.Length)
if (end == Count)
{
return false;
}
_places[end] = locomotive;
for (int i = position; i < end; ++i)
{
swap(ref _places[i], ref _places[end]);
swap(i, end);
}
return true;
}
@ -114,7 +119,7 @@ namespace ProjectElectricLocomotive.Generics
/// <returns></returns>
public bool Remove(int position)
{
if (position >= _places.Length || position < 0)
if (position >= Count || position < 0)
{
return false;
}
@ -128,13 +133,38 @@ namespace ProjectElectricLocomotive.Generics
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
public T? this[int position]
{
if (position >= _places.Length || position < 0)
get
{
return null;
if (position >= Count || position < 0)
{
return null;
}
return _places[position];
}
set
{
// TODO проверка позиции
// TODO проверка свободных мест в списке
// TODO вставка в список по позиции
}
}
/// <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 _places[position];
}
}

View File

@ -18,19 +18,88 @@ namespace ProjectElectricLocomotive
/// <summary>
/// Набор объектов
/// </summary>
private readonly LocomotivesGenericCollection<
DrawningLocomotive, DrawningObjectLocomotive> _locomotives;
private readonly LocomotivesGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormLocomotiveCollection()
{
InitializeComponent();
_locomotives = new LocomotivesGenericCollection<DrawningLocomotive, DrawningObjectLocomotive>(
pictureBoxCollection.Width, pictureBoxCollection.Height
_storage = new LocomotivesGenericStorage(
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>
/// Добавление объекта в набор
/// </summary>
@ -38,13 +107,23 @@ 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 (_locomotives + form.SelectedLocomotive > 0)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _locomotives.ShowLocomotives();
pictureBoxCollection.Image = obj.ShowLocomotives();
}
else
{
@ -59,6 +138,15 @@ 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)
@ -69,7 +157,7 @@ namespace ProjectElectricLocomotive
if (_locomotives - pos != -1)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _locomotives.ShowLocomotives();
pictureBoxCollection.Image = obj.ShowLocomotives();
}
else
{
@ -83,7 +171,13 @@ namespace ProjectElectricLocomotive
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _locomotives.ShowLocomotives();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
pictureBoxCollection.Image = obj.ShowLocomotives();
}
}
}