Фулл лаба 4

This commit is contained in:
Marselchi 2023-11-24 12:49:48 +04:00
parent 47223fcbae
commit b7d80bf20f
8 changed files with 328 additions and 70 deletions

View File

@ -29,17 +29,24 @@
private void InitializeComponent()
{
groupBoxTools = new GroupBox();
Storages = new GroupBox();
listBoxStorages = new ListBox();
buttonDeleteSet = new Button();
textBoxStorageName = new TextBox();
buttonAddStorage = new Button();
buttonRefreshCollection = new Button();
buttonDeleteLiner = new Button();
textBoxNumber = new TextBox();
buttonAddLiner = new Button();
pictureBoxCollection = new PictureBox();
groupBoxTools.SuspendLayout();
Storages.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
// groupBoxTools
//
groupBoxTools.Controls.Add(Storages);
groupBoxTools.Controls.Add(buttonRefreshCollection);
groupBoxTools.Controls.Add(buttonDeleteLiner);
groupBoxTools.Controls.Add(textBoxNumber);
@ -51,42 +58,92 @@
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Tools";
//
// Storages
//
Storages.Controls.Add(listBoxStorages);
Storages.Controls.Add(buttonDeleteSet);
Storages.Controls.Add(textBoxStorageName);
Storages.Controls.Add(buttonAddStorage);
Storages.Location = new Point(7, 19);
Storages.Name = "Storages";
Storages.Size = new Size(200, 276);
Storages.TabIndex = 4;
Storages.TabStop = false;
Storages.Text = "Sets";
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 15;
listBoxStorages.Location = new Point(6, 113);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(187, 109);
listBoxStorages.TabIndex = 4;
listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
//
// buttonDeleteSet
//
buttonDeleteSet.Location = new Point(10, 228);
buttonDeleteSet.Name = "buttonDeleteSet";
buttonDeleteSet.Size = new Size(184, 30);
buttonDeleteSet.TabIndex = 3;
buttonDeleteSet.Text = "Delete Set";
buttonDeleteSet.UseVisualStyleBackColor = true;
buttonDeleteSet.Click += ButtonRemoveObject_Click;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(34, 39);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(139, 23);
textBoxStorageName.TabIndex = 2;
//
// buttonAddStorage
//
buttonAddStorage.Location = new Point(10, 68);
buttonAddStorage.Name = "buttonAddStorage";
buttonAddStorage.Size = new Size(184, 30);
buttonAddStorage.TabIndex = 1;
buttonAddStorage.Text = "Add Set";
buttonAddStorage.UseVisualStyleBackColor = true;
buttonAddStorage.Click += ButtonAddObject_Click;
//
// buttonRefreshCollection
//
buttonRefreshCollection.Location = new Point(11, 258);
buttonRefreshCollection.Location = new Point(11, 501);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(194, 40);
buttonRefreshCollection.TabIndex = 3;
buttonRefreshCollection.Text = "Refresh Collection";
buttonRefreshCollection.UseVisualStyleBackColor = true;
buttonRefreshCollection.Click += buttonRefreshCollection_Click;
buttonRefreshCollection.Click += ButtonRefreshCollection_Click;
//
// buttonDeleteLiner
//
buttonDeleteLiner.Location = new Point(11, 117);
buttonDeleteLiner.Location = new Point(11, 408);
buttonDeleteLiner.Name = "buttonDeleteLiner";
buttonDeleteLiner.Size = new Size(194, 40);
buttonDeleteLiner.TabIndex = 2;
buttonDeleteLiner.Text = "Delete Liner";
buttonDeleteLiner.UseVisualStyleBackColor = true;
buttonDeleteLiner.Click += buttonRemoveLiner_Click;
buttonDeleteLiner.Click += ButtonRemoveLiner_Click;
//
// textBoxNumber
//
textBoxNumber.Location = new Point(41, 77);
textBoxNumber.Location = new Point(41, 368);
textBoxNumber.Name = "textBoxNumber";
textBoxNumber.Size = new Size(139, 23);
textBoxNumber.TabIndex = 1;
//
// buttonAddLiner
//
buttonAddLiner.Location = new Point(11, 22);
buttonAddLiner.Location = new Point(11, 313);
buttonAddLiner.Name = "buttonAddLiner";
buttonAddLiner.Size = new Size(194, 40);
buttonAddLiner.TabIndex = 0;
buttonAddLiner.Text = "Add Liner";
buttonAddLiner.UseVisualStyleBackColor = true;
buttonAddLiner.Click += buttonAddLiner_Click;
buttonAddLiner.Click += ButtonAddLiner_Click;
//
// pictureBoxCollection
//
@ -107,6 +164,8 @@
Text = "Liner Collection";
groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout();
Storages.ResumeLayout(false);
Storages.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false);
}
@ -119,5 +178,10 @@
private Button buttonRefreshCollection;
private Button buttonDeleteLiner;
private PictureBox pictureBoxCollection;
private GroupBox Storages;
private ListBox listBoxStorages;
private Button buttonDeleteSet;
private TextBox textBoxStorageName;
private Button buttonAddStorage;
}
}

View File

@ -21,30 +21,39 @@ namespace Liner
/// <summary>
/// Набор объектов
/// </summary>
private readonly LinerGenericCollection<DrawingLiner, DrawingObjectLiner> _liners;
private readonly LinersGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormLinerCollection()
{
InitializeComponent();
_liners = new LinerGenericCollection<DrawingLiner,
DrawingObjectLiner>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new LinersGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAddLiner_Click(object sender, EventArgs e)
private void ButtonAddLiner_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
MainScreen form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_liners + form.SelectedLiner != -1)
if (obj + form.SelectedLiner != -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _liners.ShowLiners();
pictureBoxCollection.Image = obj.ShowLiners();
}
else
{
@ -57,18 +66,37 @@ namespace Liner
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonRemoveLiner_Click(object sender, EventArgs e)
private void ButtonRemoveLiner_Click(object sender, EventArgs e)
{
int pos;
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(textBoxNumber.Text);
if (_liners - pos)
if (textBoxNumber.Text != "")
{
pos = Convert.ToInt32(textBoxNumber.Text);
}
else
{
MessageBox.Show("Не удалось удалить объект");
return;
}
pos = 0;
if (obj - pos)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _liners.ShowLiners();
pictureBoxCollection.Image = obj.ShowLiners();
}
else
{
@ -76,13 +104,86 @@ namespace Liner
}
}
/// <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]?.ShowLiners();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemoveObject_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 buttonRefreshCollection_Click(object sender, EventArgs e)
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _liners.ShowLiners();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowLiners();
}
/// <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;
}
}
}
}

View File

@ -72,8 +72,7 @@ namespace Liner.Generics
/// <returns></returns>
public static bool operator -(LinerGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
if (obj != null)
if (collect._collection[pos] != null)
{
collect._collection.Remove(pos);
return true;
@ -87,7 +86,7 @@ namespace Liner.Generics
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
@ -97,8 +96,8 @@ namespace Liner.Generics
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawObjects(gr);
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
/// <summary>
@ -124,12 +123,12 @@ namespace Liner.Generics
{
int iX = ((_pictureWidth / _placeSizeWidth) - 1) * _placeSizeWidth;
int iY = 0;
for (int i = 0; i < _collection.Count; i++)
foreach (var liner in _collection.GetLiners())
{
_collection.Get(i)?.SetPosition(iX,iY);
_collection.Get(i)?.DrawTransport(g);
liner?.SetPosition(iX, iY);
liner?.DrawTransport(g);
iX -= _placeSizeWidth;
if(iX < 0)
if (iX < 0)
{
iX = ((_pictureWidth / _placeSizeWidth) - 1) * _placeSizeWidth;
iY += _placeSizeHeight;

View File

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

View File

@ -16,18 +16,23 @@ namespace Liner.Generics
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly T?[] _places;
private readonly List<T?> _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
public int Count => _places.Capacity;
/// <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>
/// Добавление объекта в набор
@ -36,22 +41,12 @@ namespace Liner.Generics
/// <returns></returns>
public int Insert(T liner)
{
int nulli = 0;
while (_places[nulli] != null)
if (_places.Count + 1 <= _maxCount)
{
nulli++;
if(nulli == _places.Length)
{
return -1; // нет пустого места
}
_places.Insert(0, liner);
return 0;
}
//сдвиг
for(int i = nulli; i > 0; i--)
{
_places[i] = _places[i - 1];
}
_places[0] = liner;
return 0;
return -1;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
@ -61,24 +56,12 @@ namespace Liner.Generics
/// <returns></returns>
public int Insert(T liner, int position)
{
if (position < 0 || position > _places.Length)
if (_places.Count + 1 <= _maxCount && _places.Count >= position)
{
return -1;
_places.Insert(position, liner);
return position;
}
if (_places[position] != null)
{
int nulli = position;
while (_places[nulli] != null)
{
nulli++;
if (nulli == _places.Length)
{
return -1; // нет пустого места
}
}
}
_places[position] = liner;
return position;
return -1;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
@ -87,25 +70,50 @@ namespace Liner.Generics
/// <returns></returns>
public bool Remove(int position)
{
if (position < 0 || position > _places.Length)
if(_places.Count >= position && position >= 0)
{
return false;
_places.RemoveAt(position);
return true;
}
_places[position] = null;
return true;
return false;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
public T? this[int position]
{
if (position < 0 || position > _places.Length)
get
{
if (_places.Count >= position && position >= 0)
{
return _places[position];
}
return null;
}
return _places[position];
set
{
if(_places.Count + 1 <= _maxCount && position >= 0 && _places.Count > position)
{
_places[position] = value;
}
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetLiners(int? maxLiners = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxLiners.HasValue && i == maxLiners.Value)
{
yield break;
}
}
}
}
}

View File

@ -2,7 +2,6 @@ using Liner.Drawing;
using Liner.Entities;
using Liner.MovingStrategies;
using System.Drawing;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
namespace Liner
{
@ -51,6 +50,10 @@ namespace Liner
Color mainColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
Color addColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
addColor = dialog.Color;
}
_drawingLiner = new DrawingLiner(random.Next(100, 300),
random.Next(1000, 3000), mainColor, addColor,
pictureBoxLiner.Width, pictureBoxLiner.Height);
@ -93,6 +96,10 @@ namespace Liner
{
addColor = dialog.Color;
}
if (dialog.ShowDialog() == DialogResult.OK)
{
mainColor = dialog.Color;
}
_drawingLiner = new DrawingBigLiner(random.Next(100, 300),
random.Next(1000, 3000), mainColor, addColor,
Convert.ToBoolean(random.Next(0, 2)),

View File

@ -35,5 +35,4 @@ namespace Liner.MovingStrategies
public void MoveObject(DirectionType direction) =>
_drawingLiner?.MoveTransport(direction);
}
}

View File

@ -14,4 +14,4 @@ namespace Liner
Application.Run(new FormLinerCollection());
}
}
}
}