Возвращение злых кнопок(Laba4)

This commit is contained in:
Extrimal 2023-11-12 17:12:10 +03:00
parent 22f93d7571
commit 6be2956699
5 changed files with 199 additions and 67 deletions

View File

@ -31,7 +31,8 @@ namespace AircraftCarrier.Generics
_pictureHeight = picHeight; _pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height); _collection = new SetGeneric<T>(width * height);
} }
/// Перегрузка оператора сложения /// Перегрузка оператора сложения
public static int operator +(AircraftsGenericCollection<T, U> collect, T? public static int operator +(AircraftsGenericCollection<T, U> collect, T?
obj) obj)
{ {
@ -42,20 +43,22 @@ namespace AircraftCarrier.Generics
return collect._collection.Insert(obj); return collect._collection.Insert(obj);
} }
/// Перегрузка оператора вычитания /// Перегрузка оператора вычитания
public static bool operator -(AircraftsGenericCollection<T, U> collect, int pos) public static T? operator -(AircraftsGenericCollection<T, U> collect, int pos)
{ {
T? obj = collect._collection.Get(pos); T? obj = collect._collection[pos];
if (obj != null) if (obj != null)
{ {
return collect._collection.Remove(pos); collect._collection.Remove(pos);
} }
return false; return obj;
} }
/// Получение объекта IMoveableObject /// Получение объекта IMoveableObject
public U? GetU(int pos) public U? GetU(int pos)
{ {
return (U?)_collection.Get(pos)?.GetMoveableObject; return (U?)_collection[pos]?.GetMoveableObject;
} }
/// Вывод всего набора объектов /// Вывод всего набора объектов
public Bitmap ShowAircraft() public Bitmap ShowAircraft()
{ {
@ -86,15 +89,14 @@ namespace AircraftCarrier.Generics
/// Метод прорисовки объектов /// Метод прорисовки объектов
private void DrawObjects(Graphics g) private void DrawObjects(Graphics g)
{ {
int inRow = _pictureWidth / _placeSizeWidth; foreach (var car in _collection.GetAircrafts())
for (int i = 0; i < _collection.Count; i++)
{ {
DrawningAircraft aircraft = _collection.Get(i); if (car != null)
if (aircraft != null)
{ {
// TODO получение объекта
aircraft.SetPosition(i % inRow * _placeSizeWidth+3, (_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight + 5); // TODO установка позиции
aircraft.DrawTransport(g); // TODO прорисовка объекта
car.DrawTransport(g);
} }
} }
} }

View File

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AircraftCarrier.DrawningObjects;
using AircraftCarrier.MovementStrategy;
namespace AircraftCarrier.Generics
{
/// Класс для хранения коллекции
internal class AircraftsGenericStorage
{
/// Словарь (хранилище)
readonly Dictionary<string, AircraftsGenericCollection<DrawningAircraft,
DrawningObjectAircraft>> _AircraftStorages;
/// Возвращение списка названий наборов
public List<string> Keys => _AircraftStorages.Keys.ToList();
/// Ширина окна отрисовки
private readonly int _pictureWidth;
/// Высота окна отрисовки
private readonly int _pictureHeight;
/// Конструктор
public AircraftsGenericStorage(int pictureWidth, int pictureHeight)
{
_AircraftStorages = new Dictionary<string,
AircraftsGenericCollection<DrawningAircraft, DrawningObjectAircraft>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
/// Добавление набора
public void AddSet(string name)
{
// TODO Прописать логику для добавления
}
/// Удаление набора
public void DelSet(string name)
{
// TODO Прописать логику для удаления
}
/// Доступ к набору
public AircraftsGenericCollection<DrawningAircraft, DrawningObjectAircraft>?
this[string ind]
{
get
{
// TODO Продумать логику получения набора
return null;
}
}
}
}

View File

@ -30,38 +30,59 @@
{ {
PictureBoxCollection = new PictureBox(); PictureBoxCollection = new PictureBox();
PanelTools = new Panel(); PanelTools = new Panel();
PanelSets = new Panel();
ButtonRefreshCollection = new Button(); ButtonRefreshCollection = new Button();
ButtonRemoveAircraft = new Button(); ButtonRemoveAircraft = new Button();
MaskedTextBoxNumber = new MaskedTextBox(); MaskedTextBoxNumber = new MaskedTextBox();
ButtonAddAircraft = new Button(); ButtonAddAircraft = new Button();
LabelTools = new Label(); labelTools = new Label();
labelSets = new Label();
textBoxStorageName = new TextBox();
ButtonAddObject = new Button();
listBoxStorage = new ListBox();
ButtonRemoveObject = new Button();
((System.ComponentModel.ISupportInitialize)PictureBoxCollection).BeginInit(); ((System.ComponentModel.ISupportInitialize)PictureBoxCollection).BeginInit();
PanelTools.SuspendLayout(); PanelTools.SuspendLayout();
PanelSets.SuspendLayout();
SuspendLayout(); SuspendLayout();
// //
// PictureBoxCollection // PictureBoxCollection
// //
PictureBoxCollection.Location = new Point(0, 0); PictureBoxCollection.Location = new Point(0, 0);
PictureBoxCollection.Name = "PictureBoxCollection"; PictureBoxCollection.Name = "PictureBoxCollection";
PictureBoxCollection.Size = new Size(599, 450); PictureBoxCollection.Size = new Size(768, 651);
PictureBoxCollection.TabIndex = 0; PictureBoxCollection.TabIndex = 0;
PictureBoxCollection.TabStop = false; PictureBoxCollection.TabStop = false;
// //
// PanelTools // PanelTools
// //
PanelTools.BorderStyle = BorderStyle.FixedSingle; PanelTools.BorderStyle = BorderStyle.FixedSingle;
PanelTools.Controls.Add(labelTools);
PanelTools.Controls.Add(PanelSets);
PanelTools.Controls.Add(ButtonRefreshCollection); PanelTools.Controls.Add(ButtonRefreshCollection);
PanelTools.Controls.Add(ButtonRemoveAircraft); PanelTools.Controls.Add(ButtonRemoveAircraft);
PanelTools.Controls.Add(MaskedTextBoxNumber); PanelTools.Controls.Add(MaskedTextBoxNumber);
PanelTools.Controls.Add(ButtonAddAircraft); PanelTools.Controls.Add(ButtonAddAircraft);
PanelTools.Location = new Point(599, 12); PanelTools.Location = new Point(774, 12);
PanelTools.Name = "PanelTools"; PanelTools.Name = "PanelTools";
PanelTools.Size = new Size(202, 438); PanelTools.Size = new Size(232, 639);
PanelTools.TabIndex = 1; PanelTools.TabIndex = 1;
// //
// PanelSets
//
PanelSets.Controls.Add(ButtonRemoveObject);
PanelSets.Controls.Add(listBoxStorage);
PanelSets.Controls.Add(ButtonAddObject);
PanelSets.Controls.Add(textBoxStorageName);
PanelSets.Controls.Add(labelSets);
PanelSets.Location = new Point(16, 37);
PanelSets.Name = "PanelSets";
PanelSets.Size = new Size(204, 339);
PanelSets.TabIndex = 4;
//
// ButtonRefreshCollection // ButtonRefreshCollection
// //
ButtonRefreshCollection.Location = new Point(15, 206); ButtonRefreshCollection.Location = new Point(36, 572);
ButtonRefreshCollection.Name = "ButtonRefreshCollection"; ButtonRefreshCollection.Name = "ButtonRefreshCollection";
ButtonRefreshCollection.Size = new Size(168, 41); ButtonRefreshCollection.Size = new Size(168, 41);
ButtonRefreshCollection.TabIndex = 3; ButtonRefreshCollection.TabIndex = 3;
@ -71,7 +92,7 @@
// //
// ButtonRemoveAircraft // ButtonRemoveAircraft
// //
ButtonRemoveAircraft.Location = new Point(15, 137); ButtonRemoveAircraft.Location = new Point(36, 497);
ButtonRemoveAircraft.Name = "ButtonRemoveAircraft"; ButtonRemoveAircraft.Name = "ButtonRemoveAircraft";
ButtonRemoveAircraft.Size = new Size(168, 41); ButtonRemoveAircraft.Size = new Size(168, 41);
ButtonRemoveAircraft.TabIndex = 2; ButtonRemoveAircraft.TabIndex = 2;
@ -81,7 +102,7 @@
// //
// MaskedTextBoxNumber // MaskedTextBoxNumber
// //
MaskedTextBoxNumber.Location = new Point(36, 95); MaskedTextBoxNumber.Location = new Point(57, 438);
MaskedTextBoxNumber.Name = "MaskedTextBoxNumber"; MaskedTextBoxNumber.Name = "MaskedTextBoxNumber";
MaskedTextBoxNumber.Size = new Size(125, 27); MaskedTextBoxNumber.Size = new Size(125, 27);
MaskedTextBoxNumber.TabIndex = 1; MaskedTextBoxNumber.TabIndex = 1;
@ -89,7 +110,7 @@
// //
// ButtonAddAircraft // ButtonAddAircraft
// //
ButtonAddAircraft.Location = new Point(15, 19); ButtonAddAircraft.Location = new Point(36, 391);
ButtonAddAircraft.Name = "ButtonAddAircraft"; ButtonAddAircraft.Name = "ButtonAddAircraft";
ButtonAddAircraft.Size = new Size(168, 41); ButtonAddAircraft.Size = new Size(168, 41);
ButtonAddAircraft.TabIndex = 0; ButtonAddAircraft.TabIndex = 0;
@ -97,21 +118,63 @@
ButtonAddAircraft.UseVisualStyleBackColor = true; ButtonAddAircraft.UseVisualStyleBackColor = true;
ButtonAddAircraft.Click += ButtonAddAircraft_Click; ButtonAddAircraft.Click += ButtonAddAircraft_Click;
// //
// LabelTools // labelTools
// //
LabelTools.AutoSize = true; labelTools.AutoSize = true;
LabelTools.Location = new Point(615, 0); labelTools.Location = new Point(16, -4);
LabelTools.Name = "LabelTools"; labelTools.Name = "labelTools";
LabelTools.Size = new Size(103, 20); labelTools.Size = new Size(103, 20);
LabelTools.TabIndex = 0; labelTools.TabIndex = 5;
LabelTools.Text = "Инструменты"; labelTools.Text = "Инструменты";
//
// labelSets
//
labelSets.AutoSize = true;
labelSets.Location = new Point(11, 9);
labelSets.Name = "labelSets";
labelSets.Size = new Size(55, 20);
labelSets.TabIndex = 0;
labelSets.Text = "Набор";
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(41, 41);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(125, 27);
textBoxStorageName.TabIndex = 1;
//
// ButtonAddObject
//
ButtonAddObject.Location = new Point(20, 83);
ButtonAddObject.Name = "ButtonAddObject";
ButtonAddObject.Size = new Size(168, 41);
ButtonAddObject.TabIndex = 2;
ButtonAddObject.Text = "Добавить набор";
ButtonAddObject.UseVisualStyleBackColor = true;
//
// listBoxStorage
//
listBoxStorage.FormattingEnabled = true;
listBoxStorage.ItemHeight = 20;
listBoxStorage.Location = new Point(29, 147);
listBoxStorage.Name = "listBoxStorage";
listBoxStorage.Size = new Size(150, 124);
listBoxStorage.TabIndex = 3;
//
// ButtonRemoveObject
//
ButtonRemoveObject.Location = new Point(20, 286);
ButtonRemoveObject.Name = "ButtonRemoveObject";
ButtonRemoveObject.Size = new Size(168, 41);
ButtonRemoveObject.TabIndex = 4;
ButtonRemoveObject.Text = "Удалить набор";
ButtonRemoveObject.UseVisualStyleBackColor = true;
// //
// FormAircraftCollection // FormAircraftCollection
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450); ClientSize = new Size(1007, 651);
Controls.Add(LabelTools);
Controls.Add(PanelTools); Controls.Add(PanelTools);
Controls.Add(PictureBoxCollection); Controls.Add(PictureBoxCollection);
Name = "FormAircraftCollection"; Name = "FormAircraftCollection";
@ -119,8 +182,9 @@
((System.ComponentModel.ISupportInitialize)PictureBoxCollection).EndInit(); ((System.ComponentModel.ISupportInitialize)PictureBoxCollection).EndInit();
PanelTools.ResumeLayout(false); PanelTools.ResumeLayout(false);
PanelTools.PerformLayout(); PanelTools.PerformLayout();
PanelSets.ResumeLayout(false);
PanelSets.PerformLayout();
ResumeLayout(false); ResumeLayout(false);
PerformLayout();
} }
#endregion #endregion
@ -129,8 +193,14 @@
private Panel PanelTools; private Panel PanelTools;
private MaskedTextBox MaskedTextBoxNumber; private MaskedTextBox MaskedTextBoxNumber;
private Button ButtonAddAircraft; private Button ButtonAddAircraft;
private Label LabelTools;
private Button ButtonRefreshCollection; private Button ButtonRefreshCollection;
private Button ButtonRemoveAircraft; private Button ButtonRemoveAircraft;
private Panel PanelSets;
private Label labelTools;
private Label labelSets;
private Button ButtonAddObject;
private TextBox textBoxStorageName;
private Button ButtonRemoveObject;
private ListBox listBoxStorage;
} }
} }

View File

@ -58,5 +58,7 @@ namespace AircraftCarrier
{ {
PictureBoxCollection.Image = _Aircrafts.ShowAircraft(); PictureBoxCollection.Image = _Aircrafts.ShowAircraft();
} }
} }
} }

View File

@ -8,60 +8,67 @@ namespace AircraftCarrier.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 Aircraft) public bool Insert(T Aircraft)
{ {
return Insert(Aircraft, 0); // TODO вставка в начало набора
return true;
} }
/// Добавление объекта в набор на конкретную позицию /// Добавление объекта в набор на конкретную позицию
public int Insert(T Aircraft, int position) public bool Insert(T Aircraft, int position)
{ {
int nullIndex = -1, i; // TODO проверка позиции
// TODO проверка, что есть место для вставки
if (position < 0 || position >= Count) // TODO вставка по позиции
return -1;
for (i = position; i < Count; i++)
{
if (_places[i] == null)
{
nullIndex = i;
break;
}
}
if (nullIndex < 0)
return -1;
for (i = nullIndex; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = Aircraft; _places[position] = Aircraft;
return position; return true;
} }
/// Удаление объекта из набора с конкретной позиции /// Удаление объекта из набора с конкретной позиции
public bool Remove(int position) public bool Remove(int position)
{ {
if (position < 0 || position >= Count) return false; // TODO проверка позиции
_places[position] = null; // TODO удаление объекта из списка
return true; return true;
} }
/// Получение объекта из набора по позиции /// Получение объекта из набора по позиции
public T? Get(int position) public T? this[int position]
{ {
if (position < 0 || position >= Count) get
return null; {
return _places[position]; // TODO проверка позиции
return _places[position];
}
set
{
// TODO проверка позиции
// TODO проверка свободных мест в списке
// TODO вставка в список по позиции
}
}
/// Проход по списку
public IEnumerable<T?> GetAircrafts(int? maxAircrafts = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxAircrafts.HasValue && i == maxAircrafts.Value)
{
yield break;
}
}
} }
} }
} }