This commit is contained in:
ValAnn 2023-12-09 11:00:23 +04:00
parent c3ffadf6c0
commit 1edff814a3
5 changed files with 368 additions and 81 deletions

View File

@ -59,6 +59,7 @@ namespace DumpTruck.Generics
{
return -1;
}
return collect._collection.Insert(obj);
}
/// <summary>
@ -67,16 +68,17 @@ namespace DumpTruck.Generics
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(CarsGenericCollection<T, U> collect, int
pos)
public static T? operator -(CarsGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
return true;
return obj;
}
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
@ -124,21 +126,23 @@ namespace DumpTruck.Generics
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
DrawningCar car;
int numPlacesInRow = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _collection.Count; i++)
int i = 0;
foreach (var car in _collection.GetCars())
{
// TODO получение объекта
// TODO установка позиции
// TODO прорисовка объекта
car = _collection.Get(i);
if (car != null)
{
car.SetPosition((i % numPlacesInRow) * _placeSizeWidth + _placeSizeWidth / 20, _placeSizeHeight * (i / numPlacesInRow) + _placeSizeHeight / 10);
//car.SetPosition(_placeSizeWidth * (i/ numPlacesInColumn) + _placeSizeWidth / 20, (i % numPlacesInColumn ) *_placeSizeHeight + _placeSizeHeight / 10);
car.DrawTransport(g);
// TODO получение объекта
// TODO установка позиции
// TODO прорисовка объекта
car.SetPosition((i % numPlacesInRow) * _placeSizeWidth + _placeSizeWidth / 20, _placeSizeHeight * (i / numPlacesInRow) + _placeSizeHeight / 10);
car.DrawTransport(g);
}
i += 1;
}
}
}

View File

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

View File

@ -30,55 +30,43 @@
{
this.pictureBoxCollection = new System.Windows.Forms.PictureBox();
this.panelCollection = new System.Windows.Forms.Panel();
this.ButtonRefreshCollection = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.ButtonRemoveCar = new System.Windows.Forms.Button();
this.ButtonRefreshCollection = new System.Windows.Forms.Button();
this.ButtonAddCar = new System.Windows.Forms.Button();
this.panelObjects = new System.Windows.Forms.Panel();
this.ButtonDelObject_ = new System.Windows.Forms.Button();
this.textBoxStorageName = new System.Windows.Forms.TextBox();
this.listBoxStorages = new System.Windows.Forms.ListBox();
this.ButtonAddObject = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).BeginInit();
this.panelCollection.SuspendLayout();
this.panel1.SuspendLayout();
this.panelObjects.SuspendLayout();
this.SuspendLayout();
//
// pictureBoxCollection
//
this.pictureBoxCollection.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxCollection.Location = new System.Drawing.Point(0, 0);
this.pictureBoxCollection.Name = "pictureBoxCollection";
this.pictureBoxCollection.Size = new System.Drawing.Size(800, 450);
this.pictureBoxCollection.Size = new System.Drawing.Size(630, 482);
this.pictureBoxCollection.TabIndex = 0;
this.pictureBoxCollection.TabStop = false;
//
// panelCollection
//
this.panelCollection.Controls.Add(this.maskedTextBoxNumber);
this.panelCollection.Controls.Add(this.ButtonRemoveCar);
this.panelCollection.Controls.Add(this.ButtonRefreshCollection);
this.panelCollection.Controls.Add(this.ButtonAddCar);
this.panelCollection.Location = new System.Drawing.Point(601, 12);
this.panelCollection.Controls.Add(this.panel1);
this.panelCollection.Controls.Add(this.panelObjects);
this.panelCollection.Location = new System.Drawing.Point(636, 0);
this.panelCollection.Name = "panelCollection";
this.panelCollection.Size = new System.Drawing.Size(187, 426);
this.panelCollection.Size = new System.Drawing.Size(217, 470);
this.panelCollection.TabIndex = 1;
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(3, 96);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(181, 23);
this.maskedTextBoxNumber.TabIndex = 2;
this.maskedTextBoxNumber.MaskInputRejected += new System.Windows.Forms.MaskInputRejectedEventHandler(this.maskedTextBoxNumber_MaskInputRejected);
//
// ButtonRemoveCar
//
this.ButtonRemoveCar.Location = new System.Drawing.Point(3, 146);
this.ButtonRemoveCar.Name = "ButtonRemoveCar";
this.ButtonRemoveCar.Size = new System.Drawing.Size(181, 41);
this.ButtonRemoveCar.TabIndex = 2;
this.ButtonRemoveCar.Text = "Удалить машину";
this.ButtonRemoveCar.UseVisualStyleBackColor = true;
this.ButtonRemoveCar.Click += new System.EventHandler(this.ButtonRemoveCar_Click);
//
// ButtonRefreshCollection
//
this.ButtonRefreshCollection.Location = new System.Drawing.Point(3, 193);
this.ButtonRefreshCollection.Location = new System.Drawing.Point(20, 414);
this.ButtonRefreshCollection.Name = "ButtonRefreshCollection";
this.ButtonRefreshCollection.Size = new System.Drawing.Size(181, 41);
this.ButtonRefreshCollection.TabIndex = 1;
@ -86,9 +74,37 @@
this.ButtonRefreshCollection.UseVisualStyleBackColor = true;
this.ButtonRefreshCollection.Click += new System.EventHandler(this.ButtonRefreshCollection_Click);
//
// panel1
//
this.panel1.Controls.Add(this.maskedTextBoxNumber);
this.panel1.Controls.Add(this.ButtonRemoveCar);
this.panel1.Controls.Add(this.ButtonAddCar);
this.panel1.Location = new System.Drawing.Point(17, 265);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(187, 128);
this.panel1.TabIndex = 3;
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(3, 50);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(181, 23);
this.maskedTextBoxNumber.TabIndex = 2;
this.maskedTextBoxNumber.MaskInputRejected += new System.Windows.Forms.MaskInputRejectedEventHandler(this.maskedTextBoxNumber_MaskInputRejected);
//
// ButtonRemoveCar
//
this.ButtonRemoveCar.Location = new System.Drawing.Point(3, 79);
this.ButtonRemoveCar.Name = "ButtonRemoveCar";
this.ButtonRemoveCar.Size = new System.Drawing.Size(181, 41);
this.ButtonRemoveCar.TabIndex = 2;
this.ButtonRemoveCar.Text = "Удалить машину";
this.ButtonRemoveCar.UseVisualStyleBackColor = true;
this.ButtonRemoveCar.Click += new System.EventHandler(this.ButtonRemoveCar_Click);
//
// ButtonAddCar
//
this.ButtonAddCar.Location = new System.Drawing.Point(3, 37);
this.ButtonAddCar.Location = new System.Drawing.Point(3, 3);
this.ButtonAddCar.Name = "ButtonAddCar";
this.ButtonAddCar.Size = new System.Drawing.Size(181, 41);
this.ButtonAddCar.TabIndex = 0;
@ -96,18 +112,70 @@
this.ButtonAddCar.UseVisualStyleBackColor = true;
this.ButtonAddCar.Click += new System.EventHandler(this.ButtonAddCar_Click);
//
// panelObjects
//
this.panelObjects.Controls.Add(this.ButtonDelObject_);
this.panelObjects.Controls.Add(this.textBoxStorageName);
this.panelObjects.Controls.Add(this.listBoxStorages);
this.panelObjects.Controls.Add(this.ButtonAddObject);
this.panelObjects.Location = new System.Drawing.Point(17, 3);
this.panelObjects.Name = "panelObjects";
this.panelObjects.Size = new System.Drawing.Size(187, 241);
this.panelObjects.TabIndex = 4;
//
// ButtonDelObject_
//
this.ButtonDelObject_.Location = new System.Drawing.Point(3, 194);
this.ButtonDelObject_.Name = "ButtonDelObject_";
this.ButtonDelObject_.Size = new System.Drawing.Size(181, 41);
this.ButtonDelObject_.TabIndex = 3;
this.ButtonDelObject_.Text = "Удалить набор";
this.ButtonDelObject_.UseVisualStyleBackColor = true;
this.ButtonDelObject_.Click += new System.EventHandler(this.ButtonDelObject__Click);
//
// textBoxStorageName
//
this.textBoxStorageName.Location = new System.Drawing.Point(3, 12);
this.textBoxStorageName.Name = "textBoxStorageName";
this.textBoxStorageName.Size = new System.Drawing.Size(181, 23);
this.textBoxStorageName.TabIndex = 3;
this.textBoxStorageName.TextChanged += new System.EventHandler(this.textBoxStorageName_TextChanged);
//
// listBoxStorages
//
this.listBoxStorages.FormattingEnabled = true;
this.listBoxStorages.ItemHeight = 15;
this.listBoxStorages.Location = new System.Drawing.Point(3, 109);
this.listBoxStorages.Name = "listBoxStorages";
this.listBoxStorages.Size = new System.Drawing.Size(181, 79);
this.listBoxStorages.TabIndex = 2;
this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.ListBoxObjects_SelectedIndexChanged);
//
// ButtonAddObject
//
this.ButtonAddObject.Location = new System.Drawing.Point(3, 41);
this.ButtonAddObject.Name = "ButtonAddObject";
this.ButtonAddObject.Size = new System.Drawing.Size(181, 41);
this.ButtonAddObject.TabIndex = 1;
this.ButtonAddObject.Text = "Добавить набор";
this.ButtonAddObject.UseVisualStyleBackColor = true;
this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click);
//
// FormCarCollection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.ClientSize = new System.Drawing.Size(848, 482);
this.Controls.Add(this.panelCollection);
this.Controls.Add(this.pictureBoxCollection);
this.Name = "FormCarCollection";
this.Text = "Form2";
this.Text = "Набор грузовиков";
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCollection)).EndInit();
this.panelCollection.ResumeLayout(false);
this.panelCollection.PerformLayout();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.panelObjects.ResumeLayout(false);
this.panelObjects.PerformLayout();
this.ResumeLayout(false);
}
@ -120,5 +188,11 @@
private MaskedTextBox maskedTextBoxNumber;
private Button ButtonRemoveCar;
private Button ButtonRefreshCollection;
private Panel panelObjects;
private ListBox listBoxStorages;
private Button ButtonAddObject;
private Panel panel1;
private Button ButtonDelObject_;
private TextBox textBoxStorageName;
}
}

View File

@ -19,12 +19,13 @@ namespace DumpTruck
{
public partial class FormCarCollection : Form
{
private readonly CarsGenericCollection<DrawningCar, DrawningObjectCar> _cars;
//private readonly CarsGenericCollection<DrawningCar, DrawningObjectCar> _cars;
private readonly CarsGenericStorage _storage;
public FormCarCollection()
{
InitializeComponent();
_cars = new CarsGenericCollection<DrawningCar, DrawningObjectCar>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new CarsGenericStorage(pictureBoxCollection.Width,
pictureBoxCollection.Height);
}
private void Form2_Load(object sender, EventArgs e)
@ -37,42 +38,84 @@ namespace DumpTruck
}
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 ButtonAddCar_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormDumpTruck form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_cars + form.SelectedCar != null)
if (obj + form.SelectedCar != -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _cars.ShowCars();
pictureBoxCollection.Image = obj.ShowCars();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void ButtonRemoveCar_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
int pos;
if (maskedTextBoxNumber.Text == null || !int.TryParse(maskedTextBoxNumber.Text, out pos)) {
MessageBox.Show("Введите номер парковочного места");
return;
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
if (_cars - pos != null)
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _cars.ShowCars();
pictureBoxCollection.Image = obj.ShowCars();
}
else
{
@ -83,12 +126,64 @@ MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _cars.ShowCars();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowCars();
}
private void maskedTextBoxNumber_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
}
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 ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowCars();
}
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 textBoxStorageName_TextChanged(object sender, EventArgs e)
{
}
}
}

View File

@ -14,20 +14,23 @@ namespace DumpTruck.Generics
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly T?[] _places;
private readonly List<T?> _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
public int Count => _places.Count;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
///
public int startPointer = 0;
public int countMax = 0;
public SetGeneric(int count)
{
_places = new T?[count];
_places = new List<T?>(count);
countMax = count;
}
/// <summary>
/// Добавление объекта в набор
@ -36,8 +39,9 @@ namespace DumpTruck.Generics
/// <returns></returns>
public int Insert(T car)
{
if (_places[Count - 1] != null)
return -1;
if (_places.Count == countMax) { return -1; }
return Insert(car, 0);
}
/// <summary>
@ -48,22 +52,13 @@ namespace DumpTruck.Generics
/// <returns></returns>
public int Insert(T car, int position)
{
if (!(position >= 0 && position < Count))
return -1;
if (_places[position] != null)
{
int indexEnd = position + 1;
while (_places[indexEnd] != null)
{
indexEnd++;
}
for (int i = indexEnd + 1; i > position; i--)
{
_places[i] = _places[i - 1];
}
}
_places[position] = car;
if (!(position >= 0 && position <= Count && _places.Count < countMax))
{
return -1;
}
_places.Insert(position, car);
return position;
}
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
@ -89,6 +84,42 @@ namespace DumpTruck.Generics
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? this[int position]
{
get
{
// TODO проверка позиции
return _places[position];
}
set
{
// TODO проверка позиции
// TODO проверка свободных мест в списке
// TODO вставка в список по позиции
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetCars(int? maxCars = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxCars.HasValue && i == maxCars.Value)
{
yield break;
}
}
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>