var private const	Tosha_pos = 1;
//
//	ポリゴンの作成
//
function CreatePolygon()
{
	var	key, vers, i;
	var	prev, cur0, cur1, cur2, temp, temp2;
	var	pick, draw_flag = ON;
	var	pos[3];
	var	tosha_mode = OFF;
	var	prev_change, prev_sel;

	PushMenu();
	Status_org = Status_title;
	ClearStatus();
	MenuPosition( Menu( " 動作設定(&S)",
			"自動平面投射	C",	ToshaChange, Check_Toshaflag,
			"------------",		SEPARATE_MENU,
			"作成中止    	ESC",	MenuQuit
		), Menu_Title );

	Status_title[0] = "【多角形作成中】";
	if( tosha_flag == ON )
		Status_title[Tosha_pos] = "平面投射:自動";
	else
		Status_title[Tosha_pos] = "平面投射:手動";
	DrawStatus();

	quit_flag = FALSE;

	prev_sel = Select();
	prev = Cursor();
	cur0 = prev;
	cur = prev ;
	pick = cur;
	fix_vertex = cur;
	PolySetVertex( cur, 0 );
	vers = 0 ;
	input_flag = OFF;
	prev_change = -1;
	while( TRUE )
	{
		if( draw_flag == ON )
		{
			DrawLine( cur, prev );
			if( vers > 0 )
				DrawLine( cur, cur0 );
		}
		key = WaitEvent();
		if( key )
			input_flag = ON;
		if( ( ShiftStat() & 1 ) == 1 )
		{
			cur = Cursor();
			TrimCursor( fix_vertex, MouseWindow());
			Cursor( cur );
		}
		else
			cur = Cursor();
		if((tosha_mode == ON ) & ( tosha_flag == ON ) & ( pick != cur ))
		{
			cur = PolyPlane( cur, MouseWindow(),
					pos[0], pos[1], pos[2] );
			Cursor( cur );
		}
		key = KeyCode();
		if ( input_flag | key == INPUT_KEY )
		{
			if (( cur == prev ) | ( cur == PolyGetVertex( 0 )))
			{
				vers++;
				break ;
			}
			else
			{
				if(( tosha_mode == ON ) & ( tosha_flag == ON ))
				{
					temp = unit((Position(cur)-Position(pos[0]))*(Position(pos[0])-Position(pos[1])));
					temp2 = unit((Position(PolyPlane(cur, MouseWindow(), pos[0], pos[1], pos[2]))-Position(pos[0]))*(Position(pos[0])-Position(pos[1])));
				}
				else
				{
					temp = vector(0,0,1);
					temp2 = temp;
				}
				if( abs(temp.temp2) < 0.999 )
				{
					Warning();
					Message( "点が平面上にありません\n" );
				}
				else
				{
					Cursor( cur );
					if( cur != pick )
					{
						if( vers > 0 )
						{
							DrawLine( pick, prev );
							DrawLine( cur, prev );
							DrawLine( pick, cur );
						}
						else
							DrawLine( pick, cur );
					}
					vers ++ ;
					PolySetVertex( cur, vers );
					prev = cur;
					fix_vertex = cur;
					if( vers == (MAXVERTEX-1) )
					{
						Warning();
						if( MESSAGE == ON )
							Message( "一つの面にこれ以上の点を作ることができません\n"
								+"すでに確定された点で面を作成します" );
						vers++;
						break;
					}
					if(( vers >= 2 ) & ( tosha_mode == OFF ))
					{
						pos[0] = PolyGetVertex( 0 );
						pos[1] = PolyGetVertex( 1 );
						temp = Position( pos[1] )- Position( pos[0] );
						if( abs(temp*(Position(cur)- Position(pos[0]))) > 0.00001 )
						{
							pos[2] = cur;
							tosha_mode = ON;
						}
					}
				}
			}
		}
		else if( key == ESC | quit_flag == TRUE )
		{
			PopandClear( OVERWRITE );
			return;
		}
		else if ( key == BS )
		{
			if ( vers == 0 )
			{
				PopandClear( OVERWRITE );
				return ;
			}
			else
			{
				if( cur == prev )
				{
					prev_change = vers -1;
					vers --;
				}
				else
				{
					cur = PolyGetVertex( vers );
					prev_change = vers-1;
					vers --;
				}
				if( vers < 2 )
					tosha_mode = OFF;
			}
		}
		else if( ( key == 'p' ) & ( tosha_mode == ON ))
		{
			cur = PolyPlane( cur, MouseWindow(),
					pos[0], pos[1], pos[2] );
			Cursor( cur );
		}
		else if( key == 'c' )
			ToshaChange();
		else
			KeyProcess( key );
		if( ((key!=0 ) & (key != INPUT_KEY)) | ( pick != Cursor()) )
		{
			draw_flag = ON;
			DrawLine( pick, prev );
			if(( prev_change > 0 ) | ( Cursor() != cur ))
			{
				Cursor( cur );
				DrawLine( cur, PolyGetVertex( prev_change ));
			}
			if( vers == 0 )
			{
				if( prev_change >= 0 )
					DrawLine( pick, cur0 );
			}
			else if( vers == 1 )
			{
				if( pick != prev )
					DrawLine( pick, cur0 );
			}
			else
				DrawLine( pick, cur0 );
		}
		else
			draw_flag = OFF;
		if( prev_change >= 0 )
		{
			prev = PolyGetVertex( prev_change );
			fix_vertex = prev;
		}
		prev_change = -1;
		pick = Cursor();
		input_flag = OFF;
	}
	Clear( OVERWRITE );
	if ( vers >= 3 )
	{
		PolyVertexs( vers );
		PolyType( POLY_SIMPLE );
		PolyAttr( AttrCurrent() );
		PolyObj( ObjCurrent() );
		PolyAppend( TRUE );
		if( SelectPolygons(prev_sel) != 0 )
		{
			SelectAllFalse();
			Select(prev_sel^Select());
		}
		ViewCursor( OFF );
		DrawCurrent( TRUE );
		ViewCursor( ON );
		UpdateObject();
	}
	else
	{
		Warning();
		if( MESSAGE == ON )
			Message( "３点以上点が指定されていないので、\n"
				+"面が生成できませんでした" );
	}
	PopMenu();
	pers_rotation_flag = FALSE;
	Status_title = Status_org;
	DrawStatus();
}

//
//	長方形の登録
//
function private AppendRectangle( v1, v2, v3, v4 )
{
	PolySetVertex( v1, 0 );
	PolySetVertex( v2, 1 );
	PolySetVertex( v3, 2 );
	PolySetVertex( v4, 3 );
	PolyAppend( TRUE );
	ViewCursor( OFF );
	DrawCurrent( TRUE );
	ViewCursor( ON );
}

//
//	直方体・長方形の作成
//
function CreateRectangle()
{
	var	key ;
	var	prev, temp;
	var	i, a, b, c ;
	var	ver[8], pos[2][3] ;
	var	pick, draw_flag = ON;
	var	prev_sel;

	PushMenu();
	Status_org = Status_title;
	ClearStatus();
	MenuPosition( Menu( " 動作設定(&S)",
			"作成中止	ESC",	MenuQuit
		), Menu_Title );

	Status_title[0] = "【直方体作成中】";
	DrawStatus();

	quit_flag = FALSE;
	prev_sel = Select();

	prev = Cursor();
	cur = prev ;
	pick = cur;
	fix_vertex = cur;
	input_flag = OFF;
	while( TRUE )
	{
		if( draw_flag == ON )
			DrawBox( cur, prev );
		key = WaitEvent();
		if( key )
			input_flag = ON;
		if( (( ShiftStat() & 1 ) == 1))
		{
			cur = Cursor();
			TrimCursor(fix_vertex, MouseWindow());
			Cursor( cur );
		}
		else
			cur = Cursor();
		key = KeyCode();
		if ( input_flag | key == INPUT_KEY )
		{
			if( cur == prev )
			{
				Warning();
				if( MESSAGE == ON )
					Message( error_msg4 );
			}
			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;
			DrawBox( pick, prev );
		}
		else
			draw_flag = OFF;
		pick = Cursor();
		input_flag = OFF;
	}
	Clear( OVERWRITE );
	if( SelectPolygons( prev_sel ) != 0 )
	{
		SelectAllFalse();
		SelectAll( FALSE );
	}

	Position( prev, pos[0] );
	Position( cur, pos[1] );
	for( i = 0 ; i < 8 ; i += 1 )
	{
		a = ( ( i & 1 ) == 0 );
		b = ( ( i & 2 ) == 0 );
		c = ( ( i & 4 ) == 0 );
		ver[i] = Vertex( pos[a][0], pos[b][1], pos[c][2] );
	}
	PolyVertexs( 4 );
	PolyType( POLY_SIMPLE );
	PolyAttr( AttrCurrent() );
	PolyObj( ObjCurrent() );
	if ( pos[0][0] != pos[1][0] & pos[0][1] != pos[1][1] )
	{
		AppendRectangle( ver[0], ver[1], ver[3], ver[2] );
		if ( pos[0][2] != pos[1][2] )
			AppendRectangle( ver[4], ver[5], ver[7], ver[6] );
	}
	if ( pos[0][0] != pos[1][0] & pos[0][2] != pos[1][2] )
	{
		AppendRectangle( ver[0], ver[1], ver[5], ver[4] );
		if ( pos[0][1] != pos[1][1] )
			AppendRectangle( ver[2], ver[3], ver[7], ver[6] );
	}
	if ( pos[0][1] != pos[1][1] & pos[0][2] != pos[1][2] )
	{
		AppendRectangle( ver[0], ver[2], ver[6], ver[4] );
		if ( pos[0][0] != pos[1][0] )
			AppendRectangle( ver[1], ver[3], ver[7], ver[5] );
	}
	UpdateObject();
	PopMenu();
	pers_rotation_flag = FALSE;
	Status_title = Status_org;
	DrawStatus();
}

//
//	角錐の作成
//
function CreatePyramid()
{
	var vers, i, j;
	var ver[2];
	var all_edge, rinkaku_edge;

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

	all_edge = EdgeSelect();
	rinkaku_edge = EdgeSelectCount( all_edge, 1, 1 );

	SelectAllFalse();
	SelectAll( FALSE );

	cur = Cursor();
	j = Edges( rinkaku_edge );
	ViewCursor( OFF );
	for( i = 0; i< j; i++ )
	{
		EdgeGetVertex( rinkaku_edge, i, ver );
		PolyVertexs( 3 );
		PolySetVertex( cur, 0 );
		PolySetVertex( ver[0], 1 );
		PolySetVertex( ver[1], 2 );
		PolyType( POLY_SIMPLE );
		PolyAttr( AttrCurrent() );
		PolyObj( ObjCurrent() );
		PolyAppend( TRUE );
		DrawCurrent( TRUE );
	}
	ViewCursor( ON );
	UpdateObject();
	pers_rotation_flag = FALSE;
}

//
//	面立ち上げ機能
//
function CreateStandUpPolygons()
{
	var i, j, key, vers, temp;
	var org, ver[2];
	var vect;
	var all_edge, rinkaku_edge;
	var pick, draw_flag = ON;

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

	PushMenu();
	Status_org = Status_title;
	ClearStatus();
	MenuPosition( Menu( " 動作設定(&S)",
			"作成中止	ESC",	MenuQuit
		), Menu_Title );
	Status_title[0] = "【面立ち上げ中】";
	DrawStatus();

	quit_flag = FALSE;

	org = Cursor();
	cur = org;
	pick = cur;
	fix_vertex = cur;
	input_flag = OFF;
	while( TRUE )
	{
		if( draw_flag == ON )
			DrawLine( org, cur );
		key = WaitEvent();
		if( key )
			input_flag = ON;
		if( (( ShiftStat() & 1 ) == 1))
		{
			cur = Cursor();
			TrimCursor(fix_vertex, MouseWindow());
			Cursor( cur );
		}
		else
			cur = Cursor();
		key = KeyCode();
		if( input_flag | key == INPUT_KEY )
		{
			if( cur == org )
			{
				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( org, pick );
		}
		else
			draw_flag = OFF;
		pick = Cursor();
		input_flag = OFF;
	}
	Clear( OVERWRITE );

	all_edge = EdgeSelect();
	rinkaku_edge = EdgeSelectCount( all_edge, 1, 1 );

	vect = Position( cur ) - Position( org );
	Select( _CopyPolygons( vect ) );

	j = Edges( rinkaku_edge );
	ViewCursor( OFF );
	for( i = 0; i< j; i++ )
	{
		EdgeGetVertex( rinkaku_edge, i, ver );
		PolyVertexs( 4 );
		PolySetVertex( ver[0], 0 );
		PolySetVertex( ver[1], 1 );
		PolySetVertex( ver[1]+vect, 2 );
		PolySetVertex( ver[0]+vect, 3 );
		PolyType( POLY_SIMPLE );
		PolyAttr( AttrCurrent() );
		PolyObj( ObjCurrent() );
		PolyAppend( TRUE );
		DrawCurrent( TRUE );
	}
	ViewCursor( ON );
	UpdateObject();
	pers_rotation_flag = FALSE;
	PopMenu();
	Status_title = Status_org;
	DrawStatus();
}

//
//	面作成
//
function Polygon()
{
	ClearStatus();
	Status_title[0] = "【ﾓｰﾄﾞ:多角形作成】";
	DrawStatus();
	InputEvent( CreatePolygon );
	KeyEvent( CreatePolygon, INPUT_KEY );
}

//
//	正多角形作成
//
function EquilateralPolygon()
{
	ClearStatus();
	Status_title[0] = "【ﾓｰﾄﾞ:正多角形作成】";
	DrawStatus();
	InputEvent( CreateEquilateralPolygon );
	KeyEvent( CreateEquilateralPolygon, INPUT_KEY );
}

//
//	直方体作成
//
function Rectangle()
{
	ClearStatus();
	Status_title[0] = "【ﾓｰﾄﾞ:直方体作成】";
	DrawStatus();
	InputEvent( CreateRectangle );
	KeyEvent( CreateRectangle, INPUT_KEY );
}

//
//	球作成
//
function Ball()
{
	ClearStatus();
	Status_title[0] = "【ﾓｰﾄﾞ:球作成】";
	DrawStatus();
	InputEvent( CreateBall );
	KeyEvent( CreateBall, INPUT_KEY );
}

//
//	回転体作成
//
function RotationBody()
{
	ClearStatus();
	Status_title[0] = "【ﾓｰﾄﾞ:回転体作成】";
	DrawStatus();
	InputEvent( CreateRotationBody );
	KeyEvent( CreateRotationBody, INPUT_KEY );
}

//
//	角錐作成
//
function Pyramid()
{
	ClearStatus();
	Status_title[0] = "【ﾓｰﾄﾞ:角錐作成】";
	DrawStatus();
	InputEvent( CreatePyramid );
	KeyEvent( CreatePyramid, INPUT_KEY );
}

//
//	面立ち上げ
//
function StandUp()
{
	ClearStatus();
	Status_title[0] = "【ﾓｰﾄﾞ:面立ち上げ】";
	DrawStatus();
	InputEvent( CreateStandUpPolygons );
	KeyEvent( CreateStandUpPolygons, INPUT_KEY );
}

//
//	チューブを作成
//
function MakeTube()
{
	if( SelectPolygons() < 2 )
	{
		Warning();
		if( MESSAGE == ON )
			Message( "接続する面は２面以上選択してください" );
	}
	else
	{
		Tube( Select());
		pers_rotation_flag = FALSE;
		UpdateObject();
	}
}

MenuPosition(
	Menu( " 作成(&C)",
		"多角形(&P)	P",		Polygon,
		"------------------",		SEPARATE_MENU,
		"正多角形(&E)	E",		EquilateralPolygon,
		"------------------",		SEPARATE_MENU,
		"直方体(&H)	H",		Rectangle,
		"球(&G)	G",		Ball,
		"回転体(&R)	R",		RotationBody,
		"------------------",		SEPARATE_MENU,
		"角錐(&K)	K",		Pyramid,SelectCheck,
		"面立ち上げ(&U)	U",		StandUp,SelectCheck,
		"------------------",		SEPARATE_MENU,
		"ﾁｭｰﾌﾞを作成(&T)	ｼﾌﾄ+T",	MakeTube,SelectCheck
	), Menu_create
);
