зафиксировать всё

This commit is contained in:
Учебный
2023-10-30 10:01:38 +04:00
parent bad8d47c56
commit 29068866e1
5 changed files with 302 additions and 134 deletions

View File

@@ -29,11 +29,14 @@
private void InitializeComponent()
{
pictureBoxCollection = new PictureBox();
buttonAddBoat_Click = new Button();
buttonRemoveBoat_Click = new Button();
buttonRefreshCollection_Click = new Button();
textBox1 = new TextBox();
maskedTextBoxNumber = new MaskedTextBox();
listBoxStorages = new ListBox();
ButtonAddObject = new Button();
textBoxStorageName = new TextBox();
ButtonDelObject = new Button();
ButtonAddBoat = new Button();
ButtonRemoveBoat = new Button();
ButtonRefreshCollection = new Button();
textBoxBoat = new TextBox();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
@@ -41,69 +44,102 @@
//
pictureBoxCollection.Location = new Point(0, 0);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(1176, 647);
pictureBoxCollection.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBoxCollection.Size = new Size(929, 647);
pictureBoxCollection.TabIndex = 0;
pictureBoxCollection.TabStop = false;
//
// buttonAddBoat_Click
// listBoxStorages
//
buttonAddBoat_Click.Location = new Point(955, 73);
buttonAddBoat_Click.Name = "buttonAddBoat_Click";
buttonAddBoat_Click.Size = new Size(201, 47);
buttonAddBoat_Click.TabIndex = 1;
buttonAddBoat_Click.Text = "Добавить лодку";
buttonAddBoat_Click.UseVisualStyleBackColor = true;
buttonAddBoat_Click.Click += buttonAddBoat_Click_Click;
listBoxStorages.FormattingEnabled = true;
listBoxStorages.ItemHeight = 25;
listBoxStorages.Location = new Point(935, 146);
listBoxStorages.Name = "listBoxStorages";
listBoxStorages.Size = new Size(231, 129);
listBoxStorages.TabIndex = 1;
listBoxStorages.SelectedIndexChanged += listBoxStorages_SelectedIndexChanged;
//
// buttonRemoveBoat_Click
// ButtonAddObject
//
buttonRemoveBoat_Click.Location = new Point(955, 345);
buttonRemoveBoat_Click.Name = "buttonRemoveBoat_Click";
buttonRemoveBoat_Click.Size = new Size(201, 46);
buttonRemoveBoat_Click.TabIndex = 2;
buttonRemoveBoat_Click.Text = "Удалить лодку";
buttonRemoveBoat_Click.UseVisualStyleBackColor = true;
buttonRemoveBoat_Click.Click += buttonRemoveBoat_Click_Click;
ButtonAddObject.BackColor = SystemColors.ButtonHighlight;
ButtonAddObject.Location = new Point(935, 102);
ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(231, 38);
ButtonAddObject.TabIndex = 2;
ButtonAddObject.Text = "Добавить в набор";
ButtonAddObject.UseVisualStyleBackColor = false;
ButtonAddObject.Click += ButtonAddObject_Click_1;
//
// buttonRefreshCollection_Click
// textBoxStorageName
//
buttonRefreshCollection_Click.Location = new Point(955, 421);
buttonRefreshCollection_Click.Name = "buttonRefreshCollection_Click";
buttonRefreshCollection_Click.Size = new Size(201, 50);
buttonRefreshCollection_Click.TabIndex = 3;
buttonRefreshCollection_Click.Text = "Обновить коллекцию";
buttonRefreshCollection_Click.UseVisualStyleBackColor = true;
buttonRefreshCollection_Click.Click += buttonRefreshCollection_Click_Click;
textBoxStorageName.Location = new Point(935, 65);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(231, 31);
textBoxStorageName.TabIndex = 3;
//
// textBox1
// ButtonDelObject
//
textBox1.BackColor = SystemColors.MenuBar;
textBox1.Location = new Point(955, 12);
textBox1.Name = "textBox1";
textBox1.Size = new Size(150, 31);
textBox1.TabIndex = 4;
textBox1.Text = "Инструменты";
textBox1.TextAlign = HorizontalAlignment.Center;
ButtonDelObject.BackColor = SystemColors.ButtonHighlight;
ButtonDelObject.Location = new Point(935, 281);
ButtonDelObject.Name = "ButtonDelObject";
ButtonDelObject.Size = new Size(231, 34);
ButtonDelObject.TabIndex = 4;
ButtonDelObject.Text = "Удалить набор";
ButtonDelObject.UseVisualStyleBackColor = false;
ButtonDelObject.Click += ButtonDelObject_Click;
//
// maskedTextBoxNumber
// ButtonAddBoat
//
maskedTextBoxNumber.Location = new Point(979, 293);
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(150, 31);
maskedTextBoxNumber.TabIndex = 5;
maskedTextBoxNumber.Text = "_";
ButtonAddBoat.BackColor = SystemColors.ButtonHighlight;
ButtonAddBoat.Location = new Point(935, 401);
ButtonAddBoat.Name = "ButtonAddBoat";
ButtonAddBoat.Size = new Size(231, 34);
ButtonAddBoat.TabIndex = 5;
ButtonAddBoat.Text = "Добавить лодку";
ButtonAddBoat.UseVisualStyleBackColor = false;
ButtonAddBoat.Click += ButtonAddBoat_Click;
//
// ButtonRemoveBoat
//
ButtonRemoveBoat.BackColor = SystemColors.ButtonHighlight;
ButtonRemoveBoat.Location = new Point(935, 479);
ButtonRemoveBoat.Name = "ButtonRemoveBoat";
ButtonRemoveBoat.Size = new Size(231, 34);
ButtonRemoveBoat.TabIndex = 6;
ButtonRemoveBoat.Text = "Удалить лодку";
ButtonRemoveBoat.UseVisualStyleBackColor = false;
ButtonRemoveBoat.Click += ButtonRemoveBoat_Click;
//
// ButtonRefreshCollection
//
ButtonRefreshCollection.BackColor = SystemColors.ButtonHighlight;
ButtonRefreshCollection.Location = new Point(935, 575);
ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(231, 34);
ButtonRefreshCollection.TabIndex = 7;
ButtonRefreshCollection.Text = "Обновить коллекцию";
ButtonRefreshCollection.UseVisualStyleBackColor = false;
ButtonRefreshCollection.Click += ButtonRefreshCollection_Click;
//
// textBoxBoat
//
textBoxBoat.Location = new Point(958, 441);
textBoxBoat.Name = "textBoxBoat";
textBoxBoat.Size = new Size(173, 31);
textBoxBoat.TabIndex = 8;
//
// FormBoatCollection
//
AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1178, 644);
Controls.Add(maskedTextBoxNumber);
Controls.Add(textBox1);
Controls.Add(buttonRefreshCollection_Click);
Controls.Add(buttonRemoveBoat_Click);
Controls.Add(buttonAddBoat_Click);
Controls.Add(textBoxBoat);
Controls.Add(ButtonRefreshCollection);
Controls.Add(ButtonRemoveBoat);
Controls.Add(ButtonAddBoat);
Controls.Add(ButtonDelObject);
Controls.Add(textBoxStorageName);
Controls.Add(ButtonAddObject);
Controls.Add(listBoxStorages);
Controls.Add(pictureBoxCollection);
Name = "FormBoatCollection";
Text = "FormBoatCollection";
@@ -115,10 +151,13 @@
#endregion
private PictureBox pictureBoxCollection;
private Button buttonAddBoat_Click;
private Button buttonRemoveBoat_Click;
private Button buttonRefreshCollection_Click;
private TextBox textBox1;
private MaskedTextBox maskedTextBoxNumber;
private ListBox listBoxStorages;
private Button ButtonAddObject;
private TextBox textBoxStorageName;
private Button ButtonDelObject;
private Button ButtonAddBoat;
private Button ButtonRemoveBoat;
private Button ButtonRefreshCollection;
private TextBox textBoxBoat;
}
}

View File

@@ -17,25 +17,84 @@ namespace ProjectBoat_bae
{
public partial class FormBoatCollection : Form
{
private readonly BoatsGenericCollection<Drawningboat, DrawningObjectBoat> _boats;
// Набор объектов
private readonly BoatsGenericStorage _storage;
public FormBoatCollection()
{
InitializeComponent();
_boats = new BoatsGenericCollection<Drawningboat, DrawningObjectBoat>(pictureBoxCollection.Width, pictureBoxCollection.Height);
_storage = new BoatsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
// Заполнение listBoxObjects
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_1(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxStorageName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(textBoxStorageName.Text);
ReloadObjects();
}
// Удаление набора
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 buttonAddBoat_Click_Click(object sender, EventArgs e)
private void ButtonAddBoat_Click(object sender, EventArgs e)
{
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormBoat form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_boats + form.SelectedBoat != -1)
if (obj + form.SelectedBoat)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _boats.ShowBoats();
pictureBoxCollection.Image = obj.ShowBoats();
}
else
{
@@ -44,45 +103,59 @@ namespace ProjectBoat_bae
}
}
// Удаление объекта из набора
private void buttonRemoveBoat_Click_Click(object sender, EventArgs e)
//удаление объекта
private void ButtonRemoveBoat_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
//int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
//if (_boats - pos != null)
//{
// MessageBox.Show("Объект удален");
// pictureBoxCollection.Image = _boats.ShowBoats();
//}
//else
//{
// MessageBox.Show("Не удалось удалить объект");
//}
int pos = -1;
try
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
pos = Convert.ToInt32(maskedTextBoxNumber.Text);
return;
}
catch (Exception ex) { }
if (_boats - pos)
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = 0;
if (textBoxBoat != null)
pos = Convert.ToInt32(textBoxBoat.Text);
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _boats.ShowBoats();
pictureBoxCollection.Image = obj.ShowBoats();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void buttonRefreshCollection_Click_Click(object sender, EventArgs e)
// Обновление
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _boats.ShowBoats();
if (listBoxStorages.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowBoats();
}
//Выбор набора
private void listBoxStorages_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowBoats();
}
}
}

View File

@@ -20,10 +20,10 @@ namespace ProjectBoat_bae.Generics
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private readonly int _placeSizeWidth = 160;
private readonly int _placeSizeWidth = 100;
// Размер занимаемого объектом места (высота)
private readonly int _placeSizeHeight = 160;
private readonly int _placeSizeHeight = 90;
// Набор объектов
private readonly SetGeneric<T> _collection;
@@ -39,32 +39,31 @@ namespace ProjectBoat_bae.Generics
}
// Перегрузка оператора сложения
public static int operator +(BoatsGenericCollection<T, U> collect, T?
obj)
public static bool operator +(BoatsGenericCollection<T, U> collect, T obj)
{
if (obj == null)
{
return -1;
return false;
}
return collect._collection.Insert(obj);
return (bool)collect._collection.Insert(obj);
}
// Перегрузка оператора вычитания
public static bool operator -(BoatsGenericCollection<T, U> collect, int
pos)
public static T? operator -(BoatsGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
return false;
return obj;
}
// Получение объекта IMoveableObject
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
// Вывод всего набора объектов
@@ -100,13 +99,12 @@ namespace ProjectBoat_bae.Generics
{
for (int i = 0; i < _collection.Count; i++)
{
Drawningboat boat = _collection.Get(i);
if (boat != null)
T? t = _collection[i];
if (t != null)
{
int width = _pictureWidth / _placeSizeWidth;
boat.SetPosition(i % width * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
boat.DrawTransport(g);
t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (t is Drawningboat) (t as Drawningboat).DrawTransport(g);
else t.DrawTransport(g);
}
}
}

View File

@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectBoat_bae.DrawningObjects;
using ProjectBoat_bae.MovementStrategy;
namespace ProjectBoat_bae.Generics
{
internal class BoatsGenericStorage
{
readonly Dictionary<string, BoatsGenericCollection<Drawningboat, DrawningObjectBoat>> _boatStorages;
public List<string> Keys => _boatStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public BoatsGenericStorage(int pictureWidth, int pictureHeight)
{
_boatStorages = new Dictionary<string, BoatsGenericCollection<Drawningboat, DrawningObjectBoat>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
if (_boatStorages.ContainsKey(name)) return;
_boatStorages[name] = new BoatsGenericCollection<Drawningboat, DrawningObjectBoat>(_pictureWidth, _pictureHeight);
}
public void DelSet(string name)
{
if (!_boatStorages.ContainsKey(name)) return;
_boatStorages.Remove(name);
}
public BoatsGenericCollection<Drawningboat, DrawningObjectBoat>?
this[string ind]
{
get
{
if (_boatStorages.ContainsKey(ind)) return _boatStorages[ind];
return null;
}
}
}
}

View File

@@ -8,58 +8,69 @@ namespace ProjectBoat_bae.Generics
{
internal class SetGeneric<T> where T : class
{
private readonly T?[] _places;
private readonly List<T?> _places;
public int Count => _places.Count;
public int Count => _places.Length;
// Максимальное количество объектов в списке
private readonly int _maxCount;
// Конструктор
public SetGeneric(int count)
{
_places = new T?[count];
_maxCount = count;
_places = new List<T?>(_maxCount);
}
public int Insert(T boat)
public bool Insert(T boat)
{
if (_places[Count - 1] != null)
return -1;
return Insert(boat, 0);
}
public int Insert(T boat, int position)
public bool Insert(T boat, 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];
}
if (position < 0 || position >= _maxCount)
return false;
}
_places[position] = boat;
return position;
if (Count >= _maxCount)
return false;
_places.Insert(0, boat);
return true;
}
public bool Remove(int position)
{
if (position < 0 || position >= _places.Count())
if (position < 0 || position > _maxCount)
return false;
_places[position] = null;
if (position >= Count)
return false;
_places.RemoveAt(position);
return true;
}
public T? Get(int position)
public T? this[int position]
{
if (position < 0 || position >= _places.Count())
return null;
return _places[position];
get
{
if (position < 0 || position > _maxCount)
return null;
return _places[position];
}
set
{
if (position < 0 || position > _maxCount)
return;
_places[position] = value;
}
}
public IEnumerable<T?> GetBoats(int? maxBoats = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxBoats.HasValue && i == maxBoats.Value)
{
yield break;
}
}
}
}