2015-07-01 23:17:14 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2015-07-05 19:03:57 -04:00
|
|
|
|
using System.Reflection;
|
2015-07-01 23:17:14 -04:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
2015-07-03 00:12:02 -04:00
|
|
|
|
using Mesen.GUI.Config;
|
2015-07-17 20:58:57 -04:00
|
|
|
|
using Mesen.GUI.Controls;
|
2015-07-01 23:17:14 -04:00
|
|
|
|
|
|
|
|
|
namespace Mesen.GUI.Forms
|
|
|
|
|
{
|
2015-07-03 00:12:02 -04:00
|
|
|
|
public partial class BaseConfigForm : BaseForm
|
2015-07-01 23:17:14 -04:00
|
|
|
|
{
|
2015-07-05 19:03:57 -04:00
|
|
|
|
private Dictionary<string, Control> _bindings = new Dictionary<string, Control>();
|
|
|
|
|
private Dictionary<string, FieldInfo> _fieldInfo = null;
|
|
|
|
|
private object _entity;
|
|
|
|
|
private Timer _validateTimer;
|
2015-07-17 20:58:57 -04:00
|
|
|
|
|
2015-07-03 00:12:02 -04:00
|
|
|
|
public BaseConfigForm()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2015-07-05 19:03:57 -04:00
|
|
|
|
|
|
|
|
|
_validateTimer = new Timer();
|
|
|
|
|
_validateTimer.Interval = 50;
|
|
|
|
|
_validateTimer.Tick += OnValidateInput;
|
|
|
|
|
_validateTimer.Start();
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-17 20:58:57 -04:00
|
|
|
|
protected override void OnLoad(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnLoad(e);
|
|
|
|
|
|
|
|
|
|
UpdateUI();
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-05 19:03:57 -04:00
|
|
|
|
private void OnValidateInput(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
btnOK.Enabled = ValidateInput();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnFormClosing(FormClosingEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if(DialogResult == System.Windows.Forms.DialogResult.OK) {
|
|
|
|
|
if(!ValidateInput()) {
|
|
|
|
|
e.Cancel = true;
|
|
|
|
|
} else {
|
|
|
|
|
_validateTimer.Tick -= OnValidateInput;
|
|
|
|
|
_validateTimer.Stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
base.OnFormClosing(e);
|
2015-07-03 00:12:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-01 23:17:14 -04:00
|
|
|
|
protected override void OnFormClosed(FormClosedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if(this.DialogResult == System.Windows.Forms.DialogResult.OK) {
|
2015-07-17 20:58:57 -04:00
|
|
|
|
UpdateObject();
|
2015-07-01 23:17:14 -04:00
|
|
|
|
UpdateConfig();
|
2015-07-03 00:12:02 -04:00
|
|
|
|
if(ApplyChangesOnOK) {
|
|
|
|
|
ConfigManager.ApplyChanges();
|
|
|
|
|
}
|
2015-07-01 23:17:14 -04:00
|
|
|
|
} else {
|
|
|
|
|
ConfigManager.RejectChanges();
|
|
|
|
|
}
|
|
|
|
|
base.OnFormClosed(e);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-03 00:12:02 -04:00
|
|
|
|
protected virtual bool ApplyChangesOnOK
|
|
|
|
|
{
|
|
|
|
|
get { return true; }
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-01 23:17:14 -04:00
|
|
|
|
protected virtual void UpdateConfig()
|
|
|
|
|
{
|
|
|
|
|
}
|
2015-07-03 00:12:02 -04:00
|
|
|
|
|
2015-07-17 20:58:57 -04:00
|
|
|
|
protected bool Updating
|
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
private set;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-05 19:03:57 -04:00
|
|
|
|
protected object Entity
|
|
|
|
|
{
|
|
|
|
|
get { return _entity; }
|
|
|
|
|
set { _entity = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual Type BindedType
|
|
|
|
|
{
|
|
|
|
|
get { return _entity.GetType(); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual bool ValidateInput()
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void AddBinding(string fieldName, Control bindedField, object enumValue = null)
|
|
|
|
|
{
|
|
|
|
|
if(BindedType == null) {
|
|
|
|
|
throw new Exception("Need to override BindedType to use bindings");
|
|
|
|
|
} else {
|
|
|
|
|
_fieldInfo = new Dictionary<string,FieldInfo>();
|
|
|
|
|
FieldInfo[] members = BindedType.GetFields();
|
|
|
|
|
foreach(FieldInfo info in members) {
|
|
|
|
|
_fieldInfo[info.Name] = info;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
bindedField.Tag = enumValue;
|
|
|
|
|
_bindings[fieldName] = bindedField;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void UpdateUI()
|
|
|
|
|
{
|
2015-07-17 20:58:57 -04:00
|
|
|
|
this.Updating = true;
|
2015-07-05 19:03:57 -04:00
|
|
|
|
foreach(KeyValuePair<string, Control> kvp in _bindings) {
|
|
|
|
|
if(!_fieldInfo.ContainsKey(kvp.Key)) {
|
|
|
|
|
throw new Exception("Invalid binding key");
|
|
|
|
|
} else {
|
|
|
|
|
FieldInfo field = _fieldInfo[kvp.Key];
|
|
|
|
|
object value = field.GetValue(this.Entity);
|
|
|
|
|
if(kvp.Value is TextBox) {
|
|
|
|
|
if(field.FieldType == typeof(UInt32)) {
|
|
|
|
|
kvp.Value.Text = ((UInt32)value).ToString("X");
|
|
|
|
|
} else if(field.FieldType == typeof(Byte)) {
|
|
|
|
|
kvp.Value.Text = ((Byte)value).ToString("X");
|
|
|
|
|
} else {
|
|
|
|
|
kvp.Value.Text = (string)value;
|
|
|
|
|
}
|
|
|
|
|
} else if(kvp.Value is CheckBox) {
|
|
|
|
|
((CheckBox)kvp.Value).Checked = (bool)value;
|
|
|
|
|
} else if(kvp.Value is Panel) {
|
|
|
|
|
RadioButton radio = kvp.Value.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Tag.Equals(value));
|
|
|
|
|
if(radio != null) {
|
|
|
|
|
radio.Checked = true;
|
|
|
|
|
} else {
|
|
|
|
|
throw new Exception("No radio button matching value found");
|
|
|
|
|
}
|
2015-07-17 20:58:57 -04:00
|
|
|
|
} else if(kvp.Value is ctrlTrackbar) {
|
|
|
|
|
((ctrlTrackbar)kvp.Value).Value = (int)(uint)value;
|
|
|
|
|
} else if(kvp.Value is NumericUpDown) {
|
|
|
|
|
NumericUpDown nud = kvp.Value as NumericUpDown;
|
|
|
|
|
decimal val = (decimal)(uint)value;
|
|
|
|
|
val = Math.Min(Math.Max(val, nud.Minimum), nud.Maximum);
|
|
|
|
|
nud.Value = val;
|
2015-07-05 19:03:57 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-17 20:58:57 -04:00
|
|
|
|
}
|
|
|
|
|
this.Updating = false;
|
2015-07-05 19:03:57 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void UpdateObject()
|
|
|
|
|
{
|
|
|
|
|
foreach(KeyValuePair<string, Control> kvp in _bindings) {
|
|
|
|
|
if(!_fieldInfo.ContainsKey(kvp.Key)) {
|
|
|
|
|
throw new Exception("Invalid binding key");
|
|
|
|
|
} else {
|
|
|
|
|
FieldInfo field = _fieldInfo[kvp.Key];
|
|
|
|
|
if(kvp.Value is TextBox) {
|
|
|
|
|
object value = kvp.Value.Text;
|
|
|
|
|
if(field.FieldType == typeof(UInt32)) {
|
|
|
|
|
value = (object)UInt32.Parse((string)value, System.Globalization.NumberStyles.HexNumber);
|
|
|
|
|
} else if(field.FieldType == typeof(Byte)) {
|
|
|
|
|
value = (object)Byte.Parse((string)value, System.Globalization.NumberStyles.HexNumber);
|
|
|
|
|
}
|
|
|
|
|
field.SetValue(Entity, value);
|
|
|
|
|
} else if(kvp.Value is CheckBox) {
|
|
|
|
|
field.SetValue(Entity, ((CheckBox)kvp.Value).Checked);
|
|
|
|
|
} else if(kvp.Value is Panel) {
|
|
|
|
|
field.SetValue(Entity, kvp.Value.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked).Tag);
|
2015-07-17 20:58:57 -04:00
|
|
|
|
} else if(kvp.Value is ctrlTrackbar) {
|
|
|
|
|
field.SetValue(Entity, (UInt32)((ctrlTrackbar)kvp.Value).Value);
|
|
|
|
|
} else if(kvp.Value is NumericUpDown) {
|
|
|
|
|
field.SetValue(Entity, (UInt32)((NumericUpDown)kvp.Value).Value);
|
2015-07-05 19:03:57 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-17 20:58:57 -04:00
|
|
|
|
}
|
2015-07-05 19:03:57 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-03 00:12:02 -04:00
|
|
|
|
private void btnOK_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
this.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnCancel_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
this.Close();
|
|
|
|
|
}
|
2015-07-01 23:17:14 -04:00
|
|
|
|
}
|
|
|
|
|
}
|