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

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(); buttonDown = new Button();
buttonLeft = new Button(); buttonLeft = new Button();
buttonRight = new Button(); buttonRight = new Button();
createBaseButton = new Button();
comboBoxStrategy = new ComboBox();
strategyMoveButton = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyLocomotive).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyLocomotive).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -52,9 +55,9 @@
createButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; createButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
createButton.Location = new Point(12, 426); createButton.Location = new Point(12, 426);
createButton.Name = "createButton"; createButton.Name = "createButton";
createButton.Size = new Size(75, 23); createButton.Size = new Size(153, 23);
createButton.TabIndex = 1; createButton.TabIndex = 1;
createButton.Text = "Создать"; createButton.Text = "Создать тепловоз";
createButton.UseVisualStyleBackColor = true; createButton.UseVisualStyleBackColor = true;
createButton.Click += createButton_Click; createButton.Click += createButton_Click;
// //
@ -107,11 +110,45 @@
buttonRight.UseVisualStyleBackColor = true; buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += moveButton_Click; 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 // FormWarmlyLocomotive
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(884, 461); ClientSize = new Size(884, 461);
Controls.Add(strategyMoveButton);
Controls.Add(comboBoxStrategy);
Controls.Add(createBaseButton);
Controls.Add(buttonRight); Controls.Add(buttonRight);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(buttonDown); Controls.Add(buttonDown);
@ -134,5 +171,8 @@
private Button buttonDown; private Button buttonDown;
private Button buttonLeft; private Button buttonLeft;
private Button buttonRight; 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.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using WarmlyLocomotive.Drawnings; using WarmlyLocomotive.Drawnings;
using WarmlyLocomotive.MovementStrategy;
namespace WarmlyLocomotive namespace WarmlyLocomotive
{ {
public partial class FormWarmlyLocomotive : Form public partial class FormWarmlyLocomotive : Form
{ {
private DrawningWarmlyLocomotive? _drawningWarmlyLocomotive; private DrawningLocomotive? _drawningLocomotive;
private AbstractStrategy _strategy;
public FormWarmlyLocomotive() public FormWarmlyLocomotive()
{ {
InitializeComponent(); InitializeComponent();
_strategy = null;
} }
private void Draw() private void Draw()
{ {
if (_drawningWarmlyLocomotive == null) if (_drawningLocomotive == null)
{ {
return; return;
} }
Bitmap bmp = new(pictureBoxWarmlyLocomotive.Width, Bitmap bmp = new(pictureBoxWarmlyLocomotive.Width,
pictureBoxWarmlyLocomotive.Height); pictureBoxWarmlyLocomotive.Height);
Graphics gr = Graphics.FromImage(bmp); Graphics gr = Graphics.FromImage(bmp);
_drawningWarmlyLocomotive.DrawTransport(gr); _drawningLocomotive.DrawTransport(gr);
pictureBoxWarmlyLocomotive.Image = bmp; pictureBoxWarmlyLocomotive.Image = bmp;
} }
private void CreateObject(string type)
private void createButton_Click(object sender, EventArgs e){ {
Random random = new(); Random random = new();
_drawningWarmlyLocomotive = new DrawningWarmlyLocomotive(); switch (type)
_drawningWarmlyLocomotive.Init( {
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(100, 300),
random.Next(1000, 3000), 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)),
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))); Convert.ToBoolean(random.Next(0, 2)));
_drawningWarmlyLocomotive.SetPictureSize( break;
pictureBoxWarmlyLocomotive.Width, }
pictureBoxWarmlyLocomotive.Height); default:
_drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100)); return;
}
_drawningLocomotive.SetPictureSize(pictureBoxWarmlyLocomotive.Width, pictureBoxWarmlyLocomotive.Height);
_drawningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100));
_strategy = null;
comboBoxStrategy.Enabled = true;
Draw(); 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) private void moveButton_Click(object sender, EventArgs e)
{ {
if (_drawningWarmlyLocomotive == null) if (_drawningLocomotive == null)
{ {
return; return;
} }
@ -63,19 +103,19 @@ namespace WarmlyLocomotive
{ {
case "buttonUp": case "buttonUp":
result = result =
_drawningWarmlyLocomotive.MoveTransport(DirectionType.Up); _drawningLocomotive.MoveTransport(DirectionType.Up);
break; break;
case "buttonDown": case "buttonDown":
result = result =
_drawningWarmlyLocomotive.MoveTransport(DirectionType.Down); _drawningLocomotive.MoveTransport(DirectionType.Down);
break; break;
case "buttonLeft": case "buttonLeft":
result = result =
_drawningWarmlyLocomotive.MoveTransport(DirectionType.Left); _drawningLocomotive.MoveTransport(DirectionType.Left);
break; break;
case "buttonRight": case "buttonRight":
result = result =
_drawningWarmlyLocomotive.MoveTransport(DirectionType.Right); _drawningLocomotive.MoveTransport(DirectionType.Right);
break; break;
} }
if (result) 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;
}
}
} }
} }