//---------------------------------------------------------------------------
//	Ninja Sample
//	COPYRIGHT (C) SEGA ENTERPRISES, LTD.
//	
//	CameraEx:	Manual Demo
//---------------------------------------------------------------------------
#include	<shinobi.h>
#include	"nj.h"
#include	"Common.h"


static float			frame_no;
static NJS_POINT3		CameraPos;

enum {
	MODE_NEWCAMERA	= 0,
	MODE_NEWCAMERA_ABS,
	MODE_NEWCAMERA_LOOKAT,
	MODE_NEWCAMERA_LOOKAT_ABS,
	MODE_MAX
};

static int		Mode;
static char		*ModeStr[MODE_MAX]	= {
					"Standard",
					"Abs",
					"LookAt",
					"LookAt Abs"
				};



void ManualDemo( int State )
{
	if ( State == STATE_ATTACH ) {
		nuLoadTexture( CHARA_TEXLIST );
		
		njPushMatrix( &_nj_unit_matrix_ );
		
		frame_no	= 0.0f;
		
		Mode		= MODE_NEWCAMERA;
		
		CameraPos.x		= 0.0f;
		CameraPos.y		= 0.0f;
		CameraPos.z		= 0.0f;
		
		njInitCamera( UFOMAN_CAMERA );
		
	}
	else if ( State == STATE_DETACH ) {
		njPopMatrix( 1 );
		
		njReleaseTexture( CHARA_TEXLIST );
	}
	else {
		const PDS_PERIPHERAL		*per;
		per		= pdGetPeripheral(PDD_PORT_A0);
		
		if(per->press & PDD_DGT_TX){
#if DEBUG
			njPrint( NJM_LOCATION(10,13), "BUTTON X" );
#endif
			Mode++;
			if ( Mode >= MODE_MAX ) {
				Mode	= 0;
			}
			CameraPos.x		= 0.0f;
			CameraPos.y		= 0.0f;
			CameraPos.z		= 0.0f;
		}
#if DEBUG
		njPrint( NJM_LOCATION(5,5), "MODE:%s", ModeStr[Mode] );
#endif		
		//	ライトの設定
		#if 0
		njCnkSetEasyLight( 0.0f, -0.7f, -0.7f );
		njCnkSetEasyLightIntensity( 1.0f, 0.0f );
		njCnkSetEasyLightColor( 255, 255, 255 );
		#else
		njCnkSetSimpleLight( 0.0f, -0.7f, -0.7f );
		njCnkSetSimpleLightIntensity( 1.0f, 0.0f );
		njCnkSetSimpleLightColor( 255, 255, 255 );
		#endif
		
		if ( Mode == MODE_NEWCAMERA || Mode == MODE_NEWCAMERA_ABS ) {
			//	カメラの設定
			
			if(per->on & PDD_DGT_ST){
#if DEBUG
				njPrint( NJM_LOCATION(10,8), "BUTTON START" );
#endif
				njUnitMatrix( NULL );
			}
			
			{
				Float				TransX;
				Float				TransY;
				Float				TransZ;
				
				TransX		= 0.0f;
				TransY		= 0.0f;
				TransZ		= 0.0f;
				
				TransX	= per->x1 * 0.01f;
				TransZ	= per->y1 * 0.01f;
				
#if DEBUG
				njPrint( NJM_LOCATION(10,9), "x=%d,y=%d", per->x1, per->y1 );
#endif				
				if(per->on & PDD_DGT_TY){
#if DEBUG
					njPrint( NJM_LOCATION(10,10), "BUTTON Y" );
#endif
					TransY	= 128.0f * 0.01f;
				}
				if(per->on & PDD_DGT_TA){
#if DEBUG
					njPrint( NJM_LOCATION(10,11), "BUTTON A" );
#endif
					TransY	= -128.0f * 0.01f;
				}
				
				switch( Mode ) {
				case	MODE_NEWCAMERA :
					njTranslateCameraEx( TransX, TransY, TransZ );
					break;
				case	MODE_NEWCAMERA_ABS :
					njTranslateAbsoluteCameraEx( TransX, TransY, TransZ );
					break;
				default:
					break;
				}
			}
			
			
			if(per->on & PDD_DGT_KL){
#if DEBUG
				njPrint( NJM_LOCATION(20,10), "DIGITAL DIR LEFT" );
#endif
				njRotateYCameraEx( 0x80 );
			}
			if(per->on & PDD_DGT_KR){
#if DEBUG
				njPrint( NJM_LOCATION(20,11), "DIGITAL DIR RIGHT" );
#endif
				njRotateYCameraEx( -0x80 );
			}
			if(per->on & PDD_DGT_KU){
#if DEBUG
				njPrint( NJM_LOCATION(20,12), "DIGITAL DIR UP" );
#endif
				njRotateXCameraEx( 0x80 );
			}
			if(per->on & PDD_DGT_KD){
#if DEBUG
				njPrint( NJM_LOCATION(20,13), "DIGITAL DIR DOWN" );
#endif
				njRotateXCameraEx( -0x80 );
			}
			if(per->on & PDD_DGT_TL){
#if DEBUG
				njPrint( NJM_LOCATION(20,14), "BUTTON L" );
#endif
				njRotateZCameraEx( 0x80 );
			}
			if(per->on & PDD_DGT_TR){
#if DEBUG
				njPrint( NJM_LOCATION(20,15), "BUTTON R" );
#endif
				njRotateZCameraEx( -0x80 );
			}
			
			{
				NJS_POINT3		pos;
				njGetCameraExPosition( &pos );
#if DEBUG
				njPrint( NJM_LOCATION(5,6), "%g,%g,%g", pos.x, pos.y, pos.z );
#endif
				njSetCameraExPosition( &pos );
			}
		}
		else if ( Mode == MODE_NEWCAMERA_LOOKAT_ABS ) {
			//	カメラの設定
			
			
			if(per->on & PDD_DGT_ST){
#if DEBUG
				njPrint( NJM_LOCATION(10,8), "BUTTON START" );
#endif				
				CameraPos.x		= 0.0f;
				CameraPos.y		= 0.0f;
				CameraPos.z		= 0.0f;
			}
			
			{
				Float				TransX;
				Float				TransY;
				Float				TransZ;
				
				TransX		= 0.0f;
				TransY		= 0.0f;
				TransZ		= 0.0f;
				
				TransX	= per->x1 * 0.01f;
				TransZ	= per->y1 * 0.01f;
				
#if DEBUG
				njPrint( NJM_LOCATION(10,9), "x=%d,y=%d", per->x1, per->y1 );
#endif				
				if(per->on & PDD_DGT_TY){
#if DEBUG
					njPrint( NJM_LOCATION(10,10), "BUTTON Y" );
#endif
					TransY	= 128.0f * 0.01f;
				}
				if(per->on & PDD_DGT_TA){
#if DEBUG
					njPrint( NJM_LOCATION(10,11), "BUTTON A" );
#endif
					TransY	= -128.0f * 0.01f;
				}
				
				CameraPos.x	+= TransX;
				CameraPos.y	+= TransY;
				CameraPos.z	+= TransZ;
			}
			
			njUnitMatrix( NULL );
			{
				static NJS_POINT3			Center	= { CHARA_POS_X, CHARA_POS_Y, CHARA_POS_Z };
				static NJS_POINT3			Up		= { 0.0f, 1.0f, 0.0f };
				njCameraExLookAt( &CameraPos, &Center, &Up );
			}
			
			{
				NJS_POINT3		pos;
				njGetCameraExPosition( &pos );
#if DEBUG
				njPrint( NJM_LOCATION(5,6), "%g,%g,%g", pos.x, pos.y, pos.z );
#endif
			}
		}
		else if ( Mode == MODE_NEWCAMERA_LOOKAT ) {
			//	カメラの設定
			
			if(per->on & PDD_DGT_ST){
#if DEBUG
				njPrint( NJM_LOCATION(10,8), "BUTTON START" );
#endif				
				njUnitMatrix( NULL );
			}
			
			{
				Float				TransX;
				Float				TransY;
				Float				TransZ;
				
				TransX		= 0.0f;
				TransY		= 0.0f;
				TransZ		= 0.0f;
				
				TransX	= per->x1 * 0.01f;
				TransZ	= per->y1 * 0.01f;
				
#if DEBUG
				njPrint( NJM_LOCATION(10,9), "x=%d,y=%d", per->x1, per->y1 );
#endif				
				if(per->on & PDD_DGT_TY){
#if DEBUG
					njPrint( NJM_LOCATION(10,10), "BUTTON Y" );
#endif
					TransY	= 128.0f * 0.01f;
				}
				if(per->on & PDD_DGT_TA){
#if DEBUG
					njPrint( NJM_LOCATION(10,11), "BUTTON A" );
#endif
					TransY	= -128.0f * 0.01f;
				}
				
				njTranslateCameraEx( TransX, TransY, TransZ );
			}
			
			{
				NJS_POINT3					pos;
				static NJS_POINT3			Center	= { CHARA_POS_X, CHARA_POS_Y, CHARA_POS_Z };
				static NJS_POINT3			Up		= { 0.0f, 1.0f, 0.0f };
				
				njGetCameraExPosition( &pos );
				
#if DEBUG
				njPrint( NJM_LOCATION(5,6), "%g,%g,%g", pos.x, pos.y, pos.z );
#endif				
				njUnitMatrix( NULL );
				
				njCameraExLookAt( &pos, &Center, &Up );
			}
		}
		
		//	描画
		DrawWorldAxis();
		
		njSetTexture( CHARA_TEXLIST );
		
		
		njPushMatrix( NULL );
		njTranslate( NULL, CHARA_POS_X, CHARA_POS_Y, CHARA_POS_Z );
		njRotateY( NULL, 0x8000 );
		njScale( NULL, 0.8f, 0.8f, 0.8f );
		njCnkSimpleDrawMotion( CHARA_OBJECT , CHARA_MOTION , frame_no );
		njPopMatrix( 1 );
		
		
		
		frame_no	+= 1.0f;
		
		if( frame_no >= CHARA_MOTION->nbFrame - 1.0f ) {
			frame_no	= 0.0f;
		}
	}
}

