//
//	３面図の移動
//
function NormalMove( key )
{
	var const	MOVE_LENGTH = 100;
	var	win_type;
	var	vec_x, vec_y, vec_z, vec_ue, vec_yoko;
	var	temp;
	var	chuou;
	var	pos[3];
	var draw, draw_ue, draw_yoko;

	draw_ue = 0;
	draw_yoko = 0;

	vec_x = vector( 1, 0, 0 );
	vec_y = vector( 0, 1, 0 );
	vec_z = vector( 0, 0, 1 );

	win_type = MouseWindow();
	if( win_type == WIN_XY )
	{
		vec_ue = vec_y;
		vec_yoko = vec_x;
		draw_ue = WIN_XY | WIN_YZ;
		draw_yoko   = WIN_XY | WIN_ZX;
	}
	else if( win_type == WIN_YZ )
	{
		vec_ue = vec_z;
		vec_yoko = vec_y;
		draw_ue = WIN_YZ | WIN_ZX;
		draw_yoko   = WIN_YZ | WIN_XY;
	}
	else if( win_type == WIN_ZX )
	{
		vec_ue = vec_z;
		vec_yoko = vec_x;
		draw_ue = WIN_ZX | WIN_YZ;
		draw_yoko   = WIN_ZX | WIN_XY;
	}
	else
	{
		vec_ue = vector( 0, 0, 0);
		vec_yoko = vec_ue;
	}

	if( zoomsize < 0 )
		temp = 1.0 / abs(zoomsize) *MOVE_LENGTH;
	else
		temp = zoomsize *MOVE_LENGTH;

	if( key == KEY_UP )
	{
		chuou = Center() + vec_ue*temp;
		draw = draw_ue;
	}
	else if( key == KEY_DOWN )
	{
		chuou = Center() - vec_ue*temp;
		draw = draw_ue;
	}
	else if( key == KEY_RIGHT )
	{
		chuou = Center() + vec_yoko*temp;
		draw = draw_yoko;
	}
	else if( key == KEY_LEFT )
	{
		chuou = Center() - vec_yoko*temp;
		draw = draw_yoko;
	}

	if (draw_ue != 0) {
		Center( chuou );
		Update( CLEAR, draw );
	}

}

function private	DefaultPosition()
{
	var	mat_vec[4];

	getvector( pers_mat, mat_vec );
	pers_mat = Matrix( mat_vec[0], mat_vec[1], mat_vec[2], vector(2000,0,0));
	SetPers( pers_mat );
}

function DefaultPers()
{
	pers_mat = rot( UNIT_MATRIX, AXISY, 20 )*rot( UNIT_MATRIX, AXISZ, 150 )
			*move(UNIT_MATRIX, 2000,0,0 );
	SetPers( pers_mat );
}

function	PersRot( dx, dy, dz )
{
	var temp[2], temp_vec, temp_mat, temp2_mat;
	var temp_sel;

	if( pers_rotation_flag == FALSE )
	{
		temp_sel = Select();
		SelectAll( TRUE );
		SelectBox( temp );
		Select( temp_sel );
		pers_rotation_vec = (Position(temp[0])+Position(temp[1]))*scale(UNIT_MATRIX, 0.5,0.5,0.5 ));
		pers_rotation_flag = TRUE;
	}
	temp_vec = pers_rotation_vec*pers_mat;
	temp_mat = move( UNIT_MATRIX, -temp_vec );
	temp2_mat = move( UNIT_MATRIX, temp_vec );
	if( dx != 0 )
		pers_mat = pers_mat*temp_mat*rot(UNIT_MATRIX, AXISX, dx )*temp2_mat;
	if( dy != 0 )
		pers_mat = pers_mat*temp_mat*rot(UNIT_MATRIX, AXISY, dy )*temp2_mat;
	if( dz != 0 )
		pers_mat = pers_mat*temp_mat*rot(UNIT_MATRIX, AXISZ, dz )*temp2_mat;
	SetPers( pers_mat );
}

function	PersMove( dx, dy, dz )
{
	pers_mat = pers_mat*move( UNIT_MATRIX, dx, dy, dz );
	SetPers( pers_mat );
}


var private rend_back_color, rend_options, rend_star;
rend_back_color = 0;
rend_options = "";
rend_star = 1;
//
//	レンダリング
//
function Rend()
{
	var i;
	var	obj, sel ;
	var	rend ;
	var r, g, b;

	r = 0.0;
	g = 0.0;
	b = 0.0;
	if (rend_back_color & 2) r = 1.0;
	if (rend_back_color & 4) g = 1.0;
	if (rend_back_color & 1) b = 1.0;

	if ( MACHINE == "PC98" )
		rend = "rend386 /c512" ;
	else if ( MACHINE == "WINDOWS" )
		rend = "rendwin /c320x240";
	else
		rend = "rend /c512" ;

	if (rend_star) {
		rend = rend + " -i2000";
	}

	WriteFrame( pers_mat, model_dir+"_temp.frm", "_temp", vector(r,g,b) );

	var flag = FILE_OVERWRITE;
	for (i = 0; i < Attrs(); i++) {
		if (AttrPolygons(i) > 0) {
			AttrWriteFile(model_dir+"_temp.atr", i, flag);
			flag = FILE_APPEND;
		}
	}

	sel = Select();
	SelectAll( TRUE );
	obj = ObjAppend( "_temp", model_dir+"_temp.suf" );
	WriteFile( obj );
	ObjDelete( obj );
	Select( sel );
	delete( model_dir+"_temp.ind" );
	fprint( model_dir+"_temp.ind", "-n -g "+model_dir+"_temp.frm "
		+model_dir+"_temp.atr "+model_dir+"_temp.suf "+ rend_options );
	Child( rend + " @"+model_dir+"_temp.ind" );
}

function Fxga()
{
	var i;
	var	obj, sel ;

	var flag = FILE_OVERWRITE;
	for (i = 0; i < Attrs(); i++) {
		if (AttrPolygons(i) > 0) {
			AttrWriteFile( model_dir+"_temp.atr", i, flag);
			flag = FILE_APPEND;
		}
	}
	sel = Select();
	SelectAll( TRUE );
	obj = ObjAppend( "_temp", model_dir+"_temp.suf" );
	WriteFile( obj );
	ObjDelete( obj );
	Select( sel );
	delete( model_dir+"_temp.ind" );
	fprint( model_dir+"_temp.ind", "-fxga "+model_dir+"_temp.atr "+model_dir+"_temp.suf" );
	Child( model_dir+"suf2agc @"+model_dir+"_temp.ind" );
}

function RendSetup()
{
	var msg[8], smsg[2];
	msg = {"黒", "青","赤","紫","緑","水","黄","白" };
	smsg = {"なし", "あり"};

	DlogOpen("作画設定", 5);
	DlogSelect( 0, "背景色          ", msg, rend_back_color );
	DlogSelect( 2, "背景星          ", smsg, rend_star );
	DlogString( 4, "特殊オプション  ", rend_options, 20 );
	if (DlogWait()) {
		rend_back_color = DlogAnswer(0);
		rend_star = DlogAnswer(2);
		rend_options = DlogAnswer(4);
	}
}


//
//	キーが押された時の処理
//
function KeyProcess( key )
{
	var	temp;

	if ( key == 'v' )
		_Vertex();
	else if( key == 'V' )
		BoxNearVertex();
	else if( key == 'V'-'A'+1 ) /* CTRL+V */
		_Vertex2();
	else if( key == KEY_CLR )
	{
		temp = InputVertex( cur );
		if( temp == TRUE )
			Cursor( cur );
	}
	else if( key == 'f' )
		FixVertex();
	else if ( key == KEY_HOME )
		DefaultPers();
	else if ( key == KEY_NUMPAD_PLUS )
		DefaultPosition();
	else if ( key == KEY_NUMPAD_7 )
		PersMove( -50, 0, 0 );
	else if ( key == KEY_NUMPAD_9 )
		PersMove( 50, 0, 0 );
	else if ( key == KEY_UP )
	{
		if( ShiftStat() & 2 )
			NormalMove( key );
		else
			PersMove( 0, 0, -50 );
	}
	else if ( key == KEY_DOWN )
	{
		if( ShiftStat() & 2 )
			NormalMove( key );
		else
			PersMove( 0, 0, 50 );
	}
	else if ( key == KEY_RIGHT )
	{
		if( ShiftStat() & 2 )
			NormalMove( key );
		else
			PersMove( 0, 50, 0 );
	}
	else if ( key == KEY_LEFT )
	{
		if( ShiftStat() & 2 )
			NormalMove( key );
		else
			PersMove( 0, -50, 0 );
	}
	else if ( key == KEY_NUMPAD_2 )
		PersRot( 0, 10, 0 );
	else if ( key == KEY_NUMPAD_8 )
		PersRot( 0, -10, 0 );
	else if ( key == KEY_NUMPAD_6 )
		PersRot( 0, 0, -10 );
	else if ( key == KEY_NUMPAD_4 )
		PersRot( 0, 0, 10 );
	else if ( key == KEY_NUMPAD_3 )
		PersRot( 10, 0, 0 );
	else if ( key == KEY_NUMPAD_1 )
		PersRot( -10, 0, 0 );
	FlushKey();
}
