private void DrawPositionControl(int i, Matrix4x4 localToWorld, Quaternion localRotation) { Spline.Waypoint wp = _target.Waypoints[i]; Vector3 pos = localToWorld.MultiplyPoint(wp.Position); EditorGUI.BeginChangeCheck(); Handles.color = _target.Appearances.PathColor; Quaternion rotation; if (Tools.pivotRotation != PivotRotation.Local) { rotation = Quaternion.identity; } else { rotation = _target.EvaluateOrientationAtUnit(i, SplineBase.PositionUnits.PathUnits); } float size = HandleUtility.GetHandleSize(pos) * 0.1f; Handles.SphereHandleCap(0, pos, rotation, size, EventType.Repaint); pos = Handles.PositionHandle(pos, rotation); pos = _target.AttemptToApplyGrid(pos); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(target, "Move Waypoint"); wp.Position = Matrix4x4.Inverse(localToWorld).MultiplyPoint(pos); _target.Waypoints[i] = wp; _target.InvalidateDistanceCache(); InspectorUtility.RepaintGameView(_target); } }
void InsertWaypointAtIndex(int indexA) { Vector3 pos = Vector3.forward; Vector3 tangent = Vector3.right; float roll = 0; // Get new values from the current indexA (if any) int numWaypoints = _target.Waypoints.Length; if (indexA < 0) { indexA = numWaypoints - 1; } if (indexA >= 0) { int indexB = indexA + 1; if (_target.m_Looped && indexB >= numWaypoints) { indexB = 0; } if (indexB >= numWaypoints) { // Extrapolate the end if (!_target.Waypoints[indexA].Tangent.AlmostZero()) { tangent = _target.Waypoints[indexA].Tangent; } pos = _target.Waypoints[indexA].Position + tangent; roll = _target.Waypoints[indexA].Roll; } else { // Interpolate pos = _target.transform.InverseTransformPoint( _target.EvaluatePosition(0.5f + indexA)); tangent = _target.transform.InverseTransformDirection( _target.EvaluateTangent(0.5f + indexA).normalized); roll = Mathf.Lerp( _target.Waypoints[indexA].Roll, _target.Waypoints[indexB].Roll, 0.5f); } } UpdateAllPointListerBeforeChangingSpline(_target); Undo.RecordObject(_target, "Add waypoint"); var wp = new Spline.Waypoint(); wp.Position = pos; wp.Tangent = tangent; wp.Roll = roll; var list = new List <Spline.Waypoint>(_target.Waypoints); list.Insert(indexA + 1, wp); _target.Waypoints = list.ToArray(); _target.InvalidateDistanceCache(); _waypointList.index = indexA + 1; // select it UpdateAllPointLister(_target); }
void InsertWaypointAtIndex(int indexA) { Vector3 pos = Vector3.right; float roll = 0; // Get new values from the current indexA (if any) int numWaypoints = _target.Waypoints.Length; if (indexA < 0) { indexA = numWaypoints - 1; } if (indexA >= 0) { int indexB = indexA + 1; if (_target.m_Looped && indexB >= numWaypoints) { indexB = 0; } if (indexB >= numWaypoints) { Vector3 delta = Vector3.right; if (indexA > 0) { delta = _target.Waypoints[indexA].Position - _target.Waypoints[indexA - 1].Position; } pos = _target.Waypoints[indexA].Position + delta; roll = _target.Waypoints[indexA].Roll; } else { // Interpolate pos = _target.transform.InverseTransformPoint(_target.EvaluatePosition(0.5f + indexA)); roll = Mathf.Lerp(_target.Waypoints[indexA].Roll, _target.Waypoints[indexB].Roll, 0.5f); } } Undo.RecordObject(_target, "Add waypoint"); var wp = new Spline.Waypoint(); wp.Position = pos; wp.Roll = roll; var list = new List <Spline.Waypoint>(_target.Waypoints); list.Insert(indexA + 1, wp); _target.Waypoints = list.ToArray(); _target.InvalidateDistanceCache(); InspectorUtility.RepaintGameView(_target); _waypointList.index = indexA + 1; // select it }
private void InsertWaypointAtIndexAndPosition(int indexA, Vector3 defaultPosition) { Undo.RecordObject(_target, "Add waypoint"); var wp = new Spline.Waypoint(); wp.Position = defaultPosition; wp.Roll = 0; var list = new List <Spline.Waypoint>(_target.Waypoints); list.Insert(indexA + 1, wp); _target.Waypoints = list.ToArray(); _target.InvalidateDistanceCache(); InspectorUtility.RepaintGameView(_target); _waypointList.index = indexA + 1; // select it }
private void DrawRotationControl(int i, Matrix4x4 localToWorld, Quaternion localRotation) { Spline.Waypoint wp = _target.Waypoints[i]; Vector3 pos = localToWorld.MultiplyPoint(wp.Position); EditorGUI.BeginChangeCheck(); Handles.color = Color.yellow; Quaternion rotation; if (Tools.pivotRotation != PivotRotation.Local) { rotation = Quaternion.identity; } else { rotation = _target.EvaluateOrientationAtUnit(i, SplineBase.PositionUnits.PathUnits); } float size = HandleUtility.GetHandleSize(pos) * 0.1f; Handles.SphereHandleCap(0, pos, rotation, size, EventType.Repaint); if (!_isRotatingWp) { _currentRoll = wp.Roll == 0 ? 1 : wp.Roll; } Matrix4x4 matrix = Matrix4x4.TRS(pos, rotation, Vector3.one); using (new Handles.DrawingScope(matrix)) { Quaternion newRotation = Handles.Disc(Quaternion.identity, Vector3.zero, new Vector3(0, 0, _currentRoll), 1, true, 0); if (EditorGUI.EndChangeCheck()) { _isRotatingWp = true; Undo.RecordObject(target, "Rotate Waypoint"); // Needed for accessing string names of fields SplineSmooth.Waypoint def = new SplineSmooth.Waypoint(); SerializedProperty element = _waypointList.serializedProperty.GetArrayElementAtIndex(i); SerializedProperty rollProperty = element.FindPropertyRelative(() => def.Roll); _currentRoll = newRotation.eulerAngles.z; rollProperty.floatValue = _currentRoll; serializedObject.ApplyModifiedProperties(); } } }
void DrawWaypointEditor(Rect rect, int index) { EditorGUI.BeginDisabledGroup(_target.ReadOnly); // Needed for accessing string names of fields SplineSmooth.Waypoint def = new SplineSmooth.Waypoint(); SerializedProperty element = _waypointList.serializedProperty.GetArrayElementAtIndex(index); float hSpace = 3; rect.width -= hSpace; rect.y += 1; Vector2 numberDimension = GUI.skin.label.CalcSize(new GUIContent("999")); Rect r = new Rect(rect.position, numberDimension); if (GUI.Button(r, new GUIContent(index.ToString(), "Go to the waypoint in the scene view"))) { if (SceneView.lastActiveSceneView != null) { _waypointList.index = index; SceneView.lastActiveSceneView.LookAt(_target.EvaluatePosition(index)); } } float floatFieldWidth = EditorGUIUtility.singleLineHeight * 2f; r.x += r.width + hSpace; r.width = rect.width - (r.width + hSpace) - (r.height + hSpace); EditorGUI.PropertyField(r, element.FindPropertyRelative(() => def.Position), GUIContent.none); float oldWidth = EditorGUIUtility.labelWidth; EditorGUIUtility.labelWidth = oldWidth; r.x += r.width + hSpace; r.height += 1; r.width = r.height; GUIContent setButtonContent = EditorGUIUtility.IconContent("d_RectTransform Icon"); setButtonContent.tooltip = "Set to scene-view camera position"; if (GUI.Button(r, setButtonContent, GUI.skin.label) && SceneView.lastActiveSceneView != null) { Undo.RecordObject(_target, "Set waypoint"); Spline.Waypoint wp = _target.Waypoints[index]; Vector3 pos = SceneView.lastActiveSceneView.camera.transform.position; wp.Position = _target.transform.InverseTransformPoint(pos); _target.Waypoints[index] = wp; } EditorGUI.EndDisabledGroup(); }
protected static void DrawGuizmosUnselected(Spline path) { if (path.gameObject == Selection.activeGameObject) { return; } Matrix4x4 localToWorld = path.transform.localToWorldMatrix; for (int i = 0; i < path.Waypoints.Length; ++i) { Spline.Waypoint wp = path.Waypoints[i]; Vector3 pos = localToWorld.MultiplyPoint(wp.Position); float size = HandleUtility.GetHandleSize(pos) * 0.1f; Color colorOld = Gizmos.color; Gizmos.color = Color.white; Gizmos.DrawSphere(pos, size); Gizmos.color = colorOld; } }
void DrawTangentControl(int i, Matrix4x4 localToWorld, Quaternion localRotation) { Spline.Waypoint wp = _target.Waypoints[i]; Vector3 hPos = localToWorld.MultiplyPoint(wp.Position + wp.Tangent); Handles.color = Color.yellow; Handles.DrawLine(localToWorld.MultiplyPoint(wp.Position), hPos); EditorGUI.BeginChangeCheck(); Quaternion rotation; if (Tools.pivotRotation != PivotRotation.Local) { rotation = Quaternion.identity; } else { rotation = _target.EvaluateOrientationAtUnit(i, SplineBase.PositionUnits.PathUnits); } float size = HandleUtility.GetHandleSize(hPos) * 0.1f; Handles.SphereHandleCap(0, hPos, rotation, size, EventType.Repaint); Vector3 newPos = Handles.PositionHandle(hPos, rotation); newPos = _target.AttemptToApplyGrid(newPos); if (EditorGUI.EndChangeCheck()) { UpdateAllPointListerBeforeChangingSpline(_target); Undo.RecordObject(target, "Change Waypoint Tangent"); newPos = Matrix4x4.Inverse(localToWorld).MultiplyPoint(newPos); wp.Tangent = newPos - wp.Position; _target.Waypoints[i] = wp; _target.InvalidateDistanceCache(); InspectorUtility.RepaintGameView(_target); UpdateAllPointLister(_target); } }
void DrawWaypointEditor(Rect rect, int index) { EditorGUI.BeginDisabledGroup(_target.ReadOnly); // Needed for accessing string names of fields Spline.Waypoint def = new Spline.Waypoint(); Vector2 numberDimension = GUI.skin.button.CalcSize(new GUIContent("999")); Vector2 labelDimension = GUI.skin.label.CalcSize(new GUIContent("Position")); Vector2 addButtonDimension = new Vector2(labelDimension.y + 5, labelDimension.y + 1); float vSpace = 2; float hSpace = 3; SerializedProperty element = _waypointList.serializedProperty.GetArrayElementAtIndex(index); rect.y += vSpace / 2; Rect r = new Rect(rect.position, numberDimension); Color color = GUI.color; // GUI.color = Target.m_Appearance.pathColor; if (GUI.Button(r, new GUIContent(index.ToString(), "Go to the waypoint in the scene view"))) { if (SceneView.lastActiveSceneView != null) { _waypointList.index = index; SceneView.lastActiveSceneView.LookAt(_target.EvaluatePosition(index)); } } GUI.color = color; r = new Rect(rect.position, labelDimension); r.x += hSpace + numberDimension.x; EditorGUI.LabelField(r, "Position"); r.x += hSpace + r.width; r.width = rect.width - (numberDimension.x + hSpace + r.width + hSpace + addButtonDimension.x + hSpace); EditorGUI.PropertyField(r, element.FindPropertyRelative(() => def.Position), GUIContent.none); r.x += r.width + hSpace; r.size = addButtonDimension; GUIContent buttonContent = EditorGUIUtility.IconContent("d_RectTransform Icon"); buttonContent.tooltip = "Set to scene-view camera position"; GUIStyle style = new GUIStyle(GUI.skin.label); style.alignment = TextAnchor.MiddleCenter; if (GUI.Button(r, buttonContent, style) && SceneView.lastActiveSceneView != null) { Undo.RecordObject(_target, "Set waypoint"); Spline.Waypoint wp = _target.Waypoints[index]; Vector3 pos = SceneView.lastActiveSceneView.camera.transform.position; wp.Position = _target.transform.InverseTransformPoint(pos); _target.Waypoints[index] = wp; } r = new Rect(rect.position, labelDimension); r.y += numberDimension.y + vSpace; r.x += hSpace + numberDimension.x; r.width = labelDimension.x; EditorGUI.LabelField(r, "Tangent"); r.x += hSpace + r.width; r.width = rect.width - (numberDimension.x + hSpace + r.width + hSpace + addButtonDimension.x + hSpace); EditorGUI.PropertyField(r, element.FindPropertyRelative(() => def.Tangent), GUIContent.none); r.x += r.width + hSpace; r.size = addButtonDimension; buttonContent = EditorGUIUtility.IconContent("ol minus@2x"); buttonContent.tooltip = "Remove this waypoint"; if (GUI.Button(r, buttonContent, style)) { Undo.RecordObject(_target, "Delete waypoint"); var list = new List <Spline.Waypoint>(_target.Waypoints); list.RemoveAt(index); _target.Waypoints = list.ToArray(); if (index == _target.Waypoints.Length) { _waypointList.index = index - 1; } } r = new Rect(rect.position, labelDimension); r.y += 2 * (numberDimension.y + vSpace); r.x += hSpace + numberDimension.x; r.width = labelDimension.x; EditorGUI.LabelField(r, "Roll"); r.x += hSpace + labelDimension.x; r.width = rect.width - (numberDimension.x + hSpace) - (labelDimension.x + hSpace) - (addButtonDimension.x + hSpace); r.width /= 3; EditorGUI.MultiPropertyField(r, new GUIContent[] { new GUIContent(" ") }, element.FindPropertyRelative(() => def.Roll)); r.x = rect.x + rect.width - addButtonDimension.x; r.size = addButtonDimension; buttonContent = EditorGUIUtility.IconContent("ol plus@2x"); buttonContent.tooltip = "Add a new waypoint after this one"; if (GUI.Button(r, buttonContent, style)) { _waypointList.index = index; InsertWaypointAtIndex(index); } EditorGUI.EndDisabledGroup(); }