// ColorDlgProDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ColorDlgProDlg.h"
#include "LightPanel.h"

/////////////////////////////////////////////////////////////////////////////
// CColorDlgProDlg dialog

CColorDlgProDlg::CColorDlgProDlg(COLORREF *col,CPanelDlg* pParent /*=NULL*/)
	: CDialog(CColorDlgProDlg::IDD, pParent)
{
	Owner=pParent;
	ColPtr=col;
	OrgColor=m_Color=*ColPtr;
	//{{AFX_DATA_INIT(CColorDlgProDlg)
	m_BlueNum = 0;
	m_GreenNum = 0;
	m_RedNum = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
}

void CColorDlgProDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CColorDlgProDlg)
	DDX_Control(pDX, IDC_RED, m_RedCtrl);
	DDX_Control(pDX, IDC_GREEN, m_GreenCtrl);
	DDX_Control(pDX, IDC_BLUE, m_BlueCtrl);
	DDX_Control(pDX, IDC_COLRES, m_ColDisplay);
	DDX_Text(pDX, IDC_BLUENUM, m_BlueNum);
	DDV_MinMaxUInt(pDX, m_BlueNum, 0, 255);
	DDX_Text(pDX, IDC_GREENNUM, m_GreenNum);
	DDV_MinMaxUInt(pDX, m_GreenNum, 0, 255);
	DDX_Text(pDX, IDC_REDNUM, m_RedNum);
	DDV_MinMaxUInt(pDX, m_RedNum, 0, 255);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CColorDlgProDlg, CDialog)
	//{{AFX_MSG_MAP(CColorDlgProDlg)
	ON_WM_HSCROLL()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_SHOWWINDOW()
	ON_WM_KILLFOCUS()
	ON_WM_LBUTTONUP()
	ON_EN_CHANGE(IDC_BLUENUM, OnChangeBluenum)
	ON_EN_CHANGE(IDC_GREENNUM, OnChangeGreennum)
	ON_EN_CHANGE(IDC_REDNUM, OnChangeRednum)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorDlgProDlg message handlers

BOOL CColorDlgProDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	m_BlueCtrl.SetRange(0,255);
	m_RedCtrl.SetRange(0,255);
	m_GreenCtrl.SetRange(0,255);
	SetResultColor(m_Color);
	PickerCur=AfxGetApp()->LoadCursor(IDC_PICKER);
	IsPicking=false;

	SetWindowPos(&CWnd::wndTopMost,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);       
return TRUE;  // return TRUE  unless you set the focus to a control
}



void CColorDlgProDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	UpdateResultColor();
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CColorDlgProDlg::UpdateResultColor()
{
m_Color=RGB(m_RedCtrl.GetPos(),m_GreenCtrl.GetPos(),m_BlueCtrl.GetPos());
*ColPtr=m_Color;
CBrush brush;
brush.CreateSolidBrush(m_Color);
CDC *pdc=m_ColDisplay.GetDC();
CRect rct;
m_ColDisplay.GetClientRect(&rct);
pdc->SelectObject(brush);
pdc->Rectangle(rct);
m_ColDisplay.ReleaseDC(pdc);
brush.DeleteObject();
	m_RedCtrl.SetColorRange(RGB(0,GetGValue(m_Color),GetBValue(m_Color)),RGB(255,GetGValue(m_Color),GetBValue(m_Color),));
	m_BlueCtrl.SetColorRange(RGB(GetRValue(m_Color),GetGValue(m_Color),0),RGB(GetRValue(m_Color),GetGValue(m_Color),255));
	m_GreenCtrl.SetColorRange(RGB(GetRValue(m_Color),0,GetBValue(m_Color)),RGB(GetRValue(m_Color),255,GetBValue(m_Color),));

	m_BlueNum=m_BlueCtrl.GetPos();
	m_RedNum=m_RedCtrl.GetPos();
	m_GreenNum=m_GreenCtrl.GetPos();
	UpdateData(FALSE);
m_RedCtrl.InvalidateRect(NULL);
m_RedCtrl.UpdateWindow();
m_GreenCtrl.InvalidateRect(NULL);
m_GreenCtrl.UpdateWindow();
m_BlueCtrl.InvalidateRect(NULL);
m_BlueCtrl.UpdateWindow();
Owner->UpdateGame();
}


LRESULT CColorDlgProDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	switch(message)
	{
	case WM_ACTIVATE:
		{
		}
	break;
	}

	return CDialog::WindowProc(message, wParam, lParam);
}


void CColorDlgProDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog::OnShowWindow(bShow, nStatus);
	
}

void CColorDlgProDlg::OnKillFocus(CWnd* pNewWnd) 
{
	CDialog::OnKillFocus(pNewWnd);
	// TODO: Add your message handler code here
	
}


void CColorDlgProDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CRect rct;
	m_ColDisplay.GetWindowRect(&rct);
	ScreenToClient(&rct);

	if(rct.PtInRect(point))
	{
	SetCapture();	
	OldCur=SetCursor(PickerCur);
	IsPicking=true;
	}
	CDialog::OnLButtonDown(nFlags, point);
}

void CColorDlgProDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if(IsPicking)
	{
	ReleaseCapture();
	SetCursor(OldCur);
	IsPicking=false;
	}
	
	CDialog::OnLButtonUp(nFlags, point);
}

void CColorDlgProDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(IsPicking  && nFlags&MK_LBUTTON)
	{
	ClientToScreen(&point);
		HDC pDC=::GetDC(NULL);
		SetResultColor(::GetPixel(pDC,point.x,point.y));
		::ReleaseDC(NULL,pDC);
	}
	
	CDialog::OnMouseMove(nFlags, point);
}

void CColorDlgProDlg::SetResultColor(COLORREF col)
{
	m_Color=col;
	m_RedCtrl.SetPos(GetRValue(m_Color));
	m_GreenCtrl.SetPos(GetGValue(m_Color));
	m_BlueCtrl.SetPos(GetBValue(m_Color));
	UpdateResultColor();

}

void CColorDlgProDlg::OnChangeBluenum() 
{
	UpdateData(TRUE);
	m_BlueCtrl.SetPos(m_BlueNum);
	UpdateResultColor();
	
}

void CColorDlgProDlg::OnChangeGreennum() 
{
	UpdateData(TRUE);
	m_GreenCtrl.SetPos(m_GreenNum);
	UpdateResultColor();
	
}

void CColorDlgProDlg::OnChangeRednum() 
{
	UpdateData(TRUE);
	m_RedCtrl.SetPos(m_RedNum);
	UpdateResultColor();
	
}

void CColorDlgProDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	*ColPtr=m_Color=OrgColor;
	CDialog::OnCancel();
}

void CColorDlgProDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

BOOL CColorDlgProDlg::Create(CWnd* pParentWnd) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CDialog::Create(IDD, pParentWnd);
}
