Files

159 lines
5.8 KiB
C#
Raw Permalink Normal View History

2022-08-14 12:21:50 +03:00
using System;
2022-08-15 11:34:20 +03:00
using System.Collections;
2022-08-14 00:05:25 +03:00
using System.Collections.Generic;
2022-08-14 12:21:50 +03:00
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
2022-08-16 23:07:21 +03:00
using TMPro;
2022-08-13 14:11:13 +03:00
using UnityEngine;
using UnityEngine.EventSystems;
2022-08-13 16:58:53 +03:00
using UnityEngine.InputSystem;
2022-08-13 14:11:13 +03:00
using UnityEngine.UI;
2022-08-13 16:58:53 +03:00
using static UnityEngine.InputSystem.InputActionRebindingExtensions;
2022-08-12 23:12:55 +03:00
public class RebindMenu : BaseMenu
{
[SerializeField] private InputManager _inputManager = default;
2022-12-14 18:33:44 +02:00
[SerializeField] private EventSystem _eventSystem = default;
[SerializeField] private TextMeshProUGUI _deviceText = default;
[SerializeField] private PlayerInput _playerInput = default;
[SerializeField] private Button _firstCharacterButton = default;
[SerializeField] private CharacterAssistSelector _characterAssistSelector = default;
[SerializeField] private CharacterColorSelector _characterColorSelector = default;
[SerializeField] private GameObject _assignButtonImage = default;
2023-01-16 13:26:54 +02:00
[SerializeField] private GameObject _backgroundDarken = default;
2022-12-14 18:33:44 +02:00
[SerializeField] private RectTransform _scrollView = default;
[SerializeField] private Transform _rebindContainer = default;
[SerializeField] private bool _secondPlayer = default;
private readonly List<RebindButton> _rebindButtons = new();
private readonly string _controlRebindKey = "rebinds";
private InputAction _inputAction;
private InputDevice _inputDevice;
private RebindingOperation _rebindingOperation;
private string _controlMatch;
private string _controlCancel;
2022-08-12 23:12:55 +03:00
public PromptsInput PreviousPromptsInput { get; set; }
2023-01-15 01:54:52 +02:00
//Load the bindings
2022-12-14 18:33:44 +02:00
void Awake()
{
2023-01-15 01:54:52 +02:00
string rebinds = DemonicsSaver.Load(_controlRebindKey);
2022-12-14 18:33:44 +02:00
_playerInput.actions.LoadBindingOverridesFromJson(rebinds);
}
2022-08-14 12:21:50 +03:00
2023-01-15 01:54:52 +02:00
//Update the rebind visual images based on the bindings
2022-12-14 18:33:44 +02:00
void Start()
{
for (int i = 0; i < _rebindContainer.childCount; i++)
{
if (_rebindContainer.GetChild(i).TryGetComponent(out RebindButton rebindButton))
{
_rebindButtons.Add(rebindButton);
rebindButton.UpdatePromptImage();
}
}
}
2022-08-14 00:05:25 +03:00
2023-12-05 12:42:43 +02:00
void OnDisable()
{
MouseSetup.Instance.SetCursor(true);
}
2023-01-15 01:54:52 +02:00
//Set the binding rules based on the input device
2022-12-14 18:33:44 +02:00
void OnEnable()
{
2023-12-05 12:42:43 +02:00
MouseSetup.Instance.SetCursor(false);
2023-01-16 13:26:54 +02:00
_backgroundDarken.SetActive(true);
2022-12-14 18:33:44 +02:00
if (!_secondPlayer)
{
_inputDevice = SceneSettings.ControllerOne;
}
else
{
_inputDevice = SceneSettings.ControllerTwo;
}
if (_inputDevice.displayName.Contains("Keyboard"))
{
_deviceText.text = "Keyboard";
_controlMatch = "<Keyboard>";
_controlCancel = "<Keyboard>/tab";
}
else
{
_deviceText.text = "Controller";
_controlMatch = "<Gamepad>";
_controlCancel = "<Gamepad>/start";
}
_scrollView.anchoredPosition = Vector2.zero;
}
2022-08-14 12:01:00 +03:00
2023-01-15 01:54:52 +02:00
//Called from UI, hide the menu and if none of the selectors are active then re-select the first character
2022-12-14 18:33:44 +02:00
public void HideRebind()
{
Hide();
2023-01-16 13:26:54 +02:00
_backgroundDarken.SetActive(false);
2022-12-14 18:33:44 +02:00
if (!_characterAssistSelector.gameObject.activeSelf && !_characterColorSelector.gameObject.activeSelf)
_firstCharacterButton.Select();
2023-08-29 17:42:32 +03:00
_inputManager.SetPrompts(PreviousPromptsInput);
2022-12-14 18:33:44 +02:00
}
2022-08-13 14:11:13 +03:00
2023-01-15 01:54:52 +02:00
//Called from UI, rebind the given button
2022-12-14 18:33:44 +02:00
public void AssignButton(RebindButton rebindButton)
{
_eventSystem.sendNavigationEvents = false;
_eventSystem.SetSelectedGameObject(null);
_assignButtonImage.SetActive(true);
_inputAction = rebindButton.ActionReference.action;
int index = rebindButton.CompositeIndex;
_rebindingOperation = _inputAction.PerformInteractiveRebinding(rebindButton.ControlBindingIndex)
.WithControlsHavingToMatchPath(_controlMatch)
.WithCancelingThrough(_controlCancel)
.OnMatchWaitForAnother(0.1f)
.OnCancel(operation => RebindCancelled(rebindButton))
.OnComplete(operation => RebindComplete(rebindButton));
_rebindingOperation.Start();
}
2022-08-13 16:58:53 +03:00
2023-01-15 01:54:52 +02:00
//Cancel rebind operation
2022-12-14 18:33:44 +02:00
private void RebindCancelled(RebindButton rebindButton)
{
_rebindingOperation.Dispose();
_assignButtonImage.SetActive(false);
StartCoroutine(RebindCompleteCoroutine(rebindButton));
}
2022-08-16 21:05:17 +03:00
2023-01-15 01:54:52 +02:00
//Complete rebind operation
2022-12-14 18:33:44 +02:00
private void RebindComplete(RebindButton rebindButton)
{
_rebindingOperation.Dispose();
_assignButtonImage.SetActive(false);
string rebinds = _playerInput.actions.SaveBindingOverridesAsJson();
2023-01-15 01:54:52 +02:00
DemonicsSaver.Save(_controlRebindKey, rebinds);
2022-12-14 18:33:44 +02:00
StartCoroutine(RebindCompleteCoroutine(rebindButton));
}
2022-08-15 11:34:20 +03:00
2023-01-15 01:54:52 +02:00
//Wait a small amount before updating the visuals and reselecting the button for better game-feel
2022-12-14 18:33:44 +02:00
IEnumerator RebindCompleteCoroutine(RebindButton rebindButton)
{
yield return new WaitForSeconds(0.1f);
_eventSystem.sendNavigationEvents = true;
rebindButton.UpdatePromptImage();
rebindButton.GetComponent<Button>().Select();
}
2022-08-13 14:11:13 +03:00
2023-01-15 01:54:52 +02:00
//Called from UI, reset all bindings to default
2022-12-14 18:33:44 +02:00
public void ResetRebindToDefault()
{
if (_playerInput.devices[0].displayName.Contains(_deviceText.text))
{
for (int i = 0; i < _rebindButtons.Count; i++)
{
InputAction inputAction = _rebindButtons[i].ActionReference.action;
InputActionRebindingExtensions.RemoveAllBindingOverrides(inputAction);
_rebindButtons[i].UpdatePromptImage();
}
string rebinds = _playerInput.actions.SaveBindingOverridesAsJson();
2023-01-15 01:54:52 +02:00
DemonicsSaver.Save(_controlRebindKey, rebinds);
2022-12-14 18:33:44 +02:00
}
}
2022-08-12 23:12:55 +03:00
}