This commit is contained in:
dyakonovr 2023-10-31 12:39:26 +04:00
parent 76dbe4761b
commit 8893e998d1
5 changed files with 283 additions and 61 deletions

View File

@ -34,6 +34,11 @@
button2 = new Button();
button4 = new Button();
maskedTextBoxNumber = new TextBox();
label2 = new Label();
textBoxStorageName = new TextBox();
button1 = new Button();
listBoxStorages = new ListBox();
button5 = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
@ -56,7 +61,7 @@
//
// button3
//
button3.Location = new Point(775, 247);
button3.Location = new Point(775, 448);
button3.Name = "button3";
button3.Size = new Size(139, 29);
button3.TabIndex = 4;
@ -66,7 +71,7 @@
//
// button2
//
button2.Location = new Point(775, 166);
button2.Location = new Point(775, 393);
button2.Name = "button2";
button2.Size = new Size(139, 29);
button2.TabIndex = 5;
@ -76,7 +81,7 @@
//
// button4
//
button4.Location = new Point(775, 32);
button4.Location = new Point(775, 306);
button4.Name = "button4";
button4.Size = new Size(139, 29);
button4.TabIndex = 6;
@ -86,16 +91,67 @@
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(775, 133);
maskedTextBoxNumber.Location = new Point(775, 360);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(139, 27);
maskedTextBoxNumber.TabIndex = 7;
//
// label2
//
label2.AutoSize = true;
label2.Location = new Point(775, 51);
label2.Name = "label2";
label2.Size = new Size(66, 20);
label2.TabIndex = 8;
label2.Text = "Наборы";
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(775, 74);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(139, 27);
textBoxStorageName.TabIndex = 9;
//
// button1
//
button1.Location = new Point(775, 107);
button1.Name = "button1";
button1.Size = new Size(139, 29);
button1.TabIndex = 10;
button1.Text = "Добавить набор";
button1.UseVisualStyleBackColor = true;
button1.Click += ButtonAddObject_Click;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 20;
listBoxStorages.Location = new Point(775, 142);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(139, 84);
listBoxStorages.TabIndex = 11;
listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
//
// button5
//
button5.Location = new Point(775, 232);
button5.Name = "button5";
button5.Size = new Size(139, 29);
button5.TabIndex = 12;
button5.Text = "Удалить набор";
button5.UseVisualStyleBackColor = true;
button5.Click += ButtonDelObject_Click;
//
// FormTankCollection
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(926, 486);
Controls.Add(button5);
Controls.Add(listBoxStorages);
Controls.Add(button1);
Controls.Add(textBoxStorageName);
Controls.Add(label2);
Controls.Add(maskedTextBoxNumber);
Controls.Add(button4);
Controls.Add(button2);
@ -117,5 +173,10 @@
private Button button2;
private Button button4;
private TextBox maskedTextBoxNumber;
private Label label2;
private TextBox textBoxStorageName;
private Button button1;
private ListBox listBoxStorages;
private Button button5;
}
}

View File

@ -6,19 +6,73 @@ namespace ProjectTank
{
public partial class FormTankCollection : Form
{
/// <summary>
/// Набор объектов
/// </summary>
private readonly TanksGenericCollection<DrawningTankBase, DrawningObjectTank> _tanks;
private readonly TanksGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormTankCollection()
{
InitializeComponent();
_tanks = new TanksGenericCollection<DrawningTankBase, DrawningObjectTank>(pictureBoxCollection.Width,
_storage = new TanksGenericStorage(pictureBoxCollection.Width,
pictureBoxCollection.Height);
}
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;
}
}
private void ButtonAddObject_Click(object sender, EventArgs e)
{
string objName = textBoxStorageName.Text;
if (string.IsNullOrEmpty(objName))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowTanks();
}
/// <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>
@ -26,18 +80,20 @@ namespace ProjectTank
/// <param name="e"></param>
private void ButtonAddTank_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1) return;
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null) return;
FormTank form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_tanks + form.SelectedTank != null)
if (obj + form.SelectedTank)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _tanks.ShowTanks();
}
else
{
MessageBox.Show("Не удалось добавить объект");
pictureBoxCollection.Image = obj.ShowTanks();
}
else MessageBox.Show("Не удалось добавить объект");
}
}
/// <summary>
@ -47,32 +103,41 @@ namespace ProjectTank
/// <param name="e"></param>
private void ButtonRemoveTank_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)
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_tanks - pos != null)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _tanks.ShowTanks();
maskedTextBoxNumber.Text = "";
}
else
{
MessageBox.Show("Не удалось удалить объект");
pictureBoxCollection.Image = obj.ShowTanks();
}
else MessageBox.Show("Не удалось удалить объект");
}
/// <summary>
/// Обновление рисунка по набору
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _tanks.ShowTanks();
if (listBoxStorages.SelectedIndex == -1) return;
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null) return;
pictureBoxCollection.Image = obj.ShowTanks();
}
}
}

View File

@ -9,28 +9,31 @@ namespace ProjectTank.Generics
/// <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>
/// <param name="count"></param>
private readonly int _maxCount;
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(count);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="tank">Добавляемый автомобиль</param>
/// <returns></returns>
public int Insert(T tank)
public bool Insert(T tank)
{
if (_places[Count-1] != null)
return -1;
if (_places.Count == _maxCount)
return false;
return Insert(tank, 0);
}
/// <summary>
@ -39,22 +42,12 @@ namespace ProjectTank.Generics
/// <param name="tank">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T tank, int position)
public bool Insert(T tank, int position)
{
if (!(position >= 0 && position < Count)) return -1;
if (_places[position] != null)
{
int ind = position;
while (ind < Count && _places[ind] != null)
ind++;
if (ind == Count)
return -1;
for (int i = ind - 1; i >= position; i--)
_places[i + 1] = _places[i];
}
_places[position] = tank;
return position;
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
return false;
_places.Insert(position, tank);
return true;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
@ -63,20 +56,40 @@ namespace ProjectTank.Generics
/// <returns></returns>
public bool Remove(int position)
{
if (!(position >= 0 && position < Count) || _places[position] == null)
if (!(position >= 0 && position < Count))
return false;
_places[position] = null;
_places.RemoveAt(position);
return true;
}
public T? this[int position]
{
get {
if (!(position >= 0 && position < Count))
return null;
return _places[position];
}
set {
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
return;
_places.Insert(position, value);
}
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
public IEnumerable<T?> GetTanks(int? maxTanks = null)
{
if (!(position >= 0 && position < Count)) return null;
return _places[position];
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxTanks.HasValue && i == maxTanks.Value)
{
yield break;
}
}
}
}
}

View File

@ -49,10 +49,12 @@ namespace ProjectTank.Generics
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static int operator +(TanksGenericCollection<T,U> collect, T? obj)
public static bool operator +(TanksGenericCollection<T,U> collect, T? obj)
{
if (obj == null) return -1;
return collect?._collection.Insert(obj) ?? -1;
if (obj == null)
return false;
return collect?._collection.Insert(obj) ?? false;
}
/// <summary>
/// Перегрузка оператора вычитания
@ -60,13 +62,17 @@ namespace ProjectTank.Generics
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(TanksGenericCollection<T, U> collect, int pos)
public static T? operator -(TanksGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
if (obj != null) return collect._collection.Remove(pos);
return false;
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
return obj;
}
/// <summary>
/// Получение объекта IMoveableObject
@ -75,7 +81,7 @@ namespace ProjectTank.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
@ -113,9 +119,9 @@ namespace ProjectTank.Generics
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
int i = 0;
foreach (var tank in _collection.GetTanks())
{
DrawningTankBase tank = _collection.Get(i);
if (tank != null)
{
int columnsCount = _pictureHeight / _placeSizeHeight + 1;
@ -125,6 +131,8 @@ namespace ProjectTank.Generics
tank.SetPosition(colIndex * _placeSizeWidth + 2, rowIndex * _placeSizeHeight + 2);
tank.DrawTransport(g);
i++;
}
}
}

View File

@ -0,0 +1,75 @@
using ProjectTank.DrawningObjects;
using ProjectTank.MovementStrategy;
namespace ProjectTank.Generics
{
/// <summary>
/// Класс для хранения коллекции
/// </summary>
internal class TanksGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, TanksGenericCollection<DrawningTankBase,
DrawningObjectTank>> _tankStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _tankStorages.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 TanksGenericStorage(int pictureWidth, int pictureHeight)
{
_tankStorages = new Dictionary<string,
TanksGenericCollection<DrawningTankBase, DrawningObjectTank>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
_tankStorages.Add(name, new TanksGenericCollection<DrawningTankBase, DrawningObjectTank> (_pictureWidth, _pictureHeight));
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (!_tankStorages.ContainsKey(name))
return;
_tankStorages.Remove(name);
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public TanksGenericCollection<DrawningTankBase, DrawningObjectTank>? this[string ind]
{
get
{
if (_tankStorages.ContainsKey(ind))
return _tankStorages[ind];
return null;
}
}
}
}