GUIContent.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Runtime.CompilerServices;
  4. namespace BansheeEngine
  5. {
  6. /** @addtogroup GUI_Engine
  7. * @{
  8. */
  9. /// <summary>
  10. /// Type of GUI element states.
  11. /// </summary>
  12. public enum GUIElementState
  13. {
  14. /// <summary>Normal state when button is not being iteracted with.</summary>
  15. Normal = 0x01,
  16. /// <summary>State when pointer is hovering over the button.</summary>
  17. Hover = 0x02,
  18. /// <summary>State when button is being clicked.</summary>
  19. Active = 0x04,
  20. /// <summary>State when button has been selected.</summary>
  21. Focused = 0x08,
  22. /// <summary>Normal state when button is not being iteracted with and is in "on" state.</summary>
  23. NormalOn = 0x11,
  24. /// <summary>State when pointer is hovering over the button and is in "on" state.</summary>
  25. HoverOn = 0x12,
  26. /// <summary>State when button is being clicked and is in "on" state.</summary>
  27. ActiveOn = 0x14,
  28. /// <summary>State when button has been selected and is in "on" state.</summary>
  29. FocusedOn = 0x18
  30. };
  31. /// <summary>
  32. /// Holds data used for displaying content in a GUIElement. Content can consist of a string, image, a tooltip or none
  33. /// of those.
  34. /// </summary>
  35. public sealed class GUIContent
  36. {
  37. private LocString text;
  38. private LocString tooltip;
  39. private GUIContentImages images;
  40. /// <summary>
  41. /// Returns string content (if any).
  42. /// </summary>
  43. public LocString Text
  44. {
  45. get { return text; }
  46. }
  47. /// <summary>
  48. /// Returns image content (if any).
  49. /// </summary>
  50. public SpriteTexture GetImage(GUIElementState state = GUIElementState.Normal)
  51. {
  52. switch (state)
  53. {
  54. case GUIElementState.Normal:
  55. return images.normal;
  56. case GUIElementState.Hover:
  57. return images.hover;
  58. case GUIElementState.Active:
  59. return images.active;
  60. case GUIElementState.Focused:
  61. return images.focused;
  62. case GUIElementState.NormalOn:
  63. return images.normalOn;
  64. case GUIElementState.HoverOn:
  65. return images.hoverOn;
  66. case GUIElementState.ActiveOn:
  67. return images.activeOn;
  68. case GUIElementState.FocusedOn:
  69. return images.focusedOn;
  70. default:
  71. return images.normal;
  72. }
  73. }
  74. /// <summary>
  75. /// Returns tooltip content (if any).
  76. /// </summary>
  77. public LocString Tooltip
  78. {
  79. get { return tooltip; }
  80. }
  81. /// <summary>
  82. /// Constructs content with just a string.
  83. /// </summary>
  84. /// <param name="text">Textual portion of the content to be displayed as label in GUI elements.</param>
  85. public GUIContent(LocString text)
  86. {
  87. this.text = text;
  88. }
  89. /// <summary>
  90. /// Constructs content with a string and a tooltip.
  91. /// </summary>
  92. /// <param name="text">Textual portion of the content to be displayed as label in GUI elements.</param>
  93. /// <param name="tooltip">Tooltip to be displayed when user hovers over a GUI element.</param>
  94. public GUIContent(LocString text, LocString tooltip)
  95. {
  96. this.text = text;
  97. this.tooltip = tooltip;
  98. }
  99. /// <summary>
  100. /// Constructs content with just an image.
  101. /// </summary>
  102. /// <param name="image">Image to be displayed on top of the GUI element.</param>
  103. public GUIContent(GUIContentImages image)
  104. {
  105. this.images = image;
  106. }
  107. /// <summary>
  108. /// Constructs content with an image and a tooltip.
  109. /// </summary>
  110. /// <param name="image">Image to be displayed on top of the GUI element.</param>
  111. /// <param name="tooltip">Tooltip to be displayed when user hovers over a GUI element.</param>
  112. public GUIContent(GUIContentImages image, LocString tooltip)
  113. {
  114. this.images = image;
  115. this.tooltip = tooltip;
  116. }
  117. /// <summary>
  118. /// Constructs content with a string and an image.
  119. /// </summary>
  120. /// <param name="text">Textual portion of the content to be displayed as label in GUI elements.</param>
  121. /// <param name="image">Image to be displayed on top of the GUI element. Image will be placed to the right or to the
  122. /// left of the text depending on active GUI style.</param>
  123. public GUIContent(LocString text, GUIContentImages image)
  124. {
  125. this.text = text;
  126. this.images = image;
  127. }
  128. /// <summary>
  129. /// Constructs content with a string, an image and a tooltip.
  130. /// </summary>
  131. /// <param name="text">Textual portion of the content to be displayed as label in GUI elements.</param>
  132. /// <param name="image">Image to be displayed on top of the GUI element. Image will be placed to the right or to the
  133. /// left of the text depending on active GUI style.</param>
  134. /// <param name="tooltip">Tooltip to be displayed when user hovers over a GUI element.</param>
  135. public GUIContent(LocString text, GUIContentImages image, LocString tooltip)
  136. {
  137. this.text = text;
  138. this.images = image;
  139. this.tooltip = tooltip;
  140. }
  141. /// <summary>
  142. /// Implicitly converts a localized string into a GUI content containing only text.
  143. /// </summary>
  144. /// <param name="text">Localized string to initialize the GUI content with.</param>
  145. /// <returns>GUI content containing only a string.</returns>
  146. public static implicit operator GUIContent(LocString text)
  147. {
  148. return new GUIContent(text);
  149. }
  150. /// <summary>
  151. /// Implicitly converts a string into a GUI content containing only text.
  152. /// </summary>
  153. /// <param name="text">String to initialize the GUI content with.</param>
  154. /// <returns>GUI content containing only a string.</returns>
  155. public static implicit operator GUIContent(string text)
  156. {
  157. return new GUIContent(new LocString(text));
  158. }
  159. }
  160. /// <summary>
  161. /// Contains separate GUI content images for every possible GUI element state.
  162. /// </summary>
  163. public sealed class GUIContentImages
  164. {
  165. /// <summary>
  166. /// Creates an empty content image object referencing no images.
  167. /// </summary>
  168. public GUIContentImages()
  169. { }
  170. /// <summary>
  171. /// Creates a new object where content images for all states are the same.
  172. /// </summary>
  173. /// <param name="image">Image to assign to all states.</param>
  174. public GUIContentImages(SpriteTexture image)
  175. {
  176. normal = image;
  177. hover = image;
  178. active = image;
  179. focused = image;
  180. normalOn = image;
  181. hoverOn = image;
  182. activeOn = image;
  183. focusedOn = image;
  184. }
  185. /// <summary>
  186. /// Creates a new object where content images for on and off states are different.
  187. /// </summary>
  188. /// <param name="imageOff">Image to assign to all off states.</param>
  189. /// <param name="imageOn">Image to assign to all on states.</param>
  190. public GUIContentImages(SpriteTexture imageOff, SpriteTexture imageOn)
  191. {
  192. normal = imageOff;
  193. hover = imageOff;
  194. active = imageOff;
  195. focused = imageOff;
  196. normalOn = imageOn;
  197. hoverOn = imageOn;
  198. activeOn = imageOn;
  199. focusedOn = imageOn;
  200. }
  201. /// <summary>
  202. /// Implicitly converts a sprite texture into a GUI content images object.
  203. /// </summary>
  204. /// <param name="image">Image to instantiate the GUI content images with.</param>
  205. /// <returns>GUI content images with all states set to the provided image.</returns>
  206. public static implicit operator GUIContentImages(SpriteTexture image)
  207. {
  208. return new GUIContentImages(image);
  209. }
  210. public SpriteTexture normal;
  211. public SpriteTexture hover;
  212. public SpriteTexture active;
  213. public SpriteTexture focused;
  214. public SpriteTexture normalOn;
  215. public SpriteTexture hoverOn;
  216. public SpriteTexture activeOn;
  217. public SpriteTexture focusedOn;
  218. }
  219. /** @} */
  220. }