let textimg=GetCurrentScriptDirectory~"lib\ascii.png";
LoadGraphic(textimg);

function CreateObjEffect(texture, layer)
{
	let obj = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(obj, texture);
	ObjEffect_SetLayer(obj, layer);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_CreateVertex(obj, 4);
	return obj;
}

function ObjEffect_SetXYRect(id, left, top, right, bottom)
{
	ObjEffect_SetVertexXY(id, 0, left, top);
	ObjEffect_SetVertexXY(id, 1, right, top);
	ObjEffect_SetVertexXY(id, 2, left, bottom);
	ObjEffect_SetVertexXY(id, 3, right, bottom);
}

function ObjEffect_SetUVRect(id, left, top, right, bottom)
{
	ObjEffect_SetVertexUV(id, 0, left, top);
	ObjEffect_SetVertexUV(id, 1, right, top);
	ObjEffect_SetVertexUV(id, 2, left, bottom);
	ObjEffect_SetVertexUV(id, 3, right, bottom);
}

function ObjEffect_SetColor(id, vertexes, a, r, g, b)
{
	ascent(i in 0..vertexes){ObjEffect_SetVertexColor(id, i, a, r, g, b);}
}

task DrawDialogueText(string, left, top, right, r, g, b)
{
	let stringarray=string;
	let drawnarray=[];
	let xdist=right-left;
	let curdistx=0;
	let curdisty=0;
	let curdistxb=0;
	ascent(i in 0..length(stringarray))
	{
	drawnarray=drawnarray~[CreateObjEffect(textimg, 8)];
	}
	ascent(i in 0..length(drawnarray))
	{
	SetLetterUV(drawnarray[i], stringarray[i]);
	ObjEffect_SetColor(drawnarray[i], 4, 255, r, g, b);
		if(curdistx+14<xdist)
		{
		ObjEffect_SetXYRect(drawnarray[i], left+curdistx, top+curdisty, left+curdistx+14, top+curdisty+14);
		//curdistx+=14;
		}else{
		ObjEffect_SetXYRect(drawnarray[i], left+curdistx, top+curdisty, left+curdistx+14, top+curdisty+14);
		curdistx=4;
		curdisty+=14;
		}
		curdistx+=curdistxb;
		curdistxb=0;
	}

function SetLetterUV(obj, character)
{
	let characterarray=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e',
		'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '.', '!', '?', ',', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' '];
	ascent(i in 0..length(characterarray))
	{
	if(character==characterarray[i])
	{
	if(i<=2){ObjEffect_SetUVRect(obj, (15+i)*14, 14, (16+i)*14, 28);}
	if(i>=3 && i<=20){ObjEffect_SetUVRect(obj, (i-3)*14, 28, (i-2)*14, 42);}
	if(i>=21 && i<=25){ObjEffect_SetUVRect(obj, (i-21)*14, 42, (i-20)*14, 56);}
	if(i>=26 && i<=32){ObjEffect_SetUVRect(obj, (i-15)*14, 42, (i-14)*14, 56);}
	if(i>=33 && i<=50){ObjEffect_SetUVRect(obj, (i-33)*14, 56, (i-32)*14, 70);}
	if(i==51){ObjEffect_SetUVRect(obj, (i-51)*14, 70, (i-50)*14, 84);}
	if(i==52){ObjEffect_SetUVRect(obj, (i-38)*14, 0, (i-37)*14, 14);}
	if(i==53){ObjEffect_SetUVRect(obj, (i-52)*14, 0, (i-51)*14, 14);}
	if(i==54){ObjEffect_SetUVRect(obj, (i-41)*14, 14, (i-40)*14, 28);}
	if(i==55){ObjEffect_SetUVRect(obj, (i-43)*14, 0, (i-42)*14, 14);}
	if(i==56 || i==57){ObjEffect_SetUVRect(obj, (i-40)*14, 0, (i-39)*14, 14);}
	if(i>=58 && i <=65){ObjEffect_SetUVRect(obj, (i-58)*14, 14, (i-57)*14, 28);}
	if(i<=11 || (i>=13 && i<=21) || (i>=23 && i<=25)){curdistx+=10;}
	if(i==8){curdistxb-=2;}
	if(i==12 || i==22){curdistx+=12; curdistxb+=2;}
	if( (i>=26 && i<=33) || i==36 || (i>=38 && i<=44) || (i>=46 && i<=51)){curdistx+=9; curdistxb-=1;}
	if(i==34 || i==35 || i==37 || i==45){curdistx+=9; curdistxb-=2;}
	if(i==38 || i==48){curdistx+=2; curdistxb+=2;}
	if(i==52 || i==55){curdistx+=12; curdistxb-=8;}
	if(i==53){curdistx+=8; curdistxb-=2;}
	if(i==54 || (i>=56 && i<=65) ){curdistx+=10; curdistxb-=1;}
	if(i==66){curdistx+=5;}
	}
	}
}
	while(Obj_BeDeleted(drawnarray[0])==false)
	{
	yield;
	if(GetKeyState(VK_SHOT)==KEY_PUSH || skipcount>=9)
	{
	ascent(i in 0..length(drawnarray)){Obj_Delete(drawnarray[i]);}
	}
	}

}