using ConjureOS.Input; using UnityEngine; using ConjureOS.ResourcesLoader; #if !ENABLE_INPUT_SYSTEM && ENABLE_LEGACY_INPUT_MANAGER using System; #endif namespace ConjureOS.ArcadeMenu { public class ConjureArcadeMenuController : MonoBehaviour { public const string ArcadeMenuObjectName = "ConjureArcadeMenu"; #if !ENABLE_INPUT_SYSTEM && ENABLE_LEGACY_INPUT_MANAGER private enum ConjureHorizontalAxisPressedDirection { Middle, Left, Right } [SerializeField] private string homeButton = "Home"; [SerializeField] private string selectButton = "Select"; [SerializeField] private string backButton = "Back"; [SerializeField] private string horizontalAxis = "Horizontal"; private bool isHorizontalAxisPressed; #endif public bool IsOpened { get; private set; } = false; private void Start() { #if !ENABLE_INPUT_SYSTEM && ENABLE_LEGACY_INPUT_MANAGER if (!IsButtonAvailable(homeButton)) { Debug.LogWarning( $"ConjureOS: {nameof(homeButton)} in {nameof(ConjureArcadeMenu)} must be a valid action name ('{homeButton}' is not set up). " + "To modify actions, go to 'Edit > Project Settings > Input Manager'."); } if (!IsButtonAvailable(selectButton)) { Debug.LogWarning( $"ConjureOS: {nameof(selectButton)} in {nameof(ConjureArcadeMenu)} must be a valid action name ('{selectButton}' is not set up). " + "To modify actions, go to 'Edit > Project Settings > Input Manager'."); } if (!IsButtonAvailable(backButton)) { Debug.LogWarning( $"ConjureOS: {nameof(backButton)} in {nameof(ConjureArcadeMenu)} must be a valid action name ('{backButton}' is not set up). " + "To modify actions, go to 'Edit > Project Settings > Input Manager'."); } if (!IsAxisAvailable(horizontalAxis)) { Debug.LogWarning( $"ConjureOS: {nameof(horizontalAxis)} in {nameof(ConjureArcadeMenu)} must be a valid action name ('{horizontalAxis}' is not set up). " + "To modify actions, go to 'Edit > Project Settings > Input Manager'."); } #endif CreateArcadeMenu(); } private void CreateArcadeMenu() { if (ConjureArcadeMenu.HasInstance) { return; } ConjureResources resource = ConjureResourceLoader.Resource; if (!resource) { Debug.LogError($"ConjureOS: Could not create the arcade menu because no {nameof(ConjureResources)} was found using the {nameof(ConjureResourceLoader)}."); return; } if (!resource.ConjureMenuPrefab) { Debug.LogError($"ConjureOS: Could not create the arcade menu because no {nameof(resource.ConjureMenuPrefab)} in the {nameof(ConjureResources)}."); return; } GameObject arcadeMenuGameObject = Instantiate(resource.ConjureMenuPrefab, new Vector3(0, 0, 0), Quaternion.identity); if (!arcadeMenuGameObject) { Debug.LogError($"ConjureOS: There was a problem creating the {nameof(arcadeMenuGameObject)}"); return; } arcadeMenuGameObject.name = ArcadeMenuObjectName; if (!arcadeMenuGameObject.GetComponent()) { ConjureArcadeMenu arcadeMenu = arcadeMenuGameObject.AddComponent(); if (!arcadeMenu) { Debug.LogError($"ConjureOS: There was a problem creating the {nameof(arcadeMenu)}"); return; } } DontDestroyOnLoad(arcadeMenuGameObject); } #if !ENABLE_INPUT_SYSTEM && ENABLE_LEGACY_INPUT_MANAGER private bool IsButtonAvailable(string buttonName) { try { UnityEngine.Input.GetButton(buttonName); return true; } catch (ArgumentException) { return false; } } private bool WasButtonPressedThisFrame(string buttonName) { try { return UnityEngine.Input.GetButtonDown(buttonName); } catch (ArgumentException) { return false; } } private bool IsAxisAvailable(string axisName) { try { UnityEngine.Input.GetAxis(axisName); return true; } catch (ArgumentException) { return false; } } private bool WasAxisPressed(string axisName) { try { return UnityEngine.Input.GetAxisRaw(axisName) != 0; } catch (ArgumentException) { return false; } } private ConjureHorizontalAxisPressedDirection GetAxisValue(string axisName) { try { float axisValue = UnityEngine.Input.GetAxisRaw(axisName); if (axisValue > 0) { return ConjureHorizontalAxisPressedDirection.Right; } else if (axisValue < 0) { return ConjureHorizontalAxisPressedDirection.Left; } else { return ConjureHorizontalAxisPressedDirection.Middle; } } catch (ArgumentException) { return ConjureHorizontalAxisPressedDirection.Middle; } } #endif private void Update() { #if ENABLE_INPUT_SYSTEM foreach (ConjureArcadeController arcadeController in ConjureArcadeController.allControllers) { if (arcadeController.home.wasPressedThisFrame) { OpenCloseArcadeMenu(); } else if (arcadeController.stick.left.wasPressedThisFrame) { ConjureArcadeMenu.MovePrevious(); } else if (arcadeController.stick.right.wasPressedThisFrame) { ConjureArcadeMenu.MoveNext(); } else if (arcadeController.button1.wasPressedThisFrame || arcadeController.buttonA.wasPressedThisFrame || arcadeController.start.wasPressedThisFrame) { ConjureArcadeMenu.Select(); } else if (arcadeController.button2.wasPressedThisFrame || arcadeController.buttonB.wasPressedThisFrame) { ConjureArcadeMenu.Close(); } } #elif ENABLE_LEGACY_INPUT_MANAGER if (WasButtonPressedThisFrame(homeButton)) { OpenCloseArcadeMenu(); } else if (WasButtonPressedThisFrame(selectButton)) { ConjureArcadeMenu.Select(); } else if (WasButtonPressedThisFrame(backButton)) { ConjureArcadeMenu.Close(); } if (WasAxisPressed(horizontalAxis)) { if (!isHorizontalAxisPressed) { isHorizontalAxisPressed = true; switch (GetAxisValue(horizontalAxis)) { case ConjureHorizontalAxisPressedDirection.Left: ConjureArcadeMenu.MovePrevious(); break; case ConjureHorizontalAxisPressedDirection.Right: ConjureArcadeMenu.MoveNext(); break; } } } else { isHorizontalAxisPressed = false; } #endif } private void OpenCloseArcadeMenu() { if (ConjureArcadeMenu.IsOpened) { ConjureArcadeMenu.Close(); } else { ConjureArcadeMenu.Open(); } } } }