diff --git a/Catamaran/AbstractStrategy.cs b/Catamaran/AbstractStrategy.cs index 1497fcf..40d503f 100644 --- a/Catamaran/AbstractStrategy.cs +++ b/Catamaran/AbstractStrategy.cs @@ -61,7 +61,7 @@ namespace Catamaran } return _moveableObject?.GetStep; } - + protected abstract void MoveToTarget(); protected abstract bool IsTargetDestinaion(); diff --git a/Catamaran/CatamaransGenericCollection.cs b/Catamaran/CatamaransGenericCollection.cs new file mode 100644 index 0000000..9abd405 --- /dev/null +++ b/Catamaran/CatamaransGenericCollection.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + internal class CatamaransGenericCollection + where T : DrawningCatamaran + where U : IMoveableObject + { + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + + private readonly int _placeSizeWidth = 210; + + private readonly int _placeSizeHeight = 90; + + private readonly SetGeneric _collection; + + public CatamaransGenericCollection(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 +(CatamaransGenericCollection collect, T? obj) + { + if (obj == null) + return -1; + return collect?._collection.Insert(obj) ?? -1; + } + + public static bool operator -(CatamaransGenericCollection collect, int pos) + { + T? obj = collect._collection.Get(pos); + if (obj != null) + collect._collection.Remove(pos); + return false; + } + + public U? GetU(int pos) + { + return (U?)_collection.Get(pos)?.GetMoveableObject; + } + + public Bitmap ShowCats() + { + 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++) + { + DrawningCatamaran catamaran = _collection.Get(i); + if (catamaran != null) + { + int inRow = _pictureWidth / _placeSizeWidth; + catamaran.SetPosition(i % inRow * _placeSizeWidth, ((_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight)+5); + catamaran.DrawTransport(g); + } + } + } + } +} diff --git a/Catamaran/DrawningCatamaran.cs b/Catamaran/DrawningCatamaran.cs index e5e1aa3..4f250f0 100644 --- a/Catamaran/DrawningCatamaran.cs +++ b/Catamaran/DrawningCatamaran.cs @@ -21,7 +21,9 @@ namespace Catamaran public readonly int _catamaranWidth = 120; public readonly int _catamaranHeight = 80; - + + public IMoveableObject GetMoveableObject => new DrawningObjectCatamaran(this); + public DrawningCatamaran(int speed, double weight, Color bodyColor, Color additioncolor, int width, int height, bool seats) { if (_pictureHeight < _catamaranWidth && _pictureWidth < _catamaranHeight) return; @@ -85,7 +87,6 @@ namespace Catamaran }; } - public void MoveTransport(DirectionType direction) { if (EntityCatamaran == null) diff --git a/Catamaran/FormCatamaran.Designer.cs b/Catamaran/FormCatamaran.Designer.cs index 17179bc..dfea227 100644 --- a/Catamaran/FormCatamaran.Designer.cs +++ b/Catamaran/FormCatamaran.Designer.cs @@ -2,7 +2,9 @@ { partial class FormCatamaran { + private System.ComponentModel.IContainer components = null; + protected override void Dispose(bool disposing) { if (disposing && (components != null)) @@ -14,10 +16,6 @@ #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() { pictureBox=new PictureBox(); @@ -29,6 +27,7 @@ buttonCreatePro=new Button(); comboBox=new ComboBox(); buttonStep=new Button(); + buttonSelect=new Button(); ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); SuspendLayout(); // @@ -136,11 +135,23 @@ buttonStep.UseVisualStyleBackColor=true; buttonStep.Click+=buttonStep_Click; // + // buttonSelect + // + buttonSelect.ImageAlign=ContentAlignment.TopCenter; + buttonSelect.Location=new Point(431, 638); + buttonSelect.Name="buttonSelect"; + buttonSelect.Size=new Size(197, 73); + buttonSelect.TabIndex=14; + buttonSelect.Text="Выбрать катамаран"; + buttonSelect.UseVisualStyleBackColor=true; + buttonSelect.Click+=buttonSelect_Click; + // // FormCatamaran // AutoScaleDimensions=new SizeF(10F, 25F); AutoScaleMode=AutoScaleMode.Font; ClientSize=new Size(1143, 750); + Controls.Add(buttonSelect); Controls.Add(buttonStep); Controls.Add(comboBox); Controls.Add(buttonCreatePro); @@ -170,5 +181,6 @@ private Button buttonCreatePro; private ComboBox comboBox; private Button buttonStep; + public Button buttonSelect; } } \ No newline at end of file diff --git a/Catamaran/FormCatamaran.cs b/Catamaran/FormCatamaran.cs index 73171d4..9ec31e9 100644 --- a/Catamaran/FormCatamaran.cs +++ b/Catamaran/FormCatamaran.cs @@ -9,6 +9,7 @@ namespace Catamaran private DrawningCatamaran? _DrawningCatamaran; private AbstractStrategy? _abstractStrategy; + public DrawningCatamaran? SelectedCatamaran { get; private set; } private void Draw() { @@ -22,7 +23,6 @@ namespace Catamaran pictureBox.Image = bmp; } - private void ButtonMove_Click(object sender, EventArgs e) { if (_DrawningCatamaran == null) @@ -53,7 +53,13 @@ namespace Catamaran Random random = new(); Color bodyColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); Color additionalColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); - _DrawningCatamaran = new DrawningCatamaran(random.Next(10, 30), random.Next(100, 300),bodyColor,additionalColor, pictureBox.Width, pictureBox.Height,true); + + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + bodyColor = dialog.Color; + } + _DrawningCatamaran = new DrawningCatamaran(random.Next(10, 30), random.Next(100, 300), bodyColor, additionalColor, pictureBox.Width, pictureBox.Height, true); _DrawningCatamaran.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -66,8 +72,19 @@ namespace Catamaran Color bodyColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); Color additionalColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); Color otherColor = Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)); + + ColorDialog dialog = new(); + if (dialog.ShowDialog() == DialogResult.OK) + { + bodyColor = dialog.Color; + } + if (dialog.ShowDialog() == DialogResult.OK) + { + additionalColor = dialog.Color; + } + _DrawningCatamaran = new DrawningCatamaranPro(random.Next(100, 300), - random.Next(1000, 3000),bodyColor,additionalColor,otherColor, true, true, true, pictureBox.Width, pictureBox.Height, true); + random.Next(1000, 3000), bodyColor, otherColor, additionalColor, true, true, true, pictureBox.Width, pictureBox.Height, true); _DrawningCatamaran.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -110,5 +127,12 @@ namespace Catamaran _abstractStrategy = null; } } + + private void buttonSelect_Click(object sender, EventArgs e) + { + SelectedCatamaran = _DrawningCatamaran; + DialogResult = DialogResult.OK; + } } + } \ No newline at end of file diff --git a/Catamaran/FormCatamaranCollection.Designer.cs b/Catamaran/FormCatamaranCollection.Designer.cs new file mode 100644 index 0000000..90b562f --- /dev/null +++ b/Catamaran/FormCatamaranCollection.Designer.cs @@ -0,0 +1,114 @@ +namespace Catamaran +{ + partial class FormCatamaranCollection + { + + private System.ComponentModel.IContainer components = null; + + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + + private void InitializeComponent() + { + groupBoxCollection=new GroupBox(); + maskedTextBox=new MaskedTextBox(); + buttonUpdateCollection=new Button(); + buttonDeleteCat=new Button(); + buttonAddCat=new Button(); + pictureBoxCollection=new PictureBox(); + groupBoxCollection.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + SuspendLayout(); + // + // groupBoxCollection + // + groupBoxCollection.Controls.Add(maskedTextBox); + groupBoxCollection.Controls.Add(buttonUpdateCollection); + groupBoxCollection.Controls.Add(buttonDeleteCat); + groupBoxCollection.Controls.Add(buttonAddCat); + groupBoxCollection.Location=new Point(889, 12); + groupBoxCollection.Name="groupBoxCollection"; + groupBoxCollection.Size=new Size(242, 637); + groupBoxCollection.TabIndex=0; + groupBoxCollection.TabStop=false; + groupBoxCollection.Text="Инструменты"; + // + // maskedTextBox + // + maskedTextBox.Location=new Point(21, 138); + maskedTextBox.Name="maskedTextBox"; + maskedTextBox.Size=new Size(215, 31); + maskedTextBox.TabIndex=2; + // + // buttonUpdateCollection + // + buttonUpdateCollection.Location=new Point(21, 286); + buttonUpdateCollection.Name="buttonUpdateCollection"; + buttonUpdateCollection.Size=new Size(215, 54); + buttonUpdateCollection.TabIndex=2; + buttonUpdateCollection.Text="Обновить коллекцию"; + buttonUpdateCollection.UseVisualStyleBackColor=true; + buttonUpdateCollection.Click+=ButtonRefreshCollection_Click; + // + // buttonDeleteCat + // + buttonDeleteCat.Location=new Point(21, 186); + buttonDeleteCat.Name="buttonDeleteCat"; + buttonDeleteCat.Size=new Size(215, 54); + buttonDeleteCat.TabIndex=1; + buttonDeleteCat.Text="Удалить катамаран"; + buttonDeleteCat.UseVisualStyleBackColor=true; + buttonDeleteCat.Click+=ButtonRemoveCat_Click; + // + // buttonAddCat + // + buttonAddCat.Location=new Point(21, 39); + buttonAddCat.Name="buttonAddCat"; + buttonAddCat.Size=new Size(215, 54); + buttonAddCat.TabIndex=0; + buttonAddCat.Text="Добавить катамаран"; + buttonAddCat.UseVisualStyleBackColor=true; + buttonAddCat.Click+=ButtonAddCat_Click; + // + // pictureBoxCollection + // + pictureBoxCollection.Location=new Point(0, 0); + pictureBoxCollection.Name="pictureBoxCollection"; + pictureBoxCollection.Size=new Size(883, 750); + pictureBoxCollection.TabIndex=1; + pictureBoxCollection.TabStop=false; + // + // FormCatamaranCollection + // + AutoScaleDimensions=new SizeF(10F, 25F); + AutoScaleMode=AutoScaleMode.Font; + ClientSize=new Size(1143, 750); + Controls.Add(pictureBoxCollection); + Controls.Add(groupBoxCollection); + Name="FormCatamaranCollection"; + Text="FormCatamaranCollection"; + groupBoxCollection.ResumeLayout(false); + groupBoxCollection.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxCollection; + private Button buttonAddCat; + private Button buttonUpdateCollection; + private Button buttonDeleteCat; + private MaskedTextBox maskedTextBox; + public PictureBox pictureBoxCollection; + } +} \ No newline at end of file diff --git a/Catamaran/FormCatamaranCollection.cs b/Catamaran/FormCatamaranCollection.cs new file mode 100644 index 0000000..cb3ac6b --- /dev/null +++ b/Catamaran/FormCatamaranCollection.cs @@ -0,0 +1,66 @@ +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 Catamaran +{ + public partial class FormCatamaranCollection : Form + { + + private readonly CatamaransGenericCollection _cats; + + public FormCatamaranCollection() + { + InitializeComponent(); + _cats = new CatamaransGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + } + + private void ButtonAddCat_Click(object sender, EventArgs e) + { + FormCatamaran form = new(); + if (form.ShowDialog() == DialogResult.OK) + { + if (_cats + form.SelectedCatamaran != null) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollection.Image = _cats.ShowCats(); + } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + } + + private void ButtonRemoveCat_Click(object sender, EventArgs e) + { + if (MessageBox.Show("Удалить объект?", "Удаление", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + int pos = Convert.ToInt32(maskedTextBox.Text); + if (_cats - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = _cats.ShowCats(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } + } + + private void ButtonRefreshCollection_Click(object sender, EventArgs e) + { + pictureBoxCollection.Image = _cats.ShowCats(); + } + } +} diff --git a/Catamaran/FormCatamaranCollection.resx b/Catamaran/FormCatamaranCollection.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Catamaran/FormCatamaranCollection.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/Catamaran/Program.cs b/Catamaran/Program.cs index 86dc55e..6d95577 100644 --- a/Catamaran/Program.cs +++ b/Catamaran/Program.cs @@ -2,12 +2,16 @@ namespace Catamaran { internal static class Program { - + /// + /// The main entry point for the application. + /// [STAThread] static void Main() { + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormCatamaran()); + Application.Run(new FormCatamaranCollection()); } } diff --git a/Catamaran/SetGeneric.cs b/Catamaran/SetGeneric.cs new file mode 100644 index 0000000..c5782fc --- /dev/null +++ b/Catamaran/SetGeneric.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + 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 catamaran) + { + if (_places[Count-1] != null) + return -1; + return Insert(catamaran, 0); + } + + public int Insert(T catamaran, int position) + { + if (!(position >= 0 && position < Count)) + return -1; + if (_places[position] != null) + { + int ind = position; + while (ind < Count && _places[ind] != null) + ind++; + if (ind == Count) + return -1; + for (int i = ind - 1; i >= position; i--) + _places[i + 1] = _places[i]; + } + _places[position] = catamaran; + return position; + } + + public bool Remove(int position) + { + if (!(position >= 0 && position < Count) || _places[position] == null) + return false; + _places[position] = null; + return true; + } + + public T? Get(int position) + { + if (!(position >= 0 && position < Count)) + return null; + return _places[position]; + } + } +}