////////////////////////////////////////////////////////////////////////////
//
//  Crytek Engine Source File.
//  Copyright (C), Crytek Studios, 2002.
// -------------------------------------------------------------------------
//  File name:   terraintexturepainter.h
//  Version:     v1.00
//  Created:     8/10/2002 by Timur.
//  Compilers:   Visual Studio.NET
//  Description: 
// -------------------------------------------------------------------------
//  History:
//
////////////////////////////////////////////////////////////////////////////

#ifndef __terraintexturepainter_h__
#define __terraintexturepainter_h__

#if _MSC_VER > 1000
#pragma once
#endif

#include "EditTool.h"
#include "TerrainTexGen.h"

class CHeightmap;
class CTerrainGrid;
class CLayer;

/** Terrain Texture brush types.
*/
enum ETextureBrushType
{
	ET_BRUSH_PAINT = 1,
	ET_BRUSH_CHANGERESOLUTION
};

/** Terrain texture brush.
*/
struct CTextureBrush
{
	ETextureBrushType		type;								// Type of this brush.
	float								radius;							// Radius of brush in meters
	float								hardness;						// How hard this brush.
	float								value;							//
	bool								bErase;							//

	bool								bDetailLayer;				// apply changes to the detail layer data as well
	bool								bMask_Altitude;			//
	bool								bMaskByLayerSettings;				//

	float								minRadius;					//
	float 							maxRadius;					//
	ColorF							m_cFilterColor;			//
	float								m_fBrightness;			// used together with m_cFilterColor
	uint32							m_dwMaskLayerId;		// 0xffffffff if not used

	CTextureBrush()
	{
		type = ET_BRUSH_PAINT;
		radius = 4;
		hardness = 1.0f;
		value = 255;
		bErase = false;
		minRadius = 0.01f;
		maxRadius = 256.0f;
		bDetailLayer = true;
		bMaskByLayerSettings=true;
		m_dwMaskLayerId=0xffffffff;
		m_cFilterColor=ColorF(1,1,1);
		m_fBrightness=1.0f;
	}
};

//////////////////////////////////////////////////////////////////////////
class CTerrainTexturePainter : public CEditTool
{
	DECLARE_DYNCREATE(CTerrainTexturePainter)
public:
	CTerrainTexturePainter();
	virtual ~CTerrainTexturePainter();

	virtual void BeginEditParams( IEditor *ie,int flags );
	virtual void EndEditParams();

	virtual void Display( DisplayContext &dc );

	// Ovverides from CEditTool
	bool MouseCallback( CViewport *view,EMouseEvent event,CPoint &point,int flags );

	// Key down.
	bool OnKeyDown( CViewport *view,uint nChar,uint nRepCnt,uint nFlags );
	bool OnKeyUp( CViewport *view,uint nChar,uint nRepCnt,uint nFlags ) { return false; };
	
	// Delete itself.
	void DeleteThis() { delete this; };

	void SetBrush( const CTextureBrush &brush ) { m_brush = brush; };
	void GetBrush( CTextureBrush &brush ) const { brush = m_brush; };

	//void ImportExport(bool bIsImport, bool bIsClipboard=false);

	void Action_Paint();
	void Action_PickLayerId();
	void Action_ChangeResolution();

	//Undo
	void Action_StartUndo();
	void Action_CollectUndo(float x, float y, float radius);
	void Action_StopUndo();

private:
	//////////////////////////////////////////////////////////////////////////
	// Private methods.
	//////////////////////////////////////////////////////////////////////////
	CLayer* GetSelectedLayer() const;

	// Arguments:
	//   texsector - make sure the values are in valid range
	void UpdateSectorTexture( CPoint texsector, const float fGlobalMinX, const float fGlobalMinY, const float fGlobalMaxX, const float fGlobalMaxY );

	//////////////////////////////////////////////////////////////////////////
	Vec3 m_pointerPos;

	//! Number of sectors per side.
	int m_numSectors;
	//! Size of sector texture.
	int m_sectorTexSize;
	//! Size of sector in meters.
	int m_sectorSize;
	//! Size of whole terrain texture.
	int m_surfaceTextureSize;

	//! Flag is true if painting mode. Used for Undo.
	bool m_bIsPainting;

	struct CUndoTPElement * m_pTPElem;

	std::vector<unsigned char> m_texBlock;
	
	// Cache often used interfaces.
	I3DEngine *m_3DEngine;
	IRenderer *m_renderer;
	CHeightmap *m_heightmap;
	CTerrainGrid *m_terrainGrid;

	CTerrainTexGen m_terrTexGen;

	static CTextureBrush m_brush;

	friend class CUndoTexturePainter;
};

#endif // __terraintexturepainter_h__
