Nugaev D.N. Lab_work04 #4

Closed
Damir_Nugaev_ISEbd-22 wants to merge 7 commits from Lab_work04 into Lab_work03
6 changed files with 285 additions and 68 deletions

View File

@ -1,4 +1,4 @@
using System;
using Bus;
using System.Collections.Generic;
using System.Linq;
using System.Text;

View File

@ -40,8 +40,14 @@
this.buttonAddBus = new System.Windows.Forms.Button();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBox2 = 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.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
@ -55,11 +61,10 @@
this.groupBox1.Controls.Add(this.maskedTextBoxPosition);
this.groupBox1.Controls.Add(this.buttonRemoveBus);
this.groupBox1.Controls.Add(this.buttonAddBus);
this.groupBox1.Controls.Add(this.comboBoxSelectorMap);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(550, 0);
this.groupBox1.Location = new System.Drawing.Point(598, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(250, 450);
this.groupBox1.Size = new System.Drawing.Size(250, 618);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
@ -69,7 +74,7 @@
this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRight.BackgroundImage = global::Bus.Properties.Resources.Frame_4;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(95, 398);
this.buttonRight.Location = new System.Drawing.Point(95, 566);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(40, 40);
this.buttonRight.TabIndex = 10;
@ -81,7 +86,7 @@
this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDown.BackgroundImage = global::Bus.Properties.Resources.Frame_5;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(55, 398);
this.buttonDown.Location = new System.Drawing.Point(55, 566);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(40, 40);
this.buttonDown.TabIndex = 9;
@ -93,7 +98,7 @@
this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLeft.BackgroundImage = global::Bus.Properties.Resources.Frame_6;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(15, 398);
this.buttonLeft.Location = new System.Drawing.Point(15, 566);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(40, 40);
this.buttonLeft.TabIndex = 8;
@ -105,7 +110,7 @@
this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonUp.BackgroundImage = global::Bus.Properties.Resources.Frame_3;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(55, 358);
this.buttonUp.Location = new System.Drawing.Point(55, 526);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(40, 40);
this.buttonUp.TabIndex = 7;
@ -114,7 +119,7 @@
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(6, 281);
this.buttonShowOnMap.Location = new System.Drawing.Point(15, 426);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(193, 29);
this.buttonShowOnMap.TabIndex = 5;
@ -124,7 +129,7 @@
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(6, 246);
this.buttonShowStorage.Location = new System.Drawing.Point(15, 391);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(193, 29);
this.buttonShowStorage.TabIndex = 4;
@ -134,16 +139,16 @@
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(6, 122);
this.maskedTextBoxPosition.Location = new System.Drawing.Point(15, 320);
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(100, 27);
this.maskedTextBoxPosition.TabIndex = 11;
//
// buttonRemoveBus
//
this.buttonRemoveBus.Location = new System.Drawing.Point(6, 155);
this.buttonRemoveBus.Location = new System.Drawing.Point(15, 353);
this.buttonRemoveBus.Name = "buttonRemoveBus";
this.buttonRemoveBus.Size = new System.Drawing.Size(151, 32);
this.buttonRemoveBus.Size = new System.Drawing.Size(129, 32);
this.buttonRemoveBus.TabIndex = 2;
this.buttonRemoveBus.Text = "Удалить автобус";
this.buttonRemoveBus.UseVisualStyleBackColor = true;
@ -151,9 +156,9 @@
//
// buttonAddBus
//
this.buttonAddBus.Location = new System.Drawing.Point(6, 83);
this.buttonAddBus.Location = new System.Drawing.Point(15, 281);
this.buttonAddBus.Name = "buttonAddBus";
this.buttonAddBus.Size = new System.Drawing.Size(151, 33);
this.buttonAddBus.Size = new System.Drawing.Size(129, 33);
this.buttonAddBus.TabIndex = 1;
this.buttonAddBus.Text = "Добавить автобус";
this.buttonAddBus.UseVisualStyleBackColor = true;
@ -165,9 +170,9 @@
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта",
"Водная карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 26);
this.comboBoxSelectorMap.Location = new System.Drawing.Point(8, 59);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(151, 28);
this.comboBoxSelectorMap.Size = new System.Drawing.Size(125, 28);
this.comboBoxSelectorMap.TabIndex = 0;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//
@ -176,15 +181,67 @@
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(550, 450);
this.pictureBox.Size = new System.Drawing.Size(598, 618);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.textBoxNewMapName);
this.groupBox2.Controls.Add(this.buttonDeleteMap);
this.groupBox2.Controls.Add(this.listBoxMaps);
this.groupBox2.Controls.Add(this.buttonAddMap);
this.groupBox2.Controls.Add(this.comboBoxSelectorMap);
this.groupBox2.Location = new System.Drawing.Point(605, 26);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(238, 243);
this.groupBox2.TabIndex = 12;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Карты";
//
// buttonDeleteMap
//
this.buttonDeleteMap.Location = new System.Drawing.Point(8, 203);
this.buttonDeleteMap.Name = "buttonDeleteMap";
this.buttonDeleteMap.Size = new System.Drawing.Size(193, 34);
this.buttonDeleteMap.TabIndex = 4;
this.buttonDeleteMap.Text = "Удалить карту";
this.buttonDeleteMap.UseVisualStyleBackColor = true;
this.buttonDeleteMap.Click += new System.EventHandler(this.ButtonDeleteMap_Click);
//
// listBoxMaps
//
this.listBoxMaps.FormattingEnabled = true;
this.listBoxMaps.ItemHeight = 20;
this.listBoxMaps.Location = new System.Drawing.Point(8, 133);
this.listBoxMaps.Name = "listBoxMaps";
this.listBoxMaps.Size = new System.Drawing.Size(150, 64);
this.listBoxMaps.TabIndex = 3;
this.listBoxMaps.SelectedIndexChanged += new System.EventHandler(this.ListBoxMaps_SelectedIndexChanged);
//
// buttonAddMap
//
this.buttonAddMap.Location = new System.Drawing.Point(8, 93);
this.buttonAddMap.Name = "buttonAddMap";
this.buttonAddMap.Size = new System.Drawing.Size(193, 34);
this.buttonAddMap.TabIndex = 2;
this.buttonAddMap.Text = "Добавить карту";
this.buttonAddMap.UseVisualStyleBackColor = true;
this.buttonAddMap.Click += new System.EventHandler(this.ButtonAddMap_Click);
//
// textBoxNewMapName
//
this.textBoxNewMapName.Location = new System.Drawing.Point(6, 26);
this.textBoxNewMapName.Name = "textBoxNewMapName";
this.textBoxNewMapName.Size = new System.Drawing.Size(125, 27);
this.textBoxNewMapName.TabIndex = 5;
//
// FormMapWithSetDoubleDeckerBus
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.ClientSize = new System.Drawing.Size(848, 618);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1);
this.Name = "FormMapWithSetDoubleDeckerBus";
@ -192,6 +249,8 @@
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.ResumeLayout(false);
}
@ -210,5 +269,10 @@
private Button buttonDown;
private Button buttonLeft;
private Button buttonUp;
private GroupBox groupBox2;
private Button buttonDeleteMap;
private ListBox listBoxMaps;
private Button buttonAddMap;
private TextBox textBoxNewMapName;
}
}

View File

@ -14,17 +14,46 @@ namespace Bus
{
private MapWithSetDoubleDeckerBusGeneric<DrawingObjectBus, AbstractMap> _mapBusCollectionGeneric;
//DrawingBus SelectedBus { get; private set; }
private readonly Dictionary<string, AbstractMap> _mapsDict = new()
{
{"Простая карта", new SimpleMap() },
{"Водная карта", new MyMap() },
};
private readonly MapsCollection _mapsCollection;
public FormMapWithSetDoubleDeckerBus()
{
InitializeComponent();
AbstractMap map = new SimpleMap();
_mapBusCollectionGeneric = new MapWithSetDoubleDeckerBusGeneric<DrawingObjectBus, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
comboBoxSelectorMap.Items.Clear();
foreach (var item in _mapsDict)
{
comboBoxSelectorMap.Items.Add(item.Key);
}
}
private void ReloadMaps()
{
int index = listBoxMaps.SelectedIndex;
listBoxMaps.Items.Clear();
for (int i = 0; i < _mapsCollection.Keys.Count; i++)
{
listBoxMaps.Items.Add(_mapsCollection.Keys[i]);
}
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;
@ -51,7 +80,7 @@ namespace Bus
private void ButtonAddBus_Click(object sender, EventArgs e)
{
if (_mapBusCollectionGeneric == null)
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
@ -65,11 +94,10 @@ namespace Bus
}
DrawingObjectBus bus = new(form.SelectedBus);
if (_mapBusCollectionGeneric + bus != -1)
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + bus != -1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapBusCollectionGeneric.ShowSet();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
@ -80,20 +108,27 @@ namespace Bus
private void ButtonRemoveBus_Click(object sender, EventArgs e)
{
if (listBoxMaps.SelectedIndex == -1)
{
return;
}
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapBusCollectionGeneric - pos != null)
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapBusCollectionGeneric.ShowSet();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
@ -107,7 +142,7 @@ namespace Bus
{
return;
}
pictureBox.Image = _mapBusCollectionGeneric.ShowSet();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
private void ButtonShowOnMap_Click(object sender, EventArgs e)
@ -116,7 +151,7 @@ namespace Bus
{
return;
}
pictureBox.Image = _mapBusCollectionGeneric.ShowOnMap();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap();
}
private void ButtonMove_Click(object sender, EventArgs e)
@ -144,6 +179,43 @@ namespace Bus
break;
}
pictureBox.Image = _mapBusCollectionGeneric.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 (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text))
{
MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[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

@ -49,13 +49,11 @@ namespace Bus
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i< _setBus.Count; i++)
foreach (var bus in _setBus.GetBuses())
{
var bus = _setBus.Get(i);
if (bus != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, bus);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, bus);
}
return new(_pictureWidth, _pictureHeight);
}
@ -74,11 +72,11 @@ namespace Bus
int j = _setBus.Count - 1;
for (int i = 0; i<_setBus.Count;i++)
{
if(_setBus.Get(i) == null)
if(_setBus[i] == null)
{
for(; j > i; j--)
{
var bus = _setBus.Get(j);
var bus = _setBus[j];
if (bus != null)
{
_setBus.Insert(bus, i);
@ -125,12 +123,12 @@ namespace Bus
int currentWidth = width - 1;
int currentHeight = 0;
for (int i = 0; i < _setBus.Count; i++)
foreach (var bus in _setBus.GetBuses())
{
_setBus.Get(i)?.SetObject(currentWidth * _placeSizeWidth,
bus?.SetObject(currentWidth * _placeSizeWidth,
currentHeight * _placeSizeHeight,
_pictureWidth, _pictureHeight);
_setBus.Get(i)?.DrawingObject(g);
bus?.DrawingObject(g);
if (currentWidth > 0)
currentWidth--;

56
Bus/Bus/MapsCollection.cs Normal file
View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bus
{
internal class MapsCollection
{
readonly Dictionary<string, MapWithSetDoubleDeckerBusGeneric<DrawingObjectBus, 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, MapWithSetDoubleDeckerBusGeneric<DrawingObjectBus, AbstractMap>>();
_pictureWidth = pictureWidth;
_pictureHeight = pictureHeight;
}
public void AddMap(string name, AbstractMap map)
{
if (_mapStorages.ContainsKey(name)) return; //уникальное имя
else
{
_mapStorages.Add(name, new MapWithSetDoubleDeckerBusGeneric<DrawingObjectBus, AbstractMap>(_pictureWidth, _pictureHeight, map));
}
}
public void DelMap(string name)
{
// TODO Прописать логику для удаления
if (_mapStorages.ContainsKey(name))
{
_mapStorages.Remove(name);
}
}
public MapWithSetDoubleDeckerBusGeneric<DrawingObjectBus, AbstractMap> this[string ind]
{
get
{
// TODO Продумать логику получения объекта
_mapStorages.TryGetValue(ind, out var MapWithSetDoubleDeckerBusGeneric);
return MapWithSetDoubleDeckerBusGeneric;
}
}
}
}

View File

@ -9,13 +9,16 @@ namespace Bus
internal class SetDoubleDeckerBusGeneric<T>
where T : class
{
private readonly T[] _places;
public int Count => _places.Length;
private readonly List<T> _places;
public int Count => _places.Count;
private int BusPlaces = 0;
private readonly int _maxCount;
public SetDoubleDeckerBusGeneric(int count)
{
_places = new T[count];
_maxCount = count;
_places = new List<T>();
}
public int Insert(T bus)
@ -23,40 +26,64 @@ namespace Bus
return Insert(bus, 0);
}
private bool isCorrectPosition(int position)
{
return 0 <= position && position < _maxCount;
}
public int Insert(T bus, int position)
{
if (position < 0 || position >= _places.Length || BusPlaces == _places.Length)
if (!isCorrectPosition(position))
{
return -1;
}
BusPlaces++;
while (_places[position] != null)
{
for (int i = _places.Length - 1; i > 0; --i)
{
if (_places[i] == null && _places[i - 1] != null)
{
_places[i] = _places[i - 1];
_places[i - 1] = null;
}
}
}
_places[position] = bus;
_places.Insert(position, bus);
return position;
}
public T Remove(int position)
{
if (position < 0 || position >= _places.Length) return null;
T savedBus = _places[position];
_places[position] = null;
return savedBus;
if (position < 0 || position >= _maxCount)
{
return null;
}
var result = _places[position];
_places.RemoveAt(position);
return result;
}
public T Get(int position)
public T this[int position]
{
if (position < 0 || position >= _places.Length) return null;
return _places[position];
get
{
if (position >= 0 && position < _maxCount && position < Count)
{
return _places[position];
}
else
{
return null;
}
}
set
{
Insert(value, position);
}
}
public IEnumerable<T> GetBuses()
{
foreach (var bus in _places)
{
if (bus != null)
{
yield return bus;
}
else
{
yield break;
}
}
}
}
}