#include "SquirrelButton.h"
#include "SquirrelClassEx.h"


void SquirrelButton::OnClick(Event* evt) {
	//必须是按下态才有点击判断  实际是判断悬停态
	if (this->MyState != 3)return;
	auto mouse_evt = dynamic_cast<MouseClickEvent*>(evt);
	if (mouse_evt->button == MouseButton::Left)
	{
		SquirrelClassEx::RunUpdateScript("ButtonUpdateCallBack", this->GetObjectID(), 10);
	}
	else if (mouse_evt->button == MouseButton::Right)
	{
		SquirrelClassEx::RunUpdateScript("ButtonUpdateCallBack", this->GetObjectID(), 11);
	}
	else if (mouse_evt->button == MouseButton::Middle)
	{
		SquirrelClassEx::RunUpdateScript("ButtonUpdateCallBack", this->GetObjectID(), 12);
	}
}

void SquirrelButton::OnHover() {
	//必须处于普通态时才会调用悬停态方法
	if (this->MyState == 0) {
		SetState(2);
		//this->_is_Hover = true;
	}
}

void SquirrelButton::OnOut() {
	if (this->MyState == 1)return;
	//如果移出鼠标的时候处于按下态 那么也需要在调用一次OnUp
	if (this->MyState == 3) {
		SetState(2);
		this->SetPositionY(this->GetPositionY() - 1);
	}
	SetState(0);
	//this->_is_Hover = false;
}

void SquirrelButton::OnDown(Event* evt) {
	if (this->MyState == 1)return;
	if (this->MyState == 2) {
		SetState(3);
		this->SetPositionY(this->GetPositionY() + 1);
	}
}

void SquirrelButton::OnUp(Event* evt) {
	if (this->MyState == 1)return;
	if (this->MyState == 3) {
		SetState(2);
		this->SetPositionY(this->GetPositionY() - 1);
	}
}

bool SquirrelButton::IsPress(const int Type)
{
	switch (Type)
	{
	case 0: {
		if (this->_is_Left_Press) {
			this->_is_Left_Press = false;
			return true;
		}
		else {
			return false;
		}
	}
		break;
	case 1: {
		if (this->_is_Right_Press) {
			this->_is_Right_Press = false;
			return true;
		}
		else {
			return false;
		}
	}
		  break;
	case 2: {
		if (this->_is_Middle_Press) {
			this->_is_Middle_Press = false;
			return true;
		}
		else {
			return false;
		}
	}
		  break;
	}
}

bool SquirrelButton::IsHover()
{
	return this->_is_Hover;
}

void SquirrelButton::SetState(const int state)
{
	this->MyState = state;
	SquirrelClassEx::RunUpdateScript("ButtonUpdateCallBack", this->GetObjectID(), state);
}

void SquirrelButton::Init()
{
	//必须先调用父类的Init 初始化方法才可以成功注册UI类
	this->UiFrameWork::Init();

	//监听器在父类的Init方法中已经装载 所以这里只需要新增监听信号的处理逻辑
	AddListener<MouseClickEvent>(Closure(this, &SquirrelButton::OnClick));
	AddListener<MouseDownEvent>(Closure(this, &SquirrelButton::OnDown));
	AddListener<MouseUpEvent>(Closure(this, &SquirrelButton::OnUp));

}

void SquirrelButton::OnUpdate(Duration dt) {
	//SquirrelClassEx::RunUpdateScript("ButtonUpdateCallBack", this->GetObjectID(), this->MyState);
}