This commit is contained in:
chtzsch ~ 2023-11-01 12:18:13 +03:00
parent 0b6de2c28b
commit 4dd8e12530
5 changed files with 373 additions and 67 deletions

View File

@ -60,21 +60,21 @@ namespace speed_Boat.Generics
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
public static bool? operator - (BoatsGenericCollection<T, U> collect, int pos)//bool??
public static T? operator - (BoatsGenericCollection<T, U> collect, int pos)//bool??
{
T? obj = collect._collection.Get(pos);
if (obj == null)
T? obj = collect._collection[pos];
if (obj != null)
{
return false;
collect?._collection.Remove(pos);
}
return collect?._collection.Remove(pos) ?? false;
return obj;
}
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
@ -113,26 +113,28 @@ namespace speed_Boat.Generics
/// </summary>
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
{
int col = 0;
int i = 0;
int width_Col = _pictureWidth / _placeSizeWidth;//количество колонок в окне прорисовки
for (int j = 0; j < _collection.Count; j++)
foreach (var boat in _collection.GetBoats())
{
DrawingBoat? boat = _collection.Get(j);
if (boat == null)
if(boat != null)
{
col++;
if (col > 2)
col = 0;
continue;
}
boat.SetPosition(col * _placeSizeWidth, j / width_Col * _placeSizeHeight);
boat.SetPosition(col * _placeSizeWidth, i / width_Col * _placeSizeHeight);
col++;
if (col > 2)
col = 0;
boat.DrawTransport(g);
i++;
}
else if(boat == null)
{
col++;
if (col > 2)
col = 0;
i++;
continue;
}
}
}

View File

@ -0,0 +1,92 @@
using speed_Boat.MovementStrategy;
using SpeedBoatLab.Drawings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace speed_Boat.Generics
{
/// <summary>
/// Класс для хранения коллекции
/// </summary>
internal class BoatsGenericStorage
{
///<summary>
/// Словарь(хранилище)
/// </summary>
readonly Dictionary<string, BoatsGenericCollection<DrawingBoat, DrawingObjectBoat>> _boatStorages;
///<summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _boatStorages.Keys.ToList();
///<summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
///<summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
///<summary>
/// Конструктор
/// </summary>
public BoatsGenericStorage(int pictureWidth, int pictureHeight)
{
_boatStorages = new Dictionary<string, BoatsGenericCollection<DrawingBoat, DrawingObjectBoat>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
///<summary>
/// Добавление набора
/// </summary>
public void AddSet(string name)
{
if (_boatStorages.ContainsKey(name))
{
MessageBox.Show("Словарь уже содержит набор с таким названием", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
_boatStorages.Add(name, new BoatsGenericCollection<DrawingBoat, DrawingObjectBoat>(_pictureWidth, _pictureHeight));
}
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
BoatsGenericCollection<DrawingBoat, DrawingObjectBoat> boat;
if (_boatStorages.TryGetValue(name, out boat))
{
_boatStorages.Remove(name);
}
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public BoatsGenericCollection<DrawingBoat, DrawingObjectBoat>?this[string ind]
{
get
{
BoatsGenericCollection<DrawingBoat, DrawingObjectBoat> boat;
//проверка есть ли в словаре обьект с ключом ind
if (_boatStorages.TryGetValue(ind, out boat))
{
return boat;
}
return null;
}
}
}
}

View File

@ -30,6 +30,11 @@ namespace SpeedBoatLab
private void InitializeComponent()
{
groupBox1 = new System.Windows.Forms.GroupBox();
groupBox2 = new System.Windows.Forms.GroupBox();
deleteStorageButton = new System.Windows.Forms.Button();
storagesListBox = new System.Windows.Forms.ListBox();
addStorageButton = new System.Windows.Forms.Button();
nameStorageTextBox = new System.Windows.Forms.TextBox();
label1 = new System.Windows.Forms.Label();
maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
UpdateCollectionButton = new System.Windows.Forms.Button();
@ -37,27 +42,79 @@ namespace SpeedBoatLab
AddBoatButton = new System.Windows.Forms.Button();
pictureBoxCollection = new System.Windows.Forms.PictureBox();
groupBox1.SuspendLayout();
groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
// groupBox1
//
groupBox1.Controls.Add(groupBox2);
groupBox1.Controls.Add(label1);
groupBox1.Controls.Add(maskedTextBoxNumber);
groupBox1.Controls.Add(UpdateCollectionButton);
groupBox1.Controls.Add(DeleteBoatButton);
groupBox1.Controls.Add(AddBoatButton);
groupBox1.Location = new System.Drawing.Point(581, 12);
groupBox1.Location = new System.Drawing.Point(581, 2);
groupBox1.Name = "groupBox1";
groupBox1.Size = new System.Drawing.Size(205, 426);
groupBox1.Size = new System.Drawing.Size(205, 436);
groupBox1.TabIndex = 0;
groupBox1.TabStop = false;
groupBox1.Text = "Настройки";
//
// groupBox2
//
groupBox2.Controls.Add(deleteStorageButton);
groupBox2.Controls.Add(storagesListBox);
groupBox2.Controls.Add(addStorageButton);
groupBox2.Controls.Add(nameStorageTextBox);
groupBox2.Location = new System.Drawing.Point(6, 22);
groupBox2.Name = "groupBox2";
groupBox2.Size = new System.Drawing.Size(193, 240);
groupBox2.TabIndex = 5;
groupBox2.TabStop = false;
groupBox2.Text = "Наборы";
//
// deleteStorageButton
//
deleteStorageButton.Location = new System.Drawing.Point(6, 205);
deleteStorageButton.Name = "deleteStorageButton";
deleteStorageButton.Size = new System.Drawing.Size(181, 29);
deleteStorageButton.TabIndex = 5;
deleteStorageButton.Text = "Удалить набор";
deleteStorageButton.UseVisualStyleBackColor = true;
deleteStorageButton.Click += deleteStorageButton_Click;
//
// storagesListBox
//
storagesListBox.FormattingEnabled = true;
storagesListBox.ItemHeight = 20;
storagesListBox.Location = new System.Drawing.Point(6, 94);
storagesListBox.Name = "storagesListBox";
storagesListBox.Size = new System.Drawing.Size(181, 104);
storagesListBox.TabIndex = 4;
storagesListBox.SelectedIndexChanged += storageListBox_SelectedIndexChanged;
//
// addStorageButton
//
addStorageButton.Location = new System.Drawing.Point(6, 59);
addStorageButton.Name = "addStorageButton";
addStorageButton.Size = new System.Drawing.Size(181, 29);
addStorageButton.TabIndex = 3;
addStorageButton.Text = "Добавить набор";
addStorageButton.UseVisualStyleBackColor = true;
addStorageButton.Click += storageAddButton_Click;
//
// nameStorageTextBox
//
nameStorageTextBox.Location = new System.Drawing.Point(6, 26);
nameStorageTextBox.Name = "nameStorageTextBox";
nameStorageTextBox.Size = new System.Drawing.Size(181, 27);
nameStorageTextBox.TabIndex = 2;
//
// label1
//
label1.AutoSize = true;
label1.Location = new System.Drawing.Point(6, 85);
label1.Location = new System.Drawing.Point(6, 299);
label1.Name = "label1";
label1.Size = new System.Drawing.Size(135, 20);
label1.TabIndex = 4;
@ -65,16 +122,16 @@ namespace SpeedBoatLab
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new System.Drawing.Point(6, 108);
maskedTextBoxNumber.Location = new System.Drawing.Point(6, 322);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new System.Drawing.Size(193, 27);
maskedTextBoxNumber.TabIndex = 3;
//
// UpdateCollectionButton
//
UpdateCollectionButton.Location = new System.Drawing.Point(6, 194);
UpdateCollectionButton.Location = new System.Drawing.Point(6, 391);
UpdateCollectionButton.Name = "UpdateCollectionButton";
UpdateCollectionButton.Size = new System.Drawing.Size(193, 47);
UpdateCollectionButton.Size = new System.Drawing.Size(193, 29);
UpdateCollectionButton.TabIndex = 2;
UpdateCollectionButton.Text = "Обновить коллекцию";
UpdateCollectionButton.UseVisualStyleBackColor = true;
@ -82,9 +139,9 @@ namespace SpeedBoatLab
//
// DeleteBoatButton
//
DeleteBoatButton.Location = new System.Drawing.Point(6, 141);
DeleteBoatButton.Location = new System.Drawing.Point(6, 355);
DeleteBoatButton.Name = "DeleteBoatButton";
DeleteBoatButton.Size = new System.Drawing.Size(193, 47);
DeleteBoatButton.Size = new System.Drawing.Size(193, 30);
DeleteBoatButton.TabIndex = 1;
DeleteBoatButton.Text = "Удалить Катер";
DeleteBoatButton.UseVisualStyleBackColor = true;
@ -92,9 +149,9 @@ namespace SpeedBoatLab
//
// AddBoatButton
//
AddBoatButton.Location = new System.Drawing.Point(6, 26);
AddBoatButton.Location = new System.Drawing.Point(6, 268);
AddBoatButton.Name = "AddBoatButton";
AddBoatButton.Size = new System.Drawing.Size(193, 47);
AddBoatButton.Size = new System.Drawing.Size(193, 28);
AddBoatButton.TabIndex = 0;
AddBoatButton.Text = "Добавить Катер";
AddBoatButton.UseVisualStyleBackColor = true;
@ -119,6 +176,8 @@ namespace SpeedBoatLab
Text = "Коллекция катеров";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
groupBox2.ResumeLayout(false);
groupBox2.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false);
}
@ -132,5 +191,10 @@ namespace SpeedBoatLab
private System.Windows.Forms.PictureBox pictureBoxCollection;
private System.Windows.Forms.MaskedTextBox maskedTextBoxNumber;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.ListBox storagesListBox;
private System.Windows.Forms.Button addStorageButton;
private System.Windows.Forms.TextBox nameStorageTextBox;
private System.Windows.Forms.Button deleteStorageButton;
}
}

View File

@ -19,15 +19,80 @@ namespace SpeedBoatLab
/// <summary>
/// Набор объектов
/// </summary>
private readonly BoatsGenericCollection<DrawingBoat, DrawingObjectBoat> _boats;
private readonly BoatsGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormBoatCollection()
{
InitializeComponent();
_boats = new BoatsGenericCollection<DrawingBoat, DrawingObjectBoat>
(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new BoatsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
///<summary>
/// Заполнение collectionsListBox
/// </summary>
private void ReloadObjects()
{
int index = storagesListBox.SelectedIndex;
storagesListBox.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
storagesListBox.Items.Add(_storage.Keys[i]);
}
if (storagesListBox.Items.Count > 0 && (index == -1 ||
index >= storagesListBox.Items.Count))
{
storagesListBox.SelectedIndex = 0;
}
else if (storagesListBox.Items.Count > 0 && index > -1 &&
index < storagesListBox.Items.Count)
{
storagesListBox.SelectedIndex = index;
}
}
///<summary>
/// Добавление набора в коллекцию
/// </summary>
private void storageAddButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(nameStorageTextBox.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(nameStorageTextBox.Text);
ReloadObjects();
}
///<summary>
/// Выбор набора
/// </summary>
private void storageListBox_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image =
_storage[storagesListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowBoats();
}
///<storage>
/// Удаление набора
/// </storage>
private void deleteStorageButton_Click(Object sender, EventArgs e)
{
if (storagesListBox.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {storagesListBox.SelectedItem}?",
"Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(storagesListBox.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
}
/// <summary>
/// Добавление объекта в набор
@ -36,13 +101,23 @@ namespace SpeedBoatLab
/// <param name="e"></param>
private void ButtonAddBoat_Click(object sender, EventArgs e)
{
if (storagesListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[storagesListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormSpeedBoat form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_boats + form.SelectedBoat != false)
if (obj + form.SelectedBoat)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _boats.ShowBoats();
pictureBoxCollection.Image = obj.ShowBoats();
}
else
{
@ -57,6 +132,15 @@ namespace SpeedBoatLab
/// <param name="e"></param>
private void ButtonRemoveBoat_Click(object sender, EventArgs e)
{
if (storagesListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[storagesListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
@ -67,13 +151,13 @@ namespace SpeedBoatLab
if (insertPosition != string.Empty)
{
int.TryParse(insertPosition, out pos);
if (pos < 0 || pos > _boats._collection.Count - 1)
if (pos < 0 || pos > obj._collection.Count - 1)
MessageBox.Show("Неверный формат позиции");
if (_boats - pos != false)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _boats.ShowBoats();
pictureBoxCollection.Image = obj.ShowBoats();
}
}
else if (insertPosition == string.Empty)
@ -92,7 +176,17 @@ namespace SpeedBoatLab
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _boats.ShowBoats();
if (storagesListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[storagesListBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowBoats();
}
}
}

View File

@ -13,28 +13,56 @@ namespace speed_Boat.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>
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
public GenericClass(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(count);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
public bool Insert(T boat)
{
int currentI = -1;
for(int i = 0; i < _places.Length; i++)
//TODO vstavka v nachalo nabora!!!
if(_places.Count == 0)
{
if (_places[i] == null)
_places.Add(boat);
return true;
}
else
{
if (_places.Count < _maxCount)
{
_places.Add(boat);
for(int i = 0; i < _places.Count; i++)
{
T temp = _places[i];
_places[i] = _places[_places.Count - 1];
_places[_places.Count - 1] = temp;
}
return true;
}
}
return false;
/*int currentI = -1;
for(int i = 0; i < _maxCount; i++)//_maxCount ili _places.Count?
{
if (_places[i] != null)
{
currentI = i;
break;
@ -52,7 +80,7 @@ namespace speed_Boat.Generics
}
_places[0] = boat;
return true;
return true;*/
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию.
@ -64,6 +92,8 @@ namespace speed_Boat.Generics
{
if (position < 0 || position >= Count)
return false;
if (_places == null)
return false;
if (_places[position] == null)
{
@ -71,26 +101,19 @@ namespace speed_Boat.Generics
return true;
}
int currentI = -1;
for (int i = position; i < Count; i++)
if (_places.Count < _maxCount)
{
if (_places[i] == null)
_places.Add(boat);
for (int i = position; i < _places.Count; i++)
{
currentI = i;
break;
T temp = _places[i];
_places[i] = _places[_places.Count - 1];
_places[_places.Count - 1] = temp;
}
}
if (currentI < 0)
return false;
for (int i = currentI + 1; i > 0; i--)
_places[i] = _places[i - 1];
_places[currentI] = boat;
return true;
}
return false;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
@ -107,7 +130,9 @@ namespace speed_Boat.Generics
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
public T? Get(int position)
public T? this[int position]
{
get
{
if (position < 0 || position >= Count)
{
@ -117,7 +142,36 @@ namespace speed_Boat.Generics
{
return _places[position];
}
}
set
{
if (position < 0 || position >= Count)
{
MessageBox.Show("Позиция элемента находится вне списка");
}
else if(_places.Count >= Count)
{
MessageBox.Show("Вставка невозможна, т.к. список заполнен");
}
else
{
Insert(value, position);
}
}
}
///<summary>
/// Проход по списку
/// </summary>
public IEnumerable<T?> GetBoats(int? maxBoats = null)
{
for(int i = 0; i < _places.Count; i++)
{
yield return _places[i];
if (maxBoats.HasValue && i == maxBoats.Value)
{
yield break;
}
}
}
}
}