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 System.Windows.Forms.VisualStyles;
using WarmlyShip.DrawningObjects;
namespace WarmlyShip
{
///
/// Форма создания объекта
///
public partial class FormShipConfig : Form
{
///
/// Переменная-выбранный корабль
///
DrawningShip? _ship = null;
///
/// Событие
///
private event Action? EventAddShip;
///
/// Конструктор
///
public FormShipConfig()
{
InitializeComponent();
panelBlack.MouseDown += PanelColor_MouseDown;
panelPurple.MouseDown += PanelColor_MouseDown;
panelGray.MouseDown += PanelColor_MouseDown;
panelGreen.MouseDown += PanelColor_MouseDown;
panelRed.MouseDown += PanelColor_MouseDown;
panelWhite.MouseDown += PanelColor_MouseDown;
panelYellow.MouseDown += PanelColor_MouseDown;
panelBlue.MouseDown += PanelColor_MouseDown;
labelSimpleObject.MouseDown += LabelObject_MouseDown;
labelModifiedObject.MouseDown += LabelObject_MouseDown;
buttonCancel.Click += (s, e) => Close();
}
///
/// Отрисовать корабль
///
private void DrawShip()
{
Bitmap bmp = new(pictureBoxObject.Width, pictureBoxObject.Height);
Graphics gr = Graphics.FromImage(bmp);
_ship?.SetPosition(5, 5);
_ship?.DrawTransport(gr);
pictureBoxObject.Image = bmp;
}
///
/// Добавление события
///
/// Привязанный метод
public void AddEvent(Action ev)
{
if (EventAddShip == null)
{
EventAddShip = ev;
}
else
{
EventAddShip += ev;
}
}
///
/// Передаем информацию при нажатии на 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;
}
}
///
/// Действия при приеме перетаскиваемой информации
///
///
///
private void PanelObject_DragDrop(object sender, DragEventArgs e)
{
switch (e.Data?.GetData(DataFormats.Text).ToString())
{
case "labelSimpleObject":
_ship = new DrawningShip((int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value,
Color.White, pictureBoxObject.Width, pictureBoxObject.Height);
break;
case "labelModifiedObject":
_ship = new DrawningWarmlyShip((int)numericUpDownSpeed.Value, (int)numericUpDownWeight.Value,
Color.White, Color.Black, checkBoxPipe.Checked, checkBoxFuelCompartment.Checked,
pictureBoxObject.Width, pictureBoxObject.Height);
break;
}
labelColor.BackColor = Color.Empty;
labelExtraColor.BackColor = Color.Empty;
DrawShip();
}
///
/// Отправляем цвет с панели
///
///
///
private void PanelColor_MouseDown(object sender, MouseEventArgs e)
{
(sender as Control)?.DoDragDrop((sender as Control)?.BackColor, DragDropEffects.Move | DragDropEffects.Copy);
}
///
/// Принимаем основной цвет
///
///
///
private void LabelColor_DragDrop(object sender, DragEventArgs e)
{
if (_ship == null)
{
return;
}
labelColor.BackColor = (Color)e.Data.GetData(typeof(Color));
_ship.SetColor(labelColor.BackColor);
DrawShip();
}
///
/// Проверка получаемой информации (ее типа на соответствие требуемому)
///
///
///
private void LabelColor_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Color)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
///
/// Принимаем дополнительный цвет
///
///
///
private void LabelDopColor_DragDrop(object sender, DragEventArgs e)
{
if ((_ship == null) || (_ship is DrawningWarmlyShip == false))
{
return;
}
labelExtraColor.BackColor = (Color)e.Data.GetData(typeof(Color));
((DrawningWarmlyShip)_ship).SetAddColor(labelExtraColor.BackColor);
DrawShip();
}
///
/// Добавление корабля
///
///
///
private void ButtonAdd_Click(object sender, EventArgs e)
{
EventAddShip?.Invoke(_ship);
Close();
}
}
}