From a9f980816e9a78a9d23b3cc296e5f7161c957161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D0=BA=D0=B0=D0=B4=D0=B8=D0=B9=20=D0=A0=D0=B0?= =?UTF-8?q?=D0=B4=D0=B0=D0=B5=D0=B2?= Date: Sun, 19 Nov 2023 12:36:59 +0400 Subject: [PATCH] res --- Catamaran/AbstractStrategy.cs | 2 +- Catamaran/CatamaransGenericCollection.cs | 23 ++-- Catamaran/CatamaransGenericStorage.cs | 51 ++++++++ Catamaran/DrawningCatamaran.cs | 1 + Catamaran/DrawningObjectCatamaran.cs | 7 +- Catamaran/FormCatamaran.Designer.cs | 30 ++--- Catamaran/FormCatamaran.cs | 14 +-- Catamaran/FormCatamaranCollection.Designer.cs | 87 ++++++++++++-- Catamaran/FormCatamaranCollection.cs | 112 +++++++++++++++--- Catamaran/SetGeneric.cs | 72 ++++++----- 10 files changed, 305 insertions(+), 94 deletions(-) create mode 100644 Catamaran/CatamaransGenericStorage.cs diff --git a/Catamaran/AbstractStrategy.cs b/Catamaran/AbstractStrategy.cs index 40d503f..1497fcf 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 index 9abd405..ad720a5 100644 --- a/Catamaran/CatamaransGenericCollection.cs +++ b/Catamaran/CatamaransGenericCollection.cs @@ -29,26 +29,26 @@ namespace Catamaran _collection = new SetGeneric(width * height); } - public static int operator +(CatamaransGenericCollection collect, T? obj) + public static bool operator +(CatamaransGenericCollection collect, T? obj) { if (obj == null) - return -1; - return collect?._collection.Insert(obj) ?? -1; + return false; + return collect?._collection.Insert(obj) ?? false; } - public static bool operator -(CatamaransGenericCollection collect, int pos) + public static T? operator -(CatamaransGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) collect._collection.Remove(pos); - return false; + return obj; } public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } - + public Bitmap ShowCats() { Bitmap bmp = new(_pictureWidth, _pictureHeight); @@ -75,15 +75,16 @@ namespace Catamaran } private void DrawObjects(Graphics g) { - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach (var catamaran in _collection.GetCatamarans()) { - DrawningCatamaran catamaran = _collection.Get(i); - if (catamaran != null) + if (catamaran!= null) { int inRow = _pictureWidth / _placeSizeWidth; catamaran.SetPosition(i % inRow * _placeSizeWidth, ((_collection.Count / inRow - 1 - i / inRow) * _placeSizeHeight)+5); catamaran.DrawTransport(g); } + i++; } } } diff --git a/Catamaran/CatamaransGenericStorage.cs b/Catamaran/CatamaransGenericStorage.cs new file mode 100644 index 0000000..27e2929 --- /dev/null +++ b/Catamaran/CatamaransGenericStorage.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + internal class CatamaransGenericStorage + { + readonly Dictionary> _catStorages; + + public List Keys => _catStorages.Keys.ToList(); + + private readonly int _pictureWidth; + + private readonly int _pictureHeight; + + public CatamaransGenericStorage(int pictureWidth, int pictureHeight) + { + _catStorages = new Dictionary>(); + _pictureWidth = pictureWidth; + _pictureHeight = pictureHeight; + } + + public void AddSet(string name) + { + _catStorages.Add(name, new CatamaransGenericCollection(_pictureWidth, _pictureHeight)); + } + + public void DelSet(string name) + { + if (!_catStorages.ContainsKey(name)) + return; + _catStorages.Remove(name); + } + + public CatamaransGenericCollection? this[string ind] + { + get + { + if (_catStorages.ContainsKey(ind)) + return _catStorages[ind]; + return null; + } + } + } +} diff --git a/Catamaran/DrawningCatamaran.cs b/Catamaran/DrawningCatamaran.cs index 4f250f0..9dc64d0 100644 --- a/Catamaran/DrawningCatamaran.cs +++ b/Catamaran/DrawningCatamaran.cs @@ -87,6 +87,7 @@ namespace Catamaran }; } + public void MoveTransport(DirectionType direction) { if (EntityCatamaran == null) diff --git a/Catamaran/DrawningObjectCatamaran.cs b/Catamaran/DrawningObjectCatamaran.cs index 9d99330..d7e7a8c 100644 --- a/Catamaran/DrawningObjectCatamaran.cs +++ b/Catamaran/DrawningObjectCatamaran.cs @@ -9,16 +9,15 @@ namespace Catamaran public class DrawningObjectCatamaran : IMoveableObject { private readonly DrawningCatamaran? _DrawningCatamaran = null; - public DrawningObjectCatamaran(DrawningCatamaran DrawningCatamaran) + public DrawningObjectCatamaran(DrawningCatamaran drawningCatamaran) { - _DrawningCatamaran = DrawningCatamaran; + _DrawningCatamaran = drawningCatamaran; } public ObjectParameters? GetObjectPosition { get { - if (_DrawningCatamaran == null || _DrawningCatamaran.EntityCatamaran == - null) + if (_DrawningCatamaran == null || _DrawningCatamaran.EntityCatamaran == null) { return null; } diff --git a/Catamaran/FormCatamaran.Designer.cs b/Catamaran/FormCatamaran.Designer.cs index dfea227..bdd6f22 100644 --- a/Catamaran/FormCatamaran.Designer.cs +++ b/Catamaran/FormCatamaran.Designer.cs @@ -2,7 +2,7 @@ { partial class FormCatamaran { - + private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) @@ -18,7 +18,7 @@ private void InitializeComponent() { - pictureBox=new PictureBox(); + pictureBoxCatatamaran=new PictureBox(); buttonCreateBasic=new Button(); buttonUp=new Button(); buttonRight=new Button(); @@ -28,19 +28,19 @@ comboBox=new ComboBox(); buttonStep=new Button(); buttonSelect=new Button(); - ((System.ComponentModel.ISupportInitialize)pictureBox).BeginInit(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCatatamaran).BeginInit(); SuspendLayout(); // - // pictureBox + // pictureBoxCatatamaran // - pictureBox.Dock=DockStyle.Fill; - pictureBox.Location=new Point(0, 0); - pictureBox.Margin=new Padding(4, 5, 4, 5); - pictureBox.Name="pictureBox"; - pictureBox.Size=new Size(1143, 750); - pictureBox.TabIndex=5; - pictureBox.TabStop=false; - pictureBox.Click+=ButtonMove_Click; + pictureBoxCatatamaran.Dock=DockStyle.Fill; + pictureBoxCatatamaran.Location=new Point(0, 0); + pictureBoxCatatamaran.Margin=new Padding(4, 5, 4, 5); + pictureBoxCatatamaran.Name="pictureBoxCatatamaran"; + pictureBoxCatatamaran.Size=new Size(1143, 750); + pictureBoxCatatamaran.TabIndex=5; + pictureBoxCatatamaran.TabStop=false; + pictureBoxCatatamaran.Click+=ButtonMove_Click; // // buttonCreateBasic // @@ -160,11 +160,11 @@ Controls.Add(buttonDown); Controls.Add(buttonLeft); Controls.Add(buttonCreateBasic); - Controls.Add(pictureBox); + Controls.Add(pictureBoxCatatamaran); Margin=new Padding(4, 5, 4, 5); Name="FormCatamaran"; Text="FormCatamaran"; - ((System.ComponentModel.ISupportInitialize)pictureBox).EndInit(); + ((System.ComponentModel.ISupportInitialize)pictureBoxCatatamaran).EndInit(); ResumeLayout(false); } @@ -172,7 +172,7 @@ private Button button1; private Button UpBut; - private PictureBox pictureBox; + private PictureBox pictureBoxCatatamaran; private Button buttonCreateBasic; private Button buttonUp; private Button buttonRight; diff --git a/Catamaran/FormCatamaran.cs b/Catamaran/FormCatamaran.cs index 9ec31e9..f33e6f1 100644 --- a/Catamaran/FormCatamaran.cs +++ b/Catamaran/FormCatamaran.cs @@ -17,10 +17,10 @@ namespace Catamaran { return; } - Bitmap bmp = new(pictureBox.Width, pictureBox.Height); + Bitmap bmp = new(pictureBoxCatatamaran.Width, pictureBoxCatatamaran.Height); Graphics gr = Graphics.FromImage(bmp); _DrawningCatamaran.DrawTransport(gr); - pictureBox.Image = bmp; + pictureBoxCatatamaran.Image = bmp; } private void ButtonMove_Click(object sender, EventArgs e) @@ -59,7 +59,7 @@ namespace Catamaran { bodyColor = dialog.Color; } - _DrawningCatamaran = new DrawningCatamaran(random.Next(10, 30), random.Next(100, 300), bodyColor, additionalColor, pictureBox.Width, pictureBox.Height, true); + _DrawningCatamaran = new DrawningCatamaran(random.Next(10, 30), random.Next(100, 300), bodyColor, additionalColor, pictureBoxCatatamaran.Width, pictureBoxCatatamaran.Height, true); _DrawningCatamaran.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -82,9 +82,9 @@ namespace Catamaran { additionalColor = dialog.Color; } - + _DrawningCatamaran = new DrawningCatamaranPro(random.Next(100, 300), - random.Next(1000, 3000), bodyColor, otherColor, additionalColor, true, true, true, pictureBox.Width, pictureBox.Height, true); + random.Next(1000, 3000), bodyColor, otherColor, additionalColor, true, true, true, pictureBoxCatatamaran.Width, pictureBoxCatatamaran.Height, true); _DrawningCatamaran.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); @@ -111,8 +111,8 @@ namespace Catamaran return; } _abstractStrategy.SetData(new - DrawningObjectCatamaran(_DrawningCatamaran), pictureBox.Width, - pictureBox.Height); + DrawningObjectCatamaran(_DrawningCatamaran), pictureBoxCatatamaran.Width, + pictureBoxCatatamaran.Height); comboBox.Enabled = false; } if (_abstractStrategy == null) diff --git a/Catamaran/FormCatamaranCollection.Designer.cs b/Catamaran/FormCatamaranCollection.Designer.cs index 90b562f..0439bb8 100644 --- a/Catamaran/FormCatamaranCollection.Designer.cs +++ b/Catamaran/FormCatamaranCollection.Designer.cs @@ -25,35 +25,42 @@ buttonDeleteCat=new Button(); buttonAddCat=new Button(); pictureBoxCollection=new PictureBox(); + setsBox=new GroupBox(); + AddSetButton=new Button(); + setAddBox=new TextBox(); + SetslistBox=new ListBox(); + DeleteSetButton=new Button(); groupBoxCollection.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); + setsBox.SuspendLayout(); SuspendLayout(); // // groupBoxCollection // + groupBoxCollection.Controls.Add(setsBox); 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.Size=new Size(242, 738); groupBoxCollection.TabIndex=0; groupBoxCollection.TabStop=false; groupBoxCollection.Text="Инструменты"; // // maskedTextBox // - maskedTextBox.Location=new Point(21, 138); + maskedTextBox.Location=new Point(21, 537); maskedTextBox.Name="maskedTextBox"; - maskedTextBox.Size=new Size(215, 31); + maskedTextBox.Size=new Size(204, 31); maskedTextBox.TabIndex=2; // // buttonUpdateCollection // - buttonUpdateCollection.Location=new Point(21, 286); + buttonUpdateCollection.Location=new Point(21, 666); buttonUpdateCollection.Name="buttonUpdateCollection"; - buttonUpdateCollection.Size=new Size(215, 54); + buttonUpdateCollection.Size=new Size(204, 54); buttonUpdateCollection.TabIndex=2; buttonUpdateCollection.Text="Обновить коллекцию"; buttonUpdateCollection.UseVisualStyleBackColor=true; @@ -61,23 +68,23 @@ // // buttonDeleteCat // - buttonDeleteCat.Location=new Point(21, 186); + buttonDeleteCat.Location=new Point(21, 584); buttonDeleteCat.Name="buttonDeleteCat"; - buttonDeleteCat.Size=new Size(215, 54); + buttonDeleteCat.Size=new Size(204, 54); buttonDeleteCat.TabIndex=1; buttonDeleteCat.Text="Удалить катамаран"; buttonDeleteCat.UseVisualStyleBackColor=true; - buttonDeleteCat.Click+=ButtonRemoveCat_Click; + buttonDeleteCat.Click+=ButtonRemoveCatamaran_Click; // // buttonAddCat // - buttonAddCat.Location=new Point(21, 39); + buttonAddCat.Location=new Point(21, 465); buttonAddCat.Name="buttonAddCat"; - buttonAddCat.Size=new Size(215, 54); + buttonAddCat.Size=new Size(204, 54); buttonAddCat.TabIndex=0; buttonAddCat.Text="Добавить катамаран"; buttonAddCat.UseVisualStyleBackColor=true; - buttonAddCat.Click+=ButtonAddCat_Click; + buttonAddCat.Click+=ButtonAddCatamaran_Click; // // pictureBoxCollection // @@ -87,6 +94,57 @@ pictureBoxCollection.TabIndex=1; pictureBoxCollection.TabStop=false; // + // setsBox + // + setsBox.Controls.Add(DeleteSetButton); + setsBox.Controls.Add(SetslistBox); + setsBox.Controls.Add(setAddBox); + setsBox.Controls.Add(AddSetButton); + setsBox.Location=new Point(6, 45); + setsBox.Name="setsBox"; + setsBox.RightToLeft=RightToLeft.No; + setsBox.Size=new Size(219, 374); + setsBox.TabIndex=3; + setsBox.TabStop=false; + setsBox.Text="Наборы"; + // + // AddSetButton + // + AddSetButton.Location=new Point(15, 71); + AddSetButton.Name="AddSetButton"; + AddSetButton.Size=new Size(190, 54); + AddSetButton.TabIndex=0; + AddSetButton.Text="Добавить набор"; + AddSetButton.UseVisualStyleBackColor=true; + AddSetButton.Click+=ButtonAddStorage_Click; + // + // setAddBox + // + setAddBox.Location=new Point(15, 34); + setAddBox.Name="setAddBox"; + setAddBox.Size=new Size(190, 31); + setAddBox.TabIndex=1; + // + // SetslistBox + // + SetslistBox.FormattingEnabled=true; + SetslistBox.ItemHeight=25; + SetslistBox.Location=new Point(15, 131); + SetslistBox.Name="SetslistBox"; + SetslistBox.Size=new Size(190, 129); + SetslistBox.TabIndex=2; + SetslistBox.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged; + // + // DeleteSetButton + // + DeleteSetButton.Location=new Point(15, 279); + DeleteSetButton.Name="DeleteSetButton"; + DeleteSetButton.Size=new Size(190, 64); + DeleteSetButton.TabIndex=3; + DeleteSetButton.Text="Удалить набор"; + DeleteSetButton.UseVisualStyleBackColor=true; + DeleteSetButton.Click+=ButtonDeleteStorage_Click; + // // FormCatamaranCollection // AutoScaleDimensions=new SizeF(10F, 25F); @@ -99,6 +157,8 @@ groupBoxCollection.ResumeLayout(false); groupBoxCollection.PerformLayout(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); + setsBox.ResumeLayout(false); + setsBox.PerformLayout(); ResumeLayout(false); } @@ -110,5 +170,10 @@ private Button buttonDeleteCat; private MaskedTextBox maskedTextBox; public PictureBox pictureBoxCollection; + private GroupBox setsBox; + private Button DeleteSetButton; + private ListBox SetslistBox; + private TextBox setAddBox; + private Button AddSetButton; } } \ No newline at end of file diff --git a/Catamaran/FormCatamaranCollection.cs b/Catamaran/FormCatamaranCollection.cs index cb3ac6b..484b1db 100644 --- a/Catamaran/FormCatamaranCollection.cs +++ b/Catamaran/FormCatamaranCollection.cs @@ -12,25 +12,87 @@ namespace Catamaran { public partial class FormCatamaranCollection : Form { - - private readonly CatamaransGenericCollection _cats; - + private readonly CatamaransGenericStorage _storage; + public FormCatamaranCollection() { InitializeComponent(); - _cats = new CatamaransGenericCollection(pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new CatamaransGenericStorage(pictureBoxCollection.Width, + pictureBoxCollection.Height); } - - private void ButtonAddCat_Click(object sender, EventArgs e) + + private void ReloadObjects() { + int index = SetslistBox.SelectedIndex; + SetslistBox.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) + { + SetslistBox.Items.Add(_storage.Keys[i]); + } + if (SetslistBox.Items.Count > 0 && (index == -1 || index + >= SetslistBox.Items.Count)) + { + SetslistBox.SelectedIndex = 0; + } + else if (SetslistBox.Items.Count > 0 && index > -1 && + index < SetslistBox.Items.Count) + { + SetslistBox.SelectedIndex = index; + } + } + // + private void ButtonAddStorage_Click(object sender, EventArgs e) + { + string storname = setAddBox.Text; + if (string.IsNullOrEmpty(setAddBox.Text) || _storage.Keys.Contains(storname)) + { + MessageBox.Show("Не все данные заполнены", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(setAddBox.Text); + ReloadObjects(); + } + + private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = + _storage[SetslistBox.SelectedItem?.ToString() ?? string.Empty]?.ShowCats(); + } + + private void ButtonDeleteStorage_Click(object sender, EventArgs e) + { + if (SetslistBox.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Удалить набор { SetslistBox.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) +{ + _storage.DelSet(SetslistBox.SelectedItem.ToString() + ?? string.Empty); + ReloadObjects(); + } + } + + private void ButtonAddCatamaran_Click(object sender, EventArgs e) + { + if (SetslistBox.SelectedIndex == -1) + { + return; + } + var obj = _storage[SetslistBox.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } FormCatamaran form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_cats + form.SelectedCatamaran != null) + if (obj + form.SelectedCatamaran) { MessageBox.Show("Объект добавлен"); - pictureBoxCollection.Image = _cats.ShowCats(); + pictureBoxCollection.Image = obj.ShowCats(); } else { @@ -38,29 +100,49 @@ namespace Catamaran } } } - - private void ButtonRemoveCat_Click(object sender, EventArgs e) + + private void ButtonRemoveCatamaran_Click(object sender, EventArgs e) { + if (SetslistBox.SelectedIndex == -1) + { + return; + } + var obj = _storage[SetslistBox.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBox.Text); - if (_cats - pos != null) + if (obj - pos != null) { MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = _cats.ShowCats(); + pictureBoxCollection.Image = obj.ShowCats(); } else { MessageBox.Show("Не удалось удалить объект"); } } - private void ButtonRefreshCollection_Click(object sender, EventArgs e) { - pictureBoxCollection.Image = _cats.ShowCats(); + if (SetslistBox.SelectedIndex == -1) + { + return; + } + var obj = _storage[SetslistBox.SelectedItem.ToString() ?? + string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowCats(); } + } } diff --git a/Catamaran/SetGeneric.cs b/Catamaran/SetGeneric.cs index c5782fc..4a38291 100644 --- a/Catamaran/SetGeneric.cs +++ b/Catamaran/SetGeneric.cs @@ -8,39 +8,31 @@ namespace Catamaran { internal class SetGeneric where T : class { - - private readonly T?[] _places; - - public int Count => _places.Length; - + private readonly List _places; + + public int Count => _places.Count; + + private readonly int _maxCount; public SetGeneric(int count) { - _places = new T?[count]; + _maxCount = count; + _places = new List(count); } - public int Insert(T catamaran) + public bool Insert(T catamaran) { - if (_places[Count-1] != null) - return -1; - return Insert(catamaran, 0); + if (_places.Count == _maxCount) + return false; + Insert(catamaran, 0); + return true; } - public int Insert(T catamaran, int position) + public bool 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; + if (!(position >= 0 && position <= Count && _places.Count < _maxCount)) + return false; + _places.Insert(position, catamaran); + return true; } public bool Remove(int position) @@ -50,12 +42,32 @@ namespace Catamaran _places[position] = null; return true; } - - public T? Get(int position) + public T? this[int position] { - if (!(position >= 0 && position < Count)) - return null; - return _places[position]; + get + { + if (!(position >= 0 && position < Count)) + return null; + return _places[position]; + } + set + { + if (!(position >= 0 && position < Count && _places.Count < _maxCount)) + return; + _places.Insert(position, value); + return; + } + } + public IEnumerable GetCatamarans(int? maxMonorails = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxMonorails.HasValue && i == maxMonorails.Value) + { + yield break; + } + } } } }