diff --git a/gidro/FormPlaneCollection.Designer.cs b/gidro/FormPlaneCollection.Designer.cs index ff6f7cf..a907cf0 100644 --- a/gidro/FormPlaneCollection.Designer.cs +++ b/gidro/FormPlaneCollection.Designer.cs @@ -30,12 +30,18 @@ { pictureBoxCollection = new PictureBox(); groupBox1 = new GroupBox(); + groupBox2 = new GroupBox(); + buttonDeleteObject = new Button(); + listBoxStorages = new ListBox(); + buttonAddObject = new Button(); + textBoxStorageName = new TextBox(); buttonRefreshCollection = new Button(); buttonRemovePlane = new Button(); maskedTextBoxNumber = new MaskedTextBox(); buttonAddPlane = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); groupBox1.SuspendLayout(); + groupBox2.SuspendLayout(); SuspendLayout(); // // pictureBoxCollection @@ -43,26 +49,77 @@ pictureBoxCollection.Dock = DockStyle.Fill; pictureBoxCollection.Location = new Point(0, 0); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(1026, 443); + pictureBoxCollection.Size = new Size(1026, 446); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // // groupBox1 // + groupBox1.Controls.Add(groupBox2); groupBox1.Controls.Add(buttonRefreshCollection); groupBox1.Controls.Add(buttonRemovePlane); groupBox1.Controls.Add(maskedTextBoxNumber); groupBox1.Controls.Add(buttonAddPlane); groupBox1.Location = new Point(852, 0); groupBox1.Name = "groupBox1"; - groupBox1.Size = new Size(174, 421); + groupBox1.Size = new Size(174, 439); groupBox1.TabIndex = 2; groupBox1.TabStop = false; groupBox1.Text = "Инструменты"; // + // groupBox2 + // + groupBox2.Controls.Add(buttonDeleteObject); + groupBox2.Controls.Add(listBoxStorages); + groupBox2.Controls.Add(buttonAddObject); + groupBox2.Controls.Add(textBoxStorageName); + groupBox2.Location = new Point(12, 39); + groupBox2.Name = "groupBox2"; + groupBox2.Size = new Size(150, 219); + groupBox2.TabIndex = 4; + groupBox2.TabStop = false; + groupBox2.Text = "Наборы"; + // + // buttonDeleteObject + // + buttonDeleteObject.Location = new Point(6, 190); + buttonDeleteObject.Name = "buttonDeleteObject"; + buttonDeleteObject.Size = new Size(138, 23); + buttonDeleteObject.TabIndex = 3; + buttonDeleteObject.Text = "Удалить набор"; + buttonDeleteObject.UseVisualStyleBackColor = true; + buttonDeleteObject.Click += ButtonDelObject_Click; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 15; + listBoxStorages.Location = new Point(6, 85); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(138, 94); + listBoxStorages.TabIndex = 2; + listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged; + // + // buttonAddObject + // + buttonAddObject.Location = new Point(6, 56); + buttonAddObject.Name = "buttonAddObject"; + buttonAddObject.Size = new Size(138, 23); + buttonAddObject.TabIndex = 1; + buttonAddObject.Text = "Добавить набор"; + buttonAddObject.UseVisualStyleBackColor = true; + buttonAddObject.Click += ButtonAddObject_Click; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(6, 22); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(138, 23); + textBoxStorageName.TabIndex = 0; + // // buttonRefreshCollection // - buttonRefreshCollection.Location = new Point(14, 206); + buttonRefreshCollection.Location = new Point(14, 407); buttonRefreshCollection.Name = "buttonRefreshCollection"; buttonRefreshCollection.Size = new Size(149, 23); buttonRefreshCollection.TabIndex = 3; @@ -72,7 +129,7 @@ // // buttonRemovePlane // - buttonRemovePlane.Location = new Point(14, 155); + buttonRemovePlane.Location = new Point(14, 377); buttonRemovePlane.Name = "buttonRemovePlane"; buttonRemovePlane.Size = new Size(149, 24); buttonRemovePlane.TabIndex = 2; @@ -82,14 +139,14 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(14, 101); + maskedTextBoxNumber.Location = new Point(14, 348); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(149, 23); maskedTextBoxNumber.TabIndex = 1; // // buttonAddPlane // - buttonAddPlane.Location = new Point(14, 45); + buttonAddPlane.Location = new Point(14, 317); buttonAddPlane.Name = "buttonAddPlane"; buttonAddPlane.Size = new Size(149, 25); buttonAddPlane.TabIndex = 0; @@ -101,7 +158,7 @@ // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1026, 443); + ClientSize = new Size(1026, 446); Controls.Add(groupBox1); Controls.Add(pictureBoxCollection); Name = "FormPlaneCollection"; @@ -109,6 +166,8 @@ ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); + groupBox2.ResumeLayout(false); + groupBox2.PerformLayout(); ResumeLayout(false); } @@ -120,5 +179,10 @@ private Button buttonRemovePlane; private MaskedTextBox maskedTextBoxNumber; private Button buttonAddPlane; + private GroupBox groupBox2; + private Button buttonDeleteObject; + private ListBox listBoxStorages; + private Button buttonAddObject; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/gidro/FormPlaneCollection.cs b/gidro/FormPlaneCollection.cs index ea207c1..e9001dc 100644 --- a/gidro/FormPlaneCollection.cs +++ b/gidro/FormPlaneCollection.cs @@ -18,17 +18,85 @@ namespace ProjectSeaplane /// /// Набор объектов /// - private readonly PlanesGenericCollection _planes; + private readonly PlanesGenericStorage _storage; /// /// Конструктор /// public FormPlaneCollection() { InitializeComponent(); - _planes = new PlanesGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new PlanesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); } + /// + /// Заполнение listBoxObjects + /// + private void ReloadObjects() + { + int index = listBoxStorages.SelectedIndex; + listBoxStorages.Items.Clear(); + foreach (var key in _storage.Keys) + { + listBoxStorages.Items.Add(key); + } + 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]?.ShowPlanes(); + } + /// + /// Удаление набора + /// + /// + /// + 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(); + } + } + /// /// Добавление объекта в набор /// @@ -36,13 +104,22 @@ namespace ProjectSeaplane /// private void buttonAddPlane_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } FormSeaplane form = new FormSeaplane(); if (form.ShowDialog() == DialogResult.OK) { - if (_planes + form.SelectedPlane != -1) + if (obj + form.SelectedPlane) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _planes.ShowPlanes(); + pictureBoxCollection.Image = obj.ShowPlanes(); } else { @@ -57,21 +134,26 @@ namespace ProjectSeaplane /// private void buttonRemovePlane_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (listBoxStorages.SelectedIndex == -1) { return; } - int pos = -1; - try + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) { - pos = Convert.ToInt32(maskedTextBoxNumber.Text); + return; } - catch (Exception ex) { } - if (_planes - pos) + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, + MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _planes.ShowPlanes(); + pictureBoxCollection.Image = obj.ShowPlanes(); } else { @@ -85,7 +167,18 @@ namespace ProjectSeaplane /// private void buttonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _planes.ShowPlanes(); + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowPlanes(); + } } } } diff --git a/gidro/PlanesGenericCollection.cs b/gidro/PlanesGenericCollection.cs index bdc82fb..b88025f 100644 --- a/gidro/PlanesGenericCollection.cs +++ b/gidro/PlanesGenericCollection.cs @@ -52,13 +52,13 @@ namespace ProjectSeaplane.Generics /// /// /// - public static int? operator +(PlanesGenericCollection collect, T? obj) + public static bool operator +(PlanesGenericCollection collect, T? obj) { if (obj == null) { - return -1; + return false; } - return collect?._collection.Insert(obj); + return (bool)collect?._collection.Insert(obj); } /// @@ -67,14 +67,14 @@ namespace ProjectSeaplane.Generics /// /// /// - public static bool operator -(PlanesGenericCollection collect, int pos) + public static T? operator -(PlanesGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); - if (obj == null) + T? obj = collect._collection[pos]; + if (obj != null) { - return false; + collect._collection.Remove(pos); } - return collect._collection.Remove(pos); + return obj; } /// @@ -84,7 +84,7 @@ namespace ProjectSeaplane.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// @@ -125,21 +125,17 @@ namespace ProjectSeaplane.Generics { int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach (var plane in _collection.GetPlanes()) { - // TODO получение объекта - DrawningPlane? plane = _collection.Get(i); - if (plane == null) + if (plane != null) { - continue; + plane.SetPosition((width - 1 - (i % width)) * _placeSizeWidth, (width - (i / width) - 1) * _placeSizeHeight); + plane.DrawTransport(g); } - int r = width - (i / width) - 1; - int s = width - 1 - (i % width); - // TODO установка позиции - plane.SetPosition(s * _placeSizeWidth, r * _placeSizeHeight); - // TODO прорисовка объекта - plane.DrawTransport(g); + i++; } + } } } diff --git a/gidro/PlanesGenericStorage.cs b/gidro/PlanesGenericStorage.cs new file mode 100644 index 0000000..fc37b49 --- /dev/null +++ b/gidro/PlanesGenericStorage.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectSeaplane.DrawningObjects; +using ProjectSeaplane.MovementStrategy; +using ProjectSeaplane.Generics; + +namespace ProjectSeaplane.Generics +{ + internal class PlanesGenericStorage + { + readonly Dictionary> _planeStorages; + public List Keys => _planeStorages.Keys.ToList(); + private readonly int _pictureWidth; + private readonly int _pictureHeight; + public PlanesGenericStorage(int pictureWidth, int pictureHeight) + { + _planeStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public void AddSet(string name) + { + if (_planeStorages.ContainsKey(name)) return; + _planeStorages[name] = new PlanesGenericCollection(_pictureWidth, _pictureHeight); + } + + public void DelSet(string name) + { + if (!_planeStorages.ContainsKey(name)) return; + _planeStorages.Remove(name); + } + + public PlanesGenericCollection? + this[string ind] + { + get + { + if (_planeStorages.ContainsKey(ind)) return _planeStorages[ind]; + return null; + } + } + + } + +} diff --git a/gidro/SetGeneric.cs b/gidro/SetGeneric.cs index e33d7d9..2ff9981 100644 --- a/gidro/SetGeneric.cs +++ b/gidro/SetGeneric.cs @@ -16,44 +16,32 @@ namespace ProjectSeaplane.Generics /// /// Массив объектов, которые храним /// - 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 plane) + public bool Insert(T plane) { - int index = -1; - for (int i = 0; i < _places.Length; i++) - { - if (_places[i] == null) - { - index = i; break; - } - } - if (index < 0) - { - return -1; - } - for (int i = index; i > 0; i--) - { - _places[i] = _places[i - 1]; - } - _places[0] = plane; - return 0; + return Insert(plane, 0); } /// /// Добавление объекта в набор на конкретную позицию @@ -61,31 +49,16 @@ namespace ProjectSeaplane.Generics /// Добавляемый автомобиль /// Позиция /// - public int Insert(T plane, int position) + public bool Insert(T plane, int position) { - if (position < 0 || position >= Count) - return -1; - if (_places[position] == null) - { - _places[position] = plane; - return position; - } - int index = -1; - for (int i = position; i < Count; i++) - { - if (_places[i] == null) - { - index = i; break; - } - } - if (index < 0) - return -1; - for (int i = index; index > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = plane; - return position; + if (position < 0 || position >= _maxCount) + return false; + + if (Count >= _maxCount) + return false; + _places.Insert(0, plane); + return true; + } /// /// Удаление объекта из набора с конкретной позиции @@ -94,21 +67,50 @@ namespace ProjectSeaplane.Generics /// public bool Remove(int position) { - if (position < 0 || position >= Count) + if (position < 0 || position > _maxCount) return false; - _places[position] = null; + if (position >= Count) + return false; + _places.RemoveAt(position); return true; } + /// /// Получение объекта из набора по позиции /// /// /// - 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 > _maxCount) + return null; + return _places[position]; + } + set + { + if (position < 0 || position > _maxCount) + return; + _places[position] = value; + } } + /// + /// Проход по списку + /// + /// + public IEnumerable GetPlanes(int? maxPlanes = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxPlanes.HasValue && i == maxPlanes.Value) + { + yield break; + } + } + } + + } }