Yahontov O.U. LabWork3 #4

Closed
Arklightning wants to merge 5 commits from LabWork3 into LabWork2
17 changed files with 837 additions and 44 deletions

View File

@ -0,0 +1,30 @@
using System.Threading;
using Trolleybus;
namespace Trolleybus
{
internal class AutoStopMap : SimpleMap
{
protected override void GenerateMap()
{
_map = new int[100, 100];
_size_x = (float)_width / _map.GetLength(0);
_size_y = (float)_height / _map.GetLength(1);
for (int i = 0; i < _map.GetLength(0); ++i)
{
for (int j = 0; j < _map.GetLength(1); ++j)
{
_map[i, j] = _freeRoad;
}
}
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++)
{
_map[i, j] = _barrier;
}
}
}
}
}

View File

@ -9,7 +9,7 @@ namespace Trolleybus
/// <summary>
/// Направление перемещения
/// </summary>
internal enum Direction
public enum Direction
{
None = 0,
Up = 1,

View File

@ -9,10 +9,10 @@ namespace Trolleybus
{
internal class DrawningSmallTrolleybus : DrawingTrolleybus
{
public DrawningSmallTrolleybus(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool wing, bool horns, bool battary) :
public DrawningSmallTrolleybus(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool horns, bool battary) :
base(speed, weight, bodyColor, 110, 60)
{
Trolleybus = new EntitySmallTrolleybus(speed, weight, bodyColor, dopColor, bodyKit, wing, horns, battary);
Trolleybus = new EntitySmallTrolleybus(speed, weight, bodyColor, dopColor,bodyKit, horns, battary);
}
public override void DrawTransport(Graphics g)
@ -29,7 +29,6 @@ namespace Trolleybus
Brush bWhite = new SolidBrush(Color.White);
Pen WinBlue = new Pen(Color.Blue);
//_startPosY = _startPosY + 100;
base.DrawTransport(g);
if (smallTrolleybus.BodyKit)
{
@ -55,17 +54,11 @@ namespace Trolleybus
g.DrawEllipse(pen, _startPosX + 170, _startPosY + 40, 30, 30);
}
//_startPosX += 10;
//_startPosY += 5;
//base.DrawTransport(g);
//_startPosX -= 10;
//_startPosY -= 5;
if (smallTrolleybus.Horns)
{
g.DrawLine(pen, _startPosX + 100, _startPosY - 10, _startPosX + 150, _startPosY - 20);
g.DrawLine(pen, _startPosX + 150, _startPosY - 20, _startPosX, _startPosY - 30);
//g.DrawRectangle(pen, _startPosX + 50, _startPosY - 40, 100, 20);
g.DrawRectangle(pen, _startPosX + 50, _startPosY - 10, 100, 10);
}
@ -73,12 +66,6 @@ namespace Trolleybus
{
g.DrawRectangle(pen, _startPosX + 100, _startPosY, 10, 10);
}
if (smallTrolleybus.Wing)
{
//g.FillRectangle(dopBrush, _startPosX, _startPosY + 5, 10, 50);
//g.DrawRectangle(pen, _startPosX, _startPosY + 5, 10, 50);
}
}
}
}

View File

@ -11,7 +11,7 @@ namespace Trolleybus
/// <summary>
/// Класс прорисовки и перемещения объекта
/// </summary>
internal class DrawingTrolleybus
public class DrawingTrolleybus
{
public EntityTrolleybus Trolleybus { get; protected set; }
/// <summary>

View File

@ -36,7 +36,6 @@ namespace Trolleybus
public void SetObject(int x, int y, int width, int height)
{
// _trolleybus.SetPosition(x, y, startX, startY, width, height);
_trolleybus.SetPosition(x, y, width, height);
}

View File

@ -18,10 +18,6 @@ namespace Trolleybus
/// </summary>
public bool BodyKit { get; private set; }
/// <summary>
/// Признак наличия антикрыла
/// </summary>
public bool Wing { get; private set; }
/// <summary>
/// Признак наличия гоночной полосы
/// </summary>
public bool Horns { get; private set; }
@ -34,15 +30,13 @@ namespace Trolleybus
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="dopColor">Дополнительный цвет</param>
/// <param name="bodyKit">Признак наличия обвеса</param>
/// <param name="wing">Признак наличия антикрыла</param>
/// <param name="horns">Признак наличия гоночной полосы</param>
/// /// <param name="battary">Признак наличия гоночной полосы</param>
public EntitySmallTrolleybus(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool wing, bool horns, bool battary) :
public EntitySmallTrolleybus(int speed, float weight, Color bodyColor, Color dopColor, bool bodyKit, bool horns, bool battary) :
base(speed, weight, bodyColor)
{
DopColor = dopColor;
BodyKit = bodyKit;
Wing = wing;
Horns = horns;
Battary = battary;
}

View File

@ -10,7 +10,7 @@ namespace Trolleybus
/// <summary>
/// Класс-сущность "Троллейбус"
/// </summary>
internal class EntityTrolleybus
public class EntityTrolleybus
{
/// <summary>
/// Скорость

View File

@ -40,6 +40,7 @@ namespace Trolleybus
this.buttonDown = new System.Windows.Forms.Button();
this.pictureBoxTrolleybus = new System.Windows.Forms.PictureBox();
this.buttonCreateModif = new System.Windows.Forms.Button();
this.buttonSelectTrolleybus = new System.Windows.Forms.Button();
this.statusStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxTrolleybus)).BeginInit();
this.SuspendLayout();
@ -146,11 +147,22 @@ namespace Trolleybus
this.buttonCreateModif.UseVisualStyleBackColor = true;
this.buttonCreateModif.Click += new System.EventHandler(this.buttonCreateModif_Click);
//
// buttonSelectTrolleybus
//
this.buttonSelectTrolleybus.Location = new System.Drawing.Point(578, 390);
this.buttonSelectTrolleybus.Name = "buttonSelectTrolleybus";
this.buttonSelectTrolleybus.Size = new System.Drawing.Size(75, 23);
this.buttonSelectTrolleybus.TabIndex = 8;
this.buttonSelectTrolleybus.Text = "Выбрать";
this.buttonSelectTrolleybus.UseVisualStyleBackColor = true;
this.buttonSelectTrolleybus.Click += new System.EventHandler(this.buttonSelectTrolleybus_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.buttonSelectTrolleybus);
this.Controls.Add(this.buttonCreateModif);
this.Controls.Add(this.buttonUp);
this.Controls.Add(this.buttonRight);
@ -182,6 +194,7 @@ namespace Trolleybus
private System.Windows.Forms.Button buttonRight;
private System.Windows.Forms.Button buttonUp;
private System.Windows.Forms.Button buttonCreateModif;
private System.Windows.Forms.Button buttonSelectTrolleybus;
}
}

View File

@ -13,6 +13,7 @@ namespace Trolleybus
public partial class Form1 : Form
{
private DrawingTrolleybus _trolleybus;
public DrawingTrolleybus SelectedTrolleybus { get; private set; }
public Form1()
{
InitializeComponent();
@ -32,10 +33,6 @@ namespace Trolleybus
private void SetData()
{
Random rnd = new Random();
//Bitmap bmp = new Bitmap(pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
//Graphics gr = Graphics.FromImage(bmp);
//_trolleybus?.DrawTransport(gr);
//pictureBoxTrolleybus.Image = bmp;
_trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
toolStripStatusLabelSpeed.Text = $"Скорость: {_trolleybus.Trolleybus.Speed}";
toolStripStatusLabelWeight.Text = $"Вес: {_trolleybus.Trolleybus.Weight}";
@ -50,13 +47,13 @@ namespace Trolleybus
private void buttonCreate_Click(object sender, EventArgs e)
{
Random rnd = new Random();
_trolleybus = new DrawingTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)));
// _trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), Location.X, Location.Y, pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
//_trolleybus.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100), pictureBoxTrolleybus.Width, pictureBoxTrolleybus.Height);
//toolStripStatusLabelSpeed.Text = $"Скорость: {_trolleybus.Trolleybus.Speed}";
//toolStripStatusLabelWeight.Text = $"Вес: {_trolleybus.Trolleybus.Weight}";
//toolStripStatusLabelBodyColor.Text = $"Цвет: {_trolleybus.Trolleybus.BodyColor.Name}";
Random random = new();
Color myColor = new Color();
ColorDialog MyDialog = new ColorDialog();
if (MyDialog.ShowDialog() == DialogResult.OK)
myColor = MyDialog.Color;
_trolleybus = new DrawingTrolleybus(random.Next(30, 50), random.Next(1000, 2000),
myColor);
SetData();
Draw();
}
@ -90,13 +87,28 @@ namespace Trolleybus
private void buttonCreateModif_Click(object sender, EventArgs e)
{
Random rnd = new Random();
_trolleybus = new DrawningSmallTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
Random random = new();
Color firstColor = new Color();
Color secondColor = new Color();
ColorDialog MyDialog = new ColorDialog();
if (MyDialog.ShowDialog() == DialogResult.OK)
firstColor = MyDialog.Color;
MyDialog = new ColorDialog();
if (MyDialog.ShowDialog() == DialogResult.OK)
secondColor = MyDialog.Color;
_trolleybus = new DrawningSmallTrolleybus(random.Next(100, 300), random.Next(1000, 2000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
SetData();
Draw();
}
private void buttonSelectTrolleybus_Click(object sender, EventArgs e)
{
SelectedTrolleybus = _trolleybus;
DialogResult = DialogResult.OK;
}
}
}

View File

@ -80,7 +80,7 @@ namespace Trolleybus
var trolleybus = new DrawningSmallTrolleybus(rnd.Next(100, 300), rnd.Next(1000, 2000),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)),
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)));
SetData(trolleybus);
}
/// <summary>

View File

@ -0,0 +1,210 @@
namespace Trolleybus
{
partial class FormMapWithSetTrolleybus
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.buttonLeft = new System.Windows.Forms.Button();
this.buttonRight = new System.Windows.Forms.Button();
this.buttonDown = new System.Windows.Forms.Button();
this.buttonUp = new System.Windows.Forms.Button();
this.buttonShowOnMap = new System.Windows.Forms.Button();
this.buttonShowStorage = new System.Windows.Forms.Button();
this.buttonRemoveTrolleybus = new System.Windows.Forms.Button();
this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox();
this.buttonAddTrolleybus = new System.Windows.Forms.Button();
this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox();
this.pictureBox = new System.Windows.Forms.PictureBox();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.buttonLeft);
this.groupBox1.Controls.Add(this.buttonRight);
this.groupBox1.Controls.Add(this.buttonDown);
this.groupBox1.Controls.Add(this.buttonUp);
this.groupBox1.Controls.Add(this.buttonShowOnMap);
this.groupBox1.Controls.Add(this.buttonShowStorage);
this.groupBox1.Controls.Add(this.buttonRemoveTrolleybus);
this.groupBox1.Controls.Add(this.maskedTextBoxPosition);
this.groupBox1.Controls.Add(this.buttonAddTrolleybus);
this.groupBox1.Controls.Add(this.comboBoxSelectorMap);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.groupBox1.Location = new System.Drawing.Point(600, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(200, 450);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Инструменты";
//
// buttonLeft
//
this.buttonLeft.BackgroundImage = global::Trolleybus.Properties.Resources.left30;
this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonLeft.Location = new System.Drawing.Point(59, 402);
this.buttonLeft.Name = "buttonLeft";
this.buttonLeft.Size = new System.Drawing.Size(32, 31);
this.buttonLeft.TabIndex = 9;
this.buttonLeft.UseVisualStyleBackColor = true;
this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonRight
//
this.buttonRight.BackgroundImage = global::Trolleybus.Properties.Resources.right30;
this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonRight.Location = new System.Drawing.Point(135, 402);
this.buttonRight.Name = "buttonRight";
this.buttonRight.Size = new System.Drawing.Size(32, 31);
this.buttonRight.TabIndex = 8;
this.buttonRight.UseVisualStyleBackColor = true;
this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonDown
//
this.buttonDown.BackgroundImage = global::Trolleybus.Properties.Resources.down30;
this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonDown.Location = new System.Drawing.Point(97, 402);
this.buttonDown.Name = "buttonDown";
this.buttonDown.Size = new System.Drawing.Size(32, 31);
this.buttonDown.TabIndex = 7;
this.buttonDown.UseVisualStyleBackColor = true;
this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonUp
//
this.buttonUp.BackgroundImage = global::Trolleybus.Properties.Resources.up30;
this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.buttonUp.Location = new System.Drawing.Point(97, 365);
this.buttonUp.Name = "buttonUp";
this.buttonUp.Size = new System.Drawing.Size(32, 31);
this.buttonUp.TabIndex = 6;
this.buttonUp.UseVisualStyleBackColor = true;
this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click);
//
// buttonShowOnMap
//
this.buttonShowOnMap.Location = new System.Drawing.Point(7, 256);
this.buttonShowOnMap.Name = "buttonShowOnMap";
this.buttonShowOnMap.Size = new System.Drawing.Size(181, 23);
this.buttonShowOnMap.TabIndex = 5;
this.buttonShowOnMap.Text = "Посмотреть карту";
this.buttonShowOnMap.UseVisualStyleBackColor = true;
this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click);
//
// buttonShowStorage
//
this.buttonShowStorage.Location = new System.Drawing.Point(7, 216);
this.buttonShowStorage.Name = "buttonShowStorage";
this.buttonShowStorage.Size = new System.Drawing.Size(181, 23);
this.buttonShowStorage.TabIndex = 4;
this.buttonShowStorage.Text = "Посмотреть хранилище";
this.buttonShowStorage.UseVisualStyleBackColor = true;
this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click);
//
// buttonRemoveTrolleybus
//
this.buttonRemoveTrolleybus.Location = new System.Drawing.Point(6, 170);
this.buttonRemoveTrolleybus.Name = "buttonRemoveTrolleybus";
this.buttonRemoveTrolleybus.Size = new System.Drawing.Size(182, 23);
this.buttonRemoveTrolleybus.TabIndex = 3;
this.buttonRemoveTrolleybus.Text = "Удалить троллейбус";
this.buttonRemoveTrolleybus.UseVisualStyleBackColor = true;
this.buttonRemoveTrolleybus.Click += new System.EventHandler(this.ButtonRemoveTrolleybus_Click);
//
// maskedTextBoxPosition
//
this.maskedTextBoxPosition.Location = new System.Drawing.Point(7, 128);
this.maskedTextBoxPosition.Name = "maskedTextBoxPosition";
this.maskedTextBoxPosition.Size = new System.Drawing.Size(181, 20);
this.maskedTextBoxPosition.TabIndex = 2;
//
// buttonAddTrolleybus
//
this.buttonAddTrolleybus.Location = new System.Drawing.Point(7, 85);
this.buttonAddTrolleybus.Name = "buttonAddTrolleybus";
this.buttonAddTrolleybus.Size = new System.Drawing.Size(181, 23);
this.buttonAddTrolleybus.TabIndex = 1;
this.buttonAddTrolleybus.Text = "Добавить троллейбус";
this.buttonAddTrolleybus.UseVisualStyleBackColor = true;
this.buttonAddTrolleybus.Click += new System.EventHandler(this.ButtonAddTrolleybus_Click);
//
// comboBoxSelectorMap
//
this.comboBoxSelectorMap.FormattingEnabled = true;
this.comboBoxSelectorMap.Items.AddRange(new object[] {
"Простая карта",
"Сложная карта"});
this.comboBoxSelectorMap.Location = new System.Drawing.Point(6, 42);
this.comboBoxSelectorMap.Name = "comboBoxSelectorMap";
this.comboBoxSelectorMap.Size = new System.Drawing.Size(182, 21);
this.comboBoxSelectorMap.TabIndex = 0;
this.comboBoxSelectorMap.SelectedIndexChanged += new System.EventHandler(this.ComboBoxSelectorMap_SelectedIndexChanged);
//
// pictureBox
//
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(600, 450);
this.pictureBox.TabIndex = 1;
this.pictureBox.TabStop = false;
//
// FormMapWithSetTrolleybus
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.pictureBox);
this.Controls.Add(this.groupBox1);
this.Name = "FormMapWithSetTrolleybus";
this.Text = "FormMapWithSetTrolleybus";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button buttonAddTrolleybus;
private System.Windows.Forms.ComboBox comboBoxSelectorMap;
private System.Windows.Forms.PictureBox pictureBox;
private System.Windows.Forms.Button buttonLeft;
private System.Windows.Forms.Button buttonRight;
private System.Windows.Forms.Button buttonDown;
private System.Windows.Forms.Button buttonUp;
private System.Windows.Forms.Button buttonShowOnMap;
private System.Windows.Forms.Button buttonShowStorage;
private System.Windows.Forms.Button buttonRemoveTrolleybus;
private System.Windows.Forms.MaskedTextBox maskedTextBoxPosition;
}
}

View File

@ -0,0 +1,147 @@
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;
namespace Trolleybus
{
public partial class FormMapWithSetTrolleybus : Form
{
public FormMapWithSetTrolleybus()
{
InitializeComponent();
}
private MapWithSetTrolleybusGeneric<DrawningObjectTrolleybus, AbstractMap> _mapTrolleybusCollectionGeneric;
private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e)
{
AbstractMap map = null;
switch (comboBoxSelectorMap.Text)
{
case "Простая карта":
map = new SimpleMap();
break;
case "Сложная карта":
map = new AutoStopMap();
break;
}
if (map != null)
{
_mapTrolleybusCollectionGeneric = new MapWithSetTrolleybusGeneric<DrawningObjectTrolleybus, AbstractMap>(
pictureBox.Width, pictureBox.Height, map);
}
else
{
_mapTrolleybusCollectionGeneric = null;
}
}
private void ButtonAddTrolleybus_Click(object sender, EventArgs e)
{
if (_mapTrolleybusCollectionGeneric == null)
{
return;
}
Form1 form = new();
if (form.ShowDialog() == DialogResult.OK)
{
DrawningObjectTrolleybus trolleybus = new(form.SelectedTrolleybus);
if (_mapTrolleybusCollectionGeneric + trolleybus !=-1)
{
MessageBox.Show("Объект добавлен");
pictureBox.Image = _mapTrolleybusCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
/// <summary>
/// Удаление объекта
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemoveTrolleybus_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;
}
if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_mapTrolleybusCollectionGeneric - pos !=null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapTrolleybusCollectionGeneric.ShowSet();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Вывод набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonShowStorage_Click(object sender, EventArgs e)
{
if (_mapTrolleybusCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapTrolleybusCollectionGeneric.ShowSet();
}
/// <summary>
/// Вывод карты
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonShowOnMap_Click(object sender, EventArgs e)
{
if (_mapTrolleybusCollectionGeneric == null)
{
return;
}
pictureBox.Image = _mapTrolleybusCollectionGeneric.ShowOnMap();
}
/// <summary>
/// Перемещение
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonMove_Click(object sender, EventArgs e)
{
if (_mapTrolleybusCollectionGeneric == null)
{
return;
}
//получаем имя кнопки
string name = ((Button)sender)?.Name ?? string.Empty;
Direction dir = Direction.None;
switch (name)
{
case "buttonUp":
dir = Direction.Up;
break;
case "buttonDown":
dir = Direction.Down;
break;
case "buttonLeft":
dir = Direction.Left;
break;
case "buttonRight":
dir = Direction.Right;
break;
}
pictureBox.Image = _mapTrolleybusCollectionGeneric.MoveObject(dir);
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -0,0 +1,188 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace Trolleybus
{
internal class MapWithSetTrolleybusGeneric<T, U>
where T : class, IDrawningObject
where U : AbstractMap
{
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private readonly int _placeSizeWidth = 210;
/// <summary>
/// Размер занимаемого объектом места (высота)
/// </summary>
private readonly int _placeSizeHeight = 90;
/// <summary>
/// Набор объектов
/// </summary>
private readonly SetTrolleybusGeneric<T> _setTrolleybus;
/// <summary>
/// Карта
/// </summary>
private readonly U _map;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
/// <param name="map"></param>
public MapWithSetTrolleybusGeneric(int picWidth, int picHeight, U map)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_setTrolleybus = new SetTrolleybusGeneric<T>(width * height);
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_map = map;
}
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="map"></param>
/// <param name="trolleybus"></param>
/// <returns></returns>
public static int operator +(MapWithSetTrolleybusGeneric<T, U> map, T trolleybus)
{
return map._setTrolleybus.Insert(trolleybus);
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="map"></param>
/// <param name="position"></param>
/// <returns></returns>
public static T operator -(MapWithSetTrolleybusGeneric<T, U> map, int position)
{
return map._setTrolleybus.Remove(position);
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowSet()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawTrolleybus(gr);
return bmp;
}
/// <summary>
/// Просмотр объекта на карте
/// </summary>
/// <returns></returns>
public Bitmap ShowOnMap()
{
Shaking();
for (int i = 0; i < _setTrolleybus.Count; i++)
{
var car = _setTrolleybus.Get(i);
if (car != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
}
}
return new(_pictureWidth, _pictureHeight);
}
/// <summary>
/// Перемещение объекта по крате
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
public Bitmap MoveObject(Direction direction)
{
if (_map != null)
{
return _map.MoveObject(direction);
}
return new(_pictureWidth, _pictureHeight);
}
/// <summary>
/// "Взбалтываем" набор, чтобы все элементы оказались в начале
/// </summary>
private void Shaking()
{
int j = _setTrolleybus.Count - 1;
for (int i = 0; i < _setTrolleybus.Count; i++)
{
if (_setTrolleybus.Get(i) == null)
{
for (; j > i; j--)
{
var car = _setTrolleybus.Get(j);
if (car != null)
{
_setTrolleybus.Insert(car, i);
_setTrolleybus.Remove(j);
break;
}
}
if (j <= i)
{
return;
}
}
}
}
/// <summary>
/// Метод отрисовки фона
/// </summary>
/// <param name="g"></param>
private void DrawBackground(Graphics g)
{
Pen pen = new(Color.Black, 3);
for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++)
{
for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j)
{//линия рамзетки места
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight);
g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight + 10, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight + 10);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight);
}
}
/// <summary>
/// Метод прорисовки объектов
/// </summary>
/// <param name="g"></param>
private void DrawTrolleybus(Graphics g)
{
int xForLocomotive = _pictureWidth - 2 * _placeSizeWidth + 60;
int yForLocomotive = 10;
int countInRow = 0;
for (int i = 0; i < _setTrolleybus.Count; i++)
{
if (countInRow >= _pictureWidth / (_placeSizeWidth + 30))
{
xForLocomotive = _pictureWidth - _placeSizeWidth + 40;
yForLocomotive += _placeSizeHeight;
countInRow = 0;
}
if (_setTrolleybus.Get(i) != null)
{
T locomotive = _setTrolleybus.Get(i);
locomotive.SetObject(xForLocomotive, yForLocomotive, _pictureWidth, _pictureHeight);
locomotive.DrawningObject(g);
}
xForLocomotive -= _placeSizeWidth + 30;
countInRow++;
}
}
}
}

View File

@ -16,7 +16,7 @@ namespace Trolleybus
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMap());
Application.Run(new FormMapWithSetTrolleybus());
}
}
}

View File

@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Trolleybus
{
// Параметризованный набор объектов
internal class SetTrolleybusGeneric<T>
where T : class
{
// Массив объектов, которые храним
private readonly T[] _places;
// Количество объектов в массиве
public int Count => _places.Length;
// Конструктор
public SetTrolleybusGeneric(int count)
{
_places = new T[count];
}
// Добавление объекта в набор
public int Insert(T trolleybus)
{
// вставка в начало набора
return Insert(trolleybus, 0);
}
// Добавление объекта в набор на конкретную позицию
public int Insert(T trolleybus, int position)
{
// проверка позиции
if (position >= _places.Length || position < 0)
return -1;
//проверка, что элемент массива по этой позиции пустой, если нет, то
if (_places[position] == null)
{
_places[position] = trolleybus;
return position;
}
//проверка, что после вставляемого элемента в массиве есть пустой элемент
int findEmptyPos = -1;
for (int i = position + 1; i < Count; i++)
{
if (_places[i] == null)
{
findEmptyPos = i;
break;
}
}
if (findEmptyPos < 0) return -1;
//сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
for (int i = findEmptyPos; i > position; i--)
{
_places[i] = _places[i - 1];
}
// вставка по позиции
_places[position] = trolleybus;
return position;
}
// Удаление объекта из набора с конкретной позиции
public T Remove(int position)
{
// проверка позиции
if (position >= _places.Length || position < 0) return null;
// удаление объекта из массива, присовив элементу массива значение null
T temp = _places[position];
_places[position] = null;
return temp;
}
// Получение объекта из набора по позиции
public T Get(int position)
{
// проверка позиции
if (position >= _places.Length || position < 0)
return null;
return _places[position];
}
}
}

View File

@ -67,9 +67,18 @@
<Compile Include="FormMap.Designer.cs">
<DependentUpon>FormMap.cs</DependentUpon>
</Compile>
<Compile Include="FormMapWithSetTrolleybus.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="FormMapWithSetTrolleybus.Designer.cs">
<DependentUpon>FormMapWithSetTrolleybus.cs</DependentUpon>
</Compile>
<Compile Include="HardMap.cs" />
<Compile Include="IDrawingObject.cs" />
<Compile Include="MapWithSetTrolleybusGeneric.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SetTrolleybusGeneric.cs" />
<Compile Include="SimpleMap.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
@ -77,6 +86,9 @@
<EmbeddedResource Include="FormMap.resx">
<DependentUpon>FormMap.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="FormMapWithSetTrolleybus.resx">
<DependentUpon>FormMapWithSetTrolleybus.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>