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.

101 lines
5.2 KiB

  1. // Base Highlighter|Highlighters|40010
  2. namespace VRTK.Highlighters
  3. {
  4. using UnityEngine;
  5. using System.Collections.Generic;
  6. /// <summary>
  7. /// Provides a base that all highlighters can inherit from.
  8. /// </summary>
  9. /// <remarks>
  10. /// **Script Usage:**
  11. /// > This is an abstract class that is to be inherited to a concrete class that provides highlight functionality, therefore this script should not be directly used.
  12. /// </remarks>
  13. public abstract class VRTK_BaseHighlighter : MonoBehaviour
  14. {
  15. [Tooltip("Determines if this highlighter is the active highlighter for the object the component is attached to. Only one active highlighter can be applied to a GameObject.")]
  16. public bool active = true;
  17. [Tooltip("Determines if the highlighted object should be unhighlighted when it is disabled.")]
  18. public bool unhighlightOnDisable = true;
  19. protected bool usesClonedObject = false;
  20. protected GameObject objectToAffect;
  21. /// <summary>
  22. /// The Initalise method is used to set up the state of the highlighter.
  23. /// </summary>
  24. /// <param name="color">An optional colour may be passed through at point of initialisation in case the highlighter requires it.</param>
  25. /// <param name="affectObject">An optional GameObject to specify which object to apply the highlighting to.</param>
  26. /// <param name="options">An optional dictionary of highlighter specific options that may be differ with highlighter implementations.</param>
  27. public abstract void Initialise(Color? color = null, GameObject affectObject = null, Dictionary<string, object> options = null);
  28. /// <summary>
  29. /// The ResetHighlighter method is used to reset the highlighter if anything on the object has changed. It should be called by any scripts changing object materials or colours.
  30. /// </summary>
  31. public abstract void ResetHighlighter();
  32. /// <summary>
  33. /// The Highlight method is used to initiate the highlighting logic to apply to an object.
  34. /// </summary>
  35. /// <param name="color">An optional colour to highlight the game object to. The highlight colour may already have been set in the `Initialise` method so may not be required here.</param>
  36. /// <param name="duration">An optional duration of how long before the highlight has occured. It can be used by highlighters to fade the colour if possible.</param>
  37. public abstract void Highlight(Color? color = null, float duration = 0f);
  38. /// <summary>
  39. /// The Unhighlight method is used to initiate the logic that returns an object back to it's original appearance.
  40. /// </summary>
  41. /// <param name="color">An optional colour that could be used during the unhighlight phase. Usually will be left as null.</param>
  42. /// <param name="duration">An optional duration of how long before the unhighlight has occured.</param>
  43. public abstract void Unhighlight(Color? color = null, float duration = 0f);
  44. /// <summary>
  45. /// The GetOption method is used to return a value from the options array if the given key exists.
  46. /// </summary>
  47. /// <typeparam name="T">The system type that is expected to be returned.</typeparam>
  48. /// <param name="options">The dictionary of options to check in.</param>
  49. /// <param name="key">The identifier key to look for.</param>
  50. /// <returns>The value in the options at the given key returned in the provided system type.</returns>
  51. public virtual T GetOption<T>(Dictionary<string, object> options, string key)
  52. {
  53. return (T)VRTK_SharedMethods.GetDictionaryValue(options, key, default(T));
  54. }
  55. /// <summary>
  56. /// The UsesClonedObject method is used to return whether the current highlighter creates a cloned object to do the highlighting with.
  57. /// </summary>
  58. /// <returns>Returns `true` if the highlighter creates a cloned object to apply the highlighter on, returns `false` if no additional object is created.</returns>
  59. public virtual bool UsesClonedObject()
  60. {
  61. return usesClonedObject;
  62. }
  63. /// <summary>
  64. /// The GetActiveHighlighter method checks the given GameObject for a valid and active highlighter.
  65. /// </summary>
  66. /// <param name="obj">The GameObject to check for a highlighter on.</param>
  67. /// <returns>A valid and active highlighter.</returns>
  68. public static VRTK_BaseHighlighter GetActiveHighlighter(GameObject obj)
  69. {
  70. VRTK_BaseHighlighter objectHighlighter = null;
  71. VRTK_BaseHighlighter[] foundHighlighters = obj.GetComponents<VRTK_BaseHighlighter>();
  72. for (int i = 0; i < foundHighlighters.Length; i++)
  73. {
  74. VRTK_BaseHighlighter foundHighlighter = foundHighlighters[i];
  75. if (foundHighlighter.active)
  76. {
  77. objectHighlighter = foundHighlighter;
  78. break;
  79. }
  80. }
  81. return objectHighlighter;
  82. }
  83. protected virtual void OnDisable()
  84. {
  85. if (unhighlightOnDisable)
  86. {
  87. Unhighlight();
  88. }
  89. }
  90. }
  91. }