Не работает смена цвета

This commit is contained in:
Baryshev Dmitry 2024-04-08 02:06:54 +04:00
parent 7b4eb0a558
commit d551b7b9e0
6 changed files with 120 additions and 80 deletions

View File

@ -39,7 +39,6 @@
labelCollectionName = new Label();
buttonCompanyCreate = new Button();
panelCompanyTools = new Panel();
buttonAddDumpTruck = new Button();
buttonAddTruck = new Button();
buttonRemoveTruck = new Button();
buttonRefresh = new Button();
@ -158,7 +157,6 @@
//
// panelCompanyTools
//
panelCompanyTools.Controls.Add(buttonAddDumpTruck);
panelCompanyTools.Controls.Add(buttonAddTruck);
panelCompanyTools.Controls.Add(buttonRemoveTruck);
panelCompanyTools.Controls.Add(buttonRefresh);
@ -171,19 +169,9 @@
panelCompanyTools.Size = new Size(232, 300);
panelCompanyTools.TabIndex = 10;
//
// buttonAddDumpTruck
//
buttonAddDumpTruck.Location = new Point(3, 51);
buttonAddDumpTruck.Name = "buttonAddDumpTruck";
buttonAddDumpTruck.Size = new Size(226, 48);
buttonAddDumpTruck.TabIndex = 2;
buttonAddDumpTruck.Text = "Добавление самосвала";
buttonAddDumpTruck.UseVisualStyleBackColor = true;
buttonAddDumpTruck.Click += ButtonAddDumpTruck_Click;
//
// buttonAddTruck
//
buttonAddTruck.Location = new Point(3, 3);
buttonAddTruck.Location = new Point(3, 35);
buttonAddTruck.Name = "buttonAddTruck";
buttonAddTruck.Size = new Size(226, 42);
buttonAddTruck.TabIndex = 0;
@ -271,7 +259,6 @@
#endregion
private GroupBox groupBoxTools;
private Button buttonAddDumpTruck;
private Button buttonGoToCheck;
private Button buttonRemoveTruck;
private MaskedTextBox maskedTextBoxPosition;

View File

@ -50,31 +50,29 @@ public partial class FormTruckCollection : Form
}
/// <summary>
/// Создание объекта класса-перемещения
/// Добавление грузовика
/// </summary>
/// <param name="type">Тип создаваемого объекта</param>
private void CreateObject(string type)
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddTruck_Click(object sender, EventArgs e)
{
if (_company == null)
FormTruckConfig form = new FormTruckConfig();
form.Show();
form.AddEvent(SetTruck);
}
/// <summary>
/// Добавление грузовика в автомобиль
/// </summary>
/// <param name="truck"></param>
private void SetTruck(DrawningTruck truck)
{
if (_company == null || truck == null)
{
return;
}
Random random = new();
DrawningTruck drawningTruck;
switch (type)
{
case nameof(DrawningTruck):
drawningTruck = new DrawningTruck(random.Next(100, 300),
random.Next(1000, 3000), GetColor(random));
break;
case nameof(DrawningDumpTruck):
drawningTruck = new DrawningDumpTruck(random.Next(100, 300), random.Next(1000, 3000),
GetColor(random), GetColor(random), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
break;
default:
return;
}
if (_company + drawningTruck != -1)
if (_company + truck != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _company.Show();
@ -84,35 +82,6 @@ public partial class FormTruckCollection : Form
MessageBox.Show("Не удалось добавить объект");
}
}
/// <summary>
/// Получение цвета
/// </summary>
/// <param name="random">Генератор случайных чисел</param>
/// <returns></returns>
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;
}
/// <summary>
/// Добавление грузовика
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddTruck_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTruck));
/// <summary>
/// Добавление самосвала
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddDumpTruck_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningDumpTruck));
/// <summary>
/// Удаление объекта

View File

@ -29,7 +29,6 @@
private void InitializeComponent()
{
groupBoxConfig = new GroupBox();
panel1 = new Panel();
groupBoxColours = new GroupBox();
panelPurple = new Panel();
panelWhite = new Panel();
@ -65,7 +64,6 @@
//
// groupBoxConfig
//
groupBoxConfig.Controls.Add(panel1);
groupBoxConfig.Controls.Add(groupBoxColours);
groupBoxConfig.Controls.Add(checkBoxTent);
groupBoxConfig.Controls.Add(checkBoxAwning);
@ -83,13 +81,6 @@
groupBoxConfig.TabStop = false;
groupBoxConfig.Text = "Параметры";
//
// panel1
//
panel1.Location = new Point(621, 33);
panel1.Name = "panel1";
panel1.Size = new Size(224, 125);
panel1.TabIndex = 4;
//
// groupBoxColours
//
groupBoxColours.Controls.Add(panelPurple);
@ -281,9 +272,9 @@
//
// pictureBoxObject
//
pictureBoxObject.Location = new Point(3, 76);
pictureBoxObject.Location = new Point(3, 61);
pictureBoxObject.Name = "pictureBoxObject";
pictureBoxObject.Size = new Size(226, 114);
pictureBoxObject.Size = new Size(226, 143);
pictureBoxObject.TabIndex = 1;
pictureBoxObject.TabStop = false;
//
@ -295,6 +286,7 @@
buttonAdd.TabIndex = 2;
buttonAdd.Text = "Добавить";
buttonAdd.UseVisualStyleBackColor = true;
buttonAdd.Click += ButtonAdd_Click;
//
// buttonCancel
//
@ -321,28 +313,32 @@
// labelAdditionalColor
//
labelAdditionalColor.BorderStyle = BorderStyle.FixedSingle;
labelAdditionalColor.Location = new Point(122, 14);
labelAdditionalColor.Location = new Point(122, 9);
labelAdditionalColor.Name = "labelAdditionalColor";
labelAdditionalColor.Size = new Size(102, 44);
labelAdditionalColor.TabIndex = 3;
labelAdditionalColor.Text = "Доп. цвет";
labelAdditionalColor.TextAlign = ContentAlignment.MiddleCenter;
labelAdditionalColor.DragDrop += LabelAdditionalColor_DragDrop;
labelAdditionalColor.DragEnter += LabelAdditionalColor_DragEnter;
//
// labelBaseColor
//
labelBaseColor.BorderStyle = BorderStyle.FixedSingle;
labelBaseColor.Location = new Point(6, 14);
labelBaseColor.Location = new Point(14, 9);
labelBaseColor.Name = "labelBaseColor";
labelBaseColor.Size = new Size(102, 44);
labelBaseColor.TabIndex = 2;
labelBaseColor.Text = "Цвет";
labelBaseColor.TextAlign = ContentAlignment.MiddleCenter;
labelBaseColor.DragDrop += LabelBaseColor_DragDrop;
labelBaseColor.DragEnter += LabelBaseColor_DragEnter;
//
// FormTruckConfig
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(857, 266);
ClientSize = new Size(863, 266);
Controls.Add(panelObject);
Controls.Add(buttonCancel);
Controls.Add(buttonAdd);
@ -384,7 +380,6 @@
private PictureBox pictureBoxObject;
private Button buttonAdd;
private Button buttonCancel;
private Panel panel1;
private Panel panelObject;
private Label labelBaseColor;
private Label labelAdditionalColor;

View File

@ -8,6 +8,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectDumpTruck.Drawnings;
using ProjectDumpTruck.Entities;
namespace ProjectDumpTruck;
@ -20,13 +21,19 @@ public partial class FormTruckConfig : Form
/// <summary>
/// Объект - прорисовка грузовика
/// </summary>
private DrawningTruck _truck = null;
private DrawningTruck? _truck;
/// <summary>
/// Событие для передачи объекта
/// </summary>
private event TruckDelegate? TruckDelegate;
/// <summary>
/// Конструктор
/// </summary>
public FormTruckConfig()
{
InitializeComponent();
panelRed.MouseDown += Panel_MouseDown;
@ -39,6 +46,16 @@ public partial class FormTruckConfig : Form
panelGray.MouseDown += Panel_MouseDown;
panelBlack.MouseDown += Panel_MouseDown;
panelWhite.MouseDown += Panel_MouseDown;
buttonCancel.Click += (sender, e) => Close();
}
public void AddEvent(TruckDelegate truckDelegate)
{
TruckDelegate += truckDelegate;
}
/// <summary>
@ -49,7 +66,7 @@ public partial class FormTruckConfig : Form
Bitmap bmp = new(pictureBoxObject.Width, pictureBoxObject.Height);
Graphics gr = Graphics.FromImage(bmp);
_truck?.SetPictureSize(pictureBoxObject.Width, pictureBoxObject.Height);
_truck?.SetPosition(5, 5);
_truck?.SetPosition(30, 30);
_truck?.DrawTransport(gr);
pictureBoxObject.Image = bmp;
}
@ -107,4 +124,61 @@ public partial class FormTruckConfig : Form
{
(sender as Control)?.DoDragDrop((sender as Control)?.BackColor!, DragDropEffects.Move | DragDropEffects.Copy);
}
/// <summary>
/// Передача объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAdd_Click(object sender, EventArgs e)
{
if (_truck != null)
{
TruckDelegate?.Invoke(_truck);
Close();
}
}
private void LabelBaseColor_DragDrop(object sender, DragEventArgs e)
{
if (_truck == null) return;
_truck.EntityTruck?.SetBodyColor((Color)e.Data.GetData(typeof(Color)));
DrawObject();
}
private void LabelBaseColor_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Color)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void LabelAdditionalColor_DragDrop(object sender, DragEventArgs e)
{
if (_truck?.EntityTruck is EntityDumpTruck _dumpTruck)
{
_dumpTruck.SetAdditionalColor((Color)e.Data.GetData(typeof(Color)));
}
DrawObject();
}
private void LabelAdditionalColor_DragEnter(object sender, DragEventArgs e)
{
if (_truck is DrawningDumpTruck)
{
if (e.Data.GetDataPresent(typeof(Color)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
}

View File

@ -0,0 +1,15 @@
using ProjectDumpTruck.Drawnings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectDumpTruck;
/// <summary>
/// Делегат переадчи объекта класса-прорисовки
/// </summary>
/// <param name="truck"></param>
public delegate void TruckDelegate(DrawningTruck truck);