diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericCollection.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericCollection.cs index 4113529..3bf46ba 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericCollection.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericCollection.cs @@ -21,24 +21,24 @@ namespace ProjectAirplaneWithRadar.Generics _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } - public static int operator +(AirplanesGenericCollection collect, T? obj) + public static bool operator +(AirplanesGenericCollection collect, T? obj) { if (obj == null || collect == null) { - return -1; + return false; } - return collect._collection.Insert(obj); + return collect?._collection.Insert(obj)??false; } public static bool operator -(AirplanesGenericCollection collect, int pos) { - T? obj = collect?._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null && collect != null) return collect._collection.Remove(pos); return false; } public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } public Bitmap ShowAirplanes() { @@ -66,15 +66,16 @@ namespace ProjectAirplaneWithRadar.Generics } private void DrawObjects(Graphics g) { - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach (var monorail in _collection.GetAirplanes()) { - DrawningAirplane? airplane = _collection.Get(i); - if (airplane != null) + if (monorail != null) { int inRow = _pictureWidth / _placeSizeWidth; - airplane.SetPosition(_pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth), i / inRow * _placeSizeHeight) ; - airplane.DrawTransport(g); + monorail.SetPosition(i % inRow * _placeSizeWidth, _pictureHeight - _pictureHeight % _placeSizeHeight - (i / inRow + 1) * _placeSizeHeight); + monorail.DrawTransport(g); } + i++; } } } diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericStorage.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericStorage.cs new file mode 100644 index 0000000..45b8e45 --- /dev/null +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/AirplanesGenericStorage.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectAirplaneWithRadar.DrawningObjects; +using ProjectAirplaneWithRadar.MovementStrategy; +using ProjectAirplaneWithRadar.Generics; + +namespace ProjectAirplaneWithRadar.Generics +{ + internal class AirplanesGenericStorage + { + readonly Dictionary> _airplanesStorages; + public List Keys => _airplanesStorages.Keys.ToList(); + private readonly int _pictureWidth; + private readonly int _pictureHeight; + public AirplanesGenericStorage(int pictureWidth, int pictureHeight) + { + _airplanesStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public void AddSet(string name) + { + _airplanesStorages.Add(name, new AirplanesGenericCollection(_pictureWidth, _pictureHeight)); + } + public void DelSet(string name) + { + if (!_airplanesStorages.ContainsKey(name)) + return; + _airplanesStorages.Remove(name); + } + public AirplanesGenericCollection?this[string ind] + { + get + { + if (_airplanesStorages.ContainsKey(ind)) + return _airplanesStorages[ind]; + return null; + } + } + + } +} diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.Designer.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.Designer.cs index f3a34f6..47c5322 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.Designer.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.Designer.cs @@ -30,12 +30,18 @@ { pictureBoxAirplanesCollection = new PictureBox(); groupBoxAirplaneWithRadar = new GroupBox(); + groupBoxCollection = new GroupBox(); + buttonRemoveObject = new Button(); + listBoxStorages = new ListBox(); + buttonAddObject = new Button(); + textBoxStorageName = new TextBox(); buttonUpdateCollection = new Button(); buttonDeleteAirplane = new Button(); buttonAddAirplane = new Button(); maskedTextBoxNumber = new MaskedTextBox(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirplanesCollection).BeginInit(); groupBoxAirplaneWithRadar.SuspendLayout(); + groupBoxCollection.SuspendLayout(); SuspendLayout(); // // pictureBoxAirplanesCollection @@ -49,21 +55,72 @@ // // groupBoxAirplaneWithRadar // + groupBoxAirplaneWithRadar.Controls.Add(groupBoxCollection); groupBoxAirplaneWithRadar.Controls.Add(buttonUpdateCollection); groupBoxAirplaneWithRadar.Controls.Add(buttonDeleteAirplane); groupBoxAirplaneWithRadar.Controls.Add(buttonAddAirplane); groupBoxAirplaneWithRadar.Controls.Add(maskedTextBoxNumber); groupBoxAirplaneWithRadar.Dock = DockStyle.Right; - groupBoxAirplaneWithRadar.Location = new Point(637, 0); + groupBoxAirplaneWithRadar.Location = new Point(640, 0); groupBoxAirplaneWithRadar.Name = "groupBoxAirplaneWithRadar"; - groupBoxAirplaneWithRadar.Size = new Size(250, 454); + groupBoxAirplaneWithRadar.Size = new Size(250, 453); groupBoxAirplaneWithRadar.TabIndex = 1; groupBoxAirplaneWithRadar.TabStop = false; groupBoxAirplaneWithRadar.Text = "Инструменты"; // + // groupBoxCollection + // + groupBoxCollection.Controls.Add(buttonRemoveObject); + groupBoxCollection.Controls.Add(listBoxStorages); + groupBoxCollection.Controls.Add(buttonAddObject); + groupBoxCollection.Controls.Add(textBoxStorageName); + groupBoxCollection.Location = new Point(7, 26); + groupBoxCollection.Name = "groupBoxCollection"; + groupBoxCollection.Size = new Size(237, 271); + groupBoxCollection.TabIndex = 4; + groupBoxCollection.TabStop = false; + groupBoxCollection.Text = "Наборы"; + // + // buttonRemoveObject + // + buttonRemoveObject.Location = new Point(6, 225); + buttonRemoveObject.Name = "buttonRemoveObject"; + buttonRemoveObject.Size = new Size(227, 32); + buttonRemoveObject.TabIndex = 3; + buttonRemoveObject.Text = "Удалить набор"; + buttonRemoveObject.UseVisualStyleBackColor = true; + buttonRemoveObject.Click += buttonRemoveObject_Click; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 20; + listBoxStorages.Location = new Point(6, 95); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(227, 124); + listBoxStorages.TabIndex = 2; + listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; + // + // buttonAddObject + // + buttonAddObject.Location = new Point(6, 59); + buttonAddObject.Name = "buttonAddObject"; + buttonAddObject.Size = new Size(226, 30); + buttonAddObject.TabIndex = 1; + buttonAddObject.Text = "Добавить набор"; + buttonAddObject.UseVisualStyleBackColor = true; + buttonAddObject.Click += buttonAddObject_Click; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(6, 26); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(225, 27); + textBoxStorageName.TabIndex = 0; + // // buttonUpdateCollection // - buttonUpdateCollection.Location = new Point(6, 275); + buttonUpdateCollection.Location = new Point(6, 418); buttonUpdateCollection.Name = "buttonUpdateCollection"; buttonUpdateCollection.Size = new Size(238, 29); buttonUpdateCollection.TabIndex = 3; @@ -73,7 +130,7 @@ // // buttonDeleteAirplane // - buttonDeleteAirplane.Location = new Point(6, 208); + buttonDeleteAirplane.Location = new Point(7, 383); buttonDeleteAirplane.Name = "buttonDeleteAirplane"; buttonDeleteAirplane.Size = new Size(238, 29); buttonDeleteAirplane.TabIndex = 2; @@ -83,7 +140,7 @@ // // buttonAddAirplane // - buttonAddAirplane.Location = new Point(6, 26); + buttonAddAirplane.Location = new Point(6, 315); buttonAddAirplane.Name = "buttonAddAirplane"; buttonAddAirplane.Size = new Size(238, 29); buttonAddAirplane.TabIndex = 1; @@ -93,7 +150,7 @@ // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(67, 92); + maskedTextBoxNumber.Location = new Point(64, 350); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(125, 27); maskedTextBoxNumber.TabIndex = 0; @@ -102,7 +159,7 @@ // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(887, 454); + ClientSize = new Size(890, 453); Controls.Add(groupBoxAirplaneWithRadar); Controls.Add(pictureBoxAirplanesCollection); Name = "FormAirplanesCollection"; @@ -110,6 +167,8 @@ ((System.ComponentModel.ISupportInitialize)pictureBoxAirplanesCollection).EndInit(); groupBoxAirplaneWithRadar.ResumeLayout(false); groupBoxAirplaneWithRadar.PerformLayout(); + groupBoxCollection.ResumeLayout(false); + groupBoxCollection.PerformLayout(); ResumeLayout(false); PerformLayout(); } @@ -122,5 +181,10 @@ private Button buttonAddAirplane; private MaskedTextBox maskedTextBoxNumber; private Button buttonUpdateCollection; + private GroupBox groupBoxCollection; + private Button buttonRemoveObject; + private ListBox listBoxStorages; + private Button buttonAddObject; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs index 0664ae6..f2ec634 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplanesCollection.cs @@ -6,22 +6,51 @@ namespace ProjectAirplaneWithRadar { public partial class FormAirplanesCollection : System.Windows.Forms.Form { - private readonly AirplanesGenericCollection _Airplanes; + private readonly AirplanesGenericStorage _storage; public FormAirplanesCollection() { InitializeComponent(); - _Airplanes = new AirplanesGenericCollection(pictureBoxAirplanesCollection.Width, pictureBoxAirplanesCollection.Height); + _storage = new AirplanesGenericStorage(pictureBoxAirplanesCollection.Width, pictureBoxAirplanesCollection.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 buttonAddAirplane_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } FormAirplaneWithRadar form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_Airplanes + form.SelectedAirplane != null) + if (obj + form.SelectedAirplane) { MessageBox.Show(" "); - pictureBoxAirplanesCollection.Image = _Airplanes.ShowAirplanes(); + pictureBoxAirplanesCollection.Image = obj.ShowAirplanes(); } else { @@ -31,25 +60,75 @@ namespace ProjectAirplaneWithRadar } private void buttonDeleteAirplane_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 (_Airplanes - pos != null) + if (obj - pos != null) { MessageBox.Show(" "); - pictureBoxAirplanesCollection.Image = _Airplanes.ShowAirplanes(); + pictureBoxAirplanesCollection.Image = obj.ShowAirplanes(); } else { MessageBox.Show(" "); } - } private void buttonUpdateCollection_Click(object sender, EventArgs e) { - pictureBoxAirplanesCollection.Image = _Airplanes.ShowAirplanes(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxAirplanesCollection.Image = obj.ShowAirplanes(); + } + + 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 buttonRemoveObject_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 listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxAirplanesCollection.Image =_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowAirplanes(); } } } \ No newline at end of file diff --git a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/SetGeneric.cs b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/SetGeneric.cs index 2f5dee8..4b90b21 100644 --- a/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/SetGeneric.cs +++ b/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar/SetGeneric.cs @@ -8,47 +8,61 @@ namespace ProjectAirplaneWithRadar.Generics internal class SetGeneric 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 airplane) + public bool Insert(T airplane) { - if (_places[Count - 1] != null) - return -1; - return Insert(airplane, 0); + if (_places.Count == _maxCount) + return false; + Insert(airplane, 0); + return true; } - public int Insert(T airplane, int position) + public bool Insert(T airplane, 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]; - } - _places[position] = airplane; - return position; + if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) + return false; + _places.Insert(position, airplane); + 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] { - 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 && _places.Count < _maxCount)) + return; + _places.Insert(position, value); + return; + } + } + public IEnumerable GetAirplanes(int? maxMonorails = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxMonorails.HasValue && i == maxMonorails.Value) + { + yield break; + } + } } } }