
#include "DrawRect.h"

BOOL drw::DrawRectangle::setRange(POINT rangeLeft, POINT rangeRight)
{
	if (_isSet(rangeLeft, rangeRight))
	{
		_rangeLeft = rangeLeft;
		_rangeRight = rangeRight;
		return true;
	}
	else
	{
		return false;
	}
}

void drw::DrawRectangle::getRange(POINT& rangeLeft, POINT& rangeRight)
{
	rangeLeft = _rangeLeft;
	rangeRight = _rangeRight;
}

BOOL drw::DrawRectangle::setWidth(INT16 width)
{
	if (width >= 0)
	{
		_width = width;
		return true;
	}
	else
	{
		return false;
	}
}

BOOL drw::DrawRectangle::Draw(HDC hdc)
{
	if (hdc)
	{
		if (_isSet(_left, _right))
		{
			HPEN Pen = CreatePen(PS_SOLID, _width, _color);
			RECT rectWindow;
			GetClientRect(hWnd, &rectWindow);

			

			SelectObject(hdc, Pen);
			SelectObject(hdc, GetStockObject(NULL_BRUSH));

			Rectangle(hdc, _left.x, _left.y, _right.x, _right.y);

			if (Pen) DeleteObject(Pen);

			return true;
		}
	}
	return false;
}

void drw::DrawRectangle::UpdateRectangle()
{
	POINT p;
	GetCursorPos(&p);
	ScreenToClient(hWnd, &p);

	setRight(p);
	InvalidateRect(hWnd, &_validateRect, true);
}

void drw::DrawRectangle::ValidateRect()
{
	InvalidateRect(hWnd, &_validateRect, false);
}

void drw::DrawRectangle::clearRect()
{
	_left = { 0,0 };
	_right = { 0,0 };
	_isDrawn = false;
}


void drw::DrawRectangle::enablePainting()
{
	_createValidateRect();
	_painting = true;
}

void drw::DrawRectangle::disablePainting()
{
	_isSet(_left, _right);
	_painting = false;
	_isDrawn = true;
}

BOOL drw::DrawRectangle::_isSet(POINT& left, POINT& right)
{
	if (left.x >= 0 && left.y >= 0)
	{
		if (right.x >= 0 && right.y >= 0)
		{
			return true;
		}
	}
	return false;
}

BOOL drw::DrawRectangle::_createValidateRect()
{
	if (_isSet(_rangeLeft, _rangeRight))
	{
		_validateRect = { _rangeLeft.x,_rangeLeft.y,_rangeRight.x,_rangeRight.y };
		return true;
	}
	else
	{
		return false;
	}
}