diff --git a/ElectricLocomotive/ElectricLocomotive/FormElectricLocomotive.Designer.cs b/ElectricLocomotive/ElectricLocomotive/FormElectricLocomotive.Designer.cs index a44ff89..e0ab159 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormElectricLocomotive.Designer.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormElectricLocomotive.Designer.cs @@ -132,7 +132,6 @@ namespace ElectricLocomotive comboBoxStrategy.Name = "comboBoxStrategy"; comboBoxStrategy.Size = new Size(151, 23); comboBoxStrategy.TabIndex = 6; - comboBoxStrategy.SelectedIndexChanged += comboBoxStrategy_SelectedIndexChanged; // // buttonCreateLocomotive // @@ -189,7 +188,6 @@ namespace ElectricLocomotive Name = "FormElectricLocomotive"; StartPosition = FormStartPosition.CenterScreen; Text = "ElectricLocomotive"; - Load += ElectricLocomotive_Load; ((System.ComponentModel.ISupportInitialize)pictureBoxElectricLocomotive).EndInit(); ResumeLayout(false); PerformLayout(); diff --git a/ElectricLocomotive/ElectricLocomotive/FormElectricLocomotive.cs b/ElectricLocomotive/ElectricLocomotive/FormElectricLocomotive.cs index cee1fea..3f4d731 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormElectricLocomotive.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormElectricLocomotive.cs @@ -123,17 +123,6 @@ namespace ElectricLocomotive _abstractStrategy = null; } } - private void comboBoxStrategy_SelectedIndexChanged(object sender, EventArgs e) - { - - } - private void pictureBoxElectricLocomotive_Click(object sender, EventArgs e) - { - - } - private void ElectricLocomotive_Load(object sender, EventArgs e) - { - } private void ButtonSelectLocomotive_Click(object sender, EventArgs e) { SelectedLocomotive = _drawingLocomotive; diff --git a/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.Designer.cs b/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.Designer.cs index e829010..1b467de 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.Designer.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.Designer.cs @@ -29,13 +29,19 @@ private void InitializeComponent() { panelLocomotiveCollection = new Panel(); + groupBoxSets = new GroupBox(); + textBoxSetName = new TextBox(); + buttonDeleteSet = new Button(); + listBoxStorages = new ListBox(); + buttonAddSet = new Button(); + LabelOnPanel = new Label(); maskedTextBoxNumber = new MaskedTextBox(); ButtonRefreshCollection = new Button(); ButtonRemoveLocomotive = new Button(); ButtonAddLocomotive = new Button(); pictureBoxCollection = new PictureBox(); - LabelOnPanel = new Label(); panelLocomotiveCollection.SuspendLayout(); + groupBoxSets.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // @@ -43,63 +49,68 @@ // panelLocomotiveCollection.AccessibleDescription = ""; panelLocomotiveCollection.AccessibleName = ""; + panelLocomotiveCollection.Controls.Add(groupBoxSets); panelLocomotiveCollection.Controls.Add(LabelOnPanel); panelLocomotiveCollection.Controls.Add(maskedTextBoxNumber); panelLocomotiveCollection.Controls.Add(ButtonRefreshCollection); panelLocomotiveCollection.Controls.Add(ButtonRemoveLocomotive); panelLocomotiveCollection.Controls.Add(ButtonAddLocomotive); panelLocomotiveCollection.Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point); - panelLocomotiveCollection.Location = new Point(594, 2); + panelLocomotiveCollection.Location = new Point(660, 2); panelLocomotiveCollection.Name = "panelLocomotiveCollection"; - panelLocomotiveCollection.Size = new Size(208, 448); + panelLocomotiveCollection.Size = new Size(248, 584); panelLocomotiveCollection.TabIndex = 0; panelLocomotiveCollection.Tag = "Инструменты"; - panelLocomotiveCollection.Paint += panelLocomotiveCollection_Paint; // - // maskedTextBoxNumber + // groupBoxSets // - maskedTextBoxNumber.Location = new Point(40, 74); - maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(123, 23); - maskedTextBoxNumber.TabIndex = 3; + groupBoxSets.Controls.Add(textBoxSetName); + groupBoxSets.Controls.Add(buttonDeleteSet); + groupBoxSets.Controls.Add(listBoxStorages); + groupBoxSets.Controls.Add(buttonAddSet); + groupBoxSets.Location = new Point(3, 15); + groupBoxSets.Name = "groupBoxSets"; + groupBoxSets.Size = new Size(242, 324); + groupBoxSets.TabIndex = 5; + groupBoxSets.TabStop = false; + groupBoxSets.Text = "Наборы"; // - // ButtonRefreshCollection + // textBoxSetName // - ButtonRefreshCollection.Location = new Point(3, 194); - ButtonRefreshCollection.Name = "ButtonRefreshCollection"; - ButtonRefreshCollection.Size = new Size(194, 43); - ButtonRefreshCollection.TabIndex = 2; - ButtonRefreshCollection.Text = " Обновить коллекцию"; - ButtonRefreshCollection.UseVisualStyleBackColor = true; - ButtonRefreshCollection.Click += ButtonRefreshCollection_Click; + textBoxSetName.Location = new Point(6, 38); + textBoxSetName.Name = "textBoxSetName"; + textBoxSetName.Size = new Size(226, 23); + textBoxSetName.TabIndex = 3; // - // ButtonRemoveLocomotive + // buttonDeleteSet // - ButtonRemoveLocomotive.Location = new Point(3, 138); - ButtonRemoveLocomotive.Name = "ButtonRemoveLocomotive"; - ButtonRemoveLocomotive.Size = new Size(194, 39); - ButtonRemoveLocomotive.TabIndex = 1; - ButtonRemoveLocomotive.Text = "Удалить локомотив"; - ButtonRemoveLocomotive.UseVisualStyleBackColor = true; - ButtonRemoveLocomotive.Click += ButtonRemoveLocomotive_Click; + buttonDeleteSet.Location = new Point(6, 216); + buttonDeleteSet.Name = "buttonDeleteSet"; + buttonDeleteSet.Size = new Size(226, 45); + buttonDeleteSet.TabIndex = 2; + buttonDeleteSet.Text = "Удалить набор"; + buttonDeleteSet.UseVisualStyleBackColor = true; + buttonDeleteSet.Click += buttonDeleteSet_Click; // - // ButtonAddLocomotive + // listBoxStorages // - ButtonAddLocomotive.Location = new Point(3, 15); - ButtonAddLocomotive.Name = "ButtonAddLocomotive"; - ButtonAddLocomotive.Size = new Size(194, 39); - ButtonAddLocomotive.TabIndex = 0; - ButtonAddLocomotive.Text = "Добавить локомотив"; - ButtonAddLocomotive.UseVisualStyleBackColor = true; - ButtonAddLocomotive.Click += ButtonAddLocomotive_Click; + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 15; + listBoxStorages.Location = new Point(6, 116); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(226, 94); + listBoxStorages.TabIndex = 1; + listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; // - // pictureBoxCollection + // buttonAddSet // - pictureBoxCollection.Location = new Point(2, 2); - pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(589, 448); - pictureBoxCollection.TabIndex = 1; - pictureBoxCollection.TabStop = false; + buttonAddSet.Location = new Point(6, 67); + buttonAddSet.Name = "buttonAddSet"; + buttonAddSet.Size = new Size(226, 34); + buttonAddSet.TabIndex = 0; + buttonAddSet.Text = "Добавить набор"; + buttonAddSet.UseVisualStyleBackColor = true; + buttonAddSet.Click += buttonAddSet_Click; // // LabelOnPanel // @@ -109,24 +120,68 @@ LabelOnPanel.Size = new Size(83, 15); LabelOnPanel.TabIndex = 4; LabelOnPanel.Text = "Инструменты"; - LabelOnPanel.Click += LabelOnPanel_Click; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(35, 408); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(176, 23); + maskedTextBoxNumber.TabIndex = 3; + // + // ButtonRefreshCollection + // + ButtonRefreshCollection.Location = new Point(3, 530); + ButtonRefreshCollection.Name = "ButtonRefreshCollection"; + ButtonRefreshCollection.Size = new Size(242, 43); + ButtonRefreshCollection.TabIndex = 2; + ButtonRefreshCollection.Text = " Обновить коллекцию"; + ButtonRefreshCollection.UseVisualStyleBackColor = true; + ButtonRefreshCollection.Click += buttonRefreshCollection_Click; + // + // ButtonRemoveLocomotive + // + ButtonRemoveLocomotive.Location = new Point(3, 460); + ButtonRemoveLocomotive.Name = "ButtonRemoveLocomotive"; + ButtonRemoveLocomotive.Size = new Size(242, 39); + ButtonRemoveLocomotive.TabIndex = 1; + ButtonRemoveLocomotive.Text = "Удалить локомотив"; + ButtonRemoveLocomotive.UseVisualStyleBackColor = true; + ButtonRemoveLocomotive.Click += buttonRemoveLocomotive_Click; + // + // ButtonAddLocomotive + // + ButtonAddLocomotive.Location = new Point(3, 345); + ButtonAddLocomotive.Name = "ButtonAddLocomotive"; + ButtonAddLocomotive.Size = new Size(242, 39); + ButtonAddLocomotive.TabIndex = 0; + ButtonAddLocomotive.Text = "Добавить локомотив"; + ButtonAddLocomotive.UseVisualStyleBackColor = true; + ButtonAddLocomotive.Click += buttonAddLocomotive_Click; + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new Point(2, 2); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(655, 584); + pictureBoxCollection.TabIndex = 1; + pictureBoxCollection.TabStop = false; // // FormLocomotiveCollection // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(909, 587); Controls.Add(pictureBoxCollection); Controls.Add(panelLocomotiveCollection); Name = "FormLocomotiveCollection"; Text = "Набор локомотивов"; - Load += FormLocomotiveCollection_Load; panelLocomotiveCollection.ResumeLayout(false); panelLocomotiveCollection.PerformLayout(); + groupBoxSets.ResumeLayout(false); + groupBoxSets.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ResumeLayout(false); } - #endregion private Panel panelLocomotiveCollection; @@ -136,5 +191,10 @@ private PictureBox pictureBoxCollection; private MaskedTextBox maskedTextBoxNumber; private Label LabelOnPanel; + private GroupBox groupBoxSets; + private Button buttonDeleteSet; + private ListBox listBoxStorages; + private Button buttonAddSet; + private TextBox textBoxSetName; } } \ No newline at end of file diff --git a/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs b/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs index cda95cd..ae6a12f 100644 --- a/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs +++ b/ElectricLocomotive/ElectricLocomotive/FormLocomotiveCollection.cs @@ -16,21 +16,47 @@ namespace ProjectElectricLocomotive { public partial class FormLocomotiveCollection : Form { - private readonly LocomotivesGenericCollection _locomotives; + private readonly LocomotivesGenericStorage _storage; public FormLocomotiveCollection() { InitializeComponent(); - _locomotives = new LocomotivesGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new LocomotivesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); } - private void ButtonAddLocomotive_Click(object sender, EventArgs e) + 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 buttonAddLocomotive_Click(object sender, EventArgs e) + { + if (listBoxStorages.SelectedIndex == -1) return; + + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } FormElectricLocomotive form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_locomotives + form.SelectedLocomotive != -1) + //проверяем, удалось ли нам загрузить объект + if (obj + form.SelectedLocomotive > -1) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _locomotives.ShowLocomotives(); + pictureBoxCollection.Image = obj.ShowLocomotives(); } else { @@ -38,38 +64,67 @@ namespace ProjectElectricLocomotive } } } - private void ButtonRemoveLocomotive_Click(object sender, EventArgs e) + private void buttonRemoveLocomotive_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + 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 (_locomotives - pos != null) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _locomotives.ShowLocomotives(); + pictureBoxCollection.Image = obj.ShowLocomotives(); + pictureBoxCollection.Image = obj.ShowLocomotives(); } else { MessageBox.Show("Не удалось удалить объект"); } } - private void ButtonRefreshCollection_Click(object sender, EventArgs e) + private void buttonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _locomotives.ShowLocomotives(); + if (listBoxStorages.SelectedIndex == -1) return; + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowLocomotives(); } - private void FormLocomotiveCollection_Load(object sender, EventArgs e) + private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) { + pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowLocomotives(); } - private void LabelOnPanel_Click(object sender, EventArgs e) + private void buttonAddSet_Click(object sender, EventArgs e) { - + if (string.IsNullOrEmpty(textBoxSetName.Text)) + { + MessageBox.Show("Не всё заполнено", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(textBoxSetName.Text); + ReloadObjects(); } - private void panelLocomotiveCollection_Paint(object sender, PaintEventArgs e) + private void buttonDeleteSet_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(); + } } } } diff --git a/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs b/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs index aaa36a9..51de520 100644 --- a/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs +++ b/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericCollection.cs @@ -15,7 +15,7 @@ namespace ProjectElectricLocomotive.Generics private readonly int _pictureWidth; private readonly int _pictureHeight; private readonly int _placeSizeWidth = 200; - private readonly int _placeSizeHeight = 90; + private readonly int _placeSizeHeight = 120; private readonly SetGeneric _collection; public LocomotivesGenericCollection(int picWidth, int picHeight) { @@ -31,18 +31,20 @@ namespace ProjectElectricLocomotive.Generics { return -1; } - return collect?._collection.Insert(locomotive) ?? -1; + return collect._collection.Insert(locomotive); } - public static bool operator -(LocomotivesGenericCollection collect, int pos) + public static T? operator -(LocomotivesGenericCollection 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() { @@ -74,7 +76,7 @@ namespace ProjectElectricLocomotive.Generics int Wloco = _pictureWidth / _placeSizeWidth; for (int i = 0; i < _collection.Count; i++) { - T? type = _collection.Get(i); + T? type = _collection[i]; if (type != null) { type.SetPosition( diff --git a/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericStorage.cs b/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericStorage.cs new file mode 100644 index 0000000..f29cdff --- /dev/null +++ b/ElectricLocomotive/ElectricLocomotive/LocomotivesGenericStorage.cs @@ -0,0 +1,83 @@ +using ProjectElectricLocomotive.DrawingObjects; +using ProjectElectricLocomotive.Generics; +using ProjectElectricLocomotive.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive +{ + internal class LocomotivesGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _locomotivesStorage; + + /// + /// Возвращение списка названий наборов + /// + public List Keys => _locomotivesStorage.Keys.ToList(); + + + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + + /// + /// Конструктор + /// + /// + /// + public LocomotivesGenericStorage(int pictureWidth, int pictureHeight) + { + _locomotivesStorage = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + if (!_locomotivesStorage.ContainsKey(name)) + { + _locomotivesStorage.Add(name, new LocomotivesGenericCollection(_pictureWidth, _pictureHeight)); + } + } + + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (_locomotivesStorage.ContainsKey(name)) + { + _locomotivesStorage.Remove(name); + } + } + + + /// + /// Доступ к набору + /// + /// + /// + public LocomotivesGenericCollection? + this[string ind] + { + get + { + if (_locomotivesStorage.ContainsKey(ind)) + { + return _locomotivesStorage[ind]; + } + return null; + } + } + } +} diff --git a/ElectricLocomotive/ElectricLocomotive/SetGeneric.cs b/ElectricLocomotive/ElectricLocomotive/SetGeneric.cs index 60c65ce..7b52791 100644 --- a/ElectricLocomotive/ElectricLocomotive/SetGeneric.cs +++ b/ElectricLocomotive/ElectricLocomotive/SetGeneric.cs @@ -7,59 +7,70 @@ using System.Threading.Tasks; namespace ProjectElectricLocomotive.Generics { internal class SetGeneric - where T : class + where T : class { - private readonly T?[] _places; - public int Count => _places.Length; + private readonly List _places; + + 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 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 GetLocomotives(int? maxLocos = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxLocos.HasValue && i == maxLocos.Value) + { + yield break; + } + } } } }