Создание классов

This commit is contained in:
KirillFirsof 2023-11-09 14:31:52 +04:00
parent 07e6b5fba6
commit a666cfd261
7 changed files with 590 additions and 31 deletions

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectTractor.Entities;
using ProjectTractor.MovementStrategy;
namespace ProjectTractor.DrawningObjects {
@ -79,6 +80,12 @@ namespace ProjectTractor.DrawningObjects {
EntityTractor = new EntityTractor(speed, weight, bodyColor);
}
/// <summary>
/// Получение объекта IMoveableObject из объекта DrawningCar
/// </summary>
public IMoveableObject GetMoveableObject => new
DrawningObjectTractor(this);
/// <summary>
/// Координата X объекта

View File

@ -20,30 +20,26 @@ namespace ProjectTractor
private ComboBox comboBoxStrategy;
private Button buttonRight;
/// <summary>
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
/// </summary>
private AbstractStrategy? _abstractStrategy;
public int getPictureWidth()
{
return pictureBoxTractor.Width;
}
public int getPictureHeight()
{
return pictureBoxTractor.Height;
}
/// <summary>
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
/// </summary>
private DrawningTractor? _drawningTractor;
/// <summary>
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
/// </summary>
private AbstractStrategy? _strategy;
/// <summary>
/// Âûáðàííûé àâòîìîáèëü
/// </summary>
public DrawningTractor? SelectedTractor { get; private set; }
/// <summary>
/// Èíèöèàëèçàöèÿ ôîðìû
/// </summary>
public FormTractor()
{
InitializeComponent();
_strategy = null;
SelectedTractor = null;
}
/// <summary>
/// Ìåòîä ïðîðèñîâêè ìàøèíû
@ -65,19 +61,26 @@ namespace ProjectTractor
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e)
private void ButtonCreateBulldoserTractor_Click(object sender, EventArgs e)
{
Random random = new();
_drawningTractor = new DrawningTractor(random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256),
random.Next(0, 256)),
pictureBoxTractor.Width, pictureBoxTractor.Height);
_drawningTractor.SetPosition(random.Next(10, 100),
random.Next(10, 100));
Color color = Color.FromArgb(random.Next(0, 256),
random.Next(0, 256), random.Next(0, 256));
//TODO âûáîð îñíîâíîãî öâåòà
Color dopColor = Color.FromArgb(random.Next(0, 256),
random.Next(0, 256), random.Next(0, 256));
//TODO âûáîð äîïîëíèòåëüíîãî öâåòà
_drawningTractor = new DrawningBulldoser(random.Next(100, 300),
random.Next(1000, 3000),
color,
dopColor,
Convert.ToBoolean(random.Next(0, 2)),
Convert.ToBoolean(random.Next(0, 2)),
pictureBoxTractor.Width,
pictureBoxTractor.Height);
_drawningTractor.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Èçìåíåíèå ðàçìåðîâ ôîðìû
/// </summary>
@ -92,7 +95,7 @@ namespace ProjectTractor
string name = ((Button)sender)?.Name ?? string.Empty;
switch (name)
{
case "buttonTop":
case "buttonUp":
_drawningTractor.MoveTransport(DirectionType.Up);
break;
case "buttonDown":
@ -107,6 +110,76 @@ namespace ProjectTractor
}
Draw();
}
/// <summary>
/// Ñîçäàíèå ïðîñòîãî àâòîìîáèëÿ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonCreate_Click(object sender, EventArgs e)
{
Random random = new();
Color color = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
color = dialog.Color;
}
_drawningTractor = new DrawningTractor(random.Next(100, 300),
random.Next(1000, 3000), color,
pictureBoxTractor.Width, pictureBoxTractor.Height);
_drawningTractor.SetPosition(random.Next(10, 100), random.Next(10,
100));
Draw();
}
/// <summary>
/// Øàã ñòðàòåãèè ïåðåìåùåíèÿ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonStartegyStep_Click(object sender, EventArgs e)
{
if (_drawningTractor == null)
{
return;
}
if (comboBoxStrategy.Enabled)
{
_strategy = comboBoxStrategy.SelectedIndex switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_strategy == null)
{
return;
}
_strategy.SetData(_drawningTractor.GetMoveableObject,
pictureBoxTractor.Width, pictureBoxTractor.Height);
}
if (_strategy == null)
{
return;
}
comboBoxStrategy.Enabled = false;
_strategy.MakeStep();
Draw();
if (_strategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_strategy = null;
}
}
/// <summary>
/// Âûáîð àâòîìîáèëÿ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSelectCar_Click(object sender, EventArgs e)
{
SelectedTractor = _drawningTractor;
DialogResult = DialogResult.OK;
}
private void InitializeComponent()
{
@ -239,6 +312,7 @@ namespace ProjectTractor
this.Controls.Add(this.buttonCreateTractor);
this.Controls.Add(this.pictureBoxTractor);
this.Name = "FormTractor";
this.Load += new System.EventHandler(this.FormTractor_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBoxTractor)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
@ -271,33 +345,38 @@ namespace ProjectTractor
}
if (comboBoxStrategy.Enabled)
{
_abstractStrategy = comboBoxStrategy.SelectedIndex
_strategy = comboBoxStrategy.SelectedIndex
switch
{
0 => new MoveToCenter(),
1 => new MoveToBorder(),
_ => null,
};
if (_abstractStrategy == null)
if (_strategy == null)
{
return;
}
_abstractStrategy.SetData(new
_strategy.SetData(new
DrawningObjectTractor(_drawningTractor), pictureBoxTractor.Width,
pictureBoxTractor.Height);
comboBoxStrategy.Enabled = false;
}
if (_abstractStrategy == null)
if (_strategy == null)
{
return;
}
_abstractStrategy.MakeStep();
_strategy.MakeStep();
Draw();
if (_abstractStrategy.GetStatus() == Status.Finish)
if (_strategy.GetStatus() == Status.Finish)
{
comboBoxStrategy.Enabled = true;
_abstractStrategy = null;
_strategy = null;
}
}
private void FormTractor_Load(object sender, EventArgs e)
{
}
}
}

View File

@ -0,0 +1,146 @@
namespace ProjectTractor
{
partial class FormTractorCollection
{
/// <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.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.labelPanelInstuments = new System.Windows.Forms.Label();
this.maskedTextBoxNumber = new System.Windows.Forms.MaskedTextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.pictureBox1);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.button3);
this.splitContainer1.Panel2.Controls.Add(this.button2);
this.splitContainer1.Panel2.Controls.Add(this.button1);
this.splitContainer1.Panel2.Controls.Add(this.maskedTextBoxNumber);
this.splitContainer1.Panel2.Controls.Add(this.labelPanelInstuments);
this.splitContainer1.Size = new System.Drawing.Size(800, 450);
this.splitContainer1.SplitterDistance = 591;
this.splitContainer1.TabIndex = 0;
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(591, 450);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// labelPanelInstuments
//
this.labelPanelInstuments.AutoSize = true;
this.labelPanelInstuments.Location = new System.Drawing.Point(8, 9);
this.labelPanelInstuments.Name = "labelPanelInstuments";
this.labelPanelInstuments.Size = new System.Drawing.Size(103, 20);
this.labelPanelInstuments.TabIndex = 0;
this.labelPanelInstuments.Text = "Инструменты";
//
// maskedTextBoxNumber
//
this.maskedTextBoxNumber.Location = new System.Drawing.Point(8, 146);
this.maskedTextBoxNumber.Name = "maskedTextBoxNumber";
this.maskedTextBoxNumber.Size = new System.Drawing.Size(185, 27);
this.maskedTextBoxNumber.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 54);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(185, 40);
this.button1.TabIndex = 2;
this.button1.Text = "Добавить трактор";
this.button1.UseVisualStyleBackColor = true;
//
// button2
//
this.button2.Location = new System.Drawing.Point(8, 179);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(185, 36);
this.button2.TabIndex = 3;
this.button2.Text = "Удалить трактор";
this.button2.UseVisualStyleBackColor = true;
//
// button3
//
this.button3.Location = new System.Drawing.Point(8, 376);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(185, 42);
this.button3.TabIndex = 4;
this.button3.Text = "Обновить коллекцию";
this.button3.UseVisualStyleBackColor = true;
//
// FormTractorCollection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.splitContainer1);
this.Name = "FormTractorCollection";
this.Text = "FormTractorCollection";
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private SplitContainer splitContainer1;
private PictureBox pictureBox1;
private Button button3;
private Button button2;
private Button button1;
private MaskedTextBox maskedTextBoxNumber;
private Label labelPanelInstuments;
}
}

View File

@ -0,0 +1,20 @@
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 ProjectTractor
{
public partial class FormTractorCollection : Form
{
public FormTractorCollection()
{
InitializeComponent();
}
}
}

View File

@ -0,0 +1,60 @@
<root>
<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,101 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectTractor.Generics
{
internal class SetGeneric<T>
where T : class
{
/// <summary>
/// Массив объектов, которые храним
/// </summary>
private readonly T?[] _places;
/// <summary>
/// Количество объектов в массиве
/// </summary>
public int Count => _places.Length;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="count"></param>
public SetGeneric(int count)
{
_places = new T?[count];
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="tractor">Добавляемый трактор</param>
/// <returns></returns>
public bool Insert(T tractor)
{
if (_places[0] == null)
{
_places[0] = tractor;
}
else
{
Insert(tractor, 0);
}
// TODO вставка в начало набора
return true;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="car">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public bool Insert(T tractor, int position)
{
if (!(position >= 0 && position < Count))
return false;
if (_places[position] != null)
{
int ind = position;
while (ind < Count && _places[ind] != null)
ind++;
if (ind == Count)
return false;
for (int i = ind - 1; i >= position; i--)
_places[i + 1] = _places[i];
}
_places[position] = tractor;
return true;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
if (!(position >= 0 && position < Count))
{
return false;
}
_places[position] = null;
return true;
// TODO проверка позиции
// TODO удаление объекта из массива, присвоив элементу массива значение null
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
{
if (!(position >= 0 && position < Count))
{
return null;
}
// TODO проверка позиции
return _places[position];
}
}
}

View File

@ -0,0 +1,146 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectTractor.DrawningObjects;
using ProjectTractor.MovementStrategy;
namespace ProjectTractor.Generics
{
/// <summary>
/// Параметризованный класс для набора объектов DrawningCar
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
internal class TractorsGenericCollection<T, U>
where T : DrawningTractor
where U : IMoveableObject
{
/// <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 SetGeneric<T> _collection;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
public TractorsGenericCollection(int picWidth, int picHeight)
{
int width = picWidth / _placeSizeWidth;
int height = picHeight / _placeSizeHeight;
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = new SetGeneric<T>(width * height);
}
/// <summary>
/// Перегрузка оператора сложения
/// </summary>
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static bool operator +(TractorsGenericCollection<T, U> collect, T?
obj)
{
if (obj == null)
{
return false;
}
return collect?._collection.Insert(obj) ?? false;
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static T? operator -(TractorsGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
if (obj != null)
{
collect._collection.Remove(pos);
}
return obj;
}
/// <summary>
/// Получение объекта IMoveableObject
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
public U? GetU(int pos)
{
return (U?)_collection.Get(pos)?.GetMoveableObject;
}
/// <summary>
/// Вывод всего набора объектов
/// </summary>
/// <returns></returns>
public Bitmap ShowCars()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawBackground(gr);
DrawObjects(gr);
return bmp;
}
/// <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 / 2, j *
_placeSizeHeight);
}
g.DrawLine(pen, i * _placeSizeWidth, 0, i *
_placeSizeWidth, _pictureHeight / _placeSizeHeight * _placeSizeHeight);
}
}
/// Метод прорисовки объектов
/// </summary>
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
for (int i = 0; i < _collection.Count; i++)
{
DrawningTractor? tractor = _collection.Get(i);
if (tractor != null)
{
int inRow = _pictureWidth / _placeSizeWidth;
tractor.SetPosition(_pictureWidth - _placeSizeWidth - (i % inRow * _placeSizeWidth) - _placeSizeHeight / 2 - 8, i / inRow * _placeSizeHeight + 20);
tractor.DrawTransport(g);
}
// TODO получение объекта
// TODO установка позиции
// TODO прорисовка объекта
}
}
}
}