diff --git a/Cruiser/FormCruiserCollection.Designer.cs b/Cruiser/FormCruiserCollection.Designer.cs index cc2ca2b..f6a689f 100644 --- a/Cruiser/FormCruiserCollection.Designer.cs +++ b/Cruiser/FormCruiserCollection.Designer.cs @@ -29,51 +29,46 @@ private void InitializeComponent() { groupBoxTools = new GroupBox(); - button3 = new Button(); - button2 = new Button(); + buttonDeleteCruiser = new Button(); textBoxNumber = new TextBox(); buttonAddCruiser = new Button(); + buttonRefreshCollection = new Button(); pictureBoxCollection = new PictureBox(); + groupBoxStorage = new GroupBox(); + listBoxStorages = new ListBox(); + buttonDelObject = new Button(); + textBoxStorageName = new TextBox(); + buttonAddObject = new Button(); groupBoxTools.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + groupBoxStorage.SuspendLayout(); SuspendLayout(); // // groupBoxTools // - groupBoxTools.Controls.Add(button3); - groupBoxTools.Controls.Add(button2); + groupBoxTools.Controls.Add(buttonDeleteCruiser); groupBoxTools.Controls.Add(textBoxNumber); groupBoxTools.Controls.Add(buttonAddCruiser); - groupBoxTools.Location = new Point(645, 1); + groupBoxTools.Location = new Point(653, 1); groupBoxTools.Name = "groupBoxTools"; - groupBoxTools.Size = new Size(154, 448); + groupBoxTools.Size = new Size(148, 140); groupBoxTools.TabIndex = 0; groupBoxTools.TabStop = false; groupBoxTools.Text = "Инструменты"; // - // button3 + // buttonDeleteCruiser // - button3.Location = new Point(15, 171); - button3.Name = "button3"; - button3.Size = new Size(122, 41); - button3.TabIndex = 3; - button3.Text = "Обновить коллекцию"; - button3.UseVisualStyleBackColor = true; - button3.Click += ButtonRefreshCollection_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; + buttonDeleteCruiser.Location = new Point(15, 95); + buttonDeleteCruiser.Name = "buttonDeleteCruiser"; + buttonDeleteCruiser.Size = new Size(122, 37); + buttonDeleteCruiser.TabIndex = 2; + buttonDeleteCruiser.Text = "Удалить лайнер"; + buttonDeleteCruiser.UseVisualStyleBackColor = true; + buttonDeleteCruiser.Click += ButtonRemoveCar_Click; // // textBoxNumber // - textBoxNumber.Location = new Point(15, 78); + textBoxNumber.Location = new Point(15, 66); textBoxNumber.Name = "textBoxNumber"; textBoxNumber.Size = new Size(122, 23); textBoxNumber.TabIndex = 1; @@ -88,19 +83,81 @@ buttonAddCruiser.UseVisualStyleBackColor = true; 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.Location = new Point(-1, 1); + pictureBoxCollection.Location = new Point(14, 1); pictureBoxCollection.Name = "pictureBoxCollection"; pictureBoxCollection.Size = new Size(642, 448); pictureBoxCollection.TabIndex = 1; 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 // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(buttonRefreshCollection); + Controls.Add(groupBoxStorage); Controls.Add(pictureBoxCollection); Controls.Add(groupBoxTools); Name = "FormCruiserCollection"; @@ -109,6 +166,8 @@ groupBoxTools.ResumeLayout(false); groupBoxTools.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + groupBoxStorage.ResumeLayout(false); + groupBoxStorage.PerformLayout(); ResumeLayout(false); } @@ -117,8 +176,14 @@ private GroupBox groupBoxTools; private TextBox textBoxNumber; private Button buttonAddCruiser; - private Button button3; - private Button button2; + private Button buttonRefreshCollection; + private Button buttonDeleteCruiser; private PictureBox pictureBoxCollection; + private GroupBox groupBoxStorage; + private Button button1; + private Button buttonDelObject; + private TextBox textBoxStorageName; + private Button buttonAddObject; + private ListBox listBoxStorages; } } \ No newline at end of file diff --git a/Cruiser/FormCruiserCollection.cs b/Cruiser/FormCruiserCollection.cs index 7b2df8a..cf4bf6a 100644 --- a/Cruiser/FormCruiserCollection.cs +++ b/Cruiser/FormCruiserCollection.cs @@ -22,15 +22,80 @@ namespace Cruiser /// /// Набор объектов /// - private readonly CarsGenericCollection _cruisers; + private readonly CruisersGenericStorage _storage; /// /// Конструктор /// public FormCruiserCollection() { InitializeComponent(); - _cruisers = new CarsGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new CruisersGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); } + /// + /// Заполнение listBoxObjects + /// + 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 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(); + } + } + + /// + /// Выбор набора + /// + /// + /// + private void ListBoxObjects_SelectedIndexChanged(object sender, + EventArgs e) + { + pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowCruiser(); + } + + /// /// Добавление объекта в набор /// @@ -38,13 +103,22 @@ namespace Cruiser /// 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(); if (form.ShowDialog() == DialogResult.OK) { - if (_cruisers + form.SelectedCruiser != -1) + if (obj + form.SelectedCruiser) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _cruisers.ShowCruiser(); + pictureBoxCollection.Image = obj.ShowCruiser(); } else { @@ -59,21 +133,25 @@ namespace Cruiser /// 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("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } - if (textBoxNumber.Text == "") - { - MessageBox.Show("Вы ничего не ввели"); - return; - } int pos = Convert.ToInt32(textBoxNumber.Text); - if (_cruisers - pos) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _cruisers.ShowCruiser(); + pictureBoxCollection.Image = obj.ShowCruiser(); } else { @@ -87,7 +165,17 @@ namespace Cruiser /// 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(); } diff --git a/Cruiser/Generics/CruisersGenericCollection.cs b/Cruiser/Generics/CruisersGenericCollection.cs index fd10545..945a6d2 100644 --- a/Cruiser/Generics/CruisersGenericCollection.cs +++ b/Cruiser/Generics/CruisersGenericCollection.cs @@ -56,11 +56,11 @@ namespace Cruiser.Generics /// /// /// - public static int operator +(CarsGenericCollection collect, T? obj) + public static bool operator +(CarsGenericCollection collect, T? obj) { if (obj == null) { - return -1; + return false; } return collect._collection.Insert(obj); } @@ -70,15 +70,14 @@ namespace Cruiser.Generics /// /// /// - public static bool operator -(CarsGenericCollection collect, int pos) + public static T? operator -(CarsGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { collect._collection.Remove(pos); - return true; } - return false; + return obj; } /// /// Получение объекта IMoveableObject @@ -87,7 +86,7 @@ namespace Cruiser.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// /// Вывод всего набора объектов @@ -125,16 +124,18 @@ namespace Cruiser.Generics { int Ix = 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.Get(i)?.DrawTransport(g); + _collection[i]?.SetPosition(Ix, Iy); + _collection[i]?.DrawTransport(g); Ix += _placeSizeWidth; if (Ix + _placeSizeHeight > _pictureWidth) { Ix = 0; Iy = _placeSizeHeight; - } + } + i++; } } } diff --git a/Cruiser/Generics/CruisersGenericStorage.cs b/Cruiser/Generics/CruisersGenericStorage.cs new file mode 100644 index 0000000..a38eaf7 --- /dev/null +++ b/Cruiser/Generics/CruisersGenericStorage.cs @@ -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 + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _cruiserStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _cruiserStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public CruisersGenericStorage(int pictureWidth, int pictureHeight) + { + _cruiserStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + if (_cruiserStorages.ContainsKey(name)) return; + _cruiserStorages[name] = new CarsGenericCollection(_pictureWidth, _pictureHeight); + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (!_cruiserStorages.ContainsKey(name)) return; + _cruiserStorages.Remove(name); + } + /// + /// Доступ к набору + /// + /// + /// + public CarsGenericCollection? + this[string ind] + { + get + { + if (_cruiserStorages.ContainsKey(ind)) return _cruiserStorages[ind]; + return null; + } + } + + } +} diff --git a/Cruiser/Generics/SetGeneric.cs b/Cruiser/Generics/SetGeneric.cs index 12b76e0..39d2da4 100644 --- a/Cruiser/Generics/SetGeneric.cs +++ b/Cruiser/Generics/SetGeneric.cs @@ -15,43 +15,38 @@ namespace Cruiser.Generics where T : class { /// - /// Массив объектов, которые храним + /// Список объектов, которые храним /// - private readonly T?[] _places; + private readonly List _places; /// - /// Количество объектов в массиве + /// Количество объектов в списке /// - public int Count => _places.Length; + public int Count => _places.Count; + /// + /// Максимальное количество объектов в списке + /// + private readonly int _maxCount; /// /// Конструктор /// /// public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } /// /// Добавление объекта в набор /// /// Добавляемый лайнер /// - public int Insert(T cruiser) + public bool Insert(T cruiser) { - int zero = 0; - for (; _places[zero] != null;) + if (_places.Count + 1 <= _maxCount) { - zero++; - if (zero == _places.Length) - { - return -1; - } + _places.Insert(0, cruiser); } - for (int i = zero; i > 0; i--) - { - _places[i] = _places[i - 1]; - } - _places[0] = cruiser; - return 0; + return true; } /// /// Удаление объекта из набора с конкретной позиции @@ -60,7 +55,7 @@ namespace Cruiser.Generics /// public bool Remove(int position) { - if (position < 0 || position > _places.Length) + if (position < 0 || position > _places.Count) { return false; } @@ -73,36 +68,41 @@ namespace Cruiser.Generics /// Добавляемый автомобиль /// Позиция /// - 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; - while (_places[zero] != null) + if (position < 0 || position > _maxCount) + { return null; } + return _places[position]; + } + set + { + if (position < 0 || position > _maxCount) + return; + _places[position] = value; + } + } + public IEnumerable GetCruisers(int? maxCruisers = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxCruisers.HasValue && i == maxCruisers.Value) { - zero++; - return -1; + yield break; } } - _places[position] = cruiser; - return position; - } - /// - /// Получение объекта из набора по позиции - /// - /// - /// - public T? Get(int position) - { - if (position < 0 || position > _places.Length) - { - return null; - } - return _places[position]; } + } }