1 Commits
lab4 ... lab3

7 changed files with 68 additions and 328 deletions

View File

@@ -29,24 +29,17 @@
private void InitializeComponent() private void InitializeComponent()
{ {
groupBoxTools = new GroupBox(); groupBoxTools = new GroupBox();
Storages = new GroupBox();
listBoxStorages = new ListBox();
buttonDeleteSet = new Button();
textBoxStorageName = new TextBox();
buttonAddStorage = new Button();
buttonRefreshCollection = new Button(); buttonRefreshCollection = new Button();
buttonDeleteLiner = new Button(); buttonDeleteLiner = new Button();
textBoxNumber = new TextBox(); textBoxNumber = new TextBox();
buttonAddLiner = new Button(); buttonAddLiner = new Button();
pictureBoxCollection = new PictureBox(); pictureBoxCollection = new PictureBox();
groupBoxTools.SuspendLayout(); groupBoxTools.SuspendLayout();
Storages.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// groupBoxTools // groupBoxTools
// //
groupBoxTools.Controls.Add(Storages);
groupBoxTools.Controls.Add(buttonRefreshCollection); groupBoxTools.Controls.Add(buttonRefreshCollection);
groupBoxTools.Controls.Add(buttonDeleteLiner); groupBoxTools.Controls.Add(buttonDeleteLiner);
groupBoxTools.Controls.Add(textBoxNumber); groupBoxTools.Controls.Add(textBoxNumber);
@@ -58,92 +51,42 @@
groupBoxTools.TabStop = false; groupBoxTools.TabStop = false;
groupBoxTools.Text = "Tools"; 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
// //
buttonRefreshCollection.Location = new Point(11, 501); buttonRefreshCollection.Location = new Point(11, 258);
buttonRefreshCollection.Name = "buttonRefreshCollection"; buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(194, 40); buttonRefreshCollection.Size = new Size(194, 40);
buttonRefreshCollection.TabIndex = 3; buttonRefreshCollection.TabIndex = 3;
buttonRefreshCollection.Text = "Refresh Collection"; buttonRefreshCollection.Text = "Refresh Collection";
buttonRefreshCollection.UseVisualStyleBackColor = true; buttonRefreshCollection.UseVisualStyleBackColor = true;
buttonRefreshCollection.Click += ButtonRefreshCollection_Click; buttonRefreshCollection.Click += buttonRefreshCollection_Click;
// //
// buttonDeleteLiner // buttonDeleteLiner
// //
buttonDeleteLiner.Location = new Point(11, 408); buttonDeleteLiner.Location = new Point(11, 117);
buttonDeleteLiner.Name = "buttonDeleteLiner"; buttonDeleteLiner.Name = "buttonDeleteLiner";
buttonDeleteLiner.Size = new Size(194, 40); buttonDeleteLiner.Size = new Size(194, 40);
buttonDeleteLiner.TabIndex = 2; buttonDeleteLiner.TabIndex = 2;
buttonDeleteLiner.Text = "Delete Liner"; buttonDeleteLiner.Text = "Delete Liner";
buttonDeleteLiner.UseVisualStyleBackColor = true; buttonDeleteLiner.UseVisualStyleBackColor = true;
buttonDeleteLiner.Click += ButtonRemoveLiner_Click; buttonDeleteLiner.Click += buttonRemoveLiner_Click;
// //
// textBoxNumber // textBoxNumber
// //
textBoxNumber.Location = new Point(41, 368); textBoxNumber.Location = new Point(41, 77);
textBoxNumber.Name = "textBoxNumber"; textBoxNumber.Name = "textBoxNumber";
textBoxNumber.Size = new Size(139, 23); textBoxNumber.Size = new Size(139, 23);
textBoxNumber.TabIndex = 1; textBoxNumber.TabIndex = 1;
// //
// buttonAddLiner // buttonAddLiner
// //
buttonAddLiner.Location = new Point(11, 313); buttonAddLiner.Location = new Point(11, 22);
buttonAddLiner.Name = "buttonAddLiner"; buttonAddLiner.Name = "buttonAddLiner";
buttonAddLiner.Size = new Size(194, 40); buttonAddLiner.Size = new Size(194, 40);
buttonAddLiner.TabIndex = 0; buttonAddLiner.TabIndex = 0;
buttonAddLiner.Text = "Add Liner"; buttonAddLiner.Text = "Add Liner";
buttonAddLiner.UseVisualStyleBackColor = true; buttonAddLiner.UseVisualStyleBackColor = true;
buttonAddLiner.Click += ButtonAddLiner_Click; buttonAddLiner.Click += buttonAddLiner_Click;
// //
// pictureBoxCollection // pictureBoxCollection
// //
@@ -164,8 +107,6 @@
Text = "Liner Collection"; Text = "Liner Collection";
groupBoxTools.ResumeLayout(false); groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout(); groupBoxTools.PerformLayout();
Storages.ResumeLayout(false);
Storages.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }
@@ -178,10 +119,5 @@
private Button buttonRefreshCollection; private Button buttonRefreshCollection;
private Button buttonDeleteLiner; private Button buttonDeleteLiner;
private PictureBox pictureBoxCollection; private PictureBox pictureBoxCollection;
private GroupBox Storages;
private ListBox listBoxStorages;
private Button buttonDeleteSet;
private TextBox textBoxStorageName;
private Button buttonAddStorage;
} }
} }

View File

@@ -21,39 +21,30 @@ namespace Liner
/// <summary> /// <summary>
/// Набор объектов /// Набор объектов
/// </summary> /// </summary>
private readonly LinersGenericStorage _storage; private readonly LinerGenericCollection<DrawingLiner, DrawingObjectLiner> _liners;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
public FormLinerCollection() public FormLinerCollection()
{ {
InitializeComponent(); InitializeComponent();
_storage = new LinersGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); _liners = new LinerGenericCollection<DrawingLiner,
DrawingObjectLiner>(pictureBoxCollection.Width, pictureBoxCollection.Height);
} }
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></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(); MainScreen form = new();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
if (obj + form.SelectedLiner != -1) if (_liners + form.SelectedLiner != -1)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = obj.ShowLiners(); pictureBoxCollection.Image = _liners.ShowLiners();
} }
else else
{ {
@@ -66,37 +57,18 @@ namespace Liner
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></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("Удалить объект?", "Удаление", if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{ {
return; return;
} }
if (textBoxNumber.Text != "") int pos = Convert.ToInt32(textBoxNumber.Text);
{ if (_liners - pos)
pos = Convert.ToInt32(textBoxNumber.Text);
}
else
{
MessageBox.Show("Не удалось удалить объект");
return;
}
pos = 0;
if (obj - pos)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBoxCollection.Image = obj.ShowLiners(); pictureBoxCollection.Image = _liners.ShowLiners();
} }
else else
{ {
@@ -104,86 +76,13 @@ namespace Liner
} }
} }
/// <summary> /// <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> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e) private void buttonRefreshCollection_Click(object sender, EventArgs e)
{ {
if (listBoxStorages.SelectedIndex == -1) pictureBoxCollection.Image = _liners.ShowLiners();
{
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,7 +72,8 @@ namespace Liner.Generics
/// <returns></returns> /// <returns></returns>
public static bool operator -(LinerGenericCollection<T, U> collect, int pos) public static bool operator -(LinerGenericCollection<T, U> collect, int pos)
{ {
if (collect._collection[pos] != null) T? obj = collect._collection.Get(pos);
if (obj != null)
{ {
collect._collection.Remove(pos); collect._collection.Remove(pos);
return true; return true;
@@ -86,7 +87,7 @@ namespace Liner.Generics
/// <returns></returns> /// <returns></returns>
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection[pos]?.GetMoveableObject; return (U?)_collection.Get(pos)?.GetMoveableObject;
} }
/// <summary> /// <summary>
/// Вывод всего набора объектов /// Вывод всего набора объектов
@@ -96,8 +97,8 @@ namespace Liner.Generics
{ {
Bitmap bmp = new(_pictureWidth, _pictureHeight); Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawObjects(gr); DrawObjects(gr);
DrawBackground(gr);
return bmp; return bmp;
} }
/// <summary> /// <summary>
@@ -123,12 +124,12 @@ namespace Liner.Generics
{ {
int iX = ((_pictureWidth / _placeSizeWidth) - 1) * _placeSizeWidth; int iX = ((_pictureWidth / _placeSizeWidth) - 1) * _placeSizeWidth;
int iY = 0; int iY = 0;
foreach (var liner in _collection.GetLiners()) for (int i = 0; i < _collection.Count; i++)
{ {
liner?.SetPosition(iX, iY); _collection.Get(i)?.SetPosition(iX,iY);
liner?.DrawTransport(g); _collection.Get(i)?.DrawTransport(g);
iX -= _placeSizeWidth; iX -= _placeSizeWidth;
if (iX < 0) if(iX < 0)
{ {
iX = ((_pictureWidth / _placeSizeWidth) - 1) * _placeSizeWidth; iX = ((_pictureWidth / _placeSizeWidth) - 1) * _placeSizeWidth;
iY += _placeSizeHeight; iY += _placeSizeHeight;

View File

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

@@ -50,10 +50,6 @@ namespace Liner
Color mainColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); 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)); Color addColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new(); ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
addColor = dialog.Color;
}
_drawingLiner = new DrawingLiner(random.Next(100, 300), _drawingLiner = new DrawingLiner(random.Next(100, 300),
random.Next(1000, 3000), mainColor, addColor, random.Next(1000, 3000), mainColor, addColor,
pictureBoxLiner.Width, pictureBoxLiner.Height); pictureBoxLiner.Width, pictureBoxLiner.Height);
@@ -96,10 +92,6 @@ namespace Liner
{ {
addColor = dialog.Color; addColor = dialog.Color;
} }
if (dialog.ShowDialog() == DialogResult.OK)
{
mainColor = dialog.Color;
}
_drawingLiner = new DrawingBigLiner(random.Next(100, 300), _drawingLiner = new DrawingBigLiner(random.Next(100, 300),
random.Next(1000, 3000), mainColor, addColor, random.Next(1000, 3000), mainColor, addColor,
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)),

View File

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