From 32703b5ca948372e34cd99f64f9440fa9f43ffd7 Mon Sep 17 00:00:00 2001 From: "evasina2312@gmail.com" Date: Thu, 13 Oct 2022 11:43:01 +0400 Subject: [PATCH 1/3] generic classes --- .../ProjectMachine/MapWithSetTankGeneric.cs | 178 ++++++++++++++++++ .../ProjectMachine/SetTankGeneric.cs | 82 ++++++++ 2 files changed, 260 insertions(+) create mode 100644 ProjectMachine/ProjectMachine/MapWithSetTankGeneric.cs create mode 100644 ProjectMachine/ProjectMachine/SetTankGeneric.cs diff --git a/ProjectMachine/ProjectMachine/MapWithSetTankGeneric.cs b/ProjectMachine/ProjectMachine/MapWithSetTankGeneric.cs new file mode 100644 index 0000000..7b39378 --- /dev/null +++ b/ProjectMachine/ProjectMachine/MapWithSetTankGeneric.cs @@ -0,0 +1,178 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectMachine +{ + /// + /// Карта с набром объектов под нее + /// + /// + /// + internal class MapWithSetTankGeneric + where T : class, IDrawningObject + where U : AbstractMap + { + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 210; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 90; + /// + /// Набор объектов + /// + private readonly SetTankGeneric _setTank; + /// + /// Карта + /// + private readonly U _map; + /// + /// Конструктор + /// + /// + /// + /// + public MapWithSetTankGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setTank = new SetTankGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static bool operator + (MapWithSetTankGeneric map, T car) + { + return map._setTank.Insert(car); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static bool operator -(MapWithSetTankGeneric map, int + position) + { + return map._setTank.Remove(position); + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawCars(gr); + return bmp; + } + /// + /// Просмотр объекта на карте + /// + /// + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setTank.Count; i++) + { + var car = _setTank.Get(i); + if (car != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, car); + } + } + return new(_pictureWidth, _pictureHeight); + } + /// + /// Перемещение объекта по крате + /// + /// + /// + public Bitmap MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new(_pictureWidth, _pictureHeight); + } + /// + /// "Взбалтываем" набор, чтобы все элементы оказались в начале + /// + private void Shaking() + { + int j = _setTank.Count - 1; + for (int i = 0; i < _setTank.Count; i++) + { + if (_setTank.Get(i) == null) + { + for (; j > i; j--) + { + var car = _setTank.Get(j); + if (car != null) + { + _setTank.Insert(car, i); + _setTank.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + /// + /// Метод отрисовки фона + /// + /// + 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 DrawCars(Graphics g) + { + for (int i = 0; i < _setTank.Count; i++) + { + // TODO установка позиции + _setTank.Get(i)?.DrawningObject(g); + } + } + } +} diff --git a/ProjectMachine/ProjectMachine/SetTankGeneric.cs b/ProjectMachine/ProjectMachine/SetTankGeneric.cs new file mode 100644 index 0000000..a3b382c --- /dev/null +++ b/ProjectMachine/ProjectMachine/SetTankGeneric.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectMachine +{ + /// + /// Параметризованный набор объектов + /// + /// + internal class SetTankGeneric + where T : class + { + /// + /// Массив объектов, которые храним + /// + private readonly T[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetTankGeneric(int count) + { + _places = new T[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый автомобиль + /// + public bool Insert(T car) + { + // TODO вставка в начало набора + return true; + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый автомобиль + /// Позиция + /// + public bool Insert(T car, int position) + { + // TODO проверка позиции + // TODO проверка, что элемент массива по этой позиции пустой, если нет, то + // проверка, что после вставляемого элемента в массиве есть пустой элемент + // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + // TODO вставка по позиции + _places[position] = car; + return true; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public bool Remove(int position) + { + // TODO проверка позиции + // TODO удаление объекта из массива, присовив элементу массива значение null + return true; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T Get(int position) + { + // TODO проверка позиции + return _places[position]; + } + } + +} + From ff1da657f853deab61023e1eb10f25921e4f4c6e Mon Sep 17 00:00:00 2001 From: "evasina2312@gmail.com" Date: Thu, 13 Oct 2022 13:38:37 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=84=D0=BE=D1=80=D0=BC=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProjectMachine/ProjectMachine/Direction.cs | 2 +- .../ProjectMachine/DrawningMachine.cs | 2 +- .../ProjectMachine/EntityMachine.cs | 2 +- .../ProjectMachine/FormMachine.Designer.cs | 12 ++ ProjectMachine/ProjectMachine/FormMachine.cs | 30 ++++- .../FormMapWithSetTank.Designer.cs | 39 ++++++ .../ProjectMachine/FormMapWithSetTank.cs | 20 +++ .../ProjectMachine/FormMapWithSetTank.resx | 120 ++++++++++++++++++ 8 files changed, 222 insertions(+), 5 deletions(-) create mode 100644 ProjectMachine/ProjectMachine/FormMapWithSetTank.Designer.cs create mode 100644 ProjectMachine/ProjectMachine/FormMapWithSetTank.cs create mode 100644 ProjectMachine/ProjectMachine/FormMapWithSetTank.resx diff --git a/ProjectMachine/ProjectMachine/Direction.cs b/ProjectMachine/ProjectMachine/Direction.cs index 9688957..e667584 100644 --- a/ProjectMachine/ProjectMachine/Direction.cs +++ b/ProjectMachine/ProjectMachine/Direction.cs @@ -9,7 +9,7 @@ namespace ProjectMachine /// /// Направление перемещения /// - internal enum Direction + public enum Direction { None = 0, Up = 1, diff --git a/ProjectMachine/ProjectMachine/DrawningMachine.cs b/ProjectMachine/ProjectMachine/DrawningMachine.cs index b7ab6f2..be8ed7e 100644 --- a/ProjectMachine/ProjectMachine/DrawningMachine.cs +++ b/ProjectMachine/ProjectMachine/DrawningMachine.cs @@ -9,7 +9,7 @@ namespace ProjectMachine /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// - internal class DrawningMachine + public class DrawningMachine { /// /// Класс-сущность diff --git a/ProjectMachine/ProjectMachine/EntityMachine.cs b/ProjectMachine/ProjectMachine/EntityMachine.cs index d3266a0..90e8aee 100644 --- a/ProjectMachine/ProjectMachine/EntityMachine.cs +++ b/ProjectMachine/ProjectMachine/EntityMachine.cs @@ -9,7 +9,7 @@ namespace ProjectMachine /// /// Класс-сущность "Бронированная машина" /// - internal class EntityMachine + public class EntityMachine { /// /// Скорость diff --git a/ProjectMachine/ProjectMachine/FormMachine.Designer.cs b/ProjectMachine/ProjectMachine/FormMachine.Designer.cs index f7e8390..c89e945 100644 --- a/ProjectMachine/ProjectMachine/FormMachine.Designer.cs +++ b/ProjectMachine/ProjectMachine/FormMachine.Designer.cs @@ -39,6 +39,7 @@ this.buttonUp = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); this.buttonCreateModif = new System.Windows.Forms.Button(); + this.buttonSelectTank = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxMachine)).BeginInit(); this.statusStrip1.SuspendLayout(); this.SuspendLayout(); @@ -159,11 +160,21 @@ this.buttonCreateModif.UseVisualStyleBackColor = true; this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click); // + // buttonSelectTank + // + this.buttonSelectTank.Location = new System.Drawing.Point(515, 385); + this.buttonSelectTank.Name = "buttonSelectTank"; + this.buttonSelectTank.Size = new System.Drawing.Size(94, 29); + this.buttonSelectTank.TabIndex = 8; + this.buttonSelectTank.Text = "Выбрать"; + this.buttonSelectTank.UseVisualStyleBackColor = true; + // // FormMachine // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(733, 453); + this.Controls.Add(this.buttonSelectTank); this.Controls.Add(this.buttonCreateModif); this.Controls.Add(this.buttonLeft); this.Controls.Add(this.buttonUp); @@ -195,5 +206,6 @@ private Button buttonUp; private Button buttonLeft; private Button buttonCreateModif; + private Button buttonSelectTank; } } \ No newline at end of file diff --git a/ProjectMachine/ProjectMachine/FormMachine.cs b/ProjectMachine/ProjectMachine/FormMachine.cs index 8973582..50cc88a 100644 --- a/ProjectMachine/ProjectMachine/FormMachine.cs +++ b/ProjectMachine/ProjectMachine/FormMachine.cs @@ -3,7 +3,10 @@ namespace ProjectMachine public partial class FormMachine : Form { private DrawningMachine _machine; - + /// + /// + /// + public DrawningMachine SelectedTank { get; private set; } public FormMachine() { InitializeComponent(); @@ -85,9 +88,32 @@ namespace ProjectMachine private void ButtonCreateModif_Click(object sender, EventArgs e) { Random rnd = new(); - _machine = new DrawningTank(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); + Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + Color dopColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + ColorDialog dialogDop = new(); + if (dialogDop.ShowDialog() == DialogResult.OK) + { + dopColor = dialogDop.Color; + } + _machine = new DrawningTank(rnd.Next(100, 300), rnd.Next(1000, 2000), color, dopColor, + Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); SetData(); Draw(); } + /// + /// "" + /// + /// + /// + private void ButtonSelectCar_Click(object sender, EventArgs e) + { + SelectedTank = _machine; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/ProjectMachine/ProjectMachine/FormMapWithSetTank.Designer.cs b/ProjectMachine/ProjectMachine/FormMapWithSetTank.Designer.cs new file mode 100644 index 0000000..ccdb92f --- /dev/null +++ b/ProjectMachine/ProjectMachine/FormMapWithSetTank.Designer.cs @@ -0,0 +1,39 @@ +namespace ProjectMachine +{ + partial class FormMapWithSetTank + { + /// + /// 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.components = new System.ComponentModel.Container(); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Text = "FormMapWithSetTank"; + } + + #endregion + } +} \ No newline at end of file diff --git a/ProjectMachine/ProjectMachine/FormMapWithSetTank.cs b/ProjectMachine/ProjectMachine/FormMapWithSetTank.cs new file mode 100644 index 0000000..26928fc --- /dev/null +++ b/ProjectMachine/ProjectMachine/FormMapWithSetTank.cs @@ -0,0 +1,20 @@ +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; + +namespace ProjectMachine +{ + public partial class FormMapWithSetTank : Form + { + public FormMapWithSetTank() + { + InitializeComponent(); + } + } +} diff --git a/ProjectMachine/ProjectMachine/FormMapWithSetTank.resx b/ProjectMachine/ProjectMachine/FormMapWithSetTank.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/ProjectMachine/ProjectMachine/FormMapWithSetTank.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 From 000c848d814b802bcbe1adecb82f199e27804588 Mon Sep 17 00:00:00 2001 From: "evasina2312@gmail.com" Date: Thu, 27 Oct 2022 15:46:11 +0400 Subject: [PATCH 3/3] =?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=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectMachine/FormMachine.Designer.cs | 1 + ProjectMachine/ProjectMachine/FormMachine.cs | 10 +- .../FormMapWithSetTank.Designer.cs | 191 +++++++++++++++++- .../ProjectMachine/FormMapWithSetTank.cs | 144 +++++++++++++ .../ProjectMachine/FormMapWithSetTank.resx | 62 +----- .../ProjectMachine/MapWithSetTankGeneric.cs | 45 +++-- ProjectMachine/ProjectMachine/Program.cs | 2 +- .../ProjectMachine/SetTankGeneric.cs | 65 ++++-- 8 files changed, 419 insertions(+), 101 deletions(-) diff --git a/ProjectMachine/ProjectMachine/FormMachine.Designer.cs b/ProjectMachine/ProjectMachine/FormMachine.Designer.cs index c89e945..9fcc35e 100644 --- a/ProjectMachine/ProjectMachine/FormMachine.Designer.cs +++ b/ProjectMachine/ProjectMachine/FormMachine.Designer.cs @@ -168,6 +168,7 @@ this.buttonSelectTank.TabIndex = 8; this.buttonSelectTank.Text = "Выбрать"; this.buttonSelectTank.UseVisualStyleBackColor = true; + this.buttonSelectTank.Click += new System.EventHandler(this.buttonSelectTank_Click); // // FormMachine // diff --git a/ProjectMachine/ProjectMachine/FormMachine.cs b/ProjectMachine/ProjectMachine/FormMachine.cs index 50cc88a..237b6c8 100644 --- a/ProjectMachine/ProjectMachine/FormMachine.cs +++ b/ProjectMachine/ProjectMachine/FormMachine.cs @@ -40,7 +40,13 @@ namespace ProjectMachine private void ButtonCreate_Click(object sender, EventArgs e) { Random rnd = new(); - _machine = new DrawningMachine(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); + Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + _machine = new DrawningMachine(rnd.Next(100, 300), rnd.Next(1000, 2000), color); SetData(); Draw(); } @@ -110,7 +116,7 @@ namespace ProjectMachine /// /// /// - private void ButtonSelectCar_Click(object sender, EventArgs e) + private void buttonSelectTank_Click(object sender, EventArgs e) { SelectedTank = _machine; DialogResult = DialogResult.OK; diff --git a/ProjectMachine/ProjectMachine/FormMapWithSetTank.Designer.cs b/ProjectMachine/ProjectMachine/FormMapWithSetTank.Designer.cs index ccdb92f..aaf171e 100644 --- a/ProjectMachine/ProjectMachine/FormMapWithSetTank.Designer.cs +++ b/ProjectMachine/ProjectMachine/FormMapWithSetTank.Designer.cs @@ -28,12 +28,199 @@ /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); + this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonUp = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.buttonShowOnMap = new System.Windows.Forms.Button(); + this.buttonShowStorage = new System.Windows.Forms.Button(); + this.buttonRemoveTank = new System.Windows.Forms.Button(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.buttonAddTank = new System.Windows.Forms.Button(); + this.pictureBox = new System.Windows.Forms.PictureBox(); + this.groupBoxTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.SuspendLayout(); + // + // groupBoxTools + // + this.groupBoxTools.Controls.Add(this.buttonLeft); + this.groupBoxTools.Controls.Add(this.buttonUp); + this.groupBoxTools.Controls.Add(this.buttonRight); + this.groupBoxTools.Controls.Add(this.buttonDown); + this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap); + this.groupBoxTools.Controls.Add(this.buttonShowOnMap); + this.groupBoxTools.Controls.Add(this.buttonShowStorage); + this.groupBoxTools.Controls.Add(this.buttonRemoveTank); + this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); + this.groupBoxTools.Controls.Add(this.buttonAddTank); + this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; + this.groupBoxTools.Location = new System.Drawing.Point(807, 0); + this.groupBoxTools.Name = "groupBoxTools"; + this.groupBoxTools.Size = new System.Drawing.Size(233, 546); + this.groupBoxTools.TabIndex = 0; + this.groupBoxTools.TabStop = false; + this.groupBoxTools.Text = "Инструменты"; + // + // buttonLeft + // + this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonLeft.BackgroundImage = global::ProjectMachine.Properties.Resources.лево; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonLeft.Location = new System.Drawing.Point(69, 491); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(34, 30); + this.buttonLeft.TabIndex = 13; + this.buttonLeft.Text = " \r\n\r\n"; + this.buttonLeft.UseVisualStyleBackColor = true; + this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonUp + // + this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonUp.BackgroundImage = global::ProjectMachine.Properties.Resources.вверх; + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonUp.Location = new System.Drawing.Point(105, 455); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(34, 30); + this.buttonUp.TabIndex = 12; + this.buttonUp.Text = " \r\n\r\n"; + this.buttonUp.UseVisualStyleBackColor = true; + this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonRight + // + this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRight.BackgroundImage = global::ProjectMachine.Properties.Resources.право; + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonRight.Location = new System.Drawing.Point(141, 491); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(34, 30); + this.buttonRight.TabIndex = 11; + this.buttonRight.Text = " \r\n\r\n"; + this.buttonRight.UseVisualStyleBackColor = true; + this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonDown + // + this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonDown.BackgroundImage = global::ProjectMachine.Properties.Resources.вниз; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonDown.Location = new System.Drawing.Point(105, 491); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(34, 30); + this.buttonDown.TabIndex = 10; + this.buttonDown.Text = " \r\n\r\n"; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "Простая карта", + "Город"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(26, 35); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(199, 28); + this.comboBoxSelectorMap.TabIndex = 9; + this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + // + // buttonShowOnMap + // + this.buttonShowOnMap.Location = new System.Drawing.Point(25, 379); + this.buttonShowOnMap.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonShowOnMap.Name = "buttonShowOnMap"; + this.buttonShowOnMap.Size = new System.Drawing.Size(200, 47); + this.buttonShowOnMap.TabIndex = 6; + this.buttonShowOnMap.Text = "Посмотреть карту"; + this.buttonShowOnMap.UseVisualStyleBackColor = true; + this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); + // + // buttonShowStorage + // + this.buttonShowStorage.Location = new System.Drawing.Point(25, 291); + this.buttonShowStorage.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonShowStorage.Name = "buttonShowStorage"; + this.buttonShowStorage.Size = new System.Drawing.Size(200, 47); + this.buttonShowStorage.TabIndex = 5; + this.buttonShowStorage.Text = "Посмотреть хранилище"; + this.buttonShowStorage.UseVisualStyleBackColor = true; + this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); + // + // buttonRemoveTank + // + this.buttonRemoveTank.Location = new System.Drawing.Point(25, 205); + this.buttonRemoveTank.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonRemoveTank.Name = "buttonRemoveTank"; + this.buttonRemoveTank.Size = new System.Drawing.Size(200, 47); + this.buttonRemoveTank.TabIndex = 4; + this.buttonRemoveTank.Text = "Удалить танк"; + this.buttonRemoveTank.UseVisualStyleBackColor = true; + this.buttonRemoveTank.Click += new System.EventHandler(this.ButtonRemoveTank_Click); + // + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Location = new System.Drawing.Point(26, 170); + this.maskedTextBoxPosition.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(199, 27); + this.maskedTextBoxPosition.TabIndex = 3; + this.maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonAddTank + // + this.buttonAddTank.Location = new System.Drawing.Point(25, 82); + this.buttonAddTank.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonAddTank.Name = "buttonAddTank"; + this.buttonAddTank.Size = new System.Drawing.Size(200, 47); + this.buttonAddTank.TabIndex = 2; + this.buttonAddTank.Text = "Добавить танк"; + this.buttonAddTank.UseVisualStyleBackColor = true; + this.buttonAddTank.Click += new System.EventHandler(this.ButtonAddTank_Click); + // + // pictureBox + // + this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBox.Location = new System.Drawing.Point(0, 0); + this.pictureBox.Name = "pictureBox"; + this.pictureBox.Size = new System.Drawing.Size(807, 546); + this.pictureBox.TabIndex = 1; + this.pictureBox.TabStop = false; + // + // FormMapWithSetTank + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(1040, 546); + this.Controls.Add(this.pictureBox); + this.Controls.Add(this.groupBoxTools); + this.Name = "FormMapWithSetTank"; this.Text = "FormMapWithSetTank"; + this.groupBoxTools.ResumeLayout(false); + this.groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.ResumeLayout(false); + } #endregion + + private GroupBox groupBoxTools; + private PictureBox pictureBox; + private Button buttonShowOnMap; + private Button buttonShowStorage; + private Button buttonRemoveTank; + private MaskedTextBox maskedTextBoxPosition; + private Button buttonAddTank; + private ComboBox comboBoxSelectorMap; + private Button buttonLeft; + private Button buttonUp; + private Button buttonRight; + private Button buttonDown; } } \ No newline at end of file diff --git a/ProjectMachine/ProjectMachine/FormMapWithSetTank.cs b/ProjectMachine/ProjectMachine/FormMapWithSetTank.cs index 26928fc..cb44d13 100644 --- a/ProjectMachine/ProjectMachine/FormMapWithSetTank.cs +++ b/ProjectMachine/ProjectMachine/FormMapWithSetTank.cs @@ -12,9 +12,153 @@ namespace ProjectMachine { public partial class FormMapWithSetTank : Form { + /// + /// Объект от класса карты с набором объектов + /// + private MapWithSetTankGeneric _mapTankCollectionGeneric; + /// + /// Конструктор + /// public FormMapWithSetTank() { InitializeComponent(); } + /// + /// Выбор карты + /// + /// + /// + private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + { + AbstractMap map = null; + switch (comboBoxSelectorMap.Text) + { + case "Простая карта": + map = new SimpleMap(); + break; + case "Город": + map = new TownMap(); + break; + } + if (map != null) + { + _mapTankCollectionGeneric = new MapWithSetTankGeneric( + pictureBox.Width, pictureBox.Height, map); + } + else + { + _mapTankCollectionGeneric = null; + } + } + /// + /// Добавление объекта + /// + /// + /// + private void ButtonAddTank_Click(object sender, EventArgs e) + { + if (_mapTankCollectionGeneric == null) + { + return; + } + FormMachine form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + DrawningObject tank = new(form.SelectedTank); + if ((_mapTankCollectionGeneric + tank) > -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapTankCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта + /// + /// + /// + private void ButtonRemoveTank_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); + if ((_mapTankCollectionGeneric - pos) != null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _mapTankCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Вывод набора + /// + /// + /// + private void ButtonShowStorage_Click(object sender, EventArgs e) + { + if (_mapTankCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapTankCollectionGeneric.ShowSet(); + } + /// + /// Вывод карты + /// + /// + /// + private void ButtonShowOnMap_Click(object sender, EventArgs e) + { + if (_mapTankCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapTankCollectionGeneric.ShowOnMap(); + } + /// + /// Перемещение + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_mapTankCollectionGeneric == null) + { + return; + } + //получаем имя кнопки + string name = ((Button)sender)?.Name ?? string.Empty; + Direction dir = Direction.None; + switch (name) + { + case "buttonUp": + dir = Direction.Up; + break; + case "buttonDown": + dir = Direction.Down; + break; + case "buttonLeft": + dir = Direction.Left; + break; + case "buttonRight": + dir = Direction.Right; + break; + } + pictureBox.Image = _mapTankCollectionGeneric.MoveObject(dir); + } } } + diff --git a/ProjectMachine/ProjectMachine/FormMapWithSetTank.resx b/ProjectMachine/ProjectMachine/FormMapWithSetTank.resx index 1af7de1..f298a7b 100644 --- a/ProjectMachine/ProjectMachine/FormMapWithSetTank.resx +++ b/ProjectMachine/ProjectMachine/FormMapWithSetTank.resx @@ -1,64 +1,4 @@ - - - + diff --git a/ProjectMachine/ProjectMachine/MapWithSetTankGeneric.cs b/ProjectMachine/ProjectMachine/MapWithSetTankGeneric.cs index 7b39378..c69531e 100644 --- a/ProjectMachine/ProjectMachine/MapWithSetTankGeneric.cs +++ b/ProjectMachine/ProjectMachine/MapWithSetTankGeneric.cs @@ -26,11 +26,11 @@ namespace ProjectMachine /// /// Размер занимаемого объектом места (ширина) /// - private readonly int _placeSizeWidth = 210; + private readonly int _placeSizeWidth = 200; /// /// Размер занимаемого объектом места (высота) /// - private readonly int _placeSizeHeight = 90; + private readonly int _placeSizeHeight = 70; /// /// Набор объектов /// @@ -58,11 +58,11 @@ namespace ProjectMachine /// Перегрузка оператора сложения /// /// - /// + /// /// - public static bool operator + (MapWithSetTankGeneric map, T car) + public static int operator +(MapWithSetTankGeneric map, T tank) { - return map._setTank.Insert(car); + return map._setTank.Insert(tank); } /// /// Перегрузка оператора вычитания @@ -70,8 +70,7 @@ namespace ProjectMachine /// /// /// - public static bool operator -(MapWithSetTankGeneric map, int - position) + public static T operator -(MapWithSetTankGeneric map, int position) { return map._setTank.Remove(position); } @@ -81,10 +80,11 @@ namespace ProjectMachine /// public Bitmap ShowSet() { + Shaking(); Bitmap bmp = new(_pictureWidth, _pictureHeight); Graphics gr = Graphics.FromImage(bmp); DrawBackground(gr); - DrawCars(gr); + DrawTanks(gr); return bmp; } /// @@ -96,10 +96,10 @@ namespace ProjectMachine Shaking(); for (int i = 0; i < _setTank.Count; i++) { - var car = _setTank.Get(i); - if (car != null) + var tank = _setTank.Get(i); + if (tank != null) { - return _map.CreateMap(_pictureWidth, _pictureHeight, car); + return _map.CreateMap(_pictureWidth, _pictureHeight, tank); } } return new(_pictureWidth, _pictureHeight); @@ -150,7 +150,9 @@ namespace ProjectMachine /// private void DrawBackground(Graphics g) { - Pen pen = new(Color.Black, 3); + Pen pen = new(Color.Black, 6); + Brush br = new SolidBrush(Color.Moccasin); + g.FillRectangle(br, 0, 0, _pictureWidth, _pictureHeight); for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) { for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) @@ -166,12 +168,23 @@ namespace ProjectMachine /// Метод прорисовки объектов /// /// - private void DrawCars(Graphics g) + private void DrawTanks(Graphics g) { - for (int i = 0; i < _setTank.Count; i++) + int i = 0; + int j = 0; + for (int k = 0; k < _setTank.Count; k++) { - // TODO установка позиции - _setTank.Get(i)?.DrawningObject(g); + _setTank.Get(k)?.SetObject(j + 10, i + 20, _pictureWidth, _pictureHeight); + _setTank.Get(k)?.DrawningObject(g); + if (j >= _pictureWidth - 2*_placeSizeWidth) + { + j = 0; + i += _placeSizeHeight; + } + else + { + j += _placeSizeWidth; + } } } } diff --git a/ProjectMachine/ProjectMachine/Program.cs b/ProjectMachine/ProjectMachine/Program.cs index 4d6e0f6..3db1ecc 100644 --- a/ProjectMachine/ProjectMachine/Program.cs +++ b/ProjectMachine/ProjectMachine/Program.cs @@ -11,7 +11,7 @@ namespace ProjectMachine // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormMap()); + Application.Run(new FormMapWithSetTank()); } } } \ No newline at end of file diff --git a/ProjectMachine/ProjectMachine/SetTankGeneric.cs b/ProjectMachine/ProjectMachine/SetTankGeneric.cs index a3b382c..5c1fb15 100644 --- a/ProjectMachine/ProjectMachine/SetTankGeneric.cs +++ b/ProjectMachine/ProjectMachine/SetTankGeneric.cs @@ -32,39 +32,63 @@ namespace ProjectMachine /// /// Добавление объекта в набор /// - /// Добавляемый автомобиль + /// Добавляемый танк /// - public bool Insert(T car) + public int Insert(T tank) { - // TODO вставка в начало набора - return true; + if (tank == null) + { + return -1; + } + for (int i = Count -1; i > 0; i--) + { + _places[i] = _places[i - 1]; + } + _places[0] = tank; + return 0; } /// /// Добавление объекта в набор на конкретную позицию /// - /// Добавляемый автомобиль + /// Добавляемый танк /// Позиция /// - public bool Insert(T car, int position) + public int Insert(T tank, int position) { - // TODO проверка позиции - // TODO проверка, что элемент массива по этой позиции пустой, если нет, то - // проверка, что после вставляемого элемента в массиве есть пустой элемент - // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента - // TODO вставка по позиции - _places[position] = car; - return true; + if (position < 0 || position > Count) + { + return -1; + } + int firstNullElementIndex = position; //индекс первого нулевого элемента + while(_places[firstNullElementIndex] != null) + { + if (firstNullElementIndex >= Count) + { + return -1; + } + firstNullElementIndex++; + } + for (int i = firstNullElementIndex; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = tank; + return 0; } /// /// Удаление объекта из набора с конкретной позиции /// /// /// - public bool Remove(int position) + public T Remove(int position) { - // TODO проверка позиции - // TODO удаление объекта из массива, присовив элементу массива значение null - return true; + if (_places[position] == null) + { + return null; + } + var result = _places[position]; + _places[position] = null; + return result; } /// /// Получение объекта из набора по позиции @@ -73,8 +97,11 @@ namespace ProjectMachine /// public T Get(int position) { - // TODO проверка позиции - return _places[position]; + if (_places[position] != null) + { + return _places[position]; + } + return null; } }