var private const	UVmax_pos = 1;
var private const	Deg_pos = 2;
var private const	Houkou_pos = 3;
var private	Menu_temp1;
var private	Menu_temp2;
var private	Menu_temp3;

function private InputUVmax()
{
	var	key, temp, msg[2];

	DlogOpen( "平面ﾏｯﾋﾟﾝｸﾞ 設定変更", 1 );
	DlogString( 0, "UV最大値   ", tostring( UVmax ), 6 );
	key = DlogWait();
	if( key == 0 )
		return FALSE;
	else
	{
		UVmax = atoi(DlogAnswer( 0 ));
		Status_title[UVmax_pos] = "UV最大値:"+tostring( UVmax, 4 )+" ";
		DrawStatus();
		Menu_temp3 = ON;
	}
	return TRUE;
}

function private InputSetup()
{
	var	key, temp, msg[2];

	msg = {"右回り","左回り" };
	DlogOpen( "円筒ﾏｯﾋﾟﾝｸﾞ 設定変更", 5 );
	DlogString( 0, "UV最大値   ", tostring( UVmax ), 6 );
	DlogString( 2, "角度       ", tostring( Menu_temp1 ), 6 );
	DlogSelect( 4, "ﾏｯﾋﾟﾝｸﾞ方向", msg, ( 1- Menu_temp2 ) );
	key = DlogWait();
	if( key == 0 )
		return FALSE;
	else
	{
		UVmax = atoi(DlogAnswer( 0 ));
		Menu_temp1 = atoi(DlogAnswer( 2 ));
		temp = DlogAnswer( 4 );
		if( temp == 0 )
			Menu_temp2 = ON;
		else
			Menu_temp2 = OFF;
		Status_title[UVmax_pos] = "UV最大値:"+tostring( UVmax, 4 )+" ";
		Status_title[Deg_pos] = "角度:"+ tostring( Menu_temp1 , 5)+" ";
		if( Menu_temp2 == ON )
			Status_title[Houkou_pos] = "ﾏｯﾋﾟﾝｸﾞ方向:右回り";
		else
			Status_title[Houkou_pos] = "ﾏｯﾋﾟﾝｸﾞ方向:左回り";
		DrawStatus();
		Menu_temp3 = ON;
	}
	return TRUE;
}

//
//	マッピング座標の設定
//
function private	DrawMap( v0, v1, v2 )
{
	var	pos ;
	DrawLine( v1, v2 );
	DrawLine( v1, v0 );
	pos = v2 - v1 + v0 ;
	DrawLine( pos, v2 );
	DrawLine( pos, v0 );
}

//
//	平面ﾏｯﾋﾟﾝｸﾞＵＶ座標生成
//
function _MapPlane()
{
	var	i, key, mat, vers;
	var	org_pos[2], pos[3], temp;
	var	u_vec, v_vec, mae_vec, move_vec;
	var	pick, draw_flag = ON;

	if ( SelectPolygons() == 0 )
	{
		Warning();
		if( MESSAGE == ON )
			Message( error_msg3 );
		return ;
	}

	PushMenu();
	Status_org = Status_title;
	MenuPosition( Menu( " 動作設定(&S)",
			"設定変更   ",		InputUVmax,
			"-----------",		SEPARATE_MENU,
			"ﾏｯﾋﾟﾝｸﾞ中止	ESC",	MenuQuit
		), Menu_Title );
	ClearStatus();
	Status_title[0] = "【平面ﾏｯﾋﾟﾝｸﾞ中】";
	Status_title[UVmax_pos] = "UV最大値:"+tostring( UVmax, 4 )+" ";
	DrawStatus();
	quit_flag = FALSE;

	SelectBox( org_pos );

	cur = Cursor();
	pos[0] = cur;
	pick = cur;
	vers = 1;
	input_flag = OFF;
	while( TRUE )
	{
		if( draw_flag == ON )
		{
			if( vers == 1 )
				DrawLine( pos[0], cur );
			else
				DrawMap( pos[0], pos[1], cur );
		}
		key = WaitEvent();
		if( key )
			input_flag = ON;
		if( (( ShiftStat() & 1 ) == 1))
		{
			cur = Cursor();
			TrimCursor(pos[vers-1], MouseWindow());
			Cursor( cur );
		}
		else
			cur = Cursor();
		key = KeyCode();
		if ( input_flag | key == INPUT_KEY )
		{
			if( vers == 1 )
			{
				if( pos[0] == cur )
				{
					Warning();
					if( MESSAGE == ON )
						Message( error_msg4 );
				}
				else
				{
					pos[1] = cur;
					vers++;
					DrawLine( pos[0], pos[1] );
				}
			}
			else
			{
				u_vec = unit(Position( pos[1] ) - Position( pos[0] ));
				v_vec = unit(Position( pos[1] ) - Position( cur ));
				if( abs(v_vec*u_vec) < 0.00001 )
				{
					Warning();
					if( MESSAGE == ON )
						Message( "３点が一直線上にあるので\n"
							+"平面マッピングできません" );
				}
				else
				{
					pos[2] = cur;
					break;
				}
			}
		}
		else if( key == ESC | quit_flag == TRUE )
		{
			PopandClear( OVERWRITE );
			return;
		}
		else if( key == BS )
		{
			if( vers == 1 )
			{
				PopandClear( OVERWRITE );
				return;
			}
			else
			{
				vers--;
				DrawMap(pos[0], pos[1], pick );
			}
		}
		else
			temp = KeyProcess( key );
		if( ((key!=0 ) & (key != INPUT_KEY)) | ( pick != Cursor()) )
		{
			draw_flag = ON;
			if( vers == 1 )
				DrawLine( pos[0], pick );
			else
				DrawMap( pos[0], pos[1], pick );
		}
		else
			draw_flag = OFF;
		pick = Cursor();
		input_flag = OFF;
	}
	Clear( OVERWRITE );
	move_vec = Position( pos[0] );
	u_vec = ( Position( pos[1] ) - Position( pos[0] ))*(1.0/UVmax);
	v_vec = ( Position( pos[2] ) - Position( pos[1] ))*(1.0/UVmax);
	mae_vec = unit(u_vec*v_vec);
	temp = vector( 0, 0, 0 );
	mat = move(UNIT_MATRIX,move_vec*(-1)) / Matrix( u_vec, v_vec, mae_vec, temp );
	PolyMap( TRUE, mat );
	UpdateObject();
	PopMenu();
	Status_title = Status_org;
	DrawStatus();
}

//
//	円筒ﾏｯﾋﾟﾝｸﾞＵＶ座標の生成
//
function _CylinderMap()
{
	var	axis[2];
	var	key, vers, len, ty, u, v;
	var	axis_vec, mae_vec, yoko_vec;
	var	temp, temp_mat, temp_pos, temp_vec;
	var	rot_mat, move_mat;
	var	z_mat, draw_edge;
	var	box[2], b0[3], b1[3], rot_box[8], rot_box_pos[3];
	var	i, j, k, theta;
	var	line[5][2];
	var	pick, draw_flag = ON;

	if( SelectPolygons() == 0 )
	{
		Warning();
		if( MESSAGE == ON )
			Message( error_msg3 );
		return;
	}

	Menu_temp1 = 360;
	Menu_temp2 = OFF;	/*	生成方向 ON:右（？)	*/
	Menu_temp3 = OFF;

	SelectBox( box );
	PushMenu();
	Status_org = Status_title;
	MenuPosition( Menu( " 動作設定(&S)",
			"設定変更   ",		InputSetup,
			"-----------",		SEPARATE_MENU,
			"ﾏｯﾋﾟﾝｸﾞ中止	ESC",	MenuQuit
		), Menu_Title );
	ClearStatus();
	Status_title[0] = "【円筒ﾏｯﾋﾟﾝｸﾞ中】";
	Status_title[UVmax_pos] = "UV最大値:"+tostring( UVmax, 4 )+" ";
	Status_title[Deg_pos] = "角度:"+ tostring( Menu_temp1 , 5)+" ";
	if( Menu_temp2 == ON )
		Status_title[Houkou_pos] = "ﾏｯﾋﾟﾝｸﾞ方向:右回り";
	else
		Status_title[Houkou_pos] = "ﾏｯﾋﾟﾝｸﾞ方向:左回り";
	DrawStatus();

	quit_flag = FALSE;

//	DegMenu( Menu_temp1 );
//	HoukouMenu( Menu_temp2 );

	axis[0] = Cursor();
	cur = Cursor();
	pick = cur;
	vers = 1;
	input_flag = OFF;
	while( TRUE )
	{
		if( draw_flag == ON )
			DrawLine( axis[0], cur );
		key = WaitEvent();
		if( key )
			input_flag = ON;
		if( (( ShiftStat() & 1 ) == 1))
		{
			cur = Cursor();
			TrimCursor(axis[0], MouseWindow());
			Cursor( cur );
		}
		else
			cur = Cursor();
		key = KeyCode();
		if( input_flag | key == INPUT_KEY )
		{
			axis[vers] = cur;
			if( cur == axis[0] )
			{
				Warning();
				if( MESSAGE == ON )
					Message( "円筒マッピングの軸を指定してください" );
			}
			else
				break;
		}
		else if( key == ESC | key == BS | quit_flag == TRUE )
		{
			PopandClear( OVERWRITE );
			return;
		}
		else
			temp = KeyProcess( key );
		if( ((key!=0 ) & (key != INPUT_KEY)) | ( pick != Cursor()) )
		{
			draw_flag = ON;
			DrawLine( axis[0], pick );
		}
		else
			draw_flag = OFF;
		pick = Cursor();
		input_flag = OFF;
	}
	axis_vec = unit(Position( axis[1] )- Position( axis[0] ));
	rot_mat = CalRotMatrix( axis_vec, vector( 0,0,1 ));
	move_mat = move(UNIT_MATRIX, Position( axis[0] )*(-1));
	z_mat = Matrix( 1,0,0, 0,1,0, 0,0,0, 0,0,0 );
	temp_mat = move_mat*rot_mat*z_mat;

	Position( box[0], b0 );
	Position( box[1], b1 );
	rot_box[0] = Vertex( b0[0], b0[1], b0[2] )*temp_mat;
	rot_box[1] = Vertex( b0[0], b0[1], b1[2] )*temp_mat;
	rot_box[2] = Vertex( b0[0], b1[1], b0[2] )*temp_mat;
	rot_box[3] = Vertex( b0[0], b1[1], b1[2] )*temp_mat;
	rot_box[4] = Vertex( b1[0], b0[1], b0[2] )*temp_mat;
	rot_box[5] = Vertex( b1[0], b0[1], b1[2] )*temp_mat;
	rot_box[6] = Vertex( b1[0], b1[1], b0[2] )*temp_mat;
	rot_box[7] = Vertex( b1[0], b1[1], b1[2] )*temp_mat;

	temp_pos = Vertex( 0,0,0 );
	len = VertexLength2( rot_box[0],temp_pos );
	for( i = 1; i< 8; i++ )
	{
		temp = VertexLength2( rot_box[i], temp_pos );
		if( temp > len )
			len = temp;
	}
	len = sqrt( len );

	mae_vec = vector( 1,0,0 );
	yoko_vec = vector( 0, 1, 0 );

	temp_mat = scale(UNIT_MATRIX, len, len, len )/rot_mat;
	draw_edge = DrawEquilateral( 30,axis[0], axis[0]+(mae_vec*temp_mat),
					axis[0]+(yoko_vec*temp_mat), OFF );
	draw_edge += DrawEquilateral( 30,axis[1], axis[1]+(mae_vec*temp_mat),
					axis[1]+(yoko_vec*temp_mat), OFF );
	DrawEdge( draw_edge );

	draw_flag = ON;
	cur = Cursor();
	pick = cur;
	temp_mat = rot_mat*z_mat/rot_mat;
	input_flag = OFF;
	while( TRUE )
	{
		if( draw_flag == ON )
		{
			if( abs((Position(axis[0])-Position(cur))*axis_vec) > 0.00001 )
			{
				temp_vec = unit((Position(cur)-Position(axis[0]))*temp_mat);
				box[0] = axis[0]+temp_vec*len;
				box[1] = axis[1]+temp_vec*len;
				temp_vec = unit(axis_vec*temp_vec);
				if( Menu_temp2 == OFF )
					temp_vec = temp_vec*(-1);
				i = ViewWidth(MouseWindow())/10;
				line[0][0] = axis[0];
				line[0][1] = box[0];
				line[1][0] = axis[1];
				line[1][1] = box[1];
				line[2][0] = box[0];
				line[2][1] = box[1];
				line[3][0] = box[0];
				line[3][1] = box[0]+temp_vec*i+axis_vec*5;
				line[4][0] = box[1];
				line[4][1] = box[1]+temp_vec*i+axis_vec*5;
				for( i = 0; i< 5;i++ )
					DrawLine( line[i][0], line[i][1] );
			}
		}
		key = WaitEvent();
		if( key )
			input_flag = ON;
		if(( ShiftStat() & 1 ) == 1 )
		{
			cur = Cursor();
			TrimCursor(axis[0], MouseWindow());
			Cursor( cur );
		}
		else
			cur = Cursor();
		key = KeyCode();
		if ( input_flag | key == INPUT_KEY )
		{
			if( abs((Position(axis[0])-Position(cur))*axis_vec) > 0.00001 )
				break;
		}
		else if( key == ESC | key == BS | quit_flag == TRUE )
		{
			PopandClear( OVERWRITE );
			return;
		}
		else
			temp = KeyProcess( key );
		if( ((key!=0 ) & (key != INPUT_KEY))|( pick != Cursor())|(Menu_temp3 == ON))
		{
			draw_flag = ON;
			if( abs((Position(axis[0])-Position(pick))*axis_vec) > 0.00001 )
			{
				for( i = 0; i< 5;i++ )
					DrawLine( line[i][0], line[i][1] );
			}
		}
		else
			draw_flag = OFF;
		Menu_temp3 = OFF;
		pick = Cursor();
		input_flag = OFF;
	}
	Clear( OVERWRITE );

	mae_vec = unit(Position( cur )- Position( axis[0] ))*rot_mat*z_mat;
	temp_mat = move_mat*rot_mat*CalRotMatrix(mae_vec, vector(1,0,0));
	if( Menu_temp2 == OFF )
		temp_mat = temp_mat*scale( UNIT_MATRIX, 1, -1, 1 );
	len = abs( Position( axis[1] ) - Position( axis[0] ));
	PolyLoadInit();
	while( PolyLoad() )
	{
		vers = PolyVertexs();
		ty = PolyType();
		j = 0;
		k = 0;
		for( i = 0; i< vers;i++ )
		{
			temp_vec = Position(PolyGetVertex( i ))*temp_mat;
			if( vx( temp_vec ) > 0 )
			{
				if( vy(temp_vec) < 0 )
					j++;
				else
					k++;
			}
		}
		for( i = 0; i< vers;i++ )
		{
			temp = PolyGetVertex( i );
			temp_pos = Position( temp );
			temp_vec = Vector( temp );
			temp = temp*temp_mat;
			Position( temp, b0 );
			v = (0.0 + b0[2])*UVmax/len;
			if(( b0[0] == 0 ) & ( b0[1] == 0 ))
				theta = 0;
			else
				theta = atan2( b0[1], b0[0] );
			if( theta < (0) )
				u = (theta*180/PI+360)*UVmax/Menu_temp1;
			else
				u = (theta*180/PI)*UVmax/Menu_temp1;
			if( ( j != 0 ) & ( k != 0 ))
			{
				if( j>= k )
				{
					if( theta >= (0) )
						u = (theta*180/PI+360)*UVmax/Menu_temp1;
				}
				else
				{
					if( theta < (0) )
						u = (theta*180/PI)*UVmax/Menu_temp1;
				}
			}
			PolySetVertex( Vertex( temp_pos, temp_vec, u, v ), i );
		}
		if( ty == POLY_SIMPLE )
			ty = POLY_UV;
		else if( ty == POLY_SHADE )
			ty = POLY_UVSHADE;
		PolyType( ty );
		PolySave();
	}
	UpdateObject();
	PopMenu();
	Status_title = Status_org;
	DrawStatus();
}
