Skip to content

Commit

Permalink
Generalized Auto Configure control, started on ConfigurableJoint's ed…
Browse files Browse the repository at this point in the history
…itor
  • Loading branch information
AquaGeneral committed Jun 2, 2017
1 parent b0148b7 commit aaf38a6
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 50 deletions.
78 changes: 78 additions & 0 deletions Editor/EnjoinedConfigurableJoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Written by Jesse Stiller: www.rollingkinetics.com
* License: Mozilla Public License Version 2.0 (https://www.mozilla.org/en-US/MPL/2.0/)
*/

using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;

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, angularYZLimitSpring, angularXLimitSpring, angularXMotion, angularYMotion, angularZMotion, zMotion, yMotion, xMotion, secondaryAxis, configuredInWorldSpace, swapBodies;

private void OnEnable() {
FieldInfo[] fields = typeof(EnjoinedConfigurableJoint).GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
foreach(FieldInfo fieldInfo in fields) {
if(fieldInfo.FieldType != typeof(SerializedProperty)) continue;

string propertyPath = "m_" + char.ToUpper(fieldInfo.Name[0]) + fieldInfo.Name.Substring(1);
fieldInfo.SetValue(this, serializedObject.FindProperty(propertyPath));
}
}

public override void OnInspectorGUI() {
serializedObject.Update();

DrawSerializedProperties(connectedBody, axis, anchor);

GUIUtilities.DrawConnectedAnchorProperty(connectedAnchor, autoConfigureConnectedAnchor);

EditorGUILayout.PropertyField(secondaryAxis);

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

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

EditorGUILayout.Space();
EditorGUILayout.Space();

serializedObject.ApplyModifiedProperties();

base.OnInspectorGUI();
}

private void DrawSerializedProperties(params SerializedProperty[] properties) {
foreach(SerializedProperty property in properties) {
EditorGUILayout.PropertyField(property);
}
}

private static void MultiPropertyField(string label, GUIContent[] 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, fillRect.y, propertyCellWidth, fillRect.height);
for(int i = 0; i < properties.Length; i++) {
EditorGUI.PropertyField(cellRect, properties[i], propertyLabels[i]);
cellRect.x += propertyCellWidth + 2f;
}

EditorGUIUtility.labelWidth = lastLabelWidth;
}
}
}
12 changes: 12 additions & 0 deletions Editor/EnjoinedConfigurableJoint.cs.meta

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

79 changes: 29 additions & 50 deletions Editor/EnjoinedSpringJoint.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,41 @@
/*
* Written by Jesse Stiller: www.rollingkinetics.com
* License: Mozilla Public License Version 2.0
* License: Mozilla Public License Version 2.0 (https://www.mozilla.org/en-US/MPL/2.0/)
*/

using System;
using UnityEditor;
using UnityEngine;

[CanEditMultipleObjects]
[CustomEditor(typeof(SpringJoint))]
public class EnjoinedSpringJoint : Editor {
private SerializedProperty connectedBody, anchor, autoConfigureConnectedAnchor, connectedAnchor, spring, damper,
minDistance, maxDistance, tolerance, breakForce, breakTorque, enableCollision, enablePreprocessing;
namespace JesseStiller.Enjoined {
[CanEditMultipleObjects]
[CustomEditor(typeof(SpringJoint))]
public class EnjoinedSpringJoint : Editor {
private SerializedProperty connectedAnchor, autoConfigureConnectedAnchor;

private void OnEnable() {
connectedBody = serializedObject.FindProperty("_connectedBody");
anchor = serializedObject.FindProperty("_anchor");
autoConfigureConnectedAnchor = serializedObject.FindProperty("_autoConfigureConnectedAnchor");
connectedAnchor = serializedObject.FindProperty("_connectedAnchor");
spring = serializedObject.FindProperty("spring");
damper = serializedObject.FindProperty("damper");
minDistance = serializedObject.FindProperty("minDistance");
maxDistance = serializedObject.FindProperty("maxDistance");
tolerance = serializedObject.FindProperty("tolerance");
breakForce = serializedObject.FindProperty("breakForce");
breakTorque = serializedObject.FindProperty("breakTorque");
enableCollision = serializedObject.FindProperty("enableCollision");
enablePreprocessing = serializedObject.FindProperty("enablePreprocessing");
}

//public override void OnInspectorGUI() {
// EditorGUILayout.PropertyField(connectedBody);
// EditorGUILayout.PropertyField(anchor);
// EditorGUILayout.PropertyField(autoConfigureConnectedAnchor);
// using(new EditorGUI.DisabledScope(autoConfigureConnectedAnchor.boolValue)) {
// EditorGUILayout.PropertyField(connectedAnchor);
// }
//}

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

Rect toggleRect = new Rect(controlRect);
toggleRect.xMax = EditorGUIUtility.labelWidth;
toggleRect.yMin -= 1f;
using(new EditorGUI.DisabledScope(enableToggle)) {
toggleControl(toggleRect);
}

if(enableFillControl == false) {
GUI.enabled = false;
private void OnEnable() {
connectedAnchor = serializedObject.FindProperty("m_ConnectedAnchor");
autoConfigureConnectedAnchor = serializedObject.FindProperty("m_AutoConfigureConnectedAnchor");
}
Rect fillRect = new Rect(controlRect);
fillRect.xMin = EditorGUIUtility.labelWidth + 14f;
fillControl(fillRect);

if(enableFillControl == false) {
GUI.enabled = true;
public override void OnInspectorGUI() {
/**
* Currently the only difference is that the connectedAnchor vector field is greyed out if autoConfigureConnectedAnchor is true
*/
serializedObject.Update();
SerializedProperty iterator = serializedObject.GetIterator();
for(bool enterChildren = true; iterator.NextVisible(enterChildren); enterChildren = false) {
switch(iterator.propertyPath) {
case "m_AutoConfigureConnectedAnchor":
break;
case "m_ConnectedAnchor":
GUIUtilities.DrawConnectedAnchorProperty(connectedAnchor, autoConfigureConnectedAnchor);
break;
default:
EditorGUILayout.PropertyField(iterator, true, null);
break;
}
}
serializedObject.ApplyModifiedProperties();
}
}
}
}
59 changes: 59 additions & 0 deletions Editor/GUIUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Written by Jesse Stiller: www.rollingkinetics.com
* License: Mozilla Public License Version 2.0 (https://www.mozilla.org/en-US/MPL/2.0/)
*/

using System;
using UnityEditor;
using UnityEngine;

namespace JesseStiller.Enjoined {
public static class GUIUtilities {
public static void DrawConnectedAnchorProperty(SerializedProperty connectedAnchor, SerializedProperty autoConfigureConnectedAnchor) {
Rect controlRect = EditorGUILayout.GetControlRect();
Rect fillRect = EditorGUI.PrefixLabel(controlRect, new GUIContent("Connected Anchor"));
float autoLabelWidth = GUI.skin.toggle.CalcSize(new GUIContent("Auto")).x;

bool newAutoConfigure = EditorGUI.ToggleLeft(
new Rect(fillRect.x - autoLabelWidth, controlRect.y, autoLabelWidth, controlRect.height),
"Auto", autoConfigureConnectedAnchor.boolValue);

if(autoConfigureConnectedAnchor.boolValue != newAutoConfigure) {
autoConfigureConnectedAnchor.boolValue = newAutoConfigure;

// Forcibly reset the connectedAnchor point if it's set to auto configure.
if(newAutoConfigure == true) {
Joint joint = (Joint)connectedAnchor.serializedObject.targetObject;
joint.autoConfigureConnectedAnchor = true; // The property must be set to true right now for connectedAnchor to be updated.
connectedAnchor.vector3Value = joint.connectedAnchor;
}
}

using(new EditorGUI.DisabledScope(autoConfigureConnectedAnchor.boolValue)) {
EditorGUI.PropertyField(fillRect, connectedAnchor, GUIContent.none);
}
}

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

Rect toggleRect = new Rect(controlRect);
toggleRect.xMax = EditorGUIUtility.labelWidth;
toggleRect.yMin -= 1f;
using(new EditorGUI.DisabledScope(enableToggle)) {
toggleControl(toggleRect);
}

if(enableFillControl == false) {
GUI.enabled = false;
}
Rect fillRect = new Rect(controlRect);
fillRect.xMin = EditorGUIUtility.labelWidth + 14f;
fillControl(fillRect);

if(enableFillControl == false) {
GUI.enabled = true;
}
}
}
}
12 changes: 12 additions & 0 deletions Editor/GUIUtilities.cs.meta

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

0 comments on commit aaf38a6

Please sign in to comment.