Navigation.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. using System;
  2. using UnityEngine.Serialization;
  3. namespace UnityEngine.UI
  4. {
  5. [Serializable]
  6. /// <summary>
  7. /// Structure storing details related to navigation.
  8. /// </summary>
  9. public struct Navigation : IEquatable<Navigation>
  10. {
  11. /*
  12. * This looks like it's not flags, but it is flags,
  13. * the reason is that Automatic is considered horizontal
  14. * and verical mode combined
  15. */
  16. [Flags]
  17. /// <summary>
  18. /// Navigation mode enumeration.
  19. /// </summary>
  20. /// <remarks>
  21. /// This looks like it's not flags, but it is flags, the reason is that Automatic is considered horizontal and vertical mode combined
  22. /// </remarks>
  23. /// <example>
  24. /// <code>
  25. /// using UnityEngine;
  26. /// using System.Collections;
  27. /// using UnityEngine.UI; // Required when Using UI elements.
  28. ///
  29. /// public class ExampleClass : MonoBehaviour
  30. /// {
  31. /// public Button button;
  32. ///
  33. /// void Start()
  34. /// {
  35. /// //Set the navigation to the mode "Vertical".
  36. /// if (button.navigation.mode == Navigation.Mode.Vertical)
  37. /// {
  38. /// Debug.Log("The button's navigation mode is Vertical");
  39. /// }
  40. /// }
  41. /// }
  42. /// </code>
  43. /// </example>
  44. public enum Mode
  45. {
  46. /// <summary>
  47. /// No navigation is allowed from this object.
  48. /// </summary>
  49. None = 0,
  50. /// <summary>
  51. /// Horizontal Navigation.
  52. /// </summary>
  53. /// <remarks>
  54. /// Navigation should only be allowed when left / right move events happen.
  55. /// </remarks>
  56. Horizontal = 1,
  57. /// <summary>
  58. /// Vertical navigation.
  59. /// </summary>
  60. /// <remarks>
  61. /// Navigation should only be allowed when up / down move events happen.
  62. /// </remarks>
  63. Vertical = 2,
  64. /// <summary>
  65. /// Automatic navigation.
  66. /// </summary>
  67. /// <remarks>
  68. /// Attempt to find the 'best' next object to select. This should be based on a sensible heuristic.
  69. /// </remarks>
  70. Automatic = 3,
  71. /// <summary>
  72. /// Explicit navigation.
  73. /// </summary>
  74. /// <remarks>
  75. /// User should explicitly specify what is selected by each move event.
  76. /// </remarks>
  77. Explicit = 4,
  78. }
  79. // Which method of navigation will be used.
  80. [SerializeField]
  81. private Mode m_Mode;
  82. [Tooltip("Enables navigation to wrap around from last to first or first to last element. Does not work for automatic grid navigation")]
  83. [SerializeField]
  84. private bool m_WrapAround;
  85. // Game object selected when the joystick moves up. Used when navigation is set to "Explicit".
  86. [SerializeField]
  87. private Selectable m_SelectOnUp;
  88. // Game object selected when the joystick moves down. Used when navigation is set to "Explicit".
  89. [SerializeField]
  90. private Selectable m_SelectOnDown;
  91. // Game object selected when the joystick moves left. Used when navigation is set to "Explicit".
  92. [SerializeField]
  93. private Selectable m_SelectOnLeft;
  94. // Game object selected when the joystick moves right. Used when navigation is set to "Explicit".
  95. [SerializeField]
  96. private Selectable m_SelectOnRight;
  97. /// <summary>
  98. /// Navigation mode.
  99. /// </summary>
  100. public Mode mode { get { return m_Mode; } set { m_Mode = value; } }
  101. /// <summary>
  102. /// Enables navigation to wrap around from last to first or first to last element.
  103. /// Will find the furthest element from the current element in the opposite direction of movement.
  104. /// </summary>
  105. /// <example>
  106. /// Note: If you have a grid of elements and you are on the last element in a row it will not wrap over to the next row it will pick the furthest element in the opposite direction.
  107. /// </example>
  108. public bool wrapAround { get { return m_WrapAround; } set { m_WrapAround = value; } }
  109. /// <summary>
  110. /// Specify a Selectable UI GameObject to highlight when the Up arrow key is pressed.
  111. /// </summary>
  112. /// <example>
  113. /// <code>
  114. /// using UnityEngine;
  115. /// using System.Collections;
  116. /// using UnityEngine.UI; // Required when Using UI elements.
  117. ///
  118. /// public class HighlightOnKey : MonoBehaviour
  119. /// {
  120. /// public Button btnSave;
  121. /// public Button btnLoad;
  122. ///
  123. /// public void Start()
  124. /// {
  125. /// // get the Navigation data
  126. /// Navigation navigation = btnLoad.navigation;
  127. ///
  128. /// // switch mode to Explicit to allow for custom assigned behavior
  129. /// navigation.mode = Navigation.Mode.Explicit;
  130. ///
  131. /// // highlight the Save button if the up arrow key is pressed
  132. /// navigation.selectOnUp = btnSave;
  133. ///
  134. /// // reassign the struct data to the button
  135. /// btnLoad.navigation = navigation;
  136. /// }
  137. /// }
  138. /// </code>
  139. /// </example>
  140. public Selectable selectOnUp { get { return m_SelectOnUp; } set { m_SelectOnUp = value; } }
  141. /// <summary>
  142. /// Specify a Selectable UI GameObject to highlight when the down arrow key is pressed.
  143. /// </summary>
  144. /// <example>
  145. /// <code>
  146. /// using UnityEngine;
  147. /// using System.Collections;
  148. /// using UnityEngine.UI; // Required when Using UI elements.
  149. ///
  150. /// public class HighlightOnKey : MonoBehaviour
  151. /// {
  152. /// public Button btnSave;
  153. /// public Button btnLoad;
  154. ///
  155. /// public void Start()
  156. /// {
  157. /// // get the Navigation data
  158. /// Navigation navigation = btnLoad.navigation;
  159. ///
  160. /// // switch mode to Explicit to allow for custom assigned behavior
  161. /// navigation.mode = Navigation.Mode.Explicit;
  162. ///
  163. /// // highlight the Save button if the down arrow key is pressed
  164. /// navigation.selectOnDown = btnSave;
  165. ///
  166. /// // reassign the struct data to the button
  167. /// btnLoad.navigation = navigation;
  168. /// }
  169. /// }
  170. /// </code>
  171. /// </example>
  172. public Selectable selectOnDown { get { return m_SelectOnDown; } set { m_SelectOnDown = value; } }
  173. /// <summary>
  174. /// Specify a Selectable UI GameObject to highlight when the left arrow key is pressed.
  175. /// </summary>
  176. /// <example>
  177. /// <code>
  178. /// using UnityEngine;
  179. /// using System.Collections;
  180. /// using UnityEngine.UI; // Required when Using UI elements.
  181. ///
  182. /// public class HighlightOnKey : MonoBehaviour
  183. /// {
  184. /// public Button btnSave;
  185. /// public Button btnLoad;
  186. ///
  187. /// public void Start()
  188. /// {
  189. /// // get the Navigation data
  190. /// Navigation navigation = btnLoad.navigation;
  191. ///
  192. /// // switch mode to Explicit to allow for custom assigned behavior
  193. /// navigation.mode = Navigation.Mode.Explicit;
  194. ///
  195. /// // highlight the Save button if the left arrow key is pressed
  196. /// navigation.selectOnLeft = btnSave;
  197. ///
  198. /// // reassign the struct data to the button
  199. /// btnLoad.navigation = navigation;
  200. /// }
  201. /// }
  202. /// </code>
  203. /// </example>
  204. public Selectable selectOnLeft { get { return m_SelectOnLeft; } set { m_SelectOnLeft = value; } }
  205. /// <summary>
  206. /// Specify a Selectable UI GameObject to highlight when the right arrow key is pressed.
  207. /// </summary>
  208. /// <example>
  209. /// <code>
  210. /// using UnityEngine;
  211. /// using System.Collections;
  212. /// using UnityEngine.UI; // Required when Using UI elements.
  213. ///
  214. /// public class HighlightOnKey : MonoBehaviour
  215. /// {
  216. /// public Button btnSave;
  217. /// public Button btnLoad;
  218. ///
  219. /// public void Start()
  220. /// {
  221. /// // get the Navigation data
  222. /// Navigation navigation = btnLoad.navigation;
  223. ///
  224. /// // switch mode to Explicit to allow for custom assigned behavior
  225. /// navigation.mode = Navigation.Mode.Explicit;
  226. ///
  227. /// // highlight the Save button if the right arrow key is pressed
  228. /// navigation.selectOnRight = btnSave;
  229. ///
  230. /// // reassign the struct data to the button
  231. /// btnLoad.navigation = navigation;
  232. /// }
  233. /// }
  234. /// </code>
  235. /// </example>
  236. public Selectable selectOnRight { get { return m_SelectOnRight; } set { m_SelectOnRight = value; } }
  237. /// <summary>
  238. /// Return a Navigation with sensible default values.
  239. /// </summary>
  240. /// <example>
  241. /// <code>
  242. /// using UnityEngine;
  243. /// using System.Collections;
  244. /// using UnityEngine.UI; // Required when Using UI elements.
  245. ///
  246. /// public class ExampleClass : MonoBehaviour
  247. /// {
  248. /// public Button button;
  249. ///
  250. /// void Start()
  251. /// {
  252. /// //Set the navigation to the default value. ("Automatic" is the default value).
  253. /// button.navigation = Navigation.defaultNavigation;
  254. /// }
  255. /// }
  256. /// </code>
  257. /// </example>
  258. static public Navigation defaultNavigation
  259. {
  260. get
  261. {
  262. var defaultNav = new Navigation();
  263. defaultNav.m_Mode = Mode.Automatic;
  264. defaultNav.m_WrapAround = false;
  265. return defaultNav;
  266. }
  267. }
  268. public bool Equals(Navigation other)
  269. {
  270. return mode == other.mode &&
  271. selectOnUp == other.selectOnUp &&
  272. selectOnDown == other.selectOnDown &&
  273. selectOnLeft == other.selectOnLeft &&
  274. selectOnRight == other.selectOnRight;
  275. }
  276. }
  277. }