From 25a690a85eaf752ff19eb877e683859c94778f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=B8=D0=BB=20=D0=9F=D1=83=D1=82?= =?UTF-8?q?=D0=B8=D0=BD=D1=86=D0=B5=D0=B2?= Date: Tue, 24 Oct 2023 18:58:46 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=204?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RoadTrain/DrawningObjectTrain.cs | 6 +- RoadTrain/DrawningRoadTrain.cs | 16 +++ RoadTrain/DrawningTrain.cs | 2 +- RoadTrain/FormRoadTrain.Designer.cs | 53 ++++--- RoadTrain/FormTrainCollection.Designer.cs | 134 +++++++++++++++--- RoadTrain/FormTrainCollection.cs | 108 ++++++++++++-- ...ction.cs => RoadTrainGenericCollection.cs} | 13 +- RoadTrain/RoadTrainGenericStorage.cs | 80 +++++++++++ RoadTrain/SetGeneric.cs | 65 +++++---- 9 files changed, 387 insertions(+), 90 deletions(-) rename RoadTrain/{TrainGenericCollection.cs => RoadTrainGenericCollection.cs} (91%) create mode 100644 RoadTrain/RoadTrainGenericStorage.cs diff --git a/RoadTrain/DrawningObjectTrain.cs b/RoadTrain/DrawningObjectTrain.cs index 2581acc..054eb0a 100644 --- a/RoadTrain/DrawningObjectTrain.cs +++ b/RoadTrain/DrawningObjectTrain.cs @@ -5,9 +5,9 @@ using RoadTrain; namespace RoadTrain.MovementStrategy { /// - /// Реализация интерфейса IDrawningObject для работы с объектом DrawningCar (паттерн Adapter) -/// -public class DrawningObjectTrain : IMoveableObject + /// Реализация интерфейса IDrawningObject для работы с объектом DrawningRoadTrain (паттерн Adapter) + /// + public class DrawningObjectTrain : IMoveableObject { private readonly DrawningRoadTrain? _drawningRoadTrain = null; public DrawningObjectTrain (DrawningRoadTrain drawningRoadTrain) diff --git a/RoadTrain/DrawningRoadTrain.cs b/RoadTrain/DrawningRoadTrain.cs index 2fdc037..a09dd18 100644 --- a/RoadTrain/DrawningRoadTrain.cs +++ b/RoadTrain/DrawningRoadTrain.cs @@ -102,6 +102,22 @@ namespace RoadTrain.DrawningObjects /// Координата Y public void SetPosition(int x, int y) { + if (x < 0) + { + x = 0; + } + else if (x + _trainWidth > _pictureWidth) + { + x = _pictureWidth - _trainWidth; + } + if (y < 0) + { + y = 0; + } + else if (y + _trainHeight > _pictureHeight) + { + y = _pictureHeight - _trainHeight; + } _startPosX = x; _startPosY = y; } diff --git a/RoadTrain/DrawningTrain.cs b/RoadTrain/DrawningTrain.cs index 0f45d9f..22b3046 100644 --- a/RoadTrain/DrawningTrain.cs +++ b/RoadTrain/DrawningTrain.cs @@ -20,7 +20,7 @@ namespace RoadTrain.DrawningObjects /// Ширина картинки /// Высота картинки public DrawningTrain(int speed, double weight, Color bodyColor, Color - additionalColor, bool waterContainer, bool sweepingBrush, int width, int height) :base(speed, weight, bodyColor, 70, 30) + additionalColor, bool waterContainer, bool sweepingBrush, int width, int height) :base(speed, weight, bodyColor, width, height) { if (EntityRoadTrain != null) { diff --git a/RoadTrain/FormRoadTrain.Designer.cs b/RoadTrain/FormRoadTrain.Designer.cs index 6e05ae9..86af0a4 100644 --- a/RoadTrain/FormRoadTrain.Designer.cs +++ b/RoadTrain/FormRoadTrain.Designer.cs @@ -44,8 +44,9 @@ // pictureBoxRoadTrain // pictureBoxRoadTrain.Location = new Point(0, 0); + pictureBoxRoadTrain.Margin = new Padding(3, 4, 3, 4); pictureBoxRoadTrain.Name = "pictureBoxRoadTrain"; - pictureBoxRoadTrain.Size = new Size(685, 361); + pictureBoxRoadTrain.Size = new Size(776, 481); pictureBoxRoadTrain.TabIndex = 10; pictureBoxRoadTrain.TabStop = false; // @@ -54,9 +55,10 @@ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.BackgroundImage = Properties.Resources.left; buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; - buttonLeft.Location = new Point(533, 269); + buttonLeft.Location = new Point(609, 359); + buttonLeft.Margin = new Padding(3, 4, 3, 4); buttonLeft.Name = "buttonLeft"; - buttonLeft.Size = new Size(30, 30); + buttonLeft.Size = new Size(34, 40); buttonLeft.TabIndex = 2; buttonLeft.UseVisualStyleBackColor = true; buttonLeft.Click += ButtonMove_Click; @@ -66,9 +68,10 @@ buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.BackgroundImage = Properties.Resources.up; buttonUp.BackgroundImageLayout = ImageLayout.Zoom; - buttonUp.Location = new Point(577, 223); + buttonUp.Location = new Point(659, 297); + buttonUp.Margin = new Padding(3, 4, 3, 4); buttonUp.Name = "buttonUp"; - buttonUp.Size = new Size(30, 30); + buttonUp.Size = new Size(34, 40); buttonUp.TabIndex = 3; buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += ButtonMove_Click; @@ -78,9 +81,10 @@ buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.BackgroundImage = Properties.Resources.right; buttonRight.BackgroundImageLayout = ImageLayout.Zoom; - buttonRight.Location = new Point(620, 269); + buttonRight.Location = new Point(709, 359); + buttonRight.Margin = new Padding(3, 4, 3, 4); buttonRight.Name = "buttonRight"; - buttonRight.Size = new Size(30, 30); + buttonRight.Size = new Size(34, 40); buttonRight.TabIndex = 4; buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; @@ -90,9 +94,10 @@ buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.BackgroundImage = Properties.Resources.down; buttonDown.BackgroundImageLayout = ImageLayout.Zoom; - buttonDown.Location = new Point(577, 315); + buttonDown.Location = new Point(659, 420); + buttonDown.Margin = new Padding(3, 4, 3, 4); buttonDown.Name = "buttonDown"; - buttonDown.Size = new Size(30, 30); + buttonDown.Size = new Size(34, 40); buttonDown.TabIndex = 5; buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += ButtonMove_Click; @@ -102,16 +107,18 @@ comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxStrategy.FormattingEnabled = true; comboBoxStrategy.Items.AddRange(new object[] { "Центр формы", "Граница формы" }); - comboBoxStrategy.Location = new Point(552, 12); + comboBoxStrategy.Location = new Point(631, 16); + comboBoxStrategy.Margin = new Padding(3, 4, 3, 4); comboBoxStrategy.Name = "comboBoxStrategy"; - comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.Size = new Size(138, 28); comboBoxStrategy.TabIndex = 8; // // ButtonStep // - ButtonStep.Location = new Point(581, 50); + ButtonStep.Location = new Point(664, 67); + ButtonStep.Margin = new Padding(3, 4, 3, 4); ButtonStep.Name = "ButtonStep"; - ButtonStep.Size = new Size(75, 23); + ButtonStep.Size = new Size(86, 31); ButtonStep.TabIndex = 9; ButtonStep.Text = "Шаг"; ButtonStep.UseVisualStyleBackColor = true; @@ -119,9 +126,10 @@ // // ButtonCreateTrain // - ButtonCreateTrain.Location = new Point(30, 285); + ButtonCreateTrain.Location = new Point(34, 380); + ButtonCreateTrain.Margin = new Padding(3, 4, 3, 4); ButtonCreateTrain.Name = "ButtonCreateTrain"; - ButtonCreateTrain.Size = new Size(115, 43); + ButtonCreateTrain.Size = new Size(131, 80); ButtonCreateTrain.TabIndex = 11; ButtonCreateTrain.Text = "Создать моющую машину"; ButtonCreateTrain.UseVisualStyleBackColor = true; @@ -129,9 +137,10 @@ // // ButtonCreateRoadTrain // - ButtonCreateRoadTrain.Location = new Point(180, 285); + ButtonCreateRoadTrain.Location = new Point(206, 380); + ButtonCreateRoadTrain.Margin = new Padding(3, 4, 3, 4); ButtonCreateRoadTrain.Name = "ButtonCreateRoadTrain"; - ButtonCreateRoadTrain.Size = new Size(93, 43); + ButtonCreateRoadTrain.Size = new Size(106, 80); ButtonCreateRoadTrain.TabIndex = 12; ButtonCreateRoadTrain.Text = "Создать поезд"; ButtonCreateRoadTrain.UseVisualStyleBackColor = true; @@ -139,9 +148,10 @@ // // ButtonSelectTrain // - ButtonSelectTrain.Location = new Point(567, 94); + ButtonSelectTrain.Location = new Point(648, 125); + ButtonSelectTrain.Margin = new Padding(3, 4, 3, 4); ButtonSelectTrain.Name = "ButtonSelectTrain"; - ButtonSelectTrain.Size = new Size(89, 42); + ButtonSelectTrain.Size = new Size(102, 56); ButtonSelectTrain.TabIndex = 13; ButtonSelectTrain.Text = "Добавить поезд"; ButtonSelectTrain.UseVisualStyleBackColor = true; @@ -149,9 +159,9 @@ // // FormRoadTrain // - AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(679, 363); + ClientSize = new Size(776, 484); Controls.Add(ButtonSelectTrain); Controls.Add(ButtonCreateRoadTrain); Controls.Add(ButtonCreateTrain); @@ -162,6 +172,7 @@ Controls.Add(buttonUp); Controls.Add(buttonLeft); Controls.Add(pictureBoxRoadTrain); + Margin = new Padding(3, 4, 3, 4); Name = "FormRoadTrain"; Text = "FormRoadTrain"; ((System.ComponentModel.ISupportInitialize)pictureBoxRoadTrain).EndInit(); diff --git a/RoadTrain/FormTrainCollection.Designer.cs b/RoadTrain/FormTrainCollection.Designer.cs index ac2ea2b..a57013d 100644 --- a/RoadTrain/FormTrainCollection.Designer.cs +++ b/RoadTrain/FormTrainCollection.Designer.cs @@ -29,40 +29,110 @@ private void InitializeComponent() { panel1 = new Panel(); + label2 = new Label(); + panel2 = new Panel(); + textBoxStorageName = new TextBox(); + ButtonDelObject = new Button(); + ButtonAddObject = new Button(); + listBoxStorages = new ListBox(); InputTextBox = new TextBox(); ButtonRefreshCollection = new Button(); ButtonRemoveTrain = new Button(); ButtonAddTrain = new Button(); label1 = new Label(); pictureBoxCollection = new PictureBox(); + label = new Label(); panel1.SuspendLayout(); + panel2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); SuspendLayout(); // // panel1 // + panel1.Controls.Add(label2); + panel1.Controls.Add(panel2); panel1.Controls.Add(InputTextBox); panel1.Controls.Add(ButtonRefreshCollection); panel1.Controls.Add(ButtonRemoveTrain); panel1.Controls.Add(ButtonAddTrain); - panel1.Controls.Add(label1); - panel1.Location = new Point(597, 12); + panel1.Dock = DockStyle.Right; + panel1.Location = new Point(678, 0); + panel1.Margin = new Padding(3, 4, 3, 4); panel1.Name = "panel1"; - panel1.Size = new Size(200, 426); + panel1.Size = new Size(236, 600); panel1.TabIndex = 0; // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(43, 18); + label2.Name = "label2"; + label2.Size = new Size(66, 20); + label2.TabIndex = 0; + label2.Text = "Наборы"; + // + // panel2 + // + panel2.Controls.Add(textBoxStorageName); + panel2.Controls.Add(ButtonDelObject); + panel2.Controls.Add(ButtonAddObject); + panel2.Controls.Add(listBoxStorages); + panel2.Location = new Point(24, 29); + panel2.Name = "panel2"; + panel2.Size = new Size(190, 278); + panel2.TabIndex = 5; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(21, 30); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(150, 27); + textBoxStorageName.TabIndex = 3; + // + // ButtonDelObject + // + ButtonDelObject.Location = new Point(21, 179); + ButtonDelObject.Name = "ButtonDelObject"; + ButtonDelObject.Size = new Size(150, 29); + ButtonDelObject.TabIndex = 2; + ButtonDelObject.Text = "Удалить набор"; + ButtonDelObject.UseVisualStyleBackColor = true; + ButtonDelObject.Click += ButtonDelObject_Click; + // + // ButtonAddObject + // + ButtonAddObject.Location = new Point(21, 77); + ButtonAddObject.Name = "ButtonAddObject"; + ButtonAddObject.Size = new Size(150, 29); + ButtonAddObject.TabIndex = 1; + ButtonAddObject.Text = "Добавить набор"; + ButtonAddObject.UseVisualStyleBackColor = true; + ButtonAddObject.Click += ButtonAddObject_Click; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 20; + listBoxStorages.Location = new Point(21, 121); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(150, 44); + listBoxStorages.TabIndex = 0; + listBoxStorages.SelectedIndexChanged += ListBoxStorages_SelectedIndexChanged; + // // InputTextBox // - InputTextBox.Location = new Point(49, 118); + InputTextBox.Location = new Point(59, 384); + InputTextBox.Margin = new Padding(3, 4, 3, 4); InputTextBox.Name = "InputTextBox"; - InputTextBox.Size = new Size(100, 23); + InputTextBox.Size = new Size(114, 27); InputTextBox.TabIndex = 4; // // ButtonRefreshCollection // - ButtonRefreshCollection.Location = new Point(39, 233); + ButtonRefreshCollection.Location = new Point(45, 485); + ButtonRefreshCollection.Margin = new Padding(3, 4, 3, 4); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; - ButtonRefreshCollection.Size = new Size(126, 44); + ButtonRefreshCollection.Size = new Size(144, 59); ButtonRefreshCollection.TabIndex = 3; ButtonRefreshCollection.Text = "Обновить коллекцию"; ButtonRefreshCollection.UseVisualStyleBackColor = true; @@ -70,9 +140,10 @@ // // ButtonRemoveTrain // - ButtonRemoveTrain.Location = new Point(39, 160); + ButtonRemoveTrain.Location = new Point(45, 428); + ButtonRemoveTrain.Margin = new Padding(3, 4, 3, 4); ButtonRemoveTrain.Name = "ButtonRemoveTrain"; - ButtonRemoveTrain.Size = new Size(126, 30); + ButtonRemoveTrain.Size = new Size(144, 40); ButtonRemoveTrain.TabIndex = 2; ButtonRemoveTrain.Text = "Удалить поезд"; ButtonRemoveTrain.UseVisualStyleBackColor = true; @@ -80,9 +151,10 @@ // // ButtonAddTrain // - ButtonAddTrain.Location = new Point(39, 45); + ButtonAddTrain.Location = new Point(45, 322); + ButtonAddTrain.Margin = new Padding(3, 4, 3, 4); ButtonAddTrain.Name = "ButtonAddTrain"; - ButtonAddTrain.Size = new Size(126, 33); + ButtonAddTrain.Size = new Size(144, 44); ButtonAddTrain.TabIndex = 1; ButtonAddTrain.Text = "Добавить поезд"; ButtonAddTrain.UseVisualStyleBackColor = true; @@ -90,34 +162,49 @@ // // label1 // - label1.AutoSize = true; - label1.Location = new Point(66, 14); + label1.Location = new Point(0, 0); label1.Name = "label1"; - label1.Size = new Size(83, 15); - label1.TabIndex = 0; - label1.Text = "Инструменты"; + label1.Size = new Size(114, 31); + label1.TabIndex = 2; // // pictureBoxCollection // - pictureBoxCollection.Location = new Point(0, 1); + pictureBoxCollection.Dock = DockStyle.Fill; + pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Margin = new Padding(3, 4, 3, 4); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(591, 446); + pictureBoxCollection.Size = new Size(678, 600); pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabStop = false; // + // label + // + label.AutoSize = true; + label.Location = new Point(718, 16); + label.Name = "label"; + label.Size = new Size(103, 20); + label.TabIndex = 5; + label.Text = "Инструменты"; + // // FormTrainCollection // - AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(914, 600); + Controls.Add(label); Controls.Add(pictureBoxCollection); Controls.Add(panel1); + Controls.Add(label1); + Margin = new Padding(3, 4, 3, 4); Name = "FormTrainCollection"; Text = "FormTrainCollection"; panel1.ResumeLayout(false); panel1.PerformLayout(); + panel2.ResumeLayout(false); + panel2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ResumeLayout(false); + PerformLayout(); } #endregion @@ -129,5 +216,12 @@ private Button ButtonRefreshCollection; private Button ButtonRemoveTrain; private PictureBox pictureBoxCollection; + private Label label; + private Label label2; + private Panel panel2; + private ListBox listBoxStorages; + private Button ButtonDelObject; + private Button ButtonAddObject; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/RoadTrain/FormTrainCollection.cs b/RoadTrain/FormTrainCollection.cs index 3919c5d..e6ac440 100644 --- a/RoadTrain/FormTrainCollection.cs +++ b/RoadTrain/FormTrainCollection.cs @@ -15,56 +15,144 @@ namespace RoadTrain { public partial class FormTrainCollection : Form { - private readonly RoadTrainGenericCollection _trains; + private readonly RoadTrainGenericStorage _storage; public FormTrainCollection() { InitializeComponent(); - _trains = new RoadTrainGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new RoadTrainGenericStorage(pictureBoxCollection.Width, +pictureBoxCollection.Height); + } + /// + /// Заполнение listBoxObjects + /// + 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 ButtonAddTrain_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } FormRoadTrain form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_trains + form.SelectedTrain != -1) + if (obj + form.SelectedTrain != -1) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _trains.ShowTrains(); + pictureBoxCollection.Image = obj.ShowTrains(); } else { MessageBox.Show("Не удалось добавить объект"); } } + } private void ButtonRemoveTrain_Click(object sender, EventArgs e) { + 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) + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(InputTextBox.Text); - if (_trains - pos != null) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _trains.ShowTrains(); + pictureBoxCollection.Image = obj.ShowTrains(); } else { MessageBox.Show("Не удалось удалить объект"); } + } private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _trains.ShowTrains(); + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowTrains(); + } + } + 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 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(); + } + + } + + private void ListBoxStorages_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = +_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowTrains(); } } } diff --git a/RoadTrain/TrainGenericCollection.cs b/RoadTrain/RoadTrainGenericCollection.cs similarity index 91% rename from RoadTrain/TrainGenericCollection.cs rename to RoadTrain/RoadTrainGenericCollection.cs index aaf630f..9ce4edb 100644 --- a/RoadTrain/TrainGenericCollection.cs +++ b/RoadTrain/RoadTrainGenericCollection.cs @@ -70,7 +70,7 @@ namespace RoadTrain.Generics public static T? operator -(RoadTrainGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { collect._collection.Remove(pos); @@ -84,7 +84,7 @@ namespace RoadTrain.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } /// /// Вывод всего набора объектов @@ -126,14 +126,13 @@ namespace RoadTrain.Generics { { int width = _pictureWidth / _placeSizeWidth; - int height = _pictureHeight / _placeSizeHeight; for (int i = 0; i < _collection.Count; i++) { - DrawningRoadTrain? tank = _collection.Get(i); - if (tank == null) + DrawningRoadTrain? train = _collection[i]; + if (train == null) continue; - tank.SetPosition(i % width * _placeSizeWidth, i / width * _placeSizeHeight); - tank.DrawTransport(g); + train.SetPosition(i % width * _placeSizeWidth, i / width * _placeSizeHeight); + train.DrawTransport(g); } } } diff --git a/RoadTrain/RoadTrainGenericStorage.cs b/RoadTrain/RoadTrainGenericStorage.cs new file mode 100644 index 0000000..343f50d --- /dev/null +++ b/RoadTrain/RoadTrainGenericStorage.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RoadTrain.DrawningObjects; +using RoadTrain.MovementStrategy; + + +namespace RoadTrain.Generics +{ + /// + /// Класс для хранения коллекции + /// + internal class RoadTrainGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _trainStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _trainStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public RoadTrainGenericStorage(int pictureWidth, int pictureHeight) + { + _trainStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + if (_trainStorages.ContainsKey(name)) return; + _trainStorages[name] = new RoadTrainGenericCollection(_pictureWidth, _pictureHeight); + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + if (_trainStorages.ContainsKey(name)) _trainStorages.Remove(name); + return; + } + /// + /// Доступ к набору + /// + /// + /// + public RoadTrainGenericCollection? + this[string ind] + { + get + { + if (_trainStorages.ContainsKey(ind)) return _trainStorages[ind]; + return null; + } + } + } +} + diff --git a/RoadTrain/SetGeneric.cs b/RoadTrain/SetGeneric.cs index c64736d..4dd6251 100644 --- a/RoadTrain/SetGeneric.cs +++ b/RoadTrain/SetGeneric.cs @@ -12,35 +12,28 @@ namespace RoadTrain.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 train) { - for (int i = Count - 1; i >= 1; i--) - { - if (_places[i - 1] == null) - continue; - _places[i] = _places[i - 1]; - } - _places[0] = train; - - return 0; + return Insert(train, 0); } /// /// Добавление объекта в набор на конкретную позицию @@ -50,16 +43,12 @@ namespace RoadTrain.Generics /// public int Insert(T train, int position) { - if ((position < 0) && (position > Count)) return -1; - if (_places[position] != null) - { - for (int i = Count - 1; i >= position; i--) - { - if (_places[i - 1] != null) - _places[i] = _places[i - 1]; - } - } - _places[position] = train; + if (position < 0 || position >= _maxCount) + return -1; + + if (Count >= _maxCount) + return -1; + _places.Insert(position, train); return position; } /// @@ -69,7 +58,7 @@ namespace RoadTrain.Generics /// public bool Remove(int position) { - if ((position < 0) || (position > Count)) return false; + if ((position < 0) || (position > _maxCount)) return false; _places[position] = null; return true; } @@ -78,11 +67,31 @@ namespace RoadTrain.Generics /// /// /// - 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 GetTrains(int? maxTrains = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxTrains.HasValue && i == maxTrains.Value) + { + yield break; + } + } + } + + } }