From 1dd901762a0bbc8cbed22a5c6fce76e76e1e5812 Mon Sep 17 00:00:00 2001 From: "nikbel2004@outlook.com" Date: Sat, 28 Oct 2023 21:14:08 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tank/Tank/FormTank.Designer.cs | 1 + Tank/Tank/FormTank.cs | 5 + Tank/Tank/FormTanksCollections.Designer.cs | 116 ++++++++++++++++++--- Tank/Tank/FormTanksCollections.cs | 108 ++++++++++++++++--- Tank/Tank/FormTanksCollections.resx | 2 +- Tank/Tank/SetGeneric.cs | 98 ++++++++--------- Tank/Tank/TanksGenericCollection.cs | 42 ++++---- Tank/Tank/TanksGenericStorage.cs | 46 ++++++++ 8 files changed, 312 insertions(+), 106 deletions(-) create mode 100644 Tank/Tank/TanksGenericStorage.cs diff --git a/Tank/Tank/FormTank.Designer.cs b/Tank/Tank/FormTank.Designer.cs index 4a148c0..0f9e611 100644 --- a/Tank/Tank/FormTank.Designer.cs +++ b/Tank/Tank/FormTank.Designer.cs @@ -173,6 +173,7 @@ Margin = new Padding(3, 4, 3, 4); Name = "FormTank"; Text = "FormTank"; + Load += FormTank_Load; ((System.ComponentModel.ISupportInitialize)pictureBoxTank).EndInit(); ResumeLayout(false); } diff --git a/Tank/Tank/FormTank.cs b/Tank/Tank/FormTank.cs index aae6222..8d1fe16 100644 --- a/Tank/Tank/FormTank.cs +++ b/Tank/Tank/FormTank.cs @@ -128,5 +128,10 @@ namespace Tank SelectedTank = _Tank; DialogResult = DialogResult.OK; } + + private void FormTank_Load(object sender, EventArgs e) + { + + } } } \ No newline at end of file diff --git a/Tank/Tank/FormTanksCollections.Designer.cs b/Tank/Tank/FormTanksCollections.Designer.cs index d93389d..3b605e4 100644 --- a/Tank/Tank/FormTanksCollections.Designer.cs +++ b/Tank/Tank/FormTanksCollections.Designer.cs @@ -29,6 +29,12 @@ private void InitializeComponent() { panel1 = new Panel(); + panel2 = new Panel(); + DeleteCollectButton = new Button(); + CollectionListBox = new ListBox(); + AddCollectButton = new Button(); + SetTextBox = new TextBox(); + label2 = new Label(); UpdateButton = new Button(); DeleteButton = new Button(); AddButton = new Button(); @@ -36,27 +42,94 @@ label1 = new Label(); DrawTank = new PictureBox(); panel1.SuspendLayout(); + panel2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)DrawTank).BeginInit(); SuspendLayout(); // // panel1 // + panel1.Controls.Add(panel2); panel1.Controls.Add(UpdateButton); panel1.Controls.Add(DeleteButton); panel1.Controls.Add(AddButton); panel1.Controls.Add(InputNum); panel1.Controls.Add(label1); panel1.Dock = DockStyle.Right; - panel1.Location = new Point(550, 0); + panel1.Location = new Point(568, 0); + panel1.Margin = new Padding(3, 2, 3, 2); panel1.Name = "panel1"; - panel1.Size = new Size(250, 451); + panel1.Size = new Size(207, 429); panel1.TabIndex = 0; // + // panel2 + // + panel2.Controls.Add(DeleteCollectButton); + panel2.Controls.Add(CollectionListBox); + panel2.Controls.Add(AddCollectButton); + panel2.Controls.Add(SetTextBox); + panel2.Controls.Add(label2); + panel2.Location = new Point(14, 19); + panel2.Margin = new Padding(3, 2, 3, 2); + panel2.Name = "panel2"; + panel2.Size = new Size(187, 165); + panel2.TabIndex = 6; + // + // DeleteCollectButton + // + DeleteCollectButton.Location = new Point(3, 136); + DeleteCollectButton.Margin = new Padding(3, 2, 3, 2); + DeleteCollectButton.Name = "DeleteCollectButton"; + DeleteCollectButton.Size = new Size(182, 22); + DeleteCollectButton.TabIndex = 4; + DeleteCollectButton.Text = "Удалить набор"; + DeleteCollectButton.UseVisualStyleBackColor = true; + DeleteCollectButton.Click += ButtonDelObject_Click; + // + // CollectionListBox + // + CollectionListBox.FormattingEnabled = true; + CollectionListBox.ItemHeight = 15; + CollectionListBox.Location = new Point(3, 68); + CollectionListBox.Margin = new Padding(3, 2, 3, 2); + CollectionListBox.Name = "CollectionListBox"; + CollectionListBox.Size = new Size(182, 64); + CollectionListBox.TabIndex = 3; + CollectionListBox.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged; + // + // AddCollectButton + // + AddCollectButton.Location = new Point(3, 42); + AddCollectButton.Margin = new Padding(3, 2, 3, 2); + AddCollectButton.Name = "AddCollectButton"; + AddCollectButton.Size = new Size(182, 22); + AddCollectButton.TabIndex = 2; + AddCollectButton.Text = "Добавить набор"; + AddCollectButton.UseVisualStyleBackColor = true; + AddCollectButton.Click += ButtonAddObject_Click; + // + // SetTextBox + // + SetTextBox.Location = new Point(2, 17); + SetTextBox.Margin = new Padding(3, 2, 3, 2); + SetTextBox.Name = "SetTextBox"; + SetTextBox.Size = new Size(183, 23); + SetTextBox.TabIndex = 1; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(3, 0); + label2.Name = "label2"; + label2.Size = new Size(52, 15); + label2.TabIndex = 0; + label2.Text = "Наборы"; + // // UpdateButton // - UpdateButton.Location = new Point(10, 241); + UpdateButton.Location = new Point(9, 283); + UpdateButton.Margin = new Padding(3, 2, 3, 2); UpdateButton.Name = "UpdateButton"; - UpdateButton.Size = new Size(229, 37); + UpdateButton.Size = new Size(192, 28); UpdateButton.TabIndex = 4; UpdateButton.Text = "Обновить коллекцию"; UpdateButton.UseVisualStyleBackColor = true; @@ -64,9 +137,10 @@ // // DeleteButton // - DeleteButton.Location = new Point(10, 176); + DeleteButton.Location = new Point(9, 253); + DeleteButton.Margin = new Padding(3, 2, 3, 2); DeleteButton.Name = "DeleteButton"; - DeleteButton.Size = new Size(229, 37); + DeleteButton.Size = new Size(192, 28); DeleteButton.TabIndex = 3; DeleteButton.Text = "Удалить технику"; DeleteButton.UseVisualStyleBackColor = true; @@ -74,9 +148,10 @@ // // AddButton // - AddButton.Location = new Point(10, 53); + AddButton.Location = new Point(9, 194); + AddButton.Margin = new Padding(3, 2, 3, 2); AddButton.Name = "AddButton"; - AddButton.Size = new Size(229, 37); + AddButton.Size = new Size(192, 28); AddButton.TabIndex = 2; AddButton.Text = "Добавить технику"; AddButton.UseVisualStyleBackColor = true; @@ -84,17 +159,18 @@ // // InputNum // - InputNum.Location = new Point(10, 133); + InputNum.Location = new Point(9, 225); + InputNum.Margin = new Padding(3, 2, 3, 2); InputNum.Name = "InputNum"; - InputNum.Size = new Size(228, 27); + InputNum.Size = new Size(192, 23); InputNum.TabIndex = 1; // // label1 // label1.AutoSize = true; - label1.Location = new Point(2, 3); + label1.Location = new Point(2, 2); label1.Name = "label1"; - label1.Size = new Size(103, 20); + label1.Size = new Size(83, 15); label1.TabIndex = 0; label1.Text = "Инструменты"; // @@ -102,22 +178,26 @@ // DrawTank.Dock = DockStyle.Fill; DrawTank.Location = new Point(0, 0); + DrawTank.Margin = new Padding(3, 2, 3, 2); DrawTank.Name = "DrawTank"; - DrawTank.Size = new Size(550, 451); + DrawTank.Size = new Size(568, 429); DrawTank.TabIndex = 1; DrawTank.TabStop = false; // // CollectionsFrame // - AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 451); + ClientSize = new Size(775, 429); Controls.Add(DrawTank); Controls.Add(panel1); + Margin = new Padding(3, 2, 3, 2); Name = "CollectionsFrame"; Text = "CollectionsFrame"; panel1.ResumeLayout(false); panel1.PerformLayout(); + panel2.ResumeLayout(false); + panel2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)DrawTank).EndInit(); ResumeLayout(false); } @@ -131,5 +211,11 @@ private TextBox InputNum; private Label label1; private PictureBox DrawTank; + private Panel panel2; + private Button DeleteCollectButton; + private ListBox CollectionListBox; + private Button AddCollectButton; + private TextBox SetTextBox; + private Label label2; } } \ No newline at end of file diff --git a/Tank/Tank/FormTanksCollections.cs b/Tank/Tank/FormTanksCollections.cs index 4aef8b2..e29f71a 100644 --- a/Tank/Tank/FormTanksCollections.cs +++ b/Tank/Tank/FormTanksCollections.cs @@ -15,23 +15,86 @@ namespace Tank { public partial class FormTanksCollections : Form { - private readonly TanksGenericCollection _tanks; + private readonly TanksGenericStorage _storage; + // Конструктор public FormTanksCollections() { InitializeComponent(); - _tanks = new TanksGenericCollection(DrawTank.Width, DrawTank.Height); + _storage = new TanksGenericStorage(DrawTank.Width, DrawTank.Height); + } + + private void ReloadObjects() + { + int index = CollectionListBox.SelectedIndex; + CollectionListBox.Items.Clear(); + foreach (var key in _storage.Keys) + { + CollectionListBox.Items.Add(key); + } + if (CollectionListBox.Items.Count > 0 && (index == -1 || index + >= CollectionListBox.Items.Count)) + { + CollectionListBox.SelectedIndex = 0; + } + else if (CollectionListBox.Items.Count > 0 && index > -1 && + index < CollectionListBox.Items.Count) + { + CollectionListBox.SelectedIndex = index; + } + } + + private void ButtonAddObject_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(SetTextBox.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(SetTextBox.Text); + ReloadObjects(); + } + + private void ListBoxObjects_SelectedIndexChanged(object sender, + EventArgs e) + { + DrawTank.Image = + _storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowTanks(); + } + + private void ButtonDelObject_Click(object sender, EventArgs e) + { + if (CollectionListBox.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить объект {CollectionListBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, + MessageBoxIcon.Question) == DialogResult.Yes) + { + _storage.DelSet(CollectionListBox.SelectedItem.ToString() ?? string.Empty); + ReloadObjects(); + } } private void ButtonAddTank_Click(object sender, EventArgs e) { - FormTank form = new FormTank(); + if (CollectionListBox.SelectedIndex == -1) + { + return; + } + var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + FormTank form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_tanks + form.SelectedTank != -1) + if (obj + form.SelectedTank) { MessageBox.Show("Объект добавлен"); - DrawTank.Image = _tanks.ShowTanks(); + DrawTank.Image = obj.ShowTanks(); } else { @@ -41,21 +104,27 @@ namespace Tank } private void ButtonRemoveTank_Click(object sender, EventArgs e) { + if (CollectionListBox.SelectedIndex == -1) + { + return; + } + var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } - int pos = -1; - try - { - pos = Convert.ToInt32(InputNum.Text); - } - catch (Exception ex) { } - if (_tanks - pos) + int pos = Convert.ToInt32(InputNum.Text); + if (obj - pos != null) { MessageBox.Show("Объект удален"); - DrawTank.Image = _tanks.ShowTanks(); + DrawTank.Image = obj.ShowTanks(); } else { @@ -65,7 +134,18 @@ namespace Tank private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - DrawTank.Image = _tanks.ShowTanks(); + if (CollectionListBox.SelectedIndex == -1) + { + return; + } + var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + DrawTank.Image = obj.ShowTanks(); } + } } diff --git a/Tank/Tank/FormTanksCollections.resx b/Tank/Tank/FormTanksCollections.resx index a395bff..af32865 100644 --- a/Tank/Tank/FormTanksCollections.resx +++ b/Tank/Tank/FormTanksCollections.resx @@ -18,7 +18,7 @@ System.Resources.ResXResourceReader, System.Windows.Forms, ... System.Resources.ResXResourceWriter, System.Windows.Forms, ... this is my long stringthis is a comment - Blue + Blue [base64 mime encoded serialized .NET Framework object] diff --git a/Tank/Tank/SetGeneric.cs b/Tank/Tank/SetGeneric.cs index cf12286..5a3d429 100644 --- a/Tank/Tank/SetGeneric.cs +++ b/Tank/Tank/SetGeneric.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.VisualBasic; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,84 +9,69 @@ namespace Tank { internal class SetGeneric where T : class { - // Массив объектов, которые храним - private readonly T?[] _places; + private readonly List _places; + public int Count => _places.Count; - // Количество объектов в массиве - public int Count => _places.Length; + private readonly int _maxCount; - // Конструктор public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(_maxCount); } - //Добавление объекта в набор - public int Insert(T tank) + public bool Insert(T tank) { - 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] = tank; - return 0; + return Insert(tank, 0); } - // Добавление объекта в набор на конкретную позицию public bool Insert(T tank, int position) { - if (position < 0 || position >= Count) + if (position < 0 || position >= _maxCount) return false; - if (_places[position] == null) - { - _places[position] = tank; - return true; - } - int index = -1; - for(int i = position; i < Count; i++) - { - if (_places[i] == null) - { - index = i; break; - } - } - if (index < 0) + + if (Count >= _maxCount) return false; - for(int i = index; index > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = tank; + _places.Insert(0, tank); return true; } - // Удаление объекта из набора с конкретной позиции 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 GetTanks(int? maxCars = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxCars.HasValue && i == maxCars.Value) + { + yield break; + } + } } } } \ No newline at end of file diff --git a/Tank/Tank/TanksGenericCollection.cs b/Tank/Tank/TanksGenericCollection.cs index 1eddea4..da282e9 100644 --- a/Tank/Tank/TanksGenericCollection.cs +++ b/Tank/Tank/TanksGenericCollection.cs @@ -35,30 +35,30 @@ namespace Tank.Generics } // Перегрузка оператора сложения - public static int? operator +(TanksGenericCollection collect, T? obj) + public static bool operator +(TanksGenericCollection collect, T? obj) { - if (obj == null) - { - return -1; - } - return collect?._collection.Insert(obj); - } - - // Перегрузка оператора вычитания - public static bool operator -(TanksGenericCollection collect, int pos) - { - T? obj = collect._collection.Get(pos); if (obj == null) { return false; } - return collect._collection.Remove(pos); + return (bool)collect?._collection.Insert(obj); + } + + // Перегрузка оператора вычитания + public static T? operator -(TanksGenericCollection collect, int pos) + { + T? obj = collect._collection[pos]; + if (obj != null) + { + collect._collection.Remove(pos); + } + return obj; } // Получение объекта IMoveableObject public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } // Вывод всего набора объектов @@ -94,13 +94,15 @@ namespace Tank.Generics { int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach(var tank in _collection.GetTanks()) { - DrawArmoVehicle? tank = _collection.Get(i); - if (tank == null) - continue; - tank.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); - tank.DrawTransport(g); + if (tank != null) + { + tank.SetPosition(i % width * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); + tank.DrawTransport(g); + } + i++; } } } diff --git a/Tank/Tank/TanksGenericStorage.cs b/Tank/Tank/TanksGenericStorage.cs new file mode 100644 index 0000000..a5fac2f --- /dev/null +++ b/Tank/Tank/TanksGenericStorage.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tank.Generics; +using Tank.DrawningObjects; +using Tank.MovementStrategy; + +namespace Tank +{ + internal class TanksGenericStorage + { + readonly Dictionary> _tankStorages; + public List Keys => _tankStorages.Keys.ToList(); + private readonly int _pictureWidth; + private readonly int _pictureHeight; + public TanksGenericStorage(int pictureWidth, int pictureHeight) + { + _tankStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public void AddSet(string name) + { + if (_tankStorages.ContainsKey(name)) return; + _tankStorages[name] = new TanksGenericCollection(_pictureWidth, _pictureHeight); + } + + public void DelSet(string name) + { + if (!_tankStorages.ContainsKey(name)) return; + _tankStorages.Remove(name); + } + + public TanksGenericCollection? + this[string ind] + { + get + { + if (_tankStorages.ContainsKey(ind)) return _tankStorages[ind]; + return null; + } + } + } +}