From 88833f3f180bee324d9ef9460a9c9776ea418761 Mon Sep 17 00:00:00 2001 From: Extrimal Date: Sun, 12 Nov 2023 23:43:30 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=83=D0=B3=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=B04?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AircraftsGenericCollection.cs | 25 +-- .../AircraftsGenericStorage.cs | 15 +- .../AircraftCarrier/FormAircraftCarrier.cs | 3 - .../FormAircraftCollection.Designer.cs | 147 +++++++++--------- .../AircraftCarrier/FormAircraftCollection.cs | 97 ++++++++++-- AircraftCarrier/AircraftCarrier/SetGeneric.cs | 32 ++-- 6 files changed, 203 insertions(+), 116 deletions(-) diff --git a/AircraftCarrier/AircraftCarrier/AircraftsGenericCollection.cs b/AircraftCarrier/AircraftCarrier/AircraftsGenericCollection.cs index 13d8a2a..d2ca4b9 100644 --- a/AircraftCarrier/AircraftCarrier/AircraftsGenericCollection.cs +++ b/AircraftCarrier/AircraftCarrier/AircraftsGenericCollection.cs @@ -43,24 +43,22 @@ namespace AircraftCarrier.Generics return collect._collection.Insert(obj); } /// Перегрузка оператора вычитания - public static T? operator -(AircraftsGenericCollection collect, int pos) + public static bool operator -(AircraftsGenericCollection collect, int pos) { T? obj = collect._collection[pos]; if (obj != null) { - collect._collection.Remove(pos); + return collect._collection.Remove(pos); } - return obj; + return false; } - /// Получение объекта IMoveableObject public U? GetU(int pos) { return (U?)_collection[pos]?.GetMoveableObject; } - /// Вывод всего набора объектов - public Bitmap ShowAircraft() + public Bitmap ShowAircrafts() { Bitmap bmp = new(_pictureWidth, _pictureHeight); Graphics gr = Graphics.FromImage(bmp); @@ -89,15 +87,18 @@ namespace AircraftCarrier.Generics /// Метод прорисовки объектов private void DrawObjects(Graphics g) { - foreach (var car in _collection.GetAircrafts()) + int Width = _pictureWidth / _placeSizeWidth; + int Height = _pictureHeight / _placeSizeHeight; + int i = 0; + foreach (var aircraft in _collection.GetAircrafts()) { - if (car != null) + if (aircraft != null) { - // TODO получение объекта - // TODO установка позиции - // TODO прорисовка объекта - car.DrawTransport(g); + + aircraft.SetPosition(i % Width * _placeSizeWidth + 3, 600); + aircraft.DrawTransport(g); } + i++; } } } diff --git a/AircraftCarrier/AircraftCarrier/AircraftsGenericStorage.cs b/AircraftCarrier/AircraftCarrier/AircraftsGenericStorage.cs index c61bce3..2a5bd08 100644 --- a/AircraftCarrier/AircraftCarrier/AircraftsGenericStorage.cs +++ b/AircraftCarrier/AircraftCarrier/AircraftsGenericStorage.cs @@ -30,12 +30,18 @@ namespace AircraftCarrier.Generics /// Добавление набора public void AddSet(string name) { - // TODO Прописать логику для добавления + if (!_AircraftStorages.ContainsKey(name)) + { + _AircraftStorages.Add(name, new AircraftsGenericCollection(_pictureWidth, _pictureHeight)); + } } /// Удаление набора public void DelSet(string name) { - // TODO Прописать логику для удаления + if (_AircraftStorages.ContainsKey(name)) + { + _AircraftStorages.Remove(name); + } } /// Доступ к набору public AircraftsGenericCollection? @@ -43,7 +49,10 @@ namespace AircraftCarrier.Generics { get { - // TODO Продумать логику получения набора + if (_AircraftStorages.ContainsKey(ind)) + { + return _AircraftStorages[ind]; + } return null; } } diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs index 0d6d11e..c49d400 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCarrier.cs @@ -50,7 +50,6 @@ namespace AircraftCarrier } Draw(); } - private void ButtonCreateAircraft_Click(object sender, EventArgs e) { Random random = new(); @@ -67,7 +66,6 @@ namespace AircraftCarrier 100)); Draw(); } - private void ButtonCreateAircraftCarrier_Click(object sender, EventArgs e) { Random random = new(); @@ -90,7 +88,6 @@ namespace AircraftCarrier _drawingAircraft.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } - private void buttonStep_Click(object sender, EventArgs e) { if (_drawingAircraft == null) diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCollection.Designer.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCollection.Designer.cs index add90e1..365da19 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCollection.Designer.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCollection.Designer.cs @@ -28,31 +28,31 @@ /// private void InitializeComponent() { - PictureBoxCollection = new PictureBox(); + pictureBoxCollection = new PictureBox(); PanelTools = new Panel(); + labelTools = new Label(); PanelSets = new Panel(); + ButtonRemoveObject = new Button(); + listBoxStorage = new ListBox(); + ButtonAddObject = new Button(); + textBoxStorageName = new TextBox(); + labelSets = new Label(); ButtonRefreshCollection = new Button(); ButtonRemoveAircraft = new Button(); MaskedTextBoxNumber = new MaskedTextBox(); ButtonAddAircraft = new Button(); - labelTools = new Label(); - labelSets = new Label(); - textBoxStorageName = new TextBox(); - ButtonAddObject = new Button(); - listBoxStorage = new ListBox(); - ButtonRemoveObject = new Button(); - ((System.ComponentModel.ISupportInitialize)PictureBoxCollection).BeginInit(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); PanelTools.SuspendLayout(); PanelSets.SuspendLayout(); SuspendLayout(); // - // PictureBoxCollection + // pictureBoxCollection // - PictureBoxCollection.Location = new Point(0, 0); - PictureBoxCollection.Name = "PictureBoxCollection"; - PictureBoxCollection.Size = new Size(768, 651); - PictureBoxCollection.TabIndex = 0; - PictureBoxCollection.TabStop = false; + pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(793, 724); + pictureBoxCollection.TabIndex = 0; + pictureBoxCollection.TabStop = false; // // PanelTools // @@ -63,11 +63,20 @@ PanelTools.Controls.Add(ButtonRemoveAircraft); PanelTools.Controls.Add(MaskedTextBoxNumber); PanelTools.Controls.Add(ButtonAddAircraft); - PanelTools.Location = new Point(774, 12); + PanelTools.Location = new Point(799, 12); PanelTools.Name = "PanelTools"; - PanelTools.Size = new Size(232, 639); + PanelTools.Size = new Size(232, 712); PanelTools.TabIndex = 1; // + // labelTools + // + labelTools.AutoSize = true; + labelTools.Location = new Point(16, -4); + labelTools.Name = "labelTools"; + labelTools.Size = new Size(103, 20); + labelTools.TabIndex = 5; + labelTools.Text = "Инструменты"; + // // PanelSets // PanelSets.Controls.Add(ButtonRemoveObject); @@ -80,6 +89,52 @@ PanelSets.Size = new Size(204, 339); PanelSets.TabIndex = 4; // + // ButtonRemoveObject + // + ButtonRemoveObject.Location = new Point(20, 286); + ButtonRemoveObject.Name = "ButtonRemoveObject"; + ButtonRemoveObject.Size = new Size(168, 41); + ButtonRemoveObject.TabIndex = 4; + ButtonRemoveObject.Text = "Удалить набор"; + ButtonRemoveObject.UseVisualStyleBackColor = true; + ButtonRemoveObject.Click += ButtonRemoveObject_Click; + // + // listBoxStorage + // + listBoxStorage.FormattingEnabled = true; + listBoxStorage.ItemHeight = 20; + listBoxStorage.Location = new Point(29, 147); + listBoxStorage.Name = "listBoxStorage"; + listBoxStorage.Size = new Size(150, 124); + listBoxStorage.TabIndex = 3; + listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged; + // + // ButtonAddObject + // + ButtonAddObject.Location = new Point(20, 83); + ButtonAddObject.Name = "ButtonAddObject"; + ButtonAddObject.Size = new Size(168, 41); + ButtonAddObject.TabIndex = 2; + ButtonAddObject.Text = "Добавить набор"; + ButtonAddObject.UseVisualStyleBackColor = true; + ButtonAddObject.Click += ButtonAddObject_Click; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(41, 41); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(125, 27); + textBoxStorageName.TabIndex = 1; + // + // labelSets + // + labelSets.AutoSize = true; + labelSets.Location = new Point(11, 9); + labelSets.Name = "labelSets"; + labelSets.Size = new Size(55, 20); + labelSets.TabIndex = 0; + labelSets.Text = "Набор"; + // // ButtonRefreshCollection // ButtonRefreshCollection.Location = new Point(36, 572); @@ -118,68 +173,16 @@ ButtonAddAircraft.UseVisualStyleBackColor = true; ButtonAddAircraft.Click += ButtonAddAircraft_Click; // - // labelTools - // - labelTools.AutoSize = true; - labelTools.Location = new Point(16, -4); - labelTools.Name = "labelTools"; - labelTools.Size = new Size(103, 20); - labelTools.TabIndex = 5; - labelTools.Text = "Инструменты"; - // - // labelSets - // - labelSets.AutoSize = true; - labelSets.Location = new Point(11, 9); - labelSets.Name = "labelSets"; - labelSets.Size = new Size(55, 20); - labelSets.TabIndex = 0; - labelSets.Text = "Набор"; - // - // textBoxStorageName - // - textBoxStorageName.Location = new Point(41, 41); - textBoxStorageName.Name = "textBoxStorageName"; - textBoxStorageName.Size = new Size(125, 27); - textBoxStorageName.TabIndex = 1; - // - // ButtonAddObject - // - ButtonAddObject.Location = new Point(20, 83); - ButtonAddObject.Name = "ButtonAddObject"; - ButtonAddObject.Size = new Size(168, 41); - ButtonAddObject.TabIndex = 2; - ButtonAddObject.Text = "Добавить набор"; - ButtonAddObject.UseVisualStyleBackColor = true; - // - // listBoxStorage - // - listBoxStorage.FormattingEnabled = true; - listBoxStorage.ItemHeight = 20; - listBoxStorage.Location = new Point(29, 147); - listBoxStorage.Name = "listBoxStorage"; - listBoxStorage.Size = new Size(150, 124); - listBoxStorage.TabIndex = 3; - // - // ButtonRemoveObject - // - ButtonRemoveObject.Location = new Point(20, 286); - ButtonRemoveObject.Name = "ButtonRemoveObject"; - ButtonRemoveObject.Size = new Size(168, 41); - ButtonRemoveObject.TabIndex = 4; - ButtonRemoveObject.Text = "Удалить набор"; - ButtonRemoveObject.UseVisualStyleBackColor = true; - // // FormAircraftCollection // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1007, 651); + ClientSize = new Size(1033, 724); Controls.Add(PanelTools); - Controls.Add(PictureBoxCollection); + Controls.Add(pictureBoxCollection); Name = "FormAircraftCollection"; Text = "FormAircraftCollection"; - ((System.ComponentModel.ISupportInitialize)PictureBoxCollection).EndInit(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); PanelTools.ResumeLayout(false); PanelTools.PerformLayout(); PanelSets.ResumeLayout(false); @@ -189,7 +192,7 @@ #endregion - private PictureBox PictureBoxCollection; + private PictureBox pictureBoxCollection; private Panel PanelTools; private MaskedTextBox MaskedTextBoxNumber; private Button ButtonAddAircraft; diff --git a/AircraftCarrier/AircraftCarrier/FormAircraftCollection.cs b/AircraftCarrier/AircraftCarrier/FormAircraftCollection.cs index 89e381f..f4ebfd2 100644 --- a/AircraftCarrier/AircraftCarrier/FormAircraftCollection.cs +++ b/AircraftCarrier/AircraftCarrier/FormAircraftCollection.cs @@ -14,40 +14,107 @@ namespace AircraftCarrier { public partial class FormAircraftCollection : Form { - private readonly AircraftsGenericCollection _Aircrafts; + private readonly AircraftsGenericStorage _storage; public FormAircraftCollection() { InitializeComponent(); - _Aircrafts = new AircraftsGenericCollection(PictureBoxCollection.Width, PictureBoxCollection.Height); + _storage = new AircraftsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + /// Заполнение listBoxObjects + private void ReloadObjects() + { + int index = listBoxStorage.SelectedIndex; + listBoxStorage.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) + { + listBoxStorage.Items.Add(_storage.Keys[i]); + } + if (listBoxStorage.Items.Count > 0 && (index == -1 || index >= listBoxStorage.Items.Count)) + { + listBoxStorage.SelectedIndex = 0; + } + else if (listBoxStorage.Items.Count > 0 && index > -1 && index < listBoxStorage.Items.Count) + { + listBoxStorage.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 listBoxStorage_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = _storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowAircrafts(); + } + private void ButtonRemoveObject_Click(object sender, EventArgs e) + { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить объект{listBoxStorage.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, + MessageBoxIcon.Question) == DialogResult.Yes) + { + _storage.DelSet(listBoxStorage.SelectedItem.ToString() + ?? string.Empty); + ReloadObjects(); + } } private void ButtonAddAircraft_Click(object sender, EventArgs e) { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + FormAircraftCarrier form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_Aircrafts + form.SelectedAircraft != -1) + if (obj + form.SelectedAircraft != -1) { MessageBox.Show("Объект добавлен"); - PictureBoxCollection.Image = _Aircrafts.ShowAircraft(); + pictureBoxCollection.Image = obj.ShowAircrafts(); } else { MessageBox.Show("Не удалось добавить объект"); } } - } private void ButtonRemoveAircraft_Click(object sender, EventArgs e) { - if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.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 (_Aircrafts - pos != null) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - PictureBoxCollection.Image = _Aircrafts.ShowAircraft(); + pictureBoxCollection.Image = obj.ShowAircrafts(); } else { @@ -56,9 +123,17 @@ namespace AircraftCarrier } private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - PictureBoxCollection.Image = _Aircrafts.ShowAircraft(); + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowAircrafts(); } - - } } diff --git a/AircraftCarrier/AircraftCarrier/SetGeneric.cs b/AircraftCarrier/AircraftCarrier/SetGeneric.cs index 4ad9700..fdf0310 100644 --- a/AircraftCarrier/AircraftCarrier/SetGeneric.cs +++ b/AircraftCarrier/AircraftCarrier/SetGeneric.cs @@ -21,25 +21,25 @@ namespace AircraftCarrier.Generics _places = new List(count); } /// Добавление объекта в набор - public bool Insert(T Aircraft) + public int Insert(T Aircraft) { - // TODO вставка в начало набора - return true; + return Insert(Aircraft, 0); } /// Добавление объекта в набор на конкретную позицию - public bool Insert(T Aircraft, int position) + public int Insert(T Aircraft, int position) { - // TODO проверка позиции - // TODO проверка, что есть место для вставки - // TODO вставка по позиции - _places[position] = Aircraft; - return true; + if (position < 0 || position > Count || Count >= _maxCount) + return -1; + + _places.Insert(position, Aircraft); + return position; } /// Удаление объекта из набора с конкретной позиции public bool Remove(int position) { - // TODO проверка позиции - // TODO удаление объекта из списка + if (position < 0 || position >= Count) + return false; + _places.RemoveAt(position); return true; } /// Получение объекта из набора по позиции @@ -47,14 +47,16 @@ namespace AircraftCarrier.Generics { get { - // TODO проверка позиции + if (position < 0 || position >= Count) + return null; return _places[position]; } set { - // TODO проверка позиции - // TODO проверка свободных мест в списке - // TODO вставка в список по позиции + if (position < 0 || position > Count || Count >= _maxCount) + return; + + _places.Insert(position, value); } } /// Проход по списку