Добавление кнопок. Обновление логики формы.

This commit is contained in:
Софья Якобчук 2023-11-28 00:23:37 +04:00
parent 054f03921c
commit b7e22a4fd5
2 changed files with 147 additions and 33 deletions

View File

@ -33,7 +33,10 @@
buttonRight = new Button(); buttonRight = new Button();
buttonDown = new Button(); buttonDown = new Button();
buttonLeft = 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(); ((System.ComponentModel.ISupportInitialize)pictureBoxSailboat).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -95,23 +98,57 @@
buttonLeft.UseVisualStyleBackColor = true; buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += buttonMove_Click; buttonLeft.Click += buttonMove_Click;
// //
// buttonCreate // buttonCreateBoat
// //
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreateBoat.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 412); buttonCreateBoat.Location = new Point(149, 391);
buttonCreate.Name = "buttonCreate"; buttonCreateBoat.Name = "buttonCreateBoat";
buttonCreate.Size = new Size(94, 29); buttonCreateBoat.Size = new Size(131, 50);
buttonCreate.TabIndex = 5; buttonCreateBoat.TabIndex = 5;
buttonCreate.Text = "Создать"; buttonCreateBoat.Text = "Создать лодку";
buttonCreate.UseVisualStyleBackColor = true; buttonCreateBoat.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click; 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 // FormSailboat
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(882, 453); 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(buttonLeft);
Controls.Add(buttonDown); Controls.Add(buttonDown);
Controls.Add(buttonRight); Controls.Add(buttonRight);
@ -132,6 +169,9 @@
private Button buttonRight; private Button buttonRight;
private Button buttonDown; private Button buttonDown;
private Button buttonLeft; private Button buttonLeft;
private Button buttonCreate; private Button buttonCreateBoat;
private Button buttonCreateSailboat;
private ComboBox comboBoxStrategy;
private Button buttonStep;
} }
} }

View File

@ -1,4 +1,6 @@
using System; using Sailboat.DrawingObjects;
using Sailboat.MovementStrategy;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
@ -10,47 +12,78 @@ using System.Windows.Forms;
namespace Sailboat namespace Sailboat
{ {
/// <summary>
/// Форма работы с объектом "Парусная лодка"
/// </summary>
public partial class FormSailboat : Form public partial class FormSailboat : Form
{ {
private DrawingSailboat? _drawingSailboat; /// <summary>
private EntitySailboat? _entitySailboat; /// Поле-объект для прорисовки объекта
/// </summary>
private DrawingBoat? _drawingBoat;
private AbstractStrategy? _abstractStrategy;
/// <summary>
/// Инициализация формы
/// </summary>
public FormSailboat() public FormSailboat()
{ {
InitializeComponent(); InitializeComponent();
} }
/// <summary>
/// Метод прорисовки лодки
/// </summary>
private void Draw() private void Draw()
{ {
if (_drawingSailboat == null) if (_drawingBoat == null)
{ {
return; return;
} }
Bitmap bmp = new(pictureBoxSailboat.Width, Bitmap bmp = new(pictureBoxSailboat.Width,
pictureBoxSailboat.Height); pictureBoxSailboat.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawingSailboat.DrawTransport(gr); _drawingBoat.DrawTransport(gr);
pictureBoxSailboat.Image = bmp; pictureBoxSailboat.Image = bmp;
} }
private void buttonCreate_Click(object sender, EventArgs e) /// <summary>
/// Обработка нажатия кнопки "Создать лодку"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonCreateBoat_Click(object sender, EventArgs e)
{ {
Random random = new(); Random random = new();
_drawingSailboat = new DrawingSailboat(); _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,
_drawingSailboat.Init(random.Next(100, 300), random.Next(1000, 3000), 100));
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));
Draw(); Draw();
} }
/// <summary>
/// Обработка нажатия кнопки "Создать улучшеную лодку"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// Изменение размеров формы
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonMove_Click(object sender, EventArgs e) private void buttonMove_Click(object sender, EventArgs e)
{ {
if (_drawingSailboat == null) if (_drawingBoat == null)
{ {
return; return;
} }
@ -58,19 +91,60 @@ namespace Sailboat
switch (name) switch (name)
{ {
case "buttonUp": case "buttonUp":
_drawingSailboat.MoveTransport(DirectionType.Up); _drawingBoat.MoveTransport(DirectionType.Up);
break; break;
case "buttonDown": case "buttonDown":
_drawingSailboat.MoveTransport(DirectionType.Down); _drawingBoat.MoveTransport(DirectionType.Down);
break; break;
case "buttonLeft": case "buttonLeft":
_drawingSailboat.MoveTransport(DirectionType.Left); _drawingBoat.MoveTransport(DirectionType.Left);
break; break;
case "buttonRight": case "buttonRight":
_drawingSailboat.MoveTransport(DirectionType.Right); _drawingBoat.MoveTransport(DirectionType.Right);
break; break;
} }
Draw(); Draw();
} }
/// <summary>
/// Обработка нажатия кнопки "Шаг"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
}
} }
} }