From 04b4e75288359559754c9b9ab366bc437d9fc1f2 Mon Sep 17 00:00:00 2001 From: Efi Date: Thu, 7 Dec 2023 16:08:00 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirBomber/AirBomber/DrawingWarAirplane.cs | 2 + AirBomber/AirBomber/FormAirBomber.Designer.cs | 13 ++ AirBomber/AirBomber/FormAirBomber.cs | 39 ++++- .../FormWarAirplaneCollection.Designer.cs | 135 ++++++++++++++++++ .../AirBomber/FormWarAirplaneCollection.cs | 64 +++++++++ .../AirBomber/FormWarAirplaneCollection.resx | 120 ++++++++++++++++ AirBomber/AirBomber/Program.cs | 2 +- AirBomber/AirBomber/SetGeneric.cs | 66 +++++++++ .../AirBomber/WarAirplaneGenericCollection.cs | 94 ++++++++++++ 9 files changed, 528 insertions(+), 7 deletions(-) create mode 100644 AirBomber/AirBomber/FormWarAirplaneCollection.Designer.cs create mode 100644 AirBomber/AirBomber/FormWarAirplaneCollection.cs create mode 100644 AirBomber/AirBomber/FormWarAirplaneCollection.resx create mode 100644 AirBomber/AirBomber/SetGeneric.cs create mode 100644 AirBomber/AirBomber/WarAirplaneGenericCollection.cs diff --git a/AirBomber/AirBomber/DrawingWarAirplane.cs b/AirBomber/AirBomber/DrawingWarAirplane.cs index 719d873..95e67e0 100644 --- a/AirBomber/AirBomber/DrawingWarAirplane.cs +++ b/AirBomber/AirBomber/DrawingWarAirplane.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using AirBomber.Entities; +using AirBomber.MovementStrategy; namespace AirBomber.DrawingObjects { @@ -21,6 +22,7 @@ namespace AirBomber.DrawingObjects public int GetPosY => _startPosY; public int GetWidth => _WarAirplaneWidth; public int GetHeight => _WarAirplaneHeight; + public IMoveableObject GetMoveableObject => new DrawingObjectWarAirplane(this); public DrawingWarAirplane(int speed, double weight, Color bodyColor, int width, int height) diff --git a/AirBomber/AirBomber/FormAirBomber.Designer.cs b/AirBomber/AirBomber/FormAirBomber.Designer.cs index 2818675..be2c9e4 100644 --- a/AirBomber/AirBomber/FormAirBomber.Designer.cs +++ b/AirBomber/AirBomber/FormAirBomber.Designer.cs @@ -37,6 +37,7 @@ buttonCreateAirBomber = new Button(); buttonStep = new Button(); comboBoxStrategy = new ComboBox(); + ButtonSelectWarAirplane = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirBomber).BeginInit(); SuspendLayout(); // @@ -138,11 +139,22 @@ comboBoxStrategy.Size = new Size(156, 23); comboBoxStrategy.TabIndex = 8; // + // ButtonSelectWarAirplane + // + ButtonSelectWarAirplane.Location = new Point(379, 394); + ButtonSelectWarAirplane.Name = "ButtonSelectWarAirplane"; + ButtonSelectWarAirplane.Size = new Size(156, 41); + ButtonSelectWarAirplane.TabIndex = 9; + ButtonSelectWarAirplane.Text = "Выбрать самолёт"; + ButtonSelectWarAirplane.UseVisualStyleBackColor = true; + ButtonSelectWarAirplane.Click += ButtonSelectWarAirplane_Click; + // // FormAirBomber // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(884, 461); + Controls.Add(ButtonSelectWarAirplane); Controls.Add(comboBoxStrategy); Controls.Add(buttonStep); Controls.Add(buttonCreateAirBomber); @@ -171,5 +183,6 @@ private Button buttonCreateAirBomber; private Button buttonStep; private ComboBox comboBoxStrategy; + private Button ButtonSelectWarAirplane; } } \ No newline at end of file diff --git a/AirBomber/AirBomber/FormAirBomber.cs b/AirBomber/AirBomber/FormAirBomber.cs index 1666664..238315d 100644 --- a/AirBomber/AirBomber/FormAirBomber.cs +++ b/AirBomber/AirBomber/FormAirBomber.cs @@ -1,5 +1,7 @@ using AirBomber.DrawingObjects; using AirBomber.MovementStrategy; +using System.Drawing; +using System; using System.Windows.Forms; namespace AirBomber @@ -8,9 +10,12 @@ namespace AirBomber { private DrawingWarAirplane? _drawingWarAirplane; private AbstractStrategy? _abstractStrategy; + public DrawingWarAirplane? SelectedWarAirplane { get; private set; } public FormAirBomber() { InitializeComponent(); + _abstractStrategy = null; + SelectedWarAirplane = null; } private void Draw() @@ -28,17 +33,35 @@ namespace AirBomber private void ButtonCreateAirBomber_Click(object sender, EventArgs e) { Random rnd = new(); + Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + Color additionalColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } + if (dialog.ShowDialog() == DialogResult.OK) + { + additionalColor = dialog.Color; + } + _drawingWarAirplane = new DrawingAirBomber(rnd.Next(100, 300), rnd.Next(1000, 2000), - Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)), Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)), - Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); + color, additionalColor, Convert.ToBoolean(rnd.Next(0, 2)), Convert.ToBoolean(rnd.Next(0, 2)), + pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); _drawingWarAirplane.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100)); Draw(); } private void ButtonCreateWarAirplane_Click(object sender, EventArgs e) { Random rnd = new(); + Color color = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + color = dialog.Color; + } _drawingWarAirplane = new DrawingWarAirplane(rnd.Next(100, 300), rnd.Next(1000, 2000), - Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)), pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); + color, pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); _drawingWarAirplane.SetPosition(rnd.Next(10, 100), rnd.Next(10, 100)); Draw(); } @@ -88,15 +111,14 @@ namespace AirBomber { return; } - _abstractStrategy.SetData(new - DrawingObjectWarAirplane(_drawingWarAirplane), pictureBoxAirBomber.Width, + _abstractStrategy.SetData(_drawingWarAirplane.GetMoveableObject, pictureBoxAirBomber.Width, pictureBoxAirBomber.Height); - comboBoxStrategy.Enabled = false; } if (_abstractStrategy == null) { return; } + comboBoxStrategy.Enabled = false; _abstractStrategy.MakeStep(); Draw(); if (_abstractStrategy.GetStatus() == Status.Finish) @@ -105,6 +127,11 @@ namespace AirBomber _abstractStrategy = null; } } + private void ButtonSelectWarAirplane_Click(object sender, EventArgs e) + { + SelectedWarAirplane = _drawingWarAirplane; + DialogResult = DialogResult.OK; + } } } \ No newline at end of file diff --git a/AirBomber/AirBomber/FormWarAirplaneCollection.Designer.cs b/AirBomber/AirBomber/FormWarAirplaneCollection.Designer.cs new file mode 100644 index 0000000..92c0ee2 --- /dev/null +++ b/AirBomber/AirBomber/FormWarAirplaneCollection.Designer.cs @@ -0,0 +1,135 @@ +namespace AirBomber +{ + partial class FormWarAirplaneCollection + { + /// + /// 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() + { + panel1 = new Panel(); + Tools = new Label(); + buttonRefreshCollection = new Button(); + buttonRemove = new Button(); + buttonAdd = new Button(); + maskedTextBoxNumber = new MaskedTextBox(); + pictureBoxCollection = new PictureBox(); + panel1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + SuspendLayout(); + // + // panel1 + // + panel1.Controls.Add(Tools); + panel1.Controls.Add(buttonRefreshCollection); + panel1.Controls.Add(buttonRemove); + panel1.Controls.Add(buttonAdd); + panel1.Controls.Add(maskedTextBoxNumber); + panel1.Dock = DockStyle.Right; + panel1.Location = new Point(617, 0); + panel1.Name = "panel1"; + panel1.Size = new Size(183, 450); + panel1.TabIndex = 0; + // + // Tools + // + Tools.AutoSize = true; + Tools.Location = new Point(53, 9); + Tools.Name = "Tools"; + Tools.Size = new Size(83, 15); + Tools.TabIndex = 3; + Tools.Text = "Инструменты"; + // + // buttonRefreshCollection + // + buttonRefreshCollection.Location = new Point(6, 179); + buttonRefreshCollection.Name = "buttonRefreshCollection"; + buttonRefreshCollection.Size = new Size(174, 43); + buttonRefreshCollection.TabIndex = 2; + buttonRefreshCollection.Text = "Обновить коллекцию"; + buttonRefreshCollection.UseVisualStyleBackColor = true; + buttonRefreshCollection.Click += ButtonRefreshCollection_Click; + // + // buttonRemove + // + buttonRemove.Location = new Point(6, 120); + buttonRemove.Name = "buttonRemove"; + buttonRemove.Size = new Size(174, 31); + buttonRemove.TabIndex = 2; + buttonRemove.Text = "Удалить военный самолёт"; + buttonRemove.UseVisualStyleBackColor = true; + buttonRemove.Click += ButtonRemoveWarAirplane_Click; + // + // buttonAdd + // + buttonAdd.Location = new Point(6, 39); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(174, 31); + buttonAdd.TabIndex = 2; + buttonAdd.Text = "Добавить военный самолёт"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += ButtonAddWarAirplane_Click; + // + // maskedTextBoxNumber + // + maskedTextBoxNumber.Location = new Point(6, 91); + maskedTextBoxNumber.Name = "maskedTextBoxNumber"; + maskedTextBoxNumber.Size = new Size(174, 23); + maskedTextBoxNumber.TabIndex = 0; + // + // pictureBoxCollection + // + pictureBoxCollection.Dock = DockStyle.Left; + pictureBoxCollection.Location = new Point(0, 0); + pictureBoxCollection.Name = "pictureBoxCollection"; + pictureBoxCollection.Size = new Size(617, 450); + pictureBoxCollection.TabIndex = 1; + pictureBoxCollection.TabStop = false; + // + // FormWarAirplaneCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(pictureBoxCollection); + Controls.Add(panel1); + Name = "FormWarAirplaneCollection"; + Text = "Набор военных самолётов"; + panel1.ResumeLayout(false); + panel1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + ResumeLayout(false); + } + + #endregion + + private Panel panel1; + private PictureBox pictureBoxCollection; + private Label Tools; + private Button buttonRefreshCollection; + private Button buttonRemove; + private Button buttonAdd; + private MaskedTextBox maskedTextBoxNumber; + } +} \ No newline at end of file diff --git a/AirBomber/AirBomber/FormWarAirplaneCollection.cs b/AirBomber/AirBomber/FormWarAirplaneCollection.cs new file mode 100644 index 0000000..219e714 --- /dev/null +++ b/AirBomber/AirBomber/FormWarAirplaneCollection.cs @@ -0,0 +1,64 @@ +using AirBomber.Generics; +using AirBomber.DrawingObjects; +using AirBomber.MovementStrategy; +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 AirBomber +{ + public partial class FormWarAirplaneCollection : Form + { + private readonly WarAirplaneGenericCollection _warAirplane; + public FormWarAirplaneCollection() + { + InitializeComponent(); + _warAirplane = new WarAirplaneGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + private void ButtonAddWarAirplane_Click(object sender, EventArgs e) + { + FormAirBomber form = new(); + if(form.ShowDialog() == DialogResult.OK) + { + if (_warAirplane + form.SelectedWarAirplane != null) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _warAirplane.ShowWarAirplane(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + private void ButtonRemoveWarAirplane_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", +MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBoxNumber.Text); + if (_warAirplane - pos != null) + { + MessageBox.Show("Объект удалён"); + pictureBoxCollection.Image = _warAirplane.ShowWarAirplane(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _warAirplane.ShowWarAirplane(); + } + + } +} diff --git a/AirBomber/AirBomber/FormWarAirplaneCollection.resx b/AirBomber/AirBomber/FormWarAirplaneCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/AirBomber/AirBomber/FormWarAirplaneCollection.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/AirBomber/AirBomber/Program.cs b/AirBomber/AirBomber/Program.cs index 76b85fe..590fa0b 100644 --- a/AirBomber/AirBomber/Program.cs +++ b/AirBomber/AirBomber/Program.cs @@ -11,7 +11,7 @@ namespace AirBomber // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormAirBomber()); + Application.Run(new FormWarAirplaneCollection()); } } } \ No newline at end of file diff --git a/AirBomber/AirBomber/SetGeneric.cs b/AirBomber/AirBomber/SetGeneric.cs new file mode 100644 index 0000000..81cdd85 --- /dev/null +++ b/AirBomber/AirBomber/SetGeneric.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBomber.Generics +{ + internal class SetGeneric + where T : class + { + private readonly T?[] _places; + public int Count => _places.Length; + public int startPointer = 0; + public SetGeneric(int count) + { + _places = new T?[count]; + } + public int Insert(T WarAirplane) + { + if (_places[Count - 1] != null) + return -1; + return Insert(WarAirplane, 0); + } + public int Insert(T WarAirplane, int position) + { + if (!(position >= 0 && position < Count)) + return -1; + if (_places[position] != null) + { + int indexEnd = position + 1; + while (_places[indexEnd] != null) + { + indexEnd++; + } + for (int i = indexEnd + 1; i > position; i--) + { + _places[i] = _places[i - 1]; + } + + } + _places[position] = WarAirplane; + return position; + } + public bool Remove(int position) + { + if (position < Count && position >= 0) + { + _places[position] = null; + return true; + } + return false; + + + } + public T? Get(int position) + { + if (position < Count && position >= 0) + { + return _places[position]; + } + return null; + + } + } +} diff --git a/AirBomber/AirBomber/WarAirplaneGenericCollection.cs b/AirBomber/AirBomber/WarAirplaneGenericCollection.cs new file mode 100644 index 0000000..014eab4 --- /dev/null +++ b/AirBomber/AirBomber/WarAirplaneGenericCollection.cs @@ -0,0 +1,94 @@ +using AirBomber.MovementStrategy; +using AirBomber.DrawingObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; + +namespace AirBomber.Generics +{ + internal class WarAirplaneGenericCollection + where T : DrawingWarAirplane + where U : IMoveableObject + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 110; + private readonly int _placeSizeHeight = 110; + private readonly SetGeneric _collection; + public WarAirplaneGenericCollection(int picWidth, int picHeight) + { + int width = picWidth / _placeSizeWidth; + int height = picHeight / _placeSizeHeight; + _pictureWidth = picWidth; + _pictureHeight = picHeight; + _collection = new SetGeneric(width * height); + } + public static int operator +(WarAirplaneGenericCollection collect, T? + obj) + { + if (obj == null) + { + return -1; + } + return collect._collection.Insert(obj); + } + public static bool operator -(WarAirplaneGenericCollection collect, int + pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + { + collect._collection.Remove(pos); + } + return true; + } + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + public Bitmap ShowWarAirplane() + { + Bitmap bmp = new(_pictureWidth, _pictureHeight); + Graphics gr = Graphics.FromImage(bmp); + DrawBackground(gr); + DrawObjects(gr); + return bmp; + } + 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); + } + } + private void DrawObjects(Graphics g) + { + DrawingWarAirplane warAirplane; + + for (int i = 0; i < _collection.Count; i++) + { + warAirplane = _collection.Get(i); + if (warAirplane != null) + { + int numPlacesInRow = _pictureWidth / _placeSizeWidth; + warAirplane.SetPosition((numPlacesInRow - 1 - (i % numPlacesInRow)) * _placeSizeWidth, + i / numPlacesInRow * _placeSizeHeight); + warAirplane.DrawTransport(g); + + } + } + } + } +}