From d7c9234ae865ce577d6577c96533a807f4dc5c5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B0=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A4=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Wed, 11 Oct 2023 10:50:39 +0400 Subject: [PATCH 1/3] =?UTF-8?q?=D0=9A=D0=BB=D0=B0=D1=81=D1=81=D1=8B=20gene?= =?UTF-8?q?ric=20+=20=D0=BD=D0=B5=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE=20TODO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Hydroplane/DrawningPlane.cs | 7 + .../FormHydroplaneCollection.Designer.cs | 39 +++++ Hydroplane/FormHydroplaneCollection.cs | 20 +++ Hydroplane/FormHydroplaneCollection.resx | 120 +++++++++++++++ Hydroplane/PlanesGenericCollection.cs | 139 ++++++++++++++++++ Hydroplane/SetGeneric.cs | 106 +++++++++++++ 6 files changed, 431 insertions(+) create mode 100644 Hydroplane/FormHydroplaneCollection.Designer.cs create mode 100644 Hydroplane/FormHydroplaneCollection.cs create mode 100644 Hydroplane/FormHydroplaneCollection.resx create mode 100644 Hydroplane/PlanesGenericCollection.cs create mode 100644 Hydroplane/SetGeneric.cs diff --git a/Hydroplane/DrawningPlane.cs b/Hydroplane/DrawningPlane.cs index d224024..c30bdb6 100644 --- a/Hydroplane/DrawningPlane.cs +++ b/Hydroplane/DrawningPlane.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Hydroplane.Entities; +using Hydroplane.MovementStrategy; namespace Hydroplane.DrawningObjects { @@ -45,6 +46,12 @@ namespace Hydroplane.DrawningObjects } EntityPlane = new EntityPlane(speed, weight, bodyColor); } + + /// + /// Получение объекта IMoveableObject из объекта DrawningCar + /// + public IMoveableObject GetMoveableObject => new DrawningObjectPlane(this); + public void SetPosition(int x, int y) { _startPosX = Math.Min(x, _pictureWidth - _planeWidth); diff --git a/Hydroplane/FormHydroplaneCollection.Designer.cs b/Hydroplane/FormHydroplaneCollection.Designer.cs new file mode 100644 index 0000000..40e7d62 --- /dev/null +++ b/Hydroplane/FormHydroplaneCollection.Designer.cs @@ -0,0 +1,39 @@ +namespace Hydroplane +{ + partial class FormHydroplaneCollection + { + /// + /// 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() + { + this.components = new System.ComponentModel.Container(); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Text = "FormHydroplaneCollection"; + } + + #endregion + } +} \ No newline at end of file diff --git a/Hydroplane/FormHydroplaneCollection.cs b/Hydroplane/FormHydroplaneCollection.cs new file mode 100644 index 0000000..b0acfee --- /dev/null +++ b/Hydroplane/FormHydroplaneCollection.cs @@ -0,0 +1,20 @@ +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 Hydroplane +{ + public partial class FormHydroplaneCollection : Form + { + public FormHydroplaneCollection() + { + InitializeComponent(); + } + } +} diff --git a/Hydroplane/FormHydroplaneCollection.resx b/Hydroplane/FormHydroplaneCollection.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/Hydroplane/FormHydroplaneCollection.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/Hydroplane/PlanesGenericCollection.cs b/Hydroplane/PlanesGenericCollection.cs new file mode 100644 index 0000000..1743715 --- /dev/null +++ b/Hydroplane/PlanesGenericCollection.cs @@ -0,0 +1,139 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hydroplane.DrawningObjects; +using Hydroplane.MovementStrategy; + +namespace Hydroplane.Generics +{ + /// + /// Параметризованный класс для набора объектов DrawningPlane + /// + /// + /// + internal class PlanesGenericCollection + where T : DrawningPlane + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 175; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 80; + /// + /// Набор объектов + /// + 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 bool operator +(PlanesGenericCollection collect, T? + obj) + { + if (obj == null) + { + return false; + } + return collect?._collection.Insert(obj) ?? false; + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static T? operator -(PlanesGenericCollection 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++) + { + // TODO получение объекта + // TODO установка позиции + // TODO прорисовка объекта + } + } + } +} diff --git a/Hydroplane/SetGeneric.cs b/Hydroplane/SetGeneric.cs new file mode 100644 index 0000000..63e312d --- /dev/null +++ b/Hydroplane/SetGeneric.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hydroplane.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 bool Insert(T plane) + { + try + { + for (int i = _places.Length - 1; i > 0; i--) + { + _places[i] = _places[i - 1]; + } + _places[0] = plane; + return true; + } + catch + { + return false; + } + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + public bool Insert(T plane, int position) + { + if (position < 0 || position >= _places.Count() || plane == null) + { + return false; + } + if (_places[position] == null) + { + return false; + + } + int positionNull = Array.FindIndex(_places, position, x => x == null); + if (positionNull == -1) + return false; + for (int i = positionNull; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = plane; + return true; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + if (position < 0 || position >= _places.Count()) + return false; + + _places[position] = null; + + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T? Get(int position) + { + if (position < 0 || position >= _places.Count()) + return null; + return _places[position]; + } + } +} -- 2.25.1 From 75e016e629bd2d81f382805977a4d87c77720f7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B0=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A4=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Wed, 25 Oct 2023 02:19:13 +0400 Subject: [PATCH 2/3] 3 lab DONE --- Hydroplane/DrawningPlane.cs | 3 - Hydroplane/FormHydroplane.Designer.cs | 13 +++ Hydroplane/FormHydroplane.cs | 34 +++++- .../FormHydroplaneCollection.Designer.cs | 104 +++++++++++++++++- Hydroplane/FormHydroplaneCollection.cs | 54 ++++++++- Hydroplane/FormHydroplaneCollection.resx | 50 ++++----- Hydroplane/PlanesGenericCollection.cs | 84 ++++---------- Hydroplane/Program.cs | 2 +- Hydroplane/SetGeneric.cs | 94 +++++++--------- 9 files changed, 278 insertions(+), 160 deletions(-) diff --git a/Hydroplane/DrawningPlane.cs b/Hydroplane/DrawningPlane.cs index c30bdb6..0be8049 100644 --- a/Hydroplane/DrawningPlane.cs +++ b/Hydroplane/DrawningPlane.cs @@ -47,9 +47,6 @@ namespace Hydroplane.DrawningObjects EntityPlane = new EntityPlane(speed, weight, bodyColor); } - /// - /// Получение объекта IMoveableObject из объекта DrawningCar - /// public IMoveableObject GetMoveableObject => new DrawningObjectPlane(this); public void SetPosition(int x, int y) diff --git a/Hydroplane/FormHydroplane.Designer.cs b/Hydroplane/FormHydroplane.Designer.cs index 4f06ff9..6af98a0 100644 --- a/Hydroplane/FormHydroplane.Designer.cs +++ b/Hydroplane/FormHydroplane.Designer.cs @@ -37,6 +37,7 @@ buttonCreateHydroplane = new Button(); comboBoxStrategy = new ComboBox(); buttonStep = new Button(); + buttonChoose = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxHydroplane).BeginInit(); SuspendLayout(); // @@ -139,11 +140,22 @@ buttonStep.UseVisualStyleBackColor = true; buttonStep.Click += buttonStep_Click; // + // buttonChoose + // + buttonChoose.Location = new Point(309, 398); + buttonChoose.Name = "buttonChoose"; + buttonChoose.Size = new Size(125, 35); + buttonChoose.TabIndex = 9; + buttonChoose.Text = "Choose"; + buttonChoose.UseVisualStyleBackColor = true; + buttonChoose.Click += ButtonSelectPLane_Click; + // // FormHydroplane // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(884, 461); + Controls.Add(buttonChoose); Controls.Add(buttonStep); Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateHydroplane); @@ -172,5 +184,6 @@ private Button buttonCreateHydroplane; private ComboBox comboBoxStrategy; private Button buttonStep; + private Button buttonChoose; } } \ No newline at end of file diff --git a/Hydroplane/FormHydroplane.cs b/Hydroplane/FormHydroplane.cs index 35154bf..641e297 100644 --- a/Hydroplane/FormHydroplane.cs +++ b/Hydroplane/FormHydroplane.cs @@ -17,9 +17,13 @@ namespace Hydroplane { private DrawningPlane? _drawingPlane; private AbstractStrategy? _abstractStrategy; + public DrawningPlane? SelectedPlane { get; private set; } + public FormHydroplane() { InitializeComponent(); + _abstractStrategy = null; + SelectedPlane = null; } private void Draw() { @@ -36,7 +40,13 @@ namespace Hydroplane private void buttonCreatePlane_Click(object sender, EventArgs e) { Random random = new(); - _drawingPlane = new DrawningPlane(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxHydroplane.Width, pictureBoxHydroplane.Height); + 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; + } + _drawingPlane = new DrawningPlane(random.Next(100, 300), random.Next(1000, 3000), color, pictureBoxHydroplane.Width, pictureBoxHydroplane.Height); _drawingPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -44,7 +54,18 @@ namespace Hydroplane private void buttonCreateHydroplane_Click(object sender, EventArgs e) { Random random = new(); - _drawingPlane = new DrawningHydroplane(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)), + Color mainColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + mainColor = dialog.Color; + } + Color addColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + if (dialog.ShowDialog() == DialogResult.OK) + { + addColor = dialog.Color; + } + _drawingPlane = new DrawningHydroplane(random.Next(100, 300), random.Next(1000, 3000), mainColor, addColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxHydroplane.Width, pictureBoxHydroplane.Height); _drawingPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); @@ -94,8 +115,7 @@ namespace Hydroplane { return; } - _abstractStrategy.SetData(new DrawningObjectPlane(_drawingPlane), pictureBoxHydroplane.Width, - pictureBoxHydroplane.Height); + _abstractStrategy.SetData(_drawingPlane.GetMoveableObject, pictureBoxHydroplane.Width, pictureBoxHydroplane.Height); comboBoxStrategy.Enabled = false; } if (_abstractStrategy == null) @@ -111,5 +131,11 @@ namespace Hydroplane } } + private void ButtonSelectPLane_Click(object sender, EventArgs e) + { + SelectedPlane = _drawingPlane; + DialogResult = DialogResult.OK; + } + } } \ No newline at end of file diff --git a/Hydroplane/FormHydroplaneCollection.Designer.cs b/Hydroplane/FormHydroplaneCollection.Designer.cs index 40e7d62..bf0a451 100644 --- a/Hydroplane/FormHydroplaneCollection.Designer.cs +++ b/Hydroplane/FormHydroplaneCollection.Designer.cs @@ -28,12 +28,108 @@ /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "FormHydroplaneCollection"; + panel1 = new Panel(); + UpdateButton = new Button(); + DeleteButton = new Button(); + AddButton = new Button(); + InputNum = new TextBox(); + label1 = new Label(); + DrawPlane = new PictureBox(); + panel1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)DrawPlane).BeginInit(); + SuspendLayout(); + // + // panel1 + // + panel1.Controls.Add(UpdateButton); + panel1.Controls.Add(DeleteButton); + panel1.Controls.Add(AddButton); + panel1.Controls.Add(InputNum); + panel1.Controls.Add(label1); + panel1.Dock = DockStyle.Right; + panel1.Location = new Point(550, 0); + panel1.Name = "panel1"; + panel1.Size = new Size(250, 450); + panel1.TabIndex = 0; + // + // UpdateButton + // + UpdateButton.Location = new Point(10, 241); + UpdateButton.Name = "UpdateButton"; + UpdateButton.Size = new Size(228, 37); + UpdateButton.TabIndex = 4; + UpdateButton.Text = "Обновить коллекцию"; + UpdateButton.UseVisualStyleBackColor = true; + UpdateButton.Click += ButtonRefreshCollection_Click; + // + // DeleteButton + // + DeleteButton.Location = new Point(10, 176); + DeleteButton.Name = "DeleteButton"; + DeleteButton.Size = new Size(228, 37); + DeleteButton.TabIndex = 3; + DeleteButton.Text = "Удалить самолёт"; + DeleteButton.UseVisualStyleBackColor = true; + DeleteButton.Click += ButtonRemoveCar_Click; + // + // AddButton + // + AddButton.Location = new Point(10, 53); + AddButton.Name = "AddButton"; + AddButton.Size = new Size(228, 37); + AddButton.TabIndex = 2; + AddButton.Text = "Добавить самолёт"; + AddButton.UseVisualStyleBackColor = true; + AddButton.Click += ButtonAddTank_Click; + // + // InputNum + // + InputNum.Location = new Point(10, 133); + InputNum.Name = "InputNum"; + InputNum.Size = new Size(228, 27); + InputNum.TabIndex = 1; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(2, 2); + label1.Name = "label1"; + label1.Size = new Size(103, 20); + label1.TabIndex = 0; + label1.Text = "Инструменты"; + // + // DrawPlane + // + DrawPlane.Dock = DockStyle.Fill; + DrawPlane.Location = new Point(0, 0); + DrawPlane.Name = "DrawPlane"; + DrawPlane.Size = new Size(550, 450); + DrawPlane.TabIndex = 1; + DrawPlane.TabStop = false; + // + // CollectionsFrame + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(DrawPlane); + Controls.Add(panel1); + Name = "CollectionsFrame"; + Text = "Form1"; + panel1.ResumeLayout(false); + panel1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)DrawPlane).EndInit(); + ResumeLayout(false); } #endregion + + private Panel panel1; + private Button UpdateButton; + private Button DeleteButton; + private Button AddButton; + private TextBox InputNum; + private Label label1; + private PictureBox DrawPlane; } } \ No newline at end of file diff --git a/Hydroplane/FormHydroplaneCollection.cs b/Hydroplane/FormHydroplaneCollection.cs index b0acfee..5f94f71 100644 --- a/Hydroplane/FormHydroplaneCollection.cs +++ b/Hydroplane/FormHydroplaneCollection.cs @@ -1,4 +1,7 @@ -using System; +using Hydroplane.DrawningObjects; +using Hydroplane.Generics; +using Hydroplane.MovementStrategy; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -12,9 +15,58 @@ namespace Hydroplane { public partial class FormHydroplaneCollection : Form { + private readonly PlanesGenericCollection _planes; + public FormHydroplaneCollection() { InitializeComponent(); + _planes = new PlanesGenericCollection(DrawPlane.Width, DrawPlane.Height); + } + + private void ButtonAddTank_Click(object sender, EventArgs e) + { + FormHydroplane form = new FormHydroplane(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_planes + form.SelectedPlane != -1) + { + MessageBox.Show("Объект добавлен"); + DrawPlane.Image = _planes.ShowPlanes(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + + private void ButtonRemoveCar_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = -1; + try + { + pos = Convert.ToInt32(InputNum.Text); + } + catch (Exception ex) { } + if (_planes - pos) + { + MessageBox.Show("Объект удален"); + DrawPlane.Image = _planes.ShowPlanes(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + DrawPlane.Image = _planes.ShowPlanes(); } } } diff --git a/Hydroplane/FormHydroplaneCollection.resx b/Hydroplane/FormHydroplaneCollection.resx index 1af7de1..af32865 100644 --- a/Hydroplane/FormHydroplaneCollection.resx +++ b/Hydroplane/FormHydroplaneCollection.resx @@ -1,17 +1,17 @@  - diff --git a/Hydroplane/PlanesGenericCollection.cs b/Hydroplane/PlanesGenericCollection.cs index 1743715..1c51739 100644 --- a/Hydroplane/PlanesGenericCollection.cs +++ b/Hydroplane/PlanesGenericCollection.cs @@ -8,40 +8,16 @@ using Hydroplane.MovementStrategy; namespace Hydroplane.Generics { - /// - /// Параметризованный класс для набора объектов DrawningPlane - /// - /// - /// internal class PlanesGenericCollection where T : DrawningPlane where U : IMoveableObject { - /// - /// Ширина окна прорисовки - /// private readonly int _pictureWidth; - /// - /// Высота окна прорисовки - /// private readonly int _pictureHeight; - /// - /// Размер занимаемого объектом места (ширина) - /// private readonly int _placeSizeWidth = 175; - /// - /// Размер занимаемого объектом места (высота) - /// private readonly int _placeSizeHeight = 80; - /// - /// Набор объектов - /// private readonly SetGeneric _collection; - /// - /// Конструктор - /// - /// - /// + public PlanesGenericCollection(int picWidth, int picHeight) { int width = picWidth / _placeSizeWidth; @@ -50,51 +26,33 @@ namespace Hydroplane.Generics _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } - /// - /// Перегрузка оператора сложения - /// - /// - /// - /// - public static bool operator +(PlanesGenericCollection collect, T? + + public static int? operator +(PlanesGenericCollection collect, T? obj) { if (obj == null) { - return false; + return -1; } - return collect?._collection.Insert(obj) ?? false; + return collect?._collection.Insert(obj); } - /// - /// Перегрузка оператора вычитания - /// - /// - /// - /// - public static T? operator -(PlanesGenericCollection collect, int + + public static bool operator -(PlanesGenericCollection collect, int pos) { T? obj = collect._collection.Get(pos); - if (obj != null) + if (obj == null) { - collect._collection.Remove(pos); + return false; } - return obj; + return collect._collection.Remove(pos); } - /// - /// Получение объекта IMoveableObject - /// - /// - /// + public U? GetU(int pos) { return (U?)_collection.Get(pos)?.GetMoveableObject; } - /// - /// Вывод всего набора объектов - /// - /// - public Bitmap ShowCars() + public Bitmap ShowPlanes() { Bitmap bmp = new(_pictureWidth, _pictureHeight); Graphics gr = Graphics.FromImage(bmp); @@ -102,10 +60,6 @@ namespace Hydroplane.Generics DrawObjects(gr); return bmp; } - /// - /// Метод отрисовки фона - /// - /// private void DrawBackground(Graphics g) { Pen pen = new(Color.Black, 3); @@ -122,17 +76,17 @@ namespace Hydroplane.Generics _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); } } - /// - /// Метод прорисовки объектов - /// - /// private void DrawObjects(Graphics g) { for (int i = 0; i < _collection.Count; i++) { - // TODO получение объекта - // TODO установка позиции - // TODO прорисовка объекта + T t = _collection.Get(i); + if (t != null) + { + t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); + if (t is DrawningPlane) (t as DrawningPlane).DrawTransport(g); + else t.DrawTransport(g); + } } } } diff --git a/Hydroplane/Program.cs b/Hydroplane/Program.cs index 35abc6f..149f3d5 100644 --- a/Hydroplane/Program.cs +++ b/Hydroplane/Program.cs @@ -11,7 +11,7 @@ namespace Hydroplane // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormHydroplane()); + Application.Run(new FormHydroplaneCollection()); } } } \ No newline at end of file diff --git a/Hydroplane/SetGeneric.cs b/Hydroplane/SetGeneric.cs index 63e312d..2dab4dd 100644 --- a/Hydroplane/SetGeneric.cs +++ b/Hydroplane/SetGeneric.cs @@ -6,82 +6,66 @@ using System.Threading.Tasks; namespace Hydroplane.Generics { - /// - /// Параметризованный набор объектов - /// - /// - internal class SetGeneric - where T : class + internal class SetGeneric where T : class { - /// - /// Массив объектов, которые храним - /// private readonly T?[] _places; - /// - /// Количество объектов в массиве - /// + public int Count => _places.Length; - /// - /// Конструктор - /// - /// + public SetGeneric(int count) { _places = new T?[count]; } - /// - /// Добавление объекта в набор - /// - /// Добавляемый самолёт - /// - public bool Insert(T plane) + + public int Insert(T plane) { - try + int index = -1; + for (int i = 0; i < _places.Length; i++) { - for (int i = _places.Length - 1; i > 0; i--) + if (_places[i] == null) { - _places[i] = _places[i - 1]; + index = i; break; } - _places[0] = plane; - return true; } - catch + if (index < 0) { - return false; + return -1; } + for (int i = index; i > 0; i--) + { + _places[i] = _places[i - 1]; + } + _places[0] = plane; + return 0; } - /// - /// Добавление объекта в набор на конкретную позицию - /// - /// Добавляемый автомобиль - /// Позиция - /// - public bool Insert(T plane, int position) + + public int Insert(T plane, int position) { - if (position < 0 || position >= _places.Count() || plane == null) - { - return false; - } + if (position < 0 || position >= Count) + return -1; if (_places[position] == null) { - return false; - + _places[position] = plane; + return position; } - int positionNull = Array.FindIndex(_places, position, x => x == null); - if (positionNull == -1) - return false; - for (int i = positionNull; i > position; i--) + 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 true; + return position; } - /// - /// Удаление объекта из набора с конкретной позиции - /// - /// - /// + public bool Remove(int position) { if (position < 0 || position >= _places.Count()) @@ -91,11 +75,7 @@ namespace Hydroplane.Generics return true; } - /// - /// Получение объекта из набора по позиции - /// - /// - /// + public T? Get(int position) { if (position < 0 || position >= _places.Count()) -- 2.25.1 From a87e9d0fe54aa7cf6f203407bd9ad5a1e72f7c99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B0=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A4=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Wed, 25 Oct 2023 09:11:32 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D0=BE=D0=BF=D1=8F=D1=82=D1=8C=20done...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormHydroplaneCollection.Designer.cs | 39 ++++++++++------- Hydroplane/PlanesGenericCollection.cs | 4 +- Hydroplane/SetGeneric.cs | 43 +++++-------------- 3 files changed, 36 insertions(+), 50 deletions(-) diff --git a/Hydroplane/FormHydroplaneCollection.Designer.cs b/Hydroplane/FormHydroplaneCollection.Designer.cs index bf0a451..75355af 100644 --- a/Hydroplane/FormHydroplaneCollection.Designer.cs +++ b/Hydroplane/FormHydroplaneCollection.Designer.cs @@ -47,16 +47,18 @@ panel1.Controls.Add(InputNum); panel1.Controls.Add(label1); panel1.Dock = DockStyle.Right; - panel1.Location = new Point(550, 0); + panel1.Location = new Point(577, 0); + panel1.Margin = new Padding(3, 2, 3, 2); panel1.Name = "panel1"; - panel1.Size = new Size(250, 450); + panel1.Size = new Size(219, 338); panel1.TabIndex = 0; // // UpdateButton // - UpdateButton.Location = new Point(10, 241); + UpdateButton.Location = new Point(9, 181); + UpdateButton.Margin = new Padding(3, 2, 3, 2); UpdateButton.Name = "UpdateButton"; - UpdateButton.Size = new Size(228, 37); + UpdateButton.Size = new Size(200, 28); UpdateButton.TabIndex = 4; UpdateButton.Text = "Обновить коллекцию"; UpdateButton.UseVisualStyleBackColor = true; @@ -64,9 +66,10 @@ // // DeleteButton // - DeleteButton.Location = new Point(10, 176); + DeleteButton.Location = new Point(9, 132); + DeleteButton.Margin = new Padding(3, 2, 3, 2); DeleteButton.Name = "DeleteButton"; - DeleteButton.Size = new Size(228, 37); + DeleteButton.Size = new Size(200, 28); DeleteButton.TabIndex = 3; DeleteButton.Text = "Удалить самолёт"; DeleteButton.UseVisualStyleBackColor = true; @@ -74,9 +77,10 @@ // // AddButton // - AddButton.Location = new Point(10, 53); + AddButton.Location = new Point(9, 40); + AddButton.Margin = new Padding(3, 2, 3, 2); AddButton.Name = "AddButton"; - AddButton.Size = new Size(228, 37); + AddButton.Size = new Size(200, 28); AddButton.TabIndex = 2; AddButton.Text = "Добавить самолёт"; AddButton.UseVisualStyleBackColor = true; @@ -84,9 +88,10 @@ // // InputNum // - InputNum.Location = new Point(10, 133); + InputNum.Location = new Point(9, 100); + InputNum.Margin = new Padding(3, 2, 3, 2); InputNum.Name = "InputNum"; - InputNum.Size = new Size(228, 27); + InputNum.Size = new Size(200, 23); InputNum.TabIndex = 1; // // label1 @@ -94,7 +99,7 @@ label1.AutoSize = true; label1.Location = new Point(2, 2); label1.Name = "label1"; - label1.Size = new Size(103, 20); + label1.Size = new Size(83, 15); label1.TabIndex = 0; label1.Text = "Инструменты"; // @@ -102,19 +107,21 @@ // DrawPlane.Dock = DockStyle.Fill; DrawPlane.Location = new Point(0, 0); + DrawPlane.Margin = new Padding(3, 2, 3, 2); DrawPlane.Name = "DrawPlane"; - DrawPlane.Size = new Size(550, 450); + DrawPlane.Size = new Size(577, 338); DrawPlane.TabIndex = 1; DrawPlane.TabStop = false; // - // CollectionsFrame + // FormHydroplaneCollection // - AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(796, 338); Controls.Add(DrawPlane); Controls.Add(panel1); - Name = "CollectionsFrame"; + Margin = new Padding(3, 2, 3, 2); + Name = "FormHydroplaneCollection"; Text = "Form1"; panel1.ResumeLayout(false); panel1.PerformLayout(); diff --git a/Hydroplane/PlanesGenericCollection.cs b/Hydroplane/PlanesGenericCollection.cs index 1c51739..7077420 100644 --- a/Hydroplane/PlanesGenericCollection.cs +++ b/Hydroplane/PlanesGenericCollection.cs @@ -15,7 +15,7 @@ namespace Hydroplane.Generics private readonly int _pictureWidth; private readonly int _pictureHeight; private readonly int _placeSizeWidth = 175; - private readonly int _placeSizeHeight = 80; + private readonly int _placeSizeHeight = 85; private readonly SetGeneric _collection; public PlanesGenericCollection(int picWidth, int picHeight) @@ -80,7 +80,7 @@ namespace Hydroplane.Generics { for (int i = 0; i < _collection.Count; i++) { - T t = _collection.Get(i); + T? t = _collection.Get(i); if (t != null) { t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); diff --git a/Hydroplane/SetGeneric.cs b/Hydroplane/SetGeneric.cs index 2dab4dd..d5e3b98 100644 --- a/Hydroplane/SetGeneric.cs +++ b/Hydroplane/SetGeneric.cs @@ -19,48 +19,27 @@ namespace Hydroplane.Generics 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) - { + if (_places[Count - 1] != null) return -1; - } - for (int i = index; i > 0; i--) - { - _places[i] = _places[i - 1]; - } - _places[0] = plane; - return 0; + return Insert(plane, 0); } public int Insert(T plane, int position) { if (position < 0 || position >= Count) return -1; - if (_places[position] == null) + if (_places[position] != null) { - _places[position] = plane; - return position; - } - int index = -1; - for (int i = position; i < Count; i++) - { - if (_places[i] == null) + int indexEnd = position + 1; + while (_places[indexEnd] != null) { - index = i; break; + indexEnd++; } - } - if (index < 0) - return -1; - for (int i = index; index > position; i--) - { - _places[i] = _places[i - 1]; + for (int i = indexEnd + 1; i > position; i--) + { + _places[i] = _places[i - 1]; + } + } _places[position] = plane; return position; -- 2.25.1