Esempio n. 1
0
    public void DrawEdgeSpringsGUI()
    {
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.PropertyField(eEdgeSprings, edgeSpringsContent);
        if (EditorGUI.EndChangeCheck())
        {
            if (eEdgeSprings.isExpanded)
            {
                eInternalSprings.isExpanded = false;
                eCustomSprings.isExpanded   = false;
            }
            drawIndex = -1;
            editIndex = -1;
            SceneView.RepaintAll();
        }

        if (eEdgeSprings.isExpanded)
        {
            EditorGUI.indentLevel++;
            EditorGUILayout.BeginHorizontal();

            GUIStyle foldoutStyle = new GUIStyle(EditorStyles.foldout);

            if (eEdgeSpringK.prefabOverride || eEdgeSpringDamp.prefabOverride)
            {
                foldoutStyle.fontStyle = FontStyle.Bold;
            }

            showEdgeDefaultsFoldout = EditorGUILayout.Foldout(showEdgeDefaultsFoldout, edgeDefaultsContent, foldoutStyle);

            if (GUILayout.Button(setDefaultEdgeContent, EditorStyles.miniButton))
            {
                for (int i = 0; i < springBodyEditor.serializedObject.targetObjects.Length; i++)
                {
                    JelloSpringBody t = (JelloSpringBody)springBodyEditor.serializedObject.targetObjects[i];

                    t.setEdgeSpringConstants(t.DefaultEdgeSpringStiffness, t.DefaultEdgeSpringDamping);
                }

                EditorUtility.SetDirty(springBody);
            }
            EditorGUILayout.EndHorizontal();

            if (showEdgeDefaultsFoldout)
            {
                EditorGUI.showMixedValue = eEdgeSpringK.hasMultipleDifferentValues;
                EditorGUI.BeginChangeCheck();
                EditorGUILayout.PropertyField(eEdgeSpringK, stiffnessContent);
                if (EditorGUI.EndChangeCheck())
                {
                    for (int i = 0; i < springBodyEditor.serializedObject.targetObjects.Length; i++)
                    {
                        JelloSpringBody sb = (JelloSpringBody)springBodyEditor.serializedObject.targetObjects[i];
                        sb.DefaultEdgeSpringStiffness = eEdgeSpringK.floatValue;
                    }

                    EditorUtility.SetDirty(springBody);
                }
                EditorGUI.showMixedValue = false;

                EditorGUI.showMixedValue = eEdgeSpringDamp.hasMultipleDifferentValues;
                EditorGUI.BeginChangeCheck();
                EditorGUILayout.PropertyField(eEdgeSpringDamp, dampingContent);
                if (EditorGUI.EndChangeCheck())
                {
                    for (int i = 0; i < springBodyEditor.serializedObject.targetObjects.Length; i++)
                    {
                        JelloSpringBody sb = (JelloSpringBody)springBodyEditor.serializedObject.targetObjects[i];
                        sb.DefaultEdgeSpringDamping = eEdgeSpringDamp.floatValue;
                    }

                    EditorUtility.SetDirty(springBody);
                }
                EditorGUI.showMixedValue = false;
            }

            EditorGUILayout.Separator();

            if (multiEditing)
            {
                EditorGUILayout.HelpBox("Springs may not be edited when multiple GameObjects are selected", MessageType.Info);
                EditorGUI.indentLevel--;
                return;
            }

            int offset = 0;            // zero because nothing comes before edgesprings.

            scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false, GUILayout.MinHeight(minScrollViewHeight));

            for (int i = 0; i < springBody.EdgeSpringCount; i++)
            {
                EditorGUILayout.BeginHorizontal();

                string text = "Spring # " + (i + offset);

                string tooltip = "PMA: " + springBody.getEdgeSpring(i).pointMassA +
                                 " \nPMB: " + springBody.getEdgeSpring(i).pointMassB +
                                 "\nRest Length: " + springBody.getEdgeSpring(i).length +
                                 "\nSpring K: " + springBody.getEdgeSpring(i).stiffness +
                                 "\nDamping: " + springBody.getEdgeSpring(i).damping;

                SerializedProperty eSpring = eEdgeSprings.GetArrayElementAtIndex(i);
                eSpring.isExpanded = editIndex == i + offset;

                EditorGUI.BeginChangeCheck();
                EditorGUILayout.PropertyField(eSpring, new GUIContent(text, tooltip));
                if (EditorGUI.EndChangeCheck())
                {
                    if (!eSpring.isExpanded)
                    {
                        editIndex = -1;
                    }
                    else if (eSpring.isExpanded)
                    {
                        editIndex          = i + offset;
                        handlePositions[0] = springBody.Shape.getVertex(springBody.getEdgeSpring(i).pointMassA);
                        handlePositions[1] = springBody.Shape.getVertex(springBody.getEdgeSpring(i).pointMassB);
                    }
                    SceneView.RepaintAll();
                }

                if (Event.current.type == EventType.Repaint && GUI.tooltip != prevTooltip)
                {
                    //mouse out
                    if (prevTooltip != "")
                    {
                        drawIndex = -1;
                    }

                    //mouse over
                    if (GUI.tooltip != "")
                    {
                        drawIndex = i + offset;                        //offset this in internal springs and in custom springs...
                    }
                    prevTooltip = GUI.tooltip;

                    SceneView.RepaintAll();
                }

                if (GUILayout.Button(deleteSpringContent, EditorStyles.miniButton, GUILayout.Width(20f)))
                {
                    springBody.RemoveSpring(springBody.getEdgeSpring(i));

                    if (editIndex > i + offset)
                    {
                        editIndex--;
                    }
                    else if (editIndex == i + offset)
                    {
                        editIndex = -1;
                    }
                    if (drawIndex > i + offset)
                    {
                        drawIndex--;
                    }
                    else if (editIndex == i + offset)
                    {
                        drawIndex = -1;
                    }

                    EditorUtility.SetDirty(springBody);

                    break;
                }

                EditorGUILayout.EndHorizontal();

                if (editIndex == i + offset)                //offset this in internal and custom
                {
                    DrawEditSpringGUI(eEdgeSprings, i);
                }
            }
            EditorGUILayout.EndScrollView();
            EditorGUI.indentLevel--;
        }
    }