Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.7 KiB

  1. namespace VRTK.Examples.PanelMenu
  2. {
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /// <summary>
  6. /// Demo GridLayoutGroup component that subscribes to PanelMenuItemController events.
  7. /// </summary>
  8. /// <example>
  9. /// See the demo scene for a complete example: [ 040_Controls_Panel_Menu ]
  10. /// </example>
  11. public class PanelMenuUIGrid : MonoBehaviour
  12. {
  13. #region Variables
  14. public enum Direction
  15. {
  16. None,
  17. Up,
  18. Down,
  19. Left,
  20. Right
  21. }
  22. private readonly Color colorDefault = Color.white;
  23. private readonly Color colorSelected = Color.green;
  24. private readonly float colorAlpha = 0.25f;
  25. private GridLayoutGroup gridLayoutGroup;
  26. private int selectedIndex = 0;
  27. #endregion Variables
  28. #region Unity Methods
  29. private void Start()
  30. {
  31. gridLayoutGroup = GetComponent<GridLayoutGroup>();
  32. if (gridLayoutGroup == null)
  33. {
  34. VRTK_Logger.Warn(VRTK_Logger.GetCommonMessage(VRTK_Logger.CommonMessageKeys.REQUIRED_COMPONENT_MISSING_FROM_GAMEOBJECT, "PanelMenuUIGrid", "GridLayoutGroup", "the same"));
  35. return;
  36. }
  37. GetComponentInParent<VRTK_PanelMenuItemController>().PanelMenuItemSwipeTop += new PanelMenuItemControllerEventHandler(OnPanelMenuItemSwipeTop);
  38. GetComponentInParent<VRTK_PanelMenuItemController>().PanelMenuItemSwipeBottom += new PanelMenuItemControllerEventHandler(OnPanelMenuItemSwipeBottom);
  39. GetComponentInParent<VRTK_PanelMenuItemController>().PanelMenuItemSwipeLeft += new PanelMenuItemControllerEventHandler(OnPanelMenuItemSwipeLeft);
  40. GetComponentInParent<VRTK_PanelMenuItemController>().PanelMenuItemSwipeRight += new PanelMenuItemControllerEventHandler(OnPanelMenuItemSwipeRight);
  41. GetComponentInParent<VRTK_PanelMenuItemController>().PanelMenuItemTriggerPressed += new PanelMenuItemControllerEventHandler(OnPanelMenuItemTriggerPressed);
  42. SetGridLayoutItemSelectedState(selectedIndex);
  43. }
  44. #endregion Unity Methods
  45. #region Interation
  46. public bool MoveSelectGridLayoutItem(Direction direction, GameObject interactableObject)
  47. {
  48. int newIndex = FindNextItemBasedOnMoveDirection(direction);
  49. if (newIndex != selectedIndex)
  50. {
  51. SetGridLayoutItemSelectedState(newIndex);
  52. selectedIndex = newIndex;
  53. }
  54. return true;
  55. }
  56. private int FindNextItemBasedOnMoveDirection(Direction direction)
  57. {
  58. float width = gridLayoutGroup.preferredWidth;
  59. float cellWidth = gridLayoutGroup.cellSize.x;
  60. float spacing = gridLayoutGroup.spacing.x;
  61. int cellsAccross = (int)Mathf.Floor(width / (cellWidth + (spacing / 2))); // quick / dirty
  62. int childCount = gridLayoutGroup.transform.childCount;
  63. switch (direction)
  64. {
  65. case Direction.Up:
  66. int nextUp = selectedIndex - cellsAccross;
  67. return (nextUp >= 0) ? nextUp : selectedIndex;
  68. case Direction.Down:
  69. int nextDown = selectedIndex + cellsAccross;
  70. return (nextDown < childCount) ? nextDown : selectedIndex;
  71. case Direction.Left:
  72. int nextLeft = selectedIndex - 1;
  73. return (nextLeft >= 0) ? nextLeft : selectedIndex;
  74. case Direction.Right:
  75. int nextRight = selectedIndex + 1;
  76. return (nextRight < childCount) ? nextRight : selectedIndex;
  77. default:
  78. return selectedIndex;
  79. }
  80. }
  81. private void SetGridLayoutItemSelectedState(int index)
  82. {
  83. foreach (Transform childTransform in gridLayoutGroup.transform)
  84. {
  85. var child = childTransform.gameObject;
  86. if (child != null)
  87. {
  88. Color color = colorDefault;
  89. color.a = colorAlpha;
  90. child.GetComponent<Image>().color = color;
  91. }
  92. }
  93. var selected = gridLayoutGroup.transform.GetChild(index);
  94. if (selected != null)
  95. {
  96. Color color = colorSelected;
  97. color.a = colorAlpha;
  98. selected.GetComponent<Image>().color = color;
  99. }
  100. }
  101. #endregion Interaction
  102. #region UI Events
  103. private void OnPanelMenuItemSwipeTop(object sender, PanelMenuItemControllerEventArgs e)
  104. {
  105. MoveSelectGridLayoutItem(Direction.Up, e.interactableObject);
  106. }
  107. private void OnPanelMenuItemSwipeBottom(object sender, PanelMenuItemControllerEventArgs e)
  108. {
  109. MoveSelectGridLayoutItem(Direction.Down, e.interactableObject);
  110. }
  111. private void OnPanelMenuItemSwipeLeft(object sender, PanelMenuItemControllerEventArgs e)
  112. {
  113. MoveSelectGridLayoutItem(Direction.Left, e.interactableObject);
  114. }
  115. private void OnPanelMenuItemSwipeRight(object sender, PanelMenuItemControllerEventArgs e)
  116. {
  117. MoveSelectGridLayoutItem(Direction.Right, e.interactableObject);
  118. }
  119. private void OnPanelMenuItemTriggerPressed(object sender, PanelMenuItemControllerEventArgs e)
  120. {
  121. SendMessageToInteractableObject(e.interactableObject);
  122. }
  123. private void SendMessageToInteractableObject(GameObject interactableObject)
  124. {
  125. interactableObject.SendMessage("UpdateGridLayoutValue", selectedIndex);
  126. }
  127. #endregion UI Events
  128. }
  129. }