From 7583b8b681b0868a9f533c373fc80fe879f3e682 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Mon, 23 Oct 2023 20:14:39 +0400 Subject: [PATCH] =?UTF-8?q?=D1=84=D0=B8=D0=BD=D0=B0=D0=BB=203=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawningObjects/DrawningLocomotive.cs | 14 +- .../FormElectricLocomotive.Designer.cs | 20 ++- ElectricLocomotive/FormElectricLocomotive.cs | 7 +- .../FormLocomotiveCollection.Designer.cs | 153 ++++++++++++++++++ .../FormLocomotiveCollection.cs | 30 ++++ .../FormLocomotiveCollection.resx | 120 ++++++++++++++ .../Generics/LocomotivesGenericCollection.cs | 147 +++++++++++++++++ ElectricLocomotive/Generics/SetGeneric.cs | 141 ++++++++++++++++ .../LogicFormElectricLocomotive.cs | 87 +++++++--- .../LogicFormLocomotiveCollection.cs | 89 ++++++++++ ElectricLocomotive/Program.cs | 2 +- 11 files changed, 779 insertions(+), 31 deletions(-) create mode 100644 ElectricLocomotive/FormLocomotiveCollection.Designer.cs create mode 100644 ElectricLocomotive/FormLocomotiveCollection.cs create mode 100644 ElectricLocomotive/FormLocomotiveCollection.resx create mode 100644 ElectricLocomotive/Generics/LocomotivesGenericCollection.cs create mode 100644 ElectricLocomotive/Generics/SetGeneric.cs create mode 100644 ElectricLocomotive/LogicFormLocomotiveCollection.cs diff --git a/ElectricLocomotive/DrawningObjects/DrawningLocomotive.cs b/ElectricLocomotive/DrawningObjects/DrawningLocomotive.cs index 99007b6..16842eb 100644 --- a/ElectricLocomotive/DrawningObjects/DrawningLocomotive.cs +++ b/ElectricLocomotive/DrawningObjects/DrawningLocomotive.cs @@ -1,10 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ProjectElectricLocomotive.Entities; +using ProjectElectricLocomotive.Entities; using ProjectElectricLocomotive; +using ProjectElectricLocomotive.MovementStrategy; namespace ProjectElectricLocomotive.DrawningObjects { @@ -59,6 +55,12 @@ namespace ProjectElectricLocomotive.DrawningObjects /// public int GetHeight => _locomotiveHeight; + /// + /// Получение объекта IMoveableObject из объекта DrawningCar + /// + public IMoveableObject GetMoveableObject => new DrawningObjectLocomotive(this); + + /// /// Конструктор /// diff --git a/ElectricLocomotive/FormElectricLocomotive.Designer.cs b/ElectricLocomotive/FormElectricLocomotive.Designer.cs index ab9f09c..b21395d 100644 --- a/ElectricLocomotive/FormElectricLocomotive.Designer.cs +++ b/ElectricLocomotive/FormElectricLocomotive.Designer.cs @@ -37,6 +37,7 @@ buttonCreateElectricLocomotive = new Button(); comboBoxStrategy = new ComboBox(); buttonStep = new Button(); + buttonSelectLocomotive = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxElectricLocomotive).BeginInit(); SuspendLayout(); // @@ -71,7 +72,7 @@ // buttonCreateLocomotive // buttonCreateLocomotive.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonCreateLocomotive.Location = new Point(816, 562); + buttonCreateLocomotive.Location = new Point(816, 514); buttonCreateLocomotive.Margin = new Padding(3, 4, 3, 4); buttonCreateLocomotive.Name = "buttonCreateLocomotive"; buttonCreateLocomotive.Size = new Size(182, 40); @@ -122,7 +123,7 @@ // buttonCreateElectricLocomotive // buttonCreateElectricLocomotive.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; - buttonCreateElectricLocomotive.Location = new Point(816, 514); + buttonCreateElectricLocomotive.Location = new Point(816, 466); buttonCreateElectricLocomotive.Margin = new Padding(3, 4, 3, 4); buttonCreateElectricLocomotive.Name = "buttonCreateElectricLocomotive"; buttonCreateElectricLocomotive.Size = new Size(182, 40); @@ -154,11 +155,24 @@ buttonStep.UseVisualStyleBackColor = true; buttonStep.Click += buttonStep_Click; // + // buttonSelectLocomotive + // + buttonSelectLocomotive.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonSelectLocomotive.Location = new Point(816, 562); + buttonSelectLocomotive.Margin = new Padding(3, 4, 3, 4); + buttonSelectLocomotive.Name = "buttonSelectLocomotive"; + buttonSelectLocomotive.Size = new Size(182, 40); + buttonSelectLocomotive.TabIndex = 9; + buttonSelectLocomotive.Text = "выбрать"; + buttonSelectLocomotive.UseVisualStyleBackColor = true; + buttonSelectLocomotive.Click += buttonSelectLocomotive_Click; + // // FormLocomotive // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(1010, 615); + Controls.Add(buttonSelectLocomotive); Controls.Add(buttonStep); Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateElectricLocomotive); @@ -188,5 +202,7 @@ private Button buttonCreateElectricLocomotive; private ComboBox comboBoxStrategy; private Button buttonStep; + private Button button1; + private Button buttonSelectLocomotive; } } \ No newline at end of file diff --git a/ElectricLocomotive/FormElectricLocomotive.cs b/ElectricLocomotive/FormElectricLocomotive.cs index 16c282b..d6e2a01 100644 --- a/ElectricLocomotive/FormElectricLocomotive.cs +++ b/ElectricLocomotive/FormElectricLocomotive.cs @@ -34,12 +34,17 @@ namespace ProjectElectricLocomotive private void buttonStep_Click(object sender, EventArgs e) { - ButtonStep_Click(sender, e); + ButtonnStartegyStep_Click(sender, e); } private void buttonCreateElectricLocomotive_Click(object sender, EventArgs e) { ButtonCreateElectricLocomotive_Click(sender, e); } + + private void buttonSelectLocomotive_Click(object sender, EventArgs e) + { + ButtonSelectLocomotive_Click(sender, e); + } } } \ No newline at end of file diff --git a/ElectricLocomotive/FormLocomotiveCollection.Designer.cs b/ElectricLocomotive/FormLocomotiveCollection.Designer.cs new file mode 100644 index 0000000..792d6cd --- /dev/null +++ b/ElectricLocomotive/FormLocomotiveCollection.Designer.cs @@ -0,0 +1,153 @@ +namespace ProjectElectricLocomotive +{ + partial class FormLocomotiveCollection + { + /// + /// 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() + { + tableLayoutPanel1 = new TableLayoutPanel(); + pictureBoxCollection = new PictureBox(); + groupBox1 = new GroupBox(); + maskedTextBoxNumber = new MaskedTextBox(); + buttonRefreshCollection = new Button(); + buttonRemoveLocomotive = new Button(); + buttonAddLocomotive = new Button(); + tableLayoutPanel1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + groupBox1.SuspendLayout(); + SuspendLayout(); + // + // tableLayoutPanel1 + // + tableLayoutPanel1.AutoSize = true; + tableLayoutPanel1.ColumnCount = 2; + tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F)); + tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle()); + tableLayoutPanel1.Controls.Add(pictureBoxCollection, 0, 0); + tableLayoutPanel1.Controls.Add(groupBox1, 1, 0); + tableLayoutPanel1.Dock = DockStyle.Fill; + tableLayoutPanel1.Location = new Point(0, 0); + tableLayoutPanel1.Name = "tableLayoutPanel1"; + tableLayoutPanel1.RowCount = 1; + tableLayoutPanel1.RowStyles.Add(new RowStyle()); + tableLayoutPanel1.Size = new Size(885, 449); + tableLayoutPanel1.TabIndex = 0; + // + // pictureBoxCollection + // + pictureBoxCollection.Dock = DockStyle.Fill; + pictureBoxCollection.Location = new Point(3, 3); + pictureBoxCollection.MinimumSize = new Size(100, 100); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(673, 444); + pictureBoxCollection.TabIndex = 1; + pictureBoxCollection.TabStop = false; + // + // groupBox1 + // + groupBox1.Anchor = AnchorStyles.Top | AnchorStyles.Right; + groupBox1.AutoSize = true; + groupBox1.Controls.Add(maskedTextBoxNumber); + groupBox1.Controls.Add(buttonRefreshCollection); + groupBox1.Controls.Add(buttonRemoveLocomotive); + groupBox1.Controls.Add(buttonAddLocomotive); + groupBox1.Location = new Point(682, 3); + groupBox1.MaximumSize = new Size(200, 1000000); + groupBox1.MinimumSize = new Size(200, 200); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new Size(200, 220); + groupBox1.TabIndex = 0; + groupBox1.TabStop = false; + groupBox1.Text = "Инструменты"; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(6, 97); + maskedTextBoxNumber.Mask = "00000"; + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(184, 27); + maskedTextBoxNumber.TabIndex = 4; + maskedTextBoxNumber.ValidatingType = typeof(int); + // + // buttonRefreshCollection + // + buttonRefreshCollection.Location = new Point(6, 165); + buttonRefreshCollection.Name = "buttonRefreshCollection"; + buttonRefreshCollection.Size = new Size(184, 29); + buttonRefreshCollection.TabIndex = 3; + buttonRefreshCollection.Text = "Обновить колекцию"; + buttonRefreshCollection.UseVisualStyleBackColor = true; + buttonRefreshCollection.Click += buttonRefreshCollection_Click; + // + // buttonRemoveLocomotive + // + buttonRemoveLocomotive.Location = new Point(6, 130); + buttonRemoveLocomotive.Name = "buttonRemoveLocomotive"; + buttonRemoveLocomotive.Size = new Size(184, 29); + buttonRemoveLocomotive.TabIndex = 2; + buttonRemoveLocomotive.Text = "Удалить локомотив"; + buttonRemoveLocomotive.UseVisualStyleBackColor = true; + buttonRemoveLocomotive.Click += buttonRemoveLocomotive_Click; + // + // buttonAddLocomotive + // + buttonAddLocomotive.Location = new Point(6, 26); + buttonAddLocomotive.Name = "buttonAddLocomotive"; + buttonAddLocomotive.Size = new Size(184, 29); + buttonAddLocomotive.TabIndex = 0; + buttonAddLocomotive.Text = "Добавить локомотив"; + buttonAddLocomotive.UseVisualStyleBackColor = true; + buttonAddLocomotive.Click += buttonAddLocomotive_Click; + // + // FormLocomotiveCollection + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(885, 449); + Controls.Add(tableLayoutPanel1); + MinimumSize = new Size(700, 400); + Name = "FormLocomotiveCollection"; + Text = "FormLocomotiveCollection"; + tableLayoutPanel1.ResumeLayout(false); + tableLayoutPanel1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + groupBox1.ResumeLayout(false); + groupBox1.PerformLayout(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private TableLayoutPanel tableLayoutPanel1; + private GroupBox groupBox1; + private Button buttonAddLocomotive; + private PictureBox pictureBoxCollection; + private Button buttonRemoveLocomotive; + private Button buttonRefreshCollection; + private MaskedTextBox maskedTextBoxNumber; + } +} \ No newline at end of file diff --git a/ElectricLocomotive/FormLocomotiveCollection.cs b/ElectricLocomotive/FormLocomotiveCollection.cs new file mode 100644 index 0000000..3ce62d8 --- /dev/null +++ b/ElectricLocomotive/FormLocomotiveCollection.cs @@ -0,0 +1,30 @@ +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 ProjectElectricLocomotive +{ + public partial class FormLocomotiveCollection + { + private void buttonAddLocomotive_Click(object sender, EventArgs e) + { + ButtonAddLocomotive_Click(sender, e); + } + + private void buttonRemoveLocomotive_Click(object sender, EventArgs e) + { + ButtonRemoveLocomotive_Click(sender, e); + } + + private void buttonRefreshCollection_Click(object sender, EventArgs e) + { + ButtonRefreshCollection_Click(sender, e); + } + } +} diff --git a/ElectricLocomotive/FormLocomotiveCollection.resx b/ElectricLocomotive/FormLocomotiveCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ElectricLocomotive/FormLocomotiveCollection.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/ElectricLocomotive/Generics/LocomotivesGenericCollection.cs b/ElectricLocomotive/Generics/LocomotivesGenericCollection.cs new file mode 100644 index 0000000..6bb2849 --- /dev/null +++ b/ElectricLocomotive/Generics/LocomotivesGenericCollection.cs @@ -0,0 +1,147 @@ +using ProjectElectricLocomotive.DrawningObjects; +using ProjectElectricLocomotive.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.Generics +{ + /// + /// Параметризованный класс для набора объектов DrawningLocomotive + /// + /// + /// + internal class LocomotivesGenericCollection + where T : DrawningLocomotive + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 210; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 90; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public LocomotivesGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static bool operator +(LocomotivesGenericCollection collect, T? + obj) + { + if (obj == null) + { + return false; + } + return collect?._collection.Insert(obj) ?? false; + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static T? operator -(LocomotivesGenericCollection collect, int + pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + collect._collection.Remove(pos); + } + return obj; + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowCars() + { + 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) + { + for (int i = 0; i < _collection.Count; i++) + { + DrawningLocomotive drawningLocomotive = _collection.Get(i); + if (drawningLocomotive == null) + { + continue; + } + + int widthCount = _pictureHeight / _placeSizeHeight; + drawningLocomotive.SetPosition((i / widthCount) * _placeSizeWidth, (i % widthCount) * _placeSizeHeight); + drawningLocomotive.DrawTransport(g); + } + } + } +} \ No newline at end of file diff --git a/ElectricLocomotive/Generics/SetGeneric.cs b/ElectricLocomotive/Generics/SetGeneric.cs new file mode 100644 index 0000000..0750fd2 --- /dev/null +++ b/ElectricLocomotive/Generics/SetGeneric.cs @@ -0,0 +1,141 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.Generics +{ + /// + /// Параметризованный набор объектов + /// + /// + internal class SetGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T?[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetGeneric(int count) + { + _places = new T?[count]; + } + + /// + /// обмен двух элементов + /// + /// Первый элемент + /// Второй элемент + /// + private void swap(ref T first, ref T second) + { + T temp = first; + first = second; + second = temp; + } + + /// + /// Добавление объекта в набор + /// + /// Добавляемый локомотив + /// + public bool Insert(T locomotive) + { + int end = _places.Length; + for (int i = 0; i < _places.Length; ++i) + { + if (_places[i] == null) + { + end = i; + break; + } + } + if (end == _places.Length) + { + return false; + } + for (int i = end; i >= 1; --i) + { + _places[i] = _places[i - 1]; + } + _places[0] = locomotive; + return true; + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый поезд + /// Позиция + /// + public bool Insert(T locomotive, int position) + { + if (position >= _places.Length || position < 0) + { + return false; + } + if (_places[position] == null) + { + _places[position] = locomotive; + return true; + } + int end = _places.Length; + for (int i = position; i < _places.Length; ++i) + { + if (_places[i] == null) + { + end = i; + break; + } + } + if (end == _places.Length) + { + return false; + } + _places[end] = locomotive; + for (int i = position; i < end; ++i) + { + swap(ref _places[i], ref _places[end]); + } + return true; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + if (position >= _places.Length || position < 0) + { + return false; + } + + _places[position] = null; + return true; + + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + if (position >= _places.Length || position < 0) + { + return null; + } + return _places[position]; + } + + } +} \ No newline at end of file diff --git a/ElectricLocomotive/LogicFormElectricLocomotive.cs b/ElectricLocomotive/LogicFormElectricLocomotive.cs index 4838d11..4396926 100644 --- a/ElectricLocomotive/LogicFormElectricLocomotive.cs +++ b/ElectricLocomotive/LogicFormElectricLocomotive.cs @@ -2,6 +2,7 @@ using ProjectElectricLocomotive.MovementStrategy; using System; using System.Collections.Generic; +using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -20,16 +21,22 @@ namespace ProjectElectricLocomotive /// /// Стратегия перемещения /// - private AbstractStrategy? _abstractStrategy; + private AbstractStrategy? _strategy; + /// + /// Выбранный электровоз + /// + public DrawningLocomotive? SelectedLocomotive { get; private set; } /// /// Инициализация формы /// public FormLocomotive() { InitializeComponent(); + _strategy = null; + SelectedLocomotive = null; } /// - /// Метод прорисовки машины + /// Метод прорисовки локомотива /// private void Draw() { @@ -51,11 +58,30 @@ namespace ProjectElectricLocomotive private void ButtonCreateElectricLocomotive_Click(object sender, EventArgs e) { Random random = new(); + Color bodyColor = Color.FromArgb( + random.Next(0, 256), + random.Next(0, 256), + random.Next(0, 256)); + Color additionalColor = Color.FromArgb( + random.Next(0, 256), + random.Next(0, 256), + random.Next(0, 256)); + ColorDialog dialog; + dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + bodyColor = dialog.Color; + } + dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + additionalColor = dialog.Color; + } _drawningLocomotive = new DrawningElectricLocomotive( 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)), + bodyColor, + additionalColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height); @@ -71,10 +97,19 @@ namespace ProjectElectricLocomotive private void ButtonCreateLocomotive_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; + } _drawningLocomotive = new DrawningLocomotive( random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + color, pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height); _drawningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -89,9 +124,9 @@ namespace ProjectElectricLocomotive { Size size = ((PictureBox)sender)?.Size ?? Size.Empty; - if (_abstractStrategy != null) + if (_strategy != null) { - _abstractStrategy.SetFieldSize(size); + _strategy.SetFieldSize(size); } if (_drawningLocomotive != null) { @@ -130,11 +165,11 @@ namespace ProjectElectricLocomotive } /// - /// Обработка нажатия кнопки "Шаг" + /// Шаг стратегии перемещения /// /// /// - private void ButtonStep_Click(object sender, EventArgs e) + private void ButtonnStartegyStep_Click(object sender, EventArgs e) { if (_drawningLocomotive == null) { @@ -142,35 +177,45 @@ namespace ProjectElectricLocomotive } if (comboBoxStrategy.Enabled) { - _abstractStrategy = comboBoxStrategy.SelectedIndex - switch + _strategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), 1 => new MoveToBorder(), _ => null, }; - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.SetData(new - DrawningObjectLocomotive(_drawningLocomotive), pictureBoxElectricLocomotive.Width, - pictureBoxElectricLocomotive.Height); - comboBoxStrategy.Enabled = false; + _strategy.SetData( + _drawningLocomotive.GetMoveableObject, + pictureBoxElectricLocomotive.Width, + pictureBoxElectricLocomotive.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 ButtonSelectLocomotive_Click(object sender, EventArgs e) + { + SelectedLocomotive = _drawningLocomotive; + DialogResult = DialogResult.OK; + } } } diff --git a/ElectricLocomotive/LogicFormLocomotiveCollection.cs b/ElectricLocomotive/LogicFormLocomotiveCollection.cs new file mode 100644 index 0000000..f2cb6ac --- /dev/null +++ b/ElectricLocomotive/LogicFormLocomotiveCollection.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectElectricLocomotive.DrawningObjects; +using ProjectElectricLocomotive.Generics; +using ProjectElectricLocomotive.MovementStrategy; + +namespace ProjectElectricLocomotive +{ + /// + /// Форма для работы с набором объектов класса DrawningLocomotive + /// + + public partial class FormLocomotiveCollection : Form + { + /// + /// Набор объектов + /// + private readonly LocomotivesGenericCollection< + DrawningLocomotive, DrawningObjectLocomotive> _locomotives; + /// + /// Конструктор + /// + public FormLocomotiveCollection() + { + InitializeComponent(); + _locomotives = new LocomotivesGenericCollection( + pictureBoxCollection.Width, pictureBoxCollection.Height + ); + } + + /// + /// Добавление объекта в набор + /// + /// + /// + private void ButtonAddLocomotive_Click(object sender, EventArgs e) + { + FormLocomotive form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_locomotives + form.SelectedLocomotive) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _locomotives.ShowCars(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта из набора + /// + /// + /// + private void ButtonRemoveLocomotive_Click(object sender, EventArgs e) + { + if (MessageBox.Show( + "Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_locomotives - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _locomotives.ShowCars(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Обновление рисунка по набору + /// + /// + /// + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _locomotives.ShowCars(); + } + } +} diff --git a/ElectricLocomotive/Program.cs b/ElectricLocomotive/Program.cs index ca0ba7e..5def1ba 100644 --- a/ElectricLocomotive/Program.cs +++ b/ElectricLocomotive/Program.cs @@ -11,7 +11,7 @@ namespace ProjectElectricLocomotive // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormLocomotive()); + Application.Run(new FormLocomotiveCollection()); } } } \ No newline at end of file -- 2.25.1