This commit is contained in:
Whoisthatjulia 2023-12-13 02:57:47 +04:00
parent ce8e5ace33
commit 4de997dcb5
8 changed files with 361 additions and 116 deletions

View File

@ -37,7 +37,7 @@ namespace AirFighter.Generics
}
public static bool operator -(AirFighterGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
T? obj = collect._collection[pos];
if (obj != null)
{
return collect._collection.Remove(pos);
@ -46,7 +46,7 @@ namespace AirFighter.Generics
}
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
return (U?)_collection[pos]?.GetMoveableObject;
}
public Bitmap ShowAirFighter()
{
@ -77,18 +77,16 @@ namespace AirFighter.Generics
int width = _pictureWidth / _placeSizeWidth;
int height = _pictureHeight / _placeSizeHeight;
for (int i = 0; i < _collection.Count; i++)
foreach (var fighter in _collection.GetAirFighter())
{
DrawningAirFighter? fighter = _collection.Get(i);
if (fighter == null)
continue;
int row = height - 1 - (i / width);
int col = width - 1 - (i % width);
fighter.SetPosition(col * _placeSizeWidth + 10, row * _placeSizeHeight + 5);
if (fighter != null)
{
int index = _collection.GetAirFighter().ToList().IndexOf(fighter);
int row = height - 1 - (index / width);
int col = width - 1 - (index % width);
fighter.SetPosition(col * _placeSizeWidth + 10, row * _placeSizeHeight + 5);
}
fighter.DrawTransport(g);
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirFighter.Drawnings;
using AirFighter.MovementStrategy;
using AirFighter.DrawningObjects;
namespace AirFighter.Generics
{
internal class AirFighterGenericStorage
{
readonly Dictionary<string, AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>> _AirFighterStorages;
public List<string> Keys => _AirFighterStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public AirFighterGenericStorage(int pictureWidth, int pictureHeight)
{
_AirFighterStorages = new Dictionary<string, AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddSet(string name)
{
if (!_AirFighterStorages.ContainsKey(name))
{
var fighterCollection = new AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>(_pictureWidth, _pictureHeight);
_AirFighterStorages.Add(name, fighterCollection);
}
}
public void DelSet(string name)
{
if (_AirFighterStorages.ContainsKey(name))
{
_AirFighterStorages.Remove(name);
}
}
public AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>? this[string ind]
{
get
{
if (_AirFighterStorages.ContainsKey(ind))
{
return _AirFighterStorages[ind];
}
return null;
}
}
}
}

View File

@ -1,6 +1,6 @@
namespace AirFighter
{
partial class FormFighterCollection
partial class FormAirFighterCollection
{
/// <summary>
/// Required designer variable.
@ -29,17 +29,24 @@
private void InitializeComponent()
{
groupBox1 = new GroupBox();
groupBox2 = new GroupBox();
buttonDelObject = new Button();
listBoxStorage = new ListBox();
buttonAddObject = new Button();
textBoxStorageName = new TextBox();
maskedTextBoxNumber = new MaskedTextBox();
buttonRemoveFighter = new Button();
buttonRefreshCollection = new Button();
buttonAddFighter = new Button();
pictureBoxCollection = new PictureBox();
groupBox1.SuspendLayout();
groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
// groupBox1
//
groupBox1.Controls.Add(groupBox2);
groupBox1.Controls.Add(maskedTextBoxNumber);
groupBox1.Controls.Add(buttonRemoveFighter);
groupBox1.Controls.Add(buttonRefreshCollection);
@ -51,10 +58,60 @@
groupBox1.TabStop = false;
groupBox1.Text = "Инструменты";
//
// groupBox2
//
groupBox2.Controls.Add(buttonDelObject);
groupBox2.Controls.Add(listBoxStorage);
groupBox2.Controls.Add(buttonAddObject);
groupBox2.Controls.Add(textBoxStorageName);
groupBox2.Location = new Point(3, 19);
groupBox2.Name = "groupBox2";
groupBox2.Size = new Size(200, 284);
groupBox2.TabIndex = 6;
groupBox2.TabStop = false;
groupBox2.Text = "Наборы";
//
// buttonDelObject
//
buttonDelObject.Location = new Point(18, 244);
buttonDelObject.Name = "buttonDelObject";
buttonDelObject.Size = new Size(159, 34);
buttonDelObject.TabIndex = 2;
buttonDelObject.Text = "Удалить набор";
buttonDelObject.UseVisualStyleBackColor = true;
buttonDelObject.Click += ButtonDelObject_Click;
//
// listBoxStorage
//
listBoxStorage.FormattingEnabled = true;
listBoxStorage.ItemHeight = 15;
listBoxStorage.Location = new Point(15, 88);
listBoxStorage.Name = "listBoxStorage";
listBoxStorage.Size = new Size(162, 139);
listBoxStorage.TabIndex = 2;
listBoxStorage.SelectedIndexChanged += listBoxStorage_SelectedIndexChanged;
//
// buttonAddObject
//
buttonAddObject.Location = new Point(18, 48);
buttonAddObject.Name = "buttonAddObject";
buttonAddObject.Size = new Size(159, 34);
buttonAddObject.TabIndex = 1;
buttonAddObject.Text = "Добавить набор";
buttonAddObject.UseVisualStyleBackColor = true;
buttonAddObject.Click += ButtonAddObject_Click;
//
// textBoxStorageName
//
textBoxStorageName.Location = new Point(3, 19);
textBoxStorageName.Name = "textBoxStorageName";
textBoxStorageName.Size = new Size(191, 23);
textBoxStorageName.TabIndex = 0;
//
// maskedTextBoxNumber
//
maskedTextBoxNumber.Font = new Font("Showcard Gothic", 9F, FontStyle.Regular, GraphicsUnit.Point);
maskedTextBoxNumber.Location = new Point(37, 103);
maskedTextBoxNumber.Location = new Point(39, 346);
maskedTextBoxNumber.Mask = "00";
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
maskedTextBoxNumber.Size = new Size(100, 22);
@ -62,7 +119,7 @@
//
// buttonRemoveFighter
//
buttonRemoveFighter.Location = new Point(37, 155);
buttonRemoveFighter.Location = new Point(17, 374);
buttonRemoveFighter.Name = "buttonRemoveFighter";
buttonRemoveFighter.Size = new Size(166, 31);
buttonRemoveFighter.TabIndex = 3;
@ -72,7 +129,7 @@
//
// buttonRefreshCollection
//
buttonRefreshCollection.Location = new Point(37, 209);
buttonRefreshCollection.Location = new Point(17, 411);
buttonRefreshCollection.Name = "buttonRefreshCollection";
buttonRefreshCollection.Size = new Size(166, 31);
buttonRefreshCollection.TabIndex = 4;
@ -82,7 +139,7 @@
//
// buttonAddFighter
//
buttonAddFighter.Location = new Point(37, 36);
buttonAddFighter.Location = new Point(17, 309);
buttonAddFighter.Name = "buttonAddFighter";
buttonAddFighter.Size = new Size(166, 31);
buttonAddFighter.TabIndex = 2;
@ -111,6 +168,8 @@
Text = "Набор истребителя";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
groupBox2.ResumeLayout(false);
groupBox2.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false);
}
@ -123,5 +182,10 @@
private Button buttonRefreshCollection;
private Button buttonAddFighter;
private MaskedTextBox maskedTextBoxNumber;
private GroupBox groupBox2;
private TextBox textBoxStorageName;
private Button buttonDelObject;
private ListBox listBoxStorage;
private Button buttonAddObject;
}
}

View File

@ -0,0 +1,187 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AirFighter.DrawningObjects;
using AirFighter.Drawnings;
using AirFighter.Generics;
using AirFighter.MovementStrategy;
namespace AirFighter
{
/// <summary>
/// Форма для работы с набором объектов класса DrawningCar
/// </summary>
public partial class FormAirFighterCollection : Form
{
/// <summary>
/// Набор объектов
/// </summary>
private readonly AirFighterGenericStorage _storage;
/// <summary>
/// Конструктор
/// </summary>
public FormAirFighterCollection()
{
InitializeComponent();
_storage = new AirFighterGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
/// <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;
}
}
/// <summary>
/// Добавление набора в коллекцию
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// Выбор набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBoxStorage_SelectedIndexChanged(object sender,
EventArgs e)
{
pictureBoxCollection.Image =
_storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowAirFighter();
}
/// <summary>
/// Удаление набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonDelObject_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();
}
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonAddAirFighter_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
FormAirFighter form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (obj + form.SelectedAirFighter > -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = obj.ShowAirFighter();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
/// <summary>
/// Удаление объекта из набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemoveAirFighter_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)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (obj - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = obj.ShowAirFighter();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Обновление рисунка по набору
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
if (listBoxStorage.SelectedIndex == -1)
{
return;
}
var obj = _storage[listBoxStorage.SelectedItem.ToString() ??
string.Empty];
if (obj == null)
{
return;
}
pictureBoxCollection.Image = obj.ShowAirFighter();
}
}
}

View File

@ -1,69 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AirFighter.DrawningObjects;
using AirFighter.Drawnings;
using AirFighter.Generics;
using AirFighter.MovementStrategy;
namespace AirFighter
{
public partial class FormFighterCollection : Form
{
private readonly AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter> _fighter;
public FormFighterCollection()
{
InitializeComponent();
_fighter = new AirFighterGenericCollection<DrawningAirFighter, DrawningObjectAirFighter>(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
private void ButtonAddAirFighter_Click(object sender, EventArgs e)
{
FormAirFighter form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_fighter + form.SelectedAirFighter != null)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _fighter.ShowAirFighter();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void ButtonRemoveAirFighter_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxNumber.Text);
if (_fighter - pos != null)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _fighter.ShowAirFighter();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _fighter.ShowAirFighter();
}
}
}

View File

@ -11,7 +11,7 @@ namespace AirFighter
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new FormFighterCollection());
Application.Run(new FormAirFighterCollection());
}
}
}

View File

@ -3,53 +3,63 @@
internal class SetGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
private readonly List<T?> _places;
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 fighter)
{
for (int i = 0; i < _places.Length; i++)
{
if (_places[i] == null)
{
_places[i] = fighter;
return i;
}
}
return -1;
return Insert(fighter, 0);
}
private bool isCorrectPosition(int position)
{
return 0 <= position && position < _maxCount;
}
public int Insert(T fighter, int position)
{
int index = position;
while (_places[index] != null && index < _places.Length) index++;
if (index == _places.Length) return -1;
for (int i = index; i > position; --i) _places[i] = _places[i - 1];
_places[position] = fighter;
if (!isCorrectPosition(position))
{
return -1;
}
_places.Insert(position, fighter);
return position;
}
public bool Remove(int position)
{
if (position < 0 || position >= _places.Length)
{
return false;
}
_places[position] = null;
if ((position < 0) || (position > _maxCount)) return false;
_places.RemoveAt(position);
return true;
}
public T? Get(int position)
public T? this[int position]
{
if (position < 0 || position >= _places.Length)
get
{
return null;
if (position < 0 || position > _maxCount)
return null;
return _places[position];
}
set
{
if (position < 0 || position > _maxCount)
return;
_places[position] = value;
}
}
public IEnumerable<T?> GetAirFighter(int? maxAirFighter = null)
{
for (int i = 0; i < _places.Count; ++i)
{
yield return _places[i];
if (maxAirFighter.HasValue && i == maxAirFighter.Value)
{
yield break;
}
}
return _places[position];
}
}
}