using ProjectStormtrooper.CollectionGenericObjects; using ProjectStormtrooper.Drawnings; 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 ProjectStormtrooper; /// /// Форма работы с компанией и ее коллекцией /// public partial class FormStormtrooperCollection : Form { /// /// Компания /// private AbstractCompany? _company; /// /// Конструктор /// public FormStormtrooperCollection() { InitializeComponent(); } /// /// Выбор компании /// /// /// private void СomboBoxSelectorCompany_SelectedIndexChanged(object sender, EventArgs e) { switch (comboBoxSelectorCompany.Text) { case "Хранилище": _company = new StormtrooperSharingService(pictureBox.Width, pictureBox.Height, new MassiveGenericObjects()); break; } } /// /// Создание объекта класса-перемещения /// /// private void CreateObject(string type) { if (_company == null) { return; } Random random = new(); DrawningStormtrooperBase drawningStormtrooperBase; switch (type) { case nameof(DrawingStormtrooper): drawningStormtrooperBase = new DrawingStormtrooper(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)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); break; case nameof(DrawningStormtrooperBase): drawningStormtrooperBase = new DrawningStormtrooperBase(random.Next(100, 300), random.Next(1000, 3000), GetColor(random)); break; default: return; } if (_company + drawningStormtrooperBase != -1) { MessageBox.Show("Объект добавлен"); pictureBox.Image = _company.Show(); } else { MessageBox.Show("Не удалось добавить объект"); } } /// /// Получение цвета /// /// Генератор случайных чисел /// private static Color GetColor(Random random) { 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; } return color; } /// /// Добавление базового штурмовика /// /// /// private void ButtonAddStormtrooperBase_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningStormtrooperBase)); /// /// Добавление штурмовика /// /// /// private void ButtonAddStormtrooper_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingStormtrooper)); /// /// Удаление объекта /// /// /// private void ButtonRemoveStormtrooper_Click(object sender, EventArgs e) { if (_company == null) { return; } if (string.IsNullOrEmpty(maskedTextBox.Text) || _company == null) { return; } if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBox.Text); if (_company - pos != null) { MessageBox.Show("Объект удален"); pictureBox.Image = _company.Show(); } else { MessageBox.Show("Не удалось удалить объект"); } } /// /// Передача объекта в другую форму /// /// /// private void ButtonGoToCheck_Click(object sender, EventArgs e) { if (_company == null) { return; } DrawningStormtrooperBase? stormtrooper = null; int counter = 100; while (stormtrooper == null) { stormtrooper = _company.GetRandomObject(); counter--; if (counter < -0) { break; } } if (stormtrooper == null) { return; } FormStormtrooper form = new() { SetStormtrooper = stormtrooper }; form.ShowDialog(); } /// /// Перерисовка коллекции /// /// /// private void ButtonRefresh_Click(object sender, EventArgs e) { if (_company == null) { return; } pictureBox.Image = _company.Show(); } }