/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/src/sgx/input/input_controller.cpp

*/

#include "sgx/core/core.hpp"
#include "sgx/input/input.hpp"


const CSGXInputManager::CControllerJoypad *CSGXInputManager::getController(const tController controller) {
    if (controller >= eFirst && controller <= eFourth) {
        // Map logical button to physical one and re-call ourselves
        return getController(tController(m_Mapping[controller-eFirst]));
    }

    if (controller >= ePhysical0 && controller <= ePhysical3) {
		return m_pController[controller];
    }
	sgxError("There is no such controller as %d", controller);
	return NULL;
}

const CSGXInputManager::CControlButton &CSGXInputManager::getControlButton(const tController controller, const tControl button) {
	//TODO:GetPhysicalController
    if (controller >= eFirst && controller <= eFourth) {
        // Map logical button to physical one and re-call ourselves
        return getControlButton(tController(m_Mapping[controller-eFirst]), button);
    }

    if (controller >= ePhysical0 && controller <= ePhysical3) {
		if (button >= 0 && button < SGX_MAXINP_BUTTONS) {
			return m_pController[controller]->m_Buttons[button];	
		}
    }

	static CControlButton nullButton;
	sgxError("There is no such button as %d on controller %d", button, controller);
    return nullButton;
}

const CSGXInputManager::CControlStick &CSGXInputManager::getControlStick(const tController controller, const tControl stick) {

	//TODO:GetPhysicalController
    if (controller >= eFirst && controller <= eFourth) {
        // Map logical button to physical one and re-call ourselves
        return getControlStick(tController(m_Mapping[controller-eFirst]), stick);
    }

    if (controller >= ePhysical0 && controller <= ePhysical3) {
		if (stick < eLastControl-eFirstStick) {
			return m_pController[controller]->m_Sticks[stick];	
		}
    }

	static CControlStick nullStick;
	sgxError("There is no such stick axis as %d on controller %d", stick, controller);
    return nullStick;
}


const CSGXInputManager::CKeyboardKey  &		
CSGXInputManager::getControlKeyboardKey(const tKey key) {
	if (m_pDeviceKeyboard) {
		return m_pDeviceKeyboard->m_Key[key];
	}

	static CKeyboardKey nullKey;
	sgxError("There is no such key  %d", key);
    return nullKey;
}
