diff --git a/base/Catamaran/Catamaran/Catamaran.cs b/base/Catamaran/Catamaran/Catamaran.cs index 5549617..6eaeb63 100644 --- a/base/Catamaran/Catamaran/Catamaran.cs +++ b/base/Catamaran/Catamaran/Catamaran.cs @@ -19,7 +19,7 @@ namespace Catamaran private AbstractStrategy? _strategy; /// - /// + /// /// public DrawningCatamaran? SelectedCatamaran { get; private set; } @@ -83,7 +83,7 @@ namespace Catamaran } /// - /// + /// /// /// /// @@ -106,7 +106,7 @@ namespace Catamaran } /// - /// + /// /// /// /// @@ -175,7 +175,7 @@ namespace Catamaran } } /// - /// + /// /// /// /// diff --git a/base/Catamaran/Catamaran/CatamaransGenericCollection.cs b/base/Catamaran/Catamaran/CatamaransGenericCollection.cs index d86bc47..f421270 100644 --- a/base/Catamaran/Catamaran/CatamaransGenericCollection.cs +++ b/base/Catamaran/Catamaran/CatamaransGenericCollection.cs @@ -56,13 +56,13 @@ namespace Catamaran.Generics /// /// /// - public static int operator +(CatamaransGenericCollection collect, T? obj) + public static bool operator +(CatamaransGenericCollection collect, T? obj) { if (obj == null) { - return -1; + return false; } - return collect._collection.Insert(obj); + return collect?._collection.Insert(obj) ?? false; } /// /// Перегрузка оператора вычитания @@ -70,15 +70,16 @@ namespace Catamaran.Generics /// /// /// - public static bool operator -(CatamaransGenericCollection collect, int pos) + public static T? operator -(CatamaransGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { - return collect._collection.Remove(pos); + collect._collection.Remove(pos); } - return false; + return obj; } + /// /// Получение объекта IMoveableObject /// @@ -86,8 +87,9 @@ namespace Catamaran.Generics /// public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } + /// /// Вывод всего набора объектов /// @@ -126,27 +128,29 @@ namespace Catamaran.Generics /// private void DrawObjects(Graphics g) { - // TODO получение объекта - // TODO установка позиции - // TODO прорисовка объекта - T? obj; - int width = _pictureWidth / _placeSizeWidth; - int height = _pictureHeight / _placeSizeHeight; - int diff = 1, currWidth = 0; - for (int i = 0; i < _collection.Count; i++) + { - currWidth++; - if (currWidth > width) + // TODO получение объекта + // TODO установка позиции + // TODO прорисовка объекта + int width = _pictureWidth / _placeSizeWidth; + int height = _pictureHeight / _placeSizeHeight; + int diff = 1, currWidth = 0, i = 0; + foreach (var catamaran in _collection.GetCatamarans()) { - diff++; - currWidth = 1; - } - obj = _collection.Get(i); - if (obj != null) - { - obj.SetPosition(i % width * _placeSizeWidth + _placeSizeWidth / 40, - (height - diff) * _placeSizeHeight + _placeSizeHeight / 15); - obj.DrawTransport(g); + currWidth++; + if (currWidth > width) + { + diff++; + currWidth = 1; + } + if (catamaran != null) + { + catamaran.SetPosition(i % width * _placeSizeWidth + _placeSizeWidth / 40, + (height - diff) * _placeSizeHeight + _placeSizeHeight / 15); + catamaran.DrawTransport(g); + } + i++; } } } diff --git a/base/Catamaran/Catamaran/CatamaransGenericStorage.cs b/base/Catamaran/Catamaran/CatamaransGenericStorage.cs new file mode 100644 index 0000000..39c92da --- /dev/null +++ b/base/Catamaran/Catamaran/CatamaransGenericStorage.cs @@ -0,0 +1,77 @@ +using Catamaran.DrawningObjects; +using Catamaran.MovementStrategy; +namespace Catamaran.Generics +{ + /// + /// Класс для хранения коллекции + /// + internal class CatamaransGenericStorage + { + /// + /// Словарь (хранилище) + /// + readonly Dictionary> _catamaranStorages; + /// + /// Возвращение списка названий наборов + /// + public List Keys => _catamaranStorages.Keys.ToList(); + /// + /// Ширина окна отрисовки + /// + private readonly int _pictureWidth; + /// + /// Высота окна отрисовки + /// + private readonly int _pictureHeight; + /// + /// Конструктор + /// + /// + /// + public CatamaransGenericStorage(int pictureWidth, int pictureHeight) + { + _catamaranStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + /// + /// Добавление набора + /// + /// Название набора + public void AddSet(string name) + { + // TODO Прописать логику для добавления + if (_catamaranStorages.ContainsKey(name)) return; + _catamaranStorages[name] = new CatamaransGenericCollection(_pictureWidth, _pictureHeight); + } + /// + /// Удаление набора + /// + /// Название набора + public void DelSet(string name) + { + // TODO Прописать логику для удаления + if (!_catamaranStorages.ContainsKey(name)) + return; + _catamaranStorages.Remove(name); + } + /// + /// Доступ к набору + /// + /// + /// + public CatamaransGenericCollection? + this[string ind] + { + get + { + // TODO Продумать логику получения набора + if (_catamaranStorages.ContainsKey(ind)) + return _catamaranStorages[ind]; + return null; + } + } + } +} diff --git a/base/Catamaran/Catamaran/FormCatamaranCollection.Designer.cs b/base/Catamaran/Catamaran/FormCatamaranCollection.Designer.cs index c133790..238e6f2 100644 --- a/base/Catamaran/Catamaran/FormCatamaranCollection.Designer.cs +++ b/base/Catamaran/Catamaran/FormCatamaranCollection.Designer.cs @@ -30,24 +30,31 @@ { this.pictureBoxCollection = new System.Windows.Forms.PictureBox(); this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.groupBoxSets = new System.Windows.Forms.GroupBox(); + this.textBoxStorageName = new System.Windows.Forms.TextBox(); + this.buttonDelObject = new System.Windows.Forms.Button(); + this.listBoxStorages = new System.Windows.Forms.ListBox(); + this.buttonAddObject = new System.Windows.Forms.Button(); this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox(); this.ButtonRefreshCollection = new System.Windows.Forms.Button(); this.ButtonRemoveCatamaran = new System.Windows.Forms.Button(); this.ButtonAddCatamaran = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit(); this.groupBoxTools.SuspendLayout(); + this.groupBoxSets.SuspendLayout(); this.SuspendLayout(); // // pictureBoxCollection // this.pictureBoxCollection.Location = new System.Drawing.Point(0, 1); this.pictureBoxCollection.Name = "pictureBoxCollection"; - this.pictureBoxCollection.Size = new System.Drawing.Size(741, 459); + this.pictureBoxCollection.Size = new System.Drawing.Size(736, 459); this.pictureBoxCollection.TabIndex = 0; this.pictureBoxCollection.TabStop = false; // // groupBoxTools // + this.groupBoxTools.Controls.Add(this.groupBoxSets); this.groupBoxTools.Controls.Add(this.maskedTextBoxNumber); this.groupBoxTools.Controls.Add(this.ButtonRefreshCollection); this.groupBoxTools.Controls.Add(this.ButtonRemoveCatamaran); @@ -59,18 +66,68 @@ this.groupBoxTools.TabStop = false; this.groupBoxTools.Text = "Инструменты"; // + // groupBoxSets + // + this.groupBoxSets.Controls.Add(this.textBoxStorageName); + this.groupBoxSets.Controls.Add(this.buttonDelObject); + this.groupBoxSets.Controls.Add(this.listBoxStorages); + this.groupBoxSets.Controls.Add(this.buttonAddObject); + this.groupBoxSets.Location = new System.Drawing.Point(3, 21); + this.groupBoxSets.Name = "groupBoxSets"; + this.groupBoxSets.Size = new System.Drawing.Size(138, 245); + this.groupBoxSets.TabIndex = 4; + this.groupBoxSets.TabStop = false; + this.groupBoxSets.Text = "Наборы"; + // + // textBoxStorageName + // + this.textBoxStorageName.Location = new System.Drawing.Point(3, 36); + this.textBoxStorageName.Name = "textBoxStorageName"; + this.textBoxStorageName.Size = new System.Drawing.Size(134, 23); + this.textBoxStorageName.TabIndex = 4; + // + // buttonDelObject + // + this.buttonDelObject.Location = new System.Drawing.Point(3, 209); + this.buttonDelObject.Name = "buttonDelObject"; + this.buttonDelObject.Size = new System.Drawing.Size(134, 27); + this.buttonDelObject.TabIndex = 3; + this.buttonDelObject.Text = "Удалить набор"; + this.buttonDelObject.UseVisualStyleBackColor = true; + this.buttonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click); + // + // listBoxStorages + // + this.listBoxStorages.FormattingEnabled = true; + this.listBoxStorages.ItemHeight = 15; + this.listBoxStorages.Location = new System.Drawing.Point(3, 102); + this.listBoxStorages.Name = "listBoxStorages"; + this.listBoxStorages.Size = new System.Drawing.Size(134, 94); + this.listBoxStorages.TabIndex = 2; + this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged); + // + // buttonAddObject + // + this.buttonAddObject.Location = new System.Drawing.Point(3, 65); + this.buttonAddObject.Name = "buttonAddObject"; + this.buttonAddObject.Size = new System.Drawing.Size(134, 25); + this.buttonAddObject.TabIndex = 1; + this.buttonAddObject.Text = "Добавить набор"; + this.buttonAddObject.UseVisualStyleBackColor = true; + this.buttonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click); + // // maskedTextBoxNumber // - this.maskedTextBoxNumber.Location = new System.Drawing.Point(0, 108); + this.maskedTextBoxNumber.Location = new System.Drawing.Point(3, 321); this.maskedTextBoxNumber.Name = "maskedTextBoxNumber"; - this.maskedTextBoxNumber.Size = new System.Drawing.Size(141, 23); + this.maskedTextBoxNumber.Size = new System.Drawing.Size(137, 23); this.maskedTextBoxNumber.TabIndex = 3; // // ButtonRefreshCollection // - this.ButtonRefreshCollection.Location = new System.Drawing.Point(0, 211); + this.ButtonRefreshCollection.Location = new System.Drawing.Point(0, 420); this.ButtonRefreshCollection.Name = "ButtonRefreshCollection"; - this.ButtonRefreshCollection.Size = new System.Drawing.Size(141, 34); + this.ButtonRefreshCollection.Size = new System.Drawing.Size(140, 34); this.ButtonRefreshCollection.TabIndex = 2; this.ButtonRefreshCollection.Text = "Обновить коллекцию"; this.ButtonRefreshCollection.UseVisualStyleBackColor = true; @@ -78,9 +135,9 @@ // // ButtonRemoveCatamaran // - this.ButtonRemoveCatamaran.Location = new System.Drawing.Point(0, 157); + this.ButtonRemoveCatamaran.Location = new System.Drawing.Point(0, 363); this.ButtonRemoveCatamaran.Name = "ButtonRemoveCatamaran"; - this.ButtonRemoveCatamaran.Size = new System.Drawing.Size(141, 34); + this.ButtonRemoveCatamaran.Size = new System.Drawing.Size(140, 34); this.ButtonRemoveCatamaran.TabIndex = 1; this.ButtonRemoveCatamaran.Text = "Удалить катамаран"; this.ButtonRemoveCatamaran.UseVisualStyleBackColor = true; @@ -88,9 +145,9 @@ // // ButtonAddCatamaran // - this.ButtonAddCatamaran.Location = new System.Drawing.Point(0, 22); + this.ButtonAddCatamaran.Location = new System.Drawing.Point(3, 270); this.ButtonAddCatamaran.Name = "ButtonAddCatamaran"; - this.ButtonAddCatamaran.Size = new System.Drawing.Size(141, 34); + this.ButtonAddCatamaran.Size = new System.Drawing.Size(137, 34); this.ButtonAddCatamaran.TabIndex = 0; this.ButtonAddCatamaran.Text = "Добавить катамаран"; this.ButtonAddCatamaran.UseVisualStyleBackColor = true; @@ -104,10 +161,12 @@ this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.pictureBoxCollection); this.Name = "FormCatamaranCollection"; - this.Text = "FormCatamaranCollection"; + this.Text = "Набор катамаранов"; ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit(); this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.PerformLayout(); + this.groupBoxSets.ResumeLayout(false); + this.groupBoxSets.PerformLayout(); this.ResumeLayout(false); } @@ -120,5 +179,10 @@ private Button ButtonRefreshCollection; private Button ButtonRemoveCatamaran; private MaskedTextBox maskedTextBoxNumber; + private GroupBox groupBoxSets; + private Button buttonAddObject; + private Button buttonDelObject; + private ListBox listBoxStorages; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/base/Catamaran/Catamaran/FormCatamaranCollection.cs b/base/Catamaran/Catamaran/FormCatamaranCollection.cs index 3db931c..5710d91 100644 --- a/base/Catamaran/Catamaran/FormCatamaranCollection.cs +++ b/base/Catamaran/Catamaran/FormCatamaranCollection.cs @@ -12,17 +12,87 @@ namespace Catamaran /// /// Набор объектов /// - private readonly CatamaransGenericCollection _catamarans; + private readonly CatamaransGenericStorage _storage; + /// /// Конструктор /// public FormCatamaranCollection() { InitializeComponent(); - _catamarans = new CatamaransGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new CatamaransGenericStorage(pictureBoxCollection.Width,pictureBoxCollection.Height); } + + /// + /// Заполнение listBoxStorages + /// + 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 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 ListBoxObjects_SelectedIndexChanged(object sender, + EventArgs e) + { + pictureBoxCollection.Image = + _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowCatamarans(); + } + /// + /// Удаление набора + /// + /// + /// + 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(); + } + } + /// /// Добавление объекта в набор /// @@ -30,13 +100,23 @@ namespace Catamaran /// private void ButtonAddCatamaran_Click(object sender, EventArgs e) { - FormCatamaran form = new(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + + FormCatamaran form = new FormCatamaran(); if (form.ShowDialog() == DialogResult.OK) { - if (_catamarans + form.SelectedCatamaran != -1) + if (obj + form.SelectedCatamaran) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _catamarans.ShowCatamarans(); + pictureBoxCollection.Image = obj.ShowCatamarans(); } else { @@ -46,27 +126,38 @@ namespace Catamaran } /// /// Удаление объекта из набора - /// + /// listBoxStorages /// /// private void ButtonRemoveCatamaran_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) { return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (_catamarans - pos != null) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _catamarans.ShowCatamarans(); + pictureBoxCollection.Image = obj.ShowCatamarans(); } else { MessageBox.Show("Не удалось удалить объект"); } } + /// /// Обновление рисунка по набору /// @@ -75,7 +166,18 @@ namespace Catamaran private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _catamarans.ShowCatamarans(); + if (listBoxStorages.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowCatamarans(); + } } } diff --git a/base/Catamaran/Catamaran/SetGeneric.cs b/base/Catamaran/Catamaran/SetGeneric.cs index 116b0bc..64ee575 100644 --- a/base/Catamaran/Catamaran/SetGeneric.cs +++ b/base/Catamaran/Catamaran/SetGeneric.cs @@ -1,10 +1,11 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; using System.Text; using System.Threading.Tasks; -namespace Catamaran +namespace Catamaran.Generics { /// /// Параметризованный набор объектов @@ -14,30 +15,40 @@ namespace Catamaran where T : class { /// - /// Массив объектов, которые храним + /// Список объектов, которые храним /// - private readonly T?[] _places; + private readonly List _places; /// - /// Количество объектов в массиве + /// Количество объектов в списке /// - public int Count => _places.Length; + public int Count => _places.Count; + /// + /// Максимальное количество объектов в списке + /// + private readonly int _maxCount; /// /// Конструктор /// /// public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(_maxCount); } /// /// Добавление объекта в набор /// /// Добавляемый катамаран /// - public int Insert(T catamaran) + public bool Insert(T catamaran) { // TODO вставка в начало набора - return Insert(catamaran, 0); + if (_places.Count == _maxCount) + { + return false; + } + Insert(catamaran, 0); + return true; } /// /// Добавление объекта в набор на конкретную позицию @@ -45,36 +56,20 @@ namespace Catamaran /// Добавляемый катамаран /// Позиция /// - public int Insert(T catamaran, int position) + public bool Insert(T catamaran, int position) { // TODO проверка позиции // TODO проверка, что элемент массива по этой позиции пустой, //если нет, то проверка, что после вставляемого элемента в массиве есть пустой элемент // сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента // TODO вставка по позиции - int nullIndex = -1, i; - - if (position < 0 || position >= Count) - return -1; - - for (i = position; i < Count; i++) + if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) { - if (_places[i] == null) - { - nullIndex = i; - break; - } - } - if (nullIndex < 0) - return -1; - - for (i = nullIndex; i > position; i--) - { - _places[i] = _places[i - 1]; + return false; } - _places[position] = catamaran; - return position; + _places.Insert(position, catamaran); + return true; } /// /// Удаление объекта из набора с конкретной позиции @@ -85,9 +80,11 @@ namespace Catamaran { // TODO проверка позиции // TODO удаление объекта из массива, присвоив элементу массива значение null - if (position < 0 || position >= Count) { - return false; } - _places[position] = null; + if (position < 0 || position >= Count) + { + return false; + } + _places.RemoveAt(position); return true; } /// @@ -95,12 +92,40 @@ namespace Catamaran /// /// /// - public T? Get(int position) + public T? this[int position] { - // TODO проверка позиции - if (position < 0 || position >= Count) - return null; - return _places[position]; + get + { + // TODO проверка позиции + if (!(position >= 0 && position < Count)) + return null; + return _places[position]; + } + set + { + // TODO проверка позиции + // TODO проверка свободных мест в списке + // TODO вставка в список по позиции + if (!(position >= 0 && position < Count && _places.Count < _maxCount)) + return; + _places.Insert(position, value); + return; + } + } + /// + /// Проход по списку + /// + /// + public IEnumerable GetCatamarans(int? maxCatamarans = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxCatamarans.HasValue && i == maxCatamarans.Value) + { + yield break; + } + } } } } \ No newline at end of file