/*
-----------------------------------------------------------------------------
This source file is part of OGRE
    (Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/

Copyright (c) 2000-2005 The OGRE Team
Also see acknowledgements in Readme.html

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt.
-----------------------------------------------------------------------------
*/

#ifndef __Skeleton_H__
#define __Skeleton_H__

#include "OgrePrerequisites.h"
#include "OgreResource.h"
#include "OgreQuaternion.h"
#include "OgreVector3.h"
#include "OgreIteratorWrappers.h"

namespace Ogre {

    /**  */
    enum SkeletonAnimationBlendMode {
        /// Animations are applied by calculating a weighted average of all animations
	    ANIMBLEND_AVERAGE,
        /// Animations are applied by calculating a weighted cumulative total
	    ANIMBLEND_CUMULATIVE
    };

#define OGRE_MAX_NUM_BONES 256

	
	struct LinkedSkeletonAnimationSource;

    /** A collection of Bone objects used to animate a skinned mesh.
    @remarks
        Skeletal animation works by having a collection of 'bones' which are 
        actually just joints with a position and orientation, arranged in a tree structure.
        For example, the wrist joint is a child of the elbow joint, which in turn is a
        child of the shoulder joint. Rotating the shoulder automatically moves the elbow
        and wrist as well due to this hierarchy.
    @par
        So how does this animate a mesh? Well every vertex in a mesh is assigned to one or more
        bones which affects it's position when the bone is moved. If a vertex is assigned to 
        more than one bone, then weights must be assigned to determine how much each bone affects
        the vertex (actually a weight of 1.0 is used for single bone assignments). 
        Weighted vertex assignments are especially useful around the joints themselves
        to avoid 'pinching' of the mesh in this region. 
    @par
        Therefore by moving the skeleton using preset animations, we can animate the mesh. The
        advantage of using skeletal animation is that you store less animation data, especially
        as vertex counts increase. In addition, you are able to blend multiple animations together
        (e.g. walking and looking around, running and shooting) and provide smooth transitions
        between animations without incurring as much of an overhead as would be involved if you
        did this on the core vertex data.
    @par
        Skeleton definitions are loaded from datafiles, namely the .skeleton file format. They
        are loaded on demand, especially when referenced by a Mesh.
    */
    class _OgreExport Skeleton : public Resource
    {
		friend class SkeletonInstance;
	protected:
		/// Internal constructor for use by SkeletonInstance only
		Skeleton();

    public:
        /** Constructor, don't call directly, use SkeletonManager.
        @remarks
            On creation, a Skeleton has a no bones, you should create them and link
            them together appropriately. 
        */
        Skeleton(ResourceManager* creator, const String& name, ResourceHandle handle,
            const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
        virtual ~Skeleton();


        /** Creates a brand new Bone owned by this Skeleton. 
        @remarks
            This method creates an unattached new Bone for this skeleton.
			Unless this is to be a root bone (there may be more than one of 
			these), you must attach it to another Bone in the skeleton using addChild for it to be any use.
            For this reason you will likely be better off creating child bones using the
            Bone::createChild method instead, once you have created the root bone. 
        @par
            Note that this method automatically generates a handle for the bone, which you
            can retrieve using Bone::getHandle. If you wish the new Bone to have a specific
            handle, use the alternate form of this method which takes a handle as a parameter,
            although you should note the restrictions.
        */
        virtual Bone* createBone(void);

        /** Creates a brand new Bone owned by this Skeleton. 
        @remarks
            This method creates an unattached new Bone for this skeleton and assigns it a 
            specific handle. Unless this is to be a root bone (there may be more than one of 
			these), you must attach it to another Bone in the skeleton using addChild for it to be any use. 
            For this reason you will likely be better off creating child bones using the
            Bone::createChild method instead, once you have created a root bone. 
        @param handle The handle to give to this new bone - must be unique within this skeleton. 
            You should also ensure that all bone handles are eventually contiguous (this is to simplify
            their compilation into an indexed array of transformation matrices). For this reason
            it is advised that you use the simpler createBone method which automatically assigns a
            sequential handle starting from 0.
        */
        virtual Bone* createBone(unsigned short handle);

        /** Creates a brand new Bone owned by this Skeleton. 
        @remarks
            This method creates an unattached new Bone for this skeleton and assigns it a 
            specific name.Unless this is to be a root bone (there may be more than one of 
			these), you must attach it to another Bone in the skeleton using addChild for it to be any use.
            For this reason you will likely be better off creating child bones using the
            Bone::createChild method instead, once you have created the root bone. 
        @param name The name to give to this new bone - must be unique within this skeleton. 
            Note that the way OGRE looks up bones is via a numeric handle, so if you name a
            Bone this way it will be given an automatic sequential handle. The name is just
            for your convenience, although it is recommended that you only use the handle to 
            retrieve the bone in performance-critical code.
        */
        virtual Bone* createBone(const String& name);

        /** Creates a brand new Bone owned by this Skeleton. 
        @remarks
            This method creates an unattached new Bone for this skeleton and assigns it a 
            specific name and handle. Unless this is to be a root bone (there may be more than one of 
			these), you must attach it to another Bone in the skeleton using addChild for it to be any use.
            For this reason you will likely be better off creating child bones using the
            Bone::createChild method instead, once you have created the root bone. 
        @param name The name to give to this new bone - must be unique within this skeleton. 
        @param handle The handle to give to this new bone - must be unique within this skeleton. 
        */
        virtual Bone* createBone(const String& name, unsigned short handle);

        /** Returns the number of bones in this skeleton. */
        virtual unsigned short getNumBones(void) const;

        /** Gets the root bone of the skeleton: deprecated in favour of getRootBoneIterator. 
        @remarks
            The system derives the root bone the first time you ask for it. The root bone is the
            only bone in the skeleton which has no parent. The system locates it by taking the
            first bone in the list and going up the bone tree until there are no more parents,
            and saves this top bone as the root. If you are building the skeleton manually using
            createBone then you must ensure there is only one bone which is not a child of 
            another bone, otherwise your skeleton will not work properly. If you use createBone
            only once, and then use Bone::createChild from then on, then inherently the first
            bone you create will by default be the root.
        */
        virtual Bone* getRootBone(void) const;

        typedef std::vector<Bone*> BoneList;
        typedef VectorIterator<BoneList> BoneIterator;
        /// Get an iterator over the root bones in the skeleton, ie those with no parents
        virtual BoneIterator getRootBoneIterator(void);
        /// Get an iterator over all the bones in the skeleton
        virtual BoneIterator getBoneIterator(void);

        /** Gets a bone by it's handle. */
        virtual Bone* getBone(unsigned short handle) const;

        /** Gets a bone by it's name. */
        virtual Bone* getBone(const String& name) const;

        /** Sets the current position / orientation to be the 'binding pose' ie the layout in which 
            bones were originally bound to a mesh.
        */
        virtual void setBindingPose(void);

        /** Resets the position and orientation of all bones in this skeleton to their original binding position.
        @remarks
            A skeleton is bound to a mesh in a binding pose. Bone positions are then modified from this
            position during animation. This method returns all the bones to their original position and
            orientation.
        @param resetManualBones If set to true, causes the state of manual bones to be reset
            too, which is normally not done to allow the manual state to persist even 
            when keyframe animation is applied.
        */
        virtual void reset(bool resetManualBones = false);

        /** Creates a new Animation object for animating this skeleton. 
        @param name The name of this animation
        @param length The length of the animation in seconds
        */
        virtual Animation* createAnimation(const String& name, Real length);

        /** Returns the named Animation object. 
		@remarks
			Will pick up animations in linked skeletons 
			(@see addLinkedSkeletonAnimationSource). 
		@param name The name of the animation
		@param linker Optional pointer to a pointer to the linked skeleton animation
			where this is coming from.
		*/
        virtual Animation* getAnimation(const String& name, 
			const LinkedSkeletonAnimationSource** linker = 0) const;

		/// Internal accessor for animations (returns null if animation does not exist)
		virtual Animation* _getAnimationImpl(const String& name, 
			const LinkedSkeletonAnimationSource** linker = 0) const;


		/** Returns whether this skeleton contains the named animation. */
		virtual bool hasAnimation(const String& name);

        /** Removes an Animation from this skeleton. */
        virtual void removeAnimation(const String& name);

        /** Changes the state of the skeleton to reflect the application of the passed in collection of animations.
        @remarks
            Animating a skeleton involves both interpolating between keyframes of a specific animation,
            and blending between the animations themselves. Calling this method sets the state of
            the skeleton so that it reflects the combination of all the passed in animations, at the
            time index specified for each, using the weights specified. Note that the weights between 
            animations do not have to sum to 1.0, because some animations may affect only subsets
            of the skeleton. If the weights exceed 1.0 for the same area of the skeleton, the 
            movement will just be exaggerated.
            @param 
        */
        virtual void setAnimationState(const AnimationStateSet& animSet);


        /** Initialise an animation set suitable for use with this skeleton. 
        @remarks
            Only recommended for use inside the engine, not by applications.
        */
        virtual void _initAnimationState(AnimationStateSet* animSet);

		/** Refresh an animation set suitable for use with this skeleton. 
		@remarks
			Only recommended for use inside the engine, not by applications.
		*/
		virtual void _refreshAnimationState(AnimationStateSet* animSet);

		/** Populates the passed in array with the bone matrices based on the current position.
        @remarks
            Internal use only. The array pointed to by the passed in pointer must
            be at least as large as the number of bones.
            Assumes animation has already been updated.
        */
        virtual void _getBoneMatrices(Matrix4* pMatrices);

        /** Gets the number of animations on this skeleton. */
        virtual unsigned short getNumAnimations(void) const;

        /** Gets a single animation by index. 
		@remarks
			Will NOT pick up animations in linked skeletons 
			(@see addLinkedSkeletonAnimationSource).
		*/
        virtual Animation* getAnimation(unsigned short index) const;


		/** Gets the animation blending mode which this skeleton will use. */
        virtual SkeletonAnimationBlendMode getBlendMode() const;
        /** Sets the animation blending mode this skeleton will use. */
		virtual void setBlendMode(SkeletonAnimationBlendMode state);

        /// Updates all the derived transforms in the skeleton
        virtual void _updateTransforms(void);

		/** Optimise all of this skeleton's animations.
		@see Animation::optimise
		*/
		virtual void optimiseAllAnimations(void);

		/** Allows you to use the animations from another Skeleton object to animate
			this skeleton.
		@remarks
			If you have skeletons of identical structure (that means identically
			named bones with identical handles, and with the same hierarchy), but
			slightly different proportions or binding poses, you can re-use animations
			from one in the other. Because animations are actually stored as
			changes to bones from their bind positions, it's possible to use the
			same animation data for different skeletons, provided the skeletal
			structure matches and the 'deltas' stored in the keyframes apply
			equally well to the other skeletons bind position (so they must be
			roughly similar, but don't have to be identical). You can use the 
			'scale' option to adjust the translation and scale keyframes where
			there are large differences in size between the skeletons.
		@note
			This method takes a skeleton name, rather than a more specific 
			animation name, for two reasons; firstly it allows some validation 
			of compatibility of skeletal structure, and secondly skeletons are
			the unit of loading. Linking a skeleton to another in this way means
			that the linkee will be prevented from being destroyed until the 
			linker is destroyed.

			You cannot set up cyclic relationships, e.g. SkeletonA uses SkeletonB's
			animations, and SkeletonB uses SkeletonA's animations. This is because
			it would set up a circular dependency which would prevent proper 
			unloading - make one of the skeletons the 'master' in this case.
		@param skelName Name of the skeleton to link animations from. This 
			skeleton will be loaded immediately if this skeleton is already 
			loaded, otherwise it will be loaded when this skeleton is.
		@param scale A scale factor to apply to translation and scaling elements
			of the keyframes in the other skeleton when applying the animations
			to this one. Compensates for skeleton size differences.
		*/
		virtual void addLinkedSkeletonAnimationSource(const String& skelName, 
			Real scale = 1.0f);
		/// Remove all links to other skeletons for the purposes of sharing animation
		virtual void removeAllLinkedSkeletonAnimationSources(void);
		
		typedef std::vector<LinkedSkeletonAnimationSource> 
			LinkedSkeletonAnimSourceList;
		typedef ConstVectorIterator<LinkedSkeletonAnimSourceList> 
			LinkedSkeletonAnimSourceIterator;
		/// Get an iterator over the linked skeletons used as animation sources
		virtual LinkedSkeletonAnimSourceIterator 
			getLinkedSkeletonAnimationSourceIterator(void) const;

		/// Internal method for marking the manual bones as dirty
		virtual void _notifyManualBonesDirty(void);
		/// Internal method for notifying that a bone is manual
		virtual void _notifyManualBoneStateChange(Bone* bone);

		/// Have manual bones been modified since the skeleton was last updated?
		virtual bool getManualBonesDirty(void) const { return mManualBonesDirty; }
		/// Are there any manually controlled bones?
		virtual bool hasManualBones(void) const { return !mManualBones.empty(); }

	protected:
		SkeletonAnimationBlendMode mBlendState;
        /// Storage of bones, indexed by bone handle
        BoneList mBoneList;
        /// Lookup by bone name
        typedef std::map<String, Bone*> BoneListByName;
        BoneListByName mBoneListByName;


        /// Pointer to root bones (can now have multiple roots)
        mutable BoneList mRootBones;
        /// Bone automatic handles
        unsigned short mNextAutoHandle;
		typedef std::set<Bone*> BoneSet;
		/// Manual bones
		BoneSet mManualBones;
		/// Manual bones dirty?
		bool mManualBonesDirty;


        /// Storage of animations, lookup by name
        typedef std::map<String, Animation*> AnimationList;
        AnimationList mAnimationsList;

		/// List of references to other skeletons to use animations from 
		mutable LinkedSkeletonAnimSourceList mLinkedSkeletonAnimSourceList;

        /** Internal method which parses the bones to derive the root bone. 
        @remarks
            Must be const because called in getRootBone but mRootBone is mutable
            since lazy-updated.
        */
        void deriveRootBone(void) const;

        /// Debugging method
        void _dumpContents(const String& filename);

        /** @copydoc Resource::loadImpl
        */
        void loadImpl(void);

        /** @copydoc Resource::unloadImpl
        */
        void unloadImpl(void);
		/// @copydoc Resource::calculateSize
		size_t calculateSize(void) const { return 0; } // TODO 

    };

    /** Specialisation of SharedPtr to allow SharedPtr to be assigned to SkeletonPtr 
    @note Has to be a subclass since we need operator=.
    We could templatise this instead of repeating per Resource subclass, 
    except to do so requires a form VC6 does not support i.e.
    ResourceSubclassPtr<T> : public SharedPtr<T>
    */
    class _OgreExport SkeletonPtr : public SharedPtr<Skeleton> 
    {
    public:
        SkeletonPtr() : SharedPtr<Skeleton>() {}
        explicit SkeletonPtr(Skeleton* rep) : SharedPtr<Skeleton>(rep) {}
        SkeletonPtr(const SkeletonPtr& r) : SharedPtr<Skeleton>(r) {} 
        SkeletonPtr(const ResourcePtr& r) : SharedPtr<Skeleton>()
        {
			// lock & copy other mutex pointer
            OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME)
            {
			    OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
			    OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
                pRep = static_cast<Skeleton*>(r.getPointer());
                pUseCount = r.useCountPointer();
                if (pUseCount)
                {
                    ++(*pUseCount);
                }
            }
        }

        /// Operator used to convert a ResourcePtr to a SkeletonPtr
        SkeletonPtr& operator=(const ResourcePtr& r)
        {
            if (pRep == static_cast<Skeleton*>(r.getPointer()))
                return *this;
            release();
			// lock & copy other mutex pointer
            OGRE_MUTEX_CONDITIONAL(r.OGRE_AUTO_MUTEX_NAME)
            {
			    OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
			    OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
                pRep = static_cast<Skeleton*>(r.getPointer());
                pUseCount = r.useCountPointer();
                if (pUseCount)
                {
                    ++(*pUseCount);
                }
            }
            return *this;
        }
    };

	/// Link to another skeleton to share animations
	struct LinkedSkeletonAnimationSource
	{
		String skeletonName;
		SkeletonPtr pSkeleton;
		Real scale;
		LinkedSkeletonAnimationSource(const String& skelName, Real scl)
			: skeletonName(skelName), scale(scl) {}
			LinkedSkeletonAnimationSource(const String& skelName, Real scl, 
				SkeletonPtr skelPtr)
				: skeletonName(skelName), pSkeleton(skelPtr), scale(scl) {}
	};
}


#endif

