diff --git a/DumpTruck/DrawingObjects/DrawingTruck.cs b/DumpTruck/DrawingObjects/DrawingTruck.cs index d8e7566..f419bce 100644 --- a/DumpTruck/DrawingObjects/DrawingTruck.cs +++ b/DumpTruck/DrawingObjects/DrawingTruck.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using DumpTruck.Entities; +using DumpTruck.MovementStrategy; namespace DumpTruck.DrawingObjects { @@ -54,6 +55,10 @@ namespace DumpTruck.DrawingObjects /// Высота объекта /// public int GetHeight => _truckHeight; + /// + /// Получение объекта IMoveableObject из объекта DrawingTruck + /// + public IMoveableObject GetMoveableObject => new DrawningObjectTruck(this); /// /// Конструктор diff --git a/DumpTruck/FormDumpTruck.Designer.cs b/DumpTruck/FormDumpTruck.Designer.cs index f4ff2da..07f26c3 100644 --- a/DumpTruck/FormDumpTruck.Designer.cs +++ b/DumpTruck/FormDumpTruck.Designer.cs @@ -37,6 +37,7 @@ this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); this.ButtonCreateTruck = new System.Windows.Forms.Button(); this.ButtonStep = new System.Windows.Forms.Button(); + this.buttonSelectTruck = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDumpTruck)).BeginInit(); this.SuspendLayout(); // @@ -143,11 +144,22 @@ this.ButtonStep.UseVisualStyleBackColor = true; this.ButtonStep.Click += new System.EventHandler(this.ButtonStep_Click); // + // buttonSelectTruck + // + this.buttonSelectTruck.Location = new System.Drawing.Point(284, 408); + this.buttonSelectTruck.Name = "buttonSelectTruck"; + this.buttonSelectTruck.Size = new System.Drawing.Size(75, 30); + this.buttonSelectTruck.TabIndex = 9; + this.buttonSelectTruck.Text = "Выбрать"; + this.buttonSelectTruck.UseVisualStyleBackColor = true; + this.buttonSelectTruck.Click += new System.EventHandler(this.buttonSelectTruck_Click); + // // FormDumpTruck // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.buttonSelectTruck); this.Controls.Add(this.ButtonStep); this.Controls.Add(this.ButtonCreateTruck); this.Controls.Add(this.comboBoxStrategy); @@ -175,5 +187,6 @@ private ComboBox comboBoxStrategy; private Button ButtonCreateTruck; private Button ButtonStep; + private Button buttonSelectTruck; } } \ No newline at end of file diff --git a/DumpTruck/FormDumpTruck.cs b/DumpTruck/FormDumpTruck.cs index b74fd94..b25edf6 100644 --- a/DumpTruck/FormDumpTruck.cs +++ b/DumpTruck/FormDumpTruck.cs @@ -5,14 +5,18 @@ namespace DumpTruck { public partial class FormDumpTruck : Form { - public FormDumpTruck() - { - InitializeComponent(); - } - private DrawingTruck? _drawingTruck; - private AbstractStrategy? _abstractStrategy; + private AbstractStrategy? _strategy; + + public DrawingTruck? SelectedTruck { get; private set; } + + public FormDumpTruck() + { + InitializeComponent(); + _strategy = null; + SelectedTruck = null; + } private void Draw() { @@ -55,11 +59,32 @@ namespace DumpTruck private void ButtonCreateDumpTruck_Click(object sender, EventArgs e) { Random random = new(); + Color bodyColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + bodyColor = dialog.Color; + } + + Color dumpBoxColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog secondDialog = new(); + if (secondDialog.ShowDialog() == DialogResult.OK) + { + dumpBoxColor = secondDialog.Color; + } + + Color tentColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog thirdDialog = new(); + if (thirdDialog.ShowDialog() == DialogResult.OK) + { + tentColor = thirdDialog.Color; + } + _drawingTruck = new DrawingDumpTruck(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + bodyColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), - 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)), + tentColor, + dumpBoxColor, pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); _drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -68,7 +93,13 @@ namespace DumpTruck private void ButtonCreateTruck_Click(object sender, EventArgs e) { Random random = new(); - _drawingTruck = new DrawingTruck(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + Color bodyColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + bodyColor = dialog.Color; + } + _drawingTruck = new DrawingTruck(random.Next(100, 300), random.Next(1000, 3000), bodyColor, pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); _drawingTruck.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -82,33 +113,39 @@ namespace DumpTruck } if (comboBoxStrategy.Enabled) { - _abstractStrategy = comboBoxStrategy.SelectedIndex + _strategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), 1 => new MoveToBorder(), _ => null, }; - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.SetData(new + _strategy.SetData(new DrawningObjectTruck(_drawingTruck), pictureBoxDumpTruck.Width, pictureBoxDumpTruck.Height); comboBoxStrategy.Enabled = false; } - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.MakeStep(); + _strategy.MakeStep(); Draw(); - if (_abstractStrategy.GetStatus() == Status.Finish) + if (_strategy.GetStatus() == Status.Finish) { comboBoxStrategy.Enabled = true; - _abstractStrategy = null; + _strategy = null; } } + + private void buttonSelectTruck_Click(object sender, EventArgs e) + { + SelectedTruck = _drawingTruck; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/DumpTruck/FormTruckCollection.Designer.cs b/DumpTruck/FormTruckCollection.Designer.cs new file mode 100644 index 0000000..c623e3d --- /dev/null +++ b/DumpTruck/FormTruckCollection.Designer.cs @@ -0,0 +1,137 @@ +namespace DumpTruck +{ + partial class FormTruckCollection + { + /// + /// 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.panelTools = new System.Windows.Forms.Panel(); + this.buttonRefreshCollection = new System.Windows.Forms.Button(); + this.labelTools = new System.Windows.Forms.Label(); + this.buttonRemoveTruck = new System.Windows.Forms.Button(); + this.buttonAddTruck = new System.Windows.Forms.Button(); + this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); + this.panelTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); + this.SuspendLayout(); + // + // panelTools + // + this.panelTools.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.panelTools.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.panelTools.Controls.Add(this.maskedTextBoxNumber); + this.panelTools.Controls.Add(this.buttonRefreshCollection); + this.panelTools.Controls.Add(this.labelTools); + this.panelTools.Controls.Add(this.buttonRemoveTruck); + this.panelTools.Controls.Add(this.buttonAddTruck); + this.panelTools.Location = new System.Drawing.Point(596, 3); + this.panelTools.Name = "panelTools"; + this.panelTools.Size = new System.Drawing.Size(203, 445); + this.panelTools.TabIndex = 0; + // + // buttonRefreshCollection + // + this.buttonRefreshCollection.Location = new System.Drawing.Point(25, 168); + this.buttonRefreshCollection.Name = "buttonRefreshCollection"; + this.buttonRefreshCollection.Size = new System.Drawing.Size(150, 30); + this.buttonRefreshCollection.TabIndex = 3; + this.buttonRefreshCollection.Text = "Обновить коллекцию"; + this.buttonRefreshCollection.UseVisualStyleBackColor = true; + this.buttonRefreshCollection.Click += new System.EventHandler(this.buttonRefreshCollection_Click); + // + // labelTools + // + this.labelTools.Anchor = System.Windows.Forms.AnchorStyles.Top; + this.labelTools.AutoSize = true; + this.labelTools.Location = new System.Drawing.Point(62, 0); + this.labelTools.Name = "labelTools"; + this.labelTools.Size = new System.Drawing.Size(83, 15); + this.labelTools.TabIndex = 0; + this.labelTools.Text = "Инструменты"; + // + // buttonRemoveTruck + // + this.buttonRemoveTruck.Location = new System.Drawing.Point(25, 109); + this.buttonRemoveTruck.Name = "buttonRemoveTruck"; + this.buttonRemoveTruck.Size = new System.Drawing.Size(150, 30); + this.buttonRemoveTruck.TabIndex = 2; + this.buttonRemoveTruck.Text = "Удалить грузовик"; + this.buttonRemoveTruck.UseVisualStyleBackColor = true; + this.buttonRemoveTruck.Click += new System.EventHandler(this.buttonRemoveTruck_Click); + // + // buttonAddTruck + // + this.buttonAddTruck.Location = new System.Drawing.Point(25, 30); + this.buttonAddTruck.Name = "buttonAddTruck"; + this.buttonAddTruck.Size = new System.Drawing.Size(150, 30); + this.buttonAddTruck.TabIndex = 1; + this.buttonAddTruck.Text = "Добавить грузовик"; + this.buttonAddTruck.UseVisualStyleBackColor = true; + this.buttonAddTruck.Click += new System.EventHandler(this.buttonAddTruck_Click); + // + // pictureBoxCollection + // + this.pictureBoxCollection.Location = new System.Drawing.Point(2, 3); + this.pictureBoxCollection.Name = "pictureBoxCollection"; + this.pictureBoxCollection.Size = new System.Drawing.Size(588, 445); + this.pictureBoxCollection.TabIndex = 1; + this.pictureBoxCollection.TabStop = false; + // + // maskedTextBoxNumber + // + this.maskedTextBoxNumber.Location = new System.Drawing.Point(25, 75); + this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + this.maskedTextBoxNumber.Size = new System.Drawing.Size(150, 23); + this.maskedTextBoxNumber.TabIndex = 4; + // + // FormTruckCollection + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.pictureBoxCollection); + this.Controls.Add(this.panelTools); + this.Name = "FormTruckCollection"; + this.Text = "FormTruckCollection"; + this.panelTools.ResumeLayout(false); + this.panelTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private Panel panelTools; + private Button buttonAddTruck; + private Label labelTools; + private Button buttonRefreshCollection; + private Button buttonRemoveTruck; + private PictureBox pictureBoxCollection; + private MaskedTextBox maskedTextBoxNumber; + } +} \ No newline at end of file diff --git a/DumpTruck/FormTruckCollection.cs b/DumpTruck/FormTruckCollection.cs new file mode 100644 index 0000000..65fb7c3 --- /dev/null +++ b/DumpTruck/FormTruckCollection.cs @@ -0,0 +1,77 @@ +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; +using DumpTruck.Generics; +using DumpTruck.DrawingObjects; +using DumpTruck.MovementStrategy; + +namespace DumpTruck +{ + /// + /// Форма для работы с набором объектов класса DrawingTruck + /// + public partial class FormTruckCollection : Form + { + /// + /// Набор объектов + /// + private readonly TrucksGenericCollection _trucks; + /// + /// Конструктор + /// + public FormTruckCollection() + { + InitializeComponent(); + _trucks = new TrucksGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + private void buttonAddTruck_Click(object sender, EventArgs e) + { + FormDumpTruck form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_trucks + form.SelectedTruck != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _trucks.ShowTrucks(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + + private void buttonRemoveTruck_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_trucks - pos) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _trucks.ShowTrucks(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void buttonRefreshCollection_Click(object sender, EventArgs e) + { + + pictureBoxCollection.Image = _trucks.ShowTrucks(); + } + } + +} diff --git a/DumpTruck/FormTruckCollection.resx b/DumpTruck/FormTruckCollection.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/DumpTruck/FormTruckCollection.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/DumpTruck/Generics/SetGeneric.cs b/DumpTruck/Generics/SetGeneric.cs new file mode 100644 index 0000000..e2f9c43 --- /dev/null +++ b/DumpTruck/Generics/SetGeneric.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.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 truck) + { + return Insert(truck, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый грузовик + /// Позиция + /// + public int Insert(T truck, int position) + { + if (position < 0 || position >= Count) return -1; + + 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; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = truck; + 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/DumpTruck/Generics/TrucksGenericCollection.cs b/DumpTruck/Generics/TrucksGenericCollection.cs new file mode 100644 index 0000000..ceac85a --- /dev/null +++ b/DumpTruck/Generics/TrucksGenericCollection.cs @@ -0,0 +1,142 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using DumpTruck.DrawingObjects; +using DumpTruck.MovementStrategy; + +namespace DumpTruck.Generics +{ + /// + /// Параметризованный класс для набора объектов DrawingTruck + /// + /// + /// + internal class TrucksGenericCollection + where T : DrawingTruck + where U : IMoveableObject + { + /// + /// Ширина окна прорисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна прорисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 180; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 100; + /// + /// Набор объектов + /// + private readonly SetGeneric _collection; + /// + /// Конструктор + /// + /// + /// + public TrucksGenericCollection(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 +(TrucksGenericCollection collect, T? obj) + { + if (obj == null) + { + return -1; + } + return collect._collection.Insert(obj); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(TrucksGenericCollection collect, int pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + return collect._collection.Remove(pos); + } + return false; + } + /// + /// Получение объекта IMoveableObject + /// + /// + /// + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowTrucks() + { + 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++) + { + T? obj = _collection.Get(i); + + if (obj != null) + { + obj.SetPosition(i % (_pictureWidth / _placeSizeWidth) * _placeSizeWidth, i / (_pictureWidth / _placeSizeWidth) * _placeSizeHeight); + obj.DrawTransport(g); + } + } + } + } +} diff --git a/DumpTruck/Program.cs b/DumpTruck/Program.cs index e0b9e16..7cab292 100644 --- a/DumpTruck/Program.cs +++ b/DumpTruck/Program.cs @@ -11,7 +11,7 @@ namespace DumpTruck // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormDumpTruck()); + Application.Run(new FormTruckCollection()); } } } \ No newline at end of file