PKHeX/PKHeX.WinForms/Subforms/Save Editors/SAV_EventFlags2.cs

326 lines
10 KiB
C#
Raw Normal View History

Refactoring: Move Source (Legality) (#3560) Rewrites a good amount of legality APIs pertaining to: * Legal moves that can be learned * Evolution chains & cross-generation paths * Memory validation with forgotten moves In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data. The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space. The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation. * `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game. * `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`). * Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
using System;
2022-04-10 01:12:57 +00:00
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using PKHeX.Core;
using static PKHeX.Core.MessageStrings;
namespace PKHeX.WinForms;
public sealed partial class SAV_EventFlags2 : Form
2022-04-10 01:12:57 +00:00
{
private readonly EventWorkspace<SAV2, byte> Editor;
private readonly Dictionary<int, NumericUpDown> WorkDict = new();
private readonly Dictionary<int, int> FlagDict = new();
2022-04-10 01:12:57 +00:00
private bool editing;
2022-04-10 01:12:57 +00:00
public SAV_EventFlags2(SAV2 sav)
{
InitializeComponent();
WinFormsUtil.TranslateInterface(this, Main.CurrentLanguage);
2022-04-10 01:12:57 +00:00
var editor = Editor = new EventWorkspace<SAV2, byte>(sav);
DragEnter += Main_DragEnter;
DragDrop += Main_DragDrop;
2022-04-10 01:12:57 +00:00
editing = true;
CB_Stats.Items.Clear();
for (int i = 0; i < editor.Values.Length; i++)
CB_Stats.Items.Add(i.ToString());
2022-04-10 01:12:57 +00:00
dgv.SuspendLayout();
TLP_Const.SuspendLayout();
TLP_Const.Scroll += WinFormsUtil.PanelScroll;
TLP_Const.Controls.Clear();
AddFlagList(editor.Labels, editor.Flags);
AddConstList(editor.Labels, editor.Values);
2022-04-10 01:12:57 +00:00
dgv.ResumeLayout();
TLP_Const.ResumeLayout();
2022-04-10 01:12:57 +00:00
Text = $"{Text} ({sav.Version})";
2022-04-10 01:12:57 +00:00
if (CB_Stats.Items.Count > 0)
{
CB_Stats.SelectedIndex = 0;
}
else
{
L_Stats.Visible = CB_Stats.Visible = MT_Stat.Visible = false;
tabControl1.TabPages.Remove(GB_Constants);
2022-04-10 01:12:57 +00:00
}
NUD_Flag.Maximum = editor.Flags.Length - 1;
NUD_Flag.Value = 0;
c_CustomFlag.Checked = editor.Flags[0];
editing = false;
}
2022-04-10 01:12:57 +00:00
private void B_Cancel_Click(object sender, EventArgs e)
{
Close();
}
private void B_Save_Click(object sender, EventArgs e)
{
Editor.Save();
Close();
}
private void AddFlagList(EventLabelCollection list, bool[] values)
{
var labels = list.Flag;
if (labels.Count == 0)
2022-04-10 01:12:57 +00:00
{
dgv.Visible = false;
var research = new Label { Text = MsgResearchRequired, Name = "TLP_Flags_Research", ForeColor = Color.Red, AutoSize = true, Location = new Point(20, 20) };
GB_Flags.Controls.Add(research);
return;
2022-04-10 01:12:57 +00:00
}
var cFlag = new DataGridViewCheckBoxColumn
{
DisplayIndex = 0,
Width = 20,
SortMode = DataGridViewColumnSortMode.NotSortable,
};
var cLabel = new DataGridViewTextBoxColumn
{
DisplayIndex = 1,
AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
ReadOnly = true,
SortMode = DataGridViewColumnSortMode.NotSortable,
};
cFlag.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
cLabel.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
dgv.Columns.Add(cFlag);
dgv.Columns.Add(cLabel);
var hideBelow = Main.Settings.Advanced.HideEventTypeBelow;
labels = labels.Where(z => z.Type >= hideBelow).OrderByDescending(z => z.Type).ToList();
dgv.Rows.Add(labels.Count);
for (int i = 0; i < labels.Count; i++)
FlagDict[labels[i].Index] = i;
for (int i = 0; i < labels.Count; i++)
2022-04-10 01:12:57 +00:00
{
var (name, index, _) = labels[i];
var cells = dgv.Rows[i].Cells;
cells[0].Value = values[index];
cells[1].Value = name;
2022-04-10 01:12:57 +00:00
}
dgv.CellValueChanged += (s, e) =>
{
if (e.ColumnIndex != 0 || e.RowIndex == -1)
return;
2022-04-10 01:12:57 +00:00
bool chk = (bool)dgv.Rows[e.RowIndex].Cells[0].Value;
var index = labels[e.RowIndex].Index;
values[index] = chk;
if (NUD_Flag.Value == index)
c_CustomFlag.Checked = chk;
};
dgv.CellMouseUp += (s, e) =>
2022-04-10 01:12:57 +00:00
{
if (e.RowIndex == -1)
return;
if (e.ColumnIndex == 0)
2022-04-10 01:12:57 +00:00
{
dgv.EndEdit();
2022-04-10 01:12:57 +00:00
return;
}
if (e.ColumnIndex != 1)
return;
bool chk = (bool)dgv.Rows[e.RowIndex].Cells[0].Value;
dgv.Rows[e.RowIndex].Cells[0].Value = !chk;
var index = labels[e.RowIndex].Index;
values[index] = !chk;
if (NUD_Flag.Value == index)
c_CustomFlag.Checked = !chk;
};
}
Refactoring: Move Source (Legality) (#3560) Rewrites a good amount of legality APIs pertaining to: * Legal moves that can be learned * Evolution chains & cross-generation paths * Memory validation with forgotten moves In generation 8, there are 3 separate contexts an entity can exist in: SW/SH, BD/SP, and LA. Not every entity can cross between them, and not every entity from generation 7 can exist in generation 8 (Gogoat, etc). By creating class models representing the restrictions to cross each boundary, we are able to better track and validate data. The old implementation of validating moves was greedy: it would iterate for all generations and evolutions, and build a full list of every move that can be learned, storing it on the heap. Now, we check one game group at a time to see if the entity can learn a move that hasn't yet been validated. End result is an algorithm that requires 0 allocation, and a smaller/quicker search space. The old implementation of storing move parses was inefficient; for each move that was parsed, a new object is created and adjusted depending on the parse. Now, move parse results are `struct` and store the move parse contiguously in memory. End result is faster parsing and 0 memory allocation. * `PersonalTable` objects have been improved with new API methods to check if a species+form can exist in the game. * `IEncounterTemplate` objects have been improved to indicate the `EntityContext` they originate in (similar to `Generation`). * Some APIs have been extended to accept `Span<T>` instead of Array/IEnumerable
2022-08-03 23:15:27 +00:00
private void AddConstList(EventLabelCollection list, ReadOnlySpan<byte> values)
{
var labels = list.Work;
if (labels.Count == 0)
{
TLP_Const.Controls.Add(new Label { Text = MsgResearchRequired, Name = "TLP_Const_Research", ForeColor = Color.Red, AutoSize = true }, 0, 0);
return;
}
var hide = Main.Settings.Advanced.HideEventTypeBelow;
labels = labels.OrderByDescending(z => z.Type).ToList();
for (var i = 0; i < labels.Count; i++)
{
var entry = labels[i];
if (entry.Type < hide)
break;
var lbl = new Label { Text = entry.Name, Margin = Padding.Empty, AutoSize = true };
var mtb = new NumericUpDown
2022-04-10 01:12:57 +00:00
{
Maximum = ushort.MaxValue,
Minimum = ushort.MinValue,
Margin = Padding.Empty,
Width = 50,
2022-04-10 01:12:57 +00:00
};
var map = entry.PredefinedValues.Select(z => new ComboItem(z.Name, z.Value)).ToList();
var cb = new ComboBox
2022-04-10 01:12:57 +00:00
{
Margin = Padding.Empty,
Width = 150,
DropDownStyle = ComboBoxStyle.DropDownList,
BindingContext = BindingContext,
DropDownWidth = Width + 100,
2022-04-10 01:12:57 +00:00
};
cb.InitializeBinding();
cb.DataSource = map;
2022-04-10 01:12:57 +00:00
lbl.Click += (sender, e) => mtb.Value = 0;
bool updating = false;
mtb.ValueChanged += ChangeConstValue;
void ChangeConstValue(object? sender, EventArgs e)
{
if (updating)
return;
2022-04-10 01:12:57 +00:00
updating = true;
var value = (byte)mtb.Value;
var (_, valueID) = map.Find(z => z.Value == value) ?? map[0];
if (WinFormsUtil.GetIndex(cb) != valueID)
cb.SelectedValue = valueID;
2022-04-10 01:12:57 +00:00
Editor.Values[entry.Index] = value;
if (CB_Stats.SelectedIndex == entry.Index)
MT_Stat.Text = ((int)mtb.Value).ToString();
updating = false;
2022-04-10 01:12:57 +00:00
}
cb.SelectedValueChanged += (o, args) =>
2022-04-10 01:12:57 +00:00
{
if (editing || updating)
2022-04-10 01:12:57 +00:00
return;
var value = WinFormsUtil.GetIndex(cb);
mtb.Value = value == NamedEventConst.CustomMagicValue ? 0 : value;
2022-04-10 01:12:57 +00:00
};
mtb.Value = values[entry.Index];
if (mtb.Value == 0)
ChangeConstValue(this, EventArgs.Empty);
2022-04-10 01:12:57 +00:00
TLP_Const.Controls.Add(lbl, 0, i);
TLP_Const.Controls.Add(cb, 1, i);
TLP_Const.Controls.Add(mtb, 2, i);
2022-04-10 01:12:57 +00:00
WorkDict.Add(entry.Index, mtb);
2022-04-10 01:12:57 +00:00
}
}
2022-04-10 01:12:57 +00:00
private void ChangeCustomBool(object sender, EventArgs e)
{
if (editing)
return;
editing = true;
var index = (int) NUD_Flag.Value;
Editor.Flags[index] = c_CustomFlag.Checked;
if (FlagDict.TryGetValue(index, out var rowIndex))
dgv.Rows[rowIndex].Cells[0].Value = c_CustomFlag.Checked;
editing = false;
}
2022-04-10 01:12:57 +00:00
private void ChangeCustomFlag(object sender, EventArgs e)
{
int flag = (int)NUD_Flag.Value;
c_CustomFlag.Checked = Editor.Flags[flag];
}
2022-04-10 01:12:57 +00:00
private void ChangeCustomFlag(object sender, KeyEventArgs e) => ChangeCustomFlag(sender, (EventArgs)e);
2022-04-10 01:12:57 +00:00
private void ChangeConstantIndex(object sender, EventArgs e)
{
var constants = Editor.Values;
var index = CB_Stats.SelectedIndex;
MT_Stat.Text = constants[index].ToString();
}
2022-04-10 01:12:57 +00:00
private void ChangeCustomConst(object sender, EventArgs e)
{
if (editing)
return;
editing = true;
var index = CB_Stats.SelectedIndex;
var parse = byte.TryParse(MT_Stat.Text, out var value) ? value : (byte)0;
Editor.Values[index] = parse;
if (WorkDict.TryGetValue(index, out var mtb))
mtb.Value = parse;
editing = false;
}
2022-04-10 01:12:57 +00:00
private void ChangeSAV(object sender, EventArgs e)
{
if (TB_NewSAV.Text.Length > 0 && TB_OldSAV.Text.Length > 0)
DiffSaves();
}
2022-04-10 01:12:57 +00:00
private void OpenSAV(object sender, EventArgs e)
{
using var ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
LoadSAV(sender, ofd.FileName);
}
2022-04-10 01:12:57 +00:00
private void LoadSAV(object sender, string path)
{
var dest = sender == B_LoadOld ? TB_OldSAV : TB_NewSAV;
dest.Text = path;
}
2022-04-10 01:12:57 +00:00
private void DiffSaves()
{
var diff = new EventBlockDiff<SAV2, byte>(TB_OldSAV.Text, TB_NewSAV.Text);
if (diff.Message != EventWorkDiffCompatibility.Valid)
2022-04-10 01:12:57 +00:00
{
WinFormsUtil.Alert(diff.Message.GetMessage());
return;
2022-04-10 01:12:57 +00:00
}
TB_IsSet.Text = string.Join(", ", diff.SetFlags.Select(z => $"{z:0000}"));
TB_UnSet.Text = string.Join(", ", diff.ClearedFlags.Select(z => $"{z:0000}"));
2022-04-10 01:12:57 +00:00
if (diff.WorkDiff.Count == 0)
2022-04-10 01:12:57 +00:00
{
WinFormsUtil.Alert("No Event Constant diff found.");
return;
2022-04-10 01:12:57 +00:00
}
var promptCopy = WinFormsUtil.Prompt(MessageBoxButtons.YesNo, "Copy Event Constant diff to clipboard?");
if (promptCopy == DialogResult.Yes)
WinFormsUtil.SetClipboardText(string.Join(Environment.NewLine, diff.WorkDiff));
}
2022-04-10 01:12:57 +00:00
private static void Main_DragEnter(object? sender, DragEventArgs? e)
{
if (e?.Data is null)
return;
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
private void Main_DragDrop(object? sender, DragEventArgs? e)
{
if (e?.Data?.GetData(DataFormats.FileDrop) is not string[] { Length: not 0 } files)
return;
var dr = WinFormsUtil.Prompt(MessageBoxButtons.YesNo, Name, "Yes: Old Save" + Environment.NewLine + "No: New Save");
var button = dr == DialogResult.Yes ? B_LoadOld : B_LoadNew;
LoadSAV(button, files[0]);
2022-04-10 01:12:57 +00:00
}
}