You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

676 lines
27 KiB
C#

4 years ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using SiegeSong;
3 years ago
using UnityEngine.UI;
using UnityEngine.EventSystems;
4 years ago
namespace SiegeSong
{
public class InputManager : MonoBehaviour
4 years ago
{
3 years ago
public InputScheme CurrentScheme = InputScheme.Xbox;
public InputState CurrentState = InputState.OutGameMenu;
public Motor Motor;
public CameraManager CameraManager;
4 years ago
public InventoryManager InventoryManager;
public Selector Selector;
3 years ago
public MenuManager MenuManager;
4 years ago
3 years ago
public GameObject CursorObject;
4 years ago
public CommandConsole CommandConsole;
3 years ago
public EventSystem UIEventSystem;
public GraphicRaycaster UIRaycaster;
public AudioSource AudioSource;
4 years ago
public Dictionary<string, bool> HandleButtonInputOldState;
public Dictionary<string, bool> HandleButtonChordedPressed;
public Dictionary<string, bool> HandleButtonDoublePressStarted;
3 years ago
public Dictionary<string, bool> HandleButtonTimerActive;
4 years ago
public Dictionary<string, bool> HandleButtonChordedTimerActive;
public Dictionary<string, bool> HandleButtonInputInUse;
public Dictionary<string, float> HandleButtonTimer;
public Dictionary<string, float> HandleButtonChordedTimer;
private string RunForwardKeyboardInput = "w";
private string RunBackwardKeyboardInput = "s";
private string StrafeLeftKeyboardInput = "a";
private string StrafeRightKeyboardInput = "d";
private string ChangeCameraKeyboardInput = "f";
private string RotateCameraVerticalKeyboardInput = "Mouse Y";
3 years ago
private string RotateCameraHorizontalKeyboardInput = "Mouse X";
private string menuOpenGamepadInput = "Back";
private string menuVerticalGamepadInput = "D-Pad Vertical";
private string menuHorizontalGamepadInput = "D-Pad Horizontal";
3 years ago
private string gamepadAcceptInput = "A";
private string gamepadCancelInput = "B";
private string gamepadDropInput = "X";
private string gamepadUseRightInput = "RT";
private string gamepadUseLeftInput = "LT";
private string gamepadSwitchLeftInput = "LB";
private string gamepadSwitchRightInput = "RB";
private string runGamepadInput = "LeftAnalogVertical";
private string strafeGamepadInput = "LeftAnalogHorizontal";
private string rotateCameraVerticalGamepadInput = "RightAnalogVertical";
private string rotateCameraHorizontalGamepadInput = "RightAnalogHorizontal";
private string changeCameraGamepadInput = "RightStickClick";
3 years ago
private string[] keyboardAxisInputs;
private string[] keyboardButtonInputs;
private string[] gamepadAxisInputs;
private string[] gamepadButtonInputs;
private float inputMin = 0.01f;
private float longPressTime = 0.4f;
private float doublePressDelayTime = 0.1f;
private bool oldActivateState;
private bool oldMenuState;
private bool oldCameraState;
private bool oldDropState;
private bool oldCrouchState;
4 years ago
3 years ago
private bool oldOpenInputState;
private bool oldActivateInputState;
private bool oldDropInputState;
private bool oldDownArrowInputState;
private bool oldUpArrowInputState;
4 years ago
void Start()
{
3 years ago
keyboardAxisInputs = new string[]
{
RotateCameraVerticalKeyboardInput,
RotateCameraHorizontalKeyboardInput
};
keyboardButtonInputs = new string[]
{
RunForwardKeyboardInput,
RunBackwardKeyboardInput,
StrafeLeftKeyboardInput,
StrafeRightKeyboardInput,
ChangeCameraKeyboardInput
};
gamepadAxisInputs = new string[]
{
menuVerticalGamepadInput,
menuHorizontalGamepadInput,
gamepadUseRightInput,
gamepadUseLeftInput,
runGamepadInput,
strafeGamepadInput,
rotateCameraVerticalGamepadInput,
rotateCameraHorizontalGamepadInput
};
gamepadButtonInputs = new string[]
{
menuOpenGamepadInput,
gamepadAcceptInput,
gamepadCancelInput,
gamepadDropInput,
gamepadSwitchLeftInput,
gamepadSwitchRightInput,
changeCameraGamepadInput
};
4 years ago
}
public InputDirection GetInputDirection(bool leftStick = false)
{
var stickVerticalGamepad = Input.GetAxis(leftStick ? runGamepadInput : rotateCameraVerticalGamepadInput);
var stickHorizontalGamepad = Input.GetAxis(leftStick ? strafeGamepadInput : rotateCameraHorizontalGamepadInput);
4 years ago
var up = false;
var down = false;
var left = false;
var right = false;
if (stickVerticalGamepad > 0)
up = true;
if (stickVerticalGamepad < 0)
down = true;
if (stickHorizontalGamepad > 0)
left = true;
if (stickHorizontalGamepad < 0)
right = true;
if (up && left)
return InputDirection.TopLeft;
if (up && right)
return InputDirection.TopRight;
if (down && right)
return InputDirection.BottomRight;
if (down && left)
return InputDirection.BottomLeft;
if (up)
return InputDirection.TopMid;
if (down)
return InputDirection.BottomMid;
if (left)
return InputDirection.MidLeft;
if (right)
return InputDirection.MidRight;
return InputDirection.MidMid;
}
void Update()
{
if (Input.GetKeyDown("`"))
CommandConsole.CommandConsoleOpen = !CommandConsole.CommandConsoleOpen;
3 years ago
foreach (var xboxInput in gamepadAxisInputs)
if (Input.GetAxis(xboxInput) > inputMin)
CurrentScheme = InputScheme.Xbox;
foreach (var xboxInput in gamepadButtonInputs)
if (Input.GetButton(xboxInput))
CurrentScheme = InputScheme.Xbox;
foreach (var pcInput in keyboardAxisInputs)
if (Input.GetAxis(pcInput) > inputMin)
CurrentScheme = InputScheme.PC;
foreach (var pcInput in keyboardButtonInputs)
if (Input.GetKey(pcInput))
CurrentScheme = InputScheme.PC;
if (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2))
CurrentScheme = InputScheme.PC;
switch (CurrentState)
{
case InputState.World:
updateWorldInput();
break;
case InputState.InGameMenu:
updateInGameMenuInput();
break;
case InputState.OutGameMenu:
updateOutGameMenuInput();
break;
4 years ago
}
3 years ago
}
void updateCursorLocation()
{
if (!CursorObject.activeSelf)
CursorObject.SetActive(true);
4 years ago
3 years ago
if (Cursor.visible)
Cursor.visible = false;
CursorObject.transform.position = Input.mousePosition;
var pointer = new PointerEventData(UIEventSystem) { position = Input.mousePosition };
var results = new List<RaycastResult>();
UIRaycaster.Raycast(pointer, results);
foreach (var result in results)
{
var navigationOption = result.gameObject.GetComponent<NavigationOption>();
if (navigationOption == null)
{
if (result.gameObject.transform.parent != null && result.gameObject.transform.parent.GetComponent<NavigationOption>() != null)
navigationOption = result.gameObject.transform.parent.GetComponent<NavigationOption>();
}
if (navigationOption != null)
{
switch (CurrentState)
{
case InputState.OutGameMenu:
if (navigationOption.Type == NavigationButtonType.Row)
if (!MenuManager.SaveLoadSubmenuOpen && !MenuManager.SettingsSubmenuOpen)
MenuManager.SelectedIndex = navigationOption.ItemIndex;
else
MenuManager.SubMenuSelectedIndex = navigationOption.ItemIndex;
break;
case InputState.InGameMenu:
if (navigationOption.Type == NavigationButtonType.Row)
{
InventoryManager.SelectedItemIndex = navigationOption.ItemIndex;
InventoryManager.UpdateInventoryScreen();
}
break;
case InputState.World:
break;
}
if (Input.GetMouseButton(0))
{
SelectNavigationOption(navigationOption);
}
}
}
}
public void SelectNavigationOption(NavigationOption navigationOption)
{
if (CurrentState == InputState.InGameMenu)
{
switch (navigationOption.Type)
{
case NavigationButtonType.Tab:
InventoryManager.ActiveTab = navigationOption.ItemIndex;
break;
case NavigationButtonType.Page:
InventoryManager.ActivePage = navigationOption.ItemIndex;
break;
case NavigationButtonType.Row:
InventoryManager.ActivateItem();
break;
}
}
else if (MenuManager.MenuActive)
{
MenuManager.ActivateSelectedOption();
}
}
void updateWorldInput()
{
var shouldStop = false;
4 years ago
var shouldRotate = false;
var zooming = false;
3 years ago
var strafe = 0.0f;
var run = 0.0f;
if (CurrentScheme == InputScheme.Xbox)
4 years ago
{
var cameraHorizontalGamepad = Input.GetAxis(rotateCameraHorizontalGamepadInput);
var cameraVerticalGamepad = Input.GetAxis(rotateCameraVerticalGamepadInput);
var runVertical = Input.GetAxis(runGamepadInput);
var runHorizontal = Input.GetAxis(strafeGamepadInput);
var menuVertical = Input.GetAxis(menuVerticalGamepadInput);
var menuHorizontal = Input.GetAxis(menuHorizontalGamepadInput);
4 years ago
var changeCameraGamepad = Input.GetAxis(changeCameraGamepadInput);
4 years ago
3 years ago
var activateGamepadInput = Input.GetButton(gamepadAcceptInput);
var dropGamepadInput = Input.GetButton(gamepadDropInput);
var openMenuGamepadInput = Input.GetButton(menuOpenGamepadInput);
3 years ago
var crouchState = Input.GetButton(gamepadCancelInput);
if (openMenuGamepadInput && !oldMenuState)
{
CurrentState = InputState.InGameMenu;
InventoryManager.OpenInventoryScreen();
}
Selector.InteractInputNewState = !oldActivateState && activateGamepadInput;
4 years ago
3 years ago
HandleButtonInput(gamepadUseRightInput, Motor.UseRight, null, null, null, null, true);
HandleButtonInput(gamepadUseLeftInput, Motor.UseLeft, null, null, null, null, true);
HandleButtonInput(gamepadDropInput, Motor.UseAbility, null, null, null, null, true);
HandleButtonInput(gamepadSwitchRightInput, Motor.NextRightTechnique, Motor.NextRightEquip);
HandleButtonInput(gamepadSwitchLeftInput, Motor.NextLeftTechnique, Motor.NextLeftEquip, Motor.NextAbilityEquip, Motor.NextAbilityTechnique, gamepadSwitchRightInput);
HandleButtonInput(changeCameraGamepadInput, CameraManager.SwitchCamera, null, null, null, null, true);
4 years ago
3 years ago
if (!InventoryManager.ActivateInputNewState && activateGamepadInput && !oldActivateState && Motor.UsingStation && Motor.TargetStation != null && Selector.Target == null)
Motor.StopUsingStation(Motor.TargetStation);
4 years ago
3 years ago
if (Motor != null)
4 years ago
{
3 years ago
if (runVertical > inputMin || runVertical < -inputMin)
4 years ago
{
3 years ago
run = runVertical;
Motor.Run(runVertical);
shouldRotate = true;
}
else
{
run = 0;
Motor.Run(0);
4 years ago
}
3 years ago
if (runHorizontal != 0)
{
strafe = runHorizontal;
Motor.Strafe(runHorizontal);
shouldRotate = true;
}
else
{
strafe = 0;
Motor.Strafe(0);
}
4 years ago
3 years ago
switch (CurrentScheme)
{
case InputScheme.Xbox:
4 years ago
3 years ago
break;
case InputScheme.PC:
4 years ago
3 years ago
break;
}
4 years ago
3 years ago
if (cameraVerticalGamepad != 0)
CameraManager.RotateVertical(cameraVerticalGamepad);
else
CameraManager.RotateVertical(0);
4 years ago
3 years ago
if (cameraHorizontalGamepad != 0)
CameraManager.RotateHorizontal(cameraHorizontalGamepad);
else
CameraManager.RotateHorizontal(0);
4 years ago
3 years ago
if (oldCameraState && changeCameraGamepad != 0)
4 years ago
{
3 years ago
if (runVertical != 0)
{
zooming = true;
if (runVertical > 0)
CameraManager.ZoomOut();
else
CameraManager.ZoomIn();
}
4 years ago
}
3 years ago
if (!zooming && !(runVertical > inputMin || runVertical < -inputMin) && !(runHorizontal > inputMin || runHorizontal < -inputMin))
shouldStop = true;
}
4 years ago
oldCameraState = changeCameraGamepad != 0;
oldActivateState = activateGamepadInput;
oldDropState = dropGamepadInput;
oldMenuState = openMenuGamepadInput;
oldCrouchState = crouchState;
}
else
{
var runForward = Input.GetKey(RunForwardKeyboardInput);
var runBackward = Input.GetKey(RunBackwardKeyboardInput);
var strafeLeft = Input.GetKey(StrafeLeftKeyboardInput);
var strafeRight = Input.GetKey(StrafeRightKeyboardInput);
var cameraHorizontalKeyboad = Input.GetAxis(RotateCameraHorizontalKeyboardInput);
var cameraVerticalKeyboad = Input.GetAxis(RotateCameraVerticalKeyboardInput);
var changeCameraKeyboad = Input.GetKeyDown(ChangeCameraKeyboardInput);
3 years ago
if (Motor != null && CameraManager != null)
{
if (!changeCameraKeyboad && cameraVerticalKeyboad > inputMin || cameraVerticalKeyboad < -inputMin)
CameraManager.RotateVertical(cameraVerticalKeyboad);
4 years ago
3 years ago
if (cameraHorizontalKeyboad > inputMin || cameraHorizontalKeyboad < -inputMin)
CameraManager.RotateHorizontal(cameraHorizontalKeyboad);
4 years ago
3 years ago
if (changeCameraKeyboad)
4 years ago
{
3 years ago
if (runForward || runBackward)
{
zooming = true;
if (runForward)
CameraManager.ZoomOut();
if (runBackward)
CameraManager.ZoomIn();
}
}
if (runForward && !changeCameraKeyboad && !zooming)
{
run = 1;
Motor.Run(1);
shouldRotate = true;
}
else if (!runBackward)
{
run = 0;
Motor.Run(0);
4 years ago
}
3 years ago
if (runBackward && !changeCameraKeyboad && !zooming)
{
run = -1;
Motor.Run(-1);
shouldRotate = true;
}
else if (!runForward)
{
run = 0;
Motor.Run(0);
}
4 years ago
3 years ago
if (strafeLeft && !zooming)
{
strafe = -1;
Motor.Strafe(-1);
shouldRotate = true;
}
else if (!strafeRight)
{
strafe = 0;
Motor.Strafe(0);
}
4 years ago
3 years ago
if (strafeRight && !zooming)
{
strafe = 1;
Motor.Strafe(1);
shouldRotate = true;
}
else if (!strafeLeft)
{
strafe = 0;
Motor.Strafe(0);
}
4 years ago
3 years ago
if (!runForward && !runBackward && !strafeLeft && !strafeRight)
shouldStop = true;
4 years ago
}
}
if (shouldRotate || (CameraManager.FirstPersonActive && CameraManager.CameraX != 0.0f))
4 years ago
{
CameraManager.RealignCamera(true);
if (CameraManager.FirstPersonActive && CameraManager.CameraX != 0.0f)
Motor.Strafe(0, true);
4 years ago
}
3 years ago
if (shouldStop)
Motor.Stop();
4 years ago
}
3 years ago
void updateInGameMenuInput()
{
var UpArrowInputNewState = false;
var DownArrowInputNewState = false;
var menuVertical = Input.GetAxis(menuVerticalGamepadInput);
var menuHorizontal = Input.GetAxis(menuHorizontalGamepadInput);
var activateGamepadInput = Input.GetButton(gamepadAcceptInput);
var dropGamepadInput = Input.GetButton(gamepadDropInput);
var openMenuGamepadInput = Input.GetButton(menuOpenGamepadInput);
var crouchState = Input.GetButton(gamepadCancelInput);
if (crouchState && !oldCrouchState)
{
InventoryManager.CloseInventoryScreen();
InventoryManager.CloseTransferScreen();
CurrentState = InputState.World;
}
InventoryManager.ActivateInputNewState = !oldActivateState && activateGamepadInput;
InventoryManager.DropInputNewState = !oldDropState && dropGamepadInput;
if (menuVertical > inputMin)
InventoryManager.UpArrowInputNewState = true;
else
InventoryManager.UpArrowInputNewState = false;
if (menuVertical < -inputMin)
InventoryManager.DownArrowInputNewState = true;
else
InventoryManager.DownArrowInputNewState = false;
if (!oldUpArrowInputState && UpArrowInputNewState)
{
InventoryManager.SelectNextRow();
}
if (!oldDownArrowInputState && DownArrowInputNewState)
{
InventoryManager.SelectPreviousRow();
}
if (UpArrowInputNewState)
oldUpArrowInputState = true;
if (DownArrowInputNewState)
oldDownArrowInputState = true;
if (!DownArrowInputNewState && !UpArrowInputNewState)
{
oldUpArrowInputState = false;
oldDownArrowInputState = false;
}
if (oldActivateInputState && !activateGamepadInput)
{
if (InventoryManager.TransferScreenOpen)
InventoryManager.TransferSelectedItem();
else if (InventoryManager.InventoryScreenOpen)
InventoryManager.ActivateItem();
}
if (oldDropInputState && !dropGamepadInput)
{
if (InventoryManager.TransferScreenOpen)
{
InventoryManager.SwitchTransferTarget();
}
else if (!InventoryManager.DroppingItem)
{
InventoryManager.DroppingItem = true;
InventoryManager.DropItem();
}
}
oldActivateInputState = activateGamepadInput;
oldDropInputState = dropGamepadInput;
updateCursorLocation();
}
void updateOutGameMenuInput()
{
updateCursorLocation();
}
4 years ago
public delegate void ShortPress();
public delegate void LongPress();
public delegate void ChordedShortPress();
public delegate void ChordedLongPress();
public void HandleButtonInput(string inputName, ShortPress shortPress, LongPress longPress = null, ChordedShortPress chordedShortPress = null, ChordedLongPress chordedLongPress = null, string chordedInput = "", bool isAxis = false, bool chordIsAxis = false)
{
if (HandleButtonInputOldState == null)
HandleButtonInputOldState = new Dictionary<string, bool>();
if (HandleButtonChordedPressed == null)
HandleButtonChordedPressed = new Dictionary<string, bool>();
if (HandleButtonDoublePressStarted == null)
HandleButtonDoublePressStarted = new Dictionary<string, bool>();
if (HandleButtonTimerActive == null)
HandleButtonTimerActive = new Dictionary<string, bool>();
if (HandleButtonChordedTimerActive == null)
HandleButtonChordedTimerActive = new Dictionary<string, bool>();
if (HandleButtonInputInUse == null)
HandleButtonInputInUse = new Dictionary<string, bool>();
if (HandleButtonTimer == null)
HandleButtonTimer = new Dictionary<string, float>();
if (HandleButtonChordedTimer == null)
HandleButtonChordedTimer = new Dictionary<string, float>();
if (!HandleButtonInputOldState.ContainsKey(inputName))
HandleButtonInputOldState.Add(inputName, false);
if (!HandleButtonChordedPressed.ContainsKey(inputName))
HandleButtonChordedPressed.Add(inputName, false);
if (!HandleButtonDoublePressStarted.ContainsKey(inputName))
HandleButtonDoublePressStarted.Add(inputName, false);
if (!HandleButtonTimerActive.ContainsKey(inputName))
HandleButtonTimerActive.Add(inputName, false);
if (!HandleButtonChordedTimerActive.ContainsKey(inputName))
HandleButtonChordedTimerActive.Add(inputName, false);
if (!HandleButtonInputInUse.ContainsKey(inputName))
HandleButtonInputInUse.Add(inputName, false);
if (!HandleButtonTimer.ContainsKey(inputName))
HandleButtonTimer.Add(inputName, 0.0f);
if (!HandleButtonChordedTimer.ContainsKey(inputName))
HandleButtonChordedTimer.Add(inputName, 0.0f);
if (HandleButtonChordedTimerActive[inputName])
HandleButtonChordedTimer[inputName] += Time.deltaTime;
if (HandleButtonTimerActive[inputName])
HandleButtonTimer[inputName] += Time.deltaTime;
var inputValue = isAxis ? Input.GetAxis(inputName) > inputMin : Input.GetButton(inputName);
var chordedInputValue = chordedInput != "" && chordedInput != null ? (chordIsAxis ? Input.GetAxis(chordedInput) > inputMin : Input.GetButton(chordedInput)) : false;
4 years ago
if (inputValue)
{
if (!HandleButtonInputInUse[inputName])
HandleButtonInputInUse[inputName] = true;
if (!chordedInputValue)
{
if (HandleButtonInputOldState[inputName] == false)
{
HandleButtonTimer[inputName] = 0;
HandleButtonTimerActive[inputName] = true;
}
HandleButtonInputOldState[inputName] = true;
}
}
else
{
HandleButtonInputInUse[inputName] = false;
if (HandleButtonInputOldState[inputName])
{
HandleButtonInputOldState[inputName] = false;
HandleButtonTimerActive[inputName] = false;
if (HandleButtonTimer[inputName] > longPressTime && longPress != null)
{
longPress.Invoke();
}
else
{
shortPress.Invoke();
}
}
if (HandleButtonInputOldState[inputName] && !chordedInputValue)
{
if (!HandleButtonChordedPressed[inputName])
{
if (HandleButtonDoublePressStarted[inputName] && HandleButtonChordedTimer[inputName] < doublePressDelayTime)
HandleButtonChordedTimerActive[inputName] = false;
HandleButtonChordedTimer[inputName] = 0;
HandleButtonChordedTimerActive[inputName] = true;
}
HandleButtonChordedPressed[inputName] = true;
}
else
{
HandleButtonChordedTimerActive[inputName] = false;
if (HandleButtonChordedPressed[inputName])
{
if (HandleButtonChordedTimer[inputName] > longPressTime && chordedLongPress != null)
{
chordedLongPress.Invoke();
}
else if (chordedShortPress != null)
{
chordedShortPress.Invoke();
}
HandleButtonChordedTimerActive[inputName] = true;
HandleButtonDoublePressStarted[inputName] = true;
}
HandleButtonChordedTimer[inputName] = 0;
HandleButtonChordedPressed[inputName] = false;
}
}
}
}
}