//======================= Lunasa Prismriver's Cutin function ========================== 
//text : name of your spell card
//image : image used for the cutin
//X1,Y1 : coordinates for the start of your image
//X2,Y2 : coordinates for the end of your image
//bar : picture used for the bar. "PCB" for the snow one, "IN" for the butterfly one, you can enter your own picture path but it musts be 256*48
//type : style of the animation, "KOMACHI" for the left-right animation, "ALICE" for the up-down animation, "MIKO" for the diagonal animation and "LUNASA" for the same side animation
//Currently only "KOMACHI" works.

let SystemDirectory = "\system\"; //Change this line where you've put your function.

task CutIn01(type,text,image,X1,Y1,X2,Y2,bar){

	let Ypos = 968;
	let AlphaValueText = 0;
	let CSD = GetCurrentScriptDirectory();
	let fonte = CSD ~ SystemDirectory ~"Fonte.png";

	ImageDraw;	
	SubBarImage;

	while(Ypos != 48){
		SetFontColorNew(255,255,255,255,128,128);
		DrawDynamicTextNew(text ,fonte,48,Ypos,0,0.35,0.5,7,1,255,1);
		Ypos -=10;
		yield;
	}

	SetFontColorNew(255,255,255,255,128,128);
	DrawTextNew(text ,fonte,48,48,0,0.35,0.5,7,1,255);



	while(1){
		SetFontColorNew(255,255,255,255,255,255);
		DrawDynamicTextNew("Bonus" ,fonte,68,65,0,0.35,0.5,7,1,AlphaValueText,4);
			
		if(GetSpellCardBonusScore()!=0){
			
			SetFontColorNew(255,255,255,92,92,92);
			DrawDynamicTextNew(FormatNumberString2(GetSpellCardBonusScore(),10),fonte,106,68,0,0.36,0.36,7,1,AlphaValueText,4);
		}else{
			SetFontColorNew(255,255,255,255,255,255);
			DrawDynamicTextNew("Failed" ,fonte,106,65,0,0.35,0.5,7,1,AlphaValueText,4);
		}
		loop(4){yield;}
		AlphaValueText+=15;
	}

	yield;

	task ImageDraw{
		SubAlphaCutImage;
		SubCutImage;
		yield;
	}


	task SubBarImage { //Draw the bar behind the spell card name.

 		let objHalo = Obj_Create(OBJ_EFFECT);
		let HaloImage;
		

		if(bar == "IN"){
   			HaloImage = CSD ~ SystemDirectory ~ "BarIN.png";	
		}else if(bar == "PCB"){
   			HaloImage = CSD ~ SystemDirectory ~ "BarPCB.png";	
		}else{
			ToString(bar);
			HaloImage = bar;
		}

		LoadGraphic(HaloImage); 
 
	  	ObjEffect_SetLayer(objHalo,7);
	  	ObjEffect_SetRenderState(objHalo,ALPHA);
          	ObjEffect_SetTexture(objHalo, HaloImage);
          	ObjEffect_SetPrimitiveType(objHalo, PRIMITIVE_TRIANGLEFAN);
          	ObjEffect_CreateVertex(objHalo, 4);
	  	ObjEffect_SetAngle(objHalo, 0, 0, 180);

          	ObjEffect_SetVertexXY(objHalo, 0, -128, 24);
          	ObjEffect_SetVertexXY(objHalo, 1, 128, 24);
          	ObjEffect_SetVertexXY(objHalo, 2, 128, -24);
          	ObjEffect_SetVertexXY(objHalo, 3, -128, -24);

          	ObjEffect_SetVertexUV(objHalo, 0, 0, 0);
          	ObjEffect_SetVertexUV(objHalo, 1, 256, 0);
          	ObjEffect_SetVertexUV(objHalo, 2, 256, 48);
          	ObjEffect_SetVertexUV(objHalo, 3, 0, 48);

	  	ObjEffect_SetVertexColor(objHalo,0,255,255,255,255);	
	  	ObjEffect_SetVertexColor(objHalo,1,255,255,255,255);
	  	ObjEffect_SetVertexColor(objHalo,2,255,255,255,255);
	  	ObjEffect_SetVertexColor(objHalo,3,255,255,255,255);

	
	  	while(!Obj_BeDeleted(objHalo)){
			Obj_SetX(objHalo, 48+128);
        	  	Obj_SetY(objHalo, Ypos);
	  	  	yield;
		}
	
	}


    
	task SubAlphaCutImage { //Draw the blurry cutin

		let objHalo = Obj_Create(OBJ_EFFECT);
	  	let CutXpos = -500;
	  	let CutYpos = GetCenterY;
	  	let SpeedFactor = 24;
	  	let AlphaFactor = 2;
	  	let inhib1 = 0;
	  	let AlphaFading = 0;

   	  	let HaloImage = image;
	  	LoadGraphic(HaloImage); 
 
	  	ObjEffect_SetLayer(objHalo,6);
	  	ObjEffect_SetRenderState(objHalo,ALPHA);
          	ObjEffect_SetTexture(objHalo, HaloImage);
          	ObjEffect_SetPrimitiveType(objHalo, PRIMITIVE_TRIANGLEFAN);
          	ObjEffect_CreateVertex(objHalo, 4);
	  	ObjEffect_SetScale(objHalo,2,2);
	  	ObjEffect_SetAngle(objHalo, 0, 180, 180);

          	ObjEffect_SetVertexXY(objHalo, 0, -((X2-X1)/2), ((Y2-Y1)/2));
         	ObjEffect_SetVertexXY(objHalo, 1, ((X2-X1)/2), ((Y2-Y1)/2));
          	ObjEffect_SetVertexXY(objHalo, 2, ((X2-X1)/2), -((Y2-Y1)/2));
          	ObjEffect_SetVertexXY(objHalo, 3, -((X2-X1)/2), -((Y2-Y1)/2));

          	ObjEffect_SetVertexUV(objHalo, 0, X1, Y1);
          	ObjEffect_SetVertexUV(objHalo, 1, X2, Y1);
          	ObjEffect_SetVertexUV(objHalo, 2, X2, Y2);
          	ObjEffect_SetVertexUV(objHalo, 3, X1, Y2);

	  	ObjEffect_SetVertexColor(objHalo,0,128,255,255,255);	
	  	ObjEffect_SetVertexColor(objHalo,1,128,255,255,255);
	  	ObjEffect_SetVertexColor(objHalo,2,128,255,255,255);
	  	ObjEffect_SetVertexColor(objHalo,3,128,255,255,255);

	
	  	while(!Obj_BeDeleted(objHalo)){
		  	Obj_SetX(objHalo, CutXpos);
        	  	Obj_SetY(objHalo, CutYpos);

		  	if(Obj_GetX(objHalo)<=GetCenterX-100){
				CutXpos+=SpeedFactor;
				yield;
			}
		  	SpeedFactor-=0.207*2.27;
		  	yield;
		  	if(Obj_GetX(objHalo)>=GetCenterX-100){
				if(inhib1==0){
					loop(60){yield;};
					inhib1++;
				}
				if(inhib1==1){
					ObjEffect_SetScale(objHalo,AlphaFactor,AlphaFactor);
					ascent(i in 0..4){
						ObjEffect_SetVertexColor(objHalo,i,128-AlphaFading,255,255,255);	
					}
					AlphaFactor+=0.0666;
					AlphaFading+=3;
					if(AlphaFading>128){
						Obj_Delete(objHalo);
					}
				}
		  	}
		  
		}
	
	}


    
	task SubCutImage { //Draw the cutin portrait

 		let objHalo = Obj_Create(OBJ_EFFECT);
	  	let CutXpos = +500+384;
	  	let CutYpos = GetCenterY;
	  	let SpeedFactor = 24;
	  	let AlphaFactor = 1;
	  	let AlphaFading = 0;
	  	let inhib1 = 0;

   	  	let HaloImage = image;
	  	LoadGraphic(HaloImage); 
 
	  	ObjEffect_SetLayer(objHalo,6);
	  	ObjEffect_SetRenderState(objHalo,ALPHA);
          	ObjEffect_SetTexture(objHalo, HaloImage);
          	ObjEffect_SetPrimitiveType(objHalo, PRIMITIVE_TRIANGLEFAN);
          	ObjEffect_CreateVertex(objHalo, 4);
	  	ObjEffect_SetScale(objHalo,1,1);
	  	ObjEffect_SetAngle(objHalo, 0, 180, 180);

          	ObjEffect_SetVertexXY(objHalo, 0, -((X2-X1)/2), ((Y2-Y1)/2));
          	ObjEffect_SetVertexXY(objHalo, 1, ((X2-X1)/2), ((Y2-Y1)/2));
          	ObjEffect_SetVertexXY(objHalo, 2, ((X2-X1)/2), -((Y2-Y1)/2));
          	ObjEffect_SetVertexXY(objHalo, 3, -((X2-X1)/2), -((Y2-Y1)/2));

          	ObjEffect_SetVertexUV(objHalo, 0, X1, Y1);
          	ObjEffect_SetVertexUV(objHalo, 1, X2, Y1);
          	ObjEffect_SetVertexUV(objHalo, 2, X2, Y2);
          	ObjEffect_SetVertexUV(objHalo, 3, X1, Y2);

	  	ObjEffect_SetVertexColor(objHalo,0,255,255,255,255);	
	 	ObjEffect_SetVertexColor(objHalo,1,255,255,255,255);
	 	ObjEffect_SetVertexColor(objHalo,2,255,255,255,255);
	  	ObjEffect_SetVertexColor(objHalo,3,255,255,255,255);

	
	  	while(!Obj_BeDeleted(objHalo)){
		  	Obj_SetX(objHalo, CutXpos);
        	  	Obj_SetY(objHalo, CutYpos);

		  	if(Obj_GetX(objHalo)>=GetCenterX+37){
				CutXpos-=SpeedFactor;
				yield;
			}
		  	SpeedFactor-=0.207*2.275;
		  	yield;
		  	if(Obj_GetX(objHalo)<=GetCenterX+37){
				if(inhib1==0){
					loop(60){yield;};
					inhib1++;
				}
				if(inhib1==1){
					ObjEffect_SetScale(objHalo,AlphaFactor,AlphaFactor);
					ascent(i in 0..4){
						ObjEffect_SetVertexColor(objHalo,i,255-AlphaFading,255,255,255);	
					}
					AlphaFactor+=0.0666;
					AlphaFading+=3;
					if(AlphaFading>255){
						Obj_Delete(objHalo);
					}
				}
		 	}
		  
		}
	
	}	





	let newtextcolor=[255,255,255,255,255,255];

	function SetFontColorNew(r1,g1,b1,r2,g2,b2){
		newtextcolor=[r1,g1,b1,r2,g2,b2];
	}

	task DrawTextNew(string,font,x,y,angle,xsize,ysize,layer,align,alpha){ //Used for fix text. Thanks to Azure for this function.


		let obj = Obj_Create(OBJ_EFFECT);
		let ii=0;
		let midx=0;
		let miny=0;
		let string2=[];

		LoadGraphic(font);

		Obj_SetPosition(obj,x,y-8);
		Obj_SetSpeed      (obj, 0);
		ObjEffect_SetAngle(obj, 0,0,angle);
		ObjEffect_SetTexture(obj,font);
		ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLESTRIP);
		ObjEffect_SetLayer(obj,layer);
		ObjEffect_CreateVertex(obj,length(string)*6);
		ObjEffect_SetRenderState(obj,ALPHA);
		ObjEffect_SetScale(obj,xsize,ysize);

		let char=0;
		let CharSpace = 0;
		let LastCharSpace = 0;
		ascent(ii in 0..length(string)){
			char=ToNumber(string[ii])*32;
			midx+= LastCharSpace/2+CharSpace/2;
	
			ObjEffect_SetVertexXY(obj,(ii*6)  ,midx,0);
			ObjEffect_SetVertexXY(obj,(ii*6)+1,midx,0);
			ObjEffect_SetVertexXY(obj,(ii*6)+2,midx+32,0);
			ObjEffect_SetVertexXY(obj,(ii*6)+3,midx,32);
			ObjEffect_SetVertexXY(obj,(ii*6)+4,midx+32,32);
			ObjEffect_SetVertexXY(obj,(ii*6)+5,midx+32,32);
	
			ObjEffect_SetVertexUV(obj,(ii*6)  ,char,0);
			ObjEffect_SetVertexUV(obj,(ii*6)+1,char,0);
			ObjEffect_SetVertexUV(obj,(ii*6)+2,char+32,0);
			ObjEffect_SetVertexUV(obj,(ii*6)+3,char,32);
			ObjEffect_SetVertexUV(obj,(ii*6)+4,char+32,32);
			ObjEffect_SetVertexUV(obj,(ii*6)+5,char+32,32);

			ObjEffect_SetVertexColor(obj,(ii*6)  ,alpha,newtextcolor[0],newtextcolor[1],newtextcolor[2]);
			ObjEffect_SetVertexColor(obj,(ii*6)+1,alpha,newtextcolor[0],newtextcolor[1],newtextcolor[2]);
			ObjEffect_SetVertexColor(obj,(ii*6)+2,alpha,newtextcolor[0],newtextcolor[1],newtextcolor[2]);
			ObjEffect_SetVertexColor(obj,(ii*6)+3,alpha,newtextcolor[3],newtextcolor[4],newtextcolor[5]);
			ObjEffect_SetVertexColor(obj,(ii*6)+4,alpha,newtextcolor[3],newtextcolor[4],newtextcolor[5]);
			ObjEffect_SetVertexColor(obj,(ii*6)+5,alpha,newtextcolor[3],newtextcolor[4],newtextcolor[5]);
		
			LastCharSpace = CharSpace;
    	}
    	loop(2){yield;}

	function ToNumber(character){

	//ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,!?()'"-~


		let ret=0;
		alternative(character)
			case('A') {ret=0;CharSpace=20;} case('B') {ret=1;CharSpace=20;} case('C') {ret=2;CharSpace=20;} case('D') {ret=3;CharSpace=20;}
			case('E') {ret=4;CharSpace=20;} case('F') {ret=5;CharSpace=20;} case('G') {ret=6;CharSpace=20;} case('H') {ret=7;CharSpace=20;}
			case('I') {ret=8;CharSpace=14;} case('J') {ret=9;CharSpace=20;} case('K') {ret=10;CharSpace=20;} case('L') {ret=11;CharSpace=20;}
			case('M') {ret=12;CharSpace=20;} case('N') {ret=13;CharSpace=20;} case('O') {ret=14;CharSpace=20;} case('P') {ret=15;CharSpace=20;}
			case('Q') {ret=16;CharSpace=20;} case('R') {ret=17;CharSpace=20;} case('S') {ret=18;CharSpace=20;} case('T') {ret=19;CharSpace=16;}
			case('U') {ret=20;CharSpace=20;} case('V') {ret=21;CharSpace=20;} case('W') {ret=22;CharSpace=20;} case('X') {ret=23;CharSpace=20;}
			case('Y') {ret=24;CharSpace=20;} case('Z') {ret=25;CharSpace=20;}

			case('a') {ret=26;CharSpace=20;} case('b') {ret=27;CharSpace=20;} case('c') {ret=28;CharSpace=18;} case('d') {ret=29;CharSpace=20;}
			case('e') {ret=30;CharSpace=20;} case('f') {ret=31;CharSpace=14;} case('g') {ret=32;CharSpace=20;} case('h') {ret=33;CharSpace=20;}
			case('i') {ret=34;CharSpace=12;} case('j') {ret=35;CharSpace=14;} case('k') {ret=36;CharSpace=20;} case('l') {ret=37;CharSpace=12;}
			case('m') {ret=38;CharSpace=20;} case('n') {ret=39;CharSpace=20;} case('o') {ret=40;CharSpace=20;} case('p') {ret=41;CharSpace=20;}
			case('q') {ret=42;CharSpace=20;} case('r') {ret=43;CharSpace=20;} case('s') {ret=44;CharSpace=20;} case('t') {ret=45;CharSpace=12;}
			case('u') {ret=46;CharSpace=20;} case('v') {ret=47;CharSpace=20;} case('w') {ret=48;CharSpace=20;} case('x') {ret=49;CharSpace=20;}
			case('y') {ret=50;CharSpace=16;} case('z') {ret=51;CharSpace=20;}
			
			case('0') {ret=52;CharSpace=20;} case('1') {ret=53;CharSpace=16;} case('2') {ret=54;CharSpace=20;} case('3') {ret=55;CharSpace=24;}
			case('4') {ret=56;CharSpace=20;} case('5') {ret=57;CharSpace=20;} case('6') {ret=58;CharSpace=20;} case('7') {ret=59;CharSpace=20;}
			case('8') {ret=60;CharSpace=20;} case('9') {ret=61;CharSpace=20;} 

			case('.') {ret=62;CharSpace=10;} case(',') {ret=63;CharSpace=10;} case('!') {ret=64;CharSpace=12;} case('?') {ret=65;CharSpace=18;} 
			case('(') {ret=66;CharSpace=14;} case(')') {ret=67;CharSpace=14;} case('\') {ret=68;CharSpace=18;} case('/') {ret=69;CharSpace=14;} 
			case('-') {ret=70;CharSpace=24;} case('~') {ret=71;CharSpace=20;} case('+') {ret=72;CharSpace=24;}
			
			others {ret=-1;CharSpace=15;}

		
			return ret;
		}
	}


	task DrawDynamicTextNew(string,font,x,y,angle,xsize,ysize,layer,align,alpha,n){ //Used for moving or updateable text. Modified from Azure function.

		let obj = Obj_Create(OBJ_EFFECT);
		let ii=0;
		let midx=0;
		let miny=0;
		let string2=[];

		LoadGraphic(font);

		Obj_SetPosition(obj,x,y-8);
		Obj_SetSpeed      (obj, 0);
		ObjEffect_SetAngle(obj, 0,0,angle);
		ObjEffect_SetTexture(obj,font);
		ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLESTRIP);
		ObjEffect_SetLayer(obj,layer);
		ObjEffect_CreateVertex(obj,length(string)*6);
		ObjEffect_SetRenderState(obj,ALPHA);
		ObjEffect_SetScale(obj,xsize,ysize);

		let char=0;
		let CharSpace = 0;
		let LastCharSpace = 0;
		ascent(ii in 0..length(string)){
			char=ToNumber(string[ii])*32;
			midx+= LastCharSpace/2+CharSpace/2;
	
			ObjEffect_SetVertexXY(obj,(ii*6)  ,midx,0);
			ObjEffect_SetVertexXY(obj,(ii*6)+1,midx,0);
			ObjEffect_SetVertexXY(obj,(ii*6)+2,midx+32,0);
			ObjEffect_SetVertexXY(obj,(ii*6)+3,midx,32);
			ObjEffect_SetVertexXY(obj,(ii*6)+4,midx+32,32);
			ObjEffect_SetVertexXY(obj,(ii*6)+5,midx+32,32);
	
			ObjEffect_SetVertexUV(obj,(ii*6)  ,char,0);
			ObjEffect_SetVertexUV(obj,(ii*6)+1,char,0);
			ObjEffect_SetVertexUV(obj,(ii*6)+2,char+32,0);
			ObjEffect_SetVertexUV(obj,(ii*6)+3,char,32);
			ObjEffect_SetVertexUV(obj,(ii*6)+4,char+32,32);
			ObjEffect_SetVertexUV(obj,(ii*6)+5,char+32,32);

			ObjEffect_SetVertexColor(obj,(ii*6)  ,alpha,newtextcolor[0],newtextcolor[1],newtextcolor[2]);
			ObjEffect_SetVertexColor(obj,(ii*6)+1,alpha,newtextcolor[0],newtextcolor[1],newtextcolor[2]);
			ObjEffect_SetVertexColor(obj,(ii*6)+2,alpha,newtextcolor[0],newtextcolor[1],newtextcolor[2]);
			ObjEffect_SetVertexColor(obj,(ii*6)+3,alpha,newtextcolor[3],newtextcolor[4],newtextcolor[5]);
			ObjEffect_SetVertexColor(obj,(ii*6)+4,alpha,newtextcolor[3],newtextcolor[4],newtextcolor[5]);
			ObjEffect_SetVertexColor(obj,(ii*6)+5,alpha,newtextcolor[3],newtextcolor[4],newtextcolor[5]);
		
			LastCharSpace = CharSpace;
    		}
    		loop(n){yield};
    		Obj_Delete(obj);
	
	
    
	
	

	function ToNumber(character){

		//ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,!?()'"-~


		let ret=0;
		alternative(character)
			case('A') {ret=0;CharSpace=20;} case('B') {ret=1;CharSpace=20;} case('C') {ret=2;CharSpace=20;} case('D') {ret=3;CharSpace=20;}
			case('E') {ret=4;CharSpace=20;} case('F') {ret=5;CharSpace=20;} case('G') {ret=6;CharSpace=20;} case('H') {ret=7;CharSpace=20;}
			case('I') {ret=8;CharSpace=14;} case('J') {ret=9;CharSpace=20;} case('K') {ret=10;CharSpace=20;} case('L') {ret=11;CharSpace=20;}
			case('M') {ret=12;CharSpace=20;} case('N') {ret=13;CharSpace=20;} case('O') {ret=14;CharSpace=20;} case('P') {ret=15;CharSpace=20;}
			case('Q') {ret=16;CharSpace=20;} case('R') {ret=17;CharSpace=20;} case('S') {ret=18;CharSpace=20;} case('T') {ret=19;CharSpace=16;}
			case('U') {ret=20;CharSpace=20;} case('V') {ret=21;CharSpace=20;} case('W') {ret=22;CharSpace=20;} case('X') {ret=23;CharSpace=20;}
			case('Y') {ret=24;CharSpace=20;} case('Z') {ret=25;CharSpace=20;}

			case('a') {ret=26;CharSpace=20;} case('b') {ret=27;CharSpace=20;} case('c') {ret=28;CharSpace=18;} case('d') {ret=29;CharSpace=20;}
			case('e') {ret=30;CharSpace=20;} case('f') {ret=31;CharSpace=14;} case('g') {ret=32;CharSpace=20;} case('h') {ret=33;CharSpace=20;}
			case('i') {ret=34;CharSpace=12;} case('j') {ret=35;CharSpace=14;} case('k') {ret=36;CharSpace=20;} case('l') {ret=37;CharSpace=12;}
			case('m') {ret=38;CharSpace=20;} case('n') {ret=39;CharSpace=20;} case('o') {ret=40;CharSpace=20;} case('p') {ret=41;CharSpace=20;}
			case('q') {ret=42;CharSpace=20;} case('r') {ret=43;CharSpace=20;} case('s') {ret=44;CharSpace=20;} case('t') {ret=45;CharSpace=12;}
			case('u') {ret=46;CharSpace=20;} case('v') {ret=47;CharSpace=20;} case('w') {ret=48;CharSpace=20;} case('x') {ret=49;CharSpace=20;}
			case('y') {ret=50;CharSpace=16;} case('z') {ret=51;CharSpace=20;}
			
			case('0') {ret=52;CharSpace=20;} case('1') {ret=53;CharSpace=16;} case('2') {ret=54;CharSpace=20;} case('3') {ret=55;CharSpace=24;}
			case('4') {ret=56;CharSpace=20;} case('5') {ret=57;CharSpace=20;} case('6') {ret=58;CharSpace=20;} case('7') {ret=59;CharSpace=20;}
			case('8') {ret=60;CharSpace=20;} case('9') {ret=61;CharSpace=20;} 

			case('.') {ret=62;CharSpace=10;} case(',') {ret=63;CharSpace=10;} case('!') {ret=64;CharSpace=12;} case('?') {ret=65;CharSpace=18;} 
			case('(') {ret=66;CharSpace=14;} case(')') {ret=67;CharSpace=14;} case('\') {ret=68;CharSpace=18;} case('/') {ret=69;CharSpace=14;} 
			case('-') {ret=70;CharSpace=24;} case('~') {ret=71;CharSpace=20;} case('+') {ret=72;CharSpace=24;}
			
			others {ret=-1;CharSpace=15;}

		
			return ret;
		}
	}


	function FormatNumberString2(number, digits){ //Used to convert spell card's bonus into String. Thanks to Naut for this function.

    		let str = ToString(number);
    		loop(7){
        		str = erase(str, length(str)-1);	
    		}
    		while(length(str) < digits){
        		str = "0" ~ str; 	
    		}
    		while(length(str) > digits){
        		str = erase(str, length(str)-1); 	
    		}
   		return str;
		}
	}