GUIContent.cs 7.9 KB

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