2022-11-09 22:12:18 +04:00

135 lines
4.7 KiB
C#

namespace Battleship
{
public partial class FormBattleshipConfig : Form
{
DrawningBattleship _battleship = null;
private event Action<DrawningBattleship> EventAddBattleship;
public FormBattleshipConfig()
{
InitializeComponent();
PanelBlue.MouseDown += PanelColor_MouseDown;
PanelCyan.MouseDown += PanelColor_MouseDown;
PanelFuchsia.MouseDown += PanelColor_MouseDown;
PanelLime.MouseDown += PanelColor_MouseDown;
PanelOrange.MouseDown += PanelColor_MouseDown;
PanelRed.MouseDown += PanelColor_MouseDown;
PanelSilver.MouseDown += PanelColor_MouseDown;
PanelYellow.MouseDown += PanelColor_MouseDown;
ButtonCancel.Click += (sender, e) => Close();
}
private void DrawBattleship()
{
Bitmap bmp = new(PictureBoxBattleship.Width, PictureBoxBattleship.Height);
Graphics gr = Graphics.FromImage(bmp);
_battleship?.SetPosition(5, 5, PictureBoxBattleship.Width, PictureBoxBattleship.Height);
_battleship?.DrawTransport(gr);
PictureBoxBattleship.Image = bmp;
}
public void AddEvent(Action<DrawningBattleship> ev)
{
if (EventAddBattleship == null)
{
EventAddBattleship = ev;
}
else
{
EventAddBattleship += ev;
}
}
private void LabelBattleship_MouseDown(object sender, MouseEventArgs e)
{
(sender as Label).DoDragDrop((sender as Label).Name, DragDropEffects.Move | DragDropEffects.Copy);
}
private void PanelBattleship_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void PanelBattleship_DragDrop(object sender, DragEventArgs e)
{
switch (e.Data.GetData(DataFormats.Text).ToString())
{
case "LabelBasicBattleship":
_battleship = new DrawningBattleship((int)NumericUpDownSpeed.Value, (int)NumericUpDownWeight.Value, Color.White);
break;
case "LabelGunBattleship":
_battleship = new DrawningGunBattleship((int)NumericUpDownSpeed.Value, (int)NumericUpDownWeight.Value, Color.White, Color.Black,
CheckBoxCompartmentRocket.Checked, CheckBoxGunTower.Checked, CheckBoxSternFence.Checked);
break;
}
DrawBattleship();
}
private void PanelColor_MouseDown(object sender, MouseEventArgs e)
{
(sender as Control).DoDragDrop((sender as Control).BackColor, DragDropEffects.Move | DragDropEffects.Copy);
}
private void LabelBodyColor_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Color)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void LabelModifColor_DragEnter(object sender, DragEventArgs e)
{
if (_battleship is DrawningGunBattleship)
{
if (e.Data.GetDataPresent(typeof(Color)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
private void LabelBodyColor_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Color)) && _battleship != null)
{
_battleship.Battleship.ChangeColor((Color)e.Data.GetData(typeof(Color)));
DrawBattleship();
}
}
private void LabelModifColor_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Color)) && _battleship != null)
{
if (_battleship is DrawningGunBattleship _gunBattleship)
{
if (_gunBattleship.Battleship is EntityGunBattleship _entityGunBattleship)
{
_entityGunBattleship.ChangeDopColor((Color)e.Data.GetData(typeof(Color)));
DrawBattleship();
}
}
}
}
private void ButtomAdd_Click(object sender, EventArgs e)
{
EventAddBattleship?.Invoke(_battleship);
Close();
}
}
}