public static void DeleteBone(Uni2DSprite a_rSprite, Uni2DSmoothBindingBone a_rBoneToDelete)
    {
        if (a_rBoneToDelete != null)
        {
            Uni2DSmoothBindingBone rBoneParent = a_rBoneToDelete.Parent;

            // Need to pack bone hierarchy
            if (rBoneParent != null && rBoneParent.IsFakeRootBone)
            {
                Uni2DSmoothBindingBone rBoneGrandParent = rBoneParent.Parent;

                Object.DestroyImmediate(rBoneParent.gameObject);

                // We broke the branch => flatten bone hierarchy
                if (rBoneGrandParent != null && rBoneGrandParent.IsBranchBone == false)
                {
                    Uni2DSmoothBindingBone rFakeParent = rBoneGrandParent.Children[0];
                    rFakeParent.Children[0].Parent = rBoneGrandParent;

                    Object.DestroyImmediate(rFakeParent.gameObject);
                }
            }
            else
            {
                Object.DestroyImmediate(a_rBoneToDelete.gameObject);
            }
        }
    }
    // Adds a bone to a sprite from a given mouse GUI position
    public static Uni2DSmoothBindingBone AddBoneToSprite(Uni2DSprite a_rSprite, Vector2 a_f2MouseGUIPos, Uni2DSmoothBindingBone a_rBoneRoot)
    {
        Transform rSpriteTransform = a_rSprite.transform;
        Ray       oWorldRay        = HandleUtility.GUIPointToWorldRay(a_f2MouseGUIPos);
        Plane     oSpritePlane     = new Plane(rSpriteTransform.forward, rSpriteTransform.position);    // INFO: normal specific to Uni2D's planes

        float fDistance;

        if (oSpritePlane.Raycast(oWorldRay, out fDistance))
        {
            Uni2DSmoothBindingBone oBone;

            if (a_rBoneRoot == null)
            {
                oBone = Uni2DEditorSmoothBindingUtils.CreateNewBone(a_rSprite);
            }
            else
            {
                switch (a_rBoneRoot.ChildCount)
                {
                case 0:
                {
                    // Add child
                    oBone = Uni2DEditorSmoothBindingUtils.CreateNewBone(a_rBoneRoot);
                }
                break;

                case 1:
                {
                    // Create branch for existing child + add another one
                    Uni2DSmoothBindingBone oFakeParent1 = Uni2DEditorSmoothBindingUtils.CreateNewBone(a_rBoneRoot, true);
                    Uni2DSmoothBindingBone oFakeParent2 = Uni2DEditorSmoothBindingUtils.CreateNewBone(a_rBoneRoot, true);

                    a_rBoneRoot.Children[0].Parent = oFakeParent1;
                    oBone = Uni2DEditorSmoothBindingUtils.CreateNewBone(oFakeParent2);
                }
                break;

                default:
                {
                    // Add branch
                    Uni2DSmoothBindingBone oFakeParent = Uni2DEditorSmoothBindingUtils.CreateNewBone(a_rBoneRoot, true);
                    oBone = Uni2DEditorSmoothBindingUtils.CreateNewBone(oFakeParent);
                }
                break;
                }
            }

            oBone.transform.position = oWorldRay.GetPoint(fDistance);

            return(oBone);
        }

        return(null);
    }
    // Adds a bone to a sprite from a given mouse GUI position
    public static Uni2DSmoothBindingBone AddBoneToSprite( Uni2DSprite a_rSprite, Vector2 a_f2MouseGUIPos, Uni2DSmoothBindingBone a_rBoneRoot )
    {
        Transform rSpriteTransform = a_rSprite.transform;
        Ray oWorldRay = HandleUtility.GUIPointToWorldRay( a_f2MouseGUIPos );
        Plane oSpritePlane = new Plane( rSpriteTransform.forward, rSpriteTransform.position );	// INFO: normal specific to Uni2D's planes

        float fDistance;
        if( oSpritePlane.Raycast( oWorldRay, out fDistance ) )
        {
            Uni2DSmoothBindingBone oBone;

            if( a_rBoneRoot == null )
            {
                oBone = Uni2DEditorSmoothBindingUtils.CreateNewBone( a_rSprite );
            }
            else
            {
                switch( a_rBoneRoot.ChildCount )
                {
                    case 0:
                    {
                        // Add child
                        oBone = Uni2DEditorSmoothBindingUtils.CreateNewBone( a_rBoneRoot );
                    }
                    break;

                    case 1:
                    {
                        // Create branch for existing child + add another one
                        Uni2DSmoothBindingBone oFakeParent1 = Uni2DEditorSmoothBindingUtils.CreateNewBone( a_rBoneRoot, true );
                        Uni2DSmoothBindingBone oFakeParent2 = Uni2DEditorSmoothBindingUtils.CreateNewBone( a_rBoneRoot, true );

                        a_rBoneRoot.Children[ 0 ].Parent = oFakeParent1;
                        oBone = Uni2DEditorSmoothBindingUtils.CreateNewBone( oFakeParent2 );
                    }
                    break;

                    default:
                    {
                        // Add branch
                        Uni2DSmoothBindingBone oFakeParent = Uni2DEditorSmoothBindingUtils.CreateNewBone( a_rBoneRoot, true );
                        oBone = Uni2DEditorSmoothBindingUtils.CreateNewBone( oFakeParent );
                    }
                    break;
                }
            }

            oBone.transform.position = oWorldRay.GetPoint( fDistance );

            return oBone;
        }

        return null;
    }
Exemplo n.º 4
0
    private float ComputeDistanceToBone(Uni2DSmoothBindingBone a_rBone, Vector3 a_f3VertexCoords)
    {
        Vector3 f3LocalRootPosition = this.transform.InverseTransformPoint(a_rBone.transform.position);
        float   fMinDistance        = Vector3.Distance(f3LocalRootPosition, a_f3VertexCoords);

        foreach (Uni2DSmoothBindingBone rBoneChild in a_rBone.Children)
        {
            float fDistance = Uni2DMathUtils.DistancePointToLineSegment(a_f3VertexCoords, f3LocalRootPosition, this.transform.InverseTransformPoint(rBoneChild.transform.position));
            fMinDistance = Mathf.Min(fMinDistance, fDistance);
        }

        return(fMinDistance);
    }
 // Specialized picking call used in anim mode
 public static Uni2DSmoothBindingBone PickNearestBoneInAnimMode(Uni2DSprite a_rSprite,
                                                                Vector2 a_f2MouseGUIPos,
                                                                out BoneHandle a_ePickedHandle,
                                                                Uni2DSmoothBindingBone a_rExclude = null)
 {
     return(Uni2DEditorSmoothBindingUtils.PickNearestBone(a_rSprite,
                                                          a_f2MouseGUIPos,
                                                          out a_ePickedHandle,
                                                          a_rExclude,
                                                          true,
                                                          Uni2DEditorSmoothBindingUtils.boneAnimPickRadius,
                                                          Uni2DEditorSmoothBindingUtils.boneLinkAnimPickRadius));
 }
 // Specialized picking call used in posing mode and when drawing disc handles
 public static Uni2DSmoothBindingBone PickNearestBoneArticulationInPosingMode(Uni2DSprite a_rSprite,
                                                                              Vector2 a_f2MouseGUIPos,
                                                                              out BoneHandle a_ePickedHandle,
                                                                              Uni2DSmoothBindingBone a_rExclude = null)
 {
     return(Uni2DEditorSmoothBindingUtils.PickNearestBone(a_rSprite,
                                                          a_f2MouseGUIPos,
                                                          out a_ePickedHandle,
                                                          a_rExclude,
                                                          false,
                                                          Uni2DEditorSmoothBindingUtils.bonePosingPickRadius,
                                                          Uni2DEditorSmoothBindingUtils.boneLinkPosingPickRadius));
 }
    ///// Bone creation /////
    private static Uni2DSmoothBindingBone CreateNewBone(Transform a_rTransform, bool a_bCreateFakeBone = false)
    {
        GameObject             oBoneGameObject = new GameObject( );
        Transform              oBoneTransform  = oBoneGameObject.transform;
        Uni2DSmoothBindingBone oBone           = oBoneGameObject.AddComponent <Uni2DSmoothBindingBone>( );

        oBoneGameObject.name = (a_bCreateFakeBone ? "Uni2DFakeBone_" : "Uni2DBone_") + Mathf.Abs(oBoneGameObject.GetInstanceID( ));

        oBoneTransform.position      = a_rTransform.position;
        oBoneTransform.parent        = a_rTransform;
        oBoneTransform.localRotation = Quaternion.identity;

        return(oBone);
    }
Exemplo n.º 8
0
    public void RotateBoneAlongSpritePlane(Vector2 a_f2MouseGUIPos)
    {
        Uni2DSprite rSprite = this.Sprite;

        if (rSprite != null)
        {
            Transform rSpriteTransform = rSprite.transform;
            Ray       oWorldRay        = HandleUtility.GUIPointToWorldRay(a_f2MouseGUIPos);
            Plane     oSpritePlane     = new Plane(rSpriteTransform.forward, rSpriteTransform.position);

            float fDistance;
            if (oSpritePlane.Raycast(oWorldRay, out fDistance))
            {
                Uni2DSmoothBindingBone rBoneToMoveParent = this.Parent;
                if (rBoneToMoveParent != null)
                {
                    float   fAngle;
                    Vector3 f3Axis;

                    Transform rBoneToMoveParentTransform = rBoneToMoveParent.transform;
                    Vector3   f3BoneToMoveParentPosition = rBoneToMoveParentTransform.position;

                    Vector3 f3CurrentDirection = this.transform.position - f3BoneToMoveParentPosition;
                    Vector3 f3NewDirection     = oWorldRay.GetPoint(fDistance) - f3BoneToMoveParentPosition;

                    Quaternion oRot = Quaternion.FromToRotation(f3CurrentDirection, f3NewDirection);
                    oRot.ToAngleAxis(out fAngle, out f3Axis);

                                        #if AFTER_UNITY_4_3
                    Undo.RecordObject(rBoneToMoveParentTransform, "Rotate Bone : " + rBoneToMoveParentTransform);
                                        #endif
                    rBoneToMoveParentTransform.Rotate(f3Axis, fAngle, Space.World);
                    EditorUtility.SetDirty(rBoneToMoveParentTransform);
                }
                else
                {
                                        #if AFTER_UNITY_4_3
                    Undo.RecordObject(transform, "Move Bone : " + transform);
                                        #endif
                    this.transform.position = oWorldRay.GetPoint(fDistance);
                    EditorUtility.SetDirty(this.transform);
                }
            }
        }
    }
Exemplo n.º 9
0
    // Save bone pose position
    public void SaveBonePosePosition()
    {
        if (m_oBonePosePosition == null)
        {
            m_oBonePosePosition = new Uni2DBonePosition();
        }
        if (m_oBonePosePosition.SavePositionIfNeeded(transform))
        {
            // if the pose position has been changed reset the manipulated position
            ResetBoneManipulatedPosition();

            // if this bone have a parent reset the parent manipulated position also
            Uni2DSmoothBindingBone rParentBone = Parent;
            if (rParentBone != null)
            {
                rParentBone.ResetBoneManipulatedPosition();
            }
        }
    }
Exemplo n.º 10
0
    public void Reset(Uni2DSprite a_rSprite, bool a_bKeepCurrentModeIfPossible = false)
    {
        m_rLastAddedBone   = null;
        m_rBoneChainOrigin = null;
        Uni2DEditorSmoothBindingGUI.activeBone = null;

        m_f2MouseGUIOffset = Vector2.zero;
        ms_eEditorTool     = BoneEditorTool.Select;

        BoneEditMode eSavedEditMode = Uni2DEditorSmoothBindingGUI.CurrentBoneEditMode;

        Uni2DEditorSmoothBindingGUI.CurrentBoneEditMode = BoneEditMode.None;

        ms_rSprite = a_rSprite;

        if (a_bKeepCurrentModeIfPossible)
        {
            Uni2DEditorSmoothBindingGUI.CurrentBoneEditMode = eSavedEditMode;
        }
    }
Exemplo n.º 11
0
    private void DrawBoneGizmos(Color a_rBoneColor, Color a_rActiveBoneColor, Color a_rRootBoneColor, Color a_rActiveRootBoneColor, bool a_bHideBoneSpheres, float a_fBoneGizmoSizeLimit)
    {
        float fSphereSize;
        float fLimitFactor;

        foreach (Uni2DSmoothBindingBone rBone in this.Bones)
        {
            bool bIsActiveBone = (rBone == Uni2DEditorSmoothBindingGUI.activeBone);
            Uni2DSmoothBindingBone rBoneParent = rBone.Parent;

            Transform rBoneTransform = rBone.transform;
            Vector3   f3BonePos      = rBoneTransform.position;

            if (rBoneParent != null && rBone.IsFakeRootBone == false)
            {
                Gizmos.color = bIsActiveBone ? a_rActiveBoneColor : a_rBoneColor;
                this.DrawBone(rBoneParent.transform, rBoneTransform, a_fBoneGizmoSizeLimit);
            }

            if (a_bHideBoneSpheres == false)
            {
                if (rBoneParent == null)
                {
                    Gizmos.color = bIsActiveBone ? a_rActiveRootBoneColor : a_rRootBoneColor;
                    fSphereSize  = 0.2f;
                    fLimitFactor = 1.0f;
                }
                else
                {
                    Gizmos.color = bIsActiveBone ? a_rActiveBoneColor : a_rBoneColor;
                    fSphereSize  = 0.1f;
                    fLimitFactor = 0.5f;
                }

                Gizmos.DrawSphere(f3BonePos,
                                  Mathf.Clamp(HandleUtility.GetHandleSize(f3BonePos) * fSphereSize,
                                              0.0f,
                                              a_fBoneGizmoSizeLimit * fLimitFactor));
            }
        }
    }
Exemplo n.º 12
0
    public void Break( )
    {
        Transform rSpriteTransform = this.Sprite.transform;
        Transform rBoneTransform   = this.transform;

        // Need to pack bone hierarchy
        Uni2DSmoothBindingBone rBoneParent = this.Parent;

        if (rBoneParent != null && rBoneParent.IsFakeRootBone)
        {
            // Break bone then destroy fake parent
            rBoneTransform.parent = rSpriteTransform;
            Uni2DSmoothBindingBone rBoneGrandParent = rBoneParent.Parent;
            Object.DestroyImmediate(rBoneParent.gameObject);

            // We broke the branch => flatten bone hierarchy
            if (rBoneGrandParent != null && rBoneGrandParent.IsBranchBone == false)                     // Grand parent is no longer a branch
            {
                Transform rBoneGrandParentTransform = rBoneGrandParent.transform;

                Uni2DSmoothBindingBone rFakeChild = rBoneGrandParent.Children[0];                       // Fake child

                Transform rBoneChildTransform = rFakeChild.Children[0].transform;
                rBoneChildTransform.parent = rBoneGrandParentTransform;                 // Set grand parent as new parent
                Object.DestroyImmediate(rFakeChild.gameObject);                         // Delete fake child

                EditorUtility.SetDirty(rBoneChildTransform);
            }
        }
        else
        {
            rBoneTransform.parent = rSpriteTransform;
        }

        EditorUtility.SetDirty(rBoneTransform);
    }
    ///// Picking /////
    private static Uni2DSmoothBindingBone PickNearestBone( Uni2DSprite a_rSprite,
		Vector2 a_f2MouseGUIPos,
		out BoneHandle a_ePickedHandle,
		Uni2DSmoothBindingBone a_rExclude = null,
		bool a_bAlsoPickBoneLink    = false,
		float a_fBonePickRadius     = Uni2DEditorSmoothBindingUtils.bonePosingPickRadius,
		float a_fBoneLinkPickRadius = Uni2DEditorSmoothBindingUtils.boneLinkAnimPickRadius )
    {
        Uni2DSmoothBindingBone rNearestBone     = null;
        Uni2DSmoothBindingBone rNearestBoneLink = null;

        // Squarred pick radius
        float fMinSqrBoneDistance  = a_fBonePickRadius * a_fBonePickRadius;
        float fMinBoneLinkDistance = a_fBoneLinkPickRadius;

        Uni2DSmoothBindingBone[ ] rBones = a_rSprite.Bones; //rSpriteTransform.GetComponentsInChildren<Transform>( false ).Except( oBonesToExclude ).ToArray( );

        // Look for nearest bone
        for( int iBoneIndex = 0, iBoneCount = rBones.Length; iBoneIndex < iBoneCount; ++iBoneIndex )
        {
            Uni2DSmoothBindingBone rBone = rBones[ iBoneIndex ];

            if( a_rExclude != rBone && rBone.IsFakeRootBone == false )
            {
                Vector2 f2BoneGUIPos = HandleUtility.WorldToGUIPoint( rBone.transform.position );
                Vector2 f2BoneToMouseGUI = a_f2MouseGUIPos - f2BoneGUIPos;

                float fSqrDistance = f2BoneToMouseGUI.sqrMagnitude;

                // New min/nearest
                if( fSqrDistance < fMinSqrBoneDistance )
                {
                    rNearestBone = rBone;
                    fMinSqrBoneDistance = fSqrDistance;
                }

                // Look for nearest bone link
                Uni2DSmoothBindingBone rBoneParent = rBone.Parent;
                if( a_bAlsoPickBoneLink && rBoneParent != null )
                {
                    float fLinkDistance = HandleUtility.DistancePointToLineSegment( a_f2MouseGUIPos,
                        f2BoneGUIPos,
                        HandleUtility.WorldToGUIPoint( rBoneParent.transform.position ) );

                    if( fLinkDistance < fMinBoneLinkDistance )
                    {
                        fMinBoneLinkDistance = fLinkDistance;
                        rNearestBoneLink = rBone;
                    }
                }
            }
        }

        // Picking result
        if( rNearestBone == null && rNearestBoneLink == null )
        {
            a_ePickedHandle = BoneHandle.None;
        }
        else if( rNearestBone != null )
        {
            if( fMinSqrBoneDistance <= a_fBonePickRadius * a_fBonePickRadius * 0.25f )
            {
                a_ePickedHandle = /*invertActionAreas == false ?*/ BoneHandle.InnerDisc; //: BoneHandle.OuterDisc;
            }
            else
            {
                a_ePickedHandle = /*invertActionAreas == false ?*/ BoneHandle.OuterDisc; //: BoneHandle.InnerDisc;
            }
        }
        else
        {
            rNearestBone    = rNearestBoneLink;
            a_ePickedHandle = BoneHandle.Link;
        }

        return rNearestBone;
    }
    // Specialized picking call used in posing mode
    public static Uni2DSmoothBindingBone PickNearestBoneInPosingMode( Uni2DSprite a_rSprite,
		Vector2 a_f2MouseGUIPos,
		out BoneHandle a_ePickedHandle,
		Uni2DSmoothBindingBone a_rExclude = null )
    {
        return Uni2DEditorSmoothBindingUtils.PickNearestBone( a_rSprite,
            a_f2MouseGUIPos,
            out a_ePickedHandle,
            a_rExclude,
            true,
            Uni2DEditorSmoothBindingUtils.bonePosingPickRadius,
            Uni2DEditorSmoothBindingUtils.boneLinkPosingPickRadius );
    }
    public static void DeleteBone( Uni2DSprite a_rSprite, Uni2DSmoothBindingBone a_rBoneToDelete )
    {
        if( a_rBoneToDelete != null )
        {
            Uni2DSmoothBindingBone rBoneParent = a_rBoneToDelete.Parent;

            // Need to pack bone hierarchy
            if( rBoneParent != null && rBoneParent.IsFakeRootBone )
            {
                Uni2DSmoothBindingBone rBoneGrandParent = rBoneParent.Parent;

                Object.DestroyImmediate( rBoneParent.gameObject );

                // We broke the branch => flatten bone hierarchy
                if( rBoneGrandParent != null && rBoneGrandParent.IsBranchBone == false )
                {
                    Uni2DSmoothBindingBone rFakeParent = rBoneGrandParent.Children[ 0 ];
                    rFakeParent.Children[ 0 ].Parent = rBoneGrandParent;

                    Object.DestroyImmediate( rFakeParent.gameObject );
                }
            }
            else
            {
                Object.DestroyImmediate( a_rBoneToDelete.gameObject );
            }
        }
    }
 public static Uni2DSmoothBindingBone CreateNewBone( Uni2DSmoothBindingBone a_rBoneParent, bool a_bCreateFakeBone = false )
 {
     return Uni2DEditorSmoothBindingUtils.CreateNewBone( a_rBoneParent.transform, a_bCreateFakeBone );
 }
Exemplo n.º 17
0
	private float ComputeDistanceToBone( Uni2DSmoothBindingBone a_rBone, Vector3 a_f3VertexCoords )
	{
		Vector3 f3LocalRootPosition = this.transform.InverseTransformPoint( a_rBone.transform.position );
		float fMinDistance = Vector3.Distance( f3LocalRootPosition, a_f3VertexCoords );
		
		foreach( Uni2DSmoothBindingBone rBoneChild in a_rBone.Children )
		{
			float fDistance = Uni2DMathUtils.DistancePointToLineSegment( a_f3VertexCoords, f3LocalRootPosition, this.transform.InverseTransformPoint( rBoneChild.transform.position ) );
			fMinDistance = Mathf.Min( fMinDistance, fDistance );
		}
		
		return fMinDistance;
	}
Exemplo n.º 18
0
	// Add bone
	public void AddBone(Uni2DSmoothBindingBone a_rBone)
	{
		CheckForOldBones();
		m_oBones.Add(a_rBone);
	}
Exemplo n.º 19
0
    /*
     * private void DrawAnimEditBoneHandles( Event a_rEvent )
     * {
     *      // TODO
     * }
     */

    private void DrawPoseEditBoneHandles(Event a_rEvent)
    {
        Color oInnerBoneDiscColor = Uni2DEditorPreferences.InnerBoneDiscHandleColor;
        Color oOuterBoneDiscColor = Uni2DEditorPreferences.OuterBoneDiscHandleColor;

        Color oInnerRootBoneDiscColor = Uni2DEditorPreferences.InnerRootBoneDiscHandleColor;
        Color oOuterRootBoneDiscColor = Uni2DEditorPreferences.OuterRootBoneDiscHandleColor;

        Vector3 f3CameraForward = SceneView.currentDrawingSceneView.camera.transform.forward;

        BoneHandle             eBoneHandle;
        Uni2DSmoothBindingBone rNearestBone = Uni2DEditorSmoothBindingUtils.PickNearestBoneArticulationInPosingMode(ms_rSprite, a_rEvent.mousePosition, out eBoneHandle, null);

        Uni2DSmoothBindingBone[] oBones = ms_rSprite.Bones.Except(new Uni2DSmoothBindingBone[] { rNearestBone }).ToArray( );
        //Transform rActiveBone = Uni2DEditorSmoothBindingGUI.activeBone;
        //Selection.activeTransform.GetComponentsInChildren<Transform>( false ).Except( new Transform[ ]{ rRootBone, rNearestBone } ).ToArray( );

        Transform rActiveBoneTransform = Uni2DEditorSmoothBindingGUI.activeBone != null ? Uni2DEditorSmoothBindingGUI.activeBone.transform : null;

        oInnerBoneDiscColor.a     = 0.2f;
        oOuterBoneDiscColor.a     = 0.2f;
        oInnerRootBoneDiscColor.a = 0.2f;
        oOuterRootBoneDiscColor.a = 0.2f;

        for (int iBoneIndex = 0, iBoneCount = oBones.Length; iBoneIndex < iBoneCount; ++iBoneIndex)
        {
            Uni2DSmoothBindingBone rBone = oBones[iBoneIndex];

            if (rBone.IsFakeRootBone == false)
            {
                Vector3 f3BonePosition = rBone.transform.position;
                float   fHandleSize    = HandleUtility.GetHandleSize(f3BonePosition);

                if (rBone.Parent != null)
                {
                    // Outer disc
                    Handles.color = oOuterBoneDiscColor;
                    Handles.DrawSolidDisc(f3BonePosition,
                                          f3CameraForward,
                                          0.25f * fHandleSize);

                    // Inner disc
                    Handles.color = oInnerBoneDiscColor;
                    Handles.DrawSolidDisc(f3BonePosition,
                                          f3CameraForward,
                                          0.125f * fHandleSize);
                }
                else
                {
                    // Outer disc
                    Handles.color = oOuterRootBoneDiscColor;
                    Handles.DrawSolidDisc(f3BonePosition,
                                          f3CameraForward,
                                          0.25f * fHandleSize);

                    // Inner disc
                    Handles.color = oInnerRootBoneDiscColor;
                    Handles.DrawSolidDisc(f3BonePosition,
                                          f3CameraForward,
                                          0.125f * fHandleSize);
                }
            }
        }

        if (rNearestBone != null)
        {
            MouseCursor eMouseCursor;
            //Color oHandleColor;

            if (eBoneHandle == BoneHandle.InnerDisc)
            {
                                #if UNITY_3_5
                // Unity 3.5.x: Display an arrow while moving, and a link cursor while hovering
                eMouseCursor = rActiveBoneTransform != null ? MouseCursor.Arrow : MouseCursor.Link;
                                #else
                // Unity 4.x.y: Display an arrow with a plus sign
                eMouseCursor = MouseCursor.ArrowPlus;
                                #endif

                oInnerBoneDiscColor.a     = 0.8f;
                oInnerRootBoneDiscColor.a = 0.8f;
                oOuterBoneDiscColor.a     = 0.2f;
                oOuterRootBoneDiscColor.a = 0.2f;
            }
            else
            {
                eMouseCursor              = MouseCursor.MoveArrow;
                oInnerBoneDiscColor.a     = 0.2f;
                oInnerRootBoneDiscColor.a = 0.2f;
                oOuterBoneDiscColor.a     = 0.8f;
                oOuterRootBoneDiscColor.a = 0.8f;
            }

            Handles.BeginGUI( );
            {
                Vector2 f2MousePos  = a_rEvent.mousePosition;
                Rect    oCursorRect = new Rect(f2MousePos.x - 16.0f, f2MousePos.y - 16.0f, 32.0f, 32.0f);

                EditorGUIUtility.AddCursorRect(oCursorRect, eMouseCursor);
            }
            Handles.EndGUI( );

            Vector3 f3NearestBonePos = rNearestBone.transform.position;
            float   fHandleSize      = HandleUtility.GetHandleSize(f3NearestBonePos);

            if (rNearestBone.Parent != null)
            {
                // Outer disc
                Handles.color = oOuterBoneDiscColor;
                Handles.DrawSolidDisc(f3NearestBonePos,
                                      f3CameraForward,
                                      0.25f * fHandleSize);

                // Inner disc
                Handles.color = oInnerBoneDiscColor;
                Handles.DrawSolidDisc(f3NearestBonePos,
                                      f3CameraForward,
                                      0.125f * fHandleSize);
            }
            else
            {
                // Outer disc
                Handles.color = oOuterRootBoneDiscColor;
                Handles.DrawSolidDisc(f3NearestBonePos,
                                      f3CameraForward,
                                      0.25f * fHandleSize);

                // Inner disc
                Handles.color = oInnerRootBoneDiscColor;
                Handles.DrawSolidDisc(f3NearestBonePos,
                                      f3CameraForward,
                                      0.125f * fHandleSize);
            }
        }

        if (rActiveBoneTransform != null)
        {
            Handles.color = Uni2DEditorSmoothBindingGUI.activeBone.Parent != null
                                ? Uni2DEditorPreferences.SelectedBoneDiscHandleOutlineColor
                                : Uni2DEditorPreferences.SelectedRootBoneDiscHandleOutlineColor;

            float fHandleSize = HandleUtility.GetHandleSize(rActiveBoneTransform.position);
            Handles.DrawWireDisc(rActiveBoneTransform.position, f3CameraForward, 0.25f * fHandleSize);
        }
    }
    public static Uni2DSmoothBindingBone CreateNewBone(Uni2DSprite a_rSprite)
    {
        Uni2DSmoothBindingBone rBone = Uni2DEditorSmoothBindingUtils.CreateNewBone(a_rSprite, a_rSprite.transform, false);

        return(rBone);
    }
    ///// Picking /////
    private static Uni2DSmoothBindingBone PickNearestBone(Uni2DSprite a_rSprite,
                                                          Vector2 a_f2MouseGUIPos,
                                                          out BoneHandle a_ePickedHandle,
                                                          Uni2DSmoothBindingBone a_rExclude = null,
                                                          bool a_bAlsoPickBoneLink          = false,
                                                          float a_fBonePickRadius           = Uni2DEditorSmoothBindingUtils.bonePosingPickRadius,
                                                          float a_fBoneLinkPickRadius       = Uni2DEditorSmoothBindingUtils.boneLinkAnimPickRadius)
    {
        Uni2DSmoothBindingBone rNearestBone     = null;
        Uni2DSmoothBindingBone rNearestBoneLink = null;

        // Squarred pick radius
        float fMinSqrBoneDistance  = a_fBonePickRadius * a_fBonePickRadius;
        float fMinBoneLinkDistance = a_fBoneLinkPickRadius;

        Uni2DSmoothBindingBone[] rBones = a_rSprite.Bones;          //rSpriteTransform.GetComponentsInChildren<Transform>( false ).Except( oBonesToExclude ).ToArray( );

        // Look for nearest bone
        for (int iBoneIndex = 0, iBoneCount = rBones.Length; iBoneIndex < iBoneCount; ++iBoneIndex)
        {
            Uni2DSmoothBindingBone rBone = rBones[iBoneIndex];

            if (a_rExclude != rBone && rBone.IsFakeRootBone == false)
            {
                Vector2 f2BoneGUIPos     = HandleUtility.WorldToGUIPoint(rBone.transform.position);
                Vector2 f2BoneToMouseGUI = a_f2MouseGUIPos - f2BoneGUIPos;

                float fSqrDistance = f2BoneToMouseGUI.sqrMagnitude;

                // New min/nearest
                if (fSqrDistance < fMinSqrBoneDistance)
                {
                    rNearestBone        = rBone;
                    fMinSqrBoneDistance = fSqrDistance;
                }

                // Look for nearest bone link
                Uni2DSmoothBindingBone rBoneParent = rBone.Parent;
                if (a_bAlsoPickBoneLink && rBoneParent != null)
                {
                    float fLinkDistance = HandleUtility.DistancePointToLineSegment(a_f2MouseGUIPos,
                                                                                   f2BoneGUIPos,
                                                                                   HandleUtility.WorldToGUIPoint(rBoneParent.transform.position));

                    if (fLinkDistance < fMinBoneLinkDistance)
                    {
                        fMinBoneLinkDistance = fLinkDistance;
                        rNearestBoneLink     = rBone;
                    }
                }
            }
        }

        // Picking result
        if (rNearestBone == null && rNearestBoneLink == null)
        {
            a_ePickedHandle = BoneHandle.None;
        }
        else if (rNearestBone != null)
        {
            if (fMinSqrBoneDistance <= a_fBonePickRadius * a_fBonePickRadius * 0.25f)
            {
                a_ePickedHandle = /*invertActionAreas == false ?*/ BoneHandle.InnerDisc;                 //: BoneHandle.OuterDisc;
            }
            else
            {
                a_ePickedHandle = /*invertActionAreas == false ?*/ BoneHandle.OuterDisc;                 //: BoneHandle.InnerDisc;
            }
        }
        else
        {
            rNearestBone    = rNearestBoneLink;
            a_ePickedHandle = BoneHandle.Link;
        }

        return(rNearestBone);
    }
    private void UpdateAnimEditGUI( Event a_rEvent )
    {
        Vector2 f2MouseGUIPos = a_rEvent.mousePosition;

        if( a_rEvent.isMouse )
        {
            int iMouseButton = a_rEvent.button;

            switch( a_rEvent.type )
            {
                case EventType.MouseMove:
                {
                    // Reset state
                    m_bIsDragging = false;

                    // Consume event
                    a_rEvent.Use( );
                }
                break;	// End MouseMove

                case EventType.MouseDown:
                {
                    // Left click
                    if( iMouseButton == 0 )
                    {
                        // Reset drag state
                        m_bIsDragging = false;

                        // Pick bones in scene
                        BoneHandle ePickedHandle;
                        Uni2DSmoothBindingBone rNearestBone = Uni2DEditorSmoothBindingUtils.PickNearestBoneInAnimMode( ms_rSprite, f2MouseGUIPos, out ePickedHandle, null );

                        // Selection
                        if( ms_eEditorTool == BoneEditorTool.Select && rNearestBone != null )
                        {
                            // Change selection and editor tool
                            Uni2DEditorSmoothBindingGUI.activeBone = rNearestBone;

                            m_f2MouseGUIOffset = f2MouseGUIPos - HandleUtility.WorldToGUIPoint( rNearestBone.transform.position );
                        }	// end if( editor tool == select )
                    }

                    // Consume mouse event
                    a_rEvent.Use( );
                }
                break;	// end MouseDown

                case EventType.MouseDrag:
                {
                    if( iMouseButton == 0 )
                    {
                        switch( ms_eEditorTool )
                        {
                            case BoneEditorTool.Move:
                            {
                                // Something dragged? MOVE IT
                                if( Uni2DEditorSmoothBindingGUI.activeBone != null )
                                {
                                    // We're moving an existing bone => register undo at first drag event
                                    if( m_bIsDragging == false )
                                    {
                                        Undo.SetSnapshotTarget( Uni2DEditorSmoothBindingGUI.activeBone, "Move Uni2D Bone" );
                                        Undo.CreateSnapshot( );
                                        Undo.RegisterSnapshot( );
                                    }

                                    // Move/drag along sprite plane
                                    Uni2DEditorSmoothBindingGUI.activeBone.RotateBoneAlongSpritePlane( f2MouseGUIPos - m_f2MouseGUIOffset );
                                }
                                m_bIsDragging = true;
                            }
                            break;

                            case BoneEditorTool.Select:
                            {
                                // Dragging a bone in select mode == dragging on inner disc => add a child to active bone
                                if( Uni2DEditorSmoothBindingGUI.activeBone != null && m_bIsDragging == false )
                                {
                                    ms_eEditorTool = BoneEditorTool.Move;
                                    m_f2MouseGUIOffset = f2MouseGUIPos - HandleUtility.WorldToGUIPoint( Uni2DEditorSmoothBindingGUI.activeBone.transform.position );

                                    Uni2DEditorSmoothBindingGUI.activeBone.RotateBoneAlongSpritePlane( f2MouseGUIPos - m_f2MouseGUIOffset );
                                }
                                m_bIsDragging = true;
                            }
                            break;
                        }
                    }

                    // Consume event
                    a_rEvent.Use( );
                }
                break;	// End MouseDrag

                case EventType.MouseUp:
                {
                    if( iMouseButton == 0 )
                    {
                        if( ms_eEditorTool == BoneEditorTool.Move )
                        {
                            Undo.ClearSnapshotTarget( );
                            Undo.RegisterSnapshot( );
                            ms_eEditorTool = BoneEditorTool.Select;
                        }

                        // Reset dragging state
                        m_bIsDragging = false;
                        m_f2MouseGUIOffset = Vector2.zero;
                    }	// end if( left button )
                    else if( iMouseButton == 1 && ms_eEditorTool == BoneEditorTool.Move )	// Delete / stop bone creation
                    {
                        ms_eEditorTool = BoneEditorTool.Select;
                    }	// End if( right button )

                    // Consume event
                    a_rEvent.Use( );
                }
                break;	// End MouseUp
            }	// End switch( event.type )
        }	// end if( mouse events )
        else if( a_rEvent.isKey && a_rEvent.type == EventType.keyDown )
        {
            switch( a_rEvent.keyCode )
            {
                case KeyCode.Escape:
                {
                    switch( ms_eEditorTool )
                    {
                        case BoneEditorTool.Select:
                        {
                            if( Uni2DEditorSmoothBindingGUI.activeBone != null )
                            {
                                Uni2DEditorSmoothBindingGUI.activeBone = null;
                            }
                            else
                            {
                                Uni2DEditorSmoothBindingGUI.CurrentBoneEditMode = BoneEditMode.None;
                            }
                        }
                        break;

                        case BoneEditorTool.Move:
                        {
                            Undo.RestoreSnapshot( );
                            ms_eEditorTool = BoneEditorTool.Select;
                        }
                        break;
                    }
                    a_rEvent.Use( );
                }
                break;	// End Escape

                case KeyCode.Return:
                case KeyCode.KeypadEnter:
                {
                    ms_eEditorTool = BoneEditorTool.Select;
                    a_rEvent.Use( );
                }
                break;	// End Return / KeypadEnter
            }	// end switch( event.type )
        }	// end if( key down events )
        else
        {
            switch( a_rEvent.type )
            {
                case EventType.ValidateCommand:
                {
                    if( a_rEvent.commandName == "Delete" || a_rEvent.commandName == "UndoRedoPerformed" )
                    {
                        a_rEvent.Use( );
                    }
                }
                break;	// end ValidateCommand

                case EventType.ExecuteCommand:
                {
                    if( a_rEvent.commandName == "Delete" )
                    {
                        a_rEvent.Use( );
                    }
                    else if( a_rEvent.commandName == "UndoRedoPerformed" )
                    {
                        Uni2DEditorSmoothBindingGUI.activeBone = null;
                        ms_eEditorTool = BoneEditorTool.Select;
                        //Uni2DEditorSmoothBindingGUI.CurrentBoneEditMode = BoneEditMode.None;
                        a_rEvent.Use( );
                    }
                }
                break;	// end ExecuteCommand
            }	// end switch( event.type )
        }	// end else
    }
Exemplo n.º 23
0
    private void UpdatePoseEditGUI(Event a_rEvent)
    {
        //Transform rRootTransform = Selection.activeTransform;
        Vector2 f2MouseGUIPos = a_rEvent.mousePosition;

        if (a_rEvent.isMouse)
        {
            int iMouseButton = a_rEvent.button;

            switch (a_rEvent.type)
            {
            case EventType.MouseMove:
            {
                // Reset state
                m_bIsDragging = false;

                // Something selected, edit tool used => move bone
                if (Uni2DEditorSmoothBindingGUI.activeBone != null && ms_eEditorTool != BoneEditorTool.Select)
                {
                    Uni2DEditorSmoothBindingGUI.activeBone.MoveBoneAlongSpritePlane(f2MouseGUIPos - m_f2MouseGUIOffset, a_rEvent.alt);
                }

                // Consume event
                a_rEvent.Use( );
            }
            break;                      // End MouseMove

            case EventType.MouseDown:
            {
                // Left click
                if (iMouseButton == 0)
                {
                    // Reset drag state
                    m_bIsDragging = false;


                    // Selection
                    if (ms_eEditorTool == BoneEditorTool.Select)
                    {
                        // Pick bones in scene
                        BoneHandle             ePickedHandle;
                        Uni2DSmoothBindingBone rNearestBone = Uni2DEditorSmoothBindingUtils.PickNearestBoneInPosingMode(ms_rSprite, f2MouseGUIPos, out ePickedHandle, null);

                        // Outer disc => move
                        if (ePickedHandle == BoneHandle.OuterDisc || ePickedHandle == BoneHandle.Link)
                        {
                            if (rNearestBone != null)
                            {
                                // Change selection and editor tool
                                Uni2DEditorSmoothBindingGUI.activeBone = rNearestBone;
                                ms_eEditorTool = BoneEditorTool.Move;

                                m_f2MouseGUIOffset = f2MouseGUIPos - HandleUtility.WorldToGUIPoint(rNearestBone.transform.position);
                            }
                        }
                        else                                 // Bone picked via inner disc or no bone picked
                        {
                            // A bone has been picked (via inner disc)
                            // Set selection to this bone
                            if (rNearestBone != null)
                            {
                                Uni2DEditorSmoothBindingGUI.activeBone = rNearestBone;
                            }
                            else                                     // No bone picked
                            {
                                // Get rid of selection if any
                                if (Uni2DEditorSmoothBindingGUI.activeBone != null)
                                {
                                    Uni2DEditorSmoothBindingGUI.activeBone = null;
                                }
                                else                                         // no selection, no bone picked => create a new bone chain
                                {
                                    m_rLastAddedBone = null;

                                    // Create a new root and set it as current active bone
                                    Uni2DEditorSmoothBindingGUI.activeBone = Uni2DEditorSmoothBindingUtils.AddBoneToSprite(ms_rSprite, f2MouseGUIPos, null);
                                    m_rBoneChainOrigin = Uni2DEditorSmoothBindingGUI.activeBone;

                                    // The bone is not a child of a newly created root
                                    //m_bStartFromExistingBone = false;

                                    // Change editor tool
                                    ms_eEditorTool = BoneEditorTool.Create;
                                }
                            }
                        }
                    }                                   // end if( editor tool == select )
                }

                // Consume mouse event
                a_rEvent.Use( );
            }
            break;                      // end MouseDown

            case EventType.MouseDrag:
            {
                if (iMouseButton == 0)
                {
                    switch (ms_eEditorTool)
                    {
                    case BoneEditorTool.Move:
                    {
                        // Something dragged? MOVE IT
                        if (Uni2DEditorSmoothBindingGUI.activeBone != null)
                        {
                            // We're moving an existing bone => register undo at first drag event
                            if (m_bIsDragging == false)
                            {
                                                                                #if BEFORE_UNITY_4_3
                                Undo.SetSnapshotTarget(Uni2DEditorSmoothBindingGUI.activeBone, "Move Uni2D Bone");
                                Undo.CreateSnapshot( );
                                Undo.RegisterSnapshot( );
                                                                                #else
                                Uni2DUndo.RegisterFullObjectHierarchyUndo(ms_rSprite.gameObject, "Move Uni2D Bone");
                                                                                #endif
                            }

                            // Move/drag along sprite plane;
                            Uni2DEditorSmoothBindingGUI.activeBone.MoveBoneAlongSpritePlane(f2MouseGUIPos - m_f2MouseGUIOffset, a_rEvent.alt);
                        }
                        m_bIsDragging = true;
                    }
                    break;

                    case BoneEditorTool.Select:
                    {
                        // Dragging a bone in select mode == dragging on inner disc => add a child to active bone
                        if (Uni2DEditorSmoothBindingGUI.activeBone != null && m_bIsDragging == false)
                        {
                            m_rBoneChainOrigin = Uni2DEditorSmoothBindingGUI.activeBone;
                            m_rLastAddedBone   = null;
                            Uni2DEditorSmoothBindingGUI.activeBone = Uni2DEditorSmoothBindingUtils.AddBoneToSprite(ms_rSprite, f2MouseGUIPos, Uni2DEditorSmoothBindingGUI.activeBone);
                            //m_bStartFromExistingBone = true;
                            ms_eEditorTool = BoneEditorTool.Create;
                        }
                        m_bIsDragging = true;
                    }
                    break;

                    case BoneEditorTool.Create:
                    {
                        if (Uni2DEditorSmoothBindingGUI.activeBone != null)
                        {
                            Uni2DEditorSmoothBindingGUI.activeBone.MoveBoneAlongSpritePlane(f2MouseGUIPos);
                        }
                        m_bIsDragging = true;
                    }
                    break;
                    }
                }

                // Consume event
                a_rEvent.Use( );
            }
            break;                      // End MouseDrag

            case EventType.MouseUp:
            {
                if (iMouseButton == 0)
                {
                    switch (ms_eEditorTool)
                    {
                    // Creation
                    case BoneEditorTool.Create:
                    {
                        BoneHandle             ePickedHandle;
                        Uni2DSmoothBindingBone rNearestBone = Uni2DEditorSmoothBindingUtils.PickNearestBoneInPosingMode(ms_rSprite, f2MouseGUIPos, out ePickedHandle, Uni2DEditorSmoothBindingGUI.activeBone);

                        // Mouse up near the last added bone => close bone chain
                        if (ePickedHandle == BoneHandle.InnerDisc && rNearestBone == m_rLastAddedBone)
                        {
                            Uni2DEditorSmoothBindingUtils.DeleteBone(ms_rSprite, Uni2DEditorSmoothBindingGUI.activeBone);

                            m_rLastAddedBone = null;
                            Uni2DEditorSmoothBindingGUI.activeBone = null;
                            ms_eEditorTool = BoneEditorTool.Select;
                        }
                        else
                        {
                            m_rLastAddedBone = Uni2DEditorSmoothBindingGUI.activeBone;
                            Undo.RegisterCreatedObjectUndo(m_rLastAddedBone.gameObject, "Add Uni2D Bone");

                            // Creating => validate bone and create another one
                            Uni2DEditorSmoothBindingGUI.activeBone = Uni2DEditorSmoothBindingUtils.AddBoneToSprite(ms_rSprite, f2MouseGUIPos, m_rLastAddedBone);
                        }
                    }
                    break;

                    // Move
                    case BoneEditorTool.Move:
                    {
                                                                #if BEFORE_UNITY_4_3
                        Undo.ClearSnapshotTarget( );
                        Undo.RegisterSnapshot( );
                                                                #endif
                        ms_eEditorTool = BoneEditorTool.Select;
                    }
                    break;
                    }

                    // Reset dragging state
                    m_bIsDragging      = false;
                    m_f2MouseGUIOffset = Vector2.zero;
                }                               // end if( left button )
                else if (iMouseButton == 1)     // Delete / stop bone creation
                {
                    switch (ms_eEditorTool)
                    {
                    case BoneEditorTool.Select:
                    {
                        BoneHandle             eBoneHandle;
                        Uni2DSmoothBindingBone rNearestBone = Uni2DEditorSmoothBindingUtils.PickNearestBoneInPosingMode(ms_rSprite, f2MouseGUIPos, out eBoneHandle, null);

                        if (rNearestBone != null)
                        {
                            if (eBoneHandle == BoneHandle.Link)
                            {
                                //Undo.RegisterSetTransformParentUndo( rNearestBone.transform, rRootTransform, "Break Uni2D Bone" );
                                rNearestBone.Break( );
                            }
                            else
                            {
                                                                                #if BEFORE_UNITY_4_3
                                Undo.RegisterSceneUndo("Delete Uni2D Bone");
                                                                                #else
                                Uni2DUndo.RegisterFullObjectHierarchyUndo(ms_rSprite.gameObject, "Delete Uni2D Bone");
                                                                                #endif
                                Uni2DEditorSmoothBindingUtils.DeleteBone(ms_rSprite, rNearestBone);
                            }
                        }
                    }
                    break;

                    case BoneEditorTool.Create:
                    {
                        // Close bone chain
                        Uni2DEditorSmoothBindingUtils.DeleteBone(ms_rSprite, Uni2DEditorSmoothBindingGUI.activeBone);
                        ms_eEditorTool = BoneEditorTool.Select;
                    }
                    break;

                    case BoneEditorTool.Move:
                    {
                        ms_eEditorTool = BoneEditorTool.Select;
                    }
                    break;
                    }
                }                               // End if( right button )

                // Consume event
                a_rEvent.Use( );
            }
            break;      // End MouseUp
            }           // End switch( event.type )
        }               // end if( mouse events )
        else if (a_rEvent.isKey && a_rEvent.type == EventType.keyDown)
        {
            switch (a_rEvent.keyCode)
            {
            case KeyCode.Escape:
            {
                switch (ms_eEditorTool)
                {
                case BoneEditorTool.Select:
                {
                    if (Uni2DEditorSmoothBindingGUI.activeBone != null)
                    {
                        Uni2DEditorSmoothBindingGUI.activeBone = null;
                    }
                    else
                    {
                        Uni2DEditorSmoothBindingGUI.CurrentBoneEditMode = BoneEditMode.None;
                    }
                }
                break;

                case BoneEditorTool.Move:
                {
                                                        #if BEFORE_UNITY_4_3
                    Undo.RestoreSnapshot( );
                                                        #endif
                    ms_eEditorTool = BoneEditorTool.Select;
                }
                break;

                case BoneEditorTool.Create:
                {
                    Uni2DEditorSmoothBindingUtils.DeleteBone(ms_rSprite, Uni2DEditorSmoothBindingGUI.activeBone);
                    ms_eEditorTool = BoneEditorTool.Select;
                }
                break;
                }
                a_rEvent.Use( );
            }
            break;                      // End Escape

            // Delete last created bone (if any)
            case KeyCode.Backspace:
            {
                if (ms_eEditorTool == BoneEditorTool.Create)
                {
                    if (m_rLastAddedBone != null && m_rLastAddedBone != m_rBoneChainOrigin)
                    {
                                                        #if BEFORE_UNITY_4_3
                        Undo.RegisterSceneUndo("Delete Uni2D Bone");
                                                        #else
                        Uni2DUndo.RegisterFullObjectHierarchyUndo(ms_rSprite.gameObject, "Delete Uni2D Bone");
                                                        #endif
                        Uni2DEditorSmoothBindingUtils.DeleteBone(ms_rSprite, Uni2DEditorSmoothBindingGUI.activeBone);

                        Uni2DEditorSmoothBindingGUI.activeBone = Uni2DEditorSmoothBindingUtils.AddBoneToSprite(ms_rSprite, f2MouseGUIPos, m_rLastAddedBone.Parent);
                        Uni2DEditorSmoothBindingUtils.DeleteBone(ms_rSprite, m_rLastAddedBone);

                        Uni2DSmoothBindingBone rParentActiveBone = Uni2DEditorSmoothBindingGUI.activeBone.Parent;
                        if (rParentActiveBone != null && rParentActiveBone.IsFakeRootBone)
                        {
                            m_rLastAddedBone = rParentActiveBone.Parent;                                        // Grand-pa'
                        }
                        else
                        {
                            m_rLastAddedBone = rParentActiveBone;
                        }
                    }
                }
                a_rEvent.Use( );
            }
            break;                      // End Escape

            case KeyCode.Return:
            case KeyCode.KeypadEnter:
            {
                ms_eEditorTool = BoneEditorTool.Select;
                a_rEvent.Use( );
            }
            break;      // End Return / KeypadEnter
            }           // end switch( event.type )
        }               // end if( key down events )
        else
        {
            switch (a_rEvent.type)
            {
            case EventType.ValidateCommand:
            {
                if (a_rEvent.commandName == "Delete" || a_rEvent.commandName == "UndoRedoPerformed")
                {
                    a_rEvent.Use( );
                }
            }
            break;                      // end ValidateCommand

            case EventType.ExecuteCommand:
            {
                if (a_rEvent.commandName == "Delete")
                {
                    if (Uni2DEditorSmoothBindingGUI.activeBone != null && ms_eEditorTool != BoneEditorTool.Create)
                    {
                                                        #if BEFORE_UNITY_4_3
                        Undo.RegisterSceneUndo("Delete Uni2D Bone");
                                                        #else
                        Uni2DUndo.RegisterFullObjectHierarchyUndo(ms_rSprite.gameObject, "Delete Uni2D Bone");
                                                        #endif
                        Uni2DEditorSmoothBindingUtils.DeleteBone(ms_rSprite, Uni2DEditorSmoothBindingGUI.activeBone);
                        ms_eEditorTool = BoneEditorTool.Select;
                    }
                    a_rEvent.Use( );
                }
                else if (a_rEvent.commandName == "UndoRedoPerformed")
                {
                    if (ms_eEditorTool == BoneEditorTool.Create && Uni2DEditorSmoothBindingGUI.activeBone != null)
                    {
                        Uni2DEditorSmoothBindingUtils.DeleteBone(ms_rSprite, Uni2DEditorSmoothBindingGUI.activeBone);
                    }
                    m_rLastAddedBone = null;
                    Uni2DEditorSmoothBindingGUI.activeBone = null;
                    ms_eEditorTool = BoneEditorTool.Select;
                    //Uni2DEditorSmoothBindingGUI.CurrentBoneEditMode = BoneEditMode.None;

                    a_rEvent.Use( );
                }
            }
            break;      // end ExecuteCommand
            }           // end switch( event.type )
        }               // end else
    }
Exemplo n.º 24
0
	// Remove bone
	public void RemoveBone(Uni2DSmoothBindingBone a_rBone)
	{
		CheckForOldBones();
		m_oBones.Remove(a_rBone);
		RemoveNullBones();
	}
    public void Reset( Uni2DSprite a_rSprite, bool a_bKeepCurrentModeIfPossible = false )
    {
        m_rLastAddedBone   = null;
        m_rBoneChainOrigin = null;
        Uni2DEditorSmoothBindingGUI.activeBone = null;

        m_f2MouseGUIOffset = Vector2.zero;
        ms_eEditorTool = BoneEditorTool.Select;

        BoneEditMode eSavedEditMode = Uni2DEditorSmoothBindingGUI.CurrentBoneEditMode;

        Uni2DEditorSmoothBindingGUI.CurrentBoneEditMode = BoneEditMode.None;

        ms_rSprite = a_rSprite;

        if( a_bKeepCurrentModeIfPossible )
        {
            Uni2DEditorSmoothBindingGUI.CurrentBoneEditMode = eSavedEditMode;
        }
    }
Exemplo n.º 26
0
    private void UpdateAnimEditGUI(Event a_rEvent)
    {
        Vector2 f2MouseGUIPos = a_rEvent.mousePosition;

        if (a_rEvent.isMouse)
        {
            int iMouseButton = a_rEvent.button;

            switch (a_rEvent.type)
            {
            case EventType.MouseMove:
            {
                // Reset state
                m_bIsDragging = false;

                // Consume event
                a_rEvent.Use( );
            }
            break;                      // End MouseMove

            case EventType.MouseDown:
            {
                // Left click
                if (iMouseButton == 0)
                {
                    // Reset drag state
                    m_bIsDragging = false;

                    // Pick bones in scene
                    BoneHandle             ePickedHandle;
                    Uni2DSmoothBindingBone rNearestBone = Uni2DEditorSmoothBindingUtils.PickNearestBoneInAnimMode(ms_rSprite, f2MouseGUIPos, out ePickedHandle, null);

                    // Selection
                    if (ms_eEditorTool == BoneEditorTool.Select && rNearestBone != null)
                    {
                        // Change selection and editor tool
                        Uni2DEditorSmoothBindingGUI.activeBone = rNearestBone;

                        m_f2MouseGUIOffset = f2MouseGUIPos - HandleUtility.WorldToGUIPoint(rNearestBone.transform.position);
                    }                                   // end if( editor tool == select )
                }

                // Consume mouse event
                a_rEvent.Use( );
            }
            break;                      // end MouseDown

            case EventType.MouseDrag:
            {
                if (iMouseButton == 0)
                {
                    switch (ms_eEditorTool)
                    {
                    case BoneEditorTool.Move:
                    {
                        // Something dragged? MOVE IT
                        if (Uni2DEditorSmoothBindingGUI.activeBone != null)
                        {
                            // We're moving an existing bone => register undo at first drag event
                            if (m_bIsDragging == false)
                            {
                                                                                #if BEFORE_UNITY_4_3
                                Undo.SetSnapshotTarget(Uni2DEditorSmoothBindingGUI.activeBone, "Move Uni2D Bone");
                                Undo.CreateSnapshot( );
                                Undo.RegisterSnapshot( );
                                                                                #else
                                Uni2DUndo.RegisterFullObjectHierarchyUndo(Uni2DEditorSmoothBindingGUI.activeBone.gameObject, "Move Uni2D Bone");
                                                                                #endif
                            }

                            // Move/drag along sprite plane
                            Uni2DEditorSmoothBindingGUI.activeBone.RotateBoneAlongSpritePlane(f2MouseGUIPos - m_f2MouseGUIOffset);
                        }
                        m_bIsDragging = true;
                    }
                    break;

                    case BoneEditorTool.Select:
                    {
                        // Dragging a bone in select mode == dragging on inner disc => add a child to active bone
                        if (Uni2DEditorSmoothBindingGUI.activeBone != null && m_bIsDragging == false)
                        {
                            ms_eEditorTool     = BoneEditorTool.Move;
                            m_f2MouseGUIOffset = f2MouseGUIPos - HandleUtility.WorldToGUIPoint(Uni2DEditorSmoothBindingGUI.activeBone.transform.position);

                            Uni2DEditorSmoothBindingGUI.activeBone.RotateBoneAlongSpritePlane(f2MouseGUIPos - m_f2MouseGUIOffset);
                        }
                        m_bIsDragging = true;
                    }
                    break;
                    }
                }

                // Consume event
                a_rEvent.Use( );
            }
            break;                      // End MouseDrag

            case EventType.MouseUp:
            {
                if (iMouseButton == 0)
                {
                    if (ms_eEditorTool == BoneEditorTool.Move)
                    {
                                                        #if BEFORE_UNITY_4_3
                        Undo.ClearSnapshotTarget( );
                        Undo.RegisterSnapshot( );
                                                        #endif
                        ms_eEditorTool = BoneEditorTool.Select;
                    }

                    // Reset dragging state
                    m_bIsDragging      = false;
                    m_f2MouseGUIOffset = Vector2.zero;
                }                                                                    // end if( left button )
                else if (iMouseButton == 1 && ms_eEditorTool == BoneEditorTool.Move) // Delete / stop bone creation
                {
                    ms_eEditorTool = BoneEditorTool.Select;
                }                               // End if( right button )

                // Consume event
                a_rEvent.Use( );
            }
            break;      // End MouseUp
            }           // End switch( event.type )
        }               // end if( mouse events )
        else if (a_rEvent.isKey && a_rEvent.type == EventType.keyDown)
        {
            switch (a_rEvent.keyCode)
            {
            case KeyCode.Escape:
            {
                switch (ms_eEditorTool)
                {
                case BoneEditorTool.Select:
                {
                    if (Uni2DEditorSmoothBindingGUI.activeBone != null)
                    {
                        Uni2DEditorSmoothBindingGUI.activeBone = null;
                    }
                    else
                    {
                        Uni2DEditorSmoothBindingGUI.CurrentBoneEditMode = BoneEditMode.None;
                    }
                }
                break;

                case BoneEditorTool.Move:
                {
                                                        #if BEFORE_UNITY_4_3
                    Undo.RestoreSnapshot( );
                                                        #endif
                    ms_eEditorTool = BoneEditorTool.Select;
                }
                break;
                }
                a_rEvent.Use( );
            }
            break;                      // End Escape

            case KeyCode.Return:
            case KeyCode.KeypadEnter:
            {
                ms_eEditorTool = BoneEditorTool.Select;
                a_rEvent.Use( );
            }
            break;      // End Return / KeypadEnter
            }           // end switch( event.type )
        }               // end if( key down events )
        else
        {
            switch (a_rEvent.type)
            {
            case EventType.ValidateCommand:
            {
                if (a_rEvent.commandName == "Delete" || a_rEvent.commandName == "UndoRedoPerformed")
                {
                    a_rEvent.Use( );
                }
            }
            break;                      // end ValidateCommand

            case EventType.ExecuteCommand:
            {
                if (a_rEvent.commandName == "Delete")
                {
                    a_rEvent.Use( );
                }
                else if (a_rEvent.commandName == "UndoRedoPerformed")
                {
                    Uni2DEditorSmoothBindingGUI.activeBone = null;
                    ms_eEditorTool = BoneEditorTool.Select;
                    //Uni2DEditorSmoothBindingGUI.CurrentBoneEditMode = BoneEditMode.None;
                    a_rEvent.Use( );
                }
            }
            break;      // end ExecuteCommand
            }           // end switch( event.type )
        }               // end else
    }
    private void UpdatePoseEditGUI( Event a_rEvent )
    {
        //Transform rRootTransform = Selection.activeTransform;
        Vector2 f2MouseGUIPos = a_rEvent.mousePosition;

        if( a_rEvent.isMouse )
        {
            int iMouseButton = a_rEvent.button;

            switch( a_rEvent.type )
            {
                case EventType.MouseMove:
                {
                    // Reset state
                    m_bIsDragging = false;

                    // Something selected, edit tool used => move bone
                    if( Uni2DEditorSmoothBindingGUI.activeBone != null && ms_eEditorTool != BoneEditorTool.Select )
                    {
                        Uni2DEditorSmoothBindingGUI.activeBone.MoveBoneAlongSpritePlane( f2MouseGUIPos - m_f2MouseGUIOffset, a_rEvent.alt );
                    }

                    // Consume event
                    a_rEvent.Use( );
                }
                break;	// End MouseMove

                case EventType.MouseDown:
                {
                    // Left click
                    if( iMouseButton == 0 )
                    {
                        // Reset drag state
                        m_bIsDragging = false;

                        // Selection
                        if( ms_eEditorTool == BoneEditorTool.Select )
                        {
                            // Pick bones in scene
                            BoneHandle ePickedHandle;
                            Uni2DSmoothBindingBone rNearestBone = Uni2DEditorSmoothBindingUtils.PickNearestBoneInPosingMode( ms_rSprite, f2MouseGUIPos, out ePickedHandle, null );

                            // Outer disc => move
                            if( ePickedHandle == BoneHandle.OuterDisc || ePickedHandle == BoneHandle.Link )
                            {
                                if( rNearestBone != null )
                                {
                                    // Change selection and editor tool
                                    Uni2DEditorSmoothBindingGUI.activeBone = rNearestBone;
                                    ms_eEditorTool = BoneEditorTool.Move;

                                    m_f2MouseGUIOffset = f2MouseGUIPos - HandleUtility.WorldToGUIPoint( rNearestBone.transform.position );
                                }
                            }
                            else // Bone picked via inner disc or no bone picked
                            {
                                // A bone has been picked (via inner disc)
                                // Set selection to this bone
                                if( rNearestBone != null )
                                {
                                    Uni2DEditorSmoothBindingGUI.activeBone = rNearestBone;
                                }
                                else // No bone picked
                                {
                                    // Get rid of selection if any
                                    if( Uni2DEditorSmoothBindingGUI.activeBone != null )
                                    {
                                        Uni2DEditorSmoothBindingGUI.activeBone = null;
                                    }
                                    else // no selection, no bone picked => create a new bone chain
                                    {
                                        m_rLastAddedBone = null;

                                        // Create a new root and set it as current active bone
                                        Uni2DEditorSmoothBindingGUI.activeBone = Uni2DEditorSmoothBindingUtils.AddBoneToSprite( ms_rSprite, f2MouseGUIPos, null );
                                        m_rBoneChainOrigin = Uni2DEditorSmoothBindingGUI.activeBone;

                                        // The bone is not a child of a newly created root
                                        //m_bStartFromExistingBone = false;

                                        // Change editor tool
                                        ms_eEditorTool = BoneEditorTool.Create;
                                    }
                                }
                            }
                        }	// end if( editor tool == select )
                    }

                    // Consume mouse event
                    a_rEvent.Use( );
                }
                break;	// end MouseDown

                case EventType.MouseDrag:
                {
                    if( iMouseButton == 0 )
                    {
                        switch( ms_eEditorTool )
                        {
                            case BoneEditorTool.Move:
                            {
                                // Something dragged? MOVE IT
                                if( Uni2DEditorSmoothBindingGUI.activeBone != null )
                                {
                                    // We're moving an existing bone => register undo at first drag event
                                    if( m_bIsDragging == false )
                                    {
                                        Undo.SetSnapshotTarget( Uni2DEditorSmoothBindingGUI.activeBone, "Move Uni2D Bone" );
                                        Undo.CreateSnapshot( );
                                        Undo.RegisterSnapshot( );
                                    }

                                    // Move/drag along sprite plane;
                                    Uni2DEditorSmoothBindingGUI.activeBone.MoveBoneAlongSpritePlane( f2MouseGUIPos - m_f2MouseGUIOffset, a_rEvent.alt );
                                }
                                m_bIsDragging = true;
                            }
                            break;

                            case BoneEditorTool.Select:
                            {
                                // Dragging a bone in select mode == dragging on inner disc => add a child to active bone
                                if( Uni2DEditorSmoothBindingGUI.activeBone != null && m_bIsDragging == false )
                                {
                                    m_rBoneChainOrigin       = Uni2DEditorSmoothBindingGUI.activeBone;
                                    m_rLastAddedBone         = null;
                                    Uni2DEditorSmoothBindingGUI.activeBone = Uni2DEditorSmoothBindingUtils.AddBoneToSprite( ms_rSprite, f2MouseGUIPos, Uni2DEditorSmoothBindingGUI.activeBone );
                                    //m_bStartFromExistingBone = true;
                                    ms_eEditorTool            = BoneEditorTool.Create;
                                }
                                m_bIsDragging = true;
                            }
                            break;

                            case BoneEditorTool.Create:
                            {
                                if( Uni2DEditorSmoothBindingGUI.activeBone != null )
                                {
                                    Uni2DEditorSmoothBindingGUI.activeBone.MoveBoneAlongSpritePlane( f2MouseGUIPos );
                                }
                                m_bIsDragging = true;
                            }
                            break;
                        }
                    }

                    // Consume event
                    a_rEvent.Use( );
                }
                break;	// End MouseDrag

                case EventType.MouseUp:
                {
                    if( iMouseButton == 0 )
                    {
                        switch( ms_eEditorTool )
                        {
                            // Creation
                            case BoneEditorTool.Create:
                            {
                                BoneHandle ePickedHandle;
                                Uni2DSmoothBindingBone rNearestBone = Uni2DEditorSmoothBindingUtils.PickNearestBoneInPosingMode( ms_rSprite, f2MouseGUIPos, out ePickedHandle, Uni2DEditorSmoothBindingGUI.activeBone );

                                // Mouse up near the last added bone => close bone chain
                                if( ePickedHandle == BoneHandle.InnerDisc && rNearestBone == m_rLastAddedBone )
                                {
                                    Uni2DEditorSmoothBindingUtils.DeleteBone( ms_rSprite, Uni2DEditorSmoothBindingGUI.activeBone );

                                    m_rLastAddedBone = null;
                                    Uni2DEditorSmoothBindingGUI.activeBone = null;
                                    ms_eEditorTool = BoneEditorTool.Select;
                                }
                                else
                                {
                                    m_rLastAddedBone = Uni2DEditorSmoothBindingGUI.activeBone;
                                    Undo.RegisterCreatedObjectUndo( m_rLastAddedBone.gameObject, "Add Uni2D Bone" );

                                    // Creating => validate bone and create another one
                                    Uni2DEditorSmoothBindingGUI.activeBone = Uni2DEditorSmoothBindingUtils.AddBoneToSprite( ms_rSprite, f2MouseGUIPos, m_rLastAddedBone );

                                }
                            }
                            break;

                            // Move
                            case BoneEditorTool.Move:
                            {
                                Undo.ClearSnapshotTarget( );
                                Undo.RegisterSnapshot( );
                                ms_eEditorTool = BoneEditorTool.Select;
                            }
                            break;
                        }

                        // Reset dragging state
                        m_bIsDragging = false;
                        m_f2MouseGUIOffset = Vector2.zero;
                    }	// end if( left button )
                    else if( iMouseButton == 1 )	// Delete / stop bone creation
                    {
                        switch( ms_eEditorTool )
                        {
                            case BoneEditorTool.Select:
                            {
                                BoneHandle eBoneHandle;
                                Uni2DSmoothBindingBone rNearestBone = Uni2DEditorSmoothBindingUtils.PickNearestBoneInPosingMode( ms_rSprite, f2MouseGUIPos, out eBoneHandle, null );

                                if( rNearestBone != null )
                                {
                                    if( eBoneHandle == BoneHandle.Link )
                                    {
                                        //Undo.RegisterSetTransformParentUndo( rNearestBone.transform, rRootTransform, "Break Uni2D Bone" );
                                        rNearestBone.Break( );
                                    }
                                    else
                                    {
                                        Undo.RegisterSceneUndo( "Delete Uni2D Bone" );
                                        Uni2DEditorSmoothBindingUtils.DeleteBone( ms_rSprite, rNearestBone );
                                    }
                                }
                            }
                            break;

                            case BoneEditorTool.Create:
                            {
                                // Close bone chain
                                Uni2DEditorSmoothBindingUtils.DeleteBone( ms_rSprite, Uni2DEditorSmoothBindingGUI.activeBone );
                                ms_eEditorTool = BoneEditorTool.Select;
                            }
                            break;

                            case BoneEditorTool.Move:
                            {
                                ms_eEditorTool = BoneEditorTool.Select;
                            }
                            break;
                        }
                    }	// End if( right button )

                    // Consume event
                    a_rEvent.Use( );
                }
                break;	// End MouseUp
            }	// End switch( event.type )
        }	// end if( mouse events )
        else if( a_rEvent.isKey && a_rEvent.type == EventType.keyDown )
        {
            switch( a_rEvent.keyCode )
            {
                case KeyCode.Escape:
                {
                    switch( ms_eEditorTool )
                    {
                        case BoneEditorTool.Select:
                        {
                            if( Uni2DEditorSmoothBindingGUI.activeBone != null )
                            {
                                Uni2DEditorSmoothBindingGUI.activeBone = null;
                            }
                            else
                            {
                                Uni2DEditorSmoothBindingGUI.CurrentBoneEditMode = BoneEditMode.None;
                            }
                        }
                        break;

                        case BoneEditorTool.Move:
                        {
                            Undo.RestoreSnapshot( );
                            ms_eEditorTool = BoneEditorTool.Select;
                        }
                        break;

                        case BoneEditorTool.Create:
                        {
                            Uni2DEditorSmoothBindingUtils.DeleteBone( ms_rSprite, Uni2DEditorSmoothBindingGUI.activeBone );
                            ms_eEditorTool = BoneEditorTool.Select;
                        }
                        break;
                    }
                    a_rEvent.Use( );
                }
                break;	// End Escape

                // Delete last created bone (if any)
                case KeyCode.Backspace:
                {
                    if( ms_eEditorTool == BoneEditorTool.Create )
                    {
                        if( m_rLastAddedBone != null && m_rLastAddedBone != m_rBoneChainOrigin )
                        {
                            Undo.RegisterSceneUndo( "Delete Uni2D Bone" );
                            Uni2DEditorSmoothBindingUtils.DeleteBone( ms_rSprite, Uni2DEditorSmoothBindingGUI.activeBone );

                            Uni2DEditorSmoothBindingGUI.activeBone = Uni2DEditorSmoothBindingUtils.AddBoneToSprite( ms_rSprite, f2MouseGUIPos, m_rLastAddedBone.Parent );
                            Uni2DEditorSmoothBindingUtils.DeleteBone( ms_rSprite, m_rLastAddedBone );

                            Uni2DSmoothBindingBone rParentActiveBone = Uni2DEditorSmoothBindingGUI.activeBone.Parent;
                            if( rParentActiveBone != null && rParentActiveBone.IsFakeRootBone )
                            {
                                m_rLastAddedBone = rParentActiveBone.Parent;	// Grand-pa'
                            }
                            else
                            {
                                m_rLastAddedBone = rParentActiveBone;
                            }
                        }
                    }
                    a_rEvent.Use( );
                }
                break;	// End Escape

                case KeyCode.Return:
                case KeyCode.KeypadEnter:
                {
                    ms_eEditorTool = BoneEditorTool.Select;
                    a_rEvent.Use( );
                }
                break;	// End Return / KeypadEnter
            }	// end switch( event.type )
        }	// end if( key down events )
        else
        {
            switch( a_rEvent.type )
            {
                case EventType.ValidateCommand:
                {
                    if( a_rEvent.commandName == "Delete" || a_rEvent.commandName == "UndoRedoPerformed" )
                    {
                        a_rEvent.Use( );
                    }
                }
                break;	// end ValidateCommand

                case EventType.ExecuteCommand:
                {
                    if( a_rEvent.commandName == "Delete" )
                    {
                        if( Uni2DEditorSmoothBindingGUI.activeBone != null && ms_eEditorTool != BoneEditorTool.Create )
                        {
                            Undo.RegisterSceneUndo( "Delete Uni2D Bone" );
                            Uni2DEditorSmoothBindingUtils.DeleteBone( ms_rSprite, Uni2DEditorSmoothBindingGUI.activeBone );
                            ms_eEditorTool = BoneEditorTool.Select;
                        }
                        a_rEvent.Use( );
                    }
                    else if( a_rEvent.commandName == "UndoRedoPerformed" )
                    {
                        if( ms_eEditorTool == BoneEditorTool.Create && Uni2DEditorSmoothBindingGUI.activeBone != null )
                        {
                            Uni2DEditorSmoothBindingUtils.DeleteBone( ms_rSprite, Uni2DEditorSmoothBindingGUI.activeBone );
                        }
                        m_rLastAddedBone = null;
                        Uni2DEditorSmoothBindingGUI.activeBone = null;
                        ms_eEditorTool = BoneEditorTool.Select;
                        //Uni2DEditorSmoothBindingGUI.CurrentBoneEditMode = BoneEditMode.None;

                        a_rEvent.Use( );
                    }
                }
                break;	// end ExecuteCommand
            }	// end switch( event.type )
        }	// end else
    }
 public static Uni2DSmoothBindingBone CreateNewBone(Uni2DSmoothBindingBone a_rBoneParent, bool a_bCreateFakeBone = false)
 {
     return(Uni2DEditorSmoothBindingUtils.CreateNewBone(a_rBoneParent.transform, a_bCreateFakeBone));
 }