Ejemplo n.º 1
0
        /// <summary>
        /// Convenience function to help determine what function a handle serves
        /// </summary>
        /// <param name="handleType"></param>
        /// <returns></returns>
        public static OperationEnum GetBoundingBoxOperationFromHandleType(BoundingBoxHandle.HandleTypeEnum handleType)
        {
            switch (handleType)
            {
            case BoundingBoxHandle.HandleTypeEnum.Drag:
                return(OperationEnum.Drag);

            //TODO - break this up into axis scales
            case BoundingBoxHandle.HandleTypeEnum.Scale_LBF:
            case BoundingBoxHandle.HandleTypeEnum.Scale_LBB:
            case BoundingBoxHandle.HandleTypeEnum.Scale_LTF:
            case BoundingBoxHandle.HandleTypeEnum.Scale_LTB:
            case BoundingBoxHandle.HandleTypeEnum.Scale_RBF:
            case BoundingBoxHandle.HandleTypeEnum.Scale_RBB:
            case BoundingBoxHandle.HandleTypeEnum.Scale_RTF:
            case BoundingBoxHandle.HandleTypeEnum.Scale_RTB:
                return(OperationEnum.ScaleUniform);

            case BoundingBoxHandle.HandleTypeEnum.Rotate_LTF_RTF:
            case BoundingBoxHandle.HandleTypeEnum.Rotate_LBF_RBF:
            case BoundingBoxHandle.HandleTypeEnum.Rotate_RTB_LTB:
            case BoundingBoxHandle.HandleTypeEnum.Rotate_RBB_LBB:
                return(OperationEnum.RotateX);

            case BoundingBoxHandle.HandleTypeEnum.Rotate_LTF_LBF:
            case BoundingBoxHandle.HandleTypeEnum.Rotate_RTB_RBB:
            case BoundingBoxHandle.HandleTypeEnum.Rotate_LTB_LBB:
            case BoundingBoxHandle.HandleTypeEnum.Rotate_RTF_RBF:
                return(OperationEnum.RotateY);

            case BoundingBoxHandle.HandleTypeEnum.Rotate_RBF_RBB:
            case BoundingBoxHandle.HandleTypeEnum.Rotate_RTF_RTB:
            case BoundingBoxHandle.HandleTypeEnum.Rotate_LBF_LBB:
            case BoundingBoxHandle.HandleTypeEnum.Rotate_LTF_LTB:
                return(OperationEnum.RotateZ);

            default:
                return(OperationEnum.None);
            }
        }
        /// <summary>
        /// Stores target under our transform helper and prepares for manipulation
        /// </summary>
        private void StartManipulating()
        {
            // Goes without saying
            if (!acceptInput)
            {
                return;
            }

            if (target == null)
            {
                return;
            }

            if (manipulatingNow)
            {
                return;
            }

            manipulatingNow = true;

            if (!Application.isPlaying)
            {
                return;
            }

            CreateTransforms();

            // Reset the transform helper to 1,1,1 / idenity
            transformHelper.parent     = null;
            transformHelper.localScale = Vector3.one;
            transformHelper.rotation   = Quaternion.identity;
            adjustedScaleTarget        = Vector3.one;
            smoothVelocity             = Vector3.zero;

            // Set up our transforms and gestures based on the operation we're performing
            OperationEnum operation = GetBoundingBoxOperationFromHandleType(ActiveHandle.HandleType, ActiveHandle.HandleTypeFlattened);

            Debug.Log("Operation on start manipulating is " + operation);
            switch (operation)
            {
            case OperationEnum.ScaleUniform:
            case OperationEnum.ScaleX:
            case OperationEnum.ScaleY:
            case OperationEnum.ScaleZ:
                // If we're scaling, move the transform helper to the position OPPOSITE the scale handle
                // That way the object will grow in the right direction
                BoundingBoxHandle oppositeHandle = null;
                BoundingBoxHandle.HandleTypeEnum oppositeHandleType = BoundingBoxHandle.GetOpposingHandle(ActiveHandle.HandleType);
                foreach (GameObject bbhGo in Interactibles)
                {
                    BoundingBoxHandle bbh = bbhGo.GetComponent <BoundingBoxHandle>();
                    if (bbh != null && bbh.HandleType == oppositeHandleType)
                    {
                        oppositeHandle = bbh;
                        break;
                    }
                }
                if (oppositeHandle == null)
                {
                    Debug.LogWarning("Couldn't find opposing handle for type " + ActiveHandle.HandleType);
                    transformHelper.position = transform.position;
                    targetStandIn.position   = target.transform.position;
                }
                else
                {
                    transformHelper.position = oppositeHandle.transform.position;
                    targetStandIn.position   = target.transform.position;
                }
                break;

            case OperationEnum.Drag:
                // If we're rotating or moving, move the transform helper to the center of the gizmo
                transformHelper.position = transform.position;
                targetStandIn.position   = target.transform.position;
                break;

            case OperationEnum.RotateX:
            case OperationEnum.RotateY:
            case OperationEnum.RotateZ:
            default:
                // Rotation
                // If we're rotating or moving, move the transform helper to the center of the gizmo
                transformHelper.position = transform.position;
                targetStandIn.position   = target.transform.position;
                break;
            }

            scaleOnStartManipulation = targetStandIn.localScale;

            if (target != null)
            {
                // Set our transforms to the target immediately
                targetStandIn.position   = target.transform.position;
                targetStandIn.rotation   = target.transform.rotation;
                targetStandIn.localScale = target.transform.lossyScale;
            }
        }