#include	<shinobi.h>
#include	<Ninja.h>
#include	"Nj.h"
#include	"Menu.h"

#define	MENU_TOP_X	2
#define	MENU_TOP_Y	4
#define	PERI_WAIT	3


static int			MenuCurY	= 0;
static int			PeriWaitCnt	= 0;

void ExecMenu( TMenuInfo *pMenuInfo, int MenuInfoMax )
{
	int					i;
	TMenuInfo			*pMI;
	PDS_PERIPHERAL		*per;
	
	
	per		= pdGetPeripheral( PDD_PORT_A0 );
	if ( PeriWaitCnt == 0 ) {
		if ( per->on & PDD_DGT_KU ) {
			MenuCurY--;
			if ( MenuCurY < 0 ) {
				MenuCurY	= MenuInfoMax - 1;
			}
		}
		if ( per->on & PDD_DGT_KD ) {
			MenuCurY++;
			if ( MenuCurY >= MenuInfoMax ) {
				MenuCurY	= 0;
			}
		}
	}
	
	pMI		= pMenuInfo;
	for ( i = 0; i < MenuInfoMax; i++ ) {
		//	カーソル位置の色を変える
		if ( i == MenuCurY ) {
			njPrintColor( 0xffff0000 );
		}
		else {
			njPrintColor( 0xffffffff );
		}
#if DEBUG		
		//	メニュー項目名の表示
		njPrint( NJM_LOCATION( MENU_TOP_X, MENU_TOP_Y + i ), "%s", pMI->pStr );
#endif		
		//	メニュー項目の変更
		if ( PeriWaitCnt == 0 && i == MenuCurY ) {
			switch ( pMI->Type ) {
			case MENU_TYPE_INT : {
				//	整数型メニュー項目
				TMenuItemInt			*pMIInt		= (TMenuItemInt *)pMI->pMenuItem;
				
				if ( per->on & PDD_DGT_KR ) {
					pMIInt->No	+= pMIInt->Step;
					if ( pMIInt->No > pMIInt->Max ) {
						pMIInt->No	= pMIInt->Min;
					}
				}
				if ( per->on & PDD_DGT_KL ) {
					pMIInt->No	-= pMIInt->Step;
					if ( pMIInt->No < pMIInt->Min ) {
						pMIInt->No	= pMIInt->Max;
					}
				}
				break;
			}
			case MENU_TYPE_FLOAT : {
				//	Float型メニュー項目
				TMenuItemFloat			*pMIFloat	= (TMenuItemFloat *)pMI->pMenuItem;
				
				if ( per->on & PDD_DGT_KR ) {
					pMIFloat->No	+= pMIFloat->Step;
					if ( pMIFloat->No > pMIFloat->Max ) {
						pMIFloat->No	= pMIFloat->Min;
					}
				}
				if ( per->on & PDD_DGT_KL ) {
					pMIFloat->No	-= pMIFloat->Step;
					if ( pMIFloat->No < pMIFloat->Min ) {
						pMIFloat->No	= pMIFloat->Max;
					}
				}
				break;
			}
			}
		}
		
		//	メニュー項目の表示
		switch ( pMI->Type ) {
		case MENU_TYPE_INT : {
			//	整数型メニュー項目
			TMenuItemInt			*pMIInt		= (TMenuItemInt *)pMI->pMenuItem;
#if DEBUG		
			njPrint( NJM_LOCATION( MENU_TOP_X + 20, MENU_TOP_Y + i ), "%d", pMIInt->No );
#endif
			break;
		}
		case MENU_TYPE_FLOAT : {
			//	Float型メニュー項目
			TMenuItemFloat			*pMIFloat	= (TMenuItemFloat *)pMI->pMenuItem;
#if DEBUG		
			njPrint( NJM_LOCATION( MENU_TOP_X + 20, MENU_TOP_Y + i ), "%g", pMIFloat->No );
#endif
			break;
		}
		}
		
		pMI++;
	}
	
	PeriWaitCnt++;
	if ( PeriWaitCnt >= PERI_WAIT ) {
		PeriWaitCnt		= 0;
	}
	
	
	
	njPrintColor( 0xffffffff );
}
