diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs index c3b8367..611bee8 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.Designer.cs @@ -34,6 +34,10 @@ maskedTextBoxNumber = new MaskedTextBox(); pictureBoxCollection = new PictureBox(); panel = new Panel(); + textBoxStorageName = new TextBox(); + listBoxStorages = new ListBox(); + ButtonDelObject = new Button(); + ButtonAddObject = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); panel.SuspendLayout(); SuspendLayout(); @@ -41,7 +45,7 @@ // buttonAddBus // buttonAddBus.Anchor = AnchorStyles.Top | AnchorStyles.Right; - buttonAddBus.Location = new Point(14, 3); + buttonAddBus.Location = new Point(13, 277); buttonAddBus.Name = "buttonAddBus"; buttonAddBus.Size = new Size(138, 31); buttonAddBus.TabIndex = 0; @@ -52,7 +56,7 @@ // buttonRemoveBus // buttonRemoveBus.Anchor = AnchorStyles.Top | AnchorStyles.Right; - buttonRemoveBus.Location = new Point(14, 69); + buttonRemoveBus.Location = new Point(13, 343); buttonRemoveBus.Name = "buttonRemoveBus"; buttonRemoveBus.Size = new Size(138, 31); buttonRemoveBus.TabIndex = 1; @@ -63,7 +67,7 @@ // ButtonRefreshCollection // ButtonRefreshCollection.Anchor = AnchorStyles.Top | AnchorStyles.Right; - ButtonRefreshCollection.Location = new Point(14, 106); + ButtonRefreshCollection.Location = new Point(13, 406); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Size = new Size(138, 31); ButtonRefreshCollection.TabIndex = 2; @@ -74,9 +78,9 @@ // maskedTextBoxNumber // maskedTextBoxNumber.Anchor = AnchorStyles.Top | AnchorStyles.Right; - maskedTextBoxNumber.Location = new Point(14, 40); + maskedTextBoxNumber.Location = new Point(30, 314); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - maskedTextBoxNumber.Size = new Size(138, 23); + maskedTextBoxNumber.Size = new Size(100, 23); maskedTextBoxNumber.TabIndex = 3; maskedTextBoxNumber.Text = "_"; // @@ -84,28 +88,69 @@ // pictureBoxCollection.Location = new Point(2, 2); pictureBoxCollection.Name = "pictureBoxCollection"; - pictureBoxCollection.Size = new Size(703, 457); + pictureBoxCollection.Size = new Size(633, 454); pictureBoxCollection.TabIndex = 4; pictureBoxCollection.TabStop = false; // // panel // panel.Anchor = AnchorStyles.Top | AnchorStyles.Right; + panel.Controls.Add(textBoxStorageName); + panel.Controls.Add(listBoxStorages); + panel.Controls.Add(ButtonDelObject); + panel.Controls.Add(ButtonAddObject); panel.Controls.Add(buttonAddBus); panel.Controls.Add(maskedTextBoxNumber); panel.Controls.Add(ButtonRefreshCollection); panel.Controls.Add(buttonRemoveBus); - panel.Location = new Point(721, 2); + panel.Location = new Point(660, 2); panel.Name = "panel"; - panel.Size = new Size(162, 460); + panel.Size = new Size(162, 454); panel.TabIndex = 5; panel.Tag = ""; // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(13, 20); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(131, 23); + textBoxStorageName.TabIndex = 9; + // + // listBoxStorages + // + listBoxStorages.FormattingEnabled = true; + listBoxStorages.ItemHeight = 15; + listBoxStorages.Location = new Point(13, 100); + listBoxStorages.Name = "listBoxStorages"; + listBoxStorages.Size = new Size(138, 64); + listBoxStorages.TabIndex = 8; + listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged; + // + // ButtonDelObject + // + ButtonDelObject.Location = new Point(13, 170); + ButtonDelObject.Name = "ButtonDelObject"; + ButtonDelObject.Size = new Size(138, 27); + ButtonDelObject.TabIndex = 7; + ButtonDelObject.Text = "Удалить набор"; + ButtonDelObject.UseVisualStyleBackColor = true; + ButtonDelObject.Click += ButtonDelObject_Click; + // + // ButtonAddObject + // + ButtonAddObject.Location = new Point(13, 65); + ButtonAddObject.Name = "ButtonAddObject"; + ButtonAddObject.Size = new Size(138, 29); + ButtonAddObject.TabIndex = 6; + ButtonAddObject.Text = "Добавить набор"; + ButtonAddObject.UseVisualStyleBackColor = true; + ButtonAddObject.Click += ButtonAddObject_Click; + // // FormBusCollection // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(884, 461); + ClientSize = new Size(823, 468); Controls.Add(panel); Controls.Add(pictureBoxCollection); Name = "FormBusCollection"; @@ -125,5 +170,9 @@ private PictureBox pictureBoxCollection; private Button ButtonRefreshCollection; private Panel panel; + private ListBox listBoxStorages; + private Button ButtonDelObject; + private Button ButtonAddObject; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs index bc9b63c..7c97583 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusCollection.cs @@ -9,28 +9,86 @@ using System.Threading.Tasks; using System.Windows.Forms; using DoubleDeckerBus.DrawningObjects; using DoubleDeckerBus.Generics; +using DoubleDeckerBus.Generics.ProjectSportCar.Generics; using DoubleDeckerBus.MovementStrategy; namespace DoubleDeckerBus { public partial class FormBusCollection : Form { - private readonly BusesGenericCollection _buses; + private readonly BusesGenericStorage _storage; public FormBusCollection() { InitializeComponent(); - _buses = new BusesGenericCollection - (pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new BusesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + private void ReloadObjects() + { + int index = listBoxStorages.SelectedIndex; + + listBoxStorages.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) + { + listBoxStorages.Items.Add(_storage.Keys[i]); + } + + if (listBoxStorages.Items.Count > 0 && (index == -1 || index >= listBoxStorages.Items.Count)) + { + listBoxStorages.SelectedIndex = 0; + } + + else if (listBoxStorages.Items.Count > 0 && index > -1 && index < listBoxStorages.Items.Count) + { + listBoxStorages.SelectedIndex = index; + } + } + private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBuses(); + } + private void ButtonAddObject_Click(object sender, EventArgs e) + { + + if (string.IsNullOrEmpty(textBoxStorageName.Text)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(textBoxStorageName.Text); + ReloadObjects(); + } + private void ButtonDelObject_Click(object sender, EventArgs e) + { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить объект{listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, + MessageBoxIcon.Question) == DialogResult.Yes) + { + _storage.DelSet(listBoxStorages.SelectedItem.ToString() + ?? string.Empty); + ReloadObjects(); + } } private void buttonAddBus_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } DoubleDeckerBus form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_buses + form.SelectedBus != -1) + if (obj + form.SelectedBus) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _buses.ShowBuses(); + pictureBoxCollection.Image = obj.ShowBuses(); } else { @@ -40,16 +98,26 @@ namespace DoubleDeckerBus } private void buttonRemoveBus_Click(object sender, EventArgs e) { + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } if (MessageBox.Show("Удалить объект?", "Удаление", - MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (_buses - pos != null) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _buses.ShowBuses(); + pictureBoxCollection.Image = obj.ShowBuses(); } else { @@ -58,7 +126,17 @@ namespace DoubleDeckerBus } private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _buses.ShowBuses(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowBuses(); } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericCollection.cs index 6f021b3..62aff82 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericCollection.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericCollection.cs @@ -25,28 +25,29 @@ namespace DoubleDeckerBus.Generics _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } - public static int operator +(BusesGenericCollection collect, T? obj) + public static bool operator +(BusesGenericCollection collect, T? obj) { if (obj == null) { - return -1; + return false; } - return collect?._collection.Insert(obj) ?? -1; + + return collect?._collection.Insert(obj) ?? false; } - public static bool operator -(BusesGenericCollection collect, int pos) + public static T? operator -(BusesGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { collect._collection.Remove(pos); - return true; } - return false; + return obj; } public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } + public Bitmap ShowBuses() { Bitmap bmp = new(_pictureWidth, _pictureHeight); @@ -69,19 +70,18 @@ namespace DoubleDeckerBus.Generics } private void DrawObjects(Graphics g) { - int widthObjCount = _pictureWidth / _placeSizeWidth; - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach (var bus in _collection.GetTheBuses()) { - T? type = _collection.Get(i); - if (type != null) + if (bus != null) { - int row = i / widthObjCount; - int col = widthObjCount - 1 - (i % widthObjCount); - - type.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight); - type?.DrawTransport(g); + int inRow = _pictureWidth / _placeSizeWidth; + bus.SetPosition((inRow - 1 - (i % inRow)) * _placeSizeWidth, i / inRow * _placeSizeHeight); + bus.DrawTransport(g); } + i++; } } - } + + } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericStorage.cs b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericStorage.cs new file mode 100644 index 0000000..6b9eb8e --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/Generics/BusesGenericStorage.cs @@ -0,0 +1,53 @@ +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 +{ + namespace ProjectSportCar.Generics + { + internal class BusesGenericStorage + { + readonly Dictionary> _busStorages; + public List Keys => _busStorages.Keys.ToList(); + private readonly int _pictureWidth; + private readonly int _pictureHeight; + public BusesGenericStorage(int pictureWidth, int pictureHeight) + { + _busStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + public void AddSet(string name) + { + _busStorages.Add(name, new BusesGenericCollection(_pictureWidth, _pictureHeight)); + } + public void DelSet(string name) + { + if (!_busStorages.ContainsKey(name)) + { + return; + } + + _busStorages.Remove(name); + } + public BusesGenericCollection? + this[string ind] + { + get + { + if (_busStorages.ContainsKey(ind)) + { + return _busStorages[ind]; + } + + return null; + } + } + } + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/Generics/SetGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/Generics/SetGeneric.cs index 2a45004..22e9633 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Generics/SetGeneric.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Generics/SetGeneric.cs @@ -8,46 +8,35 @@ using System.Threading.Tasks; namespace DoubleDeckerBus.Generics { internal class SetGeneric - where T : class + where T : class { - private readonly T?[] _places; - public int Count => _places.Length; + private readonly List _places; + public int Count => _places.Count; + private readonly int _maxCount; public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } - public int Insert(T bus) + public bool Insert(T bus) { - return Insert(bus, 0); + if (_places.Count == _maxCount) + { + return false; + } + + Insert(bus, 0); + return true; } - public int Insert(T bus, int position) + public bool Insert(T bus, int position) { - int NoEmpty = 0, temp = 0; - for (int i = position; i < Count; i++) + if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) { - if (_places[i] != null) NoEmpty++; + return false; } - 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; + _places.Insert(position, bus); + return true; } public bool Remove(int position) { @@ -56,16 +45,41 @@ namespace DoubleDeckerBus.Generics return false; } - _places[position] = null; + _places.RemoveAt(position); return true; } - public T? Get(int position) + public T? this[int position] { - if (position < 0 || position >= _places.Length) + get { - return null; + if (position < 0 || position >= _maxCount) + { + return null; + } + + return _places[position]; + } + set + { + if (!(position >= 0 && position < Count && _places.Count < _maxCount)) + { + return; + } + + _places.Insert(position, value); + return; + } + } + public IEnumerable GetTheBuses(int? maxTheBuses = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxTheBuses.HasValue && i == maxTheBuses.Value) + { + yield break; + } } - return _places[position]; } } }