ПИбд-23 Волков Никита Андреевич Лабораторная работа №4 #4

Closed
bocchanskyy wants to merge 1 commits from Lab4 into Lab3
6 changed files with 439 additions and 183 deletions

View File

@ -28,97 +28,172 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
pictureBoxCollection = new PictureBox(); this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
panelTools = new Panel(); this.panelTools = new System.Windows.Forms.Panel();
textBox = new TextBox(); this.groupListBulldozersBox = new System.Windows.Forms.GroupBox();
buttonUpdateColletion = new Button(); this.buttonDeleteBulldozersList = new System.Windows.Forms.Button();
buttonDeleteBulldozer = new Button(); this.buttonAddBulldozersList = new System.Windows.Forms.Button();
buttonAddBulldozer = new Button(); this.listBoxBulldozerStorages = new System.Windows.Forms.ListBox();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); this.textBoxStorageName = new System.Windows.Forms.TextBox();
panelTools.SuspendLayout(); this.textBoxDeletingBulldozer = new System.Windows.Forms.TextBox();
SuspendLayout(); this.buttonUpdateColletion = new System.Windows.Forms.Button();
this.buttonDeleteBulldozer = new System.Windows.Forms.Button();
this.buttonAddBulldozer = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.panelTools.SuspendLayout();
this.groupListBulldozersBox.SuspendLayout();
this.SuspendLayout();
// //
// pictureBoxCollection // pictureBoxCollection
// //
pictureBoxCollection.Anchor = AnchorStyles.Left; this.pictureBoxCollection.Anchor = System.Windows.Forms.AnchorStyles.Left;
pictureBoxCollection.BackColor = SystemColors.Control; this.pictureBoxCollection.BackColor = System.Drawing.SystemColors.Control;
pictureBoxCollection.Location = new Point(12, 12); this.pictureBoxCollection.Location = new System.Drawing.Point(14, 16);
pictureBoxCollection.Name = "pictureBoxCollection"; this.pictureBoxCollection.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
pictureBoxCollection.Size = new Size(711, 571); this.pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.TabIndex = 0; this.pictureBoxCollection.Size = new System.Drawing.Size(813, 761);
pictureBoxCollection.TabStop = false; this.pictureBoxCollection.TabIndex = 0;
this.pictureBoxCollection.TabStop = false;
// //
// panelTools // panelTools
// //
panelTools.Controls.Add(textBox); this.panelTools.Anchor = System.Windows.Forms.AnchorStyles.Right;
panelTools.Controls.Add(buttonUpdateColletion); this.panelTools.Controls.Add(this.groupListBulldozersBox);
panelTools.Controls.Add(buttonDeleteBulldozer); this.panelTools.Controls.Add(this.textBoxDeletingBulldozer);
panelTools.Controls.Add(buttonAddBulldozer); this.panelTools.Controls.Add(this.buttonUpdateColletion);
panelTools.Location = new Point(729, 12); this.panelTools.Controls.Add(this.buttonDeleteBulldozer);
panelTools.Name = "panelTools"; this.panelTools.Controls.Add(this.buttonAddBulldozer);
panelTools.Size = new Size(234, 571); this.panelTools.Location = new System.Drawing.Point(833, 16);
panelTools.TabIndex = 1; this.panelTools.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
panelTools.Tag = ""; this.panelTools.Name = "panelTools";
this.panelTools.Size = new System.Drawing.Size(267, 761);
this.panelTools.TabIndex = 1;
this.panelTools.Tag = "";
// //
// textBox // groupListBulldozersBox
// //
textBox.Location = new Point(23, 164); this.groupListBulldozersBox.Controls.Add(this.buttonDeleteBulldozersList);
textBox.Name = "textBox"; this.groupListBulldozersBox.Controls.Add(this.buttonAddBulldozersList);
textBox.Size = new Size(193, 23); this.groupListBulldozersBox.Controls.Add(this.listBoxBulldozerStorages);
textBox.TabIndex = 3; this.groupListBulldozersBox.Controls.Add(this.textBoxStorageName);
this.groupListBulldozersBox.Location = new System.Drawing.Point(16, 33);
this.groupListBulldozersBox.Name = "groupListBulldozersBox";
this.groupListBulldozersBox.Size = new System.Drawing.Size(230, 282);
this.groupListBulldozersBox.TabIndex = 8;
this.groupListBulldozersBox.TabStop = false;
this.groupListBulldozersBox.Text = "Наборы";
//
// buttonDeleteBulldozersList
//
this.buttonDeleteBulldozersList.Location = new System.Drawing.Point(6, 226);
this.buttonDeleteBulldozersList.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonDeleteBulldozersList.Name = "buttonDeleteBulldozersList";
this.buttonDeleteBulldozersList.Size = new System.Drawing.Size(221, 53);
this.buttonDeleteBulldozersList.TabIndex = 7;
this.buttonDeleteBulldozersList.Text = "Удалить набор";
this.buttonDeleteBulldozersList.UseVisualStyleBackColor = true;
this.buttonDeleteBulldozersList.Click += new System.EventHandler(this.buttonDeleteBulldozersList_Click);
//
// buttonAddBulldozersList
//
this.buttonAddBulldozersList.Location = new System.Drawing.Point(6, 55);
this.buttonAddBulldozersList.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.buttonAddBulldozersList.Name = "buttonAddBulldozersList";
this.buttonAddBulldozersList.Size = new System.Drawing.Size(221, 53);
this.buttonAddBulldozersList.TabIndex = 6;
this.buttonAddBulldozersList.Text = "Добавить набор";
this.buttonAddBulldozersList.UseVisualStyleBackColor = true;
this.buttonAddBulldozersList.Click += new System.EventHandler(this.buttonAddBulldozersList_Click);
//
// listBoxBulldozerStorages
//
this.listBoxBulldozerStorages.FormattingEnabled = true;
this.listBoxBulldozerStorages.ItemHeight = 20;
this.listBoxBulldozerStorages.Location = new System.Drawing.Point(6, 115);
this.listBoxBulldozerStorages.Name = "listBoxBulldozerStorages";
this.listBoxBulldozerStorages.Size = new System.Drawing.Size(220, 104);
this.listBoxBulldozerStorages.TabIndex = 5;
this.listBoxBulldozerStorages.SelectedIndexChanged += new System.EventHandler(this.listBoxBulldozerStorages_SelectedIndexChanged);
//
// textBoxStorageName
//
this.textBoxStorageName.Location = new System.Drawing.Point(7, 20);
this.textBoxStorageName.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.textBoxStorageName.Name = "textBoxStorageName";
this.textBoxStorageName.Size = new System.Drawing.Size(220, 27);
this.textBoxStorageName.TabIndex = 4;
//
// textBoxDeletingBulldozer
//
this.textBoxDeletingBulldozer.Location = new System.Drawing.Point(25, 525);
this.textBoxDeletingBulldozer.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.textBoxDeletingBulldozer.Name = "textBoxDeletingBulldozer";
this.textBoxDeletingBulldozer.Size = new System.Drawing.Size(220, 27);
this.textBoxDeletingBulldozer.TabIndex = 3;
// //
// buttonUpdateColletion // buttonUpdateColletion
// //
buttonUpdateColletion.Location = new Point(23, 263); this.buttonUpdateColletion.Location = new System.Drawing.Point(25, 692);
buttonUpdateColletion.Name = "buttonUpdateColletion"; this.buttonUpdateColletion.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
buttonUpdateColletion.Size = new Size(193, 40); this.buttonUpdateColletion.Name = "buttonUpdateColletion";
buttonUpdateColletion.TabIndex = 2; this.buttonUpdateColletion.Size = new System.Drawing.Size(221, 53);
buttonUpdateColletion.Text = "Обновить коллекцию"; this.buttonUpdateColletion.TabIndex = 2;
buttonUpdateColletion.UseVisualStyleBackColor = true; this.buttonUpdateColletion.Text = "Обновить коллекцию";
buttonUpdateColletion.Click += ButtonUpdateCollection_Click; this.buttonUpdateColletion.UseVisualStyleBackColor = true;
// //
// buttonDeleteBulldozer // buttonDeleteBulldozer
// //
buttonDeleteBulldozer.Location = new Point(23, 193); this.buttonDeleteBulldozer.Location = new System.Drawing.Point(25, 563);
buttonDeleteBulldozer.Name = "buttonDeleteBulldozer"; this.buttonDeleteBulldozer.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
buttonDeleteBulldozer.Size = new Size(193, 40); this.buttonDeleteBulldozer.Name = "buttonDeleteBulldozer";
buttonDeleteBulldozer.TabIndex = 1; this.buttonDeleteBulldozer.Size = new System.Drawing.Size(221, 53);
buttonDeleteBulldozer.Text = "Удалить объект"; this.buttonDeleteBulldozer.TabIndex = 1;
buttonDeleteBulldozer.UseVisualStyleBackColor = true; this.buttonDeleteBulldozer.Text = "Удалить объект";
buttonDeleteBulldozer.Click += ButtonDeleteBulldozer_Click; this.buttonDeleteBulldozer.UseVisualStyleBackColor = true;
this.buttonDeleteBulldozer.Click += new System.EventHandler(this.buttonDeleteBulldozer_Click);
// //
// buttonAddBulldozer // buttonAddBulldozer
// //
buttonAddBulldozer.Location = new Point(23, 32); this.buttonAddBulldozer.Location = new System.Drawing.Point(24, 464);
buttonAddBulldozer.Name = "buttonAddBulldozer"; this.buttonAddBulldozer.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
buttonAddBulldozer.Size = new Size(193, 40); this.buttonAddBulldozer.Name = "buttonAddBulldozer";
buttonAddBulldozer.TabIndex = 0; this.buttonAddBulldozer.Size = new System.Drawing.Size(221, 53);
buttonAddBulldozer.Text = "Добавить объект"; this.buttonAddBulldozer.TabIndex = 0;
buttonAddBulldozer.UseVisualStyleBackColor = true; this.buttonAddBulldozer.Text = "Добавить объект";
buttonAddBulldozer.Click += ButtonAddBulldozer_Click; this.buttonAddBulldozer.UseVisualStyleBackColor = true;
this.buttonAddBulldozer.Click += new System.EventHandler(this.buttonAddBulldozer_Click);
// //
// FormBulldozerCollection // FormBulldozerCollection
// //
AutoScaleDimensions = new SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
ClientSize = new Size(971, 595); this.ClientSize = new System.Drawing.Size(1110, 793);
Controls.Add(panelTools); this.Controls.Add(this.panelTools);
Controls.Add(pictureBoxCollection); this.Controls.Add(this.pictureBoxCollection);
Name = "FormBulldozerCollection"; this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
Text = "Bulldozer Collection"; this.Name = "FormBulldozerCollection";
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); this.Text = "Bulldozer Collection";
panelTools.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
panelTools.PerformLayout(); this.panelTools.ResumeLayout(false);
ResumeLayout(false); this.panelTools.PerformLayout();
this.groupListBulldozersBox.ResumeLayout(false);
this.groupListBulldozersBox.PerformLayout();
this.ResumeLayout(false);
} }
#endregion #endregion
private PictureBox pictureBoxCollection; private PictureBox pictureBoxCollection;
private Panel panelTools; private Panel panelTools;
private TextBox textBox; private TextBox textBoxDeletingBulldozer;
private Button buttonUpdateColletion; private Button buttonUpdateColletion;
private Button buttonDeleteBulldozer; private Button buttonDeleteBulldozer;
private Button buttonAddBulldozer; private Button buttonAddBulldozer;
private ListBox listBoxBulldozerStorages;
private TextBox textBoxStorageName;
private GroupBox groupListBulldozersBox;
private Button buttonDeleteBulldozersList;
private Button buttonAddBulldozersList;
} }
} }

View File

@ -7,44 +7,142 @@ namespace Bulldozer
{ {
public partial class FormBulldozerCollection : Form public partial class FormBulldozerCollection : Form
{ {
private readonly BulldozersGenericCollection<DrawingBulldozer, DrawingObjectBulldozer> _bulldozers; /// <summary>
/// Набор объектов
/// </summary>
private readonly BulldozersGenericStorage _storage;
public FormBulldozerCollection() public FormBulldozerCollection()
{ {
InitializeComponent(); InitializeComponent();
_bulldozers = new BulldozersGenericCollection<DrawingBulldozer, _storage = new BulldozersGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
DrawingObjectBulldozer>(pictureBoxCollection.Width, pictureBoxCollection.Height);
} }
private void ButtonAddBulldozer_Click(object sender, EventArgs e)
/// <summary>
/// Заполнение listBoxBulldozerStorages
/// </summary>
private void ReloadObjects()
{ {
int index = listBoxBulldozerStorages.SelectedIndex;
listBoxBulldozerStorages.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxBulldozerStorages.Items.Add(_storage.Keys[i]);
}
if (listBoxBulldozerStorages.Items.Count > 0 && (index == -1 || index >= listBoxBulldozerStorages.Items.Count))
{
listBoxBulldozerStorages.SelectedIndex = 0;
}
else if (listBoxBulldozerStorages.Items.Count > 0 && index > -1 && index < listBoxBulldozerStorages.Items.Count)
{
listBoxBulldozerStorages.SelectedIndex = index;
}
}
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAddBulldozersList_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBoxBulldozerStorages_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image = _storage[listBoxBulldozerStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBulldozers();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonDeleteBulldozersList_Click(object sender, EventArgs e)
{
if (listBoxBulldozerStorages.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {listBoxBulldozerStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxBulldozerStorages.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAddBulldozer_Click(object sender, EventArgs e)
{
if (listBoxBulldozerStorages.SelectedIndex == -1)
{
MessageBox.Show("Выберите набор в списке.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var obj = _storage[listBoxBulldozerStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
MessageBox.Show("Выбранный набор не найден.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
FormBulldozer form = new(); FormBulldozer form = new();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
if (_bulldozers + form.SelectedBulldozer != null) if (obj + form.SelectedBulldozer > -1)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _bulldozers.ShowBulldozers(); pictureBoxCollection.Image = obj.ShowBulldozers();
} }
else else
{ {
MessageBox.Show("Не удалось добавить объект"); MessageBox.Show("Не удалось добавить объект");
} }
} }
} }
private void ButtonDeleteBulldozer_Click(object sender, EventArgs e) /// <summary>
/// Удаление объекта из набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonDeleteBulldozer_Click(object sender, EventArgs e)
{ {
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) if (listBoxBulldozerStorages.SelectedIndex == -1)
{ {
return; return;
} }
int pos = Convert.ToInt32(textBox.Text); var obj = _storage[listBoxBulldozerStorages.SelectedItem.ToString() ?? string.Empty];
if (_bulldozers - pos != null) if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(textBoxDeletingBulldozer.Text);
if (obj - pos != null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _bulldozers.ShowBulldozers(); pictureBoxCollection.Image = obj.ShowBulldozers();
} }
else else
{ {
@ -52,9 +150,23 @@ namespace Bulldozer
} }
} }
/// <summary>
/// Обновление рисунка по набору
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonUpdateCollection_Click(object sender, EventArgs e) private void ButtonUpdateCollection_Click(object sender, EventArgs e)
{ {
pictureBoxCollection.Image = _bulldozers.ShowBulldozers(); if (listBoxBulldozerStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxBulldozerStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowBulldozers();
} }
} }
} }

View File

@ -1,64 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <root>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" /> <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true"> <xsd:element name="root" msdata:IsDataSet="true">

View File

@ -18,7 +18,7 @@ namespace Bulldozer.Generics
/// <summary> /// <summary>
/// Размер занимаемого объектом места (ширина) /// Размер занимаемого объектом места (ширина)
/// </summary> /// </summary>
private readonly int _placeSizeWidth = 300; private readonly int _placeSizeWidth = 270;
/// <summary> /// <summary>
/// Размер занимаемого объектом места (высота) /// Размер занимаемого объектом места (высота)
/// </summary> /// </summary>
@ -53,13 +53,20 @@ namespace Bulldozer.Generics
return collect?._collection.Insert(obj) ?? -1; return collect?._collection.Insert(obj) ?? -1;
} }
public static bool operator -(BulldozersGenericCollection<T, U> collect, int /// <summary>
pos) /// Перегрузка оператора вычитания
/// </summary>
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static T? operator -(BulldozersGenericCollection<T, U> collect, int pos)
{ {
T? obj = collect._collection.Get(pos); T? obj = collect._collection[pos];
if (obj != null) if (obj != null)
return collect._collection.Remove(pos); {
return false; collect._collection.Remove(pos);
}
return obj;
} }
/// <summary> /// <summary>
/// Получение объекта IMoveableObject /// Получение объекта IMoveableObject
@ -68,7 +75,7 @@ pos)
/// <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>
/// Вывод всего набора объектов /// Вывод всего набора объектов
@ -95,7 +102,7 @@ pos)
1; ++j) 1; ++j)
{ {
g.DrawLine(pen, i * _placeSizeWidth, j * g.DrawLine(pen, i * _placeSizeWidth, j *
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2 + 20, j *
_placeSizeHeight); _placeSizeHeight);
} }
g.DrawLine(pen, i * _placeSizeWidth, 0, i * g.DrawLine(pen, i * _placeSizeWidth, 0, i *
@ -111,17 +118,22 @@ pos)
int heightObjCount = _pictureHeight / _placeSizeHeight; int heightObjCount = _pictureHeight / _placeSizeHeight;
int widthObjCount = _pictureWidth / _placeSizeWidth; int widthObjCount = _pictureWidth / _placeSizeWidth;
int totalObjects = _collection.Count; int totalObjects = _collection.Count;
int i = 0;
for (int i = 0; i < totalObjects; i++) foreach (var bulldozer in _collection.GetBulldozers())
{ {
T? type = _collection.Get(i);
if (type != null) if (bulldozer != null)
{ {
int col = widthObjCount - 1 - (i % widthObjCount); int col = widthObjCount - 1 - (i % widthObjCount);
int row = heightObjCount - 1 - (i / widthObjCount); int row = heightObjCount - 1 - (i / widthObjCount);
type.SetPosition(col * _placeSizeWidth, row * _placeSizeHeight); bulldozer.SetPosition(col * _placeSizeWidth + 3, row * _placeSizeHeight + 3);
type?.DrawBulldozer(g); bulldozer?.DrawBulldozer(g);
i++;
}
if (i > totalObjects)
{
return;
} }
} }
} }

View File

@ -0,0 +1,87 @@
using Bulldozer.DrawingObjects;
using Bulldozer.MovementStrategy;
namespace Bulldozer.Generics
{
/// <summary>
/// Класс для хранения коллекции
/// </summary>
internal class BulldozersGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, BulldozersGenericCollection<DrawingBulldozer, DrawingObjectBulldozer>> _bulldozerStorages;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _bulldozerStorages.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 BulldozersGenericStorage(int pictureWidth, int pictureHeight)
{
_bulldozerStorages = new Dictionary<string, BulldozersGenericCollection<DrawingBulldozer, DrawingObjectBulldozer>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (!_bulldozerStorages.ContainsKey(name))
{
_bulldozerStorages.Add(name, new BulldozersGenericCollection<DrawingBulldozer, DrawingObjectBulldozer>(_pictureWidth, _pictureHeight));
}
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
// TODO: Прописать логику для удаления набора
if (_bulldozerStorages.ContainsKey(name))
{
_bulldozerStorages.Remove(name);
}
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public BulldozersGenericCollection<DrawingBulldozer, DrawingObjectBulldozer>?
this[string ind]
{
get
{
// TODO: Продумать логику получения набора
if (_bulldozerStorages.ContainsKey(ind))
{
return _bulldozerStorages[ind];
}
else
{
return null;
}
}
}
}
}

View File

@ -1,4 +1,6 @@
namespace Bulldozer.Generics using System.Numerics;
namespace Bulldozer.Generics
{ {
/// <summary> /// <summary>
/// Параметризованный набор объектов /// Параметризованный набор объектов
@ -8,57 +10,49 @@
where T : class where T : class
{ {
/// <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>
private readonly int _maxCount;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
/// </summary> /// </summary>
/// <param name="count"></param> /// <param name="count"></param>
public SetGeneric(int count) public SetGeneric(int count)
{ {
_places = new T?[count]; _maxCount = count;
_places = new List<T?>();
} }
/// <summary> /// <summary>
/// Добавление объекта в набор /// Добавление объекта в набор
/// </summary> /// </summary>
/// <param name="bulldozer">Добавляемый автомобиль</param> /// <param name="bulldozer">Добавляемый бульдозер</param>
/// <returns></returns> /// <returns></returns>
public int Insert(T bulldozer) public int Insert(T bulldozer)
{ {
if (_places[Count - 1] != null) if (_places.Count == _maxCount)
return -1; return -1;
return Insert(bulldozer, 0); Insert(bulldozer, 0);
return 1;
} }
/// <summary> /// <summary>
/// Добавление объекта в набор на конкретную позицию /// Добавление объекта в набор на конкретную позицию
/// </summary> /// </summary>
/// <param name="bulldozer">Добавляемый автомобиль</param> /// <param name="bulldozer">Добавляемый бульдозер</param>
/// <param name="position">Позиция</param>
/// <returns></returns> /// <returns></returns>
public int Insert(T bulldozer, int position) public int Insert(T bulldozer , int position)
{ {
if (!(position >= 0 && position < Count)) if (position < 0 || position >= _maxCount) return -1;
return -1; _places.Insert(position, bulldozer);
if (_places[position] != null)
{
int ind = position;
while (ind < Count && _places[ind] != null)
ind++;
if (ind == Count)
return -1;
for (int i = ind - 1; i >= position; i--)
_places[i + 1] = _places[i];
}
_places[position] = bulldozer;
return position; return position;
} }
/// <summary> /// <summary>
/// Удаление объекта из набора с конкретной позиции /// Удаление объекта из набора с конкретной позиции
@ -67,9 +61,9 @@
/// <returns></returns> /// <returns></returns>
public bool Remove(int position) public bool Remove(int position)
{ {
if (!(position >= 0 && position < Count) || _places[position] == null) if (position < 0 || position >= Count)
return false; return false;
_places[position] = null; _places.RemoveAt(position);
return true; return true;
} }
/// <summary> /// <summary>
@ -79,9 +73,45 @@
/// <returns></returns> /// <returns></returns>
public T? Get(int position) public T? Get(int position)
{ {
if (!(position >= 0 && position < Count)) if (position < 0 || position >= Count)
{
// Позиция находится за пределами допустимого диапазона, вернем null
return null; return null;
}
return _places[position]; return _places[position];
} }
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? this[int position]
{
get
{
if (position < 0 || position >= Count) return null;
return _places[position];
}
set
{
if (position < 0 || position >= Count || Count == _maxCount) return;
_places.Insert(position, value);
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetBulldozers(int? maxBulldozers = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxBulldozers.HasValue && i == maxBulldozers.Value)
{
yield break;
}
}
}
} }
} }