From e46aed4b0b5830ba724dac8a68d189f9b33a8ddd Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 7 Jul 2018 15:37:47 -0700 Subject: [PATCH] Relocate bean logic to core --- PKHeX.Core/Saves/Substructures/BeanPouch.cs | 62 +++++++++++ .../Save Editors/Gen7/SAV_Pokebean.cs | 104 ++++++++---------- 2 files changed, 108 insertions(+), 58 deletions(-) create mode 100644 PKHeX.Core/Saves/Substructures/BeanPouch.cs diff --git a/PKHeX.Core/Saves/Substructures/BeanPouch.cs b/PKHeX.Core/Saves/Substructures/BeanPouch.cs new file mode 100644 index 000000000..c637bc884 --- /dev/null +++ b/PKHeX.Core/Saves/Substructures/BeanPouch.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace PKHeX.Core +{ + public class BeanPouch + { + public const int Count = 15; + public static readonly string[] BeanIndexNames = GetBeanList(); + + public static string[] GetBeanList() + { + var colors = Enum.GetNames(typeof(BeanColor)); + var beans = new List(); + // 7 regular, 7 patterned, one rainbow + beans.AddRange(colors.Select(z => $"{z} Bean")); + beans.AddRange(colors.Select(z => $"{z} Patterned Bean")); + beans.Add("Rainbow Bean"); + return beans.ToArray(); + } + + private enum BeanColor + { + Red, + Blue, + LightBlue, + Green, + Yellow, + Purple, + Orange, + } + + private readonly SAV7 SAV; + public BeanPouch(SAV7 sav) => SAV = sav; + + public int[] Beans + { + get => GetBeanCounts(); + set => SetBeanCounts(value); + } + private int[] GetBeanCounts() + { + int[] beans = new int[Count]; + for (int i = 0; i < beans.Length; i++) + beans[i] = SAV.GetPokebeanCount(i); + return beans; + } + private void SetBeanCounts(IReadOnlyList beans) + { + if (beans.Count != Count) + return; + for (int i = 0; i < beans.Count; i++) + SAV.SetPokebeanCount(i, beans[i]); + } + public void SetCountAll(int val) + { + for (int i = 0; i < Count; i++) + SAV.SetPokebeanCount(i, val); + } + } +} diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Pokebean.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Pokebean.cs index 4440d316d..638ef3a69 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Pokebean.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Pokebean.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Windows.Forms; using PKHeX.Core; @@ -13,66 +12,53 @@ public SAV_Pokebean(SaveFile sav) WinFormsUtil.TranslateInterface(this, Main.CurrentLanguage); SAV = (SAV7)(Origin = sav).Clone(); - var colors = new[] {"Red", "Blue", "Light Blue", "Green", "Yellow", "Purple", "Orange"}; - var beans = new List(); - foreach (var color in colors) - { - beans.Add($"{color} Bean"); - } - foreach (var color in colors) - { - beans.Add($"{color} Patterned Bean"); - } - beans.Add("Rainbow Bean"); - beanlist = beans.ToArray(); - - Setup(); + InitializeGrid(); + Pouch = new BeanPouch(SAV); + LoadValues(); } - private const int MaxBeanID = 14; + private readonly BeanPouch Pouch; private readonly SaveFile Origin; private readonly SAV7 SAV; - private readonly string[] beanlist; - private void Setup() + private void LoadValues() { dgv.Rows.Clear(); - dgv.Columns.Clear(); - DataGridViewColumn dgvBean = new DataGridViewTextBoxColumn(); + var beans = Pouch.Beans; + dgv.Rows.Add(beans.Length); + for (int i = 0; i < beans.Length; i++) { - dgvBean.HeaderText = "Slot"; - dgvBean.DisplayIndex = 0; - dgvBean.Width = 135; - dgvBean.ReadOnly = true; - dgvBean.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; - } - DataGridViewComboBoxColumn dgvCount = new DataGridViewComboBoxColumn - { - DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing, - DisplayIndex = 0, - Width = 135, - FlatStyle = FlatStyle.Flat, - ValueType = typeof(int) - }; - { - for (var i = 0; i < 256; i++) - dgvCount.Items.Add(i); - - dgvCount.DisplayIndex = 1; - dgvCount.Width = 45; - dgvCount.FlatStyle = FlatStyle.Flat; - } - dgv.Columns.Add(dgvBean); - dgv.Columns.Add(dgvCount); - - dgv.Rows.Add(MaxBeanID + 1); - for (int i = 0; i <= MaxBeanID; i++) - { - dgv.Rows[i].Cells[0].Value = beanlist[i]; - dgv.Rows[i].Cells[1].Value = SAV.GetPokebeanCount(i); + dgv.Rows[i].Cells[0].Value = BeanPouch.BeanIndexNames[i]; + dgv.Rows[i].Cells[1].Value = beans[i]; } } + + private void InitializeGrid() + { + var dgvBean = new DataGridViewTextBoxColumn + { + HeaderText = "Slot", + DisplayIndex = 0, + Width = 135, + ReadOnly = true, + DefaultCellStyle = {Alignment = DataGridViewContentAlignment.MiddleCenter} + }; + var dgvCount = new DataGridViewComboBoxColumn + { + DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing, + FlatStyle = FlatStyle.Flat, + ValueType = typeof(int), + DisplayIndex = 1, + Width = 45 + }; + for (var i = 0; i < 256; i++) + dgvCount.Items.Add(i); + + dgv.Columns.Add(dgvBean); + dgv.Columns.Add(dgvCount); + } + private static void DropClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex != 1) return; @@ -85,21 +71,23 @@ private void B_Cancel_Click(object sender, EventArgs e) } private void B_All_Click(object sender, EventArgs e) { - for (int i = 0; i <= MaxBeanID; i++) - SAV.SetPokebeanCount(i, 255); - Setup(); + Pouch.SetCountAll(255); + LoadValues(); + System.Media.SystemSounds.Asterisk.Play(); } private void B_None_Click(object sender, EventArgs e) { - for (int i = 0; i <= MaxBeanID; i++) - SAV.SetPokebeanCount(i, 0); - Setup(); + Pouch.SetCountAll(0); + LoadValues(); + System.Media.SystemSounds.Asterisk.Play(); } private void B_Save_Click(object sender, EventArgs e) { - for (int i = 0; i <= MaxBeanID; i++) - SAV.SetPokebeanCount(i, (int)dgv.Rows[i].Cells[1].Value); + var beans = Pouch.Beans; + for (int i = 0; i < beans.Length; i++) + beans[i] = (int)dgv.Rows[i].Cells[1].Value; + Pouch.Beans = beans; Origin.SetData(SAV.Data, 0); Close(); }