예제 #1
0
        public static Vector3[] PositionHandle(ref PositionHandleIDs handleIDs, Vector3[] points, Vector3 position, Quaternion rotation, Axes enabledAxes = Axes.XYZ)
        {
            var xAxisId   = handleIDs.xAxisId;
            var yAxisId   = handleIDs.yAxisId;
            var zAxisId   = handleIDs.zAxisId;
            var xzPlaneId = handleIDs.xzPlaneId;
            var xyPlaneId = handleIDs.xyPlaneId;
            var yzPlaneId = handleIDs.yzPlaneId;
            var centerId  = handleIDs.centerId;

            var originalColor = SceneHandles.color;

            var handleSize = UnityEditor.HandleUtility.GetHandleSize(position);

            UnityEditor.HandleUtility.AddControl(centerId, UnityEditor.HandleUtility.DistanceToCircle(position, handleSize * 0.055f));

            var evt  = Event.current;
            var type = evt.GetTypeForControl(centerId);

            switch (type)
            {
            case EventType.MouseDown:
            {
                if (GUIUtility.hotControl != 0)
                {
                    break;
                }

                if ((UnityEditor.HandleUtility.nearestControl != centerId || evt.button != 0) &&
                    (GUIUtility.keyboardControl != centerId || evt.button != 2))
                {
                    break;
                }

                handleIDs.originalPosition = position;
                GUIUtility.hotControl      = GUIUtility.keyboardControl = centerId;
                evt.Use();
                EditorGUIUtility.SetWantsMouseJumping(1);
                break;
            }

            case EventType.MouseMove:
            {
                handleIDs.originalPosition = position;
                break;
            }

            case EventType.MouseDrag:
            {
                if (GUIUtility.hotControl != centerId)
                {
                    break;
                }
                break;
            }

            case EventType.MouseUp:
            {
                if (GUIUtility.hotControl == centerId && (evt.button == 0 || evt.button == 2))
                {
                    GUIUtility.hotControl      = 0;
                    GUIUtility.keyboardControl = 0;
                    evt.Use();
                    Snapping.ActiveAxes = Axes.XYZ;
                    EditorGUIUtility.SetWantsMouseJumping(0);
                    SceneView.RepaintAll();
                    handleIDs.originalPosition = position;
                }
                break;
            }
            }

            //,.,.., look at 2018.1 how the position handle works w/ colors

            var xAxisLocked   = (handleIDs.xAxisState & ControlState.Locked) == ControlState.Locked;
            var yAxisLocked   = (handleIDs.yAxisState & ControlState.Locked) == ControlState.Locked;
            var zAxisLocked   = (handleIDs.zAxisState & ControlState.Locked) == ControlState.Locked;
            var xzPlaneLocked = (handleIDs.xzPlaneState & ControlState.Locked) == ControlState.Locked;
            var xyPlaneLocked = (handleIDs.xyPlaneState & ControlState.Locked) == ControlState.Locked;
            var yzPlaneLocked = (handleIDs.yzPlaneState & ControlState.Locked) == ControlState.Locked;

            var xAxisDisabled   = ((enabledAxes & Axes.X) != Axes.X) || (handleIDs.xAxisState & ControlState.Disabled) == ControlState.Disabled;
            var yAxisDisabled   = ((enabledAxes & Axes.Y) != Axes.Y) || (handleIDs.yAxisState & ControlState.Disabled) == ControlState.Disabled;
            var zAxisDisabled   = ((enabledAxes & Axes.Z) != Axes.Z) || (handleIDs.zAxisState & ControlState.Disabled) == ControlState.Disabled;
            var xyPlaneDisabled = ((enabledAxes & Axes.XY) != Axes.XY) || (handleIDs.xyPlaneState & ControlState.Disabled) == ControlState.Disabled;
            var yzPlaneDisabled = ((enabledAxes & Axes.YZ) != Axes.YZ) || (handleIDs.yzPlaneState & ControlState.Disabled) == ControlState.Disabled;
            var xzPlaneDisabled = ((enabledAxes & Axes.XZ) != Axes.XZ) || (handleIDs.xzPlaneState & ControlState.Disabled) == ControlState.Disabled;

            var xAxisSelected   = (handleIDs.xAxisIndirectState & (ControlState.Focused | ControlState.Active)) != ControlState.None;
            var yAxisSelected   = (handleIDs.yAxisIndirectState & (ControlState.Focused | ControlState.Active)) != ControlState.None;
            var zAxisSelected   = (handleIDs.zAxisIndirectState & (ControlState.Focused | ControlState.Active)) != ControlState.None;
            var xzPlaneSelected = (handleIDs.xzPlaneState & (ControlState.Focused | ControlState.Active)) != ControlState.None;
            var xyPlaneSelected = (handleIDs.xyPlaneState & (ControlState.Focused | ControlState.Active)) != ControlState.None;
            var yzPlaneSelected = (handleIDs.yzPlaneState & (ControlState.Focused | ControlState.Active)) != ControlState.None;

            var xAxisColor   = SceneHandles.StateColor(SceneHandles.xAxisColor, xAxisDisabled, xAxisSelected);
            var yAxisColor   = SceneHandles.StateColor(SceneHandles.yAxisColor, yAxisDisabled, yAxisSelected);
            var zAxisColor   = SceneHandles.StateColor(SceneHandles.zAxisColor, zAxisDisabled, zAxisSelected);
            var xzPlaneColor = SceneHandles.StateColor(SceneHandles.yAxisColor, xzPlaneDisabled, xzPlaneSelected);
            var xyPlaneColor = SceneHandles.StateColor(SceneHandles.zAxisColor, xyPlaneDisabled, xyPlaneSelected);
            var yzPlaneColor = SceneHandles.StateColor(SceneHandles.xAxisColor, yzPlaneDisabled, yzPlaneSelected);


            var prevDisabled = SceneHandles.disabled;

            if (!xAxisLocked)
            {
                SceneHandles.disabled = xAxisDisabled;
                SceneHandles.color    = xAxisColor;
                points = Slider1DHandle(xAxisId, Axis.X, points, position, rotation * Vector3.right, Snapping.MoveSnappingSteps.x, handleSize, ArrowHandleCap, selectLockingAxisOnClick: true);
            }

            if (!yAxisLocked)
            {
                SceneHandles.disabled = yAxisDisabled;
                SceneHandles.color    = yAxisColor;
                points = Slider1DHandle(yAxisId, Axis.Y, points, position, rotation * Vector3.up, Snapping.MoveSnappingSteps.y, handleSize, ArrowHandleCap, selectLockingAxisOnClick: true);
            }

            if (!zAxisLocked)
            {
                SceneHandles.disabled = zAxisDisabled;
                SceneHandles.color    = zAxisColor;
                points = Slider1DHandle(zAxisId, Axis.Z, points, position, rotation * Vector3.forward, Snapping.MoveSnappingSteps.z, handleSize, ArrowHandleCap, selectLockingAxisOnClick: true);
            }

            if (!xzPlaneLocked)
            {
                SceneHandles.disabled = xzPlaneDisabled;
                SceneHandles.color    = xzPlaneColor;
                points = PlanarHandle(xzPlaneId, PlaneAxes.XZ, points, position, rotation, handleSize * 0.3f, selectLockingAxisOnClick: true);
            }

            if (!xyPlaneLocked)
            {
                SceneHandles.disabled = xyPlaneDisabled;
                SceneHandles.color    = xyPlaneColor;
                points = PlanarHandle(xyPlaneId, PlaneAxes.XY, points, position, rotation, handleSize * 0.3f, selectLockingAxisOnClick: true);
            }

            if (!yzPlaneLocked)
            {
                SceneHandles.disabled = yzPlaneDisabled;
                SceneHandles.color    = yzPlaneColor;
                points = PlanarHandle(yzPlaneId, PlaneAxes.YZ, points, position, rotation, handleSize * 0.3f, selectLockingAxisOnClick: true);
            }


            if ((handleIDs.centerState & ControlState.Disabled) != ControlState.Disabled)
            {
                switch (type)
                {
                case EventType.Repaint:
                {
                    var focused = (handleIDs.centerState & ControlState.Focused) == ControlState.Focused;
                    SceneHandles.color = SceneHandles.StateColor(SceneHandles.centerColor, false, focused);
                    SceneHandles.RenderBorderedCircle(position, handleSize * 0.05f);
                    break;
                }
                }
            }


            SceneHandles.disabled = prevDisabled;
            SceneHandles.color    = originalColor;

            return(points);
        }
        public static Vector3[] PositionHandle(PositionHandleIDs handleIDs, Vector3[] points, Vector3 position, Quaternion rotation, Axes enabledAxes = Axes.XYZ)
        {
            var xAxisId   = handleIDs.xAxisId;
            var yAxisId   = handleIDs.yAxisId;
            var zAxisId   = handleIDs.zAxisId;
            var xzPlaneId = handleIDs.xzPlaneId;
            var xyPlaneId = handleIDs.xyPlaneId;
            var yzPlaneId = handleIDs.yzPlaneId;
            var centerId  = handleIDs.centerId;

            var isStatic     = (!Tools.hidden && EditorApplication.isPlaying && GameObjectUtility.ContainsStatic(Selection.gameObjects));
            var prevDisabled = SceneHandles.disabled;

            var hotControl = GUIUtility.hotControl;

            var xAxisIsHot  = (xAxisId == hotControl);
            var yAxisIsHot  = (yAxisId == hotControl);
            var zAxisIsHot  = (zAxisId == hotControl);
            var xzAxisIsHot = (xzPlaneId == hotControl);
            var xyAxisIsHot = (xyPlaneId == hotControl);
            var yzAxisIsHot = (yzPlaneId == hotControl);
            var centerIsHot = (centerId == hotControl);

            var isControlHot = xAxisIsHot || yAxisIsHot || zAxisIsHot || xzAxisIsHot || xyAxisIsHot || yzAxisIsHot || centerIsHot;

            var handleSize    = UnityEditor.HandleUtility.GetHandleSize(position);
            var originalColor = SceneHandles.color;

            var activeAxes = Snapping.ActiveAxes;

            UnityEditor.HandleUtility.AddControl(centerId, UnityEditor.HandleUtility.DistanceToCircle(position, handleSize * 0.055f));

            var evt  = Event.current;
            var type = evt.GetTypeForControl(centerId);

            switch (type)
            {
            case EventType.MouseDown:
            {
                if (GUIUtility.hotControl != 0)
                {
                    break;
                }

                if ((UnityEditor.HandleUtility.nearestControl != centerId || evt.button != 0) &&
                    (GUIUtility.keyboardControl != centerId || evt.button != 2))
                {
                    break;
                }

                GUIUtility.hotControl = GUIUtility.keyboardControl = centerId;
                evt.Use();
                EditorGUIUtility.SetWantsMouseJumping(1);
                break;
            }

            case EventType.MouseDrag:
            {
                if (GUIUtility.hotControl != centerId)
                {
                    break;
                }
                break;
            }

            case EventType.MouseUp:
            {
                if (GUIUtility.hotControl == centerId && (evt.button == 0 || evt.button == 2))
                {
                    GUIUtility.hotControl      = 0;
                    GUIUtility.keyboardControl = 0;
                    evt.Use();
                    Snapping.ActiveAxes = Axes.XYZ;
                    EditorGUIUtility.SetWantsMouseJumping(0);
                    SceneView.RepaintAll();
                }
                break;
            }
            }

            //,.,.., look at 2018.1 how the position handle works w/ colors

            var xAxisActive = !Snapping.AxisLocking[0];
            var yAxisActive = !Snapping.AxisLocking[1];
            var zAxisActive = !Snapping.AxisLocking[2];

            var xzPlaneActive = xAxisActive && zAxisActive;
            var xyPlaneActive = xAxisActive && yAxisActive;
            var yzPlaneActive = yAxisActive && zAxisActive;

            var xAxisDisabled   = isStatic || prevDisabled || ((enabledAxes & Axes.X) == 0) || !xAxisActive || (isControlHot && !xAxisIsHot && !xzAxisIsHot && !xyAxisIsHot);
            var yAxisDisabled   = isStatic || prevDisabled || ((enabledAxes & Axes.Y) == 0) || !yAxisActive || (isControlHot && !yAxisIsHot && !xyAxisIsHot && !yzAxisIsHot);
            var zAxisDisabled   = isStatic || prevDisabled || ((enabledAxes & Axes.Z) == 0) || !zAxisActive || (isControlHot && !zAxisIsHot && !xzAxisIsHot && !yzAxisIsHot);
            var xzPlaneDisabled = isStatic || prevDisabled || ((enabledAxes & Axes.XZ) != Axes.XZ) || !xzPlaneActive || (isControlHot && !xzAxisIsHot);
            var xyPlaneDisabled = isStatic || prevDisabled || ((enabledAxes & Axes.XY) != Axes.XY) || !xyPlaneActive || (isControlHot && !xyAxisIsHot);
            var yzPlaneDisabled = isStatic || prevDisabled || ((enabledAxes & Axes.YZ) != Axes.YZ) || !yzPlaneActive || (isControlHot && !yzAxisIsHot);

            var currentFocusControl = SceneHandleUtility.focusControl;

            var xAxisIndirectlyFocused = (currentFocusControl == xyPlaneId || currentFocusControl == xzPlaneId);
            var yAxisIndirectlyFocused = (currentFocusControl == xyPlaneId || currentFocusControl == yzPlaneId);
            var zAxisIndirectlyFocused = (currentFocusControl == xzPlaneId || currentFocusControl == yzPlaneId);

            var xAxisIndirectlyActive = activeAxes == Axes.XY || activeAxes == Axes.XZ;
            var yAxisIndirectlyActive = activeAxes == Axes.XY || activeAxes == Axes.YZ;
            var zAxisIndirectlyActive = activeAxes == Axes.XZ || activeAxes == Axes.YZ;

            var xAxisSelected = xAxisIndirectlyFocused || xAxisIndirectlyActive || activeAxes == Axes.X;
            var yAxisSelected = yAxisIndirectlyFocused || yAxisIndirectlyActive || activeAxes == Axes.Y;
            var zAxisSelected = zAxisIndirectlyFocused || zAxisIndirectlyActive || activeAxes == Axes.Z;
            var xzAxiSelected = activeAxes == Axes.XZ;
            var xyAxiSelected = activeAxes == Axes.XZ;
            var yzAxiSelected = activeAxes == Axes.YZ;

            var xAxisColor   = SceneHandles.StateColor(SceneHandles.xAxisColor, xAxisDisabled, xAxisSelected);
            var yAxisColor   = SceneHandles.StateColor(SceneHandles.yAxisColor, yAxisDisabled, yAxisSelected);
            var zAxisColor   = SceneHandles.StateColor(SceneHandles.zAxisColor, zAxisDisabled, zAxisSelected);
            var xzPlaneColor = SceneHandles.StateColor(SceneHandles.yAxisColor, xzPlaneDisabled, xzAxiSelected);
            var xyPlaneColor = SceneHandles.StateColor(SceneHandles.zAxisColor, xyPlaneDisabled, xyAxiSelected);
            var yzPlaneColor = SceneHandles.StateColor(SceneHandles.xAxisColor, yzPlaneDisabled, yzAxiSelected);


            if (xAxisActive)
            {
                SceneHandles.disabled = xAxisDisabled;
                SceneHandles.color    = xAxisColor;
                points = Slider1DHandle(xAxisId, Axis.X, points, position, rotation * Vector3.right, Snapping.MoveSnappingSteps.x, handleSize, ArrowHandleCap, selectLockingAxisOnClick: true);
            }

            if (yAxisActive)
            {
                SceneHandles.disabled = yAxisDisabled;
                SceneHandles.color    = yAxisColor;
                points = Slider1DHandle(yAxisId, Axis.Y, points, position, rotation * Vector3.up, Snapping.MoveSnappingSteps.y, handleSize, ArrowHandleCap, selectLockingAxisOnClick: true);
            }

            if (zAxisActive)
            {
                SceneHandles.disabled = zAxisDisabled;
                SceneHandles.color    = zAxisColor;
                points = Slider1DHandle(zAxisId, Axis.Z, points, position, rotation * Vector3.forward, Snapping.MoveSnappingSteps.z, handleSize, ArrowHandleCap, selectLockingAxisOnClick: true);
            }

            if (xzPlaneActive)
            {
                SceneHandles.disabled = xzPlaneDisabled;
                SceneHandles.color    = xzPlaneColor;
                points = PlanarHandle(xzPlaneId, PlaneAxes.XZ, points, position, rotation, handleSize * 0.3f, selectLockingAxisOnClick: true);
            }

            if (xyPlaneActive)
            {
                SceneHandles.disabled = xyPlaneDisabled;
                SceneHandles.color    = xyPlaneColor;
                points = PlanarHandle(xyPlaneId, PlaneAxes.XY, points, position, rotation, handleSize * 0.3f, selectLockingAxisOnClick: true);
            }

            if (yzPlaneActive)
            {
                SceneHandles.disabled = yzPlaneDisabled;
                SceneHandles.color    = yzPlaneColor;
                points = PlanarHandle(yzPlaneId, PlaneAxes.YZ, points, position, rotation, handleSize * 0.3f, selectLockingAxisOnClick: true);
            }


            switch (type)
            {
            case EventType.Repaint:
            {
                SceneHandles.color = SceneHandles.StateColor(SceneHandles.centerColor, false, centerId == SceneHandleUtility.focusControl);
                SceneHandles.RenderBorderedCircle(position, handleSize * 0.05f);
                break;
            }
            }



            SceneHandles.disabled = prevDisabled;
            SceneHandles.color    = originalColor;

            return(points);
        }