コード例 #1
0
        public static float FieldSlider(float value, float increment, float incrementLarge, Vector2 limits, string name, out bool changed, Color backgroundColor, int valueType, bool allowFine = true)
        {
            if (!WingProceduralManager.uiStyleConfigured)
            {
                WingProceduralManager.ConfigureStyles();
            }
            GUILayout.BeginHorizontal();
            double range = limits.y - limits.x;
            double value01 = (value - limits.x) / range; // rescaling value to be 0-100% of range for convenience
            double increment01 = increment / range;
            double valueOld = value01;
            float  buttonWidth = 12, spaceWidth = 3;

            GUILayout.Label("", WingProceduralManager.uiStyleLabelHint);
            Rect rectLast        = GUILayoutUtility.GetLastRect();
            Rect rectSlider      = new Rect(rectLast.xMin + buttonWidth + spaceWidth, rectLast.yMin, rectLast.width - 2 * (buttonWidth + spaceWidth), rectLast.height);
            Rect rectSliderValue = new Rect(rectSlider.xMin, rectSlider.yMin, rectSlider.width * (float)value01, rectSlider.height - 3f);
            Rect rectButtonL     = new Rect(rectLast.xMin, rectLast.yMin, buttonWidth, rectLast.height);
            Rect rectButtonR     = new Rect(rectLast.xMin + rectLast.width - buttonWidth, rectLast.yMin, buttonWidth, rectLast.height);
            Rect rectLabelValue  = new Rect(rectSlider.xMin + rectSlider.width * 0.75f, rectSlider.yMin, rectSlider.width * 0.25f, rectSlider.height);

            if (GUI.Button(rectButtonL, "", WingProceduralManager.uiStyleButton))
            {
                if (Input.GetMouseButtonUp(0) || !allowFine)
                {
                    value01 -= incrementLarge / range;
                }
                else if (Input.GetMouseButtonUp(1) && allowFine)
                {
                    value01 -= increment01;
                }
            }
            if (GUI.Button(rectButtonR, "", WingProceduralManager.uiStyleButton))
            {
                if (Input.GetMouseButtonUp(0) || !allowFine)
                {
                    value01 += incrementLarge / range;
                }
                else if (Input.GetMouseButtonUp(1) && allowFine)
                {
                    value01 += increment01;
                }
            }

            if (rectLast.Contains(Event.current.mousePosition) && (Event.current.type == EventType.MouseDrag || Event.current.type == EventType.MouseDown) && // right click drag doesn't work properly without the event check
                Event.current.type != EventType.MouseUp)        // drag event covers this, but don't want it to
            {
                value01 = GUI.HorizontalSlider(rectSlider, (float)value01, 0f, 1f, WingProceduralManager.uiStyleSlider, WingProceduralManager.uiStyleSliderThumb);

                if (valueOld != value01)
                {
                    if (Input.GetMouseButton(0) || !allowFine) // normal control
                    {
                        double excess = value01 / increment01;
                        value01 -= (excess - Math.Round(excess)) * increment01;
                    }
                    else if (Input.GetMouseButton(1) && allowFine) // fine control
                    {
                        double excess = valueOld / increment01;
                        value01 = (valueOld - (excess - Math.Round(excess)) * increment01) + Math.Min(value01 - 0.5, 0.4999) * increment01;
                    }
                }
            }
            else
            {
                GUI.HorizontalSlider(rectSlider, (float)value01, 0f, 1f, WingProceduralManager.uiStyleSlider, WingProceduralManager.uiStyleSliderThumb);
            }

            value   = Mathf.Clamp((float)(value01 * range + limits.x), Mathf.Min((float)(limits.x * 0.5), limits.x), limits.y); // lower limit is halved so the fine control can reduce it further but the normal tweak still snaps. Min makes -ve values work
            changed = valueOld != value ? true : false;

            GUI.DrawTexture(rectSliderValue, backgroundColor.GetTexture2D());
            GUI.Label(rectSlider, "  " + name, WingProceduralManager.uiStyleLabelHint);
            GUI.Label(rectLabelValue, GetValueTranslation(value, valueType), WingProceduralManager.uiStyleLabelHint);

            GUILayout.EndHorizontal();
            return(value);
        }
コード例 #2
0
        public static float IntegerSlider(float value, float increment, float incrementLarge, int min, int max, string name, out bool changed, Color backgroundColor, int valueType)
        {
            if (!WingProceduralManager.uiStyleConfigured)
            {
                WingProceduralManager.ConfigureStyles();
            }
            GUILayout.BeginHorizontal();
            int    range = max - min;
            double value01 = (value - min) / range;
            double increment01 = 1 / range;
            double valueOld = value01;
            float  buttonWidth = 12, spaceWidth = 3;

            GUILayout.Label("", WingProceduralManager.uiStyleLabelHint);
            Rect rectLast        = GUILayoutUtility.GetLastRect();
            Rect rectSlider      = new Rect(rectLast.xMin + buttonWidth + spaceWidth, rectLast.yMin, rectLast.width - 2 * (buttonWidth + spaceWidth), rectLast.height);
            Rect rectSliderValue = new Rect(rectSlider.xMin, rectSlider.yMin, rectSlider.width * (float)value01, rectSlider.height - 3f);
            Rect rectButtonL     = new Rect(rectLast.xMin, rectLast.yMin, buttonWidth, rectLast.height);
            Rect rectButtonR     = new Rect(rectLast.xMin + rectLast.width - buttonWidth, rectLast.yMin, buttonWidth, rectLast.height);
            Rect rectLabelValue  = new Rect(rectSlider.xMin + rectSlider.width * 0.75f, rectSlider.yMin, rectSlider.width * 0.25f, rectSlider.height);

            //Debug.Log("Slider created.");

            if (GUI.Button(rectButtonL, "", WingProceduralManager.uiStyleButton))
            {
                value01 = 0;
            }
            if (GUI.Button(rectButtonR, "", WingProceduralManager.uiStyleButton))
            {
                value01 = 1;
            }

            if (rectLast.Contains(Event.current.mousePosition) && (Event.current.type == EventType.MouseDrag || Event.current.type == EventType.MouseDown) && // right click drag doesn't work properly without the event check
                Event.current.type != EventType.MouseUp)        // drag event covers this, but don't want it to
            {
                value01 = GUI.HorizontalSlider(rectSlider, (float)value01, 0f, 1f, WingProceduralManager.uiStyleSlider, WingProceduralManager.uiStyleSliderThumb);

                /*   if (valueOld != value01)
                 * {
                 *     if (Input.GetMouseButton(0) || !allowFine) // normal control
                 *     {
                 *         double excess = value01 / increment01;
                 *         value01 -= (excess - Math.Round(excess)) * increment01;
                 *         //Debug.Log("Normal: value01 => " + value01);
                 *     }
                 *     else if (Input.GetMouseButton(1) && allowFine) // fine control
                 *     {
                 *         double excess = valueOld / increment01;
                 *         value01 = (valueOld - (excess - Math.Round(excess)) * increment01) + Math.Min(value01 - 0.5, 0.4999) * increment01;
                 *         //Debug.Log("Fine: value01 => " + value01);
                 *     }
                 * }*/
            }
            else
            {
                GUI.HorizontalSlider(rectSlider, (float)value01, 0f, 1f, WingProceduralManager.uiStyleSlider, WingProceduralManager.uiStyleSliderThumb);
            }


            value = Mathf.Round((float)value01 * range) + min; // lower limit is halved so the fine control can reduce it further but the normal tweak still snaps. Min makes -ve values work
            //value = (float)(value01 * range + limits.x);  //releases clamp
            changed = valueOld != value ? true : false;

            GUI.DrawTexture(rectSliderValue, backgroundColor.GetTexture2D());
            GUI.Label(rectSlider, "  " + name, WingProceduralManager.uiStyleLabelHint);
            GUI.Label(rectLabelValue, GetValueTranslation(value, valueType), WingProceduralManager.uiStyleLabelHint);

            GUILayout.EndHorizontal();
            //Vector2 value1 = new Vector2(value, delta);
            //Debug.Log("B9PW: Value changed to " + value + ", delta = " + delta);  //log it
            //return value1;
            return(value);
        }