From 0a16b545394b91d19f6bd84026ff7123e5c00222 Mon Sep 17 00:00:00 2001 From: sardq Date: Sun, 12 Nov 2023 10:47:58 +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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HoistingCrane/CraneVisual.Designer.cs | 14 ++ HoistingCrane/HoistingCrane/CraneVisual.cs | 32 ++++- .../HoistingCrane/CranesGenericCollection.cs | 103 +++++++++++++++ HoistingCrane/HoistingCrane/DrawingCrane.cs | 2 + .../FormCraneCollection.Designer.cs | 125 ++++++++++++++++++ .../HoistingCrane/FormCraneCollection.cs | 60 +++++++++ .../HoistingCrane/FormCraneCollection.resx | 60 +++++++++ HoistingCrane/HoistingCrane/Program.cs | 2 +- HoistingCrane/HoistingCrane/SetGeneric.cs | 68 ++++++++++ 9 files changed, 463 insertions(+), 3 deletions(-) create mode 100644 HoistingCrane/HoistingCrane/CranesGenericCollection.cs create mode 100644 HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs create mode 100644 HoistingCrane/HoistingCrane/FormCraneCollection.cs create mode 100644 HoistingCrane/HoistingCrane/FormCraneCollection.resx create mode 100644 HoistingCrane/HoistingCrane/SetGeneric.cs diff --git a/HoistingCrane/HoistingCrane/CraneVisual.Designer.cs b/HoistingCrane/HoistingCrane/CraneVisual.Designer.cs index 554ada7..fa46198 100644 --- a/HoistingCrane/HoistingCrane/CraneVisual.Designer.cs +++ b/HoistingCrane/HoistingCrane/CraneVisual.Designer.cs @@ -38,6 +38,7 @@ this.buttonStep = new System.Windows.Forms.Button(); this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); this.buttonCreateAdditional = new System.Windows.Forms.Button(); + this.buttonChoose = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCrane)).BeginInit(); this.SuspendLayout(); // @@ -144,11 +145,23 @@ this.buttonCreateAdditional.UseVisualStyleBackColor = true; this.buttonCreateAdditional.Click += new System.EventHandler(this.ButtonCreateAdditional_Click); // + // buttonChoose + // + this.buttonChoose.ImeMode = System.Windows.Forms.ImeMode.NoControl; + this.buttonChoose.Location = new System.Drawing.Point(430, 378); + this.buttonChoose.Name = "buttonChoose"; + this.buttonChoose.Size = new System.Drawing.Size(170, 59); + this.buttonChoose.TabIndex = 28; + this.buttonChoose.Text = "Выбрать"; + this.buttonChoose.UseVisualStyleBackColor = true; + this.buttonChoose.Click += new System.EventHandler(this.buttonChoose_Click); + // // CraneVisual // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(878, 444); + this.Controls.Add(this.buttonChoose); this.Controls.Add(this.buttonStep); this.Controls.Add(this.comboBoxStrategy); this.Controls.Add(this.buttonCreateAdditional); @@ -176,5 +189,6 @@ private Button buttonStep; private ComboBox comboBoxStrategy; private Button buttonCreateAdditional; + private Button buttonChoose; } } \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/CraneVisual.cs b/HoistingCrane/HoistingCrane/CraneVisual.cs index 092449c..52105e5 100644 --- a/HoistingCrane/HoistingCrane/CraneVisual.cs +++ b/HoistingCrane/HoistingCrane/CraneVisual.cs @@ -7,6 +7,7 @@ namespace HoistingCrane { private DrawingCrane? _drawingCrane; private AbstractStrategy? _abstractStrategy; + public DrawingCrane? SelectedCrane { get; private set; } public CraneVisual() { InitializeComponent(); @@ -22,13 +23,35 @@ namespace HoistingCrane private void ButtonCreate_Click(object sender, EventArgs e) { Random random = new(); - _drawingCrane = new DrawingCrane(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxCrane.Width, pictureBoxCrane.Height); _drawingCrane.SetPosition(random.Next(10, 100), random.Next(10, 100)); + 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; + } + _drawingCrane = new DrawingCrane(random.Next(100, 300), + random.Next(1000, 3000), + color, pictureBoxCrane.Width, pictureBoxCrane.Height); + _drawingCrane.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } private void ButtonCreateAdditional_Click(object sender, EventArgs e) { Random random = new Random(); - _drawingCrane = new AdditionalDrawingCrane(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)), pictureBoxCrane.Width, pictureBoxCrane.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; + } + Color Additionalcolor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + Additionalcolor = dialog.Color; + } + _drawingCrane = new AdditionalDrawingCrane(random.Next(100, 300), random.Next(1000, 3000), color, Additionalcolor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxCrane.Width, pictureBoxCrane.Height); + _drawingCrane.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } private void ButtonMove_Click(object sender, EventArgs e) @@ -52,6 +75,11 @@ namespace HoistingCrane } Draw(); } + private void buttonChoose_Click(object sender, EventArgs e) + { + SelectedCrane = _drawingCrane; + DialogResult = DialogResult.OK; + } private void buttonStep_Click(object sender, EventArgs e) { if (_drawingCrane == null) diff --git a/HoistingCrane/HoistingCrane/CranesGenericCollection.cs b/HoistingCrane/HoistingCrane/CranesGenericCollection.cs new file mode 100644 index 0000000..162531f --- /dev/null +++ b/HoistingCrane/HoistingCrane/CranesGenericCollection.cs @@ -0,0 +1,103 @@ +using HoistingCrane.DrawningObjects; +using HoistingCrane.MovementStrategy; + +namespace HoistingCrane.Generics +{ + /// Параметризованный класс для набора объектов DrawningCar + internal class CranesGenericCollection + where T : DrawingCrane + where U : IMoveableObject + { + /// Ширина окна прорисовки + private readonly int _pictureWidth; + /// Высота окна прорисовки + private readonly int _pictureHeight; + /// Размер занимаемого объектом места (ширина) + private readonly int _placeSizeWidth = 200; + /// Размер занимаемого объектом места (высота) + private readonly int _placeSizeHeight = 150; + /// Набор объектов + private readonly SetGeneric _collection; + /// Конструктор + public CranesGenericCollection(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 +(CranesGenericCollection collect, T? obj) + { + if (obj == null) + { + return -1; + } + return collect._collection.Insert(obj); + } + /// Перегрузка оператора вычитания + public static bool operator -(CranesGenericCollection collect, int pos) + { + T? obj = collect._collection.Get(pos); + if (obj == null) + { + return false; + } + return collect?._collection.Remove(pos) ?? false; + } + /// Получение объекта 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) + { + T? obj; + int j = 3; + int k = 1; + for (int i = 0; i < _collection.Count; i++) + { + if(_collection.Get(i) == null) + continue; + if (j < 0) + { + j += 4; + k--; + } + if (_collection.Get(i) != null) + { + obj = _collection.Get(i); + obj.SetPosition(_placeSizeWidth * j, _placeSizeHeight * k); + obj.DrawTransport(g); + } + j--; + } + } + } +} + + diff --git a/HoistingCrane/HoistingCrane/DrawingCrane.cs b/HoistingCrane/HoistingCrane/DrawingCrane.cs index 2c95f9b..7fe469e 100644 --- a/HoistingCrane/HoistingCrane/DrawingCrane.cs +++ b/HoistingCrane/HoistingCrane/DrawingCrane.cs @@ -1,4 +1,5 @@ using HoistingCrane.Entities; +using HoistingCrane.MovementStrategy; namespace HoistingCrane.DrawningObjects { @@ -15,6 +16,7 @@ namespace HoistingCrane.DrawningObjects protected readonly int _craneWidth = 200; protected readonly int _craneHeight = 150; + public IMoveableObject GetMoveableObject => new DrawningObjectsCrane(this); public DrawingCrane(int speed, double weight, Color bodyColor, int width, int height) { if ((_craneWidth > width) || (_craneHeight > height)) return; diff --git a/HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs b/HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs new file mode 100644 index 0000000..ceb8413 --- /dev/null +++ b/HoistingCrane/HoistingCrane/FormCraneCollection.Designer.cs @@ -0,0 +1,125 @@ +namespace HoistingCrane +{ + partial class FormCraneCollection + { + /// + /// 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.groupBoxInstruments = new System.Windows.Forms.GroupBox(); + this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); + this.buttonReload = new System.Windows.Forms.Button(); + this.buttonDelete = new System.Windows.Forms.Button(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.groupBoxInstruments.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); + this.SuspendLayout(); + // + // groupBoxInstruments + // + this.groupBoxInstruments.Controls.Add(this.maskedTextBoxNumber); + this.groupBoxInstruments.Controls.Add(this.buttonReload); + this.groupBoxInstruments.Controls.Add(this.buttonDelete); + this.groupBoxInstruments.Controls.Add(this.buttonAdd); + this.groupBoxInstruments.Location = new System.Drawing.Point(811, 0); + this.groupBoxInstruments.Name = "groupBoxInstruments"; + this.groupBoxInstruments.Size = new System.Drawing.Size(185, 451); + this.groupBoxInstruments.TabIndex = 0; + this.groupBoxInstruments.TabStop = false; + this.groupBoxInstruments.Text = "Инструменты"; + // + // maskedTextBoxNumber + // + this.maskedTextBoxNumber.Location = new System.Drawing.Point(11, 71); + this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 31); + this.maskedTextBoxNumber.TabIndex = 9; + // + // buttonReload + // + this.buttonReload.Location = new System.Drawing.Point(6, 172); + this.buttonReload.Name = "buttonReload"; + this.buttonReload.Size = new System.Drawing.Size(167, 35); + this.buttonReload.TabIndex = 8; + this.buttonReload.Text = "Обновить экран"; + this.buttonReload.UseVisualStyleBackColor = true; + this.buttonReload.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); + // + // buttonDelete + // + this.buttonDelete.Location = new System.Drawing.Point(6, 108); + this.buttonDelete.Name = "buttonDelete"; + this.buttonDelete.Size = new System.Drawing.Size(167, 35); + this.buttonDelete.TabIndex = 7; + this.buttonDelete.Text = "Удалить кран"; + this.buttonDelete.UseVisualStyleBackColor = true; + this.buttonDelete.Click += new System.EventHandler(this.ButtonRemoveCrane_Click); + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(6, 30); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(167, 35); + this.buttonAdd.TabIndex = 6; + this.buttonAdd.Text = "Добавить кран"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.ButtonAddCrane_Click); + // + // pictureBoxCollection + // + this.pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0); + this.pictureBoxCollection.Name = "pictureBoxCollection"; + this.pictureBoxCollection.Size = new System.Drawing.Size(996, 444); + this.pictureBoxCollection.TabIndex = 5; + this.pictureBoxCollection.TabStop = false; + // + // FormCraneCollection + // + this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(996, 444); + this.Controls.Add(this.groupBoxInstruments); + this.Controls.Add(this.pictureBoxCollection); + this.Name = "FormCraneCollection"; + this.Text = "FormCraneCollection"; + this.groupBoxInstruments.ResumeLayout(false); + this.groupBoxInstruments.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private GroupBox groupBoxInstruments; + private MaskedTextBox maskedTextBoxNumber; + private Button buttonReload; + private Button buttonDelete; + private Button buttonAdd; + private PictureBox pictureBoxCollection; + } +} \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/FormCraneCollection.cs b/HoistingCrane/HoistingCrane/FormCraneCollection.cs new file mode 100644 index 0000000..5d71394 --- /dev/null +++ b/HoistingCrane/HoistingCrane/FormCraneCollection.cs @@ -0,0 +1,60 @@ +using HoistingCrane.DrawningObjects; +using HoistingCrane.Generics; +using HoistingCrane.MovementStrategy; + +namespace HoistingCrane +{ + public partial class FormCraneCollection : Form + { + private readonly CranesGenericCollection _cranes; + public FormCraneCollection() + { + InitializeComponent(); + _cranes = new CranesGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + private void ButtonAddCrane_Click(object sender, EventArgs e) + { + CraneVisual form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_cranes + form.SelectedCrane != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _cranes.ShowCars(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + + + private void ButtonRemoveCrane_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_cranes - pos) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _cranes.ShowCars(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void ButtonRefreshCollection_Click(object sender, EventArgs +e) + { + pictureBoxCollection.Image = _cranes.ShowCars(); + } + } + +} + diff --git a/HoistingCrane/HoistingCrane/FormCraneCollection.resx b/HoistingCrane/HoistingCrane/FormCraneCollection.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/HoistingCrane/HoistingCrane/FormCraneCollection.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/HoistingCrane/HoistingCrane/Program.cs b/HoistingCrane/HoistingCrane/Program.cs index ceea9e4..141ac24 100644 --- a/HoistingCrane/HoistingCrane/Program.cs +++ b/HoistingCrane/HoistingCrane/Program.cs @@ -11,7 +11,7 @@ namespace HoistingCrane // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new CraneVisual()); + Application.Run(new FormCraneCollection()); } } } \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/SetGeneric.cs b/HoistingCrane/HoistingCrane/SetGeneric.cs new file mode 100644 index 0000000..a08ab89 --- /dev/null +++ b/HoistingCrane/HoistingCrane/SetGeneric.cs @@ -0,0 +1,68 @@ +namespace HoistingCrane.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 crane) + { + int pos = -1; + for (int i = 0; i < Count; i++) + { + if (_places[i] == null) + { + pos = i; + break; + } + } + if (pos != -1) + { + for (int i = pos; i > 0; i--) + { + _places[i] = _places[i - 1]; + } + } + _places[0] = crane; + return 0; + } + public int Insert(T crane, 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] = crane; + 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]; + } + } +} +