2022-08-27 06:43:36 +00:00
|
|
|
|
using System;
|
2017-04-09 21:06:50 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-12-12 05:44:05 +00:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Windows.Forms;
|
2017-04-09 21:06:50 +00:00
|
|
|
|
using PKHeX.Core;
|
2014-12-12 05:44:05 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
namespace PKHeX.WinForms;
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public partial class TrashEditor : Form
|
|
|
|
|
{
|
|
|
|
|
private readonly SaveFile SAV;
|
2020-10-18 18:02:39 +00:00
|
|
|
|
|
2023-12-04 04:13:20 +00:00
|
|
|
|
public TrashEditor(TextBoxBase TB_NN, SaveFile sav) : this(TB_NN, [], sav) { }
|
2017-04-09 21:06:50 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public TrashEditor(TextBoxBase TB_NN, Span<byte> raw, SaveFile sav)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
WinFormsUtil.TranslateInterface(this, Main.CurrentLanguage);
|
|
|
|
|
SAV = sav;
|
2017-04-09 21:06:50 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
FinalString = TB_NN.Text;
|
2017-04-09 21:06:50 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
editing = true;
|
|
|
|
|
if (raw.Length != 0)
|
2023-01-22 04:02:33 +00:00
|
|
|
|
{
|
|
|
|
|
Raw = FinalBytes = raw.ToArray();
|
2022-06-18 18:04:24 +00:00
|
|
|
|
AddTrashEditing(raw.Length);
|
2023-01-22 04:02:33 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-12-04 04:13:20 +00:00
|
|
|
|
Raw = FinalBytes = [];
|
2023-01-22 04:02:33 +00:00
|
|
|
|
}
|
2017-04-09 21:06:50 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var f = FontUtil.GetPKXFont();
|
|
|
|
|
AddCharEditing(f);
|
|
|
|
|
TB_Text.MaxLength = TB_NN.MaxLength;
|
|
|
|
|
TB_Text.Text = TB_NN.Text;
|
|
|
|
|
TB_Text.Font = f;
|
2017-04-10 04:53:53 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (FLP_Characters.Controls.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
FLP_Characters.Visible = false;
|
|
|
|
|
FLP_Hex.Height *= 2;
|
2017-04-09 21:06:50 +00:00
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
else if (FLP_Hex.Controls.Count == 0)
|
2017-04-09 21:06:50 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
FLP_Characters.Location = FLP_Hex.Location;
|
|
|
|
|
FLP_Characters.Height *= 2;
|
2017-04-09 21:06:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
editing = false;
|
|
|
|
|
CenterToParent();
|
|
|
|
|
}
|
2017-04-09 21:06:50 +00:00
|
|
|
|
|
2023-12-04 04:13:20 +00:00
|
|
|
|
private readonly List<NumericUpDown> Bytes = [];
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public string FinalString;
|
|
|
|
|
public byte[] FinalBytes;
|
|
|
|
|
private readonly byte[] Raw;
|
|
|
|
|
private bool editing;
|
|
|
|
|
private void B_Cancel_Click(object sender, EventArgs e) => Close();
|
|
|
|
|
|
|
|
|
|
private void B_Save_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
FinalString = TB_Text.Text;
|
|
|
|
|
if (FinalBytes.Length == 0)
|
|
|
|
|
FinalBytes = Raw;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddCharEditing(Font f)
|
|
|
|
|
{
|
2023-03-26 00:55:55 +00:00
|
|
|
|
var chars = GetChars(SAV.Generation);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (chars.Length == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
FLP_Characters.Visible = true;
|
|
|
|
|
foreach (ushort c in chars)
|
|
|
|
|
{
|
|
|
|
|
var l = GetLabel(((char)c).ToString());
|
|
|
|
|
l.Font = f;
|
|
|
|
|
l.AutoSize = false;
|
|
|
|
|
l.Size = new Size(20, 20);
|
|
|
|
|
l.Click += (s, e) => { if (TB_Text.Text.Length < TB_Text.MaxLength) TB_Text.AppendText(l.Text); };
|
|
|
|
|
FLP_Characters.Controls.Add(l);
|
2017-04-09 21:06:50 +00:00
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
private void AddTrashEditing(int count)
|
|
|
|
|
{
|
|
|
|
|
FLP_Hex.Visible = true;
|
|
|
|
|
GB_Trash.Visible = true;
|
|
|
|
|
NUD_Generation.Value = SAV.Generation;
|
|
|
|
|
for (int i = 0; i < count; i++)
|
2017-04-09 21:06:50 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var l = GetLabel($"${i:X2}");
|
|
|
|
|
l.Font = NUD_Generation.Font;
|
|
|
|
|
var n = GetNUD(min: 0, max: 255, hex: true);
|
|
|
|
|
n.Click += (s, e) =>
|
2014-12-12 05:44:05 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
switch (ModifierKeys)
|
2017-04-09 21:06:50 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
case Keys.Shift: n.Value = n.Maximum; break;
|
|
|
|
|
case Keys.Alt: n.Value = n.Minimum; break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
n.Value = Raw[i];
|
|
|
|
|
n.ValueChanged += (o, args) => UpdateNUD(n, args);
|
|
|
|
|
|
|
|
|
|
FLP_Hex.Controls.Add(l);
|
|
|
|
|
FLP_Hex.Controls.Add(n);
|
|
|
|
|
Bytes.Add(n);
|
2024-01-04 07:46:34 +00:00
|
|
|
|
if (i % 4 == 3)
|
|
|
|
|
FLP_Hex.SetFlowBreak(n, true);
|
2014-12-12 05:44:05 +00:00
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
TB_Text.TextChanged += (o, args) => UpdateString(TB_Text, args);
|
2016-01-17 21:27:24 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
CB_Species.InitializeBinding();
|
|
|
|
|
CB_Species.DataSource = new BindingSource(GameInfo.SpeciesDataSource, null);
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
CB_Language.InitializeBinding();
|
|
|
|
|
CB_Language.DataSource = GameInfo.LanguageDataSource(SAV.Generation);
|
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
private void UpdateNUD(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (editing)
|
|
|
|
|
return;
|
|
|
|
|
editing = true;
|
|
|
|
|
// build bytes
|
|
|
|
|
if (sender is not NumericUpDown nud)
|
|
|
|
|
throw new Exception();
|
|
|
|
|
int index = Bytes.IndexOf(nud);
|
|
|
|
|
Raw[index] = (byte)nud.Value;
|
|
|
|
|
|
|
|
|
|
TB_Text.Text = GetString();
|
|
|
|
|
editing = false;
|
|
|
|
|
}
|
2017-04-09 21:06:50 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
private void UpdateString(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (editing)
|
|
|
|
|
return;
|
|
|
|
|
editing = true;
|
|
|
|
|
// build bytes
|
|
|
|
|
byte[] data = SetString(TB_Text.Text);
|
|
|
|
|
Array.Copy(data, Raw, Math.Min(data.Length, Raw.Length));
|
|
|
|
|
for (int i = 0; i < Raw.Length; i++)
|
|
|
|
|
Bytes[i].Value = Raw[i];
|
|
|
|
|
editing = false;
|
|
|
|
|
}
|
2017-04-09 21:06:50 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
private void B_ApplyTrash_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2022-08-27 06:43:36 +00:00
|
|
|
|
var species = (ushort)WinFormsUtil.GetIndex(CB_Species);
|
|
|
|
|
var language = WinFormsUtil.GetIndex(CB_Language);
|
2024-02-23 03:20:54 +00:00
|
|
|
|
var gen = (byte)NUD_Generation.Value;
|
2022-08-27 06:43:36 +00:00
|
|
|
|
string speciesName = SpeciesName.GetSpeciesNameGeneration(species, language, gen);
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2022-08-27 06:43:36 +00:00
|
|
|
|
if (string.IsNullOrEmpty(speciesName)) // no result
|
|
|
|
|
speciesName = CB_Species.Text;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
|
|
byte[] current = SetString(TB_Text.Text);
|
2022-08-27 06:43:36 +00:00
|
|
|
|
byte[] data = SetString(speciesName);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (data.Length <= current.Length)
|
2017-04-09 21:06:50 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
WinFormsUtil.Alert("Trash byte layer is hidden by current text.",
|
|
|
|
|
$"Current Bytes: {current.Length}" + Environment.NewLine + $"Layer Bytes: {data.Length}");
|
|
|
|
|
return;
|
2017-04-09 21:06:50 +00:00
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (data.Length > Bytes.Count)
|
2022-01-03 05:35:59 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
WinFormsUtil.Alert("Trash byte layer is too long to apply.");
|
|
|
|
|
return;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
for (int i = current.Length; i < data.Length; i++)
|
|
|
|
|
Bytes[i].Value = data[i];
|
|
|
|
|
}
|
2022-01-03 05:35:59 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
private void B_ClearTrash_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
byte[] current = SetString(TB_Text.Text);
|
|
|
|
|
for (int i = current.Length; i < Bytes.Count; i++)
|
|
|
|
|
Bytes[i].Value = 0;
|
|
|
|
|
}
|
2017-04-09 21:06:50 +00:00
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
|
private byte[] SetString(ReadOnlySpan<char> text)
|
2022-06-18 18:04:24 +00:00
|
|
|
|
{
|
|
|
|
|
Span<byte> temp = stackalloc byte[Raw.Length];
|
2023-01-22 04:02:33 +00:00
|
|
|
|
var written = SAV.SetString(temp, text, text.Length, StringConverterOption.None);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
return temp[..written].ToArray();
|
|
|
|
|
}
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
|
private string GetString() => SAV.GetString(Raw);
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
// Helpers
|
2023-01-22 04:02:33 +00:00
|
|
|
|
private static Label GetLabel(string str) => new() { Text = str, AutoSize = false, Size = new Size(40, 24), TextAlign = ContentAlignment.MiddleRight };
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
|
private static NumericUpDown GetNUD(byte min, byte max, bool hex) => new()
|
2022-06-18 18:04:24 +00:00
|
|
|
|
{
|
|
|
|
|
Maximum = max,
|
|
|
|
|
Minimum = min,
|
|
|
|
|
Hexadecimal = hex,
|
2023-01-22 04:02:33 +00:00
|
|
|
|
Width = 40,
|
2022-06-18 18:04:24 +00:00
|
|
|
|
Padding = new Padding(0),
|
|
|
|
|
Margin = new Padding(0),
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-23 03:20:54 +00:00
|
|
|
|
private static ReadOnlySpan<ushort> GetChars(byte generation) => generation switch
|
2022-06-18 18:04:24 +00:00
|
|
|
|
{
|
2023-12-04 04:13:20 +00:00
|
|
|
|
5 => SpecialCharsGen5,
|
2023-03-26 00:55:55 +00:00
|
|
|
|
6 => SpecialCharsGen67,
|
|
|
|
|
7 => SpecialCharsGen67,
|
2023-12-04 04:13:20 +00:00
|
|
|
|
_ => [], // Undocumented
|
2022-06-18 18:04:24 +00:00
|
|
|
|
};
|
|
|
|
|
|
2023-12-04 04:13:20 +00:00
|
|
|
|
// Unicode codepoints for special characters, incorrectly starting at 0x2460 instead of 0xE0xx.
|
|
|
|
|
private static ReadOnlySpan<ushort> SpecialCharsGen5 =>
|
|
|
|
|
[
|
|
|
|
|
0x2460, // Full Neutral (Happy in Gen7)
|
|
|
|
|
0x2461, // Full Happy (Angry in Gen7)
|
|
|
|
|
0x2462, // Full Sad
|
|
|
|
|
0x2463, // Full Angry (Neutral in Gen7)
|
|
|
|
|
0x2464, // Full Right-up arrow
|
|
|
|
|
0x2465, // Full Right-down arrow
|
|
|
|
|
0x2466, // Full Zz
|
|
|
|
|
0x2467, // ×
|
|
|
|
|
0x2468, // ÷
|
|
|
|
|
// Skip 69-6B, can't be entered.
|
|
|
|
|
0x246C, // …
|
|
|
|
|
0x246D, // ♂
|
|
|
|
|
0x246E, // ♀
|
|
|
|
|
0x246F, // ♠
|
|
|
|
|
0x2470, // ♣
|
|
|
|
|
0x2471, // ♥
|
|
|
|
|
0x2472, // ♦
|
|
|
|
|
0x2473, // ★
|
|
|
|
|
0x2474, // ◎
|
|
|
|
|
0x2475, // ○
|
|
|
|
|
0x2476, // □
|
|
|
|
|
0x2477, // △
|
|
|
|
|
0x2478, // ◇
|
|
|
|
|
0x2479, // ♪
|
|
|
|
|
0x247A, // ☀
|
|
|
|
|
0x247B, // ☁
|
|
|
|
|
0x247C, // ☂
|
|
|
|
|
0x247D, // ☃
|
|
|
|
|
0x247E, // Half Neutral
|
|
|
|
|
0x247F, // Half Happy
|
|
|
|
|
0x2480, // Half Sad
|
|
|
|
|
0x2481, // Half Angry
|
|
|
|
|
0x2482, // Half Right-up arrow
|
|
|
|
|
0x2483, // Half Right-down arrow
|
|
|
|
|
0x2484, // Half Zz
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
private static ReadOnlySpan<ushort> SpecialCharsGen67 =>
|
|
|
|
|
[
|
|
|
|
|
0xE081, // Full Neutral (Happy in Gen7)
|
|
|
|
|
0xE082, // Full Happy (Angry in Gen7)
|
|
|
|
|
0xE083, // Full Sad
|
|
|
|
|
0xE084, // Full Angry (Neutral in Gen7)
|
|
|
|
|
0xE085, // Full Right-up arrow
|
|
|
|
|
0xE086, // Full Right-down arrow
|
|
|
|
|
0xE087, // Full Zz
|
|
|
|
|
0xE088, // ×
|
|
|
|
|
0xE089, // ÷
|
|
|
|
|
// Skip 8A-8C, can't be entered.
|
|
|
|
|
0xE08D, // …
|
|
|
|
|
0xE08E, // ♂
|
|
|
|
|
0xE08F, // ♀
|
|
|
|
|
0xE090, // ♠
|
|
|
|
|
0xE091, // ♣
|
|
|
|
|
0xE092, // ♥
|
|
|
|
|
0xE093, // ♦
|
|
|
|
|
0xE094, // ★
|
|
|
|
|
0xE095, // ◎
|
|
|
|
|
0xE096, // ○
|
|
|
|
|
0xE097, // □
|
|
|
|
|
0xE098, // △
|
|
|
|
|
0xE099, // ◇
|
|
|
|
|
0xE09A, // ♪
|
|
|
|
|
0xE09B, // ☀
|
|
|
|
|
0xE09C, // ☁
|
|
|
|
|
0xE09D, // ☂
|
|
|
|
|
0xE09E, // ☃
|
|
|
|
|
0xE09F, // Half Neutral
|
|
|
|
|
0xE0A0, // Half Happy
|
|
|
|
|
0xE0A1, // Half Sad
|
|
|
|
|
0xE0A2, // Half Angry
|
|
|
|
|
0xE0A3, // Half Right-up arrow
|
|
|
|
|
0xE0A4, // Half Right-down arrow
|
|
|
|
|
0xE0A5, // Half Zz
|
|
|
|
|
];
|
2014-12-12 05:44:05 +00:00
|
|
|
|
}
|