diff --git a/Trolleybus/Trolleybus/DrawingBus.cs b/Trolleybus/Trolleybus/DrawingBus.cs index 114ecb1..3980aeb 100644 --- a/Trolleybus/Trolleybus/DrawingBus.cs +++ b/Trolleybus/Trolleybus/DrawingBus.cs @@ -102,6 +102,13 @@ namespace Trolleybus.DrawingObjects _busHeight = busHeight; EntityBus = new EntityBus(speed, weight, bodyColor); } + //Метод для того, чтобы изменить размеры окна, т.к. pictureBoxObject в FormBusConfig и pictureBoxCollection в FormBusesCollection + //имеют разные размеры, из-за чего объекты некорректно отрисовываются в FormBusesCollection после FormBusConfig + public void ChangePictureSize(int width, int height) + { + _pictureWidth = width; + _pictureHeight = height; + } /// /// Установка позиции /// diff --git a/Trolleybus/Trolleybus/EntityBus.cs b/Trolleybus/Trolleybus/EntityBus.cs index 25c6fbd..10b4e5b 100644 --- a/Trolleybus/Trolleybus/EntityBus.cs +++ b/Trolleybus/Trolleybus/EntityBus.cs @@ -39,5 +39,9 @@ namespace Trolleybus.Entities Weight = weight; BodyColor = bodyColor; } + public void ChangeBodyColor(Color color) + { + BodyColor = color; + } } } diff --git a/Trolleybus/Trolleybus/EntityTrolleybus.cs b/Trolleybus/Trolleybus/EntityTrolleybus.cs index 940184a..de3025b 100644 --- a/Trolleybus/Trolleybus/EntityTrolleybus.cs +++ b/Trolleybus/Trolleybus/EntityTrolleybus.cs @@ -38,6 +38,10 @@ namespace Trolleybus.Entities Horns = horns; Batteries = batteries; } + public void ChangeAdditionalColor(Color color) + { + AdditionalColor = color; + } } } diff --git a/Trolleybus/Trolleybus/FormBusConfig.Designer.cs b/Trolleybus/Trolleybus/FormBusConfig.Designer.cs new file mode 100644 index 0000000..058751d --- /dev/null +++ b/Trolleybus/Trolleybus/FormBusConfig.Designer.cs @@ -0,0 +1,357 @@ +namespace Trolleybus +{ + partial class FormBusConfig + { + /// + /// 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() + { + groupBoxConfig = new GroupBox(); + labelAdvancedObject = new Label(); + labelSimpleObject = new Label(); + groupBoxColor = new GroupBox(); + panelLightBlue = new Panel(); + panelBlue = new Panel(); + panelPurple = new Panel(); + panelGray = new Panel(); + panelGreen = new Panel(); + panelYellow = new Panel(); + panelOrange = new Panel(); + panelRed = new Panel(); + checkBoxBatteries = new CheckBox(); + checkBoxHorns = new CheckBox(); + numericUpDownSpeed = new NumericUpDown(); + numericUpDownWeight = new NumericUpDown(); + labelWeight = new Label(); + labelSpeed = new Label(); + pictureBoxObject = new PictureBox(); + panelObject = new Panel(); + labelAdditionalColor = new Label(); + labelMainColor = new Label(); + buttonAdd = new Button(); + buttonCancel = new Button(); + groupBoxConfig.SuspendLayout(); + groupBoxColor.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)numericUpDownSpeed).BeginInit(); + ((System.ComponentModel.ISupportInitialize)numericUpDownWeight).BeginInit(); + ((System.ComponentModel.ISupportInitialize)pictureBoxObject).BeginInit(); + panelObject.SuspendLayout(); + SuspendLayout(); + // + // groupBoxConfig + // + groupBoxConfig.Controls.Add(labelAdvancedObject); + groupBoxConfig.Controls.Add(labelSimpleObject); + groupBoxConfig.Controls.Add(groupBoxColor); + groupBoxConfig.Controls.Add(checkBoxBatteries); + groupBoxConfig.Controls.Add(checkBoxHorns); + groupBoxConfig.Controls.Add(numericUpDownSpeed); + groupBoxConfig.Controls.Add(numericUpDownWeight); + groupBoxConfig.Controls.Add(labelWeight); + groupBoxConfig.Controls.Add(labelSpeed); + groupBoxConfig.Location = new Point(12, 81); + groupBoxConfig.Name = "groupBoxConfig"; + groupBoxConfig.Size = new Size(570, 250); + groupBoxConfig.TabIndex = 0; + groupBoxConfig.TabStop = false; + groupBoxConfig.Text = "Параметры"; + // + // labelAdvancedObject + // + labelAdvancedObject.BorderStyle = BorderStyle.FixedSingle; + labelAdvancedObject.Location = new Point(440, 188); + labelAdvancedObject.Name = "labelAdvancedObject"; + labelAdvancedObject.Size = new Size(120, 30); + labelAdvancedObject.TabIndex = 8; + labelAdvancedObject.Text = "Продвинутый"; + labelAdvancedObject.TextAlign = ContentAlignment.MiddleCenter; + labelAdvancedObject.MouseDown += LabelObject_MouseDown; + // + // labelSimpleObject + // + labelSimpleObject.BorderStyle = BorderStyle.FixedSingle; + labelSimpleObject.Location = new Point(283, 188); + labelSimpleObject.Name = "labelSimpleObject"; + labelSimpleObject.Size = new Size(120, 30); + labelSimpleObject.TabIndex = 7; + labelSimpleObject.Text = "Простой"; + labelSimpleObject.TextAlign = ContentAlignment.MiddleCenter; + labelSimpleObject.MouseDown += LabelObject_MouseDown; + // + // groupBoxColor + // + groupBoxColor.Controls.Add(panelLightBlue); + groupBoxColor.Controls.Add(panelBlue); + groupBoxColor.Controls.Add(panelPurple); + groupBoxColor.Controls.Add(panelGray); + groupBoxColor.Controls.Add(panelGreen); + groupBoxColor.Controls.Add(panelYellow); + groupBoxColor.Controls.Add(panelOrange); + groupBoxColor.Controls.Add(panelRed); + groupBoxColor.Location = new Point(283, 32); + groupBoxColor.Name = "groupBoxColor"; + groupBoxColor.Size = new Size(277, 145); + groupBoxColor.TabIndex = 6; + groupBoxColor.TabStop = false; + groupBoxColor.Text = "Цвета"; + // + // panelLightBlue + // + panelLightBlue.BackColor = Color.FromArgb(128, 255, 255); + panelLightBlue.Location = new Point(5, 85); + panelLightBlue.Name = "panelLightBlue"; + panelLightBlue.Size = new Size(50, 40); + panelLightBlue.TabIndex = 0; + // + // panelBlue + // + panelBlue.BackColor = Color.FromArgb(0, 0, 192); + panelBlue.Location = new Point(75, 85); + panelBlue.Name = "panelBlue"; + panelBlue.Size = new Size(50, 40); + panelBlue.TabIndex = 0; + // + // panelPurple + // + panelPurple.BackColor = Color.DarkViolet; + panelPurple.Location = new Point(145, 85); + panelPurple.Name = "panelPurple"; + panelPurple.Size = new Size(50, 40); + panelPurple.TabIndex = 0; + // + // panelGray + // + panelGray.BackColor = Color.Gray; + panelGray.Location = new Point(215, 85); + panelGray.Name = "panelGray"; + panelGray.Size = new Size(50, 40); + panelGray.TabIndex = 0; + // + // panelGreen + // + panelGreen.BackColor = Color.FromArgb(0, 192, 0); + panelGreen.Location = new Point(215, 25); + panelGreen.Name = "panelGreen"; + panelGreen.Size = new Size(50, 40); + panelGreen.TabIndex = 0; + // + // panelYellow + // + panelYellow.BackColor = Color.Yellow; + panelYellow.Location = new Point(145, 25); + panelYellow.Name = "panelYellow"; + panelYellow.Size = new Size(50, 40); + panelYellow.TabIndex = 0; + // + // panelOrange + // + panelOrange.BackColor = Color.FromArgb(255, 128, 0); + panelOrange.Location = new Point(75, 25); + panelOrange.Name = "panelOrange"; + panelOrange.Size = new Size(50, 40); + panelOrange.TabIndex = 0; + // + // panelRed + // + panelRed.BackColor = Color.Firebrick; + panelRed.Location = new Point(5, 25); + panelRed.Name = "panelRed"; + panelRed.Size = new Size(50, 40); + panelRed.TabIndex = 0; + // + // checkBoxBatteries + // + checkBoxBatteries.AutoSize = true; + checkBoxBatteries.Location = new Point(13, 149); + checkBoxBatteries.Name = "checkBoxBatteries"; + checkBoxBatteries.Size = new Size(230, 24); + checkBoxBatteries.TabIndex = 5; + checkBoxBatteries.Text = "Наличие отсека для батарей"; + checkBoxBatteries.UseVisualStyleBackColor = true; + // + // checkBoxHorns + // + checkBoxHorns.AutoSize = true; + checkBoxHorns.Location = new Point(13, 119); + checkBoxHorns.Name = "checkBoxHorns"; + checkBoxHorns.Size = new Size(149, 24); + checkBoxHorns.TabIndex = 4; + checkBoxHorns.Text = "Наличие \"рогов\""; + checkBoxHorns.UseVisualStyleBackColor = true; + // + // numericUpDownSpeed + // + numericUpDownSpeed.Location = new Point(94, 32); + numericUpDownSpeed.Maximum = new decimal(new int[] { 1000, 0, 0, 0 }); + numericUpDownSpeed.Minimum = new decimal(new int[] { 100, 0, 0, 0 }); + numericUpDownSpeed.Name = "numericUpDownSpeed"; + numericUpDownSpeed.Size = new Size(150, 27); + numericUpDownSpeed.TabIndex = 3; + numericUpDownSpeed.Value = new decimal(new int[] { 100, 0, 0, 0 }); + // + // numericUpDownWeight + // + numericUpDownWeight.Location = new Point(94, 83); + numericUpDownWeight.Maximum = new decimal(new int[] { 1000, 0, 0, 0 }); + numericUpDownWeight.Minimum = new decimal(new int[] { 100, 0, 0, 0 }); + numericUpDownWeight.Name = "numericUpDownWeight"; + numericUpDownWeight.Size = new Size(150, 27); + numericUpDownWeight.TabIndex = 2; + numericUpDownWeight.Value = new decimal(new int[] { 100, 0, 0, 0 }); + // + // labelWeight + // + labelWeight.AutoSize = true; + labelWeight.Location = new Point(6, 85); + labelWeight.Name = "labelWeight"; + labelWeight.Size = new Size(36, 20); + labelWeight.TabIndex = 1; + labelWeight.Text = "Вес:"; + // + // labelSpeed + // + labelSpeed.AutoSize = true; + labelSpeed.Location = new Point(6, 32); + labelSpeed.Name = "labelSpeed"; + labelSpeed.Size = new Size(76, 20); + labelSpeed.TabIndex = 0; + labelSpeed.Text = "Скорость:"; + // + // pictureBoxObject + // + pictureBoxObject.BorderStyle = BorderStyle.FixedSingle; + pictureBoxObject.Location = new Point(35, 84); + pictureBoxObject.Name = "pictureBoxObject"; + pictureBoxObject.Size = new Size(327, 204); + pictureBoxObject.TabIndex = 1; + pictureBoxObject.TabStop = false; + // + // panelObject + // + panelObject.AllowDrop = true; + panelObject.Controls.Add(labelAdditionalColor); + panelObject.Controls.Add(labelMainColor); + panelObject.Controls.Add(pictureBoxObject); + panelObject.Location = new Point(612, 29); + panelObject.Name = "panelObject"; + panelObject.Size = new Size(401, 302); + panelObject.TabIndex = 2; + panelObject.DragDrop += PanelObject_DragDrop; + panelObject.DragEnter += PanelObject_DragEnter; + // + // labelAdditionalColor + // + labelAdditionalColor.AllowDrop = true; + labelAdditionalColor.BorderStyle = BorderStyle.FixedSingle; + labelAdditionalColor.Location = new Point(272, 19); + labelAdditionalColor.Name = "labelAdditionalColor"; + labelAdditionalColor.Size = new Size(90, 50); + labelAdditionalColor.TabIndex = 3; + labelAdditionalColor.Text = "Доп. цвет"; + labelAdditionalColor.TextAlign = ContentAlignment.MiddleCenter; + labelAdditionalColor.DragDrop += LabelAdditionalColor_DragDrop; + labelAdditionalColor.DragEnter += LabelAdditionalColor_DragEnter; + // + // labelMainColor + // + labelMainColor.AllowDrop = true; + labelMainColor.BorderStyle = BorderStyle.FixedSingle; + labelMainColor.Location = new Point(35, 19); + labelMainColor.Name = "labelMainColor"; + labelMainColor.Size = new Size(90, 50); + labelMainColor.TabIndex = 2; + labelMainColor.Text = "Цвет"; + labelMainColor.TextAlign = ContentAlignment.MiddleCenter; + labelMainColor.DragDrop += LabelMainColor_DragDrop; + labelMainColor.DragEnter += LabelMainColor_DragEnter; + // + // buttonAdd + // + buttonAdd.Location = new Point(647, 337); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(94, 29); + buttonAdd.TabIndex = 3; + buttonAdd.Text = "Добавить"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += ButtonAdd_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(884, 337); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(94, 29); + buttonCancel.TabIndex = 4; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + // + // FormBusConfig + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1032, 383); + Controls.Add(buttonCancel); + Controls.Add(buttonAdd); + Controls.Add(panelObject); + Controls.Add(groupBoxConfig); + Name = "FormBusConfig"; + Text = "FormBusConfig"; + groupBoxConfig.ResumeLayout(false); + groupBoxConfig.PerformLayout(); + groupBoxColor.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)numericUpDownSpeed).EndInit(); + ((System.ComponentModel.ISupportInitialize)numericUpDownWeight).EndInit(); + ((System.ComponentModel.ISupportInitialize)pictureBoxObject).EndInit(); + panelObject.ResumeLayout(false); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxConfig; + private NumericUpDown numericUpDownSpeed; + private NumericUpDown numericUpDownWeight; + private Label labelWeight; + private Label labelSpeed; + private CheckBox checkBoxBatteries; + private CheckBox checkBoxHorns; + private GroupBox groupBoxColor; + private Panel panelLightBlue; + private Panel panelBlue; + private Panel panelPurple; + private Panel panelGray; + private Panel panelGreen; + private Panel panelYellow; + private Panel panelOrange; + private Panel panelRed; + private Label labelAdvancedObject; + private Label labelSimpleObject; + private PictureBox pictureBoxObject; + private Panel panelObject; + private Label labelAdditionalColor; + private Label labelMainColor; + private Button buttonAdd; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/Trolleybus/Trolleybus/FormBusConfig.cs b/Trolleybus/Trolleybus/FormBusConfig.cs new file mode 100644 index 0000000..5c30082 --- /dev/null +++ b/Trolleybus/Trolleybus/FormBusConfig.cs @@ -0,0 +1,198 @@ +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; +using Trolleybus.DrawingObjects; +using Trolleybus.Entities; + +namespace Trolleybus +{ + public partial class FormBusConfig : Form + { + /// + /// Переменная-выбранный автобус + /// + DrawingBus? _bus = null; + /// + /// Событие + /// + private event Action? EventAddBus; + + /// + /// Конструктор + /// + public FormBusConfig() + { + InitializeComponent(); + panelRed.MouseDown += PanelColor_MouseDown; + panelOrange.MouseDown += PanelColor_MouseDown; + panelYellow.MouseDown += PanelColor_MouseDown; + panelGreen.MouseDown += PanelColor_MouseDown; + panelLightBlue.MouseDown += PanelColor_MouseDown; + panelBlue.MouseDown += PanelColor_MouseDown; + panelPurple.MouseDown += PanelColor_MouseDown; + panelGray.MouseDown += PanelColor_MouseDown; + buttonCancel.Click += (s, e) => Close(); + } + + /// + /// Отрисовка объекта + /// + private void DrawBus() + { + Bitmap bmp = new(pictureBoxObject.Width, pictureBoxObject.Height); + Graphics gr = Graphics.FromImage(bmp); + _bus?.SetPosition(5, 5); + _bus?.DrawTransport(gr); + pictureBoxObject.Image = bmp; + } + /// + /// Добавление события + /// + /// Привязанный метод + public void AddEvent(Action ev) + { + if (EventAddBus == null) + { + EventAddBus = ev; + } + else + { + EventAddBus += ev; + } + } + + /// + /// Передача информации (имени Label) при нажатии на Label (простой / продвинутый объект) + /// + /// + /// + private void LabelObject_MouseDown(object sender, MouseEventArgs e) + { + (sender as Label)?.DoDragDrop((sender as Label)?.Name, + DragDropEffects.Move | DragDropEffects.Copy); + } + /// + /// Проверка получаемой информации (ее типа на соответствие требуемому: текст) для панели + /// + /// + /// + private void PanelObject_DragEnter(object sender, DragEventArgs e) + { + if (e.Data?.GetDataPresent(DataFormats.Text) ?? false) + { + e.Effect = DragDropEffects.Copy; + } + else + { + e.Effect = DragDropEffects.None; + } + } + /// + /// Действия при приеме перетаскиваемой информации (имени Label) + /// + /// + /// + private void PanelObject_DragDrop(object sender, DragEventArgs e) + { + switch (e.Data?.GetData(DataFormats.Text).ToString()) + { + case "labelSimpleObject": + _bus = new DrawingBus( + (int)numericUpDownSpeed.Value, + (int)numericUpDownWeight.Value, + Color.White, + pictureBoxObject.Width, pictureBoxObject.Height); + break; + + case "labelAdvancedObject": + _bus = new DrawingTrolleybus( + (int)numericUpDownSpeed.Value, + (int)numericUpDownWeight.Value, + Color.White, Color.Black, + checkBoxHorns.Checked, checkBoxBatteries.Checked, + pictureBoxObject.Width, pictureBoxObject.Height); + break; + } + DrawBus(); + } + /// + /// Передача цвета при нажатии на одну из Panel с цветом + /// + /// + /// + private void PanelColor_MouseDown(object sender, MouseEventArgs e) + { + (sender as Panel)?.DoDragDrop((sender as Panel)?.BackColor, + DragDropEffects.Move | DragDropEffects.Copy); + } + /// + /// Проверка получаемой информации (ее типа на соответствие требуемому: цвет) для Label с основным цветом + /// + /// + /// + private void LabelMainColor_DragEnter(object sender, DragEventArgs e) + { + if (e.Data.GetDataPresent(typeof(Color)) && _bus != null) + { + e.Effect = DragDropEffects.Copy; + } + else + { + e.Effect = DragDropEffects.None; + } + } + /// + /// Действия при приеме перетаскиваемого цвета + /// + /// + /// + private void LabelMainColor_DragDrop(object sender, DragEventArgs e) + { + var color = (Color)e.Data.GetData(typeof(Color)); + _bus.EntityBus.ChangeBodyColor(color); + DrawBus(); + } + /// + /// Проверка получаемой информации (ее типа на соответствие требуемому: цвет) для Label с доп. цветом + /// и проверка, что создаётся троллейбус + /// + /// + /// + private void LabelAdditionalColor_DragEnter(object sender, DragEventArgs e) + { + if (e.Data.GetDataPresent(typeof(Color)) && _bus != null && _bus is DrawingTrolleybus) + { + e.Effect = DragDropEffects.Copy; + } + else + { + e.Effect = DragDropEffects.None; + } + } + private void LabelAdditionalColor_DragDrop(object sender, DragEventArgs e) + { + var color = (Color)e.Data.GetData(typeof(Color)); + //Приведение к EntityTrolleybus для замены доп. цвета + EntityTrolleybus? _trolleybus = _bus.EntityBus as EntityTrolleybus; + _trolleybus.ChangeAdditionalColor(color); + DrawBus(); + } + + /// + /// Добавление автобуса + /// + /// + /// + private void ButtonAdd_Click(object sender, EventArgs e) + { + EventAddBus?.Invoke(_bus); + Close(); + } + } +} diff --git a/Trolleybus/Trolleybus/FormBusConfig.resx b/Trolleybus/Trolleybus/FormBusConfig.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Trolleybus/Trolleybus/FormBusConfig.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/Trolleybus/Trolleybus/FormBusesCollection.cs b/Trolleybus/Trolleybus/FormBusesCollection.cs index 7e04e06..c4fde4f 100644 --- a/Trolleybus/Trolleybus/FormBusesCollection.cs +++ b/Trolleybus/Trolleybus/FormBusesCollection.cs @@ -97,6 +97,12 @@ namespace Trolleybus /// /// private void ButtonAddBus_Click(object sender, EventArgs e) + { + var formBusConfig = new FormBusConfig(); + formBusConfig.AddEvent(AddBus); + formBusConfig.Show(); + } + private void AddBus(DrawingBus selectedBus) { if (listBoxSets.SelectedIndex == -1) { @@ -107,19 +113,16 @@ namespace Trolleybus { return; } - - FormTrolleybus form = new FormTrolleybus(); - if (form.ShowDialog() == DialogResult.OK) + //Возвращение объекту размеров pictureBox с этой формы (до этого создался с размерами pictureBox с FormBusConfig) + selectedBus.ChangePictureSize(pictureBoxCollection.Width, pictureBoxCollection.Height); + if (obj + selectedBus != -1) { - if (obj + form.SelectedBus != -1) - { - MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = obj.ShowBuses(); - } - else - { - MessageBox.Show("Не удалось добавить объект"); - } + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = obj.ShowBuses(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); } } ///