// PlanetsDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Planets.h"
#include "PlanetsDlg.h"

#include <math.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPlanetsDlg dialog

CPlanetsDlg::CPlanetsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPlanetsDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPlanetsDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_initialized = FALSE;
	m_planetCanvas = NULL;
}

CPlanetsDlg::~CPlanetsDlg()
{
	// Discard the currently displayed planet's data.
	if(m_colorMap != NULL)
	{
		DeleteObject(m_colorMap);
	}
	if(m_topoMap != NULL)
	{
		DeleteObject(m_topoMap);
	}

	// Delete the canvas image (if any).
	if(m_planetCanvas)
	{
		DeleteObject(m_planetCanvas);
	}
}

void CPlanetsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPlanetsDlg)
	DDX_Control(pDX, IDC_ROTATE, m_rotateBar);
	DDX_Control(pDX, IDC_PLANETDISPLAY, m_planetDisplay);
	DDX_Control(pDX, IDC_PLANETINFO, m_planetInfo);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPlanetsDlg, CDialog)
	//{{AFX_MSG_MAP(CPlanetsDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_SIZE()
	ON_WM_HSCROLL()
	ON_COMMAND(ID_PLANETS_EXIT, OnPlanetsExit)
	ON_COMMAND(ID_PLANETS_EARTH, OnPlanetsEarth)
	ON_COMMAND(ID_PLANETS_MOON, OnPlanetsMoon)
	ON_COMMAND(ID_PLANETS_MARS, OnPlanetsMars)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPlanetsDlg message handlers

BOOL CPlanetsDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	m_rotateBar.SetScrollRange(-179, 180, FALSE);
	m_rotationAngle = 0;
	SetPlanetRotation();
	Layout();
	OnPlanetsMoon();

	m_initialized = TRUE;

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CPlanetsDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPlanetsDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPlanetsDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	if(m_initialized)
	{
		Layout();
		Render();
	}
}

void CPlanetsDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	switch(nSBCode)
	{
	case SB_LEFT:
		m_rotationAngle = -180;
		break;

	case SB_RIGHT:
		m_rotationAngle = 180;
		break;

	case SB_LINELEFT:
		m_rotationAngle -= 1;
		break;

	case SB_LINERIGHT:
		m_rotationAngle += 1;
		break;

	case SB_PAGELEFT:
		m_rotationAngle -= 10;
		break;

	case SB_PAGERIGHT:
		m_rotationAngle += 10;
		break;

	case SB_THUMBPOSITION:
		m_rotationAngle = nPos;
		break;

	case SB_THUMBTRACK:
		m_rotationAngle = nPos;
		break;
	}

	m_rotationAngle =
		(m_rotationAngle < -180)? -180 :
		(m_rotationAngle <= 180)? m_rotationAngle :
		-180;

	m_rotateBar.SetScrollPos(m_rotationAngle, TRUE);
	SetPlanetRotation();
	Render();
	
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CPlanetsDlg::Layout()
{
	if(m_planetCanvas != NULL)
	{
		m_planetDisplay.SetBitmap(NULL);
		DeleteObject(m_planetCanvas);
		m_planetCanvas = NULL;
	}

	const DWORD dwBarHeight = 16;
	const DWORD dwPadding = 10;

	RECT clientRect;
	GetClientRect(&clientRect);
	DWORD dwClientWidth = clientRect.right + 1 - clientRect.left;
	DWORD dwClientHeight = clientRect.bottom + 1 - clientRect.top;
	DWORD dwCanvasHeight =
		(dwClientHeight - 2 * dwPadding - dwBarHeight) / 2;

	m_planetDisplay.MoveWindow(
		dwPadding,
		dwPadding,
		dwClientWidth - 2 * dwPadding,
		dwCanvasHeight,
		TRUE);
	m_rotateBar.MoveWindow(
		dwPadding,
		dwPadding + dwCanvasHeight,
		dwClientWidth - 2 * dwPadding,
		dwBarHeight,
		TRUE);
	m_planetInfo.MoveWindow(
		dwPadding,
		dwPadding + dwCanvasHeight + dwBarHeight,
		dwClientWidth - 2 * dwPadding,
		dwCanvasHeight,
		TRUE);

	if(dwCanvasHeight > 0)
	{
		HDC dc = GetDC()->m_hDC;
		BITMAPINFO canvasInfo =
		{
			{
				sizeof(BITMAPINFOHEADER),
				dwClientWidth - 2 * dwPadding,
				dwCanvasHeight,
				1,
				24,
				0
			},
			0
		};
		VOID *pCanvasBits;
		m_planetCanvas = CreateDIBSection(
			dc,
			&canvasInfo,
			DIB_RGB_COLORS,
			&pCanvasBits,
			NULL,
			0);
	}
	m_planetDisplay.SetBitmap(m_planetCanvas);
}

void CPlanetsDlg::SetPlanetRotation()
{
	double angle = m_rotationAngle * (m_pi / 180);
	m_planetView.SetViewerPosition(angle, 1 / cos(87 * (m_pi / 180)));
	m_planetView.SetLightPosition(angle + m_pi * 5 / 12);
}

void CPlanetsDlg::LoadPlanetData(LPCSTR lpszColorFile, LPCSTR lpszTopographyFile, double heightRange)
{
	if(m_colorMap != NULL)
	{
		DeleteObject(m_colorMap);
	}
	if(m_topoMap != NULL)
	{
		DeleteObject(m_topoMap);
	}
	
	m_colorMap = (HBITMAP) LoadImage(
		NULL,
		lpszColorFile,
		IMAGE_BITMAP,
		0,
		0,
		LR_CREATEDIBSECTION | LR_LOADFROMFILE);
	m_topoMap = (HBITMAP) LoadImage(
		NULL,
		lpszTopographyFile,
		IMAGE_BITMAP,
		0,
		0,
		LR_CREATEDIBSECTION | LR_LOADFROMFILE);
	// Exaggerate the height for appearances.
	m_heightRange = 30 * heightRange;
}

void CPlanetsDlg::Render()
{
	if(m_colorMap != NULL && m_topoMap != NULL && m_planetCanvas != NULL)
	{
		m_planetView.Render(
			m_colorMap,
			m_topoMap,
			m_heightRange,
			m_planetCanvas);
		m_planetDisplay.SetBitmap(m_planetCanvas);
	}
}

void CPlanetsDlg::OnPlanetsEarth() 
{
	LoadPlanetData(
		"maps\\earth\\earthcolor.bmp",
		"maps\\earth\\earthtopo.bmp",
		0);
	m_planetInfo.Navigate(
		"about:blank",
		NULL,
		NULL,
		NULL,
		NULL);
	Render();
}

void CPlanetsDlg::OnPlanetsMoon() 
{
	LoadPlanetData(
		"maps\\moon\\mooncolor.bmp",
		"maps\\moon\\moontopo.bmp",
		0.0096);
	
	//COleVariant url("file:c:/mssdk/projects/planets/html/moon.html");
	//m_planetInfo.Navigate2(
	//	(LPVARIANT) url,
	//	NULL,
	//	NULL,
	//	NULL,
	//	NULL);

	Render();
}

void CPlanetsDlg::OnPlanetsMars() 
{
	
}

void CPlanetsDlg::OnPlanetsExit() 
{
	EndDialog(0);
}

const double CPlanetsDlg::m_pi = 3.14159;
