#include "lgi/common/Lgi.h"
#include "lgi/common/Path.h"
#include "EncoderDisplay.h"

DeclFactory(LEncoderDisplay)
#define ENC_PX	41

LEncoderDisplay::LEncoderDisplay() : ResObject(Res_Custom)
{
	Val = 0;
}

bool LEncoderDisplay::OnLayout(LViewLayoutInfo &Inf)
{
	static int Px = ENC_PX;
	if (!Inf.Width.Min)
	{
		Inf.Width.Min = Px;
		Inf.Width.Max = Px;
	}
	else
	{
		Inf.Height.Min = Px;
		Inf.Height.Max = Px;
	}

	return true;
}

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

void LEncoderDisplay::OnPaint(LSurface *pDC)
{
	LColour Back = StyleColour(LCss::PropBackgroundColor, LColour(L_MED));
	LRect c = GetClient();
	if (Back)
	{
		pDC->Colour(Back);
		pDC->Rectangle();
	}
	
	pDC->Colour(LColour::White);

	c.SetSize(ENC_PX, ENC_PX);
	int Cx = c.x1 + (c.X() >> 1);
	int Cy = c.y1 + (c.Y() >> 1);
	pDC->Ellipse(Cx, Cy, Cx, Cy);
	
	if (Val >= 0)
	{
		double BaseOffset = 0.75 * LGI_PI;
		LPointF p(c.X() * 0.45, 0);
		p.Rotate((double)Val * LGI_PI * 1.5 / 127 + BaseOffset);
		pDC->Line(Cx, Cy, Cx + (int)p.x, Cy + (int)p.y);
	}
}
