Merge pull request 'Mochalov D.V. LabWork 4' (#4) from LabWork04 into LabWork03
Reviewed-on: http://student.git.athene.tech/Danila_Mochalov/PIbd-23_Mochalov_D.V._Locomotive_Base/pulls/4
This commit is contained in:
commit
3eaa599611
@ -3,7 +3,6 @@ namespace Locomotive
|
||||
public partial class FormLocomotive : Form
|
||||
{
|
||||
private DrawningLocomotive _locomotive;
|
||||
|
||||
public DrawningLocomotive SelectedLocomotive { get; private set; }
|
||||
|
||||
public FormLocomotive()
|
||||
@ -41,7 +40,6 @@ namespace Locomotive
|
||||
SetData();
|
||||
Draw();
|
||||
}
|
||||
|
||||
private void ButtonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
//ïîëó÷àåì èìÿ êíîïêè
|
||||
|
@ -29,6 +29,12 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.groupBoxTools = new System.Windows.Forms.GroupBox();
|
||||
this.groupBox1 = new System.Windows.Forms.GroupBox();
|
||||
this.buttonDeleteMap = new System.Windows.Forms.Button();
|
||||
this.listBoxMaps = new System.Windows.Forms.ListBox();
|
||||
this.buttonAddMap = new System.Windows.Forms.Button();
|
||||
this.textBoxNewMapName = new System.Windows.Forms.TextBox();
|
||||
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
|
||||
this.buttonLeft = new System.Windows.Forms.Button();
|
||||
this.buttonRight = new System.Windows.Forms.Button();
|
||||
this.buttonDown = new System.Windows.Forms.Button();
|
||||
@ -38,14 +44,15 @@
|
||||
this.buttonRemoveLocomotive = new System.Windows.Forms.Button();
|
||||
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
|
||||
this.buttonAddLocomotive = new System.Windows.Forms.Button();
|
||||
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
|
||||
this.pictureBox = new System.Windows.Forms.PictureBox();
|
||||
this.groupBoxTools.SuspendLayout();
|
||||
this.groupBox1.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// groupBoxTools
|
||||
//
|
||||
this.groupBoxTools.Controls.Add(this.groupBox1);
|
||||
this.groupBoxTools.Controls.Add(this.buttonLeft);
|
||||
this.groupBoxTools.Controls.Add(this.buttonRight);
|
||||
this.groupBoxTools.Controls.Add(this.buttonDown);
|
||||
@ -55,20 +62,82 @@
|
||||
this.groupBoxTools.Controls.Add(this.buttonRemoveLocomotive);
|
||||
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
|
||||
this.groupBoxTools.Controls.Add(this.buttonAddLocomotive);
|
||||
this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap);
|
||||
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
|
||||
this.groupBoxTools.Location = new System.Drawing.Point(580, 0);
|
||||
this.groupBoxTools.Name = "groupBoxTools";
|
||||
this.groupBoxTools.Size = new System.Drawing.Size(220, 504);
|
||||
this.groupBoxTools.Size = new System.Drawing.Size(220, 546);
|
||||
this.groupBoxTools.TabIndex = 0;
|
||||
this.groupBoxTools.TabStop = false;
|
||||
this.groupBoxTools.Text = "Tools";
|
||||
//
|
||||
// groupBox1
|
||||
//
|
||||
this.groupBox1.Controls.Add(this.buttonDeleteMap);
|
||||
this.groupBox1.Controls.Add(this.listBoxMaps);
|
||||
this.groupBox1.Controls.Add(this.buttonAddMap);
|
||||
this.groupBox1.Controls.Add(this.textBoxNewMapName);
|
||||
this.groupBox1.Controls.Add(this.comboBoxSelectorMap);
|
||||
this.groupBox1.Location = new System.Drawing.Point(6, 26);
|
||||
this.groupBox1.Name = "groupBox1";
|
||||
this.groupBox1.Size = new System.Drawing.Size(208, 250);
|
||||
this.groupBox1.TabIndex = 8;
|
||||
this.groupBox1.TabStop = false;
|
||||
this.groupBox1.Text = "Maps";
|
||||
//
|
||||
// buttonDeleteMap
|
||||
//
|
||||
this.buttonDeleteMap.Location = new System.Drawing.Point(6, 198);
|
||||
this.buttonDeleteMap.Name = "buttonDeleteMap";
|
||||
this.buttonDeleteMap.Size = new System.Drawing.Size(196, 29);
|
||||
this.buttonDeleteMap.TabIndex = 3;
|
||||
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 = 20;
|
||||
this.listBoxMaps.Location = new System.Drawing.Point(6, 128);
|
||||
this.listBoxMaps.Name = "listBoxMaps";
|
||||
this.listBoxMaps.Size = new System.Drawing.Size(196, 64);
|
||||
this.listBoxMaps.TabIndex = 2;
|
||||
this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.listBoxMaps_SelectedIndexChanged);
|
||||
//
|
||||
// buttonAddMap
|
||||
//
|
||||
this.buttonAddMap.Location = new System.Drawing.Point(6, 93);
|
||||
this.buttonAddMap.Name = "buttonAddMap";
|
||||
this.buttonAddMap.Size = new System.Drawing.Size(196, 29);
|
||||
this.buttonAddMap.TabIndex = 1;
|
||||
this.buttonAddMap.Text = "Add Map";
|
||||
this.buttonAddMap.UseVisualStyleBackColor = true;
|
||||
this.buttonAddMap.Click += new System.EventHandler(this.buttonAddMap_Click);
|
||||
//
|
||||
// textBoxNewMapName
|
||||
//
|
||||
this.textBoxNewMapName.Location = new System.Drawing.Point(6, 26);
|
||||
this.textBoxNewMapName.Name = "textBoxNewMapName";
|
||||
this.textBoxNewMapName.Size = new System.Drawing.Size(196, 27);
|
||||
this.textBoxNewMapName.TabIndex = 0;
|
||||
//
|
||||
// comboBoxSelectorMap
|
||||
//
|
||||
this.comboBoxSelectorMap.FormattingEnabled = true;
|
||||
this.comboBoxSelectorMap.Items.AddRange(new object[] {
|
||||
"Simple Map",
|
||||
"Spike Map",
|
||||
"Rail Map"});
|
||||
this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 59);
|
||||
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
|
||||
this.comboBoxSelectorMap.Size = new System.Drawing.Size(196, 28);
|
||||
this.comboBoxSelectorMap.TabIndex = 0;
|
||||
//
|
||||
// buttonLeft
|
||||
//
|
||||
this.buttonLeft.BackgroundImage = global::Locomotive.Properties.Resources.left_arrow;
|
||||
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
|
||||
this.buttonLeft.Location = new System.Drawing.Point(42, 446);
|
||||
this.buttonLeft.Location = new System.Drawing.Point(42, 502);
|
||||
this.buttonLeft.Name = "buttonLeft";
|
||||
this.buttonLeft.Size = new System.Drawing.Size(40, 40);
|
||||
this.buttonLeft.TabIndex = 7;
|
||||
@ -79,7 +148,7 @@
|
||||
//
|
||||
this.buttonRight.BackgroundImage = global::Locomotive.Properties.Resources.right_arrow;
|
||||
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
|
||||
this.buttonRight.Location = new System.Drawing.Point(134, 446);
|
||||
this.buttonRight.Location = new System.Drawing.Point(134, 502);
|
||||
this.buttonRight.Name = "buttonRight";
|
||||
this.buttonRight.Size = new System.Drawing.Size(40, 40);
|
||||
this.buttonRight.TabIndex = 7;
|
||||
@ -90,7 +159,7 @@
|
||||
//
|
||||
this.buttonDown.BackgroundImage = global::Locomotive.Properties.Resources.down_arrow;
|
||||
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
|
||||
this.buttonDown.Location = new System.Drawing.Point(88, 446);
|
||||
this.buttonDown.Location = new System.Drawing.Point(88, 502);
|
||||
this.buttonDown.Name = "buttonDown";
|
||||
this.buttonDown.Size = new System.Drawing.Size(40, 40);
|
||||
this.buttonDown.TabIndex = 7;
|
||||
@ -101,7 +170,7 @@
|
||||
//
|
||||
this.buttonUp.BackgroundImage = global::Locomotive.Properties.Resources.up_arrow;
|
||||
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
|
||||
this.buttonUp.Location = new System.Drawing.Point(88, 400);
|
||||
this.buttonUp.Location = new System.Drawing.Point(88, 456);
|
||||
this.buttonUp.Name = "buttonUp";
|
||||
this.buttonUp.Size = new System.Drawing.Size(40, 40);
|
||||
this.buttonUp.TabIndex = 6;
|
||||
@ -110,7 +179,7 @@
|
||||
//
|
||||
// buttonShowOnMap
|
||||
//
|
||||
this.buttonShowOnMap.Location = new System.Drawing.Point(7, 282);
|
||||
this.buttonShowOnMap.Location = new System.Drawing.Point(7, 421);
|
||||
this.buttonShowOnMap.Name = "buttonShowOnMap";
|
||||
this.buttonShowOnMap.Size = new System.Drawing.Size(207, 29);
|
||||
this.buttonShowOnMap.TabIndex = 5;
|
||||
@ -120,9 +189,9 @@
|
||||
//
|
||||
// buttonShowStorage
|
||||
//
|
||||
this.buttonShowStorage.Location = new System.Drawing.Point(7, 247);
|
||||
this.buttonShowStorage.Location = new System.Drawing.Point(6, 386);
|
||||
this.buttonShowStorage.Name = "buttonShowStorage";
|
||||
this.buttonShowStorage.Size = new System.Drawing.Size(207, 29);
|
||||
this.buttonShowStorage.Size = new System.Drawing.Size(208, 29);
|
||||
this.buttonShowStorage.TabIndex = 4;
|
||||
this.buttonShowStorage.Text = "Show Storage";
|
||||
this.buttonShowStorage.UseVisualStyleBackColor = true;
|
||||
@ -130,7 +199,7 @@
|
||||
//
|
||||
// buttonRemoveLocomotive
|
||||
//
|
||||
this.buttonRemoveLocomotive.Location = new System.Drawing.Point(7, 211);
|
||||
this.buttonRemoveLocomotive.Location = new System.Drawing.Point(7, 350);
|
||||
this.buttonRemoveLocomotive.Name = "buttonRemoveLocomotive";
|
||||
this.buttonRemoveLocomotive.Size = new System.Drawing.Size(207, 30);
|
||||
this.buttonRemoveLocomotive.TabIndex = 3;
|
||||
@ -140,7 +209,7 @@
|
||||
//
|
||||
// maskedTextBoxPosition
|
||||
//
|
||||
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 159);
|
||||
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 317);
|
||||
this.maskedTextBoxPosition.Mask = "00";
|
||||
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
|
||||
this.maskedTextBoxPosition.Size = new System.Drawing.Size(208, 27);
|
||||
@ -148,7 +217,7 @@
|
||||
//
|
||||
// buttonAddLocomotive
|
||||
//
|
||||
this.buttonAddLocomotive.Location = new System.Drawing.Point(6, 96);
|
||||
this.buttonAddLocomotive.Location = new System.Drawing.Point(6, 282);
|
||||
this.buttonAddLocomotive.Name = "buttonAddLocomotive";
|
||||
this.buttonAddLocomotive.Size = new System.Drawing.Size(208, 29);
|
||||
this.buttonAddLocomotive.TabIndex = 1;
|
||||
@ -156,25 +225,12 @@
|
||||
this.buttonAddLocomotive.UseVisualStyleBackColor = true;
|
||||
this.buttonAddLocomotive.Click += new System.EventHandler(this.buttonAddLocomotive_Click);
|
||||
//
|
||||
// comboBoxSelectorMap
|
||||
//
|
||||
this.comboBoxSelectorMap.FormattingEnabled = true;
|
||||
this.comboBoxSelectorMap.Items.AddRange(new object[] {
|
||||
"Simple Map",
|
||||
"Spike Map",
|
||||
"Rail Map"});
|
||||
this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 39);
|
||||
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
|
||||
this.comboBoxSelectorMap.Size = new System.Drawing.Size(208, 28);
|
||||
this.comboBoxSelectorMap.TabIndex = 0;
|
||||
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.comboBoxSelectorMap_SelectedIndexChanged);
|
||||
//
|
||||
// pictureBox
|
||||
//
|
||||
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.pictureBox.Location = new System.Drawing.Point(0, 0);
|
||||
this.pictureBox.Name = "pictureBox";
|
||||
this.pictureBox.Size = new System.Drawing.Size(580, 504);
|
||||
this.pictureBox.Size = new System.Drawing.Size(580, 546);
|
||||
this.pictureBox.TabIndex = 1;
|
||||
this.pictureBox.TabStop = false;
|
||||
//
|
||||
@ -182,13 +238,15 @@
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(800, 504);
|
||||
this.ClientSize = new System.Drawing.Size(800, 546);
|
||||
this.Controls.Add(this.pictureBox);
|
||||
this.Controls.Add(this.groupBoxTools);
|
||||
this.Name = "FormMapWithSetLocomotives";
|
||||
this.Text = "FormMapWithSetLocomotives";
|
||||
this.groupBoxTools.ResumeLayout(false);
|
||||
this.groupBoxTools.PerformLayout();
|
||||
this.groupBox1.ResumeLayout(false);
|
||||
this.groupBox1.PerformLayout();
|
||||
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
|
||||
this.ResumeLayout(false);
|
||||
|
||||
@ -208,5 +266,10 @@
|
||||
private Button buttonDown;
|
||||
private Button buttonRight;
|
||||
private Button buttonLeft;
|
||||
private GroupBox groupBox1;
|
||||
private TextBox textBoxNewMapName;
|
||||
private Button buttonAddMap;
|
||||
private Button buttonDeleteMap;
|
||||
private ListBox listBoxMaps;
|
||||
}
|
||||
}
|
@ -12,42 +12,82 @@ namespace Locomotive
|
||||
{
|
||||
public partial class FormMapWithSetLocomotives : Form
|
||||
{
|
||||
/// Объект от класса карты с набором объектов
|
||||
private MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap> _mapLocomotivesCollectionGeneric;
|
||||
/// Словарь для выпадающего списка
|
||||
private readonly Dictionary<string, AbstractMap> _mapsDict = new()
|
||||
{
|
||||
{ "Simple Map", new SimpleMap() },
|
||||
{ "Spike Map", new SpikeMap() },
|
||||
{ "Rail Map", new RailroadMap() }
|
||||
};
|
||||
/// Объект от коллекции карт
|
||||
private readonly MapsCollection _mapsCollection;
|
||||
/// Конструктор
|
||||
public FormMapWithSetLocomotives()
|
||||
{
|
||||
InitializeComponent();
|
||||
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
|
||||
comboBoxSelectorMap.Items.Clear();
|
||||
foreach (var elem in _mapsDict)
|
||||
{
|
||||
comboBoxSelectorMap.Items.Add(elem.Key);
|
||||
}
|
||||
}
|
||||
/// Заполнение listBoxMaps
|
||||
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 buttonAddMap_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text))
|
||||
{
|
||||
MessageBox.Show("Not all data is complete", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text))
|
||||
{
|
||||
MessageBox.Show("No such map", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
_mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]);
|
||||
ReloadMaps();
|
||||
}
|
||||
/// Выбор карты
|
||||
private void comboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
|
||||
private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
|
||||
{
|
||||
AbstractMap map = null;
|
||||
switch (comboBoxSelectorMap.Text)
|
||||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||||
}
|
||||
/// Удаление карты
|
||||
private void buttonDeleteMap_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (listBoxMaps.SelectedIndex == -1)
|
||||
{
|
||||
case "Simple Map":
|
||||
map = new SimpleMap();
|
||||
break;
|
||||
case "Spike Map":
|
||||
map = new SpikeMap();
|
||||
break;
|
||||
case "Rail Map":
|
||||
map = new RailroadMap();
|
||||
break;
|
||||
return;
|
||||
}
|
||||
if (map != null)
|
||||
if (MessageBox.Show($"Delete map {listBoxMaps.SelectedItem}?","Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
||||
{
|
||||
_mapLocomotivesCollectionGeneric = new MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap>
|
||||
(pictureBox.Width, pictureBox.Height, map);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mapLocomotivesCollectionGeneric = null;
|
||||
_mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
|
||||
ReloadMaps();
|
||||
}
|
||||
}
|
||||
/// Добавление объекта
|
||||
private void buttonAddLocomotive_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_mapLocomotivesCollectionGeneric == null)
|
||||
if (listBoxMaps.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -55,61 +95,67 @@ namespace Locomotive
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
DrawningObjectLocomotive locomotive = new(form.SelectedLocomotive);
|
||||
if (_mapLocomotivesCollectionGeneric + locomotive != -1)
|
||||
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + locomotive != -1)
|
||||
{
|
||||
MessageBox.Show("Объект добавлен");
|
||||
pictureBox.Image = _mapLocomotivesCollectionGeneric.ShowSet();
|
||||
MessageBox.Show("Object added");
|
||||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не удалось добавить объект");
|
||||
MessageBox.Show("Failed to add object");
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Удаление объекта
|
||||
private void buttonRemoveLocomotive_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (listBoxMaps.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||||
if (MessageBox.Show("Remove object?", "Removing", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
||||
if (_mapLocomotivesCollectionGeneric - pos is not null)
|
||||
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
|
||||
{
|
||||
MessageBox.Show("Объект удален");
|
||||
pictureBox.Image = _mapLocomotivesCollectionGeneric.ShowSet();
|
||||
MessageBox.Show("Object removed");
|
||||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Не удалось удалить объект");
|
||||
MessageBox.Show("Failed to remove object");
|
||||
}
|
||||
}
|
||||
/// Вывод набора
|
||||
private void buttonShowStorage_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_mapLocomotivesCollectionGeneric == null)
|
||||
if (listBoxMaps.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
pictureBox.Image = _mapLocomotivesCollectionGeneric.ShowSet();
|
||||
pictureBox.Image =
|
||||
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
|
||||
}
|
||||
/// Вывод карты
|
||||
private void buttonShowOnMap_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_mapLocomotivesCollectionGeneric == null)
|
||||
if (listBoxMaps.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
pictureBox.Image = _mapLocomotivesCollectionGeneric.ShowOnMap();
|
||||
pictureBox.Image =
|
||||
_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap();
|
||||
}
|
||||
/// Перемещение
|
||||
private void buttonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_mapLocomotivesCollectionGeneric == null)
|
||||
if (listBoxMaps.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -131,7 +177,8 @@ namespace Locomotive
|
||||
dir = Direction.Right;
|
||||
break;
|
||||
}
|
||||
pictureBox.Image = _mapLocomotivesCollectionGeneric.MoveObject(dir);
|
||||
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -55,13 +55,9 @@ namespace Locomotive
|
||||
public Bitmap ShowOnMap()
|
||||
{
|
||||
Shaking();
|
||||
for (int i = 0; i < _setLocomotives.Count; i++)
|
||||
foreach (var locomotive in _setLocomotives.GetLocomotives())
|
||||
{
|
||||
var locomotive = _setLocomotives.Get(i);
|
||||
if (locomotive != null)
|
||||
{
|
||||
return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
|
||||
}
|
||||
return _map.CreateMap(_pictureWidth, _pictureHeight, locomotive);
|
||||
}
|
||||
return new(_pictureWidth, _pictureHeight);
|
||||
}
|
||||
@ -80,11 +76,11 @@ namespace Locomotive
|
||||
int j = _setLocomotives.Count - 1;
|
||||
for (int i = 0; i < _setLocomotives.Count; i++)
|
||||
{
|
||||
if (_setLocomotives.Get(i) == null)
|
||||
if (_setLocomotives[i] == null)
|
||||
{
|
||||
for (; j > i; j--)
|
||||
{
|
||||
var locomotive = _setLocomotives.Get(j);
|
||||
var locomotive = _setLocomotives[j];
|
||||
if (locomotive != null)
|
||||
{
|
||||
_setLocomotives.Insert(locomotive, i);
|
||||
@ -148,11 +144,11 @@ namespace Locomotive
|
||||
int curWidth = 0;
|
||||
int curHeight = 0;
|
||||
|
||||
for (int i = 0; i < _setLocomotives.Count; i++)
|
||||
foreach (var locomotive in _setLocomotives.GetLocomotives())
|
||||
{
|
||||
// установка позиции
|
||||
_setLocomotives.Get(i)?.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 15, _pictureWidth, _pictureHeight);
|
||||
_setLocomotives.Get(i)?.DrawningObject(g);
|
||||
locomotive?.SetObject(curWidth * _placeSizeWidth + 10, curHeight * _placeSizeHeight + 15, _pictureWidth, _pictureHeight);
|
||||
locomotive?.DrawningObject(g);
|
||||
if (curWidth < width) curWidth++;
|
||||
else
|
||||
{
|
||||
|
52
Locomotive/Locomotive/MapsCollection.cs
Normal file
52
Locomotive/Locomotive/MapsCollection.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Locomotive
|
||||
{
|
||||
internal class MapsCollection
|
||||
{
|
||||
/// Словарь (хранилище) с картами
|
||||
readonly Dictionary<string, MapWithSetLocomotivesGeneric<DrawningObjectLocomotive,
|
||||
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,
|
||||
MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
/// Добавление карты
|
||||
public void AddMap(string name, AbstractMap map)
|
||||
{
|
||||
// Логика для добавления
|
||||
if (!_mapStorages.ContainsKey(name)) _mapStorages.Add(name, new MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
}
|
||||
/// Удаление карты
|
||||
public void DelMap(string name)
|
||||
{
|
||||
// Логика для удаления
|
||||
if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name);
|
||||
}
|
||||
/// Доступ к парковке
|
||||
public MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap> this[string ind]
|
||||
{
|
||||
get
|
||||
{
|
||||
// Логика получения объекта
|
||||
if (_mapStorages.ContainsKey(ind)) return _mapStorages[ind];
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -9,14 +9,18 @@ namespace Locomotive
|
||||
internal class SetLocomotivesGeneric <T>
|
||||
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 SetLocomotivesGeneric(int count)
|
||||
{
|
||||
_places = new T[count];
|
||||
_maxCount = count;
|
||||
_places = new List<T>();
|
||||
}
|
||||
/// Добавление объекта в набор
|
||||
public int Insert(T locomotive)
|
||||
@ -26,53 +30,47 @@ namespace Locomotive
|
||||
/// Добавление объекта в набор на конкретную позицию
|
||||
public int Insert(T locomotive, int position)
|
||||
{
|
||||
if (position >= _places.Length|| position < 0) return -1;
|
||||
if (position >= _maxCount|| position < 0) return -1;
|
||||
_places.Insert(position, locomotive);
|
||||
|
||||
if (_places[position] == null)
|
||||
{
|
||||
_places[position] = locomotive;
|
||||
return position;
|
||||
}
|
||||
|
||||
int emptyEl = -1;
|
||||
|
||||
for (int i = position + 1; i < Count; i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
emptyEl = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (emptyEl == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = emptyEl; i > position; i--)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
}
|
||||
_places[position] = locomotive;
|
||||
return position;
|
||||
}
|
||||
/// Удаление объекта из набора с конкретной позиции
|
||||
public T Remove(int position)
|
||||
{
|
||||
if (position >= _places.Length || position < 0) return null;
|
||||
if (position >= _maxCount || position < 0) return null;
|
||||
T result = _places[position];
|
||||
_places[position] = null;
|
||||
_places.RemoveAt(position);
|
||||
return result;
|
||||
}
|
||||
/// Получение объекта из набора по позиции
|
||||
public T Get(int position)
|
||||
// Индексатор
|
||||
public T this[int position]
|
||||
{
|
||||
if (position >= _places.Length || position < 0)
|
||||
get
|
||||
{
|
||||
return null;
|
||||
if (position >= _maxCount || position < 0) return null;
|
||||
return _places[position];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (position >= _maxCount || position < 0) return;
|
||||
Insert(value, position);
|
||||
}
|
||||
}
|
||||
/// Проход по набору до первого пустого
|
||||
public IEnumerable<T> GetLocomotives()
|
||||
{
|
||||
foreach (var locomotive in _places)
|
||||
{
|
||||
if (locomotive != null)
|
||||
{
|
||||
yield return locomotive;
|
||||
}
|
||||
else
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
return _places[position];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user