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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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