Event work

This commit is contained in:
Nap 2022-11-09 22:10:29 +04:00
parent 19745ae5c0
commit 0a991a25e4
8 changed files with 67 additions and 52 deletions

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Battleship
{
/// <summary>
/// Делегат для передачи объекта-корабля
/// </summary>
/// <param name="battleship"></param>
public delegate void BattleshipDelegate(DrawningBattleship battleship);
}

View File

@ -30,6 +30,12 @@ namespace Battleship
return;
}
_startPosX += 10;
_startPosY += 5;
base.DrawTransport(g);
_startPosX -= 10;
_startPosY -= 5;
Pen pen = new(Color.Black);
Brush dopBrush = new SolidBrush(gunBattleship.DopColor);
@ -40,12 +46,6 @@ namespace Battleship
g.DrawRectangle(pen, _startPosX + 85, _startPosY + 10, 15, 20);
}
_startPosX += 10;
_startPosY += 5;
base.DrawTransport(g);
_startPosX -= 10;
_startPosY -= 5;
if (gunBattleship.GunTower)
{
g.FillEllipse(dopBrush, _startPosX + 105, _startPosY + 12, 17, 17);

View File

@ -22,7 +22,7 @@ namespace Battleship
/// <summary>
/// Цвет кузова
/// </summary>
public Color BodyColor { get; set; }
public Color BodyColor { get; private set; }
/// <summary>
/// Шаг перемещения боевого корабля
/// </summary>
@ -41,5 +41,10 @@ namespace Battleship
Weight = weight <= 0 ? rnd.Next(40, 70) : weight;
BodyColor = bodyColor;
}
public void ChangeColor(Color color)
{
BodyColor = color;
}
}
}

View File

@ -45,5 +45,10 @@ namespace Battleship
GunTower = gunTower;
SternFence = sternFence;
}
public void ChangeDopColor(Color color)
{
DopColor = color;
}
}
}

View File

@ -188,7 +188,7 @@
this.CheckBoxCompartmentRocket.AutoSize = true;
this.CheckBoxCompartmentRocket.Location = new System.Drawing.Point(42, 205);
this.CheckBoxCompartmentRocket.Name = "CheckBoxCompartmentRocket";
this.CheckBoxCompartmentRocket.Size = new System.Drawing.Size(200, 19);
this.CheckBoxCompartmentRocket.Size = new System.Drawing.Size(217, 19);
this.CheckBoxCompartmentRocket.TabIndex = 6;
this.CheckBoxCompartmentRocket.Text = "Признак наличия отсека для ракет";
this.CheckBoxCompartmentRocket.UseVisualStyleBackColor = true;
@ -198,7 +198,7 @@
this.CheckBoxGunTower.AutoSize = true;
this.CheckBoxGunTower.Location = new System.Drawing.Point(42, 168);
this.CheckBoxGunTower.Name = "CheckBoxGunTower";
this.CheckBoxGunTower.Size = new System.Drawing.Size(173, 19);
this.CheckBoxGunTower.Size = new System.Drawing.Size(228, 19);
this.CheckBoxGunTower.TabIndex = 5;
this.CheckBoxGunTower.Text = "Признак наличия орудийной башни";
this.CheckBoxGunTower.UseVisualStyleBackColor = true;
@ -208,7 +208,7 @@
this.CheckBoxSternFence.AutoSize = true;
this.CheckBoxSternFence.Location = new System.Drawing.Point(42, 129);
this.CheckBoxSternFence.Name = "CheckBoxSternFence";
this.CheckBoxSternFence.Size = new System.Drawing.Size(256, 19);
this.CheckBoxSternFence.Size = new System.Drawing.Size(218, 19);
this.CheckBoxSternFence.TabIndex = 4;
this.CheckBoxSternFence.Text = "Признак наличия забора на корме";
this.CheckBoxSternFence.UseVisualStyleBackColor = true;
@ -220,7 +220,7 @@
this.NumericUpDownWeight.Size = new System.Drawing.Size(120, 23);
this.NumericUpDownWeight.TabIndex = 3;
this.NumericUpDownWeight.Value = new decimal(new int[] {
100,
1,
0,
0,
0});
@ -232,7 +232,7 @@
this.NumericUpDownSpeed.Size = new System.Drawing.Size(120, 23);
this.NumericUpDownSpeed.TabIndex = 2;
this.NumericUpDownSpeed.Value = new decimal(new int[] {
100,
1,
0,
0,
0});

View File

@ -1,14 +1,4 @@
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 Battleship
namespace Battleship
{
public partial class FormBattleshipConfig : Form
{
@ -74,7 +64,8 @@ namespace Battleship
_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, CheckBoxSternFence.Checked, CheckBoxGunTower.Checked, CheckBoxCompartmentRocket.Checked);
_battleship = new DrawningGunBattleship((int)NumericUpDownSpeed.Value, (int)NumericUpDownWeight.Value, Color.White, Color.Black,
CheckBoxCompartmentRocket.Checked, CheckBoxGunTower.Checked, CheckBoxSternFence.Checked);
break;
}
DrawBattleship();
@ -84,7 +75,6 @@ namespace Battleship
{
(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)))
@ -113,27 +103,25 @@ namespace Battleship
private void LabelBodyColor_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Color)))
if (e.Data.GetDataPresent(typeof(Color)) && _battleship != null)
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
_battleship.Battleship.ChangeColor((Color)e.Data.GetData(typeof(Color)));
DrawBattleship();
}
}
private void LabelModifColor_DragDrop(object sender, DragEventArgs e)
{
var color = e.Data.GetData(typeof(Color));
if (color != null)
if (e.Data.GetDataPresent(typeof(Color)) && _battleship != null)
{
(sender as Label).BackColor = (Color)color;
}
if (_battleship != null)
{
_battleship.Battleship.BodyColor = (Color)color;
DrawBattleship();
if (_battleship is DrawningGunBattleship _gunBattleship)
{
if (_gunBattleship.Battleship is EntityGunBattleship _entityGunBattleship)
{
_entityGunBattleship.ChangeDopColor((Color)e.Data.GetData(typeof(Color)));
DrawBattleship();
}
}
}
}

View File

@ -252,7 +252,7 @@
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Name = "FormMapWithSetBattleship";
this.Text = "FormMapWithSetBattleship";
this.Text = "Карта с набором объектов";
this.groupBoxTools.ResumeLayout(false);
this.groupBoxTools.PerformLayout();
this.groupBoxMaps.ResumeLayout(false);

View File

@ -50,24 +50,27 @@ namespace Battleship
}
private void ButtonAddBattleship_Click(object sender, EventArgs e)
{
var formBattleshipConfig = new FormBattleshipConfig();
formBattleshipConfig.AddEvent(AddBattleshipOnMap);
formBattleshipConfig.Show();
}
private void AddBattleshipOnMap(DrawningBattleship battleship)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
FormBattleship form = new();
if (form.ShowDialog() == DialogResult.OK)
DrawningObjectBattleship boat = new(battleship);
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + boat >= 0)
{
DrawningObjectBattleship battleship = new(form.SelectedBattleship);
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + battleship != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}