#include "stdafx.h"
#include "Resource.h"
#include "MyButton.h"
#include "JoyStickDlg.h"

BEGIN_MESSAGE_MAP(CMyButton, CButton)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()

VOID CMyButton::SetTargetHWND(CJoyStickDlg* dlg)
{
	m_dlg = dlg;
}

VOID CMyButton::SetIndex(LONG lType, LONG lIndex)
{
	m_lType = lType;
	m_lIndex = lIndex;
}

// On_MouseDown
void CMyButton::OnLButtonDown(UINT nFlags, CPoint point)
{
	m_bClicked = TRUE;

	// Set focus and pushed state
	this->SetFocus();
	this->SetState(0x4);

	// Set move direction and joint
	m_dlg->SetManualButton(m_lType, m_lIndex);

	m_dlg->StartTimer();
}

// On_MouseUp
void CMyButton::OnLButtonUp(UINT nFlags, CPoint point)
{
	m_bClicked = FALSE;

	// Set unpushed state
	this->SetState(0x0);

	// Reset move direction and joint
	m_dlg->SetManualButton(0L, 0L);

	m_dlg->KillTimer(1);
}

// On enter or leave button area
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
	CRect rect;
	GetClientRect(&rect);

	// Enter button area
	if(PtInRect(rect, point))
	{
		if(GetCapture() == NULL)
		{
			SetCapture();
		}
	}
	else // Leave button area
	{
		if(GetCapture() != NULL)
		{
			ReleaseCapture();

			if(m_bClicked == TRUE){
				OnLButtonUp(nFlags, point);
			}
		}
	}
}
