#include "wcolwidget.h"
#include "wcolgradgen.h"
#include "gui_defines.h"


WColWidget::WColWidget()
	:Widget(),
	image(0),
	selectedAttractor(-1)
{
}


void WColWidget::Draw(Bitmap &canvas) {
	if (cache) {
		cache.Blit(canvas, 0, 0, 0, 0, w(), h());

		if (selectedAttractor >= 0 && selectedAttractor < image->GetAttractorCount()) {
			WColImage::Attractor a = image->GetAttractor(selectedAttractor);
			int x = (int)(a.x * w());
			int y = (int)(a.y * h());
			int r = (int)(a.range * (w() + h())/2);

			if (a.type == CA_GLOBAL) {
				x = w()/2;
				y = h()/2;
				r = (w() + h())/8;
			}
			else if (a.type == CA_VBAR) {
				r = (int)(a.range*w());
			}
			else if (a.type == CA_HBAR) {
				r = (int)(a.range*h());
			}

			int col = makecol(a.r, a.g, a.b);

			switch (a.type) {
				case CA_GLOBAL:
					rectfill(canvas, x-4, y-4, x+3, y+3, col);
					rect(canvas, x-4, y-4, x+3, y+3, Color::black);
					rect(canvas, x-r, y-r, x+r-1, y+r-1, Color::green);
				break;

				case CA_POINT:
					circlefill(canvas, x, y, 4, col);
					circle(canvas, x, y, 4, Color::black);
					circle(canvas, x, y, r, Color::green);
				break;

				case CA_HBAR:
					rectfill(canvas, x-16, y-4, x+15, y+3, col);
					rect(canvas, x-16, y-4, x+15, y+3, Color::black);
					rect(canvas, x-3*r, y-r, x+3*r-1, y+r-1, Color::green);
				break;

				case CA_VBAR:
					rectfill(canvas, x-4, y-16, x+3, y+15, col);
					rect(canvas, x-4, y-16, x+3, y+15, Color::black);
					rect(canvas, x-r, y-3*r, x+r-1, y+3*r-1, Color::green);
				break;
			};
		}
	}
	else {
		canvas.Clear(Color::black);
	}
}


void WColWidget::OnChanged() {
	if (!cache || cache.w() != w() || cache.h() != h()) {
		cache.Create(w(), h());
	}

	if (image && !TestFlag(D_PRESSED)) {
		image->Render(cache);
	}

	Redraw();
}


void WColWidget::MsgLPress() {
	Widget::MsgLPress();
	SetFlag(D_PRESSED);
}


void WColWidget::MsgRPress() {
	Widget::MsgRPress();
	SetFlag(D_PRESSED);
}


void WColWidget::MsgLRelease() {
	Widget::MsgLRelease();
	if (TestFlag(D_PRESSED)) {
		ClearFlag(D_PRESSED);
		if (selectedAttractor >= 0 && selectedAttractor < image->GetAttractorCount()) {
			Point mp = GetMousePos();
			GetParent()->HandleEvent(*this, WCOL_MSG_PLACE_ATTRACTOR, mp.x(), mp.y());
		}
	}
}


void WColWidget::MsgRRelease() {
	Widget::MsgRRelease();
	if (TestFlag(D_PRESSED)) {
		ClearFlag(D_PRESSED);
		if (selectedAttractor >= 0 && selectedAttractor < image->GetAttractorCount()) {
			Point mp = GetMousePos();
			GetParent()->HandleEvent(*this, WCOL_MSG_SET_ATTRACTOR_RANGE, mp.x(), mp.y());
		}
	}
}


void WColWidget::MsgMousemove(const Point &d) {
	Widget::MsgMousemove(d);

	if (TestFlag(D_PRESSED)) {

		Point mp = GetMousePos();
		if (mouse_b & 1) {
			GetParent()->HandleEvent(*this, WCOL_MSG_PLACE_ATTRACTOR, mp.x(), mp.y());
		}
		else if (mouse_b & 2) {
			GetParent()->HandleEvent(*this, WCOL_MSG_SET_ATTRACTOR_RANGE, mp.x(), mp.y());
		}
	}
}


bool WColWidget::MsgWantmouse() {
	Widget::MsgWantmouse();
	return true;
}


bool WColWidget::MsgWantfocus() {
	Widget::MsgWantfocus();
	return true;
}
