diff --git a/ContainerShip/ContainerShip/Direction.cs b/ContainerShip/ContainerShip/Direction.cs index d3661a5..aad9ec2 100644 --- a/ContainerShip/ContainerShip/Direction.cs +++ b/ContainerShip/ContainerShip/Direction.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ContainerShip { - internal enum Direction + public enum Direction { None = 0, Up = 1, diff --git a/ContainerShip/ContainerShip/DrawingShip.cs b/ContainerShip/ContainerShip/DrawingShip.cs index 75b76ad..bf9530e 100644 --- a/ContainerShip/ContainerShip/DrawingShip.cs +++ b/ContainerShip/ContainerShip/DrawingShip.cs @@ -7,7 +7,7 @@ using static ContainerShip.EntityShip; namespace ContainerShip { - internal class DrawingShip + public class DrawingShip { public EntityShip Ship { protected set; get; } protected float _startPosX; diff --git a/ContainerShip/ContainerShip/EntityShip.cs b/ContainerShip/ContainerShip/EntityShip.cs index df462f0..05fb955 100644 --- a/ContainerShip/ContainerShip/EntityShip.cs +++ b/ContainerShip/ContainerShip/EntityShip.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ContainerShip { - internal class EntityShip + public class EntityShip { /// /// Скорость diff --git a/ContainerShip/ContainerShip/FormMap.cs b/ContainerShip/ContainerShip/FormMap.cs deleted file mode 100644 index 0dedff7..0000000 --- a/ContainerShip/ContainerShip/FormMap.cs +++ /dev/null @@ -1,102 +0,0 @@ -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 ContainerShip -{ - public partial class FormMap : Form - { - private DrawingShip _ship; - private AbstractMap _abstractMap; - - public FormMap() - { - InitializeComponent(); - _abstractMap = new SimpleMap(); - } - /// - /// Изменение размеров формы - /// - /// - /// - private void SetData(DrawingShip ship) - { - toolStripStatusLabelSpeed.Text = $"Скорость: {ship.Ship.Speed}"; - toolStripStatusLabelWeight.Text = $"Вес: {ship.Ship.Weight}"; - toolStripStatusLabelBodyColor.Text = $"Цвет: {ship.Ship.BodyColor.Name}"; - pictureBoxShip.Image = _abstractMap.CreateMap(pictureBoxShip.Width, pictureBoxShip.Height, - new DrawingObjectShip(ship)); - } - private void ButtonCreate_Click(object sender, EventArgs e) - { - Random rnd = new(); - var ship = new DrawingShip(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); - SetData(ship); - } - /// - /// Изменение размеров формы - /// - /// - /// - private void ButtonMove_Click(object sender, EventArgs e) - { - //получаем имя кнопки - 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; - } - pictureBoxShip.Image = _abstractMap?.MoveObject(dir); - } - /// - /// Обработка нажатия кнопки "Модификация" - /// - /// - /// - private void ButtonCreateModif_Click(object sender, EventArgs e) - { - Random rnd = new(); - var ship = new DrawingContainerShip(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))); - SetData(ship); - } - /// - /// Смена карты - /// - /// - /// - - private void ComboBoxSelectorMap_SelectedIndexChanged_1(object sender, EventArgs e) - { - switch (ComboBoxSelectorMap.Text) - { - case "Простая карта": - _abstractMap = new SimpleMap(); - break; - case "Модифицированная карта": - _abstractMap = new ModifyMap(); - break; - } - } - } -} diff --git a/ContainerShip/ContainerShip/FormMap.Designer.cs b/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs similarity index 50% rename from ContainerShip/ContainerShip/FormMap.Designer.cs rename to ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs index 8cdc83f..4e94da7 100644 --- a/ContainerShip/ContainerShip/FormMap.Designer.cs +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.Designer.cs @@ -1,6 +1,6 @@ namespace ContainerShip { - partial class FormMap + partial class FormMapWithSetShips { /// /// Required designer variable. @@ -28,86 +28,97 @@ /// private void InitializeComponent() { - this.statusStrip1 = new System.Windows.Forms.StatusStrip(); - this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel(); - this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); - this.toolStripStatusLabelBodyColor = new System.Windows.Forms.ToolStripStatusLabel(); - this.buttonRight = new System.Windows.Forms.Button(); + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.ButtonAddShip = new System.Windows.Forms.Button(); + this.ComboBoxSelectorMap = new System.Windows.Forms.ComboBox(); this.buttonUp = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button(); - this.pictureBoxShip = new System.Windows.Forms.PictureBox(); - this.ButtonCreate = new System.Windows.Forms.Button(); - this.ButtonCreateModif = new System.Windows.Forms.Button(); - this.ComboBoxSelectorMap = new System.Windows.Forms.ComboBox(); - this.statusStrip1.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).BeginInit(); + this.button3 = new System.Windows.Forms.Button(); + this.ButtonShowStorage = new System.Windows.Forms.Button(); + this.ButtonRemoveShip = new System.Windows.Forms.Button(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.pictureBox = new System.Windows.Forms.PictureBox(); + this.groupBox1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // - // statusStrip1 + // groupBox1 // - this.statusStrip1.ImageScalingSize = new System.Drawing.Size(24, 24); - this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripStatusLabelSpeed, - this.toolStripStatusLabelWeight, - this.toolStripStatusLabelBodyColor}); - this.statusStrip1.Location = new System.Drawing.Point(0, 418); - this.statusStrip1.Name = "statusStrip1"; - this.statusStrip1.Size = new System.Drawing.Size(800, 32); - this.statusStrip1.TabIndex = 0; - this.statusStrip1.Text = "statusStrip1"; + this.groupBox1.Controls.Add(this.ButtonAddShip); + this.groupBox1.Controls.Add(this.ComboBoxSelectorMap); + this.groupBox1.Controls.Add(this.buttonUp); + this.groupBox1.Controls.Add(this.buttonRight); + this.groupBox1.Controls.Add(this.buttonDown); + this.groupBox1.Controls.Add(this.buttonLeft); + this.groupBox1.Controls.Add(this.button3); + this.groupBox1.Controls.Add(this.ButtonShowStorage); + this.groupBox1.Controls.Add(this.ButtonRemoveShip); + this.groupBox1.Controls.Add(this.maskedTextBoxPosition); + this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; + this.groupBox1.Location = new System.Drawing.Point(553, 0); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(300, 508); + this.groupBox1.TabIndex = 0; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Инструменты"; // - // toolStripStatusLabelSpeed + // ButtonAddShip // - this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; - this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(93, 25); - this.toolStripStatusLabelSpeed.Text = "Скорость:"; + this.ButtonAddShip.Location = new System.Drawing.Point(6, 74); + this.ButtonAddShip.Name = "ButtonAddShip"; + this.ButtonAddShip.Size = new System.Drawing.Size(288, 34); + this.ButtonAddShip.TabIndex = 11; + this.ButtonAddShip.Text = "Добавить корабль"; + this.ButtonAddShip.UseVisualStyleBackColor = true; + this.ButtonAddShip.Click += new System.EventHandler(this.ButtonAddShip_Click); // - // toolStripStatusLabelWeight + // ComboBoxSelectorMap // - this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; - this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(43, 25); - this.toolStripStatusLabelWeight.Text = "Вес:"; - // - // toolStripStatusLabelBodyColor - // - this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor"; - this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(55, 25); - this.toolStripStatusLabelBodyColor.Text = "Цвет:"; - // - // buttonRight - // - this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRight.BackgroundImage = global::ContainerShip.Properties.Resources.LeftArrow; - this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(738, 353); - this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(50, 50); - this.buttonRight.TabIndex = 2; - this.buttonRight.UseVisualStyleBackColor = true; - this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + this.ComboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.ComboBoxSelectorMap.Items.AddRange(new object[] { + "Простая карта", + "Модифицированная карта"}); + this.ComboBoxSelectorMap.Location = new System.Drawing.Point(6, 35); + this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap"; + this.ComboBoxSelectorMap.Size = new System.Drawing.Size(288, 33); + this.ComboBoxSelectorMap.TabIndex = 10; + this.ComboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); // // buttonUp // this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.BackgroundImage = global::ContainerShip.Properties.Resources.upArrow; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(682, 306); + this.buttonUp.Location = new System.Drawing.Point(131, 390); this.buttonUp.Name = "buttonUp"; this.buttonUp.Size = new System.Drawing.Size(50, 50); - this.buttonUp.TabIndex = 3; + this.buttonUp.TabIndex = 9; 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::ContainerShip.Properties.Resources.LeftArrow; + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonRight.Location = new System.Drawing.Point(187, 446); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(50, 50); + this.buttonRight.TabIndex = 8; + 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::ContainerShip.Properties.Resources.DownArrow; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(682, 352); + this.buttonDown.Location = new System.Drawing.Point(131, 446); this.buttonDown.Name = "buttonDown"; this.buttonDown.Size = new System.Drawing.Size(50, 50); - this.buttonDown.TabIndex = 4; + this.buttonDown.TabIndex = 7; this.buttonDown.UseVisualStyleBackColor = true; this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); // @@ -116,97 +127,89 @@ this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.BackgroundImage = global::ContainerShip.Properties.Resources.RightArrow; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(626, 353); + this.buttonLeft.Location = new System.Drawing.Point(75, 446); this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Size = new System.Drawing.Size(50, 50); - this.buttonLeft.TabIndex = 5; + this.buttonLeft.TabIndex = 6; this.buttonLeft.UseVisualStyleBackColor = true; this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); // - // pictureBoxShip + // button3 // - this.pictureBoxShip.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBoxShip.Location = new System.Drawing.Point(0, 0); - this.pictureBoxShip.Name = "pictureBoxShip"; - this.pictureBoxShip.Size = new System.Drawing.Size(800, 418); - this.pictureBoxShip.TabIndex = 7; - this.pictureBoxShip.TabStop = false; + this.button3.Location = new System.Drawing.Point(8, 292); + this.button3.Name = "button3"; + this.button3.Size = new System.Drawing.Size(286, 34); + this.button3.TabIndex = 3; + this.button3.Text = "Посмотреть карту"; + this.button3.UseVisualStyleBackColor = true; + this.button3.Click += new System.EventHandler(this.ButtonShowOnMap_Click); // - // ButtonCreate + // ButtonShowStorage // - this.ButtonCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.ButtonCreate.BackColor = System.Drawing.SystemColors.Window; - this.ButtonCreate.Location = new System.Drawing.Point(12, 381); - this.ButtonCreate.Name = "ButtonCreate"; - this.ButtonCreate.Size = new System.Drawing.Size(112, 34); - this.ButtonCreate.TabIndex = 8; - this.ButtonCreate.Text = "Создать"; - this.ButtonCreate.UseVisualStyleBackColor = false; - this.ButtonCreate.Click += new System.EventHandler(this.ButtonCreate_Click); + this.ButtonShowStorage.Location = new System.Drawing.Point(6, 243); + this.ButtonShowStorage.Name = "ButtonShowStorage"; + this.ButtonShowStorage.Size = new System.Drawing.Size(288, 34); + this.ButtonShowStorage.TabIndex = 2; + this.ButtonShowStorage.Text = "Просмотреть хранилище"; + this.ButtonShowStorage.UseVisualStyleBackColor = true; + this.ButtonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); // - // ButtonCreateModif + // ButtonRemoveShip // - this.ButtonCreateModif.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.ButtonCreateModif.BackColor = System.Drawing.SystemColors.Window; - this.ButtonCreateModif.Location = new System.Drawing.Point(130, 381); - this.ButtonCreateModif.Name = "ButtonCreateModif"; - this.ButtonCreateModif.Size = new System.Drawing.Size(143, 34); - this.ButtonCreateModif.TabIndex = 9; - this.ButtonCreateModif.Text = "Модификация"; - this.ButtonCreateModif.UseVisualStyleBackColor = false; - this.ButtonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click); + this.ButtonRemoveShip.Location = new System.Drawing.Point(6, 162); + this.ButtonRemoveShip.Name = "ButtonRemoveShip"; + this.ButtonRemoveShip.Size = new System.Drawing.Size(288, 41); + this.ButtonRemoveShip.TabIndex = 1; + this.ButtonRemoveShip.Text = "Удалить корабль"; + this.ButtonRemoveShip.UseVisualStyleBackColor = true; + this.ButtonRemoveShip.Click += new System.EventHandler(this.ButtonRemoveShip_Click); // - // ComboBoxSelectorMap + // maskedTextBoxPosition // - 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(279, 385); - this.ComboBoxSelectorMap.Name = "ComboBoxSelectorMap"; - this.ComboBoxSelectorMap.Size = new System.Drawing.Size(182, 33); - this.ComboBoxSelectorMap.TabIndex = 10; - this.ComboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged_1); + this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 125); + this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(288, 31); + this.maskedTextBoxPosition.TabIndex = 0; // - // FormMap + // 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(553, 508); + this.pictureBox.TabIndex = 1; + this.pictureBox.TabStop = false; + // + // FormMapWithSetShips // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Controls.Add(this.ComboBoxSelectorMap); - this.Controls.Add(this.ButtonCreateModif); - this.Controls.Add(this.ButtonCreate); - this.Controls.Add(this.buttonLeft); - this.Controls.Add(this.buttonDown); - this.Controls.Add(this.buttonUp); - this.Controls.Add(this.buttonRight); - this.Controls.Add(this.pictureBoxShip); - this.Controls.Add(this.statusStrip1); - this.Name = "FormMap"; - this.Text = "Карта"; - this.Click += new System.EventHandler(this.ButtonMove_Click); - this.statusStrip1.ResumeLayout(false); - this.statusStrip1.PerformLayout(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).EndInit(); + this.ClientSize = new System.Drawing.Size(853, 508); + this.Controls.Add(this.pictureBox); + this.Controls.Add(this.groupBox1); + this.Name = "FormMapWithSetShips"; + this.Text = "Карта с набором объектов"; + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); - this.PerformLayout(); } #endregion - private StatusStrip statusStrip1; - private Button buttonRight; - private Button buttonUp; - private Button buttonDown; + private GroupBox groupBox1; + private Button button3; + private Button ButtonShowStorage; + private Button ButtonRemoveShip; + private MaskedTextBox maskedTextBoxPosition; + private PictureBox pictureBox; private Button buttonLeft; - private ToolStripStatusLabel toolStripStatusLabelSpeed; - private ToolStripStatusLabel toolStripStatusLabelWeight; - private ToolStripStatusLabel toolStripStatusLabelBodyColor; - private PictureBox pictureBoxShip; - private Button ButtonCreate; - private Button ButtonCreateModif; + private Button buttonDown; + private Button buttonUp; + private Button buttonRight; + private Button ButtonAddShip; private ComboBox ComboBoxSelectorMap; } } \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormMapWithSetShips.cs b/ContainerShip/ContainerShip/FormMapWithSetShips.cs new file mode 100644 index 0000000..faa900b --- /dev/null +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.cs @@ -0,0 +1,158 @@ +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 static System.Windows.Forms.DataFormats; + +namespace ContainerShip +{ + public partial class FormMapWithSetShips : Form + { + /// + /// Объект от класса карты с набором объектов + /// + private MapWithSetShipsGeneric _mapShipsCollectionGeneric; + /// + /// Конструктор + /// + public FormMapWithSetShips() + { + InitializeComponent(); + } + /// + /// Добавление объекта + /// + private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + { + AbstractMap map = null; + switch (ComboBoxSelectorMap.Text) + { + case "Простая карта": + map = new SimpleMap(); + break; + case "Модифицированная карта": + map = new ModifyMap(); + break; + } + if (map != null) + { + _mapShipsCollectionGeneric = new MapWithSetShipsGeneric(pictureBox.Width, pictureBox.Height, map); + } + else +{ + _mapShipsCollectionGeneric = null; +} + } + /// + /// + private void ButtonAddShip_Click(object sender, EventArgs e) + { + if (_mapShipsCollectionGeneric == null) + { + return; + } + FormShip form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + DrawingObjectShip ship = new(form.SelectedShip); + if (_mapShipsCollectionGeneric + ship != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта + /// + /// + /// + private void ButtonRemoveShip_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 (_mapShipsCollectionGeneric - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Вывод набора + /// + /// + /// + private void ButtonShowStorage_Click(object sender, EventArgs e) + { + if (_mapShipsCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapShipsCollectionGeneric.ShowSet(); + } + /// + /// Вывод карты + /// + /// + /// + private void ButtonShowOnMap_Click(object sender, EventArgs e) + { + if (_mapShipsCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapShipsCollectionGeneric.ShowOnMap(); + } + /// + /// Перемещение + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_mapShipsCollectionGeneric == 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 = _mapShipsCollectionGeneric.MoveObject(dir); + } + } +} diff --git a/ContainerShip/ContainerShip/FormMap.resx b/ContainerShip/ContainerShip/FormMapWithSetShips.resx similarity index 93% rename from ContainerShip/ContainerShip/FormMap.resx rename to ContainerShip/ContainerShip/FormMapWithSetShips.resx index 5cb320f..f298a7b 100644 --- a/ContainerShip/ContainerShip/FormMap.resx +++ b/ContainerShip/ContainerShip/FormMapWithSetShips.resx @@ -57,7 +57,4 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - 17, 17 - \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormShip.Designer.cs b/ContainerShip/ContainerShip/FormShip.Designer.cs index 7d6bb68..b786c0b 100644 --- a/ContainerShip/ContainerShip/FormShip.Designer.cs +++ b/ContainerShip/ContainerShip/FormShip.Designer.cs @@ -32,13 +32,14 @@ this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusLabelBodyColor = new System.Windows.Forms.ToolStripStatusLabel(); - this.buttonRight = new System.Windows.Forms.Button(); - this.buttonUp = new System.Windows.Forms.Button(); - this.buttonDown = new System.Windows.Forms.Button(); - this.buttonLeft = new System.Windows.Forms.Button(); this.pictureBoxShip = new System.Windows.Forms.PictureBox(); this.ButtonCreate = new System.Windows.Forms.Button(); this.ButtonCreateModif = new System.Windows.Forms.Button(); + this.ButtonSelectShip = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); + this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonUp = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); this.statusStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxShip)).BeginInit(); this.SuspendLayout(); @@ -61,66 +62,21 @@ this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(93, 25); this.toolStripStatusLabelSpeed.Text = "Скорость:"; + this.toolStripStatusLabelSpeed.Click += new System.EventHandler(this.toolStripStatusLabelSpeed_Click); // // toolStripStatusLabelWeight // this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(43, 25); this.toolStripStatusLabelWeight.Text = "Вес:"; + this.toolStripStatusLabelWeight.Click += new System.EventHandler(this.toolStripStatusLabelWeight_Click); // // toolStripStatusLabelBodyColor // this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor"; this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(55, 25); this.toolStripStatusLabelBodyColor.Text = "Цвет:"; - // - // buttonRight - // - this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRight.BackgroundImage = global::ContainerShip.Properties.Resources.LeftArrow; - this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonRight.Location = new System.Drawing.Point(738, 353); - this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(50, 50); - this.buttonRight.TabIndex = 2; - this.buttonRight.UseVisualStyleBackColor = true; - this.buttonRight.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::ContainerShip.Properties.Resources.upArrow; - this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonUp.Location = new System.Drawing.Point(682, 306); - this.buttonUp.Name = "buttonUp"; - this.buttonUp.Size = new System.Drawing.Size(50, 50); - this.buttonUp.TabIndex = 3; - this.buttonUp.UseVisualStyleBackColor = true; - this.buttonUp.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::ContainerShip.Properties.Resources.DownArrow; - this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonDown.Location = new System.Drawing.Point(682, 352); - this.buttonDown.Name = "buttonDown"; - this.buttonDown.Size = new System.Drawing.Size(50, 50); - this.buttonDown.TabIndex = 4; - this.buttonDown.UseVisualStyleBackColor = true; - this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); - // - // buttonLeft - // - this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonLeft.BackgroundImage = global::ContainerShip.Properties.Resources.RightArrow; - this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; - this.buttonLeft.Location = new System.Drawing.Point(626, 353); - this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(50, 50); - this.buttonLeft.TabIndex = 5; - this.buttonLeft.UseVisualStyleBackColor = true; - this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); + this.toolStripStatusLabelBodyColor.Click += new System.EventHandler(this.toolStripStatusLabelBodyColor_Click); // // pictureBoxShip // @@ -130,6 +86,7 @@ this.pictureBoxShip.Size = new System.Drawing.Size(800, 418); this.pictureBoxShip.TabIndex = 7; this.pictureBoxShip.TabStop = false; + this.pictureBoxShip.Click += new System.EventHandler(this.pictureBoxShip_Click); // // ButtonCreate // @@ -155,11 +112,72 @@ this.ButtonCreateModif.UseVisualStyleBackColor = false; this.ButtonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click); // + // ButtonSelectShip + // + this.ButtonSelectShip.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.ButtonSelectShip.BackColor = System.Drawing.SystemColors.Window; + this.ButtonSelectShip.Location = new System.Drawing.Point(479, 381); + this.ButtonSelectShip.Name = "ButtonSelectShip"; + this.ButtonSelectShip.Size = new System.Drawing.Size(112, 34); + this.ButtonSelectShip.TabIndex = 10; + this.ButtonSelectShip.Text = "Выбрать"; + this.ButtonSelectShip.UseVisualStyleBackColor = false; + this.ButtonSelectShip.Click += new System.EventHandler(this.ButtonSelectShip_Click); + // + // buttonRight + // + this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRight.BackgroundImage = global::ContainerShip.Properties.Resources.LeftArrow; + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonRight.Location = new System.Drawing.Point(738, 362); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(50, 50); + this.buttonRight.TabIndex = 2; + this.buttonRight.UseVisualStyleBackColor = true; + this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonLeft + // + this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonLeft.BackgroundImage = global::ContainerShip.Properties.Resources.RightArrow; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonLeft.Location = new System.Drawing.Point(626, 362); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(50, 50); + this.buttonLeft.TabIndex = 5; + 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::ContainerShip.Properties.Resources.upArrow; + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonUp.Location = new System.Drawing.Point(682, 306); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(50, 50); + this.buttonUp.TabIndex = 3; + this.buttonUp.UseVisualStyleBackColor = true; + this.buttonUp.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::ContainerShip.Properties.Resources.DownArrow; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonDown.Location = new System.Drawing.Point(682, 362); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(50, 50); + this.buttonDown.TabIndex = 4; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + // // FormShip // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.ButtonSelectShip); this.Controls.Add(this.ButtonCreateModif); this.Controls.Add(this.ButtonCreate); this.Controls.Add(this.buttonLeft); @@ -182,15 +200,16 @@ #endregion private StatusStrip statusStrip1; - private Button buttonRight; - private Button buttonUp; - private Button buttonDown; - private Button buttonLeft; private ToolStripStatusLabel toolStripStatusLabelSpeed; private ToolStripStatusLabel toolStripStatusLabelWeight; private ToolStripStatusLabel toolStripStatusLabelBodyColor; private PictureBox pictureBoxShip; private Button ButtonCreate; private Button ButtonCreateModif; + private Button ButtonSelectShip; + private Button buttonRight; + private Button buttonLeft; + private Button buttonUp; + private Button buttonDown; } } \ No newline at end of file diff --git a/ContainerShip/ContainerShip/FormShip.cs b/ContainerShip/ContainerShip/FormShip.cs index 3fbc50b..58377a5 100644 --- a/ContainerShip/ContainerShip/FormShip.cs +++ b/ContainerShip/ContainerShip/FormShip.cs @@ -1,8 +1,14 @@ +using System.Drawing; + namespace ContainerShip { public partial class FormShip : Form { private DrawingShip _ship; + /// + /// + /// + public DrawingShip SelectedShip { get; private set; } public FormShip() { InitializeComponent(); @@ -71,25 +77,63 @@ namespace ContainerShip } private void ButtonCreate_Click(object sender, EventArgs e) { - Random rnd = new(); - _ship = new DrawingShip(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); - _ship.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxShip.Width, pictureBoxShip.Height); - toolStripStatusLabelSpeed.Text = $": {_ship.Ship.Speed}"; - toolStripStatusLabelWeight.Text = $": {_ship.Ship.Weight}"; - toolStripStatusLabelBodyColor.Text = $": {_ship.Ship.BodyColor.Name}"; - Draw(); + Random rnd = new(); + 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; + } + _ship = new DrawingShip(rnd.Next(100, 300), rnd.Next(1000, 2000), color); + SetData(); + Draw(); } private void ButtonCreateModif_Click(object sender, EventArgs e) { Random rnd = new(); - _ship = new DrawingContainerShip(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))); + 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; + } + _ship = new DrawingContainerShip(rnd.Next(100, 300), rnd.Next(1000, 2000), color, dopColor, + Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); SetData(); Draw(); } + private void ButtonSelectShip_Click(object sender, EventArgs e) + { + SelectedShip = _ship; + DialogResult = DialogResult.OK; + } + + private void pictureBoxShip_Click(object sender, EventArgs e) + { + + } + + private void toolStripStatusLabelBodyColor_Click(object sender, EventArgs e) + { + + } + + private void toolStripStatusLabelWeight_Click(object sender, EventArgs e) + { + + } + + private void toolStripStatusLabelSpeed_Click(object sender, EventArgs e) + { + + } } } \ No newline at end of file diff --git a/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs new file mode 100644 index 0000000..dfda657 --- /dev/null +++ b/ContainerShip/ContainerShip/MapWithSetShipsGeneric.cs @@ -0,0 +1,173 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ContainerShip +{ + internal class MapWithSetShipsGeneric + where T : class, IDrawingObject + where U : AbstractMap + { + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 135; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 70; + /// + /// Набор объектов + /// + private readonly SetShipsGeneric _setShips; + /// + /// Карта + /// + private readonly U _map; + /// + /// Конструктор + /// + /// + /// + /// + public MapWithSetShipsGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setShips = new SetShipsGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawShips(gr); + return bmp; + } + /// + /// Просмотр объекта на карте + /// + /// + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setShips.Count; i++) + { + var ship = _setShips.Get(i); + if (ship != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, ship); + } + } + 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 = _setShips.Count - 1; + for (int i = 0; i < _setShips.Count; i++) + { + if (_setShips.Get(i) == null) + { + for (; j > i; j--) + { + var ship = _setShips.Get(j); + if (ship != null) + { + _setShips.Insert(ship, i); + _setShips.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Brown, 3); + g.FillRectangle(Brushes.Aqua, 0, 0, _pictureWidth, _pictureHeight); + 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 DrawShips(Graphics g) + { + for (int i = 0; i < _setShips.Count; i++) + { + int countOfShipsInLine = _pictureWidth / _placeSizeWidth; + int countOfShipsInColumn = _pictureHeight / _placeSizeHeight; + _setShips.Get(i)?.SetObject((countOfShipsInLine - (i % countOfShipsInLine) - 1) * _placeSizeWidth, (countOfShipsInColumn - (i / countOfShipsInLine) - 1) * _placeSizeHeight, _pictureWidth, _pictureHeight); + _setShips.Get(i)?.DrawingObject(g); + } + } + /// + /// Перегрузка операторов + /// + /// + /// + /// + public static int operator +(MapWithSetShipsGeneric map, T ship) + { + return map._setShips.Insert(ship); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static T operator -(MapWithSetShipsGeneric map, int position) + { + return map._setShips.Remove(position); + } + } +} diff --git a/ContainerShip/ContainerShip/Program.cs b/ContainerShip/ContainerShip/Program.cs index 8f4b98f..d579063 100644 --- a/ContainerShip/ContainerShip/Program.cs +++ b/ContainerShip/ContainerShip/Program.cs @@ -11,7 +11,7 @@ namespace ContainerShip // 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 FormMapWithSetShips()); } } } \ No newline at end of file diff --git a/ContainerShip/ContainerShip/SetShipsGeneric.cs b/ContainerShip/ContainerShip/SetShipsGeneric.cs new file mode 100644 index 0000000..883942d --- /dev/null +++ b/ContainerShip/ContainerShip/SetShipsGeneric.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ContainerShip +{ + internal class SetShipsGeneric + where T: class + { + /// + /// Массив объектов, которые храним + /// + private readonly T[] _places; + /// + /// Количество объектов в массиве + /// + public int Count => _places.Length; + /// + /// Конструктор + /// + /// + public SetShipsGeneric(int count) + { + _places = new T[count]; + } + /// + /// Добавление объекта в набор + /// + /// Добавляемый автомобиль + /// + public int Insert(T ship) + { + return Insert(ship, 0); + } + /// + /// Добавление объекта в набор на конкретную позицию + /// + /// Добавляемый корабль + /// Позиция + /// + public int Insert(T ship, int position) + { + if (position < 0 || position >= _places.Length) + { + return -1; + } + if (_places[position] == null) + { + + _places[position] = ship; + } + else + { + int freePlace = -1; + for (int i = position; i < _places.Length; i++) + { + if (_places[i] == null) + { + freePlace = i; + } + } + if (freePlace == -1) + { + return -1; + } + else + { + for (int i = freePlace; i > position; i--) + { + T buf = _places[i]; + _places[i] = _places[i - 1]; + _places[i - 1] = buf; + } + _places[position] = ship; + } + } + return position; + } + /// + /// Удаление объекта из набора с конкретной позиции + /// + /// + /// + public T Remove(int position) + { + if (position < 0 || position >= _places.Length) + { + return null; + } + T ship = _places[position]; + _places[position] = null; + return ship; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T Get(int position) + { + if (position < 0 || position >= _places.Length) + { + return null; + } + return _places[position]; + } + } +}