Exemplo n.º 1
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.DeviceSubType) || key.EndsWith(SettingName.ForceType) || 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 = new SettingsConverter(value, key).ToFrmSetting();
             SetComboBoxValue(cbx, text);
         }
     }
     else if (control is TextBox)
     {
         // if setting is readonly.
         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;
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Save setting from windows form control to current INI file.
        /// </summary>
        /// <param name="path">path of parameter (related to actual control)</param>
        /// <param name="dstIniSection">if not null then section will be different inside INI file than specified in path</param>
        public bool SaveSetting(Ini ini, string path)
        {
            var    control = SettingsMap[path];
            string key     = path.Split('\\')[1];
            string v       = string.Empty;

            if (key == SettingName.HookMode || key.EndsWith(SettingName.DeviceSubType) || key.EndsWith(SettingName.ForceType) || key.EndsWith(SettingName.PassThroughIndex) || key.EndsWith(SettingName.CombinedIndex))
            {
                var v1 = ((ComboBox)control).SelectedItem;
                if (v1 == null)
                {
                    v = "0";
                }
                else if (v1 is KeyValuePair)
                {
                    v = ((KeyValuePair)v1).Value;
                }
                else
                {
                    v = System.Convert.ToInt32(v1).ToString();
                }
            }
            // If di menu strip attached.
            else if (control is ComboBox)
            {
                var cbx = (ComboBox)control;
                if (control.ContextMenuStrip == null)
                {
                    v = control.Text;
                }
                else
                {
                    v = new SettingsConverter(control.Text, key).ToIniSetting();
                    // make sure that disabled button value is "0".
                    if (SettingName.IsButton(key) && string.IsNullOrEmpty(v))
                    {
                        v = "0";
                    }
                }
            }
            else if (control is TextBox)
            {
                // if setting is readonly.
                if (key == SettingName.InstanceGuid || key == SettingName.ProductGuid)
                {
                    v = string.IsNullOrEmpty(control.Text) ? Guid.Empty.ToString("D") : control.Text;
                }
                else
                {
                    v = control.Text;
                }
            }
            else if (control is ListBox)
            {
                var lbx = (ListBox)control;
                v = string.Join(",", lbx.Items.Cast <string>().ToArray());
            }
            else if (control is NumericUpDown)
            {
                NumericUpDown nud = (NumericUpDown)control;
                v = nud.Value.ToString();
            }
            else if (control is TrackBar)
            {
                TrackBar tc = (TrackBar)control;
                // convert 100%  to 256
                if (key == SettingName.AxisToDPadDeadZone || key == SettingName.AxisToDPadOffset || key == SettingName.LeftTriggerDeadZone || key == SettingName.RightTriggerDeadZone)
                {
                    v = System.Convert.ToInt32((float)tc.Value / 100F * 256F).ToString();
                }
                // convert 100%  to 500
                else if (key == SettingName.LeftMotorPeriod || key == SettingName.RightMotorPeriod)
                {
                    v = System.Convert.ToInt32((float)tc.Value / 100F * 500F).ToString();
                }
                // Convert 100% to 32767
                else if (key == SettingName.LeftThumbDeadZoneX || key == SettingName.LeftThumbDeadZoneY || key == SettingName.RightThumbDeadZoneX || key == SettingName.RightThumbDeadZoneY)
                {
                    v = System.Convert.ToInt32((float)tc.Value / 100F * ((float)Int16.MaxValue)).ToString();
                }
                else
                {
                    v = tc.Value.ToString();
                }
            }
            else if (control is CheckBox)
            {
                CheckBox tc = (CheckBox)control;
                v = tc.Checked ? "1" : "0";
            }
            if (SettingName.IsThumbAxis(key))
            {
                v = v.Replace(SettingName.SType.Axis, "");
            }
            if (SettingName.IsDPad(key))
            {
                v = v.Replace(SettingName.SType.DPad, "");
            }
            if (v == "v1")
            {
                v = "UP";
            }
            if (v == "v2")
            {
                v = "RIGHT";
            }
            if (v == "v3")
            {
                v = "DOWN";
            }
            if (v == "v4")
            {
                v = "LEFT";
            }
            if (v == "")
            {
                if (key == SettingName.DPadUp)
                {
                    v = "UP";
                }
                if (key == SettingName.DPadDown)
                {
                    v = "DOWN";
                }
                if (key == SettingName.DPadLeft)
                {
                    v = "LEFT";
                }
                if (key == SettingName.DPadRight)
                {
                    v = "RIGHT";
                }
            }
            // add comment.
            //var l = SettingName.MaxNameLength - key.Length + 24;
            //v = string.Format("{0, -" + l + "} # {1} Default: '{2}'.", v, SettingName.GetDescription(key), SettingName.GetDefaultValue(key));
            var section  = path.Split('\\')[0];
            var padIndex = SettingName.GetPadIndex(path);

            // If this is PAD section then
            if (padIndex > -1)
            {
                section = GetInstanceSection(padIndex);
                // If destination section is empty because controller is not connected then skip.
                if (string.IsNullOrEmpty(section))
                {
                    return(false);
                }
            }
            var oldValue = ini.GetValue(section, key);
            var saved    = false;

            if (oldValue != v)
            {
                ini.SetValue(section, key, v);
                saveCount++;
                saved = true;
                if (ConfigSaved != null)
                {
                    ConfigSaved(this, new SettingEventArgs(IniFileName, saveCount));
                }
            }
            // Flush XML too.
            SettingsFile.Current.Save();
            return(saved);
        }
Exemplo n.º 3
0
 /// <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;
     }
 }
Exemplo n.º 4
0
        public string GetSettingValue(object control)
        {
            var    item     = SettingsMap.First(x => x.Control == control);
            var    path     = item.IniPath;
            var    section  = path.Split('\\')[0];
            string key      = path.Split('\\')[1];
            var    padIndex = SettingName.GetPadIndex(path);
            string v        = string.Empty;

            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 v1 = ((ComboBox)control).SelectedItem;
                if (v1 == null)
                {
                    v = "0";
                }
                else if (v1 is KeyValuePair)
                {
                    v = ((KeyValuePair)v1).Value;
                }
                else
                {
                    v = System.Convert.ToInt32(v1).ToString();
                }
            }
            // 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)
                {
                    v = SettingsConverter.ToIniValue(cbx.Text);
                    // make sure that disabled button value is "0".
                    if (SettingName.IsButton(key) && string.IsNullOrEmpty(v))
                    {
                        v = "0";
                    }
                }
                else
                {
                    v = cbx.Text;
                }
            }
            else if (control is TextBox tbx)
            {
                // if setting is read-only.
                if (key == SettingName.InstanceGuid || key == SettingName.ProductGuid)
                {
                    v = string.IsNullOrEmpty(tbx.Text) ? Guid.Empty.ToString("D") : tbx.Text;
                }
                else
                {
                    v = tbx.Text;
                }
            }
            else if (control is NumericUpDown nud)
            {
                v = nud.Value.ToString();
            }
            else if (control is TrackBar)
            {
                TrackBar tc = (TrackBar)control;
                // convert 100%  to 256
                if (key == SettingName.AxisToDPadDeadZone || key == SettingName.AxisToDPadOffset || key == SettingName.LeftTriggerDeadZone || key == SettingName.RightTriggerDeadZone)
                {
                    v = System.Convert.ToInt32((float)tc.Value / 100F * 256F).ToString();
                }
                // convert 100%  to 500
                else if (key == SettingName.LeftMotorPeriod || key == SettingName.RightMotorPeriod)
                {
                    v = System.Convert.ToInt32((float)tc.Value / 100F * 500F).ToString();
                }
                // Convert 100% to 32767
                else if (key == SettingName.LeftThumbDeadZoneX || key == SettingName.LeftThumbDeadZoneY || key == SettingName.RightThumbDeadZoneX || key == SettingName.RightThumbDeadZoneY)
                {
                    v = System.Convert.ToInt32((float)tc.Value / 100F * ((float)Int16.MaxValue)).ToString();
                }
                else
                {
                    v = tc.Value.ToString();
                }
            }
            else if (control is CheckBox)
            {
                CheckBox tc = (CheckBox)control;
                v = tc.Checked ? "1" : "0";
            }
            else if (control is DataGridView)
            {
                var grid = (DataGridView)control;
                if (grid.Enabled)
                {
                    var data = grid.Rows.Cast <DataGridViewRow>().Where(x => x.Visible).Select(x => x.DataBoundItem as UserSetting)
                               // Make sure that only enabled controllers are added.
                               .Where(x => x != null && x.IsEnabled).ToArray();
                    data = FilterSettings(data);
                    var sections = data.Select(x => GetInstanceSection(x.InstanceGuid)).ToArray();
                    // x360ce.dll must combine devices separated by comma.
                    //v = string.Join(",", sections);
                    // Use backwards compatible mode (one device at the time).
                    v = sections.FirstOrDefault() ?? "";
                    // Note: Must code device combine workaround.
                }
                else
                {
                    v = "AUTO";
                }
            }
            return(v);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Called when recording is in progress.
        /// </summary>
        /// <param name="state">Current direct input activity.</param>
        /// <returns>True if recording stopped, otherwise false.</returns>
        public bool StopRecording(CustomDiState state = null)
        {
            lock (recordingLock)
            {
                // If recording is not in progress then return false.
                if (!Recording)
                {
                    recordingSnapshot = null;
                    return(false);
                }
                // Must stop recording if null state passed i.e. probably ESC key was pressed.
                var    stop   = state == null;
                string action = null;
                var    map    = CurrentMap;
                var    code   = map.Code;
                var    box    = (ComboBox)map.Control;
                if (state != null)
                {
                    // If recording snapshot was not created yet then...
                    if (recordingSnapshot == null)
                    {
                        // Make snapshot out of the first state during recording.
                        recordingSnapshot = state;
                        return(false);
                    }
                    var actions = state == null
                                                  ? Array.Empty <string>()
                                  // Get actions by comparing initial snapshot with current state.
                                                  : Recorder.CompareTo(recordingSnapshot, state, map.Code);

                    // if recording and at least one action was recorded then...
                    if (!stop && actions.Length > 0)
                    {
                        MapType type;
                        int     index;
                        SettingsConverter.TryParseTextValue(actions[0], out type, out index);
                        // If this is Thumb Up, Left, Right, Down and axis was mapped.
                        if (SettingsConverter.ThumbDirections.Contains(code) && SettingsConverter.IsAxis(type))
                        {
                            // Make full axis.
                            type = SettingsConverter.ToFull(type);
                            var isUp =
                                code == MapCode.LeftThumbUp ||
                                code == MapCode.RightThumbUp;
                            var isLeft =
                                code == MapCode.LeftThumbLeft ||
                                code == MapCode.RightThumbLeft;
                            var isRight =
                                code == MapCode.LeftThumbRight ||
                                code == MapCode.RightThumbRight;
                            var isDown =
                                code == MapCode.LeftThumbDown ||
                                code == MapCode.RightThumbDown;
                            // Invert.
                            if (isLeft || isDown)
                            {
                                type = SettingsConverter.Invert(type);
                            }
                            var newCode     = code;
                            var isLeftThumb = SettingsConverter.LeftThumbCodes.Contains(code);
                            if (isRight || isLeft)
                            {
                                newCode = isLeftThumb
                                                                        ? MapCode.LeftThumbAxisX
                                                                        : MapCode.RightThumbAxisX;
                            }
                            if (isUp || isDown)
                            {
                                newCode = isLeftThumb
                                                                        ? MapCode.LeftThumbAxisY
                                                                        : MapCode.RightThumbAxisY;
                            }
                            // Change destination control.
                            var rMap = SettingsManager.Current.SettingsMap.First(x => x.MapTo == map.MapTo && x.Code == newCode);
                            box    = (ComboBox)rMap.Control;
                            action = SettingsConverter.ToTextValue(type, index);
                            stop   = true;
                        }
                        // If this is DPad ComboBox then...
                        else if (code == MapCode.DPad)
                        {
                            // Get first action suitable for DPad
                            Regex dPadRx     = new Regex("(POV [0-9]+)");
                            var   dPadAction = actions.FirstOrDefault(x => dPadRx.IsMatch(x));
                            if (dPadAction != null)
                            {
                                action = dPadRx.Match(dPadAction).Groups[0].Value;
                                stop   = true;
                            }
                        }
                        else
                        {
                            // Get first recorded action.
                            action = actions[0];
                            stop   = true;
                        }
                    }
                }
                // If recording must stop then...
                if (stop)
                {
                    Recording  = false;
                    CurrentMap = null;
                    // If stop was initiated before action was recorded then...
                    if (string.IsNullOrEmpty(action))
                    {
                        box.Items.Clear();
                    }
                    else
                    {
                        // If suitable action was recorded then...
                        SettingsManager.Current.SetComboBoxValue(box, action);
                        // Save setting and notify if value changed.
                        SettingsManager.Current.RaiseSettingsChanged(box);
                    }
                    //box.ForeColor = SystemColors.WindowText;
                }
                return(stop);
            }
        }
Exemplo n.º 6
0
        public string GetSettingValue(Control control)
        {
            var    item     = SettingsMap.First(x => x.Control == control);
            var    path     = item.IniPath;
            var    section  = path.Split('\\')[0];
            string key      = path.Split('\\')[1];
            var    padIndex = SettingName.GetPadIndex(path);
            string v        = string.Empty;

            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 v1 = ((ComboBox)control).SelectedItem;
                if (v1 == null)
                {
                    v = "0";
                }
                else if (v1 is KeyValuePair)
                {
                    v = ((KeyValuePair)v1).Value;
                }
                else
                {
                    v = System.Convert.ToInt32(v1).ToString();
                }
            }
            // If di menu strip attached.
            else if (control is ComboBox)
            {
                var cbx = (ComboBox)control;
                if (control.ContextMenuStrip == null)
                {
                    v = control.Text;
                }
                else
                {
                    v = SettingsConverter.ToIniValue(control.Text);
                    // make sure that disabled button value is "0".
                    if (SettingName.IsButton(key) && string.IsNullOrEmpty(v))
                    {
                        v = "0";
                    }
                }
                // Save to XML.
                if (key == SettingName.InternetDatabaseUrl)
                {
                    Options.InternetDatabaseUrl = v;
                }
            }
            else if (control is TextBox)
            {
                // if setting is read-only.
                if (key == SettingName.InstanceGuid || key == SettingName.ProductGuid)
                {
                    v = string.IsNullOrEmpty(control.Text) ? Guid.Empty.ToString("D") : control.Text;
                }
                else
                {
                    v = control.Text;
                }
            }
            else if (control is NumericUpDown)
            {
                NumericUpDown nud = (NumericUpDown)control;
                v = nud.Value.ToString();
            }
            else if (control is TrackBar)
            {
                TrackBar tc = (TrackBar)control;
                // convert 100%  to 256
                if (key == SettingName.AxisToDPadDeadZone || key == SettingName.AxisToDPadOffset || key == SettingName.LeftTriggerDeadZone || key == SettingName.RightTriggerDeadZone)
                {
                    v = System.Convert.ToInt32((float)tc.Value / 100F * 256F).ToString();
                }
                // convert 100%  to 500
                else if (key == SettingName.LeftMotorPeriod || key == SettingName.RightMotorPeriod)
                {
                    v = System.Convert.ToInt32((float)tc.Value / 100F * 500F).ToString();
                }
                // Convert 100% to 32767
                else if (key == SettingName.LeftThumbDeadZoneX || key == SettingName.LeftThumbDeadZoneY || key == SettingName.RightThumbDeadZoneX || key == SettingName.RightThumbDeadZoneY)
                {
                    v = System.Convert.ToInt32((float)tc.Value / 100F * ((float)Int16.MaxValue)).ToString();
                }
                else
                {
                    v = tc.Value.ToString();
                }
            }
            else if (control is CheckBox)
            {
                CheckBox tc = (CheckBox)control;
                v = tc.Checked ? "1" : "0";
            }
            else if (control is DataGridView)
            {
                var grid = (DataGridView)control;
                if (grid.Enabled)
                {
                    var data = grid.Rows.Cast <DataGridViewRow>().Where(x => x.Visible).Select(x => x.DataBoundItem as Setting)
                               // Make sure that only enabled controllers are added.
                               .Where(x => x != null && x.IsEnabled).ToArray();
                    var instances = data.Select(x => GetInstanceSection(x.InstanceGuid)).ToArray();
                    // Separate devices with comma. x360ce.dll must combine devices separated by comma.
                    v = string.Join(",", instances);
                }
                else
                {
                    v = "AUTO";
                }
            }
            return(v);
        }
Exemplo n.º 7
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.DeviceSubType) || key.EndsWith(SettingName.ForceType))
			{
				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 = new SettingsConverter(value, key).ToFrmSetting();
                    SetComboBoxValue(cbx, text);
                }
			}
            else if (control is TextBox)
			{
				// if setting is readonly.
				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;
			}
		}
Exemplo n.º 8
0
		/// <summary>
		/// Save setting from windows form control to current INI file.
		/// </summary>
		/// <param name="path">path of parameter (related to actual control)</param>
		/// <param name="dstIniSection">if not null then section will be different inside INI file than specified in path</param>
		public bool SaveSetting(Ini ini, string path)
		{
			var control = SettingsMap[path];
			string key = path.Split('\\')[1];
			string v = string.Empty;
            if (key == SettingName.HookMode || key.EndsWith(SettingName.DeviceSubType) || key.EndsWith(SettingName.ForceType))
			{
				var v1 = ((ComboBox)control).SelectedItem;
				if (v1 == null) { v = "0"; }
				else if (v1 is KeyValuePair) { v = ((KeyValuePair)v1).Value; }
				else { v = System.Convert.ToInt32(v1).ToString(); }
			}
			// If di menu strip attached.
			else if (control is ComboBox)
			{
                var cbx = (ComboBox)control;
                if (control.ContextMenuStrip == null)
                {
                    v = control.Text;
                }
                else
                {
                    v = new SettingsConverter(control.Text, key).ToIniSetting();
                    // make sure that disabled button value is "0".
                    if (SettingName.IsButton(key) && string.IsNullOrEmpty(v)) v = "0";
                }
			}
			else if (control is TextBox)
			{
				// if setting is readonly.
				if (key == SettingName.InstanceGuid || key == SettingName.ProductGuid)
				{
					v = string.IsNullOrEmpty(control.Text) ? Guid.Empty.ToString("D") : control.Text;
				}
				else v = control.Text;
			}
			else if (control is ListBox)
			{
				var lbx = (ListBox)control;
				v = string.Join(",", lbx.Items.Cast<string>().ToArray());
			}
			else if (control is NumericUpDown)
			{
				NumericUpDown nud = (NumericUpDown)control;
				v = nud.Value.ToString();
			}
			else if (control is TrackBar)
			{
				TrackBar tc = (TrackBar)control;
				// convert 100%  to 256
				if (key == SettingName.AxisToDPadDeadZone || key == SettingName.AxisToDPadOffset || key == SettingName.LeftTriggerDeadZone || key == SettingName.RightTriggerDeadZone)
				{
					v = System.Convert.ToInt32((float)tc.Value / 100F * 256F).ToString();
				}
				// convert 100%  to 500
				else if (key == SettingName.LeftMotorPeriod || key == SettingName.RightMotorPeriod)
				{
					v = System.Convert.ToInt32((float)tc.Value / 100F * 500F).ToString();
				}
				// Convert 100% to 32767
				else if (key == SettingName.LeftThumbDeadZoneX || key == SettingName.LeftThumbDeadZoneY || key == SettingName.RightThumbDeadZoneX || key == SettingName.RightThumbDeadZoneY)
				{
					v = System.Convert.ToInt32((float)tc.Value / 100F * ((float)Int16.MaxValue)).ToString();
				}
				else v = tc.Value.ToString();
			}
			else if (control is CheckBox)
			{
				CheckBox tc = (CheckBox)control;
				v = tc.Checked ? "1" : "0";
			}
			if (SettingName.IsThumbAxis(key))
			{
				v = v.Replace(SettingName.SType.Axis, "");
			}
			if (SettingName.IsDPad(key)) v = v.Replace(SettingName.SType.DPad, "");
			if (v == "v1") v = "UP";
			if (v == "v2") v = "RIGHT";
			if (v == "v3") v = "DOWN";
			if (v == "v4") v = "LEFT";
			if (v == "")
			{
				if (key == SettingName.DPadUp) v = "UP";
				if (key == SettingName.DPadDown) v = "DOWN";
				if (key == SettingName.DPadLeft) v = "LEFT";
				if (key == SettingName.DPadRight) v = "RIGHT";
			}
			// add comment.
			//var l = SettingName.MaxNameLength - key.Length + 24;
			//v = string.Format("{0, -" + l + "} # {1} Default: '{2}'.", v, SettingName.GetDescription(key), SettingName.GetDefaultValue(key));
			var section = path.Split('\\')[0];
			var padIndex = SettingName.GetPadIndex(path);
			// If this is PAD section then
			if (padIndex > -1)
			{
				section = GetInstanceSection(padIndex);
				// If destination section is empty because controller is not connected then skip.
				if (string.IsNullOrEmpty(section)) return false;
			}
			var oldValue = ini.GetValue(section, key);
			var saved = false;
			if (oldValue != v)
			{
				ini.SetValue(section, key, v);
				saveCount++;
				saved = true;
				if (ConfigSaved != null) ConfigSaved(this, new SettingEventArgs(IniFileName, saveCount));
			}
			// Flush XML too.
			SettingsFile.Current.Save();
			return saved;
		}
Exemplo n.º 9
0
        string GetSettingValue(Control control)
        {
            var    item     = SettingsMap.First(x => x.Control == control);
            var    path     = item.IniPath;
            var    section  = path.Split('\\')[0];
            string key      = path.Split('\\')[1];
            var    padIndex = SettingName.GetPadIndex(path);
            string v        = string.Empty;

            if (key == SettingName.HookMode ||
                key.EndsWith(SettingName.DeviceSubType) ||
                key.EndsWith(SettingName.ForceType) ||
                key.EndsWith(SettingName.LeftMotorDirection) ||
                key.EndsWith(SettingName.RightMotorDirection) ||
                key.EndsWith(SettingName.PassThroughIndex) ||
                key.EndsWith(SettingName.CombinedIndex))
            {
                var v1 = ((ComboBox)control).SelectedItem;
                if (v1 == null)
                {
                    v = "0";
                }
                else if (v1 is KeyValuePair)
                {
                    v = ((KeyValuePair)v1).Value;
                }
                else
                {
                    v = System.Convert.ToInt32(v1).ToString();
                }
            }
            // If di menu strip attached.
            else if (control is ComboBox)
            {
                var cbx = (ComboBox)control;
                if (control.ContextMenuStrip == null)
                {
                    v = control.Text;
                }
                else
                {
                    v = new SettingsConverter(control.Text, key).ToIniValue();
                    // make sure that disabled button value is "0".
                    if (SettingName.IsButton(key) && string.IsNullOrEmpty(v))
                    {
                        v = "0";
                    }
                }
            }
            else if (control is TextBox)
            {
                // if setting is read-only.
                if (key == SettingName.InstanceGuid || key == SettingName.ProductGuid)
                {
                    v = string.IsNullOrEmpty(control.Text) ? Guid.Empty.ToString("D") : control.Text;
                }
                else
                {
                    v = control.Text;
                }
            }
            else if (control is ListBox)
            {
                var lbx = (ListBox)control;
                v = string.Join(",", lbx.Items.Cast <string>().ToArray());
            }
            else if (control is NumericUpDown)
            {
                NumericUpDown nud = (NumericUpDown)control;
                v = nud.Value.ToString();
            }
            else if (control is TrackBar)
            {
                TrackBar tc = (TrackBar)control;
                // convert 100%  to 256
                if (key == SettingName.AxisToDPadDeadZone || key == SettingName.AxisToDPadOffset || key == SettingName.LeftTriggerDeadZone || key == SettingName.RightTriggerDeadZone)
                {
                    v = System.Convert.ToInt32((float)tc.Value / 100F * 256F).ToString();
                }
                // convert 100%  to 500
                else if (key == SettingName.LeftMotorPeriod || key == SettingName.RightMotorPeriod)
                {
                    v = System.Convert.ToInt32((float)tc.Value / 100F * 500F).ToString();
                }
                // Convert 100% to 32767
                else if (key == SettingName.LeftThumbDeadZoneX || key == SettingName.LeftThumbDeadZoneY || key == SettingName.RightThumbDeadZoneX || key == SettingName.RightThumbDeadZoneY)
                {
                    v = System.Convert.ToInt32((float)tc.Value / 100F * ((float)Int16.MaxValue)).ToString();
                }
                else
                {
                    v = tc.Value.ToString();
                }
            }
            else if (control is CheckBox)
            {
                CheckBox tc = (CheckBox)control;
                v = tc.Checked ? "1" : "0";
            }
            else if (control is DataGridView)
            {
                var grid      = (DataGridView)control;
                var data      = grid.Rows.Cast <DataGridViewRow>().Where(x => x.Visible).Select(x => x.DataBoundItem as Setting).Where(x => x != null).ToArray();
                var instances = data.Select(x => string.Format("IG_{0:N}", x.InstanceGuid).ToUpper()).ToArray();
                v = string.Join(",", instances);
            }
            if (SettingName.IsThumbAxis(key))
            {
                v = v.Replace(SettingName.SType.Axis, "");
            }
            // If this is DPad setting then remove prefix.
            if (key == SettingName.DPad)
            {
                v = v.Replace(SettingName.SType.DPad, "");
            }
            return(v);
        }