Полностью сделанная 4 лабораторная работа

This commit is contained in:
d.agil 2022-11-02 10:30:12 +04:00
parent 5181a68461
commit 5497eabfee
5 changed files with 272 additions and 122 deletions

View File

@ -29,6 +29,12 @@
private void InitializeComponent() private void InitializeComponent()
{ {
this.groupBox1 = new System.Windows.Forms.GroupBox(); this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBoxMaps = new System.Windows.Forms.GroupBox();
this.listBoxMaps = new System.Windows.Forms.ListBox();
this.buttonDeleteMap = new System.Windows.Forms.Button();
this.buttonAddMap = new System.Windows.Forms.Button();
this.textBoxNewMapName = new System.Windows.Forms.TextBox();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.buttonRight = new System.Windows.Forms.Button(); this.buttonRight = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button(); this.buttonDown = new System.Windows.Forms.Button();
this.buttonLeft = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button();
@ -38,14 +44,15 @@
this.buttonRemoveAirBomber = new System.Windows.Forms.Button(); this.buttonRemoveAirBomber = new System.Windows.Forms.Button();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonAddAirBomber = new System.Windows.Forms.Button(); this.buttonAddAirBomber = new System.Windows.Forms.Button();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.pictureBox = new System.Windows.Forms.PictureBox(); this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBox1.SuspendLayout(); this.groupBox1.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// groupBox1 // groupBox1
// //
this.groupBox1.Controls.Add(this.groupBoxMaps);
this.groupBox1.Controls.Add(this.buttonRight); this.groupBox1.Controls.Add(this.buttonRight);
this.groupBox1.Controls.Add(this.buttonDown); this.groupBox1.Controls.Add(this.buttonDown);
this.groupBox1.Controls.Add(this.buttonLeft); this.groupBox1.Controls.Add(this.buttonLeft);
@ -55,21 +62,83 @@
this.groupBox1.Controls.Add(this.buttonRemoveAirBomber); this.groupBox1.Controls.Add(this.buttonRemoveAirBomber);
this.groupBox1.Controls.Add(this.maskedTextBoxPosition); this.groupBox1.Controls.Add(this.maskedTextBoxPosition);
this.groupBox1.Controls.Add(this.buttonAddAirBomber); this.groupBox1.Controls.Add(this.buttonAddAirBomber);
this.groupBox1.Controls.Add(this.comboBoxSelectorMap);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right; this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(600, 0); this.groupBox1.Location = new System.Drawing.Point(1034, 0);
this.groupBox1.Name = "groupBox1"; this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(200, 450); this.groupBox1.Size = new System.Drawing.Size(200, 764);
this.groupBox1.TabIndex = 0; this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false; this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты"; this.groupBox1.Text = "Инструменты";
// //
// groupBoxMaps
//
this.groupBoxMaps.Controls.Add(this.listBoxMaps);
this.groupBoxMaps.Controls.Add(this.buttonDeleteMap);
this.groupBoxMaps.Controls.Add(this.buttonAddMap);
this.groupBoxMaps.Controls.Add(this.textBoxNewMapName);
this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxMaps.Location = new System.Drawing.Point(22, 32);
this.groupBoxMaps.Name = "groupBoxMaps";
this.groupBoxMaps.Size = new System.Drawing.Size(150, 326);
this.groupBoxMaps.TabIndex = 11;
this.groupBoxMaps.TabStop = false;
this.groupBoxMaps.Text = "Карты";
//
// listBoxMaps
//
this.listBoxMaps.FormattingEnabled = true;
this.listBoxMaps.ItemHeight = 15;
this.listBoxMaps.Location = new System.Drawing.Point(0, 155);
this.listBoxMaps.Name = "listBoxMaps";
this.listBoxMaps.Size = new System.Drawing.Size(150, 94);
this.listBoxMaps.TabIndex = 5;
this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged);
//
// buttonDeleteMap
//
this.buttonDeleteMap.Location = new System.Drawing.Point(0, 273);
this.buttonDeleteMap.Name = "buttonDeleteMap";
this.buttonDeleteMap.Size = new System.Drawing.Size(150, 34);
this.buttonDeleteMap.TabIndex = 4;
this.buttonDeleteMap.Text = "Удалить карту";
this.buttonDeleteMap.UseVisualStyleBackColor = true;
this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click);
//
// buttonAddMap
//
this.buttonAddMap.Location = new System.Drawing.Point(0, 102);
this.buttonAddMap.Name = "buttonAddMap";
this.buttonAddMap.Size = new System.Drawing.Size(150, 34);
this.buttonAddMap.TabIndex = 3;
this.buttonAddMap.Text = "Добавить карту";
this.buttonAddMap.UseVisualStyleBackColor = true;
this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click);
//
// textBoxNewMapName
//
this.textBoxNewMapName.Location = new System.Drawing.Point(0, 22);
this.textBoxNewMapName.Name = "textBoxNewMapName";
this.textBoxNewMapName.Size = new System.Drawing.Size(150, 23);
this.textBoxNewMapName.TabIndex = 2;
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта",
"Вторая карта",
"Третья карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(0, 60);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(150, 23);
this.comboBoxSelectorMap.TabIndex = 1;
//
// buttonRight // buttonRight
// //
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::AirBomber.Properties.Resources.ArrowRight; this.buttonRight.BackgroundImage = global::AirBomber.Properties.Resources.ArrowRight;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(116, 404); this.buttonRight.Location = new System.Drawing.Point(116, 718);
this.buttonRight.Name = "buttonRight"; this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30); this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 10; this.buttonRight.TabIndex = 10;
@ -81,7 +150,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::AirBomber.Properties.Resources.ArrowDown; this.buttonDown.BackgroundImage = global::AirBomber.Properties.Resources.ArrowDown;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(80, 404); this.buttonDown.Location = new System.Drawing.Point(80, 718);
this.buttonDown.Name = "buttonDown"; this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30); this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 9; this.buttonDown.TabIndex = 9;
@ -93,7 +162,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::AirBomber.Properties.Resources.ArrowLeft; this.buttonLeft.BackgroundImage = global::AirBomber.Properties.Resources.ArrowLeft;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(44, 404); this.buttonLeft.Location = new System.Drawing.Point(44, 718);
this.buttonLeft.Name = "buttonLeft"; this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30); this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 8; this.buttonLeft.TabIndex = 8;
@ -105,7 +174,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::AirBomber.Properties.Resources.ArrowUp; this.buttonUp.BackgroundImage = global::AirBomber.Properties.Resources.ArrowUp;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(80, 368); this.buttonUp.Location = new System.Drawing.Point(80, 682);
this.buttonUp.Name = "buttonUp"; this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30); this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 7; this.buttonUp.TabIndex = 7;
@ -114,7 +183,7 @@
// //
// buttonShowOnMap // buttonShowOnMap
// //
this.buttonShowOnMap.Location = new System.Drawing.Point(22, 319); this.buttonShowOnMap.Location = new System.Drawing.Point(22, 602);
this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(150, 33); this.buttonShowOnMap.Size = new System.Drawing.Size(150, 33);
this.buttonShowOnMap.TabIndex = 5; this.buttonShowOnMap.TabIndex = 5;
@ -124,7 +193,7 @@
// //
// buttonShowStorage // buttonShowStorage
// //
this.buttonShowStorage.Location = new System.Drawing.Point(22, 258); this.buttonShowStorage.Location = new System.Drawing.Point(22, 560);
this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(150, 36); this.buttonShowStorage.Size = new System.Drawing.Size(150, 36);
this.buttonShowStorage.TabIndex = 4; this.buttonShowStorage.TabIndex = 4;
@ -134,7 +203,7 @@
// //
// buttonRemoveAirBomber // buttonRemoveAirBomber
// //
this.buttonRemoveAirBomber.Location = new System.Drawing.Point(22, 195); this.buttonRemoveAirBomber.Location = new System.Drawing.Point(22, 504);
this.buttonRemoveAirBomber.Name = "buttonRemoveAirBomber"; this.buttonRemoveAirBomber.Name = "buttonRemoveAirBomber";
this.buttonRemoveAirBomber.Size = new System.Drawing.Size(150, 36); this.buttonRemoveAirBomber.Size = new System.Drawing.Size(150, 36);
this.buttonRemoveAirBomber.TabIndex = 3; this.buttonRemoveAirBomber.TabIndex = 3;
@ -144,7 +213,7 @@
// //
// maskedTextBoxPosition // maskedTextBoxPosition
// //
this.maskedTextBoxPosition.Location = new System.Drawing.Point(22, 166); this.maskedTextBoxPosition.Location = new System.Drawing.Point(22, 475);
this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(150, 23); this.maskedTextBoxPosition.Size = new System.Drawing.Size(150, 23);
@ -152,7 +221,7 @@
// //
// buttonAddAirBomber // buttonAddAirBomber
// //
this.buttonAddAirBomber.Location = new System.Drawing.Point(22, 94); this.buttonAddAirBomber.Location = new System.Drawing.Point(22, 422);
this.buttonAddAirBomber.Name = "buttonAddAirBomber"; this.buttonAddAirBomber.Name = "buttonAddAirBomber";
this.buttonAddAirBomber.Size = new System.Drawing.Size(150, 35); this.buttonAddAirBomber.Size = new System.Drawing.Size(150, 35);
this.buttonAddAirBomber.TabIndex = 1; this.buttonAddAirBomber.TabIndex = 1;
@ -160,25 +229,12 @@
this.buttonAddAirBomber.UseVisualStyleBackColor = true; this.buttonAddAirBomber.UseVisualStyleBackColor = true;
this.buttonAddAirBomber.Click += new System.EventHandler(this.ButtonAddAirBomber_Click); this.buttonAddAirBomber.Click += new System.EventHandler(this.ButtonAddAirBomber_Click);
// //
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта",
"Вторая карта",
"Третья карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(22, 44);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(150, 23);
this.comboBoxSelectorMap.TabIndex = 0;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//
// pictureBox // pictureBox
// //
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0); this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Name = "pictureBox"; this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(600, 450); this.pictureBox.Size = new System.Drawing.Size(1034, 764);
this.pictureBox.TabIndex = 1; this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false; this.pictureBox.TabStop = false;
// //
@ -186,13 +242,15 @@
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450); this.ClientSize = new System.Drawing.Size(1234, 764);
this.Controls.Add(this.pictureBox); this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1); this.Controls.Add(this.groupBox1);
this.Name = "FormMapWithSetAirBomber"; this.Name = "FormMapWithSetAirBomber";
this.Text = "FormMapWithSetAirBomber"; this.Text = "Карта с набором объектов";
this.groupBox1.ResumeLayout(false); this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout(); this.groupBox1.PerformLayout();
this.groupBoxMaps.ResumeLayout(false);
this.groupBoxMaps.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
@ -203,7 +261,6 @@
private GroupBox groupBox1; private GroupBox groupBox1;
private MaskedTextBox maskedTextBoxPosition; private MaskedTextBox maskedTextBoxPosition;
private Button buttonAddAirBomber; private Button buttonAddAirBomber;
private ComboBox comboBoxSelectorMap;
private PictureBox pictureBox; private PictureBox pictureBox;
private Button buttonShowOnMap; private Button buttonShowOnMap;
private Button buttonShowStorage; private Button buttonShowStorage;
@ -212,5 +269,11 @@
private Button buttonDown; private Button buttonDown;
private Button buttonLeft; private Button buttonLeft;
private Button buttonUp; private Button buttonUp;
private GroupBox groupBoxMaps;
private TextBox textBoxNewMapName;
private ComboBox comboBoxSelectorMap;
private ListBox listBoxMaps;
private Button buttonDeleteMap;
private Button buttonAddMap;
} }
} }

View File

@ -14,14 +14,49 @@ namespace AirBomber
{ {
private MapWithSetAirBomberGeneric<DrawningObjectBomber, AbstractMap> _mapAirBomberCollectionGeneric; private MapWithSetAirBomberGeneric<DrawningObjectBomber, AbstractMap> _mapAirBomberCollectionGeneric;
private readonly Dictionary<string, AbstractMap> _mapsDict = new()
{
{ "Простая карта", new SimpleMap() },
{ "Вторая карта", new SecondMap() },
{ "Третья карта", new ThirdMap() }
};
private readonly MapsCollection _mapsCollection;
public FormMapWithSetAirBomber() public FormMapWithSetAirBomber()
{ {
InitializeComponent(); InitializeComponent();
} _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
comboBoxSelectorMap.Items.Clear();
foreach (var elem in _mapsDict)
{
comboBoxSelectorMap.Items.Add(elem.Key);
}
}
private void ReloadMaps()
{
int index = listBoxMaps.SelectedIndex;
listBoxMaps.Items.Clear();
for (int i = 0; i < _mapsCollection.Keys.Count; i++)
{
listBoxMaps.Items.Add(_mapsCollection.Keys[i]);
}
if (listBoxMaps.Items.Count > 0 && (index == -1 || index >=
listBoxMaps.Items.Count))
{
listBoxMaps.SelectedIndex = 0;
}
else if (listBoxMaps.Items.Count > 0 && index > -1 && index <
listBoxMaps.Items.Count)
{
listBoxMaps.SelectedIndex = index;
}
}
private void ButtonAddAirBomber_Click(object sender, EventArgs e) private void ButtonAddAirBomber_Click(object sender, EventArgs e)
{ {
if (_mapAirBomberCollectionGeneric == null) if (listBoxMaps.SelectedIndex == -1)
{ {
return; return;
} }
@ -29,10 +64,10 @@ namespace AirBomber
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
DrawningObjectBomber airBomber = new(form.SelectedAirBomber); DrawningObjectBomber airBomber = new(form.SelectedAirBomber);
if (_mapAirBomberCollectionGeneric + airBomber > -1) if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + airBomber > -1)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapAirBomberCollectionGeneric.ShowSet(); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
} }
else else
{ {
@ -43,6 +78,10 @@ namespace AirBomber
private void ButtonRemoveAirBomber_Click(object sender, EventArgs e) private void ButtonRemoveAirBomber_Click(object sender, EventArgs e)
{ {
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{ {
return; return;
@ -52,10 +91,10 @@ namespace AirBomber
return; return;
} }
int pos = Convert.ToInt32(maskedTextBoxPosition.Text); int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapAirBomberCollectionGeneric - pos != null) if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBox.Image = _mapAirBomberCollectionGeneric.ShowSet(); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
} }
else else
{ {
@ -65,50 +104,25 @@ namespace AirBomber
private void ButtonShowStorage_Click(object sender, EventArgs e) private void ButtonShowStorage_Click(object sender, EventArgs e)
{ {
if (_mapAirBomberCollectionGeneric == null) if (listBoxMaps.SelectedIndex == -1)
{ {
return; return;
} }
pictureBox.Image = _mapAirBomberCollectionGeneric.ShowSet(); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
} }
private void ButtonShowOnMap_Click(object sender, EventArgs e) private void ButtonShowOnMap_Click(object sender, EventArgs e)
{ {
if (_mapAirBomberCollectionGeneric == null) if (listBoxMaps.SelectedIndex == -1)
{ {
return; return;
} }
pictureBox.Image = _mapAirBomberCollectionGeneric.ShowOnMap(); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap();
}
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{
AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
{
case "Простая карта":
map = new SimpleMap();
break;
case "Вторая карта":
map = new SecondMap();
break;
case "Третья карта":
map = new ThirdMap();
break;
}
if (map != null)
{
_mapAirBomberCollectionGeneric = new MapWithSetAirBomberGeneric<DrawningObjectBomber, AbstractMap>(pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapAirBomberCollectionGeneric = null;
}
} }
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
{ {
if (_mapAirBomberCollectionGeneric == null) if (listBoxMaps.SelectedIndex == -1)
{ {
return; return;
} }
@ -129,7 +143,42 @@ namespace AirBomber
dir = Direction.Right; dir = Direction.Right;
break; break;
} }
pictureBox.Image = _mapAirBomberCollectionGeneric.MoveObject(dir); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
}
private void ButtonAddMap_Click(object sender, EventArgs e)
{
if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text))
{
MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]);
ReloadMaps();
}
private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonDeleteMap_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
ReloadMaps();
}
} }
} }
} }

View File

@ -49,20 +49,16 @@ namespace AirBomber
public Bitmap ShowOnMap() public Bitmap ShowOnMap()
{ {
Shaking(); Shaking();
for (int i = 0; i < _setAirBomber.Count; i++) foreach (var airBomber in _setAirBomber.GetAirBomber())
{ {
var airBomber = _setAirBomber.Get(i); return _map.CreateMap(_pictureWidth, _pictureHeight, airBomber);
if (airBomber != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, airBomber);
}
} }
return new(_pictureWidth, _pictureHeight); return new(_pictureWidth, _pictureHeight);
} }
public Bitmap MoveObject(Direction direction) public Bitmap MoveObject(Direction direction)
{ {
if ( _map != null) if (_map != null)
{ {
return _map.MoveObject(direction); return _map.MoveObject(direction);
} }
@ -74,11 +70,11 @@ namespace AirBomber
int j = _setAirBomber.Count - 1; int j = _setAirBomber.Count - 1;
for (int i = 0; i < _setAirBomber.Count; i++) for (int i = 0; i < _setAirBomber.Count; i++)
{ {
if (_setAirBomber.Get(i) == null) if (_setAirBomber[i] == null)
{ {
for (; j > i; j--) for (; j > i; j--)
{ {
var airBomber = _setAirBomber.Get(j); var airBomber = _setAirBomber[j];
if (airBomber != null) if (airBomber != null)
{ {
_setAirBomber.Insert(airBomber, i); _setAirBomber.Insert(airBomber, i);
@ -112,13 +108,15 @@ namespace AirBomber
private void DrawAirBomber(Graphics g) private void DrawAirBomber(Graphics g)
{ {
for (int i = 0; i < _setAirBomber.Count; ++i) int i = 0;
foreach (var airBomber in _setAirBomber.GetAirBomber())
{ {
if (_setAirBomber.Get(i) != null) if (airBomber != null)
{ {
_setAirBomber.Get(i).SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight, _pictureWidth, _pictureHeight); _setAirBomber[i].SetObject((_pictureWidth / _placeSizeWidth - (i % (_pictureWidth / _placeSizeWidth)) - 1) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight, _pictureWidth, _pictureHeight);
_setAirBomber.Get(i).DrawningObject(g); airBomber.DrawningObject(g);
} }
i++;
} }
} }
} }

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AirBomber
{
internal class MapsCollection
{
readonly Dictionary<string, MapWithSetAirBomberGeneric<DrawningObjectBomber, AbstractMap>> _mapStorages;
public List<string> Keys => _mapStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string, MapWithSetAirBomberGeneric<DrawningObjectBomber, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddMap(string name, AbstractMap map)
{
if (_mapStorages.ContainsKey(name))
return;
_mapStorages.Add(name, new MapWithSetAirBomberGeneric<DrawningObjectBomber, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
public void DelMap(string name)
{
if (!_mapStorages.ContainsKey(name))
return;
_mapStorages.Remove(name);
}
public MapWithSetAirBomberGeneric<DrawningObjectBomber, AbstractMap> this[string ind]
{
get
{
if (_mapStorages.ContainsKey(ind)) return _mapStorages[ind];
return null;
}
}
}
}

View File

@ -9,13 +9,16 @@ namespace AirBomber
internal class SetAirBomberGeneric<T> internal class SetAirBomberGeneric<T>
where T : class where T : class
{ {
private readonly T[] _places; private readonly List<T> _places;
public int Count => _places.Length; public int Count => _places.Count;
private readonly int _maxCount;
public SetAirBomberGeneric(int count) public SetAirBomberGeneric(int count)
{ {
_places = new T[count]; _maxCount = count;
_places = new List<T>();
} }
public int Insert(T airBomber) public int Insert(T airBomber)
@ -25,48 +28,17 @@ namespace AirBomber
public int Insert(T airBomber, int position) public int Insert(T airBomber, int position)
{ {
if (position < 0 || position >= Count) if (position < 0 || position > _maxCount)
{
return -1;
}
int emptyPos = -1;
if (_places[position] == null)
{
_places[position] = airBomber;
return position;
}
else
{
for (int i = position + 1; i < Count; ++i)
{
if (_places[i] == null)
{
emptyPos = i;
break;
}
}
if (emptyPos != -1)
{
for (int i = emptyPos; i > position; --i)
{
_places[i] = _places[i - 1];
}
}
}
if (emptyPos != -1)
{
_places[position] = airBomber;
return position;
}
else
{ {
return -1; return -1;
} }
_places.Insert(position, airBomber);
return position;
} }
public T Remove(int position) public T Remove(int position)
{ {
if (position < 0 || position >= Count) if (position < 0 || position > _maxCount)
{ {
return null; return null;
} }
@ -75,13 +47,35 @@ namespace AirBomber
return elem; return elem;
} }
public T Get(int position) public T this[int position]
{ {
if (position < 0 || position >= Count) get
{ {
return null; if (position < 0 || position > _maxCount)
{
return null;
}
return _places[position];
}
set
{
Insert(value, position);
}
}
public IEnumerable<T> GetAirBomber()
{
foreach (var airBomber in _places)
{
if (airBomber != null)
{
yield return airBomber;
}
else
{
yield break;
}
} }
return _places[position];
} }
} }
} }