ISBd_21.GordeevI.V._LabWork_04 #8

Closed
Igor_Gordeev wants to merge 3 commits from ISBd_21.GordeevI.V._LabWork_04 into ISBd_21.GordeevI.V._LabWork_03
6 changed files with 307 additions and 88 deletions

View File

@ -207,7 +207,6 @@ namespace ElectricLocomotive.DrawningObject
DirectionType.Down => _startPosY + EntityLocomotive.Step < _pictureHeight,
};
}
public IMoveableObject GetMoveableObject => new DrawningObjectLocomotive(this);
}
}

View File

@ -30,77 +30,134 @@
{
CollectionPictureBox = new PictureBox();
ToolsBox = new GroupBox();
NaborGroupBox = new GroupBox();
ButtomRemoveNabor = new Button();
listBoxStorage = new ListBox();
AddNabor = new Button();
textBoxStorageName = new TextBox();
Input = new TextBox();
RefreshCollection = new Button();
DelLocomotive = new Button();
AddLocomotive = new Button();
((System.ComponentModel.ISupportInitialize)CollectionPictureBox).BeginInit();
ToolsBox.SuspendLayout();
NaborGroupBox.SuspendLayout();
SuspendLayout();
//
// CollectionPictureBox
//
CollectionPictureBox.Location = new Point(3, 0);
CollectionPictureBox.Name = "CollectionPictureBox";
CollectionPictureBox.Size = new Size(634, 457);
CollectionPictureBox.Size = new Size(696, 501);
CollectionPictureBox.TabIndex = 0;
CollectionPictureBox.TabStop = false;
//
// ToolsBox
//
ToolsBox.Controls.Add(NaborGroupBox);
ToolsBox.Controls.Add(Input);
ToolsBox.Controls.Add(RefreshCollection);
ToolsBox.Controls.Add(DelLocomotive);
ToolsBox.Controls.Add(AddLocomotive);
ToolsBox.Location = new Point(643, 0);
ToolsBox.Location = new Point(705, 12);
ToolsBox.Name = "ToolsBox";
ToolsBox.Size = new Size(200, 457);
ToolsBox.Size = new Size(200, 489);
ToolsBox.TabIndex = 1;
ToolsBox.TabStop = false;
ToolsBox.Text = "Инструменты";
//
// NaborGroupBox
//
NaborGroupBox.Controls.Add(ButtomRemoveNabor);
NaborGroupBox.Controls.Add(listBoxStorage);
NaborGroupBox.Controls.Add(AddNabor);
NaborGroupBox.Controls.Add(textBoxStorageName);
NaborGroupBox.Location = new Point(7, 22);
NaborGroupBox.Name = "NaborGroupBox";
NaborGroupBox.Size = new Size(185, 274);
NaborGroupBox.TabIndex = 4;
NaborGroupBox.TabStop = false;
NaborGroupBox.Text = "Наборы";
//
// ButtomRemoveNabor
//
ButtomRemoveNabor.Location = new Point(6, 212);
ButtomRemoveNabor.Name = "ButtomRemoveNabor";
ButtomRemoveNabor.Size = new Size(173, 48);
ButtomRemoveNabor.TabIndex = 3;
ButtomRemoveNabor.Text = "Удалить набор";
ButtomRemoveNabor.UseVisualStyleBackColor = true;
ButtomRemoveNabor.Click += ButtonRemoveObject_Click;
//
// listBoxStorage
//
listBoxStorage.FormattingEnabled = true;
listBoxStorage.ItemHeight = 15;
listBoxStorage.Location = new Point(6, 112);
listBoxStorage.Name = "listBoxStorage";
listBoxStorage.Size = new Size(173, 94);
listBoxStorage.TabIndex = 2;
listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged;
//
// AddNabor
//
AddNabor.Location = new Point(6, 63);
AddNabor.Name = "AddNabor";
AddNabor.Size = new Size(173, 43);
AddNabor.TabIndex = 1;
AddNabor.Text = "Добавить набор";
AddNabor.UseVisualStyleBackColor = true;
AddNabor.Click += ButtonAddObject_Click;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(6, 34);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(173, 23);
textBoxStorageName.TabIndex = 0;
//
// Input
//
Input.Location = new Point(7, 91);
Input.Location = new Point(7, 354);
Input.Name = "Input";
Input.Size = new Size(186, 23);
Input.TabIndex = 3;
//
// RefreshCollection
//
RefreshCollection.Location = new Point(7, 240);
RefreshCollection.Location = new Point(7, 435);
RefreshCollection.Name = "RefreshCollection";
RefreshCollection.Size = new Size(187, 46);
RefreshCollection.TabIndex = 2;
RefreshCollection.Text = "Обновить коллекцию";
RefreshCollection.Text = "Обновить все";
RefreshCollection.UseVisualStyleBackColor = true;
RefreshCollection.Click += refreshCollection_Click;
RefreshCollection.Click += ButtonRefreshCollection_Click;
//
// DelLocomotive
//
DelLocomotive.Location = new Point(7, 134);
DelLocomotive.Location = new Point(7, 383);
DelLocomotive.Name = "DelLocomotive";
DelLocomotive.Size = new Size(187, 46);
DelLocomotive.TabIndex = 1;
DelLocomotive.Text = "Удалить локомотив";
DelLocomotive.UseVisualStyleBackColor = true;
DelLocomotive.Click += deleteLoco_Click;
DelLocomotive.Click += ButtonRemoveLocomotive_Click;
//
// AddLocomotive
//
AddLocomotive.Location = new Point(6, 22);
AddLocomotive.Location = new Point(5, 302);
AddLocomotive.Name = "AddLocomotive";
AddLocomotive.Size = new Size(187, 46);
AddLocomotive.TabIndex = 0;
AddLocomotive.Text = "Добавить локомотив";
AddLocomotive.UseVisualStyleBackColor = true;
AddLocomotive.Click += addLocomotiv_Click;
AddLocomotive.Click += ButtonAddLocomotive_Click;
//
// FormLocomotiveCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(848, 469);
ClientSize = new Size(910, 507);
Controls.Add(ToolsBox);
Controls.Add(CollectionPictureBox);
Name = "FormLocomotiveCollection";
@ -108,6 +165,8 @@
((System.ComponentModel.ISupportInitialize)CollectionPictureBox).EndInit();
ToolsBox.ResumeLayout(false);
ToolsBox.PerformLayout();
NaborGroupBox.ResumeLayout(false);
NaborGroupBox.PerformLayout();
ResumeLayout(false);
}
@ -119,5 +178,10 @@
private Button RefreshCollection;
private Button DelLocomotive;
private Button AddLocomotive;
private GroupBox NaborGroupBox;
private Button AddNabor;
private TextBox textBoxStorageName;
private Button ButtomRemoveNabor;
private ListBox listBoxStorage;
}
}

View File

@ -14,22 +14,85 @@ namespace ElectricLocomotive
{
public partial class FormLocomotiveCollection : Form
{
private readonly LocomotivesGenericCollection<DrawningLocomotive, DrawningObjectLocomotive> _locos;
private readonly LocomotiveGenericStorage _storage;
public FormLocomotiveCollection()
{
InitializeComponent();
_locos = new(CollectionPictureBox.Width, CollectionPictureBox.Height);
_storage = new LocomotiveGenericStorage(CollectionPictureBox.Width, CollectionPictureBox.Height);
}
private void addLocomotiv_Click(object sender, EventArgs e)
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
private void ReloadObjects()
{
int index = listBoxStorage.SelectedIndex;
listBoxStorage.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorage.Items.Add(_storage.Keys[i]);
}
if (listBoxStorage.Items.Count > 0 && (index == -1 || index >= listBoxStorage.Items.Count))
{
listBoxStorage.SelectedIndex = 0;
}
else if (listBoxStorage.Items.Count > 0 && index > -1 && index < listBoxStorage.Items.Count)
{
listBoxStorage.SelectedIndex = index;
}
}
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();
}
private void listBoxStorage_SelectedIndexChanged(object sender, EventArgs e)
{
CollectionPictureBox.Image = _storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowLocomotives();
}
private void ButtonRemoveObject_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {listBoxStorage.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorage.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
}
private void ButtonAddLocomotive_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1) return;
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
ElectricLocomotive form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_locos + form.SelectedLocomotive != null)
//проверяем, удалось ли нам загрузить объект
if (obj + form.SelectedLocomotive > -1)
{
MessageBox.Show("Объект добавлен");
CollectionPictureBox.Image = _locos.ShowLocomotives();
CollectionPictureBox.Image = obj.ShowLocomotives();
}
else
{
@ -38,17 +101,24 @@ namespace ElectricLocomotive
}
}
private void deleteLoco_Click(object sender, EventArgs e)
private void ButtonRemoveLocomotive_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1) return;
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(Input.Text);
if (_locos - pos != null)
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
CollectionPictureBox.Image = _locos.ShowLocomotives();
CollectionPictureBox.Image = obj.ShowLocomotives();
}
else
{
@ -56,10 +126,15 @@ namespace ElectricLocomotive
}
}
private void refreshCollection_Click(object sender, EventArgs e)
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
CollectionPictureBox.Image = _locos.ShowLocomotives();
if (listBoxStorage.SelectedIndex == -1) return;
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
CollectionPictureBox.Image = obj.ShowLocomotives();
}
}
}
}

View File

@ -0,0 +1,79 @@
using ElectricLocomotive.DrawningObject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectricLocomotive.Generics
{
internal class LocomotiveGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, LocomotivesGenericCollection<DrawningLocomotive, DrawningObjectLocomotive>> _locomotivesStorage;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _locomotivesStorage.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public LocomotiveGenericStorage(int pictureWidth, int pictureHeight)
{
_locomotivesStorage = new Dictionary<string, LocomotivesGenericCollection<DrawningLocomotive, DrawningObjectLocomotive>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (!_locomotivesStorage.ContainsKey(name))
{
_locomotivesStorage.Add(name, new LocomotivesGenericCollection<DrawningLocomotive, DrawningObjectLocomotive>(_pictureWidth, _pictureHeight));
}
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (_locomotivesStorage.ContainsKey(name))
{
_locomotivesStorage.Remove(name);
}
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public LocomotivesGenericCollection<DrawningLocomotive, DrawningObjectLocomotive>?
this[string ind]
{
get
{
// TODO Продумать логику получения набора
if (_locomotivesStorage.ContainsKey(ind))
{
return _locomotivesStorage[ind];
}
return null;
}
}
}
}

View File

@ -17,6 +17,7 @@ namespace ElectricLocomotive.Generics
private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 200;
private readonly int _placeSizeHeight = 90;
private readonly SetGeneric<T> _collection;
public LocomotivesGenericCollection(int picWidth, int picHeight)
{
@ -26,24 +27,29 @@ namespace ElectricLocomotive.Generics
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public static int operator +(LocomotivesGenericCollection<T, U> collect, T? locomotive)
public static int operator +(LocomotivesGenericCollection<T, U> collect, T? loco)
{
if (locomotive == null)
if (loco == null)
{
return -1;
}
return collect?._collection.Insert(locomotive) ?? -1;
return collect._collection.Insert(loco);
}
public static bool operator -(LocomotivesGenericCollection<T, U> collect, int pos)
/// Перегрузка оператора вычитания
public static T? operator -(LocomotivesGenericCollection<T, U> collect, int pos)
{
T? locomotive = collect._collection.Get(pos);
if (locomotive != null)
return collect._collection.Remove(pos);
return false;
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
return obj;
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
public Bitmap ShowLocomotives()
{
@ -58,30 +64,26 @@ namespace ElectricLocomotive.Generics
Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight +
1; ++j)
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{
g.DrawLine(pen, i * _placeSizeWidth, j *
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j *
_placeSizeHeight);
//линия рамзетки места
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i *
_placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
private void DrawObjects(Graphics g)
{
int HLoco = _pictureHeight / _placeSizeHeight;
int Wloco = _pictureWidth / _placeSizeWidth;
int HeightObjCount = _pictureHeight / _placeSizeHeight;
int WidthObjCount = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _collection.Count; i++)
{
T? type = _collection.Get(i);
T? type = _collection[i];
if (type != null)
{
type.SetPosition(
(int)(i / HLoco * _placeSizeWidth),
(HLoco - 1) * _placeSizeHeight - (int)(i % HLoco * _placeSizeHeight)
);
(int)(i / HeightObjCount * _placeSizeWidth),
(HeightObjCount - 1) * _placeSizeHeight - (int)(i % HeightObjCount * _placeSizeHeight));
type?.DrawTransport(g);
}
}

View File

@ -6,60 +6,60 @@ using System.Threading.Tasks;
namespace ProjectElectricLocomotive.Generics
{
internal class SetGeneric<T>
where T : class
internal class SetGeneric<T> where T : class
{
private readonly T?[] _places;
public int Count => _places.Length;
private readonly List<T?> _places;
public int Count => _places.Count;
/// Максимальное количество объектов в списке
private readonly int _maxCount;
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(count);
}
public int Insert(T locomotive)
public int Insert(T loco)
{
return Insert(locomotive, 0);
return Insert(loco, 0);
}
public int Insert(T locomotive, int position)
public int Insert(T loco, int position)
{
int Full = 0;
int temp = 0;
for (int i = position; i < Count; i++)
{
if (_places[i] != null) Full++;
}
if (Full == Count - position - 1)
return -1;
if (position < Count && position >= 0)
{
for (int j = position; j < Count; j++)
{
if (_places[j] == null)
{
temp = j;
break;
}
}
for (int i = temp; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = locomotive;
return position;
}
return -1;
if (position < 0 || position >= _maxCount) return -1;
_places.Insert(position, loco);
return position;
}
public bool Remove(int position)
public T? Remove(int position)
{
if (position >= Count || position < 0)
if (!(position >= 0 && position < Count) || _places[position] == null)
return false;
return null;
T? tmp = _places[position];
_places[position] = null;
return true;
return tmp;
}
public T Get(int position)
public T? this[int position]
{
if (position < 0 || position >= Count) return null;
return _places[position];
get
{
if (position < 0 || position >= Count) return null;
return _places[position];
}
set
{
if (position < 0 || position >= Count || Count == _maxCount) return;
_places.Insert(position, value);
}
}
public IEnumerable<T?> GetLocomotives(int? maxLocos = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxLocos.HasValue && i == maxLocos.Value)
{
yield break;
}
}
}
}
}