2018-03-23 06:04:04 +00:00
|
|
|
|
using System;
|
2018-03-24 05:37:33 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-03-24 02:05:35 +00:00
|
|
|
|
using System.Configuration;
|
2019-01-28 05:57:26 +00:00
|
|
|
|
using System.Drawing;
|
2019-03-24 02:05:35 +00:00
|
|
|
|
using System.IO;
|
2018-03-23 06:04:04 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
using PKHeX.Core;
|
|
|
|
|
using PKHeX.WinForms.Properties;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.WinForms
|
|
|
|
|
{
|
|
|
|
|
public partial class SettingsEditor : Form
|
|
|
|
|
{
|
2018-03-24 05:37:33 +00:00
|
|
|
|
public SettingsEditor(object obj, params string[] blacklist)
|
2018-03-23 06:04:04 +00:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2018-08-14 02:40:00 +00:00
|
|
|
|
SettingsObject = obj ?? Settings.Default;
|
2018-03-24 05:37:33 +00:00
|
|
|
|
LoadSettings(blacklist);
|
2018-03-23 06:04:04 +00:00
|
|
|
|
|
|
|
|
|
WinFormsUtil.TranslateInterface(this, Main.CurrentLanguage);
|
2018-08-20 04:25:21 +00:00
|
|
|
|
// reorder checkboxes
|
2019-02-17 22:52:04 +00:00
|
|
|
|
var checkboxes = FLP_Settings.Controls.OfType<CheckBox>().OrderBy(z => z.Text).ToList();
|
|
|
|
|
var ctr = 0;
|
|
|
|
|
foreach (var c in checkboxes)
|
2018-08-20 04:25:21 +00:00
|
|
|
|
FLP_Settings.Controls.SetChildIndex(c, ctr++);
|
|
|
|
|
|
2019-01-05 23:42:20 +00:00
|
|
|
|
if (obj is Settings s)
|
|
|
|
|
{
|
2019-01-15 00:25:54 +00:00
|
|
|
|
var noSelectVersions = new[] {GameVersion.GO};
|
2019-01-05 23:42:20 +00:00
|
|
|
|
CB_Blank.InitializeBinding();
|
2019-01-15 00:25:54 +00:00
|
|
|
|
CB_Blank.DataSource = GameInfo.VersionDataSource.Where(z => !noSelectVersions.Contains((GameVersion)z.Value)).ToList();
|
2019-01-05 23:42:20 +00:00
|
|
|
|
CB_Blank.SelectedValue = (int) s.DefaultSaveVersion;
|
|
|
|
|
CB_Blank.SelectedValueChanged += (_, __) => s.DefaultSaveVersion = (GameVersion)WinFormsUtil.GetIndex(CB_Blank);
|
2019-03-24 02:05:35 +00:00
|
|
|
|
B_Reset.Click += (x, e) => DeleteSettings();
|
2019-01-05 23:42:20 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FLP_Blank.Visible = false;
|
2019-03-24 02:05:35 +00:00
|
|
|
|
B_Reset.Visible = false;
|
2019-01-05 23:42:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-17 22:52:04 +00:00
|
|
|
|
PG_Color.SelectedObject = Main.Draw;
|
2018-03-24 05:37:33 +00:00
|
|
|
|
this.CenterToForm(FindForm());
|
2018-03-23 06:04:04 +00:00
|
|
|
|
}
|
2019-01-05 23:42:20 +00:00
|
|
|
|
|
2018-03-23 06:04:04 +00:00
|
|
|
|
private void SettingsEditor_FormClosing(object sender, FormClosingEventArgs e) => SaveSettings();
|
|
|
|
|
|
2018-03-24 05:37:33 +00:00
|
|
|
|
private readonly object SettingsObject;
|
2019-01-05 23:42:20 +00:00
|
|
|
|
|
2019-01-28 05:57:26 +00:00
|
|
|
|
private void LoadSettings(IReadOnlyList<string> blacklist)
|
2018-03-23 06:04:04 +00:00
|
|
|
|
{
|
2018-03-24 05:37:33 +00:00
|
|
|
|
var type = SettingsObject.GetType();
|
2019-01-28 05:57:26 +00:00
|
|
|
|
var props = ReflectUtil.GetPropertiesCanWritePublicDeclared(type);
|
|
|
|
|
if (ModifierKeys != Keys.Control)
|
|
|
|
|
props = props.Except(blacklist);
|
2018-03-23 06:04:04 +00:00
|
|
|
|
foreach (var p in props)
|
|
|
|
|
{
|
|
|
|
|
var state = ReflectUtil.GetValue(Settings.Default, p);
|
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
case bool b:
|
|
|
|
|
var chk = GetCheckBox(p, b);
|
|
|
|
|
FLP_Settings.Controls.Add(chk);
|
|
|
|
|
FLP_Settings.SetFlowBreak(chk, true);
|
2019-01-28 05:57:26 +00:00
|
|
|
|
if (blacklist.Contains(p))
|
|
|
|
|
chk.ForeColor = Color.Red;
|
2018-03-23 06:04:04 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-05 23:42:20 +00:00
|
|
|
|
|
2018-03-23 06:04:04 +00:00
|
|
|
|
private void SaveSettings()
|
|
|
|
|
{
|
|
|
|
|
foreach (var s in FLP_Settings.Controls.OfType<Control>())
|
2018-03-24 05:37:33 +00:00
|
|
|
|
ReflectUtil.SetValue(SettingsObject, s.Name, GetValue(s));
|
2018-03-23 06:04:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static CheckBox GetCheckBox(string name, bool state) => new CheckBox
|
|
|
|
|
{
|
|
|
|
|
Name = name, Checked = state, Text = name,
|
|
|
|
|
AutoSize = true,
|
|
|
|
|
};
|
2019-01-05 23:42:20 +00:00
|
|
|
|
|
2018-03-23 06:04:04 +00:00
|
|
|
|
private static object GetValue(IDisposable control)
|
|
|
|
|
{
|
2019-10-26 19:33:58 +00:00
|
|
|
|
return control switch
|
2018-03-23 06:04:04 +00:00
|
|
|
|
{
|
2019-10-26 19:33:58 +00:00
|
|
|
|
CheckBox cb => cb.Checked,
|
2020-06-28 04:36:53 +00:00
|
|
|
|
_ => null
|
2019-10-26 19:33:58 +00:00
|
|
|
|
};
|
2018-03-23 06:04:04 +00:00
|
|
|
|
}
|
2019-02-17 23:08:14 +00:00
|
|
|
|
|
|
|
|
|
private void SettingsEditor_KeyDown(object sender, KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.KeyCode == Keys.W && ModifierKeys == Keys.Control)
|
|
|
|
|
Close();
|
|
|
|
|
}
|
2019-03-24 02:05:35 +00:00
|
|
|
|
|
|
|
|
|
private static void DeleteSettings()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var dr = WinFormsUtil.Prompt(MessageBoxButtons.YesNo, "Resetting settings requires the program to exit.", MessageStrings.MsgContinue);
|
|
|
|
|
if (dr != DialogResult.Yes)
|
|
|
|
|
return;
|
|
|
|
|
var path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
|
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
File.Delete(path);
|
|
|
|
|
Environment.Exit(0);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
WinFormsUtil.Error("Failed to delete settings.", ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-23 06:04:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|