/// <summary> /// Read setting from INI file into windows form control. /// </summary> public void LoadSetting(object control, string key = null, string value = null) { if (key != null && ( key == SettingName.HookMode || key.EndsWith(SettingName.GamePadType) || key.EndsWith(SettingName.ForceType) || key.EndsWith(SettingName.LeftMotorDirection) || key.EndsWith(SettingName.RightMotorDirection) || key.EndsWith(SettingName.PassThroughIndex) || key.EndsWith(SettingName.CombinedIndex) ) ) { var cbx = (ComboBox)control; for (int i = 0; i < cbx.Items.Count; i++) { if (cbx.Items[i] is KeyValuePair) { var kv = (KeyValuePair)cbx.Items[i]; if (kv.Value == value) { cbx.SelectedIndex = i; break; } } else { var kv = System.Convert.ToInt32(cbx.Items[i]); if (kv.ToString() == value) { cbx.SelectedIndex = i; break; } } } } // If DI menu strip attached. else if (control is ComboBox cbx) { var map = SettingsMap.FirstOrDefault(x => x.Control == control); if (map != null && map.Code != default) { var text = SettingsConverter.FromIniValue(value); SetComboBoxValue(cbx, text); } else { cbx.Text = value; } } else if (control is TextBox tbx) { // if setting is read-only. if (key == SettingName.ProductName) { return; } if (key == SettingName.ProductGuid) { return; } if (key == SettingName.InstanceGuid) { return; } // Always override version. if (key == SettingName.Version) { value = SettingName.DefaultVersion; } tbx.Text = value; } else if (control is NumericUpDown nud) { decimal n = 0; decimal.TryParse(value, out n); if (n < nud.Minimum) { n = nud.Minimum; } if (n > nud.Maximum) { n = nud.Maximum; } nud.Value = n; } else if (control is TrackBar tc) { int n = 0; int.TryParse(value, out n); // convert 256 to 100% if (key == SettingName.AxisToDPadDeadZone || key == SettingName.AxisToDPadOffset || key == SettingName.LeftTriggerDeadZone || key == SettingName.RightTriggerDeadZone) { if (key == SettingName.AxisToDPadDeadZone && value == "") { n = 256; } n = System.Convert.ToInt32((float)n / 256F * 100F); } // Convert 500 to 100% else if (key == SettingName.LeftMotorPeriod || key == SettingName.RightMotorPeriod) { n = System.Convert.ToInt32((float)n / 500F * 100F); } // Convert 32767 to 100% else if (key == SettingName.LeftThumbDeadZoneX || key == SettingName.LeftThumbDeadZoneY || key == SettingName.RightThumbDeadZoneX || key == SettingName.RightThumbDeadZoneY) { n = System.Convert.ToInt32((float)n / ((float)Int16.MaxValue) * 100F); } if (n < tc.Minimum) { n = tc.Minimum; } if (n > tc.Maximum) { n = tc.Maximum; } tc.Value = n; } else if (control is CheckBox chb) { int n = 0; int.TryParse(value, out n); chb.Checked = n != 0; } }
/// <summary> /// Read setting from INI file into windows form control. /// </summary> public void ReadSettingTo(Control control, string key, string value) { if (key == SettingName.HookMode || key.EndsWith(SettingName.GamePadType) || key.EndsWith(SettingName.ForceType) || key.EndsWith(SettingName.LeftMotorDirection) || key.EndsWith(SettingName.RightMotorDirection) || key.EndsWith(SettingName.PassThroughIndex) || key.EndsWith(SettingName.CombinedIndex)) { var cbx = (ComboBox)control; for (int i = 0; i < cbx.Items.Count; i++) { if (cbx.Items[i] is KeyValuePair) { var kv = (KeyValuePair)cbx.Items[i]; if (kv.Value == value) { cbx.SelectedIndex = i; break; } } else { var kv = System.Convert.ToInt32(cbx.Items[i]); if (kv.ToString() == value) { cbx.SelectedIndex = i; break; } } } } // If Di menu strip attached. else if (control is ComboBox) { var cbx = (ComboBox)control; if (control.ContextMenuStrip == null) { control.Text = value; } else { var text = SettingsConverter.FromIniValue(value); SetComboBoxValue(cbx, text); } } else if (control is TextBox) { // if setting is read-only. if (key == SettingName.ProductName) { return; } if (key == SettingName.ProductGuid) { return; } if (key == SettingName.InstanceGuid) { return; } if (key == SettingName.InternetDatabaseUrl && string.IsNullOrEmpty(value)) { value = SettingName.DefaultInternetDatabaseUrl; } // Always override version. if (key == SettingName.Version) { value = SettingName.DefaultVersion; } control.Text = value; } else if (control is ListBox) { var lbx = (ListBox)control; lbx.Items.Clear(); if (string.IsNullOrEmpty(value)) { var folders = new List <string>(); if (Environment.Is64BitOperatingSystem) { var pf = Environment.GetEnvironmentVariable("ProgramW6432"); if (System.IO.Directory.Exists(pf)) { folders.Add(pf); } } var pf86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); if (System.IO.Directory.Exists(pf86)) { folders.Add(pf86); } lbx.Items.AddRange(folders.ToArray()); } else { lbx.Items.AddRange(value.Split(',')); } } else if (control is NumericUpDown) { var nud = (NumericUpDown)control; decimal n = 0; decimal.TryParse(value, out n); if (n < nud.Minimum) { n = nud.Minimum; } if (n > nud.Maximum) { n = nud.Maximum; } nud.Value = n; } else if (control is TrackBar) { TrackBar tc = (TrackBar)control; int n = 0; int.TryParse(value, out n); // convert 256 to 100% if (key == SettingName.AxisToDPadDeadZone || key == SettingName.AxisToDPadOffset || key == SettingName.LeftTriggerDeadZone || key == SettingName.RightTriggerDeadZone) { if (key == SettingName.AxisToDPadDeadZone && value == "") { n = 256; } n = System.Convert.ToInt32((float)n / 256F * 100F); } // Convert 500 to 100% else if (key == SettingName.LeftMotorPeriod || key == SettingName.RightMotorPeriod) { n = System.Convert.ToInt32((float)n / 500F * 100F); } // Convert 32767 to 100% else if (key == SettingName.LeftThumbDeadZoneX || key == SettingName.LeftThumbDeadZoneY || key == SettingName.RightThumbDeadZoneX || key == SettingName.RightThumbDeadZoneY) { n = System.Convert.ToInt32((float)n / ((float)Int16.MaxValue) * 100F); } if (n < tc.Minimum) { n = tc.Minimum; } if (n > tc.Maximum) { n = tc.Maximum; } tc.Value = n; } else if (control is CheckBox) { CheckBox tc = (CheckBox)control; int n = 0; int.TryParse(value, out n); tc.Checked = n != 0; } }