From efebd664bad1959269c5aedd84906a89b9aacb14 Mon Sep 17 00:00:00 2001 From: vettaql Date: Thu, 11 Apr 2024 11:07:17 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=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=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=20=E2=84=963?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractCompany.cs | 116 +++++++++++ .../ICollectionGenericObjects.cs | 48 +++++ .../MassiveGenericObjects.cs | 136 +++++++++++++ .../CollectionGenericObjects/TankBase.cs | 55 +++++ .../ProjectTank/Drawnings/DrawningTank.cs | 16 +- .../ProjectTank/Drawnings/DrawningTank2.cs | 32 +-- ProjectTank/ProjectTank/FormTank.Designer.cs | 67 +++--- ProjectTank/ProjectTank/FormTank.cs | 62 ++---- .../FormTankCollection.Designer.cs | 175 ++++++++++++++++ ProjectTank/ProjectTank/FormTankCollection.cs | 190 ++++++++++++++++++ .../ProjectTank/FormTankCollection.resx | 120 +++++++++++ ProjectTank/ProjectTank/Program.cs | 7 +- 12 files changed, 913 insertions(+), 111 deletions(-) create mode 100644 ProjectTank/ProjectTank/CollectionGenericObjects/AbstractCompany.cs create mode 100644 ProjectTank/ProjectTank/CollectionGenericObjects/ICollectionGenericObjects.cs create mode 100644 ProjectTank/ProjectTank/CollectionGenericObjects/MassiveGenericObjects.cs create mode 100644 ProjectTank/ProjectTank/CollectionGenericObjects/TankBase.cs create mode 100644 ProjectTank/ProjectTank/FormTankCollection.Designer.cs create mode 100644 ProjectTank/ProjectTank/FormTankCollection.cs create mode 100644 ProjectTank/ProjectTank/FormTankCollection.resx diff --git a/ProjectTank/ProjectTank/CollectionGenericObjects/AbstractCompany.cs b/ProjectTank/ProjectTank/CollectionGenericObjects/AbstractCompany.cs new file mode 100644 index 0000000..0c756a0 --- /dev/null +++ b/ProjectTank/ProjectTank/CollectionGenericObjects/AbstractCompany.cs @@ -0,0 +1,116 @@ +using ProjectTank.Drawnings; + +namespace ProjectTank.CollectionGenericObjects; + +/// +/// Абстракция компании, хранящий коллекцию автомобилей +/// +public abstract class AbstractCompany +{ + /// + /// Размер места (ширина) + /// + protected readonly int _placeSizeWidth = 210; + + /// + /// Размер места (высота) + /// + protected readonly int _placeSizeHeight = 80; + + /// + /// Ширина окна + /// + 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, DrawningTank2 tank) + { + return company._collection.Insert(tank); + } + + /// + /// Перегрузка оператора удаления для класса + /// + /// Компания + /// Номер удаляемого объекта + /// + public static DrawningTank2? operator -(AbstractCompany company, int position) + { + return company._collection?.Remove(position); + } + + /// + /// Получение случайного объекта из коллекции + /// + /// + public DrawningTank2? 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) + { + DrawningTank2? 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/ProjectTank/ProjectTank/CollectionGenericObjects/ICollectionGenericObjects.cs b/ProjectTank/ProjectTank/CollectionGenericObjects/ICollectionGenericObjects.cs new file mode 100644 index 0000000..89f081e --- /dev/null +++ b/ProjectTank/ProjectTank/CollectionGenericObjects/ICollectionGenericObjects.cs @@ -0,0 +1,48 @@ +namespace ProjectTank.CollectionGenericObjects; + +/// +/// Интерфейс описания действий для набора хранимых объектов +/// +/// Параметр: ограничение - ссылочный тип +public interface ICollectionGenericObjects + where T : class +{ + /// + /// Количество объектов в коллекции + /// + int Count { get; } + + /// + /// Установка максимального количества элементов + /// + int SetMaxCount { set; } + + /// + /// Добавление объекта в коллекцию + /// + /// Добавляемый объект + /// true - вставка прошла удачно, false - вставка не удалась + int Insert(T obj); + + /// + /// Добавление объекта в коллекцию на конкретную позицию + /// + /// Добавляемый объект + /// Позиция + /// true - вставка прошла удачно, false - вставка не удалась + int Insert(T obj, int position); + + /// + /// Удаление объекта из коллекции с конкретной позиции + /// + /// Позиция + /// true - удаление прошло удачно, false - удаление не удалось + T? Remove(int position); + + /// + /// Получение объекта по позиции + /// + /// Позиция + /// Объект + T? Get(int position); +} \ No newline at end of file diff --git a/ProjectTank/ProjectTank/CollectionGenericObjects/MassiveGenericObjects.cs b/ProjectTank/ProjectTank/CollectionGenericObjects/MassiveGenericObjects.cs new file mode 100644 index 0000000..93a34cb --- /dev/null +++ b/ProjectTank/ProjectTank/CollectionGenericObjects/MassiveGenericObjects.cs @@ -0,0 +1,136 @@ +using System.Runtime.Remoting; +using ProjectTank.Drawnings; + +namespace ProjectTank.CollectionGenericObjects; + +internal 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 >= 0 && position < Count) + { + return _collection[position]; + } + + return null; + } + + public int Insert(T obj) + { + // вставка в свободное место набора + for (int i = 0; i < Count; i++) + { + if (_collection[i] == null) + { + _collection[i] = obj; + return i; + } + } + + return -1; + } + + public int Insert(T obj, int position) + { + // проверка позиции + if (position < 0 || position >= Count) + { + return -1; + } + + // проверка, что элемент массива по этой позиции пустой, если нет, то + // ищется свободное место после этой позиции и идет вставка туда + // если нет после, ищем до + if (_collection[position] != null) + { + bool pushed = false; + for (int index = position + 1; index < Count; index++) + { + if (_collection[index] == null) + { + position = index; + pushed = true; + break; + } + } + + if (!pushed) + { + for (int index = position - 1; index >= 0; index--) + { + if (_collection[index] == null) + { + position = index; + pushed = true; + break; + } + } + } + + if (!pushed) + { + return position; + } + } + + // вставка + _collection[position] = obj; + return position; + } + + public T? Remove(int position) + { + // проверка позиции + if (position < 0 || position >= Count) + { + return null; + } + + if (_collection[position] == null) return null; + + T? temp = _collection[position]; + _collection[position] = null; + return temp; + } + +} + + + + + + + diff --git a/ProjectTank/ProjectTank/CollectionGenericObjects/TankBase.cs b/ProjectTank/ProjectTank/CollectionGenericObjects/TankBase.cs new file mode 100644 index 0000000..7ecac01 --- /dev/null +++ b/ProjectTank/ProjectTank/CollectionGenericObjects/TankBase.cs @@ -0,0 +1,55 @@ +using ProjectTank.Drawnings; +using ProjectTank.Entities; +using System; + +namespace ProjectTank.CollectionGenericObjects; + +/// +/// Реализация абстрактной компании - аренда поезда +/// +public class TankBase : AbstractCompany +{ + /// + /// Конструктор + /// + /// + /// + /// + public TankBase(int picWidth, int picHeight, ICollectionGenericObjects collection) : base(picWidth, picHeight, collection) + { + } + + protected override void DrawBackgound(Graphics g) + { + Pen pen = new(Color.Black); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++) + { + g.DrawLine(pen, new(_placeSizeWidth * i, _placeSizeHeight * j), new((int)(_placeSizeWidth * (i + 0.5f)), _placeSizeHeight * j)); + g.DrawLine(pen, new(_placeSizeWidth * i, _placeSizeHeight * j), new(_placeSizeWidth * i, _placeSizeHeight * (j + 1))); + } + g.DrawLine(pen, new(_placeSizeWidth * i, _placeSizeHeight * (_pictureHeight / _placeSizeHeight)), new((int)(_placeSizeWidth * (i + 0.5f)), _placeSizeHeight * (_pictureHeight / _placeSizeHeight))); + } + + + } + + protected override void SetObjectsPosition() + { + int n = 0; + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight; j++) + { + DrawningTank2? drawningTank2 = _collection?.Get(n); + n++; + if (drawningTank2 != null) + { + drawningTank2.SetPictureSize(_pictureWidth, _pictureHeight); + drawningTank2.SetPosition(i * _placeSizeWidth + 5, j * _placeSizeHeight + 5); + } + } + } + } +} \ No newline at end of file diff --git a/ProjectTank/ProjectTank/Drawnings/DrawningTank.cs b/ProjectTank/ProjectTank/Drawnings/DrawningTank.cs index bcc32c6..7df44b9 100644 --- a/ProjectTank/ProjectTank/Drawnings/DrawningTank.cs +++ b/ProjectTank/ProjectTank/Drawnings/DrawningTank.cs @@ -40,19 +40,19 @@ public class DrawningTank : DrawningTank2 if (tank.GunTurret) { - g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 42, 85, 8); - g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 42, 85, 8); + g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 17, 85, 8); + g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 17, 85, 8); } if (tank.MachineGun) { - g.DrawRectangle(pen, _startPosX.Value + 101, _startPosY.Value + 27, 24, 12); - g.DrawRectangle(pen, _startPosX.Value + 109, _startPosY.Value + 9, 5, 18); - g.DrawRectangle(pen, _startPosX.Value + 91, _startPosY.Value + 13, 19, 5); + g.DrawRectangle(pen, _startPosX.Value + 101, _startPosY.Value + 3, 24, 12); + g.DrawRectangle(pen, _startPosX.Value + 109, _startPosY.Value + -14, 5, 18); + g.DrawRectangle(pen, _startPosX.Value + 91, _startPosY.Value + -12, 19, 5); - g.FillRectangle(additionalBrush, _startPosX.Value + 101, _startPosY.Value + 27, 24, 12); - g.FillRectangle(additionalBrush, _startPosX.Value + 109, _startPosY.Value + 9, 5, 18); - g.FillRectangle(additionalBrush, _startPosX.Value + 91, _startPosY.Value + 13, 19, 5); + g.FillRectangle(additionalBrush, _startPosX.Value + 101, _startPosY.Value + 3, 24, 12); + g.FillRectangle(additionalBrush, _startPosX.Value + 109, _startPosY.Value + -14, 5, 18); + g.FillRectangle(additionalBrush, _startPosX.Value + 91, _startPosY.Value + -12, 19, 5); } diff --git a/ProjectTank/ProjectTank/Drawnings/DrawningTank2.cs b/ProjectTank/ProjectTank/Drawnings/DrawningTank2.cs index 7c16542..a3c4fb1 100644 --- a/ProjectTank/ProjectTank/Drawnings/DrawningTank2.cs +++ b/ProjectTank/ProjectTank/Drawnings/DrawningTank2.cs @@ -195,33 +195,33 @@ public class DrawningTank2 Pen pen = new(Color.Black); //границы танка + гусеницы + пулемёт + башня с оружием - g.DrawRectangle(pen, _startPosX.Value + 48, _startPosY.Value + 39, 55, 17); - g.DrawRectangle(pen, _startPosX.Value + 12, _startPosY.Value + 56, 137, 13); + g.DrawRectangle(pen, _startPosX.Value + 48, _startPosY.Value + 16, 55, 17); + g.DrawRectangle(pen, _startPosX.Value + 12, _startPosY.Value + 31, 137, 13); - g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 59, 160, 35); - g.DrawEllipse(pen, _startPosX.Value + 51, _startPosY.Value + 65, 29, 23); - g.DrawEllipse(pen, _startPosX.Value + 111, _startPosY.Value + 65, 29, 23); - g.DrawEllipse(pen, _startPosX.Value + 91, _startPosY.Value + 73, 18, 15); - g.DrawEllipse(pen, _startPosX.Value + 71, _startPosY.Value + 73, 18, 15); - g.DrawEllipse(pen, _startPosX.Value + 51, _startPosY.Value + 73, 18, 15); + g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 34, 160, 35); + g.DrawEllipse(pen, _startPosX.Value + 51, _startPosY.Value + 38, 29, 23); + g.DrawEllipse(pen, _startPosX.Value + 111, _startPosY.Value + 30, 29, 23); + g.DrawEllipse(pen, _startPosX.Value + 91, _startPosY.Value + 48, 18, 15); + g.DrawEllipse(pen, _startPosX.Value + 71, _startPosY.Value + 48, 18, 15); + g.DrawEllipse(pen, _startPosX.Value + 51, _startPosY.Value + 48, 18, 15); //танк Brush br = new SolidBrush(EntityTank2.BodyColor); - g.FillRectangle(br, _startPosX.Value + 48, _startPosY.Value + 39, 55, 17); - g.FillRectangle(br, _startPosX.Value + 12, _startPosY.Value + 56, 137, 13); + g.FillRectangle(br, _startPosX.Value + 48, _startPosY.Value + 14, 55, 17); + g.FillRectangle(br, _startPosX.Value + 12, _startPosY.Value + 31, 137, 13); Brush brDBlue = new SolidBrush(Color.DarkBlue); - g.FillEllipse(brDBlue, _startPosX.Value, _startPosY.Value + 59, 160, 35); + g.FillEllipse(brDBlue, _startPosX.Value, _startPosY.Value + 34, 160, 35); Brush brBlue = new SolidBrush(Color.LightBlue); - g.FillEllipse(brBlue, _startPosX.Value + 19, _startPosY.Value + 65, 29, 23); - g.FillEllipse(brBlue, _startPosX.Value + 111, _startPosY.Value + 65, 29, 23); - g.FillEllipse(brBlue, _startPosX.Value + 91, _startPosY.Value + 73, 18, 15); - g.FillEllipse(brBlue, _startPosX.Value + 71, _startPosY.Value + 73, 18, 15); - g.FillEllipse(brBlue, _startPosX.Value + 51, _startPosY.Value + 73, 18, 15); + g.FillEllipse(brBlue, _startPosX.Value + 19, _startPosY.Value + 40, 29, 23); + g.FillEllipse(brBlue, _startPosX.Value + 111, _startPosY.Value + 40, 29, 23); + g.FillEllipse(brBlue, _startPosX.Value + 91, _startPosY.Value + 48, 18, 15); + g.FillEllipse(brBlue, _startPosX.Value + 71, _startPosY.Value + 48, 18, 15); + g.FillEllipse(brBlue, _startPosX.Value + 51, _startPosY.Value + 48, 18, 15); diff --git a/ProjectTank/ProjectTank/FormTank.Designer.cs b/ProjectTank/ProjectTank/FormTank.Designer.cs index 63b89a0..9b52639 100644 --- a/ProjectTank/ProjectTank/FormTank.Designer.cs +++ b/ProjectTank/ProjectTank/FormTank.Designer.cs @@ -33,12 +33,10 @@ namespace ProjectTank private void InitializeComponent() { pictureBoxTank = new PictureBox(); - buttonCreateTank = new Button(); buttonLeft = new Button(); buttonUp = new Button(); buttonDown = new Button(); buttonRight = new Button(); - buttonCreateTank2 = new Button(); comboBoxStrategy = new ComboBox(); buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxTank).BeginInit(); @@ -48,30 +46,22 @@ namespace ProjectTank // pictureBoxTank.Dock = DockStyle.Fill; pictureBoxTank.Location = new Point(0, 0); + pictureBoxTank.Margin = new Padding(3, 4, 3, 4); pictureBoxTank.Name = "pictureBoxTank"; - pictureBoxTank.Size = new Size(923, 597); + pictureBoxTank.Size = new Size(1055, 796); pictureBoxTank.TabIndex = 0; pictureBoxTank.TabStop = false; - // - // buttonCreateTank - // - buttonCreateTank.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateTank.Location = new Point(12, 562); - buttonCreateTank.Name = "buttonCreateTank"; - buttonCreateTank.Size = new Size(223, 23); - buttonCreateTank.TabIndex = 1; - buttonCreateTank.Text = "Создать танк с пулемётом"; - buttonCreateTank.UseVisualStyleBackColor = true; - buttonCreateTank.Click += ButtonCreateTank_Click; + pictureBoxTank.Click += pictureBoxTank_Click; // // buttonLeft // buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.BackgroundImage = Properties.Resources.Left; buttonLeft.BackgroundImageLayout = ImageLayout.Stretch; - buttonLeft.Location = new Point(787, 550); + buttonLeft.Location = new Point(899, 733); + buttonLeft.Margin = new Padding(3, 4, 3, 4); buttonLeft.Name = "buttonLeft"; - buttonLeft.Size = new Size(35, 35); + buttonLeft.Size = new Size(40, 47); buttonLeft.TabIndex = 2; buttonLeft.UseVisualStyleBackColor = true; buttonLeft.Click += ButtonMove_Click; @@ -81,9 +71,10 @@ namespace ProjectTank buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.BackgroundImage = Properties.Resources.Up; buttonUp.BackgroundImageLayout = ImageLayout.Stretch; - buttonUp.Location = new Point(828, 509); + buttonUp.Location = new Point(946, 679); + buttonUp.Margin = new Padding(3, 4, 3, 4); buttonUp.Name = "buttonUp"; - buttonUp.Size = new Size(35, 35); + buttonUp.Size = new Size(40, 47); buttonUp.TabIndex = 3; buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += ButtonMove_Click; @@ -93,9 +84,10 @@ namespace ProjectTank buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.BackgroundImage = Properties.Resources.Down; buttonDown.BackgroundImageLayout = ImageLayout.Stretch; - buttonDown.Location = new Point(828, 550); + buttonDown.Location = new Point(946, 733); + buttonDown.Margin = new Padding(3, 4, 3, 4); buttonDown.Name = "buttonDown"; - buttonDown.Size = new Size(35, 35); + buttonDown.Size = new Size(40, 47); buttonDown.TabIndex = 4; buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += ButtonMove_Click; @@ -105,39 +97,31 @@ namespace ProjectTank buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.BackgroundImage = Properties.Resources.Right; buttonRight.BackgroundImageLayout = ImageLayout.Stretch; - buttonRight.Location = new Point(869, 550); + buttonRight.Location = new Point(993, 733); + buttonRight.Margin = new Padding(3, 4, 3, 4); buttonRight.Name = "buttonRight"; - buttonRight.Size = new Size(35, 35); + buttonRight.Size = new Size(40, 47); buttonRight.TabIndex = 5; buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; // - // buttonCreateTank2 - // - buttonCreateTank2.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreateTank2.Location = new Point(250, 562); - buttonCreateTank2.Name = "buttonCreateTank2"; - buttonCreateTank2.Size = new Size(223, 23); - buttonCreateTank2.TabIndex = 6; - buttonCreateTank2.Text = "Создать обычный танк"; - buttonCreateTank2.UseVisualStyleBackColor = true; - buttonCreateTank2.Click += ButtonCreateTank2_Click; - // // comboBoxStrategy // comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxStrategy.FormattingEnabled = true; comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); - comboBoxStrategy.Location = new Point(790, 12); + comboBoxStrategy.Location = new Point(903, 16); + comboBoxStrategy.Margin = new Padding(3, 4, 3, 4); comboBoxStrategy.Name = "comboBoxStrategy"; - comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.Size = new Size(138, 28); comboBoxStrategy.TabIndex = 7; // // buttonStrategyStep // - buttonStrategyStep.Location = new Point(836, 41); + buttonStrategyStep.Location = new Point(955, 55); + buttonStrategyStep.Margin = new Padding(3, 4, 3, 4); buttonStrategyStep.Name = "buttonStrategyStep"; - buttonStrategyStep.Size = new Size(75, 23); + buttonStrategyStep.Size = new Size(86, 31); buttonStrategyStep.TabIndex = 8; buttonStrategyStep.Text = "Шаг"; buttonStrategyStep.UseVisualStyleBackColor = true; @@ -145,18 +129,17 @@ namespace ProjectTank // // FormTank // - AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(923, 597); + ClientSize = new Size(1055, 796); Controls.Add(buttonStrategyStep); Controls.Add(comboBoxStrategy); - Controls.Add(buttonCreateTank2); Controls.Add(buttonRight); Controls.Add(buttonDown); Controls.Add(buttonUp); Controls.Add(buttonLeft); - Controls.Add(buttonCreateTank); Controls.Add(pictureBoxTank); + Margin = new Padding(3, 4, 3, 4); Name = "FormTank"; Text = "Танк с пулемётом"; ((System.ComponentModel.ISupportInitialize)pictureBoxTank).EndInit(); @@ -166,12 +149,10 @@ namespace ProjectTank #endregion private PictureBox pictureBoxTank; - private Button buttonCreateTank; private Button buttonLeft; private Button buttonUp; private Button buttonDown; private Button buttonRight; - private Button buttonCreateTank2; private ComboBox comboBoxStrategy; private Button buttonStrategyStep; } diff --git a/ProjectTank/ProjectTank/FormTank.cs b/ProjectTank/ProjectTank/FormTank.cs index 3e3ff92..45fa5af 100644 --- a/ProjectTank/ProjectTank/FormTank.cs +++ b/ProjectTank/ProjectTank/FormTank.cs @@ -14,6 +14,21 @@ public partial class FormTank : Form /// private AbstractStrategy? _strategy; + /// + /// Получение объекта + /// + public DrawningTank2 SetTank + { + set + { + _drawningTank2 = value; + _drawningTank2.SetPictureSize(pictureBoxTank.Width, pictureBoxTank.Height); + comboBoxStrategy.Enabled = true; + _strategy = null; + Draw(); + } + } + /// /// Конструктор формы /// @@ -38,48 +53,6 @@ public partial class FormTank : Form pictureBoxTank.Image = bmp; } - /// - /// Создание объекта класса-перемещения - /// - /// Тип создаваемого объекта - private void CreateObject(string type) - { - Random random = new(); - switch (type) - { - case nameof(DrawningTank2): - _drawningTank2 = new DrawningTank2(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(DrawningTank): - _drawningTank2 = new DrawningTank(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; - } - - _drawningTank2.SetPictureSize(pictureBoxTank.Width, pictureBoxTank.Height); - _drawningTank2.SetPosition(random.Next(10, 100), random.Next(10, 100)); - _strategy = null; - comboBoxStrategy.Enabled = true; - Draw(); - } - /// - /// Обработка нажатия кнопки "Создать танк с пулемётом" - /// - /// - /// - private void ButtonCreateTank_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTank)); - - /// - /// Обработка нажатия кнопки "Создать обычный танк" - /// - /// - /// - private void ButtonCreateTank2_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTank2)); /// /// Перемещение объекта по форме (нажатие кнопок навигации) @@ -164,6 +137,11 @@ public partial class FormTank : Form { } + + private void pictureBoxTank_Click(object sender, EventArgs e) + { + + } } diff --git a/ProjectTank/ProjectTank/FormTankCollection.Designer.cs b/ProjectTank/ProjectTank/FormTankCollection.Designer.cs new file mode 100644 index 0000000..1a8283f --- /dev/null +++ b/ProjectTank/ProjectTank/FormTankCollection.Designer.cs @@ -0,0 +1,175 @@ +namespace ProjectTank +{ + partial class FormTankCollection + { + /// + /// 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(); + comboBoxSelectorCompany = new ComboBox(); + buttonAddTank2 = new Button(); + buttonAddTank = new Button(); + pictureBox = new PictureBox(); + maskedTextBox = new MaskedTextBox(); + buttonDelTank = new Button(); + buttonGoToCheck = new Button(); + buttonRefresh = new Button(); + groupBoxTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + SuspendLayout(); + // + // groupBoxTools + // + groupBoxTools.Controls.Add(buttonRefresh); + groupBoxTools.Controls.Add(buttonGoToCheck); + groupBoxTools.Controls.Add(buttonDelTank); + groupBoxTools.Controls.Add(maskedTextBox); + groupBoxTools.Controls.Add(buttonAddTank); + groupBoxTools.Controls.Add(buttonAddTank2); + groupBoxTools.Controls.Add(comboBoxSelectorCompany); + groupBoxTools.Dock = DockStyle.Right; + groupBoxTools.Location = new Point(931, 0); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(218, 687); + groupBoxTools.TabIndex = 0; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // comboBoxSelectorCompany + // + comboBoxSelectorCompany.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxSelectorCompany.FormattingEnabled = true; + comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); + comboBoxSelectorCompany.Location = new Point(6, 26); + comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; + comboBoxSelectorCompany.Size = new Size(206, 28); + comboBoxSelectorCompany.TabIndex = 0; + comboBoxSelectorCompany.SelectedIndexChanged += ComboBoxSelectorCompany_SelectedIndexChanged; + // + // buttonAddTank2 + // + buttonAddTank2.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddTank2.Location = new Point(6, 111); + buttonAddTank2.Name = "buttonAddTank2"; + buttonAddTank2.Size = new Size(206, 49); + buttonAddTank2.TabIndex = 1; + buttonAddTank2.Text = "Добавление бронированной машины"; + buttonAddTank2.UseVisualStyleBackColor = true; + buttonAddTank2.Click += ButtonAddTank2_Click; + // + // buttonAddTank + // + buttonAddTank.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonAddTank.Location = new Point(6, 166); + buttonAddTank.Name = "buttonAddTank"; + buttonAddTank.Size = new Size(206, 49); + buttonAddTank.TabIndex = 2; + buttonAddTank.Text = "Добавление танка "; + buttonAddTank.UseVisualStyleBackColor = true; + buttonAddTank.Click += ButtonAddTank_Click; + // + // pictureBox + // + pictureBox.Dock = DockStyle.Fill; + pictureBox.Location = new Point(0, 0); + pictureBox.Name = "pictureBox"; + pictureBox.Size = new Size(931, 687); + pictureBox.TabIndex = 1; + pictureBox.TabStop = false; + + // + // maskedTextBox + // + maskedTextBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + maskedTextBox.Location = new Point(6, 276); + maskedTextBox.Mask = "00"; + maskedTextBox.Name = "maskedTextBox"; + maskedTextBox.Size = new Size(206, 27); + maskedTextBox.TabIndex = 3; + maskedTextBox.ValidatingType = typeof(int); + // + // buttonDelTank + // + buttonDelTank.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonDelTank.Location = new Point(6, 309); + buttonDelTank.Name = "buttonDelTank"; + buttonDelTank.Size = new Size(206, 49); + buttonDelTank.TabIndex = 4; + buttonDelTank.Text = "Удалить танка "; + buttonDelTank.UseVisualStyleBackColor = true; + buttonDelTank.Click += ButtonDelTank_Click; + // + // buttonGoToCheck + // + buttonGoToCheck.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonGoToCheck.Location = new Point(6, 428); + buttonGoToCheck.Name = "buttonGoToCheck"; + buttonGoToCheck.Size = new Size(206, 49); + buttonGoToCheck.TabIndex = 5; + buttonGoToCheck.Text = "Передать на тесты"; + buttonGoToCheck.UseVisualStyleBackColor = true; + buttonGoToCheck.Click += ButtonGoToCheck_Click; + // + // buttonRefresh + // + buttonRefresh.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + buttonRefresh.Location = new Point(6, 556); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(206, 49); + buttonRefresh.TabIndex = 6; + buttonRefresh.Text = "Обновить"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += ButtonRefresh_Click; + // + // FormTankCollection + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1149, 687); + Controls.Add(pictureBox); + Controls.Add(groupBoxTools); + Name = "FormTankCollection"; + Text = "Коллекция танков"; + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxTools; + private Button buttonAddTank2; + private ComboBox comboBoxSelectorCompany; + private Button buttonAddTank; + private Button buttonDelTank; + private MaskedTextBox maskedTextBox; + private PictureBox pictureBox; + private Button buttonGoToCheck; + private Button buttonRefresh; + } +} \ No newline at end of file diff --git a/ProjectTank/ProjectTank/FormTankCollection.cs b/ProjectTank/ProjectTank/FormTankCollection.cs new file mode 100644 index 0000000..51ae841 --- /dev/null +++ b/ProjectTank/ProjectTank/FormTankCollection.cs @@ -0,0 +1,190 @@ +using ProjectTank.CollectionGenericObjects; +using ProjectTank.Drawnings; +using System.Windows.Forms; + +namespace ProjectTank; + +/// +/// Форма работы с компанией и ее коллекцией +/// +public partial class FormTankCollection : Form +{ + /// + /// Компания + /// + private AbstractCompany? _company = null; + + /// + /// Конструктор + /// + public FormTankCollection() + { + InitializeComponent(); + } + + /// + /// Выбор компании + /// + /// + /// + private void ComboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxSelectorCompany.Text) + { + case "Хранилище": + _company = new TankBase(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); + break; + } + } + + /// + /// Добавление обычного автомобиля + /// + /// + /// + private void ButtonAddTank2_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTank2)); + + /// + /// Добавление спортивного автомобиля + /// + /// + /// + private void ButtonAddTank_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTank)); + + /// + /// Создание объекта класса-перемещения + /// + /// Тип создаваемого объекта + private void CreateObject(string type) + { + if (_company == null) + { + return; + } + + Random random = new(); + DrawningTank2 drawningTank2; + switch (type) + { + case nameof(DrawningTank2): + drawningTank2 = new DrawningTank2(random.Next(100, 300), random.Next(1000, 3000), GetColor(random)); + break; + case nameof(DrawningTank): + // вызов диалогового окна для выбора цвета + drawningTank2 = new DrawningTank(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 + drawningTank2 != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _company.Show(); + } + else + { + _ = MessageBox.Show(drawningTank2.ToString()); + } + } + + /// + /// Получение цвета + /// + /// Генератор случайных чисел + /// + 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 ButtonDelTank_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null) + { + return; + } + + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) + { + return; + } + + int pos = Convert.ToInt32(maskedTextBox.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; + } + + DrawningTank2? tank = null; + int counter = 100; + while (tank == null) + { + tank = _company.GetRandomObject(); + counter--; + if (counter <= 0) + { + break; + } + } + + if (tank == null) + { + return; + } + + FormTank form = new() + { + SetTank = tank + }; + form.ShowDialog(); + } + + /// + /// Перерисовка коллекции + /// + /// + /// + private void ButtonRefresh_Click(object sender, EventArgs e) + { + if (_company == null) + { + return; + } + + pictureBox.Image = _company.Show(); + } +} \ No newline at end of file diff --git a/ProjectTank/ProjectTank/FormTankCollection.resx b/ProjectTank/ProjectTank/FormTankCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ProjectTank/ProjectTank/FormTankCollection.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/ProjectTank/ProjectTank/Program.cs b/ProjectTank/ProjectTank/Program.cs index 61d6fe8..7cb3cff 100644 --- a/ProjectTank/ProjectTank/Program.cs +++ b/ProjectTank/ProjectTank/Program.cs @@ -1,9 +1,11 @@ +using System.Drawing; + namespace ProjectTank { internal static class Program { /// - /// The main entry point for the application. + /// The main entry point for the application. /// [STAThread] static void Main() @@ -11,7 +13,8 @@ namespace ProjectTank // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormTank()); + Application.Run(new FormTankCollection()); } } + } \ No newline at end of file