This commit is contained in:
Dasha 2023-12-02 00:46:08 +04:00
parent 83429b9e53
commit 339545f735
5 changed files with 370 additions and 92 deletions

View File

@ -29,49 +29,115 @@
private void InitializeComponent()
{
pictureBoxCollection = new PictureBox();
toolsLabel = new Panel();
label1 = new Label();
toolsPanel = new Panel();
LabelSets = new Label();
panelSets = new Panel();
ListBoxObjects = new ListBox();
textBoxStorageName = new TextBox();
ButtonDelObject = new Button();
ButtonAddObject = new Button();
LabelTools = new Label();
ButtonRefreshCollection = new Button();
ButtonDeleteShip = new Button();
maskedTextBoxNumber = new TextBox();
ButtonAddShip = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
toolsLabel.SuspendLayout();
toolsPanel.SuspendLayout();
panelSets.SuspendLayout();
SuspendLayout();
//
// pictureBoxCollection
//
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Location = new Point(-1, 0);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(655, 395);
pictureBoxCollection.Size = new Size(655, 507);
pictureBoxCollection.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxCollection.TabIndex = 0;
pictureBoxCollection.TabStop = false;
//
// toolsLabel
// toolsPanel
//
toolsLabel.Controls.Add(label1);
toolsLabel.Controls.Add(ButtonRefreshCollection);
toolsLabel.Controls.Add(ButtonDeleteShip);
toolsLabel.Controls.Add(maskedTextBoxNumber);
toolsLabel.Controls.Add(ButtonAddShip);
toolsLabel.Location = new Point(660, 12);
toolsLabel.Name = "toolsLabel";
toolsLabel.Size = new Size(212, 383);
toolsLabel.TabIndex = 0;
toolsPanel.Controls.Add(LabelSets);
toolsPanel.Controls.Add(panelSets);
toolsPanel.Controls.Add(LabelTools);
toolsPanel.Controls.Add(ButtonRefreshCollection);
toolsPanel.Controls.Add(ButtonDeleteShip);
toolsPanel.Controls.Add(maskedTextBoxNumber);
toolsPanel.Controls.Add(ButtonAddShip);
toolsPanel.Location = new Point(660, 12);
toolsPanel.Name = "toolsLabel";
toolsPanel.Size = new Size(230, 495);
toolsPanel.TabIndex = 0;
//
// label1
// LabelSets
//
label1.AutoSize = true;
label1.Location = new Point(58, -3);
label1.Name = "label1";
label1.Size = new Size(83, 15);
label1.TabIndex = 1;
label1.Text = "Инструменты";
LabelSets.AutoSize = true;
LabelSets.Location = new Point(89, 21);
LabelSets.Name = "LabelSets";
LabelSets.Size = new Size(52, 15);
LabelSets.TabIndex = 0;
LabelSets.Text = "Наборы";
//
// panelSets
//
panelSets.Controls.Add(ListBoxObjects);
panelSets.Controls.Add(textBoxStorageName);
panelSets.Controls.Add(ButtonDelObject);
panelSets.Controls.Add(ButtonAddObject);
panelSets.Location = new Point(17, 30);
panelSets.Name = "panelSets";
panelSets.Size = new Size(200, 225);
panelSets.TabIndex = 3;
//
// ListBoxObjects
//
ListBoxObjects.FormattingEnabled = true;
ListBoxObjects.ItemHeight = 15;
ListBoxObjects.Location = new Point(28, 76);
ListBoxObjects.Name = "ListBoxObjects";
ListBoxObjects.Size = new Size(142, 94);
ListBoxObjects.TabIndex = 3;
ListBoxObjects.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(28, 9);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(142, 23);
textBoxStorageName.TabIndex = 2;
//
// ButtonDelObject
//
ButtonDelObject.Location = new Point(28, 180);
ButtonDelObject.Name = "ButtonDelObject";
ButtonDelObject.Size = new Size(142, 33);
ButtonDelObject.TabIndex = 1;
ButtonDelObject.Text = "Удалить набор";
ButtonDelObject.UseVisualStyleBackColor = true;
ButtonDelObject.Click += ButtonDelObject_Click;
//
// ButtonAddObject
//
ButtonAddObject.Location = new Point(28, 38);
ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(142, 32);
ButtonAddObject.TabIndex = 0;
ButtonAddObject.Text = "Добавить набор";
ButtonAddObject.UseVisualStyleBackColor = true;
ButtonAddObject.Click += ButtonAddObject_Click;
//
// LabelTools
//
LabelTools.AutoSize = true;
LabelTools.Location = new Point(85, -3);
LabelTools.Name = "LabelTools";
LabelTools.Size = new Size(83, 15);
LabelTools.TabIndex = 1;
LabelTools.Text = "Инструменты";
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.Location = new Point(35, 187);
ButtonRefreshCollection.Location = new Point(45, 432);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(142, 37);
ButtonRefreshCollection.TabIndex = 2;
@ -81,7 +147,7 @@
//
// ButtonDeleteShip
//
ButtonDeleteShip.Location = new Point(35, 128);
ButtonDeleteShip.Location = new Point(45, 369);
ButtonDeleteShip.Name = "ButtonDeleteShip";
ButtonDeleteShip.Size = new Size(142, 37);
ButtonDeleteShip.TabIndex = 2;
@ -91,14 +157,14 @@
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Location = new Point(58, 83);
maskedTextBoxNumber.Location = new Point(68, 340);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(100, 23);
maskedTextBoxNumber.TabIndex = 2;
//
// ButtonAddShip
//
ButtonAddShip.Location = new Point(35, 31);
ButtonAddShip.Location = new Point(45, 274);
ButtonAddShip.Name = "ButtonAddShip";
ButtonAddShip.Size = new Size(142, 36);
ButtonAddShip.TabIndex = 2;
@ -110,14 +176,16 @@
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 395);
Controls.Add(toolsLabel);
ClientSize = new Size(889, 508);
Controls.Add(toolsPanel);
Controls.Add(pictureBoxCollection);
Name = "FormShipCollection";
Text = "Набор кораблей";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
toolsLabel.ResumeLayout(false);
toolsLabel.PerformLayout();
toolsPanel.ResumeLayout(false);
toolsPanel.PerformLayout();
panelSets.ResumeLayout(false);
panelSets.PerformLayout();
ResumeLayout(false);
PerformLayout();
}
@ -125,11 +193,17 @@
#endregion
private PictureBox pictureBoxCollection;
private Panel toolsLabel;
private Panel toolsPanel;
private Button ButtonAddShip;
private Label label1;
private Label LabelTools;
private TextBox maskedTextBoxNumber;
private Button ButtonDeleteShip;
private Button ButtonRefreshCollection;
private Label LabelSets;
private Panel panelSets;
private ListBox ListBoxObjects;
private TextBox textBoxStorageName;
private Button ButtonDelObject;
private Button ButtonAddObject;
}
}

View File

@ -21,7 +21,7 @@ namespace WarmlyShip
/// <summary>
/// Набор объектов
/// </summary>
private readonly ShipsGenericCollection<DrawningShip, DrawningObjectShip> _theShips;
private readonly ShipsGenericStorage _storage;
/// <summary>
/// Конструктор
@ -29,8 +29,77 @@ namespace WarmlyShip
public FormShipCollection()
{
InitializeComponent();
_theShips = new ShipsGenericCollection<DrawningShip, DrawningObjectShip>
(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new ShipsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
/// <summary>
/// Заполнение ListBoxObjects
/// </summary>
private void ReloadObjects()
{
int index = ListBoxObjects.SelectedIndex;
ListBoxObjects.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
ListBoxObjects.Items.Add(_storage.Keys[i]);
}
if (ListBoxObjects.Items.Count > 0 && (index == -1 || index >= ListBoxObjects.Items.Count))
{
ListBoxObjects.SelectedIndex = 0;
}
else if (ListBoxObjects.Items.Count > 0 && index > -1 && index < ListBoxObjects.Items.Count)
{
ListBoxObjects.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[ListBoxObjects.SelectedItem?.ToString() ?? string.Empty]?.ShowShips();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (ListBoxObjects.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {ListBoxObjects.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(ListBoxObjects.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
}
/// <summary>
@ -40,13 +109,22 @@ namespace WarmlyShip
/// <param name="e"></param>
private void ButtonAddShip_Click(object sender, EventArgs e)
{
if (ListBoxObjects.SelectedIndex == -1)
{
return;
}
var obj = _storage[ListBoxObjects.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormWarmlyShip form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_theShips + form.SelectedShip != -1)
if (obj + form.SelectedShip)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _theShips.ShowShips();
pictureBoxCollection.Image = obj.ShowShips();
}
else
{
@ -62,17 +140,26 @@ namespace WarmlyShip
/// <param name="e"></param>
private void ButtonDeleteShip_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
if (ListBoxObjects.SelectedIndex == -1)
{
return;
}
var obj = _storage[ListBoxObjects.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 (_theShips - pos != true)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _theShips.ShowShips();
pictureBoxCollection.Image = obj.ShowShips();
}
else
{
@ -87,7 +174,17 @@ namespace WarmlyShip
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _theShips.ShowShips();
if (ListBoxObjects.SelectedIndex == -1)
{
return;
}
var obj = _storage[ListBoxObjects.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowShips();
}
}
}

View File

@ -14,22 +14,28 @@ namespace WarmlyShip.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>
/// Максимальное количество объектов в cписке
/// </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>
@ -37,9 +43,15 @@ namespace WarmlyShip.Generics
/// </summary>
/// <param name="ship">Добавляемый корабль</param>
/// <returns></returns>
public int Insert(T ship)
public bool Insert(T ship)
{
return Insert(ship, 0);
if (_places.Count == _maxCount)
{
return false;
}
Insert(ship, 0);
return true;
}
/// <summary>
@ -48,36 +60,15 @@ namespace WarmlyShip.Generics
/// <param name="ship">Добавляемый корабль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T ship, int position)
public bool Insert(T ship, int position)
{
int nullIndex = -1, i;
if (position < 0 || position >= Count)
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
{
return -1;
return false;
}
for (i = position; i < Count; i++)
{
if (_places[i] == null)
{
nullIndex = i;
break;
}
}
if (nullIndex < 0)
{
return -1;
}
for (i = nullIndex; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = ship;
return position;
_places.Insert(position, ship);
return true;
}
/// <summary>
@ -92,7 +83,7 @@ namespace WarmlyShip.Generics
return false;
}
_places[position] = null;
_places.RemoveAt(position);
return true;
}
@ -101,14 +92,42 @@ namespace WarmlyShip.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 >= _maxCount)
{
return null;
}
return _places[position];
}
set
{
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
{
return;
}
return _places[position];
_places.Insert(position, value);
return;
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetShips(int? maxShips = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxShips.HasValue && i == maxShips.Value)
{
yield break;
}
}
}
}
}

View File

@ -63,13 +63,13 @@ namespace WarmlyShip.Generics
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static int operator +(ShipsGenericCollection<T, U> collect, T? obj)
public static bool operator +(ShipsGenericCollection<T, U> collect, T? obj)
{
if (obj == null)
{
return -1;
return false;
}
return collect._collection.Insert(obj);
return collect?._collection.Insert(obj) ?? false;
}
/// <summary>
@ -78,15 +78,14 @@ namespace WarmlyShip.Generics
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(ShipsGenericCollection<T, U> collect, int
pos)
public static T? operator -(ShipsGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
return false;
return obj;
}
/// <summary>
@ -96,7 +95,7 @@ namespace WarmlyShip.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
@ -137,16 +136,16 @@ namespace WarmlyShip.Generics
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
int i = 0;
foreach (var ship in _collection.GetShips())
{
DrawningShip ship = _collection.Get(i);
if (ship != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
ship.SetPosition((inRow - 1 - (i % inRow)) * _placeSizeWidth, i / inRow * _placeSizeHeight);
ship.DrawTransport(g);
}
i++;
}
}
}

View File

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