This commit is contained in:
Игорь Гордеев 2023-12-14 20:42:45 +04:00
parent 660f9be57c
commit 3394c1cf58
6 changed files with 239 additions and 61 deletions

View File

@ -30,12 +30,18 @@
{
pictureBoxCollections = new PictureBox();
groupBoxTools = new GroupBox();
groupBox1 = new GroupBox();
listBoxStorage = new ListBox();
DelNabor = new Button();
AddNabor = new Button();
InputNabor = new MaskedTextBox();
maskedTextBoxNumbers = new MaskedTextBox();
buttonUpdateCollection = new Button();
buttonRemoveTransport = new Button();
buttonAddTransport = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCollections).BeginInit();
groupBoxTools.SuspendLayout();
groupBox1.SuspendLayout();
SuspendLayout();
//
// pictureBoxCollections
@ -48,6 +54,7 @@
//
// groupBoxTools
//
groupBoxTools.Controls.Add(groupBox1);
groupBoxTools.Controls.Add(maskedTextBoxNumbers);
groupBoxTools.Controls.Add(buttonUpdateCollection);
groupBoxTools.Controls.Add(buttonRemoveTransport);
@ -59,9 +66,60 @@
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
//
// groupBox1
//
groupBox1.Controls.Add(listBoxStorage);
groupBox1.Controls.Add(DelNabor);
groupBox1.Controls.Add(AddNabor);
groupBox1.Controls.Add(InputNabor);
groupBox1.Location = new Point(6, 22);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(188, 223);
groupBox1.TabIndex = 6;
groupBox1.TabStop = false;
groupBox1.Text = "Наборы";
//
// listBoxStorage
//
listBoxStorage.FormattingEnabled = true;
listBoxStorage.ItemHeight = 15;
listBoxStorage.Location = new Point(6, 85);
listBoxStorage.Name = "listBoxStorage";
listBoxStorage.Size = new Size(176, 94);
listBoxStorage.TabIndex = 10;
//
// DelNabor
//
DelNabor.Location = new Point(6, 187);
DelNabor.Name = "DelNabor";
DelNabor.Size = new Size(176, 30);
DelNabor.TabIndex = 9;
DelNabor.Text = "Удалить набор";
DelNabor.UseVisualStyleBackColor = true;
DelNabor.Click += ButtonRemoveObject_Click;
//
// AddNabor
//
AddNabor.Location = new Point(6, 49);
AddNabor.Name = "AddNabor";
AddNabor.Size = new Size(176, 30);
AddNabor.TabIndex = 8;
AddNabor.Text = "Добавить набор";
AddNabor.UseVisualStyleBackColor = true;
AddNabor.Click += ButtonAddObject_Click;
//
// InputNabor
//
InputNabor.Location = new Point(6, 21);
InputNabor.Margin = new Padding(3, 2, 3, 2);
InputNabor.Mask = "0";
InputNabor.Name = "InputNabor";
InputNabor.Size = new Size(176, 23);
InputNabor.TabIndex = 7;
//
// maskedTextBoxNumbers
//
maskedTextBoxNumbers.Location = new Point(6, 106);
maskedTextBoxNumbers.Location = new Point(6, 312);
maskedTextBoxNumbers.Margin = new Padding(3, 2, 3, 2);
maskedTextBoxNumbers.Mask = "0";
maskedTextBoxNumbers.Name = "maskedTextBoxNumbers";
@ -70,7 +128,7 @@
//
// buttonUpdateCollection
//
buttonUpdateCollection.Location = new Point(6, 220);
buttonUpdateCollection.Location = new Point(6, 386);
buttonUpdateCollection.Name = "buttonUpdateCollection";
buttonUpdateCollection.Size = new Size(188, 40);
buttonUpdateCollection.TabIndex = 3;
@ -80,7 +138,7 @@
//
// buttonRemoveTransport
//
buttonRemoveTransport.Location = new Point(6, 152);
buttonRemoveTransport.Location = new Point(6, 340);
buttonRemoveTransport.Name = "buttonRemoveTransport";
buttonRemoveTransport.Size = new Size(188, 40);
buttonRemoveTransport.TabIndex = 2;
@ -90,7 +148,7 @@
//
// buttonAddTransport
//
buttonAddTransport.Location = new Point(6, 39);
buttonAddTransport.Location = new Point(6, 260);
buttonAddTransport.Name = "buttonAddTransport";
buttonAddTransport.Size = new Size(188, 47);
buttonAddTransport.TabIndex = 0;
@ -110,6 +168,8 @@
((System.ComponentModel.ISupportInitialize)pictureBoxCollections).EndInit();
groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout();
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
ResumeLayout(false);
}
@ -121,5 +181,10 @@
private Button buttonRemoveTransport;
private Button buttonAddTransport;
private MaskedTextBox maskedTextBoxNumbers;
private GroupBox groupBox1;
private ListBox listBoxStorage;
private Button DelNabor;
private Button AddNabor;
private MaskedTextBox InputNabor;
}
}

View File

@ -1,6 +1,7 @@
using Bulldoser.Drawings;
using Bulldoser.Generic;
using Bulldoser.MovementStrategy;
using Bulldozer.Generics;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@ -15,24 +16,75 @@ namespace Bulldoser
{
public partial class FormTractorCollection : Form
{
private readonly TractorGenericCollection<DrawingTractor, DrawingObjectTractor> _Tractors;
private readonly TractorGenericStorage _storage;
public FormTractorCollection()
{
InitializeComponent();
_Tractors = new TractorGenericCollection<DrawingTractor, DrawingObjectTractor>(pictureBoxCollections.Width,
pictureBoxCollections.Height);
_storage = new TractorGenericStorage(pictureBoxCollections.Width, pictureBoxCollections.Height);
}
private void ReloadObjects()
{
int index = listBoxStorage.SelectedIndex;
listBoxStorage.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorage.Items.Add(_storage.Keys[i]);
}
if (listBoxStorage.Items.Count > 0 && (index == -1 || index >= listBoxStorage.Items.Count))
{
listBoxStorage.SelectedIndex = 0;
}
else if (listBoxStorage.Items.Count > 0 && index > -1 && index < listBoxStorage.Items.Count)
{
listBoxStorage.SelectedIndex = index;
}
}
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(InputNabor.Text))
{
MessageBox.Show("Не всё заполнено", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(InputNabor.Text);
ReloadObjects();
}
private void listBoxStorage_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollections.Image = _storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowTractors();
}
private void ButtonRemoveObject_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {listBoxStorage.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorage.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
}
private void ButtonAddTractor_Click(object sender, EventArgs e)
{
if (_Tractors == null) return;
if (listBoxStorage.SelectedIndex == -1) return;
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormBulldoser form = new();
if (form.ShowDialog() == DialogResult.OK)
{
//проверяем, удалось ли нам загрузить объект
if (_Tractors + form.SelectedTractor != -1)
if (obj + form.SelectedTractor > -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollections.Image = _Tractors.ShowTractors();
pictureBoxCollections.Image = obj.ShowTractors();
}
else
{
@ -40,17 +92,25 @@ namespace Bulldoser
}
}
}
private void ButtonRemoveTractor_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1) return;
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumbers.Text);
if (_Tractors - pos != null)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollections.Image = _Tractors.ShowTractors();
pictureBoxCollections.Image = obj.ShowTractors();
}
else
{
@ -59,7 +119,13 @@ namespace Bulldoser
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollections.Image = _Tractors.ShowTractors();
if (listBoxStorage.SelectedIndex == -1) return;
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollections.Image = obj.ShowTractors();
}
}
}

View File

@ -40,7 +40,7 @@ namespace Bulldoser.Generic
public static T? operator -(TractorGenericCollection<T, U> collect, int
pos)
{
T obj = collect._collection.Get(pos);
T obj = collect._collection[pos];
if (obj != null)
{
return collect._collection.Remove(pos);
@ -50,7 +50,7 @@ namespace Bulldoser.Generic
// получение объекта imoveableObj
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// Вывод всего набора объектов
public Bitmap ShowTractors()
@ -76,19 +76,19 @@ namespace Bulldoser.Generic
}
private void DrawObjects(Graphics g)
{
int HeightObjCount = _pictureHeight / _placeSizeHeight;
int WidthObjCount = _pictureWidth / _placeSizeWidth;
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++)
{
T t = _collection.Get(i);
if (t != null)
{
{
t.SetPosition((_collection.Count - i - 1) % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth,
(_collection.Count - i - 1) / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight);
t.DrawTransport(g);
}
}
// Получение объекта
var obj = _collection[i];
// Установка позиции
obj?.SetPosition(
(int)((width - 1) * _placeSizeWidth - (i % width * _placeSizeWidth)),
(int)((height - 1) * _placeSizeHeight - (i / width * _placeSizeHeight))
);
// Прорисовка объекта
obj?.DrawTransport(g);
}
}
}

View File

@ -6,13 +6,15 @@ using System.Threading.Tasks;
namespace Bulldoser.Generic
{
public class SetGeneric<T> where T : class
internal class SetGeneric<T> where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
private readonly List<T?> _places;
public int Count => _places.Count;
private readonly int _maxCount;
public SetGeneric(int count)
{
_places = new T[count];
_places = new List<T?>(count);
_maxCount = count;
}
public int Insert(T tract)
{
@ -20,32 +22,9 @@ namespace Bulldoser.Generic
}
public int Insert(T tract, int position)
{
int NoEmpty = 0, temp = 0;
for (int i = position; i < Count; i++)
{
if (_places[i] != null) NoEmpty++;
}
if (NoEmpty == Count - position - 1) return -1;
if (position < Count && position >= 0)
{
for (int j = position; j < Count; j++)
{
if (_places[j] == null)
{
temp = j;
break;
}
}
// shift right
for (int i = temp; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = tract;
return position;
}
return -1;
if (position < 0 || position >= _maxCount) return -1;
_places.Insert(position, tract);
return position;
}
public T? Remove(int position)
@ -53,15 +32,36 @@ namespace Bulldoser.Generic
if (position >= Count || position < 0)
return null;
T tmp = _places[position];
T? tmp = _places[position];
_places[position] = null;
return tmp;
}
//Получение объекта из набора по позиции
public T? Get(int position)
public T? this[int position]
{
if (position < 0 || position >= Count) return null;
return _places[position];
get
{
if (position < 0 || position >= Count) return null;
return _places[position];
}
set
{
if (position < 0 || position >= Count || Count == _maxCount) return;
_places.Insert(position, value);
}
}
public IEnumerable<T?> GetTractors(int? maxTracts = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxTracts.HasValue && i == maxTracts.Value)
{
yield break;
}
}
}
}
}

View File

@ -0,0 +1,47 @@

using Bulldoser.Drawings;
using Bulldoser.Generic;
using Bulldoser.MovementStrategy;
namespace Bulldozer.Generics
{
internal class TractorGenericStorage
{
readonly Dictionary<string, TractorGenericCollection<DrawingTractor, DrawingObjectTractor>> _TractorsStorage;
public List<string> Keys => _TractorsStorage.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public TractorGenericStorage(int pictureWidth, int pictureHeight)
{
_TractorsStorage = new Dictionary<string, TractorGenericCollection<DrawingTractor, DrawingObjectTractor>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
if (!_TractorsStorage.ContainsKey(name))
{
_TractorsStorage.Add(name, new TractorGenericCollection<DrawingTractor, DrawingObjectTractor>(_pictureWidth, _pictureHeight));
}
}
public void DelSet(string name)
{
if (_TractorsStorage.ContainsKey(name))
{
_TractorsStorage.Remove(name);
}
}
public TractorGenericCollection<DrawingTractor, DrawingObjectTractor>?
this[string ind]
{
get
{
if (_TractorsStorage.ContainsKey(ind))
{
return _TractorsStorage[ind];
}
return null;
}
}
}
}

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bulldoser
namespace Bulldoser.MovementStrategy
{
/// <summary>
/// Статус выполнения операции перемещения