Exemple #1
0
        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;
            GUIContent rollLabel       = new GUIContent("Roll");
            Vector2    labelDimension  = GUI.skin.label.CalcSize(rollLabel);
            float      rollWidth       = labelDimension.x + floatFieldWidth;

            r.x += r.width + hSpace; r.width = rect.width - (r.width + hSpace + rollWidth) - (r.height + hSpace);
            EditorGUI.PropertyField(r, element.FindPropertyRelative(() => def.Position), GUIContent.none);

            r.x += r.width + hSpace; r.width = rollWidth;
            float oldWidth = EditorGUIUtility.labelWidth;

            EditorGUIUtility.labelWidth = labelDimension.x;
            EditorGUI.PropertyField(r, element.FindPropertyRelative(() => def.Roll), rollLabel);
            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");
                SplineSmooth.Waypoint wp = _target.Waypoints[index];
                Vector3 pos = SceneView.lastActiveSceneView.camera.transform.position;
                wp.Position = _target.transform.InverseTransformPoint(pos);
                _target.Waypoints[index] = wp;
            }
            EditorGUI.EndDisabledGroup();
        }
Exemple #2
0
        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);
                }
            }
            UpdateAllPointListerBeforeChangingSpline(_target);
            Undo.RecordObject(_target, "Add waypoint");
            var wp = new SplineSmooth.Waypoint();

            wp.Position = pos;
            wp.Roll     = roll;
            var list = new List <SplineSmooth.Waypoint>(_target.Waypoints);

            list.Insert(indexA + 1, wp);
            _target.Waypoints = list.ToArray();
            _target.InvalidateDistanceCache();
            InspectorUtility.RepaintGameView(_target);
            _waypointList.index = indexA + 1; // select it
            UpdateAllPointLister(_target);
        }
        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();
                }
            }
        }
        protected static void DrawGuizmosUnselected(SplineSmooth path)
        {
            if (path.gameObject == Selection.activeGameObject)
            {
                return;
            }
            Matrix4x4 localToWorld = path.transform.localToWorldMatrix;

            for (int i = 0; i < path.Waypoints.Length; ++i)
            {
                SplineSmooth.Waypoint wp = path.Waypoints[i];
                Vector3 pos  = localToWorld.MultiplyPoint(wp.Position);
                float   size = HandleUtility.GetHandleSize(pos) * 0.05f;

                Color colorOld = Gizmos.color;
                Gizmos.color = Color.white;
                Gizmos.DrawSphere(pos, size);
                Gizmos.color = colorOld;
            }
        }
Exemple #5
0
        private void DrawPositionControl(int i, Matrix4x4 localToWorld, Quaternion localRotation)
        {
            SplineSmooth.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())
            {
                UpdateAllPointListerBeforeChangingSpline(_target);
                Undo.RecordObject(target, "Move Waypoint");
                wp.Position          = Matrix4x4.Inverse(localToWorld).MultiplyPoint(pos);
                _target.Waypoints[i] = wp;
                _target.InvalidateDistanceCache();
                InspectorUtility.RepaintGameView(_target);
                UpdateAllPointLister(_target);
            }
        }