PIbd-22. Fedorenko G.Y. Lab Work 4 #5

Closed
fedorenko wants to merge 1 commits from lab4 into lab3
5 changed files with 336 additions and 93 deletions

View File

@ -29,33 +29,104 @@
private void InitializeComponent()
{
panel1 = new Panel();
panel2 = new Panel();
DeleteCollectButton = new Button();
CollectionListBox = new ListBox();
AddCollectButton = new Button();
SetTextBox = new TextBox();
label2 = new Label();
UpdateButton = new Button();
DeleteButton = new Button();
AddButton = new Button();
InputNum = new TextBox();
PlaneTextBox = new TextBox();
label1 = new Label();
DrawPlane = new PictureBox();
panel1.SuspendLayout();
panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)DrawPlane).BeginInit();
SuspendLayout();
//
// panel1
//
panel1.Controls.Add(panel2);
panel1.Controls.Add(UpdateButton);
panel1.Controls.Add(DeleteButton);
panel1.Controls.Add(AddButton);
panel1.Controls.Add(InputNum);
panel1.Controls.Add(PlaneTextBox);
panel1.Controls.Add(label1);
panel1.Dock = DockStyle.Right;
panel1.Location = new Point(577, 0);
panel1.Location = new Point(646, 0);
panel1.Margin = new Padding(3, 2, 3, 2);
panel1.Name = "panel1";
panel1.Size = new Size(219, 338);
panel1.Size = new Size(219, 386);
panel1.TabIndex = 0;
//
// panel2
//
panel2.Controls.Add(DeleteCollectButton);
panel2.Controls.Add(CollectionListBox);
panel2.Controls.Add(AddCollectButton);
panel2.Controls.Add(SetTextBox);
panel2.Controls.Add(label2);
panel2.Location = new Point(14, 28);
panel2.Margin = new Padding(3, 2, 3, 2);
panel2.Name = "panel2";
panel2.Size = new Size(187, 165);
panel2.TabIndex = 5;
//
// DeleteCollectButton
//
DeleteCollectButton.Location = new Point(3, 136);
DeleteCollectButton.Margin = new Padding(3, 2, 3, 2);
DeleteCollectButton.Name = "DeleteCollectButton";
DeleteCollectButton.Size = new Size(182, 22);
DeleteCollectButton.TabIndex = 4;
DeleteCollectButton.Text = "Удалить набор";
DeleteCollectButton.UseVisualStyleBackColor = true;
DeleteCollectButton.Click += ButtonDelObject_Click;
//
// CollectionListBox
//
CollectionListBox.FormattingEnabled = true;
CollectionListBox.ItemHeight = 15;
CollectionListBox.Location = new Point(3, 68);
CollectionListBox.Margin = new Padding(3, 2, 3, 2);
CollectionListBox.Name = "CollectionListBox";
CollectionListBox.Size = new Size(182, 64);
CollectionListBox.TabIndex = 3;
CollectionListBox.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
//
// AddCollectButton
//
AddCollectButton.Location = new Point(3, 42);
AddCollectButton.Margin = new Padding(3, 2, 3, 2);
AddCollectButton.Name = "AddCollectButton";
AddCollectButton.Size = new Size(182, 22);
AddCollectButton.TabIndex = 2;
AddCollectButton.Text = "Добавить набор";
AddCollectButton.UseVisualStyleBackColor = true;
AddCollectButton.Click += ButtonAddObject_Click;
//
// SetTextBox
//
SetTextBox.Location = new Point(2, 17);
SetTextBox.Margin = new Padding(3, 2, 3, 2);
SetTextBox.Name = "SetTextBox";
SetTextBox.Size = new Size(183, 23);
SetTextBox.TabIndex = 1;
//
// label2
//
label2.AutoSize = true;
label2.Location = new Point(3, 0);
label2.Name = "label2";
label2.Size = new Size(52, 15);
label2.TabIndex = 0;
label2.Text = "Наборы";
//
// UpdateButton
//
UpdateButton.Location = new Point(9, 181);
UpdateButton.Location = new Point(9, 301);
UpdateButton.Margin = new Padding(3, 2, 3, 2);
UpdateButton.Name = "UpdateButton";
UpdateButton.Size = new Size(200, 28);
@ -66,33 +137,33 @@
//
// DeleteButton
//
DeleteButton.Location = new Point(9, 132);
DeleteButton.Location = new Point(9, 268);
DeleteButton.Margin = new Padding(3, 2, 3, 2);
DeleteButton.Name = "DeleteButton";
DeleteButton.Size = new Size(200, 28);
DeleteButton.TabIndex = 3;
DeleteButton.Text = "Удалить самолёт";
DeleteButton.UseVisualStyleBackColor = true;
DeleteButton.Click += ButtonRemoveCar_Click;
DeleteButton.Click += ButtonRemovePlane_Click;
//
// AddButton
//
AddButton.Location = new Point(9, 40);
AddButton.Location = new Point(9, 212);
AddButton.Margin = new Padding(3, 2, 3, 2);
AddButton.Name = "AddButton";
AddButton.Size = new Size(200, 28);
AddButton.TabIndex = 2;
AddButton.Text = "Добавить самолёт";
AddButton.UseVisualStyleBackColor = true;
AddButton.Click += ButtonAddTank_Click;
AddButton.Click += ButtonAddPlane_Click;
//
// InputNum
// PlaneTextBox
//
InputNum.Location = new Point(9, 100);
InputNum.Margin = new Padding(3, 2, 3, 2);
InputNum.Name = "InputNum";
InputNum.Size = new Size(200, 23);
InputNum.TabIndex = 1;
PlaneTextBox.Location = new Point(9, 244);
PlaneTextBox.Margin = new Padding(3, 2, 3, 2);
PlaneTextBox.Name = "PlaneTextBox";
PlaneTextBox.Size = new Size(200, 23);
PlaneTextBox.TabIndex = 1;
//
// label1
//
@ -109,7 +180,7 @@
DrawPlane.Location = new Point(0, 0);
DrawPlane.Margin = new Padding(3, 2, 3, 2);
DrawPlane.Name = "DrawPlane";
DrawPlane.Size = new Size(577, 338);
DrawPlane.Size = new Size(646, 386);
DrawPlane.TabIndex = 1;
DrawPlane.TabStop = false;
//
@ -117,14 +188,16 @@
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(796, 338);
ClientSize = new Size(865, 386);
Controls.Add(DrawPlane);
Controls.Add(panel1);
Margin = new Padding(3, 2, 3, 2);
Name = "FormHydroplaneCollection";
Text = "Form1";
Text = "Гаражи гидропланов";
panel1.ResumeLayout(false);
panel1.PerformLayout();
panel2.ResumeLayout(false);
panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)DrawPlane).EndInit();
ResumeLayout(false);
}
@ -135,8 +208,14 @@
private Button UpdateButton;
private Button DeleteButton;
private Button AddButton;
private TextBox InputNum;
private TextBox PlaneTextBox;
private Label label1;
private PictureBox DrawPlane;
private Panel panel2;
private Button DeleteCollectButton;
private ListBox CollectionListBox;
private Button AddCollectButton;
private TextBox SetTextBox;
private Label label2;
}
}

View File

@ -15,23 +15,107 @@ namespace Hydroplane
{
public partial class FormHydroplaneCollection : Form
{
private readonly PlanesGenericCollection<DrawningPlane, DrawningObjectPlane> _planes;
private readonly PlanesGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormHydroplaneCollection()
{
InitializeComponent();
_planes = new PlanesGenericCollection<DrawningPlane, DrawningObjectPlane>(DrawPlane.Width, DrawPlane.Height);
_storage = new PlanesGenericStorage(DrawPlane.Width, DrawPlane.Height);
}
private void ButtonAddTank_Click(object sender, EventArgs e)
/// <summary>
/// Заполнение listBoxObjects
/// </summary>
private void ReloadObjects()
{
FormHydroplane form = new FormHydroplane();
int index = CollectionListBox.SelectedIndex;
CollectionListBox.Items.Clear();
for (int i = 0; i < _storage.Keys.Count; i++)
{
CollectionListBox.Items.Add(_storage.Keys[i]);
}
if (CollectionListBox.Items.Count > 0 && (index == -1 || index
>= CollectionListBox.Items.Count))
{
CollectionListBox.SelectedIndex = 0;
}
else if (CollectionListBox.Items.Count > 0 && index > -1 &&
index < CollectionListBox.Items.Count)
{
CollectionListBox.SelectedIndex = index;
}
}
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddObject_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(SetTextBox.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_storage.AddSet(SetTextBox.Text);
ReloadObjects();
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListBoxObjects_SelectedIndexChanged(object sender,
EventArgs e)
{
DrawPlane.Image =
_storage[CollectionListBox.SelectedItem?.ToString() ?? string.Empty]?.ShowPlanes();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDelObject_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить объект {CollectionListBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes)
{
_storage.DelSet(CollectionListBox.SelectedItem.ToString()
?? string.Empty);
ReloadObjects();
}
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddPlane_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ?? string.Empty];
if (obj == null)
{
return;
}
FormHydroplane form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_planes + form.SelectedPlane != -1)
if (obj + form.SelectedPlane)
{
MessageBox.Show("Объект добавлен");
DrawPlane.Image = _planes.ShowPlanes();
DrawPlane.Image = obj.ShowPlanes();
}
else
{
@ -39,34 +123,60 @@ namespace Hydroplane
}
}
}
private void ButtonRemoveCar_Click(object sender, EventArgs e)
/// <summary>
/// Удаление объекта из набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemovePlane_Click(object sender, EventArgs e)
{
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = -1;
try
{
pos = Convert.ToInt32(InputNum.Text);
}
catch (Exception ex) { }
if (_planes - pos)
int pos = 0;
if (PlaneTextBox != null)
pos = Convert.ToInt32(PlaneTextBox.Text);
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
DrawPlane.Image = _planes.ShowPlanes();
DrawPlane.Image = obj.ShowPlanes();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
/// <summary>
/// Обновление рисунка по набору
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs
e)
{
DrawPlane.Image = _planes.ShowPlanes();
if (CollectionListBox.SelectedIndex == -1)
{
return;
}
var obj = _storage[CollectionListBox.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
DrawPlane.Image = obj.ShowPlanes();
}
}
}

View File

@ -27,30 +27,29 @@ namespace Hydroplane.Generics
_collection = new SetGeneric<T>(width * height);
}
public static int? operator +(PlanesGenericCollection<T, U> collect, T?
obj)
public static bool operator +(PlanesGenericCollection<T, U> collect, T obj)
{
if (obj == null)
{
return -1;
}
return collect?._collection.Insert(obj);
}
public static bool operator -(PlanesGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection.Get(pos);
if (obj == null)
{
return false;
}
return collect._collection.Remove(pos);
return (bool)collect._collection.Insert(obj);
}
public static T? operator -(PlanesGenericCollection<T, U> collect, int
pos)
{
T? obj = collect._collection[pos];
if (obj != null)
{
collect._collection.Remove(pos);
}
return obj;
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
public Bitmap ShowPlanes()
{
@ -80,7 +79,7 @@ namespace Hydroplane.Generics
{
for (int i = 0; i < _collection.Count; i++)
{
T? t = _collection.Get(i);
T? t = _collection[i];
if (t != null)
{
t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);

View File

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

View File

@ -8,58 +8,68 @@ namespace Hydroplane.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 plane)
public bool Insert(T plane)
{
if (_places[Count - 1] != null)
return -1;
return Insert(plane, 0);
}
public int Insert(T plane, int position)
public bool Insert(T plane, 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] = plane;
return position;
}
public bool Remove(int position)
{
if (position < 0 || position >= _places.Count())
if (position < 0 || position >= _maxCount)
return false;
_places[position] = null;
if (Count >= _maxCount)
return false;
_places.Insert(0, plane);
return true;
}
public T? Get(int position)
public bool Remove(int position)
{
if (position < 0 || position >= _places.Count())
return null;
return _places[position];
if (position < 0 || position > _maxCount)
return false;
if (position >= Count)
return false;
_places.RemoveAt(position);
return true;
}
public T? this[int 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?> GetPlanes(int? maxPlanes = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxPlanes.HasValue && i == maxPlanes.Value)
{
yield break;
}
}
}
}
}