#include "WimpLib:Coords.h"
#include "WimpLib:Task.h"

#include <stdlib.h>

int XToScreen(int x, const CWindCvt* pcvt)
{
	return x + pcvt->box.x0 - pcvt->s.cx;
}

int YToScreen(int y, const CWindCvt* pcvt)
{
	return y + pcvt->box.y1 - pcvt->s.cy;
}

CPoint PointToScreen(const CPoint* ppt, const CWindCvt* pcvt)
{
	CPoint npt;

	npt.x = ppt->x + pcvt->box.x0 - pcvt->s.cx;
	npt.y = ppt->y + pcvt->box.y1 - pcvt->s.cy;

	return npt;
}

CRect RectToScreen(const CRect* prct, const CWindCvt* pcvt)
{
	CRect nrct;

	nrct.x0 = prct->x0 + pcvt->box.x0 - pcvt->s.cx;
	nrct.y0 = prct->y0 + pcvt->box.y1 - pcvt->s.cy;
	nrct.x1 = prct->x1 + pcvt->box.x0 - pcvt->s.cx;
	nrct.y1 = prct->y1 + pcvt->box.y1 - pcvt->s.cy;

	return nrct;
}

int XToWindow(int x, const CWindCvt* pcvt)
{
	return x - pcvt->box.x0 + pcvt->s.cx;
}

int YToWindow(int y, const CWindCvt* pcvt)
{
	return y - pcvt->box.y1 + pcvt->s.cy;
}

CPoint PointToWindow(const CPoint* ppt, const CWindCvt* pcvt)
{
	CPoint npt;

	npt.x = ppt->x - pcvt->box.x0 + pcvt->s.cx;
	npt.y = ppt->y - pcvt->box.y1 + pcvt->s.cy;

	return npt;
}

CRect RectToWindow(const CRect* prct, const CWindCvt* pcvt)
{
	CRect nrct;

	nrct.x0 = prct->x0 - pcvt->box.x0 + pcvt->s.cx;
	nrct.y0 = prct->y0 - pcvt->box.y1 + pcvt->s.cy;
	nrct.x1 = prct->x1 - pcvt->box.x0 + pcvt->s.cx;
	nrct.y1 = prct->y1 - pcvt->box.y1 + pcvt->s.cy;

	return nrct;
}

void RoundX(int* x)
{
	const Mode_Info* Mode = Task_GetModeInfo();

	*x = (*x >> Mode->shiftx) << Mode->shiftx;
}

void RoundY(int* y)
{
	const Mode_Info* Mode = Task_GetModeInfo();

	*y = (*y >> Mode->shifty) << Mode->shifty;
}

void RoundPoint(CPoint* ppt)
{
	const Mode_Info* Mode = Task_GetModeInfo();

	ppt->x = (ppt->x >> Mode->shiftx) << Mode->shiftx;
	ppt->y = (ppt->y >> Mode->shifty) << Mode->shifty;
}

void RoundUpSize(CSize* psize)
{
	const Mode_Info* Mode = Task_GetModeInfo();
	int x, y;

	x = (psize->cx >> Mode->shiftx) << Mode->shiftx;
	if (psize->cx != x) psize->cx = x + Mode->dx;
	y = (psize->cy >> Mode->shifty) << Mode->shifty;
	if (psize->cy != y) psize->cy = y + Mode->dy;
}

void RoundRect(CRect* prct)
{
	const Mode_Info* Mode = Task_GetModeInfo();

	prct->x0 = (prct->x0 >> Mode->shiftx) << Mode->shiftx;
	prct->y0 = (prct->y0 >> Mode->shifty) << Mode->shifty;
	prct->x1 = (prct->x1 >> Mode->shiftx) << Mode->shiftx;
	prct->y1 = (prct->y1 >> Mode->shifty) << Mode->shifty;
}

void RoundUpRect(CRect* prct)
{
	const Mode_Info* Mode = Task_GetModeInfo();
	int x, y;

	prct->x0 = (prct->x0 >> Mode->shiftx) << Mode->shiftx;
	prct->y0 = (prct->y0 >> Mode->shifty) << Mode->shifty;
	x = (prct->x1 >> Mode->shiftx) << Mode->shiftx;
	if (prct->x1 != x) prct->x1 = x + Mode->dx;
	y = (prct->y1 >> Mode->shifty) << Mode->shifty;
	if (prct->y1 != y) prct->y1 = y + Mode->dy;
}

void RoundRectForPlot(CRect* prct)
{
	const Mode_Info* Mode = Task_GetModeInfo();

	prct->x1 -= Mode->dx;
	prct->y1 -= Mode->dy;
}

bool IsPointInWimpRect(const CPoint* ppt, const CRect* prct)
{
	if ((ppt->x <  prct->x0)
	||  (ppt->x >= prct->x1)
	||  (ppt->y <  prct->y0)
	||  (ppt->y >= prct->y1))
		return false;

	return true;
}
