#include "StdAfx.h"
#include "BUTTON.h"


BUTTON::BUTTON(LPCWSTR FileName1,LPCWSTR FileName2)
{
	Type = 1;
	Image		= new IMAGE;
	Pic			= new IMAGE;
	ImageEnter	= new IMAGE;

	Image->LoadImageFromFile(FileName1);
	ImageEnter->LoadImageFromFile(FileName2);

	Pic = Image;

	Size.x = (float)Image->Width;
	Size.y = (float)Image->Height;

	this->Position.x = 0;
	this->Position.y = 0;

	ClickFunc = NULL;
	EnterFunc = NULL;

	Triger = 0;

}

BUTTON::BUTTON(LPCWSTR FileName1)
{
	Type = 2;
	Image		= new IMAGE;
	Pic			= new IMAGE;

	Image->LoadImageFromFile(FileName1);

	Pic = Image;

	Size.x = (float)Image->Width;
	Size.y = (float)Image->Height;

	this->Position.x = 0;
	this->Position.y = 0;

	ClickFunc = NULL;
	EnterFunc = NULL;

	Triger = 0;

}


void	BUTTON::Draw(void)
{
		Pic->Draw();
}
void	BUTTON::Flip(void)
{
	
	Input->Update();
	POINT	Point;
	GetCursorPos(&Point);
	ScreenToClient(hWnd,&Point);

	switch (Type)
	{
	case 1:
		{
			if( (Point.x > Position.x) & (Point.x < Position.x + Size.x) &(Point.y > Position.y) & ( Point.y < Position.y + Size.y))
			{

					Pic = ImageEnter;
					this->EnterMouse();

					if(Input->MouseDown(0))
					{
						Triger = true;
						
					}
					else
					{
						if(Triger)			
							this->ClickMouse();

						Triger = false;
					}
			}
			else
			{
				Pic = Image;
			}

		}
		break;
	case 2:
		{
			if( (Point.x > Position.x) & (Point.x < Position.x + Size.x) &(Point.y > Position.y) & ( Point.y < Position.y + Size.y))	
			{
				if(Triger & !Input->MouseDown(0))	
				{
					this->ClickMouse();
				}
				
				Pic->SetARGB(255,255,150,10);
				this->EnterMouse();
	
				if(Input->MouseDown(0))
				{
					Pic->SetARGB(255,15,10,10);
					Triger = true;
			//		this->DownMouse();
				}
				else
				{
					Triger = false;
				}
			}
			else
			{
				Triger = false;
				Pic->SetARGB(255,255,255,255);
			}
		}
		break;
		}
	}
void	BUTTON::SetPosition(D3DXVECTOR2 Position)
{
	switch (Type)
	{
	case 1:
		{
			this->Position = Position;
			Pic->SetPosition(Position);
			Image->SetPosition(Position);
			ImageEnter->SetPosition(Position);

		}
		break;
	case 2:
		{
			this->Position = Position;
			Pic->SetPosition(Position);
			Image->SetPosition(Position);
		}
		break;
	}

}
void	BUTTON::SetPosition(float PosX,float PosY)
{
	D3DXVECTOR2 Pos;
	Pos.x = PosX;
	Pos.y = PosY;
	SetPosition(Pos);
}
void	BUTTON::SetSize(D3DXVECTOR2 Size)
{
	switch (Type)
	{
	case 1:
		{
			this->Size = Size;
			Pic->SetSize(Size.x/Pic->Width,Size.y/Pic->Height);
			Image->SetSize(Size.x/Image->Width,Size.y/Image->Height);

			ImageEnter->SetSize(Size.x/ImageEnter->Width,Size.y/ImageEnter->Height);

		}
		break;
	case 2:
		{
			this->Size = Size;
			Pic->SetSize(Size.x/Pic->Width,Size.y/Pic->Height);
			Image->SetSize(Size.x/Image->Width,Size.y/Image->Height);
		}
		break;
	}
}
void	BUTTON::SetSize(float SizeX,float SizeY)
{
	D3DXVECTOR2 Size;
	Size.x = SizeX;
	Size.y = SizeY;
	SetSize(Size);
}


	//Events	
void	BUTTON::ClickMouse(void)
{
	if(ClickFunc != NULL)
		this->ClickFunc();
}
void	BUTTON::EnterMouse(void) 
{
	if(EnterFunc != NULL)
		this->EnterFunc();
}

void	BUTTON::CALLBACKClick(void (*ClickFunc)(void))
{
	this->ClickFunc = ClickFunc;
}

void	BUTTON::CALLBACKEnter(void (*EnterFunc)(void))
{
	this->EnterFunc = EnterFunc;
}


BUTTON::~BUTTON(void)
{
}
