// Panel Menu|Prefabs|0130
namespace VRTK
{
using System.Collections;
using UnityEngine;
///
/// Adds a top-level controller to handle the display of up to four child PanelMenuItemController items which are displayed as a canvas UI panel.
///
///
/// **Prefab Usage:**
/// * Place the `VRTK/Prefabs/PanelMenu/PanelMenu` prefab as a child of the `VRTK_InteractableObject` the panel menu is for.
/// * Optionally remove the panel control menu item child GameObjects if they are not required, e.g. `PanelTopControls`.
/// * Set the panel menu item controllers on the `VRTK_PanelMenuController` script to determine which panel control menu items are available.
/// * The available panel control menu items can be activated by pressing the corresponding direction on the touchpad.
///
///
/// `040_Controls_Panel_Menu` contains three basic interactive object examples of the PanelMenu in use.
///
public class VRTK_PanelMenuController : MonoBehaviour
{
public enum TouchpadPressPosition
{
None,
Top,
Bottom,
Left,
Right
}
[Tooltip("The GameObject the panel should rotate towards, which is the Camera (eye) by default.")]
public GameObject rotateTowards;
[Tooltip("The scale multiplier, which relates to the scale of parent interactable object.")]
public float zoomScaleMultiplier = 1f;
[Tooltip("The top PanelMenuItemController, which is triggered by pressing up on the controller touchpad.")]
public VRTK_PanelMenuItemController topPanelMenuItemController;
[Tooltip("The bottom PanelMenuItemController, which is triggered by pressing down on the controller touchpad.")]
public VRTK_PanelMenuItemController bottomPanelMenuItemController;
[Tooltip("The left PanelMenuItemController, which is triggered by pressing left on the controller touchpad.")]
public VRTK_PanelMenuItemController leftPanelMenuItemController;
[Tooltip("The right PanelMenuItemController, which is triggered by pressing right on the controller touchpad.")]
public VRTK_PanelMenuItemController rightPanelMenuItemController;
// Relates to scale of canvas on panel items.
protected const float CanvasScaleSize = 0.001f;
// Swipe sensitivity / detection.
protected const float AngleTolerance = 30f;
protected const float SwipeMinDist = 0.2f;
protected const float SwipeMinVelocity = 4.0f;
protected VRTK_ControllerEvents controllerEvents;
protected VRTK_PanelMenuItemController currentPanelMenuItemController;
protected GameObject interactableObject;
protected GameObject canvasObject;
protected readonly Vector2 xAxis = new Vector2(1, 0);
protected readonly Vector2 yAxis = new Vector2(0, 1);
protected Vector2 touchStartPosition;
protected Vector2 touchEndPosition;
protected float touchStartTime;
protected float currentAngle;
protected bool isTrackingSwipe = false;
protected bool isPendingSwipeCheck = false;
protected bool isGrabbed = false;
protected bool isShown = false;
protected Coroutine tweenMenuScaleRoutine;
///
/// The ToggleMenu method is used to show or hide the menu.
///
public virtual void ToggleMenu()
{
if (isShown)
{
HideMenu(true);
}
else
{
ShowMenu();
}
}
///
/// The ShowMenu method is used to show the menu.
///
public virtual void ShowMenu()
{
if (!isShown)
{
isShown = true;
InitTweenMenuScale(isShown);
}
}
///
/// The HideMenu method is used to hide the menu.
///
/// If true then the menu is always hidden.
public virtual void HideMenu(bool force)
{
if (isShown && force)
{
isShown = false;
InitTweenMenuScale(isShown);
}
}
///
/// The HideMenuImmediate method is used to immediately hide the menu.
///
public virtual void HideMenuImmediate()
{
if (currentPanelMenuItemController != null && isShown)
{
HandlePanelMenuItemControllerVisibility(currentPanelMenuItemController);
}
transform.localScale = Vector3.zero;
canvasObject.transform.localScale = Vector3.zero;
isShown = false;
}
protected virtual void Awake()
{
Initialize();
VRTK_SDKManager.AttemptAddBehaviourToToggleOnLoadedSetupChange(this);
}
protected virtual void Start()
{
interactableObject = gameObject.transform.parent.gameObject;
if (interactableObject == null || interactableObject.GetComponent() == null)
{
VRTK_Logger.Warn(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_GAMEOBJECT, "PanelMenuController", "VRTK_InteractableObject", "a parent"));
return;
}
interactableObject.GetComponent().InteractableObjectGrabbed += new InteractableObjectEventHandler(DoInteractableObjectIsGrabbed);
interactableObject.GetComponent().InteractableObjectUngrabbed += new InteractableObjectEventHandler(DoInteractableObjectIsUngrabbed);
canvasObject = gameObject.transform.GetChild(0).gameObject;
if (canvasObject == null || canvasObject.GetComponent