diff --git a/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/AbstractCompany.cs b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/AbstractCompany.cs new file mode 100644 index 0000000..2ec4ac9 --- /dev/null +++ b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/AbstractCompany.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using TrolleybusProject.Drawnings; +using TrolleybusProject.MovementStrategy; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TrolleybusProject.CollectionGenericObjects; + +public abstract class AbstractCompany +{/// + /// Размер места (ширина) + /// + protected readonly int _placeSizeWidth = 280; + /// + /// Размер места (высота) + /// + protected readonly int _placeSizeHeight = 100; + /// + /// Ширина окна + /// + protected readonly int _pictureWidth; + /// + /// Высота окна + /// + protected readonly int _pictureHeight; + /// + /// Коллекция автобусов + /// + protected ICollectionGenericObjects? _collection = null; + /// + /// Вычисление максимального количества элементов, который можно разместить в окне +/// +private int GetMaxCount => _pictureWidth * _pictureHeight /(_placeSizeWidth * _placeSizeHeight); + /// + /// Конструктор + /// + /// Ширина окна + /// Высота окна + /// Коллекция автобусов + public AbstractCompany(int picWidth, int picHeight, + ICollectionGenericObjects collection) + { + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = collection; + _collection.SetMaxCount = GetMaxCount; + } + /// + /// Перегрузка оператора сложения для класса + /// + /// Компания + /// Добавляемый объект + /// + public static int operator +(AbstractCompany company, DrawningBus bus) + { + return company._collection.Insert(bus); + } + /// + /// Перегрузка оператора удаления для класса + /// + /// Компания + /// Номер удаляемого объекта + /// + public static DrawningBus operator -(AbstractCompany company, int position) + { + return company._collection.Remove(position); + } + /// + /// Получение случайного объекта из коллекции + /// + /// + public DrawningBus? GetRandomObject() + { + Random rnd = new(); + return _collection?.Get(rnd.Next(GetMaxCount)); + } + /// + /// Вывод всей коллекции + /// + /// + public Bitmap? Show() + { + Bitmap bitmap = new(_pictureWidth, _pictureHeight); + Graphics graphics = Graphics.FromImage(bitmap); + DrawBackgound(graphics); + SetObjectsPosition(); + for (int i = 0; i < (_collection?.Count ?? 0); ++i) + { + DrawningBus? obj = _collection?.Get(i); + obj?.DrawTransport(graphics); + } + return bitmap; + } + /// + /// Вывод заднего фона + /// + /// + protected abstract void DrawBackgound(Graphics g); + /// + /// Расстановка объектов + /// + protected abstract void SetObjectsPosition(); +} diff --git a/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/BusStation.cs b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/BusStation.cs new file mode 100644 index 0000000..d7364de --- /dev/null +++ b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/BusStation.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TrolleybusProject.Drawnings; + +namespace TrolleybusProject.CollectionGenericObjects; + +public class BusStation : AbstractCompany +{ + public BusStation(int picWidth, int picHeight, ICollectionGenericObjects collection) : base(picWidth, picHeight, collection) + { + } + + protected override void DrawBackgound(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 + 4, j * _placeSizeHeight, i * _placeSizeWidth +4 + _placeSizeWidth - 95, j * _placeSizeHeight); + g.DrawLine(pen, i * _placeSizeWidth +4, j * _placeSizeHeight, i * _placeSizeWidth +4, j * _placeSizeHeight + _placeSizeHeight); + } + } + } + + protected override void SetObjectsPosition() + { + int Width = _pictureWidth / _placeSizeWidth - 1; + int Height = 0; + + for (int i = 0; i < (_collection?.Count ?? 0); i++) + { + if (_collection.Get(i) != null) + { + _collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight); + _collection.Get(i).SetPosition(_placeSizeWidth * Width + 20, Height * _placeSizeHeight + 10); + } + + if (Width > 0) + Width--; + else + { + Width = _pictureWidth / _placeSizeWidth - 1; + Height++; + } + if (Height > _pictureHeight / _placeSizeHeight) + { + return; + } + } + } +} + + diff --git a/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/ICollectionGenericObjects.cs b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/ICollectionGenericObjects.cs new file mode 100644 index 0000000..1c7f803 --- /dev/null +++ b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TrolleybusProject.CollectionGenericObjects; + +public interface ICollectionGenericObjects + where T : class +{///Количество объектов в коллекции + /// + int Count { get; } + /// + /// Установка максимального количества элементов + /// + int SetMaxCount { set; } + /// + /// Добавление объекта в коллекцию + /// + /// Добавляемый объект + /// true - вставка прошла удачно, false - вставка не удалась + int Insert(T obj); + /// + /// Добавление объекта в коллекцию на конкретную позицию + /// + /// Добавляемый объект + /// Позиция + /// true - вставка прошла удачно, false - вставка не удалась +int Insert(T obj, int position); + /// + /// Удаление объекта из коллекции с конкретной позиции + /// + /// Позиция + /// true - удаление прошло удачно, false - удаление не удалось + T? Remove(int position); + /// + /// Получение объекта по позиции + /// + /// Позиция + /// Объект + T? Get(int position); +} diff --git a/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/MassiveGenericObjects.cs b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/MassiveGenericObjects.cs new file mode 100644 index 0000000..44783e9 --- /dev/null +++ b/TrolleybusProject/TrolleybusProject/CollectionGenericObjects/MassiveGenericObjects.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TrolleybusProject.CollectionGenericObjects; + +internal class MassiveGenericObjects : ICollectionGenericObjects + where T : class +{ + //// + /// Массив объектов которые храним + /// + private T?[] _collection; + + public int Count => _collection.Length; + + public int SetMaxCount { set { if (value > 0) { _collection = new T?[value]; } } } + + /// + /// Конструктор + /// + public MassiveGenericObjects() + { + _collection = Array.Empty(); + } + + public T? Get(int position) + { + if (position >= _collection.Length || position < 0) + { + return null; + } + return _collection[position]; + } + + public int Insert(T obj) + { + int index = 0; + while (index < _collection.Length) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return index; + } + index++; + } + return -1; + } + + public int Insert(T obj, int position) + { + + if (position >= _collection.Length || position < 0) + return -1; + + + if (_collection[position] != null) + { + int nullIndex = -1; + for (int i = position + 1; i < Count; i++) + { + if (_collection[i] == null) + { + nullIndex = i; + break; + } + } + if (nullIndex < 0) + { + return -1; + } + int j = nullIndex - 1; + while (j >= position) + { + _collection[j + 1] = _collection[j]; + j--; + } + } + _collection[position] = obj; + return position; + } + + public T? Remove(int position) + { + if (position >= _collection.Length || position < 0) + { + return null; + } + T? temp = _collection[position]; + _collection[position] = null; + return temp; + } + +} diff --git a/TrolleybusProject/TrolleybusProject/Drawnings/DrawningBus.cs b/TrolleybusProject/TrolleybusProject/Drawnings/DrawningBus.cs index 590f45d..d84bcad 100644 --- a/TrolleybusProject/TrolleybusProject/Drawnings/DrawningBus.cs +++ b/TrolleybusProject/TrolleybusProject/Drawnings/DrawningBus.cs @@ -38,8 +38,6 @@ public class DrawningBus /// Высота прорисовки троллейбуса /// public readonly int _drawningBusHeight = 86; - - /// /// Координата X объекта /// diff --git a/TrolleybusProject/TrolleybusProject/Drawnings/DrawningTrolleybus.cs b/TrolleybusProject/TrolleybusProject/Drawnings/DrawningTrolleybus.cs index 26a0011..3e3c86b 100644 --- a/TrolleybusProject/TrolleybusProject/Drawnings/DrawningTrolleybus.cs +++ b/TrolleybusProject/TrolleybusProject/Drawnings/DrawningTrolleybus.cs @@ -27,7 +27,6 @@ public class DrawningTrolleybus:DrawningBus EntityBus = new EntityTrolleybus(speed, weight, bodyColor, additionalColor, doors, roga, otsek); - } public override void DrawTransport(Graphics g) @@ -38,14 +37,9 @@ public class DrawningTrolleybus:DrawningBus return; } - - Pen pen = new(Color.Black); Brush additionalBrush = new SolidBrush(trolleybus.AdditionalColor); Pen addpen = new(trolleybus.AdditionalColor); - - - _startPosX += 5; _startPosY += 22; @@ -53,11 +47,8 @@ public class DrawningTrolleybus:DrawningBus _startPosX -= 5; _startPosY -= 22; - - if (trolleybus.Otsek) { - g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 48, 5, 20); g.FillRectangle(additionalBrush, _startPosX.Value + 5, _startPosY.Value + @@ -69,23 +60,11 @@ public class DrawningTrolleybus:DrawningBus //двойная дверь - - - - - if (trolleybus.Doors) { g.DrawLine(addpen, _startPosX.Value + 66, _startPosY.Value + 40, _startPosX.Value + 66, _startPosY.Value + 70); - - - } - - - - //рога if (trolleybus.Roga) @@ -95,16 +74,10 @@ public class DrawningTrolleybus:DrawningBus g.DrawLine(addpen, _startPosX.Value + 62, _startPosY.Value + 2, _startPosX.Value + 124, _startPosY.Value + 29); - } - } - - - - } diff --git a/TrolleybusProject/TrolleybusProject/FormBusCollection.Designer.cs b/TrolleybusProject/TrolleybusProject/FormBusCollection.Designer.cs new file mode 100644 index 0000000..1b19def --- /dev/null +++ b/TrolleybusProject/TrolleybusProject/FormBusCollection.Designer.cs @@ -0,0 +1,173 @@ +namespace TrolleybusProject +{ + partial class FormBusCollection + { + /// + /// 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(); + buttonRefresh = new Button(); + buttonGoToCheck = new Button(); + buttonDelTrolleybus = new Button(); + maskedTextBox = new MaskedTextBox(); + buttonAddTrolleybus = new Button(); + buttonAddBus = new Button(); + comboBoxSelectionCompany = new ComboBox(); + pictureBox = new PictureBox(); + groupBoxTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + SuspendLayout(); + // + // groupBoxTools + // + groupBoxTools.Controls.Add(buttonRefresh); + groupBoxTools.Controls.Add(buttonGoToCheck); + groupBoxTools.Controls.Add(buttonDelTrolleybus); + groupBoxTools.Controls.Add(maskedTextBox); + groupBoxTools.Controls.Add(buttonAddTrolleybus); + groupBoxTools.Controls.Add(buttonAddBus); + groupBoxTools.Controls.Add(comboBoxSelectionCompany); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(904, 0); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(300, 656); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // buttonRefresh + // + buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRefresh.Location = new Point(20, 485); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(249, 34); + buttonRefresh.TabIndex = 6; + buttonRefresh.Text = "Обновить"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += buttonRefresh_Click; + // + // buttonGoToCheck + // + buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonGoToCheck.Location = new Point(20, 395); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(249, 34); + buttonGoToCheck.TabIndex = 5; + buttonGoToCheck.Text = "Передать на тесты"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += buttonGoToCheck_Click; + // + // buttonDelTrolleybus + // + buttonDelTrolleybus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonDelTrolleybus.Location = new Point(40, 278); + buttonDelTrolleybus.Name = "buttonDelTrolleybus"; + buttonDelTrolleybus.Size = new Size(217, 34); + buttonDelTrolleybus.TabIndex = 4; + buttonDelTrolleybus.Text = "Удалить автобус"; + buttonDelTrolleybus.UseVisualStyleBackColor = true; + buttonDelTrolleybus.Click += buttonDelTrolleybus_Click; + // + // maskedTextBox + // + maskedTextBox.Location = new Point(73, 227); + maskedTextBox.Mask = "00"; + maskedTextBox.Name = "maskedTextBox"; + maskedTextBox.Size = new Size(150, 31); + maskedTextBox.TabIndex = 3; + maskedTextBox.ValidatingType = typeof(int); + // + // buttonAddTrolleybus + // + buttonAddTrolleybus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddTrolleybus.Location = new Point(19, 165); + buttonAddTrolleybus.Name = "buttonAddTrolleybus"; + buttonAddTrolleybus.Size = new Size(249, 34); + buttonAddTrolleybus.TabIndex = 2; + buttonAddTrolleybus.Text = "Добавление троллейбуса"; + buttonAddTrolleybus.UseVisualStyleBackColor = true; + buttonAddTrolleybus.Click += buttonAddTrolleybus_Click; + // + // buttonAddBus + // + buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddBus.Location = new Point(20, 97); + buttonAddBus.Name = "buttonAddBus"; + buttonAddBus.Size = new Size(249, 34); + buttonAddBus.TabIndex = 1; + buttonAddBus.Text = "Добавление автобуса"; + buttonAddBus.UseVisualStyleBackColor = true; + buttonAddBus.Click += buttonAddBus_Click; + // + // comboBoxSelectionCompany + // + comboBoxSelectionCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + comboBoxSelectionCompany.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxSelectionCompany.FormattingEnabled = true; + comboBoxSelectionCompany.Items.AddRange(new object[] { "Хранилище" }); + comboBoxSelectionCompany.Location = new Point(19, 30); + comboBoxSelectionCompany.Name = "comboBoxSelectionCompany"; + comboBoxSelectionCompany.Size = new Size(250, 33); + comboBoxSelectionCompany.TabIndex = 0; + comboBoxSelectionCompany.SelectedIndexChanged += comboBoxSelectionCompany_SelectedIndexChanged; + // + // pictureBox + // + pictureBox.Dock = DockStyle.Fill; + pictureBox.Location = new Point(0, 0); + pictureBox.Name = "pictureBox"; + pictureBox.Size = new Size(904, 656); + pictureBox.TabIndex = 1; + pictureBox.TabStop = false; + // + // FormBusCollection + // + AutoScaleDimensions = new SizeF(10F, 25F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1204, 656); + Controls.Add(pictureBox); + Controls.Add(groupBoxTools); + Name = "FormBusCollection"; + Text = "Коллекция автобусов"; + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxTools; + private ComboBox comboBoxSelectionCompany; + private Button buttonAddTrolleybus; + private Button buttonAddBus; + private Button buttonDelTrolleybus; + private MaskedTextBox maskedTextBox; + private PictureBox pictureBox; + private Button buttonRefresh; + private Button buttonGoToCheck; + } +} \ No newline at end of file diff --git a/TrolleybusProject/TrolleybusProject/FormBusCollection.cs b/TrolleybusProject/TrolleybusProject/FormBusCollection.cs new file mode 100644 index 0000000..abcd519 --- /dev/null +++ b/TrolleybusProject/TrolleybusProject/FormBusCollection.cs @@ -0,0 +1,145 @@ +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; +using TrolleybusProject.CollectionGenericObjects; +using TrolleybusProject.Drawnings; + +namespace TrolleybusProject; + +public partial class FormBusCollection : Form + +{ + private AbstractCompany? _company = null; + public FormBusCollection() + { + InitializeComponent(); + } + + + private void CreateObject(string type) + { + if (_company == null) + { + return; + } + Random random = new(); + DrawningBus drawningBus; + switch (type) + { + case nameof(DrawningBus): + drawningBus = new DrawningBus(random.Next(100, 300), + random.Next(1000, 3000), GetColor(random)); + break; + case nameof(DrawningTrolleybus): + drawningBus = new DrawningTrolleybus(random.Next(100, 300), random.Next(1000, 3000), GetColor(random), + GetColor(random), Convert.ToBoolean(random.Next(0, 2)), + Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); + break; + default: + return; + } + if (_company + drawningBus != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _company.Show(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + + private void buttonAddBus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningBus)); + + private void comboBoxSelectionCompany_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxSelectionCompany.Text) + { + case "Хранилище": + _company = new BusStation(pictureBox.Width, + pictureBox.Height, new MassiveGenericObjects()); + break; + } + + } + + private void buttonAddTrolleybus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTrolleybus)); + + private static Color GetColor(Random random) + { + 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; + } + return color; + } + + private void buttonDelTrolleybus_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) + { + return; + } + int pos = Convert.ToInt32(maskedTextBox.Text); + if (_company - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _company.Show(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void buttonGoToCheck_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + DrawningBus? bus = null; + int counter = 100; + while (bus == null) + { + bus = _company.GetRandomObject(); + counter--; + + } + if (bus == null) + { + return; + } + FormTrolleybus form = new() + { + SetBus = bus + }; + form.ShowDialog(); + } + + private void buttonRefresh_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + pictureBox.Image = _company.Show(); + } + + +} + diff --git a/TrolleybusProject/TrolleybusProject/FormBusCollection.resx b/TrolleybusProject/TrolleybusProject/FormBusCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/TrolleybusProject/TrolleybusProject/FormBusCollection.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/TrolleybusProject/TrolleybusProject/FormTrolleybus.Designer.cs b/TrolleybusProject/TrolleybusProject/FormTrolleybus.Designer.cs index 0205489..0240b26 100644 --- a/TrolleybusProject/TrolleybusProject/FormTrolleybus.Designer.cs +++ b/TrolleybusProject/TrolleybusProject/FormTrolleybus.Designer.cs @@ -29,12 +29,10 @@ private void InitializeComponent() { pictureBoxTrolleybus = new PictureBox(); - buttonCreate = new Button(); buttonLeft = new Button(); buttonRight = new Button(); buttonUp = new Button(); buttonDown = new Button(); - buttonCreateBus = new Button(); comboBoxStrategy = new ComboBox(); buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxTrolleybus).BeginInit(); @@ -49,17 +47,6 @@ pictureBoxTrolleybus.TabIndex = 0; pictureBoxTrolleybus.TabStop = false; // - // buttonCreate - // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(21, 515); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(188, 34); - buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать Троллейбус"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += buttonCreate_Click; - // // buttonLeft // buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; @@ -108,17 +95,6 @@ buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += buttonMove_Click; // - // buttonCreateBus - // - buttonCreateBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateBus.Location = new Point(249, 515); - buttonCreateBus.Name = "buttonCreateBus"; - buttonCreateBus.Size = new Size(188, 34); - buttonCreateBus.TabIndex = 6; - buttonCreateBus.Text = "Создать Автобус"; - buttonCreateBus.UseVisualStyleBackColor = true; - buttonCreateBus.Click += buttonCreateBus_Click; - // // comboBoxStrategy // comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; @@ -146,12 +122,10 @@ ClientSize = new Size(1273, 561); Controls.Add(buttonStrategyStep); Controls.Add(comboBoxStrategy); - Controls.Add(buttonCreateBus); Controls.Add(buttonDown); Controls.Add(buttonUp); Controls.Add(buttonRight); Controls.Add(buttonLeft); - Controls.Add(buttonCreate); Controls.Add(pictureBoxTrolleybus); Name = "FormTrolleybus"; Text = "Троллейбус"; @@ -162,12 +136,10 @@ #endregion private PictureBox pictureBoxTrolleybus; - private Button buttonCreate; private Button buttonLeft; private Button buttonRight; private Button buttonUp; private Button buttonDown; - private Button buttonCreateBus; private ComboBox comboBoxStrategy; private Button buttonStrategyStep; } diff --git a/TrolleybusProject/TrolleybusProject/FormTrolleybus.cs b/TrolleybusProject/TrolleybusProject/FormTrolleybus.cs index 644be9c..e12770d 100644 --- a/TrolleybusProject/TrolleybusProject/FormTrolleybus.cs +++ b/TrolleybusProject/TrolleybusProject/FormTrolleybus.cs @@ -10,176 +10,130 @@ using System.Windows.Forms; using TrolleybusProject.Drawnings; using TrolleybusProject.MovementStrategy; -namespace TrolleybusProject +namespace TrolleybusProject; + +public partial class FormTrolleybus : Form { - public partial class FormTrolleybus : Form + private DrawningBus? _drawningBus; + + private AbstractractStrategy? _strategy; + + + public DrawningBus SetBus { - private DrawningBus? _drawningBus; - - private AbstractractStrategy? _strategy; - public FormTrolleybus() + set { - InitializeComponent(); - _strategy = null; - } - - - - - /// - /// Метод прорисовки машины - /// - private void Draw() - { - if (_drawningBus == null) - { - return; - } - Bitmap bmp = new(pictureBoxTrolleybus.Width, - pictureBoxTrolleybus.Height); - Graphics gr = Graphics.FromImage(bmp); - _drawningBus.DrawTransport(gr); - pictureBoxTrolleybus.Image = bmp; - } - - - - /// - /// Создание объекта класса-перемещения - /// - /// Тип создаваемого объекта - private void CreateObject(string type) - { - Random random = new(); - switch (type) - { - case nameof(DrawningBus): - _drawningBus = new DrawningBus(random.Next(100, 300), - random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), - random.Next(0, 256), random.Next(0, 256))); - break; - case nameof(DrawningTrolleybus): - _drawningBus = new DrawningTrolleybus(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)), - Convert.ToBoolean(random.Next(0, 2)), - Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); - break; - default: - return; - } + _drawningBus = value; _drawningBus.SetPictureSize(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); - _drawningBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); - - _strategy = null; comboBoxStrategy.Enabled = true; - Draw(); + _strategy = null; + Draw(); } + } + public FormTrolleybus() + { + InitializeComponent(); + _strategy = null; + } - - - - - - - private void buttonCreate_Click(object sender, EventArgs e) + /// + /// Метод прорисовки машины + /// + private void Draw() + { + if (_drawningBus == null) { - CreateObject(nameof(DrawningTrolleybus)); + return; } + Bitmap bmp = new(pictureBoxTrolleybus.Width, + pictureBoxTrolleybus.Height); + Graphics gr = Graphics.FromImage(bmp); + _drawningBus.DrawTransport(gr); + pictureBoxTrolleybus.Image = bmp; + } - - - private void buttonMove_Click(object sender, EventArgs e) + private void buttonMove_Click(object sender, EventArgs e) + { + if (_drawningBus == null) { - if (_drawningBus == null) - { - return; - } - string name = ((Button)sender)?.Name ?? string.Empty; - bool result = false; - switch (name) - { - case "buttonUp": - result = _drawningBus.MoveTransport(DirectionType.Up); - break; - case "buttonDown": - result = _drawningBus.MoveTransport(DirectionType.Down); - break; - case "buttonLeft": - result = _drawningBus.MoveTransport(DirectionType.Left); - break; - case "buttonRight": - result = _drawningBus.MoveTransport(DirectionType.Right); - break; + return; + } + string name = ((Button)sender)?.Name ?? string.Empty; + bool result = false; + switch (name) + { + case "buttonUp": + result = _drawningBus.MoveTransport(DirectionType.Up); + break; + case "buttonDown": + result = _drawningBus.MoveTransport(DirectionType.Down); + break; + case "buttonLeft": + result = _drawningBus.MoveTransport(DirectionType.Left); + break; + case "buttonRight": + result = _drawningBus.MoveTransport(DirectionType.Right); + break; - } - if (result) - { - Draw(); + } + if (result) + { + Draw(); - } - } + } - private void buttonCreateBus_Click(object sender, EventArgs e) + + + + + + + + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawningBus == null) { - CreateObject(nameof(DrawningBus)); - + return; } - - - - - - private void buttonStrategyStep_Click(object sender, EventArgs e) + if (comboBoxStrategy.Enabled) { - if (_drawningBus == null) + _strategy = comboBoxStrategy.SelectedIndex switch { - return; - } - if (comboBoxStrategy.Enabled) - { - _strategy = comboBoxStrategy.SelectedIndex switch - { - 0 => new MoveToCenter(), - 1 => new MoveToBorder(), - _ => null, - }; - if (_strategy == null) - { - return; - } - _strategy.SetData(new MoveableBus(_drawningBus), - pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); - } + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; if (_strategy == null) { return; } - comboBoxStrategy.Enabled = false; - _strategy.MakeStep(); - Draw(); - if (_strategy.GetStatus() == StrategyStatus.Finish) - { - comboBoxStrategy.Enabled = true; - _strategy = null; - } + _strategy.SetData(new MoveableBus(_drawningBus), + pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); + } + if (_strategy == null) + { + return; + } + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; } - } + } diff --git a/TrolleybusProject/TrolleybusProject/Program.cs b/TrolleybusProject/TrolleybusProject/Program.cs index 4dc2fb8..1b9f49a 100644 --- a/TrolleybusProject/TrolleybusProject/Program.cs +++ b/TrolleybusProject/TrolleybusProject/Program.cs @@ -11,7 +11,7 @@ namespace TrolleybusProject // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormTrolleybus()); + Application.Run(new FormBusCollection()); } } } \ No newline at end of file