/*----------------------------------------------------------------------------------*/
#define _KM_USE_VERTEX_MACRO_L4_
#include <shinobi.h>
#include <kamui2.h>
#include <km2util.h>
#include <string.h>
#include <stdlib.h>
#include "dispstrex.h"
#include "command.h"
#include "km2kamuix.h"
#include "cursor.h"
#include "icon.h"

CURSOR csr;
static PDS_PERIPHERAL *per;
/*----------------------------------------------------------------------------------*/
void InitCursor( KMINT32 nPort )
{
	KMSTRIPCONTEXT CTX;

	kmCreateTextureSurface( &csr.sTexSurface,  128,128 , KM_TEXTURE_VQ|KM_TEXTURE_ARGB4444);
	kmLoadTexture( &csr.sTexSurface, cursor );

	InitStripContext03( &CTX, &csr.sStripHead, &csr.sTexSurface);
	kmChangeStripListType( &csr.sStripHead, KM_TRANS_POLYGON );
	kmChangeStripBlendingMode( &csr.sStripHead, KM_IMAGE_PARAM1, KM_SRCALPHA, KM_INVSRCALPHA);
	kmChangeStripUseAlpha( &csr.sStripHead,  KM_IMAGE_PARAM1, KM_TRUE);
	kmChangeStripSuperSampleMode( &csr.sStripHead,  KM_IMAGE_PARAM1, KM_TRUE);

	csr.bDisplay = TRUE;
	csr.nXpos    = 320;
	csr.nYpos    = 240;
	csr.nNo      = 0;

	csr.sRect.nXmin =   0;
	csr.sRect.nYmin =   0;
	csr.sRect.nXmax = 640;
	csr.sRect.nYmax = 480;

	per = pdGetPeripheral( nPort );
}
/*----------------------------------------------------------------------------------*/
void ExitCursor( void )
{
	kmFreeTexture( &csr.sTexSurface );
}
/*----------------------------------------------------------------------------------*/
void CursorServer( void )
{
	KMINT32 x = csr.nXpos;
	KMINT32 y = csr.nYpos;

	csr.nXpos += (per->x1>>4);
	csr.nYpos += (per->y1>>4);

	if (x!=csr.nXpos || y!=csr.nYpos) {
		SetCursorPos( csr.nXpos, csr.nYpos );
		Invalidate();
	}

	if (per->press & PDD_DGT_TY) {
		SetCursorNo( (csr.nNo+1)%9 );
		Invalidate();
	}
}
/*----------------------------------------------------------------------------------*/
void DrawCursor( PKMVERTEXBUFFDESC pBufferDesc )
{
	float x,y,u,v;

	kmxxGetCurrentPtr( pBufferDesc );
	kmxxStartStrip( pBufferDesc, &csr.sStripHead );

	x = (float)csr.nXpos - 8.f;
	y = (float)csr.nYpos - 8.f;
	u = (float)(csr.nNo%4) * 0.25f;
	v = (float)(csr.nNo/4) * 0.25f;

	kmxxSetVertex_3(KM_VERTEXPARAM_NORMAL		, x     , y      , 1000.f, u      ,v      , 0xffffffff, 0);
	kmxxSetVertex_3(KM_VERTEXPARAM_NORMAL		, x     , y+32.f , 1000.f, u      ,v+0.25f, 0xffffffff, 0);
	kmxxSetVertex_3(KM_VERTEXPARAM_NORMAL		, x+32.f, y      , 1000.f, u+0.25f,v      , 0xffffffff, 0);
	kmxxSetVertex_3(KM_VERTEXPARAM_ENDOFSTRIP	, x+32.f, y+32.f , 1000.f, u+0.25f,v+0.25f, 0xffffffff, 0);

	kmEndStrip( pBufferDesc );
	kmxxReleaseCurrentPtr( pBufferDesc );
}
/*----------------------------------------------------------------------------------*/
void SetCursorPos( KMINT32 nXpos, KMINT32 nYpos )
{
	if (nXpos < csr.sRect.nXmin) nXpos = csr.sRect.nXmin;
	if (nXpos > csr.sRect.nXmax) nXpos = csr.sRect.nXmax;
	if (nYpos < csr.sRect.nYmin) nYpos = csr.sRect.nYmin;
	if (nYpos > csr.sRect.nYmax) nYpos = csr.sRect.nYmax;

	csr.nXpos = nXpos;
	csr.nYpos = nYpos;
}
/*----------------------------------------------------------------------------------*/
void GetCursorPos( PKMINT32 pXpos, PKMINT32 pYpos )
{
	*pXpos = csr.nXpos;
	*pYpos = csr.nYpos;
}
/*----------------------------------------------------------------------------------*/
void SetCursorNo( KMINT32 nNo )
{
	csr.nNo = nNo;
}
/*----------------------------------------------------------------------------------*/