From 5d96a63aa72291dd61ddd2db48ad9698630516e2 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, 10 Oct 2023 22:08:33 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=203?= =?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/DrawningRoadTrain.cs | 8 +- RoadTrain/FormRoadTrain.Designer.cs | 66 +++++----- RoadTrain/FormRoadTrain.cs | 83 +++++++++---- RoadTrain/FormTrainCollection.Designer.cs | 133 ++++++++++++++++++++ RoadTrain/FormTrainCollection.cs | 70 +++++++++++ RoadTrain/FormTrainCollection.resx | 120 ++++++++++++++++++ RoadTrain/Program.cs | 2 +- RoadTrain/SetGeneric.cs | 88 ++++++++++++++ RoadTrain/TrainGenericCollection.cs | 141 ++++++++++++++++++++++ 9 files changed, 653 insertions(+), 58 deletions(-) create mode 100644 RoadTrain/FormTrainCollection.Designer.cs create mode 100644 RoadTrain/FormTrainCollection.cs create mode 100644 RoadTrain/FormTrainCollection.resx create mode 100644 RoadTrain/SetGeneric.cs create mode 100644 RoadTrain/TrainGenericCollection.cs diff --git a/RoadTrain/DrawningRoadTrain.cs b/RoadTrain/DrawningRoadTrain.cs index 8a261ed..2fdc037 100644 --- a/RoadTrain/DrawningRoadTrain.cs +++ b/RoadTrain/DrawningRoadTrain.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using RoadTrain.Entities; +using RoadTrain.MovementStrategy; namespace RoadTrain.DrawningObjects { @@ -101,10 +102,6 @@ namespace RoadTrain.DrawningObjects /// Координата Y public void SetPosition(int x, int y) { - if (x < 0) { x = 0; } - if (y < 0) { y = 0; } - if (x > 200) { x = 200; } - if (y > 200) { y = 200; } _startPosX = x; _startPosY = y; } @@ -139,6 +136,9 @@ namespace RoadTrain.DrawningObjects break; } } + public IMoveableObject GetMoveableObject => new +DrawningObjectTrain(this); + /// /// Прорисовка объекта /// diff --git a/RoadTrain/FormRoadTrain.Designer.cs b/RoadTrain/FormRoadTrain.Designer.cs index b407a4d..6e05ae9 100644 --- a/RoadTrain/FormRoadTrain.Designer.cs +++ b/RoadTrain/FormRoadTrain.Designer.cs @@ -33,10 +33,11 @@ buttonUp = new Button(); buttonRight = new Button(); buttonDown = new Button(); - buttonCreate = new Button(); - button1 = new Button(); comboBoxStrategy = new ComboBox(); ButtonStep = new Button(); + ButtonCreateTrain = new Button(); + ButtonCreateRoadTrain = new Button(); + ButtonSelectTrain = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxRoadTrain).BeginInit(); SuspendLayout(); // @@ -96,27 +97,6 @@ buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += ButtonMove_Click; // - // buttonCreate - // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(82, 236); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(107, 38); - buttonCreate.TabIndex = 6; - buttonCreate.Text = "создать грузовик"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += buttonCreate_Click; - // - // button1 - // - button1.Location = new Point(82, 289); - button1.Name = "button1"; - button1.Size = new Size(107, 55); - button1.TabIndex = 7; - button1.Text = "создать очистительную машину"; - button1.UseVisualStyleBackColor = true; - button1.Click += button1_Click; - // // comboBoxStrategy // comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; @@ -137,15 +117,46 @@ ButtonStep.UseVisualStyleBackColor = true; ButtonStep.Click += ButtonStep_Click_1; // + // ButtonCreateTrain + // + ButtonCreateTrain.Location = new Point(30, 285); + ButtonCreateTrain.Name = "ButtonCreateTrain"; + ButtonCreateTrain.Size = new Size(115, 43); + ButtonCreateTrain.TabIndex = 11; + ButtonCreateTrain.Text = "Создать моющую машину"; + ButtonCreateTrain.UseVisualStyleBackColor = true; + ButtonCreateTrain.Click += ButtonCreateTrain_Click; + // + // ButtonCreateRoadTrain + // + ButtonCreateRoadTrain.Location = new Point(180, 285); + ButtonCreateRoadTrain.Name = "ButtonCreateRoadTrain"; + ButtonCreateRoadTrain.Size = new Size(93, 43); + ButtonCreateRoadTrain.TabIndex = 12; + ButtonCreateRoadTrain.Text = "Создать поезд"; + ButtonCreateRoadTrain.UseVisualStyleBackColor = true; + ButtonCreateRoadTrain.Click += ButtonCreateRoadTrain_Click; + // + // ButtonSelectTrain + // + ButtonSelectTrain.Location = new Point(567, 94); + ButtonSelectTrain.Name = "ButtonSelectTrain"; + ButtonSelectTrain.Size = new Size(89, 42); + ButtonSelectTrain.TabIndex = 13; + ButtonSelectTrain.Text = "Добавить поезд"; + ButtonSelectTrain.UseVisualStyleBackColor = true; + ButtonSelectTrain.Click += ButtonSelectTrain_Click; + // // FormRoadTrain // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(679, 363); + Controls.Add(ButtonSelectTrain); + Controls.Add(ButtonCreateRoadTrain); + Controls.Add(ButtonCreateTrain); Controls.Add(ButtonStep); Controls.Add(comboBoxStrategy); - Controls.Add(button1); - Controls.Add(buttonCreate); Controls.Add(buttonDown); Controls.Add(buttonRight); Controls.Add(buttonUp); @@ -165,9 +176,10 @@ private Button buttonUp; private Button buttonRight; private Button buttonDown; - private Button buttonCreate; - private Button button1; private ComboBox comboBoxStrategy; private Button ButtonStep; + private Button ButtonCreateTrain; + private Button ButtonCreateRoadTrain; + private Button ButtonSelectTrain; } } \ No newline at end of file diff --git a/RoadTrain/FormRoadTrain.cs b/RoadTrain/FormRoadTrain.cs index 807a737..90e9a79 100644 --- a/RoadTrain/FormRoadTrain.cs +++ b/RoadTrain/FormRoadTrain.cs @@ -1,5 +1,6 @@ using RoadTrain.MovementStrategy; using RoadTrain.DrawningObjects; +using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window; namespace RoadTrain @@ -12,12 +13,16 @@ namespace RoadTrain private DrawningRoadTrain? _drawningRoadTrain; private AbstractStrategy? _abstractStrategy; + public DrawningRoadTrain? SelectedTrain { get; private set; } + /// /// /// public FormRoadTrain() { InitializeComponent(); + _abstractStrategy = null; + SelectedTrain = null; } /// /// @@ -70,33 +75,7 @@ namespace RoadTrain Draw(); } - private void buttonCreate_Click(object sender, EventArgs e) - { - Random random = new(); - _drawningRoadTrain = new DrawningRoadTrain(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), - random.Next(0, 256)), - pictureBoxRoadTrain.Width, pictureBoxRoadTrain.Height); - _drawningRoadTrain.SetPosition(random.Next(10, 100), - random.Next(10, 100)); - Draw(); - } - private void button1_Click(object sender, EventArgs e) - { - Random random = new(); - _drawningRoadTrain = new DrawningTrain(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)), - pictureBoxRoadTrain.Width, pictureBoxRoadTrain.Height); - _drawningRoadTrain.SetPosition(random.Next(10, 100), - random.Next(10, 100)); - Draw(); - } private void ButtonStep_Click_1(object sender, EventArgs e) @@ -135,5 +114,57 @@ namespace RoadTrain _abstractStrategy = null; } } + + private void ButtonCreateRoadTrain_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; + } + _drawningRoadTrain = new DrawningRoadTrain(random.Next(100, 300), + random.Next(1000, 3000), color, + pictureBoxRoadTrain.Width, pictureBoxRoadTrain.Height); + _drawningRoadTrain.SetPosition(random.Next(10, 100), random.Next(10, + 100)); + Draw(); + + } + + private void ButtonCreateTrain_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)); + ColorDialog dialog2 = new(); + if (dialog2.ShowDialog() == DialogResult.OK) + { + color = dialog2.Color; + } + _drawningRoadTrain = new DrawningTrain(random.Next(100, 300), + random.Next(1000, 3000), color, + dopColor, Convert.ToBoolean(random.Next(0, 2)), + Convert.ToBoolean(random.Next(0, 2)), + pictureBoxRoadTrain.Width, pictureBoxRoadTrain.Height); + _drawningRoadTrain.SetPosition(random.Next(10, 100), random.Next(10, + 100)); + Draw(); + } + + private void ButtonSelectTrain_Click(object sender, EventArgs e) + { + SelectedTrain = _drawningRoadTrain; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/RoadTrain/FormTrainCollection.Designer.cs b/RoadTrain/FormTrainCollection.Designer.cs new file mode 100644 index 0000000..ac2ea2b --- /dev/null +++ b/RoadTrain/FormTrainCollection.Designer.cs @@ -0,0 +1,133 @@ +namespace RoadTrain +{ + partial class FormTrainCollection + { + /// + /// 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() + { + panel1 = new Panel(); + InputTextBox = new TextBox(); + ButtonRefreshCollection = new Button(); + ButtonRemoveTrain = new Button(); + ButtonAddTrain = new Button(); + label1 = new Label(); + pictureBoxCollection = new PictureBox(); + panel1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + SuspendLayout(); + // + // panel1 + // + 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.Name = "panel1"; + panel1.Size = new Size(200, 426); + panel1.TabIndex = 0; + // + // InputTextBox + // + InputTextBox.Location = new Point(49, 118); + InputTextBox.Name = "InputTextBox"; + InputTextBox.Size = new Size(100, 23); + InputTextBox.TabIndex = 4; + // + // ButtonRefreshCollection + // + ButtonRefreshCollection.Location = new Point(39, 233); + ButtonRefreshCollection.Name = "ButtonRefreshCollection"; + ButtonRefreshCollection.Size = new Size(126, 44); + ButtonRefreshCollection.TabIndex = 3; + ButtonRefreshCollection.Text = "Обновить коллекцию"; + ButtonRefreshCollection.UseVisualStyleBackColor = true; + ButtonRefreshCollection.Click += ButtonRefreshCollection_Click; + // + // ButtonRemoveTrain + // + ButtonRemoveTrain.Location = new Point(39, 160); + ButtonRemoveTrain.Name = "ButtonRemoveTrain"; + ButtonRemoveTrain.Size = new Size(126, 30); + ButtonRemoveTrain.TabIndex = 2; + ButtonRemoveTrain.Text = "Удалить поезд"; + ButtonRemoveTrain.UseVisualStyleBackColor = true; + ButtonRemoveTrain.Click += ButtonRemoveTrain_Click; + // + // ButtonAddTrain + // + ButtonAddTrain.Location = new Point(39, 45); + ButtonAddTrain.Name = "ButtonAddTrain"; + ButtonAddTrain.Size = new Size(126, 33); + ButtonAddTrain.TabIndex = 1; + ButtonAddTrain.Text = "Добавить поезд"; + ButtonAddTrain.UseVisualStyleBackColor = true; + ButtonAddTrain.Click += ButtonAddTrain_Click; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(66, 14); + label1.Name = "label1"; + label1.Size = new Size(83, 15); + label1.TabIndex = 0; + label1.Text = "Инструменты"; + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new Point(0, 1); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(591, 446); + pictureBoxCollection.TabIndex = 1; + pictureBoxCollection.TabStop = false; + // + // FormTrainCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(pictureBoxCollection); + Controls.Add(panel1); + Name = "FormTrainCollection"; + Text = "FormTrainCollection"; + panel1.ResumeLayout(false); + panel1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + ResumeLayout(false); + } + + #endregion + + private Panel panel1; + private Button ButtonAddTrain; + private Label label1; + private TextBox InputTextBox; + private Button ButtonRefreshCollection; + private Button ButtonRemoveTrain; + private PictureBox pictureBoxCollection; + } +} \ No newline at end of file diff --git a/RoadTrain/FormTrainCollection.cs b/RoadTrain/FormTrainCollection.cs new file mode 100644 index 0000000..3919c5d --- /dev/null +++ b/RoadTrain/FormTrainCollection.cs @@ -0,0 +1,70 @@ +using RoadTrain.DrawningObjects; +using RoadTrain.Generics; +using RoadTrain.MovementStrategy; +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 RoadTrain +{ + public partial class FormTrainCollection : Form + { + private readonly RoadTrainGenericCollection _trains; + public FormTrainCollection() + { + InitializeComponent(); + _trains = new RoadTrainGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + private void ButtonAddTrain_Click(object sender, EventArgs e) + { + FormRoadTrain form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_trains + form.SelectedTrain != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _trains.ShowTrains(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + + private void ButtonRemoveTrain_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", +MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(InputTextBox.Text); + if (_trains - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _trains.ShowTrains(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + + } + + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _trains.ShowTrains(); + + } + } +} diff --git a/RoadTrain/FormTrainCollection.resx b/RoadTrain/FormTrainCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/RoadTrain/FormTrainCollection.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/RoadTrain/Program.cs b/RoadTrain/Program.cs index 23d1345..8726baa 100644 --- a/RoadTrain/Program.cs +++ b/RoadTrain/Program.cs @@ -11,7 +11,7 @@ namespace RoadTrain // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormRoadTrain()); + Application.Run(new FormTrainCollection()); } } } \ No newline at end of file diff --git a/RoadTrain/SetGeneric.cs b/RoadTrain/SetGeneric.cs new file mode 100644 index 0000000..c64736d --- /dev/null +++ b/RoadTrain/SetGeneric.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RoadTrain.Generics +{ + 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 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; + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + 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; + return position; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + if ((position < 0) || (position > Count)) return false; + _places[position] = null; + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + if ((position < 0) || (position > Count)) return null; + return _places[position]; + } + } + +} diff --git a/RoadTrain/TrainGenericCollection.cs b/RoadTrain/TrainGenericCollection.cs new file mode 100644 index 0000000..aaf630f --- /dev/null +++ b/RoadTrain/TrainGenericCollection.cs @@ -0,0 +1,141 @@ +using RoadTrain.Generics; +using RoadTrain.MovementStrategy; +using System; +using RoadTrain.DrawningObjects; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RoadTrain.Generics +{ + internal class RoadTrainGenericCollection + where T : DrawningRoadTrain + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 110; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 50; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public RoadTrainGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static int operator +(RoadTrainGenericCollection collect, T? + obj) + { + if (obj == null) + { + return -1; + } + return collect._collection.Insert(obj); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static T? operator -(RoadTrainGenericCollection 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 ShowTrains() + { + 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 width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + for (int i = 0; i < _collection.Count; i++) + { + DrawningRoadTrain? tank = _collection.Get(i); + if (tank == null) + continue; + tank.SetPosition(i % width * _placeSizeWidth, i / width * _placeSizeHeight); + tank.DrawTransport(g); + } + } + } + } +}