// BackGroundProps.cpp : implementation file
//

#include "stdafx.h"
#include "imagegrinder.h"
#include "BackGroundProps.h"
#include <Compost\TexturePool.hpp>


/////////////////////////////////////////////////////////////////////////////
// CBackGroundProps dialog


CBackGroundProps::CBackGroundProps(CWnd* pParent /*=NULL*/)
	: CDialog(CBackGroundProps::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBackGroundProps)
	m_stretch = FALSE;
	//}}AFX_DATA_INIT
}


void CBackGroundProps::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBackGroundProps)
	DDX_Control(pDX, IDC_MATLIST, m_MatList);
	DDX_Control(pDX, IDC_TEXTURELIST, m_TextureList);
	DDX_Check(pDX, IDC_STRETCH, m_stretch);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBackGroundProps, CDialog)
	//{{AFX_MSG_MAP(CBackGroundProps)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBackGroundProps message handlers

int CBackGroundProps::DoModal(Compost::Tool_Feature *fet) 
{
	
	feature_data=fet;
	return CDialog::DoModal();
}

BOOL CBackGroundProps::OnInitDialog() 
{
	CDialog::OnInitDialog();
	int i;
	for( i=0;i<Compost::MaterialEntriesCount;i++)
	{
		if(!Compost::MaterialEntries[i].MatName) break;
		int itm = m_MatList.AddString((char *)Compost::MaterialEntries[i].MatName);
		m_MatList.SetItemData(itm, i);
	}
	m_MatList.SetCurSel(feature_data->GetMaterial());

	Compost::Feature_Texture *FTexture;
	Stuff::TableIteratorOf<Compost::Feature_Texture*, int> imageTableIterator(Compost::TexturePool::Instance->GetImageTable());
	
	while(NULL!=(FTexture=imageTableIterator.ReadAndNext()))
	{
		int idx;
		idx=m_TextureList.AddString((char *)(*(FTexture->GetName())));
		m_TextureList.SetItemDataPtr(idx,FTexture);
	}

	int sel=-1;
	for(i=0;i<m_TextureList.GetCount();i++)
	{
	if(m_TextureList.GetItemDataPtr(i)==feature_data->GetFeatureTexture())
		sel=i;
	}
	if(sel!=-1)
	m_TextureList.SetCurSel(sel);
	else
	m_TextureList.SetCurSel(0);

	m_stretch=(feature_data->GetBlendMode()==Compost::Feature::Blend_Mode::StretchPaste);
	UpdateData(FALSE);
		return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CBackGroundProps::OnOK() 
{
	feature_data->SetFeatureTexture((Compost::Feature_Texture *)m_TextureList.GetItemDataPtr(m_TextureList.GetCurSel()));
	feature_data->SetMaterial(m_MatList.GetCurSel());


	Compost::TexturePool::Instance->Load(feature_data->GetFeatureTexture());
	Compost::TexturePool::Instance->Load(feature_data->GetFeatureMask0());
	Compost::TexturePool::Instance->Load(feature_data->GetFeatureMask1());
	
	CDialog::OnOK();
}

