mirror of
https://github.com/kwsch/PKHeX
synced 2024-11-26 14:00:21 +00:00
readonly ComboItem
create once, never modified after
This commit is contained in:
parent
2b8ef9236a
commit
de774ed131
15 changed files with 89 additions and 64 deletions
|
@ -1,14 +1,38 @@
|
|||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace PKHeX.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Key Value pair for a displayed <see cref="string"/> and underlying <see cref="int"/> value.
|
||||
/// Key Value pair for a displayed <see cref="T:System.String" /> and underlying <see cref="T:System.Int32" /> value.
|
||||
/// </summary>
|
||||
public struct ComboItem
|
||||
public readonly struct ComboItem : IEquatable<int>
|
||||
{
|
||||
public string Text { get; set; }
|
||||
public int Value { get; set; }
|
||||
public ComboItem(string text, int value)
|
||||
{
|
||||
Text = text;
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public string Text { get; }
|
||||
public int Value { get; }
|
||||
|
||||
public bool Equals(ComboItem other) => Value == other.Value && string.Equals(Text, other.Text);
|
||||
public bool Equals(int other) => Value == other;
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is null) return false;
|
||||
return obj is ComboItem other && Equals(other);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
return ((Text?.GetHashCode() ?? 0) * 397) ^ Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class ComboItemExtensions
|
||||
|
|
|
@ -575,11 +575,8 @@ namespace PKHeX.Core
|
|||
public IReadOnlyList<ComboItem> GetAbilityDataSource(IEnumerable<int> abils)
|
||||
{
|
||||
return abils.Select(GetItem).ToList();
|
||||
ComboItem GetItem(int ability, int index) => new ComboItem
|
||||
{
|
||||
Value = ability,
|
||||
Text = abilitylist[ability] + abilIdentifier[index]
|
||||
};
|
||||
ComboItem GetItem(int ability, int index)
|
||||
=> new ComboItem(abilitylist[ability] + abilIdentifier[index], ability);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -81,7 +81,11 @@ namespace PKHeX.Core
|
|||
|
||||
var result = new ComboItem[values.Length];
|
||||
for (int i = 0; i < result.Length; i++)
|
||||
result[i] = new ComboItem {Text = Util.ToTitleCase(names[i].Replace('_', ' ')), Value = values[i]};
|
||||
{
|
||||
var replaced = names[i].Replace('_', ' ');
|
||||
var titled = Util.ToTitleCase(replaced);
|
||||
result[i] = new ComboItem(titled, values[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -257,7 +257,7 @@ namespace PKHeX.Core
|
|||
|
||||
var val = line.Substring(0, zeroth);
|
||||
var text = GetNthEntry(line, index, zeroth);
|
||||
var item = new ComboItem {Text = text, Value = Convert.ToInt32(val)};
|
||||
var item = new ComboItem(text, Convert.ToInt32(val));
|
||||
arr.Add(item);
|
||||
}
|
||||
return arr;
|
||||
|
@ -288,7 +288,7 @@ namespace PKHeX.Core
|
|||
{
|
||||
var list = new List<ComboItem>(inStrings.Count);
|
||||
for (int i = 0; i < inStrings.Count; i++)
|
||||
list.Add(new ComboItem {Text = inStrings[i], Value = i});
|
||||
list.Add(new ComboItem(inStrings[i], i));
|
||||
list.Sort(Comparer);
|
||||
return list;
|
||||
}
|
||||
|
@ -311,7 +311,7 @@ namespace PKHeX.Core
|
|||
|
||||
public static void AddCBWithOffset(List<ComboItem> list, IReadOnlyList<string> inStrings, int offset, int index)
|
||||
{
|
||||
var item = new ComboItem {Text = inStrings[index - offset], Value = index};
|
||||
var item = new ComboItem(inStrings[index - offset], index);
|
||||
list.Add(item);
|
||||
}
|
||||
|
||||
|
@ -321,7 +321,7 @@ namespace PKHeX.Core
|
|||
for (int i = 0; i < allowed.Length; i++)
|
||||
{
|
||||
int index = allowed[i];
|
||||
var item = new ComboItem {Text = inStrings[index - offset], Value = index};
|
||||
var item = new ComboItem(inStrings[index - offset], index);
|
||||
cbList.Add(item);
|
||||
}
|
||||
cbList.Sort(beginCount, allowed.Length, Comparer);
|
||||
|
@ -333,7 +333,7 @@ namespace PKHeX.Core
|
|||
for (int i = 0; i < allowed.Length; i++)
|
||||
{
|
||||
int index = allowed[i];
|
||||
var item = new ComboItem {Text = inStrings[index], Value = index};
|
||||
var item = new ComboItem(inStrings[index], index);
|
||||
cbList.Add(item);
|
||||
}
|
||||
cbList.Sort(beginCount, allowed.Length, Comparer);
|
||||
|
@ -344,9 +344,9 @@ namespace PKHeX.Core
|
|||
const int forcedTop = 3; // 3 Balls are preferentially first
|
||||
var list = new List<ComboItem>(forcedTop + stringNum.Length)
|
||||
{
|
||||
new ComboItem {Text = inStrings[4], Value = (int)Ball.Poke},
|
||||
new ComboItem {Text = inStrings[3], Value = (int)Ball.Great},
|
||||
new ComboItem {Text = inStrings[2], Value = (int)Ball.Ultra},
|
||||
new ComboItem(inStrings[4], (int)Ball.Poke),
|
||||
new ComboItem(inStrings[3], (int)Ball.Great),
|
||||
new ComboItem(inStrings[2], (int)Ball.Ultra),
|
||||
};
|
||||
|
||||
for (int i = 0; i < stringNum.Length; i++)
|
||||
|
@ -354,7 +354,7 @@ namespace PKHeX.Core
|
|||
int index = stringNum[i];
|
||||
var val = stringVal[i];
|
||||
var txt = inStrings[index];
|
||||
list.Add(new ComboItem {Text = txt, Value = val});
|
||||
list.Add(new ComboItem(txt, val));
|
||||
}
|
||||
|
||||
list.Sort(forcedTop, stringNum.Length, Comparer);
|
||||
|
@ -363,7 +363,7 @@ namespace PKHeX.Core
|
|||
}
|
||||
|
||||
private static readonly FunctorComparer<ComboItem> Comparer =
|
||||
new FunctorComparer<ComboItem>((a, b) => string.Compare(a.Text, b.Text, StringComparison.Ordinal));
|
||||
new FunctorComparer<ComboItem>((a, b) => string.CompareOrdinal(a.Text, b.Text));
|
||||
|
||||
private sealed class FunctorComparer<T> : IComparer<T>
|
||||
{
|
||||
|
|
|
@ -1049,7 +1049,7 @@ namespace PKHeX.WinForms.Controls
|
|||
if (sav is SAV4BR br)
|
||||
{
|
||||
L_SaveSlot.Visible = CB_SaveSlot.Visible = true;
|
||||
var list = br.SaveNames.Select((z, i) => new ComboItem {Text = z, Value = i}).ToList();
|
||||
var list = br.SaveNames.Select((z, i) => new ComboItem(z, i)).ToList();
|
||||
CB_SaveSlot.InitializeBinding();
|
||||
CB_SaveSlot.DataSource = new BindingSource(list, null);
|
||||
CB_SaveSlot.SelectedValue = br.CurrentSlot;
|
||||
|
|
|
@ -672,9 +672,9 @@ namespace PKHeX.WinForms
|
|||
return MC.SelectedGameVersion;
|
||||
|
||||
var games = new List<ComboItem>();
|
||||
if (MC.HasCOLO) games.Add(new ComboItem { Text = MsgGameColosseum, Value = (int)GameVersion.COLO });
|
||||
if (MC.HasXD) games.Add(new ComboItem { Text = MsgGameXD, Value = (int)GameVersion.XD });
|
||||
if (MC.HasRSBOX) games.Add(new ComboItem { Text = MsgGameRSBOX, Value = (int)GameVersion.RSBOX });
|
||||
if (MC.HasCOLO) games.Add(new ComboItem(MsgGameColosseum, (int)GameVersion.COLO));
|
||||
if (MC.HasXD) games.Add(new ComboItem(MsgGameXD, (int)GameVersion.XD));
|
||||
if (MC.HasRSBOX) games.Add(new ComboItem(MsgGameRSBOX, (int)GameVersion.RSBOX));
|
||||
|
||||
var dialog = new SAV_GameSelect(games, MsgFileLoadSaveMultiple, MsgFileLoadSaveSelectGame);
|
||||
dialog.ShowDialog();
|
||||
|
|
|
@ -245,7 +245,7 @@ namespace PKHeX.WinForms
|
|||
CB_GameOrigin.InitializeBinding();
|
||||
CB_HPType.InitializeBinding();
|
||||
|
||||
var Any = new ComboItem {Text = MsgAny, Value = -1};
|
||||
var Any = new ComboItem(MsgAny, -1);
|
||||
|
||||
var DS_Species = new List<ComboItem>(GameInfo.SpeciesDataSource);
|
||||
DS_Species.RemoveAt(0); DS_Species.Insert(0, Any); CB_Species.DataSource = DS_Species;
|
||||
|
|
|
@ -154,7 +154,7 @@ namespace PKHeX.WinForms
|
|||
CB_Species.InitializeBinding();
|
||||
CB_GameOrigin.InitializeBinding();
|
||||
|
||||
var Any = new ComboItem {Text = MsgAny, Value = -1};
|
||||
var Any = new ComboItem(MsgAny, -1);
|
||||
|
||||
var DS_Species = new List<ComboItem>(GameInfo.SpeciesDataSource);
|
||||
DS_Species.RemoveAt(0); DS_Species.Insert(0, Any); CB_Species.DataSource = DS_Species;
|
||||
|
|
|
@ -166,7 +166,7 @@ namespace PKHeX.WinForms
|
|||
CB_HeldItem.InitializeBinding();
|
||||
CB_Species.InitializeBinding();
|
||||
|
||||
var Any = new ComboItem {Text = MsgAny, Value = -1};
|
||||
var Any = new ComboItem(MsgAny, -1);
|
||||
|
||||
var DS_Species = new List<ComboItem>(GameInfo.SpeciesDataSource);
|
||||
DS_Species.RemoveAt(0);
|
||||
|
|
|
@ -48,10 +48,9 @@ namespace PKHeX.WinForms
|
|||
346,347,341,342,343,373,374,375,381,325,395,396,397,398,399,400,
|
||||
401,402,403,407,408,404,405,406,409,410
|
||||
};
|
||||
var speciesList = GameInfo.SpeciesDataSource.Where(v => v.Value <= SAV.MaxSpeciesID).Select(v => new ComboItem {
|
||||
Text = v.Text,
|
||||
Value = v.Value < 252 ? v.Value : HoennListMixed[v.Value - 252],
|
||||
}).ToList();
|
||||
var speciesList = GameInfo.SpeciesDataSource.Where(v => v.Value <= SAV.MaxSpeciesID).Select(v =>
|
||||
new ComboItem (v.Text, v.Value < 252 ? v.Value : HoennListMixed[v.Value - 252])
|
||||
).ToList();
|
||||
int ofsTCM = SAV.GetBlockOffset(2) + 0x106;
|
||||
for (int i = 0; i < cba.Length; i++)
|
||||
{
|
||||
|
|
|
@ -116,10 +116,10 @@ namespace PKHeX.WinForms
|
|||
// Roamer
|
||||
cbr = new[] { CB_Roamer642, CB_Roamer641 };
|
||||
List<ComboItem> getStates() => new List<ComboItem> {
|
||||
new ComboItem { Text = "Not roamed", Value = 0 },
|
||||
new ComboItem { Text = "Roaming", Value = 1 },
|
||||
new ComboItem { Text = "Defeated", Value = 2 },
|
||||
new ComboItem { Text = "Captured", Value = 3 }
|
||||
new ComboItem("Not roamed", 0),
|
||||
new ComboItem("Roaming", 1),
|
||||
new ComboItem("Defeated", 2),
|
||||
new ComboItem("Captured", 3),
|
||||
};
|
||||
// CurrentStat:ComboboxSource
|
||||
// Not roamed: Not roamed/Defeated/Captured
|
||||
|
@ -133,7 +133,7 @@ namespace PKHeX.WinForms
|
|||
|
||||
var states = getStates();
|
||||
if (states.All(z => z.Value != c))
|
||||
states.Add(new ComboItem {Text = $"Unknown (0x{c:X2})", Value = c});
|
||||
states.Add(new ComboItem($"Unknown (0x{c:X2})", c));
|
||||
cbr[i].Items.Clear();
|
||||
cbr[i].InitializeBinding();
|
||||
cbr[i].DataSource = new BindingSource(states.Where(v => v.Value >= 2 || v.Value == c).ToList(), null);
|
||||
|
@ -327,7 +327,7 @@ namespace PKHeX.WinForms
|
|||
55, 56, 57, 62,
|
||||
39, 40, 41, 42, 43, 44, 45, 59, 61, 63
|
||||
};
|
||||
ComboItem[] PassPowerB = PassPowerA.Zip(PassPowerC, (f, s) => new ComboItem { Text = f, Value = s }).ToArray();
|
||||
ComboItem[] PassPowerB = PassPowerA.Zip(PassPowerC, (f, s) => new ComboItem(f, s)).ToArray();
|
||||
cba = new[] { CB_PassPower1, CB_PassPower2, CB_PassPower3 };
|
||||
for (int i = 0; i < cba.Length; i++)
|
||||
{
|
||||
|
@ -557,7 +557,7 @@ namespace PKHeX.WinForms
|
|||
CHK_Area9.Checked = Forest.Unlock9thArea;
|
||||
|
||||
var areas = AllSlots.Select(z => z.Area).Distinct()
|
||||
.Select(z => new ComboItem {Text = z.ToString(), Value = (int) z}).ToList();
|
||||
.Select(z => new ComboItem(z.ToString(), (int) z)).ToList();
|
||||
|
||||
CB_Species.InitializeBinding();
|
||||
CB_Move.InitializeBinding();
|
||||
|
@ -680,13 +680,14 @@ namespace PKHeX.WinForms
|
|||
var list = new List<ComboItem>();
|
||||
if (pi.Genderless)
|
||||
{
|
||||
list.Add(new ComboItem{Text = "Genderless", Value = 2});
|
||||
list.Add(new ComboItem("Genderless", 2));
|
||||
return list;
|
||||
}
|
||||
|
||||
if (!pi.OnlyFemale)
|
||||
list.Add(new ComboItem { Text = "Male", Value = 0 });
|
||||
list.Add(new ComboItem("Male", 0));
|
||||
if (!pi.OnlyMale)
|
||||
list.Add(new ComboItem { Text = "Female", Value = 1 });
|
||||
list.Add(new ComboItem("Female", 1));
|
||||
return list;
|
||||
}
|
||||
|
||||
|
|
|
@ -87,7 +87,7 @@ namespace PKHeX.WinForms
|
|||
|
||||
var names = Enum.GetNames(typeof(TrainerSprite6));
|
||||
var values = (int[])Enum.GetValues(typeof(TrainerSprite6));
|
||||
var data = names.Zip(values, (a, b) => new ComboItem {Text = a, Value = b})
|
||||
var data = names.Zip(values, (a, b) => new ComboItem(a, b))
|
||||
.Where(z => z.Value >= 2) // ignore Calem & Serena (no sprite)
|
||||
.ToList();
|
||||
|
||||
|
|
|
@ -90,9 +90,9 @@ namespace PKHeX.WinForms
|
|||
{
|
||||
var alolatime_list = new ComboItem[24];
|
||||
for (int i = 1; i < alolatime_list.Length; i++)
|
||||
alolatime_list[i] = new ComboItem {Text = $"+{i:00} Hours", Value = i * 60 * 60};
|
||||
alolatime_list[0] = new ComboItem {Text = "Sun Time", Value = 24 * 60 * 60};
|
||||
alolatime_list[12] = new ComboItem {Text = "Moon Time", Value = 12 * 60 * 60};
|
||||
alolatime_list[i] = new ComboItem($"+{i:00} Hours", i * 60 * 60);
|
||||
alolatime_list[0] = new ComboItem("Sun Time", 24 * 60 * 60);
|
||||
alolatime_list[12] = new ComboItem("Moon Time", 12 * 60 * 60);
|
||||
return alolatime_list;
|
||||
}
|
||||
|
||||
|
|
|
@ -134,7 +134,7 @@ namespace PKHeX.WinForms
|
|||
DropDownWidth = Width + 100
|
||||
};
|
||||
cb.InitializeBinding();
|
||||
cb.DataSource = new BindingSource(f.Options.Select(z => new ComboItem{Text = z.Text, Value = z.Value}).ToList(), null);
|
||||
cb.DataSource = new BindingSource(f.Options.Select(z => new ComboItem(z.Text, z.Value)).ToList(), null);
|
||||
cb.SelectedValue = f.Value;
|
||||
if (cb.SelectedIndex < 0)
|
||||
cb.SelectedIndex = 0;
|
||||
|
|
|
@ -118,30 +118,30 @@ namespace PKHeX.WinForms
|
|||
CB_AuthorVersion.InitializeBinding();
|
||||
CB_AuthorVersion.DataSource = new BindingSource(Gen == 4
|
||||
? new[] {
|
||||
new ComboItem { Text = "Diamond", Value = (int)GameVersion.D },
|
||||
new ComboItem { Text = "Pearl", Value = (int)GameVersion.P },
|
||||
new ComboItem { Text = "Platinum", Value = (int)GameVersion.Pt },
|
||||
new ComboItem { Text = "HeartGold", Value = (int)GameVersion.HG },
|
||||
new ComboItem { Text = "SoulSilver", Value = (int)GameVersion.SS },
|
||||
new ComboItem("Diamond", (int)GameVersion.D),
|
||||
new ComboItem("Pearl", (int)GameVersion.P),
|
||||
new ComboItem("Platinum", (int)GameVersion.Pt),
|
||||
new ComboItem("HeartGold", (int)GameVersion.HG),
|
||||
new ComboItem("SoulSilver", (int)GameVersion.SS),
|
||||
}.ToList()
|
||||
: new[] {
|
||||
new ComboItem { Text = "Black", Value = (int)GameVersion.B },
|
||||
new ComboItem { Text = "White", Value = (int)GameVersion.W },
|
||||
new ComboItem { Text = "Black2", Value = (int)GameVersion.B2 },
|
||||
new ComboItem { Text = "White2", Value = (int)GameVersion.W2 },
|
||||
new ComboItem("Black", (int)GameVersion.B),
|
||||
new ComboItem("White", (int)GameVersion.W),
|
||||
new ComboItem("Black2", (int)GameVersion.B2),
|
||||
new ComboItem("White2", (int)GameVersion.W2),
|
||||
}.ToList(), null);
|
||||
|
||||
CB_AuthorLang.Items.Clear();
|
||||
CB_AuthorLang.InitializeBinding();
|
||||
CB_AuthorLang.DataSource = new BindingSource(new[] {
|
||||
// not sure
|
||||
new ComboItem { Text = "JPN", Value = 1 },
|
||||
new ComboItem { Text = "ENG", Value = 2 },
|
||||
new ComboItem { Text = "FRE", Value = 3 },
|
||||
new ComboItem { Text = "ITA", Value = 4 },
|
||||
new ComboItem { Text = "GER", Value = 5 },
|
||||
new ComboItem { Text = "ESP", Value = 7 },
|
||||
new ComboItem { Text = "KOR", Value = 8 },
|
||||
new ComboItem("JPN", 1),
|
||||
new ComboItem("ENG", 2),
|
||||
new ComboItem("FRE", 3),
|
||||
new ComboItem("ITA", 4),
|
||||
new ComboItem("GER", 5),
|
||||
new ComboItem("ESP", 7),
|
||||
new ComboItem("KOR", 8),
|
||||
}.ToList(), null);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue