Advertisement
Guest User

RewiredUFEInputManager.cs

a guest
May 13th, 2021
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.76 KB | None | 0 0
  1. // Copyright (c) 2017 Augie R. Maddox, Guavaman Enterprises. All rights reserved.
  2.  
  3. #pragma warning disable 0649
  4.  
  5. using System;
  6. using UnityEngine;
  7. using UnityEngine.EventSystems;
  8.  
  9. namespace Rewired.Integration.UniversalFightingEngine {
  10.     using UFE3D;
  11.  
  12.     /// <summary>
  13.     /// This cannot be used with Universal Fighting Engine 1.x.
  14.     /// This integration is compatible only with UFE2.
  15.     /// </summary>
  16.     public class RewiredUFEInputManager : MonoBehaviour, RewiredInputController.IInputSource, RewiredInputController.ITouchInputUI, RewiredInputController.IInputConfiguration {
  17.  
  18.         private const string className = "RewiredUFEInputManager";
  19.  
  20.         #region Variables
  21.  
  22.         [Tooltip("Link the Rewired Event System here.")]
  23.         [SerializeField]
  24.         private EventSystem _eventSystem;
  25.  
  26.         [Header("Control Mapper")]
  27.         [Tooltip("Link Control Mapper here if you're using it.")]
  28.         [SerializeField]
  29.         private Rewired.UI.ControlMapper.ControlMapper _controlMapper;
  30.  
  31.         [Tooltip("Actions to disable while Control Mapper is open.")]
  32.         [SerializeField]
  33.         private string[] _actionsToDisableIfControlMapperOpen = new string[] {
  34.             "Start"
  35.         };
  36.  
  37.         [Header("Touch Controls")]
  38.         [Tooltip("Link the touch controls GameObject here. This GameObject will be enabled and disabled when the touch UI is shown / hidden.")]
  39.         [SerializeField]
  40.         private GameObject _touchControls;
  41.  
  42.         [Tooltip("Should touch controls be hidden when a joystick is connected?")]
  43.         [SerializeField]
  44.         private bool _hideTouchControlsWhenJoysticksConnected = true;
  45.  
  46.         [Tooltip("Show touch controls on the following platforms.")]
  47.         [SerializeField]
  48.         [Rewired.Utils.Attributes.BitmaskToggle(typeof(PlatformFlags))]
  49.         private PlatformFlags _showTouchControlsOn = PlatformFlags.Android | PlatformFlags.IOS;
  50.  
  51.         [NonSerialized]
  52.         private bool _initialized;
  53.         [NonSerialized]
  54.         private bool _autoTouchControlUI_isActive;
  55.         [NonSerialized]
  56.         private bool _touchControlsActiveExternal;
  57.         [NonSerialized]
  58.         private Action _inputConfigurationUIClosedCallback;
  59.         [NonSerialized]
  60.         private bool _ufeEventSystemEnabledPrev;
  61.  
  62.         #endregion
  63.  
  64.         #region RewiredInputController.IInputSource Implementation
  65.  
  66.         public float GetAxis(int playerId, string name) {
  67.             if (!_initialized || !isEnabled) return 0f;
  68.             return ReInput.players.GetPlayer(playerId).GetAxis(name);
  69.         }
  70.  
  71.         public float GetAxisRaw(int playerId, string name) {
  72.             if (!_initialized || !isEnabled) return 0f;
  73.             return ReInput.players.GetPlayer(playerId).GetAxisRaw(name);
  74.         }
  75.  
  76.         public bool GetButton(int playerId, string name) {
  77.             if (!_initialized || !isEnabled) return false;
  78.             return ReInput.players.GetPlayer(playerId).GetButton(name);
  79.         }
  80.  
  81.         public bool GetButtonUp(int playerId, string name)
  82.         {
  83.             if (!_initialized || !isEnabled) return false;
  84.             return ReInput.players.GetPlayer(playerId).GetButtonUp(name);
  85.         }
  86.  
  87.         public bool GetNegativeButton(int playerId, string name)
  88.         {
  89.             if (!_initialized || !isEnabled) return false;
  90.             return ReInput.players.GetPlayer(playerId).GetNegativeButton(name);
  91.         }
  92.  
  93.         #endregion
  94.  
  95.         #region RewiredInputController.ITouchInputUI Implementation
  96.  
  97.         public bool showTouchControls {
  98.             get {
  99.                 return _initialized &&
  100.                     isEnabled &&
  101.                     supportsTouchControls &&
  102.                     _touchControlsActiveExternal &&
  103.                     (_hideTouchControlsWhenJoysticksConnected ? _autoTouchControlUI_isActive : true);
  104.             }
  105.             set {
  106.                 _touchControlsActiveExternal = value;
  107.                 if (value && (!_initialized || !isEnabled || !supportsTouchControls)) return;
  108.                 EvaluateTouchControlsActive();
  109.             }
  110.         }
  111.  
  112.         #endregion
  113.  
  114.         #region RewiredInputController.IInputConfiguration Implementation
  115.  
  116.         public bool showInputConfigurationUI {
  117.             get {
  118.                 if (_controlMapper == null) return false;
  119.                 return _controlMapper.isOpen;
  120.             }
  121.             set {
  122.                 if (_controlMapper == null) return;
  123.                 if (_controlMapper.isOpen == value) return;
  124.                 if (value) {
  125.                     _controlMapper.Open();
  126.                 } else {
  127.                     _controlMapper.Close(true);
  128.                 }
  129.             }
  130.         }
  131.  
  132.         public void ShowInputConfigurationUI(Action closedCallback) {
  133.             if (_controlMapper == null) return;
  134.             _inputConfigurationUIClosedCallback = closedCallback;
  135.             showInputConfigurationUI = true;
  136.         }
  137.  
  138.         #endregion
  139.  
  140.         #region Monobehaviour Callbacks
  141.  
  142.         private void Awake() {
  143.             if (!ReInput.isReady) {
  144.                 Debug.LogError(className + ": Rewired is not initialized. You must have an active Rewired Input Manager in the scene.");
  145.                 return;
  146.             }
  147.  
  148.             if (_eventSystem == null) {
  149.                 _eventSystem = gameObject.GetComponentInChildren<EventSystem>(true);
  150.                 if (_eventSystem == null) {
  151.                     Debug.LogError(className + ": Event System must be linked in the inspector.");
  152.                     return;
  153.                 }
  154.             }
  155.  
  156.             // Disable navigation events in the Rewired Event System initially.
  157.             // This is done because UFE cannot work with navigation events enabled because their UFEScreen system
  158.             // has its own navigation system and will cause double navigation.
  159.             // Control Mapper requires it, so only enable it when Control Mapper is open.
  160.             // Cannot just disable the Rewired Standalone Input Module entirely because Touch Controls wouldn't work.
  161.             _eventSystem.sendNavigationEvents = false;
  162.  
  163.             // Show/hide the touch UI initially
  164.             bool showTouchControls = supportsTouchControls;
  165.             _touchControlsActiveExternal = showTouchControls;
  166.             SetTouchControlsActive(showTouchControls);
  167.  
  168.             if (_hideTouchControlsWhenJoysticksConnected) {
  169.                 _autoTouchControlUI_isActive = showTouchControls;
  170.                 CheckHideTouchControlsWhenJoystickConnected();
  171.             }
  172.  
  173.             _initialized = true;
  174.         }
  175.  
  176.         private void Start() {
  177.  
  178.             // Enable the Rewired event system.
  179.             // Rewired event system must start disabled so UFE doesn't see it and
  180.             // try to use it as the primary event system for its custom navigation system.
  181.             _eventSystem.enabled = true;
  182.  
  183.             // Remove the Standalone Input Module and Event Systems created by UFE.cs in Awake.
  184.             // These just cause problems with Rewired.
  185.             if (UFE.standaloneInputModule != null) {
  186.                 EventSystem eventSystem = UFE.standaloneInputModule.GetComponent<EventSystem>();
  187.                 UnityEngine.Object.DestroyImmediate(UFE.standaloneInputModule);
  188.                 UnityEngine.Object.DestroyImmediate(eventSystem);
  189.             }
  190.  
  191.             // Make sure Rewired is always the current event system because
  192.             // it is the one used for mouse events for the entire application,
  193.             // not just for UI navigation events when Control Mapper is open.
  194.             if (_eventSystem != null) EventSystem.current = _eventSystem;
  195.         }
  196.  
  197.         private void OnEnable() {
  198.             // Set up static references in UFE
  199.             RewiredInputController.inputSource = this;
  200.             if (_touchControls != null) RewiredInputController.touchInputUI = this;
  201.             if (_controlMapper != null) RewiredInputController.inputConfiguration = this;
  202.  
  203.             // Subscribe to events
  204.             ReInput.ControllerConnectedEvent += OnControllerConnected;
  205.             ReInput.ControllerDisconnectedEvent += OnControllerDisconnected;
  206.             if (_controlMapper != null) {
  207.                 _controlMapper.ScreenOpenedEvent += OnControlMapperOpened;
  208.                 _controlMapper.ScreenClosedEvent += OnControlMapperClosed;
  209.             }
  210.         }
  211.  
  212.         private void OnDisable() {
  213.             // Remove static references in UFE
  214.             if (RewiredInputController.inputSource == this as RewiredInputController.IInputSource) {
  215.                 RewiredInputController.inputSource = null;
  216.             }
  217.             if (RewiredInputController.touchInputUI == this as RewiredInputController.ITouchInputUI) {
  218.                 RewiredInputController.touchInputUI = null;
  219.             }
  220.             if (RewiredInputController.inputConfiguration == this as RewiredInputController.IInputConfiguration) {
  221.                 RewiredInputController.inputConfiguration = null;
  222.             }
  223.  
  224.             // Unsubscribe from events
  225.             ReInput.ControllerConnectedEvent -= OnControllerConnected;
  226.             ReInput.ControllerDisconnectedEvent -= OnControllerDisconnected;
  227.             if (_controlMapper != null) {
  228.                 _controlMapper.ScreenOpenedEvent -= OnControlMapperOpened;
  229.                 _controlMapper.ScreenClosedEvent -= OnControlMapperClosed;
  230.             }
  231.         }
  232.  
  233.         #endregion
  234.  
  235.         #region Private Properties
  236.  
  237.         private bool supportsTouchControls {
  238.             get {
  239.                 if (_showTouchControlsOn == PlatformFlags.None) return false;
  240.                 if (_touchControls == null) return false;
  241.  
  242.                 PlatformFlags p = _showTouchControlsOn;
  243.                 if (Rewired.Utils.UnityTools.isEditor && (p & PlatformFlags.Editor) != 0) return true;
  244.  
  245.                 if (!UnityEngine.Input.touchSupported) return false;
  246.  
  247.                 var platform = Rewired.Utils.UnityTools.platform;
  248.                 if (platform == Rewired.Platforms.Platform.Windows) return (p & PlatformFlags.Windows) != 0;
  249.                 if (platform == Rewired.Platforms.Platform.OSX) return (p & PlatformFlags.OSX) != 0;
  250.                 if (platform == Rewired.Platforms.Platform.Linux) return (p & PlatformFlags.Linux) != 0;
  251.                 if (platform == Rewired.Platforms.Platform.iOS) return (p & PlatformFlags.IOS) != 0;
  252.                 if (platform == Rewired.Platforms.Platform.tvOS) return (p & PlatformFlags.TVOS) != 0;
  253.                 if (platform == Rewired.Platforms.Platform.Android) return (p & PlatformFlags.Android) != 0;
  254.                 if (platform == Rewired.Platforms.Platform.AmazonFireTV) return (p & PlatformFlags.AmazonFireTV) != 0;
  255.                 if (platform == Rewired.Platforms.Platform.RazerForgeTV) return (p & PlatformFlags.RazerForgeTV) != 0;
  256.                 if (platform == Rewired.Platforms.Platform.Windows81Store || platform == Rewired.Platforms.Platform.WindowsAppStore) return (p & PlatformFlags.Windows8Store) != 0;
  257.                 if (platform == Rewired.Platforms.Platform.WindowsUWP) return (p & PlatformFlags.WindowsUWP10) != 0;
  258.                 if (platform == Rewired.Platforms.Platform.WebGL) return (p & PlatformFlags.WebGL) != 0;
  259.                 if (platform == Rewired.Platforms.Platform.PS4) return (p & PlatformFlags.PS4) != 0;
  260.                 if (platform == Rewired.Platforms.Platform.PSMobile || platform == Rewired.Platforms.Platform.PSVita) return (p & PlatformFlags.PSVita) != 0;
  261.                 if (platform == Rewired.Platforms.Platform.Xbox360) return (p & PlatformFlags.Xbox360) != 0;
  262.                 if (platform == Rewired.Platforms.Platform.XboxOne) return (p & PlatformFlags.XboxOne) != 0;
  263.                 if (platform == Rewired.Platforms.Platform.SamsungTV) return (p & PlatformFlags.SamsungTV) != 0;
  264.                 if (platform == Rewired.Platforms.Platform.WiiU) return (p & PlatformFlags.WiiU) != 0;
  265.                 if (platform == Rewired.Platforms.Platform.N3DS) return (p & PlatformFlags.Nintendo3DS) != 0;
  266.                 if (platform == Rewired.Platforms.Platform.Switch) return (p & PlatformFlags.Switch) != 0;
  267.                 if (platform == Rewired.Platforms.Platform.Stadia) return (p & PlatformFlags.Stadia) != 0;
  268.                 if (platform == Rewired.Platforms.Platform.GameCoreXboxOne) return (p & PlatformFlags.GameCoreXboxOne) != 0;
  269.                 if (platform == Rewired.Platforms.Platform.GameCoreScarlett) return (p & PlatformFlags.GameCoreScarlett) != 0;
  270.                 return (p & PlatformFlags.Unknown) != 0;
  271.             }
  272.         }
  273.  
  274.         private bool isEnabled { get { return this.isActiveAndEnabled; } }
  275.  
  276.         #endregion
  277.  
  278.         #region Private Methods
  279.  
  280.         private void CheckHideTouchControlsWhenJoystickConnected() {
  281.             if (!_hideTouchControlsWhenJoysticksConnected) return;
  282.  
  283.             if (_autoTouchControlUI_isActive) {
  284.                 // Disable touch controls if a joystick is connected
  285.                 if (Rewired.ReInput.controllers.joystickCount > 0) {
  286.                     _autoTouchControlUI_isActive = false;
  287.                 }
  288.             } else {
  289.                 // Enable touch controls if no joysticks are connected
  290.                 if (Rewired.ReInput.controllers.joystickCount == 0) {
  291.                     _autoTouchControlUI_isActive = true;
  292.                 }
  293.             }
  294.  
  295.             EvaluateTouchControlsActive();
  296.         }
  297.  
  298.         private void EvaluateTouchControlsActive() {
  299.             // Set the active state on the touch controls based on the external state and the auto state
  300.             // Only set active if both external and internal are active
  301.             SetTouchControlsActive(showTouchControls);
  302.         }
  303.  
  304.         private void SetTouchControlsActive(bool active) {
  305.             if (_touchControls == null) return;
  306.             // Make sure Rewired's always the active event system
  307.             if (_eventSystem != null) EventSystem.current = _eventSystem;
  308.             if (_touchControls.activeInHierarchy == active) return;
  309.             _touchControls.SetActive(active);
  310.         }
  311.  
  312.         // Event Handlers
  313.  
  314.         private void OnControllerConnected(Rewired.ControllerStatusChangedEventArgs args) {
  315.             if (args.controllerType != ControllerType.Joystick) return;
  316.             CheckHideTouchControlsWhenJoystickConnected();
  317.         }
  318.  
  319.         private void OnControllerDisconnected(Rewired.ControllerStatusChangedEventArgs args) {
  320.             if (args.controllerType != ControllerType.Joystick) return;
  321.             CheckHideTouchControlsWhenJoystickConnected();
  322.         }
  323.  
  324.         private void OnControlMapperOpened() {
  325.  
  326.             // Disable the UFE event system because their custom UI navigation system causes tons of problems
  327.             _ufeEventSystemEnabledPrev = UFE.eventSystem.enabled;
  328.             UFE.eventSystem.enabled = false;
  329.  
  330.             // Enable Rewired event system so it can be used to by Control Mapper
  331.             if (_eventSystem != null) {
  332.                 _eventSystem.sendNavigationEvents = true; // enable the Rewired Event System events
  333.                 EventSystem.current = _eventSystem;
  334.             }
  335.  
  336.             // Disable Start action to prevent game from unpausing behind Control Mapper
  337.             for (int i = 0; i < ReInput.players.playerCount; i++) {
  338.                 Player player = ReInput.players.Players[i];
  339.                 foreach (var map in player.controllers.maps.GetAllMaps()) {
  340.                     foreach (var a in _actionsToDisableIfControlMapperOpen) {
  341.                         map.ForEachElementMapMatch(x => x.actionId == ReInput.mapping.GetAction(a).id, x => x.enabled = false);
  342.                     }
  343.                 }
  344.             }
  345.         }
  346.  
  347.         private void OnControlMapperClosed() {
  348.  
  349.             // Disable Rewired event system navigation events
  350.             if (_eventSystem != null) _eventSystem.sendNavigationEvents = false;
  351.  
  352.             // Restore the UFE event system enabled state
  353.             StartCoroutine(RestoreUFEEventSystemSettingsDelayed());
  354.  
  355.             // Make sure Rewired's always the active event system
  356.             if (_eventSystem != null) EventSystem.current = _eventSystem;
  357.  
  358.             // Invoke callback
  359.             if (_inputConfigurationUIClosedCallback != null) {
  360.                 try {
  361.                     _inputConfigurationUIClosedCallback();
  362.                 } catch (Exception ex) {
  363.                     Debug.LogError(className + ": An exception occurred while invoking callback.\n" + ex);
  364.                 } finally {
  365.                     _inputConfigurationUIClosedCallback = null; // clear the callback each time
  366.                 }
  367.             }
  368.  
  369.             // Re-enable Start action
  370.             for (int i = 0; i < ReInput.players.playerCount; i++) {
  371.                 Player player = ReInput.players.Players[i];
  372.                 foreach (var map in player.controllers.maps.GetAllMaps()) {
  373.                     foreach (var a in _actionsToDisableIfControlMapperOpen) {
  374.                         map.ForEachElementMapMatch(x => x.actionId == ReInput.mapping.GetAction(a).id, x => x.enabled = true);
  375.                     }
  376.                 }
  377.             }
  378.             ReInput.userDataStore.Save(); // save data again so bindings are saved enabled
  379.         }
  380.  
  381.         System.Collections.IEnumerator RestoreUFEEventSystemSettingsDelayed() {
  382.             // Delay enabling the UFE Event System because it will pick up the key press
  383.             // that closed Control Mapper and will re-open it again.
  384.             // Wait two frames so button down event expires.
  385.             yield return null;
  386.             yield return null;
  387.             UFE.eventSystem.enabled = _ufeEventSystemEnabledPrev;
  388.         }
  389.  
  390.         #endregion
  391.  
  392.         #region Enums
  393.  
  394.         private enum PlatformFlags {
  395.             None = 0,
  396.             Editor = 1,
  397.             Windows = 1 << 1,
  398.             OSX = 1 << 2,
  399.             Linux = 1 << 3,
  400.             IOS = 1 << 4,
  401.             TVOS = 1 << 5,
  402.             Android = 1 << 6,
  403.             Windows8Store = 1 << 7,
  404.             WindowsUWP10 = 1 << 8,
  405.             WebGL = 1 << 9,
  406.             PS4 = 1 << 10,
  407.             PSVita = 1 << 11,
  408.             Xbox360 = 1 << 12,
  409.             XboxOne = 1 << 13,
  410.             SamsungTV = 1 << 14,
  411.             WiiU = 1 << 15,
  412.             Nintendo3DS = 1 << 16,
  413.             Switch = 1 << 17,
  414.             AmazonFireTV = 1 << 18,
  415.             RazerForgeTV = 1 << 19,
  416.             Stadia = 1 << 20,
  417.             GameCoreXboxOne = 1 << 21,
  418.             GameCoreScarlett = 1 << 22,
  419.             Unknown = 1 << 31
  420.         }
  421.  
  422.         #endregion
  423.     }
  424. }
  425.  
  426. namespace UFE3D {
  427.     // This is a placeholder so the using statement doesn't throw an error when used with old versions of UFE which did not have the UFE3D namespace.
  428.     // Added for UFE 2.4.1 where breaking change was made moving classes into namespaces.
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement