PIbd-21 Malafeev L.S. CrusierBase Lab4 #4

Closed
mallafeev wants to merge 2 commits from lab4 into lab3
5 changed files with 322 additions and 94 deletions

View File

@ -29,51 +29,46 @@
private void InitializeComponent()
{
groupBoxTools = new GroupBox();
button3 = new Button();
button2 = new Button();
buttonDeleteCruiser = new Button();
textBoxNumber = new TextBox();
buttonAddCruiser = new Button();
buttonRefreshCollection = new Button();
pictureBoxCollection = new PictureBox();
groupBoxStorage = new GroupBox();
listBoxStorages = new ListBox();
buttonDelObject = new Button();
textBoxStorageName = new TextBox();
buttonAddObject = new Button();
groupBoxTools.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
groupBoxStorage.SuspendLayout();
SuspendLayout();
//
// groupBoxTools
//
groupBoxTools.Controls.Add(button3);
groupBoxTools.Controls.Add(button2);
groupBoxTools.Controls.Add(buttonDeleteCruiser);
groupBoxTools.Controls.Add(textBoxNumber);
groupBoxTools.Controls.Add(buttonAddCruiser);
groupBoxTools.Location = new Point(645, 1);
groupBoxTools.Location = new Point(653, 1);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(154, 448);
groupBoxTools.Size = new Size(148, 140);
groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
//
// button3
// buttonDeleteCruiser
//
button3.Location = new Point(15, 171);
button3.Name = "button3";
button3.Size = new Size(122, 41);
button3.TabIndex = 3;
button3.Text = "Обновить коллекцию";
button3.UseVisualStyleBackColor = true;
button3.Click += ButtonRefreshCollection_Click;
//
// button2
//
button2.Location = new Point(15, 107);
button2.Name = "button2";
button2.Size = new Size(122, 37);
button2.TabIndex = 2;
button2.Text = "Удалить лайнер";
button2.UseVisualStyleBackColor = true;
button2.Click += ButtonRemoveCar_Click;
buttonDeleteCruiser.Location = new Point(15, 95);
buttonDeleteCruiser.Name = "buttonDeleteCruiser";
buttonDeleteCruiser.Size = new Size(122, 37);
buttonDeleteCruiser.TabIndex = 2;
buttonDeleteCruiser.Text = "Удалить лайнер";
buttonDeleteCruiser.UseVisualStyleBackColor = true;
buttonDeleteCruiser.Click += ButtonRemoveCar_Click;
//
// textBoxNumber
//
textBoxNumber.Location = new Point(15, 78);
textBoxNumber.Location = new Point(15, 66);
textBoxNumber.Name = "textBoxNumber";
textBoxNumber.Size = new Size(122, 23);
textBoxNumber.TabIndex = 1;
@ -88,19 +83,81 @@
buttonAddCruiser.UseVisualStyleBackColor = true;
buttonAddCruiser.Click += buttonAddCruiser_Click;
//
// buttonRefreshCollection
//
buttonRefreshCollection.Location = new Point(662, 397);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(131, 41);
buttonRefreshCollection.TabIndex = 3;
buttonRefreshCollection.Text = "Обновить коллекцию";
buttonRefreshCollection.UseVisualStyleBackColor = true;
buttonRefreshCollection.Click += ButtonRefreshCollection_Click;
//
// pictureBoxCollection
//
pictureBoxCollection.Location = new Point(-1, 1);
pictureBoxCollection.Location = new Point(14, 1);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(642, 448);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
// groupBoxStorage
//
groupBoxStorage.Controls.Add(listBoxStorages);
groupBoxStorage.Controls.Add(buttonDelObject);
groupBoxStorage.Controls.Add(textBoxStorageName);
groupBoxStorage.Controls.Add(buttonAddObject);
groupBoxStorage.Location = new Point(662, 139);
groupBoxStorage.Name = "groupBoxStorage";
groupBoxStorage.Size = new Size(136, 252);
groupBoxStorage.TabIndex = 2;
groupBoxStorage.TabStop = false;
groupBoxStorage.Text = "Наборы";
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(10, 117);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(120, 124);
listBoxStorages.TabIndex = 7;
listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
//
// buttonDelObject
//
buttonDelObject.Location = new Point(8, 86);
buttonDelObject.Name = "buttonDelObject";
buttonDelObject.Size = new Size(122, 25);
buttonDelObject.TabIndex = 2;
buttonDelObject.Text = "Удалить набор";
buttonDelObject.UseVisualStyleBackColor = true;
buttonDelObject.Click += ButtonDelObject_Click;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(8, 22);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(122, 23);
textBoxStorageName.TabIndex = 1;
//
// buttonAddObject
//
buttonAddObject.Location = new Point(8, 52);
buttonAddObject.Name = "buttonAddObject";
buttonAddObject.Size = new Size(122, 28);
buttonAddObject.TabIndex = 0;
buttonAddObject.Text = "Добавить набор";
buttonAddObject.UseVisualStyleBackColor = true;
buttonAddObject.Click += ButtonAddObject_Click;
//
// FormCruiserCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(buttonRefreshCollection);
Controls.Add(groupBoxStorage);
Controls.Add(pictureBoxCollection);
Controls.Add(groupBoxTools);
Name = "FormCruiserCollection";
@ -109,6 +166,8 @@
groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
groupBoxStorage.ResumeLayout(false);
groupBoxStorage.PerformLayout();
ResumeLayout(false);
}
@ -117,8 +176,13 @@
private GroupBox groupBoxTools;
private TextBox textBoxNumber;
private Button buttonAddCruiser;
private Button button3;
private Button button2;
private Button buttonRefreshCollection;
private Button buttonDeleteCruiser;
private PictureBox pictureBoxCollection;
private GroupBox groupBoxStorage;
private Button buttonDelObject;
private TextBox textBoxStorageName;
private Button buttonAddObject;
private ListBox listBoxStorages;
}
}

View File

@ -22,15 +22,80 @@ namespace Cruiser
/// <summary>
/// Набор объектов
/// </summary>
private readonly CarsGenericCollection<DrawingCruiser, DrawningObjectCar> _cruisers;
private readonly CruisersGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormCruiserCollection()
{
InitializeComponent();
_cruisers = new CarsGenericCollection<DrawingCruiser, DrawningObjectCar>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new CruisersGenericStorage(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 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>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBoxObjects_SelectedIndexChanged(object sender,
EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowCruiser();
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
@ -38,13 +103,22 @@ namespace Cruiser
/// <param name="e"></param>
private void buttonAddCruiser_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormCruiser form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_cruisers + form.SelectedCruiser != -1)
if (obj + form.SelectedCruiser)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _cruisers.ShowCruiser();
pictureBoxCollection.Image = obj.ShowCruiser();
}
else
{
@ -59,21 +133,25 @@ namespace Cruiser
/// <param name="e"></param>
private void ButtonRemoveCar_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)
{
return;
}
if (textBoxNumber.Text == "")
{
MessageBox.Show("Вы ничего не ввели");
return;
}
int pos = Convert.ToInt32(textBoxNumber.Text);
if (_cruisers - pos)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _cruisers.ShowCruiser();
pictureBoxCollection.Image = obj.ShowCruiser();
}
else
{
@ -87,7 +165,17 @@ namespace Cruiser
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _cruisers.ShowCruiser();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowCruiser();
}

View File

@ -56,11 +56,11 @@ namespace Cruiser.Generics
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static int operator +(CarsGenericCollection<T, U> collect, T? obj)
public static bool operator +(CarsGenericCollection<T, U> collect, T? obj)
{
if (obj == null)
{
return -1;
return false;
}
return collect._collection.Insert(obj);
}
@ -70,15 +70,14 @@ namespace Cruiser.Generics
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(CarsGenericCollection<T, U> collect, int pos)
public static T? operator -(CarsGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
return true;
}
return false;
return obj;
}
/// <summary>
/// Получение объекта IMoveableObject
@ -87,7 +86,7 @@ namespace Cruiser.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
@ -125,16 +124,18 @@ namespace Cruiser.Generics
{
int Ix = 0;
int Iy = 0;
for (int i = 0; i < _collection.Count - 1; i++)
int i = 0;
foreach (var cruiser in _collection.GetCruisers())
{
_collection.Get(i)?.SetPosition(Ix, Iy);
_collection.Get(i)?.DrawTransport(g);
_collection[i]?.SetPosition(Ix, Iy);
_collection[i]?.DrawTransport(g);
Ix += _placeSizeWidth;
if (Ix + _placeSizeHeight > _pictureWidth)
{
Ix = 0;
Iy = _placeSizeHeight;
}
}
i++;
}
}
}

View File

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.Drawing;
using Cruiser.MovementStrategy;
namespace Cruiser.Generics
{
internal class CruisersGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, CarsGenericCollection<DrawingCruiser,DrawningObjectCar>> _cruiserStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _cruiserStorages.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 CruisersGenericStorage(int pictureWidth, int pictureHeight)
{
_cruiserStorages = new Dictionary<string,CarsGenericCollection<DrawingCruiser, DrawningObjectCar>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (_cruiserStorages.ContainsKey(name)) return;
_cruiserStorages[name] = new CarsGenericCollection<DrawingCruiser, DrawningObjectCar>(_pictureWidth, _pictureHeight);
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (!_cruiserStorages.ContainsKey(name)) return;
_cruiserStorages.Remove(name);
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public CarsGenericCollection<DrawingCruiser, DrawningObjectCar>?
this[string ind]
{
get
{
if (_cruiserStorages.ContainsKey(ind)) return _cruiserStorages[ind];
return null;
}
}
}
}

View File

@ -15,43 +15,38 @@ namespace Cruiser.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>
/// Добавление объекта в набор
/// </summary>
/// <param name="cruiser">Добавляемый лайнер</param>
/// <returns></returns>
public int Insert(T cruiser)
public bool Insert(T cruiser)
{
int zero = 0;
for (; _places[zero] != null;)
if (_places.Count + 1 <= _maxCount)
{
zero++;
if (zero == _places.Length)
{
return -1;
}
_places.Insert(0, cruiser);
}
for (int i = zero; i > 0; i--)
{
_places[i] = _places[i - 1];
}
_places[0] = cruiser;
return 0;
return true;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
@ -60,7 +55,7 @@ namespace Cruiser.Generics
/// <returns></returns>
public bool Remove(int position)
{
if (position < 0 || position > _places.Length)
if (position < 0 || position > _places.Count)
{
return false;
}
@ -73,36 +68,41 @@ namespace Cruiser.Generics
/// <param name="cruiser">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T cruiser, int position)
public bool Insert(T cruiser, int position)
{
if (position < 0 || position > _places.Length)
if (_places.Count + 1 <= _maxCount && _places.Count >= position)
{
return 0;
_places.Insert(position, cruiser);
return true;
}
if (_places.Length != null)
return false;
}
public T? this[int position]
{
get
{
int zero = position;
while (_places[zero] != null)
if (position < 0 || position > _maxCount)
{ return null; }
return _places[position];
}
set
{
if (position < 0 || position > _maxCount)
return;
_places[position] = value;
}
}
public IEnumerable<T?> GetCruisers(int? maxCruisers = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxCruisers.HasValue && i == maxCruisers.Value)
{
zero++;
return -1;
yield break;
}
}
_places[position] = cruiser;
return position;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
{
if (position < 0 || position > _places.Length)
{
return null;
}
return _places[position];
}
}
}