Skip to content

Commit

Permalink
Good simplification progress
Browse files Browse the repository at this point in the history
  • Loading branch information
AquaGeneral committed Nov 16, 2017
1 parent e65681b commit 8339151
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 21 deletions.
76 changes: 55 additions & 21 deletions Editor/EnjoinedConfigurableJoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* License: Mozilla Public License Version 2.0 (https://www.mozilla.org/en-US/MPL/2.0/)
*/

using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
Expand All @@ -11,18 +12,21 @@ namespace JesseStiller.Enjoined {
[CanEditMultipleObjects]
[CustomEditor(typeof(ConfigurableJoint))]
public class EnjoinedConfigurableJoint : Editor {
// Joint properties
private SerializedProperty connectedBody, axis, anchor, autoConfigureConnectedAnchor, connectedAnchor, breakForce, breakTorque, enableCollision, enablePreprocessing;

// Configurable Joint properties
private SerializedProperty projectionAngle, projectionDistance, projectionMode, slerpDrive, angularYZDrive, angularXDrive, rotationDriveMode, targetAngularVelocity, targetRotation, zDrive, yDrive, xDrive, targetVelocity, targetPosition, angularZLimit, angularYLimit, highAngularXLimit, lowAngularXLimit, linearLimit, linearLimitSpring, angularYZLimitSpring, angularXLimitSpring, angularXMotion, angularYMotion, angularZMotion, zMotion, yMotion, xMotion, secondaryAxis, configuredInWorldSpace, swapBodies;
private bool linearLimitFoldoutState = false;

private ConfigurableJoint joint;

private Dictionary<string, SerializedProperty> properties = new Dictionary<string, SerializedProperty>();

private void OnEnable() {
joint = (ConfigurableJoint)serializedObject.targetObject;

SerializedProperty iterator = serializedObject.GetIterator();
while(iterator.Next(true)) {
// Remove the initial "m_" prefix
properties.Add(iterator.propertyPath.Remove(0, 2), iterator.Copy());
}

FieldInfo[] fields = typeof(EnjoinedConfigurableJoint).GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
foreach(FieldInfo fieldInfo in fields) {
if(fieldInfo.FieldType != typeof(SerializedProperty)) continue;
Expand All @@ -35,13 +39,13 @@ private void OnEnable() {
public override void OnInspectorGUI() {
serializedObject.Update();

DrawSerializedProperties(connectedBody, anchor, axis);
DrawSerializedProperties("ConnectedBody", "Anchor", "Axis");

GUIUtilities.DrawConnectedAnchorProperty(connectedAnchor, autoConfigureConnectedAnchor);
GUIUtilities.DrawConnectedAnchorProperty(properties["ConnectedAnchor"], properties["AutoConfigureConnectedAnchor"]);

EditorGUILayout.PropertyField(secondaryAxis);
EditorGUILayout.PropertyField(properties["SecondaryAxis"]);

MultiPropertyField("Linear Motion", new GUIContent[] { new GUIContent("X"), new GUIContent("Y"), new GUIContent("Z") }, xMotion, yMotion, zMotion);
MultiPropertyField("Linear Motion", new GUIContent[] { new GUIContent("X"), new GUIContent("Y"), new GUIContent("Z") }, properties["XMotion"], properties["YMotion"], properties["ZMotion"]);

/**
* Linear Limit
Expand All @@ -68,22 +72,32 @@ public override void OnInspectorGUI() {
joint.linearLimitSpring = linearLimitSpring;
}

MultiPropertyField("Angular Motion", new GUIContent[] { new GUIContent("X"), new GUIContent("Y"), new GUIContent("Z") }, angularXMotion, angularYMotion, angularZMotion);
MultiPropertyField("Angular Motion", new GUIContent[] { new GUIContent("X"), new GUIContent("Y"), new GUIContent("Z") }, properties["AngularXMotion"], properties["AngularYMotion"], properties["AngularZMotion"]);

/**
* Angular Limit
*/
DrawSerializedProperties(angularXLimitSpring, lowAngularXLimit, highAngularXLimit, angularYLimit, angularYZLimitSpring, angularYLimit, angularZLimit);

//DrawSerializedProperties(angularXLimitSpring, lowAngularXLimit, highAngularXLimit, angularYLimit, angularYZLimitSpring, angularYLimit, angularZLimit);
EditorGUILayout.PrefixLabel("X-axis Angular Limit");
EditorGUI.indentLevel = 1;
float newLowerLimitValue = joint.lowAngularXLimit.limit;
float newUpperLimitValue = joint.highAngularXLimit.limit;
if(GUIUtilities.MinMaxWithFloatFields("Angle", ref newLowerLimitValue, ref newUpperLimitValue, -180f, 180f, 3)) {
properties["LowAngularXLimit.limit"].floatValue = newLowerLimitValue;
properties["HighAngularXLimit.limit"].floatValue = newUpperLimitValue;
}
MultiPropertyField("Bounciness", new string[] { "min", "max" }, properties["LowAngularXLimit.bounciness"], properties["HighAngularXLimit.bounciness"]);
EditorGUI.indentLevel = 0;

/**
* Rotation Drive
*/
DrawSerializedProperties(rotationDriveMode);
DrawSerializedProperties("RotationDriveMode");
EditorGUI.indentLevel = 1;
if((RotationDriveMode)rotationDriveMode.enumValueIndex == RotationDriveMode.XYAndZ) {
DrawSerializedProperties(angularXDrive, angularYZDrive);
if((RotationDriveMode)properties["RotationDriveMode"].enumValueIndex == RotationDriveMode.XYAndZ) {
DrawSerializedProperties("AngularXDrive", "AngularYZDrive");
} else {
DrawSerializedProperties(slerpDrive);
DrawSerializedProperties("SlerpDrive");
}
EditorGUI.indentLevel = 0;

Expand All @@ -94,10 +108,30 @@ public override void OnInspectorGUI() {
DrawDefaultInspector();
}

private void DrawSerializedProperties(params SerializedProperty[] properties) {
foreach(SerializedProperty property in properties) {
EditorGUILayout.PropertyField(property, true, null);
private void DrawSerializedProperties(params string[] propertyNames) {
foreach(string propertyName in propertyNames) {
EditorGUILayout.PropertyField(properties[propertyName], true, null);
}
}

private static void MultiPropertyField(string label, string[] propertyLabels, params SerializedProperty[] properties) {
Debug.Assert(propertyLabels.Length == properties.Length);

Rect controlRect = EditorGUILayout.GetControlRect();

Rect fillRect = EditorGUI.PrefixLabel(controlRect, new GUIContent(label));
float propertyCellWidth = (fillRect.width - (properties.Length - 1f) * 2f) / properties.Length;

float lastLabelWidth = EditorGUIUtility.labelWidth;
EditorGUIUtility.labelWidth = 13f;

Rect cellRect = new Rect(fillRect.x - 1, fillRect.y, propertyCellWidth, fillRect.height);
for(int i = 0; i < properties.Length; i++) {
EditorGUI.PropertyField(cellRect, properties[i], new GUIContent(propertyLabels[i]));
cellRect.x += propertyCellWidth + 2f;
}

EditorGUIUtility.labelWidth = lastLabelWidth;
}

private static void MultiPropertyField(string label, GUIContent[] propertyLabels, params SerializedProperty[] properties) {
Expand Down
37 changes: 37 additions & 0 deletions Editor/GUIUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,43 @@ public static void DrawConnectedAnchorProperty(SerializedProperty connectedAncho
}
}

/// <summary>
/// An EditorGUI control with a label, a min/max slider and min/max float fields.
/// </summary>
/// <returns>Returns a bool indicating if the controls' min/max values have changed or not.</returns>
internal static bool MinMaxWithFloatFields(string label, ref float minValue, ref float maxValue, float minValueBoundary, float maxValueBoundary, int significantDigits) {
Rect controlRect = EditorGUILayout.GetControlRect();

Rect labelRect = new Rect(controlRect);
labelRect.xMax = EditorGUIUtility.labelWidth + 14;
labelRect.yMin -= 1f;
EditorGUI.LabelField(labelRect, label);

EditorGUI.BeginChangeCheck();
Rect fillRect = new Rect(controlRect);
fillRect.xMin = EditorGUIUtility.labelWidth;

Rect leftRect = new Rect(fillRect);
leftRect.xMax = leftRect.xMin + 50f;
minValue = Utilities.FloorToSignificantDigits(Mathf.Clamp(EditorGUI.FloatField(leftRect, minValue), minValueBoundary, maxValueBoundary), significantDigits);

Rect middleRect = new Rect(fillRect);
middleRect.xMin = leftRect.xMin + 40;
middleRect.xMax = fillRect.xMax - 40f;
middleRect.y -= 2;
EditorGUI.MinMaxSlider(middleRect, ref minValue, ref maxValue, minValueBoundary, maxValueBoundary);

Rect rightRect = new Rect(fillRect);
rightRect.xMin = rightRect.xMax - 50;
maxValue = Utilities.FloorToSignificantDigits(Mathf.Clamp(EditorGUI.FloatField(rightRect, maxValue), minValueBoundary, maxValueBoundary), significantDigits);
return EditorGUI.EndChangeCheck();
}

internal static void MinMaxPropertyFields(string label, SerializedProperty minProperty, SerializedProperty maxProperty) {
Rect controlRect = EditorGUILayout.GetControlRect();

}

internal static void ToggleAndFill(Action<Rect> toggleControl, Action<Rect> fillControl, bool enableFillControl, bool enableToggle) {
Rect controlRect = EditorGUILayout.GetControlRect();

Expand Down
15 changes: 15 additions & 0 deletions Editor/Utilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace JesseStiller.Enjoined {
internal static class Utilities {
internal static float FloorToSignificantDigits(float number, int numberOfDigits) {
if(number == 0f) return 0f;

float scale = Mathf.Pow(10, Mathf.Floor(Mathf.Log10(Mathf.Abs(number))) + 1f);
return scale * (float)Math.Round(number / scale, numberOfDigits);
}
}
}
13 changes: 13 additions & 0 deletions Editor/Utilities.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8339151

Please sign in to comment.