示例#1
0
        internal static Vector3 Do(int id, Vector3 position, Vector3 direction, float size, CSGHandles.CapFunction capFunction, SnapMode snapMode, Vector3[] snapVertices, CSGHandles.InitFunction initFunction = null, CSGHandles.InitFunction shutdownFunction = null)
        {
            var evt = Event.current;

            switch (evt.GetTypeForControl(id))
            {
            case EventType.Layout:
            {
                if (capFunction != null)
                {
                    capFunction(id, position, Quaternion.LookRotation(direction), size, EventType.Layout);
                }
                else
                {
                    HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(position, size * .2f));
                }
                break;
            }

            case EventType.MouseDown:
            {
                if (CSGHandles.disabled)
                {
                    break;
                }
                if (((HandleUtility.nearestControl == id && evt.button == 0) ||
                     (GUIUtility.keyboardControl == id && evt.button == 2)) && GUIUtility.hotControl == 0)
                {
                    s_SnapVertices = null;
                    if (initFunction != null)
                    {
                        initFunction();
                    }
                    GUIUtility.hotControl  = GUIUtility.keyboardControl = id;
                    s_CurrentMousePosition = s_StartMousePosition = evt.mousePosition;
                    s_StartPosition        = position;
                    evt.Use();
                    EditorGUIUtility.SetWantsMouseJumping(1);
                }

                break;
            }

            case EventType.MouseDrag:
            {
                if (GUIUtility.hotControl == id)
                {
                    if (s_SnapVertices == null)
                    {
                        if (snapVertices != null)
                        {
                            s_SnapVertices = snapVertices.ToArray();
                        }
                        else
                        {
                            s_SnapVertices = new Vector3[] { s_StartPosition }
                        };
                    }
                    s_CurrentMousePosition += evt.delta;
                    var dist           = HandleUtility.CalcLineTranslation(s_StartMousePosition, s_CurrentMousePosition, s_StartPosition, direction);
                    var worldDirection = Handles.matrix.MultiplyVector(direction);
                    var worldPosition  = Handles.matrix.MultiplyPoint(s_StartPosition) + (worldDirection * dist);
                    position = Handles.inverseMatrix.MultiplyPoint(worldPosition);

                    switch (snapMode)
                    {
                    case SnapMode.GridSnapping:
                    {
                        var delta = RealtimeCSG.CSGGrid.SnapDeltaToGrid(position - s_StartPosition, s_SnapVertices, snapToGridPlane: false, snapToSelf: true);
                        position = delta + s_StartPosition;
                        break;
                    }

                    case SnapMode.RelativeSnapping:
                    {
                        var delta = RealtimeCSG.CSGGrid.SnapDeltaRelative(position - s_StartPosition, snapToGridPlane: false);
                        position = delta + s_StartPosition;
                        break;
                    }

                    default:
                    case SnapMode.None:
                    {
                        position = RealtimeCSG.CSGGrid.HandleLockedAxi(position - s_StartPosition) + s_StartPosition;
                        break;
                    }
                    }

                    GUI.changed = true;
                    evt.Use();
                }
                break;
            }

            case EventType.MouseUp:
            {
                if (GUIUtility.hotControl == id && (evt.button == 0 || evt.button == 2))
                {
                    GUIUtility.hotControl = 0;
                    if (shutdownFunction != null)
                    {
                        shutdownFunction();
                    }
                    evt.Use();
                    EditorGUIUtility.SetWantsMouseJumping(0);
                }
                break;
            }

            case EventType.Repaint:
            {
                var originalColor = Handles.color;
                Handles.color = CSGHandles.StateColor(originalColor, CSGHandles.disabled, CSGHandles.FocusControl == id);
                if (capFunction != null)
                {
                    capFunction(id, position, Quaternion.LookRotation(direction), size, EventType.Repaint);
                }

                Handles.color = originalColor;
                break;
            }
            }
            return(position);
        }
示例#2
0
        public static Vector3 PositionHandle(Camera camera, Vector3 position, Quaternion rotation, SnapMode snapMode, Vector3[] snapVertices = null, InitFunction initFunction = null, InitFunction shutdownFunction = null)
        {
            GUI.SetNextControlName("xAxis");   var xAxisSlider   = GUIUtility.GetControlID(s_xAxisMoveHandleHash, FocusType.Passive);
            GUI.SetNextControlName("yAxis");   var yAxisSlider   = GUIUtility.GetControlID(s_yAxisMoveHandleHash, FocusType.Passive);
            GUI.SetNextControlName("zAxis");   var zAxisSlider   = GUIUtility.GetControlID(s_zAxisMoveHandleHash, FocusType.Passive);
            GUI.SetNextControlName("xzPlane"); var xzPlaneSlider = GUIUtility.GetControlID(s_xzAxisMoveHandleHash, FocusType.Passive);
            GUI.SetNextControlName("xyPlane"); var xyPlaneSlider = GUIUtility.GetControlID(s_xyAxisMoveHandleHash, FocusType.Passive);
            GUI.SetNextControlName("yzPlane"); var yzPlaneSlider = GUIUtility.GetControlID(s_yzAxisMoveHandleHash, FocusType.Passive);

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

            var hotControl = GUIUtility.hotControl;

            var xAxisIsHot  = (xAxisSlider == hotControl);
            var yAxisIsHot  = (yAxisSlider == hotControl);
            var zAxisIsHot  = (zAxisSlider == hotControl);
            var xzAxisIsHot = (xzPlaneSlider == hotControl);
            var xyAxisIsHot = (xyPlaneSlider == hotControl);
            var yzAxisIsHot = (yzPlaneSlider == hotControl);

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

            var size          = HandleUtility.GetHandleSize(position);
            var originalColor = Handles.color;

            var lockedAxisX = RealtimeCSG.CSGSettings.LockAxisX;
            var lockedAxisY = RealtimeCSG.CSGSettings.LockAxisY;
            var lockedAxisZ = RealtimeCSG.CSGSettings.LockAxisZ;

            var lockedAxisXY = lockedAxisX || lockedAxisY;
            var lockedAxisXZ = lockedAxisX || lockedAxisZ;
            var lockedAxisYZ = lockedAxisY || lockedAxisZ;


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

            var xAxisDisabled   = isStatic || prevDisabled || lockedAxisX || (isControlHot && !xAxisIsHot && !xzAxisIsHot && !xyAxisIsHot);
            var yAxisDisabled   = isStatic || prevDisabled || lockedAxisY || (isControlHot && !yAxisIsHot && !xyAxisIsHot && !yzAxisIsHot);
            var zAxisDisabled   = isStatic || prevDisabled || lockedAxisZ || (isControlHot && !zAxisIsHot && !xzAxisIsHot && !yzAxisIsHot);
            var xzPlaneDisabled = isStatic || prevDisabled || lockedAxisXZ || (isControlHot && !xzAxisIsHot);
            var xyPlaneDisabled = isStatic || prevDisabled || lockedAxisXY || (isControlHot && !xyAxisIsHot);
            var yzPlaneDisabled = isStatic || prevDisabled || lockedAxisYZ || (isControlHot && !yzAxisIsHot);

            var currentFocusControl = FocusControl;

            var xAxisIndirectlyFocused = (currentFocusControl == xyPlaneSlider || currentFocusControl == xzPlaneSlider);
            var yAxisIndirectlyFocused = (currentFocusControl == xyPlaneSlider || currentFocusControl == yzPlaneSlider);
            var zAxisIndirectlyFocused = (currentFocusControl == xzPlaneSlider || currentFocusControl == yzPlaneSlider);

            var xAxisSelected = xAxisIndirectlyFocused || (currentFocusControl == xAxisSlider);
            var yAxisSelected = yAxisIndirectlyFocused || (currentFocusControl == yAxisSlider);
            var zAxisSelected = zAxisIndirectlyFocused || (currentFocusControl == zAxisSlider);
            var xzAxiSelected = (currentFocusControl == xAxisSlider || currentFocusControl == zAxisSlider);
            var xyAxiSelected = (currentFocusControl == xAxisSlider || currentFocusControl == yAxisSlider);
            var yzAxiSelected = (currentFocusControl == yAxisSlider || currentFocusControl == zAxisSlider);

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


            CSGHandles.disabled = xAxisDisabled;
            {
                Handles.color = xAxisColor;

                position = CSGSlider1D.Do(camera, xAxisSlider, position, rotation * Vector3.right, size, ArrowHandleCap, snapMode, snapVertices, initFunction, shutdownFunction);
            }

            CSGHandles.disabled = yAxisDisabled;
            {
                Handles.color = yAxisColor;

                position = CSGSlider1D.Do(camera, yAxisSlider, position, rotation * Vector3.up, size, ArrowHandleCap, snapMode, snapVertices, initFunction, shutdownFunction);
            }

            CSGHandles.disabled = zAxisDisabled;
            {
                Handles.color = zAxisColor;

                position = CSGSlider1D.Do(camera, zAxisSlider, position, rotation * Vector3.forward, size, ArrowHandleCap, snapMode, snapVertices, initFunction, shutdownFunction);
            }


            CSGHandles.disabled = xzPlaneDisabled;
            if (!CSGHandles.disabled)
            {
                Handles.color = xzPlaneColor;
                position      = DoPlanarHandle(camera, xzPlaneSlider, PrincipleAxis2.XZ, position, rotation, size * 0.25f, snapMode, snapVertices, initFunction, shutdownFunction);
            }

            CSGHandles.disabled = xyPlaneDisabled;
            if (!CSGHandles.disabled)
            {
                Handles.color = xyPlaneColor;
                position      = DoPlanarHandle(camera, xyPlaneSlider, PrincipleAxis2.XY, position, rotation, size * 0.25f, snapMode, snapVertices, initFunction, shutdownFunction);
            }

            CSGHandles.disabled = yzPlaneDisabled;
            if (!CSGHandles.disabled)
            {
                Handles.color = yzPlaneColor;
                position      = DoPlanarHandle(camera, yzPlaneSlider, PrincipleAxis2.YZ, position, rotation, size * 0.25f, snapMode, snapVertices, initFunction, shutdownFunction);
            }


            CSGHandles.disabled = prevDisabled;
            Handles.color       = originalColor;

            return(position);
        }