// ImageGrinderDoc.h : interface of the CImageGrinderDoc class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_IMAGEGRINDERDOC_H__1B275CEB_0C5E_11D3_A5F1_00902712C9AE__INCLUDED_)
#define AFX_IMAGEGRINDERDOC_H__1B275CEB_0C5E_11D3_A5F1_00902712C9AE__INCLUDED_

#include <Compost\FeatureGrid.hpp>
#include "UndoManager.h"

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define MAXSELECTION 1024
#define HANDLESIZE 10

#define  HF_NONE	0 
#define  HF_TOP		1<<0 
#define  HF_BOTTOM	1<<1 
#define  HF_LEFT	1<<2 
#define  HF_RIGHT	1<<3


class CImageGrinderDoc : public CDocument
{
protected: // create from serialization only
	CImageGrinderDoc();
	DECLARE_DYNCREATE(CImageGrinderDoc)

// Attributes
public:
	Compost::FeatureGrid *FGrid;
	Compost::FeaturePool *FPool;
	Compost::Tool_Feature *CurSelFet;
	UndoManager UndoMan;

	bool *UpdateList,ManualUpdate;
	CPoint NewPlacePoint;
//	HeightField BaseHF,FinalHF;
	
	// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CImageGrinderDoc)
	public:
	virtual BOOL OnNewDocument();
	virtual void Serialize(CArchive& ar);
	virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);
	virtual BOOL OnSaveDocument(LPCTSTR lpszPathName);
	//}}AFX_VIRTUAL

// Implementation
public:
	CString MapPath();
	void SaveIGDataToGame();
	void SaveToGame();
	void SaveAs(LPCTSTR lpszPathName,int xoffset=0,int zoffset=0);
	void SaveFgd(LPCTSTR lpszPathName,int xoffset=0,int zoffset=0);
	void SaveText(LPCTSTR lpszPathName,int xoffset=0,int zoffset=0);
	void OpenFgd(LPCTSTR lpszPathName);
	void OpenText(LPCTSTR lpszPathName);
	void CloseCurrent();
	void PostLoadInit();
//	void BuildHF();
//	void LoadBaseHF();
//	void LoadBaseHF(CString &str);
	void RemoveFeature(Compost::Tool_Feature *fet);
	void ClearSelection();
	void UnLockAll();
	bool IsHLFeature(Compost::FeatureInstance *fetint);
	void CloneSelection();
	CString MakeNameUnique(CString &str);
	bool InList(CString &str);
	void MakeSelectedUnique();
	void MakeUnique(Compost::FeatureInstance *fetint);
	Compost::FeatureInstance *Clone(Compost::FeatureInstance *fetint);
	Compost::FeatureInstance *GetTopSelected(CPoint &pnt);
	Compost::FeatureInstance *GetTopFeature(CPoint &pnt);
	DWORD IsOnScaleHandle(CPoint &pnt,Compost::FeatureInstance *fetint);
	bool IsOnSelectHandle(CPoint &pnt,Compost::FeatureInstance *fetint);
	bool IsOnSelectHandle(CPoint &pnt);
	bool InSelectedRegion(CPoint &pnt);
	bool IsSelected(Compost::FeatureInstance *fetint);
	bool IsSelected(Compost::Tool_Feature *fet);
	void DeleteSelected();
	void SetDrawFlags(Compost::Feature *fet);
	void SetDrawFlags(Compost::FeatureInstance *fet);
	void SetDrawFlags();
	void SelectionByList(CPoint &pnt);
	void NewSelection(CRect &pnt);
	void AddSelection(CRect &pnt);
	void SubSelection(CRect &pnt);
	void NewSelection(CPoint &pnt);
	void SavePosForUndo();
	void AddSelection(CPoint &pnt);
	void SubSelection(CPoint &pnt);
	void NewSelection(Compost::FeatureInstance *fet);
	void AddSelection(Compost::FeatureInstance *fet);
	void SubSelection(Compost::FeatureInstance *fet);
	void OffsetSelection(int x,int y);
	void AddFeature(Compost::Feature *fet);
	void AddFeature(Compost::Feature *fet,int x,int y);
	void AddFeature(int x,int y) { if(CurSelFet) AddFeature(CurSelFet,x,y); }
	virtual ~CImageGrinderDoc();
	void ClearHL();
	void AddHLFeature(Compost::Tool_Feature *fet);
	void SubHLFeature(Compost::Tool_Feature *fet);
	void LockSelection();
	void LockFeature(Compost::FeatureInstance  *fet);
	void UnLockFeature(Compost::FeatureInstance  *fet);
	bool IsLocked(Compost::FeatureInstance  *fet);
	void MakeWorldSpaceTexture(Compost::FeatureInstance  *fet);
	void MakeLocalTexture(Compost::FeatureInstance  *fet);
	void MakeSelectionWorldSpaceTexture();
	void MakeSelectionLocalTexture();
	void SetFlagOnSelection(UINT flg);
	void ClearFlagOnSelection(UINT flg);
	void GroupSelection();
	void UnGroupSelection();
	int GetAvailableTag();
	void SelectGroupMembers(Compost::FeatureInstance  *fet);
	void DeSelectGroupMembers(Compost::FeatureInstance  *fet);
	void ExportMaterialMap(CString &path);
	void ExportSingleMaterialMap(CString &path);
	
	int SnapMask;

#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
public:
	//{{AFX_MSG(CImageGrinderDoc)
	afx_msg void OnFileExportTgafiles();
	afx_msg void OnFileExportImagedata();
	afx_msg void OnFileExportSingletga();
	afx_msg void OnFileMergefeatures();
	afx_msg void OnFileExportUsedimagelist();
	afx_msg void OnFileExportMaterialmap();
	afx_msg void OnFileExportSinglematerialmap();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_IMAGEGRINDERDOC_H__1B275CEB_0C5E_11D3_A5F1_00902712C9AE__INCLUDED_)
