Ihonkina E.S. Lab work 4 #4

Merged
eegov merged 2 commits from LabWork04 into LabWork03 2022-11-07 10:27:26 +04:00
5 changed files with 258 additions and 85 deletions

View File

@ -29,6 +29,10 @@
private void InitializeComponent()
{
this.groupBoxTools = new System.Windows.Forms.GroupBox();
this.ButtonDeleteMap = new System.Windows.Forms.Button();
this.ListBoxMaps = new System.Windows.Forms.ListBox();
this.ButtonAddMap = new System.Windows.Forms.Button();
this.textBoxNewMapName = new System.Windows.Forms.TextBox();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonRemoveBoat = new System.Windows.Forms.Button();
this.buttonShowStorage = new System.Windows.Forms.Button();
@ -46,6 +50,10 @@
//
// groupBoxTools
//
this.groupBoxTools.Controls.Add(this.ButtonDeleteMap);
this.groupBoxTools.Controls.Add(this.ListBoxMaps);
this.groupBoxTools.Controls.Add(this.ButtonAddMap);
this.groupBoxTools.Controls.Add(this.textBoxNewMapName);
this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition);
this.groupBoxTools.Controls.Add(this.buttonRemoveBoat);
this.groupBoxTools.Controls.Add(this.buttonShowStorage);
@ -57,16 +65,53 @@
this.groupBoxTools.Controls.Add(this.buttonAddBoat);
this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap);
this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBoxTools.Location = new System.Drawing.Point(738, 0);
this.groupBoxTools.Location = new System.Drawing.Point(809, 0);
this.groupBoxTools.Name = "groupBoxTools";
this.groupBoxTools.Size = new System.Drawing.Size(251, 561);
this.groupBoxTools.Size = new System.Drawing.Size(251, 645);
this.groupBoxTools.TabIndex = 0;
this.groupBoxTools.TabStop = false;
this.groupBoxTools.Text = "Инструменты";
this.groupBoxTools.Text = "v";
//
// ButtonDeleteMap
//
this.ButtonDeleteMap.Location = new System.Drawing.Point(38, 209);
this.ButtonDeleteMap.Name = "ButtonDeleteMap";
this.ButtonDeleteMap.Size = new System.Drawing.Size(175, 33);
this.ButtonDeleteMap.TabIndex = 34;
this.ButtonDeleteMap.Text = "Удалить карту";
this.ButtonDeleteMap.UseVisualStyleBackColor = true;
this.ButtonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click);
//
// ListBoxMaps
//
this.ListBoxMaps.FormattingEnabled = true;
this.ListBoxMaps.ItemHeight = 15;
this.ListBoxMaps.Location = new System.Drawing.Point(38, 124);
this.ListBoxMaps.Name = "ListBoxMaps";
this.ListBoxMaps.Size = new System.Drawing.Size(175, 79);
this.ListBoxMaps.TabIndex = 33;
this.ListBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged);
//
// ButtonAddMap
//
this.ButtonAddMap.Location = new System.Drawing.Point(38, 89);
this.ButtonAddMap.Name = "ButtonAddMap";
this.ButtonAddMap.Size = new System.Drawing.Size(175, 29);
this.ButtonAddMap.TabIndex = 32;
this.ButtonAddMap.Text = "Добавить карту";
this.ButtonAddMap.UseVisualStyleBackColor = true;
this.ButtonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click);
//
// textBoxNewMapName
//
this.textBoxNewMapName.Location = new System.Drawing.Point(38, 31);
this.textBoxNewMapName.Name = "textBoxNewMapName";
this.textBoxNewMapName.Size = new System.Drawing.Size(175, 23);
this.textBoxNewMapName.TabIndex = 31;
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(38, 174);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(38, 328);
this.maskedTextBoxPosition.Mask = "00";
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23);
@ -75,7 +120,7 @@
//
// buttonRemoveBoat
//
this.buttonRemoveBoat.Location = new System.Drawing.Point(38, 203);
this.buttonRemoveBoat.Location = new System.Drawing.Point(38, 357);
this.buttonRemoveBoat.Name = "buttonRemoveBoat";
this.buttonRemoveBoat.Size = new System.Drawing.Size(175, 35);
this.buttonRemoveBoat.TabIndex = 14;
@ -85,7 +130,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(38, 298);
this.buttonShowStorage.Location = new System.Drawing.Point(38, 412);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(175, 35);
this.buttonShowStorage.TabIndex = 15;
@ -98,7 +143,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::MotorBoat.Properties.Resources.d;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(112, 469);
this.buttonDown.Location = new System.Drawing.Point(112, 553);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(30, 30);
this.buttonDown.TabIndex = 20;
@ -110,7 +155,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::MotorBoat.Properties.Resources.up;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(148, 469);
this.buttonRight.Location = new System.Drawing.Point(148, 553);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(30, 30);
this.buttonRight.TabIndex = 19;
@ -122,7 +167,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::MotorBoat.Properties.Resources.left;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(76, 469);
this.buttonLeft.Location = new System.Drawing.Point(76, 553);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(30, 30);
this.buttonLeft.TabIndex = 18;
@ -134,7 +179,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::MotorBoat.Properties.Resources.r;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(112, 433);
this.buttonUp.Location = new System.Drawing.Point(112, 517);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(30, 30);
this.buttonUp.TabIndex = 17;
@ -143,7 +188,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(38, 377);
this.buttonShowOnMap.Location = new System.Drawing.Point(38, 453);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35);
this.buttonShowOnMap.TabIndex = 16;
@ -153,7 +198,7 @@
//
// buttonAddBoat
//
this.buttonAddBoat.Location = new System.Drawing.Point(38, 115);
this.buttonAddBoat.Location = new System.Drawing.Point(38, 287);
this.buttonAddBoat.Name = "buttonAddBoat";
this.buttonAddBoat.Size = new System.Drawing.Size(175, 35);
this.buttonAddBoat.TabIndex = 12;
@ -169,7 +214,7 @@
"Простая карта",
"Розовая карта",
"Морская карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(38, 44);
this.comboBoxSelectorMap.Location = new System.Drawing.Point(38, 60);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(175, 23);
this.comboBoxSelectorMap.TabIndex = 11;
@ -180,7 +225,7 @@
this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox.Name = "pictureBox";
this.pictureBox.Size = new System.Drawing.Size(738, 561);
this.pictureBox.Size = new System.Drawing.Size(809, 645);
this.pictureBox.TabIndex = 0;
this.pictureBox.TabStop = false;
//
@ -188,7 +233,7 @@
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(989, 561);
this.ClientSize = new System.Drawing.Size(1060, 645);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBoxTools);
this.Name = "FormMapWithSetBoats";
@ -214,5 +259,9 @@
private Button buttonAddBoat;
private ComboBox comboBoxSelectorMap;
private PictureBox pictureBox;
private Button ButtonDeleteMap;
private ListBox ListBoxMaps;
private Button ButtonAddMap;
private TextBox textBoxNewMapName;
}
}

View File

@ -12,12 +12,48 @@ namespace MotorBoat
{
public partial class FormMapWithSetBoats : Form
{
private MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap> _mapBoatsCollectionGeneric;
private MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap> _mapShipCollectionGeneric;
private readonly Dictionary<string, AbstractMap> _mapDict = new()
{
{"Простая карта", new SimpleMap() },
{"Розовая карта", new PinkMap() },
{"Морская карта",new SeaMap() }
};
private readonly MapsCollection _mapsCollection;
public FormMapWithSetBoats()
{
InitializeComponent();
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
comboBoxSelectorMap.Items.Clear();
foreach (var elem in _mapDict)
{
comboBoxSelectorMap.Items.Add(elem.Key);
}
}
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
private void ReloadMaps()
{
int index = ListBoxMaps.SelectedIndex;
ListBoxMaps.Items.Clear();
for (int i = 0; i < _mapsCollection.Keys.Count; i++)
{
string? list = _mapsCollection.Keys[i];
ListBoxMaps.Items.Add(list);
}
if (ListBoxMaps.Items.Count > 0 && (index == -1 || index >= ListBoxMaps.Items.Count))
{
ListBoxMaps.SelectedIndex = 0;
}
else if (ListBoxMaps.Items.Count > 0 && index > -1 && index < ListBoxMaps.Items.Count)
{
ListBoxMaps.SelectedIndex = index;
}
}
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender,
EventArgs e)
{
AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
@ -34,17 +70,17 @@ namespace MotorBoat
}
if (map != null)
{
_mapBoatsCollectionGeneric = new MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap>(
_mapShipCollectionGeneric = new MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapBoatsCollectionGeneric = null;
_mapShipCollectionGeneric = null;
}
}
private void ButtonAddBoat_Click(object sender, EventArgs e)
{
if (_mapBoatsCollectionGeneric == null)
if (ListBoxMaps.SelectedIndex == -1)
{
return;
}
@ -52,19 +88,26 @@ namespace MotorBoat
if (form.ShowDialog() == DialogResult.OK)
{
DrawningObjectBoat boat = new(form.SelectedBoat);
if (_mapBoatsCollectionGeneric + boat != -1)
if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ??
string.Empty] + boat !=-1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet();
pictureBox.Image =
_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
private void ButtonRemoveBoat_Click(object sender, EventArgs e)
{
if (ListBoxMaps.SelectedIndex == -1)
{
return;
}
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
@ -74,10 +117,10 @@ namespace MotorBoat
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapBoatsCollectionGeneric - pos != null)
if (_mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet();
pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
@ -86,26 +129,27 @@ namespace MotorBoat
}
private void ButtonShowStorage_Click(object sender, EventArgs e)
{
if (_mapBoatsCollectionGeneric == null)
if (ListBoxMaps.SelectedIndex == -1)
{
return;
}
pictureBox.Image = _mapBoatsCollectionGeneric.ShowSet();
pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonShowOnMap_Click(object sender, EventArgs e)
{
if (_mapBoatsCollectionGeneric == null)
if (ListBoxMaps.SelectedIndex == -1)
{
return;
}
pictureBox.Image = _mapBoatsCollectionGeneric.ShowOnMap();
pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap();
}
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_mapBoatsCollectionGeneric == null)
if (ListBoxMaps.SelectedIndex == -1)
{
return;
}
//получаем имя кнопки
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
@ -123,7 +167,40 @@ namespace MotorBoat
dir = Direction.Right;
break;
}
pictureBox.Image = _mapBoatsCollectionGeneric.MoveObject(dir);
pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir);
}
private void ButtonAddMap_Click(object sender, EventArgs e)
{
if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text))
{
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!_mapDict.ContainsKey(comboBoxSelectorMap.Text))
{
MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_mapsCollection.AddMap(textBoxNewMapName.Text, _mapDict[comboBoxSelectorMap.Text]);
ReloadMaps();
}
private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox.Image = _mapsCollection[ListBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonDeleteMap_Click(object sender, EventArgs e)
{
if (ListBoxMaps.SelectedIndex == -1)
{
return;
}
if (MessageBox.Show($"Удалить карту {ListBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
_mapsCollection.DelMap(ListBoxMaps.SelectedItem?.ToString() ?? string.Empty);
ReloadMaps();
}
}
}

View File

@ -50,13 +50,9 @@
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setBoats.Count; i++)
foreach (var boat in _setBoats.GetBoat())
{
var boat = _setBoats.Get(i);
if (boat != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, boat);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, boat);
}
return new(_pictureWidth, _pictureHeight);
}
@ -75,14 +71,14 @@
int j = _setBoats.Count - 1;
for (int i = 0; i < _setBoats.Count; i++)
{
if (_setBoats.Get(i) == null)
if (_setBoats[i] == null)
{
for (; j > i; j--)
{
var car = _setBoats.Get(j);
if (car != null)
var boat = _setBoats[j];
if (boat != null)
{
_setBoats.Insert(car, i);
_setBoats.Insert(boat, i);
_setBoats.Remove(j);
break;
}
@ -116,14 +112,14 @@
bool down = false;
bool rightdown = false;
for (int i = 0; i < _setBoats.Count; i++)
foreach (var boat in _setBoats.GetBoat())
{
if (right)
{
_setBoats.Get(i)?.SetObject(curWidth, curHeight + _placeSizeHeight, _placeSizeWidth, _placeSizeHeight);
boat.SetObject(curWidth, curHeight + _placeSizeHeight, _placeSizeWidth, _placeSizeHeight);
g.RotateTransform(270, 0);
g.TranslateTransform(-190, 0);
_setBoats.Get(i)?.DrawningObject(g);
boat.DrawningObject(g);
g.TranslateTransform(190, 0);
g.RotateTransform(-270, 0);
@ -147,18 +143,18 @@
if (rightdown)
{
rightdown = false;
_setBoats.Get(i)?.SetObject(curWidth, curHeight + _placeSizeHeight, _placeSizeWidth, _placeSizeHeight);
boat.SetObject(curWidth, curHeight + _placeSizeHeight, _placeSizeWidth, _placeSizeHeight);
g.TranslateTransform(_placeSizeWidth + 90, 0);
_setBoats.Get(i)?.DrawningObject(g);
boat.DrawningObject(g);
curHeight += _placeSizeHeight;
}
else
{
rightdown = true;
_setBoats.Get(i)?.SetObject(curWidth, curHeight + _placeSizeHeight, _placeSizeWidth, _placeSizeHeight);
boat.SetObject(curWidth, curHeight + _placeSizeHeight, _placeSizeWidth, _placeSizeHeight);
g.TranslateTransform(-_placeSizeWidth - 90, 0);
_setBoats.Get(i)?.DrawningObject(g);
boat.DrawningObject(g);
}
if (curHeight >= _pictureHeight - 30) { down = false; return; }

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MotorBoat
{
internal class MapsCollection
{
readonly Dictionary<string, MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap>> _mapStorages;
public List<string> Keys => _mapStorages.Keys.ToList();
private readonly int _pictureWidth;
private readonly int _pictureHeight;
public MapsCollection(int pictureWidth, int pictureHeight)
{
_mapStorages = new Dictionary<string, MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddMap(string name, AbstractMap map)
{
if (!_mapStorages.ContainsKey(name))
{
_mapStorages.Add(name, new MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
}
public void DelMap(string name)
{
if (_mapStorages.ContainsKey(name)) _mapStorages.Remove(name);
}
public MapWithSetBoatsGeneric<DrawningObjectBoat, AbstractMap> this[string ind]
{
get
{
if (_mapStorages.ContainsKey(ind))
{
return _mapStorages[ind];
}
return null;
}
}
}
}

View File

@ -9,63 +9,70 @@ namespace MotorBoat
internal class SetBoatsGeneric<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 SetBoatsGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
public int Insert(T boat)
{
if (_places.Count > _maxCount)
{
return -1;
}
return Insert(boat, 0);
}
public int Insert(T boat, int position)
{
// TODO проверка позиции
if (position >= _places.Length || position < 0)
return -1;
// TODO проверка, что элемент массива по этой позиции пустой, если нет, то
if (_places[position] == null)
{
_places[position] = boat;
return position;
}
// проверка, что после вставляемого элемента в массиве есть пустой элемент
int EmptyPos = -1;
for (int i = position + 1; i < Count; i++)
{
if (_places[i] == null)
{
EmptyPos = i;
break;
}
}
if (EmptyPos < 0) return -1;
// сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
for (int i = EmptyPos; i > position; i--)
{
_places[i] = _places[i - 1];
}
// вставка по позиции
_places[position] = boat;
if (position >= _maxCount || position < 0) return -1;
_places.Insert(position, boat);
return position;
}
public T Remove(int position)
{
// TODO проверка позиции
if (position >= _places.Length || position < 0) return null;
if (position >= _maxCount || position < 0) return null;
// TODO удаление объекта из массива, присовив элементу массива значение null
T temp = _places[position];
_places[position] = null;
_places.RemoveAt(position);
return temp;
}
public T Get(int position)
public T this[int position]
{
// TODO проверка позиции
if (position >= _places.Length || position < 0)
return null;
return _places[position];
get
{
if (position >= _places.Count || position < 0)
{
return null;
}
return _places[position];
}
set
{
if (position >= _places.Count || position < 0)
{
return;
}
Insert(value, position);
}
}
public IEnumerable<T> GetBoat()
{
foreach (var boat in _places)
{
if (boat != null)
{
yield return boat;
}
else
{
yield break;
}
}
}
}
}