This commit is contained in:
Alenka 2023-11-24 20:01:41 +04:00
parent c7f9f0fc86
commit f0cb0ab565
5 changed files with 346 additions and 77 deletions

View File

@ -27,28 +27,29 @@ namespace Cruiser.Generics
_pictureHeight = picHeight; _pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height); _collection = new SetGeneric<T>(width * height);
} }
public static int operator +(CruiserGenericCollection<T, U> collect, T? public static bool operator +(CruiserGenericCollection<T, U> collect, T? obj)
obj)
{ {
if (obj == null) if (obj == null)
{ {
return -1; return false;
} }
return collect._collection.Insert(obj) ;
return collect?._collection.Insert(obj) ?? false;
} }
public static bool operator -(CruiserGenericCollection<T, U> collect, int public static T? operator -(CruiserGenericCollection<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);
} }
return false;
return obj;
} }
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection.Get(pos)?.GetMoveableObject; return (U?)_collection[pos]?.GetMoveableObject;
} }
public Bitmap ShowCruisers() public Bitmap ShowCruisers()
{ {
@ -76,15 +77,16 @@ namespace Cruiser.Generics
} }
private void DrawObjects(Graphics g) private void DrawObjects(Graphics g)
{ {
for (int i = 0; i < _collection.Count; i++) int i = 0;
foreach (var cruiser in _collection.GetTheBuses())
{ {
DrawningCruiser cruiser = _collection.Get(i);
if (cruiser != null) if (cruiser != null)
{ {
int numPlacesInRow = _pictureWidth / _placeSizeWidth; int inRow = _pictureWidth / _placeSizeWidth;
cruiser.SetPosition((i % numPlacesInRow) * _placeSizeWidth + _placeSizeWidth / 20, _placeSizeHeight * (i / numPlacesInRow) + _placeSizeHeight / 10); cruiser.SetPosition((i % inRow) * _placeSizeWidth + _placeSizeWidth / 20, _placeSizeHeight * (i / inRow) + _placeSizeHeight / 10);
cruiser.DrawTransport(g); cruiser.DrawTransport(g);
} }
i++;
} }
} }
} }

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.DrawningObjects;
using Cruiser.MovementStrategy;
namespace Cruiser.Generics
{
internal class CruiserGenericStorage
{
readonly Dictionary<string, CruiserGenericCollection<DrawningCruiser,
DrawningObjectCruiser>> _cruiserStorages;
public List<string> Keys => _cruiserStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public CruiserGenericStorage(int pictureWidth, int pictureHeight)
{
_cruiserStorages = new Dictionary<string, CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
_cruiserStorages.Add(name, new CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>(_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (!_cruiserStorages.ContainsKey(name))
{
return;
}
_cruiserStorages.Remove(name);
}
public CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>? this[string ind]
{
get
{
if (_cruiserStorages.ContainsKey(ind))
{
return _cruiserStorages[ind];
}
return null;
}
}
}
}

View File

@ -17,12 +17,18 @@
{ {
this.pictureBoxCruiser = new System.Windows.Forms.PictureBox(); this.pictureBoxCruiser = new System.Windows.Forms.PictureBox();
this.panelCruiser = new System.Windows.Forms.Panel(); this.panelCruiser = new System.Windows.Forms.Panel();
this.panelSet = new System.Windows.Forms.Panel();
this.ButtonDelObject = new System.Windows.Forms.Button();
this.listBoxStorages = new System.Windows.Forms.ListBox();
this.ButtonAddObject = new System.Windows.Forms.Button();
this.textBoxSet = new System.Windows.Forms.TextBox();
this.ButtonRefresh = new System.Windows.Forms.Button();
this.ButtonRemoveCruiser = new System.Windows.Forms.Button(); this.ButtonRemoveCruiser = new System.Windows.Forms.Button();
this.ButtonAddCruiser = new System.Windows.Forms.Button(); this.ButtonAddCruiser = new System.Windows.Forms.Button();
this.textBoxCruiser = new System.Windows.Forms.TextBox(); this.textBoxCruiser = new System.Windows.Forms.TextBox();
this.ButtonRefresh = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCruiser)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCruiser)).BeginInit();
this.panelCruiser.SuspendLayout(); this.panelCruiser.SuspendLayout();
this.panelSet.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// pictureBoxCruiser // pictureBoxCruiser
@ -30,27 +36,91 @@
this.pictureBoxCruiser.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBoxCruiser.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBoxCruiser.Location = new System.Drawing.Point(0, 0); this.pictureBoxCruiser.Location = new System.Drawing.Point(0, 0);
this.pictureBoxCruiser.Name = "pictureBoxCruiser"; this.pictureBoxCruiser.Name = "pictureBoxCruiser";
this.pictureBoxCruiser.Size = new System.Drawing.Size(800, 450); this.pictureBoxCruiser.Size = new System.Drawing.Size(904, 510);
this.pictureBoxCruiser.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; this.pictureBoxCruiser.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBoxCruiser.TabIndex = 0; this.pictureBoxCruiser.TabIndex = 0;
this.pictureBoxCruiser.TabStop = false; this.pictureBoxCruiser.TabStop = false;
this.pictureBoxCruiser.Click += new System.EventHandler(this.pictureBoxCruiser_Click);
// //
// panelCruiser // panelCruiser
// //
this.panelCruiser.Controls.Add(this.panelSet);
this.panelCruiser.Controls.Add(this.ButtonRefresh); this.panelCruiser.Controls.Add(this.ButtonRefresh);
this.panelCruiser.Controls.Add(this.ButtonRemoveCruiser); this.panelCruiser.Controls.Add(this.ButtonRemoveCruiser);
this.panelCruiser.Controls.Add(this.ButtonAddCruiser); this.panelCruiser.Controls.Add(this.ButtonAddCruiser);
this.panelCruiser.Controls.Add(this.textBoxCruiser); this.panelCruiser.Controls.Add(this.textBoxCruiser);
this.panelCruiser.Location = new System.Drawing.Point(589, 12); this.panelCruiser.Location = new System.Drawing.Point(589, 12);
this.panelCruiser.Name = "panelCruiser"; this.panelCruiser.Name = "panelCruiser";
this.panelCruiser.Size = new System.Drawing.Size(211, 438); this.panelCruiser.Size = new System.Drawing.Size(303, 486);
this.panelCruiser.TabIndex = 1; this.panelCruiser.TabIndex = 1;
this.panelCruiser.Paint += new System.Windows.Forms.PaintEventHandler(this.panelCruiser_Paint);
//
// panelSet
//
this.panelSet.AccessibleDescription = "";
this.panelSet.AccessibleName = "";
this.panelSet.Controls.Add(this.ButtonDelObject);
this.panelSet.Controls.Add(this.listBoxStorages);
this.panelSet.Controls.Add(this.ButtonAddObject);
this.panelSet.Controls.Add(this.textBoxSet);
this.panelSet.Location = new System.Drawing.Point(26, 3);
this.panelSet.Name = "panelSet";
this.panelSet.Size = new System.Drawing.Size(258, 242);
this.panelSet.TabIndex = 4;
this.panelSet.Tag = "";
//
// ButtonDelObject
//
this.ButtonDelObject.Location = new System.Drawing.Point(40, 188);
this.ButtonDelObject.Name = "ButtonDelObject";
this.ButtonDelObject.Size = new System.Drawing.Size(161, 39);
this.ButtonDelObject.TabIndex = 3;
this.ButtonDelObject.Text = "Удалить набор";
this.ButtonDelObject.UseVisualStyleBackColor = true;
this.ButtonDelObject.Click += new System.EventHandler(this.ButtonDelObject_Click);
//
// listBoxStorages
//
this.listBoxStorages.FormattingEnabled = true;
this.listBoxStorages.ItemHeight = 25;
this.listBoxStorages.Location = new System.Drawing.Point(77, 103);
this.listBoxStorages.Name = "listBoxStorages";
this.listBoxStorages.Size = new System.Drawing.Size(103, 79);
this.listBoxStorages.TabIndex = 2;
this.listBoxStorages.SelectedIndexChanged += new System.EventHandler(this.listBoxObjects_SelectedIndexChanged);
//
// ButtonAddObject
//
this.ButtonAddObject.Location = new System.Drawing.Point(40, 53);
this.ButtonAddObject.Name = "ButtonAddObject";
this.ButtonAddObject.Size = new System.Drawing.Size(161, 44);
this.ButtonAddObject.TabIndex = 1;
this.ButtonAddObject.Text = "Создать набор";
this.ButtonAddObject.UseVisualStyleBackColor = true;
this.ButtonAddObject.Click += new System.EventHandler(this.ButtonAddObject_Click);
//
// textBoxSet
//
this.textBoxSet.Location = new System.Drawing.Point(61, 16);
this.textBoxSet.Name = "textBoxSet";
this.textBoxSet.Size = new System.Drawing.Size(119, 31);
this.textBoxSet.TabIndex = 0;
//
// ButtonRefresh
//
this.ButtonRefresh.Location = new System.Drawing.Point(57, 452);
this.ButtonRefresh.Name = "ButtonRefresh";
this.ButtonRefresh.Size = new System.Drawing.Size(168, 31);
this.ButtonRefresh.TabIndex = 2;
this.ButtonRefresh.Text = "Обновить";
this.ButtonRefresh.UseVisualStyleBackColor = true;
this.ButtonRefresh.Click += new System.EventHandler(this.ButtonRefreshCollection_Click);
// //
// ButtonRemoveCruiser // ButtonRemoveCruiser
// //
this.ButtonRemoveCruiser.Location = new System.Drawing.Point(31, 168); this.ButtonRemoveCruiser.Location = new System.Drawing.Point(57, 394);
this.ButtonRemoveCruiser.Name = "ButtonRemoveCruiser"; this.ButtonRemoveCruiser.Name = "ButtonRemoveCruiser";
this.ButtonRemoveCruiser.Size = new System.Drawing.Size(150, 34); this.ButtonRemoveCruiser.Size = new System.Drawing.Size(168, 34);
this.ButtonRemoveCruiser.TabIndex = 2; this.ButtonRemoveCruiser.TabIndex = 2;
this.ButtonRemoveCruiser.Text = "Удалить"; this.ButtonRemoveCruiser.Text = "Удалить";
this.ButtonRemoveCruiser.UseVisualStyleBackColor = true; this.ButtonRemoveCruiser.UseVisualStyleBackColor = true;
@ -58,36 +128,26 @@
// //
// ButtonAddCruiser // ButtonAddCruiser
// //
this.ButtonAddCruiser.Location = new System.Drawing.Point(31, 37); this.ButtonAddCruiser.Location = new System.Drawing.Point(57, 262);
this.ButtonAddCruiser.Name = "ButtonAddCruiser"; this.ButtonAddCruiser.Name = "ButtonAddCruiser";
this.ButtonAddCruiser.Size = new System.Drawing.Size(150, 34); this.ButtonAddCruiser.Size = new System.Drawing.Size(168, 54);
this.ButtonAddCruiser.TabIndex = 3; this.ButtonAddCruiser.TabIndex = 3;
this.ButtonAddCruiser.Text = "Добавить"; this.ButtonAddCruiser.Text = "Добавить объект";
this.ButtonAddCruiser.UseVisualStyleBackColor = true; this.ButtonAddCruiser.UseVisualStyleBackColor = true;
this.ButtonAddCruiser.Click += new System.EventHandler(this.ButtonAddCruiser_Click); this.ButtonAddCruiser.Click += new System.EventHandler(this.ButtonAddCruiser_Click);
// //
// textBoxCruiser // textBoxCruiser
// //
this.textBoxCruiser.Location = new System.Drawing.Point(31, 104); this.textBoxCruiser.Location = new System.Drawing.Point(57, 336);
this.textBoxCruiser.Name = "textBoxCruiser"; this.textBoxCruiser.Name = "textBoxCruiser";
this.textBoxCruiser.Size = new System.Drawing.Size(150, 31); this.textBoxCruiser.Size = new System.Drawing.Size(168, 31);
this.textBoxCruiser.TabIndex = 2; this.textBoxCruiser.TabIndex = 2;
// //
// ButtonRefresh
//
this.ButtonRefresh.Location = new System.Drawing.Point(46, 324);
this.ButtonRefresh.Name = "ButtonRefresh";
this.ButtonRefresh.Size = new System.Drawing.Size(120, 31);
this.ButtonRefresh.TabIndex = 2;
this.ButtonRefresh.Text = "Обновить";
this.ButtonRefresh.UseVisualStyleBackColor = true;
this.ButtonRefresh.Click += new System.EventHandler(this.ButtonRefreshCollection_Click);
//
// FormCruiserCollection // FormCruiserCollection
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450); this.ClientSize = new System.Drawing.Size(904, 510);
this.Controls.Add(this.panelCruiser); this.Controls.Add(this.panelCruiser);
this.Controls.Add(this.pictureBoxCruiser); this.Controls.Add(this.pictureBoxCruiser);
this.Name = "FormCruiserCollection"; this.Name = "FormCruiserCollection";
@ -95,6 +155,8 @@
((System.ComponentModel.ISupportInitialize)(this.pictureBoxCruiser)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxCruiser)).EndInit();
this.panelCruiser.ResumeLayout(false); this.panelCruiser.ResumeLayout(false);
this.panelCruiser.PerformLayout(); this.panelCruiser.PerformLayout();
this.panelSet.ResumeLayout(false);
this.panelSet.PerformLayout();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@ -107,5 +169,10 @@
private Button ButtonAddCruiser; private Button ButtonAddCruiser;
private TextBox textBoxCruiser; private TextBox textBoxCruiser;
private Button ButtonRefresh; private Button ButtonRefresh;
private Panel panelSet;
private Button ButtonDelObject;
private ListBox listBoxStorages;
private Button ButtonAddObject;
private TextBox textBoxSet;
} }
} }

View File

@ -15,21 +15,79 @@ namespace Cruiser
{ {
public partial class FormCruiserCollection : Form public partial class FormCruiserCollection : Form
{ {
private readonly CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser> _cruiser; private readonly CruiserGenericStorage _storage;
public FormCruiserCollection() public FormCruiserCollection()
{ {
InitializeComponent(); InitializeComponent();
_cruiser = new CruiserGenericCollection<DrawningCruiser, DrawningObjectCruiser>(pictureBoxCruiser.Width, pictureBoxCruiser.Height); _storage = new CruiserGenericStorage(pictureBoxCruiser.Width, pictureBoxCruiser.Height);
}
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 ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxSet.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxSet.Text);
ReloadObjects();
}
private void listBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCruiser.Image = _storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowCruisers();
}
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 ButtonAddCruiser_Click(object sender, EventArgs e) private void ButtonAddCruiser_Click(object sender, EventArgs e)
{ {
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
CruiserForm form = new(); CruiserForm form = new();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
if (_cruiser + form.SelectedCruiser != -1) if (obj + form.SelectedCruiser)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
pictureBoxCruiser.Image = _cruiser.ShowCruisers(); pictureBoxCruiser.Image = obj.ShowCruisers();
} }
else else
{ {
@ -39,17 +97,26 @@ namespace Cruiser
} }
private void ButtonRemoveCruiser_Click(object sender, EventArgs e) private void ButtonRemoveCruiser_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(textBoxCruiser.Text); int pos = Convert.ToInt32(textBoxCruiser.Text);
if (obj - pos != null)
if (_cruiser - pos != true)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
pictureBoxCruiser.Image = _cruiser.ShowCruisers(); pictureBoxCruiser.Image = obj.ShowCruisers();
} }
else else
{ {
@ -58,7 +125,27 @@ namespace Cruiser
} }
private void ButtonRefreshCollection_Click(object sender, EventArgs e) private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{ {
pictureBoxCruiser.Image = _cruiser.ShowCruisers(); if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCruiser.Image = obj.ShowCruisers();
}
private void panelCruiser_Paint(object sender, PaintEventArgs e)
{
}
private void pictureBoxCruiser_Click(object sender, EventArgs e)
{
} }
} }
} }

View File

@ -9,59 +9,122 @@ namespace Cruiser.Generics
internal class SetGeneric<T> internal class SetGeneric<T>
where T : class where T : class
{ {
private readonly T?[] _places; /// <summary>
public int Count => _places.Length; /// Список объектов, которые храним
public int startPointer = 0; /// </summary>
private readonly List<T?> _places;
/// <summary>
/// Количество объектов в cписке
/// </summary>
public int Count => _places.Count;
/// <summary>
/// Максимальное количество объектов в cписке
/// </summary>
private readonly int _maxCount;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count) public SetGeneric(int count)
{ {
_places = new T?[count]; _maxCount = count;
_places = new List<T?>(count);
} }
public int Insert(T cruiser)
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="bus">Добавляемый автобус</param>
/// <returns></returns>
public bool Insert(T bus)
{ {
return Insert(cruiser, 0); if (_places.Count == _maxCount)
{
return false;
}
Insert(bus, 0);
return true;
} }
public int Insert(T cruiser, int position)
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="bus">Добавляемый автобус</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public bool Insert(T bus, int position)
{ {
int nullIndex = -1, i; if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
if (position < 0 || position >= Count)
{ {
return -1; return false;
} }
for (i = position; i < Count; i++)
{ _places.Insert(position, bus);
if (_places[i] == null) return true;
{
nullIndex = i;
break;
}
}
if (nullIndex < 0)
{
return -1;
}
for (i = nullIndex; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = cruiser;
return position;
} }
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position) public bool Remove(int position)
{ {
if (position < 0 || position >= Count) if (position < 0 || position >= Count)
{ {
return false; return false;
} }
_places[position] = null;
_places.RemoveAt(position);
return true; return true;
} }
public T? Get(int position)
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? this[int position]
{ {
if (position < 0 || position >= Count) get
{ {
return null; if (position < 0 || position >= _maxCount)
{
return null;
}
return _places[position];
}
set
{
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
{
return;
}
_places.Insert(position, value);
return;
}
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public IEnumerable<T?> GetTheBuses(int? maxTheBuses = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxTheBuses.HasValue && i == maxTheBuses.Value)
{
yield break;
}
} }
return _places[position];
} }
} }
} }