diff --git a/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.Designer.cs index 0c2e372..d95457b 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.Designer.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.Designer.cs @@ -37,6 +37,7 @@ buttonCreatDoubleDeckerDus = new Button(); buttonStep_Click = new Button(); comboBoxStrategy = new ComboBox(); + buttonSelectBus = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxBus).BeginInit(); SuspendLayout(); // @@ -49,7 +50,6 @@ pictureBoxBus.SizeMode = PictureBoxSizeMode.AutoSize; pictureBoxBus.TabIndex = 0; pictureBoxBus.TabStop = false; - // // buttonCreateBus // @@ -144,11 +144,22 @@ comboBoxStrategy.Size = new Size(121, 23); comboBoxStrategy.TabIndex = 8; // + // buttonSelectBus + // + buttonSelectBus.Location = new Point(277, 418); + buttonSelectBus.Name = "buttonSelectBus"; + buttonSelectBus.Size = new Size(109, 31); + buttonSelectBus.TabIndex = 9; + buttonSelectBus.Text = "Выбрать"; + buttonSelectBus.UseVisualStyleBackColor = true; + buttonSelectBus.Click += buttonSelectBus_Click; + // // DoubleDeckerBus // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(884, 461); + Controls.Add(buttonSelectBus); Controls.Add(comboBoxStrategy); Controls.Add(buttonStep_Click); Controls.Add(buttonCreatDoubleDeckerDus); @@ -177,5 +188,6 @@ private Button buttonCreatDoubleDeckerDus; private Button buttonStep_Click; private ComboBox comboBoxStrategy; + private Button buttonSelectBus; } } \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.cs index e4e5994..113dd87 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.cs @@ -6,10 +6,13 @@ namespace DoubleDeckerBus public partial class DoubleDeckerBus : Form { private DrawningBus? _drawningBus; - private AbstractStrategy? _abstractStrategy; + private AbstractStrategy? _strategy; + public DrawningBus? SelectedBus { get; private set; } public DoubleDeckerBus() { InitializeComponent(); + _strategy = null; + SelectedBus = null; } private void Draw() { @@ -23,31 +26,45 @@ namespace DoubleDeckerBus _drawningBus.DrawTransport(gr); pictureBoxBus.Image = bmp; } - private void buttonCreateBus_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } _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)), + random.Next(1000, 3000), color, pictureBoxBus.Width, pictureBoxBus.Height); _drawningBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } - private void buttonCreatDoubleDeckerDus_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialogColor = new(); + if (dialogColor.ShowDialog() == DialogResult.OK) + { + color = dialogColor.Color; + } + Color dopColor = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialogDopColor = new(); + if (dialogDopColor.ShowDialog() == DialogResult.OK) + { + dopColor = dialogDopColor.Color; + } _drawningBus = new DrawningDoubleDeckerBus(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)), + random.Next(1000, 3000), color, + dopColor, Convert.ToBoolean(random.Next(0, 2)), + Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), - Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxBus.Width, pictureBoxBus.Height); _drawningBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); @@ -77,7 +94,6 @@ namespace DoubleDeckerBus } Draw(); } - private void buttonStep_Click_Click(object sender, EventArgs e) { if (_drawningBus == null) @@ -86,34 +102,37 @@ namespace DoubleDeckerBus } if (comboBoxStrategy.Enabled) { - _abstractStrategy = comboBoxStrategy.SelectedIndex - switch + _strategy = comboBoxStrategy.SelectedIndex switch { 0 => new MoveToCenter(), 1 => new MoveToBorder(), _ => null, }; - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.SetData(new - DrawningObjectBus(_drawningBus), pictureBoxBus.Width, - pictureBoxBus.Height); - comboBoxStrategy.Enabled = false; + _strategy.SetData(_drawningBus.GetMoveableObject, + pictureBoxBus.Width, pictureBoxBus.Height); } - if (_abstractStrategy == null) + if (_strategy == null) { return; } - _abstractStrategy.MakeStep(); + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); Draw(); - if (_abstractStrategy.GetStatus() == Status.Finish) + if (_strategy.GetStatus() == Status.Finish) { comboBoxStrategy.Enabled = true; - _abstractStrategy = null; + _strategy = null; } } + private void buttonSelectBus_Click(object sender, EventArgs e) + { + SelectedBus = _drawningBus; + DialogResult = DialogResult.OK; + } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningBus.cs index 7940935..5e3eb5f 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/DrawningObjects/DrawningBus.cs @@ -1,4 +1,5 @@ using DoubleDeckerBus.Entities; +using DoubleDeckerBus.MovementStrategy; using System; using System.Collections.Generic; using System.Linq; @@ -10,6 +11,7 @@ namespace DoubleDeckerBus.DrawningObjects public class DrawningBus { public EntityBus? EntityBus { get; protected set; } + public IMoveableObject GetMoveableObject => new DrawningObjectBus(this); private int? _pictureWidth; private int? _pictureHeight; protected int _startPosX; @@ -20,8 +22,7 @@ namespace DoubleDeckerBus.DrawningObjects public int GetPosY => _startPosY; public int GetWidth => _busWidth; public int GetHeight => _busHeight; - public DrawningBus(int speed, float weight, Color bodyColor, int - width, int height) + public DrawningBus(int speed, float weight, Color bodyColor, int width, int height) { if (width < _busWidth || height < _busHeight) { @@ -80,7 +81,7 @@ namespace DoubleDeckerBus.DrawningObjects return; } switch (direction) - { + { //влево case DirectionBus.Left: _startPosX -= (int)EntityBus.Step; @@ -99,7 +100,6 @@ namespace DoubleDeckerBus.DrawningObjects break; } } - public virtual void DrawTransport(Graphics g) { if (EntityBus == null) @@ -112,7 +112,6 @@ namespace DoubleDeckerBus.DrawningObjects } Pen pen = new(Color.Black); - g.DrawRectangle(pen, _startPosX - 1, _startPosY + 11, 100, 30); Brush brBodyColor = new SolidBrush(EntityBus.BodyColor); diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs new file mode 100644 index 0000000..c3b8367 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs @@ -0,0 +1,129 @@ +namespace DoubleDeckerBus +{ + partial class FormBusCollection + { + /// + /// 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() + { + buttonAddBus = new Button(); + buttonRemoveBus = new Button(); + ButtonRefreshCollection = new Button(); + maskedTextBoxNumber = new MaskedTextBox(); + pictureBoxCollection = new PictureBox(); + panel = new Panel(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + panel.SuspendLayout(); + SuspendLayout(); + // + // buttonAddBus + // + buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonAddBus.Location = new Point(14, 3); + buttonAddBus.Name = "buttonAddBus"; + buttonAddBus.Size = new Size(138, 31); + buttonAddBus.TabIndex = 0; + buttonAddBus.Text = "Добавить автобус"; + buttonAddBus.UseVisualStyleBackColor = true; + buttonAddBus.Click += buttonAddBus_Click; + // + // buttonRemoveBus + // + buttonRemoveBus.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonRemoveBus.Location = new Point(14, 69); + buttonRemoveBus.Name = "buttonRemoveBus"; + buttonRemoveBus.Size = new Size(138, 31); + buttonRemoveBus.TabIndex = 1; + buttonRemoveBus.Text = "Удалить автобус"; + buttonRemoveBus.UseVisualStyleBackColor = true; + buttonRemoveBus.Click += buttonRemoveBus_Click; + // + // ButtonRefreshCollection + // + ButtonRefreshCollection.Anchor = AnchorStyles.Top | AnchorStyles.Right; + ButtonRefreshCollection.Location = new Point(14, 106); + ButtonRefreshCollection.Name = "ButtonRefreshCollection"; + ButtonRefreshCollection.Size = new Size(138, 31); + ButtonRefreshCollection.TabIndex = 2; + ButtonRefreshCollection.Text = "Обновить коллекцию"; + ButtonRefreshCollection.UseVisualStyleBackColor = true; + ButtonRefreshCollection.Click += ButtonRefreshCollection_Click; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Anchor = AnchorStyles.Top | AnchorStyles.Right; + maskedTextBoxNumber.Location = new Point(14, 40); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(138, 23); + maskedTextBoxNumber.TabIndex = 3; + maskedTextBoxNumber.Text = "_"; + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new Point(2, 2); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(703, 457); + pictureBoxCollection.TabIndex = 4; + pictureBoxCollection.TabStop = false; + // + // panel + // + panel.Anchor = AnchorStyles.Top | AnchorStyles.Right; + panel.Controls.Add(buttonAddBus); + panel.Controls.Add(maskedTextBoxNumber); + panel.Controls.Add(ButtonRefreshCollection); + panel.Controls.Add(buttonRemoveBus); + panel.Location = new Point(721, 2); + panel.Name = "panel"; + panel.Size = new Size(162, 460); + panel.TabIndex = 5; + panel.Tag = ""; + // + // FormBusCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(884, 461); + Controls.Add(panel); + Controls.Add(pictureBoxCollection); + Name = "FormBusCollection"; + Text = "Набор автобусов"; + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + panel.ResumeLayout(false); + panel.PerformLayout(); + ResumeLayout(false); + } + + #endregion + + private Button buttonAddBus; + private Button buttonRemoveBus; + private Button button3; + private MaskedTextBox maskedTextBoxNumber; + private PictureBox pictureBoxCollection; + private Button ButtonRefreshCollection; + private Panel panel; + } +} \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs new file mode 100644 index 0000000..bc9b63c --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs @@ -0,0 +1,64 @@ +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; +using DoubleDeckerBus.DrawningObjects; +using DoubleDeckerBus.Generics; +using DoubleDeckerBus.MovementStrategy; + +namespace DoubleDeckerBus +{ + public partial class FormBusCollection : Form + { + private readonly BusesGenericCollection _buses; + public FormBusCollection() + { + InitializeComponent(); + _buses = new BusesGenericCollection + (pictureBoxCollection.Width, pictureBoxCollection.Height); + } + private void buttonAddBus_Click(object sender, EventArgs e) + { + DoubleDeckerBus form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_buses + form.SelectedBus != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _buses.ShowBuses(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + private void buttonRemoveBus_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_buses - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _buses.ShowBuses(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _buses.ShowBuses(); + } + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.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/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericCollection.cs new file mode 100644 index 0000000..6f021b3 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericCollection.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DoubleDeckerBus.DrawningObjects; +using DoubleDeckerBus.MovementStrategy; + +namespace DoubleDeckerBus.Generics +{ + internal class BusesGenericCollection + where T : DrawningBus + where U : IMoveableObject + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 210; + private readonly int _placeSizeHeight = 90; + private readonly SetGeneric _collection; + public BusesGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + public static int operator +(BusesGenericCollection collect, T? obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj) ?? -1; + } + public static bool operator -(BusesGenericCollection collect, int pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + collect._collection.Remove(pos); + return true; + } + return false; + } + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + public Bitmap ShowBuses() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + 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 / 2, j * _placeSizeHeight); + } + g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + private void DrawObjects(Graphics g) + { + int widthObjCount = _pictureWidth / _placeSizeWidth; + for (int i = 0; i < _collection.Count; i++) + { + T? type = _collection.Get(i); + if (type != null) + { + int row = i / widthObjCount; + int col = widthObjCount - 1 - (i % widthObjCount); + + type.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight); + type?.DrawTransport(g); + } + } + } + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/Generics/SetGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/Generics/SetGeneric.cs new file mode 100644 index 0000000..2a45004 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/Generics/SetGeneric.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus.Generics +{ + internal class SetGeneric + where T : class + { + private readonly T?[] _places; + public int Count => _places.Length; + public SetGeneric(int count) + { + _places = new T?[count]; + } + public int Insert(T bus) + { + return Insert(bus, 0); + } + public int Insert(T bus, int position) + { + int NoEmpty = 0, temp = 0; + for (int i = position; i < Count; i++) + { + if (_places[i] != null) NoEmpty++; + } + if (NoEmpty == Count - position - 1) return -1; + + if (position < Count && position >= 0) + { + for (int j = position; j < Count; j++) + { + if (_places[j] == null) + { + temp = j; + break; + } + } + + for (int i = temp; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = bus; + return position; + } + return -1; + } + public bool Remove(int position) + { + if (position < 0 || position >= Count) + { + return false; + } + + _places[position] = null; + return true; + } + public T? Get(int position) + { + if (position < 0 || position >= _places.Length) + { + return null; + } + return _places[position]; + } + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/Program.cs b/DoubleDeckerBus/DoubleDeckerBus/Program.cs index fa559b5..08776e0 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Program.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Program.cs @@ -1,17 +1,14 @@ +using System.Drawing; + namespace DoubleDeckerBus { internal static class Program { - /// - /// The main entry point for the application. - /// [STAThread] static void Main() { - // To customize application configuration such as set high DPI settings or default font, - // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new DoubleDeckerBus()); + Application.Run(new FormBusCollection()); } } } \ No newline at end of file