From 0f32f24709d61d063aa30974cbe7f12c098fe361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?10=D0=93=20=D0=95=D0=B3=D0=BE=D1=80=20=D0=A0=D0=BE=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Mon, 10 Oct 2022 19:53:02 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A2=D1=80=D0=B5=D1=82=D1=8C=D1=8F=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMapWithSetHoistingCrane.Designer.cs | 231 ++++++++++++++++++ .../FormMapWithSetHoistingCrane.cs | 139 +++++++++++ .../FormMapWithSetHoistingCrane.resx | 60 +++++ .../MapWithSetHoistingCraneGeneric.cs | 137 +++++++++++ .../HoistingCrane/SetHoistingCraneGeneric.cs | 83 +++++++ 5 files changed, 650 insertions(+) create mode 100644 HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.Designer.cs create mode 100644 HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.cs create mode 100644 HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.resx create mode 100644 HoistingCrane/HoistingCrane/MapWithSetHoistingCraneGeneric.cs create mode 100644 HoistingCrane/HoistingCrane/SetHoistingCraneGeneric.cs diff --git a/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.Designer.cs b/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.Designer.cs new file mode 100644 index 0000000..554be16 --- /dev/null +++ b/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.Designer.cs @@ -0,0 +1,231 @@ +namespace HoistingCrane +{ + partial class FormMapWithSetHoistingCrane + { + /// + /// Required designer variable. + /// + /// + /// 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() + { + this.groupBoxTools = new System.Windows.Forms.GroupBox(); + this.maskedTextBoxPosition = new System.Windows.Forms.MaskedTextBox(); + this.buttonRemoveHoistingCrane = new System.Windows.Forms.Button(); + this.buttonShowStorage = new System.Windows.Forms.Button(); + this.buttonDown = new System.Windows.Forms.Button(); + this.buttonRight = new System.Windows.Forms.Button(); + this.buttonLeft = new System.Windows.Forms.Button(); + this.buttonUp = new System.Windows.Forms.Button(); + this.buttonShowOnMap = new System.Windows.Forms.Button(); + this.buttonAddHoistingCrane = new System.Windows.Forms.Button(); + this.comboBoxSelectorMap = new System.Windows.Forms.ComboBox(); + this.pictureBox = new System.Windows.Forms.PictureBox(); + this.groupBoxTools.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + this.SuspendLayout(); + // + // groupBoxTools + // + this.groupBoxTools.Controls.Add(this.maskedTextBoxPosition); + this.groupBoxTools.Controls.Add(this.buttonRemoveHoistingCrane); + this.groupBoxTools.Controls.Add(this.buttonShowStorage); + this.groupBoxTools.Controls.Add(this.buttonDown); + this.groupBoxTools.Controls.Add(this.buttonRight); + this.groupBoxTools.Controls.Add(this.buttonLeft); + this.groupBoxTools.Controls.Add(this.buttonUp); + this.groupBoxTools.Controls.Add(this.buttonShowOnMap); + this.groupBoxTools.Controls.Add(this.buttonAddHoistingCrane); + this.groupBoxTools.Controls.Add(this.comboBoxSelectorMap); + this.groupBoxTools.Dock = System.Windows.Forms.DockStyle.Right; + this.groupBoxTools.Location = new System.Drawing.Point(811, 0); + this.groupBoxTools.Name = "groupBoxTools"; + this.groupBoxTools.Size = new System.Drawing.Size(204, 554); + this.groupBoxTools.TabIndex = 0; + this.groupBoxTools.TabStop = false; + this.groupBoxTools.Text = "Инструменты"; + // + // maskedTextBoxPosition + // + this.maskedTextBoxPosition.Location = new System.Drawing.Point(17, 166); + this.maskedTextBoxPosition.Mask = "00"; + this.maskedTextBoxPosition.Name = "maskedTextBoxPosition"; + this.maskedTextBoxPosition.Size = new System.Drawing.Size(175, 23); + this.maskedTextBoxPosition.TabIndex = 2; + this.maskedTextBoxPosition.ValidatingType = typeof(int); + // + // buttonRemoveHoistingCrane + // + this.buttonRemoveHoistingCrane.Location = new System.Drawing.Point(17, 195); + this.buttonRemoveHoistingCrane.Name = "buttonRemoveHoistingCrane"; + this.buttonRemoveHoistingCrane.Size = new System.Drawing.Size(175, 35); + this.buttonRemoveHoistingCrane.TabIndex = 3; + this.buttonRemoveHoistingCrane.Text = "Удалить подъёмный кран"; + this.buttonRemoveHoistingCrane.UseVisualStyleBackColor = true; + this.buttonRemoveHoistingCrane.Click += new System.EventHandler(this.ButtonRemoveHoistingCrane_Click); + // + // buttonShowStorage + // + this.buttonShowStorage.Location = new System.Drawing.Point(17, 287); + this.buttonShowStorage.Name = "buttonShowStorage"; + this.buttonShowStorage.Size = new System.Drawing.Size(175, 35); + this.buttonShowStorage.TabIndex = 4; + this.buttonShowStorage.Text = "Посмотреть хранилище"; + this.buttonShowStorage.UseVisualStyleBackColor = true; + this.buttonShowStorage.Click += new System.EventHandler(this.ButtonShowStorage_Click); + // + // buttonDown + // + this.buttonDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonDown.BackgroundImage = global::HoistingCrane.Properties.Resources.down; + this.buttonDown.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonDown.Location = new System.Drawing.Point(91, 504); + this.buttonDown.Name = "buttonDown"; + this.buttonDown.Size = new System.Drawing.Size(30, 30); + this.buttonDown.TabIndex = 10; + this.buttonDown.UseVisualStyleBackColor = true; + this.buttonDown.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonRight + // + this.buttonRight.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRight.BackgroundImage = global::HoistingCrane.Properties.Resources.right; + this.buttonRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonRight.Location = new System.Drawing.Point(127, 504); + this.buttonRight.Name = "buttonRight"; + this.buttonRight.Size = new System.Drawing.Size(30, 30); + this.buttonRight.TabIndex = 9; + this.buttonRight.UseVisualStyleBackColor = true; + this.buttonRight.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonLeft + // + this.buttonLeft.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonLeft.BackgroundImage = global::HoistingCrane.Properties.Resources.left; + this.buttonLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonLeft.Location = new System.Drawing.Point(55, 504); + this.buttonLeft.Name = "buttonLeft"; + this.buttonLeft.Size = new System.Drawing.Size(30, 30); + this.buttonLeft.TabIndex = 8; + this.buttonLeft.UseVisualStyleBackColor = true; + this.buttonLeft.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonUp + // + this.buttonUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonUp.BackgroundImage = global::HoistingCrane.Properties.Resources.up; + this.buttonUp.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; + this.buttonUp.Location = new System.Drawing.Point(91, 468); + this.buttonUp.Name = "buttonUp"; + this.buttonUp.Size = new System.Drawing.Size(30, 30); + this.buttonUp.TabIndex = 7; + this.buttonUp.UseVisualStyleBackColor = true; + this.buttonUp.Click += new System.EventHandler(this.ButtonMove_Click); + // + // buttonShowOnMap + // + this.buttonShowOnMap.Location = new System.Drawing.Point(17, 391); + this.buttonShowOnMap.Name = "buttonShowOnMap"; + this.buttonShowOnMap.Size = new System.Drawing.Size(175, 35); + this.buttonShowOnMap.TabIndex = 5; + this.buttonShowOnMap.Text = "Посмотреть карту"; + this.buttonShowOnMap.UseVisualStyleBackColor = true; + this.buttonShowOnMap.Click += new System.EventHandler(this.ButtonShowOnMap_Click); + // + // buttonAddHoistingCrane + // + this.buttonAddHoistingCrane.Location = new System.Drawing.Point(17, 106); + this.buttonAddHoistingCrane.Name = "buttonAddWarship"; + this.buttonAddHoistingCrane.Size = new System.Drawing.Size(175, 35); + this.buttonAddHoistingCrane.TabIndex = 1; + this.buttonAddHoistingCrane.Text = "Добавить подъёмный кран"; + this.buttonAddHoistingCrane.UseVisualStyleBackColor = true; + this.buttonAddHoistingCrane.Click += new System.EventHandler(this.ButtonAddHoistingCrane_Click); + // + // comboBoxSelectorMap + // + this.comboBoxSelectorMap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxSelectorMap.FormattingEnabled = true; + this.comboBoxSelectorMap.Items.AddRange(new object[] { + "Первая карта", + "Вторая карта"}); + this.comboBoxSelectorMap.Location = new System.Drawing.Point(17, 32); + this.comboBoxSelectorMap.Name = "comboBoxSelectorMap"; + this.comboBoxSelectorMap.Size = new System.Drawing.Size(175, 23); + 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(811, 554); + this.pictureBox.TabIndex = 1; + this.pictureBox.TabStop = false; + this.pictureBox.Click += new System.EventHandler(this.pictureBox_Click); + // + // FormMapWithSetHoistingCrane + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(1015, 554); + this.Controls.Add(this.pictureBox); + this.Controls.Add(this.groupBoxTools); + this.Name = "FormMapWithSetHoistingCrane"; + this.Text = "Карта с набором объектов"; + this.groupBoxTools.ResumeLayout(false); + this.groupBoxTools.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private GroupBox groupBoxTools; + private PictureBox pictureBox; + private ComboBox comboBoxSelectorMap; + private Button buttonShowOnMap; + private Button buttonAddHoistingCrane; + private Button buttonDown; + private Button buttonRight; + private Button buttonLeft; + private Button buttonUp; + private Button buttonShowStorage; + private Button buttonRemoveHoistingCrane; + private MaskedTextBox maskedTextBoxPosition; + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + + + #endregion + } +} \ No newline at end of file diff --git a/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.cs b/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.cs new file mode 100644 index 0000000..8784862 --- /dev/null +++ b/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.cs @@ -0,0 +1,139 @@ +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 HoistingCrane +{ + public partial class FormMapWithSetHoistingCrane : Form + { + + private MapWithSetHoistingCraneGeneric _mapHoistingCraneCollectionGeneric; + + public FormMapWithSetHoistingCrane() + { + InitializeComponent(); + } + + private void ComboBoxSelectorMap_SelectedIndexChanged(object sender, EventArgs e) + { + AbstractMap map = null; + switch (comboBoxSelectorMap.Text) + { + case "Первая карта": + map = new SimpleMap(); + break; + case "Вторая карта": + map = new SecondMap(); + break; + } + if (map != null) + { + _mapHoistingCraneCollectionGeneric = new MapWithSetHoistingCraneGeneric( + pictureBox.Width, pictureBox.Height, map); + } + else + { + _mapHoistingCraneCollectionGeneric = null; + } + } + + private void ButtonAddHoistingCrane_Click(object sender, EventArgs e) + { + if (_mapHoistingCraneCollectionGeneric == null) + { + return; + } + FormHoistingCrane form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + DrawingObjectHoistingCrane hoistingCrane = new(form.SelectedHoistingCrane); + if (_mapHoistingCraneCollectionGeneric + hoistingCrane == 1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _mapHoistingCraneCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + + private void ButtonRemoveHoistingCrane_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 (_mapHoistingCraneCollectionGeneric - pos == null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _mapHoistingCraneCollectionGeneric.ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void ButtonShowStorage_Click(object sender, EventArgs e) + { + if (_mapHoistingCraneCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapHoistingCraneCollectionGeneric.ShowSet(); + } + + private void ButtonShowOnMap_Click(object sender, EventArgs e) + { + if (_mapHoistingCraneCollectionGeneric == null) + { + return; + } + pictureBox.Image = _mapHoistingCraneCollectionGeneric.ShowOnMap(); + } + + private void ButtonMove_Click(object sender, EventArgs e) + { + if (_mapHoistingCraneCollectionGeneric == null) + { + return; + } + string name = ((Button)sender)?.Name ?? string.Empty; + Direction enums = Direction.None; + switch (name) + { + case "buttonUp": + enums = Direction.Up; + break; + case "buttonDown": + enums = Direction.Down; + break; + case "buttonLeft": + enums = Direction.Left; + break; + case "buttonRight": + enums = Direction.Right; + break; + } + pictureBox.Image = _mapHoistingCraneCollectionGeneric.MoveObject(enums); + } + + private void pictureBox_Click(object sender, EventArgs e) + { + + } + } +} diff --git a/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.resx b/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/HoistingCrane/HoistingCrane/FormMapWithSetHoistingCrane.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/HoistingCrane/HoistingCrane/MapWithSetHoistingCraneGeneric.cs b/HoistingCrane/HoistingCrane/MapWithSetHoistingCraneGeneric.cs new file mode 100644 index 0000000..5ab5352 --- /dev/null +++ b/HoistingCrane/HoistingCrane/MapWithSetHoistingCraneGeneric.cs @@ -0,0 +1,137 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HoistingCrane +{ + internal class MapWithSetHoistingCraneGeneric + where T : class, IDrawingObject + where U : AbstractMap + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 200; + private readonly int _placeSizeHeight = 120; + private readonly SetHoistingCraneGeneric _setHoistingCrane; + private readonly U _map; + + public MapWithSetHoistingCraneGeneric(int picWidth, int picHeight, U map) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _setHoistingCrane = new SetHoistingCraneGeneric(width * height); + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _map = map; + } + + public static int operator +(MapWithSetHoistingCraneGeneric map, T bulldozer) + { + return map._setHoistingCrane.Insert(bulldozer); + } + + public static T operator -(MapWithSetHoistingCraneGeneric map, int position) + { + return map._setHoistingCrane.Remove(position); + } + + public Bitmap ShowSet() + { + Bitmap bmp = new(_pictureWidth, _pictureWidth); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawBulldozers(gr); + return bmp; + } + + public Bitmap ShowOnMap() + { + Shaking(); + for (int i = 0; i < _setHoistingCrane.Count; i++) + { + var bulldozer = _setHoistingCrane.Get(i); + if (bulldozer != null) + { + return _map.CreateMap(_pictureWidth, _pictureHeight, bulldozer); + } + } + return new(_pictureWidth, _pictureHeight); + } + + public Bitmap MoveObject(Direction direction) + { + if (_map != null) + { + return _map.MoveObject(direction); + } + return new(_pictureWidth, _pictureHeight); + } + + public void Shaking() + { + int j = _setHoistingCrane.Count - 1; + for (int i = 0; i < _setHoistingCrane.Count; i++) + { + if (_setHoistingCrane.Get(i) == null) + { + for (; j > i; j--) + { + var warship = _setHoistingCrane.Get(j); + if (warship != null) + { + _setHoistingCrane.Insert(warship, i); + _setHoistingCrane.Remove(j); + break; + } + } + if (j <= i) + { + return; + } + } + } + } + + private void DrawBackground(Graphics gr) + { + Pen pen = new(Color.Black, 3); + for (int i = 0; i <= _pictureWidth / _placeSizeWidth; i++) + { + for (int j = 0; j <= _pictureHeight / _placeSizeHeight + 1; ++j) + {//линия разметки места + gr.DrawLine(pen, i * _placeSizeWidth, j * _placeSizeHeight, i * _placeSizeWidth + _placeSizeWidth, j * _placeSizeHeight); + } + gr.DrawLine(pen, i * _placeSizeWidth, 0, i * _placeSizeWidth, (_pictureHeight / _placeSizeHeight) * _placeSizeHeight); + } + } + + private void DrawBulldozers(Graphics gr) + { + int heightEl = _pictureHeight / _placeSizeHeight; + int widthEl = _pictureWidth / _placeSizeWidth; + int curWidth = 3; + int curHeight = 3; + + for (int i = 0; i < _setHoistingCrane.Count; i++) + { + if (curHeight < 0) + { + return; + } + _setHoistingCrane.Get(i)?.SetObject(curWidth * _placeSizeWidth + 10, + curHeight * _placeSizeHeight + 10, _pictureWidth, _pictureHeight); + _setHoistingCrane.Get(i)?.DrawingObject(gr); + + if (curWidth == 0) + { + curWidth = 3; + curHeight--; + continue; + } + curWidth--; + } + } + } +} diff --git a/HoistingCrane/HoistingCrane/SetHoistingCraneGeneric.cs b/HoistingCrane/HoistingCrane/SetHoistingCraneGeneric.cs new file mode 100644 index 0000000..32f00d6 --- /dev/null +++ b/HoistingCrane/HoistingCrane/SetHoistingCraneGeneric.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HoistingCrane +{ + internal class SetHoistingCraneGeneric + where T : class + { + public readonly T[] _places; + + public int Count => _places.Length; + + public SetHoistingCraneGeneric(int count) + { + _places = new T[count]; + } + + public int Insert(T hoistingCrane) + { + return Insert(hoistingCrane, 0); + } + + public int Insert(T hoistingCrane, int position) + { + int emptypos = -1; + if (position >= Count && position < 0) + { + return -1; + } + + if (_places[position] == null) + { + _places[position] = hoistingCrane; + return 1; + } + + for (int i = position; i < Count; i++) + { + if (_places[i] == null) + { + emptypos = i; + break; + } + } + + if (emptypos != -1) + { + for (int i = emptypos; i > position; i--) + { + _places[i] = _places[i - 1]; + } + _places[position] = hoistingCrane; + return 1; + } + + return -1; + } + + public T Remove(int position) + { + if (position < Count && position >= 0 && _places[position] != null) + { + _places[position] = null; + T removed = _places[position]; + return removed; + } + + return null; + } + + public T Get(int position) + { + if (position >= Count && position < 0) + { + return null; + } + return _places[position]; + } + } +}