using System; using System.Collections.Generic; using UnityEngine; namespace UnityEditor.Splines { static class PathGUIControls { internal static readonly List pointsBuffer = new List(); public static void MultiEditVector3Field(GUIContent label, IList points) { if (!EditorGUIUtility.wideMode) GUILayout.Label(label); EditorGUILayout.BeginHorizontal(); if (EditorGUIUtility.wideMode) EditorGUILayout.PrefixLabel(label); var prevMixedValue = EditorGUI.showMixedValue; var result = GetMultiEditValue(points, out Vector3 value); EditorGUI.showMixedValue = result.xMixed; EditorGUI.BeginChangeCheck(); GUILayout.Label("X"); float x = EditorGUILayout.FloatField(value.x); if (EditorGUI.EndChangeCheck()) { for (int i = 0; i < points.Count; ++i) { var oldValue = points[i]; oldValue.x = x; points[i] = oldValue; } GUI.changed = true; } EditorGUI.showMixedValue = result.yMixed; EditorGUI.BeginChangeCheck(); GUILayout.Label("Y"); float y = EditorGUILayout.FloatField(value.y); if (EditorGUI.EndChangeCheck()) { for (int i = 0; i < points.Count; ++i) { var oldValue = points[i]; oldValue.y = y; points[i] = oldValue; } GUI.changed = true; } EditorGUI.showMixedValue = result.zMixed; EditorGUI.BeginChangeCheck(); GUILayout.Label("Z"); float z = EditorGUILayout.FloatField(value.z); if (EditorGUI.EndChangeCheck()) { for (int i = 0; i < points.Count; ++i) { var oldValue = points[i]; oldValue.z = z; points[i] = oldValue; } GUI.changed = true; } EditorGUILayout.EndHorizontal(); EditorGUI.showMixedValue = prevMixedValue; } //Returns true if the value has multiple values static (bool xMixed, bool yMixed, bool zMixed) GetMultiEditValue(IList points, out Vector3 value) { value = points[0]; bool xMixed = false; bool yMixed = false; bool zMixed = false; for (int i = 1; i < points.Count; ++i) { Vector3 point = points[i]; xMixed |= !Mathf.Approximately(value.x, point.x); yMixed |= !Mathf.Approximately(value.y, point.y); zMixed |= !Mathf.Approximately(value.z, point.z); if (xMixed && yMixed && zMixed) break; } return (xMixed, yMixed, zMixed); } /// /// Get the mixed value for a field /// /// The target type /// A list of all the values /// The value that should be used for the field /// True if has mixed values public static bool GetMixedValue(IList values, out T value) where T : IEquatable { value = values[0]; for (int i = 1; i < values.Count; ++i) { if (!value.Equals(values[i])) return true; } return false; } } }