From 63128db2e0f0302dbf01d810ecf04e4c00f76002 Mon Sep 17 00:00:00 2001 From: katana Date: Tue, 31 Oct 2023 20:18:37 +0400 Subject: [PATCH] =?UTF-8?q?3=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D1=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectSeaplane/DrawingPlane.cs | 6 + .../FormPlaneCollection.Designer.cs | 124 +++++++++++++++ .../ProjectSeaplane/FormPlaneCollection.cs | 91 +++++++++++ .../ProjectSeaplane/FormPlaneCollection.resx | 60 ++++++++ .../ProjectSeaplane/FormSeaplane.Designer.cs | 13 ++ .../ProjectSeaplane/FormSeaplane.cs | 41 ++++- .../PlanesGenericCollection.cs | 145 ++++++++++++++++++ ProjectSeaplane/ProjectSeaplane/Program.cs | 2 +- ProjectSeaplane/ProjectSeaplane/SetGeneric.cs | 114 ++++++++++++++ 9 files changed, 590 insertions(+), 6 deletions(-) create mode 100644 ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.Designer.cs create mode 100644 ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.cs create mode 100644 ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.resx create mode 100644 ProjectSeaplane/ProjectSeaplane/PlanesGenericCollection.cs create mode 100644 ProjectSeaplane/ProjectSeaplane/SetGeneric.cs diff --git a/ProjectSeaplane/ProjectSeaplane/DrawingPlane.cs b/ProjectSeaplane/ProjectSeaplane/DrawingPlane.cs index 92a916e..e1ed025 100644 --- a/ProjectSeaplane/ProjectSeaplane/DrawingPlane.cs +++ b/ProjectSeaplane/ProjectSeaplane/DrawingPlane.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using ProjectSeaplane.Entities; +using ProjectSeaplane.MovementStrategy; using static ProjectSeaplane.Direction; namespace ProjectSeaplane.DrawningObjects { @@ -176,6 +177,11 @@ namespace ProjectSeaplane.DrawningObjects } } + /// + /// Получение объекта IMoveableObject из объекта DrawningCar + /// + public IMoveableObject GetMoveableObject => new DrawningObjectPlane(this); + /// /// Прорисовка объекта /// diff --git a/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.Designer.cs b/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.Designer.cs new file mode 100644 index 0000000..ff6f7cf --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.Designer.cs @@ -0,0 +1,124 @@ +namespace ProjectSeaplane +{ + partial class FormPlaneCollection + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + pictureBoxCollection = new PictureBox(); + groupBox1 = new GroupBox(); + buttonRefreshCollection = new Button(); + buttonRemovePlane = new Button(); + maskedTextBoxNumber = new MaskedTextBox(); + buttonAddPlane = new Button(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + groupBox1.SuspendLayout(); + SuspendLayout(); + // + // pictureBoxCollection + // + pictureBoxCollection.Dock = DockStyle.Fill; + pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(1026, 443); + pictureBoxCollection.TabIndex = 1; + pictureBoxCollection.TabStop = false; + // + // groupBox1 + // + groupBox1.Controls.Add(buttonRefreshCollection); + groupBox1.Controls.Add(buttonRemovePlane); + groupBox1.Controls.Add(maskedTextBoxNumber); + groupBox1.Controls.Add(buttonAddPlane); + groupBox1.Location = new Point(852, 0); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new Size(174, 421); + groupBox1.TabIndex = 2; + groupBox1.TabStop = false; + groupBox1.Text = "Инструменты"; + // + // buttonRefreshCollection + // + buttonRefreshCollection.Location = new Point(14, 206); + buttonRefreshCollection.Name = "buttonRefreshCollection"; + buttonRefreshCollection.Size = new Size(149, 23); + buttonRefreshCollection.TabIndex = 3; + buttonRefreshCollection.Text = "Обновить коллекцию"; + buttonRefreshCollection.UseVisualStyleBackColor = true; + buttonRefreshCollection.Click += buttonRefreshCollection_Click; + // + // buttonRemovePlane + // + buttonRemovePlane.Location = new Point(14, 155); + buttonRemovePlane.Name = "buttonRemovePlane"; + buttonRemovePlane.Size = new Size(149, 24); + buttonRemovePlane.TabIndex = 2; + buttonRemovePlane.Text = "Удалить самолет"; + buttonRemovePlane.UseVisualStyleBackColor = true; + buttonRemovePlane.Click += buttonRemovePlane_Click; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(14, 101); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(149, 23); + maskedTextBoxNumber.TabIndex = 1; + // + // buttonAddPlane + // + buttonAddPlane.Location = new Point(14, 45); + buttonAddPlane.Name = "buttonAddPlane"; + buttonAddPlane.Size = new Size(149, 25); + buttonAddPlane.TabIndex = 0; + buttonAddPlane.Text = "Добавить самолет"; + buttonAddPlane.UseVisualStyleBackColor = true; + buttonAddPlane.Click += buttonAddPlane_Click; + // + // FormPlaneCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1026, 443); + Controls.Add(groupBox1); + Controls.Add(pictureBoxCollection); + Name = "FormPlaneCollection"; + Text = "Набор самолетов"; + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + groupBox1.ResumeLayout(false); + groupBox1.PerformLayout(); + ResumeLayout(false); + } + + #endregion + + private PictureBox pictureBoxCollection; + private GroupBox groupBox1; + private Button buttonRefreshCollection; + private Button buttonRemovePlane; + private MaskedTextBox maskedTextBoxNumber; + private Button buttonAddPlane; + } +} \ No newline at end of file diff --git a/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.cs b/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.cs new file mode 100644 index 0000000..ea207c1 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.cs @@ -0,0 +1,91 @@ +using ProjectSeaplane.DrawningObjects; +using ProjectSeaplane.Generics; +using ProjectSeaplane.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 ProjectSeaplane +{ + public partial class FormPlaneCollection : Form + { + /// + /// Набор объектов + /// + private readonly PlanesGenericCollection _planes; + /// + /// Конструктор + /// + public FormPlaneCollection() + { + InitializeComponent(); + _planes = new PlanesGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + /// + /// Добавление объекта в набор + /// + /// + /// + private void buttonAddPlane_Click(object sender, EventArgs e) + { + FormSeaplane form = new FormSeaplane(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_planes + form.SelectedPlane != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _planes.ShowPlanes(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта из набора + /// + /// + /// + private void buttonRemovePlane_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = -1; + try + { + pos = Convert.ToInt32(maskedTextBoxNumber.Text); + } + catch (Exception ex) { } + if (_planes - pos) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _planes.ShowPlanes(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Обновление рисунка по набору + /// + /// + /// + private void buttonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _planes.ShowPlanes(); + } + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.resx b/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/FormPlaneCollection.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs index dbddc87..07aa3c2 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs @@ -37,6 +37,7 @@ ComboBox = new ComboBox(); buttonCreateSeaplane = new Button(); buttonStep = new Button(); + buttonSelectPlane = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxSeaplane).BeginInit(); SuspendLayout(); // @@ -138,11 +139,22 @@ buttonStep.UseVisualStyleBackColor = true; buttonStep.Click += buttonStep_Click; // + // buttonSelectPlane + // + buttonSelectPlane.Location = new Point(338, 431); + buttonSelectPlane.Name = "buttonSelectPlane"; + buttonSelectPlane.Size = new Size(118, 23); + buttonSelectPlane.TabIndex = 9; + buttonSelectPlane.Text = "Выбрать объект"; + buttonSelectPlane.UseVisualStyleBackColor = true; + buttonSelectPlane.Click += buttonSelectPlane_Click; + // // FormSeaplane // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(884, 461); + Controls.Add(buttonSelectPlane); Controls.Add(buttonStep); Controls.Add(buttonCreateSeaplane); Controls.Add(ComboBox); @@ -171,5 +183,6 @@ private ComboBox ComboBox; private Button buttonCreateSeaplane; private Button buttonStep; + private Button buttonSelectPlane; } } \ No newline at end of file diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs index ff6f6c3..d83211e 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs @@ -13,11 +13,17 @@ namespace ProjectSeaplane /// /// private AbstractStrategy? _abstractStrategy; + /// + /// + /// + public DrawningPlane? SelectedPlane { get; private set; } public FormSeaplane() { InitializeComponent(); ComboBox.Items.Add(0); ComboBox.Items.Add(1); + _abstractStrategy = null; + SelectedPlane = null; } private void Draw() @@ -35,8 +41,15 @@ namespace ProjectSeaplane private void buttonCreatePlane_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } _drawningSeaplane = new DrawningPlane(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); + color, pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); _drawningSeaplane.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } @@ -70,11 +83,23 @@ namespace ProjectSeaplane private void buttonCreateSeaplane_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) + { + dopColor = dialog2.Color; + } _drawningSeaplane = new DrawningSeaplane(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)), + color, dopColor, true, true, pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); _drawningSeaplane.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -114,5 +139,11 @@ namespace ProjectSeaplane _abstractStrategy = null; } } + + private void buttonSelectPlane_Click(object sender, EventArgs e) + { + SelectedPlane = _drawningSeaplane; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/ProjectSeaplane/ProjectSeaplane/PlanesGenericCollection.cs b/ProjectSeaplane/ProjectSeaplane/PlanesGenericCollection.cs new file mode 100644 index 0000000..bdc82fb --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/PlanesGenericCollection.cs @@ -0,0 +1,145 @@ +using ProjectSeaplane.DrawningObjects; +using ProjectSeaplane.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.Generics +{ + internal class PlanesGenericCollection + where T : DrawningPlane + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 210; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 110; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + + /// + /// Конструктор + /// + /// + /// + public PlanesGenericCollection(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 +(PlanesGenericCollection collect, T? obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj); + } + + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(PlanesGenericCollection collect, int pos) + { + T? obj = collect._collection.Get(pos); + if (obj == null) + { + return false; + } + return collect._collection.Remove(pos); + } + + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowPlanes() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + {//линия рамзетки места + g.DrawLine(pen, i * _placeSizeWidth, j * + _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth, 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++) + { + // TODO получение объекта + DrawningPlane? plane = _collection.Get(i); + if (plane == null) + { + continue; + } + int r = width - (i / width) - 1; + int s = width - 1 - (i % width); + // TODO установка позиции + plane.SetPosition(s * _placeSizeWidth, r * _placeSizeHeight); + // TODO прорисовка объекта + plane.DrawTransport(g); + } + } + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/Program.cs b/ProjectSeaplane/ProjectSeaplane/Program.cs index 4e58da8..0b08fb7 100644 --- a/ProjectSeaplane/ProjectSeaplane/Program.cs +++ b/ProjectSeaplane/ProjectSeaplane/Program.cs @@ -11,7 +11,7 @@ namespace ProjectSeaplane // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormSeaplane()); + Application.Run(new FormPlaneCollection()); } } } \ No newline at end of file diff --git a/ProjectSeaplane/ProjectSeaplane/SetGeneric.cs b/ProjectSeaplane/ProjectSeaplane/SetGeneric.cs new file mode 100644 index 0000000..e33d7d9 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/SetGeneric.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.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 plane) + { + int index = -1; + for (int i = 0; i < _places.Length; i++) + { + if (_places[i] == null) + { + index = i; break; + } + } + if (index < 0) + { + return -1; + } + for (int i = index; i > 0; i--) + { + _places[i] = _places[i - 1]; + } + _places[0] = plane; + return 0; + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + public int Insert(T plane, int position) + { + if (position < 0 || position >= Count) + return -1; + if (_places[position] == null) + { + _places[position] = plane; + return position; + } + int index = -1; + for (int i = position; i < Count; i++) + { + if (_places[i] == null) + { + index = i; break; + } + } + if (index < 0) + return -1; + for (int i = index; index > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = plane; + 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]; + } + } +}