| 1 | /************************************************************************************ |
| 2 | Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. |
| 3 | |
| 4 | Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use |
| 5 | the Utilities SDK except in compliance with the License, which is provided at the time of installation |
| 6 | or download, or which otherwise accompanies this software in either electronic or hard copy form. |
| 7 | |
| 8 | You may obtain a copy of the License at |
| 9 | https://developer.oculus.com/licenses/utilities-1.31 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, the Utilities SDK distributed |
| 12 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 13 | ANY KIND, either express or implied. See the License for the specific language governing |
| 14 | permissions and limitations under the License. |
| 15 | ************************************************************************************/ |
| 16 | |
| 17 | using UnityEngine; |
| 18 | using VR = UnityEngine.VR; |
| 19 | using System.Collections; |
| 20 | using System.Collections.Generic; |
| 21 | |
| 22 | /// <summary> |
| 23 | /// Shows the Oculus plaform UI. |
| 24 | /// </summary> |
| 25 | public class OVRPlatformMenu : MonoBehaviour |
| 26 | { |
| 27 | /// <summary> |
| 28 | /// The key code. |
| 29 | /// </summary> |
| 30 | private OVRInput.RawButton inputCode = OVRInput.RawButton.Back; |
| 31 | |
| 32 | public enum eHandler |
| 33 | { |
| 34 | ShowConfirmQuit, |
| 35 | RetreatOneLevel, |
| 36 | }; |
| 37 | |
| 38 | public eHandler shortPressHandler = eHandler.ShowConfirmQuit; |
| 39 | |
| 40 | /// <summary> |
| 41 | /// Callback to handle short press. Returns true if ConfirmQuit menu should be shown. |
| 42 | /// </summary> |
| 43 | public System.Func<bool> OnShortPress; |
| 44 | private static Stack<string> sceneStack = new Stack<string>(); |
| 45 | |
| 46 | enum eBackButtonAction |
| 47 | { |
| 48 | NONE, |
| 49 | SHORT_PRESS |
| 50 | }; |
| 51 | |
| 52 | eBackButtonAction HandleBackButtonState() |
| 53 | { |
| 54 | eBackButtonAction action = eBackButtonAction.NONE; |
| 55 | |
| 56 | if (OVRInput.GetDown(inputCode)) |
| 57 | { |
| 58 | action = eBackButtonAction.SHORT_PRESS; |
| 59 | } |
| 60 | |
| 61 | return action; |
| 62 | } |
| 63 | |
| 64 | /// <summary> |
| 65 | /// Instantiate the cursor timer |
| 66 | /// </summary> |
| 67 | void Awake() |
| 68 | { |
| 69 | if (shortPressHandler == eHandler.RetreatOneLevel && OnShortPress == null) |
| 70 | OnShortPress = RetreatOneLevel; |
| 71 | |
| 72 | if (!OVRManager.isHmdPresent) |
| 73 | { |
| 74 | enabled = false; |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | sceneStack.Push(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name); |
| 79 | } |
| 80 | |
| 81 | /// <summary> |
| 82 | /// Show the confirm quit menu |
| 83 | /// </summary> |
| 84 | void ShowConfirmQuitMenu() |
| 85 | { |
| 86 | #if UNITY_ANDROID && !UNITY_EDITOR |
| 87 | Debug.Log("[PlatformUI-ConfirmQuit] Showing @ " + Time.time); |
| 88 | OVRManager.PlatformUIConfirmQuit(); |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | /// <summary> |
| 93 | /// Sample handler for short press which retreats to the previous scene that used OVRPlatformMenu. |
| 94 | /// </summary> |
| 95 | private static bool RetreatOneLevel() |
| 96 | { |
| 97 | if (sceneStack.Count > 1) |
| 98 | { |
| 99 | string parentScene = sceneStack.Pop(); |
| 100 | UnityEngine.SceneManagement.SceneManager.LoadSceneAsync (parentScene); |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | /// <summary> |
| 108 | /// Tests for long-press and activates global platform menu when detected. |
| 109 | /// as per the Unity integration doc, the back button responds to "mouse 1" button down/up/etc |
| 110 | /// </summary> |
| 111 | void Update() |
| 112 | { |
| 113 | #if UNITY_ANDROID |
| 114 | eBackButtonAction action = HandleBackButtonState(); |
| 115 | if (action == eBackButtonAction.SHORT_PRESS) |
| 116 | { |
| 117 | if (OnShortPress == null || OnShortPress()) |
| 118 | { |
| 119 | ShowConfirmQuitMenu(); |
| 120 | } |
| 121 | } |
| 122 | #endif |
| 123 | } |
| 124 | } |