From b9edbcd1dd04c8a1562bcd38e30b498046ed23f4 Mon Sep 17 00:00:00 2001 From: MayDayR Date: Sat, 25 Nov 2023 13:13:49 +0300 Subject: [PATCH] PIbd22_Kamcharova_K.A._lab3 --- DoubleDeckerBus/AbstractStrategy.cs | 3 +- DoubleDeckerBus/BusGenericCollection.cs | 105 ++++++++ DoubleDeckerBus/DirectionType.cs | 2 +- DoubleDeckerBus/DoubleDeckerBus.sln | 25 -- DoubleDeckerBus/DrawingBus.cs | 16 +- DoubleDeckerBus/DrawingDoubleDeckerbus.cs | 12 +- DoubleDeckerBus/EntityDoubleDeckerBus.cs | 4 +- DoubleDeckerBus/FormBusCollection.Designer.cs | 128 ++++++++++ DoubleDeckerBus/FormBusCollection.cs | 74 ++++++ DoubleDeckerBus/FormBusCollection.resx | 60 +++++ .../FormDoubleDeckerBus.Designer.cs | 230 ++++++++++-------- DoubleDeckerBus/FormDoubleDeckerBus.cs | 45 +++- DoubleDeckerBus/MoveToBorder.cs | 1 + DoubleDeckerBus/MoveToCenter.cs | 1 + DoubleDeckerBus/Program.cs | 6 +- DoubleDeckerBus/SetGeneric.cs | 85 +++++++ 16 files changed, 630 insertions(+), 167 deletions(-) create mode 100644 DoubleDeckerBus/BusGenericCollection.cs delete mode 100644 DoubleDeckerBus/DoubleDeckerBus.sln create mode 100644 DoubleDeckerBus/FormBusCollection.Designer.cs create mode 100644 DoubleDeckerBus/FormBusCollection.cs create mode 100644 DoubleDeckerBus/FormBusCollection.resx create mode 100644 DoubleDeckerBus/SetGeneric.cs diff --git a/DoubleDeckerBus/AbstractStrategy.cs b/DoubleDeckerBus/AbstractStrategy.cs index 817dbde..e3d4f72 100644 --- a/DoubleDeckerBus/AbstractStrategy.cs +++ b/DoubleDeckerBus/AbstractStrategy.cs @@ -71,5 +71,4 @@ namespace DoubleDeckerbus.MovementStrategy return false; } } -} - +} \ No newline at end of file diff --git a/DoubleDeckerBus/BusGenericCollection.cs b/DoubleDeckerBus/BusGenericCollection.cs new file mode 100644 index 0000000..397a7e1 --- /dev/null +++ b/DoubleDeckerBus/BusGenericCollection.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DoubleDeckerbus.DrawingObjects; +using DoubleDeckerbus.MovementStrategy; + +namespace DoubleDeckerbus +{ + internal class BusGenericCollection + where T : DrawingBus + where U : IMoveableObject + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 200; + private readonly int _placeSizeHeight = 120; + private readonly SetGeneric _collection; + + public BusGenericCollection(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 +(BusGenericCollection collect, T? obj) + { + if (obj != null) + { + return collect._collection.Insert(obj); + } + return -1; + } + public static bool operator -(BusGenericCollection collect, int pos) + { + if (collect._collection.Get(pos) == null) + { + return false; + } + return collect?._collection.Remove(pos) ?? false; + } + + public int ReturnLength() + { + return _collection.Count; + } + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + public Bitmap ShowBus() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + private void DrawBackground(Graphics gr) + { + Pen pen = new(Color.Black, 3); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; ++i) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + gr.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight); + gr.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight); + } + } + } + private void DrawObjects(Graphics g) + { + int x = 0; + int y = 0; + int index = -1; + for (int i = 0; i <= _collection.Count; ++i) + { + DrawingBus _bus = _collection.Get(i); + x = 0; + y = 0; + if (_bus != null) + { + index = _collection.Count - i; + while ((index - _pictureWidth / _placeSizeWidth) >= 0) + { + y++; + index -= _pictureWidth / _placeSizeWidth; + } + if (index > 0) + { + x += index; + } + x = _pictureWidth / _placeSizeWidth - 1 - x; + _bus.SetPosition(_placeSizeWidth * x, _placeSizeHeight * y); + + _bus.DrawTransport(g); + } + } + } + } +} \ No newline at end of file diff --git a/DoubleDeckerBus/DirectionType.cs b/DoubleDeckerBus/DirectionType.cs index 52f8626..e13f540 100644 --- a/DoubleDeckerBus/DirectionType.cs +++ b/DoubleDeckerBus/DirectionType.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace DoubleDeckerbus { public enum DirectionType - { + { Up = 1, Down = 2, Left = 3, diff --git a/DoubleDeckerBus/DoubleDeckerBus.sln b/DoubleDeckerBus/DoubleDeckerBus.sln deleted file mode 100644 index c8fac7f..0000000 --- a/DoubleDeckerBus/DoubleDeckerBus.sln +++ /dev/null @@ -1,25 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.7.34024.191 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DoubleDeckerBus", "DoubleDeckerBus.csproj", "{2AA7FB83-8FC3-451D-B277-158CCA4DAE44}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {2AA7FB83-8FC3-451D-B277-158CCA4DAE44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {2AA7FB83-8FC3-451D-B277-158CCA4DAE44}.Debug|Any CPU.Build.0 = Debug|Any CPU - {2AA7FB83-8FC3-451D-B277-158CCA4DAE44}.Release|Any CPU.ActiveCfg = Release|Any CPU - {2AA7FB83-8FC3-451D-B277-158CCA4DAE44}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {7F269221-EAB0-4961-A192-4F793DC88242} - EndGlobalSection -EndGlobal diff --git a/DoubleDeckerBus/DrawingBus.cs b/DoubleDeckerBus/DrawingBus.cs index cfbbe7a..03af2a5 100644 --- a/DoubleDeckerBus/DrawingBus.cs +++ b/DoubleDeckerBus/DrawingBus.cs @@ -5,18 +5,20 @@ using System.Text; using System.Threading.Tasks; using DoubleDeckerbus.Entities; +using DoubleDeckerbus.MovementStrategy; namespace DoubleDeckerbus.DrawingObjects { public class DrawingBus { + public IMoveableObject GetMoveableObject => new DrawingObjectBus(this); public EntityBus? EntityBus { get; protected set; } public int _pictureWidth; public int _pictureHeight; protected int _startPosX; protected int _startPosY; - protected readonly int _busWidth = 175; - protected readonly int _busHeight = 115; + protected readonly int _busWidth = 175; + protected readonly int _busHeight = 115; public int GetPosX => _startPosX; public int GetPosY => _startPosY; public int GetWidth => _busWidth; @@ -53,7 +55,7 @@ namespace DoubleDeckerbus.DrawingObjects _startPosY = y; } public void MoveTransport(DirectionType direction) - { + { if (!CanMove(direction) || EntityBus == null) { return; @@ -89,19 +91,19 @@ namespace DoubleDeckerbus.DrawingObjects public virtual void DrawTransport(Graphics g) { Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(EntityBus.BodyColor); - g.FillRectangle(additionalBrush, _startPosX + 147, _startPosY + 52, 21, 20); //маленький - g.FillRectangle(additionalBrush, _startPosX + 147, _startPosY + 71, 30, 27); //2 - g.FillRectangle(additionalBrush, _startPosX + 10, _startPosY + 52, 137, 46); //большой прямоугольник 2 + g.FillRectangle(additionalBrush, _startPosX + 147, _startPosY + 52, 21, 20); + g.FillRectangle(additionalBrush, _startPosX + 147, _startPosY + 71, 30, 27); + g.FillRectangle(additionalBrush, _startPosX + 10, _startPosY + 52, 137, 46); Brush brBlue = new SolidBrush(Color.LightBlue); g.FillRectangle(brBlue, _startPosX + 150, _startPosY + 55, 15, 15); g.FillRectangle(brBlue, _startPosX + 42, _startPosY + 55, 15, 15); g.FillRectangle(brBlue, _startPosX + 69, _startPosY + 55, 15, 15); g.FillRectangle(brBlue, _startPosX + 96, _startPosY + 55, 15, 15); g.FillRectangle(brBlue, _startPosX + 123, _startPosY + 55, 15, 15); - g.DrawLine(pen, _startPosX + 30, _startPosY + 52, _startPosX + 30, _startPosY + 98); g.DrawLine(pen, _startPosX + 35, _startPosY + 52, _startPosX + 35, _startPosY + 98); diff --git a/DoubleDeckerBus/DrawingDoubleDeckerbus.cs b/DoubleDeckerBus/DrawingDoubleDeckerbus.cs index 3429e68..01e7374 100644 --- a/DoubleDeckerBus/DrawingDoubleDeckerbus.cs +++ b/DoubleDeckerBus/DrawingDoubleDeckerbus.cs @@ -17,6 +17,7 @@ namespace DoubleDeckerbus { EntityBus = new EntityDoubleDeckerBus(speed, weight, bodyColor, additionalColor, secondfloor, stairs); } + } public override void DrawTransport(Graphics g) { @@ -25,19 +26,15 @@ namespace DoubleDeckerbus return; } Pen pen = new(Color.Black); - Pen additionalPen = new(doubleDeckerBus.AdditionalColor); - Brush additionalBrush = new SolidBrush(doubleDeckerBus.AdditionalColor); + Pen additionalPen = new(doubleDeckerBus.Body); + Brush additionalBrush = new SolidBrush(doubleDeckerBus.Body); base.DrawTransport(g); - if (doubleDeckerBus.Secondfloor) { - Brush additionalBrush2 = new SolidBrush(doubleDeckerBus.AdditionalColor); + Brush additionalBrush2 = new SolidBrush(doubleDeckerBus.Body); Brush brBlue = new SolidBrush(Color.LightBlue); - g.FillRectangle(additionalBrush2, _startPosX + 7, _startPosY + 12, 172, 40); - g.DrawLine(pen, _startPosX + 7, _startPosY + 36, _startPosX + 178, _startPosY + 36); - g.FillRectangle(brBlue, _startPosX + 15, _startPosY + 15, 15, 15); g.FillRectangle(brBlue, _startPosX + 42, _startPosY + 15, 15, 15); g.FillRectangle(brBlue, _startPosX + 69, _startPosY + 15, 15, 15); @@ -45,7 +42,6 @@ namespace DoubleDeckerbus g.FillRectangle(brBlue, _startPosX + 123, _startPosY + 15, 15, 15); g.FillRectangle(brBlue, _startPosX + 150, _startPosY + 15, 15, 15); } - if (doubleDeckerBus.Stairs) { g.DrawLine(pen, _startPosX + 10, _startPosY + 55, _startPosX + 34, _startPosY + 55); diff --git a/DoubleDeckerBus/EntityDoubleDeckerBus.cs b/DoubleDeckerBus/EntityDoubleDeckerBus.cs index fca0681..11cd618 100644 --- a/DoubleDeckerBus/EntityDoubleDeckerBus.cs +++ b/DoubleDeckerBus/EntityDoubleDeckerBus.cs @@ -9,12 +9,12 @@ namespace DoubleDeckerbus.Entities { public class EntityDoubleDeckerBus : EntityBus { - public Color AdditionalColor { get; private set; } + public Color Body { get; private set; } public bool Secondfloor { get; private set; } public bool Stairs { get; private set; } public EntityDoubleDeckerBus(int speed, double weight, Color bodyColor, Color additionalColor, bool secondfloor, bool stairs) : base(speed, weight, bodyColor) { - AdditionalColor = additionalColor; + Body = additionalColor; Secondfloor = secondfloor; Stairs = stairs; } diff --git a/DoubleDeckerBus/FormBusCollection.Designer.cs b/DoubleDeckerBus/FormBusCollection.Designer.cs new file mode 100644 index 0000000..08ff655 --- /dev/null +++ b/DoubleDeckerBus/FormBusCollection.Designer.cs @@ -0,0 +1,128 @@ +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() + { + this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); + this.labelInstruments = new System.Windows.Forms.Label(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.maskedTextBoxNumber = new System.Windows.Forms.TextBox(); + this.buttonDelete = new System.Windows.Forms.Button(); + this.buttonUpdate = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); + this.SuspendLayout(); + // + // pictureBoxCollection + // + this.pictureBoxCollection.Location = new System.Drawing.Point(1, -2); + this.pictureBoxCollection.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.pictureBoxCollection.Name = "pictureBoxCollection"; + this.pictureBoxCollection.Size = new System.Drawing.Size(803, 924); + this.pictureBoxCollection.TabIndex = 0; + this.pictureBoxCollection.TabStop = false; + // + // labelInstruments + // + this.labelInstruments.AutoSize = true; + this.labelInstruments.Location = new System.Drawing.Point(852, 22); + this.labelInstruments.Name = "labelInstruments"; + this.labelInstruments.Size = new System.Drawing.Size(83, 15); + this.labelInstruments.TabIndex = 1; + this.labelInstruments.Text = "Инструменты"; + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(823, 63); + this.buttonAdd.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(161, 44); + this.buttonAdd.TabIndex = 2; + this.buttonAdd.Text = "Добавить автобус"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.ButtonAddBus_Click); + // + // maskedTextBoxNumber + // + this.maskedTextBoxNumber.Location = new System.Drawing.Point(874, 140); + this.maskedTextBoxNumber.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + this.maskedTextBoxNumber.Size = new System.Drawing.Size(110, 23); + this.maskedTextBoxNumber.TabIndex = 3; + // + // buttonDelete + // + this.buttonDelete.Location = new System.Drawing.Point(840, 167); + this.buttonDelete.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonDelete.Name = "buttonDelete"; + this.buttonDelete.Size = new System.Drawing.Size(165, 45); + this.buttonDelete.TabIndex = 4; + this.buttonDelete.Text = "Удалить автобус"; + this.buttonDelete.UseVisualStyleBackColor = true; + this.buttonDelete.Click += new System.EventHandler(this.ButtonRemoveBus_Click); + // + // buttonUpdate + // + this.buttonUpdate.Location = new System.Drawing.Point(840, 260); + this.buttonUpdate.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonUpdate.Name = "buttonUpdate"; + this.buttonUpdate.Size = new System.Drawing.Size(165, 45); + this.buttonUpdate.TabIndex = 5; + this.buttonUpdate.Text = "Обновить"; + this.buttonUpdate.UseVisualStyleBackColor = true; + this.buttonUpdate.Click += new System.EventHandler(this.ButtonRefreshCollection_Click); + // + // FormBusCollection + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(1152, 975); + this.Controls.Add(this.buttonUpdate); + this.Controls.Add(this.buttonDelete); + this.Controls.Add(this.maskedTextBoxNumber); + this.Controls.Add(this.buttonAdd); + this.Controls.Add(this.labelInstruments); + this.Controls.Add(this.pictureBoxCollection); + this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.Name = "FormBusCollection"; + this.Text = "Автобус"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private PictureBox pictureBoxCollection; + private Label labelInstruments; + private Button buttonAdd; + private TextBox maskedTextBoxNumber; + private Button buttonDelete; + private Button buttonUpdate; + } +} \ No newline at end of file diff --git a/DoubleDeckerBus/FormBusCollection.cs b/DoubleDeckerBus/FormBusCollection.cs new file mode 100644 index 0000000..c78c379 --- /dev/null +++ b/DoubleDeckerBus/FormBusCollection.cs @@ -0,0 +1,74 @@ +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.DrawingObjects; +using DoubleDeckerbus; +using DoubleDeckerbus.MovementStrategy; + +namespace DoubleDeckerbus +{ + public partial class FormBusCollection : Form + { + private readonly BusGenericCollection _bus; + public FormBusCollection() + { + InitializeComponent(); + _bus = new BusGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + + } + private void ButtonAddBus_Click(object sender, EventArgs e) + { + FormDoubleDeckerbus form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_bus + form.SelectedBus != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _bus.ShowBus(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + private void ButtonRemoveBus_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = 0; + try + { + pos = Convert.ToInt32(maskedTextBoxNumber.Text) ; + + } + catch + { + MessageBox.Show("Ошибка ввода данных"); + return; + } + if (_bus - (_bus.ReturnLength() - pos)) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _bus.ShowBus(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _bus.ShowBus(); + } + } +} diff --git a/DoubleDeckerBus/FormBusCollection.resx b/DoubleDeckerBus/FormBusCollection.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/DoubleDeckerBus/FormBusCollection.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/FormDoubleDeckerBus.Designer.cs b/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs index a8d0b5e..42d8263 100644 --- a/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs +++ b/DoubleDeckerBus/FormDoubleDeckerBus.Designer.cs @@ -1,6 +1,6 @@ namespace DoubleDeckerbus { - partial class FormDoubleDeckerBus + partial class FormDoubleDeckerbus { /// /// Required designer variable. @@ -28,141 +28,158 @@ /// private void InitializeComponent() { - this.pictureBoxDoubleDeckerbus = new System.Windows.Forms.PictureBox(); - this.DoubleDeckerbus = new System.Windows.Forms.Button(); - this.buttonUp = new System.Windows.Forms.Button(); - this.buttonLeft = new System.Windows.Forms.Button(); - this.buttonDown = new System.Windows.Forms.Button(); - this.buttonRight = new System.Windows.Forms.Button(); - this.comboBoxStrategy = new System.Windows.Forms.ComboBox(); - this.buttonStep = new System.Windows.Forms.Button(); - this.buttonCreateBus = new System.Windows.Forms.Button(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDoubleDeckerbus)).BeginInit(); - this.SuspendLayout(); + pictureBoxDoubleDeckerbus = new PictureBox(); + DoubleDeckerbus = new Button(); + buttonUp = new Button(); + buttonLeft = new Button(); + buttonDown = new Button(); + buttonRight = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStep = new Button(); + buttonCreateBus = new Button(); + buttonSelect = new Button(); + ((System.ComponentModel.ISupportInitialize)pictureBoxDoubleDeckerbus).BeginInit(); + SuspendLayout(); // // pictureBoxDoubleDeckerbus // - this.pictureBoxDoubleDeckerbus.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBoxDoubleDeckerbus.Location = new System.Drawing.Point(0, 0); - this.pictureBoxDoubleDeckerbus.Name = "pictureBoxDoubleDeckerbus"; - this.pictureBoxDoubleDeckerbus.Size = new System.Drawing.Size(882, 453); - this.pictureBoxDoubleDeckerbus.TabIndex = 5; - this.pictureBoxDoubleDeckerbus.TabStop = false; + pictureBoxDoubleDeckerbus.Dock = DockStyle.Fill; + pictureBoxDoubleDeckerbus.Location = new Point(0, 0); + pictureBoxDoubleDeckerbus.Margin = new Padding(3, 2, 3, 2); + pictureBoxDoubleDeckerbus.Name = "pictureBoxDoubleDeckerbus"; + pictureBoxDoubleDeckerbus.Size = new Size(772, 340); + pictureBoxDoubleDeckerbus.TabIndex = 5; + pictureBoxDoubleDeckerbus.TabStop = false; // // DoubleDeckerbus // - this.DoubleDeckerbus.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.DoubleDeckerbus.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.DoubleDeckerbus.Location = new System.Drawing.Point(11, 365); - this.DoubleDeckerbus.Name = "DoubleDeckerbus"; - this.DoubleDeckerbus.Size = new System.Drawing.Size(120, 73); - this.DoubleDeckerbus.TabIndex = 6; - this.DoubleDeckerbus.Text = "Создать Двухэтажный автобус"; - this.DoubleDeckerbus.UseVisualStyleBackColor = true; - this.DoubleDeckerbus.Click += new System.EventHandler(this.buttonCreateDoubleDeckerbus_Click); + DoubleDeckerbus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + DoubleDeckerbus.Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point); + DoubleDeckerbus.Location = new Point(10, 274); + DoubleDeckerbus.Margin = new Padding(3, 2, 3, 2); + DoubleDeckerbus.Name = "DoubleDeckerbus"; + DoubleDeckerbus.Size = new Size(105, 55); + DoubleDeckerbus.TabIndex = 6; + DoubleDeckerbus.Text = "Создать Двухэтажный автобус"; + DoubleDeckerbus.UseVisualStyleBackColor = true; + DoubleDeckerbus.Click += buttonCreateDoubleDeckerbus_Click; // // buttonUp // - this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonUp.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.ArrowUp; - this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonUp.Location = new System.Drawing.Point(811, 377); - this.buttonUp.Name = "buttonUp"; - this.buttonUp.Size = new System.Drawing.Size(30, 29); - this.buttonUp.TabIndex = 7; - this.buttonUp.UseVisualStyleBackColor = true; - this.buttonUp.Click += new System.EventHandler(this.buttonMove_Click); + buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonUp.BackgroundImageLayout = ImageLayout.Zoom; + buttonUp.Location = new Point(710, 283); + buttonUp.Margin = new Padding(3, 2, 3, 2); + buttonUp.Name = "buttonUp"; + buttonUp.Size = new Size(26, 22); + buttonUp.TabIndex = 7; + buttonUp.UseVisualStyleBackColor = true; + buttonUp.Click += buttonMove_Click; // // buttonLeft // - this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonLeft.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.ArrowLeft; - this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonLeft.Location = new System.Drawing.Point(776, 413); - this.buttonLeft.Name = "buttonLeft"; - this.buttonLeft.Size = new System.Drawing.Size(30, 29); - this.buttonLeft.TabIndex = 8; - this.buttonLeft.UseVisualStyleBackColor = true; - this.buttonLeft.Click += new System.EventHandler(this.buttonMove_Click); + buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; + buttonLeft.Location = new Point(679, 310); + buttonLeft.Margin = new Padding(3, 2, 3, 2); + buttonLeft.Name = "buttonLeft"; + buttonLeft.Size = new Size(26, 22); + buttonLeft.TabIndex = 8; + buttonLeft.UseVisualStyleBackColor = true; + buttonLeft.Click += buttonMove_Click; // // buttonDown // - this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonDown.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.ArrowDown; - this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonDown.Location = new System.Drawing.Point(811, 413); - this.buttonDown.Name = "buttonDown"; - this.buttonDown.Size = new System.Drawing.Size(30, 29); - this.buttonDown.TabIndex = 9; - this.buttonDown.UseVisualStyleBackColor = true; - this.buttonDown.Click += new System.EventHandler(this.buttonMove_Click); + buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonDown.BackgroundImageLayout = ImageLayout.Zoom; + buttonDown.Location = new Point(710, 310); + buttonDown.Margin = new Padding(3, 2, 3, 2); + buttonDown.Name = "buttonDown"; + buttonDown.Size = new Size(26, 22); + buttonDown.TabIndex = 9; + buttonDown.UseVisualStyleBackColor = true; + buttonDown.Click += buttonMove_Click; // // buttonRight // - this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonRight.BackgroundImage = global::DoubleDeckerBus.Properties.Resources.ArrowRight; - this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; - this.buttonRight.Location = new System.Drawing.Point(848, 413); - this.buttonRight.Name = "buttonRight"; - this.buttonRight.Size = new System.Drawing.Size(30, 29); - this.buttonRight.TabIndex = 10; - this.buttonRight.UseVisualStyleBackColor = true; - this.buttonRight.Click += new System.EventHandler(this.buttonMove_Click); + buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonRight.BackgroundImageLayout = ImageLayout.Zoom; + buttonRight.Location = new Point(742, 310); + buttonRight.Margin = new Padding(3, 2, 3, 2); + buttonRight.Name = "buttonRight"; + buttonRight.Size = new Size(26, 22); + buttonRight.TabIndex = 10; + buttonRight.UseVisualStyleBackColor = true; + buttonRight.Click += buttonMove_Click; // // comboBoxStrategy // - this.comboBoxStrategy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.comboBoxStrategy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.comboBoxStrategy.FormattingEnabled = true; - this.comboBoxStrategy.Items.AddRange(new object[] { - "В центр", - "В правый нижний угол"}); - this.comboBoxStrategy.Location = new System.Drawing.Point(719, 12); - this.comboBoxStrategy.Name = "comboBoxStrategy"; - this.comboBoxStrategy.Size = new System.Drawing.Size(151, 28); - this.comboBoxStrategy.TabIndex = 11; + comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "В центр", "В правый нижний угол" }); + comboBoxStrategy.Location = new Point(629, 9); + comboBoxStrategy.Margin = new Padding(3, 2, 3, 2); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(133, 23); + comboBoxStrategy.TabIndex = 11; // // buttonStep // - this.buttonStep.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.buttonStep.Location = new System.Drawing.Point(747, 45); - this.buttonStep.Name = "buttonStep"; - this.buttonStep.Size = new System.Drawing.Size(94, 29); - this.buttonStep.TabIndex = 12; - this.buttonStep.Text = "Шаг"; - this.buttonStep.UseVisualStyleBackColor = true; - this.buttonStep.Click += new System.EventHandler(this.buttonStep_Click); + buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonStep.Location = new Point(654, 34); + buttonStep.Margin = new Padding(3, 2, 3, 2); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(82, 22); + buttonStep.TabIndex = 12; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += buttonStep_Click; // // buttonCreateBus // - this.buttonCreateBus.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonCreateBus.Location = new System.Drawing.Point(138, 388); - this.buttonCreateBus.Name = "buttonCreateBus"; - this.buttonCreateBus.Size = new System.Drawing.Size(120, 51); - this.buttonCreateBus.TabIndex = 13; - this.buttonCreateBus.Text = "Создать автобус"; - this.buttonCreateBus.UseVisualStyleBackColor = true; - this.buttonCreateBus.Click += new System.EventHandler(this.buttonCreateBus_Click); + buttonCreateBus.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateBus.Location = new Point(121, 291); + buttonCreateBus.Margin = new Padding(3, 2, 3, 2); + buttonCreateBus.Name = "buttonCreateBus"; + buttonCreateBus.Size = new Size(105, 38); + buttonCreateBus.TabIndex = 13; + buttonCreateBus.Text = "Создать автобус"; + buttonCreateBus.UseVisualStyleBackColor = true; + buttonCreateBus.Click += buttonCreateBus_Click; + // + // buttonSelect + // + buttonSelect.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonSelect.Location = new Point(239, 274); + buttonSelect.Margin = new Padding(3, 2, 3, 2); + buttonSelect.Name = "buttonSelect"; + buttonSelect.Size = new Size(105, 55); + buttonSelect.TabIndex = 14; + buttonSelect.Text = "Выбрать автобус"; + buttonSelect.UseVisualStyleBackColor = true; + buttonSelect.Click += ButtonSelectBus_Click; // // FormDoubleDeckerbus // - this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(882, 453); - this.Controls.Add(this.buttonCreateBus); - this.Controls.Add(this.buttonStep); - this.Controls.Add(this.comboBoxStrategy); - this.Controls.Add(this.buttonRight); - this.Controls.Add(this.buttonDown); - this.Controls.Add(this.buttonLeft); - this.Controls.Add(this.buttonUp); - this.Controls.Add(this.DoubleDeckerbus); - this.Controls.Add(this.pictureBoxDoubleDeckerbus); - this.Name = "FormDoubleDeckerbus"; - this.Text = "DoubleDeckerbus"; - ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDoubleDeckerbus)).EndInit(); - this.ResumeLayout(false); - + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(772, 340); + Controls.Add(buttonSelect); + Controls.Add(buttonCreateBus); + Controls.Add(buttonStep); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonRight); + Controls.Add(buttonDown); + Controls.Add(buttonLeft); + Controls.Add(buttonUp); + Controls.Add(DoubleDeckerbus); + Controls.Add(pictureBoxDoubleDeckerbus); + Margin = new Padding(3, 2, 3, 2); + Name = "FormDoubleDeckerbus"; + Text = "Автобус"; + ((System.ComponentModel.ISupportInitialize)pictureBoxDoubleDeckerbus).EndInit(); + ResumeLayout(false); } #endregion @@ -176,5 +193,6 @@ private ComboBox comboBoxStrategy; private Button buttonStep; private Button buttonCreateBus; + private Button buttonSelect; } } \ No newline at end of file diff --git a/DoubleDeckerBus/FormDoubleDeckerBus.cs b/DoubleDeckerBus/FormDoubleDeckerBus.cs index 2589a18..65e991a 100644 --- a/DoubleDeckerBus/FormDoubleDeckerBus.cs +++ b/DoubleDeckerBus/FormDoubleDeckerBus.cs @@ -3,13 +3,16 @@ using DoubleDeckerbus.MovementStrategy; namespace DoubleDeckerbus { - public partial class FormDoubleDeckerBus : Form + public partial class FormDoubleDeckerbus : Form { private DrawingBus? _drawingBus; private AbstractStrategy? _abstractStrategy; - public FormDoubleDeckerBus() + public DrawingBus? SelectedBus { get; private set; } + public FormDoubleDeckerbus() { InitializeComponent(); + _abstractStrategy = null; + SelectedBus = null; } private void Draw() { @@ -25,25 +28,36 @@ namespace DoubleDeckerbus private void buttonCreateDoubleDeckerbus_Click(object sender, EventArgs e) { Random random = new(); + Color color = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + Color color1 = Color.FromArgb(random.Next(0, 256), + random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + ColorDialog dialog1 = new(); + if (dialog.ShowDialog() == DialogResult.OK && dialog1.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + color1 = dialog1.Color; + } _drawingBus = new DrawingDoubleDeckerbus(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)), - true, - Convert.ToBoolean(random.Next(0, 2)), + random.Next(1000, 3000), color, color1, true, true, pictureBoxDoubleDeckerbus.Width, pictureBoxDoubleDeckerbus.Height); - _drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _drawingBus.SetPosition(random.Next(10, 100), random.Next(10, + 100)); Draw(); } 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; + } _drawingBus = new DrawingBus(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, pictureBoxDoubleDeckerbus.Width, pictureBoxDoubleDeckerbus.Height); _drawingBus.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -106,5 +120,10 @@ namespace DoubleDeckerbus _abstractStrategy = null; } } + private void ButtonSelectBus_Click(object sender, EventArgs e) + { + SelectedBus = _drawingBus; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/DoubleDeckerBus/MoveToBorder.cs b/DoubleDeckerBus/MoveToBorder.cs index 2134eed..e7fa9b3 100644 --- a/DoubleDeckerBus/MoveToBorder.cs +++ b/DoubleDeckerBus/MoveToBorder.cs @@ -38,6 +38,7 @@ namespace DoubleDeckerbus.MovementStrategy { MoveRight(); } + } var diffY = objParams.ObjectMiddleVertical - FieldHeight; if (Math.Abs(diffY) > GetStep()) diff --git a/DoubleDeckerBus/MoveToCenter.cs b/DoubleDeckerBus/MoveToCenter.cs index 5846414..beebbe9 100644 --- a/DoubleDeckerBus/MoveToCenter.cs +++ b/DoubleDeckerBus/MoveToCenter.cs @@ -38,6 +38,7 @@ namespace DoubleDeckerbus.MovementStrategy { MoveRight(); } + } var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; if (Math.Abs(diffY) > GetStep()) diff --git a/DoubleDeckerBus/Program.cs b/DoubleDeckerBus/Program.cs index 7ac98e0..d4e484f 100644 --- a/DoubleDeckerBus/Program.cs +++ b/DoubleDeckerBus/Program.cs @@ -1,5 +1,3 @@ -using System.Drawing; - namespace DoubleDeckerbus { internal static class Program @@ -10,8 +8,10 @@ namespace DoubleDeckerbus [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 FormDoubleDeckerBus()); + Application.Run(new FormBusCollection()); } } } \ No newline at end of file diff --git a/DoubleDeckerBus/SetGeneric.cs b/DoubleDeckerBus/SetGeneric.cs new file mode 100644 index 0000000..6a0415f --- /dev/null +++ b/DoubleDeckerBus/SetGeneric.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerbus +{ + internal class SetGeneric + where T : class + + { + private readonly T?[] _places; + public int Count => _places.Length - 1; + public SetGeneric(int count) + { + _places = new T?[count]; + } + public int Insert(T bus) + { + int pos = Count - 1; + if (_places[Count] != null) + { + for (int i = pos; i > 0; --i) + { + if (_places[i] == null) + { + pos = i; + break; + } + } + for (int i = pos + 1; i <= Count; ++i) + { + _places[i - 1] = _places[i]; + } + } + _places[Count] = bus; + return pos; + } + public bool Insert(T bus, int position) + { + if (position < 0 || position > Count) + { + return false; + } + if (_places[Count] != null) + { + int pos = Count; + for (int i = Count; i > 0; --i) + { + + if (_places[i] == null) + { + pos = i; + break; + } + } + for (int i = Count; i >= pos; --i) + { + _places[i - 1] = _places[i]; + } + } + _places[Count] = bus; + return true; + } + 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 > Count) + { + return null; + } + return _places[position]; + } + } +}