ISEbd21 Zinovev V.V. Lab Work 04 #5

Closed
vladimir_zinovev wants to merge 6 commits from TankBasicLabFourth into TankBasicLabThird
6 changed files with 338 additions and 82 deletions

View File

@ -30,6 +30,11 @@
{
this.components = new System.ComponentModel.Container();
this.ArmoredTransportCollectionFormToolsGroup = new System.Windows.Forms.GroupBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.textBoxStorageName = new System.Windows.Forms.TextBox();
this.listBoxStorages = new System.Windows.Forms.ListBox();
this.buttonDelObject = new System.Windows.Forms.Button();
this.buttonAddObject = new System.Windows.Forms.Button();
this.ButtonRefreshCollection = new System.Windows.Forms.Button();
this.ButtonRemoveArmoredTransport = new System.Windows.Forms.Button();
this.maskedTextBoxNumber = new System.Windows.Forms.TextBox();
@ -37,6 +42,7 @@
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.ArmoredTransportCollectionFormToolsGroup.SuspendLayout();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.SuspendLayout();
//
@ -44,6 +50,7 @@
//
this.ArmoredTransportCollectionFormToolsGroup.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Right)));
this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.groupBox1);
this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.ButtonRefreshCollection);
this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.ButtonRemoveArmoredTransport);
this.ArmoredTransportCollectionFormToolsGroup.Controls.Add(this.maskedTextBoxNumber);
@ -55,10 +62,65 @@
this.ArmoredTransportCollectionFormToolsGroup.TabStop = false;
this.ArmoredTransportCollectionFormToolsGroup.Text = "Инструменты";
//
// groupBox1
//
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.groupBox1.Controls.Add(this.textBoxStorageName);
this.groupBox1.Controls.Add(this.listBoxStorages);
this.groupBox1.Controls.Add(this.buttonDelObject);
this.groupBox1.Controls.Add(this.buttonAddObject);
this.groupBox1.Location = new System.Drawing.Point(22, 38);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(391, 434);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Наборы";
//
// textBoxStorageName
//
this.textBoxStorageName.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.textBoxStorageName.Location = new System.Drawing.Point(19, 38);
this.textBoxStorageName.Name = "textBoxStorageName";
this.textBoxStorageName.Size = new System.Drawing.Size(353, 39);
this.textBoxStorageName.TabIndex = 3;
//
// listBoxStorages
//
this.listBoxStorages.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.listBoxStorages.FormattingEnabled = true;
this.listBoxStorages.ItemHeight = 32;
this.listBoxStorages.Location = new System.Drawing.Point(19, 165);
this.listBoxStorages.Name = "listBoxStorages";
this.listBoxStorages.Size = new System.Drawing.Size(353, 196);
this.listBoxStorages.TabIndex = 2;
this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged);
Outdated
Review

Выбрно неверное событие у listBox

Выбрно неверное событие у listBox
//
// buttonDelObject
//
this.buttonDelObject.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDelObject.Location = new System.Drawing.Point(19, 374);
this.buttonDelObject.Name = "buttonDelObject";
this.buttonDelObject.Size = new System.Drawing.Size(353, 46);
this.buttonDelObject.TabIndex = 1;
this.buttonDelObject.Text = "Удалить набор";
this.buttonDelObject.UseVisualStyleBackColor = true;
this.buttonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click);
//
// buttonAddObject
//
this.buttonAddObject.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonAddObject.Location = new System.Drawing.Point(19, 98);
this.buttonAddObject.Name = "buttonAddObject";
this.buttonAddObject.Size = new System.Drawing.Size(353, 46);
this.buttonAddObject.TabIndex = 0;
this.buttonAddObject.Text = "Добавить набор";
this.buttonAddObject.UseVisualStyleBackColor = true;
this.buttonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click);
//
// ButtonRefreshCollection
//
this.ButtonRefreshCollection.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.ButtonRefreshCollection.Location = new System.Drawing.Point(22, 394);
this.ButtonRefreshCollection.Location = new System.Drawing.Point(22, 773);
this.ButtonRefreshCollection.Name = "ButtonRefreshCollection";
this.ButtonRefreshCollection.Size = new System.Drawing.Size(391, 47);
this.ButtonRefreshCollection.TabIndex = 3;
@ -69,7 +131,7 @@
// ButtonRemoveArmoredTransport
//
this.ButtonRemoveArmoredTransport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.ButtonRemoveArmoredTransport.Location = new System.Drawing.Point(22, 251);
this.ButtonRemoveArmoredTransport.Location = new System.Drawing.Point(22, 641);
this.ButtonRemoveArmoredTransport.Name = "ButtonRemoveArmoredTransport";
this.ButtonRemoveArmoredTransport.Size = new System.Drawing.Size(391, 82);
this.ButtonRemoveArmoredTransport.TabIndex = 2;
@ -80,15 +142,17 @@
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.maskedTextBoxNumber.Location = new System.Drawing.Point(71, 188);
this.maskedTextBoxNumber.Location = new System.Drawing.Point(71, 589);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(275, 39);
this.maskedTextBoxNumber.TabIndex = 1;
this.maskedTextBoxNumber.Tag = "";
this.maskedTextBoxNumber.Text = "_";
//
// ButtonAddArmoredTransport
//
this.ButtonAddArmoredTransport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.ButtonAddArmoredTransport.Location = new System.Drawing.Point(22, 88);
this.ButtonAddArmoredTransport.Location = new System.Drawing.Point(22, 492);
this.ButtonAddArmoredTransport.Name = "ButtonAddArmoredTransport";
this.ButtonAddArmoredTransport.Size = new System.Drawing.Size(391, 82);
this.ButtonAddArmoredTransport.TabIndex = 0;
@ -123,6 +187,8 @@
this.Text = "ArmoredTransportCollectionForm";
this.ArmoredTransportCollectionFormToolsGroup.ResumeLayout(false);
this.ArmoredTransportCollectionFormToolsGroup.PerformLayout();
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
@ -138,5 +204,10 @@
private Button ButtonAddArmoredTransport;
private ContextMenuStrip contextMenuStrip1;
private PictureBox pictureBoxCollection;
private GroupBox groupBox1;
private TextBox textBoxStorageName;
private ListBox listBoxStorages;
private Button buttonDelObject;
private Button buttonAddObject;
}
}

View File

@ -1,34 +1,100 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectTank.DrawningObjects;
using ProjectTank.DrawningObjects;
using ProjectTank.Generics;
using ProjectTank.MovementStrategy;
namespace ProjectTank
{
/// <summary>
/// Форма для работы с набором объектов класса DrawningCar
/// Форма для работы с набором объектов класса DrawningArmoredTransport
/// </summary>
public partial class ArmoredTransportCollectionForm : Form
{
/// <summary>
/// Набор объектов
/// </summary>
private readonly ArmoredTransportsGenericCollection<DrawningArmoredTransport, DrawningObjectArmoredTransport> _armoredTransports;
private readonly ArmoredTransportsGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public ArmoredTransportCollectionForm()
{
InitializeComponent();
_armoredTransports = new ArmoredTransportsGenericCollection<DrawningArmoredTransport,DrawningObjectArmoredTransport>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new ArmoredTransportsGenericStorage(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]?.ShowArmoredTransports();
}
/// <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();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowArmoredTransports();
}
}
/// <summary>
/// Добавление объекта в набор
@ -37,13 +103,22 @@ namespace ProjectTank
/// <param name="e"></param>
private void ButtonAddArmoredTransport_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString()??string.Empty];
if (obj == null)
{
return;
}
TankForm form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_armoredTransports + form.SelectedArmoredTransport != null)
if (obj + form.SelectedArmoredTransport)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _armoredTransports.ShowArmoredTransports();
pictureBoxCollection.Image = obj.ShowArmoredTransports();
}
else
{
@ -58,16 +133,25 @@ namespace ProjectTank
/// <param name="e"></param>
private void ButtonRemoveArmoredTransport_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
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;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_armoredTransports - pos != null)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _armoredTransports.ShowArmoredTransports();
pictureBoxCollection.Image = obj.ShowArmoredTransports();
}
else
{
@ -79,10 +163,18 @@ namespace ProjectTank
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs
e)
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _armoredTransports.ShowArmoredTransports();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowArmoredTransports();
}
}
}

View File

@ -56,13 +56,13 @@ namespace ProjectTank.Generics
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static int operator +(ArmoredTransportsGenericCollection<T, U> collect, T? obj)
public static bool operator +(ArmoredTransportsGenericCollection<T, U> collect, T? obj)
{
if (obj == null || collect == null)
{
return -1;
return false;
}
return collect._collection.Insert(obj);
return collect?._collection.Insert(obj)??false;
}
/// <summary>
/// Перегрузка оператора вычитания
@ -70,14 +70,14 @@ namespace ProjectTank.Generics
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(ArmoredTransportsGenericCollection<T, U> collect, int pos)
public static T? operator -(ArmoredTransportsGenericCollection<T, U> collect, int pos)
{
T? obj = collect?._collection.Get(pos);
if (obj != null && collect != null)
T? obj = collect._collection[pos];
if (obj != null)
{
return collect._collection.Remove(pos);
collect._collection.Remove(pos);
}
return false;
return obj;
}
/// <summary>
/// Получение объекта IMoveableObject
@ -86,7 +86,7 @@ namespace ProjectTank.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
@ -119,7 +119,6 @@ namespace ProjectTank.Generics
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth,
_pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
/// <summary>
@ -128,18 +127,19 @@ namespace ProjectTank.Generics
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
int width = (_pictureWidth / _placeSizeWidth);
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++)
int width = (_pictureWidth / _placeSizeWidth);
int height = _pictureHeight / _placeSizeHeight;
int i = 0;
foreach (var armoredTransport in _collection.GetArmoredTransports())
{
DrawningArmoredTransport? armoredTransport = _collection.Get(i);
if (armoredTransport != null)
{
int row = height - 1 - (i / width);
int col = width - 1 - (i % width);
armoredTransport.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight);
armoredTransport.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight);
armoredTransport.DrawTransport(g);
}
i++;
}
}
}

View File

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

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace ProjectTank.MovementStrategy
{
/// <summary>
/// Стратегия перемещения объекта в центр экрана
/// Стратегия перемещения объекта в центр экранa
/// </summary>
public class MoveToCenter : AbstractStrategy
{

View File

@ -6,67 +6,59 @@ using System.Threading.Tasks;
namespace ProjectTank.Generics
{
/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T"></typeparam>
internal class SetGeneric<T>
where T : class
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="armoredTransport">Добавляемый автомобиль</param>
/// <param name="armoredTransport">Добавляемый броне-транспорт</param>
/// <returns></returns>
public int Insert(T armoredTransport)
public bool Insert(T armoredTransport)
{
if (_places[Count - 1] != null)
{
return -1;
}
else
{
return Insert(armoredTransport,0);
}
if (_places.Count == _maxCount)
return false;
Insert(armoredTransport, 0);
return true;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="car">Добавляемый броне-транспорт</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T armoredTransport, int position)
public bool Insert(T armoredTransport, 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] = armoredTransport;
return position;
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
return false;
_places.Insert(position, armoredTransport);
return true;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
@ -75,11 +67,9 @@ 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;
}
/// <summary>
@ -87,13 +77,36 @@ namespace ProjectTank.Generics
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
public T? this[int position]
{
if (!(position >= 0 && position < Count))
get
{
return null;
if (!(position >= 0 && position < Count))
return null;
return _places[position];
}
set
{
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
return;
_places.Insert(position, value);
return;
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetArmoredTransports(int? maxArmoredTransport = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxArmoredTransport.HasValue && i == maxArmoredTransport.Value)
{
yield break;
}
}
return _places[position];
}
}
}
}