diff --git a/Hydroplane/DrawningPlane.cs b/Hydroplane/DrawningPlane.cs index d224024..0be8049 100644 --- a/Hydroplane/DrawningPlane.cs +++ b/Hydroplane/DrawningPlane.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Hydroplane.Entities; +using Hydroplane.MovementStrategy; namespace Hydroplane.DrawningObjects { @@ -45,6 +46,9 @@ namespace Hydroplane.DrawningObjects } EntityPlane = new EntityPlane(speed, weight, bodyColor); } + + public IMoveableObject GetMoveableObject => new DrawningObjectPlane(this); + public void SetPosition(int x, int y) { _startPosX = Math.Min(x, _pictureWidth - _planeWidth); diff --git a/Hydroplane/FormHydroplane.Designer.cs b/Hydroplane/FormHydroplane.Designer.cs index 4f06ff9..6af98a0 100644 --- a/Hydroplane/FormHydroplane.Designer.cs +++ b/Hydroplane/FormHydroplane.Designer.cs @@ -37,6 +37,7 @@ buttonCreateHydroplane = new Button(); comboBoxStrategy = new ComboBox(); buttonStep = new Button(); + buttonChoose = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxHydroplane).BeginInit(); SuspendLayout(); // @@ -139,11 +140,22 @@ buttonStep.UseVisualStyleBackColor = true; buttonStep.Click += buttonStep_Click; // + // buttonChoose + // + buttonChoose.Location = new Point(309, 398); + buttonChoose.Name = "buttonChoose"; + buttonChoose.Size = new Size(125, 35); + buttonChoose.TabIndex = 9; + buttonChoose.Text = "Choose"; + buttonChoose.UseVisualStyleBackColor = true; + buttonChoose.Click += ButtonSelectPLane_Click; + // // FormHydroplane // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(884, 461); + Controls.Add(buttonChoose); Controls.Add(buttonStep); Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateHydroplane); @@ -172,5 +184,6 @@ private Button buttonCreateHydroplane; private ComboBox comboBoxStrategy; private Button buttonStep; + private Button buttonChoose; } } \ No newline at end of file diff --git a/Hydroplane/FormHydroplane.cs b/Hydroplane/FormHydroplane.cs index 35154bf..641e297 100644 --- a/Hydroplane/FormHydroplane.cs +++ b/Hydroplane/FormHydroplane.cs @@ -17,9 +17,13 @@ namespace Hydroplane { private DrawningPlane? _drawingPlane; private AbstractStrategy? _abstractStrategy; + public DrawningPlane? SelectedPlane { get; private set; } + public FormHydroplane() { InitializeComponent(); + _abstractStrategy = null; + SelectedPlane = null; } private void Draw() { @@ -36,7 +40,13 @@ namespace Hydroplane private void buttonCreatePlane_Click(object sender, EventArgs e) { Random random = new(); - _drawingPlane = new DrawningPlane(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxHydroplane.Width, pictureBoxHydroplane.Height); + 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; + } + _drawingPlane = new DrawningPlane(random.Next(100, 300), random.Next(1000, 3000), color, pictureBoxHydroplane.Width, pictureBoxHydroplane.Height); _drawingPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -44,7 +54,18 @@ namespace Hydroplane private void buttonCreateHydroplane_Click(object sender, EventArgs e) { Random random = new(); - _drawingPlane = new DrawningHydroplane(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)), + Color mainColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + mainColor = dialog.Color; + } + Color addColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + if (dialog.ShowDialog() == DialogResult.OK) + { + addColor = dialog.Color; + } + _drawingPlane = new DrawningHydroplane(random.Next(100, 300), random.Next(1000, 3000), mainColor, addColor, Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxHydroplane.Width, pictureBoxHydroplane.Height); _drawingPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); @@ -94,8 +115,7 @@ namespace Hydroplane { return; } - _abstractStrategy.SetData(new DrawningObjectPlane(_drawingPlane), pictureBoxHydroplane.Width, - pictureBoxHydroplane.Height); + _abstractStrategy.SetData(_drawingPlane.GetMoveableObject, pictureBoxHydroplane.Width, pictureBoxHydroplane.Height); comboBoxStrategy.Enabled = false; } if (_abstractStrategy == null) @@ -111,5 +131,11 @@ namespace Hydroplane } } + private void ButtonSelectPLane_Click(object sender, EventArgs e) + { + SelectedPlane = _drawingPlane; + DialogResult = DialogResult.OK; + } + } } \ No newline at end of file diff --git a/Hydroplane/FormHydroplaneCollection.Designer.cs b/Hydroplane/FormHydroplaneCollection.Designer.cs new file mode 100644 index 0000000..75355af --- /dev/null +++ b/Hydroplane/FormHydroplaneCollection.Designer.cs @@ -0,0 +1,142 @@ +namespace Hydroplane +{ + partial class FormHydroplaneCollection + { + /// + /// 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(); + UpdateButton = new Button(); + DeleteButton = new Button(); + AddButton = new Button(); + InputNum = new TextBox(); + label1 = new Label(); + DrawPlane = new PictureBox(); + panel1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)DrawPlane).BeginInit(); + SuspendLayout(); + // + // panel1 + // + panel1.Controls.Add(UpdateButton); + panel1.Controls.Add(DeleteButton); + panel1.Controls.Add(AddButton); + panel1.Controls.Add(InputNum); + panel1.Controls.Add(label1); + panel1.Dock = DockStyle.Right; + panel1.Location = new Point(577, 0); + panel1.Margin = new Padding(3, 2, 3, 2); + panel1.Name = "panel1"; + panel1.Size = new Size(219, 338); + panel1.TabIndex = 0; + // + // UpdateButton + // + UpdateButton.Location = new Point(9, 181); + UpdateButton.Margin = new Padding(3, 2, 3, 2); + UpdateButton.Name = "UpdateButton"; + UpdateButton.Size = new Size(200, 28); + UpdateButton.TabIndex = 4; + UpdateButton.Text = "Обновить коллекцию"; + UpdateButton.UseVisualStyleBackColor = true; + UpdateButton.Click += ButtonRefreshCollection_Click; + // + // DeleteButton + // + DeleteButton.Location = new Point(9, 132); + DeleteButton.Margin = new Padding(3, 2, 3, 2); + DeleteButton.Name = "DeleteButton"; + DeleteButton.Size = new Size(200, 28); + DeleteButton.TabIndex = 3; + DeleteButton.Text = "Удалить самолёт"; + DeleteButton.UseVisualStyleBackColor = true; + DeleteButton.Click += ButtonRemoveCar_Click; + // + // AddButton + // + AddButton.Location = new Point(9, 40); + AddButton.Margin = new Padding(3, 2, 3, 2); + AddButton.Name = "AddButton"; + AddButton.Size = new Size(200, 28); + AddButton.TabIndex = 2; + AddButton.Text = "Добавить самолёт"; + AddButton.UseVisualStyleBackColor = true; + AddButton.Click += ButtonAddTank_Click; + // + // InputNum + // + InputNum.Location = new Point(9, 100); + InputNum.Margin = new Padding(3, 2, 3, 2); + InputNum.Name = "InputNum"; + InputNum.Size = new Size(200, 23); + InputNum.TabIndex = 1; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(2, 2); + label1.Name = "label1"; + label1.Size = new Size(83, 15); + label1.TabIndex = 0; + label1.Text = "Инструменты"; + // + // DrawPlane + // + DrawPlane.Dock = DockStyle.Fill; + DrawPlane.Location = new Point(0, 0); + DrawPlane.Margin = new Padding(3, 2, 3, 2); + DrawPlane.Name = "DrawPlane"; + DrawPlane.Size = new Size(577, 338); + DrawPlane.TabIndex = 1; + DrawPlane.TabStop = false; + // + // FormHydroplaneCollection + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(796, 338); + Controls.Add(DrawPlane); + Controls.Add(panel1); + Margin = new Padding(3, 2, 3, 2); + Name = "FormHydroplaneCollection"; + Text = "Form1"; + panel1.ResumeLayout(false); + panel1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)DrawPlane).EndInit(); + ResumeLayout(false); + } + + #endregion + + private Panel panel1; + private Button UpdateButton; + private Button DeleteButton; + private Button AddButton; + private TextBox InputNum; + private Label label1; + private PictureBox DrawPlane; + } +} \ No newline at end of file diff --git a/Hydroplane/FormHydroplaneCollection.cs b/Hydroplane/FormHydroplaneCollection.cs new file mode 100644 index 0000000..5f94f71 --- /dev/null +++ b/Hydroplane/FormHydroplaneCollection.cs @@ -0,0 +1,72 @@ +using Hydroplane.DrawningObjects; +using Hydroplane.Generics; +using Hydroplane.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 Hydroplane +{ + public partial class FormHydroplaneCollection : Form + { + private readonly PlanesGenericCollection _planes; + + public FormHydroplaneCollection() + { + InitializeComponent(); + _planes = new PlanesGenericCollection(DrawPlane.Width, DrawPlane.Height); + } + + private void ButtonAddTank_Click(object sender, EventArgs e) + { + FormHydroplane form = new FormHydroplane(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_planes + form.SelectedPlane != -1) + { + MessageBox.Show("Объект добавлен"); + DrawPlane.Image = _planes.ShowPlanes(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + + private void ButtonRemoveCar_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = -1; + try + { + pos = Convert.ToInt32(InputNum.Text); + } + catch (Exception ex) { } + if (_planes - pos) + { + MessageBox.Show("Объект удален"); + DrawPlane.Image = _planes.ShowPlanes(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + DrawPlane.Image = _planes.ShowPlanes(); + } + } +} diff --git a/Hydroplane/FormHydroplaneCollection.resx b/Hydroplane/FormHydroplaneCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Hydroplane/FormHydroplaneCollection.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/Hydroplane/PlanesGenericCollection.cs b/Hydroplane/PlanesGenericCollection.cs new file mode 100644 index 0000000..7077420 --- /dev/null +++ b/Hydroplane/PlanesGenericCollection.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hydroplane.DrawningObjects; +using Hydroplane.MovementStrategy; + +namespace Hydroplane.Generics +{ + internal class PlanesGenericCollection + where T : DrawningPlane + where U : IMoveableObject + { + private readonly int _pictureWidth; + private readonly int _pictureHeight; + private readonly int _placeSizeWidth = 175; + private readonly int _placeSizeHeight = 85; + private readonly SetGeneric _collection; + + public PlanesGenericCollection(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 +(PlanesGenericCollection collect, T? + obj) + { + if (obj == null) + { + return -1; + } + return collect?._collection.Insert(obj); + } + + public static bool operator -(PlanesGenericCollection collect, int + pos) + { + T? obj = collect._collection.Get(pos); + if (obj == null) + { + return false; + } + return collect._collection.Remove(pos); + } + + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + public Bitmap ShowPlanes() + { + 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) + { + for (int i = 0; i < _collection.Count; i++) + { + T? t = _collection.Get(i); + if (t != null) + { + t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); + if (t is DrawningPlane) (t as DrawningPlane).DrawTransport(g); + else t.DrawTransport(g); + } + } + } + } +} diff --git a/Hydroplane/Program.cs b/Hydroplane/Program.cs index 35abc6f..149f3d5 100644 --- a/Hydroplane/Program.cs +++ b/Hydroplane/Program.cs @@ -11,7 +11,7 @@ namespace Hydroplane // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormHydroplane()); + Application.Run(new FormHydroplaneCollection()); } } } \ No newline at end of file diff --git a/Hydroplane/SetGeneric.cs b/Hydroplane/SetGeneric.cs new file mode 100644 index 0000000..d5e3b98 --- /dev/null +++ b/Hydroplane/SetGeneric.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hydroplane.Generics +{ + internal class SetGeneric where T : class + { + private readonly T?[] _places; + + public int Count => _places.Length; + + public SetGeneric(int count) + { + _places = new T?[count]; + } + + public int Insert(T plane) + { + if (_places[Count - 1] != null) + return -1; + return Insert(plane, 0); + } + + public int Insert(T plane, 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] = plane; + return position; + } + + public bool Remove(int position) + { + if (position < 0 || position >= _places.Count()) + return false; + + _places[position] = null; + + return true; + } + + public T? Get(int position) + { + if (position < 0 || position >= _places.Count()) + return null; + return _places[position]; + } + } +}