готовая лаба 4
This commit is contained in:
parent
68d1b76596
commit
4e005d11fe
@ -46,12 +46,17 @@ namespace RoadTrain.DrawingObjects
|
||||
}
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (x >= 0 && x + _roadTrainWidth <= _pictureWidth && y >= 0 && y + _roadTrainHeight <= _pictureHeight)
|
||||
if (x < 0 || x + _roadTrainWidth > _pictureWidth)
|
||||
{
|
||||
x = 0;
|
||||
}
|
||||
if (y < 0 || y + _roadTrainWidth > _pictureHeight)
|
||||
{
|
||||
y = 0;
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
}
|
||||
}
|
||||
public void MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (!CanMove(direction) || EntityRoadTrain == null)
|
||||
|
@ -9,58 +9,66 @@ namespace RoadTrain.Generics
|
||||
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;
|
||||
public readonly int _maxCount;
|
||||
|
||||
public SetGeneric(int count)
|
||||
{
|
||||
_places = new T?[count];
|
||||
_maxCount = count;
|
||||
_places = new List<T>(count);
|
||||
}
|
||||
public int Insert(T roadtrain)
|
||||
public bool Insert(T roadtrain)
|
||||
{
|
||||
return Insert(roadtrain, 0);
|
||||
if (_places.Count == _maxCount)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
public int Insert(T roadtrain, int position)
|
||||
{
|
||||
int nullIndex = -1;
|
||||
int i;
|
||||
|
||||
if (position < 0 || position >= Count)
|
||||
return -1;
|
||||
|
||||
for (i = position; i < Count; i++)
|
||||
{
|
||||
if (_places[i] == null)
|
||||
{
|
||||
nullIndex = i;
|
||||
break;
|
||||
Insert(roadtrain, 0);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (nullIndex < 0)
|
||||
return -1;
|
||||
|
||||
for (i = nullIndex; i > position; i--)
|
||||
public bool Insert(T roadtrain, int position)
|
||||
{
|
||||
_places[i] = _places[i - 1];
|
||||
if (!(position >= 0 && position <= Count && _places.Count < _maxCount))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_places[position] = roadtrain;
|
||||
return position;
|
||||
_places.Insert(position, roadtrain);
|
||||
return true;
|
||||
}
|
||||
public bool Remove(int position)
|
||||
{
|
||||
if (position < 0 || position >= Count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_places[position] = null;
|
||||
_places.RemoveAt(position);
|
||||
return true;
|
||||
}
|
||||
public T? Get(int position)
|
||||
public T? this[int position]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (position < 0 || position >= Count)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _places[position];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (position < 0 || position > Count || Count >= _maxCount)
|
||||
return;
|
||||
|
||||
_places.Insert(position, value);
|
||||
}
|
||||
}
|
||||
public IEnumerable<T?> GetTrains(int? maxTrains = null)
|
||||
{
|
||||
for (int i = 0; i < _places.Count; ++i)
|
||||
{
|
||||
yield return _places[i];
|
||||
if (maxTrains.HasValue && i == maxTrains.Value)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
89
RoadTrain/RoadTrain/TrainCollection.Designer.cs
generated
89
RoadTrain/RoadTrain/TrainCollection.Designer.cs
generated
@ -29,6 +29,11 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
panelCollection = new Panel();
|
||||
groupBoxSets = new GroupBox();
|
||||
textBoxStorageName = new TextBox();
|
||||
buttonDelObject = new Button();
|
||||
buttonnAddObject = new Button();
|
||||
listBoxStorages = new ListBox();
|
||||
maskedTextBoxNumber = new MaskedTextBox();
|
||||
labelCollection = new Label();
|
||||
buttonRefreshCollection = new Button();
|
||||
@ -36,25 +41,83 @@
|
||||
buttonAddTrain = new Button();
|
||||
pictureBoxCollection = new PictureBox();
|
||||
panelCollection.SuspendLayout();
|
||||
groupBoxSets.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
// panelCollection
|
||||
//
|
||||
panelCollection.Controls.Add(groupBoxSets);
|
||||
panelCollection.Controls.Add(maskedTextBoxNumber);
|
||||
panelCollection.Controls.Add(labelCollection);
|
||||
panelCollection.Controls.Add(buttonRefreshCollection);
|
||||
panelCollection.Controls.Add(buttonRemoveTrain);
|
||||
panelCollection.Controls.Add(buttonAddTrain);
|
||||
panelCollection.Location = new Point(611, -1);
|
||||
panelCollection.Location = new Point(763, -1);
|
||||
panelCollection.Margin = new Padding(3, 2, 3, 2);
|
||||
panelCollection.Name = "panelCollection";
|
||||
panelCollection.Size = new Size(191, 451);
|
||||
panelCollection.Size = new Size(191, 402);
|
||||
panelCollection.TabIndex = 3;
|
||||
//
|
||||
// groupBoxSets
|
||||
//
|
||||
groupBoxSets.Controls.Add(textBoxStorageName);
|
||||
groupBoxSets.Controls.Add(buttonDelObject);
|
||||
groupBoxSets.Controls.Add(buttonnAddObject);
|
||||
groupBoxSets.Controls.Add(listBoxStorages);
|
||||
groupBoxSets.Location = new Point(10, 32);
|
||||
groupBoxSets.Margin = new Padding(3, 2, 3, 2);
|
||||
groupBoxSets.Name = "groupBoxSets";
|
||||
groupBoxSets.Padding = new Padding(3, 2, 3, 2);
|
||||
groupBoxSets.Size = new Size(174, 182);
|
||||
groupBoxSets.TabIndex = 7;
|
||||
groupBoxSets.TabStop = false;
|
||||
groupBoxSets.Text = "Наборы";
|
||||
//
|
||||
// textBoxStorageName
|
||||
//
|
||||
textBoxStorageName.Location = new Point(0, 20);
|
||||
textBoxStorageName.Margin = new Padding(3, 2, 3, 2);
|
||||
textBoxStorageName.Name = "textBoxStorageName";
|
||||
textBoxStorageName.Size = new Size(169, 23);
|
||||
textBoxStorageName.TabIndex = 3;
|
||||
//
|
||||
// buttonDelObject
|
||||
//
|
||||
buttonDelObject.Location = new Point(5, 155);
|
||||
buttonDelObject.Margin = new Padding(3, 2, 3, 2);
|
||||
buttonDelObject.Name = "buttonDelObject";
|
||||
buttonDelObject.Size = new Size(164, 22);
|
||||
buttonDelObject.TabIndex = 2;
|
||||
buttonDelObject.Text = "Удалить набор";
|
||||
buttonDelObject.UseVisualStyleBackColor = true;
|
||||
buttonDelObject.Click += ButtonDelObject_Click;
|
||||
//
|
||||
// buttonnAddObject
|
||||
//
|
||||
buttonnAddObject.Location = new Point(4, 53);
|
||||
buttonnAddObject.Margin = new Padding(3, 2, 3, 2);
|
||||
buttonnAddObject.Name = "buttonnAddObject";
|
||||
buttonnAddObject.Size = new Size(165, 22);
|
||||
buttonnAddObject.TabIndex = 1;
|
||||
buttonnAddObject.Text = "Добавить набор";
|
||||
buttonnAddObject.UseVisualStyleBackColor = true;
|
||||
buttonnAddObject.Click += ButtonAddObject_Click;
|
||||
//
|
||||
// listBoxStorages
|
||||
//
|
||||
listBoxStorages.FormattingEnabled = true;
|
||||
listBoxStorages.ItemHeight = 15;
|
||||
listBoxStorages.Location = new Point(5, 88);
|
||||
listBoxStorages.Margin = new Padding(3, 2, 3, 2);
|
||||
listBoxStorages.Name = "listBoxStorages";
|
||||
listBoxStorages.Size = new Size(164, 64);
|
||||
listBoxStorages.TabIndex = 0;
|
||||
listBoxStorages.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged;
|
||||
//
|
||||
// maskedTextBoxNumber
|
||||
//
|
||||
maskedTextBoxNumber.Location = new Point(14, 88);
|
||||
maskedTextBoxNumber.Location = new Point(14, 281);
|
||||
maskedTextBoxNumber.Margin = new Padding(3, 2, 3, 2);
|
||||
maskedTextBoxNumber.Name = "maskedTextBoxNumber";
|
||||
maskedTextBoxNumber.Size = new Size(150, 23);
|
||||
@ -71,7 +134,7 @@
|
||||
//
|
||||
// buttonRefreshCollection
|
||||
//
|
||||
buttonRefreshCollection.Location = new Point(14, 179);
|
||||
buttonRefreshCollection.Location = new Point(14, 362);
|
||||
buttonRefreshCollection.Margin = new Padding(3, 2, 3, 2);
|
||||
buttonRefreshCollection.Name = "buttonRefreshCollection";
|
||||
buttonRefreshCollection.Size = new Size(150, 30);
|
||||
@ -82,7 +145,7 @@
|
||||
//
|
||||
// buttonRemoveTrain
|
||||
//
|
||||
buttonRemoveTrain.Location = new Point(14, 124);
|
||||
buttonRemoveTrain.Location = new Point(14, 318);
|
||||
buttonRemoveTrain.Margin = new Padding(3, 2, 3, 2);
|
||||
buttonRemoveTrain.Name = "buttonRemoveTrain";
|
||||
buttonRemoveTrain.Size = new Size(150, 30);
|
||||
@ -93,7 +156,7 @@
|
||||
//
|
||||
// buttonAddTrain
|
||||
//
|
||||
buttonAddTrain.Location = new Point(14, 28);
|
||||
buttonAddTrain.Location = new Point(14, 232);
|
||||
buttonAddTrain.Margin = new Padding(3, 2, 3, 2);
|
||||
buttonAddTrain.Name = "buttonAddTrain";
|
||||
buttonAddTrain.Size = new Size(150, 30);
|
||||
@ -104,10 +167,10 @@
|
||||
//
|
||||
// pictureBoxCollection
|
||||
//
|
||||
pictureBoxCollection.Location = new Point(-1, -1);
|
||||
pictureBoxCollection.Location = new Point(2, -1);
|
||||
pictureBoxCollection.Margin = new Padding(3, 2, 3, 2);
|
||||
pictureBoxCollection.Name = "pictureBoxCollection";
|
||||
pictureBoxCollection.Size = new Size(609, 451);
|
||||
pictureBoxCollection.Size = new Size(756, 402);
|
||||
pictureBoxCollection.TabIndex = 2;
|
||||
pictureBoxCollection.TabStop = false;
|
||||
//
|
||||
@ -115,13 +178,14 @@
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(800, 450);
|
||||
ClientSize = new Size(956, 400);
|
||||
Controls.Add(panelCollection);
|
||||
Controls.Add(pictureBoxCollection);
|
||||
Name = "TrainCollection";
|
||||
Text = "TrainCollection";
|
||||
panelCollection.ResumeLayout(false);
|
||||
panelCollection.PerformLayout();
|
||||
groupBoxSets.ResumeLayout(false);
|
||||
groupBoxSets.PerformLayout();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
|
||||
ResumeLayout(false);
|
||||
}
|
||||
@ -129,6 +193,11 @@
|
||||
#endregion
|
||||
|
||||
private Panel panelCollection;
|
||||
private GroupBox groupBoxSets;
|
||||
private TextBox textBoxStorageName;
|
||||
private Button buttonDelObject;
|
||||
private Button buttonnAddObject;
|
||||
private ListBox listBoxStorages;
|
||||
private MaskedTextBox maskedTextBoxNumber;
|
||||
private Label labelCollection;
|
||||
private Button buttonRefreshCollection;
|
||||
|
@ -17,23 +17,79 @@ namespace RoadTrain
|
||||
public partial class TrainCollection : Form
|
||||
{
|
||||
|
||||
private readonly TrainsGenericCollection<DrawingRoadTrain,
|
||||
DrawingObjectTrain> _trains;
|
||||
private readonly TrainsGenericStorage _storage;
|
||||
|
||||
public TrainCollection()
|
||||
{
|
||||
InitializeComponent();
|
||||
_trains = new TrainsGenericCollection<DrawingRoadTrain, DrawingObjectTrain>
|
||||
(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||||
_storage = new TrainsGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||||
}
|
||||
private void ReloadObjects()
|
||||
{
|
||||
int index = listBoxStorages.SelectedIndex;
|
||||
listBoxStorages.Items.Clear();
|
||||
for (int i = 0; i < _storage.Keys.Count; i++)
|
||||
{
|
||||
listBoxStorages.Items.Add(_storage.Keys[i]);
|
||||
}
|
||||
if (listBoxStorages.Items.Count > 0 && (index == -1 || index
|
||||
>= listBoxStorages.Items.Count))
|
||||
{
|
||||
listBoxStorages.SelectedIndex = 0;
|
||||
}
|
||||
else if (listBoxStorages.Items.Count > 0 && index > -1 &&
|
||||
index < listBoxStorages.Items.Count)
|
||||
{
|
||||
listBoxStorages.SelectedIndex = index;
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e)
|
||||
{
|
||||
pictureBoxCollection.Image =
|
||||
_storage[listBoxStorages.SelectedItem?.ToString() ?? string.Empty]?.ShowTrains();
|
||||
}
|
||||
private void ButtonDelObject_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (MessageBox.Show($"Удалить объект {listBoxStorages.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
||||
{
|
||||
_storage.DelSet(listBoxStorages.SelectedItem.ToString() ?? string.Empty);
|
||||
ReloadObjects();
|
||||
}
|
||||
}
|
||||
private void ButtonAddTrain_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||||
string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
RoadTrain form = new();
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (_trains + form.SelectedTrain != -1)
|
||||
if (obj + form.SelectedTrain)
|
||||
{
|
||||
MessageBox.Show("Объект добавлен");
|
||||
pictureBoxCollection.Image = _trains.ShowTrains();
|
||||
pictureBoxCollection.Image = obj.ShowTrains();
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -43,16 +99,24 @@ namespace RoadTrain
|
||||
}
|
||||
private void ButtonRemoveTrain_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (MessageBox.Show("Удалить объект?", "Удаление",
|
||||
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.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 (_trains - pos != null)
|
||||
if (obj - pos != null)
|
||||
{
|
||||
MessageBox.Show("Объект удален");
|
||||
pictureBoxCollection.Image = _trains.ShowTrains();
|
||||
pictureBoxCollection.Image = obj.ShowTrains();
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -61,7 +125,19 @@ namespace RoadTrain
|
||||
}
|
||||
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
||||
{
|
||||
pictureBoxCollection.Image = _trains.ShowTrains();
|
||||
}
|
||||
if (listBoxStorages.SelectedIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var obj = _storage[listBoxStorages.SelectedItem.ToString() ??
|
||||
string.Empty];
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
pictureBoxCollection.Image = obj.ShowTrains();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using RoadTrain.DrawingObjects;
|
||||
using RoadTrain.MovementStrategy;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
||||
|
||||
namespace RoadTrain.Generics
|
||||
{
|
||||
@ -24,28 +27,26 @@ namespace RoadTrain.Generics
|
||||
_pictureHeight = picHeight;
|
||||
_collection = new SetGeneric<T>(width * height);
|
||||
}
|
||||
public static int operator +(TrainsGenericCollection<T, U> collect, T?
|
||||
obj)
|
||||
public static bool operator +(TrainsGenericCollection<T, U> collect, T? obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
return collect._collection.Insert(obj);
|
||||
return (bool)collect?._collection.Insert(obj);
|
||||
}
|
||||
public static bool operator -(TrainsGenericCollection<T, U> collect, int
|
||||
pos)
|
||||
public static T? operator -(TrainsGenericCollection<T, U> collect, int pos)
|
||||
{
|
||||
T? obj = collect._collection.Get(pos);
|
||||
T? obj = collect._collection[pos];
|
||||
if (obj != null)
|
||||
{
|
||||
return collect._collection.Remove(pos);
|
||||
collect._collection.Remove(pos);
|
||||
}
|
||||
return false;
|
||||
return obj;
|
||||
}
|
||||
public U? GetU(int pos)
|
||||
{
|
||||
return (U?)_collection.Get(pos)?.GetMoveableObject;
|
||||
return (U?)_collection[pos]?.GetMoveableObject;
|
||||
}
|
||||
public Bitmap ShowTrains()
|
||||
{
|
||||
@ -73,17 +74,18 @@ namespace RoadTrain.Generics
|
||||
}
|
||||
private void DrawObjects(Graphics g)
|
||||
{
|
||||
for (int i = 0; i < _collection.Count; i++)
|
||||
int i = 0;
|
||||
foreach (var trains in _collection.GetTrains())
|
||||
{
|
||||
DrawingRoadTrain train = _collection.Get(i);
|
||||
|
||||
if (train != null)
|
||||
if (trains != null)
|
||||
{
|
||||
int width = _pictureWidth / _placeSizeWidth;
|
||||
train.SetPosition(i % width * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
|
||||
train.DrawTransport(g);
|
||||
}
|
||||
trains.SetPosition(i % width * _placeSizeWidth, _pictureHeight - _pictureHeight % _placeSizeHeight - (i / width + 1) * _placeSizeHeight);
|
||||
trains.DrawTransport(g);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
47
RoadTrain/RoadTrain/TrainsGenericStorage.cs
Normal file
47
RoadTrain/RoadTrain/TrainsGenericStorage.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using RoadTrain.DrawingObjects;
|
||||
using RoadTrain.MovementStrategy;
|
||||
|
||||
namespace RoadTrain.Generics
|
||||
{
|
||||
internal class TrainsGenericStorage
|
||||
{
|
||||
readonly Dictionary<string, TrainsGenericCollection<DrawingRoadTrain, DrawingObjectTrain>> _trainStorages;
|
||||
public List<string> Keys => _trainStorages.Keys.ToList();
|
||||
private readonly int _pictureWidth;
|
||||
private readonly int _pictureHeight;
|
||||
public TrainsGenericStorage(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_trainStorages = new Dictionary<string,
|
||||
TrainsGenericCollection<DrawingRoadTrain, DrawingObjectTrain>>();
|
||||
_pictureWidth = pictureWidth;
|
||||
_pictureHeight = pictureHeight;
|
||||
}
|
||||
public void AddSet(string name)
|
||||
{
|
||||
if (!_trainStorages.ContainsKey(name))
|
||||
_trainStorages.Add(name, new TrainsGenericCollection<DrawingRoadTrain, DrawingObjectTrain>(_pictureWidth, _pictureHeight));
|
||||
}
|
||||
public void DelSet(string name)
|
||||
{
|
||||
if (_trainStorages.ContainsKey(name))
|
||||
_trainStorages.Remove(name);
|
||||
}
|
||||
public TrainsGenericCollection<DrawingRoadTrain, DrawingObjectTrain>?
|
||||
this[string ind]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_trainStorages.ContainsKey(ind))
|
||||
{
|
||||
return _trainStorages[ind];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user