/******************************************************************************************
	CRTEST PS   (c) 1999-2001 ISL

	cntrscreen.c:			Screen centering stuff
******************************************************************************************/


long	centrescn_currentx;
long	centrescn_currenty;

extern unsigned short menuclosing;
extern int menuwidth;
extern TextureType *blackbox;		


void centreScreenCycle()
{
	mapviewBasicMapCycle();

	if (((padData.digital[0]&PAD_UP)||(padData.analogYS[0]<-1024)) && centrescn_currenty>0)
	{
//		sfxPlay(globalFX,SFX_GENERIC_BLEEP_1);
		centrescn_currenty--;
	}
	if (((padData.digital[0]&PAD_DOWN)||(padData.analogYS[0]>1024)) && centrescn_currenty<30)
	{
//		sfxPlay(globalFX,SFX_GENERIC_BLEEP_1);
		centrescn_currenty++;
	}

	if (((padData.digital[0]&PAD_LEFT)||(padData.analogXS[0]<-1024)) && centrescn_currentx>-20)
	{
//		sfxPlay(globalFX,SFX_GENERIC_BLEEP_1);
		centrescn_currentx--;
	}
	if (((padData.digital[0]&PAD_RIGHT)||(padData.analogXS[0]>1024))&& centrescn_currentx<30)
	{
//		sfxPlay(globalFX,SFX_GENERIC_BLEEP_1);
		centrescn_currentx++;
	}

	displayPage[0].dispenv.screen.x = centrescn_currentx;
	displayPage[1].dispenv.screen.x = centrescn_currentx;
	displayPage[0].dispenv.screen.y = centrescn_currenty;
	displayPage[1].dispenv.screen.y = centrescn_currenty;

	if (padData.debounce[0]&PAD_CROSS)
	{
// Write current values into the options
		optionData.screen.offset_x = centrescn_currentx;
		optionData.screen.offset_y = centrescn_currenty;

//		sfxPlay(globalFX,SFX_GENERIC_BLEEP_3);
		draw_routine = optionsDraw;
		cycle_routine = optionsCycle;
		lastoptionstate=SCREENSETUP_MAIN;
		optionstate = OPTIONS_MAIN;
		menuclosing=1;
		menuwidth=400;

		return;
	}
	else if (padData.debounce[0]&PAD_TRIANGLE)
	{
//		sfxPlay(globalFX,SFX_GENERIC_BLEEP_3);

// Restore current values from the options
		displayPage[0].dispenv.screen.x = optionData.screen.offset_x;
		displayPage[1].dispenv.screen.x = optionData.screen.offset_x;
		displayPage[0].dispenv.screen.y = optionData.screen.offset_y;
		displayPage[1].dispenv.screen.y = optionData.screen.offset_y;
		draw_routine = optionsDraw;
		cycle_routine = optionsCycle;
		lastoptionstate=SCREENSETUP_MAIN;
		optionstate = OPTIONS_MAIN;
		menuclosing=1;
		menuwidth=400;
		return;
	}
}

#define fontPrintCentred(f,x,y,str,r,g,b) fontPrint((f),(x)-fontExtentW((f),(str))/2, (y),(str),(r),(g),(b))

void centreScreenDraw()
{
	//vertical bits of corners
	overlayFilledRectangle(   -220,-100,12,16, 0x0000ff);
	overlayFilledRectangle(   -220,  84,12,16, 0x0000ff);
	overlayFilledRectangle( 220-12,-100,12,16, 0x0000ff);
	overlayFilledRectangle( 220-12,  84,12,16, 0x0000ff);

	//horizontal bits of corners
	overlayFilledRectangle(   -220,-108,36,8, 0x0000ff);
	overlayFilledRectangle(   -220, 100,36,8, 0x0000ff);
	overlayFilledRectangle( 220-36,-108,36,8, 0x0000ff);
	overlayFilledRectangle( 220-36, 100,36,8, 0x0000ff);
	
	optionsDrawOnly();

	fontCentredMultiLine(font,  0,-68,400, TEXTSTR(TEXTSTR_OPTIONS_MENU_MOVESCREEN), 128,128,128);

	{
		char *str;
		int size;

		str = TEXTSTR(TEXTSTR_OPTIONS_CANCEL_ACCEPT);	//"@T Cancel     @X Accept"
		size = fontExtentW(font,str);
		fontPrint(font,-(size/2),110+(100-(menuwidth/4))/4,str,128,128,128);
		optionsAlphaRectangle(blackbox, (-(size)/2)-4,105+(100-(menuwidth/4))/4, (size)+8,18, 0, 0x003000);
	}
}


void centreScreenStart()
{
	centrescn_currentx = optionData.screen.offset_x;	//displayPage[0].dispenv.screen.x;
	centrescn_currenty = optionData.screen.offset_y;	//displayPage[0].dispenv.screen.y;

	draw_routine = centreScreenDraw;
	cycle_routine = centreScreenCycle;
}
