This commit is contained in:
kirin 2023-10-31 13:25:13 +04:00
parent e787a6d765
commit 3be2467d2f
5 changed files with 246 additions and 61 deletions

View File

@ -28,20 +28,27 @@ partial class FormLocomotivCollection {
/// </summary>
private void InitializeComponent() {
toolsBox = new GroupBox();
collectionGroupBoxes = new GroupBox();
storageListBox = new ListBox();
delStorageButton = new Button();
addStorageButton = new Button();
storageIndexInput = new TextBox();
refreshCollection = new Button();
deleteLoco = new Button();
input = new TextBox();
locoIndexInput = new TextBox();
addLocomotiv = new Button();
collectionPictureBox = new PictureBox();
toolsBox.SuspendLayout();
collectionGroupBoxes.SuspendLayout();
((ISupportInitialize)collectionPictureBox).BeginInit();
SuspendLayout();
//
// toolsBox
//
toolsBox.Controls.Add(collectionGroupBoxes);
toolsBox.Controls.Add(refreshCollection);
toolsBox.Controls.Add(deleteLoco);
toolsBox.Controls.Add(input);
toolsBox.Controls.Add(locoIndexInput);
toolsBox.Controls.Add(addLocomotiv);
toolsBox.Location = new Point(944, 12);
toolsBox.Name = "toolsBox";
@ -50,9 +57,59 @@ partial class FormLocomotivCollection {
toolsBox.TabStop = false;
toolsBox.Text = "Инструменты";
//
// collectionGroupBoxes
//
collectionGroupBoxes.Controls.Add(storageListBox);
collectionGroupBoxes.Controls.Add(delStorageButton);
collectionGroupBoxes.Controls.Add(addStorageButton);
collectionGroupBoxes.Controls.Add(storageIndexInput);
collectionGroupBoxes.Location = new Point(7, 38);
collectionGroupBoxes.Name = "collectionGroupBoxes";
collectionGroupBoxes.Size = new Size(296, 325);
collectionGroupBoxes.TabIndex = 5;
collectionGroupBoxes.TabStop = false;
collectionGroupBoxes.Text = "Наборы";
//
// storageListBox
//
storageListBox.FormattingEnabled = true;
storageListBox.ItemHeight = 20;
storageListBox.Location = new Point(14, 139);
storageListBox.Name = "storageListBox";
storageListBox.Size = new Size(271, 124);
storageListBox.TabIndex = 7;
storageListBox.SelectedIndexChanged += storagesListBox_SelectedIndexChanged;
//
// delStorageButton
//
delStorageButton.Location = new Point(14, 278);
delStorageButton.Name = "delStorageButton";
delStorageButton.Size = new Size(271, 32);
delStorageButton.TabIndex = 6;
delStorageButton.Text = "Удалить набор";
delStorageButton.UseVisualStyleBackColor = true;
delStorageButton.Click += delStorageButton_Click;
//
// addStorageButton
//
addStorageButton.Location = new Point(14, 87);
addStorageButton.Name = "addStorageButton";
addStorageButton.Size = new Size(271, 31);
addStorageButton.TabIndex = 5;
addStorageButton.Text = "Добавить набор";
addStorageButton.UseVisualStyleBackColor = true;
addStorageButton.Click += addStorageButton_Click;
//
// storageIndexInput
//
storageIndexInput.Location = new Point(14, 41);
storageIndexInput.Name = "storageIndexInput";
storageIndexInput.Size = new Size(271, 27);
storageIndexInput.TabIndex = 4;
//
// refreshCollection
//
refreshCollection.Location = new Point(19, 324);
refreshCollection.Location = new Point(21, 642);
refreshCollection.Name = "refreshCollection";
refreshCollection.Size = new Size(271, 59);
refreshCollection.TabIndex = 3;
@ -62,7 +119,7 @@ partial class FormLocomotivCollection {
//
// deleteLoco
//
deleteLoco.Location = new Point(21, 199);
deleteLoco.Location = new Point(21, 559);
deleteLoco.Name = "deleteLoco";
deleteLoco.Size = new Size(271, 59);
deleteLoco.TabIndex = 2;
@ -70,16 +127,16 @@ partial class FormLocomotivCollection {
deleteLoco.UseVisualStyleBackColor = true;
deleteLoco.Click += deleteLoco_Click;
//
// input
// locoIndexInput
//
input.Location = new Point(51, 152);
input.Name = "input";
input.Size = new Size(214, 27);
input.TabIndex = 1;
locoIndexInput.Location = new Point(53, 512);
locoIndexInput.Name = "locoIndexInput";
locoIndexInput.Size = new Size(214, 27);
locoIndexInput.TabIndex = 1;
//
// addLocomotiv
//
addLocomotiv.Location = new Point(21, 40);
addLocomotiv.Location = new Point(21, 431);
addLocomotiv.Name = "addLocomotiv";
addLocomotiv.Size = new Size(271, 59);
addLocomotiv.TabIndex = 0;
@ -106,6 +163,8 @@ partial class FormLocomotivCollection {
Text = "Набор локомотивов";
toolsBox.ResumeLayout(false);
toolsBox.PerformLayout();
collectionGroupBoxes.ResumeLayout(false);
collectionGroupBoxes.PerformLayout();
((ISupportInitialize)collectionPictureBox).EndInit();
ResumeLayout(false);
}
@ -113,9 +172,14 @@ partial class FormLocomotivCollection {
#endregion
private GroupBox toolsBox;
private TextBox input;
private TextBox locoIndexInput;
private Button addLocomotiv;
private PictureBox collectionPictureBox;
private Button refreshCollection;
private Button deleteLoco;
private GroupBox collectionGroupBoxes;
private Button delStorageButton;
private Button addStorageButton;
private TextBox storageIndexInput;
private ListBox storageListBox;
}

View File

@ -1,33 +1,64 @@
namespace ElectricLocomotive;
public partial class FormLocomotivCollection : Form {
private readonly LocosGenericCollection<DrawingLocomotiv, DrawingObjectLocomotiv> _locos;
private readonly LocosGenericStorage _storage;
public FormLocomotivCollection() {
InitializeComponent();
_locos = new(collectionPictureBox.Width, collectionPictureBox.Height);
_storage = new(collectionPictureBox.Width, collectionPictureBox.Height);
}
private void ReloadObjects() {
int index = storageListBox.SelectedIndex;
storageListBox.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++) {
storageListBox.Items.Add(_storage.Keys[i]);
}
if (storageListBox.Items.Count > 0 && (index == -1 || index
>= storageListBox.Items.Count)) {
storageListBox.SelectedIndex = 0;
}
else if (storageListBox.Items.Count > 0 && index > -1 &&
index < storageListBox.Items.Count) {
storageListBox.SelectedIndex = index;
}
}
private void addLocomotiv_Click(object sender, EventArgs e) {
if (storageListBox.SelectedIndex == -1) {
return;
}
var obj = _storage[storageListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null) {
return;
}
FormLocomotiv form = new();
if (form.ShowDialog() == DialogResult.OK) {
if (_locos + form.SelectedLocomotiv != null) {
if (obj + form.SelectedLocomotiv) {
MessageBox.Show("Объект добавлен");
collectionPictureBox.Image = _locos.ShowLocos();
collectionPictureBox.Image = obj.ShowLocos();
}
else {
MessageBox.Show("Не удалось добавить объект");
}
}
}
}
}
private void deleteLoco_Click(object sender, EventArgs e) {
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) {
if (storageListBox.SelectedIndex == -1) {
return;
}
int pos = Convert.ToInt32(input.Text);
if (_locos - pos != null) {
var obj = _storage[storageListBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null) {
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) {
return;
}
int pos = Convert.ToInt32(locoIndexInput.Text);
if (obj - pos != null) {
MessageBox.Show("Объект удален");
collectionPictureBox.Image = _locos.ShowLocos();
collectionPictureBox.Image = obj.ShowLocos();
}
else {
MessageBox.Show("Не удалось удалить объект");
@ -35,8 +66,41 @@ public partial class FormLocomotivCollection : Form {
}
private void refreshCollection_Click(object sender, EventArgs e) {
collectionPictureBox.Image = _locos.ShowLocos();
if (storageListBox.SelectedIndex == -1) {
return;
}
var obj = _storage[storageListBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null) {
return;
}
collectionPictureBox.Image = obj.ShowLocos();
}
private void storagesListBox_SelectedIndexChanged(object sender, EventArgs e) {
collectionPictureBox.Image =
_storage[storageListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowLocos();
}
private void delStorageButton_Click(object sender, EventArgs e) {
if (storageListBox.SelectedIndex == -1) {
return;
}
if (MessageBox.Show($"Удалить объект{storageListBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes) {
_storage.DelSet(storageListBox.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
}
private void addStorageButton_Click(object sender, EventArgs e) {
if (string.IsNullOrEmpty(storageIndexInput.Text)) {
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(storageIndexInput.Text);
ReloadObjects();
}
}

View File

@ -15,27 +15,27 @@ public class LocosGenericCollection <T, U> where T : DrawingLocomotiv where U :
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
public static int operator +(LocosGenericCollection<T, U> collect, T?
public static bool operator +(LocosGenericCollection<T, U> collect, T?
obj)
{
if (obj == null) return -1;
if (obj == null) return false;
return collect?._collection.Insert(obj) ?? -1;
return collect?._collection.Insert(obj) ?? false;
}
public static bool operator -(LocosGenericCollection<T, U> collect, int
public static T? operator -(LocosGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
return collect._collection.Remove(pos);
collect._collection.Remove(pos);
}
return false;
return obj;
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
public Bitmap ShowLocos()
{
@ -63,13 +63,16 @@ public class LocosGenericCollection <T, U> where T : DrawingLocomotiv where U :
}
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++) {
DrawingLocomotiv locomotiv = _collection.Get(i);
if (locomotiv != null) {
int i = 0;
foreach(var electricLoco in _collection.GetElectricLocos())
{
if (electricLoco != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
locomotiv.SetPosition(i % inRow * _placeSizeWidth, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight);
locomotiv.DrawLoco(g);
electricLoco.SetPosition(i % inRow * _placeSizeWidth, _pictureHeight - _pictureHeight % _placeSizeHeight - (i / inRow + 1) * _placeSizeHeight);
electricLoco.DrawLoco(g);
}
i++;
}
}

View File

@ -0,0 +1,41 @@
namespace ElectricLocomotive;
public class LocosGenericStorage
{
readonly Dictionary<string, LocosGenericCollection<DrawingLocomotiv, DrawingObjectLocomotiv>> _electricLocoStorages;
public List<string> Keys => _electricLocoStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public LocosGenericStorage(int pictureWidth, int pictureHeight)
{
_electricLocoStorages = new Dictionary<string,
LocosGenericCollection<DrawingLocomotiv, DrawingObjectLocomotiv>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
_electricLocoStorages.Add(name, new LocosGenericCollection<DrawingLocomotiv, DrawingObjectLocomotiv> (_pictureWidth, _pictureHeight));
}
public void DelSet(string name)
{
if (!_electricLocoStorages.ContainsKey(name))
return;
_electricLocoStorages.Remove(name);
}
public LocosGenericCollection<DrawingLocomotiv, DrawingObjectLocomotiv>? this[string ind]
{
get
{
if (_electricLocoStorages.ContainsKey(ind))
return _electricLocoStorages[ind];
return null;
}
}
}

View File

@ -2,51 +2,64 @@
public class SetGeneric <T> 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)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T>(count);
}
public int Insert(T electricLocomotiv)
public bool Insert(T electricLocomotiv)
{
if (_places[Count - 1] != null)
return -1;
return Insert(electricLocomotiv, 0);
if (_places.Count == _maxCount)
return false;
Insert(electricLocomotiv, 0);
return true;
}
public int Insert(T electricLocomotiv, int position)
public bool Insert(T electricLocomotiv, int position)
{
if (!(position >= 0 && position < Count))
return -1;
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] = electricLocomotiv;
return position;
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
return false;
_places.Insert(position, electricLocomotiv);
return true;
}
public bool Remove(int position)
{
if (!(position >= 0 && position < Count) || _places[position] == null)
if (!(position >= 0 && position < Count))
return false;
_places[position] = null;
_places.RemoveAt(position);
return true;
}
public T? Get(int position)
public T? this[int position]
{
get {
if (!(position >= 0 && position < Count))
return null;
return _places[position];
}
set {
if (!(position >= 0 && position < Count && _places.Count < _maxCount))
return;
_places.Insert(position, value);
return;
}
}
public IEnumerable<T?> GetElectricLocos(int? maxElectricLocos = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxElectricLocos.HasValue && i == maxElectricLocos.Value)
{
yield break;
}
}
}
}