#include "std.mi"

/*********************************************************************************************
  自作関数宣言
*********************************************************************************************/
Function updatedisplay();
Function fn_ReSizeGroup(Group g, int TargetW, int TargetH, Float SpeedW, Float SpeedH, int TargetA);
Function fn_MoveObjText(Text t,int TargetW,int TargetH,int TargetA,Float Speed);

/*********************************************************************************************
  グローバル関数宣言
*********************************************************************************************/
Global Group	grpMain;
Global Group	grpTitle;
Global Group	grpTime;
Global Text	titleT; 
Global Text	timeT;
Global Text	timeTdammy;
Global Timer	TickerTimer;
Global int	intTitleW;
Global int	intTimerW;
Global int	intTimeTdammyW;
Global int	intTimeFlag;
//Global vis	visSize;

/*********************************************************************************************
  スクリプト読み込み時の設定
*********************************************************************************************/
System.onScriptLoaded() {

	grpMain		= getScriptGroup();
	grpTitle	= grpMain.getObject("Title_Bg.Group");
	grpTime		= grpMain.getObject("Time_Bg.Group");
	titleT		= grpTitle.getObject("songname");
	timeT		= grpTime.getObject("SongTime");
	timeTdammy	= grpTime.getObject("timeTdammy");
//	visSize		= grpMain.getObject("Visualzation");

//	intTimeFlag	= System.getPrivateInt("FreePositionImage", "TimeFlagValu", 0);

	TickerTimer = new Timer;
	TickerTimer.setDelay(50);
	TickerTimer.start();

	intTimeFlag = 0;
}

/*********************************************************************************************
  終了時の設定＆保存
*********************************************************************************************/
System.onScriptUnloading() {

//	System.setPrivateInt("FreePositionImage", "TimeFlagValu", intTimeFlag);

	TickerTimer.stop();
	delete TickerTimer;
}
/*********************************************************************************************
  TickerTimerの時間になったとき
*********************************************************************************************/
TickerTimer.onTimer() {

  UpdateDisplay();
}

/*********************************************************************************************
  曲名変更時
*********************************************************************************************/
System.onTitlechange(String newTitle) {

  UpdateDisplay();
}

/*********************************************************************************************
  自作関数
*********************************************************************************************/
updatedisplay(){

	string pre_title	= System.getPlayItemDisplayTitle();

	titleT.setText(pre_title);

	intTitleW = titleT.getTextWidth();

	if(intTitleW <= 400) {
		fn_ReSizeGroup(grpTitle, intTitleW +15, 23, 0.1, 0.1, 255);
		fn_MoveObjText(titleT, intTitleW, 23, 255, 0.1);
//		visSize.setXMLParam("w", "intTitleW +10");
	}
	else if(intTitleW > 400) {
		fn_ReSizeGroup(grpTitle, 418, 23, 0.1, 0.1, 255);
		fn_MoveObjText(titleT, 385, 23, 255, 0.1);
//		visSize.setXMLParam("w", "intTitleW");
	}

	if(intTimeFlag == 0) {
		timeT.setText(System.integerToTime(System.getPosition()) + " / " + System.integerToTime(System.getPlayItemLength()));
		timeTdammy.setText(System.integerToTime(System.getPlayItemLength()));
		intTimerW = timeT.getTextWidth();
		inttimeTdammyW = timeTdammy.getTextWidth();
		fn_ReSizeGroup(grpTime, intTimerW + 35 + inttimeTdammyW, 23, 0.1, 0.1, 255);
		fn_MoveObjText(timeT, intTimerW + 35 + inttimeTdammyW, 23, 255, 0.1);
	}
	else if(intTimeFlag == 1) {
		timeT.setText("- " + System.integerToTime(System.getPlayItemLength() - System.getPosition()) + " / " + System.integerToTime(System.getPlayItemLength()));
		timeTdammy.setText(System.integerToTime(System.getPlayItemLength()));
		intTimerW = timeT.getTextWidth();
		inttimeTdammyW = timeTdammy.getTextWidth();
		fn_ReSizeGroup(grpTime, intTimerW + 35 + inttimeTdammyW, 23, 0.1, 0.1, 255);
		fn_MoveObjText(timeT, intTimerW + 35 + inttimeTdammyW, 23, 255, 0.1);
	}
}

timeT.onLeftButtonUP(int x,int y) {

	if(intTimeFlag == 1) intTimeFlag = 0;
	else if(intTimeFlag == 0) intTimeFlag = 1;
}

fn_ReSizeGroup(Group g, int TargetW, int TargetH, Float SpeedW, Float SpeedH, int TargetA){

        g.setTargetW(TargetW);
        g.setTargetH(TargetH);
        g.setTargetSpeed(SpeedW);
        g.setTargetSpeed(SpeedH);
        g.setTargetA(TargetA);
        g.gotoTarget();
}

fn_MoveObjText(Text t,int TargetW,int TargetH,int TargetA,Float Speed){

        t.setTargetW(TargetW);
        t.setTargetH(TargetH);
        t.setTargetA(TargetA);
        t.setTargetSpeed(Speed);
        t.gotoTarget();
}