Готовая форма

This commit is contained in:
Данил Лопатин 2024-04-15 17:08:30 +03:00
parent d511717899
commit 63173ed478
2 changed files with 140 additions and 24 deletions

View File

@ -34,6 +34,9 @@
buttonDown = new Button();
buttonLeft = new Button();
buttonRight = new Button();
createBaseButton = new Button();
comboBoxStrategy = new ComboBox();
strategyMoveButton = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyLocomotive).BeginInit();
SuspendLayout();
//
@ -52,9 +55,9 @@
createButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
createButton.Location = new Point(12, 426);
createButton.Name = "createButton";
createButton.Size = new Size(75, 23);
createButton.Size = new Size(153, 23);
createButton.TabIndex = 1;
createButton.Text = "Создать";
createButton.Text = "Создать тепловоз";
createButton.UseVisualStyleBackColor = true;
createButton.Click += createButton_Click;
//
@ -107,11 +110,45 @@
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += moveButton_Click;
//
// createBaseButton
//
createBaseButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
createBaseButton.Location = new Point(171, 426);
createBaseButton.Name = "createBaseButton";
createBaseButton.Size = new Size(153, 23);
createBaseButton.TabIndex = 6;
createBaseButton.Text = "Создать локомотив";
createBaseButton.UseVisualStyleBackColor = true;
createBaseButton.Click += createBaseButton_Click;
//
// comboBoxStrategy
//
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
comboBoxStrategy.Location = new Point(753, 12);
comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(121, 23);
comboBoxStrategy.TabIndex = 7;
//
// strategyMoveButton
//
strategyMoveButton.Location = new Point(797, 41);
strategyMoveButton.Name = "strategyMoveButton";
strategyMoveButton.Size = new Size(75, 23);
strategyMoveButton.TabIndex = 8;
strategyMoveButton.Text = "Шаг";
strategyMoveButton.UseVisualStyleBackColor = true;
strategyMoveButton.Click += strategyMoveButton_Click;
//
// FormWarmlyLocomotive
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461);
Controls.Add(strategyMoveButton);
Controls.Add(comboBoxStrategy);
Controls.Add(createBaseButton);
Controls.Add(buttonRight);
Controls.Add(buttonLeft);
Controls.Add(buttonDown);
@ -134,5 +171,8 @@
private Button buttonDown;
private Button buttonLeft;
private Button buttonRight;
private Button createBaseButton;
private ComboBox comboBoxStrategy;
private Button strategyMoveButton;
}
}

View File

@ -8,52 +8,92 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WarmlyLocomotive.Drawnings;
using WarmlyLocomotive.MovementStrategy;
namespace WarmlyLocomotive
{
public partial class FormWarmlyLocomotive : Form
{
private DrawningWarmlyLocomotive? _drawningWarmlyLocomotive;
private DrawningLocomotive? _drawningLocomotive;
private AbstractStrategy _strategy;
public FormWarmlyLocomotive()
{
InitializeComponent();
_strategy = null;
}
private void Draw()
{
if (_drawningWarmlyLocomotive == null)
if (_drawningLocomotive == null)
{
return;
}
Bitmap bmp = new(pictureBoxWarmlyLocomotive.Width,
pictureBoxWarmlyLocomotive.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningWarmlyLocomotive.DrawTransport(gr);
_drawningLocomotive.DrawTransport(gr);
pictureBoxWarmlyLocomotive.Image = bmp;
}
private void createButton_Click(object sender, EventArgs e){
private void CreateObject(string type)
{
Random random = new();
_drawningWarmlyLocomotive = new DrawningWarmlyLocomotive();
_drawningWarmlyLocomotive.Init(
switch (type)
{
case nameof(DrawningLocomotive):
{
_drawningLocomotive = new DrawningLocomotive(
random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
break;
}
case nameof(DrawningWarmlyLocomotive):
{
_drawningLocomotive = new DrawningWarmlyLocomotive(
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)));
_drawningWarmlyLocomotive.SetPictureSize(
pictureBoxWarmlyLocomotive.Width,
pictureBoxWarmlyLocomotive.Height);
_drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
break;
}
default:
return;
}
_drawningLocomotive.SetPictureSize(pictureBoxWarmlyLocomotive.Width, pictureBoxWarmlyLocomotive.Height);
_drawningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
_strategy = null;
comboBoxStrategy.Enabled = true;
Draw();
}
private void createButton_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningWarmlyLocomotive));
private void createBaseButton_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningLocomotive));
//{
//Random random = new();
//_drawningWarmlyLocomotive = new DrawningWarmlyLocomotive();
//_drawningWarmlyLocomotive.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)));
//_drawningWarmlyLocomotive.SetPictureSize(
// pictureBoxWarmlyLocomotive.Width,
// pictureBoxWarmlyLocomotive.Height);
//_drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
//Draw();
//}
private void moveButton_Click(object sender, EventArgs e)
{
if (_drawningWarmlyLocomotive == null)
if (_drawningLocomotive == null)
{
return;
}
@ -63,19 +103,19 @@ namespace WarmlyLocomotive
{
case "buttonUp":
result =
_drawningWarmlyLocomotive.MoveTransport(DirectionType.Up);
_drawningLocomotive.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
result =
_drawningWarmlyLocomotive.MoveTransport(DirectionType.Down);
_drawningLocomotive.MoveTransport(DirectionType.Down);
break;
case "buttonLeft":
result =
_drawningWarmlyLocomotive.MoveTransport(DirectionType.Left);
_drawningLocomotive.MoveTransport(DirectionType.Left);
break;
case "buttonRight":
result =
_drawningWarmlyLocomotive.MoveTransport(DirectionType.Right);
_drawningLocomotive.MoveTransport(DirectionType.Right);
break;
}
if (result)
@ -85,5 +125,41 @@ namespace WarmlyLocomotive
}
private void strategyMoveButton_Click(object sender, EventArgs e)
{
if (_drawningLocomotive == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(new MoveableLocomotive(_drawningLocomotive),
pictureBoxWarmlyLocomotive.Width, pictureBoxWarmlyLocomotive.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == StrategyStatus.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
}
}