Готовая 4 лабораторная

This commit is contained in:
Даниил Путинцев 2023-10-24 18:58:46 +04:00
parent 5d96a63aa7
commit 25a690a85e
9 changed files with 387 additions and 90 deletions

View File

@ -5,9 +5,9 @@ using RoadTrain;
namespace RoadTrain.MovementStrategy namespace RoadTrain.MovementStrategy
{ {
/// <summary> /// <summary>
/// Реализация интерфейса IDrawningObject для работы с объектом DrawningCar (паттерн Adapter) /// Реализация интерфейса IDrawningObject для работы с объектом DrawningRoadTrain (паттерн Adapter)
/// </summary> /// </summary>
public class DrawningObjectTrain : IMoveableObject public class DrawningObjectTrain : IMoveableObject
{ {
private readonly DrawningRoadTrain? _drawningRoadTrain = null; private readonly DrawningRoadTrain? _drawningRoadTrain = null;
public DrawningObjectTrain (DrawningRoadTrain drawningRoadTrain) public DrawningObjectTrain (DrawningRoadTrain drawningRoadTrain)

View File

@ -102,6 +102,22 @@ namespace RoadTrain.DrawningObjects
/// <param name="y">Координата Y</param> /// <param name="y">Координата Y</param>
public void SetPosition(int x, int y) public void SetPosition(int x, int y)
{ {
if (x < 0)
{
x = 0;
}
else if (x + _trainWidth > _pictureWidth)
{
x = _pictureWidth - _trainWidth;
}
if (y < 0)
{
y = 0;
}
else if (y + _trainHeight > _pictureHeight)
{
y = _pictureHeight - _trainHeight;
}
_startPosX = x; _startPosX = x;
_startPosY = y; _startPosY = y;
} }

View File

@ -20,7 +20,7 @@ namespace RoadTrain.DrawningObjects
/// <param name="width">Ширина картинки</param> /// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param> /// <param name="height">Высота картинки</param>
public DrawningTrain(int speed, double weight, Color bodyColor, Color public DrawningTrain(int speed, double weight, Color bodyColor, Color
additionalColor, bool waterContainer, bool sweepingBrush, int width, int height) :base(speed, weight, bodyColor, 70, 30) additionalColor, bool waterContainer, bool sweepingBrush, int width, int height) :base(speed, weight, bodyColor, width, height)
{ {
if (EntityRoadTrain != null) if (EntityRoadTrain != null)
{ {

View File

@ -44,8 +44,9 @@
// pictureBoxRoadTrain // pictureBoxRoadTrain
// //
pictureBoxRoadTrain.Location = new Point(0, 0); pictureBoxRoadTrain.Location = new Point(0, 0);
pictureBoxRoadTrain.Margin = new Padding(3, 4, 3, 4);
pictureBoxRoadTrain.Name = "pictureBoxRoadTrain"; pictureBoxRoadTrain.Name = "pictureBoxRoadTrain";
pictureBoxRoadTrain.Size = new Size(685, 361); pictureBoxRoadTrain.Size = new Size(776, 481);
pictureBoxRoadTrain.TabIndex = 10; pictureBoxRoadTrain.TabIndex = 10;
pictureBoxRoadTrain.TabStop = false; pictureBoxRoadTrain.TabStop = false;
// //
@ -54,9 +55,10 @@
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.left; buttonLeft.BackgroundImage = Properties.Resources.left;
buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; buttonLeft.BackgroundImageLayout = ImageLayout.Zoom;
buttonLeft.Location = new Point(533, 269); buttonLeft.Location = new Point(609, 359);
buttonLeft.Margin = new Padding(3, 4, 3, 4);
buttonLeft.Name = "buttonLeft"; buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(30, 30); buttonLeft.Size = new Size(34, 40);
buttonLeft.TabIndex = 2; buttonLeft.TabIndex = 2;
buttonLeft.UseVisualStyleBackColor = true; buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += ButtonMove_Click; buttonLeft.Click += ButtonMove_Click;
@ -66,9 +68,10 @@
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.up; buttonUp.BackgroundImage = Properties.Resources.up;
buttonUp.BackgroundImageLayout = ImageLayout.Zoom; buttonUp.BackgroundImageLayout = ImageLayout.Zoom;
buttonUp.Location = new Point(577, 223); buttonUp.Location = new Point(659, 297);
buttonUp.Margin = new Padding(3, 4, 3, 4);
buttonUp.Name = "buttonUp"; buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(30, 30); buttonUp.Size = new Size(34, 40);
buttonUp.TabIndex = 3; buttonUp.TabIndex = 3;
buttonUp.UseVisualStyleBackColor = true; buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += ButtonMove_Click; buttonUp.Click += ButtonMove_Click;
@ -78,9 +81,10 @@
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.right; buttonRight.BackgroundImage = Properties.Resources.right;
buttonRight.BackgroundImageLayout = ImageLayout.Zoom; buttonRight.BackgroundImageLayout = ImageLayout.Zoom;
buttonRight.Location = new Point(620, 269); buttonRight.Location = new Point(709, 359);
buttonRight.Margin = new Padding(3, 4, 3, 4);
buttonRight.Name = "buttonRight"; buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(30, 30); buttonRight.Size = new Size(34, 40);
buttonRight.TabIndex = 4; buttonRight.TabIndex = 4;
buttonRight.UseVisualStyleBackColor = true; buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += ButtonMove_Click; buttonRight.Click += ButtonMove_Click;
@ -90,9 +94,10 @@
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.down; buttonDown.BackgroundImage = Properties.Resources.down;
buttonDown.BackgroundImageLayout = ImageLayout.Zoom; buttonDown.BackgroundImageLayout = ImageLayout.Zoom;
buttonDown.Location = new Point(577, 315); buttonDown.Location = new Point(659, 420);
buttonDown.Margin = new Padding(3, 4, 3, 4);
buttonDown.Name = "buttonDown"; buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(30, 30); buttonDown.Size = new Size(34, 40);
buttonDown.TabIndex = 5; buttonDown.TabIndex = 5;
buttonDown.UseVisualStyleBackColor = true; buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += ButtonMove_Click; buttonDown.Click += ButtonMove_Click;
@ -102,16 +107,18 @@
comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxStrategy.FormattingEnabled = true; comboBoxStrategy.FormattingEnabled = true;
comboBoxStrategy.Items.AddRange(new object[] { "Центр формы", "Граница формы" }); comboBoxStrategy.Items.AddRange(new object[] { "Центр формы", "Граница формы" });
comboBoxStrategy.Location = new Point(552, 12); comboBoxStrategy.Location = new Point(631, 16);
comboBoxStrategy.Margin = new Padding(3, 4, 3, 4);
comboBoxStrategy.Name = "comboBoxStrategy"; comboBoxStrategy.Name = "comboBoxStrategy";
comboBoxStrategy.Size = new Size(121, 23); comboBoxStrategy.Size = new Size(138, 28);
comboBoxStrategy.TabIndex = 8; comboBoxStrategy.TabIndex = 8;
// //
// ButtonStep // ButtonStep
// //
ButtonStep.Location = new Point(581, 50); ButtonStep.Location = new Point(664, 67);
ButtonStep.Margin = new Padding(3, 4, 3, 4);
ButtonStep.Name = "ButtonStep"; ButtonStep.Name = "ButtonStep";
ButtonStep.Size = new Size(75, 23); ButtonStep.Size = new Size(86, 31);
ButtonStep.TabIndex = 9; ButtonStep.TabIndex = 9;
ButtonStep.Text = "Шаг"; ButtonStep.Text = "Шаг";
ButtonStep.UseVisualStyleBackColor = true; ButtonStep.UseVisualStyleBackColor = true;
@ -119,9 +126,10 @@
// //
// ButtonCreateTrain // ButtonCreateTrain
// //
ButtonCreateTrain.Location = new Point(30, 285); ButtonCreateTrain.Location = new Point(34, 380);
ButtonCreateTrain.Margin = new Padding(3, 4, 3, 4);
ButtonCreateTrain.Name = "ButtonCreateTrain"; ButtonCreateTrain.Name = "ButtonCreateTrain";
ButtonCreateTrain.Size = new Size(115, 43); ButtonCreateTrain.Size = new Size(131, 80);
ButtonCreateTrain.TabIndex = 11; ButtonCreateTrain.TabIndex = 11;
ButtonCreateTrain.Text = "Создать моющую машину"; ButtonCreateTrain.Text = "Создать моющую машину";
ButtonCreateTrain.UseVisualStyleBackColor = true; ButtonCreateTrain.UseVisualStyleBackColor = true;
@ -129,9 +137,10 @@
// //
// ButtonCreateRoadTrain // ButtonCreateRoadTrain
// //
ButtonCreateRoadTrain.Location = new Point(180, 285); ButtonCreateRoadTrain.Location = new Point(206, 380);
ButtonCreateRoadTrain.Margin = new Padding(3, 4, 3, 4);
ButtonCreateRoadTrain.Name = "ButtonCreateRoadTrain"; ButtonCreateRoadTrain.Name = "ButtonCreateRoadTrain";
ButtonCreateRoadTrain.Size = new Size(93, 43); ButtonCreateRoadTrain.Size = new Size(106, 80);
ButtonCreateRoadTrain.TabIndex = 12; ButtonCreateRoadTrain.TabIndex = 12;
ButtonCreateRoadTrain.Text = "Создать поезд"; ButtonCreateRoadTrain.Text = "Создать поезд";
ButtonCreateRoadTrain.UseVisualStyleBackColor = true; ButtonCreateRoadTrain.UseVisualStyleBackColor = true;
@ -139,9 +148,10 @@
// //
// ButtonSelectTrain // ButtonSelectTrain
// //
ButtonSelectTrain.Location = new Point(567, 94); ButtonSelectTrain.Location = new Point(648, 125);
ButtonSelectTrain.Margin = new Padding(3, 4, 3, 4);
ButtonSelectTrain.Name = "ButtonSelectTrain"; ButtonSelectTrain.Name = "ButtonSelectTrain";
ButtonSelectTrain.Size = new Size(89, 42); ButtonSelectTrain.Size = new Size(102, 56);
ButtonSelectTrain.TabIndex = 13; ButtonSelectTrain.TabIndex = 13;
ButtonSelectTrain.Text = "Добавить поезд"; ButtonSelectTrain.Text = "Добавить поезд";
ButtonSelectTrain.UseVisualStyleBackColor = true; ButtonSelectTrain.UseVisualStyleBackColor = true;
@ -149,9 +159,9 @@
// //
// FormRoadTrain // FormRoadTrain
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(679, 363); ClientSize = new Size(776, 484);
Controls.Add(ButtonSelectTrain); Controls.Add(ButtonSelectTrain);
Controls.Add(ButtonCreateRoadTrain); Controls.Add(ButtonCreateRoadTrain);
Controls.Add(ButtonCreateTrain); Controls.Add(ButtonCreateTrain);
@ -162,6 +172,7 @@
Controls.Add(buttonUp); Controls.Add(buttonUp);
Controls.Add(buttonLeft); Controls.Add(buttonLeft);
Controls.Add(pictureBoxRoadTrain); Controls.Add(pictureBoxRoadTrain);
Margin = new Padding(3, 4, 3, 4);
Name = "FormRoadTrain"; Name = "FormRoadTrain";
Text = "FormRoadTrain"; Text = "FormRoadTrain";
((System.ComponentModel.ISupportInitialize)pictureBoxRoadTrain).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxRoadTrain).EndInit();

View File

@ -29,40 +29,110 @@
private void InitializeComponent() private void InitializeComponent()
{ {
panel1 = new Panel(); panel1 = new Panel();
label2 = new Label();
panel2 = new Panel();
textBoxStorageName = new TextBox();
ButtonDelObject = new Button();
ButtonAddObject = new Button();
listBoxStorages = new ListBox();
InputTextBox = new TextBox(); InputTextBox = new TextBox();
ButtonRefreshCollection = new Button(); ButtonRefreshCollection = new Button();
ButtonRemoveTrain = new Button(); ButtonRemoveTrain = new Button();
ButtonAddTrain = new Button(); ButtonAddTrain = new Button();
label1 = new Label(); label1 = new Label();
pictureBoxCollection = new PictureBox(); pictureBoxCollection = new PictureBox();
label = new Label();
panel1.SuspendLayout(); panel1.SuspendLayout();
panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// panel1 // panel1
// //
panel1.Controls.Add(label2);
panel1.Controls.Add(panel2);
panel1.Controls.Add(InputTextBox); panel1.Controls.Add(InputTextBox);
panel1.Controls.Add(ButtonRefreshCollection); panel1.Controls.Add(ButtonRefreshCollection);
panel1.Controls.Add(ButtonRemoveTrain); panel1.Controls.Add(ButtonRemoveTrain);
panel1.Controls.Add(ButtonAddTrain); panel1.Controls.Add(ButtonAddTrain);
panel1.Controls.Add(label1); panel1.Dock = DockStyle.Right;
panel1.Location = new Point(597, 12); panel1.Location = new Point(678, 0);
panel1.Margin = new Padding(3, 4, 3, 4);
panel1.Name = "panel1"; panel1.Name = "panel1";
panel1.Size = new Size(200, 426); panel1.Size = new Size(236, 600);
panel1.TabIndex = 0; panel1.TabIndex = 0;
// //
// label2
//
label2.AutoSize = true;
label2.Location = new Point(43, 18);
label2.Name = "label2";
label2.Size = new Size(66, 20);
label2.TabIndex = 0;
label2.Text = "Наборы";
//
// panel2
//
panel2.Controls.Add(textBoxStorageName);
panel2.Controls.Add(ButtonDelObject);
panel2.Controls.Add(ButtonAddObject);
panel2.Controls.Add(listBoxStorages);
panel2.Location = new Point(24, 29);
panel2.Name = "panel2";
panel2.Size = new Size(190, 278);
panel2.TabIndex = 5;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(21, 30);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(150, 27);
textBoxStorageName.TabIndex = 3;
//
// ButtonDelObject
//
ButtonDelObject.Location = new Point(21, 179);
ButtonDelObject.Name = "ButtonDelObject";
ButtonDelObject.Size = new Size(150, 29);
ButtonDelObject.TabIndex = 2;
ButtonDelObject.Text = "Удалить набор";
ButtonDelObject.UseVisualStyleBackColor = true;
ButtonDelObject.Click += ButtonDelObject_Click;
//
// ButtonAddObject
//
ButtonAddObject.Location = new Point(21, 77);
ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(150, 29);
ButtonAddObject.TabIndex = 1;
ButtonAddObject.Text = "Добавить набор";
ButtonAddObject.UseVisualStyleBackColor = true;
ButtonAddObject.Click += ButtonAddObject_Click;
//
// listBoxStorages
//
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 20;
listBoxStorages.Location = new Point(21, 121);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(150, 44);
listBoxStorages.TabIndex = 0;
listBoxStorages.SelectedIndexChanged += ListBoxStorages_SelectedIndexChanged;
//
// InputTextBox // InputTextBox
// //
InputTextBox.Location = new Point(49, 118); InputTextBox.Location = new Point(59, 384);
InputTextBox.Margin = new Padding(3, 4, 3, 4);
InputTextBox.Name = "InputTextBox"; InputTextBox.Name = "InputTextBox";
InputTextBox.Size = new Size(100, 23); InputTextBox.Size = new Size(114, 27);
InputTextBox.TabIndex = 4; InputTextBox.TabIndex = 4;
// //
// ButtonRefreshCollection // ButtonRefreshCollection
// //
ButtonRefreshCollection.Location = new Point(39, 233); ButtonRefreshCollection.Location = new Point(45, 485);
ButtonRefreshCollection.Margin = new Padding(3, 4, 3, 4);
ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(126, 44); ButtonRefreshCollection.Size = new Size(144, 59);
ButtonRefreshCollection.TabIndex = 3; ButtonRefreshCollection.TabIndex = 3;
ButtonRefreshCollection.Text = "Обновить коллекцию"; ButtonRefreshCollection.Text = "Обновить коллекцию";
ButtonRefreshCollection.UseVisualStyleBackColor = true; ButtonRefreshCollection.UseVisualStyleBackColor = true;
@ -70,9 +140,10 @@
// //
// ButtonRemoveTrain // ButtonRemoveTrain
// //
ButtonRemoveTrain.Location = new Point(39, 160); ButtonRemoveTrain.Location = new Point(45, 428);
ButtonRemoveTrain.Margin = new Padding(3, 4, 3, 4);
ButtonRemoveTrain.Name = "ButtonRemoveTrain"; ButtonRemoveTrain.Name = "ButtonRemoveTrain";
ButtonRemoveTrain.Size = new Size(126, 30); ButtonRemoveTrain.Size = new Size(144, 40);
ButtonRemoveTrain.TabIndex = 2; ButtonRemoveTrain.TabIndex = 2;
ButtonRemoveTrain.Text = "Удалить поезд"; ButtonRemoveTrain.Text = "Удалить поезд";
ButtonRemoveTrain.UseVisualStyleBackColor = true; ButtonRemoveTrain.UseVisualStyleBackColor = true;
@ -80,9 +151,10 @@
// //
// ButtonAddTrain // ButtonAddTrain
// //
ButtonAddTrain.Location = new Point(39, 45); ButtonAddTrain.Location = new Point(45, 322);
ButtonAddTrain.Margin = new Padding(3, 4, 3, 4);
ButtonAddTrain.Name = "ButtonAddTrain"; ButtonAddTrain.Name = "ButtonAddTrain";
ButtonAddTrain.Size = new Size(126, 33); ButtonAddTrain.Size = new Size(144, 44);
ButtonAddTrain.TabIndex = 1; ButtonAddTrain.TabIndex = 1;
ButtonAddTrain.Text = "Добавить поезд"; ButtonAddTrain.Text = "Добавить поезд";
ButtonAddTrain.UseVisualStyleBackColor = true; ButtonAddTrain.UseVisualStyleBackColor = true;
@ -90,34 +162,49 @@
// //
// label1 // label1
// //
label1.AutoSize = true; label1.Location = new Point(0, 0);
label1.Location = new Point(66, 14);
label1.Name = "label1"; label1.Name = "label1";
label1.Size = new Size(83, 15); label1.Size = new Size(114, 31);
label1.TabIndex = 0; label1.TabIndex = 2;
label1.Text = "Инструменты";
// //
// pictureBoxCollection // pictureBoxCollection
// //
pictureBoxCollection.Location = new Point(0, 1); pictureBoxCollection.Dock = DockStyle.Fill;
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Margin = new Padding(3, 4, 3, 4);
pictureBoxCollection.Name = "pictureBoxCollection"; pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(591, 446); pictureBoxCollection.Size = new Size(678, 600);
pictureBoxCollection.TabIndex = 1; pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false; pictureBoxCollection.TabStop = false;
// //
// label
//
label.AutoSize = true;
label.Location = new Point(718, 16);
label.Name = "label";
label.Size = new Size(103, 20);
label.TabIndex = 5;
label.Text = "Инструменты";
//
// FormTrainCollection // FormTrainCollection
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450); ClientSize = new Size(914, 600);
Controls.Add(label);
Controls.Add(pictureBoxCollection); Controls.Add(pictureBoxCollection);
Controls.Add(panel1); Controls.Add(panel1);
Controls.Add(label1);
Margin = new Padding(3, 4, 3, 4);
Name = "FormTrainCollection"; Name = "FormTrainCollection";
Text = "FormTrainCollection"; Text = "FormTrainCollection";
panel1.ResumeLayout(false); panel1.ResumeLayout(false);
panel1.PerformLayout(); panel1.PerformLayout();
panel2.ResumeLayout(false);
panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false); ResumeLayout(false);
PerformLayout();
} }
#endregion #endregion
@ -129,5 +216,12 @@
private Button ButtonRefreshCollection; private Button ButtonRefreshCollection;
private Button ButtonRemoveTrain; private Button ButtonRemoveTrain;
private PictureBox pictureBoxCollection; private PictureBox pictureBoxCollection;
private Label label;
private Label label2;
private Panel panel2;
private ListBox listBoxStorages;
private Button ButtonDelObject;
private Button ButtonAddObject;
private TextBox textBoxStorageName;
} }
} }

View File

@ -15,56 +15,144 @@ namespace RoadTrain
{ {
public partial class FormTrainCollection : Form public partial class FormTrainCollection : Form
{ {
private readonly RoadTrainGenericCollection<DrawningRoadTrain, private readonly RoadTrainGenericStorage _storage;
DrawningObjectTrain> _trains;
public FormTrainCollection() public FormTrainCollection()
{ {
InitializeComponent(); InitializeComponent();
_trains = new RoadTrainGenericCollection<DrawningRoadTrain, _storage = new RoadTrainGenericStorage(pictureBoxCollection.Width,
DrawningObjectTrain>(pictureBoxCollection.Width, pictureBoxCollection.Height); pictureBoxCollection.Height);
}
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
private void ReloadObjects()
{
int index = listBoxStorages.SelectedIndex;
listBoxStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorages.Items.Add(_storage.Keys[i]);
}
if (listBoxStorages.Items.Count > 0 && (index == -1 || index
>= listBoxStorages.Items.Count))
{
listBoxStorages.SelectedIndex = 0;
}
else if (listBoxStorages.Items.Count > 0 && index > -1 &&
index < listBoxStorages.Items.Count)
{
listBoxStorages.SelectedIndex = index;
}
} }
private void ButtonAddTrain_Click(object sender, EventArgs e) private void ButtonAddTrain_Click(object sender, EventArgs e)
{ {
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
FormRoadTrain form = new(); FormRoadTrain form = new();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
if (_trains + form.SelectedTrain != -1) if (obj + form.SelectedTrain != -1)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _trains.ShowTrains(); pictureBoxCollection.Image = obj.ShowTrains();
} }
else else
{ {
MessageBox.Show("Не удалось добавить объект"); MessageBox.Show("Не удалось добавить объект");
} }
} }
} }
private void ButtonRemoveTrain_Click(object sender, EventArgs e) private void ButtonRemoveTrain_Click(object sender, EventArgs e)
{ {
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{ {
return; return;
} }
int pos = Convert.ToInt32(InputTextBox.Text); int pos = Convert.ToInt32(InputTextBox.Text);
if (_trains - pos != null) if (obj - pos != null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _trains.ShowTrains(); pictureBoxCollection.Image = obj.ShowTrains();
} }
else else
{ {
MessageBox.Show("Не удалось удалить объект"); MessageBox.Show("Не удалось удалить объект");
} }
} }
private void ButtonRefreshCollection_Click(object sender, EventArgs e) private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{ {
pictureBoxCollection.Image = _trains.ShowTrains(); {
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowTrains();
}
}
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorages.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
}
private void ListBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowTrains();
} }
} }
} }

View File

@ -70,7 +70,7 @@ namespace RoadTrain.Generics
public static T? operator -(RoadTrainGenericCollection<T, U> collect, int public static T? operator -(RoadTrainGenericCollection<T, U> collect, int
pos) pos)
{ {
T? obj = collect._collection.Get(pos); T? obj = collect._collection[pos];
if (obj != null) if (obj != null)
{ {
collect._collection.Remove(pos); collect._collection.Remove(pos);
@ -84,7 +84,7 @@ namespace RoadTrain.Generics
/// <returns></returns> /// <returns></returns>
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection.Get(pos)?.GetMoveableObject; return (U?)_collection[pos]?.GetMoveableObject;
} }
/// <summary> /// <summary>
/// Вывод всего набора объектов /// Вывод всего набора объектов
@ -126,14 +126,13 @@ namespace RoadTrain.Generics
{ {
{ {
int width = _pictureWidth / _placeSizeWidth; int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++) for (int i = 0; i < _collection.Count; i++)
{ {
DrawningRoadTrain? tank = _collection.Get(i); DrawningRoadTrain? train = _collection[i];
if (tank == null) if (train == null)
continue; continue;
tank.SetPosition(i % width * _placeSizeWidth, i / width * _placeSizeHeight); train.SetPosition(i % width * _placeSizeWidth, i / width * _placeSizeHeight);
tank.DrawTransport(g); train.DrawTransport(g);
} }
} }
} }

View File

@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RoadTrain.DrawningObjects;
using RoadTrain.MovementStrategy;
namespace RoadTrain.Generics
{
/// <summary>
/// Класс для хранения коллекции
/// </summary>
internal class RoadTrainGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, RoadTrainGenericCollection<DrawningRoadTrain,
DrawningObjectTrain>> _trainStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _trainStorages.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 RoadTrainGenericStorage(int pictureWidth, int pictureHeight)
{
_trainStorages = new Dictionary<string,
RoadTrainGenericCollection<DrawningRoadTrain, DrawningObjectTrain>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (_trainStorages.ContainsKey(name)) return;
_trainStorages[name] = new RoadTrainGenericCollection<DrawningRoadTrain, DrawningObjectTrain>(_pictureWidth, _pictureHeight);
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (_trainStorages.ContainsKey(name)) _trainStorages.Remove(name);
return;
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public RoadTrainGenericCollection<DrawningRoadTrain, DrawningObjectTrain>?
this[string ind]
{
get
{
if (_trainStorages.ContainsKey(ind)) return _trainStorages[ind];
return null;
}
}
}
}

View File

@ -12,35 +12,28 @@ namespace RoadTrain.Generics
/// <summary> /// <summary>
/// Массив объектов, которые храним /// Массив объектов, которые храним
/// </summary> /// </summary>
private readonly T?[] _places; private readonly List<T?> _places;
/// <summary> /// <summary>
/// Количество объектов в массиве /// Количество объектов в массиве
/// </summary> /// </summary>
public int Count => _places.Length; public int Count => _places.Count;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
/// <param name="count"></param> /// <param name="count"></param>
private readonly int _maxCount;
public SetGeneric(int count) public SetGeneric(int count)
{ {
_places = new T?[count]; _maxCount = count;
_places = new List<T?> (count);
} }
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
/// </summary> /// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <returns></returns> /// <returns></returns>
public int Insert(T train) public int Insert(T train)
{ {
for (int i = Count - 1; i >= 1; i--) return Insert(train, 0);
{
if (_places[i - 1] == null)
continue;
_places[i] = _places[i - 1];
}
_places[0] = train;
return 0;
} }
/// <summary> /// <summary>
/// Добавление объекта в набор на конкретную позицию /// Добавление объекта в набор на конкретную позицию
@ -50,16 +43,12 @@ namespace RoadTrain.Generics
/// <returns></returns> /// <returns></returns>
public int Insert(T train, int position) public int Insert(T train, int position)
{ {
if ((position < 0) && (position > Count)) return -1; if (position < 0 || position >= _maxCount)
if (_places[position] != null) return -1;
{
for (int i = Count - 1; i >= position; i--) if (Count >= _maxCount)
{ return -1;
if (_places[i - 1] != null) _places.Insert(position, train);
_places[i] = _places[i - 1];
}
}
_places[position] = train;
return position; return position;
} }
/// <summary> /// <summary>
@ -69,7 +58,7 @@ namespace RoadTrain.Generics
/// <returns></returns> /// <returns></returns>
public bool Remove(int position) public bool Remove(int position)
{ {
if ((position < 0) || (position > Count)) return false; if ((position < 0) || (position > _maxCount)) return false;
_places[position] = null; _places[position] = null;
return true; return true;
} }
@ -78,11 +67,31 @@ namespace RoadTrain.Generics
/// </summary> /// </summary>
/// <param name="position"></param> /// <param name="position"></param>
/// <returns></returns> /// <returns></returns>
public T? Get(int position) public T? this[int position]
{ {
if ((position < 0) || (position > Count)) return null; get
return _places[position]; {
if ((position < 0) || (position > _maxCount)) return null;
return _places[position];
}
set
{
if ((position < 0) || (position > _maxCount)) return;
_places[position] = value;
}
} }
}
public IEnumerable<T?> GetTrains(int? maxTrains = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxTrains.HasValue && i == maxTrains.Value)
{
yield break;
}
}
}
}
} }