diff --git a/Trolleybus/Trolleybus/AbstractMap.cs b/Trolleybus/Trolleybus/AbstractMap.cs new file mode 100644 index 0000000..39a1034 --- /dev/null +++ b/Trolleybus/Trolleybus/AbstractMap.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Drawing; + +namespace Trolleybus +{ + internal abstract class AbstractMap + { + private IDrawningObject _drawningObject = null; + protected int[,] _map = null; + protected int _width; + protected int _height; + protected float _size_x; + protected float _size_y; + protected readonly Random _random = new Random(); + protected readonly int _freeRoad = 0; + protected readonly int _barrier = 1; + + public Bitmap CreateMap(int width, int height, IDrawningObject drawningObject) + { + _width = width; + _height = height; + _drawningObject = drawningObject; + GenerateMap(); + while (!SetObjectOnMap()) + { + GenerateMap(); + } + return DrawMapWithObject(); + } + public Bitmap MoveObject(Direction direction) + { + (float leftX, float topY, float rightX, float bottomY) = _drawningObject.GetCurrentPosition(); + + for (int i = 0; i < _map.GetLength(0); i++) + { + for (int j = 0; j < _map.GetLength(1); j++) + { + if (_map[i, j] == _barrier) + { + switch (direction) + { + case Direction.Up: + if (_size_y * (j + 1) >= topY - _drawningObject.Step && _size_y * (j + 1) < topY && _size_x * (i + 1) > leftX + && _size_x * (i + 1) <= rightX) + { + return DrawMapWithObject(); + } + break; + case Direction.Down: + if (_size_y * j <= bottomY + _drawningObject.Step && _size_y * j > bottomY && _size_x * (i + 1) > leftX + && _size_x * (i + 1) <= rightX) + { + return DrawMapWithObject(); + } + break; + case Direction.Left: + if (_size_x * (i + 1) >= leftX - _drawningObject.Step && _size_x * (i + 1) < leftX && _size_y * (j + 1) < bottomY + && _size_y * (j + 1) >= topY) + { + return DrawMapWithObject(); + } + break; + case Direction.Right: + if (_size_x * i <= rightX + _drawningObject.Step && _size_x * i > leftX && _size_y * (j + 1) < bottomY + && _size_y * (j + 1) >= topY) + { + return DrawMapWithObject(); + } + break; + } + } + } + } + _drawningObject.MoveObject(direction); + return DrawMapWithObject(); + } + private bool SetObjectOnMap() + { + (float leftX, float topY, float rightX, float bottomY) = _drawningObject.GetCurrentPosition(); + if (_drawningObject == null || _map == null) + { + return false; + } + + float airplaneWidth = rightX - leftX; + float airplaneHeight = bottomY - topY; + + int x = _random.Next(0, 10); + int y = _random.Next(0, 10); + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++j) + { + if (_map[i, j] == _barrier) + { + if (x + airplaneWidth >= _size_x * i && x <= _size_x * i && y + airplaneHeight > _size_y * j && y <= _size_y * j) + { + return false; + } + } + } + } + _drawningObject.SetObject(x, y, _width, _height); + return true; + } + private Bitmap DrawMapWithObject() + { + Bitmap bmp = new(_width, _height); + if (_drawningObject == null || _map == null) + { + return bmp; + } + Graphics gr = Graphics.FromImage(bmp); + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++j) + { + if (_map[i, j] == _freeRoad) + { + DrawRoadPart(gr, i, j); + } + else if (_map[i, j] == _barrier) + { + DrawBarrierPart(gr, i, j); + } + } + } + _drawningObject.DrawningObject(gr); + return bmp; + } + + protected abstract void GenerateMap(); + protected abstract void DrawRoadPart(Graphics g, int i, int j); + protected abstract void DrawBarrierPart(Graphics g, int i, int j); + } +} \ No newline at end of file diff --git a/Trolleybus/Trolleybus/AutoStopMap.cs b/Trolleybus/Trolleybus/AutoStopMap.cs new file mode 100644 index 0000000..aec3177 --- /dev/null +++ b/Trolleybus/Trolleybus/AutoStopMap.cs @@ -0,0 +1,30 @@ +using System.Threading; +using Trolleybus; + +namespace Trolleybus +{ + internal class AutoStopMap : SimpleMap + { + protected override void GenerateMap() + { + _map = new int[100, 100]; + _size_x = (float)_width / _map.GetLength(0); + _size_y = (float)_height / _map.GetLength(1); + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++j) + { + _map[i, j] = _freeRoad; + } + } + for (int i = 0; i < 50; i++) + { + for (int j = 0; j < 50; j++) + { + _map[i, j] = _barrier; + } + } + } + } + +} \ No newline at end of file diff --git a/Trolleybus/Trolleybus/Direction.cs b/Trolleybus/Trolleybus/Direction.cs index 68bfa63..0cfd670 100644 --- a/Trolleybus/Trolleybus/Direction.cs +++ b/Trolleybus/Trolleybus/Direction.cs @@ -9,8 +9,9 @@ namespace Trolleybus /// /// Направление перемещения /// - internal enum Direction + public enum Direction { + None = 0, Up = 1, Down = 2, Left = 3, diff --git a/Trolleybus/Trolleybus/DrawingSmallTrolleybus.cs b/Trolleybus/Trolleybus/DrawingSmallTrolleybus.cs new file mode 100644 index 0000000..af5dd4d --- /dev/null +++ b/Trolleybus/Trolleybus/DrawingSmallTrolleybus.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Drawing; + +namespace Trolleybus +{ + internal class DrawningSmallTrolleybus : DrawingTrolleybus + { + public DrawningSmallTrolleybus(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool horns, bool battary) : + base(speed, weight, bodyColor, 110, 60) + { + Trolleybus = new EntitySmallTrolleybus(speed, weight, bodyColor, dopColor,bodyKit, horns, battary); + } + + public override void DrawTransport(Graphics g) + { + if (Trolleybus is not EntitySmallTrolleybus smallTrolleybus) + { + return; + } + + Pen pen = new Pen(Color.Black); + Brush dopBrush = new SolidBrush(smallTrolleybus.DopColor); + Brush brBlue = new SolidBrush(Color.LightBlue); + Brush dBlue = new SolidBrush(Color.DarkBlue); + Brush bWhite = new SolidBrush(Color.White); + Pen WinBlue = new Pen(Color.Blue); + + base.DrawTransport(g); + if (smallTrolleybus.BodyKit) + { + g.DrawRectangle(pen, _startPosX, _startPosY, 200, 50); + g.DrawRectangle(pen, _startPosX + 100, _startPosY + 10, 20, 40); + g.FillEllipse(brBlue, _startPosX, _startPosY + 5, 20, 25); + g.DrawEllipse(WinBlue, _startPosX, _startPosY + 5, 20, 25); + g.FillEllipse(brBlue, _startPosX + 25, _startPosY + 5, 20, 25); + g.DrawEllipse(WinBlue, _startPosX + 25, _startPosY + 5, 20, 25); + g.FillEllipse(brBlue, _startPosX + 50, _startPosY + 5, 20, 25); + g.DrawEllipse(WinBlue, _startPosX + 50, _startPosY + 5, 20, 25); + g.FillEllipse(brBlue, _startPosX + 75, _startPosY + 5, 20, 25); + g.DrawEllipse(WinBlue, _startPosX + 75, _startPosY + 5, 20, 25); + g.FillEllipse(brBlue, _startPosX + 120, _startPosY + 5, 20, 25); + g.DrawEllipse(WinBlue, _startPosX + 120, _startPosY + 5, 20, 25); + g.FillEllipse(brBlue, _startPosX + 145, _startPosY + 5, 20, 25); + g.DrawEllipse(WinBlue, _startPosX + 145, _startPosY + 5, 20, 25); + g.FillEllipse(brBlue, _startPosX + 170, _startPosY + 5, 20, 25); + g.DrawEllipse(WinBlue, _startPosX + 170, _startPosY + 5, 20, 25); + g.FillEllipse(bWhite, _startPosX, _startPosY + 40, 30, 30); + g.DrawEllipse(pen, _startPosX, _startPosY + 40, 30, 30); + g.FillEllipse(bWhite, _startPosX + 170, _startPosY + 40, 30, 30); + g.DrawEllipse(pen, _startPosX + 170, _startPosY + 40, 30, 30); + } + + + if (smallTrolleybus.Horns) + { + g.DrawLine(pen, _startPosX + 100, _startPosY - 10, _startPosX + 150, _startPosY - 20); + g.DrawLine(pen, _startPosX + 150, _startPosY - 20, _startPosX, _startPosY - 30); + g.DrawRectangle(pen, _startPosX + 50, _startPosY - 10, 100, 10); + } + + if (smallTrolleybus.Battary) + { + g.DrawRectangle(pen, _startPosX + 100, _startPosY, 10, 10); + } + } + } +} diff --git a/Trolleybus/Trolleybus/DrawingTrolleybus.cs b/Trolleybus/Trolleybus/DrawingTrolleybus.cs index d963d78..38e8d7a 100644 --- a/Trolleybus/Trolleybus/DrawingTrolleybus.cs +++ b/Trolleybus/Trolleybus/DrawingTrolleybus.cs @@ -11,25 +11,25 @@ namespace Trolleybus /// /// Класс прорисовки и перемещения объекта /// - internal class DrawingTrolleybus + public class DrawingTrolleybus { - public EntityTrolleybus Trolleybus { get; private set; } + public EntityTrolleybus Trolleybus { get; protected set; } /// /// левая координата отрисовки /// - private float _startPosX; + public float _startPosX; /// /// верхняя координата отрисовки /// - private float _startPosY; + public float _startPosY; /// /// левая координата начала отрисовки /// - private float _startX; + public float _startX; /// /// Верхняя координата начала отрисовки /// - private float _startY; + public float _startY; /// /// Ширина окна отрисовки /// @@ -52,10 +52,15 @@ namespace Trolleybus /// Скорость /// Вес автомобиля /// Цвет кузова - public void Init(int speed, float weight, Color bodyColor) + public DrawingTrolleybus(int speed, float weight, Color bodyColor) { - Trolleybus = new EntityTrolleybus(); - Trolleybus.Init(speed, weight, bodyColor); + Trolleybus = new EntityTrolleybus(speed, weight, bodyColor); + } + protected DrawingTrolleybus(int speed, float weight, Color bodyColor, int trolleybusWidth, int trolleybusHeight) + { + Trolleybus = new EntityTrolleybus(speed, weight, bodyColor); + _trolleybusWidth = trolleybusWidth; + _trolleybusHeight = trolleybusHeight; } /// /// Установка позиции @@ -65,12 +70,12 @@ namespace Trolleybus /// Ширина картинки /// Высота картинки // public void SetPosition(int x, int y, int startX, int startY, int width, int height) - public void SetPosition(int x, int y, int startX, int startY, int width, int height) + public void SetPosition(int x, int y, int width, int height) { _startPosX = x; _startPosY = y; - _startX = startX; - _startY = startY; +// _startX = startX; +// _startY = startY; _pictureWidth = width; _pictureHeight = height; } @@ -95,14 +100,16 @@ namespace Trolleybus break; //влево case Direction.Left: - if (_startPosX > _startX) - { - _startPosX -= Trolleybus.Step; +// if (_startPosX > _startX) + if (_startPosX - Trolleybus.Step > 0) + { + _startPosX -= Trolleybus.Step; } break; //вверх case Direction.Up: - if (_startPosY > _startY) +// if (_startPosY > _startY) + if (_startPosY - Trolleybus.Step > 0) { _startPosY -= Trolleybus.Step; } @@ -120,7 +127,7 @@ namespace Trolleybus /// Отрисовка троллейбуса /// /// - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { if (_startPosX < 0 || _startPosY < 0 || !_pictureHeight.HasValue || !_pictureWidth.HasValue) @@ -177,5 +184,10 @@ namespace Trolleybus _startPosY = _pictureHeight.Value - _trolleybusHeight; } } + + public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() + { + return (_startPosX, _startPosY, _startPosX + _trolleybusWidth, _startPosY + _trolleybusHeight); + } } } diff --git a/Trolleybus/Trolleybus/DrawningObject.cs b/Trolleybus/Trolleybus/DrawningObject.cs new file mode 100644 index 0000000..0f8aae8 --- /dev/null +++ b/Trolleybus/Trolleybus/DrawningObject.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Windows.Forms.AxHost; + +namespace Trolleybus +{ + internal class DrawningObject : IDrawningObject + { + private DrawingTrolleybus _trolleybus = null; + + public DrawningObject(DrawingTrolleybus car) + { + _trolleybus = car; + } + + public float Step => _trolleybus?.Trolleybus?.Step ?? 0; + + public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() + { + return _trolleybus?.GetCurrentPosition() ?? default; + } + + public void MoveObject(Direction direction) + { + _trolleybus?.MoveTransport(direction); + } + + public void nothing() + { + throw new NotImplementedException(); + } + + public void SetObject(int x, int y, int width, int height) + { + _trolleybus.SetPosition(x, y, width, height); + } + + void IDrawningObject.DrawningObject(Graphics g) + { + // TODO + } + } +} diff --git a/Trolleybus/Trolleybus/DrawningObjectTrolleybus.cs b/Trolleybus/Trolleybus/DrawningObjectTrolleybus.cs new file mode 100644 index 0000000..2d65aed --- /dev/null +++ b/Trolleybus/Trolleybus/DrawningObjectTrolleybus.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Drawing; + +namespace Trolleybus +{ + internal class DrawningObjectTrolleybus : IDrawningObject + { + private DrawingTrolleybus _trolleybus = null; + + public DrawningObjectTrolleybus(DrawingTrolleybus trolleybus) + { + _trolleybus = trolleybus; + } + + public float Step => _trolleybus?.Trolleybus?.Step ?? 0; + + public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() + { + return _trolleybus?.GetCurrentPosition() ?? default; + } + + public void MoveObject(Direction direction) + { + _trolleybus?.MoveTransport(direction); + } + + public void nothing() + { + throw new NotImplementedException(); + } + + public void SetObject(int x, int y, int width, int height) + { + _trolleybus.SetPosition(x, y, width, height); + } + + void IDrawningObject.DrawningObject(Graphics g) + { + _trolleybus.DrawTransport(g); + } + } +} diff --git a/Trolleybus/Trolleybus/EntitySmallTrolleybus.cs b/Trolleybus/Trolleybus/EntitySmallTrolleybus.cs new file mode 100644 index 0000000..796bace --- /dev/null +++ b/Trolleybus/Trolleybus/EntitySmallTrolleybus.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Drawing; + +namespace Trolleybus +{ + class EntitySmallTrolleybus : EntityTrolleybus + { + /// + /// Дополнительный цвет + /// + public Color DopColor { get; private set; } + /// + /// Признак наличия обвеса + /// + public bool BodyKit { get; private set; } + /// + /// Признак наличия гоночной полосы + /// + public bool Horns { get; private set; } + public bool Battary { get; private set; } + /// + /// Инициализация свойств + /// + /// Скорость + /// Вес автомобиля + /// Цвет кузова + /// Дополнительный цвет + /// Признак наличия обвеса + /// Признак наличия гоночной полосы + /// /// Признак наличия гоночной полосы + public EntitySmallTrolleybus(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool horns, bool battary) : + base(speed, weight, bodyColor) + { + DopColor = dopColor; + BodyKit = bodyKit; + Horns = horns; + Battary = battary; + } + } +} diff --git a/Trolleybus/Trolleybus/EntityTrolleybus.cs b/Trolleybus/Trolleybus/EntityTrolleybus.cs index 686b943..10bb18d 100644 --- a/Trolleybus/Trolleybus/EntityTrolleybus.cs +++ b/Trolleybus/Trolleybus/EntityTrolleybus.cs @@ -10,7 +10,7 @@ namespace Trolleybus /// /// Класс-сущность "Троллейбус" /// - internal class EntityTrolleybus + public class EntityTrolleybus { /// /// Скорость @@ -35,12 +35,12 @@ namespace Trolleybus /// /// /// - public void Init(int speed, float weight, Color bodyColor) + public EntityTrolleybus(int speed, float weight, Color bodyColor) { Random rnd = new Random(); Speed = speed <= 0? rnd.Next(50, 150): speed; Weight = weight <= 0 ? rnd.Next(40, 70) : weight; - bodyColor = bodyColor; + BodyColor = bodyColor; } } -} +} \ No newline at end of file diff --git a/Trolleybus/Trolleybus/Form1.Designer.cs b/Trolleybus/Trolleybus/Form1.Designer.cs index 7160c1d..0c2b32f 100644 --- a/Trolleybus/Trolleybus/Form1.Designer.cs +++ b/Trolleybus/Trolleybus/Form1.Designer.cs @@ -39,6 +39,8 @@ namespace Trolleybus this.buttonLeft = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button(); this.pictureBoxTrolleybus = new System.Windows.Forms.PictureBox(); + this.buttonCreateModif = new System.Windows.Forms.Button(); + this.buttonSelectTrolleybus = new System.Windows.Forms.Button(); this.statusStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxTrolleybus)).BeginInit(); this.SuspendLayout(); @@ -129,17 +131,39 @@ namespace Trolleybus // // pictureBoxTrolleybus // - this.pictureBoxTrolleybus.Location = new System.Drawing.Point(12, 12); + this.pictureBoxTrolleybus.Location = new System.Drawing.Point(0, 0); this.pictureBoxTrolleybus.Name = "pictureBoxTrolleybus"; - this.pictureBoxTrolleybus.Size = new System.Drawing.Size(776, 413); + this.pictureBoxTrolleybus.Size = new System.Drawing.Size(800, 450); this.pictureBoxTrolleybus.TabIndex = 0; this.pictureBoxTrolleybus.TabStop = false; // + // buttonCreateModif + // + this.buttonCreateModif.Location = new System.Drawing.Point(94, 389); + this.buttonCreateModif.Name = "buttonCreateModif"; + this.buttonCreateModif.Size = new System.Drawing.Size(86, 23); + this.buttonCreateModif.TabIndex = 7; + this.buttonCreateModif.Text = "Модификация"; + this.buttonCreateModif.UseVisualStyleBackColor = true; + this.buttonCreateModif.Click += new System.EventHandler(this.buttonCreateModif_Click); + // + // buttonSelectTrolleybus + // + this.buttonSelectTrolleybus.Location = new System.Drawing.Point(578, 390); + this.buttonSelectTrolleybus.Name = "buttonSelectTrolleybus"; + this.buttonSelectTrolleybus.Size = new System.Drawing.Size(75, 23); + this.buttonSelectTrolleybus.TabIndex = 8; + this.buttonSelectTrolleybus.Text = "Выбрать"; + this.buttonSelectTrolleybus.UseVisualStyleBackColor = true; + this.buttonSelectTrolleybus.Click += new System.EventHandler(this.buttonSelectTrolleybus_Click); + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.buttonSelectTrolleybus); + this.Controls.Add(this.buttonCreateModif); this.Controls.Add(this.buttonUp); this.Controls.Add(this.buttonRight); this.Controls.Add(this.buttonLeft); @@ -169,6 +193,8 @@ namespace Trolleybus private System.Windows.Forms.Button buttonLeft; private System.Windows.Forms.Button buttonRight; private System.Windows.Forms.Button buttonUp; + private System.Windows.Forms.Button buttonCreateModif; + private System.Windows.Forms.Button buttonSelectTrolleybus; } } diff --git a/Trolleybus/Trolleybus/Form1.cs b/Trolleybus/Trolleybus/Form1.cs index 07959cc..37c3ae0 100644 --- a/Trolleybus/Trolleybus/Form1.cs +++ b/Trolleybus/Trolleybus/Form1.cs @@ -13,6 +13,7 @@ namespace Trolleybus public partial class Form1 : Form { private DrawingTrolleybus _trolleybus; + public DrawingTrolleybus SelectedTrolleybus { get; private set; } public Form1() { InitializeComponent(); @@ -29,6 +30,15 @@ namespace Trolleybus pictureBoxTrolleybus.Image = bmp; } + private void SetData() + { + Random rnd = new Random(); + _trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); + toolStripStatusLabelSpeed.Text = $"Скорость: {_trolleybus.Trolleybus.Speed}"; + toolStripStatusLabelWeight.Text = $"Вес: {_trolleybus.Trolleybus.Weight}"; + toolStripStatusLabelBodyColor.Text = $"Цвет: {_trolleybus.Trolleybus.BodyColor.Name}"; + } + /// /// Обработка нажатия "Создать" /// @@ -37,14 +47,14 @@ namespace Trolleybus private void buttonCreate_Click(object sender, EventArgs e) { - Random rnd = new Random(); - _trolleybus = new DrawingTrolleybus(); - _trolleybus.Init(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); - _trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), Location.X, Location.Y, pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); -// _trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); - toolStripStatusLabelSpeed.Text = $"Скорость: {_trolleybus.Trolleybus.Speed}"; - toolStripStatusLabelWeight.Text = $"Вес: {_trolleybus.Trolleybus.Weight}"; - toolStripStatusLabelBodyColor.Text = $"Цвет: {_trolleybus.Trolleybus.BodyColor.Name}"; + Random random = new(); + Color myColor = new Color(); + ColorDialog MyDialog = new ColorDialog(); + if (MyDialog.ShowDialog() == DialogResult.OK) + myColor = MyDialog.Color; + _trolleybus = new DrawingTrolleybus(random.Next(30, 50), random.Next(1000, 2000), + myColor); + SetData(); Draw(); } @@ -74,5 +84,31 @@ namespace Trolleybus _trolleybus?.ChangeBorders(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height); Draw(); } + + private void buttonCreateModif_Click(object sender, EventArgs e) + { + Random random = new(); + Color firstColor = new Color(); + Color secondColor = new Color(); + ColorDialog MyDialog = new ColorDialog(); + if (MyDialog.ShowDialog() == DialogResult.OK) + firstColor = MyDialog.Color; + MyDialog = new ColorDialog(); + if (MyDialog.ShowDialog() == DialogResult.OK) + secondColor = MyDialog.Color; + _trolleybus = new DrawningSmallTrolleybus(random.Next(100, 300), random.Next(1000, 2000), + 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))); + + SetData(); + Draw(); + } + + private void buttonSelectTrolleybus_Click(object sender, EventArgs e) + { + SelectedTrolleybus = _trolleybus; + DialogResult = DialogResult.OK; + } } } diff --git a/Trolleybus/Trolleybus/FormMap.Designer.cs b/Trolleybus/Trolleybus/FormMap.Designer.cs new file mode 100644 index 0000000..97c3940 --- /dev/null +++ b/Trolleybus/Trolleybus/FormMap.Designer.cs @@ -0,0 +1,199 @@ +namespace Trolleybus +{ + partial class FormMap + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.statusStrip1 = new System.Windows.Forms.StatusStrip(); + this.toolStripStatusLabelSpeed = new System.Windows.Forms.ToolStripStatusLabel(); + this.toolStripStatusLabelWeight = new System.Windows.Forms.ToolStripStatusLabel(); + this.toolStripStatusLabelBodyColor = new System.Windows.Forms.ToolStripStatusLabel(); + this.buttonCreate = new System.Windows.Forms.Button(); + this.buttonCreateModif = new System.Windows.Forms.Button(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.buttonUp = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); + this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); + this.pictureBoxTrolleybus = new System.Windows.Forms.PictureBox(); + this.statusStrip1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxTrolleybus)).BeginInit(); + this.SuspendLayout(); + // + // statusStrip1 + // + this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripStatusLabelSpeed, + this.toolStripStatusLabelWeight, + this.toolStripStatusLabelBodyColor}); + this.statusStrip1.Location = new System.Drawing.Point(0, 428); + this.statusStrip1.Name = "statusStrip1"; + this.statusStrip1.Size = new System.Drawing.Size(800, 22); + this.statusStrip1.TabIndex = 1; + this.statusStrip1.Text = "statusStrip1"; + // + // toolStripStatusLabelSpeed + // + this.toolStripStatusLabelSpeed.Name = "toolStripStatusLabelSpeed"; + this.toolStripStatusLabelSpeed.Size = new System.Drawing.Size(59, 17); + this.toolStripStatusLabelSpeed.Text = "Скорость"; + // + // toolStripStatusLabelWeight + // + this.toolStripStatusLabelWeight.Name = "toolStripStatusLabelWeight"; + this.toolStripStatusLabelWeight.Size = new System.Drawing.Size(26, 17); + this.toolStripStatusLabelWeight.Text = "Вес"; + // + // toolStripStatusLabelBodyColor + // + this.toolStripStatusLabelBodyColor.Name = "toolStripStatusLabelBodyColor"; + this.toolStripStatusLabelBodyColor.Size = new System.Drawing.Size(33, 17); + this.toolStripStatusLabelBodyColor.Text = "Цвет"; + // + // buttonCreate + // + this.buttonCreate.Location = new System.Drawing.Point(12, 390); + this.buttonCreate.Name = "buttonCreate"; + this.buttonCreate.Size = new System.Drawing.Size(75, 23); + this.buttonCreate.TabIndex = 2; + this.buttonCreate.Text = "Создать"; + this.buttonCreate.UseVisualStyleBackColor = true; + this.buttonCreate.Click += new System.EventHandler(this.ButtonCreate_Click); + // + // buttonCreateModif + // + this.buttonCreateModif.Location = new System.Drawing.Point(94, 389); + this.buttonCreateModif.Name = "buttonCreateModif"; + this.buttonCreateModif.Size = new System.Drawing.Size(86, 23); + this.buttonCreateModif.TabIndex = 7; + this.buttonCreateModif.Text = "Модификация"; + this.buttonCreateModif.UseVisualStyleBackColor = true; + this.buttonCreateModif.Click += new System.EventHandler(this.ButtonCreateModif_Click); + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "Простая карта"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(12, 12); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(121, 21); + this.comboBoxSelectorMap.TabIndex = 8; + // + // buttonUp + // + this.buttonUp.BackgroundImage = global::Trolleybus.Properties.Resources.up30; + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonUp.Location = new System.Drawing.Point(722, 353); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(30, 30); + this.buttonUp.TabIndex = 6; + this.buttonUp.UseVisualStyleBackColor = true; + this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonRight + // + this.buttonRight.BackgroundImage = global::Trolleybus.Properties.Resources.right30; + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonRight.Location = new System.Drawing.Point(758, 389); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(30, 30); + this.buttonRight.TabIndex = 5; + this.buttonRight.UseVisualStyleBackColor = true; + this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonLeft + // + this.buttonLeft.BackgroundImage = global::Trolleybus.Properties.Resources.left30; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonLeft.Location = new System.Drawing.Point(686, 389); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(30, 30); + this.buttonLeft.TabIndex = 4; + this.buttonLeft.UseVisualStyleBackColor = true; + this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonDown + // + this.buttonDown.BackgroundImage = global::Trolleybus.Properties.Resources.down30; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonDown.Location = new System.Drawing.Point(722, 389); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(30, 30); + this.buttonDown.TabIndex = 3; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + // + // pictureBoxTrolleybus + // + this.pictureBoxTrolleybus.Location = new System.Drawing.Point(12, 12); + this.pictureBoxTrolleybus.Name = "pictureBoxTrolleybus"; + this.pictureBoxTrolleybus.Size = new System.Drawing.Size(776, 413); + this.pictureBoxTrolleybus.TabIndex = 0; + this.pictureBoxTrolleybus.TabStop = false; + // + // FormMap + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.comboBoxSelectorMap); + this.Controls.Add(this.buttonCreateModif); + this.Controls.Add(this.buttonUp); + this.Controls.Add(this.buttonRight); + this.Controls.Add(this.buttonLeft); + this.Controls.Add(this.buttonDown); + this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.statusStrip1); + this.Controls.Add(this.pictureBoxTrolleybus); + this.Name = "FormMap"; + this.Text = "Карта"; + this.statusStrip1.ResumeLayout(false); + this.statusStrip1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxTrolleybus)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.PictureBox pictureBoxTrolleybus; + private System.Windows.Forms.StatusStrip statusStrip1; + private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelSpeed; + private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelWeight; + private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelBodyColor; + private System.Windows.Forms.Button buttonCreate; + private System.Windows.Forms.Button buttonDown; + private System.Windows.Forms.Button buttonLeft; + private System.Windows.Forms.Button buttonRight; + private System.Windows.Forms.Button buttonUp; + private System.Windows.Forms.Button buttonCreateModif; + private System.Windows.Forms.ComboBox comboBoxSelectorMap; + } +} \ No newline at end of file diff --git a/Trolleybus/Trolleybus/FormMap.cs b/Trolleybus/Trolleybus/FormMap.cs new file mode 100644 index 0000000..b8b47c9 --- /dev/null +++ b/Trolleybus/Trolleybus/FormMap.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Trolleybus +{ + public partial class FormMap : Form + { + private AbstractMap _abstractMap; + public FormMap() + { + InitializeComponent(); + _abstractMap = new SimpleMap(); + } + /// + /// Заполнение информации по объекту + /// + /// + private void SetData(DrawingTrolleybus trolleybus) + { + toolStripStatusLabelSpeed.Text = $"Скорость: {trolleybus.Trolleybus.Speed}"; + toolStripStatusLabelWeight.Text = $"Вес: {trolleybus.Trolleybus.Weight}"; + toolStripStatusLabelBodyColor.Text = $"Цвет: {trolleybus.Trolleybus.BodyColor.Name}"; + pictureBoxTrolleybus.Image = _abstractMap.CreateMap(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height, + new DrawningObjectTrolleybus(trolleybus)); + } + /// + /// Обработка нажатия кнопки "Создать" + /// + /// + /// + private void ButtonCreate_Click(object sender, EventArgs e) + { + Random rnd = new Random(); + var trolleybus = new DrawingTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256))); + SetData(trolleybus); + } + /// + /// Изменение размеров формы + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) + { + //получаем имя кнопки + string name = ((Button)sender)?.Name ?? string.Empty; + Direction dir = Direction.None; + switch (name) + { + case "buttonUp": + dir = Direction.Up; + break; + case "buttonDown": + dir = Direction.Down; + break; + case "buttonLeft": + dir = Direction.Left; + break; + case "buttonRight": + dir = Direction.Right; + break; + } + pictureBoxTrolleybus.Image = _abstractMap?.MoveObject(dir); + } + /// + /// Обработка нажатия кнопки "Модификация" + /// + /// + /// + private void ButtonCreateModif_Click(object sender, EventArgs e) + { + Random rnd = new Random(); + var trolleybus = new DrawningSmallTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000), + Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), + Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), + Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2))); + SetData(trolleybus); + } + /// + /// Смена карты + /// + /// + /// + private void comboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + { + switch (comboBoxSelectorMap.Text) + { + case "Простая карта": + _abstractMap = new SimpleMap(); + break; + } + } + } +} diff --git a/Trolleybus/Trolleybus/FormMap.resx b/Trolleybus/Trolleybus/FormMap.resx new file mode 100644 index 0000000..174ebc7 --- /dev/null +++ b/Trolleybus/Trolleybus/FormMap.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.Designer.cs b/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.Designer.cs new file mode 100644 index 0000000..992de1d --- /dev/null +++ b/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.Designer.cs @@ -0,0 +1,210 @@ +namespace Trolleybus +{ + partial class FormMapWithSetTrolleybus + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.groupBox1 = new System.Windows.Forms.GroupBox(); + this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); + this.buttonUp = new System.Windows.Forms.Button(); + this.buttonShowOnMap = new System.Windows.Forms.Button(); + this.buttonShowStorage = new System.Windows.Forms.Button(); + this.buttonRemoveTrolleybus = new System.Windows.Forms.Button(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.buttonAddTrolleybus = new System.Windows.Forms.Button(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.pictureBox = new System.Windows.Forms.PictureBox(); + this.groupBox1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.SuspendLayout(); + // + // groupBox1 + // + this.groupBox1.Controls.Add(this.buttonLeft); + this.groupBox1.Controls.Add(this.buttonRight); + this.groupBox1.Controls.Add(this.buttonDown); + this.groupBox1.Controls.Add(this.buttonUp); + this.groupBox1.Controls.Add(this.buttonShowOnMap); + this.groupBox1.Controls.Add(this.buttonShowStorage); + this.groupBox1.Controls.Add(this.buttonRemoveTrolleybus); + this.groupBox1.Controls.Add(this.maskedTextBoxPosition); + this.groupBox1.Controls.Add(this.buttonAddTrolleybus); + this.groupBox1.Controls.Add(this.comboBoxSelectorMap); + this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; + this.groupBox1.Location = new System.Drawing.Point(600, 0); + this.groupBox1.Name = "groupBox1"; + this.groupBox1.Size = new System.Drawing.Size(200, 450); + this.groupBox1.TabIndex = 0; + this.groupBox1.TabStop = false; + this.groupBox1.Text = "Инструменты"; + // + // buttonLeft + // + this.buttonLeft.BackgroundImage = global::Trolleybus.Properties.Resources.left30; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonLeft.Location = new System.Drawing.Point(59, 402); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(32, 31); + this.buttonLeft.TabIndex = 9; + this.buttonLeft.UseVisualStyleBackColor = true; + this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonRight + // + this.buttonRight.BackgroundImage = global::Trolleybus.Properties.Resources.right30; + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonRight.Location = new System.Drawing.Point(135, 402); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(32, 31); + this.buttonRight.TabIndex = 8; + this.buttonRight.UseVisualStyleBackColor = true; + this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonDown + // + this.buttonDown.BackgroundImage = global::Trolleybus.Properties.Resources.down30; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonDown.Location = new System.Drawing.Point(97, 402); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(32, 31); + this.buttonDown.TabIndex = 7; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonUp + // + this.buttonUp.BackgroundImage = global::Trolleybus.Properties.Resources.up30; + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonUp.Location = new System.Drawing.Point(97, 365); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(32, 31); + this.buttonUp.TabIndex = 6; + this.buttonUp.UseVisualStyleBackColor = true; + this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonShowOnMap + // + this.buttonShowOnMap.Location = new System.Drawing.Point(7, 256); + this.buttonShowOnMap.Name = "buttonShowOnMap"; + this.buttonShowOnMap.Size = new System.Drawing.Size(181, 23); + this.buttonShowOnMap.TabIndex = 5; + this.buttonShowOnMap.Text = "Посмотреть карту"; + this.buttonShowOnMap.UseVisualStyleBackColor = true; + this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); + // + // buttonShowStorage + // + this.buttonShowStorage.Location = new System.Drawing.Point(7, 216); + this.buttonShowStorage.Name = "buttonShowStorage"; + this.buttonShowStorage.Size = new System.Drawing.Size(181, 23); + this.buttonShowStorage.TabIndex = 4; + this.buttonShowStorage.Text = "Посмотреть хранилище"; + this.buttonShowStorage.UseVisualStyleBackColor = true; + this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); + // + // buttonRemoveTrolleybus + // + this.buttonRemoveTrolleybus.Location = new System.Drawing.Point(6, 170); + this.buttonRemoveTrolleybus.Name = "buttonRemoveTrolleybus"; + this.buttonRemoveTrolleybus.Size = new System.Drawing.Size(182, 23); + this.buttonRemoveTrolleybus.TabIndex = 3; + this.buttonRemoveTrolleybus.Text = "Удалить троллейбус"; + this.buttonRemoveTrolleybus.UseVisualStyleBackColor = true; + this.buttonRemoveTrolleybus.Click += new System.EventHandler(this.ButtonRemoveTrolleybus_Click); + // + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Location = new System.Drawing.Point(7, 128); + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(181, 20); + this.maskedTextBoxPosition.TabIndex = 2; + // + // buttonAddTrolleybus + // + this.buttonAddTrolleybus.Location = new System.Drawing.Point(7, 85); + this.buttonAddTrolleybus.Name = "buttonAddTrolleybus"; + this.buttonAddTrolleybus.Size = new System.Drawing.Size(181, 23); + this.buttonAddTrolleybus.TabIndex = 1; + this.buttonAddTrolleybus.Text = "Добавить троллейбус"; + this.buttonAddTrolleybus.UseVisualStyleBackColor = true; + this.buttonAddTrolleybus.Click += new System.EventHandler(this.ButtonAddTrolleybus_Click); + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "Простая карта", + "Сложная карта"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 42); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(182, 21); + this.comboBoxSelectorMap.TabIndex = 0; + this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged); + // + // pictureBox + // + this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBox.Location = new System.Drawing.Point(0, 0); + this.pictureBox.Name = "pictureBox"; + this.pictureBox.Size = new System.Drawing.Size(600, 450); + this.pictureBox.TabIndex = 1; + this.pictureBox.TabStop = false; + // + // FormMapWithSetTrolleybus + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.pictureBox); + this.Controls.Add(this.groupBox1); + this.Name = "FormMapWithSetTrolleybus"; + this.Text = "FormMapWithSetTrolleybus"; + this.groupBox1.ResumeLayout(false); + this.groupBox1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.GroupBox groupBox1; + private System.Windows.Forms.Button buttonAddTrolleybus; + private System.Windows.Forms.ComboBox comboBoxSelectorMap; + private System.Windows.Forms.PictureBox pictureBox; + private System.Windows.Forms.Button buttonLeft; + private System.Windows.Forms.Button buttonRight; + private System.Windows.Forms.Button buttonDown; + private System.Windows.Forms.Button buttonUp; + private System.Windows.Forms.Button buttonShowOnMap; + private System.Windows.Forms.Button buttonShowStorage; + private System.Windows.Forms.Button buttonRemoveTrolleybus; + private System.Windows.Forms.MaskedTextBox maskedTextBoxPosition; + } +} \ No newline at end of file diff --git a/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs b/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs new file mode 100644 index 0000000..caac312 --- /dev/null +++ b/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.cs @@ -0,0 +1,147 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Trolleybus +{ + public partial class FormMapWithSetTrolleybus : Form + { + public FormMapWithSetTrolleybus() + { + InitializeComponent(); + } + private MapWithSetTrolleybusGeneric _mapTrolleybusCollectionGeneric; + private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + { + AbstractMap map = null; + switch (comboBoxSelectorMap.Text) + { + case "Простая карта": + map = new SimpleMap(); + break; + case "Сложная карта": + map = new AutoStopMap(); + break; + } + if (map != null) + { + _mapTrolleybusCollectionGeneric = new MapWithSetTrolleybusGeneric( + pictureBox.Width, pictureBox.Height, map); + } + else + { + _mapTrolleybusCollectionGeneric = null; + } + } + private void ButtonAddTrolleybus_Click(object sender, EventArgs e) + { + if (_mapTrolleybusCollectionGeneric == null) + { + return; + } + Form1 form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + DrawningObjectTrolleybus trolleybus = new(form.SelectedTrolleybus); + if (_mapTrolleybusCollectionGeneric + trolleybus !=-1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapTrolleybusCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + /// + /// Удаление объекта + /// + /// + /// + private void ButtonRemoveTrolleybus_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) + { + return; + } + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxPosition.Text); + if (_mapTrolleybusCollectionGeneric - pos !=null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _mapTrolleybusCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + /// + /// Вывод набора + /// + /// + /// + private void ButtonShowStorage_Click(object sender, EventArgs e) + { + if (_mapTrolleybusCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapTrolleybusCollectionGeneric.ShowSet(); + } + /// + /// Вывод карты + /// + /// + /// + private void ButtonShowOnMap_Click(object sender, EventArgs e) + { + if (_mapTrolleybusCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapTrolleybusCollectionGeneric.ShowOnMap(); + } + /// + /// Перемещение + /// + /// + /// + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_mapTrolleybusCollectionGeneric == null) + { + return; + } + //получаем имя кнопки + string name = ((Button)sender)?.Name ?? string.Empty; + Direction dir = Direction.None; + switch (name) + { + case "buttonUp": + dir = Direction.Up; + break; + case "buttonDown": + dir = Direction.Down; + break; + case "buttonLeft": + dir = Direction.Left; + break; + case "buttonRight": + dir = Direction.Right; + break; + } + pictureBox.Image = _mapTrolleybusCollectionGeneric.MoveObject(dir); + } + } +} diff --git a/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.resx b/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/Trolleybus/Trolleybus/FormMapWithSetTrolleybus.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Trolleybus/Trolleybus/IDrawingObject.cs b/Trolleybus/Trolleybus/IDrawingObject.cs new file mode 100644 index 0000000..143c459 --- /dev/null +++ b/Trolleybus/Trolleybus/IDrawingObject.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Drawing; + +namespace Trolleybus +{ + internal interface IDrawningObject + { + /// + /// Шаг перемещения объекта + /// + public float Step { get; } + /// + /// Установка позиции объекта + /// + /// Координата X + /// Координата Y + /// Ширина полотна + /// Высота полотна + void SetObject(int x, int y, int width, int height); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + /// + void MoveObject(Direction direction); + /// + /// Отрисовка объекта + /// + /// + void DrawningObject(Graphics g); + /// + /// Получение текущей позиции объекта + /// + /// + void nothing(); + (float Left, float Right, float Top, float Bottom) GetCurrentPosition(); + } +} diff --git a/Trolleybus/Trolleybus/MapWithSetTrolleybusGeneric.cs b/Trolleybus/Trolleybus/MapWithSetTrolleybusGeneric.cs new file mode 100644 index 0000000..361e541 --- /dev/null +++ b/Trolleybus/Trolleybus/MapWithSetTrolleybusGeneric.cs @@ -0,0 +1,188 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Drawing; + +namespace Trolleybus +{ + internal class MapWithSetTrolleybusGeneric + where T : class, IDrawningObject + where U : AbstractMap + { + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Размер занимаемого объектом места (ширина) + /// + private readonly int _placeSizeWidth = 210; + /// + /// Размер занимаемого объектом места (высота) + /// + private readonly int _placeSizeHeight = 90; + /// + /// Набор объектов + /// + private readonly SetTrolleybusGeneric _setTrolleybus; + /// + /// Карта + /// + private readonly U _map; + /// + /// Конструктор + /// + /// + /// + /// + public MapWithSetTrolleybusGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setTrolleybus = new SetTrolleybusGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + /// + /// Перегрузка оператора сложения + /// + /// + /// + /// + public static int operator +(MapWithSetTrolleybusGeneric map, T trolleybus) + { + return map._setTrolleybus.Insert(trolleybus); + } + /// + /// Перегрузка оператора вычитания + /// + /// + /// + /// + public static T operator -(MapWithSetTrolleybusGeneric map, int position) + { + return map._setTrolleybus.Remove(position); + } + /// + /// Вывод всего набора объектов + /// + /// + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawTrolleybus(gr); + return bmp; + } + /// + /// Просмотр объекта на карте + /// + /// + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setTrolleybus.Count; i++) + { + var car = _setTrolleybus.Get(i); + if (car != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, car); + } + } + return new(_pictureWidth, _pictureHeight); + } + /// + /// Перемещение объекта по крате + /// + /// + /// + public Bitmap MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new(_pictureWidth, _pictureHeight); + } + /// + /// "Взбалтываем" набор, чтобы все элементы оказались в начале + /// + private void Shaking() + { + int j = _setTrolleybus.Count - 1; + for (int i = 0; i < _setTrolleybus.Count; i++) + { + if (_setTrolleybus.Get(i) == null) + { + for (; j > i; j--) + { + var car = _setTrolleybus.Get(j); + if (car != null) + { + _setTrolleybus.Insert(car, i); + _setTrolleybus.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + /// + /// Метод отрисовки фона + /// + /// + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Black, 3); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + {//линия рамзетки места + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight); + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight + 10, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + 10); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + } + /// + /// Метод прорисовки объектов + /// + /// + private void DrawTrolleybus(Graphics g) + { + int xForLocomotive = _pictureWidth - 2 * _placeSizeWidth + 60; + int yForLocomotive = 10; + int countInRow = 0; + for (int i = 0; i < _setTrolleybus.Count; i++) + { + if (countInRow >= _pictureWidth / (_placeSizeWidth + 30)) + { + xForLocomotive = _pictureWidth - _placeSizeWidth + 40; + yForLocomotive += _placeSizeHeight; + countInRow = 0; + } + if (_setTrolleybus.Get(i) != null) + { + T locomotive = _setTrolleybus.Get(i); + locomotive.SetObject(xForLocomotive, yForLocomotive, _pictureWidth, _pictureHeight); + locomotive.DrawningObject(g); + } + xForLocomotive -= _placeSizeWidth + 30; + countInRow++; + } + + } + } +} diff --git a/Trolleybus/Trolleybus/Program.cs b/Trolleybus/Trolleybus/Program.cs index 3a773ae..7ada713 100644 --- a/Trolleybus/Trolleybus/Program.cs +++ b/Trolleybus/Trolleybus/Program.cs @@ -16,7 +16,7 @@ namespace Trolleybus { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new Form1()); + Application.Run(new FormMapWithSetTrolleybus()); } } } diff --git a/Trolleybus/Trolleybus/SetTrolleybusGeneric.cs b/Trolleybus/Trolleybus/SetTrolleybusGeneric.cs new file mode 100644 index 0000000..49eb79c --- /dev/null +++ b/Trolleybus/Trolleybus/SetTrolleybusGeneric.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Trolleybus +{ + // Параметризованный набор объектов + internal class SetTrolleybusGeneric + where T : class + { + // Массив объектов, которые храним + private readonly T[] _places; + // Количество объектов в массиве + public int Count => _places.Length; + // Конструктор + public SetTrolleybusGeneric(int count) + { + _places = new T[count]; + } + // Добавление объекта в набор + public int Insert(T trolleybus) + { + // вставка в начало набора + return Insert(trolleybus, 0); + } + // Добавление объекта в набор на конкретную позицию + public int Insert(T trolleybus, int position) + { + // проверка позиции + if (position >= _places.Length || position < 0) + return -1; + //проверка, что элемент массива по этой позиции пустой, если нет, то + if (_places[position] == null) + { + _places[position] = trolleybus; + return position; + } + //проверка, что после вставляемого элемента в массиве есть пустой элемент + int findEmptyPos = -1; + + for (int i = position + 1; i < Count; i++) + { + if (_places[i] == null) + { + findEmptyPos = i; + break; + } + } + if (findEmptyPos < 0) return -1; + + //сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента + for (int i = findEmptyPos; i > position; i--) + { + _places[i] = _places[i - 1]; + } + // вставка по позиции + _places[position] = trolleybus; + return position; + } + // Удаление объекта из набора с конкретной позиции + public T Remove(int position) + { + // проверка позиции + if (position >= _places.Length || position < 0) return null; + // удаление объекта из массива, присовив элементу массива значение null + T temp = _places[position]; + _places[position] = null; + return temp; + } + // Получение объекта из набора по позиции + public T Get(int position) + { + // проверка позиции + if (position >= _places.Length || position < 0) + return null; + return _places[position]; + } + } +} \ No newline at end of file diff --git a/Trolleybus/Trolleybus/SimpleMap.cs b/Trolleybus/Trolleybus/SimpleMap.cs new file mode 100644 index 0000000..0f8af7f --- /dev/null +++ b/Trolleybus/Trolleybus/SimpleMap.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Drawing; + +namespace Trolleybus +{ + class SimpleMap : AbstractMap + { + /// + /// Цвет участка закрытого + /// + private readonly Brush barrierColor = new SolidBrush(Color.Black); + /// + /// Цвет участка открытого + /// + private readonly Brush roadColor = new SolidBrush(Color.Gray); + + protected override void DrawBarrierPart(Graphics g, int i, int j) + { + g.FillRectangle(barrierColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1)); + } + protected override void DrawRoadPart(Graphics g, int i, int j) + { + g.FillRectangle(roadColor, i * _size_x, j * _size_y, i * (_size_x + 1), j * (_size_y + 1)); + } + protected override void GenerateMap() + { + _map = new int[100, 100]; + _size_x = (float)_width / _map.GetLength(0); + _size_y = (float)_height / _map.GetLength(1); + int counter = 0; + for (int i = 0; i < _map.GetLength(0); ++i) + { + for (int j = 0; j < _map.GetLength(1); ++j) + { + _map[i, j] = _freeRoad; + } + } + while (counter < 50) + { + int x = _random.Next(0, 100); + int y = _random.Next(0, 100); + if (_map[x, y] == _freeRoad) + { + _map[x, y] = _barrier; + counter++; + } + } + } + } +} diff --git a/Trolleybus/Trolleybus/Trolleybus.csproj b/Trolleybus/Trolleybus/Trolleybus.csproj index ce41cd2..ddd44f9 100644 --- a/Trolleybus/Trolleybus/Trolleybus.csproj +++ b/Trolleybus/Trolleybus/Trolleybus.csproj @@ -11,6 +11,7 @@ v4.7.2 512 true + 9.0 true @@ -46,8 +47,13 @@ + + + + + Form @@ -55,11 +61,34 @@ Form1.cs + + Form + + + FormMap.cs + + + Form + + + FormMapWithSetTrolleybus.cs + + + + + + Form1.cs + + FormMap.cs + + + FormMapWithSetTrolleybus.cs + ResXFileCodeGenerator Resources.Designer.cs diff --git a/Trolleybus/Trolleybus/save/Trolleybus.csproj b/Trolleybus/Trolleybus/save/Trolleybus.csproj new file mode 100644 index 0000000..af2fa34 --- /dev/null +++ b/Trolleybus/Trolleybus/save/Trolleybus.csproj @@ -0,0 +1,114 @@ + + + + + Debug + AnyCPU + {07E18270-5508-4D1A-A699-8B170C1E8502} + WinExe + Trolleybus + Trolleybus + v4.7.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + Form + + + Form1.cs + + + Form + + + FormMap.cs + + + + + + + Form1.cs + + + FormMap.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + True + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + + + + + + + + + + + + + \ No newline at end of file