
#include <all.inc>

// 各チャンネルで使用する場合, 以下をincludeする必要あり
//#include "../common/bn_defs.inc"
//#include "../parts/focus.inc"
//#include "../parts/listsys.inc"

// スクリプトは以下をロードしておく
// <SCRIPT id="window" src="parts/window.amx" type="binary/small"/>
// <SCRIPT id="general_menu" src="parts/general_menu.amx" type="binary/small"/>
// <SCRIPT id="label_item" src="parts/label_item.amx" type="binary/small"/>

// メニューの文言は xml にて以下のように宣言
// <TEXT id="navimenu" group="menu_title" value="navigator menu" subgroup="navimenu_item" />
// <TEXT id="navimenu_item0" group="navimenu_item" value="About Broadband Navigator" />

const float:title_r = MENU_TITLE_COL_R ;//200/200/200
const float:title_g = MENU_TITLE_COL_G ;
const float:title_b = MENU_TITLE_COL_B ;

const float:item_r = MENU_ITEM_COL_R ;//60/150/230
const float:item_g = MENU_ITEM_COL_G ;
const float:item_b = MENU_ITEM_COL_B ;

enum{
	NAVIMENU_STAT_NONE = 0,
		//STDMENU_STAT_RETURNTOP,
};


public obj:focus_navimenu; // = CreateFocus( "focus" );


public navi_menu_core()
{
	//obj:parent, item_num, const title_str[], const group_str[] )

	new title_str[64];
	new group_str[64];
	new item_num;
	new tmp[64];

	GetArg( "num", tmp );
	item_num = atoi( tmp );
	GetArg( "title", title_str );
	GetArg( "group", group_str );

	new obj:self = Self();
	new float:vp_w;
	new float:vp_h;
	bn_get_viewport( vp_w, vp_h );

	new param_a;
	WaitMessage( TAG("INIT"), param_a );
	new obj:parent = obj:param_a; // 親オブジェ取得

	new obj:popmenu = CreateNull( self );
	{
		SetPos( popmenu, vp_w*0.15, vp_h*0.39, 0.0 );
		SetOptionI( popmenu, TAG("ZSRT"), ZSORT_TOP_MOST );
		AttachScript( popmenu, "general_menu", "general_menu" );
		SetPubVar( popmenu, "manager", Self() );
		//SetPubVar( popmenu, "focus", GetPubVar( parent, "focus_glowfly") );
		SetPubVar( popmenu, "focus", focus_navimenu );

		// list initialization
		SendMessage( popmenu, TAG("INUM"), item_num );
		SendMessage( popmenu, TAG("CLRW"), 1, 3 );
		SendMessage( popmenu, TAG("DIST"), 0.0, vp_h*0.12 );
		SendMessage( popmenu, TAG("MGIN"), 2, 2 );
		SendMessage( popmenu, TAG("MODE"), LIST_CTRL_POPUP, LIST_ANIM_TODIRECT, LIST_OPEN_NONE );
		SendMessage( popmenu, TAG("ANIM"), 400.0, 0.0, ANIM_TIMER_EXPONENT );
		// フォーカス渡す POPUPなので返って来ない
		SendMessage( popmenu, TAG("FOCS"), TAG("FRST") ); //フォーカス入れ
	}

	//new text[256];
    new obj:menutitle;
    {
		menutitle = CreateLabel( self );
		SetPos( menutitle, vp_w*0.1, vp_h*0.22, 0.0 );
		SetColor( menutitle, title_r, title_g, title_b, 0.0 );
		AnimColor( menutitle, title_r, title_g, title_b, 1.0, 300.0 );
		SetOptionI( menutitle, TAG("FONT"), FontID(FONT_HGRSGU) );
		SetOptionS( menutitle, TAG("UTF8"), title_str );
		SetOptionI( menutitle, TAG("FSIZ"), 24 );
		SetOptionI( menutitle, TAG("ZSRT"), ZSORT_TOP_MOST );
    }
	/*
	new obj:scroll_bar; //メニュー 3つ以下なら出さない!!
	//new bool:scroll_active = false;
	if( item_num > 3 ){
		//scroll_active = true;
	    scroll_bar = CreateNull( self );

		SetPos( scroll_bar, vp_w*0.15-20.0, vp_h*0.48, 0.0 );
		SetScale( scroll_bar, 1.0, 0.7, 1.0 );
		SetOptionI( scroll_bar, TAG("ZSRT"), ZSORT_TOP_MOST );
		AttachScript( scroll_bar, _, "scroll_bar" );
		SetPubVar( scroll_bar, "manager", Self() );
		SetPubVar( scroll_bar, "focus", GetPubVar( parent, "focus_glowfly") );

		SetPubVar( scroll_bar, "top", 0.0 );
		SetPubVar( scroll_bar, "length", 0.5 );
	}
	*/

	new selected = -1;
	for(;;)
	{
		new bool:listexit = false;
		new id, param, param1, param2;
		while( RecvMessage( id, param, param1, param2 ) )
		{
			if( id==TAG("FNSH"))
			{
				// 親からのmenu終了命令
				listexit = true;
			}
			if( id==TAG("ITEM"))
			{
				// 子からのアイテム作成要求
				if( obj:param == popmenu )
				{
					// 子アイテムへスクリプトアタッチ
					//	new obj:newitem = globalmenu_item_setscript( obj:param1, param2 );
					new obj:newitem = navi_menu_item_create( obj:param1, param2, group_str );
					SendMessage( obj:param, TAG("ITEM"), newitem, param2 );
				}
			}
			// popup menu
			if( id==TAG("PUSH"))
			{
				// 子からのボタン押下
				if( param1 == TAG("RRGT") )
				{
					//printf("list push_no %d\n", param);
					selected = param;
					SendMessage( popmenu, TAG("PUSH") );
					SendMessage( parent, TAG("PUSH"), param, param1 );
					WaitMessage( TAG("PUSH") ); //親からの処理再開待ち
				}
				if( param1 == TAG("RDWN") )
				{
					//printf("list push_no %d\n", param);
					selected = param;
					SendMessage( popmenu, TAG("PUSH") );
					SendMessage( parent, TAG("PUSH"), param, param1 );
					WaitMessage( TAG("PUSH") ); //親からの処理再開待ち
				}
			}
		}

		if( ScriptState(popmenu)==SCRIPT_FINISHED || listexit ) break;
		sleep;
 	}

	SendMessage( popmenu, TAG("FADE"), 0.0, 300.0 );
	AnimColor( menutitle, title_r, title_g, title_b, 0.0, 300.0 ); // 200/200/200 仕様決
	wait(300);

    if(popmenu) Destroy( popmenu );
    //if(scroll_bar) Destroy( scroll_bar );
	if(menutitle) Destroy( menutitle );

   	return selected;
}

public earth_round()
{
	new obj:self = Self();
	for(;;){
		AnimRotate( self, 0.0, -PI, 0.0, 20000.0  );
		wait(20000);
		AnimRotate( self, 0.0, -PI*2.0, 0.0, 20000.0  );
		wait(20000);
		SetRot( self, 0.0, 0.0, 0.0 ); //ゼロリセット
	}
}

public pop_navi_menu( title_group[], title_no )
{
	new float:vp_w;
	new float:vp_h;
	bn_get_viewport( vp_w, vp_h );

	new menu_status = NAVIMENU_STAT_NONE;

	// 後でもっときれいな defocus に換える
	new obj:defocus;
	{
		new float:x,float:y,float:z;
		defocus = CreateDefocus( Disp2D() );
		//defocus = CreateDefocus( Self() );
		//bn_get_2Dto3D( x, y, z, vp_w*0.5, vp_h*0.5 );
		SetPos( defocus, x, y, z-10.0 );
		SetOptionI( defocus, TAG("LEVL"), 0 );
		SetColor( defocus, 0.95, 0.95, 0.95, 1.0 );
	}

	// focus 作成
	focus_navimenu = CreateFocus( "focus" );

	// メニューのベース座標 左上！！ を決定
	new obj:menubase = CreateNull( Disp2D() );
	SetPos( menubase,  vp_w*0.2, vp_h*0.2, 0.0 );

	// バックの色落し
	new obj:backfade = CreatePlane( Disp2D() );
	{
		SetPos( backfade, vp_w*0.5, vp_h*0.5, -10.0 );
		SetLocalScale( backfade, vp_w*1.2, vp_h*1.2, 1.0 );
		SetColor( backfade, 0.0, 0.0, 0.0, 0.0 );
		AnimColor( backfade, 0.0, 0.0, 0.0, 0.4, 400.0 );
	}

	{//defocus anim
		SetOptionI( defocus, TAG("LEVL"), 1 );
		wait(100);
		SetOptionI( defocus, TAG("LEVL"), 2 );
	}

	new obj:window1;
	{
	    window1 = CreateNull( Disp2D() );
		//GetOffPos( menubase, x, y, z, vp_w*0.25, vp_h*0.5, 0.0, Disp2D() );
		SetPos( window1, vp_w*0.5, vp_h*0.5, 0.0 );
		SetOptionI( window1, TAG("ZSRT"), ZSORT_BOTTOM );
		AttachScript( window1, _, "window" );
		SetPubVar( window1, "manager", Self() );
		//SetPubVar( window1, "focus", focus );
		SetPubVar( window1, "focus", focus_navimenu );
		SetPubVar( window1, "width", vp_w*0.7 );
		SetPubVar( window1, "height", vp_h*0.6 );
	}

	{
		wait(100);
		SetOptionI( defocus, TAG("LEVL"), 3 );
		wait(100);
		SetOptionI( defocus, TAG("LEVL"), 4 );
	}

	//@@
	new obj:earth = CreatePrim( Disp2D() );
	{
		SetPos( earth, vp_w*0.5, vp_h*0.5, 70.0);
		SetColor( earth, 0.6, 0.8, 1.0, 0.0 );
		// デコード起動
	    imgLoadTim2Start("file://opt0/bn/script/common/texture/earth128.tm2" );
		imgLoadTim2Wait();

		SetLocalScale( earth, 128.0, 128.0, 128.0  );
		SetTexture( earth, imgTexID() ) ;	// 形状タイプ
		SetOptionI( earth, TAG( "TYPE" ), PRIM_SHAPE_BALL ) ;	// 形状タイプ
		SetOptionI( earth, TAG( "FACE" ), PRIM_FACE_BOTH ) ;	// 裏表面指定
		SetOptionI( earth, TAG( "HDIV" ), 48 ) ;		// 経度方向分割数
		SetOptionI( earth, TAG( "VDIV" ), 24 ) ;		// 緯度方向分割数
		SetOptionI( earth, TAG( "ALPH" ), ALPHA_BLEND_ADD ) ;		// 緯度方向分割数

		AnimColor( earth, 0.6, 0.8, 1.0, 0.8, 200.0 );

		AttachScript( earth, "earth_round" );
	}

	/*
    new obj:progress_bar = CreateNull( Disp2D() );
	SetPos( prwindow1, vp_w*0.5, vp_h*0.5, 0.0 );
	SetPos( progress_bar, 320.0, 224.0, 0.0 );
	SetOptionI( progress_bar, TAG("ZSRT"), ZSORT_TOP_MOST );
	AttachScript( progress_bar, _, "progress_bar" );
	SetPubVar( progress_bar, "manager", Self() );
	SetPubVar( progress_bar, "progress", 0 );
	*/

	// menu 階層の仕様が欲しい!! 取り合えず 3段でつくる
	const hier_max = 3;
	new selected[hier_max];
	new title_str[hier_max][64];
	new group_str[hier_max][64];

	// 初期 title & group
	new hierarchy = 0; //現在の階層
	xmlGetGroupAttr( "TEXT", title_group, title_no, "value", title_str[hierarchy] );
	xmlGetGroupAttr( "TEXT", title_group, title_no, "subgroup", group_str[hierarchy] );

	selected[0] = -1;
	for(;;)
	{
	// menu 階層を再帰的に繰り返すルーチン
		new hier = hierarchy;
		new item_num = xmlGroupNum( "TEXT", group_str[hier] );

		// menuスクリプトアタッチ
		new args[256];
		sprintf(args, "num = %d; title = %s ; group = %s",
				item_num, title_str[hier], group_str[hier]);
		AttachScript( menubase, "navi_menu_core", _, args ); // 新しいmenuスクリプト
		SendMessage( menubase, TAG("INIT"), Self() ); // メッセージを投げてくれるように
	//@@ フォーカスセット
		SetPubVar( menubase, "focus_navimenu", focus_navimenu );

		for(;;)
		{
			// menu項目処理ルーチン
			new id, param0, param1, param2;
			while( RecvMessage( id, param0, param1, param2 ) )
		   	{
				if(id == TAG("PUSH"))
				{
					if( param1 == TAG("RRGT") )
					{
						new sel = param0;
						// 注意 hier と sel は新規に書き換えられて戻ってくる!!
						menu_status = navi_menu_process( hier, sel );

						// 階層が違うので終了させる
						if( hier!=hierarchy ) SendMessage( menubase, TAG("FNSH") );
						selected[hierarchy] = sel;
						SendMessage( menubase, TAG("PUSH") );
					}
					if( param1 == TAG("RDWN") )
					{
						// backで良い?
						hier--;
						SendMessage( menubase, TAG("FNSH") );
						SendMessage( menubase, TAG("PUSH") );
					}
				}
			}

			// menuスクリプト階層終了し他の階層に行く
			if( ScriptState(menubase)==SCRIPT_FINISHED ) break;

			sleep;
		}

		if( hier < 0 ){ hierarchy = 0; break;} //menu finished

		// 次の階層に行く準備
		if( hier > hierarchy ) //go to sub_hier
		{
			xmlGetGroupAttr( "TEXT", group_str[hierarchy], selected[hierarchy], "value", title_str[hier] );
			xmlGetGroupAttr( "TEXT", group_str[hierarchy], selected[hierarchy], "subgroup", group_str[hier] );
		}
		if( hier < hierarchy ) //back to parent
		{
			//if( hier < 0 ){ hierarchy = 0; break;} //menu finished
		}

		hierarchy = hier;

	}

	AnimColor( backfade, 0.0, 0.0, 0.0, 0.0, 300.0 );
	AnimColor( earth, 1.0, 1.0, 1.0, 0.0, 300.0 );
	SendMessage( window1, TAG("FADE"), 0.0, 300.0 );

	{//defocus anim
		SetOptionI( defocus, TAG("LEVL"), 3 );
		wait(100);
		SetOptionI( defocus, TAG("LEVL"), 2 );
		wait(100);
		SetOptionI( defocus, TAG("LEVL"), 1 );
		wait(100);
		SetOptionI( defocus, TAG("LEVL"), 0 );
	}

	if(window1) Destroy( window1 );
	if(defocus) Destroy( defocus );
	if(earth) Destroy( earth );
	if(menubase) Destroy( menubase );
	if(focus_navimenu) Destroy( focus_navimenu );
	if(backfade) Destroy( backfade );

	//return selected[hierarchy];
	return menu_status;

}

obj: navi_menu_item_create( obj:parent, index, const group_str[] )
{
	new text[256];
	new args[256];
    new obj:label;
    {
		label = CreateLabel( parent );
		//xmlGetGroupAttr( "TEXT", "global_menu_item", index, "value", text );
		xmlGetGroupAttr( "TEXT", group_str, index, "value", text );
		SetColor( label, item_r, item_g, item_b, 0.0 ); //
		SetOptionI( label, TAG("ALPH"), ALPHA_BLEND_ADD ); //加算モードで
		//SetOptionI( label, TAG("FONT"), FontID(FONT_HGRSGU) );
		SetOptionI( label, TAG("FONT"), FontID(FONT_TIMESBI) );
		SetOptionS( label, TAG("UTF8"), text );
		SetOptionI( label, TAG("FSIZ"), 22 );
    }
	sprintf( args, "label_string = %s", text );
	AttachScript( label, "label_item", "label_item", args );

	return label;
}
