From e7b93671685d08b1792ec8288ddec6393d0a2e69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D0=BE=D0=BD=D0=B8=D0=B4=20=D0=9C=D0=B0=D0=BB?= =?UTF-8?q?=D0=B0=D1=84=D0=B5=D0=B5=D0=B2?= Date: Sat, 21 Oct 2023 14:10:32 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=A2=D1=80=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cruiser/Drawing/DrawingCruiser.cs | 7 + Cruiser/FormCruiser.Designer.cs | 14 ++ Cruiser/FormCruiser.cs | 46 +++++- Cruiser/FormCruiserCollection.Designer.cs | 124 +++++++++++++++ Cruiser/FormCruiserCollection.cs | 100 ++++++++++++ Cruiser/FormCruiserCollection.resx | 120 +++++++++++++++ Cruiser/Generics/CruisersGenericCollection.cs | 142 ++++++++++++++++++ Cruiser/Generics/SetGeneric.cs | 108 +++++++++++++ Cruiser/Program.cs | 2 +- 9 files changed, 659 insertions(+), 4 deletions(-) create mode 100644 Cruiser/FormCruiserCollection.Designer.cs create mode 100644 Cruiser/FormCruiserCollection.cs create mode 100644 Cruiser/FormCruiserCollection.resx create mode 100644 Cruiser/Generics/CruisersGenericCollection.cs create mode 100644 Cruiser/Generics/SetGeneric.cs diff --git a/Cruiser/Drawing/DrawingCruiser.cs b/Cruiser/Drawing/DrawingCruiser.cs index 9206af7..a479a79 100644 --- a/Cruiser/Drawing/DrawingCruiser.cs +++ b/Cruiser/Drawing/DrawingCruiser.cs @@ -1,4 +1,5 @@ using Cruiser.Entities; +using Cruiser.MovementStrategy; using System; using System.Collections.Generic; using System.Linq; @@ -97,6 +98,12 @@ namespace Cruiser.Drawing /// /// Координата X /// Координата Y + + /// + /// Получение объекта IMoveableObject из объекта DrawningCar + /// + public IMoveableObject GetMoveableObject => new + DrawningObjectCar(this); public void SetPosition(int x, int y) { if (x < 0 || y < 0) diff --git a/Cruiser/FormCruiser.Designer.cs b/Cruiser/FormCruiser.Designer.cs index 2c2f206..077ac2d 100644 --- a/Cruiser/FormCruiser.Designer.cs +++ b/Cruiser/FormCruiser.Designer.cs @@ -37,6 +37,7 @@ buttonCreateProLiner = new Button(); comboBoxStrategy = new ComboBox(); ButtonStep = new Button(); + buttonSelectCruiser = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCruiser).BeginInit(); SuspendLayout(); // @@ -133,11 +134,22 @@ ButtonStep.UseVisualStyleBackColor = true; ButtonStep.Click += ButtonStep_Click; // + // buttonSelectCruiser + // + buttonSelectCruiser.Location = new Point(274, 396); + buttonSelectCruiser.Name = "buttonSelectCruiser"; + buttonSelectCruiser.Size = new Size(127, 48); + buttonSelectCruiser.TabIndex = 9; + buttonSelectCruiser.Text = "Выбрать этот лайнер"; + buttonSelectCruiser.UseVisualStyleBackColor = true; + buttonSelectCruiser.Click += ButtonSelectCruiser_Click; + // // FormCruiser // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(buttonSelectCruiser); Controls.Add(ButtonStep); Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateProLiner); @@ -149,6 +161,7 @@ Controls.Add(pictureBoxCruiser); Name = "FormCruiser"; Text = "Cruiser"; + Load += FormCruiser_Load; ((System.ComponentModel.ISupportInitialize)pictureBoxCruiser).EndInit(); ResumeLayout(false); } @@ -164,5 +177,6 @@ private Button buttonCreateProLiner; private ComboBox comboBoxStrategy; private Button ButtonStep; + private Button buttonSelectCruiser; } } \ No newline at end of file diff --git a/Cruiser/FormCruiser.cs b/Cruiser/FormCruiser.cs index 91f29d1..7cee9c9 100644 --- a/Cruiser/FormCruiser.cs +++ b/Cruiser/FormCruiser.cs @@ -16,6 +16,11 @@ namespace Cruiser /// /// private AbstractStrategy? _abstractStrategy; + /// + /// + /// + public DrawingCruiser? SelectedCruiser { get; private set; } + /// /// /// @@ -23,6 +28,8 @@ namespace Cruiser { InitializeComponent(); bmp = new(pictureBoxCruiser.Width, pictureBoxCruiser.Width); + _abstractStrategy = null; + SelectedCruiser = null; } @@ -116,9 +123,16 @@ namespace Cruiser private void buttonCreateLiner_Click(object sender, EventArgs e) { Random random = new(); + Color colorFirst = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + colorFirst = dialog.Color; + } + _drawningCruiser = new DrawingCruiser(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + colorFirst, Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxCruiser.Width, pictureBoxCruiser.Height); @@ -129,10 +143,21 @@ namespace Cruiser private void buttonCreateProLiner_Click(object sender, EventArgs e) { Random random = new(); + Color colorFirst = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + colorFirst = dialog.Color; + } + Color colorSecond = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + if (dialog.ShowDialog() == DialogResult.OK) + { + colorSecond = dialog.Color; + } _drawningCruiser = new DrawingProCruiser(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)), + colorFirst, + colorSecond, Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), Convert.ToBoolean(random.Next(1, 2)), Convert.ToBoolean(random.Next(1, 2)), @@ -141,5 +166,20 @@ namespace Cruiser _drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } + /// + /// + /// + /// + /// + private void ButtonSelectCruiser_Click(object sender, EventArgs e) + { + SelectedCruiser = _drawningCruiser; + DialogResult = DialogResult.OK; + } + + private void FormCruiser_Load(object sender, EventArgs e) + { + + } } } \ No newline at end of file diff --git a/Cruiser/FormCruiserCollection.Designer.cs b/Cruiser/FormCruiserCollection.Designer.cs new file mode 100644 index 0000000..cc2ca2b --- /dev/null +++ b/Cruiser/FormCruiserCollection.Designer.cs @@ -0,0 +1,124 @@ +namespace Cruiser +{ + partial class FormCruiserCollection + { + /// + /// 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(); + button3 = new Button(); + button2 = new Button(); + textBoxNumber = new TextBox(); + buttonAddCruiser = new Button(); + pictureBoxCollection = new PictureBox(); + groupBoxTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + SuspendLayout(); + // + // groupBoxTools + // + groupBoxTools.Controls.Add(button3); + groupBoxTools.Controls.Add(button2); + groupBoxTools.Controls.Add(textBoxNumber); + groupBoxTools.Controls.Add(buttonAddCruiser); + groupBoxTools.Location = new Point(645, 1); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(154, 448); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // button3 + // + button3.Location = new Point(15, 171); + button3.Name = "button3"; + button3.Size = new Size(122, 41); + button3.TabIndex = 3; + button3.Text = "Обновить коллекцию"; + button3.UseVisualStyleBackColor = true; + button3.Click += ButtonRefreshCollection_Click; + // + // button2 + // + button2.Location = new Point(15, 107); + button2.Name = "button2"; + button2.Size = new Size(122, 37); + button2.TabIndex = 2; + button2.Text = "Удалить лайнер"; + button2.UseVisualStyleBackColor = true; + button2.Click += ButtonRemoveCar_Click; + // + // textBoxNumber + // + textBoxNumber.Location = new Point(15, 78); + textBoxNumber.Name = "textBoxNumber"; + textBoxNumber.Size = new Size(122, 23); + textBoxNumber.TabIndex = 1; + // + // buttonAddCruiser + // + buttonAddCruiser.Location = new Point(15, 22); + buttonAddCruiser.Name = "buttonAddCruiser"; + buttonAddCruiser.Size = new Size(122, 38); + buttonAddCruiser.TabIndex = 0; + buttonAddCruiser.Text = "Добавить лайнер"; + buttonAddCruiser.UseVisualStyleBackColor = true; + buttonAddCruiser.Click += buttonAddCruiser_Click; + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new Point(-1, 1); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(642, 448); + pictureBoxCollection.TabIndex = 1; + pictureBoxCollection.TabStop = false; + // + // FormCruiserCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(pictureBoxCollection); + Controls.Add(groupBoxTools); + Name = "FormCruiserCollection"; + Text = "Набор Крейсеров"; + Load += FormCruiserCollection_Load; + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxTools; + private TextBox textBoxNumber; + private Button buttonAddCruiser; + private Button button3; + private Button button2; + private PictureBox pictureBoxCollection; + } +} \ No newline at end of file diff --git a/Cruiser/FormCruiserCollection.cs b/Cruiser/FormCruiserCollection.cs new file mode 100644 index 0000000..7b2df8a --- /dev/null +++ b/Cruiser/FormCruiserCollection.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Reflection.Metadata.Ecma335; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using Cruiser.Drawing; +using Cruiser.Generics; +using Cruiser.MovementStrategy; + +namespace Cruiser +{ + /// + /// Форма для работы с набором объектов класса DrawingCruiser + /// + public partial class FormCruiserCollection : Form + { + /// + /// Набор объектов + /// + private readonly CarsGenericCollection _cruisers; + /// + /// Конструктор + /// + public FormCruiserCollection() + { + InitializeComponent(); + _cruisers = new CarsGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + /// + /// Добавление объекта в набор + /// + /// + /// + private void buttonAddCruiser_Click(object sender, EventArgs e) + { + FormCruiser form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_cruisers + form.SelectedCruiser != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _cruisers.ShowCruiser(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта из набора + /// + /// + /// + private void ButtonRemoveCar_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + if (textBoxNumber.Text == "") + { + MessageBox.Show("Вы ничего не ввели"); + return; + } + int pos = Convert.ToInt32(textBoxNumber.Text); + if (_cruisers - pos) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _cruisers.ShowCruiser(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Обновление рисунка по набору + /// + /// + /// + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _cruisers.ShowCruiser(); + } + + + + private void FormCruiserCollection_Load(object sender, EventArgs e) + { + + } + } +} diff --git a/Cruiser/FormCruiserCollection.resx b/Cruiser/FormCruiserCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Cruiser/FormCruiserCollection.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/Cruiser/Generics/CruisersGenericCollection.cs b/Cruiser/Generics/CruisersGenericCollection.cs new file mode 100644 index 0000000..fd10545 --- /dev/null +++ b/Cruiser/Generics/CruisersGenericCollection.cs @@ -0,0 +1,142 @@ +using Cruiser.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Cruiser.Drawing; + +namespace Cruiser.Generics +{ + /// + /// Параметризованный класс для набора объектов DrawingCruiser + /// + /// + /// + internal class CarsGenericCollection + where T : DrawingCruiser + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 160; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 60; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public CarsGenericCollection(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 +(CarsGenericCollection collect, T? obj) + { + if (obj == null) + { + return -1; + } + return collect._collection.Insert(obj); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(CarsGenericCollection collect, int pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + collect._collection.Remove(pos); + return true; + } + return false; + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowCruiser() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawObjects(gr); + DrawBackground(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; j++) + { + g.DrawRectangle(pen, i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth, _placeSizeHeight); + } + + } + } + /// + /// /// Метод прорисовки объектов + /// + /// + private void DrawObjects(Graphics g) + { + int Ix = 0; + int Iy = 0; + for (int i = 0; i < _collection.Count - 1; i++) + { + _collection.Get(i)?.SetPosition(Ix, Iy); + _collection.Get(i)?.DrawTransport(g); + Ix += _placeSizeWidth; + if (Ix + _placeSizeHeight > _pictureWidth) + { + Ix = 0; + Iy = _placeSizeHeight; + } + } + } + } + +} diff --git a/Cruiser/Generics/SetGeneric.cs b/Cruiser/Generics/SetGeneric.cs new file mode 100644 index 0000000..12b76e0 --- /dev/null +++ b/Cruiser/Generics/SetGeneric.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.Eventing.Reader; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Cruiser.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 cruiser) + { + int zero = 0; + for (; _places[zero] != null;) + { + zero++; + if (zero == _places.Length) + { + return -1; + } + } + for (int i = zero; i > 0; i--) + { + _places[i] = _places[i - 1]; + } + _places[0] = cruiser; + return 0; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + if (position < 0 || position > _places.Length) + { + return false; + } + _places[position] = null; + return true; + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + public int Insert(T cruiser, int position) + { + if (position < 0 || position > _places.Length) + { + return 0; + } + if (_places.Length != null) + { + int zero = position; + while (_places[zero] != null) + { + zero++; + return -1; + } + } + _places[position] = cruiser; + return position; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + if (position < 0 || position > _places.Length) + { + return null; + } + return _places[position]; + } + } +} diff --git a/Cruiser/Program.cs b/Cruiser/Program.cs index 626bc14..2672862 100644 --- a/Cruiser/Program.cs +++ b/Cruiser/Program.cs @@ -11,7 +11,7 @@ namespace Cruiser // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormCruiser()); + Application.Run(new FormCruiserCollection()); } } } \ No newline at end of file