diff --git a/ProjectAccordionBus/ProjectAccordionBus/DrawningAccordionBus.cs b/ProjectAccordionBus/ProjectAccordionBus/DrawningAccordionBus.cs deleted file mode 100644 index d430639..0000000 --- a/ProjectAccordionBus/ProjectAccordionBus/DrawningAccordionBus.cs +++ /dev/null @@ -1,206 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectAccordionBus; - -public class DrawningAccordionBus -{ - public EntityAccordionBus? EntityAccordionBus { get; private set; } - - private int? _pictureWidth; - - private int? _pictureHeight; - - private int? _startPosX; - - private int? _startPosY; - - private int _drawningAccordionBusWidth = 60; - - private readonly int _drawningAccordionBusHeight = 50; - - public void Init(EntityAccordionBus entity) - { - EntityAccordionBus = entity; - _pictureWidth = null; - _pictureHeight = null; - _startPosX = null; - _startPosY = null; - } - - public bool SetPictureSize(int width, int height) - { - if (_drawningAccordionBusWidth <= width && _drawningAccordionBusHeight <= height) - { - _pictureWidth = width; - _pictureHeight = height; - if (_startPosX.HasValue && _startPosY.HasValue) - { - if (_startPosX + _drawningAccordionBusWidth > width) - { - _startPosX = width - _drawningAccordionBusWidth; - } - if (_startPosY + _drawningAccordionBusHeight > height) - { - _startPosY = height - _drawningAccordionBusHeight; - } - } - return true; - } - return false; - } - - public void SetPosition(int x, int y) - { - if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) - { - return; - } - _startPosX = x; - _startPosY = y; - if (x < 0) - { - _startPosX = 0; - } - - if (y < 0) - { - _startPosY = 0; - } - - if (x + _drawningAccordionBusWidth > _pictureWidth) { - _startPosX = _pictureWidth - _drawningAccordionBusWidth; - } - - if (y + _drawningAccordionBusHeight > _pictureHeight) - { - _startPosY = _pictureHeight - _drawningAccordionBusHeight; - } - } - - public bool MoveTransport(DirectionType direction) - { - if (EntityAccordionBus == null || !_startPosX.HasValue || !_startPosY.HasValue) - { - return false; - } - switch (direction) - { - //влево - case DirectionType.Left: - if (_startPosX.Value - EntityAccordionBus.Step > 0) - { - _startPosX -= (int)EntityAccordionBus.Step; - } - return true; - //вверх - case DirectionType.Up: - if (_startPosY.Value - EntityAccordionBus.Step > 0) - { - _startPosY -= (int)EntityAccordionBus.Step; - } - return true; - // вправо - case DirectionType.Right: - if (_startPosX.Value + _drawningAccordionBusWidth + EntityAccordionBus.Step < _pictureWidth.Value) - { - _startPosX += (int)EntityAccordionBus.Step; - } - return true; - //вниз - case DirectionType.Down: - if (_startPosY.Value + _drawningAccordionBusHeight + EntityAccordionBus.Step < _pictureHeight.Value) - { - _startPosY += (int)EntityAccordionBus.Step; - } - return true; - default: - return false; - } - } - - public void DrawTransport(Graphics g) - { - if (EntityAccordionBus == null || !_startPosX.HasValue || !_startPosY.HasValue) - { - return; - } - - Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityAccordionBus.AdditionalColor); - Brush bodyBrush = new SolidBrush(EntityAccordionBus.BodyColor); - - // границы автобуса - g.FillRectangle(bodyBrush, _startPosX.Value + 10, _startPosY.Value + 10, 50, 30); - - - g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 10, 50, 30); - - // окна - Brush brushBlue = new SolidBrush(Color.Blue); - g.FillEllipse(brushBlue, _startPosX.Value + 15, _startPosY.Value + 15, 5, 10); - g.FillEllipse(brushBlue, _startPosX.Value + 50, _startPosY.Value + 15, 5, 10); - g.FillEllipse(brushBlue, _startPosX.Value + 40, _startPosY.Value + 15, 5, 10); - - g.DrawEllipse(pen, _startPosX.Value + 15, _startPosY.Value + 15, 5, 10); - g.DrawEllipse(pen, _startPosX.Value + 50, _startPosY.Value + 15, 5, 10); - g.DrawEllipse(pen, _startPosX.Value + 40, _startPosY.Value + 15, 5, 10); - - - - - // крылья - if (EntityAccordionBus.Compartment) - { - _drawningAccordionBusWidth = 130; - // отсек - g.FillRectangle(bodyBrush, _startPosX.Value + 70, _startPosY.Value + 10, 50, 30); - g.FillRectangle(additionalBrush, _startPosX.Value + 60, _startPosY.Value + 15, 10, 20); - - g.DrawRectangle(pen, _startPosX.Value + 70, _startPosY.Value + 10, 50, 30); - g.DrawRectangle(pen, _startPosX.Value + 60, _startPosY.Value + 15, 10, 20); - - // колеса - g.FillEllipse(additionalBrush, _startPosX.Value + 75, _startPosY.Value + 35, 10, 10); - g.FillEllipse(additionalBrush, _startPosX.Value + 105, _startPosY.Value + 35, 10, 10); - - g.DrawEllipse(pen, _startPosX.Value + 75, _startPosY.Value + 35, 10, 10); - g.DrawEllipse(pen, _startPosX.Value + 105, _startPosY.Value + 35, 10, 10); - - if (EntityAccordionBus.Entrance) - { - g.FillRectangle(additionalBrush, _startPosX.Value + 88, _startPosY.Value + 20, 10, 20); - - g.DrawRectangle(pen, _startPosX.Value + 88, _startPosY.Value + 20, 10, 20); - } - - if (EntityAccordionBus.Windows) - { - g.FillEllipse(brushBlue, _startPosX.Value + 75, _startPosY.Value + 15, 5, 10); - g.FillEllipse(brushBlue, _startPosX.Value + 110, _startPosY.Value + 15, 5, 10); - g.FillEllipse(brushBlue, _startPosX.Value + 100, _startPosY.Value + 15, 5, 10); - - g.DrawEllipse(pen, _startPosX.Value + 75, _startPosY.Value + 15, 5, 10); - g.DrawEllipse(pen, _startPosX.Value + 110, _startPosY.Value + 15, 5, 10); - g.DrawEllipse(pen, _startPosX.Value + 100, _startPosY.Value + 15, 5, 10); - } - - } - - // двери - g.FillRectangle(additionalBrush, _startPosX.Value + 28, _startPosY.Value + 20, 10, 20); - - g.DrawRectangle(pen, _startPosX.Value + 28, _startPosY.Value + 20, 10, 20); - - // колеса - g.FillEllipse(additionalBrush, _startPosX.Value + 15, _startPosY.Value + 35, 10, 10); - g.FillEllipse(additionalBrush, _startPosX.Value + 45, _startPosY.Value + 35, 10, 10); - - g.DrawEllipse(pen, _startPosX.Value + 15, _startPosY.Value + 35, 10, 10); - g.DrawEllipse(pen, _startPosX.Value + 45, _startPosY.Value + 35, 10, 10); - } -} - diff --git a/ProjectAccordionBus/ProjectAccordionBus/DirectionType.cs b/ProjectAccordionBus/ProjectAccordionBus/Drawnings/DirectionType.cs similarity index 77% rename from ProjectAccordionBus/ProjectAccordionBus/DirectionType.cs rename to ProjectAccordionBus/ProjectAccordionBus/Drawnings/DirectionType.cs index 6ff2454..2e66619 100644 --- a/ProjectAccordionBus/ProjectAccordionBus/DirectionType.cs +++ b/ProjectAccordionBus/ProjectAccordionBus/Drawnings/DirectionType.cs @@ -4,13 +4,18 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectAccordionBus; +namespace ProjectAccordionBus.Drawnings; /// /// Направление перемещения /// public enum DirectionType { + /// + /// Неизвестное направление + /// + Unknow = -1, + /// /// Вверх /// diff --git a/ProjectAccordionBus/ProjectAccordionBus/Drawnings/DrawningAccordionBus.cs b/ProjectAccordionBus/ProjectAccordionBus/Drawnings/DrawningAccordionBus.cs new file mode 100644 index 0000000..cc0df31 --- /dev/null +++ b/ProjectAccordionBus/ProjectAccordionBus/Drawnings/DrawningAccordionBus.cs @@ -0,0 +1,62 @@ +using ProjectAccordionBus.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; + +namespace ProjectAccordionBus.Drawnings; + +public class DrawningAccordionBus : DrawningBus +{ + public DrawningAccordionBus(int speed, double weight, Color bodyColor, Color additionalColor, bool compartment, bool entrance, bool windows) : base(110, 40) + { + EntityBus = new EntityAccordionBus(speed, weight, bodyColor, additionalColor, compartment, entrance, windows); + } + + public override void DrawTransport(Graphics g) + { + if (EntityBus == null || EntityBus is not EntityAccordionBus accordionBus || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(accordionBus.AdditionalColor); + + base.DrawTransport(g); + + Brush brushBlue = new SolidBrush(Color.Blue); + // отсек + g.FillRectangle(additionalBrush, _startPosX.Value + 60, _startPosY.Value, 50, 30); + g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 5, 10, 20); + + g.DrawRectangle(pen, _startPosX.Value + 60, _startPosY.Value, 50, 30); + g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value + 5, 10, 20); + + // колеса + g.FillEllipse(additionalBrush, _startPosX.Value + 65, _startPosY.Value + 25, 10, 10); + g.FillEllipse(additionalBrush, _startPosX.Value + 95, _startPosY.Value + 25, 10, 10); + + g.DrawEllipse(pen, _startPosX.Value + 65, _startPosY.Value + 25, 10, 10); + g.DrawEllipse(pen, _startPosX.Value + 95, _startPosY.Value + 25, 10, 10); + + if (accordionBus.Entrance) + { + g.FillRectangle(additionalBrush, _startPosX.Value + 78, _startPosY.Value + 10, 10, 20); + + g.DrawRectangle(pen, _startPosX.Value + 78, _startPosY.Value + 10, 10, 20); + } + + if (accordionBus.Windows) + { + g.FillEllipse(brushBlue, _startPosX.Value + 65, _startPosY.Value + 5, 5, 10); + g.FillEllipse(brushBlue, _startPosX.Value + 100, _startPosY.Value + 5, 5, 10); + g.FillEllipse(brushBlue, _startPosX.Value + 90, _startPosY.Value + 5, 5, 10); + + g.DrawEllipse(pen, _startPosX.Value + 65, _startPosY.Value + 5, 5, 10); + g.DrawEllipse(pen, _startPosX.Value + 100, _startPosY.Value + 5, 5, 10); + g.DrawEllipse(pen, _startPosX.Value + 90, _startPosY.Value + 5, 5, 10); + } + } +} \ No newline at end of file diff --git a/ProjectAccordionBus/ProjectAccordionBus/Drawnings/DrawningBus.cs b/ProjectAccordionBus/ProjectAccordionBus/Drawnings/DrawningBus.cs new file mode 100644 index 0000000..7e3eddb --- /dev/null +++ b/ProjectAccordionBus/ProjectAccordionBus/Drawnings/DrawningBus.cs @@ -0,0 +1,217 @@ +using ProjectAccordionBus.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAccordionBus.Drawnings; +public class DrawningBus +{ + /// + /// Класс-сущность + /// + public EntityBus? EntityBus { get; protected set; } + + /// + /// Ширина окна + /// + private int? _pictureWidth; + + /// + /// Высота окна + /// + private int? _pictureHeight; + + /// + /// Левая координата прорисовки транспорта + /// + protected int? _startPosX; + + /// + /// Верхняя координата прорисовки транспорта + /// + protected int? _startPosY; + + /// + /// Ширина прорисовки транспорта + /// + private readonly int _drawningBusWidth = 50; + + /// + /// Высота прорисовки транспорта + /// + private readonly int _drawningBusHeight = 35; + + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawningBusWidth; + + /// + /// Высота объекта + /// + public int GetHight => _drawningBusHeight; + + // Пустой конструктор + private DrawningBus() + { + _pictureWidth = null; + _pictureHeight = null; + _startPosX = null; + _startPosY = null; + } + + // Конструктор + public DrawningBus(int speed, double weight, Color bodyColor) : this() + { + EntityBus = new EntityBus(speed, weight, bodyColor); + } + + // Конструктор + protected DrawningBus(int drawningBusWidth, int drawningBusHeight) : this() + { + _drawningBusWidth = drawningBusWidth; + _drawningBusHeight = drawningBusHeight; + } + + public bool SetPictureSize(int width, int height) + { + if (_drawningBusWidth <= width && _drawningBusHeight <= height) + { + _pictureWidth = width; + _pictureHeight = height; + if (_startPosX.HasValue && _startPosY.HasValue) + { + if (_startPosX + _drawningBusWidth > width) + { + _startPosX = width - _drawningBusWidth; + } + if (_startPosY + _drawningBusHeight > height) + { + _startPosY = height - _drawningBusHeight; + } + } + return true; + } + return false; + } + + public void SetPosition(int x, int y) + { + if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) + { + return; + } + _startPosX = x; + _startPosY = y; + if (x < 0) + { + _startPosX = 0; + } + + if (y < 0) + { + _startPosY = 0; + } + + if (x + _drawningBusWidth > _pictureWidth) + { + _startPosX = _pictureWidth - _drawningBusWidth; + } + + if (y + _drawningBusHeight > _pictureHeight) + { + _startPosY = _pictureHeight - _drawningBusHeight; + } + } + + public bool MoveTransport(DirectionType direction) + { + if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue) + { + return false; + } + switch (direction) + { + //влево + case DirectionType.Left: + if (_startPosX.Value - EntityBus.Step > 0) + { + _startPosX -= (int)EntityBus.Step; + } + return true; + //вверх + case DirectionType.Up: + if (_startPosY.Value - EntityBus.Step > 0) + { + _startPosY -= (int)EntityBus.Step; + } + return true; + // вправо + case DirectionType.Right: + if (_startPosX.Value + _drawningBusWidth + EntityBus.Step < _pictureWidth.Value) + { + _startPosX += (int)EntityBus.Step; + } + return true; + //вниз + case DirectionType.Down: + if (_startPosY.Value + _drawningBusHeight + EntityBus.Step < _pictureHeight.Value) + { + _startPosY += (int)EntityBus.Step; + } + return true; + default: + return false; + } + } + + public virtual void DrawTransport(Graphics g) + { + if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + + Pen pen = new(Color.Black); + Brush bodyBrush = new SolidBrush(EntityBus.BodyColor); + + // границы автобуса + g.FillRectangle(bodyBrush, _startPosX.Value, _startPosY.Value, 50, 30); + g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value, 50, 30); + + // окна + Brush brushBlue = new SolidBrush(Color.Blue); + g.FillEllipse(brushBlue, _startPosX.Value + 5, _startPosY.Value + 5, 5, 10); + g.FillEllipse(brushBlue, _startPosX.Value + 40, _startPosY.Value + 5, 5, 10); + g.FillEllipse(brushBlue, _startPosX.Value + 30, _startPosY.Value + 5, 5, 10); + + g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 5, 5, 10); + g.DrawEllipse(pen, _startPosX.Value + 40, _startPosY.Value + 5, 5, 10); + g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 5, 5, 10); + + // двери + g.FillRectangle(bodyBrush, _startPosX.Value + 18, _startPosY.Value + 10, 10, 20); + + g.DrawRectangle(pen, _startPosX.Value + 18, _startPosY.Value + 10, 10, 20); + + // колеса + g.FillEllipse(bodyBrush, _startPosX.Value + 5, _startPosY.Value + 25, 10, 10); + g.FillEllipse(bodyBrush, _startPosX.Value + 35, _startPosY.Value + 25, 10, 10); + + g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 25, 10, 10); + g.DrawEllipse(pen, _startPosX.Value + 35, _startPosY.Value + 25, 10, 10); + } +} + diff --git a/ProjectAccordionBus/ProjectAccordionBus/EntityAccordionBus.cs b/ProjectAccordionBus/ProjectAccordionBus/Entities/EntityAccordionBus.cs similarity index 56% rename from ProjectAccordionBus/ProjectAccordionBus/EntityAccordionBus.cs rename to ProjectAccordionBus/ProjectAccordionBus/Entities/EntityAccordionBus.cs index c19f39a..ed7ce3c 100644 --- a/ProjectAccordionBus/ProjectAccordionBus/EntityAccordionBus.cs +++ b/ProjectAccordionBus/ProjectAccordionBus/Entities/EntityAccordionBus.cs @@ -4,20 +4,12 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectAccordionBus; +namespace ProjectAccordionBus.Entities; -public class EntityAccordionBus +public class EntityAccordionBus : EntityBus { - public int Speed { get; private set; } - - public double Weight { get; private set; } - - public Color BodyColor { get; private set; } - public Color AdditionalColor { get; private set; } - public double Step => Speed * 50 / Weight; - public bool Compartment { get; private set; } public bool Entrance { get; private set; } @@ -25,24 +17,18 @@ public class EntityAccordionBus public bool Windows { get; private set; } /// - /// Инициализация полей объекта-класса спортивного автомобиля + /// Инициализация полей объекта-класса accordion bus /// - /// Скорость - /// Вес автомобиля - /// Основной цвет /// Дополнительный цвет /// Признак наличия доп. отсека с гармошкой /// Признак наличия входа у доп. отсека /// Признак наличия окон у доп. отсека - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool compartment, bool entrance, bool windows) + public EntityAccordionBus(int Speed, double Weight, Color bodyColor, Color additionalColor, bool compartment, bool entrance, bool windows) : base(Speed, Weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; Compartment = compartment; // отсек - Entrance = entrance; // крылья + Entrance = entrance; Windows = windows; } diff --git a/ProjectAccordionBus/ProjectAccordionBus/Entities/EntityBus.cs b/ProjectAccordionBus/ProjectAccordionBus/Entities/EntityBus.cs new file mode 100644 index 0000000..39b8765 --- /dev/null +++ b/ProjectAccordionBus/ProjectAccordionBus/Entities/EntityBus.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; + +namespace ProjectAccordionBus.Entities; +public class EntityBus +{ + public int Speed { get; private set; } + + public double Weight { get; private set; } + + public Color BodyColor { get; private set; } + + public double Step => Speed * 50 / Weight; + + /// + /// Конструктор сущности + /// + /// Скорость + /// Вес автомобиля + /// Основной цвет + + public EntityBus(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } +} diff --git a/ProjectAccordionBus/ProjectAccordionBus/FormAccordionBus.Designer.cs b/ProjectAccordionBus/ProjectAccordionBus/FormAccordionBus.Designer.cs index 29f1c19..de70f29 100644 --- a/ProjectAccordionBus/ProjectAccordionBus/FormAccordionBus.Designer.cs +++ b/ProjectAccordionBus/ProjectAccordionBus/FormAccordionBus.Designer.cs @@ -35,6 +35,9 @@ buttonDown = new Button(); buttonUp = new Button(); buttonRight = new Button(); + buttonCreateBus = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAccordionBus).BeginInit(); SuspendLayout(); // @@ -53,9 +56,9 @@ buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonCreate.Location = new Point(12, 351); buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(75, 23); + buttonCreate.Size = new Size(188, 23); buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; + buttonCreate.Text = "Создать автобус-гармошку"; buttonCreate.UseVisualStyleBackColor = true; buttonCreate.Click += ButtonCreate_Click; // @@ -107,11 +110,46 @@ buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; // + // buttonCreateBus + // + buttonCreateBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateBus.Location = new Point(218, 351); + buttonCreateBus.Name = "buttonCreateBus"; + buttonCreateBus.Size = new Size(138, 23); + buttonCreateBus.TabIndex = 6; + buttonCreateBus.Text = "Создать автобус"; + buttonCreateBus.UseVisualStyleBackColor = true; + buttonCreateBus.Click += buttonCreateBus_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(595, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; + comboBoxStrategy.SelectedIndexChanged += comboBoxStrategy_SelectedIndexChanged; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(641, 41); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(75, 23); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormAccordionBus // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(728, 386); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonCreateBus); Controls.Add(buttonRight); Controls.Add(buttonUp); Controls.Add(buttonDown); @@ -137,5 +175,8 @@ private Button buttonDown; private Button buttonUp; private Button buttonRight; + private Button buttonCreateBus; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/ProjectAccordionBus/ProjectAccordionBus/FormAccordionBus.cs b/ProjectAccordionBus/ProjectAccordionBus/FormAccordionBus.cs index ac6b116..74a57e6 100644 --- a/ProjectAccordionBus/ProjectAccordionBus/FormAccordionBus.cs +++ b/ProjectAccordionBus/ProjectAccordionBus/FormAccordionBus.cs @@ -1,4 +1,7 @@ -using System; +using ProjectAccordionBus.Drawnings; +using ProjectAccordionBus.Entities; +using ProjectAccordionBus.MovementStrategy; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -10,78 +13,142 @@ using System.Windows.Forms; namespace ProjectAccordionBus; public partial class FormAccordionBus : Form +{ + + private DrawningBus? _drawningBus; + + private AbstractStrategy? _strategy; + + + public FormAccordionBus() { + InitializeComponent(); + _strategy = null; + } - private DrawningAccordionBus? _drawningAccordionBus; - - private EntityAccordionBus? _entity; - - - public FormAccordionBus() + private void Draw() + { + if (_drawningBus == null) { - InitializeComponent(); + return; } - private void Draw() + Bitmap bmp = new(pictureBoxAccordionBus.Width, pictureBoxAccordionBus.Height); + Graphics gr = Graphics.FromImage(bmp); + _drawningBus.DrawTransport(gr); + pictureBoxAccordionBus.Image = bmp; + } + + private void CreateObject(string type) + { + Random random = new(); + switch (type) { - if (_drawningAccordionBus == null) - { + case nameof(DrawningBus): + _drawningBus = new DrawningBus(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(DrawningAccordionBus): + _drawningBus = new DrawningAccordionBus(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))); + break; + default: return; - } - - Bitmap bmp = new(pictureBoxAccordionBus.Width, pictureBoxAccordionBus.Height); - Graphics gr = Graphics.FromImage(bmp); - _drawningAccordionBus.DrawTransport(gr); - pictureBoxAccordionBus.Image = bmp; } - private void ButtonCreate_Click(object sender, EventArgs e) - { - Random random = new(); - _drawningAccordionBus = new DrawningAccordionBus(); - _entity = new EntityAccordionBus(); - _entity.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))); - _drawningAccordionBus.Init(_entity); - _drawningAccordionBus.SetPictureSize(pictureBoxAccordionBus.Width, pictureBoxAccordionBus.Height); - _drawningAccordionBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _drawningBus.SetPictureSize(pictureBoxAccordionBus.Width, pictureBoxAccordionBus.Height); + _drawningBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; + Draw(); + } + private void ButtonCreate_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawningAccordionBus)); + } + + private void buttonCreateBus_Click(object sender, EventArgs e) + { + CreateObject(nameof(DrawningBus)); + } + + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_drawningBus == null) + { + return; + } + + string name = ((Button)sender)?.Name ?? string.Empty; + bool result = false; + switch (name) + { + case "buttonUp": + result = _drawningBus.MoveTransport(DirectionType.Up); + break; + case "buttonDown": + result = _drawningBus.MoveTransport(DirectionType.Down); + break; + case "buttonLeft": + result = _drawningBus.MoveTransport(DirectionType.Left); + break; + case "buttonRight": + result = _drawningBus.MoveTransport(DirectionType.Right); + break; + } + + if (result) + { Draw(); } - - private void ButtonMove_Click(object sender, EventArgs e) - { - if (_drawningAccordionBus == null) - { - return; - } - - string name = ((Button)sender)?.Name ?? string.Empty; - bool result = false; - switch (name) - { - case "buttonUp": - result = _drawningAccordionBus.MoveTransport(DirectionType.Up); - break; - case "buttonDown": - result = _drawningAccordionBus.MoveTransport(DirectionType.Down); - break; - case "buttonLeft": - result = _drawningAccordionBus.MoveTransport(DirectionType.Left); - break; - case "buttonRight": - result = _drawningAccordionBus.MoveTransport(DirectionType.Right); - break; - } - - if (result) - { - Draw(); - } - } } + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawningBus == null) + { + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableBus(_drawningBus), pictureBoxAccordionBus.Width, pictureBoxAccordionBus.Height); + } + + if (_strategy == null) + { + return; + } + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } + + private void comboBoxStrategy_SelectedIndexChanged(object sender, EventArgs e) + { + + } +} + diff --git a/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/AbstractStrategy.cs b/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..fd3b8d8 --- /dev/null +++ b/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAccordionBus.MovementStrategy; + +public abstract class AbstractStrategy +{ + /// + /// Перемещаемый объект + /// + private IMoveableObject? _moveableObject; + + /// + /// Статус перемещения + /// + private StrategyStatus _state = StrategyStatus.NotInit; + + /// + /// Ширина поля + /// + protected int FieldWidth { get; private set; } + + /// + /// Высота поля + /// + protected int FieldHeight { get; private set; } + + /// + /// Статус перемещения + /// + /// + public StrategyStatus GetStatus() { return _state; } + + /// + /// Установка данных + /// + /// + /// + /// + public void SetData(IMoveableObject moveableObject, int width, int height) + { + if (moveableObject == null) + { + _state = StrategyStatus.NotInit; + return; + } + _state = StrategyStatus.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + + public void MakeStep() + { + if (_state != StrategyStatus.InProgress) + { + return; + } + + if (IsTargetDestination()) + { + _state = StrategyStatus.Finish; + return; + } + + MoveToTarget(); + } + + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместится, false - неудача) + protected bool MoveLeft() => MoveTo(MovementDirection.Left); + + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместится, false - неудача) + protected bool MoveRight() => MoveTo(MovementDirection.Right); + + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместится, false - неудача) + protected bool MoveUp() => MoveTo(MovementDirection.Up); + + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместится, false - неудача) + protected bool MoveDown() => MoveTo(MovementDirection.Down); + + /// + /// Параметры объекта + /// + protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition; + + protected int? GetStep() + { + if (_state != StrategyStatus.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + + protected abstract bool IsTargetDestination(); + + protected bool MoveTo(MovementDirection movementDirection) + { + if (_state != StrategyStatus.InProgress) + { + return false; + } + + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } +} diff --git a/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/IMoveableObject.cs b/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..ed3a6c9 --- /dev/null +++ b/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAccordionBus.MovementStrategy; +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Попытка переместить объект в указанном направлении + /// + /// + /// + bool TryMoveObject(MovementDirection direction); +} diff --git a/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/MoveToBorder.cs b/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..631f194 --- /dev/null +++ b/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAccordionBus.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + + return objParams.RightBorder <= FieldWidth && objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder <= FieldHeight && + objParams.DownBorder + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + + int diffX = objParams.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + + int diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/MoveToCenter.cs b/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..cbe9ece --- /dev/null +++ b/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAccordionBus.MovementStrategy; + +public class MoveToCenter : AbstractStrategy +{ + protected override bool IsTargetDestination() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + + return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + + int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } +} diff --git a/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/MoveableBus.cs b/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/MoveableBus.cs new file mode 100644 index 0000000..89d463f --- /dev/null +++ b/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/MoveableBus.cs @@ -0,0 +1,61 @@ +using ProjectAccordionBus.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAccordionBus.MovementStrategy; + +public class MoveableBus : IMoveableObject +{ + /// + /// Поле-объект класса DrawningBus или его наследника + /// + private readonly DrawningBus? _bus = null; + + /// + /// Конструктор + /// + /// Объект класса DrawningBus + public MoveableBus(DrawningBus bus) + { + _bus = bus; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_bus == null || _bus.EntityBus == null || !_bus.GetPosX.HasValue || !_bus.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_bus.GetPosX.Value, _bus.GetPosY.Value, _bus.GetWidth, _bus.GetHight); + } + } + + public int GetStep => (int)(_bus?.EntityBus?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_bus == null || _bus.EntityBus == null) + { + return false; + } + + return _bus.MoveTransport(GetDirectionType(direction)); + } + + private static DirectionType GetDirectionType(MovementDirection direction) + { + return direction switch + { + MovementDirection.Left => DirectionType.Left, + MovementDirection.Right => DirectionType.Right, + MovementDirection.Up => DirectionType.Up, + MovementDirection.Down => DirectionType.Down, + _ => DirectionType.Unknow, + }; + } +} diff --git a/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/MovementDirection.cs b/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..12494ef --- /dev/null +++ b/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/MovementDirection.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAccordionBus.MovementStrategy; +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + + /// + /// Вниз + /// + Down = 2, + + /// + /// Влево + /// + Left = 3, + + /// + /// Вправо + /// + Right = 4 +} diff --git a/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/ObjectParameters.cs b/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..6c41340 --- /dev/null +++ b/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAccordionBus.MovementStrategy; +public class ObjectParameters +{ + /// + /// Координата x + /// + private readonly int _x; + + /// + /// Координата y + /// + private readonly int _y; + + /// + /// Ширина объекта + /// + private readonly int _width; + + /// + /// Высота объекта + /// + private readonly int _height; + + /// + /// Левая граница + /// + public int LeftBorder => _x; + + /// + /// Верхняя граница + /// + public int TopBorder => _y; + + /// + /// Правая граница + /// + public int RightBorder => _x + _width; + + /// + /// Нижняя граница + /// + public int DownBorder => _y + _height; + + /// + /// Середина объекта + /// + public int ObjectMiddleHorizontal => _x + _width / 2; + + /// + /// Середина объекта + /// + public int ObjectMiddleVertical => _y + _height / 2; + + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } +} \ No newline at end of file diff --git a/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/StrategyStatus.cs b/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..6f91a50 --- /dev/null +++ b/ProjectAccordionBus/ProjectAccordionBus/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectAccordionBus.MovementStrategy; +public enum StrategyStatus +{ + /// + /// Все готово к началу + NotInit, + + /// + /// Выполняется + /// + InProgress, + + /// + /// Завершено + /// + Finish +} +