diff --git a/ProjectCruiser/AbstractCompany.cs b/ProjectCruiser/AbstractCompany.cs new file mode 100644 index 0000000..e9b42e3 --- /dev/null +++ b/ProjectCruiser/AbstractCompany.cs @@ -0,0 +1,70 @@ +using ProjectCruiser.DrawningSamples; +namespace ProjectCruiser; + +/// Абстракция компании, хранящий коллекцию автомобилей +/// +public abstract class AbstractCompany +{ + // Размеры места + protected readonly int _placeSizeWidth = 312; // ширина + + protected readonly int _placeSizeHeight = 56; // высота + + // Ширина окна + protected readonly int _pictureWidth; + // Высота окна + protected readonly int _pictureHeight; + + // Коллекция автомобилей + protected ICollectionGenObj? _collection = null; + private int GetMaxCount => _pictureWidth * _pictureHeight / + (_placeSizeWidth * _placeSizeHeight); + + public AbstractCompany(int picWidth, int picHeight, + ICollectionGenObj? collection) + { + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = collection; + _collection.SetMaxCount = GetMaxCount; + } + + // Перегрузка оператора сложения для класса + // [ ! ] insted of bool: + public static int operator +(AbstractCompany cmp, + DrawningBase trasport) => (cmp._collection.Insert(trasport)); + + // Перегрузка оператора удаления для класса + public static DrawningBase operator -(AbstractCompany cmp, + int pos) => (cmp._collection.Remove(pos)); + + // Получение случайного объекта из коллекции + public DrawningBase? GetRandomObject() + { + Random rnd = new(); + return _collection?.GetItem(rnd.Next(GetMaxCount)); + } + + // Вывод всей коллекции + public Bitmap? Show() + { + Bitmap bitmap = new(_pictureWidth, _pictureHeight); + Graphics graphics = Graphics.FromImage(bitmap); + DrawBackground(graphics); + SetObjectsPosition(); + for (int i = 0; i < (_collection?.Count ?? 0); ++i) + { + DrawningBase? obj = _collection?.GetItem(i); + obj?.DrawTransport(graphics); + } + return bitmap; + } + + + // Вывод заднего фона + protected abstract void DrawBackground(Graphics g); + + // Расстановка объектов + protected abstract void SetObjectsPosition(); +} + diff --git a/ProjectCruiser/ArrayGenObj.cs b/ProjectCruiser/ArrayGenObj.cs new file mode 100644 index 0000000..a20cf25 --- /dev/null +++ b/ProjectCruiser/ArrayGenObj.cs @@ -0,0 +1,94 @@ +namespace ProjectCruiser; + +public class ArrayGenObj : ICollectionGenObj + 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 int SetMaxCount { set { if (value > 0) { _collection = new T?[value]; } } } + + public ArrayGenObj() + { + _collection = Array.Empty(); + } + + // methods : + + public T? GetItem(int index) + { + if (index > Count || index < 0) + { + return null; + } + return _collection[index]; + } + + public int Insert(T? item) + { + // any empty place + for (int i = 0; i < Count; i++) + { + if (_collection[i] == null) + { + _collection[i] = item; + return i; + } + } + return -1; + } + + public int Insert(T? item, int index) + { + if (_collection[index] == null) + { + _collection[index] = item; + return index; + } + + else + { + int min_diff = 100, min_index = 100; + + for (int i = 0; i < Count; i++) + { + if (_collection[i] == null + && min_diff > Math.Abs(index - i)) + { + min_diff = Math.Abs(index - i); + min_index = i; + } + } + + _collection[min_index] = item; + return min_index; + } + + return -1; + } + + public T? Remove(int index) + { + T? item; + if (index < Count && index >= 0) + { + item = _collection[index]; + _collection[index] = null; + return item; + } + return null; + } +} + diff --git a/ProjectCruiser/DrawningSamples/DrawningCruiser.cs b/ProjectCruiser/DrawningSamples/DrawningCruiser.cs index 90127e5..4f3f56e 100644 --- a/ProjectCruiser/DrawningSamples/DrawningCruiser.cs +++ b/ProjectCruiser/DrawningSamples/DrawningCruiser.cs @@ -6,11 +6,11 @@ public class DrawningCruiser : DrawningBase { // Инициализация свойств (все параметры класса (сущности)) public DrawningCruiser(int speed, double weight, Color bodyColor, - Color additionalColor, bool pads, bool hangar) : base(302, 42) + Color additionalColor, bool hangars) : base(302, 42) // all additional featchures 'inside' object, so size remains { EntityTransport = new EntityCruiser(speed, weight, - bodyColor, additionalColor, pads, hangar); + bodyColor, additionalColor, hangars); } public override void DrawTransport(Graphics g) @@ -31,24 +31,16 @@ public class DrawningCruiser : DrawningBase base.DrawTransport(g); - // вертолетная площадка - if (ship.HelicopterPads) - { - g.DrawEllipse(pen, _startPosX.Value + 170, _startPosY.Value + 11, 20, 20); - g.FillEllipse(PadBrush, _startPosX.Value + 170, _startPosY.Value + 11, 20, 20); - } + // вертолетная площадка - default TRUE now + g.DrawEllipse(pen, _startPosX.Value + 170, _startPosY.Value + 11, 20, 20); + g.FillEllipse(PadBrush, _startPosX.Value + 170, _startPosY.Value + 11, 20, 20); - // ангар - if (ship.Hangar) + // ангар(ы) + if (ship.Hangars) { - int n = EntityTransport.values[2]; - if (n == 1) g.FillRectangle(additionalBrush, _startPosX.Value + 250, _startPosY.Value + 20, 14, 7); - - else - { - g.FillRectangle(additionalBrush, _startPosX.Value + 80, _startPosY.Value + 10, 10, 20); - g.FillRectangle(additionalBrush, _startPosX.Value + 70, _startPosY.Value + 12, 8, 12); - } + g.FillRectangle(additionalBrush, _startPosX.Value + 80, _startPosY.Value + 10, 10, 20); + g.FillRectangle(additionalBrush, _startPosX.Value + 70, _startPosY.Value + 12, 8, 12); } + else g.FillRectangle(additionalBrush, _startPosX.Value + 250, _startPosY.Value + 20, 14, 7); } } diff --git a/ProjectCruiser/Entities/EntityCruiser.cs b/ProjectCruiser/Entities/EntityCruiser.cs index e251e02..514f8e8 100644 --- a/ProjectCruiser/Entities/EntityCruiser.cs +++ b/ProjectCruiser/Entities/EntityCruiser.cs @@ -6,14 +6,14 @@ public class EntityCruiser : EntityBase // признаки (наличия) public bool HelicopterPads { get; private set; } // вертолетная площадка - public bool Hangar { get; private set; } // ангар + public bool Hangars { get; private set; } // ангар public EntityCruiser(int speed, double weight, Color mainc, - Color additionalColor, bool pads, bool hangar) + Color additionalColor, bool hangars) : base(speed, weight, mainc) { AdditionalColor = additionalColor; - HelicopterPads = pads; - Hangar = hangar; + // HelicopterPads = pads; - default TRUE now for Advanced obj + Hangars = hangars; } } diff --git a/ProjectCruiser/ICollectionGenObj.cs b/ProjectCruiser/ICollectionGenObj.cs new file mode 100644 index 0000000..9012d74 --- /dev/null +++ b/ProjectCruiser/ICollectionGenObj.cs @@ -0,0 +1,24 @@ +namespace ProjectCruiser; + +public interface ICollectionGenObj where T : class +{ + // Кол-во объектов в коллекции + int Count { get; } + + // Установка max кол-ва элементов + int SetMaxCount { set; } + + /// Добавление объекта в коллекцию + /// Добавляемый объект + /// true - вставка прошла удачно, false - вставка не удалась + int Insert(T obj); + int Insert(T obj, int position); + + /// Удаление объекта из коллекции с конкретной позиции + /// Позиция + /// true - удаление прошло удачно, false - удаление не удалось + T? Remove(int position); + + // Получение объекта по позиции + T? GetItem(int position); +} diff --git a/ProjectCruiser/OceanForm1.cs b/ProjectCruiser/OceanForm1.cs index f214473..e8b217f 100644 --- a/ProjectCruiser/OceanForm1.cs +++ b/ProjectCruiser/OceanForm1.cs @@ -11,6 +11,19 @@ namespace ProjectCruiser // Стратегия перемещения private AbstractStrategy? _strategy; + // Получение объекта + public DrawningBase SetShip + { + set + { + _drawningCruiser = value; + _drawningCruiser.SetPictureSize(pictureBoxCr.Width, pictureBoxCr.Height); + comboBoxStrategy.Enabled = true; + _strategy = null; + Draw(); + } + } + public OceanForm1() { InitializeComponent(); @@ -51,10 +64,11 @@ namespace ProjectCruiser break; case nameof(DrawningCruiser): - _drawningCruiser = new DrawningCruiser(random.Next(100, 300), random.Next(1000, 3000), + _drawningCruiser = new DrawningCruiser( + 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))); + Convert.ToBoolean(random.Next(0, 2))); break; default: diff --git a/ProjectCruiser/Program.cs b/ProjectCruiser/Program.cs index 5669fde..faeb072 100644 --- a/ProjectCruiser/Program.cs +++ b/ProjectCruiser/Program.cs @@ -11,7 +11,8 @@ namespace ProjectCruiser // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new OceanForm1()); + Application.Run(new ServiceForm2()); + // -> OceanForm1() inside* } } } \ No newline at end of file diff --git a/ProjectCruiser/ServiceForm2.Designer.cs b/ProjectCruiser/ServiceForm2.Designer.cs new file mode 100644 index 0000000..8990b79 --- /dev/null +++ b/ProjectCruiser/ServiceForm2.Designer.cs @@ -0,0 +1,173 @@ +namespace ProjectCruiser +{ + partial class ServiceForm2 + { + /// + /// 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() + { + comboBoxArrList = new ComboBox(); + btnAddBase = new Button(); + groupBox = new GroupBox(); + btnUpdate = new Button(); + btnTest = new Button(); + btnDelete = new Button(); + maskedTextBoxPosition = new MaskedTextBox(); + btnAddCruiser = new Button(); + pictureBox = new PictureBox(); + groupBox.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + SuspendLayout(); + // + // comboBoxArrList + // + comboBoxArrList.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + comboBoxArrList.FormattingEnabled = true; + comboBoxArrList.Items.AddRange(new object[] { "Storage" }); + comboBoxArrList.Location = new Point(17, 54); + comboBoxArrList.Name = "comboBoxArrList"; + comboBoxArrList.Size = new Size(268, 40); + comboBoxArrList.TabIndex = 0; + comboBoxArrList.SelectedIndexChanged += SelectorCompany_SelectedIndexChanged; + // + // btnAddBase + // + btnAddBase.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + btnAddBase.Location = new Point(17, 561); + btnAddBase.Name = "btnAddBase"; + btnAddBase.Size = new Size(268, 46); + btnAddBase.TabIndex = 1; + btnAddBase.Text = "Add ship"; + btnAddBase.UseVisualStyleBackColor = true; + btnAddBase.Click += btnAddBase_Click; + // + // groupBox + // + groupBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + groupBox.Controls.Add(btnUpdate); + groupBox.Controls.Add(btnTest); + groupBox.Controls.Add(btnDelete); + groupBox.Controls.Add(maskedTextBoxPosition); + groupBox.Controls.Add(btnAddCruiser); + groupBox.Controls.Add(btnAddBase); + groupBox.Controls.Add(comboBoxArrList); + groupBox.Location = new Point(1437, 12); + groupBox.Name = "groupBox"; + groupBox.Size = new Size(300, 938); + groupBox.TabIndex = 2; + groupBox.TabStop = false; + groupBox.Text = "Tool panel"; + // + // btnUpdate + // + btnUpdate.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + btnUpdate.Location = new Point(17, 865); + btnUpdate.Name = "btnUpdate"; + btnUpdate.Size = new Size(268, 46); + btnUpdate.TabIndex = 6; + btnUpdate.Text = "Update"; + btnUpdate.UseVisualStyleBackColor = true; + btnUpdate.Click += btnRefresh_Click; + // + // btnTest + // + btnTest.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + btnTest.Location = new Point(17, 795); + btnTest.Name = "btnTest"; + btnTest.Size = new Size(268, 64); + btnTest.TabIndex = 5; + btnTest.Text = "Choose for testing"; + btnTest.UseVisualStyleBackColor = true; + btnTest.Click += btnChooseforTest_Click; + // + // btnDelete + // + btnDelete.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + btnDelete.Location = new Point(17, 732); + btnDelete.Name = "btnDelete"; + btnDelete.Size = new Size(268, 46); + btnDelete.TabIndex = 4; + btnDelete.Text = "Delete"; + btnDelete.UseVisualStyleBackColor = true; + btnDelete.Click += btnRemoveCar_Click; + // + // maskedTextBoxPosition + // + maskedTextBoxPosition.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + maskedTextBoxPosition.Location = new Point(17, 682); + maskedTextBoxPosition.Mask = "00"; + maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + maskedTextBoxPosition.Size = new Size(268, 39); + maskedTextBoxPosition.TabIndex = 3; + maskedTextBoxPosition.ValidatingType = typeof(int); + // + // btnAddCruiser + // + btnAddCruiser.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + btnAddCruiser.Location = new Point(17, 613); + btnAddCruiser.Name = "btnAddCruiser"; + btnAddCruiser.Size = new Size(268, 46); + btnAddCruiser.TabIndex = 2; + btnAddCruiser.Text = "Add cruiser"; + btnAddCruiser.UseVisualStyleBackColor = true; + btnAddCruiser.Click += btnAddAdvanced_Click; + // + // pictureBox + // + pictureBox.Dock = DockStyle.Left; + pictureBox.Location = new Point(0, 0); + pictureBox.Name = "pictureBox"; + pictureBox.Size = new Size(1417, 959); + pictureBox.TabIndex = 3; + pictureBox.TabStop = false; + // + // ServiceForm2 + // + AutoScaleDimensions = new SizeF(13F, 32F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1749, 959); + Controls.Add(pictureBox); + Controls.Add(groupBox); + Name = "ServiceForm2"; + Text = "ServiceForm2"; + groupBox.ResumeLayout(false); + groupBox.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + ResumeLayout(false); + } + + #endregion + + private ComboBox comboBoxArrList; + private Button btnAddBase; + private GroupBox groupBox; + private Button btnAddCruiser; + private Button btnUpdate; + private Button btnTest; + private Button btnDelete; + private MaskedTextBox maskedTextBoxPosition; + private PictureBox pictureBox; + } +} \ No newline at end of file diff --git a/ProjectCruiser/ServiceForm2.cs b/ProjectCruiser/ServiceForm2.cs new file mode 100644 index 0000000..b17ae80 --- /dev/null +++ b/ProjectCruiser/ServiceForm2.cs @@ -0,0 +1,138 @@ +using System.Windows.Forms; +using ProjectCruiser.DrawningSamples; +namespace ProjectCruiser; + +public partial class ServiceForm2 : Form +{ + // Компания + private AbstractCompany? _company; + public ServiceForm2() + { + InitializeComponent(); + _company = null; + } + + // Выбор компании + private void SelectorCompany_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxArrList.Text) + { + case "Storage": + _company = new ShipSharingService(pictureBox.Width, pictureBox.Height, + new ArrayGenObj()); + break; + } + } + + // Color picker + private static Color pickColor(Random r) + { + Color cl = Color.FromArgb(r.Next(0, 256), r.Next(0, 256), r.Next(0, 256)); + ColorDialog dialog = new(); + + if (dialog.ShowDialog() == DialogResult.OK) + { cl = dialog.Color; } + + return cl; + } + + // Добавление обычного корабля + private void btnAddBase_Click(object sender, EventArgs e) => + CreateObject(nameof(DrawningBase)); + + // Добавление продвинутого + private void btnAddAdvanced_Click(object sender, EventArgs e) => + CreateObject(nameof(DrawningCruiser)); + + // Создание объекта класса-перемещения + private void CreateObject(string type) + { + if (_company == null) + { + return; + } + Random random = new(); + DrawningBase drawningCar; + + switch (type) + { + case nameof(DrawningBase): + drawningCar = new DrawningBase(random.Next(100, 300), + random.Next(1000, 3000), pickColor(random)); + break; + + case nameof(DrawningCruiser): + // (TODO) вызов диалогового окна для выбора цвета >>> + drawningCar = new DrawningCruiser(random.Next(100, 300), + random.Next(1000, 3000), pickColor(random), pickColor(random), + Convert.ToBoolean(random.Next(0, 2))); + break; + default: + return; + } + if (_company + drawningCar != -1) + { + MessageBox.Show("> Object was added"); + pictureBox.Image = _company.Show(); + } + else + { + MessageBox.Show("[!] Failed to add object"); + } + } + + // Удаление объекта + private void btnRemoveCar_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(maskedTextBoxPosition.Text) + || _company == null) return; + + if (MessageBox.Show("[*] Remove object: Are you sure?", "Remove", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) return; + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); + + if (_company - Convert.ToInt32(maskedTextBoxPosition.Text) != null) + { + MessageBox.Show("> Object was removed"); + pictureBox.Image = _company.Show(); + } + else MessageBox.Show("[!] Failed to remove object"); + } + + // Передача объекта в другую форму + private void btnChooseforTest_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + DrawningBase? car = null; + int counter = 100; + while (car == null) + { + car = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } + } + if (car == null) + { + return; + } + + OceanForm1 form = new() { SetShip = car }; + form.ShowDialog(); + } + + // Перерисовка коллекции + private void btnRefresh_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + pictureBox.Image = _company.Show(); + } +} diff --git a/ProjectCruiser/ServiceForm2.resx b/ProjectCruiser/ServiceForm2.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ProjectCruiser/ServiceForm2.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/ProjectCruiser/ShipSharingService.cs b/ProjectCruiser/ShipSharingService.cs new file mode 100644 index 0000000..e0098d4 --- /dev/null +++ b/ProjectCruiser/ShipSharingService.cs @@ -0,0 +1,69 @@ +using ProjectCruiser.DrawningSamples; +namespace ProjectCruiser; + +public class ShipSharingService : AbstractCompany +{ + protected int MaxInRow { get; private set; } + protected int MaxInColon { get; private set; } + + private int fromBorder = 20, fromCeiling = 20, between = 30; + + public ShipSharingService(int picWidth, int picHeight, + ICollectionGenObj collection) + : base(picWidth, picHeight, collection) + { + MaxInRow = (picWidth - fromBorder - fromCeiling) + / (_placeSizeWidth + between); + MaxInColon = (picHeight - fromBorder - fromCeiling) + / _placeSizeHeight; + } + + protected override void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 2); + + int currentH = fromCeiling, currentW = fromBorder; + + for (int i = 0; i < MaxInRow; i++) + { + currentH = fromCeiling; + for (int j = 0; j < MaxInColon; j++) + { + g.DrawLine(pen, currentW + _placeSizeWidth, + currentH, currentW, currentH); + + g.DrawLine(pen, currentW, currentH, + currentW, currentH + _placeSizeHeight); + + currentH += _placeSizeHeight + 1; + } + currentW += _placeSizeWidth + between; + } + } + + protected override void SetObjectsPosition() + { + int index_collection = 0; + int newX = fromBorder + 6, newY = fromCeiling + 6; + + if (_collection != null) + { + for (int i = 0; i < MaxInColon; ++i) + { + newX = fromBorder + 2; + for (int j = 0; j < MaxInRow; ++j) + { + if (_collection.GetItem(index_collection) != null) + { + _collection.GetItem(index_collection).SetPictureSize(_pictureWidth, _pictureHeight); + _collection.GetItem(index_collection).SetPosition(newX, newY); + newX += _placeSizeWidth + between + 2; + index_collection++; + } + } + newY += _placeSizeHeight + 2; + } + } + } +} +