Problem draw collection

This commit is contained in:
Игорь Гордеев 2023-11-18 21:02:08 +04:00
parent c0b10d25c2
commit bd6f7fb3f3
5 changed files with 311 additions and 87 deletions

View File

@ -30,77 +30,134 @@
{ {
CollectionPictureBox = new PictureBox(); CollectionPictureBox = new PictureBox();
ToolsBox = new GroupBox(); ToolsBox = new GroupBox();
NaborGroupBox = new GroupBox();
ButtomRemoveNabor = new Button();
listBoxStorage = new ListBox();
AddNabor = new Button();
textBoxStorageName = new TextBox();
Input = new TextBox(); Input = new TextBox();
RefreshCollection = new Button(); RefreshCollection = new Button();
DelLocomotive = new Button(); DelLocomotive = new Button();
AddLocomotive = new Button(); AddLocomotive = new Button();
((System.ComponentModel.ISupportInitialize)CollectionPictureBox).BeginInit(); ((System.ComponentModel.ISupportInitialize)CollectionPictureBox).BeginInit();
ToolsBox.SuspendLayout(); ToolsBox.SuspendLayout();
NaborGroupBox.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
// CollectionPictureBox // CollectionPictureBox
// //
CollectionPictureBox.Location = new Point(3, 0); CollectionPictureBox.Location = new Point(3, 0);
CollectionPictureBox.Name = "CollectionPictureBox"; CollectionPictureBox.Name = "CollectionPictureBox";
CollectionPictureBox.Size = new Size(634, 457); CollectionPictureBox.Size = new Size(696, 554);
CollectionPictureBox.TabIndex = 0; CollectionPictureBox.TabIndex = 0;
CollectionPictureBox.TabStop = false; CollectionPictureBox.TabStop = false;
// //
// ToolsBox // ToolsBox
// //
ToolsBox.Controls.Add(NaborGroupBox);
ToolsBox.Controls.Add(Input); ToolsBox.Controls.Add(Input);
ToolsBox.Controls.Add(RefreshCollection); ToolsBox.Controls.Add(RefreshCollection);
ToolsBox.Controls.Add(DelLocomotive); ToolsBox.Controls.Add(DelLocomotive);
ToolsBox.Controls.Add(AddLocomotive); ToolsBox.Controls.Add(AddLocomotive);
ToolsBox.Location = new Point(643, 0); ToolsBox.Location = new Point(705, 12);
ToolsBox.Name = "ToolsBox"; ToolsBox.Name = "ToolsBox";
ToolsBox.Size = new Size(200, 457); ToolsBox.Size = new Size(200, 542);
ToolsBox.TabIndex = 1; ToolsBox.TabIndex = 1;
ToolsBox.TabStop = false; ToolsBox.TabStop = false;
ToolsBox.Text = "Инструменты"; ToolsBox.Text = "Инструменты";
// //
// NaborGroupBox
//
NaborGroupBox.Controls.Add(ButtomRemoveNabor);
NaborGroupBox.Controls.Add(listBoxStorage);
NaborGroupBox.Controls.Add(AddNabor);
NaborGroupBox.Controls.Add(textBoxStorageName);
NaborGroupBox.Location = new Point(7, 22);
NaborGroupBox.Name = "NaborGroupBox";
NaborGroupBox.Size = new Size(185, 329);
NaborGroupBox.TabIndex = 4;
NaborGroupBox.TabStop = false;
NaborGroupBox.Text = "Наборы";
//
// ButtomRemoveNabor
//
ButtomRemoveNabor.Location = new Point(6, 275);
ButtomRemoveNabor.Name = "ButtomRemoveNabor";
ButtomRemoveNabor.Size = new Size(173, 48);
ButtomRemoveNabor.TabIndex = 3;
ButtomRemoveNabor.Text = "Удалить набор";
ButtomRemoveNabor.UseVisualStyleBackColor = true;
ButtomRemoveNabor.Click += ButtonRemoveObject_Click;
//
// listBoxStorage
//
listBoxStorage.FormattingEnabled = true;
listBoxStorage.ItemHeight = 15;
listBoxStorage.Location = new Point(6, 112);
listBoxStorage.Name = "listBoxStorage";
listBoxStorage.Size = new Size(173, 139);
listBoxStorage.TabIndex = 2;
listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged;
//
// AddNabor
//
AddNabor.Location = new Point(6, 63);
AddNabor.Name = "AddNabor";
AddNabor.Size = new Size(173, 43);
AddNabor.TabIndex = 1;
AddNabor.Text = "Добавить набор";
AddNabor.UseVisualStyleBackColor = true;
AddNabor.Click += ButtonAddObject_Click;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(6, 34);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(173, 23);
textBoxStorageName.TabIndex = 0;
//
// Input // Input
// //
Input.Location = new Point(7, 91); Input.Location = new Point(6, 409);
Input.Name = "Input"; Input.Name = "Input";
Input.Size = new Size(186, 23); Input.Size = new Size(186, 23);
Input.TabIndex = 3; Input.TabIndex = 3;
// //
// RefreshCollection // RefreshCollection
// //
RefreshCollection.Location = new Point(7, 240); RefreshCollection.Location = new Point(7, 490);
RefreshCollection.Name = "RefreshCollection"; RefreshCollection.Name = "RefreshCollection";
RefreshCollection.Size = new Size(187, 46); RefreshCollection.Size = new Size(187, 46);
RefreshCollection.TabIndex = 2; RefreshCollection.TabIndex = 2;
RefreshCollection.Text = "Обновить коллекцию"; RefreshCollection.Text = "Обновить все";
RefreshCollection.UseVisualStyleBackColor = true; RefreshCollection.UseVisualStyleBackColor = true;
RefreshCollection.Click += refreshCollection_Click; RefreshCollection.Click += ButtonRefreshCollection_Click;
// //
// DelLocomotive // DelLocomotive
// //
DelLocomotive.Location = new Point(7, 134); DelLocomotive.Location = new Point(6, 438);
DelLocomotive.Name = "DelLocomotive"; DelLocomotive.Name = "DelLocomotive";
DelLocomotive.Size = new Size(187, 46); DelLocomotive.Size = new Size(187, 46);
DelLocomotive.TabIndex = 1; DelLocomotive.TabIndex = 1;
DelLocomotive.Text = "Удалить локомотив"; DelLocomotive.Text = "Удалить локомотив";
DelLocomotive.UseVisualStyleBackColor = true; DelLocomotive.UseVisualStyleBackColor = true;
DelLocomotive.Click += deleteLoco_Click; DelLocomotive.Click += ButtonRemoveLocomotive_Click;
// //
// AddLocomotive // AddLocomotive
// //
AddLocomotive.Location = new Point(6, 22); AddLocomotive.Location = new Point(5, 357);
AddLocomotive.Name = "AddLocomotive"; AddLocomotive.Name = "AddLocomotive";
AddLocomotive.Size = new Size(187, 46); AddLocomotive.Size = new Size(187, 46);
AddLocomotive.TabIndex = 0; AddLocomotive.TabIndex = 0;
AddLocomotive.Text = "Добавить локомотив"; AddLocomotive.Text = "Добавить локомотив";
AddLocomotive.UseVisualStyleBackColor = true; AddLocomotive.UseVisualStyleBackColor = true;
AddLocomotive.Click += addLocomotiv_Click; AddLocomotive.Click += ButtonAddLocomotive_Click;
// //
// FormLocomotiveCollection // FormLocomotiveCollection
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(848, 469); ClientSize = new Size(917, 566);
Controls.Add(ToolsBox); Controls.Add(ToolsBox);
Controls.Add(CollectionPictureBox); Controls.Add(CollectionPictureBox);
Name = "FormLocomotiveCollection"; Name = "FormLocomotiveCollection";
@ -108,6 +165,8 @@
((System.ComponentModel.ISupportInitialize)CollectionPictureBox).EndInit(); ((System.ComponentModel.ISupportInitialize)CollectionPictureBox).EndInit();
ToolsBox.ResumeLayout(false); ToolsBox.ResumeLayout(false);
ToolsBox.PerformLayout(); ToolsBox.PerformLayout();
NaborGroupBox.ResumeLayout(false);
NaborGroupBox.PerformLayout();
ResumeLayout(false); ResumeLayout(false);
} }
@ -119,5 +178,10 @@
private Button RefreshCollection; private Button RefreshCollection;
private Button DelLocomotive; private Button DelLocomotive;
private Button AddLocomotive; private Button AddLocomotive;
private GroupBox NaborGroupBox;
private Button AddNabor;
private TextBox textBoxStorageName;
private Button ButtomRemoveNabor;
private ListBox listBoxStorage;
} }
} }

View File

@ -14,22 +14,86 @@ namespace ElectricLocomotive
{ {
public partial class FormLocomotiveCollection : Form public partial class FormLocomotiveCollection : Form
{ {
private readonly LocomotivesGenericCollection<DrawningLocomotive, DrawningObjectLocomotive> _locos;
private readonly LocomotiveGenericStorage _storage;
//private readonly LocomotiveGenericCollection<DrawingLocomotive, DrawingObjectLocomotive> _locomotives;
public FormLocomotiveCollection() public FormLocomotiveCollection()
{ {
InitializeComponent(); InitializeComponent();
_locos = new(CollectionPictureBox.Width, CollectionPictureBox.Height); _storage = new LocomotiveGenericStorage(CollectionPictureBox.Width, CollectionPictureBox.Height);
} }
private void addLocomotiv_Click(object sender, EventArgs e) /// <summary>
/// Заполнение listBoxObjects
/// </summary>
private void ReloadObjects()
{ {
int index = listBoxStorage.SelectedIndex;
listBoxStorage.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
listBoxStorage.Items.Add(_storage.Keys[i]);
}
if (listBoxStorage.Items.Count > 0 && (index == -1 || index >= listBoxStorage.Items.Count))
{
listBoxStorage.SelectedIndex = 0;
}
else if (listBoxStorage.Items.Count > 0 && index > -1 && index < listBoxStorage.Items.Count)
{
listBoxStorage.SelectedIndex = index;
}
}
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 listBoxStorage_SelectedIndexChanged(object sender, EventArgs e)
{
CollectionPictureBox.Image = _storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowLocomotives();
}
private void ButtonRemoveObject_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {listBoxStorage.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(listBoxStorage.SelectedItem.ToString() ?? string.Empty);
ReloadObjects();
}
}
private void ButtonAddLocomotive_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1) return;
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
ElectricLocomotive form = new(); ElectricLocomotive form = new();
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
if (_locos + form.SelectedLocomotive != null) //проверяем, удалось ли нам загрузить объект
if (obj + form.SelectedLocomotive > -1)
{ {
MessageBox.Show("Объект добавлен"); MessageBox.Show("Объект добавлен");
CollectionPictureBox.Image = _locos.ShowLocomotives(); CollectionPictureBox.Image = obj.ShowLocomotives();
} }
else else
{ {
@ -38,17 +102,24 @@ namespace ElectricLocomotive
} }
} }
private void deleteLoco_Click(object sender, EventArgs e) private void ButtonRemoveLocomotive_Click(object sender, EventArgs e)
{ {
if (listBoxStorage.SelectedIndex == -1) return;
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{ {
return; return;
} }
int pos = Convert.ToInt32(Input.Text); int pos = Convert.ToInt32(Input.Text);
if (_locos - pos != null) if (obj - pos != null)
{ {
MessageBox.Show("Объект удален"); MessageBox.Show("Объект удален");
CollectionPictureBox.Image = _locos.ShowLocomotives(); CollectionPictureBox.Image = obj.ShowLocomotives();
} }
else else
{ {
@ -56,10 +127,15 @@ namespace ElectricLocomotive
} }
} }
private void refreshCollection_Click(object sender, EventArgs e) private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{ {
CollectionPictureBox.Image = _locos.ShowLocomotives(); if (listBoxStorage.SelectedIndex == -1) return;
var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
CollectionPictureBox.Image = obj.ShowLocomotives();
} }
} }
} }

View File

@ -0,0 +1,82 @@
using ElectricLocomotive.DrawningObject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectricLocomotive.Generics
{
internal class LocomotiveGenericStorage
{
/// <summary>
/// Словарь (хранилище)
/// </summary>
readonly Dictionary<string, LocomotivesGenericCollection<DrawningLocomotive, DrawningObjectLocomotive>> _locomotivesStorage;
/// <summary>
/// Возвращение списка названий наборов
/// </summary>
public List<string> Keys => _locomotivesStorage.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="pictureWidth"></param>
/// <param name="pictureHeight"></param>
public LocomotiveGenericStorage(int pictureWidth, int pictureHeight)
{
_locomotivesStorage = new Dictionary<string, LocomotivesGenericCollection<DrawningLocomotive, DrawningObjectLocomotive>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// <summary>
/// Добавление набора
/// </summary>
/// <param name="name">Название набора</param>
public void AddSet(string name)
{
if (!_locomotivesStorage.ContainsKey(name))
{
_locomotivesStorage.Add(name, new LocomotivesGenericCollection<DrawningLocomotive, DrawningObjectLocomotive>(_pictureWidth, _pictureHeight));
}
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="name">Название набора</param>
public void DelSet(string name)
{
if (_locomotivesStorage.ContainsKey(name))
{
_locomotivesStorage.Remove(name);
}
}
/// <summary>
/// Доступ к набору
/// </summary>
/// <param name="ind"></param>
/// <returns></returns>
public LocomotivesGenericCollection<DrawningLocomotive, DrawningObjectLocomotive>?
this[string ind]
{
get
{
// TODO Продумать логику получения набора
if (_locomotivesStorage.ContainsKey(ind))
{
return _locomotivesStorage[ind];
}
return null;
}
}
}
}

View File

@ -17,6 +17,7 @@ namespace ElectricLocomotive.Generics
private readonly int _pictureHeight; private readonly int _pictureHeight;
private readonly int _placeSizeWidth = 200; private readonly int _placeSizeWidth = 200;
private readonly int _placeSizeHeight = 90; private readonly int _placeSizeHeight = 90;
private readonly SetGeneric<T> _collection; private readonly SetGeneric<T> _collection;
public LocomotivesGenericCollection(int picWidth, int picHeight) public LocomotivesGenericCollection(int picWidth, int picHeight)
{ {
@ -26,24 +27,29 @@ namespace ElectricLocomotive.Generics
_pictureHeight = picHeight; _pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height); _collection = new SetGeneric<T>(width * height);
} }
public static int operator +(LocomotivesGenericCollection<T, U> collect, T? locomotive) public static int operator +(LocomotivesGenericCollection<T, U> collect, T? loco)
{ {
if (locomotive == null) if (loco == null)
{ {
return -1; return -1;
} }
return collect?._collection.Insert(locomotive) ?? -1; return collect._collection.Insert(loco);
} }
public static bool operator -(LocomotivesGenericCollection<T, U> collect, int pos)
/// Перегрузка оператора вычитания
public static T? operator -(LocomotivesGenericCollection<T, U> collect, int pos)
{ {
T? locomotive = collect._collection.Get(pos); T? obj = collect._collection[pos];
if (locomotive != null) if (obj != null)
return collect._collection.Remove(pos); {
return false; collect._collection.Remove(pos);
}
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 ShowLocomotives() public Bitmap ShowLocomotives()
{ {
@ -58,30 +64,26 @@ namespace ElectricLocomotive.Generics
Pen pen = new(Color.Black, 3); Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{ {
for (int j = 0; j < _pictureHeight / _placeSizeHeight + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
1; ++j)
{ {
g.DrawLine(pen, i * _placeSizeWidth, j * //линия рамзетки места
_placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth / 2, j * _placeSizeHeight);
_placeSizeHeight);
} }
g.DrawLine(pen, i * _placeSizeWidth, 0, i * g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
_placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
} }
} }
private void DrawObjects(Graphics g) private void DrawObjects(Graphics g)
{ {
int HLoco = _pictureHeight / _placeSizeHeight; int HeightObjCount = _pictureHeight / _placeSizeHeight;
int Wloco = _pictureWidth / _placeSizeWidth; int WidthObjCount = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _collection.Count; i++) for (int i = 0; i < _collection.Count; i++)
{ {
T? type = _collection.Get(i); T? type = _collection[i];
if (type != null) if (type != null)
{ {
type.SetPosition( type.SetPosition(
(int)(i / HLoco * _placeSizeWidth), (int)(i / HeightObjCount * _placeSizeWidth),
(HLoco - 1) * _placeSizeHeight - (int)(i % HLoco * _placeSizeHeight) (HeightObjCount - 1) * _placeSizeHeight - (int)(i % HeightObjCount * _placeSizeHeight));
);
type?.DrawTransport(g); type?.DrawTransport(g);
} }
} }

View File

@ -6,60 +6,60 @@ using System.Threading.Tasks;
namespace ProjectElectricLocomotive.Generics namespace ProjectElectricLocomotive.Generics
{ {
internal class SetGeneric<T> internal class SetGeneric<T> where T : class
where T : class
{ {
private readonly T?[] _places; private readonly List<T?> _places;
public int Count => _places.Length; public int Count => _places.Count;
/// Максимальное количество объектов в списке
private readonly int _maxCount;
public SetGeneric(int count) public SetGeneric(int count)
{ {
_places = new T?[count]; _maxCount = count;
_places = new List<T?>(count);
} }
public int Insert(T locomotive) public int Insert(T loco)
{ {
return Insert(locomotive, 0); return Insert(loco, 0);
} }
public int Insert(T locomotive, int position)
public int Insert(T loco, int position)
{ {
int Full = 0; if (position < 0 || position >= _maxCount) return -1;
int temp = 0; _places.Insert(position, loco);
for (int i = position; i < Count; i++) return position;
{
if (_places[i] != null) Full++;
}
if (Full == Count - position - 1)
return -1;
if (position < Count && position >= 0)
{
for (int j = position; j < Count; j++)
{
if (_places[j] == null)
{
temp = j;
break;
}
}
for (int i = temp; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = locomotive;
return position;
}
return -1;
} }
public bool Remove(int position) public T? Remove(int position)
{ {
if (position >= Count || position < 0) if (position >= Count || position < 0)
if (!(position >= 0 && position < Count) || _places[position] == null) return null;
return false;
T? tmp = _places[position];
_places[position] = null; _places[position] = null;
return true; return tmp;
} }
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 >= Count) return null;
return _places[position];
}
set
{
if (position < 0 || position >= Count || Count == _maxCount) return;
_places.Insert(position, value);
}
}
public IEnumerable<T?> GetLocomotives(int? maxLocos = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxLocos.HasValue && i == maxLocos.Value)
{
yield break;
}
}
} }
} }
} }