//
//	アトリビュート名入力
//
function	DlogAttr( title, attr )
{
	var	name,filename, atrno,i, j, k;
	var	org_code, code = 7;
	var	msg[8], uv[2][2], objlist[MAXOBJECT];
	var	r,g,b;

	msg = {"黒", "青","赤","紫","緑","水","黄","白" };

	if ( attr < 0 )
	{
		if( (EXPERT == TRUE ) | ( MACHINE != "WINDOWS" ))
		{
			DlogOpen( title, 5, "ｷｬﾝｾﾙ","決定後終了", "決定後さらに入力" );
			DlogString( 0, "アトリビュート名", "", 23 );
			DlogString( 2, "ファイル名      ", AttrFile( AttrCurrent()), 23 );
			DlogSelect( 4, "線の色          ", msg, code );
		}
		else
		{
			DlogOpen( title, 3, "ｷｬﾝｾﾙ","決定後終了", "決定後さらに入力" );
			DlogString( 0, "アトリビュート名", "", 23 );
			DlogSelect( 2, "線の色          ", msg, code );
		}
	}
	else
	{
		org_code = AttrCode( attr );
		if( (EXPERT == TRUE ) | ( MACHINE != "WINDOWS" ))
		{
			DlogOpen( title, 5, "ｷｬﾝｾﾙ ", " 決定 " );
			DlogString( 0, "アトリビュート名", AttrName( attr ), 23 );
			DlogString( 2, "ファイル名      ", AttrFile( attr ), 23 );
			DlogSelect( 4, "線の色          ", msg, org_code );
		}
		else
		{
			DlogOpen( title, 3, "ｷｬﾝｾﾙ ", " 決定 " );
			DlogString( 0, "アトリビュート名", AttrName( attr ), 23 );
			DlogSelect( 2, "線の色          ", msg, org_code );
		}
	}
	i = DlogWait();
	if( i == 0 )
		return FALSE;
	else
	{
		if( (EXPERT == TRUE ) | ( MACHINE != "WINDOWS" ))
		{
			name = DlogAnswer( 0 );
			filename = DlogAnswer( 2 );
			code = DlogAnswer( 4 );
			if( filename == "" )
				filename = MakeFile( name, ".atr" );
			else
				filename = MakeFile( filename, ".atr" );
		}
		else
		{
			name = DlogAnswer( 0 );
			code = DlogAnswer( 2 );
		}
		if(( attr < 0 ) & ( name == "" ))
			return FALSE;
		if( NameCheck( name ) == FALSE )
		{
			Warning();
			Message( error_msg7 );
			return TRUE;
		}
		atrno = AttrSearch( name );
		if( attr < 0 )
		{
			if( atrno < 0 )
			{
				atrno = AttrAppend( name, code );
				if( (EXPERT == TRUE ) | ( MACHINE != "WINDOWS" ))
					AttrFile( atrno, filename );
				else
				{
					AttrMap( atrno, TRUE );
					AttrMapFile( atrno, MAPPINGFILE );
					uv[0] = {0,0};
					uv[1] = {MAPWIND-1, MAPWIND-1};
					SetAttrMapWind( atrno, uv[0], uv[1] );
					uv[0] = {0,0};
					uv[1] = {UVmax, UVmax};
					SetAttrMapSize( atrno, uv[0], uv[1] );
				}
			}
			else
			{
				Warning();
				if( MESSAGE == ON )
					Message( "同名のアトリビュートが存在します" );
			}
		}
		else
		{
			if( name == "" )
			{
				Warning();
				if( MESSAGE == ON )
					Message( "アトリビュート名が指定されていません" );
				return FALSE;
			}
			if( atrno < 0 )
			{
				AttrName( attr, name );
				AttrCode( attr, code );
				r = 0.0;
				g = 0.0;
				b = 0.0;
				if (code & 2) r = 1.0;
				if (code & 4) g = 1.0;
				if (code & 1) b = 1.0;
				if( (EXPERT == TRUE ) | ( MACHINE != "WINDOWS" ))
					AttrFile( attr, filename );
				else
					AttrCol( attr, vector(r,g,b));
				if( org_code != code )
					Update( OVERWRITE );
				j = AtrtoObj( attr, objlist );
				for( k = 0; k< j;k++ )
					ObjEdit( objlist[k], TRUE );
				if( j != 0 )
					UpdateObject();
			}
			else if( atrno == attr )
			{
				if( org_code != code )
				{
					AttrCode( attr, code );
					r = 0.0;
					g = 0.0;
					b = 0.0;
					if (code & 2) r = 1.0;
					if (code & 4) g = 1.0;
					if (code & 1) b = 1.0;
					if( (EXPERT == TRUE ) | ( MACHINE != "WINDOWS" ))
						AttrFile( attr, filename );
					else
						AttrCol(atrno, vector(r,g,b));
					Update( OVERWRITE );
					j = AtrtoObj( attr, objlist );
					for( k = 0; k< j;k++ )
						ObjEdit( objlist[k], TRUE );
					if( j != 0 )
						UpdateObject();
				}
			}
			else
			{
				Warning();
				if( MESSAGE == ON )
					Message( "同名のアトリビュートが存在します" );
				return FALSE;
			}
		}
		UpdateAttribute();
		if(( attr < 0 ) & ( i == 1 ))
			return FALSE;
		else
			return TRUE;
	}
	return FALSE;
}

//
//	オブジェクト名入力
//
function	DlogObj( title, obj )
{
	var	flag, ele[3], temp;
	var	name, file,objno, i;

	if( (EXPERT == TRUE ) | ( MACHINE != "WINDOWS" ))
	{
		DlogOpen( title, 3 );
		if ( obj < 0 )
		{
			DlogString( 0, "オブジェクト名", "", 40 );
			DlogString( 2, "ファイル名    ", ObjFile(ObjCurrent()), 40, "形状ファイル(*.SUF)");
			flag = TRUE ;
		}
		else
		{
			DlogString( 0, "オブジェクト名", ObjName( obj ), 40 );
			DlogString( 2, "ファイル名    ", ObjFile( obj ), 40, "形状ファイル(*.SUF)");
			flag = FALSE ;
		}
	}
	else
	{
		DlogOpen( title, 1 );
		if ( obj < 0 )
		{
			DlogString( 0, "オブジェクト名", "", 40 );
			flag = TRUE ;
		}
		else
		{
			DlogString( 0, "オブジェクト名", ObjName( obj ), 40 );
			flag = FALSE ;
		}
	}
	if ( DlogWait() )
	{
		name = DlogAnswer( 0 );
		if ( name == "" )
			return -1;
		if( NameCheck( name ) == FALSE )
		{
			Warning();
			Message( error_msg6 );
			return -1;
		}
		if( (EXPERT == TRUE ) | ( MACHINE != "WINDOWS" ))
		{
			file = DlogAnswer( 2 );
			if ( file == "" )
				file = MakeFile( name, "suf" );
			else
				file = MakeFile( file, "suf" );
		}
		else
		{
			file = MakeFile( name, "suf" );
		}
		objno = ObjSearch( name );
		if( objno < 0 )
		{
			if( flag )
			{
				objno = ObjAppend( name, file );
				return objno;
			}
			else
			{
				if( ObjName( obj ) != name )
				{
					ObjName( obj, name );
					ObjEdit( obj, TRUE );
				}
				Bunkai( file, ele );
				if( ele[0] == "" )
				{
					temp = ObjFile( obj );
					Bunkai( temp, ele );
					file = ele[0] + file;
				}
				ObjFile( obj, file );
				return obj;
			}
		}
		else if( objno == obj & flag == FALSE )
		{
			ObjName( obj, name );
			Bunkai( file, ele );
			if( ele[0] == "" )
			{
				temp = ObjFile( obj );
				Bunkai( temp, ele );
				file = ele[0] + file;
			}
			if( ObjFile( obj ) != file )
			{
				ObjFile( obj, file );
				ObjEdit( obj, TRUE );
			}
			return obj;
		}
		else
		{
			Warning();
			if( MESSAGE == ON )
				Message( "同名のオブジェクトが存在します" );
			return -1;
		}
	}
	return -1;
}

//
//	アトリビュート追加
//
function NewAtr()
{
	var	temp;
	while( TRUE )
	{
		if( Attrs() < MAXATTR )
		{
			temp = DlogAttr( "アトリビュート追加", -1 );
			if( temp == FALSE )
				break;
		}
		else
			break;
	}
	UpdateAttribute();
}

//
//	面のアトリビュート変更
//
function ChangeAtr()
{
	var	i = TRUE;
	if( SelectPolygons() == 0 )
		return;
	if( MESSAGE == ON )
	{
		DlogOpen( "面のアトリビュート変更", 4 );
		DlogMessage( 0, "選択されている面のアトリビュートを" );
		DlogMessage( 1, AttrName(AttrCurrent())+" に変更します。");
		DlogMessage( 2, "よろしいですか？" );
		i = DlogWait();
	}
	if( i )
	{
		AttrChange( AttrCurrent() );
		UpdateObject();
	}
}

//
//	アトリビュート名変更
//
function EditAtr()
{
	DlogAttr( "アトリビュート名変更", AttrCurrent() );
	UpdateAttribute();
}

//
//	アトリビュート削除
//
function DeleteAtr()
{
	var	sel1, sel2, temp_sel;
	var	atrno, i;

	sel1 = Select();
	SelectAll( TRUE, SELECT_UPDATE );
	sel2 = Select();
	PolyInvisible( FALSE );
	atrno = AttrCurrent();
	SelectAttr( atrno, TRUE, SELECT_UPDATE );
	if( SelectPolygons() != 0 )
	{
		DlogOpen( "", 2, "いいえ", " はい " );
		DlogMessage( 0, "アトリビュート名が " + AttrName( atrno ) + " の面があります。" );
		DlogMessage( 1, "これらの面も削除されますがよろしいですか？" );
		i = DlogWait();
		if( i == 0 )
		{
			Select( sel2 );
			PolyInvisible( invisible_flag );
			Select( sel1 );
			return;
		}
		else
		{
			temp_sel = Select();
			sel1 = sel1 & ( !temp_sel );
			sel2 = sel2 & ( !temp_sel );
			PolyDelete( temp_sel );
			Select( sel2 );
			PolyInvisible( invisible_flag );
			Select( sel1 );
			Update( CLEAR );
		}
	}
	else
	{
		Select( sel2 );
		PolyInvisible( invisible_flag );
		Select( sel1 );
	}
	if( atrno == ( Attrs() - 1 ))
	{
		AttrDelete( atrno );
		AttrCurrent( Attrs()-1);
	}
	else
		AttrDelete( atrno );
	UpdateAttribute();
}

function private ColorToString(v)
{
	if (vx(v) == vy(v) && vx(v) == vz(v)) {
		return substr(tostring(0.005+vx(v)) + "      ", 4);
	} else {
		return "rgb ( "
			 + substr(tostring(0.005+vx(v)) + "      ", 4) + " "
			 + substr(tostring(0.005+vy(v)) + "      ", 4) + " "
			 + substr(tostring(0.005+vz(v)) + "      ", 4) + ")";
	}
}

var const COLOR_NUM = 21;

function colconv( n )
{
//	return substr(tostring(0.005 + (0.0+n) / 20.0) + "    ", 4);
	return n*5;
}

//
//	アトリビュート詳細表示
//
function private EditAtrParameter()
{
	var type, mapname, name, temp, i, j, objlist[MAXOBJECT];
	var c[9];
	var size1[2], size2[2];
	var wind1[2], wind2[2];
	var atrno, atrcode;
	var	msg[8];

	msg = {"黒", "青","赤","紫","緑","水","黄","白" };

	atrno = AttrCurrent();
	atrcode = AttrCode( atrno );
	c[0] = AttrCol(atrno);
	c[1] = AttrDif(atrno);
	c[2] = AttrAmb(atrno);
	c[3] = AttrTra(atrno);
	c[4] = AttrSpc(atrno);
	c[5] = AttrSize(atrno);
	c[6] = AttrRef(atrno);

	type = AttrMap(atrno);
	if (type) {
		name = AttrMapFile(atrno);
		GetAttrMapSize(atrno, size1, size2);
		GetAttrMapWind(atrno, wind1, wind2);
		DlogOpen("アトリビュート詳細設定", 20);
	} else {
		name = "";
		DlogOpen("アトリビュート詳細設定", 17);
	}
	DlogString( 0, "アトリビュート名", AttrName(atrno), 23 );
	DlogString( 2, "　　　ファイル名", AttrFile(atrno), 23 , "色ファイル(*.ATR)");
	DlogSelect( 4, "　　　　　線の色", msg, atrcode );
	DlogSlider( 6, "　　　　赤色成分", SLIDER_HABA, COLOR_NUM, 0+(0.005+vx(c[0]))*20, colconv);
	DlogSlider( 7, "　　　　緑色成分", SLIDER_HABA, COLOR_NUM, 0+(0.005+vy(c[0]))*20, colconv);
	DlogSlider( 8, "　　　　青色成分", SLIDER_HABA, COLOR_NUM, 0+(0.005+vz(c[0]))*20, colconv);
	DlogSlider( 9, "　　　　　直接光", SLIDER_HABA, COLOR_NUM, 0+(0.005+vx(c[1]))*20, colconv);
	DlogSlider(10, "　　　　　環境光", SLIDER_HABA, COLOR_NUM, 0+(0.005+vx(c[2]))*20, colconv);
	DlogSlider(11, "　　　　　透明度", SLIDER_HABA, COLOR_NUM, 0+(0.005+vx(c[3]))*20, colconv);
	DlogSlider(12, "　　散乱光の強さ", SLIDER_HABA, COLOR_NUM, 0+(0.005+vx(c[4]))*20, colconv);
	DlogSlider(13, "　　　　散乱係数", SLIDER_HABA, COLOR_NUM, 0+(0.005+vx(c[5]))*20, colconv);
	DlogSlider(14, "　　　　　反射率", SLIDER_HABA, COLOR_NUM, 0+(0.005+vx(c[6]))*20, colconv);
	DlogString(16, "　マッピング画像", name, 23, "DoGA形式画像ファイル(*.PIC)");
	if (type) {
		DlogMessage(18, "　　　ウィンドウ: " + tostring(0+wind1[0])
								   + " " + tostring(0+wind1[1])
								   + " " + tostring(0+wind2[0])
								   + " " + tostring(0+wind2[1]));
		DlogMessage(19, "　　　　　サイズ: " + tostring(0+size1[0])
								   + " " + tostring(0+size1[1])
								   + " " + tostring(0+size2[0])
								   + " " + tostring(0+size2[1]));
	}
	if (DlogWait()) {
		var l;
		name = DlogAnswer(0);
		if( NameCheck( name ) == FALSE )
		{
			Warning();
			Message( error_msg7 );
			return FALSE;
		}
		if( name == "" )
		{
			Warning();
			return FALSE;
		}
		temp = AttrSearch( name );
		if((temp < 0 ) | ( temp == atrno ))
		{
			AttrName(atrno, DlogAnswer( 0 ));
			if( DlogAnswer( 2 ) != "" )
				AttrFile(atrno, MakeFile(DlogAnswer( 2 ),".atr"));
			AttrCode(atrno, DlogAnswer( 4 ));
			c[0] = vector(0.05*DlogAnswer(6), 0.05*DlogAnswer(7), 0.05*DlogAnswer(8));
			l = 0.05*DlogAnswer(9); if (vx(c[1]) != l) c[1] = vector(l,l,l);
			l = 0.05*DlogAnswer(10); if (vx(c[1]) != l) c[2] = vector(l,l,l);
			l = 0.05*DlogAnswer(11); if (vx(c[1]) != l) c[3] = vector(l,l,l);
			l = 0.05*DlogAnswer(12); if (vx(c[1]) != l) c[4] = vector(l,l,l);
			l = 0.05*DlogAnswer(13); if (vx(c[1]) != l) c[5] = vector(l,l,l);
			l = 0.05*DlogAnswer(14); if (vx(c[1]) != l) c[6] = vector(l,l,l);

			AttrCol(atrno, c[0]);
			AttrDif(atrno, c[1]);
			AttrAmb(atrno, c[2]);
			AttrTra(atrno, c[3]);
			AttrSpc(atrno, c[4]);
			AttrSize(atrno, c[5]);
			AttrRef(atrno, c[6]);

			mapname = DlogAnswer(16);
			if (!mapname > 0) {
				if (!type) {
					AttrMap(atrno, TRUE);
					wind1[0] = 0;
					wind1[1] = 0;
					wind2[0] = 255;
					wind2[1] = 255;
					SetAttrMapWind(atrno, wind1, wind2);
					size1[0] = 0;
					size1[1] = 0;
					size2[0] = 999;
					size2[1] = 999;
					SetAttrMapSize(atrno, size1, size2);
					AttrMap(atrno, TRUE);
				}
				AttrMapFile(atrno, mapname);
			} else {
				AttrMap(atrno, FALSE);
			}
			j = AtrtoObj( atrno, objlist );
			for( i = 0; i< j;i++ )
				ObjEdit( objlist[i], TRUE );
			if( j > 0 )
				UpdateObject();
		}
		else
		{
			Warning();
			if( MESSAGE == ON )
				Message( "同名のアトリビュートが存在します" );
			return FALSE;
		}
	}
	if( atrcode != AttrCode( atrno ))
		Update( OVERWRITE );
}

//
//	オブジェクト追加
//
function private NewObject()
{
	if( Objects() < MAXOBJECT )
	{
		DlogObj( "オブジェクト作成", -1 );
		UpdateObject();
	}
	else
	{
		Warning();
		if( MESSAGE == ON )
			Message( "オブジェクト数が最大です" );
	}
}

//
//	面のオブジェクト変更
//
function ChangeObj()
{
	var	i = TRUE;
	if( SelectPolygons() == 0 )
		return;
	if( MESSAGE == ON )
	{
		DlogOpen( "面のオブジェクト変更", 4 );
		DlogMessage( 0, "選択されている面のオブジェクトを" );
		DlogMessage( 1, ObjName(ObjCurrent())+" に変更します。");
		DlogMessage( 2, "よろしいですか？" );
		i = DlogWait();
	}
	if( i )
	{
		ObjChange( ObjCurrent() );
		UpdateObject();
	}
}

//
//	オブジェクト名（＋ファイル名）変更
//
function private EditObj()
{
	DlogObj( "オブジェクト変更", ObjCurrent() );
	UpdateObject();
}

//
//	オブジェクト削除
//
function DeleteObj()
{
	var	sel1, sel2, temp_sel;
	var	objno, i;

	sel1 = Select();
	SelectAll( TRUE, SELECT_UPDATE );
	sel2 = Select();
	PolyInvisible( FALSE );
	objno = ObjCurrent();
	SelectObj( objno, TRUE, SELECT_UPDATE );
	if( SelectPolygons() != 0 )
	{
		DlogOpen( "", 2, "いいえ", " はい " );
		DlogMessage( 0, "オブジェクト名が " + ObjName( objno ) + " の面があります。" );
		DlogMessage( 1, "これらの面も削除されますがよろしいですか？" );
		i = DlogWait();
		if( i == 0 )
		{
			Select( sel2 );
			PolyInvisible( invisible_flag );
			Select( sel1 );
			return;
		}
		else
		{
			temp_sel = Select();
			sel1 = sel1 & (!temp_sel);
			sel2 = sel2 & (!temp_sel);
			PolyDelete( temp_sel );
			Select( sel2 );
			PolyInvisible( invisible_flag );
			Select( sel1 );
			Update( CLEAR );
		}
	}
	else
	{
		Select( sel2 );
		PolyInvisible( invisible_flag );
		Select( sel1 );
	}
	if( objno == (Objects()-1) )
	{
		ObjDelete( objno );
		ObjCurrent( Objects()-1);
	}
	else
	{
		ObjDelete( objno );
	}
	UpdateObject();
}

function private ObjectList()
{
	var	i, msg;
	var	obj_number;
	var	str;

	obj_number = Objects();
	DlogOpen( "オブジェクト一覧", obj_number, FALSE );
	for( i = 0 ; i < obj_number; i++ )
	{
		if ( ObjEdit( i ) )
			msg = "*" ;
		else
			msg = " " ;
		msg += substr(tostring(ObjName(i))+"                       ", 15 )
                    +  tostring(ObjPolygons(i),5)+" 面";
		msg = substr(msg+"                                       ", 30 );
		msg += tostring( ObjFile( i ));
		DlogMessage( i, msg );
	}
	DlogWait();
}

function private AtrCheck()
{
	var	temp;
	temp = MenuFunction();
	if( temp == DeleteAtr )
	{
		if( Attrs() < 2 )
			MenuEnable( MENU_DISABLE );
		else
			MenuEnable( MENU_ENABLE );
	}
	else if( temp == NewAtr )
	{
		if( Attrs() < MAXATTR )
			MenuEnable( MENU_ENABLE );
		else
			MenuEnable( MENU_DISABLE );
	}
}

function private ObjCheck()
{
	var	temp;
	temp = MenuFunction();
	if( temp == DeleteObj )
	{
		if( Objects() < 2 )
			MenuEnable( MENU_DISABLE );
		else
			MenuEnable( MENU_ENABLE );
	}
	else if( temp == NewObject )
	{
		if( Objects() < MAXOBJECT )
			MenuEnable( MENU_ENABLE );
		else
			MenuEnable( MENU_DISABLE );
	}
}

if( (EXPERT != TRUE ) & ( MACHINE == "WINDOWS" ))
{
	MenuPosition(
		Menu( " ｱﾄﾘ･ｵﾌﾞｼﾞｪ(&A)",
			"面のｱﾄﾘﾋﾞｭｰﾄ変更 	CTRL+A",	ChangeAtr,SelectCheck,
			"ｱﾄﾘﾋﾞｭｰﾄ追加...  ",			NewAtr,AtrCheck,
			"ｱﾄﾘﾋﾞｭｰﾄ名変更...",			EditAtr,
			"ｱﾄﾘﾋﾞｭｰﾄ削除     ",			DeleteAtr,AtrCheck,
			"-----------------",			SEPARATE_MENU,
			"面のｵﾌﾞｼﾞｪｸﾄ変更 	CTRL+O",	ChangeObj,SelectCheck,
			"ｵﾌﾞｼﾞｪｸﾄ追加...  ",			NewObject,ObjCheck,
			"ｵﾌﾞｼﾞｪｸﾄ名変更...",			EditObj,
			"ｵﾌﾞｼﾞｪｸﾄ削除     ",			DeleteObj,ObjCheck,
			"-----------------",			SEPARATE_MENU,
			"ｵﾌﾞｼﾞｪｸﾄ一覧...(&L)",			ObjectList
		), Menu_atrobj
	);
}
else
{
	MenuPosition(
		Menu( " ｱﾄﾘ･ｵﾌﾞｼﾞｪ(&A)",
			"面のｱﾄﾘﾋﾞｭｰﾄ変更 	CTRL+A",	ChangeAtr,SelectCheck,
			"ｱﾄﾘﾋﾞｭｰﾄ追加...  ",			NewAtr,AtrCheck,
			"ｱﾄﾘﾋﾞｭｰﾄ名変更...",			EditAtrParameter,
			"ｱﾄﾘﾋﾞｭｰﾄ削除     ",			DeleteAtr,AtrCheck,
			"-----------------",			SEPARATE_MENU,
			"面のｵﾌﾞｼﾞｪｸﾄ変更 	CTRL+O",	ChangeObj,SelectCheck,
			"ｵﾌﾞｼﾞｪｸﾄ追加...  ",			NewObject,ObjCheck,
			"ｵﾌﾞｼﾞｪｸﾄ名変更...",			EditObj,
			"ｵﾌﾞｼﾞｪｸﾄ削除     ",			DeleteObj,ObjCheck,
			"-----------------",			SEPARATE_MENU,
			"ｵﾌﾞｼﾞｪｸﾄ一覧...(&L)",			ObjectList
		), Menu_atrobj
	);
}
