using Bulldozer; using ProjectBulldozer.Generics; using ProjectBulldozer.Drawning; namespace ProjectBulldozer { public partial class FormTractorCollections : Form { private readonly TractorGenericStorage _storage; private readonly TractorGenericCollection _tractors; readonly int countPlace = 10; public FormTractorCollections() { InitializeComponent(); _storage = new TractorGenericStorage(pictureBoxCollections.Width, pictureBoxCollections.Height); } private void ReloadObjects() { int index = listBoxStorage.SelectedIndex; listBoxStorage.Items.Clear(); for (int i = 0; i < _storage.Keys.Count; i++) { listBoxStorage.Items.Add(_storage.Keys[i]); } if (listBoxStorage.Items.Count > 0 && (index == -1 || index >= listBoxStorage.Items.Count)) { listBoxStorage.SelectedIndex = 0; } else if (listBoxStorage.Items.Count > 0 && index > -1 && index < listBoxStorage.Items.Count) { listBoxStorage.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 listBoxStorage_SelectedIndexChanged(object sender, EventArgs e) { pictureBoxCollections.Image = _storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowTractors(); } private void ButtonRemoveObject_Click(object sender, EventArgs e) { if (listBoxStorage.SelectedIndex == -1) { return; } if (MessageBox.Show($"Удалить объект {listBoxStorage.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { _storage.DelSet(listBoxStorage.SelectedItem.ToString() ?? string.Empty); ReloadObjects(); } } private void ButtonAddTractor_Click(object sender, EventArgs e) { if (listBoxStorage.SelectedIndex == -1) { return; } var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } var formBulldozerConfig = new FormBulldozerConfig(); formBulldozerConfig.AddEvent(AddTractor); formBulldozerConfig.Show(); } private void AddTractor(DrawingTractor tractor) { tractor._pictureWidth = pictureBoxCollections.Width; tractor._pictureHeight = pictureBoxCollections.Height; if (listBoxStorage.SelectedIndex == -1) return; var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } int addedIndex = obj + tractor; if (addedIndex != -1 && addedIndex < countPlace) { MessageBox.Show("Объект добавлен"); pictureBoxCollections.Image = obj.ShowTractors(); } else { MessageBox.Show("Не удалось добавить объект"); } } private void ButtonRemoveTractor_Click(object sender, EventArgs e) { if (listBoxStorage.SelectedIndex == -1) return; var obj = _storage[listBoxStorage.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 (obj - pos != null) { MessageBox.Show("Объект удален"); pictureBoxCollections.Image = obj.ShowTractors(); } else { MessageBox.Show("Не удалось удалить объект"); } } private void ButtonRefreshCollection_Click(object sender, EventArgs e) { if (listBoxStorage.SelectedIndex == -1) return; var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty]; if (obj == null) { return; } pictureBoxCollections.Image = obj.ShowTractors(); } private void SaveToolStripMenuItem_Click(object sender, EventArgs e) { if (saveFileDialog.ShowDialog() == DialogResult.OK) { if (_storage.SaveData(saveFileDialog.FileName)) { MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void LoadToolStripMenuItem_Click(object sender, EventArgs e) { if (openFileDialog.ShowDialog() == DialogResult.OK) { if (_storage.LoadData(openFileDialog.FileName)) { MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } ReloadObjects(); } } } }