373 lines
11 KiB
C#
373 lines
11 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Events;
|
|
|
|
public class InputControlManager : MonoBehaviour
|
|
{
|
|
//*********************//
|
|
// Public member data //
|
|
//*********************//
|
|
public bool IsUsingJoystick;
|
|
|
|
public static event UnityAction OnControllerInput;
|
|
public static event UnityAction OnKeyboardOrMouseInput;
|
|
|
|
[HideInInspector] public bool _verticalButtonDpadIsPressedDown;
|
|
[HideInInspector] public bool _horizontalButtonDpadUpIsPressedDown;
|
|
[HideInInspector] public bool upDpadVerticalButton;
|
|
[HideInInspector] public bool downDpadVericalButton;
|
|
|
|
[HideInInspector] public bool hideAllSelectedItemHighlights;
|
|
|
|
private bool GameStartedUpdate = true;
|
|
|
|
private static InputControlManager _instance;
|
|
public static InputControlManager Getinstance() { return _instance; }
|
|
|
|
public void Awake()
|
|
{
|
|
if (!_instance)
|
|
{
|
|
_instance = this;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
// Check if there is any input from a controller
|
|
if (Input.GetJoystickNames().Length > 0)
|
|
{
|
|
// Controller is connected
|
|
OnControllerInput?.Invoke();
|
|
m_State = eInputState.Controler;
|
|
GameStartedUpdate = false;
|
|
Debug.Log("Controller connected!");
|
|
}
|
|
else
|
|
{
|
|
// No controller connected
|
|
OnKeyboardOrMouseInput?.Invoke();
|
|
m_State = eInputState.MouseKeyboard;
|
|
GameStartedUpdate = false;
|
|
Debug.Log("No controller detected.");
|
|
}
|
|
}
|
|
|
|
public void CursorVisibilityUpdater()
|
|
{
|
|
if (IsUsingJoystick)
|
|
{
|
|
Cursor.visible = false;
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
}
|
|
else
|
|
{
|
|
Cursor.visible = true;
|
|
Cursor.lockState = CursorLockMode.Confined;
|
|
}
|
|
}
|
|
|
|
public void CursorVisibility(bool cursorVisible)
|
|
{
|
|
if (cursorVisible)
|
|
{
|
|
Cursor.visible = true;
|
|
Cursor.lockState = CursorLockMode.Confined;
|
|
}
|
|
else
|
|
{
|
|
Cursor.visible = false;
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
}
|
|
}
|
|
|
|
//*********************//
|
|
// Private member data //
|
|
//*********************//
|
|
|
|
public enum eInputState
|
|
{
|
|
MouseKeyboard,
|
|
Controler
|
|
};
|
|
private eInputState m_State = eInputState.MouseKeyboard;
|
|
|
|
//*************************//
|
|
// Unity member methods //
|
|
//*************************//
|
|
|
|
void OnGUI()
|
|
{
|
|
switch (m_State)
|
|
{
|
|
case eInputState.MouseKeyboard:
|
|
if (isControlerInput())
|
|
{
|
|
IsUsingJoystick = true;
|
|
m_State = eInputState.Controler;
|
|
OnControllerInput?.Invoke();
|
|
Debug.Log("JoyStick being used");
|
|
}
|
|
break;
|
|
case eInputState.Controler:
|
|
if (isMouseKeyboard())
|
|
{
|
|
IsUsingJoystick = false;
|
|
m_State = eInputState.MouseKeyboard;
|
|
OnKeyboardOrMouseInput?.Invoke();
|
|
Debug.Log("Mouse & Keyboard being used");
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
//***************************//
|
|
// Public member methods //
|
|
//***************************//
|
|
|
|
public eInputState GetInputState()
|
|
{
|
|
return m_State;
|
|
}
|
|
|
|
//****************************//
|
|
// Private member methods //
|
|
//****************************//
|
|
|
|
private bool isMouseKeyboard()
|
|
{
|
|
// mouse & keyboard buttons
|
|
if (Event.current.isKey ||
|
|
Event.current.isMouse)
|
|
{
|
|
return true;
|
|
}
|
|
// mouse movement
|
|
if (Input.GetAxis("Mouse X") != 0.0f ||
|
|
Input.GetAxis("Mouse Y") != 0.0f)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (GameStartedUpdate)
|
|
{
|
|
if (Input.GetJoystickNames().Length > 0)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private bool isControlerInput()
|
|
{
|
|
// joystick buttons
|
|
if (Input.GetKey(KeyCode.Joystick1Button0) ||
|
|
Input.GetKey(KeyCode.Joystick1Button1) ||
|
|
Input.GetKey(KeyCode.Joystick1Button2) ||
|
|
Input.GetKey(KeyCode.Joystick1Button3) ||
|
|
Input.GetKey(KeyCode.Joystick1Button4) ||
|
|
Input.GetKey(KeyCode.Joystick1Button5) ||
|
|
Input.GetKey(KeyCode.Joystick1Button6) ||
|
|
Input.GetKey(KeyCode.Joystick1Button7) ||
|
|
Input.GetKey(KeyCode.Joystick1Button8) ||
|
|
Input.GetKey(KeyCode.Joystick1Button9) ||
|
|
Input.GetKey(KeyCode.Joystick1Button10) ||
|
|
Input.GetKey(KeyCode.Joystick1Button11) ||
|
|
Input.GetKey(KeyCode.Joystick1Button12) ||
|
|
Input.GetKey(KeyCode.Joystick1Button13) ||
|
|
Input.GetKey(KeyCode.Joystick1Button14) ||
|
|
Input.GetKey(KeyCode.Joystick1Button15) ||
|
|
Input.GetKey(KeyCode.Joystick1Button16) ||
|
|
Input.GetKey(KeyCode.Joystick1Button17) ||
|
|
Input.GetKey(KeyCode.Joystick1Button18) ||
|
|
Input.GetKey(KeyCode.Joystick1Button19))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
// joystick axis
|
|
if (Input.GetAxis("Joystick Horizontal") != 0.0f ||
|
|
Input.GetAxis("Joystick Vertical") != 0.0f ||
|
|
Input.GetAxis("LeftTrigger") != 0.0f ||
|
|
Input.GetAxis("RightTrigger") != 0.0f ||
|
|
Input.GetAxis("Joystick X") != 0.0f ||
|
|
Input.GetAxis("Joystick Y") != 0.0f ||
|
|
Input.GetAxis("DpadVertical") != 0.0f ||
|
|
Input.GetAxis("DpadHorizontal") != 0.0f)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (GameStartedUpdate)
|
|
{
|
|
if (Input.GetJoystickNames().Length > 0)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
//public void SelectedMenuItemHideAll(ref List<GameObject> selectedMenuItem)
|
|
//{
|
|
// if (hideAllSelectedItemHighlights)
|
|
// {
|
|
// for (int i = 0; i < selectedMenuItem.Count; i++)
|
|
// {
|
|
// selectedMenuItem[i].SetActive(false);
|
|
// if (i == selectedMenuItem.Count)
|
|
// {
|
|
// hideAllSelectedItemHighlights = false;
|
|
// }
|
|
// }
|
|
// }
|
|
//}
|
|
|
|
public void SelectedMenuItemHideAll(ref GameObject[] selectedMenuItem)
|
|
{
|
|
//if (hideAllSelectedItemHighlights)
|
|
//{
|
|
for (int i = 0; i < selectedMenuItem.Length; i++)
|
|
{
|
|
selectedMenuItem[i].SetActive(false);
|
|
if(i == selectedMenuItem.Length)
|
|
{
|
|
hideAllSelectedItemHighlights = false;
|
|
}
|
|
}
|
|
//}
|
|
}
|
|
|
|
public void SelectedMenuItemUpdate(ref GameObject[] selectedMenuItem, int ActiveItem)
|
|
{
|
|
//if (_verticalButtonDpadIsPressedDown)
|
|
//{
|
|
for (int i = 0; i < selectedMenuItem.Length; i++)
|
|
{
|
|
if (i == ActiveItem)
|
|
{
|
|
selectedMenuItem[i].SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
selectedMenuItem[i].SetActive(false);
|
|
}
|
|
}
|
|
//}
|
|
}
|
|
|
|
public void DpadNavigateVertical(ref int CurrentSelectedItemIndex, int selectedItemIndexLast)
|
|
{
|
|
if (Input.GetAxis("dpadVertical") > 0 && !_verticalButtonDpadIsPressedDown)
|
|
{
|
|
CurrentSelectedItemIndex -= 1;
|
|
print("dpadVertical UP");
|
|
if (CurrentSelectedItemIndex == -1)
|
|
{
|
|
CurrentSelectedItemIndex = selectedItemIndexLast;
|
|
}
|
|
if (_verticalButtonDpadIsPressedDown == false)
|
|
{
|
|
_verticalButtonDpadIsPressedDown = true;
|
|
}
|
|
upDpadVerticalButton = true;
|
|
}
|
|
else if (Input.GetAxis("dpadVertical") < 0 && !_verticalButtonDpadIsPressedDown)
|
|
{
|
|
CurrentSelectedItemIndex += 1;
|
|
print("dpadVertical DOWN");
|
|
if (CurrentSelectedItemIndex == selectedItemIndexLast + 1)
|
|
{
|
|
CurrentSelectedItemIndex = 0;
|
|
}
|
|
if (_verticalButtonDpadIsPressedDown == false)
|
|
{
|
|
_verticalButtonDpadIsPressedDown = true;
|
|
}
|
|
downDpadVericalButton = true;
|
|
}
|
|
else if (Input.GetAxis("dpadVertical") == 0)
|
|
{
|
|
_verticalButtonDpadIsPressedDown = false;
|
|
upDpadVerticalButton = false;
|
|
downDpadVericalButton = false;
|
|
}
|
|
}
|
|
|
|
public void DpadNavigateHorizontal(ref int CurrentSelectedItemIndex, int selectedItemIndexLast)
|
|
{
|
|
if (Input.GetAxis("dpadHorizontal") > 0 && !_horizontalButtonDpadUpIsPressedDown)
|
|
{
|
|
CurrentSelectedItemIndex += 1;
|
|
print("dpadHorizontal right");
|
|
if (CurrentSelectedItemIndex == selectedItemIndexLast + 1)
|
|
{
|
|
CurrentSelectedItemIndex = 0;
|
|
}
|
|
_horizontalButtonDpadUpIsPressedDown = true;
|
|
}
|
|
else if (Input.GetAxis("dpadHorizontal") < 0 && !_horizontalButtonDpadUpIsPressedDown)
|
|
{
|
|
CurrentSelectedItemIndex -= 1;
|
|
print("dpadHorizontal Left");
|
|
if (CurrentSelectedItemIndex == -1)
|
|
{
|
|
CurrentSelectedItemIndex = selectedItemIndexLast;
|
|
}
|
|
_horizontalButtonDpadUpIsPressedDown = true;
|
|
}
|
|
else if (Input.GetAxis("dpadHorizontal") == 0)
|
|
{
|
|
_horizontalButtonDpadUpIsPressedDown = false;
|
|
}
|
|
}
|
|
|
|
public void DpadNavigateHorizontalButtonPress(Button buttonLeft, Button buttonRight)
|
|
{
|
|
if (Input.GetAxis("dpadHorizontal") > 0 && !_horizontalButtonDpadUpIsPressedDown)
|
|
{
|
|
buttonRight.onClick.Invoke();
|
|
print("dpadHorizontal right");
|
|
_horizontalButtonDpadUpIsPressedDown = true;
|
|
}
|
|
else if (Input.GetAxis("dpadHorizontal") < 0 && !_horizontalButtonDpadUpIsPressedDown)
|
|
{
|
|
buttonLeft.onClick.Invoke();
|
|
print("dpadHorizontal Left");
|
|
_horizontalButtonDpadUpIsPressedDown = true;
|
|
}
|
|
else if (Input.GetAxis("dpadHorizontal") == 0)
|
|
{
|
|
_horizontalButtonDpadUpIsPressedDown = false;
|
|
}
|
|
}
|
|
|
|
public void DpadHorizontalSliderValuesUpdate(Slider slider, float speed)
|
|
{
|
|
if (Input.GetAxis("dpadHorizontal") > 0)
|
|
{
|
|
slider.value += speed;
|
|
}
|
|
else if (Input.GetAxis("dpadHorizontal") < 0)
|
|
{
|
|
slider.value -= speed;
|
|
}
|
|
}
|
|
|
|
public void DpadHorizontalSliderInvertedValuesUpdate(Slider slider, float speed)
|
|
{
|
|
if (Input.GetAxis("dpadHorizontal") > 0)
|
|
{
|
|
slider.value -= speed;
|
|
}
|
|
else if (Input.GetAxis("dpadHorizontal") < 0)
|
|
{
|
|
slider.value += speed;
|
|
}
|
|
}
|
|
} |