Exemplo n.º 1
0
        public static Control[] LoadFromString(Control[] schemeToReplace, string loadString)
        {
            //we pass the existing control scheme so that info on needed common bindings can be kept

            //create our new controls list, and add any common bindings we might need to load later when pads are swapped in/out
            List <Control> controls = new List <Control>();

            for (int c = 0; c < schemeToReplace.Length; c++)
            {
                controls.Add(new Control(schemeToReplace[c].name));
                controls[c].commonMappings = new List <CommonGamepadInputs>();
                for (int b = 0; b < schemeToReplace[c].commonMappings.Count; b++)
                {
                    controls[c].commonMappings.Add(schemeToReplace[c].commonMappings[b]);
                }
            }

            //now add saved data to the list (and to other places)
            string[] loadLines = loadString.Split('\n');
            joysticks = Input.GetJoystickNames();
            for (int l = 0; l < loadLines.Length; l++)
            {
                string[] thisLine = loadLines[l].Split(seperator[0]);

                if (thisLine[0] == "controls")
                {
                    int savedControlCount = 0;
                    if (int.TryParse(thisLine[1], out savedControlCount))
                    {
                        for (int c = 0; c < savedControlCount; c++)
                        {
                            l++;
                            //get the string that describes this control
                            thisLine = loadLines[l].Split(seperator[0]);
                            //build the control
                            //Control currentControl = new Control(thisLine[0]);
                            //currentControl.isToggle = thisLine[1] == "True";


                            int currentControlIndex = -1;
                            for (int cc = 0; cc < controls.Count; cc++)
                            {
                                if (controls[cc].name == thisLine[0])
                                {
                                    currentControlIndex = cc;
                                }
                            }
                            if (currentControlIndex == -1)
                            {
                                controls.Add(new Control(thisLine[0]));
                                currentControlIndex = controls.Count - 1;
                            }
                            controls[currentControlIndex].isToggle = thisLine[1] == "True";

                            int savedInputCount = int.Parse(thisLine[2]);

                            //add inputs

                            for (int i = 0; i < savedInputCount; i++)
                            {
                                l++;
                                //get the string that describes this input
                                thisLine = loadLines[l].Split(seperator[0]);
                                //build the input
                                DeviceInput currentInput = new DeviceInput((InputDeviceType)Enum.Parse(typeof(InputDeviceType), thisLine[0]));

                                currentInput.displayName = thisLine[1];
                                currentInput.isCustom    = thisLine[2] == "True";
                                currentInput.deviceName  = thisLine[3];

                                if (currentInput.inputType == InputDeviceType.Keyboard)
                                {
                                    currentInput.keyboardKeyCode = (KeyCode)Enum.Parse(typeof(KeyCode), thisLine[4]);
                                }
                                if (currentInput.inputType == InputDeviceType.Mouse)
                                {
                                    currentInput.mouseInputType = (MouseInputType)Enum.Parse(typeof(MouseInputType), thisLine[4]);
                                }
                                if (currentInput.inputType == InputDeviceType.GamepadButton)
                                {
                                    currentInput.gamepadButtonNumber = int.Parse(thisLine[4]);
                                }
                                if (currentInput.inputType == InputDeviceType.GamepadAxis)
                                {
                                    currentInput.gamepadAxisNumber = int.Parse(thisLine[4]);
                                    currentInput.invertAxis        = thisLine[5] == "True";
                                    currentInput.clampAxis         = thisLine[6] == "True";
                                    currentInput.rescaleAxis       = thisLine[7] == "True";
                                    currentInput.rescaleAxisMin    = float.Parse(thisLine[8]);
                                    currentInput.rescaleAxisMax    = float.Parse(thisLine[9]);
                                }
                                if (currentInput.inputType == InputDeviceType.Virtual)
                                {
                                    currentInput.virtualInputID = thisLine[4];
                                }

                                //lets not forget stuff that isn't saved, but needed anyway
                                currentInput.commonMappingType = CommonGamepadInputs.NOBUTTON;
                                currentInput.defaultAxisValue  = 0f;
                                if (currentInput.inputType == InputDeviceType.GamepadAxis || currentInput.inputType == InputDeviceType.GamepadButton)
                                {
                                    List <int> allowedSlots = new List <int>();
                                    for (int j = 0; j < joysticks.Length; j++)
                                    {
                                        if (joysticks[j].ToUpper() == currentInput.deviceName.ToUpper())
                                        {
                                            allowedSlots.Add(j);
                                        }
                                    }
                                    currentInput.allowedSlots = allowedSlots.ToArray();
                                }


                                //add the input to the control's input list
                                controls[currentControlIndex].inputs.Add(currentInput);
                            }

                            //add this control to the list
                            //controls.Add(currentControl);
                        }
                    }
                    else
                    {
                        Debug.LogError("Can't read number of saved controls.");
                    }
                }

                if (thisLine[0] == "smartcontrols")
                {
                    int savedSmartControlCount = 0;
                    if (int.TryParse(thisLine[1], out savedSmartControlCount))
                    {
                        for (int c = 0; c < savedSmartControlCount; c++)
                        {
                            l++;
                            string currentSmartControl = loadLines[l];

                            //get the inversion settings
                            l++;
                            thisLine = loadLines[l].Split(seperator[0]);
                            for (int i = 0; i < thisLine.Length; i++)
                            {
                                Sinput.SetInverted(currentSmartControl, thisLine[i] == "True", (InputDeviceSlot)i);
                            }

                            //get the scale settings
                            l++;
                            thisLine = loadLines[l].Split(seperator[0]);
                            for (int i = 0; i < thisLine.Length; i++)
                            {
                                Sinput.SetScale(currentSmartControl, float.Parse(thisLine[i]), (InputDeviceSlot)i);
                            }
                        }
                    }
                    else
                    {
                        Debug.LogError("Can't read number of saved smart controls.");
                    }
                }

                if (thisLine[0] == "mouseSensitivity")
                {
                    Sinput.mouseSensitivity = float.Parse(thisLine[1]);
                }
            }


            return(controls.ToArray());
        }