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.

78 lines
2.3 KiB

  1. namespace VRTK.Examples
  2. {
  3. using UnityEngine;
  4. using UnityEventHelper;
  5. using VRTK.Controllables;
  6. public class ControlReactor : MonoBehaviour
  7. {
  8. public TextMesh go;
  9. #pragma warning disable 0618
  10. protected VRTK_Control_UnityEvents controlEvents;
  11. #pragma warning restore 0618
  12. protected VRTK_BaseControllable controllableEvents;
  13. protected virtual void OnEnable()
  14. {
  15. #pragma warning disable 0618
  16. if (GetComponent<VRTK_Control>() != null && GetComponent<VRTK_Control_UnityEvents>() == null)
  17. {
  18. controlEvents = gameObject.AddComponent<VRTK_Control_UnityEvents>();
  19. }
  20. controlEvents = GetComponent<VRTK_Control_UnityEvents>();
  21. #pragma warning restore 0618
  22. controllableEvents = GetComponent<VRTK_BaseControllable>();
  23. ManageListeners(true);
  24. }
  25. protected virtual void OnDisable()
  26. {
  27. ManageListeners(false);
  28. }
  29. protected virtual void ManageListeners(bool state)
  30. {
  31. if (state)
  32. {
  33. if (controlEvents != null)
  34. {
  35. controlEvents.OnValueChanged.AddListener(HandleChange);
  36. }
  37. if (controllableEvents != null)
  38. {
  39. controllableEvents.ValueChanged += ValueChanged;
  40. }
  41. }
  42. else
  43. {
  44. if (controlEvents != null)
  45. {
  46. controlEvents.OnValueChanged.RemoveListener(HandleChange);
  47. }
  48. if (controllableEvents != null)
  49. {
  50. controllableEvents.ValueChanged -= ValueChanged;
  51. }
  52. }
  53. }
  54. protected virtual void ValueChanged(object sender, ControllableEventArgs e)
  55. {
  56. UpdateText(e.value.ToString("F2"), (e.normalizedValue * 100f).ToString("F0"));
  57. }
  58. protected virtual void HandleChange(object sender, Control3DEventArgs e)
  59. {
  60. UpdateText(e.value.ToString("F2"), (e.normalizedValue * 100f).ToString("F0"));
  61. }
  62. protected virtual void UpdateText(string valueText, string normalizedValueText)
  63. {
  64. if (go != null)
  65. {
  66. go.text = valueText;
  67. }
  68. }
  69. }
  70. }