//===========================================================================//
// File:	GOSvideo.cpp																			  //
// Project: MUNGA           Brick: Video Renderer Manager	                 //
// Contents: Interface specification Video Renderer Manager       			  //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 01/11/95  GAC  Initial coding.                                            //
// 07/11/95  KEO  Modify video resource format.                              //
//---------------------------------------------------------------------------//
// Copyright (C) 1995, Virtual World Entertainment, Inc.                     //
// All Rights reserved worldwide.                                            //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL.              //
//===========================================================================//

#include "AdeptHeaders.hpp"

#include "Controls.hpp"
#include "Joystick.hpp"
#include "Tool.hpp"

#define BUTTON(entry) {#entry, ControlsManager::VirtualButtonGroupID, entry}

struct {
	const char* mappingName;
	int mappingGroup;
	int mappingIndex;
}
	Control_Mappings[]=
{
	{"JoystickX",	ControlsManager::JoystickAxisGroupID,	Joystick::XAxis},
	{"JoystickY",	ControlsManager::JoystickAxisGroupID,	Joystick::YAxis},
	{"Throttle",	ControlsManager::JoystickAxisGroupID,	Joystick::ThrottleAxis},
	{"Rudder",		ControlsManager::JoystickAxisGroupID,	Joystick::RudderAxis},
	{"JoystickHat",	ControlsManager::JoystickAxisGroupID,	Joystick::HatAxis},

	{"JoystickHatL",	ControlsManager::JoystickHatID,	0},
	{"JoystickHatR",	ControlsManager::JoystickHatID,	1},
	{"JoystickHatU",	ControlsManager::JoystickHatID,	2},
	{"JoystickHatD",	ControlsManager::JoystickHatID,	3},

	{"MousePosX",	ControlsManager::MousePosXID,	0},
	{"MousePosY",	ControlsManager::MousePosYID,	0},
	{"MouseDeltaX",		ControlsManager::MouseDeltaXID,		0},
	{"MouseDeltaY",		ControlsManager::MouseDeltaYID,		0},
	{"MouseWheelDelta",ControlsManager::MouseWheelDeltaID,	0},

	{"MouseLeftButton",		ControlsManager::MouseButtonGroupID,	0},
	{"MouseRightButton",		ControlsManager::MouseButtonGroupID,	1},
	{"MouseMiddleButton",	ControlsManager::MouseButtonGroupID,	2},
	{"MouseX1Button",		ControlsManager::MouseButtonGroupID,	3},
	{"MouseX2Button",	ControlsManager::MouseButtonGroupID,	4},

	{"Keyboard",	ControlsManager::SystemKeyboardID,		0},

	BUTTON(KEY_ESCAPE),
	BUTTON(KEY_1),
	BUTTON(KEY_2),
	BUTTON(KEY_3),
	BUTTON(KEY_4),
	BUTTON(KEY_5),
	BUTTON(KEY_6),
	BUTTON(KEY_7),
	BUTTON(KEY_8),
	BUTTON(KEY_9),
	BUTTON(KEY_0),
	BUTTON(KEY_MINUS),
	BUTTON(KEY_EQUALS),
	BUTTON(KEY_BACK),
	BUTTON(KEY_TAB),
	BUTTON(KEY_Q),
	BUTTON(KEY_W),
	BUTTON(KEY_E),
	BUTTON(KEY_R),
	BUTTON(KEY_T),
	BUTTON(KEY_Y),
	BUTTON(KEY_U),
	BUTTON(KEY_I),
	BUTTON(KEY_O),
	BUTTON(KEY_P),
	BUTTON(KEY_LBRACKET),
	BUTTON(KEY_RBRACKET),
	BUTTON(KEY_RETURN),
	BUTTON(KEY_A),
	BUTTON(KEY_S),
	BUTTON(KEY_D),
	BUTTON(KEY_F),
	BUTTON(KEY_G),
	BUTTON(KEY_H),
	BUTTON(KEY_J),
	BUTTON(KEY_K),
	BUTTON(KEY_L),
	BUTTON(KEY_SEMICOLON),
	BUTTON(KEY_APOSTROPHE),
	BUTTON(KEY_GRAVE),
	BUTTON(KEY_BACKSLASH),
	BUTTON(KEY_Z),
	BUTTON(KEY_X),
	BUTTON(KEY_C),
	BUTTON(KEY_V),
	BUTTON(KEY_B),
	BUTTON(KEY_N),
	BUTTON(KEY_M),
	BUTTON(KEY_COMMA),
	BUTTON(KEY_PERIOD),
	BUTTON(KEY_SLASH),
	BUTTON(KEY_MULTIPLY),
	BUTTON(KEY_SPACE),
	BUTTON(KEY_CAPITAL),
	BUTTON(KEY_F1),
	BUTTON(KEY_F2),
	BUTTON(KEY_F3),
	BUTTON(KEY_F4),
	BUTTON(KEY_F5),
	BUTTON(KEY_F6),
	BUTTON(KEY_F7),
	BUTTON(KEY_F8),
	BUTTON(KEY_F9),
	BUTTON(KEY_F10),
	BUTTON(KEY_NUMLOCK),
	BUTTON(KEY_PAUSE),
	BUTTON(KEY_NUMPAD7),
	BUTTON(KEY_NUMPAD8),
	BUTTON(KEY_NUMPAD9),
	BUTTON(KEY_SUBTRACT),
	BUTTON(KEY_NUMPAD4),
	BUTTON(KEY_NUMPAD5),
	BUTTON(KEY_NUMPAD6),
	BUTTON(KEY_ADD),
	BUTTON(KEY_NUMPAD1),
	BUTTON(KEY_NUMPAD2),
	BUTTON(KEY_NUMPAD3),
	BUTTON(KEY_NUMPAD0),
	BUTTON(KEY_DECIMAL),
	BUTTON(KEY_F11),
	BUTTON(KEY_F12),
	BUTTON(KEY_F13),
	BUTTON(KEY_F14),
	BUTTON(KEY_F15),
	BUTTON(KEY_KANA),
	BUTTON(KEY_CONVERT),
	BUTTON(KEY_NOCONVERT),
	BUTTON(KEY_YEN),
	BUTTON(KEY_NUMPADEQUALS),
	BUTTON(KEY_CIRCUMFLEX),
	BUTTON(KEY_AT),
	BUTTON(KEY_COLON),
	BUTTON(KEY_UNDERLINE),
	BUTTON(KEY_KANJI),
	BUTTON(KEY_STOP),
	BUTTON(KEY_AX),
	BUTTON(KEY_UNLABELED),
	BUTTON(KEY_NUMPADENTER),
	BUTTON(KEY_NUMPADCOMMA),
	BUTTON(KEY_DIVIDE),
	BUTTON(KEY_SYSRQ),
	BUTTON(KEY_HOME),
	BUTTON(KEY_UP),
	BUTTON(KEY_PRIOR),
	BUTTON(KEY_LEFT),
	BUTTON(KEY_RIGHT),
	BUTTON(KEY_END),
	BUTTON(KEY_DOWN),
	BUTTON(KEY_NEXT),
	BUTTON(KEY_INSERT),
	BUTTON(KEY_DELETE),
	BUTTON(KEY_LWIN),
	BUTTON(KEY_RWIN),
	BUTTON(KEY_APPS)
};

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
bool
	Tool::FindControlGroupAndElement(
		int *group,
		int *element,
		const char *control_name
	)
{
	Check_Pointer(group);
	Check_Pointer(element);
	Check_Pointer(control_name);

	//
	//---------------------------------------
	// Check against the named elements first
	//---------------------------------------
	//
	for (int i=0; i<ELEMENTS(Control_Mappings); ++i)
	{
		if (!_stricmp(control_name, Control_Mappings[i].mappingName))
		{
			*group = Control_Mappings[i].mappingGroup;
			*element = Control_Mappings[i].mappingIndex;
			return true;
		}
	}

	//
	//----------------------------------
	// See if this is a joystick element
	//----------------------------------
	//
	if (!_strnicmp(control_name, "Joystick", 8))
	{
		//
		//---------------
		// Check for axis
		//---------------
		//
		if (!_strnicmp(control_name+8, "Axis", 4))
		{
			*group = ControlsManager::JoystickAxisGroupID;
			*element = atoi(control_name+12) - 1;
		}

		//
		//-----------------
		// Check for button
		//-----------------
		//
		else if (!_strnicmp(control_name+8, "Button", 6))
		{
			*group = ControlsManager::JoystickButtonGroupID;
			*element = atoi(control_name+14) - 1;
		}

		//
		//----------------------------------------
		// This is a bad mappings, so return false
		//----------------------------------------
		//
		else
		{
			return false;
		}
	}

	//
	//--------------
	// Its a bad one
	//--------------
	//
	else
	{
		return false;
	}

	return true;
}
