Esempio n. 1
0
        /// <summary>
        /// Returns list of indexes of values which are not valid. Returns empty list if all values are correct.
        /// </summary>
        public static List <int> AreSettingsValid(List <CVar> cvars, List <object> values)
        {
            var invalidValuesIndexes = new List <int> ();

            for (int i = 0; i < cvars.Count; i++)
            {
                if (!CVarManager.IsCVarValueValid(cvars [i], values [i]))
                {
                    invalidValuesIndexes.Add(i);
                }
            }

            return(invalidValuesIndexes);
        }
Esempio n. 2
0
        void AddCVars()
        {
            CVar cvar = new CVar();

            cvar.name     = "FFA";
            cvar.getValue = () => FFA;
            cvar.setValue = (arg) => FFA = (bool)arg;

            CVarManager.AddCVar(cvar);

            cvar = new CVar();

            cvar.name     = "Friendly fire";
            cvar.getValue = () => FriendlyFire;
            cvar.setValue = (arg) => FriendlyFire = (bool)arg;

            CVarManager.AddCVar(cvar);
        }
Esempio n. 3
0
        public void SaveSettings()
        {
            if (null == this.settingsMenuContent)
            {
                return;
            }

            // Save settings.


            // reset valid states
            SettingsMenu.ResetValidStateForAllEntries();


            // Obtain all values from menu and see which ones changed.

            var cvarsToChange        = new List <CVar> ();
            var changedValues        = new List <object> ();
            var changedValuesIndexes = new List <int> ();

            int i = 0;

            foreach (var entry in SettingsMenu.GetEntries())
            {
                var cvar             = entry.cvar;
                var currentCvarValue = CVarManager.GetCVarValue(cvar);
                var editedValue      = entry.editedValue;

                // compare with current value
                if (!currentCvarValue.Equals(editedValue))
                {
                    // value is changed
                    cvarsToChange.Add(cvar);
                    changedValues.Add(editedValue);
                    changedValuesIndexes.Add(i);
                }

                i++;
            }


            // Check if all settings are correct, and if they are, save them, otherwise show user which settings are not valid.

            var invalidValuesIndexes = SettingsMenu.AreSettingsValid(cvarsToChange, changedValues);

            if (invalidValuesIndexes.Count > 0)
            {
                // notify user which settings are invalid

                var entries = new List <SettingsMenu.Entry> (SettingsMenu.GetEntries());

                foreach (var index in invalidValuesIndexes)
                {
                    int cvarIndex = changedValuesIndexes [index];

                    //entries [cvarIndex].control.transform.GetComponent<Image> ().color = Color.red;
                    SettingsMenu.SetEntryValidState(entries [cvarIndex], false);
                }
            }
            else
            {
                // settings are correct

                // apply new values

                //					foreach (CVar cvar in CVarManager.CVars) {
                //
                //						if( cvar.isInsideCfg ) {
                //							bool isChanged = false ;
                //
                //							if( cvar.displayType == CVarDisplayType.String ) {
                //								if( PlayerPrefs.GetString( cvar.cfgName ) != cvar.currentString ) {
                //									isChanged = true ;
                //
                //									cvarsToChange.Add (cvar);
                //									changedValues.Add (cvar.currentString);
                //								//	CVarManager.ChangeCVarValue( cvar, var.currentString);
                //								}
                //
                //							//	PlayerPrefs.SetString( var.name, var.currentString );
                //
                //							} else if( cvar.displayType == CVarDisplayType.FloatSlider ) {
                //								if( PlayerPrefs.GetFloat( cvar.cfgName ) != cvar.currentFloat ) {
                //									isChanged = true ;
                //
                //									cvarsToChange.Add (cvar);
                //									changedValues.Add (cvar.currentFloat);
                //								//	CVarManager.ChangeCVarValue( cvar, var.currentFloat);
                //								}
                //
                //							//	PlayerPrefs.SetFloat( var.name, var.currentFloat );
                //							}
                //
                //						}
                //
                //					}

                CVarManager.ChangeCVars(cvarsToChange.ToArray(), changedValues.ToArray());

                CVarManager.SaveCVarsToDisk();

                string str = "Successfully saved " + cvarsToChange.Count + " cvars: ";
                foreach (var cvar in cvarsToChange)
                {
                    str += cvar.name + " ";
                }
                Debug.Log(str);

                MenuManager.singleton.OpenParentMenu();
            }
        }
Esempio n. 4
0
        private void    DrawOptionsMenu()
        {
            int areaWidth    = Screen.width / 7 * 3;            // 90 (640), 200 (1366), 270 (1920)
            int areaHeight   = Screen.height / 2;               // 240 (480), 380 (768), 540 (1080)
            int buttonHeight = (int)(35.0f * Screen.height / 650.0f / 1.5f);
            int x            = Screen.width / 2 - areaWidth / 2;
            int y            = Screen.height / 2 - areaHeight / 2;


            // Draw box as background.
            int box_offset_x = 30;
            int box_offset_y = 20;

            GUI.Box(new Rect(x - box_offset_x, y - box_offset_y, areaWidth + 2 * box_offset_x, areaHeight + 2 * box_offset_y), "OPTIONS MENU");


            GUILayout.BeginArea(new Rect(x, y, areaWidth, areaHeight));


            this.optionsMenuScrollBarPosition = GUILayout.BeginScrollView(this.optionsMenuScrollBarPosition);

            // display controls to edit cvars

            var cvarsToChange = new List <CVar> ();
            var changedValues = new List <object> ();

            foreach (var cvar in CVarManager.CVars)
            {
                // display name
                string s = cvar.displayName;
                if (s.Length < 1)
                {
                    s = cvar.name;
                }
                GUILayout.Label(s);

                var    currentCvarValue = CVarManager.GetCVarValue(cvar);
                object editedValue      = null;

                var displayType = SettingsMenu.GetCVarDisplayType(cvar);
                if (displayType == CVarDisplayType.String)
                {
                    editedValue = GUILayout.TextField((string)currentCvarValue, cvar.maxLength);
                }
                else if (displayType == CVarDisplayType.FloatSlider)
                {
                    // display current value
                    GUILayout.Label(currentCvarValue.ToString());
                    // display slider
                    editedValue = GUILayout.HorizontalSlider((float)currentCvarValue, cvar.minValue, cvar.maxValue);
                }

                // compare with current value
                if (!currentCvarValue.Equals(editedValue))
                {
                    // value is changed
                    cvarsToChange.Add(cvar);
                    changedValues.Add(editedValue);
                }
            }

            GUILayout.EndScrollView();

            GUILayout.Space(20);

            GUILayout.BeginHorizontal();

            // OK button
            //	if (GUILayout.Button ("Save", GUILayout.Width (50), GUILayout.Height (20))) {
            if (GameManager.DrawButtonWithCalculatedSize("Save"))
            {
            }

            // Cancel button
            //	if (GUILayout.Button ("Cancel", GUILayout.Width (60), GUILayout.Height (20))) {
            if (GameManager.DrawButtonWithCalculatedSize("Cancel"))
            {
                // populate options window with current settings, and exit options menu.

                //	CVarManager.ReadCVarsFromPlayerPrefs ();

                MenuManager.singleton.OpenParentMenu();
            }

            GUILayout.FlexibleSpace();

            //	if (GUILayout.Button ("Reset to defaults", GUILayout.Width (100), GUILayout.Height (20))) {
            if (GameManager.DrawButtonWithCalculatedSize("Reset to defaults"))
            {
                //	CVarManager.ResetAllCVarsToDefaultValues ();

                //	this.UpdateMenuBasedOnCVars ();
            }

            GUILayout.EndHorizontal();


            GUILayout.EndArea();
        }
Esempio n. 5
0
        private static IEnumerable <Entry> GetEntries(bool create, bool update)
        {
            if (null == singleton.settingsMenuScrollViewContent)
            {
                yield break;
            }

            int i = 0;

            if (create)
            {
                // delete all children
                for (i = 0; i < singleton.settingsMenuScrollViewContent.transform.childCount; i++)
                {
                    Destroy(singleton.settingsMenuScrollViewContent.transform.GetChild(i));
                }
            }

            i = 0;

            foreach (var cvar in CVarManager.CVars)
            {
                Entry entry = new Entry();

                entry.cvar = cvar;

                var cvarValue = CVarManager.GetCVarValue(cvar);

                //	var childLabel = singleton.settingsMenuScrollViewContent.transform.GetChild (i);
                Transform childControl = null;
                Text      label        = null;

                if (create)
                {
                    // create label
                    label      = CreateChild(singleton.labelPrefab, singleton.settingsMenuScrollViewContent.transform).GetComponentInChildren <Text> ();
                    label.text = cvar.displayName;
                }
                else
                {
                    // elements are already created => we can obtain them from transform
                    label        = singleton.settingsMenuScrollViewContent.transform.GetChild(i).GetComponentInChildren <Text>();
                    childControl = singleton.settingsMenuScrollViewContent.transform.GetChild(i + 1);
                }

                i += 3;                 // also skip empty space

                object editedValue = null;

                var displayType = SettingsMenu.GetCVarDisplayType(cvar);

                if (displayType == CVarDisplayType.String || displayType == CVarDisplayType.FloatTextBox || displayType == CVarDisplayType.IntegerTextBox)
                {
                    InputField inputField = null;

                    if (create)
                    {
                        // create input field

                        childControl = CreateChild(singleton.inputFieldPrefab, singleton.settingsMenuScrollViewContent.transform).transform;

                        inputField = childControl.GetComponentInChildren <InputField> ();
                        if (cvar.maxLength > 0)
                        {
                            inputField.characterLimit = cvar.maxLength;
                        }
                        switch (displayType)
                        {
                        case CVarDisplayType.FloatTextBox:
                            inputField.contentType = InputField.ContentType.DecimalNumber;
                            break;

                        case CVarDisplayType.IntegerTextBox:
                            inputField.contentType = InputField.ContentType.IntegerNumber;
                            break;
                        }
                    }

                    inputField = childControl.GetComponentInChildren <InputField> ();

                    if (update)
                    {
                        inputField.text = cvarValue.ToString();
                    }

                    entry.control = inputField;

                    // get current value
                    switch (displayType)
                    {
                    case CVarDisplayType.String:
                        editedValue = inputField.text;
                        break;

                    case CVarDisplayType.FloatTextBox:
                        float floatValue;
                        if (float.TryParse(inputField.text, out floatValue))
                        {
                            editedValue = floatValue;
                        }
                        break;

                    case CVarDisplayType.IntegerTextBox:
                        int intValue;
                        if (int.TryParse(inputField.text, out intValue))
                        {
                            editedValue = intValue;
                        }
                        break;
                    }
                }
                else if (displayType == CVarDisplayType.FloatSlider || displayType == CVarDisplayType.IntegerSlider)
                {
                    Slider slider = null;

                    if (create)
                    {
                        // create slider

                        childControl = CreateChild(singleton.sliderPrefab, singleton.settingsMenuScrollViewContent.transform).transform;

                        slider              = childControl.GetComponentInChildren <Slider> ();
                        slider.minValue     = cvar.minValue;
                        slider.maxValue     = cvar.maxValue;
                        slider.wholeNumbers = (displayType == CVarDisplayType.IntegerSlider);

                        // add script for updating label
                        var updater = slider.gameObject.AddComponent <SettingsMenuSliderLabelUpdate> ();
                        updater.cvarName = cvar.displayName;
                        updater.label    = label;
                    }

                    slider = childControl.GetComponentInChildren <Slider> ();

                    if (update)
                    {
                        if (displayType == CVarDisplayType.FloatSlider)
                        {
                            slider.value = (float)cvarValue;
                        }
                        else
                        {
                            slider.value = (int)cvarValue;
                        }

                        // also add current value to label
                        label.text = cvar.displayName + " : " + cvarValue.ToString();
                    }

                    entry.control = slider;

                    // get current value
                    switch (displayType)
                    {
                    case CVarDisplayType.FloatSlider:
                        editedValue = (float)slider.value;
                        break;

                    case CVarDisplayType.IntegerSlider:
                        editedValue = (int)slider.value;
                        break;
                    }
                }
                else if (displayType == CVarDisplayType.Boolean)
                {
                    // toggle

                    if (create)
                    {
                        childControl = CreateChild(singleton.togglePrefab, singleton.settingsMenuScrollViewContent.transform).transform;

                        // reduce label height, and set text of toggle label
                        label.text = "";
                        label.rectTransform.SetNormalizedRectAndAdjustAnchors(Rect.zero);

                        childControl.GetComponentInChildren <Text> ().text = " " + cvar.displayName;
                    }

                    var toggle = childControl.GetComponentInChildren <Toggle> ();

                    if (update)
                    {
                        toggle.isOn = (bool)cvarValue;
                    }

                    entry.control = toggle;

                    // get current value
                    editedValue = toggle.isOn;
                }


                if (create)
                {
                    // create empty space
                    CreateChild(singleton.emptySpacePrefab, singleton.settingsMenuScrollViewContent.transform);
                }


                entry.child       = childControl;
                entry.label       = label;
                entry.editedValue = editedValue;

                if (create)
                {
                    // add script which will remember original image color
                    entry.control.transform.gameObject.AddComponentIfDoesntExist <SettingsMenuEntryScript> ();
                }


                yield return(entry);
            }
        }
Esempio n. 6
0
        void AddCvars()
        {
            CVar cvar = new CVar();

            cvar.name     = "nick";
            cvar.getValue = () => Nick;
            cvar.setValue = (arg) => Nick = (string)arg;
            cvar.isValid  = (arg) => PlayerManager.IsValidPlayerName((string)arg);

            CVarManager.AddCVar(cvar);

            cvar          = new CVar();
            cvar.name     = "fps_max";
            cvar.minValue = 1;
            cvar.maxValue = 100;
            cvar.getValue = () => Fps_max;
            cvar.setValue = (arg) => Fps_max = (int)arg;

            CVarManager.AddCVar(cvar);

            if (this.registerInputCvars)
            {
                cvar          = new CVar();
                cvar.name     = "mouse_sensitivity_x";
                cvar.minValue = 0;
                cvar.maxValue = 2000;
                cvar.getValue = () => Mouse_sensitivity_x;
                cvar.setValue = (arg) => Mouse_sensitivity_x = (float)arg;

                CVarManager.AddCVar(cvar);

                cvar          = new CVar();
                cvar.name     = "mouse_sensitivity_y";
                cvar.minValue = 0;
                cvar.maxValue = 2000;
                cvar.getValue = () => Mouse_sensitivity_y;
                cvar.setValue = (arg) => Mouse_sensitivity_y = (float)arg;

                CVarManager.AddCVar(cvar);

                cvar          = new CVar();
                cvar.name     = "Accelerometer minimum horizontal value";
                cvar.minValue = 0;
                cvar.maxValue = 1;
                cvar.getValue = () => MinAccHorizontalValue;
                cvar.setValue = (arg) => MinAccHorizontalValue = (float)arg;

                CVarManager.AddCVar(cvar);

                cvar          = new CVar();
                cvar.name     = "Accelerometer minimum vertical value";
                cvar.minValue = 0;
                cvar.maxValue = 1;
                cvar.getValue = () => MinAccVerticalValue;
                cvar.setValue = (arg) => MinAccVerticalValue = (float)arg;

                CVarManager.AddCVar(cvar);

                cvar          = new CVar();
                cvar.name     = "Accelerometer vertical offset";
                cvar.minValue = 0;
                cvar.maxValue = 1;
                cvar.getValue = () => AccVerticalOffset;
                cvar.setValue = (arg) => AccVerticalOffset = (float)arg;

                CVarManager.AddCVar(cvar);
            }
        }