#include "lgi/common/Lgi.h"
#include "Tuner.h"

LTuner::LTuner() : ResObject(Res_Custom)
{
	Val = -1;
}

bool LTuner::OnLayout(LViewLayoutInfo &Inf)
{
	if (!Inf.Width.Min)
	{
		Inf.Width.Min = 750;
		Inf.Width.Max = 750;
	}
	else
	{
		Inf.Height.Min = 200;
		Inf.Height.Max = 200;
	}

	return true;
}

void LTuner::Value(int64 v)
{
	Val = v;
	Invalidate();
}

void LTuner::OnPaint(LSurface *pDC)
{
	LRect c = GetClient();
	int Cx = c.X()>>1;
	int Cy = c.Y()>>1;
	
	pDC->Colour(LColour::Black);
	pDC->Rectangle();
	pDC->Colour(LColour(150, 150, 150));
	pDC->HLine(c.x1, c.x2, Cy);
	pDC->VLine(c.x1 + Cx, c.y1, c.y2);
	
	if (Val >= 0)
	{
		int Center = 63;
		int Max = 127;
		int ValOff = (int)Val - Center;
		int Px = Cx + (ValOff * Cx / Center);
		pDC->Colour(LColour::White);
		pDC->Rectangle(c.x1 + Px - 1, c.y1 + 20, c.x1 + Px + 1, c.y2 - 20);
	}
}

class GTunerFactory : public LViewFactory
{
	LView *NewView
	(
		/// The name of the class to create
		const char *Class,
		/// The initial position of the view
		LRect *Pos,
		/// The initial text of the view
		const char *Text
	)
	{
		if (!_stricmp(Class, "LTuner"))
			return new LTuner;
		return NULL;
	}	
	
} TunerFactory;