1 Commits

5 changed files with 114 additions and 313 deletions

View File

@@ -29,12 +29,6 @@
private void InitializeComponent() private void InitializeComponent()
{ {
this.groupBoxTools = new System.Windows.Forms.GroupBox(); this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.groupBoxMaps = new System.Windows.Forms.GroupBox();
this.buttonAddMap = new System.Windows.Forms.Button();
this.buttonDeleteMap = new System.Windows.Forms.Button();
this.listBoxMaps = new System.Windows.Forms.ListBox();
this.textBoxNewMapName = new System.Windows.Forms.TextBox();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.buttonShowOnMap = new System.Windows.Forms.Button(); this.buttonShowOnMap = new System.Windows.Forms.Button();
this.buttonShowStorage = new System.Windows.Forms.Button(); this.buttonShowStorage = new System.Windows.Forms.Button();
this.buttonRemoveWarship = new System.Windows.Forms.Button(); this.buttonRemoveWarship = new System.Windows.Forms.Button();
@@ -44,15 +38,14 @@
this.buttonLeft = new System.Windows.Forms.Button(); this.buttonLeft = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button(); this.buttonUp = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button(); this.buttonDown = 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.groupBoxTools.SuspendLayout(); this.groupBoxTools.SuspendLayout();
this.groupBoxMaps.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// groupBoxTools // groupBoxTools
// //
this.groupBoxTools.Controls.Add(this.groupBoxMaps);
this.groupBoxTools.Controls.Add(this.buttonShowOnMap); this.groupBoxTools.Controls.Add(this.buttonShowOnMap);
this.groupBoxTools.Controls.Add(this.buttonShowStorage); this.groupBoxTools.Controls.Add(this.buttonShowStorage);
this.groupBoxTools.Controls.Add(this.buttonRemoveWarship); this.groupBoxTools.Controls.Add(this.buttonRemoveWarship);
@@ -62,82 +55,20 @@
this.groupBoxTools.Controls.Add(this.buttonLeft); this.groupBoxTools.Controls.Add(this.buttonLeft);
this.groupBoxTools.Controls.Add(this.buttonUp); this.groupBoxTools.Controls.Add(this.buttonUp);
this.groupBoxTools.Controls.Add(this.buttonDown); this.groupBoxTools.Controls.Add(this.buttonDown);
this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(754, 0); this.groupBoxTools.Location = new System.Drawing.Point(787, 0);
this.groupBoxTools.Name = "groupBoxTools"; this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(217, 649); this.groupBoxTools.Size = new System.Drawing.Size(200, 593);
this.groupBoxTools.TabIndex = 0; this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false; this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Tools"; this.groupBoxTools.Text = "Tools";
// //
// groupBoxMaps
//
this.groupBoxMaps.Controls.Add(this.buttonAddMap);
this.groupBoxMaps.Controls.Add(this.buttonDeleteMap);
this.groupBoxMaps.Controls.Add(this.listBoxMaps);
this.groupBoxMaps.Controls.Add(this.textBoxNewMapName);
this.groupBoxMaps.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxMaps.Location = new System.Drawing.Point(6, 22);
this.groupBoxMaps.Name = "groupBoxMaps";
this.groupBoxMaps.Size = new System.Drawing.Size(205, 257);
this.groupBoxMaps.TabIndex = 19;
this.groupBoxMaps.TabStop = false;
this.groupBoxMaps.Text = "Maps";
//
// buttonAddMap
//
this.buttonAddMap.Location = new System.Drawing.Point(14, 80);
this.buttonAddMap.Name = "buttonAddMap";
this.buttonAddMap.Size = new System.Drawing.Size(180, 35);
this.buttonAddMap.TabIndex = 2;
this.buttonAddMap.Text = "Add map";
this.buttonAddMap.UseVisualStyleBackColor = true;
this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click);
//
// buttonDeleteMap
//
this.buttonDeleteMap.Location = new System.Drawing.Point(14, 206);
this.buttonDeleteMap.Name = "buttonDeleteMap";
this.buttonDeleteMap.Size = new System.Drawing.Size(180, 35);
this.buttonDeleteMap.TabIndex = 4;
this.buttonDeleteMap.Text = "Delete map";
this.buttonDeleteMap.UseVisualStyleBackColor = true;
this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click);
//
// listBoxMaps
//
this.listBoxMaps.FormattingEnabled = true;
this.listBoxMaps.ItemHeight = 15;
this.listBoxMaps.Location = new System.Drawing.Point(14, 121);
this.listBoxMaps.Name = "listBoxMaps";
this.listBoxMaps.Size = new System.Drawing.Size(180, 79);
this.listBoxMaps.TabIndex = 3;
this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged);
//
// textBoxNewMapName
//
this.textBoxNewMapName.Location = new System.Drawing.Point(14, 22);
this.textBoxNewMapName.Name = "textBoxNewMapName";
this.textBoxNewMapName.Size = new System.Drawing.Size(180, 23);
this.textBoxNewMapName.TabIndex = 0;
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта",
"Преграды-линии"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(14, 51);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(180, 23);
this.comboBoxSelectorMap.TabIndex = 9;
//
// buttonShowOnMap // buttonShowOnMap
// //
this.buttonShowOnMap.Location = new System.Drawing.Point(20, 516); this.buttonShowOnMap.Location = new System.Drawing.Point(13, 431);
this.buttonShowOnMap.Name = "buttonShowOnMap"; this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(180, 35); this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35);
this.buttonShowOnMap.TabIndex = 18; this.buttonShowOnMap.TabIndex = 18;
this.buttonShowOnMap.Text = "Show map"; this.buttonShowOnMap.Text = "Show map";
this.buttonShowOnMap.UseVisualStyleBackColor = true; this.buttonShowOnMap.UseVisualStyleBackColor = true;
@@ -145,9 +76,9 @@
// //
// buttonShowStorage // buttonShowStorage
// //
this.buttonShowStorage.Location = new System.Drawing.Point(20, 475); this.buttonShowStorage.Location = new System.Drawing.Point(13, 323);
this.buttonShowStorage.Name = "buttonShowStorage"; this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(180, 35); this.buttonShowStorage.Size = new System.Drawing.Size(175, 35);
this.buttonShowStorage.TabIndex = 17; this.buttonShowStorage.TabIndex = 17;
this.buttonShowStorage.Text = "Show storage"; this.buttonShowStorage.Text = "Show storage";
this.buttonShowStorage.UseVisualStyleBackColor = true; this.buttonShowStorage.UseVisualStyleBackColor = true;
@@ -155,9 +86,9 @@
// //
// buttonRemoveWarship // buttonRemoveWarship
// //
this.buttonRemoveWarship.Location = new System.Drawing.Point(20, 434); this.buttonRemoveWarship.Location = new System.Drawing.Point(13, 208);
this.buttonRemoveWarship.Name = "buttonRemoveWarship"; this.buttonRemoveWarship.Name = "buttonRemoveWarship";
this.buttonRemoveWarship.Size = new System.Drawing.Size(180, 35); this.buttonRemoveWarship.Size = new System.Drawing.Size(175, 35);
this.buttonRemoveWarship.TabIndex = 16; this.buttonRemoveWarship.TabIndex = 16;
this.buttonRemoveWarship.Text = "Remove warship"; this.buttonRemoveWarship.Text = "Remove warship";
this.buttonRemoveWarship.UseVisualStyleBackColor = true; this.buttonRemoveWarship.UseVisualStyleBackColor = true;
@@ -165,18 +96,18 @@
// //
// maskedTextBoxPosition // maskedTextBoxPosition
// //
this.maskedTextBoxPosition.Location = new System.Drawing.Point(20, 405); this.maskedTextBoxPosition.Location = new System.Drawing.Point(13, 177);
this.maskedTextBoxPosition.Mask = "00"; this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(180, 23); this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23);
this.maskedTextBoxPosition.TabIndex = 15; this.maskedTextBoxPosition.TabIndex = 15;
this.maskedTextBoxPosition.ValidatingType = typeof(int); this.maskedTextBoxPosition.ValidatingType = typeof(int);
// //
// buttonAddWarship // buttonAddWarship
// //
this.buttonAddWarship.Location = new System.Drawing.Point(20, 364); this.buttonAddWarship.Location = new System.Drawing.Point(13, 114);
this.buttonAddWarship.Name = "buttonAddWarship"; this.buttonAddWarship.Name = "buttonAddWarship";
this.buttonAddWarship.Size = new System.Drawing.Size(180, 35); this.buttonAddWarship.Size = new System.Drawing.Size(175, 35);
this.buttonAddWarship.TabIndex = 14; this.buttonAddWarship.TabIndex = 14;
this.buttonAddWarship.Text = "Add warship"; this.buttonAddWarship.Text = "Add warship";
this.buttonAddWarship.UseVisualStyleBackColor = true; this.buttonAddWarship.UseVisualStyleBackColor = true;
@@ -187,7 +118,7 @@
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::AircraftCarrier.Properties.Resources.ArrowRight; this.buttonRight.BackgroundImage = global::AircraftCarrier.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(126, 609); this.buttonRight.Location = new System.Drawing.Point(121, 553);
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 = 13; this.buttonRight.TabIndex = 13;
@@ -200,7 +131,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::AircraftCarrier.Properties.Resources.ArrowLeft; this.buttonLeft.BackgroundImage = global::AircraftCarrier.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(54, 609); this.buttonLeft.Location = new System.Drawing.Point(49, 553);
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 = 12; this.buttonLeft.TabIndex = 12;
@@ -213,7 +144,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::AircraftCarrier.Properties.Resources.ArrowUp; this.buttonUp.BackgroundImage = global::AircraftCarrier.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(90, 573); this.buttonUp.Location = new System.Drawing.Point(85, 517);
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 = 11; this.buttonUp.TabIndex = 11;
@@ -226,7 +157,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::AircraftCarrier.Properties.Resources.ArrowDown; this.buttonDown.BackgroundImage = global::AircraftCarrier.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(90, 609); this.buttonDown.Location = new System.Drawing.Point(85, 553);
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 = 10; this.buttonDown.TabIndex = 10;
@@ -234,12 +165,25 @@
this.buttonDown.UseVisualStyleBackColor = true; this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
// //
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта",
"Преграды-линии"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(13, 22);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(175, 23);
this.comboBoxSelectorMap.TabIndex = 9;
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(754, 649); this.pictureBox.Size = new System.Drawing.Size(787, 593);
this.pictureBox.TabIndex = 1; this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false; this.pictureBox.TabStop = false;
// //
@@ -247,15 +191,13 @@
// //
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(971, 649); this.ClientSize = new System.Drawing.Size(987, 593);
this.Controls.Add(this.pictureBox); this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools); this.Controls.Add(this.groupBoxTools);
this.Name = "FormMapWithSetWarships"; this.Name = "FormMapWithSetWarships";
this.Text = "FormMapWithSetWarships"; this.Text = "FormMapWithSetWarships";
this.groupBoxTools.ResumeLayout(false); this.groupBoxTools.ResumeLayout(false);
this.groupBoxTools.PerformLayout(); this.groupBoxTools.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);
@@ -275,10 +217,5 @@
private Button buttonRemoveWarship; private Button buttonRemoveWarship;
private MaskedTextBox maskedTextBoxPosition; private MaskedTextBox maskedTextBoxPosition;
private Button buttonAddWarship; private Button buttonAddWarship;
private GroupBox groupBoxMaps;
private Button buttonAddMap;
private Button buttonDeleteMap;
private ListBox listBoxMaps;
private TextBox textBoxNewMapName;
} }
} }

View File

@@ -14,97 +14,36 @@ namespace AircraftCarrier
public partial class FormMapWithSetWarships : Form public partial class FormMapWithSetWarships : Form
{ {
/// <summary> /// <summary>
/// Словарь для выпадающего списка /// Объект от класса карты с набором объектов
/// </summary> /// </summary>
private readonly Dictionary<string, AbstractMap> _mapsDict = new() private MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> _mapWarshipsCollectionGeneric;
{
{ "Простая карта", new SimpleMap() },
{ "Преграды-линии", new LineMap() }
};
/// <summary>
/// Объект от коллекции карт
/// </summary>
private readonly MapsCollection _mapsCollection;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
public FormMapWithSetWarships() public FormMapWithSetWarships()
{ {
InitializeComponent(); InitializeComponent();
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
comboBoxSelectorMap.Items.Clear();
foreach (var elem in _mapsDict)
{
comboBoxSelectorMap.Items.Add(elem.Key);
}
} }
/// <summary> private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
/// Заполнение listBoxMaps
/// </summary>
private void ReloadMaps()
{ {
int index = listBoxMaps.SelectedIndex; AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
listBoxMaps.Items.Clear();
for (int i = 0; i < _mapsCollection.Keys.Count; i++)
{ {
listBoxMaps.Items.Add(_mapsCollection.Keys[i]); case "Простая карта":
map = new SimpleMap();
break;
case "Преграды-линии":
map = new LineMap();
break;
} }
if (map != null)
if (listBoxMaps.Items.Count > 0 && (index == -1 || index >= listBoxMaps.Items.Count))
{ {
listBoxMaps.SelectedIndex = 0; _mapWarshipsCollectionGeneric = new MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
} }
else if (listBoxMaps.Items.Count > 0 && index > -1 && index < listBoxMaps.Items.Count) else
{ {
listBoxMaps.SelectedIndex = index; _mapWarshipsCollectionGeneric = null;
}
}
/// <summary>
/// Добавление карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// Выбор карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
/// <summary>
/// Удаление карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
} }
} }
/// <summary> /// <summary>
@@ -114,7 +53,7 @@ namespace AircraftCarrier
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonAddWarship_Click(object sender, EventArgs e) private void ButtonAddWarship_Click(object sender, EventArgs e)
{ {
if (listBoxMaps.SelectedIndex == -1) if (_mapWarshipsCollectionGeneric == null)
{ {
return; return;
} }
@@ -122,10 +61,10 @@ namespace AircraftCarrier
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
DrawingObjectWarship warship = new(form.SelectedWarship); DrawingObjectWarship warship = new(form.SelectedWarship);
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + warship >= 0) if (_mapWarshipsCollectionGeneric + warship >= 0)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet();
} }
else else
{ {
@@ -140,10 +79,6 @@ namespace AircraftCarrier
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonRemoveWarship_Click(object sender, EventArgs e) private void ButtonRemoveWarship_Click(object sender, EventArgs e)
{ {
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{ {
return; return;
@@ -153,10 +88,10 @@ namespace AircraftCarrier
return; return;
} }
int pos = Convert.ToInt32(maskedTextBoxPosition.Text); int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) if (_mapWarshipsCollectionGeneric - pos != null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet();
} }
else else
{ {
@@ -170,11 +105,11 @@ namespace AircraftCarrier
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonShowStorage_Click(object sender, EventArgs e) private void ButtonShowStorage_Click(object sender, EventArgs e)
{ {
if (listBoxMaps.SelectedIndex == -1) if (_mapWarshipsCollectionGeneric == null)
{ {
return; return;
} }
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); pictureBox.Image = _mapWarshipsCollectionGeneric.ShowSet();
} }
/// <summary> /// <summary>
/// Вывод карты /// Вывод карты
@@ -183,11 +118,11 @@ namespace AircraftCarrier
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonShowOnMap_Click(object sender, EventArgs e) private void ButtonShowOnMap_Click(object sender, EventArgs e)
{ {
if (listBoxMaps.SelectedIndex == -1) if(_mapWarshipsCollectionGeneric == null)
{ {
return; return;
} }
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); pictureBox.Image = _mapWarshipsCollectionGeneric.ShowOnMap();
} }
/// <summary> /// <summary>
/// Перемещение /// Перемещение
@@ -196,7 +131,7 @@ namespace AircraftCarrier
/// <param name="e"></param> /// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e) private void ButtonMove_Click(object sender, EventArgs e)
{ {
if (listBoxMaps.SelectedIndex == -1) if (_mapWarshipsCollectionGeneric == null)
{ {
return; return;
} }
@@ -218,7 +153,7 @@ namespace AircraftCarrier
dir = Direction.Right; dir = Direction.Right;
break; break;
} }
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); pictureBox.Image = _mapWarshipsCollectionGeneric.MoveObject(dir);
} }
} }
} }

View File

@@ -93,9 +93,13 @@ namespace AircraftCarrier
public Bitmap ShowOnMap() public Bitmap ShowOnMap()
{ {
Shaking(); Shaking();
foreach(var warship in _setWarships.GetWarships()) for (int i = 0; i < _setWarships.Count; i++)
{ {
return _map.CreateMap(_pictureWidth, _pictureHeight, warship); var warship = _setWarships.Get(i);
if (warship != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, warship);
}
} }
return new(_pictureWidth, _pictureHeight); return new(_pictureWidth, _pictureHeight);
} }
@@ -120,11 +124,11 @@ namespace AircraftCarrier
int j = _setWarships.Count - 1; int j = _setWarships.Count - 1;
for (int i = 0; i < _setWarships.Count; i++) for (int i = 0; i < _setWarships.Count; i++)
{ {
if (_setWarships[i] == null) if (_setWarships.Get(i) == null)
{ {
for (; j > i; j--) for (; j > i; j--)
{ {
var car = _setWarships[j]; var car = _setWarships.Get(j);
if (car != null) if (car != null)
{ {
_setWarships.Insert(car, i); _setWarships.Insert(car, i);
@@ -149,13 +153,11 @@ namespace AircraftCarrier
g.FillRectangle(Brushes.Aqua, 0, 0, _pictureWidth, _pictureHeight); g.FillRectangle(Brushes.Aqua, 0, 0, _pictureWidth, _pictureHeight);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{ {
for (int j = 0; j < _pictureHeight / _placeSizeHeight; ++j) for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{//линия рамзетки места {//линия рамзетки места
g.DrawLine(pen, i * _placeSizeWidth + 20, j * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2); g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
g.DrawLine(pen, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2);
g.DrawLine(pen, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + _placeSizeHeight / 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), j * _placeSizeHeight + _placeSizeHeight);
} }
g.DrawLine(pen, i * _placeSizeWidth + 20, _pictureHeight / _placeSizeHeight * _placeSizeHeight + 2, i * _placeSizeWidth + (int)(_placeSizeWidth * 0.8), _pictureHeight / _placeSizeHeight * _placeSizeHeight + 2); g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
} }
} }
/// <summary> /// <summary>
@@ -164,13 +166,13 @@ namespace AircraftCarrier
/// <param name="g"></param> /// <param name="g"></param>
private void DrawWarships(Graphics g) private void DrawWarships(Graphics g)
{ {
int width = _pictureWidth / _placeSizeWidth; int countInLine = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight; int maxLeft = (countInLine - 1) * _placeSizeWidth;
//for (int i = 0; i < _setWarships.Count; i++)
for (int i = 0; i < _setWarships.Count; i++) for (int i = 0; i < _setWarships.Count; i++)
{ {
var warship = _setWarships[i]; _setWarships.Get(i)?.SetObject(maxLeft - i % countInLine * _placeSizeWidth, i / countInLine * _placeSizeHeight + 3, _pictureWidth, _pictureHeight);
warship?.SetObject(i % _pictureWidth * _placeSizeWidth, (height - 1 - i / width) * _placeSizeHeight + 4, _pictureWidth, _pictureHeight); _setWarships.Get(i)?.DrawningObject(g);
warship?.DrawningObject(g);
} }
} }
} }

View File

@@ -1,69 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftCarrier
{
internal class MapsCollection
{
/// <summary>
/// Словарь (хранилище) с картами
/// </summary>
readonly Dictionary<string, MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>> _mapStorages;
/// <summary>
/// Возвращение списка названий карт
/// </summary>
public List<string> Keys => _mapStorages.Keys.ToList();
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string, MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление карты
/// </summary>
/// <param name="name">Название карты</param>
/// <param name="map">Карта</param>
public void AddMap(string name, AbstractMap map)
{
_mapStorages.Add(name, new(_pictureWidth, _pictureHeight, map));
}
/// <summary>
/// Удаление карты
/// </summary>
/// <param name="name">Название карты</param>
public void DelMap(string name)
{
_mapStorages.Remove(name);
}
/// <summary>
/// Доступ к докам
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public MapWithSetWarshipsGeneric<DrawingObjectWarship, AbstractMap> this[string ind]
{
get
{
_mapStorages.TryGetValue(ind, out var MapWithSetWarshipsGeneric);
return MapWithSetWarshipsGeneric;
}
}
}
}

View File

@@ -14,23 +14,20 @@ namespace AircraftCarrier
where T : class where T : class
{ {
/// <summary> /// <summary>
/// Список объектов, которые храним /// Массив объектов, которые храним
/// </summary> /// </summary>
private readonly List<T> _places; private readonly T[] _places;
/// <summary> /// <summary>
/// Количество объектов в списке /// Количество объектов в массиве
/// </summary> /// </summary>
public int Count => _places.Count; public int Count => _places.Length;
private readonly int _maxCount;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
/// <param name="count"></param> /// <param name="count"></param>
public SetWarshipsGeneric(int count) public SetWarshipsGeneric(int count)
{ {
_maxCount = count; _places = new T[count];
_places = new List<T>();
} }
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
@@ -49,8 +46,32 @@ namespace AircraftCarrier
/// <returns></returns> /// <returns></returns>
public int Insert(T warship, int position) public int Insert(T warship, int position)
{ {
if (position >= _maxCount || position < 0) return -1; int EmptyElement = -1;
_places.Insert(position, warship); if (position >= Count || position < 0) return -1;
if (_places[position] == null)
{
_places[position] = warship;
return 1;
}
else if (_places[position] != null)
{
for (int i = position + 1; i < Count; i++)
if (_places[i] == null)
{
EmptyElement = i;
break;
}
if (EmptyElement == -1)
return -1;
for (int i = EmptyElement; i > position; i--)
_places[i] = _places[i - 1];
}
_places[position] = warship;
return 1; return 1;
} }
/// <summary> /// <summary>
@@ -60,47 +81,22 @@ namespace AircraftCarrier
/// <returns></returns> /// <returns></returns>
public T Remove(int position) public T Remove(int position)
{ {
if (position >= _maxCount || position < 0) if (position >= Count || position < 0 || _places[position] == null)
return null; return null;
var result = _places[position]; T deleted = _places[position];
_places.RemoveAt(position); _places[position] = null;
return result; return deleted;
} }
/// <summary> /// <summary>
/// Получение объекта из набора по позиции /// Получение объекта из набора по позиции
/// </summary> /// </summary>
/// <param name="position"></param> /// <param name="position"></param>
/// <returns></returns> /// <returns></returns>
public T this[int position] public T Get(int position)
{ {
get if (position >= _places.Length) return null;
{ return _places[position];
if (position >= _maxCount || position < 0) return null;
return _places[position];
}
set
{
Insert(value, position);
}
}
/// <summary>
/// Проход по набору до первого пустого
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetWarships()
{
foreach(var warship in _places)
{
if(warship != null)
{
yield return warship;
}
else
{
yield break;
}
}
} }
} }
} }