/*
 * UPIWIN - Micro Pi Windowing Framework Kernel
 * Copyright (C) 2019 Amy Bowersox/Erbosoft Metaverse Design Solutions
 *    
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *    
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *-------------------------------------------------------------------------
 */
#include <string.h>
#include "log.h"
#include "gfxtype.h"

BOOL G_set_rect(PRECT rect, int left, int top, int right, int bottom)
{
	rect->left = left;
	rect->top = top;
	rect->right = right;
	rect->bottom = bottom;
	return TRUE;
}

BOOL G_set_rect_empty(PRECT rect)
{
	memset(rect, 0, sizeof(RECT));
	return TRUE;
}

BOOL G_inflate_rect(PRECT rect, int dx, int dy)
{
	rect->left += dx;
	rect->top += dy;
	rect->right -= dx;
	rect->bottom -= dy;
	return TRUE;
}

BOOL G_offset_rect(PRECT rect, int dx, int dy)
{
	rect->left += dx;
	rect->top += dy;
	rect->right += dx;
	rect->bottom += dy;
	return TRUE;
}

BOOL G_is_rect_empty(PCRECT rect)
{
	return (rect->right <= rect->left) || (rect->bottom <= rect->top);
}

BOOL G_coords_in_rect(PCRECT rect, INT32 x, INT32 y)
{
	return (rect->left <= x) && (x < rect->right) && (rect->top <= y) && (y < rect->bottom);
}

BOOL G_point_in_rect(PCRECT rect, PCPOINT pt)
{
	return G_coords_in_rect(rect, pt->x, pt->y);
}

BOOL G_rect_equal(PCRECT rect1, PCRECT rect2)
{
	return (memcmp(rect1, rect2, sizeof(RECT)) == 0);
}

BOOL G_rect_intersect(PRECT dest, PCRECT src1, PCRECT src2)
{
	if (G_is_rect_empty(src1) || G_is_rect_empty(src2))
	{
		memset(dest, 0, sizeof(RECT));
		return FALSE;
	}
	if ((src1->left >= src2->right) || (src2->left >= src1->right) || (src1->top >= src2->bottom) || (src2->top >= src1->bottom))
	{
		memset(dest, 0, sizeof(RECT));
		return FALSE;
	}
	dest->left = MAX(src1->left, src2->left);
	dest->top = MAX(src1->top, src2->top);
	dest->right = MIN(src1->right, src2->right);
	dest->bottom = MIN(src1->bottom, src2->bottom);
	ASSERT(dest->left <= dest->right);
	ASSERT(dest->top <= dest->bottom);
	return TRUE;
}

BOOL G_rect_union(PRECT dest, PCRECT src1, PCRECT src2)
{
	if (G_is_rect_empty(src1))
	{
		memcpy(dest, src2, sizeof(RECT));
		return !G_is_rect_empty(src2);
	}
	else if (G_is_rect_empty(src2))
	{
		memcpy(dest, src1, sizeof(RECT));
		return TRUE;
	}
	dest->left = MIN(src1->left, src2->left);
	dest->top = MIN(src1->top, src2->top);
	dest->right = MAX(src1->right, src2->right);
	dest->bottom = MAX(src1->bottom, src2->bottom);
	return TRUE;
}
