#include "Scrollbar.h"

Scrollbar::Scrollbar(int PosX, int PosY, int Height, rect* displayArea, int textHeight, int gutterWidth)
:DisplayArea(displayArea),TextHeight(textHeight),ThumbLock(false),GutterWidth(gutterWidth),ScrollAmount(0),ThumbArea(0)
{
	UpPic=LoadTex("assets\\graphics\\upbutton.tga");
	DownPic=LoadTex("assets\\graphics\\downbutton.tga");
	TopButtonArea=new rect;
	TopButtonArea->Top=PosY;
	TopButtonArea->Left=PosX;
	TopButtonArea->Bottom=PosY+GetImageHeight(UpPic);
	TopButtonArea->Right=PosX+GetImageWidth(UpPic);
	BottomButtonArea=new rect;
	BottomButtonArea->Top=PosY+Height-GetImageHeight(DownPic);
	BottomButtonArea->Left=PosX;
	BottomButtonArea->Bottom=PosY+Height;
	BottomButtonArea->Right=PosX+GetImageWidth(DownPic);
	ScrollBarArea=new rect;
	ScrollBarArea->Top=PosY;
	ScrollBarArea->Left=PosX;
	ScrollBarArea->Bottom=PosY+Height;
	ScrollBarArea->Right=TopButtonArea->Right;
	GutterArea=new rect;
	GutterArea->Top=TopButtonArea->Bottom;
	GutterArea->Left=(TopButtonArea->Right+PosX)/2-GutterWidth/2;
	GutterArea->Bottom=BottomButtonArea->Top;
	GutterArea->Right=GutterArea->Left+GutterWidth;
	ReCalc(textHeight);
}

Scrollbar::~Scrollbar()
{
	gos_DestroyTexture(UpPic);
	gos_DestroyTexture(DownPic);
	delete TopButtonArea;
	delete BottomButtonArea;
	delete GutterArea;
	if(ThumbArea)
		delete ThumbArea;
}

bool Scrollbar::MouseDown()
{
	return ThumbLock;
}

rect* Scrollbar::GetSBArea()
{
	return ScrollBarArea;
}

int Scrollbar::Mouse(bool LMButtonDown, int x, int y)
{
	if(!Enabled)
		return 0;
	if(ThumbLock)
	{
		if(LMButtonDown)
		{
			ThumbPos+=y-LastMouseY;
			LastMouseY=y;
			if(ThumbPos<TopButtonArea->Bottom)
				ThumbPos=TopButtonArea->Bottom;
			else
				if(ThumbPos+ThumbHeight>BottomButtonArea->Top)
					ThumbPos=BottomButtonArea->Top-ThumbHeight;
			ScrollAmount=ScrollAmountFromThumb();
			return ScrollAmount;
		}
		else
		{
			ThumbLock=false;
			return ScrollAmount;
		}
	}
	else
	{
		if(LMButtonDown)
			if(WithinArea(x,y,ScrollBarArea))
				if(WithinArea(x,y,TopButtonArea))
				{
					ScrollAmount-=6;
					if(ScrollAmount<0)
						ScrollAmount=0;
					ThumbPos=ThumbFromScrollAmount();
				}
				else
					if(WithinArea(x,y,BottomButtonArea))
					{
						ScrollAmount+=6;
						if(ScrollAmount>TextHeight-DisplayArea->Bottom+DisplayArea->Top)
							ScrollAmount=TextHeight-DisplayArea->Bottom+DisplayArea->Top;
						ThumbPos=ThumbFromScrollAmount();
					}
					else
						if(WithinArea(x,y,ThumbArea))
						{
							ThumbLock=true;
							LastMouseY=y;
						}
		return ScrollAmount;
	}
}

void Scrollbar::ReCalc(int textHeight)
{
	if(textHeight!=TextHeight-ScrollAmount)
	{
		ScrollAmount=0;
		TextHeight=textHeight;
		ThumbPos=TopButtonArea->Bottom;
	}
	if(DisplayArea->Bottom-DisplayArea->Top >= TextHeight)
	{
		Enabled=false;
		return;
	}
	Enabled=true;
	ThumbHeight=((DisplayArea->Bottom-DisplayArea->Top)*(BottomButtonArea->Top-TopButtonArea->Bottom))/TextHeight;
	if(!ThumbArea)
		ThumbArea=new rect;
	ThumbArea->Top=ThumbPos;
	ThumbArea->Left=TopButtonArea->Left;
	ThumbArea->Bottom=ThumbArea->Top+ThumbHeight;
	ThumbArea->Right=TopButtonArea->Right;
}

bool Scrollbar::IsEnabled()
{
	return Enabled;
}

void Scrollbar::Draw()
{
	if(!Enabled)
		return;
	DrawImage(UpPic,TopButtonArea->Left,TopButtonArea->Top);
	DrawImage(DownPic,BottomButtonArea->Left,BottomButtonArea->Top);
	DrawF(GutterArea,0x77777777);	//gutter
	DrawF(ThumbArea,0xFFFFFFCC);	//thumb
}

int Scrollbar::ScrollAmountFromThumb()
{
	return ((ThumbPos-TopButtonArea->Bottom)*TextHeight)/(BottomButtonArea->Top-TopButtonArea->Bottom);
}

int Scrollbar::ThumbFromScrollAmount()
{
	return ((ScrollAmount*(BottomButtonArea->Top-TopButtonArea->Bottom))/TextHeight)+TopButtonArea->Bottom;
}