// FogDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed.h"
#include "FogDlg.h"

#include <Adept\Mission.hpp>
#include <ElementRenderer\StateChange.hpp>
#include <MLR\MLRPrimitiveBase.hpp>

/////////////////////////////////////////////////////////////////////////////
// FogDlg dialog


FogDlg::FogDlg(CWnd* pParent /*=NULL*/)
	: CDialog(FogDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(FogDlg)
	m_CustomDensity = 0.0f;
	m_Blue = 0.0f;
	m_CustomEnd = 0.0f;
	m_CustomStart = 0.0f;
	m_GeneralDensity = 0.0f;
	m_GeneralEnd = 0.0f;
	m_GeneralStart = 0.0f;
	m_Green = 0.0f;
	m_LightDensity = 0.0f;
	m_LightEnd = 0.0f;
	m_LightStart = 0.0f;
	m_Red = 0.0f;
	//}}AFX_DATA_INIT
}


void FogDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(FogDlg)
	DDX_Text(pDX, IDC_EDITCUSTOMDENSITY, m_CustomDensity);
	DDX_Text(pDX, IDC_EDITBLUE, m_Blue);
	DDX_Text(pDX, IDC_EDITCUSTOMEND, m_CustomEnd);
	DDX_Text(pDX, IDC_EDITCUSTOMSTART, m_CustomStart);	
	DDX_Text(pDX, IDC_EDITGENERALDENSITY, m_GeneralDensity);
	DDX_Text(pDX, IDC_EDITGENERALEND, m_GeneralEnd);
	DDX_Text(pDX, IDC_EDITGENERALSTART, m_GeneralStart);
	DDX_Text(pDX, IDC_EDITGREEN, m_Green);
	DDX_Text(pDX, IDC_EDITLIGHTDENSITY, m_LightDensity);
	DDX_Text(pDX, IDC_EDITLIGHTEND, m_LightEnd);	
	DDX_Text(pDX, IDC_EDITLIGHTSTART, m_LightStart);
	DDX_Text(pDX, IDC_EDITRED, m_Red);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(FogDlg, CDialog)
	//{{AFX_MSG_MAP(FogDlg)
	ON_EN_KILLFOCUS(IDC_EDITBLUE, OnKillfocusEditblue)
	ON_EN_KILLFOCUS(IDC_EDITCUSTOMDENSITY, OnKillfocusEditcustomdensity)
	ON_EN_KILLFOCUS(IDC_EDITCUSTOMEND, OnKillfocusEditcustomend)
	ON_EN_KILLFOCUS(IDC_EDITCUSTOMSTART, OnKillfocusEditcustomstart)
	ON_EN_KILLFOCUS(IDC_EDITGENERALDENSITY, OnKillfocusEditgeneraldensity)
	ON_EN_KILLFOCUS(IDC_EDITGENERALEND, OnKillfocusEditgeneralend)
	ON_EN_KILLFOCUS(IDC_EDITGENERALSTART, OnKillfocusEditgeneralstart)
	ON_EN_KILLFOCUS(IDC_EDITGREEN, OnKillfocusEditgreen)
	ON_EN_KILLFOCUS(IDC_EDITLIGHTDENSITY, OnKillfocusEditlightdensity)
	ON_EN_KILLFOCUS(IDC_EDITLIGHTEND, OnKillfocusEditlightend)
	ON_EN_KILLFOCUS(IDC_EDITLIGHTSTART, OnKillfocusEditlightstart)
	ON_EN_KILLFOCUS(IDC_EDITRED, OnKillfocusEditred)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPINBLUE, OnDeltaposSpinblue)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPINCUSTOMDENSITY, OnDeltaposSpincustomdensity)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPINCUSTOMEND, OnDeltaposSpincustomend)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPINCUSTOMSTART, OnDeltaposSpincustomstart)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPINGENERALDENSITY, OnDeltaposSpingeneraldensity)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPINGENERALEND, OnDeltaposSpingeneralend)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPINGENERALSTART, OnDeltaposSpingeneralstart)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPINGREEN, OnDeltaposSpingreen)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPINLIGHTDENSITY, OnDeltaposSpinlightdensity)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPINLIGHTEND, OnDeltaposSpinlightend)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPINLIGHTSTART, OnDeltaposSpinlightstart)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPINRED, OnDeltaposSpinred)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// FogDlg message handlers

BOOL FogDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	const Mission__GameModel *model = Mission::Instance->GetGameModel();

	m_CustomDensity = model->m_customFogDensity;
	m_CustomStart = model->m_customFogStart;
	m_CustomEnd = model->m_customFogEnd;
	m_GeneralDensity = model->m_generalFogDensity;
	m_GeneralStart = model->m_generalFogStart;
	m_GeneralEnd = model->m_generalFogEnd;
	m_LightDensity = model->m_lightFogDensity;
	m_LightStart = model->m_lightFogStart;
	m_LightEnd = model->m_lightFogEnd;

	Stuff::RGBAColor color = model->m_fogColor;

	m_Red = color.red;
	m_Green = color.green;
	m_Blue = color.blue;


	m_OldCustomDensity = m_CustomDensity;
	m_OldBlue =	m_Blue;
	m_OldCustomEnd = m_CustomEnd;
	m_OldCustomStart = m_CustomStart;
	m_OldGeneralDensity = m_GeneralDensity;
	m_OldGeneralEnd = m_GeneralEnd;
	m_OldGeneralStart = m_GeneralStart;
	m_OldGreen = m_Green;
	m_OldLightDensity = m_LightDensity;
	m_OldLightEnd =	m_LightEnd;
	m_OldLightStart = m_LightStart;
	m_OldRed = m_Red;

	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void FogDlg::OnOK() 
{
	UpdateData(TRUE);

	if (!SetFogValue("CustomFogDensity",m_CustomDensity))
		return;
	if (!SetFogValue("CustomFogStart",m_CustomStart))
		return;
	if (!SetFogValue("CustomFogEnd",m_CustomEnd))
		return;
	if (!SetFogValue("GeneralFogDensity",m_GeneralDensity))
		return;
	if (!SetFogValue("GeneralFogStart",m_GeneralStart))
		return;
	if (!SetFogValue("GeneralFogEnd",m_GeneralEnd))
		return;
	if (!SetFogValue("LightFogDensity",m_LightDensity))
		return;
	if (!SetFogValue("LightFogStart",m_LightStart))
		return;
	if (!SetFogValue("LightFogEnd",m_LightEnd))
		return;

	RGBAColor color(m_Red,m_Green,m_Blue,1.0f);
	if (!SetFogValue("FogColor",color))
		return;

	CDialog::OnOK();
}

void FogDlg::OnCancel() 
{
	SetFogValue("CustomFogDensity",m_OldCustomDensity);
	SetFogValue("CustomFogStart",m_OldCustomStart);
	SetFogValue("CustomFogEnd",m_OldCustomEnd);
	SetFogValue("GeneralFogDensity",m_OldGeneralDensity);
	SetFogValue("GeneralFogStart",m_OldGeneralStart);
	SetFogValue("GeneralFogEnd",m_OldGeneralEnd);
	SetFogValue("LightFogDensity",m_OldLightDensity);
	SetFogValue("LightFogStart",m_OldLightStart);
	SetFogValue("LightFogEnd",m_OldLightEnd);
	
	RGBAColor color(m_Red,m_Green,m_Blue,1.0f);
	SetFogValue("FogColor",color);

	CDialog::OnCancel();
}

bool FogDlg::SetFogValue(char* attributeName, Scalar value) 
{
	char string[16];

	return SetFogValue(attributeName,gcvt(value,3,string));
}

bool FogDlg::SetFogValue(char* attributeName, RGBAColor value) 
{
	char color[32];
	sprintf(color,"%4.2f %4.2f %4.2f 1.0",m_Red,m_Green,m_Blue);

	return SetFogValue("FogColor",color);
}

bool FogDlg::SetFogValue(char* attributeName, char* value) 
{
	bool result = true;
	char *error_message;
	error_message = new char[256];
	Register_Pointer(error_message);

	const Mission__GameModel *model = Mission::Instance->GetGameModel();
	Check_Object(model);
	Mission::ClassData *class_data;
	class_data = Mission::Instance->GetClassData();
	Check_Object(class_data);

	ModelAttributeEntry *attrib_entry;

	attrib_entry = 
			class_data->gameModelAttributeTable.GetAttributeEntry(attributeName);
	Check_Object(attrib_entry);

	if(!(*class_data->modelVerifier)((Entity__GameModel *)model, attrib_entry, value, &error_message,255))
	{
		char error[256];
		sprintf(error,"Error with %s: %s",attributeName,error_message);
		AfxMessageBox(error, MB_OK);
		result = false;
	}

	Unregister_Pointer(error_message);
	delete [] error_message;

//	const GameModel *model = GetGameModel();
//	Check_Object(model);
	ElementRenderer::StateChange::SetFogColor(model->m_fogColor);
	MidLevelRenderer::MLRPrimitiveBase::SetFogTableEntry(
		1,
		model->m_generalFogStart,
		model->m_generalFogEnd,
		model->m_generalFogDensity
	);
	MidLevelRenderer::MLRPrimitiveBase::SetFogTableEntry(
		2,
		model->m_lightFogStart,
		model->m_lightFogEnd,
		model->m_lightFogDensity
	);
	MidLevelRenderer::MLRPrimitiveBase::SetFogTableEntry(
		3,
		model->m_customFogStart,
		model->m_customFogEnd,
		model->m_customFogDensity
	);

	return result;
}

void FogDlg::OnKillfocusEditcustomdensity() 
{
	UpdateData(TRUE);
	SetFogValue("CustomFogDensity",m_CustomDensity);
}

void FogDlg::OnKillfocusEditcustomend() 
{
	UpdateData(TRUE);
	SetFogValue("CustomFogEnd",m_CustomEnd);
}

void FogDlg::OnKillfocusEditcustomstart() 
{
	UpdateData(TRUE);
	SetFogValue("CustomFogStart",m_CustomStart);
}

void FogDlg::OnKillfocusEditgeneraldensity() 
{
	UpdateData(TRUE);
	SetFogValue("GeneralFogDensity",m_GeneralDensity);
}

void FogDlg::OnKillfocusEditgeneralend() 
{
	UpdateData(TRUE);
	SetFogValue("GeneralFogEnd",m_GeneralEnd);
}

void FogDlg::OnKillfocusEditgeneralstart() 
{
	UpdateData(TRUE);
	SetFogValue("GeneralFogStart",m_GeneralStart);
}

void FogDlg::OnKillfocusEditlightdensity() 
{
	UpdateData(TRUE);
	SetFogValue("LightFogDensity",m_LightDensity);
}

void FogDlg::OnKillfocusEditlightend() 
{
	UpdateData(TRUE);
	SetFogValue("LightFogEnd",m_LightEnd);
}

void FogDlg::OnKillfocusEditlightstart() 
{
	UpdateData(TRUE);
	SetFogValue("LightFogStart",m_LightStart);
}

void FogDlg::OnKillfocusEditred() 
{
	UpdateData(TRUE);
	Stuff::RGBAColor color(m_Red,m_Green,m_Blue,1.0f);
	SetFogValue("FogColor",color);
}

void FogDlg::OnKillfocusEditgreen() 
{
	UpdateData(TRUE);
	Stuff::RGBAColor color(m_Red,m_Green,m_Blue,1.0f);
	SetFogValue("FogColor",color);
}

void FogDlg::OnKillfocusEditblue() 
{
	UpdateData(TRUE);
	Stuff::RGBAColor color(m_Red,m_Green,m_Blue,1.0f);
	SetFogValue("FogColor",color);
}

void FogDlg::OnDeltaposSpincustomdensity(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

	m_CustomDensity -= (pNMUpDown->iDelta > 0) ? 0.01f : -0.01f;
	SetFogValue("CustomFogDensity",m_CustomDensity);
	UpdateData(FALSE);
	
	*pResult = 0;
}

void FogDlg::OnDeltaposSpincustomend(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

	m_CustomEnd -= pNMUpDown->iDelta;
	SetFogValue("CustomFogEnd",m_CustomEnd);
	UpdateData(FALSE);
	
	*pResult = 0;
}

void FogDlg::OnDeltaposSpincustomstart(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

	m_CustomStart -= pNMUpDown->iDelta;
	SetFogValue("CustomFogStart",m_CustomStart);
	UpdateData(FALSE);

	*pResult = 0;
}

void FogDlg::OnDeltaposSpingeneraldensity(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

	m_GeneralDensity -= (pNMUpDown->iDelta > 0) ? 0.01f : -0.01f;
	SetFogValue("GeneralFogDensity",m_GeneralDensity);
	UpdateData(FALSE);

	*pResult = 0;
}

void FogDlg::OnDeltaposSpingeneralend(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

	m_GeneralEnd -= pNMUpDown->iDelta;
	SetFogValue("GeneralFogEnd",m_GeneralEnd);
	UpdateData(FALSE);

	*pResult = 0;
}

void FogDlg::OnDeltaposSpingeneralstart(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

	m_GeneralStart -= pNMUpDown->iDelta;
	SetFogValue("GeneralFogStart",m_GeneralStart);
	UpdateData(FALSE);

	*pResult = 0;
}

void FogDlg::OnDeltaposSpinlightdensity(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

	m_LightDensity -= (pNMUpDown->iDelta > 0) ? 0.01f : -0.01f;
	SetFogValue("LightFogDensity",m_LightDensity);
	UpdateData(FALSE);

	*pResult = 0;
}

void FogDlg::OnDeltaposSpinlightend(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

	m_LightEnd -= pNMUpDown->iDelta;
	SetFogValue("LightFogEnd",m_LightEnd);
	UpdateData(FALSE);

	*pResult = 0;
}

void FogDlg::OnDeltaposSpinlightstart(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

	m_LightStart -= pNMUpDown->iDelta;
	SetFogValue("LightFogStart",m_LightStart);
	UpdateData(FALSE);

	*pResult = 0;
}

void FogDlg::OnDeltaposSpinred(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

	m_Red -= (pNMUpDown->iDelta > 0) ? 0.01f : -0.01f;
	Stuff::RGBAColor color(m_Red,m_Green,m_Blue,1.0f);
	SetFogValue("FogColor",color);
	UpdateData(FALSE);

	*pResult = 0;
}

void FogDlg::OnDeltaposSpinblue(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

	m_Blue -= (pNMUpDown->iDelta > 0) ? 0.01f : -0.01f;
	Stuff::RGBAColor color(m_Red,m_Green,m_Blue,1.0f);
	SetFogValue("FogColor",color);
	UpdateData(FALSE);

	*pResult = 0;
}

void FogDlg::OnDeltaposSpingreen(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;

	m_Green -= (pNMUpDown->iDelta > 0) ? 0.01f : -0.01f;
	Stuff::RGBAColor color(m_Red,m_Green,m_Blue,1.0f);
	SetFogValue("FogColor",color);
	UpdateData(FALSE);

	*pResult = 0;
}

