ЛабТри

This commit is contained in:
Леонид Малафеев 2023-10-21 14:10:32 +04:00
parent 08e784d05a
commit e7b9367168
9 changed files with 659 additions and 4 deletions

View File

@ -1,4 +1,5 @@
using Cruiser.Entities;
using Cruiser.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
@ -97,6 +98,12 @@ namespace Cruiser.Drawing
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <summary>
/// Получение объекта IMoveableObject из объекта DrawningCar
/// </summary>
public IMoveableObject GetMoveableObject => new
DrawningObjectCar(this);
public void SetPosition(int x, int y)
{
if (x < 0 || y < 0)

View File

@ -37,6 +37,7 @@
buttonCreateProLiner = new Button();
comboBoxStrategy = new ComboBox();
ButtonStep = new Button();
buttonSelectCruiser = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCruiser).BeginInit();
SuspendLayout();
//
@ -133,11 +134,22 @@
ButtonStep.UseVisualStyleBackColor = true;
ButtonStep.Click += ButtonStep_Click;
//
// buttonSelectCruiser
//
buttonSelectCruiser.Location = new Point(274, 396);
buttonSelectCruiser.Name = "buttonSelectCruiser";
buttonSelectCruiser.Size = new Size(127, 48);
buttonSelectCruiser.TabIndex = 9;
buttonSelectCruiser.Text = "Выбрать этот лайнер";
buttonSelectCruiser.UseVisualStyleBackColor = true;
buttonSelectCruiser.Click += ButtonSelectCruiser_Click;
//
// FormCruiser
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(buttonSelectCruiser);
Controls.Add(ButtonStep);
Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateProLiner);
@ -149,6 +161,7 @@
Controls.Add(pictureBoxCruiser);
Name = "FormCruiser";
Text = "Cruiser";
Load += FormCruiser_Load;
((System.ComponentModel.ISupportInitialize)pictureBoxCruiser).EndInit();
ResumeLayout(false);
}
@ -164,5 +177,6 @@
private Button buttonCreateProLiner;
private ComboBox comboBoxStrategy;
private Button ButtonStep;
private Button buttonSelectCruiser;
}
}

View File

@ -16,6 +16,11 @@ namespace Cruiser
/// Ñòðàòåãèÿ ïåðåìåùåíèÿ
/// </summary>
private AbstractStrategy? _abstractStrategy;
/// <summary>
/// Âűáđŕííűé ëŕéíĺđ
/// </summary>
public DrawingCruiser? SelectedCruiser { get; private set; }
/// <summary>
/// Èíèöèàëèçàöèÿ ôîðìû
/// </summary>
@ -23,6 +28,8 @@ namespace Cruiser
{
InitializeComponent();
bmp = new(pictureBoxCruiser.Width, pictureBoxCruiser.Width);
_abstractStrategy = null;
SelectedCruiser = null;
}
@ -116,9 +123,16 @@ namespace Cruiser
private void buttonCreateLiner_Click(object sender, EventArgs e)
{
Random random = new();
Color colorFirst = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
colorFirst = dialog.Color;
}
_drawningCruiser = new DrawingCruiser(random.Next(100, 300),
random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
colorFirst,
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
pictureBoxCruiser.Width,
pictureBoxCruiser.Height);
@ -129,10 +143,21 @@ namespace Cruiser
private void buttonCreateProLiner_Click(object sender, EventArgs e)
{
Random random = new();
Color colorFirst = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
ColorDialog dialog = new();
if (dialog.ShowDialog() == DialogResult.OK)
{
colorFirst = dialog.Color;
}
Color colorSecond = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
if (dialog.ShowDialog() == DialogResult.OK)
{
colorSecond = dialog.Color;
}
_drawningCruiser = new DrawingProCruiser(random.Next(100, 300),
random.Next(1000, 3000),
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)),
colorFirst,
colorSecond,
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(1, 2)),
Convert.ToBoolean(random.Next(1, 2)),
@ -141,5 +166,20 @@ namespace Cruiser
_drawningCruiser.SetPosition(random.Next(10, 100), random.Next(10, 100));
Draw();
}
/// <summary>
/// Âűáîđ ëŕéíĺđ
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSelectCruiser_Click(object sender, EventArgs e)
{
SelectedCruiser = _drawningCruiser;
DialogResult = DialogResult.OK;
}
private void FormCruiser_Load(object sender, EventArgs e)
{
}
}
}

124
Cruiser/FormCruiserCollection.Designer.cs generated Normal file
View File

@ -0,0 +1,124 @@
namespace Cruiser
{
partial class FormCruiserCollection
{
/// <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()
{
groupBoxTools = new GroupBox();
button3 = new Button();
button2 = new Button();
textBoxNumber = new TextBox();
buttonAddCruiser = new Button();
pictureBoxCollection = new PictureBox();
groupBoxTools.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit();
SuspendLayout();
//
// groupBoxTools
//
groupBoxTools.Controls.Add(button3);
groupBoxTools.Controls.Add(button2);
groupBoxTools.Controls.Add(textBoxNumber);
groupBoxTools.Controls.Add(buttonAddCruiser);
groupBoxTools.Location = new Point(645, 1);
groupBoxTools.Name = "groupBoxTools";
groupBoxTools.Size = new Size(154, 448);
groupBoxTools.TabIndex = 0;
groupBoxTools.TabStop = false;
groupBoxTools.Text = "Инструменты";
//
// button3
//
button3.Location = new Point(15, 171);
button3.Name = "button3";
button3.Size = new Size(122, 41);
button3.TabIndex = 3;
button3.Text = "Обновить коллекцию";
button3.UseVisualStyleBackColor = true;
button3.Click += ButtonRefreshCollection_Click;
//
// button2
//
button2.Location = new Point(15, 107);
button2.Name = "button2";
button2.Size = new Size(122, 37);
button2.TabIndex = 2;
button2.Text = "Удалить лайнер";
button2.UseVisualStyleBackColor = true;
button2.Click += ButtonRemoveCar_Click;
//
// textBoxNumber
//
textBoxNumber.Location = new Point(15, 78);
textBoxNumber.Name = "textBoxNumber";
textBoxNumber.Size = new Size(122, 23);
textBoxNumber.TabIndex = 1;
//
// buttonAddCruiser
//
buttonAddCruiser.Location = new Point(15, 22);
buttonAddCruiser.Name = "buttonAddCruiser";
buttonAddCruiser.Size = new Size(122, 38);
buttonAddCruiser.TabIndex = 0;
buttonAddCruiser.Text = "Добавить лайнер";
buttonAddCruiser.UseVisualStyleBackColor = true;
buttonAddCruiser.Click += buttonAddCruiser_Click;
//
// pictureBoxCollection
//
pictureBoxCollection.Location = new Point(-1, 1);
pictureBoxCollection.Name = "pictureBoxCollection";
pictureBoxCollection.Size = new Size(642, 448);
pictureBoxCollection.TabIndex = 1;
pictureBoxCollection.TabStop = false;
//
// FormCruiserCollection
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(pictureBoxCollection);
Controls.Add(groupBoxTools);
Name = "FormCruiserCollection";
Text = "Набор Крейсеров";
Load += FormCruiserCollection_Load;
groupBoxTools.ResumeLayout(false);
groupBoxTools.PerformLayout();
((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit();
ResumeLayout(false);
}
#endregion
private GroupBox groupBoxTools;
private TextBox textBoxNumber;
private Button buttonAddCruiser;
private Button button3;
private Button button2;
private PictureBox pictureBoxCollection;
}
}

View File

@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Cruiser.Drawing;
using Cruiser.Generics;
using Cruiser.MovementStrategy;
namespace Cruiser
{
/// <summary>
/// Форма для работы с набором объектов класса DrawingCruiser
/// </summary>
public partial class FormCruiserCollection : Form
{
/// <summary>
/// Набор объектов
/// </summary>
private readonly CarsGenericCollection<DrawingCruiser, DrawningObjectCar> _cruisers;
/// <summary>
/// Конструктор
/// </summary>
public FormCruiserCollection()
{
InitializeComponent();
_cruisers = new CarsGenericCollection<DrawingCruiser, DrawningObjectCar>(pictureBoxCollection.Width, pictureBoxCollection.Height);
}
/// <summary>
/// Добавление объекта в набор
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonAddCruiser_Click(object sender, EventArgs e)
{
FormCruiser form = new();
if (form.ShowDialog() == DialogResult.OK)
{
if (_cruisers + form.SelectedCruiser != -1)
{
MessageBox.Show("Объект добавлен");
pictureBoxCollection.Image = _cruisers.ShowCruiser();
}
else
{
MessageBox.Show("Не удалось добавить объект");
}
}
}
/// <summary>
/// Удаление объекта из набора
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRemoveCar_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Удалить объект?", "Удаление",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
if (textBoxNumber.Text == "")
{
MessageBox.Show("Вы ничего не ввели");
return;
}
int pos = Convert.ToInt32(textBoxNumber.Text);
if (_cruisers - pos)
{
MessageBox.Show("Объект удален");
pictureBoxCollection.Image = _cruisers.ShowCruiser();
}
else
{
MessageBox.Show("Не удалось удалить объект");
}
}
/// <summary>
/// Обновление рисунка по набору
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
{
pictureBoxCollection.Image = _cruisers.ShowCruiser();
}
private void FormCruiserCollection_Load(object sender, EventArgs e)
{
}
}
}

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,142 @@
using Cruiser.MovementStrategy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cruiser.Drawing;
namespace Cruiser.Generics
{
/// <summary>
/// Параметризованный класс для набора объектов DrawingCruiser
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
internal class CarsGenericCollection<T, U>
where T : DrawingCruiser
where U : IMoveableObject
{
/// <summary>
/// Ширина окна прорисовки
/// </summary>
private readonly int _pictureWidth;
/// <summary>
/// Высота окна прорисовки
/// </summary>
private readonly int _pictureHeight;
/// <summary>
/// Размер занимаемого объектом места (ширина)
/// </summary>
private readonly int _placeSizeWidth = 160;
/// <summary>
/// Размер занимаемого объектом места (высота)
/// </summary>
private readonly int _placeSizeHeight = 60;
/// <summary>
/// Набор объектов
/// </summary>
private readonly SetGeneric<T> _collection;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="picWidth"></param>
/// <param name="picHeight"></param>
public CarsGenericCollection(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 int operator +(CarsGenericCollection<T, U> collect, T? obj)
{
if (obj == null)
{
return -1;
}
return collect._collection.Insert(obj);
}
/// <summary>
/// Перегрузка оператора вычитания
/// </summary>
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(CarsGenericCollection<T, U> collect, int pos)
{
T? obj = collect._collection.Get(pos);
if (obj != null)
{
collect._collection.Remove(pos);
return true;
}
return false;
}
/// <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 ShowCruiser()
{
Bitmap bmp = new(_pictureWidth, _pictureHeight);
Graphics gr = Graphics.FromImage(bmp);
DrawObjects(gr);
DrawBackground(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; j++)
{
g.DrawRectangle(pen, i * _placeSizeWidth, j * _placeSizeHeight, _placeSizeWidth, _placeSizeHeight);
}
}
}
/// <summary>
/// /// Метод прорисовки объектов
/// </summary>
/// <param name="g"></param>
private void DrawObjects(Graphics g)
{
int Ix = 0;
int Iy = 0;
for (int i = 0; i < _collection.Count - 1; i++)
{
_collection.Get(i)?.SetPosition(Ix, Iy);
_collection.Get(i)?.DrawTransport(g);
Ix += _placeSizeWidth;
if (Ix + _placeSizeHeight > _pictureWidth)
{
Ix = 0;
Iy = _placeSizeHeight;
}
}
}
}
}

View File

@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cruiser.Generics
{
/// <summary>
/// Параметризованный набор объектов
/// </summary>
/// <typeparam name="T"></typeparam>
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="cruiser">Добавляемый лайнер</param>
/// <returns></returns>
public int Insert(T cruiser)
{
int zero = 0;
for (; _places[zero] != null;)
{
zero++;
if (zero == _places.Length)
{
return -1;
}
}
for (int i = zero; i > 0; i--)
{
_places[i] = _places[i - 1];
}
_places[0] = cruiser;
return 0;
}
/// <summary>
/// Удаление объекта из набора с конкретной позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool Remove(int position)
{
if (position < 0 || position > _places.Length)
{
return false;
}
_places[position] = null;
return true;
}
/// <summary>
/// Добавление объекта в набор на конкретную позицию
/// </summary>
/// <param name="cruiser">Добавляемый автомобиль</param>
/// <param name="position">Позиция</param>
/// <returns></returns>
public int Insert(T cruiser, int position)
{
if (position < 0 || position > _places.Length)
{
return 0;
}
if (_places.Length != null)
{
int zero = position;
while (_places[zero] != null)
{
zero++;
return -1;
}
}
_places[position] = cruiser;
return position;
}
/// <summary>
/// Получение объекта из набора по позиции
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public T? Get(int position)
{
if (position < 0 || position > _places.Length)
{
return null;
}
return _places[position];
}
}
}

View File

@ -11,7 +11,7 @@ namespace Cruiser
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new FormCruiser());
Application.Run(new FormCruiserCollection());
}
}
}