Готово
This commit is contained in:
parent
7305320931
commit
5b6e6256ff
@ -52,11 +52,11 @@ namespace ProjectTrolleybus.Generics
|
||||
/// <param name="collect"></param>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static int operator +(BusesGenericCollection<T, U>? collect, T? obj)
|
||||
public static bool operator +(BusesGenericCollection<T, U>? collect, T? obj)
|
||||
{
|
||||
if (obj == null || collect == null)
|
||||
return -1;
|
||||
return collect._collection.Insert(obj);
|
||||
return false;
|
||||
return collect?._collection.Insert(obj) ?? false;
|
||||
}
|
||||
/// <summary>
|
||||
/// Перегрузка оператора вычитания
|
||||
@ -64,12 +64,12 @@ namespace ProjectTrolleybus.Generics
|
||||
/// <param name="collect"></param>
|
||||
/// <param name="pos"></param>
|
||||
/// <returns></returns>
|
||||
public static bool operator -(BusesGenericCollection<T, U> collect, int pos)
|
||||
public static T? operator -(BusesGenericCollection<T, U> collect, int pos)
|
||||
{
|
||||
T? obj = collect?._collection.Get(pos);
|
||||
if (obj == null || collect == null)
|
||||
return false;
|
||||
return collect._collection.Remove(pos);
|
||||
T? obj = collect._collection[pos];
|
||||
if (obj != null)
|
||||
collect._collection.Remove(pos);
|
||||
return obj;
|
||||
}
|
||||
/// <summary>
|
||||
/// Получение объекта IMoveableObject
|
||||
@ -78,7 +78,7 @@ namespace ProjectTrolleybus.Generics
|
||||
/// <returns></returns>
|
||||
public U? GetU(int pos)
|
||||
{
|
||||
return (U?)_collection.Get(pos)?.GetMoveableObject;
|
||||
return (U?)_collection[pos]?.GetMoveableObject;
|
||||
}
|
||||
/// <summary>
|
||||
/// Вывод всего набора объектов
|
||||
@ -118,15 +118,16 @@ namespace ProjectTrolleybus.Generics
|
||||
/// <param name="g"></param>
|
||||
private void DrawObjects(Graphics g)
|
||||
{
|
||||
for (int i = 0; i < _collection.Count; i++)
|
||||
int i = 0;
|
||||
foreach (var bus in _collection.GetBuses())
|
||||
{
|
||||
DrawingBus? trolleybus = _collection.Get(i);
|
||||
if (trolleybus != null)
|
||||
if (bus != null)
|
||||
{
|
||||
int inRow = _pictureWidth / _placeSizeWidth;
|
||||
trolleybus.SetPosition(_placeSizeWidth * (inRow - 1) - (i % inRow * _placeSizeWidth), i / inRow * _placeSizeHeight);
|
||||
trolleybus.DrawTransport(g);
|
||||
bus.SetPosition(_placeSizeWidth * (inRow - 1) - (i % inRow * _placeSizeWidth), i / inRow * _placeSizeHeight);
|
||||
bus.DrawTransport(g);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
77
Trolleybus/Trolleybus/BusesGenericStorage.cs
Normal file
77
Trolleybus/Trolleybus/BusesGenericStorage.cs
Normal file
@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ProjectTrolleybus.DrawingObjects;
|
||||
using ProjectTrolleybus.MovementStrategy;
|
||||
|
||||
namespace ProjectTrolleybus.Generics
|
||||
{
|
||||
internal class BusesGenericStorage
|
||||
{
|
||||
/// <summary>
|
||||
/// Словарь (хранилище)
|
||||
/// </summary>
|
||||
readonly Dictionary<string, BusesGenericCollection<DrawingBus,
|
||||
DrawingObjectBus>> _busStorages;
|
||||
/// <summary>
|
||||
/// Возвращение списка названий наборов
|
||||
/// </summary>
|
||||
public List<string> Keys => _busStorages.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 BusesGenericStorage(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_busStorages = new Dictionary<string,
|
||||
BusesGenericCollection<DrawingBus, DrawingObjectBus>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
/// <summary>
|
||||
/// Добавление набора
|
||||
/// </summary>
|
||||
/// <param name="name">Название набора</param>
|
||||
public void AddSet(string name)
|
||||
{
|
||||
_busStorages.Add(name, new BusesGenericCollection<DrawingBus,
|
||||
DrawingObjectBus>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
/// <summary>
|
||||
/// Удаление набора
|
||||
/// </summary>
|
||||
/// <param name="name">Название набора</param>
|
||||
public void DelSet(string name)
|
||||
{
|
||||
if (!_busStorages.ContainsKey(name))
|
||||
return;
|
||||
_busStorages.Remove(name);
|
||||
}
|
||||
/// <summary>
|
||||
/// Доступ к набору
|
||||
/// </summary>
|
||||
/// <param name="ind"></param>
|
||||
/// <returns></returns>
|
||||
public BusesGenericCollection<DrawingBus, DrawingObjectBus>?
|
||||
this[string ind]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_busStorages.ContainsKey(ind))
|
||||
return _busStorages[ind];
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
74
Trolleybus/Trolleybus/FormBusCollection.Designer.cs
generated
74
Trolleybus/Trolleybus/FormBusCollection.Designer.cs
generated
@ -29,17 +29,24 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
groupBoxTrolleybus = new GroupBox();
|
||||
groupBoxSets = new GroupBox();
|
||||
textBoxStorageName = new TextBox();
|
||||
buttonDelObject = new Button();
|
||||
listBoxStorages = new ListBox();
|
||||
buttonAddObject = new Button();
|
||||
buttonUpdateCollection = new Button();
|
||||
buttonDeleteBus = new Button();
|
||||
maskedTextBoxNumber = new MaskedTextBox();
|
||||
buttonAddBus = new Button();
|
||||
pictureBoxCollection = new PictureBox();
|
||||
groupBoxTrolleybus.SuspendLayout();
|
||||
groupBoxSets.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
// groupBoxTrolleybus
|
||||
//
|
||||
groupBoxTrolleybus.Controls.Add(groupBoxSets);
|
||||
groupBoxTrolleybus.Controls.Add(buttonUpdateCollection);
|
||||
groupBoxTrolleybus.Controls.Add(buttonDeleteBus);
|
||||
groupBoxTrolleybus.Controls.Add(maskedTextBoxNumber);
|
||||
@ -51,9 +58,61 @@
|
||||
groupBoxTrolleybus.TabStop = false;
|
||||
groupBoxTrolleybus.Text = "Инструменты";
|
||||
//
|
||||
// groupBoxSets
|
||||
//
|
||||
groupBoxSets.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
groupBoxSets.Controls.Add(textBoxStorageName);
|
||||
groupBoxSets.Controls.Add(buttonDelObject);
|
||||
groupBoxSets.Controls.Add(listBoxStorages);
|
||||
groupBoxSets.Controls.Add(buttonAddObject);
|
||||
groupBoxSets.Location = new Point(6, 22);
|
||||
groupBoxSets.Name = "groupBoxSets";
|
||||
groupBoxSets.Size = new Size(245, 234);
|
||||
groupBoxSets.TabIndex = 4;
|
||||
groupBoxSets.TabStop = false;
|
||||
groupBoxSets.Text = "Наборы";
|
||||
//
|
||||
// textBoxStorageName
|
||||
//
|
||||
textBoxStorageName.Location = new Point(6, 22);
|
||||
textBoxStorageName.Name = "textBoxStorageName";
|
||||
textBoxStorageName.Size = new Size(233, 23);
|
||||
textBoxStorageName.TabIndex = 4;
|
||||
//
|
||||
// buttonDelObject
|
||||
//
|
||||
buttonDelObject.Location = new Point(6, 194);
|
||||
buttonDelObject.Name = "buttonDelObject";
|
||||
buttonDelObject.Size = new Size(233, 30);
|
||||
buttonDelObject.TabIndex = 3;
|
||||
buttonDelObject.Text = "Удалить набор";
|
||||
buttonDelObject.UseVisualStyleBackColor = true;
|
||||
buttonDelObject.Click += ButtonDelObject_Click;
|
||||
//
|
||||
// listBoxStorages
|
||||
//
|
||||
listBoxStorages.FormattingEnabled = true;
|
||||
listBoxStorages.ItemHeight = 15;
|
||||
listBoxStorages.Location = new Point(6, 94);
|
||||
listBoxStorages.Name = "listBoxStorages";
|
||||
listBoxStorages.Size = new Size(233, 94);
|
||||
listBoxStorages.TabIndex = 2;
|
||||
listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
|
||||
//
|
||||
// buttonAddObject
|
||||
//
|
||||
buttonAddObject.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
buttonAddObject.Location = new Point(6, 62);
|
||||
buttonAddObject.Name = "buttonAddObject";
|
||||
buttonAddObject.Size = new Size(233, 26);
|
||||
buttonAddObject.TabIndex = 1;
|
||||
buttonAddObject.Text = "Добавить набор";
|
||||
buttonAddObject.UseVisualStyleBackColor = true;
|
||||
buttonAddObject.Click += ButtonAddObject_Click;
|
||||
//
|
||||
// buttonUpdateCollection
|
||||
//
|
||||
buttonUpdateCollection.Location = new Point(10, 250);
|
||||
buttonUpdateCollection.Location = new Point(10, 408);
|
||||
buttonUpdateCollection.Name = "buttonUpdateCollection";
|
||||
buttonUpdateCollection.Size = new Size(241, 28);
|
||||
buttonUpdateCollection.TabIndex = 3;
|
||||
@ -63,7 +122,7 @@
|
||||
//
|
||||
// buttonDeleteBus
|
||||
//
|
||||
buttonDeleteBus.Location = new Point(10, 198);
|
||||
buttonDeleteBus.Location = new Point(10, 343);
|
||||
buttonDeleteBus.Name = "buttonDeleteBus";
|
||||
buttonDeleteBus.Size = new Size(241, 29);
|
||||
buttonDeleteBus.TabIndex = 2;
|
||||
@ -73,7 +132,7 @@
|
||||
//
|
||||
// maskedTextBoxNumber
|
||||
//
|
||||
maskedTextBoxNumber.Location = new Point(74, 86);
|
||||
maskedTextBoxNumber.Location = new Point(70, 305);
|
||||
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
|
||||
maskedTextBoxNumber.Size = new Size(115, 23);
|
||||
maskedTextBoxNumber.TabIndex = 1;
|
||||
@ -81,7 +140,7 @@
|
||||
// buttonAddBus
|
||||
//
|
||||
buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
buttonAddBus.Location = new Point(10, 22);
|
||||
buttonAddBus.Location = new Point(10, 262);
|
||||
buttonAddBus.Name = "buttonAddBus";
|
||||
buttonAddBus.Size = new Size(241, 28);
|
||||
buttonAddBus.TabIndex = 0;
|
||||
@ -110,6 +169,8 @@
|
||||
Text = "Набор автобусов";
|
||||
groupBoxTrolleybus.ResumeLayout(false);
|
||||
groupBoxTrolleybus.PerformLayout();
|
||||
groupBoxSets.ResumeLayout(false);
|
||||
groupBoxSets.PerformLayout();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
|
||||
ResumeLayout(false);
|
||||
}
|
||||
@ -122,5 +183,10 @@
|
||||
private Button buttonUpdateCollection;
|
||||
private Button buttonDeleteBus;
|
||||
private PictureBox pictureBoxCollection;
|
||||
private GroupBox groupBoxSets;
|
||||
private Button buttonDelObject;
|
||||
private ListBox listBoxStorages;
|
||||
private Button buttonAddObject;
|
||||
private TextBox textBoxStorageName;
|
||||
}
|
||||
}
|
@ -15,27 +15,78 @@ namespace ProjectTrolleybus
|
||||
{
|
||||
public partial class FormBusCollection : Form
|
||||
{
|
||||
private readonly BusesGenericCollection<DrawingBus, DrawingObjectBus> _buses;
|
||||
|
||||
private readonly BusesGenericStorage _storage;
|
||||
public FormBusCollection()
|
||||
{
|
||||
InitializeComponent();
|
||||
_buses = new BusesGenericCollection<DrawingBus, DrawingObjectBus>(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||||
_storage = new BusesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
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 ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
|
||||
{
|
||||
pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBuses();
|
||||
}
|
||||
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 ButtonAddBus_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||||
string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
FormTrolleybus form = new();
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (_buses + form.SelectedBus != -1)
|
||||
if (obj + form.SelectedBus)
|
||||
{
|
||||
MessageBox.Show("Объект добавлен");
|
||||
pictureBoxCollection.Image = _buses.ShowBuses();
|
||||
pictureBoxCollection.Image = obj.ShowBuses();
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -43,37 +94,47 @@ namespace ProjectTrolleybus
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Удаление объекта из набора
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonRemoveBus_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("Удалить объект?", "Удаление",
|
||||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
|
||||
if (_buses - pos)
|
||||
if (obj - pos != null)
|
||||
{
|
||||
MessageBox.Show("Объект удален");
|
||||
pictureBoxCollection.Image = _buses.ShowBuses();
|
||||
pictureBoxCollection.Image = obj.ShowBuses();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не удалось удалить объект");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Обновление рисунка по набору
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
||||
{
|
||||
pictureBoxCollection.Image = _buses.ShowBuses();
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||||
string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
pictureBoxCollection.Image = obj.ShowBuses();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,54 +9,68 @@ namespace ProjectTrolleybus.Generics
|
||||
internal class SetGeneric<T>
|
||||
where T : class
|
||||
{
|
||||
private readonly T?[] _places;
|
||||
private readonly List<T?> _places;
|
||||
|
||||
public int Count => _places.Length;
|
||||
private readonly int _maxCount;
|
||||
|
||||
public int Count => _places.Count;
|
||||
|
||||
public SetGeneric(int count)
|
||||
{
|
||||
_places = new T?[count];
|
||||
_maxCount = count;
|
||||
_places = new List<T?>(count);
|
||||
}
|
||||
|
||||
public int Insert(T trolleybus)
|
||||
public bool Insert(T trolleybus)
|
||||
{
|
||||
if (_places[Count - 1] != null)
|
||||
return -1;
|
||||
return Insert(trolleybus, 0);
|
||||
if (_places.Count == _maxCount)
|
||||
return false;
|
||||
Insert(trolleybus, 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
public int Insert(T trolleybus, int position)
|
||||
public bool Insert(T trolleybus, int position)
|
||||
{
|
||||
if (!(position >= 0 && position < Count))
|
||||
return -1;
|
||||
if (_places[position] != null)
|
||||
{
|
||||
int ind = position;
|
||||
while (ind < Count && _places[ind] != null)
|
||||
ind++;
|
||||
if (ind == Count)
|
||||
return -1;
|
||||
for (int i = ind - 1; i >= position; i--)
|
||||
_places[i + 1] = _places[i];
|
||||
}
|
||||
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
|
||||
return false;
|
||||
_places.Insert(position, trolleybus);
|
||||
_places[position] = trolleybus;
|
||||
return position;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Remove(int position)
|
||||
{
|
||||
if (!(position >= 0 && position < Count) || _places[position] == null)
|
||||
if (!(position >= 0 && position < Count))
|
||||
return false;
|
||||
_places[position] = null;
|
||||
_places.RemoveAt(position);
|
||||
return true;
|
||||
}
|
||||
|
||||
public T? Get(int position)
|
||||
public T? this[int position]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!(position >= 0 && position < Count))
|
||||
return null;
|
||||
return _places[position];
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
|
||||
return;
|
||||
_places.Insert(position, value);
|
||||
}
|
||||
}
|
||||
public IEnumerable<T?> GetBuses(int? maxBuses = null)
|
||||
{
|
||||
for (int i = 0; i < _places.Count; ++i)
|
||||
{
|
||||
yield return _places[i];
|
||||
if (maxBuses.HasValue && i == maxBuses.Value)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user