Files
UltimateFishing/Assets/Scripts/Assembly-CSharp/ControlsManager.cs
2026-02-21 16:45:37 +08:00

186 lines
5.4 KiB
C#

using System;
using System.Collections.Generic;
using BitStrap;
using Rewired;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ControlsManager : MonoBehaviour
{
public List<ControlsButton> controlsButtons = new List<ControlsButton>();
[ReadOnly]
public ControlsButton currentControlsButton;
public InputMapper inputMapper = new InputMapper();
public InputMapper inputMapperMouse = new InputMapper();
public GameObject pressParent;
public Text pressText;
public float timeout = 5f;
[ReadOnly]
public float waitTimer;
private void Awake()
{
controlsButtons = GetComponentsInChildren<ControlsButton>().ToDynList();
for (int i = 0; i < controlsButtons.Count; i++)
{
controlsButtons[i].controlsManager = this;
controlsButtons[i].action = UtilitiesInput.FindInputAction(controlsButtons[i].actionName);
controlsButtons[i].actionElementMapId = UtilitiesInput.GetActionElementMapId(controlsButtons[i].action.id, controlsButtons[i].positive);
}
RefreshAllButtons();
}
private void OnEnable()
{
HidePressWindow(false);
if (ReInput.isReady)
{
inputMapper.options.timeout = timeout;
inputMapper.options.ignoreMouseXAxis = true;
inputMapper.options.ignoreMouseYAxis = true;
inputMapperMouse.options.ignoreMouseXAxis = true;
inputMapperMouse.options.ignoreMouseYAxis = true;
ReInput.ControllerConnectedEvent += OnControllerChanged;
ReInput.ControllerDisconnectedEvent += OnControllerChanged;
inputMapper.InputMappedEvent += OnInputMapped;
inputMapper.StoppedEvent += OnStopped;
inputMapperMouse.InputMappedEvent += OnInputMappedMouse;
inputMapperMouse.StoppedEvent += OnStoppedMouse;
RefreshAllButtons();
}
}
private void OnDisable()
{
inputMapper.Stop();
inputMapperMouse.Stop();
inputMapper.RemoveAllEventListeners();
inputMapperMouse.RemoveAllEventListeners();
ReInput.ControllerConnectedEvent -= OnControllerChanged;
ReInput.ControllerDisconnectedEvent -= OnControllerChanged;
Debug.Log("userDataStore_PlayerPrefs Save");
UtilitiesInput.userDataStore_PlayerPrefs.Save();
}
private void Update()
{
if (waitTimer > 0f)
{
waitTimer -= Time.unscaledDeltaTime;
pressText.text = Utilities.GetTranslation("HUD_CONTROLS/CHANGING_KEY") + " " + Mathf.CeilToInt(waitTimer);
}
}
public void ButtonClicked(ControlsButton controlsButton)
{
EventSystem.current.SetSelectedGameObject(null);
currentControlsButton = controlsButton;
Debug.Log("ButtonClicked: " + controlsButton.action.id + " " + controlsButton.actionElementMapId);
inputMapper.Start(new InputMapper.Context
{
actionId = controlsButton.action.id,
controllerMap = UtilitiesInput.controllerMapKeyboard,
actionRange = controlsButton.actionRange,
actionElementMapToReplace = UtilitiesInput.controllerMapKeyboard.GetElementMap(controlsButton.actionElementMapId)
});
inputMapperMouse.Start(new InputMapper.Context
{
actionId = controlsButton.action.id,
controllerMap = UtilitiesInput.controllerMapMouse,
actionRange = controlsButton.actionRange,
actionElementMapToReplace = UtilitiesInput.controllerMapMouse.GetElementMap(controlsButton.actionElementMapId)
});
waitTimer = inputMapper.options.timeout;
pressParent.SetActive(true);
}
[Button]
public void RefreshAllButtons()
{
for (int i = 0; i < controlsButtons.Count; i++)
{
ControlsButton controlsButton = controlsButtons[i];
controlsButton.buttonText.text = string.Empty;
controlsButton.buttonText.text = UtilitiesInput.GetActionKeyName(controlsButton.actionName, controlsButton.positive);
}
HidePressWindow(true);
}
public void ResetToDefault()
{
UtilitiesInput.ResetRewiredPrefs();
RefreshAllButtons();
}
private void OnControllerChanged(ControllerStatusChangedEventArgs args)
{
Debug.Log("OnControllerChanged");
}
private void OnInputMapped(InputMapper.InputMappedEventData data)
{
currentControlsButton.actionElementMapId = data.actionElementMap.id;
Debug.Log("OnInputMapped: " + data.actionElementMap.actionId + " " + data.actionElementMap.id);
if (UtilitiesInput.controllerMapMouse.ContainsAction(data.actionElementMap.actionId))
{
UtilitiesInput.controllerMapMouse.DeleteButtonMapsWithAction(data.actionElementMap.actionId);
}
RefreshAllButtons();
}
private void OnInputMappedMouse(InputMapper.InputMappedEventData data)
{
currentControlsButton.actionElementMapId = data.actionElementMap.id;
Debug.Log("OnInputMappedMouse: " + data.actionElementMap.actionId + " " + data.actionElementMap.id);
if (UtilitiesInput.controllerMapKeyboard.ContainsAction(data.actionElementMap.actionId))
{
UtilitiesInput.controllerMapKeyboard.DeleteButtonMapsWithAction(data.actionElementMap.actionId);
}
RefreshAllButtons();
}
private void OnStopped(InputMapper.StoppedEventData data)
{
Debug.Log("OnStopped");
HidePressWindow(true);
inputMapperMouse.Stop();
currentControlsButton = null;
}
private void OnStoppedMouse(InputMapper.StoppedEventData data)
{
Debug.Log("OnStoppedMouse");
HidePressWindow(true);
inputMapper.Stop();
currentControlsButton = null;
}
public void HidePressWindow(bool delay)
{
if (delay)
{
LeanTween.delayedCall(0.5f, (Action)delegate
{
pressParent.SetActive(false);
}).setIgnoreTimeScale(true);
}
else
{
pressParent.SetActive(false);
}
}
private void SetSelectedController(ControllerType controllerType)
{
Debug.Log("SetSelectedController");
}
}