
//**************************************************************************/
// Copyright (c) 2008 Autodesk, Inc.
// All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk license
// agreement provided at the time of installation or download, or which
// otherwise accompanies this software in either electronic or hard copy form.
//
//**************************************************************************/
// DESCRIPTION:
// CREATED: November 2009
//**************************************************************************/

#ifndef __MUDBOXSDK_BRUSHMASK_H__
#define __MUDBOXSDK_BRUSHMASK_H__

namespace mudbox {

/// This class can be used for masking out areas from the brush effect.
struct MBDLL_DECL BrushMask : public TreeNode
{
	DECLARE_CLASS;

	/// Constructor
	BrushMask( void );
	/// Destructir
	~BrushMask( void );

	/// Sets the enabled state
	void SetState( bool b );
	/// Returns the state
	bool State( void );

	/// Sets the effect multiplier factor
	float Factor( void );
	/// Returns the effect multiplier factor
	void SetFactor( float f );

	/// Sets the transformation matrix
	virtual void SetTransformation( const Matrix &mTransformation );
	/// Returns the transformation matrix
	Matrix Transformation( void );

	/// Returns the mask image
	class Image *Image( void );
	/// Changes the mask image
	void SetImage( class Image *pImage );
	/// Clear the mask
	void Clear( void );
	/// Sets the filename and attemts to load from the file to an Image
	void SetFileName( const QString &sFileName );
	/// Creates Texture objects from the in-memory Image
	virtual void CreateTextures( void );

protected:
	Matrix m_mTransformation;
	float m_fAspectRatio;

	class Image *m_pImage;
	Texture *m_pLuminanceTexture;

	abool m_bState;
	afloatr m_fFactor;
	astring m_sFileName;
};

/// A masking image aligned to a mesh surface
struct MBDLL_DECL BrushStamp : public BrushMask
{
	DECLARE_CLASS;

	/// Initializes mask parameters based on the given configuration
	void SetParameters( const StampConfiguration &config, const Vector &vPosition, const Vector &vNormal, float fSize, const Matrix &mLocalToWorld );
	/// Calculates the transformation matrix from the given position, scale and mirror parameters.
	void CalculateTransformation( const Vector &vPosition, const Vector &vNormal, const Vector &vUp, float fScaleX, float fScaleY, bool bFlip, bool bFlop, const Matrix &mLocalToWorld );
};

/// A masking image attached to a camera.
struct MBDLL_DECL BrushStencil : public BrushMask
{
	enum
	{
		eColorModeUndefined = -1,
		eColorModeLuminance,
		eColorModeColored
	};

	DECLARE_CLASS;

	/// Constructor
	BrushStencil( void );
	/// Destructor
	~BrushStencil( void );

	/// Sets the visibility
	void SetVisibility( float f );
	/// Returns the visibility
	float Visibility( void );

	/// Sets if the object can interact with the user
	void SetUIEnabled( bool b );

	/// Sets the color mode
	void SetColorMode( unsigned int mode );
	/// Returns the color mode
	unsigned int ColorMode( void );

	/// Sets the transformation matrix
	void SetTransformation( const Matrix &mTransformation );
	/// Updates the projection matrix
	void UpdateMatrix( void );
	/// Returns the final matrix
	const Matrix &Final( void ) const;
	/// Returns the matrix mirrored to the active mirror axis
	Matrix MirroredFinal( void ) const;

	/// Returns if the stencil uses mirroring
	bool IsMirrored( void ) const;

	/// Sets the stencil inverted state
    void SetInverted(bool inv) { m_bInvert.SetValue(inv); }

	/// Returns if the stencil is inverted
	bool IsInverted( void ) const;

	/// Sets the stencil texture as current. Returns true if successful.
	bool Activate( void );
	/// Sets the stencil texture as not current. Returns true if successful.
	bool Deactivate( void );

	/// Creates the stencil from an exisiting in-memory image data
	void SetFromImage( int iWidth, int iHeight, const unsigned int *pData );
	/// Creates Texture objects from the in-memory Image
	virtual void CreateTextures( void );

protected:
	bool m_bUIEnabled;
	unsigned int m_iColorMode;
	Matrix m_mProjector, m_mFinal;
	Texture *m_pColorTexture;

	// attributes
	afloatr m_fVisibility;
	abool m_bInvert;
	abool m_bUseTiles, m_bShowTiles;
	afloatr m_fOffset;
	abool m_bAutoHide;
	abool m_bMirror;
	abool m_bFlipX;
	abool m_bFlipY;

	aevent m_cImport, m_cExport;
};

}; // end of namespace mudbox

#endif
