require("ClassLink.lua");
require("LuaApp.lua");
require("LuaWidget.lua");
require("DProps.lua");
require("LuaCommonWidgets.lua");
require("Consts.lua");
require("QRand.lua");

function Color(r, g, b, a)
{
	if (a == nil)
		return {r=r, g=g, b=b, a=255};
	else
		return {r=r, g=g, b=b, a=a};
}

function IColor(r, g, b, a)
{
	if (a ~= nil)
		return (a*0x1000000)+(r*0x10000)+(g*0x100)+b;
	else
		return (0xFF000000)+(r*0x10000)+(g*0x100)+b;
}

function Rect(x, y, width, height)
{
	return {mX=x, mY=y, mWidth=width, mHeight=height};
}

function RectContains(theRect, x, y)
{
	return ((x >= theRect.mX) && (y >= theRect.mY) &&
		(x < theRect.mX + theRect.mWidth) && (y < theRect.mY + theRect.mHeight));
}

function RectUnpack(theRect)
{
	return theRect.mX, theRect.mY, theRect.mWidth, theRect.mHeight;
}

function RectIntersects(theRect1, theRect2)
{
	return !((theRect2.mX + theRect2.mWidth <= theRect1.mX) ||
			(theRect2.mY + theRect2.mHeight <= theRect1.mY) ||
			(theRect2.mX >= theRect1.mX + theRect1.mWidth) ||
			(theRect2.mY >= theRect1.mY + theRect1.mHeight));
}

function RectIntersection(theRect1, theRect2)
{
	local x1 = Math.max(theRect1.mX, theRect2.mX);
	local x2 = Math.min(theRect1.mX + theRect1.mWidth, theRect2.mX + theRect2.mWidth);
	local y1 = Math.max(theRect1.mY, theRect2.mY);
	local y2 = Math.min(theRect1.mY + theRect1.mHeight, theRect2.mY + theRect2.mHeight);
	if (x2 - x1 < 0 || y2 - y1 < 0)
		return Rect(0,0,0,0);
	else
		return Rect(x1, y1, x2 - x1, y2 - y1);
}

function Point(x, y) { return {mX = x, mY = y}; }
function Vector(x, y) { return {mX = x, mY = y}; }
function VecDotProd(vec1, vec2) { return vec1.mX*vec2.mX + vec1.mY*vec2.mY; }
function VecMagnitude(theVec) { return Math.sqrt(VecDotProd(theVec, theVec)); }
function VecNormalize(theVec) { local aMag = VecMagnitude(theVec); return VecScale(theVec, 1/aMag); }
function VecScale(theVec, theScale) { return { mX = theVec.mX*theScale, mY = theVec.mY*theScale }; }


function RandFloat()
{
	return (Rand()%100000000)/100000000.0;
}

function DupTable(theTable)
{
	local aNewTab = {};
	for aKey, aVal in theTable
	{
		aNewTab[aKey] = aVal;
	}
	return aNewTab;
}

function GetDirectoryFromPos(thePos)
{
	local aFileName = string.sub(thePos[0], 1);

	local aSlashPos = 0;
	for (i < string.len(aFileName))
		if (string.sub(aFileName, i, 1) == "\\")
			aSlashPos = i;
	
	return string.sub(aFileName, 0, aSlashPos);
}

function URLEncode(theString)
{
	local aHexChars = "0123456789ABCDEF";
	local aString = "";
	
	if (theString == nil) return aString;

	for (i < string.len(theString))
	{
		local aPattern = string.sub(theString,i,1);
		if (string.find(aPattern, "[%w]") == nil) aPattern = "%"..aPattern; // escape non-alphanumeric characters
		if (string.sub(theString,i,1) == " ")
			aString = aString.."+";
		else if (string.find("?&%+\r\n\t", aPattern) != nil)
		{
			aString = aString.."%";
			aString = aString..string.sub(aHexChars,bit.band(int(string.byte(theString,i)/16),0xF),1);
			aString = aString..string.sub(aHexChars,bit.band(int(string.byte(theString,i)),0xF),1);
		}			
		else
		{
			aString = aString..string.sub(theString,i,1);
		}
	}
	return aString;
}
