diff --git a/Sailboat/AbstractMap.cs b/Sailboat/AbstractMap.cs index 9ab4222..a233f7a 100644 --- a/Sailboat/AbstractMap.cs +++ b/Sailboat/AbstractMap.cs @@ -9,7 +9,7 @@ namespace Sailboat { internal abstract class AbstractMap { - IDrawingObject _drawingObject = null; + IDrawingObject _drawingObject = null; protected int[,] _map = null; //ширина и высота карты protected int _width; diff --git a/Sailboat/Boat.cs b/Sailboat/Boat.cs index e701e25..d43f3a7 100644 --- a/Sailboat/Boat.cs +++ b/Sailboat/Boat.cs @@ -3,7 +3,7 @@ using System.Drawing; namespace Sailboat { - internal class Boat + public class Boat { /// /// Скорость diff --git a/Sailboat/BoatForm.Designer.cs b/Sailboat/BoatForm.Designer.cs index c3c8ccd..147ccb2 100644 --- a/Sailboat/BoatForm.Designer.cs +++ b/Sailboat/BoatForm.Designer.cs @@ -40,6 +40,7 @@ namespace Sailboat this.btn_up = new System.Windows.Forms.Button(); this.btn_left = new System.Windows.Forms.Button(); this.btn_create_sailboat = new System.Windows.Forms.Button(); + this.btn_select = new System.Windows.Forms.Button(); this.statusStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBoat)).BeginInit(); this.SuspendLayout(); @@ -155,11 +156,22 @@ namespace Sailboat this.btn_create_sailboat.UseVisualStyleBackColor = true; this.btn_create_sailboat.Click += new System.EventHandler(this.btn_create_sailboat_Click); // + // btn_select + // + this.btn_select.Location = new System.Drawing.Point(13, 391); + this.btn_select.Name = "btn_select"; + this.btn_select.Size = new System.Drawing.Size(94, 29); + this.btn_select.TabIndex = 9; + this.btn_select.Text = "Выбрать"; + this.btn_select.UseVisualStyleBackColor = true; + this.btn_select.Click += new System.EventHandler(this.btn_select_Click); + // // BoatForm // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.btn_select); this.Controls.Add(this.btn_create_sailboat); this.Controls.Add(this.btn_left); this.Controls.Add(this.btn_up); @@ -193,6 +205,7 @@ namespace Sailboat private System.Windows.Forms.Button btn_up; private System.Windows.Forms.Button btn_left; private System.Windows.Forms.Button btn_create_sailboat; + private System.Windows.Forms.Button btn_select; } } diff --git a/Sailboat/BoatForm.cs b/Sailboat/BoatForm.cs index cbda23c..4bac71c 100644 --- a/Sailboat/BoatForm.cs +++ b/Sailboat/BoatForm.cs @@ -13,6 +13,10 @@ namespace Sailboat public partial class BoatForm : Form { private DrawingBoat _boat; + /// + /// Выбранный объект + /// + public DrawingBoat SelectedBoat { get; private set; } public BoatForm() { InitializeComponent(); @@ -94,5 +98,11 @@ namespace Sailboat toolStripLabel_weight.Text = $"Вес: {_boat.Boat.Weight}"; toolStripLabel_color.Text = $"Цвет: { _boat.Boat.BodyColor.Name}"; } + + private void btn_select_Click(object sender, EventArgs e) + { + SelectedBoat = _boat; + DialogResult = DialogResult.OK; + } } } diff --git a/Sailboat/Direction.cs b/Sailboat/Direction.cs index 4a1f592..bbcc4ed 100644 --- a/Sailboat/Direction.cs +++ b/Sailboat/Direction.cs @@ -9,7 +9,7 @@ namespace Sailboat /// /// Направление перемещения /// - internal enum Direction + public enum Direction { None = 0, Up = 1, diff --git a/Sailboat/DrawingBoat.cs b/Sailboat/DrawingBoat.cs index 1e8ee12..353e8ee 100644 --- a/Sailboat/DrawingBoat.cs +++ b/Sailboat/DrawingBoat.cs @@ -10,7 +10,7 @@ namespace Sailboat /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// - internal class DrawingBoat + public class DrawingBoat { /// /// Класс-сущность diff --git a/Sailboat/FormMap.Designer.cs b/Sailboat/FormMap.Designer.cs deleted file mode 100644 index 6554cf1..0000000 --- a/Sailboat/FormMap.Designer.cs +++ /dev/null @@ -1,211 +0,0 @@ - -namespace Sailboat -{ - partial class FormMap - { - /// - /// 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.statusStrip1 = new System.Windows.Forms.StatusStrip(); - this.toolStripLabel_speed = new System.Windows.Forms.ToolStripStatusLabel(); - this.toolStripLabel_weight = new System.Windows.Forms.ToolStripStatusLabel(); - this.toolStripLabel_color = new System.Windows.Forms.ToolStripStatusLabel(); - this.pictureBoxBoat = new System.Windows.Forms.PictureBox(); - this.btn_create = new System.Windows.Forms.Button(); - this.btn_right = new System.Windows.Forms.Button(); - this.btn_down = new System.Windows.Forms.Button(); - this.btn_up = new System.Windows.Forms.Button(); - this.btn_left = new System.Windows.Forms.Button(); - this.btn_create_sailboat = new System.Windows.Forms.Button(); - this.comboBoxMapSelector = new System.Windows.Forms.ComboBox(); - this.statusStrip1.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBoat)).BeginInit(); - this.SuspendLayout(); - // - // statusStrip1 - // - this.statusStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); - this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripLabel_speed, - this.toolStripLabel_weight, - this.toolStripLabel_color}); - this.statusStrip1.Location = new System.Drawing.Point(0, 424); - this.statusStrip1.Name = "statusStrip1"; - this.statusStrip1.Size = new System.Drawing.Size(800, 26); - this.statusStrip1.TabIndex = 0; - this.statusStrip1.Text = "statusStrip1"; - // - // toolStripLabel_speed - // - this.toolStripLabel_speed.Name = "toolStripLabel_speed"; - this.toolStripLabel_speed.Size = new System.Drawing.Size(76, 20); - this.toolStripLabel_speed.Text = "Скорость:"; - // - // toolStripLabel_weight - // - this.toolStripLabel_weight.Name = "toolStripLabel_weight"; - this.toolStripLabel_weight.Size = new System.Drawing.Size(36, 20); - this.toolStripLabel_weight.Text = "Вес:"; - // - // toolStripLabel_color - // - this.toolStripLabel_color.Name = "toolStripLabel_color"; - this.toolStripLabel_color.Size = new System.Drawing.Size(45, 20); - this.toolStripLabel_color.Text = "Цвет:"; - // - // pictureBoxBoat - // - this.pictureBoxBoat.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBoxBoat.Location = new System.Drawing.Point(0, 0); - this.pictureBoxBoat.Name = "pictureBoxBoat"; - this.pictureBoxBoat.Size = new System.Drawing.Size(800, 424); - this.pictureBoxBoat.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; - this.pictureBoxBoat.TabIndex = 1; - this.pictureBoxBoat.TabStop = false; - this.pictureBoxBoat.Resize += new System.EventHandler(this.btn_move_Click); - // - // btn_create - // - this.btn_create.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.btn_create.Location = new System.Drawing.Point(13, 13); - this.btn_create.Name = "btn_create"; - this.btn_create.Size = new System.Drawing.Size(94, 29); - this.btn_create.TabIndex = 2; - this.btn_create.Text = "Создать"; - this.btn_create.UseVisualStyleBackColor = true; - this.btn_create.Click += new System.EventHandler(this.btn_create_Click); - // - // btn_right - // - this.btn_right.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.btn_right.BackgroundImage = global::Sailboat.Properties.Resources.right; - this.btn_right.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.btn_right.Location = new System.Drawing.Point(694, 392); - this.btn_right.Name = "btn_right"; - this.btn_right.Size = new System.Drawing.Size(94, 29); - this.btn_right.TabIndex = 4; - this.btn_right.UseVisualStyleBackColor = true; - this.btn_right.Click += new System.EventHandler(this.btn_move_Click); - // - // btn_down - // - this.btn_down.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.btn_down.BackgroundImage = global::Sailboat.Properties.Resources.down; - this.btn_down.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.btn_down.Location = new System.Drawing.Point(594, 392); - this.btn_down.Name = "btn_down"; - this.btn_down.Size = new System.Drawing.Size(94, 29); - this.btn_down.TabIndex = 5; - this.btn_down.UseVisualStyleBackColor = true; - this.btn_down.Click += new System.EventHandler(this.btn_move_Click); - // - // btn_up - // - this.btn_up.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.btn_up.BackgroundImage = global::Sailboat.Properties.Resources.up; - this.btn_up.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.btn_up.Location = new System.Drawing.Point(594, 357); - this.btn_up.Name = "btn_up"; - this.btn_up.Size = new System.Drawing.Size(94, 29); - this.btn_up.TabIndex = 6; - this.btn_up.UseVisualStyleBackColor = true; - this.btn_up.Click += new System.EventHandler(this.btn_move_Click); - // - // btn_left - // - this.btn_left.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.btn_left.BackgroundImage = global::Sailboat.Properties.Resources.left; - this.btn_left.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.btn_left.Location = new System.Drawing.Point(494, 391); - this.btn_left.Name = "btn_left"; - this.btn_left.Size = new System.Drawing.Size(94, 29); - this.btn_left.TabIndex = 7; - this.btn_left.UseVisualStyleBackColor = true; - this.btn_left.Click += new System.EventHandler(this.btn_move_Click); - // - // btn_create_sailboat - // - this.btn_create_sailboat.Location = new System.Drawing.Point(113, 13); - this.btn_create_sailboat.Name = "btn_create_sailboat"; - this.btn_create_sailboat.Size = new System.Drawing.Size(150, 29); - this.btn_create_sailboat.TabIndex = 8; - this.btn_create_sailboat.Text = "Создать парусник"; - this.btn_create_sailboat.UseVisualStyleBackColor = true; - this.btn_create_sailboat.Click += new System.EventHandler(this.btn_create_sailboat_Click); - // - // comboBoxMapSelector - // - this.comboBoxMapSelector.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.comboBoxMapSelector.FormattingEnabled = true; - this.comboBoxMapSelector.Items.AddRange(new object[] { - "Простая карта", - "Водная карта"}); - this.comboBoxMapSelector.Location = new System.Drawing.Point(269, 14); - this.comboBoxMapSelector.Name = "comboBoxMapSelector"; - this.comboBoxMapSelector.Size = new System.Drawing.Size(151, 28); - this.comboBoxMapSelector.TabIndex = 9; - this.comboBoxMapSelector.SelectedIndexChanged += new System.EventHandler(this.comboBoxMapSelector_SelectedIndexChanged); - // - // FormMap - // - this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Controls.Add(this.comboBoxMapSelector); - this.Controls.Add(this.btn_create_sailboat); - this.Controls.Add(this.btn_left); - this.Controls.Add(this.btn_up); - this.Controls.Add(this.btn_down); - this.Controls.Add(this.btn_right); - this.Controls.Add(this.btn_create); - this.Controls.Add(this.pictureBoxBoat); - this.Controls.Add(this.statusStrip1); - this.Name = "FormMap"; - this.Click += new System.EventHandler(this.btn_move_Click); - this.statusStrip1.ResumeLayout(false); - this.statusStrip1.PerformLayout(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxBoat)).EndInit(); - this.ResumeLayout(false); - this.PerformLayout(); - - } - - #endregion - - private System.Windows.Forms.StatusStrip statusStrip1; - private System.Windows.Forms.ToolStripStatusLabel toolStripLabel_speed; - private System.Windows.Forms.ToolStripStatusLabel toolStripLabel_weight; - private System.Windows.Forms.ToolStripStatusLabel toolStripLabel_color; - private System.Windows.Forms.PictureBox pictureBoxBoat; - private System.Windows.Forms.Button btn_create; - private System.Windows.Forms.Button btn_right; - private System.Windows.Forms.Button btn_down; - private System.Windows.Forms.Button btn_up; - private System.Windows.Forms.Button btn_left; - private System.Windows.Forms.Button btn_create_sailboat; - private System.Windows.Forms.ComboBox comboBoxMapSelector; - } -} \ No newline at end of file diff --git a/Sailboat/FormMap.cs b/Sailboat/FormMap.cs deleted file mode 100644 index 0fa666f..0000000 --- a/Sailboat/FormMap.cs +++ /dev/null @@ -1,99 +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 Sailboat -{ - public partial class FormMap : Form - { - private AbstractMap _abstractMap; - public FormMap() - { - InitializeComponent(); - _abstractMap = new SimpleMap(); - } - /// - /// Обработка нажатия кнопки "Создать" - /// - /// - /// - private void btn_create_Click(object sender, EventArgs e) - { - Random rnd = new(); - var boat = new DrawingBoat(rnd.Next(100, 300), rnd.Next(1000, 2000), - Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); - SetData(boat); - } - /// - /// Обработка нажатия кнопки "Создать парусник" - /// - /// - /// - private void btn_create_sailboat_Click(object sender, EventArgs e) - { - Random rnd = new(); - var boat = new DrawingSailboat(rnd.Next(100, 300), rnd.Next(1000, 2000), - 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(boat); - } - /// - /// Обработка нажатия стрелок - /// - /// - /// - private void btn_move_Click(object sender, EventArgs e) - { - //получаем имя кнопки - string name = ((Button)sender)?.Name ?? string.Empty; - Direction dir = Direction.None; - switch (name) - { - case "btn_up": - dir = Direction.Up; - break; - case "btn_down": - dir = Direction.Down; - break; - case "btn_left": - dir = Direction.Left; - break; - case "btn_right": - dir = Direction.Right; - break; - } - pictureBoxBoat.Image = _abstractMap?.MoveObject(dir); - } - /// - /// Метод прорисовки машины - /// - private void SetData(DrawingBoat boat) - { - Random rnd = new(); - toolStripLabel_color.Text = $"Скорость: {boat?.Boat.Speed}"; - toolStripLabel_weight.Text = $"Вес: {boat?.Boat.Weight}"; - toolStripLabel_color.Text = $"Цвет: { boat?.Boat.BodyColor.Name}"; - pictureBoxBoat.Image = _abstractMap.CreateMap(pictureBoxBoat.Width, pictureBoxBoat.Height, - new DrawingObjectBoat(boat)); - } - - private void comboBoxMapSelector_SelectedIndexChanged(object sender, EventArgs e) - { - switch (comboBoxMapSelector.Text) - { - case "Простая карта": - _abstractMap = new SimpleMap(); - break; - case "Водная карта": - _abstractMap = new WaterMap(); - break; - } - } - } -} diff --git a/Sailboat/FormMapWithSetBoats.Designer.cs b/Sailboat/FormMapWithSetBoats.Designer.cs new file mode 100644 index 0000000..9aaacf6 --- /dev/null +++ b/Sailboat/FormMapWithSetBoats.Designer.cs @@ -0,0 +1,216 @@ + +namespace Sailboat +{ + partial class FormMapWithSetBoats + { + /// + /// 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.groupBox1 = new System.Windows.Forms.GroupBox(); + this.btn_left = new System.Windows.Forms.Button(); + this.btn_up = new System.Windows.Forms.Button(); + this.btn_down = new System.Windows.Forms.Button(); + this.btn_right = new System.Windows.Forms.Button(); + this.btn_show_map = new System.Windows.Forms.Button(); + this.btn_show_storage = new System.Windows.Forms.Button(); + this.btn_remove_boat = new System.Windows.Forms.Button(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.btn_add_boat = new System.Windows.Forms.Button(); + this.comboBoxMapSelector = new System.Windows.Forms.ComboBox(); + this.pictureBox = new System.Windows.Forms.PictureBox(); + this.groupBox1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.SuspendLayout(); + // + // groupBox1 + // + this.groupBox1.Controls.Add(this.btn_left); + this.groupBox1.Controls.Add(this.btn_up); + this.groupBox1.Controls.Add(this.btn_down); + this.groupBox1.Controls.Add(this.btn_right); + this.groupBox1.Controls.Add(this.btn_show_map); + this.groupBox1.Controls.Add(this.btn_show_storage); + this.groupBox1.Controls.Add(this.btn_remove_boat); + this.groupBox1.Controls.Add(this.maskedTextBoxPosition); + this.groupBox1.Controls.Add(this.btn_add_boat); + this.groupBox1.Controls.Add(this.comboBoxMapSelector); + this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; + this.groupBox1.Location = new System.Drawing.Point(704, 0); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(250, 501); + this.groupBox1.TabIndex = 0; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Инструменты"; + // + // btn_left + // + this.btn_left.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.btn_left.BackgroundImage = global::Sailboat.Properties.Resources.left; + this.btn_left.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.btn_left.Location = new System.Drawing.Point(19, 444); + this.btn_left.Name = "btn_left"; + this.btn_left.Size = new System.Drawing.Size(70, 40); + this.btn_left.TabIndex = 11; + this.btn_left.UseVisualStyleBackColor = true; + this.btn_left.Click += new System.EventHandler(this.btn_move_Click); + // + // btn_up + // + this.btn_up.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.btn_up.BackgroundImage = global::Sailboat.Properties.Resources.up; + this.btn_up.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.btn_up.Location = new System.Drawing.Point(95, 398); + this.btn_up.Name = "btn_up"; + this.btn_up.Size = new System.Drawing.Size(75, 40); + this.btn_up.TabIndex = 10; + this.btn_up.UseVisualStyleBackColor = true; + this.btn_up.Click += new System.EventHandler(this.btn_move_Click); + // + // btn_down + // + this.btn_down.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.btn_down.BackgroundImage = global::Sailboat.Properties.Resources.down; + this.btn_down.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.btn_down.Location = new System.Drawing.Point(95, 444); + this.btn_down.Name = "btn_down"; + this.btn_down.Size = new System.Drawing.Size(75, 40); + this.btn_down.TabIndex = 9; + this.btn_down.UseVisualStyleBackColor = true; + this.btn_down.Click += new System.EventHandler(this.btn_move_Click); + // + // btn_right + // + this.btn_right.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.btn_right.BackgroundImage = global::Sailboat.Properties.Resources.right; + this.btn_right.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.btn_right.Location = new System.Drawing.Point(174, 444); + this.btn_right.Name = "btn_right"; + this.btn_right.Size = new System.Drawing.Size(70, 40); + this.btn_right.TabIndex = 8; + this.btn_right.UseVisualStyleBackColor = true; + this.btn_right.Click += new System.EventHandler(this.btn_move_Click); + // + // btn_show_map + // + this.btn_show_map.Location = new System.Drawing.Point(6, 339); + this.btn_show_map.Name = "btn_show_map"; + this.btn_show_map.Size = new System.Drawing.Size(232, 29); + this.btn_show_map.TabIndex = 6; + this.btn_show_map.Text = "Посмотреть карту"; + this.btn_show_map.UseVisualStyleBackColor = true; + this.btn_show_map.Click += new System.EventHandler(this.btn_show_map_Click); + // + // btn_show_storage + // + this.btn_show_storage.Location = new System.Drawing.Point(6, 284); + this.btn_show_storage.Name = "btn_show_storage"; + this.btn_show_storage.Size = new System.Drawing.Size(232, 29); + this.btn_show_storage.TabIndex = 5; + this.btn_show_storage.Text = "Посмотреть хранилище"; + this.btn_show_storage.UseVisualStyleBackColor = true; + this.btn_show_storage.Click += new System.EventHandler(this.btn_show_storage_Click); + // + // btn_remove_boat + // + this.btn_remove_boat.Location = new System.Drawing.Point(6, 184); + this.btn_remove_boat.Name = "btn_remove_boat"; + this.btn_remove_boat.Size = new System.Drawing.Size(232, 29); + this.btn_remove_boat.TabIndex = 4; + this.btn_remove_boat.Text = "Удалить лодку"; + this.btn_remove_boat.UseVisualStyleBackColor = true; + this.btn_remove_boat.Click += new System.EventHandler(this.btn_remove_boat_Click); + // + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 151); + this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(232, 27); + this.maskedTextBoxPosition.TabIndex = 3; + // + // btn_add_boat + // + this.btn_add_boat.Location = new System.Drawing.Point(6, 89); + this.btn_add_boat.Name = "btn_add_boat"; + this.btn_add_boat.Size = new System.Drawing.Size(232, 29); + this.btn_add_boat.TabIndex = 2; + this.btn_add_boat.Text = "Добавить лодку"; + this.btn_add_boat.UseVisualStyleBackColor = true; + this.btn_add_boat.Click += new System.EventHandler(this.btn_add_boat_Click); + // + // comboBoxMapSelector + // + this.comboBoxMapSelector.FormattingEnabled = true; + this.comboBoxMapSelector.Items.AddRange(new object[] { + "Простая карта", + "Водная карта"}); + this.comboBoxMapSelector.Location = new System.Drawing.Point(6, 26); + this.comboBoxMapSelector.Name = "comboBoxMapSelector"; + this.comboBoxMapSelector.Size = new System.Drawing.Size(232, 28); + this.comboBoxMapSelector.TabIndex = 1; + this.comboBoxMapSelector.SelectedIndexChanged += new System.EventHandler(this.comboBoxMapSelector_SelectedIndexChanged); + // + // 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(704, 501); + this.pictureBox.TabIndex = 0; + this.pictureBox.TabStop = false; + // + // FormMapWithSetBoats + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(954, 501); + this.Controls.Add(this.pictureBox); + this.Controls.Add(this.groupBox1); + this.Name = "FormMapWithSetBoats"; + this.Text = "FormMapWithSetBoats"; + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.Button btn_show_map; + private System.Windows.Forms.Button btn_show_storage; + private System.Windows.Forms.Button btn_remove_boat; + private System.Windows.Forms.MaskedTextBox maskedTextBoxPosition; + private System.Windows.Forms.Button btn_add_boat; + private System.Windows.Forms.ComboBox comboBoxMapSelector; + private System.Windows.Forms.PictureBox pictureBox; + private System.Windows.Forms.Button btn_left; + private System.Windows.Forms.Button btn_up; + private System.Windows.Forms.Button btn_down; + private System.Windows.Forms.Button btn_right; + } +} \ No newline at end of file diff --git a/Sailboat/FormMapWithSetBoats.cs b/Sailboat/FormMapWithSetBoats.cs new file mode 100644 index 0000000..ff45ed5 --- /dev/null +++ b/Sailboat/FormMapWithSetBoats.cs @@ -0,0 +1,144 @@ +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 Sailboat +{ + public partial class FormMapWithSetBoats : Form + { + private MapWithSetBoatsGeneric _mapBoatsCollectionGeneric; + + public FormMapWithSetBoats() + { + InitializeComponent(); + AbstractMap map = new SimpleMap(); + _mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric( + pictureBox.Width, pictureBox.Height, map); + } + + private void comboBoxMapSelector_SelectedIndexChanged(object sender, EventArgs e) + { + AbstractMap map = null; + switch (comboBoxMapSelector.Text) + { + case "Простая карта": + map = new SimpleMap(); + break; + case "Водная карта": + map = new WaterMap(); + break; + } + if (map != null) + { + _mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric( + pictureBox.Width, pictureBox.Height, map); + } + else + { + _mapBoatsCollectionGeneric = null; + } + } + + private void btn_add_boat_Click(object sender, EventArgs e) + { + if (_mapBoatsCollectionGeneric == null) + { + return; + } + BoatForm form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (form.SelectedBoat == null) + { + MessageBox.Show("Вы не создали объект"); + return; + } + + DrawingObjectBoat bus = new(form.SelectedBoat); + + if (_mapBoatsCollectionGeneric + bus != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + + private void btn_remove_boat_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 (_mapBoatsCollectionGeneric - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void btn_show_storage_Click(object sender, EventArgs e) + { + if (_mapBoatsCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet(); + } + + private void btn_show_map_Click(object sender, EventArgs e) + { + if (_mapBoatsCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapBoatsCollectionGeneric.ShowOnMap(); + } + + private void btn_move_Click(object sender, EventArgs e) + { + if (_mapBoatsCollectionGeneric == null) + { + return; + } + + string name = ((Button)sender)?.Name ?? string.Empty; + Direction dir = Direction.None; + switch (name) + { + case "btn_up": + dir = Direction.Up; + break; + case "btn_down": + dir = Direction.Down; + break; + case "btn_left": + dir = Direction.Left; + break; + case "btn_right": + dir = Direction.Right; + break; + } + pictureBox.Image = _mapBoatsCollectionGeneric.MoveObject(dir); + } + } +} diff --git a/Sailboat/FormMap.resx b/Sailboat/FormMapWithSetBoats.resx similarity index 93% rename from Sailboat/FormMap.resx rename to Sailboat/FormMapWithSetBoats.resx index 5cb320f..f298a7b 100644 --- a/Sailboat/FormMap.resx +++ b/Sailboat/FormMapWithSetBoats.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/Sailboat/MapWithSetBoatsGeneric.cs b/Sailboat/MapWithSetBoatsGeneric.cs new file mode 100644 index 0000000..23bcf57 --- /dev/null +++ b/Sailboat/MapWithSetBoatsGeneric.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat +{ + class MapWithSetBoatsGeneric where T : class, IDrawingObject + where U : AbstractMap + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 210; + private readonly int _placeSizeHeight = 90; + private readonly SetBoatsGeneric _setBoats; + private readonly U _map; + + public MapWithSetBoatsGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setBoats = new SetBoatsGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public static int operator +(MapWithSetBoatsGeneric map, T boat) + { + return map._setBoats.Insert(boat); + } + + public static T operator -(MapWithSetBoatsGeneric map, int position) + { + return map._setBoats.Remove(position); + } + + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawBoats(gr); + return bmp; + } + + public Bitmap ShowOnMap() + { + Shaking(); + foreach (var boat in _setBoats.GetBoats()) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, boat); + } + 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 = _setBoats.Count - 1; + for (int i = 0; i < _setBoats.Count; i++) + { + if (_setBoats.Get(i) == null) + { + for (; j > i; j--) + { + var boat = _setBoats.Get(j); + if (boat != null) + { + _setBoats.Insert(boat, i); + _setBoats.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 DrawBoats(Graphics g) + { + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + + int currentWidth = 0; + int currentHeight = 0; + + foreach (var machine in _setBoats.GetBoats()) + { + int dop = 0; + if (currentWidth > 0 && currentHeight > 0) + { + dop = 30; + } + + machine.SetObject(currentWidth * _placeSizeWidth + dop, + currentHeight * _placeSizeHeight + dop, + _pictureWidth, _pictureHeight); + machine?.DrawingObject(g); + if (currentWidth < width - 1) + { + currentWidth++; + } + else + { + currentWidth = 0; + currentHeight++; + } + if (currentHeight > height) return; + } + } + } +} diff --git a/Sailboat/Program.cs b/Sailboat/Program.cs index 76ebb69..be522c6 100644 --- a/Sailboat/Program.cs +++ b/Sailboat/Program.cs @@ -17,7 +17,7 @@ namespace Sailboat Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new FormMap()); + Application.Run(new FormMapWithSetBoats()); } } } diff --git a/Sailboat/SetBoatsGeneric.cs b/Sailboat/SetBoatsGeneric.cs new file mode 100644 index 0000000..e35c492 --- /dev/null +++ b/Sailboat/SetBoatsGeneric.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat +{ + class SetBoatsGeneric where T : class + { + private readonly List _places; + private readonly int _maxCount; + + public int Count => _places.Count; + private int BusyPlaces = 0; + + public SetBoatsGeneric(int count) + { + _maxCount = count; + _places = new List(); + } + + public int Insert(T boat) + { + if (Count + 1 <= _maxCount) return Insert(boat, 0); + else return -1; + } + + public int Insert(T boat, int position) + { + if (position >= _maxCount && position < 0) + { + return -1; + } + + _places.Insert(position, boat); + + return position; + } + + public T Remove(int position) + { + if (position < _maxCount && position >= 0) + { + if (_places.ElementAt(position) != null) + { + T result = _places.ElementAt(position); + + _places.RemoveAt(position); + + return result; + } + return null; + } + return null; + } + + public T Get(int position) + { + if (position < 0 || position >= _places.Count) return null; + return _places[position]; + } + /// + /// Получение объекта из набора по позиции + /// + /// + /// + public T this[int position] + { + get + { + if (position < _maxCount && position >= 0) + { + return _places.ElementAt(position); + } + + return null; + } + set + { + if (position < _maxCount && position >= 0) + { + Insert(this[position], position); + } + } + } + + public IEnumerable GetBoats() + { + foreach (var boat in _places) + { + if (boat != null) + { + yield return boat; + } + else + { + yield break; + } + } + } + } +}