diff --git a/Sailboat/Sailboat/FormSailboat.Designer.cs b/Sailboat/Sailboat/FormSailboat.Designer.cs index 53dab1c..4b435b1 100644 --- a/Sailboat/Sailboat/FormSailboat.Designer.cs +++ b/Sailboat/Sailboat/FormSailboat.Designer.cs @@ -33,7 +33,10 @@ buttonRight = new Button(); buttonDown = new Button(); buttonLeft = new Button(); - buttonCreate = new Button(); + buttonCreateBoat = new Button(); + buttonCreateSailboat = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxSailboat).BeginInit(); SuspendLayout(); // @@ -95,23 +98,57 @@ buttonLeft.UseVisualStyleBackColor = true; buttonLeft.Click += buttonMove_Click; // - // buttonCreate + // buttonCreateBoat // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 412); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(94, 29); - buttonCreate.TabIndex = 5; - buttonCreate.Text = "Создать"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += buttonCreate_Click; + buttonCreateBoat.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateBoat.Location = new Point(149, 391); + buttonCreateBoat.Name = "buttonCreateBoat"; + buttonCreateBoat.Size = new Size(131, 50); + buttonCreateBoat.TabIndex = 5; + buttonCreateBoat.Text = "Создать лодку"; + buttonCreateBoat.UseVisualStyleBackColor = true; + buttonCreateBoat.Click += buttonCreateBoat_Click; + // + // buttonCreateSailboat + // + buttonCreateSailboat.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateSailboat.Location = new Point(12, 391); + buttonCreateSailboat.Name = "buttonCreateSailboat"; + buttonCreateSailboat.Size = new Size(131, 50); + buttonCreateSailboat.TabIndex = 6; + buttonCreateSailboat.Text = "Создать парусную лодку"; + buttonCreateSailboat.UseVisualStyleBackColor = true; + buttonCreateSailboat.Click += buttonCreateSailboat_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "До центра", "До края" }); + comboBoxStrategy.Location = new Point(719, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(151, 28); + comboBoxStrategy.TabIndex = 7; + // + // buttonStep + // + buttonStep.Location = new Point(776, 46); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(94, 29); + buttonStep.TabIndex = 8; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += buttonStep_Click; // // FormSailboat // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(882, 453); - Controls.Add(buttonCreate); + Controls.Add(buttonStep); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonCreateSailboat); + Controls.Add(buttonCreateBoat); Controls.Add(buttonLeft); Controls.Add(buttonDown); Controls.Add(buttonRight); @@ -132,6 +169,9 @@ private Button buttonRight; private Button buttonDown; private Button buttonLeft; - private Button buttonCreate; + private Button buttonCreateBoat; + private Button buttonCreateSailboat; + private ComboBox comboBoxStrategy; + private Button buttonStep; } } \ No newline at end of file diff --git a/Sailboat/Sailboat/FormSailboat.cs b/Sailboat/Sailboat/FormSailboat.cs index d74960e..597a952 100644 --- a/Sailboat/Sailboat/FormSailboat.cs +++ b/Sailboat/Sailboat/FormSailboat.cs @@ -1,4 +1,6 @@ -using System; +using Sailboat.DrawingObjects; +using Sailboat.MovementStrategy; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -10,47 +12,78 @@ using System.Windows.Forms; namespace Sailboat { + /// + /// Форма работы с объектом "Парусная лодка" + /// public partial class FormSailboat : Form { - private DrawingSailboat? _drawingSailboat; - private EntitySailboat? _entitySailboat; + /// + /// Поле-объект для прорисовки объекта + /// + private DrawingBoat? _drawingBoat; + private AbstractStrategy? _abstractStrategy; + /// + /// Инициализация формы + /// public FormSailboat() { InitializeComponent(); } + /// + /// Метод прорисовки лодки + /// private void Draw() { - if (_drawingSailboat == null) + if (_drawingBoat == null) { return; } Bitmap bmp = new(pictureBoxSailboat.Width, pictureBoxSailboat.Height); Graphics gr = Graphics.FromImage(bmp); - _drawingSailboat.DrawTransport(gr); + _drawingBoat.DrawTransport(gr); pictureBoxSailboat.Image = bmp; } - private void buttonCreate_Click(object sender, EventArgs e) + /// + /// Обработка нажатия кнопки "Создать лодку" + /// + /// + /// + private void buttonCreateBoat_Click(object sender, EventArgs e) { Random random = new(); - _drawingSailboat = new DrawingSailboat(); - - _drawingSailboat.Init(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), - pictureBoxSailboat.Width, pictureBoxSailboat.Height); - - _drawingSailboat.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _drawingBoat = new DrawingBoat(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxSailboat.Width, pictureBoxSailboat.Height); + _drawingBoat.SetPosition(random.Next(10, 100), random.Next(10, + 100)); Draw(); } + /// + /// Обработка нажатия кнопки "Создать улучшеную лодку" + /// + /// + /// + private void buttonCreateSailboat_Click(object sender, EventArgs e) + { + Random random = new(); + _drawingBoat = new DrawingSailboat(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + Convert.ToBoolean(random.Next(0, 2)), + Convert.ToBoolean(random.Next(0, 2)), pictureBoxSailboat.Width, pictureBoxSailboat.Height); + _drawingBoat.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + + /// + /// Изменение размеров формы + /// + /// + /// private void buttonMove_Click(object sender, EventArgs e) { - if (_drawingSailboat == null) + if (_drawingBoat == null) { return; } @@ -58,19 +91,60 @@ namespace Sailboat switch (name) { case "buttonUp": - _drawingSailboat.MoveTransport(DirectionType.Up); + _drawingBoat.MoveTransport(DirectionType.Up); break; case "buttonDown": - _drawingSailboat.MoveTransport(DirectionType.Down); + _drawingBoat.MoveTransport(DirectionType.Down); break; case "buttonLeft": - _drawingSailboat.MoveTransport(DirectionType.Left); + _drawingBoat.MoveTransport(DirectionType.Left); break; case "buttonRight": - _drawingSailboat.MoveTransport(DirectionType.Right); + _drawingBoat.MoveTransport(DirectionType.Right); break; } Draw(); } + + /// + /// Обработка нажатия кнопки "Шаг" + /// + /// + /// + private void buttonStep_Click(object sender, EventArgs e) + { + if (_drawingBoat == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new DrawingObjectBoat(_drawingBoat), pictureBoxSailboat.Width, + pictureBoxSailboat.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + } } -} +} \ No newline at end of file