private void Do2DBlend(Rect r, SerializedProperty property, Blend2DAttribute b2D)
        {
            using (new GUI.GroupScope(r))
            {
                Rect blendRect = new Rect(Vector2.zero, Vector2.one * blendBoxSize);

                Event e = Event.current;
                if (e.isMouse && e.button == 0 && e.rawType == EventType.MouseDown)
                {
                    if (blendRect.Contains(e.mousePosition))
                    {
                        hotControl            = GUIUtility.GetControlID(hash, FocusType.Passive, blendRect);
                        GUIUtility.hotControl = hotControl;
                        e.Use();
                    }
                }

                if (e.isMouse && e.rawType == EventType.MouseUp)
                {
                    switch (e.button)
                    {
                    case 0:
                        hotControl            = -1;
                        GUIUtility.hotControl = 0;
                        e.Use();
                        break;

                    case 1:
                        if (!blendRect.Contains(e.mousePosition))
                        {
                            break;
                        }
                        GenericMenu menu = new GenericMenu();

                        menu.AddItem(new GUIContent("Center"), false, () =>
                        {
                            property.vector2Value = Vector2.Lerp(b2D.Min, b2D.Max, 0.5f);
                            property.serializedObject.ApplyModifiedProperties();
                        });

                        menu.ShowAsContext();
                        e.Use();
                        break;
                    }
                }

                if (GUIUtility.hotControl == hotControl)
                {
                    if (e.isMouse)
                    {
                        property.vector2Value =
                            Lerp(
                                b2D.Min,
                                b2D.Max,
                                new Vector2(e.mousePosition.x / blendBoxSize, (blendBoxSize - e.mousePosition.y) / blendBoxSize));
                        e.Use();
                    }
                }

                using (new GUI.GroupScope(blendRect, EditorStyles.helpBox))
                {
                    if (e.type == EventType.Repaint)
                    {
                        GL.Begin(Application.platform == RuntimePlatform.WindowsEditor ? GL.QUADS : GL.LINES);
                        ApplyWireMaterial.Invoke(null, new object[] { CompareFunction.Always });
                        const float quarter       = 0.25f * blendBoxSize;
                        const float half          = quarter * 2;
                        const float threeQuarters = half + quarter;
                        float       lowGreyVal    = EditorGUIUtility.isProSkin ? 0.25f : 0.75f;
                        Color       lowGrey       = new Color(lowGreyVal, lowGreyVal, lowGreyVal);
                        DrawLineFast(new Vector2(quarter, 0), new Vector2(quarter, blendBoxSize), lowGrey);
                        DrawLineFast(new Vector2(quarter, 0), new Vector2(quarter, blendBoxSize), lowGrey);
                        DrawLineFast(new Vector2(threeQuarters, 0), new Vector2(threeQuarters, blendBoxSize), lowGrey);
                        DrawLineFast(new Vector2(0, quarter), new Vector2(blendBoxSize, quarter), lowGrey);
                        DrawLineFast(new Vector2(0, threeQuarters), new Vector2(blendBoxSize, threeQuarters), lowGrey);
                        DrawLineFast(new Vector2(half, 0), new Vector2(half, blendBoxSize), Color.grey);
                        DrawLineFast(new Vector2(0, half), new Vector2(blendBoxSize, half), Color.grey);
                        GL.End();

                        GUI.Label(new Rect(0, blendBoxSize - 15, blendBoxSize, 15), b2D.XLabel, EditorStyles.centeredGreyMiniLabel);
                        Matrix4x4 matrixP = GUI.matrix;
                        GUIUtility.RotateAroundPivot(-90, new Vector2(0, blendBoxSize));
                        GUI.Label(new Rect(0, blendBoxSize - 15, blendBoxSize, 15), b2D.YLabel, EditorStyles.centeredGreyMiniLabel);
                        GUI.matrix = matrixP;

                        GL.Begin(Application.platform == RuntimePlatform.WindowsEditor ? GL.QUADS : GL.LINES);
                        ApplyWireMaterial.Invoke(null, new object[] { CompareFunction.Always });
                        Vector2 circlePos = InverseLerp(b2D.Min, b2D.Max, property.vector2Value);
                        circlePos.y = 1 - circlePos.y;
                        circlePos  *= blendBoxSize;
                        DrawCircleFast(circlePos, blendBoxSize * 0.04f, 2, new Color(1, 0.5f, 0));
                        GL.End();
                    }
                }

                Vector2 v = property.vector2Value;
                using (EditorGUI.ChangeCheckScope cC = new EditorGUI.ChangeCheckScope())
                {
                    Rect labelRect = new Rect(blendBoxSize + 5, blendBoxSize / 2f - 50, Screen.width - blendBoxSize - 5, EditorGUIUtility.singleLineHeight);
                    EditorGUI.LabelField(labelRect, property.displayName, EditorStyles.boldLabel);
                    labelRect.y += 20;
                    EditorGUI.LabelField(labelRect, b2D.XLabel);
                    labelRect.y += EditorGUIUtility.singleLineHeight;
                    v.x          = EditorGUI.DelayedFloatField(labelRect, v.x);
                    labelRect.y += EditorGUIUtility.singleLineHeight;
                    EditorGUI.LabelField(labelRect, b2D.YLabel);
                    labelRect.y += EditorGUIUtility.singleLineHeight;
                    v.y          = EditorGUI.DelayedFloatField(labelRect, v.y);
                    if (cC.changed)
                    {
                        property.vector2Value = new Vector2(Mathf.Clamp(v.x, b2D.Min.x, b2D.Max.x), Mathf.Clamp(v.y, b2D.Min.y, b2D.Max.y));
                    }
                }
            }
        }
Exemple #2
0
    private void Do2DBlend(Rect r, SerializedProperty sP, Blend2DAttribute nP)
    {
        using (new GUI.GroupScope(r))
        {
            Rect blendRect = new Rect(Vector2.zero, Vector2.one * blendBoxSize);

            Event e = Event.current;
            if (e.isMouse && e.button == 0 && (e.rawType == EventType.MouseDown || e.rawType == EventType.MouseDrag))
            {
                if (blendRect.Contains(e.mousePosition))
                {
                    sP.vector2Value = new Vector2(Mathf.Clamp(e.mousePosition.x, 0, blendBoxSize),
                                                  Mathf.Clamp(blendBoxSize - e.mousePosition.y, 0, blendBoxSize)) / blendBoxSize;
                    e.Use();
                }
            }
            Vector2 v = sP.vector2Value;
            using (EditorGUI.ChangeCheckScope cC = new EditorGUI.ChangeCheckScope())
            {
                Rect labelRect = new Rect(blendBoxSize + 5, blendBoxSize / 2f - 50, Screen.width - blendBoxSize - 5, 15);
                EditorGUI.LabelField(labelRect, sP.displayName, EditorStyles.boldLabel);
                labelRect.y += 20;
                EditorGUI.LabelField(labelRect, nP.xLabel);
                labelRect.y += 15;
                v.x          = EditorGUI.FloatField(labelRect, v.x);
                labelRect.y += 15;
                EditorGUI.LabelField(labelRect, nP.yLabel);
                labelRect.y += 15;
                v.y          = EditorGUI.FloatField(labelRect, v.y);
                if (cC.changed)
                {
                    sP.vector2Value = new Vector2(Mathf.Clamp01(v.x), Mathf.Clamp01(v.y));
                }
            }

            using (new GUI.GroupScope(blendRect))
            {
                GL.Begin(Application.platform == RuntimePlatform.WindowsEditor ? GL.QUADS : GL.LINES);
                ApplyWireMaterial.Invoke(null, new object[] { CompareFunction.Always });
                const float quarter       = 0.25f * blendBoxSize;
                const float half          = quarter * 2;
                const float threeQuarters = half + quarter;
                float       lowGreyVal    = EditorGUIUtility.isProSkin ? 0.25f : 0.75f;
                Color       lowGrey       = new Color(lowGreyVal, lowGreyVal, lowGreyVal);
                DrawLineFast(new Vector2(quarter, 0), new Vector2(quarter, blendBoxSize), lowGrey);
                DrawLineFast(new Vector2(quarter, 0), new Vector2(quarter, blendBoxSize), lowGrey);
                DrawLineFast(new Vector2(threeQuarters, 0), new Vector2(threeQuarters, blendBoxSize), lowGrey);
                DrawLineFast(new Vector2(0, quarter), new Vector2(blendBoxSize, quarter), lowGrey);
                DrawLineFast(new Vector2(0, threeQuarters), new Vector2(blendBoxSize, threeQuarters), lowGrey);
                DrawLineFast(new Vector2(half, 0), new Vector2(half, blendBoxSize), Color.grey);
                DrawLineFast(new Vector2(0, half), new Vector2(blendBoxSize, half), Color.grey);
                GL.End();

                GL.Begin(Application.platform == RuntimePlatform.WindowsEditor ? GL.QUADS : GL.LINES);
                ApplyWireMaterial.Invoke(null, new object[] { CompareFunction.Always });
                Vector2 circlePos = sP.vector2Value;
                circlePos.y = 1 - circlePos.y;
                circlePos  *= blendBoxSize;
                DrawCircleFast(circlePos, blendBoxSize * 0.04f, 2, new Color(1, 0.5f, 0));
                GL.End();
            }
        }
    }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            Blend2DAttribute b2D = (Blend2DAttribute)attribute;

            Do2DBlend(position, property, b2D);
        }
Exemple #4
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        Blend2DAttribute nP = attribute as Blend2DAttribute;

        Do2DBlend(position, property, nP);
    }