From 881edef5a4ac5412cad3b6b39fa5576edd0c8e78 Mon Sep 17 00:00:00 2001 From: gg12 darfren Date: Tue, 3 Oct 2023 21:07:34 +0400 Subject: [PATCH] Done --- Monorail/Monorail/DrawningMonorail.cs | 3 +- .../FormMonorailCollection.Designer.cs | 123 ++++++++++++++++++ Monorail/Monorail/FormMonorailCollection.cs | 70 ++++++++++ Monorail/Monorail/FormMonorailCollection.resx | 120 +++++++++++++++++ Monorail/Monorail/MonorailForm.Designer.cs | 13 ++ Monorail/Monorail/MonorailForm.cs | 43 ++++-- .../Monorail/MonorailGenericCollection.cs | 98 ++++++++++++++ Monorail/Monorail/Program.cs | 2 +- Monorail/Monorail/SetGeneric.cs | 60 +++++++++ 9 files changed, 520 insertions(+), 12 deletions(-) create mode 100644 Monorail/Monorail/FormMonorailCollection.Designer.cs create mode 100644 Monorail/Monorail/FormMonorailCollection.cs create mode 100644 Monorail/Monorail/FormMonorailCollection.resx create mode 100644 Monorail/Monorail/MonorailGenericCollection.cs create mode 100644 Monorail/Monorail/SetGeneric.cs diff --git a/Monorail/Monorail/DrawningMonorail.cs b/Monorail/Monorail/DrawningMonorail.cs index 0c4972f..0076f45 100644 --- a/Monorail/Monorail/DrawningMonorail.cs +++ b/Monorail/Monorail/DrawningMonorail.cs @@ -67,7 +67,8 @@ namespace Monorail.DrawningObjects _startPosY = 1; } } - + public IMoveableObject GetMoveableObject => new +DrawningObjectMonorail(this); public bool CanMove(DirectionType direction) { if (EntityMonorail == null) diff --git a/Monorail/Monorail/FormMonorailCollection.Designer.cs b/Monorail/Monorail/FormMonorailCollection.Designer.cs new file mode 100644 index 0000000..e3013e0 --- /dev/null +++ b/Monorail/Monorail/FormMonorailCollection.Designer.cs @@ -0,0 +1,123 @@ +namespace Monorail +{ + partial class FormMonorailCollection + { + /// + /// 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() + { + toolGroupBox = new System.Windows.Forms.GroupBox(); + maskedTextBox = new System.Windows.Forms.MaskedTextBox(); + updateCollectionButton = new System.Windows.Forms.Button(); + deleteMonorailButton = new System.Windows.Forms.Button(); + addMonorailButton = new System.Windows.Forms.Button(); + pictureBoxCollection = new System.Windows.Forms.PictureBox(); + toolGroupBox.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + SuspendLayout(); + // + // toolGroupBox + // + toolGroupBox.Controls.Add(maskedTextBox); + toolGroupBox.Controls.Add(updateCollectionButton); + toolGroupBox.Controls.Add(deleteMonorailButton); + toolGroupBox.Controls.Add(addMonorailButton); + toolGroupBox.Location = new System.Drawing.Point(623, 12); + toolGroupBox.Name = "toolGroupBox"; + toolGroupBox.Size = new System.Drawing.Size(227, 426); + toolGroupBox.TabIndex = 0; + toolGroupBox.TabStop = false; + toolGroupBox.Text = "Инструменты"; + // + // maskedTextBox + // + maskedTextBox.Location = new System.Drawing.Point(6, 72); + maskedTextBox.Name = "maskedTextBox"; + maskedTextBox.Size = new System.Drawing.Size(125, 27); + maskedTextBox.TabIndex = 4; + // + // updateCollectionButton + // + updateCollectionButton.Location = new System.Drawing.Point(6, 151); + updateCollectionButton.Name = "updateCollectionButton"; + updateCollectionButton.Size = new System.Drawing.Size(215, 40); + updateCollectionButton.TabIndex = 3; + updateCollectionButton.Text = "Обновить коллекцию"; + updateCollectionButton.UseVisualStyleBackColor = true; + updateCollectionButton.Click += updateCollectionButton_Click; + // + // deleteMonorailButton + // + deleteMonorailButton.Location = new System.Drawing.Point(6, 105); + deleteMonorailButton.Name = "deleteMonorailButton"; + deleteMonorailButton.Size = new System.Drawing.Size(215, 40); + deleteMonorailButton.TabIndex = 2; + deleteMonorailButton.Text = "Удалить"; + deleteMonorailButton.UseVisualStyleBackColor = true; + deleteMonorailButton.Click += deleteMonorailButton_Click; + // + // addMonorailButton + // + addMonorailButton.Location = new System.Drawing.Point(6, 26); + addMonorailButton.Name = "addMonorailButton"; + addMonorailButton.Size = new System.Drawing.Size(215, 40); + addMonorailButton.TabIndex = 0; + addMonorailButton.Text = "Добавить"; + addMonorailButton.UseVisualStyleBackColor = true; + addMonorailButton.Click += addMonorailButton_Click; + // + // pictureBoxCollection + // + pictureBoxCollection.Location = new System.Drawing.Point(12, 12); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new System.Drawing.Size(605, 426); + pictureBoxCollection.TabIndex = 1; + pictureBoxCollection.TabStop = false; + // + // FormMonorailCollection + // + AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + ClientSize = new System.Drawing.Size(862, 450); + Controls.Add(pictureBoxCollection); + Controls.Add(toolGroupBox); + Name = "FormMonorailCollection"; + Text = "FormMonorailCollection"; + toolGroupBox.ResumeLayout(false); + toolGroupBox.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + ResumeLayout(false); + } + + #endregion + + private System.Windows.Forms.GroupBox toolGroupBox; + private System.Windows.Forms.Button updateCollectionButton; + private System.Windows.Forms.Button deleteMonorailButton; + private System.Windows.Forms.Button addMonorailButton; + private System.Windows.Forms.PictureBox pictureBoxCollection; + private System.Windows.Forms.MaskedTextBox maskedTextBox; + } +} \ No newline at end of file diff --git a/Monorail/Monorail/FormMonorailCollection.cs b/Monorail/Monorail/FormMonorailCollection.cs new file mode 100644 index 0000000..471a983 --- /dev/null +++ b/Monorail/Monorail/FormMonorailCollection.cs @@ -0,0 +1,70 @@ +using Monorail.DrawningObjects; +using Monorail.Generics; +using Monorail.MovementStrategy; +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 Monorail +{ + public partial class FormMonorailCollection : Form + { + private readonly MonorailGenericCollection _monorails; + + public FormMonorailCollection() + { + InitializeComponent(); + _monorails = new MonorailGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + private void addMonorailButton_Click(object sender, EventArgs e) + { + MonorailForm form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_monorails + form.SelectedMonorail) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _monorails.ShowMonorails(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + + } + } + + private void deleteMonorailButton_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", +MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBox.Text); + if (_monorails - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _monorails.ShowMonorails(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void updateCollectionButton_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _monorails.ShowMonorails(); + + } + } +} diff --git a/Monorail/Monorail/FormMonorailCollection.resx b/Monorail/Monorail/FormMonorailCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Monorail/Monorail/FormMonorailCollection.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/Monorail/Monorail/MonorailForm.Designer.cs b/Monorail/Monorail/MonorailForm.Designer.cs index 1cccf52..78aec86 100644 --- a/Monorail/Monorail/MonorailForm.Designer.cs +++ b/Monorail/Monorail/MonorailForm.Designer.cs @@ -39,6 +39,7 @@ namespace Monorail comboBoxStrategy = new System.Windows.Forms.ComboBox(); buttonStep = new System.Windows.Forms.Button(); buttonCreateAdvanced = new System.Windows.Forms.Button(); + buttonSelectMonorail = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxMonorail).BeginInit(); SuspendLayout(); // @@ -141,11 +142,22 @@ namespace Monorail buttonCreateAdvanced.UseVisualStyleBackColor = true; buttonCreateAdvanced.Click += buttonCreateAdvanced_Click; // + // buttonSelectMonorail + // + buttonSelectMonorail.Location = new System.Drawing.Point(384, 401); + buttonSelectMonorail.Name = "buttonSelectMonorail"; + buttonSelectMonorail.Size = new System.Drawing.Size(180, 40); + buttonSelectMonorail.TabIndex = 9; + buttonSelectMonorail.Text = "Выбрать"; + buttonSelectMonorail.UseVisualStyleBackColor = true; + buttonSelectMonorail.Click += buttonSelectMonorail_Click; + // // MonorailForm // AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; ClientSize = new System.Drawing.Size(882, 453); + Controls.Add(buttonSelectMonorail); Controls.Add(buttonCreateAdvanced); Controls.Add(buttonStep); Controls.Add(comboBoxStrategy); @@ -173,6 +185,7 @@ namespace Monorail public System.Windows.Forms.ComboBox comboBoxStrategy; private System.Windows.Forms.Button buttonStep; private System.Windows.Forms.Button buttonCreateAdvanced; + private System.Windows.Forms.Button buttonSelectMonorail; } } diff --git a/Monorail/Monorail/MonorailForm.cs b/Monorail/Monorail/MonorailForm.cs index 320945c..8b5ef2f 100644 --- a/Monorail/Monorail/MonorailForm.cs +++ b/Monorail/Monorail/MonorailForm.cs @@ -18,9 +18,13 @@ namespace Monorail private DrawningMonorail? _drawningMonorail; private AbstractStrategy? _abstractStrategy; + + public DrawningMonorail? SelectedMonorail { get; private set; } public MonorailForm() { InitializeComponent(); + _abstractStrategy = null; + SelectedMonorail = null; } private void Draw() @@ -37,10 +41,16 @@ namespace Monorail private void buttonCreate_Click(object sender, EventArgs e) { Random random = new(); + Color bodyColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + Color wheelColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + Color tireColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + bodyColor = dialog.Color; + } _drawningMonorail = new DrawningMonorail(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)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + bodyColor, wheelColor, tireColor, pictureBoxMonorail.Width, pictureBoxMonorail.Height); Draw(); } @@ -72,13 +82,20 @@ namespace Monorail private void buttonCreateAdvanced_Click(object sender, EventArgs e) { Random random = new(); + Color bodyColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + Color wheelColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + Color tireColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + Color additionalColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + bodyColor = dialog.Color; + if (dialog.ShowDialog() == DialogResult.OK) + additionalColor = dialog.Color; + int addWheelNumb = random.Next(3, 5); _drawningMonorail = new DrawningAdvancedMonorail(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)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - pictureBoxMonorail.Width, pictureBoxMonorail.Height, random.Next(3, 5), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), random.Next(0,2) > 0, - random.Next(0, 2) > 0); + bodyColor, wheelColor, tireColor, + pictureBoxMonorail.Width, pictureBoxMonorail.Height, addWheelNumb, + additionalColor, random.Next(0, 2) > 0, random.Next(0, 2) > 0); Draw(); } @@ -94,7 +111,7 @@ namespace Monorail 0 => new MoveToCenter(), 1 => new MoveToEdge(), _ => null, - } ; + }; if (_abstractStrategy == null) { return; @@ -116,5 +133,11 @@ namespace Monorail _abstractStrategy = null; } } + + private void buttonSelectMonorail_Click(object sender, EventArgs e) + { + SelectedMonorail = _drawningMonorail; + DialogResult = DialogResult.OK; + } } } diff --git a/Monorail/Monorail/MonorailGenericCollection.cs b/Monorail/Monorail/MonorailGenericCollection.cs new file mode 100644 index 0000000..476e361 --- /dev/null +++ b/Monorail/Monorail/MonorailGenericCollection.cs @@ -0,0 +1,98 @@ +using Monorail.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Monorail.DrawningObjects; +using System.Drawing; + +namespace Monorail.Generics +{ + internal class MonorailGenericCollection + where T :DrawningMonorail + where U: IMoveableObject + { + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + + private readonly int _placeSizeWidth = 133; + + private readonly int _placeSizeHeight = 50; + + private readonly SetGeneric _collection; + + public MonorailGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + + public static bool operator +(MonorailGenericCollection collect, T? obj) + { + if (obj == null) + return false; + return collect?._collection.Insert(obj) ?? false; + } + + public static T? operator -(MonorailGenericCollection collect, int +pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + collect._collection.Remove(pos); + return obj; + } + + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + + public Bitmap ShowMonorails() + { + 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) + { + for (int i = 0; i < _collection.Count; i++) + { + DrawningMonorail monorail = _collection.Get(i); + if (monorail != null) + { + int inRow = _pictureWidth / _placeSizeWidth; + monorail.SetPosition(i % inRow * _placeSizeWidth, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight ); + monorail.DrawTransport(g); + } + } + } + + + } +} diff --git a/Monorail/Monorail/Program.cs b/Monorail/Monorail/Program.cs index 524660f..2934ef4 100644 --- a/Monorail/Monorail/Program.cs +++ b/Monorail/Monorail/Program.cs @@ -17,7 +17,7 @@ namespace Monorail Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new MonorailForm()); + Application.Run(new FormMonorailCollection()); } } } diff --git a/Monorail/Monorail/SetGeneric.cs b/Monorail/Monorail/SetGeneric.cs new file mode 100644 index 0000000..a5cb775 --- /dev/null +++ b/Monorail/Monorail/SetGeneric.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail.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 bool Insert(T monorail) + { + if (_places[Count-1] != null) + return false; + return Insert(monorail, 0); + } + + public bool Insert(T monorail, int position) + { + if (!(position >= 0 && position < Count)) + return false; + if (_places[position] != null) + { + int ind = position; + while (ind < Count && _places[ind] != null) + ind++; + if (ind == Count) + return false; + for (int i = ind - 1; i >= position; i--) + _places[i + 1] = _places[i]; + } + _places[position] = monorail; + return true; + } + public bool Remove(int position) + { + if (!(position >= 0 && position < Count) || _places[position] == null) + return false; + _places[position] = null; + return true; + } + + public T? Get(int position) + { + if (!(position >= 0 && position < Count)) + return null; + return _places[position]; + } + } +}