diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs index 4edb544..5d47a03 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.Designer.cs @@ -30,12 +30,18 @@ { pictureBoxCollection = new PictureBox(); Tools = new GroupBox(); + Sets = new GroupBox(); + listBoxStorage = new ListBox(); + DeleteSetButton = new Button(); + AddSetButton = new Button(); + textBoxStorageName = new TextBox(); maskedTextBoxNumber = new TextBox(); buttonRefresh = new Button(); buttonDeleteShip = new Button(); buttonAddShip = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).BeginInit(); Tools.SuspendLayout(); + Sets.SuspendLayout(); SuspendLayout(); // // pictureBoxCollection @@ -48,6 +54,7 @@ // // Tools // + Tools.Controls.Add(Sets); Tools.Controls.Add(maskedTextBoxNumber); Tools.Controls.Add(buttonRefresh); Tools.Controls.Add(buttonDeleteShip); @@ -59,16 +66,66 @@ Tools.TabStop = false; Tools.Text = "Tools"; // + // Sets + // + Sets.Controls.Add(listBoxStorage); + Sets.Controls.Add(DeleteSetButton); + Sets.Controls.Add(AddSetButton); + Sets.Controls.Add(textBoxStorageName); + Sets.Location = new Point(10, 20); + Sets.Name = "Sets"; + Sets.Size = new Size(165, 235); + Sets.TabIndex = 5; + Sets.TabStop = false; + Sets.Text = "Sets"; + // + // listBoxStorage + // + listBoxStorage.FormattingEnabled = true; + listBoxStorage.ItemHeight = 15; + listBoxStorage.Location = new Point(5, 85); + listBoxStorage.Name = "listBoxStorage"; + listBoxStorage.Size = new Size(154, 109); + listBoxStorage.TabIndex = 7; + listBoxStorage.SelectedIndexChanged += ListBoxObjects_SelectedIndexChanged; + // + // DeleteSetButton + // + DeleteSetButton.Location = new Point(5, 200); + DeleteSetButton.Name = "DeleteSetButton"; + DeleteSetButton.Size = new Size(155, 30); + DeleteSetButton.TabIndex = 6; + DeleteSetButton.Text = "Delete Set"; + DeleteSetButton.UseVisualStyleBackColor = true; + DeleteSetButton.Click += ButtonDeleteObjectClick; + // + // AddSetButton + // + AddSetButton.Location = new Point(6, 51); + AddSetButton.Name = "AddSetButton"; + AddSetButton.Size = new Size(155, 30); + AddSetButton.TabIndex = 5; + AddSetButton.Text = "Add Set"; + AddSetButton.UseVisualStyleBackColor = true; + AddSetButton.Click += ButtonAddObject_Click; + // + // textBoxStorageName + // + textBoxStorageName.Location = new Point(5, 20); + textBoxStorageName.Name = "textBoxStorageName"; + textBoxStorageName.Size = new Size(155, 23); + textBoxStorageName.TabIndex = 5; + // // maskedTextBoxNumber // - maskedTextBoxNumber.Location = new Point(10, 120); + maskedTextBoxNumber.Location = new Point(10, 320); maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(165, 23); maskedTextBoxNumber.TabIndex = 4; // // buttonRefresh // - buttonRefresh.Location = new Point(10, 205); + buttonRefresh.Location = new Point(10, 405); buttonRefresh.Name = "buttonRefresh"; buttonRefresh.Size = new Size(165, 50); buttonRefresh.TabIndex = 2; @@ -78,7 +135,7 @@ // // buttonDeleteShip // - buttonDeleteShip.Location = new Point(10, 150); + buttonDeleteShip.Location = new Point(10, 350); buttonDeleteShip.Name = "buttonDeleteShip"; buttonDeleteShip.Size = new Size(165, 50); buttonDeleteShip.TabIndex = 1; @@ -88,7 +145,7 @@ // // buttonAddShip // - buttonAddShip.Location = new Point(10, 20); + buttonAddShip.Location = new Point(10, 260); buttonAddShip.Name = "buttonAddShip"; buttonAddShip.Size = new Size(165, 50); buttonAddShip.TabIndex = 0; @@ -108,6 +165,8 @@ ((System.ComponentModel.ISupportInitialize)pictureBoxCollection).EndInit(); Tools.ResumeLayout(false); Tools.PerformLayout(); + Sets.ResumeLayout(false); + Sets.PerformLayout(); ResumeLayout(false); } @@ -119,5 +178,10 @@ private Button buttonRefresh; private Button buttonDeleteShip; private Button buttonAddShip; + private GroupBox Sets; + private ListBox listBoxStorage; + private Button DeleteSetButton; + private Button AddSetButton; + private TextBox textBoxStorageName; } } \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs index b4284e3..22bb76f 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs @@ -18,22 +18,78 @@ namespace ProjectWarmlyShip { public partial class FormShipCollection : Form { - private readonly ShipsGenericCollection _ships; + private readonly ShipsGenericStorage _storage; public FormShipCollection() { InitializeComponent(); - _ships = new ShipsGenericCollection - (pictureBoxCollection.Width, pictureBoxCollection.Height); + _storage = new ShipsGenericStorage(pictureBoxCollection.Width, + pictureBoxCollection.Height); + } + private void ReloadObjects() + { + int index = listBoxStorage.SelectedIndex; + + listBoxStorage.Items.Clear(); + for (int i = 0; i < _storage.Keys.Count; i++) + { + listBoxStorage.Items.Add(_storage.Keys[i]); + } + + if (listBoxStorage.Items.Count > 0 && (index == -1 || index >= listBoxStorage.Items.Count)) + { + listBoxStorage.SelectedIndex = 0; + } + else if (listBoxStorage.Items.Count > 0 && index > -1 && index < listBoxStorage.Items.Count) + { + listBoxStorage.SelectedIndex = index; + } + } + private void ButtonAddObject_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxStorageName.Text)) + { + MessageBox.Show("Input not complete", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _storage.AddSet(textBoxStorageName.Text); + ReloadObjects(); + } + private void ListBoxObjects_SelectedIndexChanged(object sender, EventArgs e) + { + pictureBoxCollection.Image = + _storage[listBoxStorage.SelectedItem?.ToString() ?? string.Empty]?.ShowShips(); + } + private void ButtonDeleteObjectClick(object sender, EventArgs e) + { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + if (MessageBox.Show($"Delete Object {listBoxStorage.SelectedItem}?", "Deleting", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + _storage.DelSet(listBoxStorage.SelectedItem.ToString() ?? string.Empty); + ReloadObjects(); + } } private void ButtonAddShip_Click(object sender, EventArgs e) { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } WarmlyShipForm form = new(); if (form.ShowDialog() == DialogResult.OK) { - if (_ships + form.SelectedShip != -1) + if (obj + form.SelectedShip) { MessageBox.Show("Object Inserted"); - pictureBoxCollection.Image = _ships.ShowShips(); + pictureBoxCollection.Image = obj.ShowShips(); } else { @@ -43,16 +99,25 @@ namespace ProjectWarmlyShip } private void ButtonRemoveShip_Click(Object sender, EventArgs e) { + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } if (MessageBox.Show("Delete Object?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (_ships - pos != false) + if (obj - pos != null) { MessageBox.Show("Object Deleted"); - pictureBoxCollection.Image = _ships.ShowShips(); + pictureBoxCollection.Image = obj.ShowShips(); } else { @@ -61,7 +126,16 @@ namespace ProjectWarmlyShip } private void ButtonRefreshCollection(object sender, EventArgs e) { - pictureBoxCollection.Image = _ships.ShowShips(); + if (listBoxStorage.SelectedIndex == -1) + { + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + pictureBoxCollection.Image = obj.ShowShips(); } } } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs b/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs index cb98c71..5f3b0b8 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/SetGeneric.cs @@ -8,86 +8,65 @@ namespace ProjectWarmlyShip.Generics { 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 ship) + public bool Insert(T ship) { - if (_places[0] == null) - { - _places[0] = ship; - } - else - { - int nullIndex = 0; - for (int i = 0; i < _places.Length; i++) - { - if (_places[i] == null) - { - nullIndex = i; - break; - } - } - if (nullIndex == 0) - { - return -1; - } - for (int i = nullIndex; i > 0; i--) - { - _places[i] = _places[i - 1]; - } - _places[0] = ship; - } - return 0; + return Insert(ship, 0); } - public int Insert(T ship, int position) + public bool Insert(T ship, int position) { - if (position >= Count) - { - return -1; - } - if (_places[position] == null) - { - _places[position] = ship; - } - else - { - int nullIndex = position; - for (int i = position; i < _places.Length; i++) - { - if (_places[i] == null) - { - nullIndex = i; - break; - } - } - if (nullIndex == position) - { - return -1; - } - for (int i = nullIndex; i > position; i--) - { - _places[i] = _places[i - 1]; - } - _places[position] = ship; - } - return position; - } - public bool Remove(int position) - { - if (position >= Count) + if (position < 0 || position > _maxCount || _places.Count >= _maxCount) { return false; } - _places[position] = null; + _places.Insert(position, ship); return true; } - public T? Get(int position) + public bool Remove(int position) { - return _places[position]; + if (position >= _places.Count || position < 0) + { + return false; + } + _places.RemoveAt(position); + return true; + } + public T? this[int position] + { + get + { + if (position < 0 || position >= _places.Count) + { + return null; + } + return _places[position]; + } + set + { + if (position < 0 || position >= _places.Count) + { + return; + } + _places[position] = value; + } + } + public IEnumerable GetShips(int? maxShips = null) + { + for (int i = 0; i < _places.Count; ++i) + { + yield return _places[i]; + if (maxShips.HasValue && i == maxShips.Value) + { + yield break; + } + } } } } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs index d1d9b7f..ed0fcdd 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericCollection.cs @@ -25,27 +25,26 @@ namespace ProjectWarmlyShip.Generics _pictureHeight = picHeight; _collection = new SetGeneric(width * height); } - public static int? operator +(ShipsGenericCollection collect, T? obj) + public static bool operator +(ShipsGenericCollection collect, T? obj) { if (obj == null) { - return -1; + return false; } - return collect?._collection.Insert(obj); + return (bool)collect?._collection.Insert(obj); } - public static bool operator -(ShipsGenericCollection collect, int pos) + public static T? operator -(ShipsGenericCollection collect, int pos) { - T? obj = collect._collection.Get(pos); + T? obj = collect._collection[pos]; if (obj != null) { collect._collection.Remove(pos); - return true; } - return false; + return obj; } public U? GetU(int pos) { - return (U?)_collection.Get(pos)?.GetMoveableObject; + return (U?)_collection[pos]?.GetMoveableObject; } public Bitmap ShowShips() { @@ -75,14 +74,15 @@ namespace ProjectWarmlyShip.Generics { int width = _pictureWidth / _placeSizeWidth; int height = _pictureHeight / _placeSizeHeight; - for (int i = 0; i < _collection.Count; i++) + int i = 0; + foreach (var ship in _collection.GetShips()) { - DrawingShip? ship = _collection.Get(i); if (ship != null) { ship.SetPosition((width - (i % width) - 1) * _placeSizeWidth, (i / height) * _placeSizeHeight + 15); ship.DrawTrasport(g); } + i++; } } } diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs new file mode 100644 index 0000000..992d1cd --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/ShipsGenericStorage.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectWarmlyShip.MovementStrategy; +using ProjectWarmlyShip.DrawingObjects; +using ProjectWarmlyShip.Generics; + +namespace ProjectWarmlyShip.Generics +{ + internal class ShipsGenericStorage + { + readonly Dictionary> _shipStorages; + public List Keys => _shipStorages.Keys.ToList(); + private readonly int _pictuteWidth; + private readonly int _pictuteHeight; + public ShipsGenericStorage(int pictureWidth, int pictureHeight) + { + _shipStorages = new Dictionary>(); + _pictuteWidth = pictureWidth; + _pictuteHeight = pictureHeight; + } + public void AddSet(string name) + { + if (_shipStorages.ContainsKey(name)) + { + return; + } + _shipStorages[name] = new ShipsGenericCollection(_pictuteWidth, _pictuteHeight); + } + public void DelSet(string name) + { + if (_shipStorages.ContainsKey(name)) + { + _shipStorages.Remove(name); + } + } + public ShipsGenericCollection? this[string ind] + { + get + { + if (_shipStorages.ContainsKey(ind)) + { + return _shipStorages[ind]; + } + return null; + } + } + } +}