From 1883c5f91905f6d5944d9382311a0be1b00e9075 Mon Sep 17 00:00:00 2001 From: Efi Date: Fri, 31 Mar 2023 21:55:40 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA?= =?UTF-8?q?=D0=B8=20=D1=84=D0=BE=D1=80=D0=BC=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetLocomotive.Designer.cs | 197 ++++++++++++++++++ Monorail/Monorail/FormMapWithSetLocomotive.cs | 20 ++ .../Monorail/FormMapWithSetLocomotive.resx | 60 ++++++ .../Monorail/MapWithSetLocomotiveGeneric.cs | 151 ++++++++++++++ Monorail/Monorail/SetLocomotiveGeneric.cs | 80 +++++++ 5 files changed, 508 insertions(+) create mode 100644 Monorail/Monorail/FormMapWithSetLocomotive.Designer.cs create mode 100644 Monorail/Monorail/FormMapWithSetLocomotive.cs create mode 100644 Monorail/Monorail/FormMapWithSetLocomotive.resx create mode 100644 Monorail/Monorail/MapWithSetLocomotiveGeneric.cs create mode 100644 Monorail/Monorail/SetLocomotiveGeneric.cs diff --git a/Monorail/Monorail/FormMapWithSetLocomotive.Designer.cs b/Monorail/Monorail/FormMapWithSetLocomotive.Designer.cs new file mode 100644 index 0000000..565cc16 --- /dev/null +++ b/Monorail/Monorail/FormMapWithSetLocomotive.Designer.cs @@ -0,0 +1,197 @@ +namespace Monorail +{ + partial class FormMapWithSetLocomotive + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + pictureBoxLocomotive = new PictureBox(); + comboBoxSelectorMap = new ComboBox(); + buttonAddCar = new Button(); + buttonRemoveCar = new Button(); + buttonShowStorage = new Button(); + buttonShowOnMap = new Button(); + maskedTextBoxPosition = new MaskedTextBox(); + buttonUp = new Button(); + buttonLeft = new Button(); + buttonDown = new Button(); + buttonRight = new Button(); + groupBoxTools = new GroupBox(); + ((System.ComponentModel.ISupportInitialize)pictureBoxLocomotive).BeginInit(); + groupBoxTools.SuspendLayout(); + SuspendLayout(); + // + // pictureBoxLocomotive + // + pictureBoxLocomotive.Dock = DockStyle.Left; + pictureBoxLocomotive.Location = new Point(0, 0); + pictureBoxLocomotive.Name = "pictureBoxLocomotive"; + pictureBoxLocomotive.Size = new Size(579, 450); + pictureBoxLocomotive.TabIndex = 0; + pictureBoxLocomotive.TabStop = false; + // + // comboBoxSelectorMap + // + comboBoxSelectorMap.FormattingEnabled = true; + comboBoxSelectorMap.Location = new Point(26, 22); + comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + comboBoxSelectorMap.Size = new Size(168, 23); + comboBoxSelectorMap.TabIndex = 2; + // + // buttonAddCar + // + buttonAddCar.Location = new Point(26, 70); + buttonAddCar.Name = "buttonAddCar"; + buttonAddCar.Size = new Size(168, 28); + buttonAddCar.TabIndex = 3; + buttonAddCar.Text = "Добавить автомобиль"; + buttonAddCar.UseVisualStyleBackColor = true; + // + // buttonRemoveCar + // + buttonRemoveCar.Location = new Point(26, 151); + buttonRemoveCar.Name = "buttonRemoveCar"; + buttonRemoveCar.Size = new Size(169, 30); + buttonRemoveCar.TabIndex = 4; + buttonRemoveCar.Text = "Удалить автомобиль"; + buttonRemoveCar.UseVisualStyleBackColor = true; + // + // buttonShowStorage + // + buttonShowStorage.Location = new Point(26, 222); + buttonShowStorage.Name = "buttonShowStorage"; + buttonShowStorage.Size = new Size(168, 39); + buttonShowStorage.TabIndex = 5; + buttonShowStorage.Text = "Посмотреть хранилище"; + buttonShowStorage.UseVisualStyleBackColor = true; + // + // buttonShowOnMap + // + buttonShowOnMap.Location = new Point(26, 307); + buttonShowOnMap.Name = "buttonShowOnMap"; + buttonShowOnMap.Size = new Size(169, 34); + buttonShowOnMap.TabIndex = 6; + buttonShowOnMap.Text = "Посмотреть карту"; + buttonShowOnMap.UseVisualStyleBackColor = true; + // + // maskedTextBoxPosition + // + maskedTextBoxPosition.Location = new Point(26, 193); + maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + maskedTextBoxPosition.Size = new Size(168, 23); + maskedTextBoxPosition.TabIndex = 7; + // + // buttonUp + // + buttonUp.BackgroundImage = Properties.Resources.arrowUp; + buttonUp.BackgroundImageLayout = ImageLayout.Zoom; + buttonUp.Location = new Point(98, 366); + buttonUp.Name = "buttonUp"; + buttonUp.Size = new Size(30, 30); + buttonUp.TabIndex = 8; + buttonUp.UseVisualStyleBackColor = true; + // + // buttonLeft + // + buttonLeft.BackgroundImage = Properties.Resources.arrowLeft; + buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; + buttonLeft.Location = new Point(62, 402); + buttonLeft.Name = "buttonLeft"; + buttonLeft.Size = new Size(30, 30); + buttonLeft.TabIndex = 9; + buttonLeft.UseVisualStyleBackColor = true; + // + // buttonDown + // + buttonDown.BackgroundImage = Properties.Resources.arrowDown; + buttonDown.BackgroundImageLayout = ImageLayout.Zoom; + buttonDown.Location = new Point(98, 402); + buttonDown.Name = "buttonDown"; + buttonDown.Size = new Size(30, 30); + buttonDown.TabIndex = 10; + buttonDown.UseVisualStyleBackColor = true; + // + // buttonRight + // + buttonRight.BackgroundImage = Properties.Resources.arrowRight; + buttonRight.BackgroundImageLayout = ImageLayout.Zoom; + buttonRight.Location = new Point(134, 402); + buttonRight.Name = "buttonRight"; + buttonRight.Size = new Size(30, 30); + buttonRight.TabIndex = 11; + buttonRight.UseVisualStyleBackColor = true; + // + // groupBoxTools + // + groupBoxTools.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + groupBoxTools.Controls.Add(buttonRight); + groupBoxTools.Controls.Add(buttonDown); + groupBoxTools.Controls.Add(buttonLeft); + groupBoxTools.Controls.Add(buttonUp); + groupBoxTools.Controls.Add(maskedTextBoxPosition); + groupBoxTools.Controls.Add(buttonShowOnMap); + groupBoxTools.Controls.Add(buttonShowStorage); + groupBoxTools.Controls.Add(buttonRemoveCar); + groupBoxTools.Controls.Add(buttonAddCar); + groupBoxTools.Controls.Add(comboBoxSelectorMap); + groupBoxTools.Location = new Point(585, -2); + groupBoxTools.Name = "groupBoxTools"; + groupBoxTools.Size = new Size(212, 451); + groupBoxTools.TabIndex = 12; + groupBoxTools.TabStop = false; + groupBoxTools.Text = "Инструменты"; + // + // FormMapWithSetLocomotive + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(groupBoxTools); + Controls.Add(pictureBoxLocomotive); + Name = "FormMapWithSetLocomotive"; + Text = "Карта с набором объектов"; + ((System.ComponentModel.ISupportInitialize)pictureBoxLocomotive).EndInit(); + groupBoxTools.ResumeLayout(false); + groupBoxTools.PerformLayout(); + ResumeLayout(false); + } + + #endregion + + private PictureBox pictureBoxLocomotive; + private ComboBox comboBoxSelectorMap; + private Button buttonAddCar; + private Button buttonRemoveCar; + private Button buttonShowStorage; + private Button buttonShowOnMap; + private MaskedTextBox maskedTextBoxPosition; + private Button buttonUp; + private Button buttonLeft; + private Button buttonDown; + private Button buttonRight; + private GroupBox groupBoxTools; + } +} \ No newline at end of file diff --git a/Monorail/Monorail/FormMapWithSetLocomotive.cs b/Monorail/Monorail/FormMapWithSetLocomotive.cs new file mode 100644 index 0000000..2f7f69a --- /dev/null +++ b/Monorail/Monorail/FormMapWithSetLocomotive.cs @@ -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 Monorail +{ + public partial class FormMapWithSetLocomotive : Form + { + public FormMapWithSetLocomotive() + { + InitializeComponent(); + } + } +} diff --git a/Monorail/Monorail/FormMapWithSetLocomotive.resx b/Monorail/Monorail/FormMapWithSetLocomotive.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Monorail/Monorail/FormMapWithSetLocomotive.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Monorail/Monorail/MapWithSetLocomotiveGeneric.cs b/Monorail/Monorail/MapWithSetLocomotiveGeneric.cs new file mode 100644 index 0000000..d5e684f --- /dev/null +++ b/Monorail/Monorail/MapWithSetLocomotiveGeneric.cs @@ -0,0 +1,151 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail +{ + internal class MapWithSetLocomotivGeneric +where T : class, IDrawingObject + where U : AbstractMap + { + // Ширина окна отрисовки + private readonly int _pictureWidth; + // Высота окна отрисовки + private readonly int _pictureHeight; + // Размер занимаемого объектом места (ширина) + private readonly int _placeSizeWidth = 180; + // Размер занимаемого объектом места (высота) + private readonly int _placeSizeHeight = 150; + // Набор объектов + private readonly SetLocomotivGeneric _setLocomotive; + // Карта + private readonly U _map; + + private readonly T[] _places; + // Конструктор + public MapWithSetLocomotivGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setLocomotive = new SetLocomotivGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + // Перегрузка оператора сложения + public static int operator +(MapWithSetLocomotivGeneric map, T Locomotive) + { + return map._setLocomotive.Insert(Locomotive); + } + // Перегрузка оператора вычитания + public static T operator -(MapWithSetLocomotivGeneric map, int position) + { + + return map._setLocomotive.Remove(position); + } + // Вывод всего набора объектов + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawLocomotive(gr); + return bmp; + } + // Просмотр объекта на карте + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setLocomotive.Count; i++) + { + var Locomotive = _setLocomotive.Get(i); + if (Locomotive != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, Locomotive); + } + } + return new(_pictureWidth, _pictureHeight); + } + // Перемещение объекта по карте + public Bitmap MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new(_pictureWidth, _pictureHeight); + } + // "Взбалтываем" набор, чтобы все элементы оказались в начале + private void Shaking() + { + int j = _setLocomotive.Count - 1; + for (int i = 0; i < _setLocomotive.Count; i++) + { + if (_setLocomotive.Get(i) == null) + { + for (; j > i; j--) + { + var Locomotive = _setLocomotive.Get(j); + if (Locomotive != null) + { + _setLocomotive.Insert(Locomotive, i); + _setLocomotive.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + // Метод отрисовки фона + private void DrawBackground(Graphics g) + { + Pen pen = new(Color.Sienna, 3); + Brush brush = new SolidBrush(Color.DodgerBlue); + g.FillRectangle(brush, 0, 0, _pictureWidth, _pictureHeight); + for (int i = 0; i < _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j < _pictureHeight / _placeSizeHeight + 1; ++j) + { + g.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight + 2, i * _placeSizeWidth + _placeSizeWidth / 2 + 40, j * _placeSizeHeight + 2); + } + g.DrawLine(pen, i * _placeSizeWidth + 7, 2, i * _placeSizeWidth + 7, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + g.DrawLine(pen, i * _placeSizeWidth + 3, 2, i * _placeSizeWidth + 3, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + } + // Метод прорисовки объектов + private void DrawLocomotive(Graphics g) + { + int widthEl = _pictureWidth / _placeSizeWidth; + int heightEl = _pictureHeight / _placeSizeHeight; + + int curWidth = 0; + int curHeight = 0; + + for (int i = _setLocomotive.Count; i >= 0; i--) + { + _setLocomotive.Get(i)?.SetObject(_pictureWidth - _placeSizeWidth * curWidth - 85, + curHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); + _setLocomotive.Get(i)?.DrawingObject(g); + + if (curWidth < widthEl) + curWidth++; + else + { + curWidth = 1; + curHeight++; + } + if (curHeight > heightEl) + { + return; + } + + } + } + } +} diff --git a/Monorail/Monorail/SetLocomotiveGeneric.cs b/Monorail/Monorail/SetLocomotiveGeneric.cs new file mode 100644 index 0000000..52aa0d1 --- /dev/null +++ b/Monorail/Monorail/SetLocomotiveGeneric.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Monorail +{ + internal class SetLocomotivGeneric +where T : class + { + // Массив объектов, которые храним + private readonly T[] _places; + // Количество объектов в массиве + public int Count => _places.Length; + // Конструктор + public SetLocomotivGeneric(int count) + { + _places = new T[count]; + } + // Добавление объекта в набор + public int Insert(T Locomotive) + { + // вставка в начало набора + return Insert(Locomotive, 0); + } + // Добавление объекта в набор на конкретную позицию + public int Insert(T Locomotive, int position) + { + // проверка позиции + if (position >= _places.Length || position < 0) + return -1; + //проверка, что элемент массива по этой позиции пустой, если нет, то + if (_places[position] == null) + { + _places[position] = Locomotive; + 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] = Locomotive; + 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]; + } + } +}