Ejemplo n.º 1
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();
        }
 void OnMenuOpened()
 {
     SettingsMenu.UpdateMenuBasedOnCVars();
     SettingsMenu.ResetValidStateForAllEntries();
 }
Ejemplo n.º 3
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);
            }
        }