diff --git a/ProjectStormtrooper/ProjectStormtrooper/DrawingPlane.cs b/ProjectStormtrooper/ProjectStormtrooper/DrawingPlane.cs index 7186346..7ae0b30 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/DrawingPlane.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/DrawingPlane.cs @@ -235,5 +235,9 @@ namespace ProjectStormtrooper g.FillRectangle(brushBodyColor, _startPosX + _planeWidth / 8, _startPosY + _planeHeight / 2 - bodyHeight / 2, _planeWidth - _planeWidth / 8, bodyHeight); g.DrawRectangle(penBlack, _startPosX + _planeWidth / 8, _startPosY + _planeHeight / 2 - bodyHeight / 2, _planeWidth - _planeWidth / 8, bodyHeight); } + /// + /// Получение объекта IMoveableObject из объекта DrawingPlane + /// + public IMoveableObject GetMoveableObject => new DrawingObjectPlane(this); } } diff --git a/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.Designer.cs b/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.Designer.cs new file mode 100644 index 0000000..03ec76e --- /dev/null +++ b/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.Designer.cs @@ -0,0 +1,128 @@ +namespace ProjectStormtrooper +{ + partial class FormPlaneCollection + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + groupBoxTools = new GroupBox(); + maskedTextBoxNumber = new MaskedTextBox(); + buttonRefreshCollection = new Button(); + buttonRemovePlane = new Button(); + buttonAddPlane = new Button(); + pictureBoxCollection = new PictureBox(); + groupBoxTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + SuspendLayout(); + // + // groupBoxTools + // + groupBoxTools.Controls.Add(maskedTextBoxNumber); + groupBoxTools.Controls.Add(buttonRefreshCollection); + groupBoxTools.Controls.Add(buttonRemovePlane); + groupBoxTools.Controls.Add(buttonAddPlane); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(787, 0); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(230, 538); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(6, 87); + maskedTextBoxNumber.Mask = "00"; + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(218, 27); + maskedTextBoxNumber.TabIndex = 4; + maskedTextBoxNumber.TextAlign = HorizontalAlignment.Center; + maskedTextBoxNumber.ValidatingType = typeof(int); + // + // buttonRefreshCollection + // + buttonRefreshCollection.Location = new Point(6, 180); + buttonRefreshCollection.Name = "buttonRefreshCollection"; + buttonRefreshCollection.Size = new Size(218, 29); + buttonRefreshCollection.TabIndex = 3; + buttonRefreshCollection.Text = "Обновить коллекцию"; + buttonRefreshCollection.UseVisualStyleBackColor = true; + buttonRefreshCollection.Click += buttonRefreshCollection_Click; + // + // buttonRemovePlane + // + buttonRemovePlane.Location = new Point(6, 120); + buttonRemovePlane.Name = "buttonRemovePlane"; + buttonRemovePlane.Size = new Size(218, 29); + buttonRemovePlane.TabIndex = 2; + buttonRemovePlane.Text = "Удалить самолет"; + buttonRemovePlane.UseVisualStyleBackColor = true; + buttonRemovePlane.Click += buttonRemovePlane_Click; + // + // buttonAddPlane + // + buttonAddPlane.Location = new Point(6, 26); + buttonAddPlane.Name = "buttonAddPlane"; + buttonAddPlane.Size = new Size(218, 29); + buttonAddPlane.TabIndex = 0; + buttonAddPlane.Text = "Добавить самолет"; + buttonAddPlane.UseVisualStyleBackColor = true; + buttonAddPlane.Click += buttonAddPlane_Click; + // + // pictureBoxCollection + // + pictureBoxCollection.Dock = DockStyle.Fill; + pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(787, 538); + pictureBoxCollection.TabIndex = 1; + pictureBoxCollection.TabStop = false; + // + // FormPlaneCollection + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1017, 538); + Controls.Add(pictureBoxCollection); + Controls.Add(groupBoxTools); + Name = "FormPlaneCollection"; + Text = "Набор самолетов"; + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxTools; + private Button buttonRefreshCollection; + private Button buttonRemovePlane; + private Button buttonAddPlane; + private MaskedTextBox maskedTextBoxNumber; + private PictureBox pictureBoxCollection; + } +} \ No newline at end of file diff --git a/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.cs b/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.cs new file mode 100644 index 0000000..698e86b --- /dev/null +++ b/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ProjectStormtrooper +{ + /// + /// Форма для работы с набором объектов класса DrawningPlane + /// + public partial class FormPlaneCollection : Form + { + /// + /// Набор объектов + /// + private readonly PlanesGenericCollection _planes; + /// + /// Конструктор + /// + public FormPlaneCollection() + { + InitializeComponent(); + _planes = new PlanesGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + /// + /// Добавление объекта в набор + /// + /// + /// + private void buttonAddPlane_Click(object sender, EventArgs e) + { + FormStormtrooper form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_planes + form.SelectedPlane > -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _planes.ShowPlanes(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта из набора + /// + /// + /// + private void buttonRemovePlane_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_planes - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _planes.ShowPlanes(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Обновление рисунка по набору + /// + /// + /// + private void buttonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _planes.ShowPlanes(); + } + } +} diff --git a/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.resx b/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.resx new file mode 100644 index 0000000..a395bff --- /dev/null +++ b/ProjectStormtrooper/ProjectStormtrooper/FormPlaneCollection.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.Designer.cs b/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.Designer.cs index 46e6117..30b4862 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.Designer.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.Designer.cs @@ -37,6 +37,7 @@ buttonCreatePlane = new Button(); comboBoxStrategy = new ComboBox(); buttonStep = new Button(); + buttonSelectPlane = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxStormtrooper).BeginInit(); SuspendLayout(); // @@ -45,7 +46,7 @@ pictureBoxStormtrooper.Dock = DockStyle.Fill; pictureBoxStormtrooper.Location = new Point(0, 0); pictureBoxStormtrooper.Name = "pictureBoxStormtrooper"; - pictureBoxStormtrooper.Size = new Size(882, 453); + pictureBoxStormtrooper.Size = new Size(948, 546); pictureBoxStormtrooper.SizeMode = PictureBoxSizeMode.AutoSize; pictureBoxStormtrooper.TabIndex = 0; pictureBoxStormtrooper.TabStop = false; @@ -53,7 +54,7 @@ // buttonCreateStormtrooper // buttonCreateStormtrooper.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateStormtrooper.Location = new Point(12, 412); + buttonCreateStormtrooper.Location = new Point(12, 505); buttonCreateStormtrooper.Name = "buttonCreateStormtrooper"; buttonCreateStormtrooper.Size = new Size(201, 29); buttonCreateStormtrooper.TabIndex = 1; @@ -66,7 +67,7 @@ buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.BackgroundImage = Properties.Resources.arrowUP; buttonUp.BackgroundImageLayout = ImageLayout.Zoom; - buttonUp.Location = new Point(804, 375); + buttonUp.Location = new Point(870, 468); buttonUp.Name = "buttonUp"; buttonUp.Size = new Size(30, 30); buttonUp.TabIndex = 2; @@ -78,7 +79,7 @@ buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.BackgroundImage = Properties.Resources.arrowDOWN; buttonDown.BackgroundImageLayout = ImageLayout.Zoom; - buttonDown.Location = new Point(804, 411); + buttonDown.Location = new Point(870, 504); buttonDown.Name = "buttonDown"; buttonDown.Size = new Size(30, 30); buttonDown.TabIndex = 3; @@ -90,7 +91,7 @@ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.BackgroundImage = Properties.Resources.arrowLEFT; buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; - buttonLeft.Location = new Point(768, 411); + buttonLeft.Location = new Point(834, 504); buttonLeft.Name = "buttonLeft"; buttonLeft.Size = new Size(30, 30); buttonLeft.TabIndex = 4; @@ -102,7 +103,7 @@ buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.BackgroundImage = Properties.Resources.arrowRIGHT; buttonRight.BackgroundImageLayout = ImageLayout.Zoom; - buttonRight.Location = new Point(840, 411); + buttonRight.Location = new Point(906, 504); buttonRight.Name = "buttonRight"; buttonRight.Size = new Size(30, 30); buttonRight.TabIndex = 5; @@ -112,7 +113,7 @@ // buttonCreatePlane // buttonCreatePlane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreatePlane.Location = new Point(219, 412); + buttonCreatePlane.Location = new Point(219, 505); buttonCreatePlane.Name = "buttonCreatePlane"; buttonCreatePlane.Size = new Size(201, 29); buttonCreatePlane.TabIndex = 6; @@ -122,17 +123,19 @@ // // comboBoxStrategy // + comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxStrategy.FormattingEnabled = true; comboBoxStrategy.Items.AddRange(new object[] { "MoveToCenter", "MoveToRightBottom" }); - comboBoxStrategy.Location = new Point(719, 12); + comboBoxStrategy.Location = new Point(785, 12); comboBoxStrategy.Name = "comboBoxStrategy"; comboBoxStrategy.Size = new Size(151, 28); comboBoxStrategy.TabIndex = 7; // // buttonStep // - buttonStep.Location = new Point(776, 46); + buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonStep.Location = new Point(842, 46); buttonStep.Name = "buttonStep"; buttonStep.Size = new Size(94, 29); buttonStep.TabIndex = 8; @@ -140,11 +143,22 @@ buttonStep.UseVisualStyleBackColor = true; buttonStep.Click += buttonStep_Click; // + // buttonSelectPlane + // + buttonSelectPlane.Location = new Point(426, 505); + buttonSelectPlane.Name = "buttonSelectPlane"; + buttonSelectPlane.Size = new Size(137, 29); + buttonSelectPlane.TabIndex = 9; + buttonSelectPlane.Text = "Выбрать"; + buttonSelectPlane.UseVisualStyleBackColor = true; + buttonSelectPlane.Click += ButtonSelectPlane_Click; + // // FormStormtrooper // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(882, 453); + ClientSize = new Size(948, 546); + Controls.Add(buttonSelectPlane); Controls.Add(buttonStep); Controls.Add(comboBoxStrategy); Controls.Add(buttonCreatePlane); @@ -173,5 +187,6 @@ private Button buttonCreatePlane; private ComboBox comboBoxStrategy; private Button buttonStep; + private Button buttonSelectPlane; } } \ No newline at end of file diff --git a/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.cs b/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.cs index 96fc949..b1dc399 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.cs @@ -9,13 +9,22 @@ namespace ProjectStormtrooper /// - /// private DrawingPlane? _drawingPlane; - private AbstractStrategy? _abstractStrategy; + /// + /// + /// + private AbstractStrategy? _strategy; + /// + /// + /// + public DrawingPlane? SelectedPlane { get; private set; } /// /// /// public FormStormtrooper() { InitializeComponent(); + _strategy = null; + SelectedPlane = null; } /// /// @@ -39,11 +48,23 @@ namespace ProjectStormtrooper private void buttonCreateStormtrooper_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + Color dopColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + dopColor = dialog.Color; + } _drawingPlane = new DrawingStormtrooper( random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + color, + dopColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxStormtrooper.Width, @@ -60,10 +81,16 @@ namespace ProjectStormtrooper private void buttonCreatePlane_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } _drawingPlane = new DrawingPlane( random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + color, pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height ); @@ -112,30 +139,40 @@ namespace ProjectStormtrooper } if (comboBoxStrategy.Enabled) { - _abstractStrategy = comboBoxStrategy.SelectedIndex switch + _strategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), 1 => new MoveToRightBottom(), _ => null, }; - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.SetData(new DrawingObjectPlane(_drawingPlane), pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height); - comboBoxStrategy.Enabled = false; + _strategy.SetData(new DrawingObjectPlane(_drawingPlane), pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height); } - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.MakeStep(); + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); Draw(); - if (_abstractStrategy.GetStatus() == Status.Finish) + if (_strategy.GetStatus() == Status.Finish) { comboBoxStrategy.Enabled = true; - _abstractStrategy = null; + _strategy = null; } } + /// + /// + /// + /// + /// + private void ButtonSelectPlane_Click(object sender, EventArgs e) + { + SelectedPlane = _drawingPlane; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/ProjectStormtrooper/ProjectStormtrooper/PlanesGenericCollection.cs b/ProjectStormtrooper/ProjectStormtrooper/PlanesGenericCollection.cs new file mode 100644 index 0000000..91704fa --- /dev/null +++ b/ProjectStormtrooper/ProjectStormtrooper/PlanesGenericCollection.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectStormtrooper +{ + /// + /// Параметризованный класс для набора объектов DrawningPlane + /// + /// + /// + internal class PlanesGenericCollection + where T : DrawingPlane + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 160; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 120; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public PlanesGenericCollection(int picWidth, int picHeight) + { + int horizontalObjectsCount = picWidth / _placeSizeWidth; + int verticalObjectsCount = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(horizontalObjectsCount * verticalObjectsCount); + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static int operator +(PlanesGenericCollection collect, T? obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj) ?? -1; + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static T? operator -(PlanesGenericCollection collect, int pos) + { + return collect._collection.Remove(pos); + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowPlanes() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + // Линия разметки места + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + /// Метод прорисовки объектов + /// + /// + private void DrawObjects(Graphics g) + { + int placesColumnCount = _pictureHeight / _placeSizeHeight; + int placesRowCount = _pictureWidth / _placeSizeWidth; + for (int i = 0; i < _collection.Count; i++) + { + // получение объекта + var obj = _collection.Get(i); + // установка позиции + obj?.SetPosition( + (int)((placesRowCount - 1) * _placeSizeWidth - (i % placesColumnCount * _placeSizeWidth) + (_placeSizeWidth - obj?.GetWidth) / 2), + (int)(i / placesColumnCount * _placeSizeHeight + (_placeSizeHeight - obj?.GetHeight) / 2) + ); + // прорисовка объекта + obj?.DrawTransport(g); + } + } + } +} diff --git a/ProjectStormtrooper/ProjectStormtrooper/Program.cs b/ProjectStormtrooper/ProjectStormtrooper/Program.cs index 3db6e41..f8ade39 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/Program.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/Program.cs @@ -11,7 +11,7 @@ namespace ProjectStormtrooper // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormStormtrooper()); + Application.Run(new FormPlaneCollection()); } } } \ No newline at end of file diff --git a/ProjectStormtrooper/ProjectStormtrooper/SetGeneric.cs b/ProjectStormtrooper/ProjectStormtrooper/SetGeneric.cs new file mode 100644 index 0000000..7e6cfab --- /dev/null +++ b/ProjectStormtrooper/ProjectStormtrooper/SetGeneric.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectStormtrooper +{ + /// + /// Параметризованный набор объектов + /// + internal class SetGeneric where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _places = new T?[count]; + } + /// + /// Добавления объекта в набор + /// + /// + /// + public int Insert(T plane) + { + return Insert(plane, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// + /// + /// + public int Insert(T plane, int position) + { + // Проверка позиции + if (position < 0 || position >= Count) + { + return -1; + } + // Проверка, что элемент массива по этой позиции пустой + if (_places[position] != null) + { + // Проверка, что после вставляемого элемента в массиве есть пустой элемент + int nullIndex = -1; + for (int i = position + 1; i < Count; i++) + { + if (_places[i] == null) + { + nullIndex = i; + break; + } + } + // Если пустого элемента нет, то выходим + if (nullIndex < 0) + { + return -1; + } + // Сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + int j = nullIndex - 1; + while (j >= position) + { + _places[j + 1] = _places[j]; + j--; + } + } + // Вставка по позиции + _places[position] = plane; + return position; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public T? Remove(int position) + { + // Проверка позиции + if (position < 0 || position >= Count) + { + return null; + } + // Удаление объекта из массива, присвоив элементу массива значение null + T? plane = _places[position]; + _places[position] = null; + return plane; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + // Проверка позиции + if (position < 0 || position >= Count) + { + return null; + } + return _places[position]; + } + } +}