GUIContent.cs 8.5 KB

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