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.

107 lines
3.8 KiB

  1. namespace VRTK.Examples
  2. {
  3. using UnityEngine;
  4. using Controllables;
  5. using Controllables.PhysicsBased;
  6. using Controllables.ArtificialBased;
  7. public class PusherStickyToggle : MonoBehaviour
  8. {
  9. public VRTK_BaseControllable buttonOne;
  10. public VRTK_BaseControllable buttonTwo;
  11. public Color onColor = Color.green;
  12. public Color offColor = Color.red;
  13. protected bool buttonOnePressed = false;
  14. protected bool buttonTwoPressed = false;
  15. protected virtual void OnEnable()
  16. {
  17. SetStayPressed(buttonOne, true);
  18. SetStayPressed(buttonTwo, true);
  19. buttonOne.MaxLimitReached += ButtonOne_MaxLimitReached;
  20. buttonTwo.MaxLimitReached += ButtonTwo_MaxLimitReached;
  21. buttonOne.MaxLimitExited += ButtonOne_MaxLimitExited;
  22. buttonTwo.MaxLimitExited += ButtonTwo_MaxLimitExited;
  23. }
  24. protected virtual void OnDisable()
  25. {
  26. buttonOne.MaxLimitReached -= ButtonOne_MaxLimitReached;
  27. buttonTwo.MaxLimitReached -= ButtonTwo_MaxLimitReached;
  28. buttonOne.MaxLimitExited -= ButtonOne_MaxLimitExited;
  29. buttonTwo.MaxLimitExited -= ButtonTwo_MaxLimitExited;
  30. }
  31. protected virtual void ButtonOne_MaxLimitReached(object sender, Controllables.ControllableEventArgs e)
  32. {
  33. if (buttonTwoPressed)
  34. {
  35. SetStayPressed(buttonTwo, false);
  36. }
  37. buttonOnePressed = true;
  38. SetPositionTarget(buttonOne, 0f);
  39. ChangeColor(buttonOne.gameObject, onColor);
  40. }
  41. protected virtual void ButtonTwo_MaxLimitReached(object sender, Controllables.ControllableEventArgs e)
  42. {
  43. if (buttonOnePressed)
  44. {
  45. SetStayPressed(buttonOne, false);
  46. }
  47. buttonTwoPressed = true;
  48. SetPositionTarget(buttonTwo, 0f);
  49. ChangeColor(buttonTwo.gameObject, onColor);
  50. }
  51. protected virtual void ButtonOne_MaxLimitExited(object sender, Controllables.ControllableEventArgs e)
  52. {
  53. SetStayPressed(buttonOne, true);
  54. buttonOnePressed = false;
  55. ChangeColor(buttonOne.gameObject, offColor);
  56. }
  57. protected virtual void ButtonTwo_MaxLimitExited(object sender, Controllables.ControllableEventArgs e)
  58. {
  59. SetStayPressed(buttonTwo, true);
  60. buttonTwoPressed = false;
  61. ChangeColor(buttonTwo.gameObject, offColor);
  62. }
  63. protected virtual void ChangeColor(GameObject obj, Color col)
  64. {
  65. obj.GetComponent<Renderer>().material.color = col;
  66. }
  67. protected virtual void SetStayPressed(VRTK_BaseControllable obj, bool state)
  68. {
  69. if (obj.GetType() == typeof(VRTK_PhysicsPusher))
  70. {
  71. VRTK_PhysicsPusher physicsObj = obj as VRTK_PhysicsPusher;
  72. physicsObj.stayPressed = state;
  73. }
  74. else if (obj.GetType() == typeof(VRTK_ArtificialPusher))
  75. {
  76. VRTK_ArtificialPusher artificialObj = obj as VRTK_ArtificialPusher;
  77. artificialObj.SetStayPressed(state);
  78. }
  79. }
  80. protected virtual void SetPositionTarget(VRTK_BaseControllable obj, float newTarget)
  81. {
  82. if (obj.GetType() == typeof(VRTK_PhysicsPusher))
  83. {
  84. VRTK_PhysicsPusher physicsObj = obj as VRTK_PhysicsPusher;
  85. physicsObj.positionTarget = newTarget;
  86. }
  87. else if (obj.GetType() == typeof(VRTK_ArtificialPusher))
  88. {
  89. VRTK_ArtificialPusher artificialObj = obj as VRTK_ArtificialPusher;
  90. artificialObj.SetPositionTarget(newTarget);
  91. }
  92. }
  93. }
  94. }