ЛабаЧетыре

This commit is contained in:
Леонид Малафеев 2023-11-24 11:41:50 +04:00
parent e7b9367168
commit b2b419c6bb
5 changed files with 323 additions and 94 deletions

View File

@ -29,51 +29,46 @@
private void InitializeComponent() private void InitializeComponent()
{ {
groupBoxTools = new GroupBox(); groupBoxTools = new GroupBox();
button3 = new Button(); buttonDeleteCruiser = new Button();
button2 = new Button();
textBoxNumber = new TextBox(); textBoxNumber = new TextBox();
buttonAddCruiser = new Button(); buttonAddCruiser = new Button();
buttonRefreshCollection = new Button();
pictureBoxCollection = new PictureBox(); pictureBoxCollection = new PictureBox();
groupBoxStorage = new GroupBox();
listBoxStorages = new ListBox();
buttonDelObject = new Button();
textBoxStorageName = new TextBox();
buttonAddObject = new Button();
groupBoxTools.SuspendLayout(); groupBoxTools.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
groupBoxStorage.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
// groupBoxTools // groupBoxTools
// //
groupBoxTools.Controls.Add(button3); groupBoxTools.Controls.Add(buttonDeleteCruiser);
groupBoxTools.Controls.Add(button2);
groupBoxTools.Controls.Add(textBoxNumber); groupBoxTools.Controls.Add(textBoxNumber);
groupBoxTools.Controls.Add(buttonAddCruiser); groupBoxTools.Controls.Add(buttonAddCruiser);
groupBoxTools.Location = new Point(645, 1); groupBoxTools.Location = new Point(653, 1);
groupBoxTools.Name = "groupBoxTools"; groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(154, 448); groupBoxTools.Size = new Size(148, 140);
groupBoxTools.TabIndex = 0; groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false; groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты"; groupBoxTools.Text = "Инструменты";
// //
// button3 // buttonDeleteCruiser
// //
button3.Location = new Point(15, 171); buttonDeleteCruiser.Location = new Point(15, 95);
button3.Name = "button3"; buttonDeleteCruiser.Name = "buttonDeleteCruiser";
button3.Size = new Size(122, 41); buttonDeleteCruiser.Size = new Size(122, 37);
button3.TabIndex = 3; buttonDeleteCruiser.TabIndex = 2;
button3.Text = "Обновить коллекцию"; buttonDeleteCruiser.Text = "Удалить лайнер";
button3.UseVisualStyleBackColor = true; buttonDeleteCruiser.UseVisualStyleBackColor = true;
button3.Click += ButtonRefreshCollection_Click; buttonDeleteCruiser.Click += ButtonRemoveCar_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;
// //
// textBoxNumber // textBoxNumber
// //
textBoxNumber.Location = new Point(15, 78); textBoxNumber.Location = new Point(15, 66);
textBoxNumber.Name = "textBoxNumber"; textBoxNumber.Name = "textBoxNumber";
textBoxNumber.Size = new Size(122, 23); textBoxNumber.Size = new Size(122, 23);
textBoxNumber.TabIndex = 1; textBoxNumber.TabIndex = 1;
@ -88,19 +83,81 @@
buttonAddCruiser.UseVisualStyleBackColor = true; buttonAddCruiser.UseVisualStyleBackColor = true;
buttonAddCruiser.Click += buttonAddCruiser_Click; 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
// //
pictureBoxCollection.Location = new Point(-1, 1); pictureBoxCollection.Location = new Point(14, 1);
pictureBoxCollection.Name = "pictureBoxCollection"; pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(642, 448); pictureBoxCollection.Size = new Size(642, 448);
pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false; 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 // FormCruiserCollection
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450); ClientSize = new Size(800, 450);
Controls.Add(buttonRefreshCollection);
Controls.Add(groupBoxStorage);
Controls.Add(pictureBoxCollection); Controls.Add(pictureBoxCollection);
Controls.Add(groupBoxTools); Controls.Add(groupBoxTools);
Name = "FormCruiserCollection"; Name = "FormCruiserCollection";
@ -109,6 +166,8 @@
groupBoxTools.ResumeLayout(false); groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout(); groupBoxTools.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
groupBoxStorage.ResumeLayout(false);
groupBoxStorage.PerformLayout();
ResumeLayout(false); ResumeLayout(false);
} }
@ -117,8 +176,14 @@
private GroupBox groupBoxTools; private GroupBox groupBoxTools;
private TextBox textBoxNumber; private TextBox textBoxNumber;
private Button buttonAddCruiser; private Button buttonAddCruiser;
private Button button3; private Button buttonRefreshCollection;
private Button button2; private Button buttonDeleteCruiser;
private PictureBox pictureBoxCollection; private PictureBox pictureBoxCollection;
private GroupBox groupBoxStorage;
private Button button1;
private Button buttonDelObject;
private TextBox textBoxStorageName;
private Button buttonAddObject;
private ListBox listBoxStorages;
} }
} }

View File

@ -22,15 +22,80 @@ namespace Cruiser
/// <summary> /// <summary>
/// Набор объектов /// Набор объектов
/// </summary> /// </summary>
private readonly CarsGenericCollection<DrawingCruiser, DrawningObjectCar> _cruisers; private readonly CruisersGenericStorage _storage;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
public FormCruiserCollection() public FormCruiserCollection()
{ {
InitializeComponent(); 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>
/// Добавление объекта в набор /// Добавление объекта в набор
/// </summary> /// </summary>
@ -38,13 +103,22 @@ namespace Cruiser
/// <param name="e"></param> /// <param name="e"></param>
private void buttonAddCruiser_Click(object sender, EventArgs e) 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(); FormCruiser form = new();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
if (_cruisers + form.SelectedCruiser != -1) if (obj + form.SelectedCruiser)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _cruisers.ShowCruiser(); pictureBoxCollection.Image = obj.ShowCruiser();
} }
else else
{ {
@ -59,21 +133,25 @@ namespace Cruiser
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonRemoveCar_Click(object sender, EventArgs e) 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("Удалить объект?", "Удаление", if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{ {
return; return;
} }
if (textBoxNumber.Text == "")
{
MessageBox.Show("Вы ничего не ввели");
return;
}
int pos = Convert.ToInt32(textBoxNumber.Text); int pos = Convert.ToInt32(textBoxNumber.Text);
if (_cruisers - pos) if (obj - pos != null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _cruisers.ShowCruiser(); pictureBoxCollection.Image = obj.ShowCruiser();
} }
else else
{ {
@ -87,7 +165,17 @@ namespace Cruiser
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e) 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="collect"></param>
/// <param name="obj"></param> /// <param name="obj"></param>
/// <returns></returns> /// <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) if (obj == null)
{ {
return -1; return false;
} }
return collect._collection.Insert(obj); return collect._collection.Insert(obj);
} }
@ -70,15 +70,14 @@ namespace Cruiser.Generics
/// <param name="collect"></param> /// <param name="collect"></param>
/// <param name="pos"></param> /// <param name="pos"></param>
/// <returns></returns> /// <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) if (obj != null)
{ {
collect._collection.Remove(pos); collect._collection.Remove(pos);
return true;
} }
return false; return obj;
} }
/// <summary> /// <summary>
/// Получение объекта IMoveableObject /// Получение объекта IMoveableObject
@ -87,7 +86,7 @@ namespace Cruiser.Generics
/// <returns></returns> /// <returns></returns>
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection.Get(pos)?.GetMoveableObject; return (U?)_collection[pos]?.GetMoveableObject;
} }
/// <summary> /// <summary>
/// Вывод всего набора объектов /// Вывод всего набора объектов
@ -125,16 +124,18 @@ namespace Cruiser.Generics
{ {
int Ix = 0; int Ix = 0;
int Iy = 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[i]?.SetPosition(Ix, Iy);
_collection.Get(i)?.DrawTransport(g); _collection[i]?.DrawTransport(g);
Ix += _placeSizeWidth; Ix += _placeSizeWidth;
if (Ix + _placeSizeHeight > _pictureWidth) if (Ix + _placeSizeHeight > _pictureWidth)
{ {
Ix = 0; Ix = 0;
Iy = _placeSizeHeight; 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 where T : class
{ {
/// <summary> /// <summary>
/// Массив объектов, которые храним /// Список объектов, которые храним
/// </summary> /// </summary>
private readonly T?[] _places; private readonly List<T?> _places;
/// <summary> /// <summary>
/// Количество объектов в массиве /// Количество объектов в списке
/// </summary> /// </summary>
public int Count => _places.Length; public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в списке
/// </summary>
private readonly int _maxCount;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
/// <param name="count"></param> /// <param name="count"></param>
public SetGeneric(int count) public SetGeneric(int count)
{ {
_places = new T?[count]; _maxCount = count;
_places = new List<T?>(count);
} }
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
/// </summary> /// </summary>
/// <param name="cruiser">Добавляемый лайнер</param> /// <param name="cruiser">Добавляемый лайнер</param>
/// <returns></returns> /// <returns></returns>
public int Insert(T cruiser) public bool Insert(T cruiser)
{ {
int zero = 0; if (_places.Count + 1 <= _maxCount)
for (; _places[zero] != null;)
{ {
zero++; _places.Insert(0, cruiser);
if (zero == _places.Length)
{
return -1;
}
} }
for (int i = zero; i > 0; i--) return true;
{
_places[i] = _places[i - 1];
}
_places[0] = cruiser;
return 0;
} }
/// <summary> /// <summary>
/// Удаление объекта из набора с конкретной позиции /// Удаление объекта из набора с конкретной позиции
@ -60,7 +55,7 @@ namespace Cruiser.Generics
/// <returns></returns> /// <returns></returns>
public bool Remove(int position) public bool Remove(int position)
{ {
if (position < 0 || position > _places.Length) if (position < 0 || position > _places.Count)
{ {
return false; return false;
} }
@ -73,36 +68,41 @@ namespace Cruiser.Generics
/// <param name="cruiser">Добавляемый автомобиль</param> /// <param name="cruiser">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param> /// <param name="position">Позиция</param>
/// <returns></returns> /// <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; if (position < 0 || position > _maxCount)
while (_places[zero] != null) { 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++; yield break;
return -1;
} }
} }
_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];
} }
} }
} }