////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 RWS Inc, All Rights Reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of version 2 of the GNU General Public License as published by
// the Free Software Foundation
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
//////////////////////////////////////////////////////////////////////////////
//
// InputEvent.CPP
// 
// History:
//		01/16/97 JMI	Started.
//
//		01/18/97	JMI	Added rspClear*InputEvents() definitions.  They were
//							already declared in the .h file.
//
//////////////////////////////////////////////////////////////////////////////
//
// API to handle input events as one entity.  Merely an interface to the
// separate Blue APIs fro input events.
//
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// C Headers -- Must be included before RSPiX.h b/c RSPiX utilizes SHMalloc.
//////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// RSPiX Headers.
// If PATHS_IN_INCLUDES macro is defined, we can utilize relative
// paths to a header file.  In this case we generally go off of our
// RSPiX root directory.  System.h MUST be included before this macro
// is evaluated.  System.h is the header that, based on the current
// platform (or more so in this case on the compiler), defines 
// PATHS_IN_INCLUDES.  Blue.h includes system.h so you can include that
// instead.
///////////////////////////////////////////////////////////////////////////////
#include "Blue.h"

#ifdef PATHS_IN_INCLUDES
	#include "GREEN/InputEvent/InputEvent.h"
#else
	#include "InputEvent.h"
#endif

//////////////////////////////////////////////////////////////////////////////
// Module specific macros.
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Module specific typedefs.
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Exported (extern) variables.
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Module specific (static) variables / Instantiate class statics.
//////////////////////////////////////////////////////////////////////////////

															// a valid input event.

static RInputEvent	ms_ieKey			=			// Next key input event.
	{
	RInputEvent::Key,
	};

static int16_t			ms_sKeyEvent	= FALSE;	// TRUE, if ms_ieKey contains
															// a valid input event.

static MapleInputState	ms_MapleInputStates[4] = {};	// CURRENT MapleInput state.

//////////////////////////////////////////////////////////////////////////////
// Module specific (static) protos.
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Functions.
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
//
// Gets the next input event.
//
//////////////////////////////////////////////////////////////////////////////
int16_t rspGetNextInputEvent(	// Returns 1 if there is an event, 0 if none.
	RInputEvent*	pie)			// Out: Filled with input event type and details.
	{
	int16_t	sGotEvent	= 1;	// Assume we have one.

	// If no current key event . . .
	if (ms_sKeyEvent == FALSE)
		{
		// Get one, if avaliable.

		//todo input?
		/*if (rspGetKey(
			&ms_ieKey.lKey, 
			&ms_ieKey.lTime) == 1)
			{
			ms_sKeyEvent	= TRUE;
			}
		}*/

		
		
		if (ms_sKeyEvent != FALSE)
			{
			*pie	= ms_ieKey;
			// Invalidate current key event.
			ms_sKeyEvent	= FALSE;
			}
		else
		{
			// No event.
			sGotEvent = 0;
		}

	}
				
	return sGotEvent;
	}


///////////////////////////////////////////////////////////////////////////////
// MapleInput State stuff. Added for menu control.
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// Clears current MapleInput state.
///////////////////////////////////////////////////////////////////////////////
void ClearMapleInputState(int8_t controllerId)
{
	MapleInputState *ms_MapleInputCurrentState = &ms_MapleInputStates[controllerId];
	for (int i = 0; i < MapleInputButtons; i++)
		ms_MapleInputCurrentState->ButtonState[i] = MapleInputState::Release;
	for (int i = 0; i < MapleInputAxes; i++)
		ms_MapleInputCurrentState->AxisState[i] = MapleInputState::Release;
}

///////////////////////////////////////////////////////////////////////////////
// Updates joystick and returns current button state.
///////////////////////////////////////////////////////////////////////////////
int16_t GetMapleInputState(MapleInputState* mis, int8_t controllerId)
{
	//todo??
	rspUpdateJoy(controllerId);
	return GetMapleInputStateNoUpdate(mis, controllerId);
}
int16_t GetLastMapleInputState(MapleInputState* mis, int8_t controllerId)
{
	MapleInputState *ms_MapleInputCurrentState = &ms_MapleInputStates[controllerId];
	if (mis)
	{
		memcpy(mis, ms_MapleInputCurrentState, sizeof(MapleInputState));
		return 1;
	}
	return 0;
}
int16_t GetMapleInputStateNoUpdate(MapleInputState* mis, int8_t controllerId)
{
	// Get current input state from Blue
	U32 u32Buttons = 0;
	U32 u32Axes = 0;
	MapleInputState *ms_MapleInputCurrentState = &ms_MapleInputStates[controllerId];

	rspGetJoyState(controllerId, &u32Buttons, &u32Axes);

	for (int i = 0; i < MapleInputButtons; i++)
		mis->ButtonState[i] = MapleInputState::None;
	for (int i = 0; i < MapleInputAxes; i++)
		mis->AxisState[i] = MapleInputState::None;

	// This is pretty clunky. Again.
	if ((u32Axes & RSP_JOY_X_POS) && (ms_MapleInputCurrentState->AxisState[0] == MapleInputState::Release))
		mis->AxisState[0] = ms_MapleInputCurrentState->AxisState[0] = MapleInputState::Press;
	else if ((u32Axes & RSP_JOY_X_POS) && (ms_MapleInputCurrentState->AxisState[0] == MapleInputState::Press))
		mis->AxisState[0] = MapleInputState::None;
	else if (!(u32Axes & RSP_JOY_X_POS) && (ms_MapleInputCurrentState->AxisState[0] == MapleInputState::Press))
		mis->AxisState[0] = ms_MapleInputCurrentState->AxisState[0] = MapleInputState::Release;
	else
		mis->AxisState[0] = MapleInputState::None;

	if ((u32Axes & RSP_JOY_X_NEG) && (ms_MapleInputCurrentState->AxisState[1] == MapleInputState::Release))
		mis->AxisState[1] = ms_MapleInputCurrentState->AxisState[1] = MapleInputState::Press;
	else if ((u32Axes & RSP_JOY_X_NEG) && (ms_MapleInputCurrentState->AxisState[1] == MapleInputState::Press))
		mis->AxisState[1] = MapleInputState::None;
	else if (!(u32Axes & RSP_JOY_X_NEG) && (ms_MapleInputCurrentState->AxisState[1] == MapleInputState::Press))
		mis->AxisState[1] = ms_MapleInputCurrentState->AxisState[1] = MapleInputState::Release;
	else
		mis->AxisState[1] = MapleInputState::None;

	if ((u32Axes & RSP_JOY_Y_POS) && (ms_MapleInputCurrentState->AxisState[2] == MapleInputState::Release))
		mis->AxisState[2] = ms_MapleInputCurrentState->AxisState[2] = MapleInputState::Press;
	else if ((u32Axes & RSP_JOY_Y_POS) && (ms_MapleInputCurrentState->AxisState[2] == MapleInputState::Press))
		mis->AxisState[2] = MapleInputState::None;
	else if (!(u32Axes & RSP_JOY_Y_POS) && (ms_MapleInputCurrentState->AxisState[2] == MapleInputState::Press))
		mis->AxisState[2] = ms_MapleInputCurrentState->AxisState[2] = MapleInputState::Release;
	else
		mis->AxisState[2] = MapleInputState::None;

	if ((u32Axes & RSP_JOY_Y_NEG) && (ms_MapleInputCurrentState->AxisState[3] == MapleInputState::Release))
		mis->AxisState[3] = ms_MapleInputCurrentState->AxisState[3] = MapleInputState::Press;
	else if ((u32Axes & RSP_JOY_Y_NEG) && (ms_MapleInputCurrentState->AxisState[3] == MapleInputState::Press))
		mis->AxisState[3] = MapleInputState::None;
	else if (!(u32Axes & RSP_JOY_Y_NEG) && (ms_MapleInputCurrentState->AxisState[3] == MapleInputState::Press))
		mis->AxisState[3] = ms_MapleInputCurrentState->AxisState[3] = MapleInputState::Release;
	else
		mis->AxisState[3] = MapleInputState::None;

	if ((u32Axes & RSP_JOY_Z_POS) && (ms_MapleInputCurrentState->AxisState[4] == MapleInputState::Release))
		mis->AxisState[4] = ms_MapleInputCurrentState->AxisState[4] = MapleInputState::Press;
	else if ((u32Axes & RSP_JOY_Z_POS) && (ms_MapleInputCurrentState->AxisState[4] == MapleInputState::Press))
		mis->AxisState[4] = MapleInputState::None;
	else if (!(u32Axes & RSP_JOY_Z_POS) && (ms_MapleInputCurrentState->AxisState[4] == MapleInputState::Press))
		mis->AxisState[4] = ms_MapleInputCurrentState->AxisState[4] = MapleInputState::Release;
	else
		mis->AxisState[4] = MapleInputState::None;

	if ((u32Axes & RSP_JOY_Z_NEG) && (ms_MapleInputCurrentState->AxisState[5] == MapleInputState::Release))
		mis->AxisState[5] = ms_MapleInputCurrentState->AxisState[5] = MapleInputState::Press;
	else if ((u32Axes & RSP_JOY_Z_NEG) && (ms_MapleInputCurrentState->AxisState[5] == MapleInputState::Press))
		mis->AxisState[5] = MapleInputState::None;
	else if (!(u32Axes & RSP_JOY_Z_NEG) && (ms_MapleInputCurrentState->AxisState[5] == MapleInputState::Press))
		mis->AxisState[5] = ms_MapleInputCurrentState->AxisState[5] = MapleInputState::Release;
	else
		mis->AxisState[5] = MapleInputState::None;

	if ((u32Axes & RSP_JOY_W_POS) && (ms_MapleInputCurrentState->AxisState[6] == MapleInputState::Release))
		mis->AxisState[6] = ms_MapleInputCurrentState->AxisState[6] = MapleInputState::Press;
	else if ((u32Axes & RSP_JOY_W_POS) && (ms_MapleInputCurrentState->AxisState[6] == MapleInputState::Press))
		mis->AxisState[6] = MapleInputState::None;
	else if (!(u32Axes & RSP_JOY_W_POS) && (ms_MapleInputCurrentState->AxisState[6] == MapleInputState::Press))
		mis->AxisState[6] = ms_MapleInputCurrentState->AxisState[6] = MapleInputState::Release;
	else
		mis->AxisState[6] = MapleInputState::None;

	if ((u32Axes & RSP_JOY_W_NEG) && (ms_MapleInputCurrentState->AxisState[7] == MapleInputState::Release))
		mis->AxisState[7] = ms_MapleInputCurrentState->AxisState[7] = MapleInputState::Press;
	else if ((u32Axes & RSP_JOY_W_NEG) && (ms_MapleInputCurrentState->AxisState[7] == MapleInputState::Press))
		mis->AxisState[7] = MapleInputState::None;
	else if (!(u32Axes & RSP_JOY_W_NEG) && (ms_MapleInputCurrentState->AxisState[7] == MapleInputState::Press))
		mis->AxisState[7] = ms_MapleInputCurrentState->AxisState[7] = MapleInputState::Release;
	else
		mis->AxisState[7] = MapleInputState::None;

	if ((u32Axes & RSP_JOY_U_POS) && (ms_MapleInputCurrentState->AxisState[8] == MapleInputState::Release))
		mis->AxisState[8] = ms_MapleInputCurrentState->AxisState[8] = MapleInputState::Press;
	else if ((u32Axes & RSP_JOY_U_POS) && (ms_MapleInputCurrentState->AxisState[8] == MapleInputState::Press))
		mis->AxisState[8] = MapleInputState::None;
	else if (!(u32Axes & RSP_JOY_U_POS) && (ms_MapleInputCurrentState->AxisState[8] == MapleInputState::Press))
		mis->AxisState[8] = ms_MapleInputCurrentState->AxisState[8] = MapleInputState::Release;
	else
		mis->AxisState[8] = MapleInputState::None;


	if ((u32Axes & RSP_JOY_U_NEG) && (ms_MapleInputCurrentState->AxisState[9] == MapleInputState::Release))
		mis->AxisState[9] = ms_MapleInputCurrentState->AxisState[9] = MapleInputState::Press;
	else if ((u32Axes & RSP_JOY_U_NEG) && (ms_MapleInputCurrentState->AxisState[9] == MapleInputState::Press))
		mis->AxisState[9] = MapleInputState::None;
	else if (!(u32Axes & RSP_JOY_U_NEG) && (ms_MapleInputCurrentState->AxisState[9] == MapleInputState::Press))
		mis->AxisState[9] = ms_MapleInputCurrentState->AxisState[9] = MapleInputState::Release;
	else
		mis->AxisState[9] = MapleInputState::None;


	if ((u32Axes & RSP_JOY_V_POS) && (ms_MapleInputCurrentState->AxisState[10] == MapleInputState::Release))
		mis->AxisState[10] = ms_MapleInputCurrentState->AxisState[10] = MapleInputState::Press;
	else if ((u32Axes & RSP_JOY_V_POS) && (ms_MapleInputCurrentState->AxisState[10] == MapleInputState::Press))
		mis->AxisState[10] = MapleInputState::None;
	else if (!(u32Axes & RSP_JOY_V_POS) && (ms_MapleInputCurrentState->AxisState[10] == MapleInputState::Press))
		mis->AxisState[10] = ms_MapleInputCurrentState->AxisState[10] = MapleInputState::Release;
	else
		mis->AxisState[10] = MapleInputState::None;

	
	if ((u32Axes & RSP_JOY_V_NEG) && (ms_MapleInputCurrentState->AxisState[11] == MapleInputState::Release))
		mis->AxisState[11] = ms_MapleInputCurrentState->AxisState[11] = MapleInputState::Press;
	else if ((u32Axes & RSP_JOY_V_NEG) && (ms_MapleInputCurrentState->AxisState[11] == MapleInputState::Press))
		mis->AxisState[11] = MapleInputState::None;
	else if (!(u32Axes & RSP_JOY_V_NEG) && (ms_MapleInputCurrentState->AxisState[11] == MapleInputState::Press))
		mis->AxisState[11] = ms_MapleInputCurrentState->AxisState[11] = MapleInputState::Release;
	else
		mis->AxisState[11] = MapleInputState::None;


	if ((u32Buttons & RSP_JOY_BUT_1) && (ms_MapleInputCurrentState->ButtonState[1] == MapleInputState::Release))
		mis->ButtonState[1] = ms_MapleInputCurrentState->ButtonState[1] = MapleInputState::Press;
	else if ((u32Buttons & RSP_JOY_BUT_1) && (ms_MapleInputCurrentState->ButtonState[1] == MapleInputState::Press))
		mis->ButtonState[1] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_1) && (ms_MapleInputCurrentState->ButtonState[1] == MapleInputState::Press))
		mis->ButtonState[1] = ms_MapleInputCurrentState->ButtonState[1] = MapleInputState::Release;
	else
		mis->ButtonState[1] = MapleInputState::None;
	if ((u32Buttons & RSP_JOY_BUT_2) && (ms_MapleInputCurrentState->ButtonState[2] == MapleInputState::Release))
		mis->ButtonState[2] = ms_MapleInputCurrentState->ButtonState[2] = MapleInputState::Press;
	else if ((u32Buttons & RSP_JOY_BUT_2) && (ms_MapleInputCurrentState->ButtonState[2] == MapleInputState::Press))
		mis->ButtonState[2] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_2) && (ms_MapleInputCurrentState->ButtonState[2] == MapleInputState::Press))
		mis->ButtonState[2] = ms_MapleInputCurrentState->ButtonState[2] = MapleInputState::Release;
	else
		mis->ButtonState[2] = MapleInputState::None;
	if ((u32Buttons & RSP_JOY_BUT_3) && (ms_MapleInputCurrentState->ButtonState[3] == MapleInputState::Release))
		mis->ButtonState[3] = ms_MapleInputCurrentState->ButtonState[3] = MapleInputState::Press;
	else if ((u32Buttons & RSP_JOY_BUT_3) && (ms_MapleInputCurrentState->ButtonState[3] == MapleInputState::Press))
		mis->ButtonState[3] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_3) && (ms_MapleInputCurrentState->ButtonState[3] == MapleInputState::Press))
		mis->ButtonState[3] = ms_MapleInputCurrentState->ButtonState[3] = MapleInputState::Release;
	else
		mis->ButtonState[3] = MapleInputState::None;
	if ((u32Buttons & RSP_JOY_BUT_4) && (ms_MapleInputCurrentState->ButtonState[4] == MapleInputState::Release))
		mis->ButtonState[4] = ms_MapleInputCurrentState->ButtonState[4] = MapleInputState::Press;
	else if ((u32Buttons & RSP_JOY_BUT_4) && (ms_MapleInputCurrentState->ButtonState[4] == MapleInputState::Press))
		mis->ButtonState[4] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_4) && (ms_MapleInputCurrentState->ButtonState[4] == MapleInputState::Press))
		mis->ButtonState[4] = ms_MapleInputCurrentState->ButtonState[4] = MapleInputState::Release;
	else
		mis->ButtonState[4] = MapleInputState::None;
	if ((u32Buttons & RSP_JOY_BUT_5) && (ms_MapleInputCurrentState->ButtonState[5] == MapleInputState::Release))
		mis->ButtonState[5] = ms_MapleInputCurrentState->ButtonState[5] = MapleInputState::Press;
	else if ((u32Buttons & RSP_JOY_BUT_5) && (ms_MapleInputCurrentState->ButtonState[5] == MapleInputState::Press))
		mis->ButtonState[5] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_5) && (ms_MapleInputCurrentState->ButtonState[5] == MapleInputState::Press))
		mis->ButtonState[5] = ms_MapleInputCurrentState->ButtonState[5] = MapleInputState::Release;
	else
		mis->ButtonState[5] = MapleInputState::None;
	if ((u32Buttons & RSP_JOY_BUT_6) && (ms_MapleInputCurrentState->ButtonState[6] == MapleInputState::Release))
		mis->ButtonState[6] = ms_MapleInputCurrentState->ButtonState[6] = MapleInputState::Press;
	else if ((u32Buttons & RSP_JOY_BUT_6) && (ms_MapleInputCurrentState->ButtonState[6] == MapleInputState::Press))
		mis->ButtonState[6] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_6) && (ms_MapleInputCurrentState->ButtonState[6] == MapleInputState::Press))
		mis->ButtonState[6] = ms_MapleInputCurrentState->ButtonState[6] = MapleInputState::Release;
	else
		mis->ButtonState[6] = MapleInputState::None;
	if ((u32Buttons & RSP_JOY_BUT_7) && (ms_MapleInputCurrentState->ButtonState[7] == MapleInputState::Release))
		mis->ButtonState[7] = ms_MapleInputCurrentState->ButtonState[7] = MapleInputState::Press;			
	else if ((u32Buttons & RSP_JOY_BUT_7) && (ms_MapleInputCurrentState->ButtonState[7] == MapleInputState::Press))
		mis->ButtonState[7] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_7) && (ms_MapleInputCurrentState->ButtonState[7] == MapleInputState::Press))
		mis->ButtonState[7] = ms_MapleInputCurrentState->ButtonState[7] = MapleInputState::Release;
	else
		mis->ButtonState[7] = MapleInputState::None;
	if ((u32Buttons & RSP_JOY_BUT_8) && (ms_MapleInputCurrentState->ButtonState[8] == MapleInputState::Release))
		mis->ButtonState[8] = ms_MapleInputCurrentState->ButtonState[8] = MapleInputState::Press;
	else if ((u32Buttons & RSP_JOY_BUT_8) && (ms_MapleInputCurrentState->ButtonState[8] == MapleInputState::Press))
		mis->ButtonState[8] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_8) && (ms_MapleInputCurrentState->ButtonState[8] == MapleInputState::Press))
		mis->ButtonState[8] = ms_MapleInputCurrentState->ButtonState[8] = MapleInputState::Release;
	else
		mis->ButtonState[8] = MapleInputState::None;
	if ((u32Buttons & RSP_JOY_BUT_9) && (ms_MapleInputCurrentState->ButtonState[9] == MapleInputState::Release))
		mis->ButtonState[9] = ms_MapleInputCurrentState->ButtonState[9] = MapleInputState::Press;
	else if ((u32Buttons & RSP_JOY_BUT_9) && (ms_MapleInputCurrentState->ButtonState[9] == MapleInputState::Press))
		mis->ButtonState[9] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_9) && (ms_MapleInputCurrentState->ButtonState[9] == MapleInputState::Press))
		mis->ButtonState[9] = ms_MapleInputCurrentState->ButtonState[9] = MapleInputState::Release;
	else
		mis->ButtonState[9] = MapleInputState::None;
	if ((u32Buttons & RSP_JOY_BUT_10) && (ms_MapleInputCurrentState->ButtonState[10] == MapleInputState::Release))
		mis->ButtonState[10] = ms_MapleInputCurrentState->ButtonState[10] = MapleInputState::Press;
	else if ((u32Buttons & RSP_JOY_BUT_10) && (ms_MapleInputCurrentState->ButtonState[10] == MapleInputState::Press))
		mis->ButtonState[10] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_10) && (ms_MapleInputCurrentState->ButtonState[10] == MapleInputState::Press))
		mis->ButtonState[10] = ms_MapleInputCurrentState->ButtonState[10] = MapleInputState::Release;
	else
		mis->ButtonState[10] = MapleInputState::None;
	if ((u32Buttons & RSP_JOY_BUT_11) && (ms_MapleInputCurrentState->ButtonState[11] == MapleInputState::Release))
		mis->ButtonState[11] = ms_MapleInputCurrentState->ButtonState[11] = MapleInputState::Press;
	else if ((u32Buttons & RSP_JOY_BUT_11) && (ms_MapleInputCurrentState->ButtonState[11] == MapleInputState::Press))
		mis->ButtonState[11] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_11) && (ms_MapleInputCurrentState->ButtonState[11] == MapleInputState::Press))
		mis->ButtonState[11] = ms_MapleInputCurrentState->ButtonState[11] = MapleInputState::Release;
	else
		mis->ButtonState[11] = MapleInputState::None;
	if ((u32Buttons & RSP_JOY_BUT_12) && (ms_MapleInputCurrentState->ButtonState[12] == MapleInputState::Release))
		mis->ButtonState[12] = ms_MapleInputCurrentState->ButtonState[12] = MapleInputState::Press;
	else if ((u32Buttons & RSP_JOY_BUT_12) && (ms_MapleInputCurrentState->ButtonState[12] == MapleInputState::Press))
		mis->ButtonState[12] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_12) && (ms_MapleInputCurrentState->ButtonState[12] == MapleInputState::Press))
		mis->ButtonState[12] = ms_MapleInputCurrentState->ButtonState[12] = MapleInputState::Release;
	else
		mis->ButtonState[12] = MapleInputState::None;
	if ((u32Buttons & RSP_JOY_BUT_13) && (ms_MapleInputCurrentState->ButtonState[13] == MapleInputState::Release))
		mis->ButtonState[13] = ms_MapleInputCurrentState->ButtonState[13] = MapleInputState::Press;
	else if ((u32Buttons & RSP_JOY_BUT_13) && (ms_MapleInputCurrentState->ButtonState[13] == MapleInputState::Press))
		mis->ButtonState[13] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_13) && (ms_MapleInputCurrentState->ButtonState[13] == MapleInputState::Press))
		mis->ButtonState[13] = ms_MapleInputCurrentState->ButtonState[13] = MapleInputState::Release;
	else
		mis->ButtonState[13] = MapleInputState::None;
	if ((u32Buttons & RSP_JOY_BUT_14) && (ms_MapleInputCurrentState->ButtonState[14] == MapleInputState::Release))
		mis->ButtonState[14] = ms_MapleInputCurrentState->ButtonState[14] = MapleInputState::Press;
	else if ((u32Buttons & RSP_JOY_BUT_14) && (ms_MapleInputCurrentState->ButtonState[14] == MapleInputState::Press))
		mis->ButtonState[14] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_14) && (ms_MapleInputCurrentState->ButtonState[14] == MapleInputState::Press))
		mis->ButtonState[14] = ms_MapleInputCurrentState->ButtonState[14] = MapleInputState::Release;
	else
		mis->ButtonState[14] = MapleInputState::None;
	if ((u32Buttons & RSP_JOY_BUT_15) && (ms_MapleInputCurrentState->ButtonState[15] == MapleInputState::Release))
		mis->ButtonState[15] = ms_MapleInputCurrentState->ButtonState[15] = MapleInputState::Press;
	else if ((u32Buttons & RSP_JOY_BUT_15) && (ms_MapleInputCurrentState->ButtonState[15] == MapleInputState::Press))
		mis->ButtonState[15] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_15) && (ms_MapleInputCurrentState->ButtonState[15] == MapleInputState::Press))
		mis->ButtonState[15] = ms_MapleInputCurrentState->ButtonState[15] = MapleInputState::Release;
	else
		mis->ButtonState[15] = MapleInputState::None;
	if ((u32Buttons & RSP_JOY_BUT_16) && (ms_MapleInputCurrentState->ButtonState[16] == MapleInputState::Release))
		mis->ButtonState[16] = ms_MapleInputCurrentState->ButtonState[16] = MapleInputState::Press;
	else if ((u32Buttons & RSP_JOY_BUT_16) && (ms_MapleInputCurrentState->ButtonState[16] == MapleInputState::Press))
		mis->ButtonState[16] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_16) && (ms_MapleInputCurrentState->ButtonState[16] == MapleInputState::Press))
		mis->ButtonState[16] = ms_MapleInputCurrentState->ButtonState[16] = MapleInputState::Release;
	else
		mis->ButtonState[16] = MapleInputState::None;
	if ((u32Buttons & RSP_JOY_BUT_17) && (ms_MapleInputCurrentState->ButtonState[17] == MapleInputState::Release))
		mis->ButtonState[17] = ms_MapleInputCurrentState->ButtonState[17] = MapleInputState::Press;
	else if ((u32Buttons & RSP_JOY_BUT_17) && (ms_MapleInputCurrentState->ButtonState[17] == MapleInputState::Press))
		mis->ButtonState[17] = MapleInputState::None;
	else if (!(u32Buttons & RSP_JOY_BUT_17) && (ms_MapleInputCurrentState->ButtonState[17] == MapleInputState::Press))
		mis->ButtonState[17] = ms_MapleInputCurrentState->ButtonState[17] = MapleInputState::Release;
	else
		mis->ButtonState[17] = MapleInputState::None;

	return 1; //!! FIXME: return 0 if controller isn't plugged in?
}

///////////////////////////////////////////////////////////////////////////////
// Checks current joystick state and returns 1 if any button is pressed.
///////////////////////////////////////////////////////////////////////////////
int16_t IsMapleInputButtonPressed(int8_t controllerId)
{
	MapleInputState mis = {};

	GetMapleInputState(&mis, controllerId);

	for (int i = 0; i < MapleInputButtons; i++)
		if (mis.ButtonState[i] == MapleInputState::Press)
			return 1;

	return 0;
}

///////////////////////////////////////////////////////////////////////////////
// EOF
///////////////////////////////////////////////////////////////////////////////
