diff --git a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/AbstractCompany.cs b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/AbstractCompany.cs new file mode 100644 index 0000000..574522b --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/AbstractCompany.cs @@ -0,0 +1,62 @@ +using ProjectSeaplane.Drawings; + +namespace ProjectSeaplane.CollectionGenericObjects; + +public abstract class AbstractCompany +{ + protected readonly int _placeSizeWidth = 210; + + protected readonly int _placeSizeHeight = 100; + + protected readonly int _pictureWidth; + + protected readonly int _pictureHeight; + + protected ICollectionGenericObjects? _collection = null; + + private int GetMaxCount => _pictureWidth * _pictureHeight / (_placeSizeWidth * _placeSizeHeight); + + public AbstractCompany(int picWidth, int picHeight, ICollectionGenericObjects collection) + { + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = collection; + _collection.SetMaxCount = GetMaxCount; + } + + public static int? operator +(AbstractCompany company, DrawingPlane plane) + { + return company._collection?.Insert(plane); + } + + public static DrawingPlane operator -(AbstractCompany company, int position) + { + return company._collection?.Remove(position); + } + + public DrawingPlane? GetRandomObject() + { + Random rnd = new(); + return _collection?.Get(rnd.Next(GetMaxCount)); + } + + public Bitmap? Show() + { + Bitmap bitmap = new(_pictureWidth, _pictureHeight); + Graphics graphics = Graphics.FromImage(bitmap); + DrawBackgound(graphics); + + SetObjectsPosition(); + for (int i = 0; i < (_collection?.Count ?? 0); ++i) + { + DrawingPlane? obj = _collection?.Get(i); + obj?.DrawTransport(graphics); + } + + return bitmap; + } + + protected abstract void DrawBackgound(Graphics g); + + protected abstract void SetObjectsPosition(); +} \ No newline at end of file diff --git a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ICollectionGenericObjects.cs new file mode 100644 index 0000000..a1b455c --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -0,0 +1,17 @@ +namespace ProjectSeaplane.CollectionGenericObjects; + +public interface ICollectionGenericObjects + where T : class +{ + int Count { get; } + + int SetMaxCount { set; } + + int Insert(T obj); + + int Insert(T obj, int position); + + T Remove(int position); + + T? Get(int position); +} diff --git a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/MassiveGenericObjects.cs new file mode 100644 index 0000000..0725008 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/MassiveGenericObjects.cs @@ -0,0 +1,92 @@ +namespace ProjectSeaplane.CollectionGenericObjects; + +public class MassiveGenericObjects : ICollectionGenericObjects + where T : class +{ + private T?[] _collection; + + public int Count => _collection.Length; + + public int SetMaxCount + { + set + { + if (value > 0) + { + if (_collection.Length > 0) + { + Array.Resize(ref _collection, value); + } + else + { + _collection = new T?[value]; + } + } + } + } + + public MassiveGenericObjects() + { + _collection = Array.Empty(); + } + + public T? Get(int position) + { + if (position >= _collection.Length || position < 0) return null; + return _collection[position]; + } + + public int Insert(T obj) + { + int index = 0; + while (index < _collection.Length) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return index; + } + ++index; + } + return -1; + } + public int Insert(T obj, int position) + { + if (position >= _collection.Length || position < 0) + return -1; + if (_collection[position] == null) + { + _collection[position] = obj; + return position; + } + int index = position + 1; + while (index < _collection.Length) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return index; + } + ++index; + } + index = position - 1; + while (index >= 0) + { + if (_collection[index] == null) + { + _collection[index] = obj; + return index; + } + --index; + } + return -1; + } + public T Remove(int position) + { + if (position >= _collection.Length || position < 0) + return null; + T obj = _collection[position]; + _collection[position] = null; + return obj; + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/PlaneSharingService.cs b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/PlaneSharingService.cs new file mode 100644 index 0000000..ef049f2 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/CollectionGenericObjects/PlaneSharingService.cs @@ -0,0 +1,53 @@ +using ProjectSeaplane.Drawings; + +namespace ProjectSeaplane.CollectionGenericObjects; + +internal class PlaneSharingService : AbstractCompany +{ + public PlaneSharingService(int picWidth, int picHeight, ICollectionGenericObjects collection) : base(picWidth, picHeight, collection) + { + } + + protected override void DrawBackgound(Graphics g) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + Pen pen = new(Color.Black, 3); + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height + 1; ++j) + { + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth - 5, j * _placeSizeHeight); + } + } + } + + protected override void SetObjectsPosition() + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + int curWidth = width - 1; + int curHeight = 0; + + for (int i = 0; i < (_collection?.Count ?? 0); i++) + { + if (_collection.Get(i) != null) + { + _collection.Get(i).SetPictureSize(_pictureWidth, _pictureHeight); + _collection.Get(i).SetPosition(_placeSizeWidth * curWidth, curHeight * _placeSizeHeight + 4); + } + if (curWidth > 0) + curWidth--; + else + { + curWidth = width - 1; + curHeight++; + } + if (curHeight > height) + { + return; + } + } + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs index bcace8f..42737ba 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs @@ -29,12 +29,10 @@ private void InitializeComponent() { pictureBoxSeaplane = new PictureBox(); - buttonCreateSeaplane = new Button(); buttonLeft = new Button(); buttonRight = new Button(); buttonDown = new Button(); buttonUp = new Button(); - buttonCreatePlane = new Button(); comboBoxStrategy = new ComboBox(); buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxSeaplane).BeginInit(); @@ -49,17 +47,6 @@ pictureBoxSeaplane.TabIndex = 0; pictureBoxSeaplane.TabStop = false; // - // buttonCreateSeaplane - // - buttonCreateSeaplane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateSeaplane.Location = new Point(12, 511); - buttonCreateSeaplane.Name = "buttonCreateSeaplane"; - buttonCreateSeaplane.Size = new Size(205, 23); - buttonCreateSeaplane.TabIndex = 1; - buttonCreateSeaplane.Text = "Создать Seaplane"; - buttonCreateSeaplane.UseVisualStyleBackColor = true; - buttonCreateSeaplane.Click += buttonCreateSeaplane_Click; - // // buttonLeft // buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; @@ -108,17 +95,6 @@ buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += ButtonMove_Click; // - // buttonCreatePlane - // - buttonCreatePlane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreatePlane.Location = new Point(223, 511); - buttonCreatePlane.Name = "buttonCreatePlane"; - buttonCreatePlane.Size = new Size(205, 23); - buttonCreatePlane.TabIndex = 6; - buttonCreatePlane.Text = "Создать Plane"; - buttonCreatePlane.UseVisualStyleBackColor = true; - buttonCreatePlane.Click += buttonCreatePlane_Click; - // // comboBoxStrategy // comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; @@ -146,12 +122,10 @@ ClientSize = new Size(817, 546); Controls.Add(buttonStrategyStep); Controls.Add(comboBoxStrategy); - Controls.Add(buttonCreatePlane); Controls.Add(buttonUp); Controls.Add(buttonDown); Controls.Add(buttonRight); Controls.Add(buttonLeft); - Controls.Add(buttonCreateSeaplane); Controls.Add(pictureBoxSeaplane); Name = "FormSeaplane"; Text = "FormSeaplane"; @@ -162,12 +136,10 @@ #endregion private PictureBox pictureBoxSeaplane; - private Button buttonCreateSeaplane; private Button buttonLeft; private Button buttonRight; private Button buttonDown; private Button buttonUp; - private Button buttonCreatePlane; private ComboBox comboBoxStrategy; private Button buttonStrategyStep; } diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs index 9946e60..1b7f082 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs @@ -1,13 +1,4 @@ -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 ProjectSeaplane.Drawings; +using ProjectSeaplane.Drawings; using ProjectSeaplane.MovementStrategy; namespace ProjectSeaplane @@ -18,6 +9,18 @@ namespace ProjectSeaplane private AbstractStrategy? _strategy; + public DrawingPlane SetPlane + { + set + { + _drawingPlane = value; + _drawingPlane.SetPictureSize(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); + comboBoxStrategy.Enabled = true; + _strategy = null; + Draw(); + } + } + /// /// Конструктор формы /// @@ -40,36 +43,6 @@ namespace ProjectSeaplane pictureBoxSeaplane.Image = bmp; } - private void CreateObject(string type) - { - Random random = new(); - switch (type) - { - case nameof(DrawingPlane): - _drawingPlane = new DrawingPlane(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); - break; - case nameof(DrawingSeaplane): - _drawingPlane = new DrawingSeaplane(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))); - break; - default: - return; - } - _drawingPlane.SetPictureSize(pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); - _drawingPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); - _strategy = null; - comboBoxStrategy.Enabled = true; - - Draw(); - } - - private void buttonCreateSeaplane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingSeaplane)); - - private void buttonCreatePlane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingPlane)); - private void ButtonMove_Click(object sender, EventArgs e) { if (_drawingPlane == null) diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.Designer.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.Designer.cs new file mode 100644 index 0000000..e527e20 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.Designer.cs @@ -0,0 +1,168 @@ +namespace ProjectSeaplane +{ + partial class FormSeaplaneCollection + { + /// + /// 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(); + buttonRefresh = new Button(); + buttonGoToCheck = new Button(); + buttonDelPlane = new Button(); + maskedTextBoxPosition = new MaskedTextBox(); + buttonAddSeaplane = new Button(); + buttonAddPlane = new Button(); + comboBoxSelectorCompany = new ComboBox(); + pictureBox = new PictureBox(); + groupBoxTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + SuspendLayout(); + // + // groupBoxTools + // + groupBoxTools.Controls.Add(buttonRefresh); + groupBoxTools.Controls.Add(buttonGoToCheck); + groupBoxTools.Controls.Add(buttonDelPlane); + groupBoxTools.Controls.Add(maskedTextBoxPosition); + groupBoxTools.Controls.Add(buttonAddSeaplane); + groupBoxTools.Controls.Add(buttonAddPlane); + groupBoxTools.Controls.Add(comboBoxSelectorCompany); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(655, 0); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(200, 565); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // buttonRefresh + // + buttonRefresh.Location = new Point(6, 469); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(182, 41); + buttonRefresh.TabIndex = 6; + buttonRefresh.Text = "Обновить"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += buttonRefresh_Click; + // + // buttonGoToCheck + // + buttonGoToCheck.Location = new Point(6, 322); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(182, 41); + buttonGoToCheck.TabIndex = 5; + buttonGoToCheck.Text = "Передать на тесты"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += buttonGoToCheck_Click; + // + // buttonDelPlane + // + buttonDelPlane.Location = new Point(6, 235); + buttonDelPlane.Name = "buttonDelPlane"; + buttonDelPlane.Size = new Size(182, 41); + buttonDelPlane.TabIndex = 4; + buttonDelPlane.Text = "Удаление самолета"; + buttonDelPlane.UseVisualStyleBackColor = true; + buttonDelPlane.Click += buttonDelPlane_Click; + // + // maskedTextBoxPosition + // + maskedTextBoxPosition.Location = new Point(6, 206); + maskedTextBoxPosition.Mask = "00"; + maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + maskedTextBoxPosition.Size = new Size(182, 23); + maskedTextBoxPosition.TabIndex = 3; + maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonAddSeaplane + // + buttonAddSeaplane.Location = new Point(6, 123); + buttonAddSeaplane.Name = "buttonAddSeaplane"; + buttonAddSeaplane.Size = new Size(182, 41); + buttonAddSeaplane.TabIndex = 2; + buttonAddSeaplane.Text = "Добавление водного самолета"; + buttonAddSeaplane.UseVisualStyleBackColor = true; + buttonAddSeaplane.Click += ButtonAddSeaplane_Click; + // + // buttonAddPlane + // + buttonAddPlane.Location = new Point(6, 76); + buttonAddPlane.Name = "buttonAddPlane"; + buttonAddPlane.Size = new Size(182, 41); + buttonAddPlane.TabIndex = 1; + buttonAddPlane.Text = "Добавление самолета"; + buttonAddPlane.UseVisualStyleBackColor = true; + buttonAddPlane.Click += ButtonAddPlane_Click; + // + // comboBoxSelectorCompany + // + comboBoxSelectorCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + comboBoxSelectorCompany.AutoCompleteCustomSource.AddRange(new string[] { "Хранилище" }); + comboBoxSelectorCompany.FormattingEnabled = true; + comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); + comboBoxSelectorCompany.Location = new Point(6, 22); + comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; + comboBoxSelectorCompany.Size = new Size(182, 23); + comboBoxSelectorCompany.TabIndex = 0; + comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged; + // + // pictureBox + // + pictureBox.Dock = DockStyle.Fill; + pictureBox.Location = new Point(0, 0); + pictureBox.Name = "pictureBox"; + pictureBox.Size = new Size(655, 565); + pictureBox.TabIndex = 1; + pictureBox.TabStop = false; + // + // FormSeaplaneCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(855, 565); + Controls.Add(pictureBox); + Controls.Add(groupBoxTools); + Name = "FormSeaplaneCollection"; + Text = "FormSeaplaneCollection"; + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxTools; + private Button buttonAddPlane; + private ComboBox comboBoxSelectorCompany; + private Button buttonRefresh; + private Button buttonGoToCheck; + private Button buttonDelPlane; + private MaskedTextBox maskedTextBoxPosition; + private Button buttonAddSeaplane; + private PictureBox pictureBox; + } +} \ No newline at end of file diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.cs new file mode 100644 index 0000000..ce60ae6 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.cs @@ -0,0 +1,140 @@ +using ProjectSeaplane.CollectionGenericObjects; +using ProjectSeaplane.Drawings; +using System.Windows.Forms; + +namespace ProjectSeaplane; + +public partial class FormSeaplaneCollection : Form +{ + private AbstractCompany? _company = null; + + public FormSeaplaneCollection() + { + InitializeComponent(); + } + + private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxSelectorCompany.Text) + { + case "Хранилище": + _company = new PlaneSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); + break; + } + } + + private void ButtonAddPlane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingPlane)); + + private void ButtonAddSeaplane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingSeaplane)); + + private void CreateObject(string type) + { + if (_company == null) + { + return; + } + + Random random = new(); + DrawingPlane drawningPlane; + switch (type) + { + case nameof(DrawingPlane): + drawningPlane = new DrawingPlane(random.Next(100, 300), random.Next(1000, 3000), GetColor(random)); + break; + case nameof(DrawingSeaplane): + drawningPlane = new DrawingSeaplane(random.Next(100, 300), random.Next(1000, 3000), + GetColor(random), GetColor(random), + Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); + break; + default: + return; + } + + if (_company + drawningPlane != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _company.Show(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + + private static Color GetColor(Random random) + { + 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; + } + + return color; + } + + private void buttonDelPlane_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(maskedTextBoxPosition.Text) || _company == null) + { + return; + } + + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) + { + return; + } + + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); + if (_company - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _company.Show(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void buttonGoToCheck_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + + DrawingPlane? plane = null; + int counter = 100; + while (plane == null) + { + plane = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } + } + + if (plane == null) + { + return; + } + + FormSeaplane form = new() + { + SetPlane = plane + }; + form.ShowDialog(); + } + + private void buttonRefresh_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + + pictureBox.Image = _company.Show(); + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.resx b/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplaneCollection.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/ProjectSeaplane/ProjectSeaplane/Program.cs b/ProjectSeaplane/ProjectSeaplane/Program.cs index 4e58da8..0405d27 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 FormSeaplaneCollection()); } } } \ No newline at end of file