GUICanvas.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup GUI_Engine
  8. * @{
  9. */
  10. /// <summary>
  11. /// A GUI element that allows the user to draw custom graphics. All drawn elements relative to the canvas, to its origin
  12. /// in the top left corner.
  13. /// </summary>
  14. public sealed class GUICanvas : GUIElement
  15. {
  16. /// <summary>
  17. /// Creates a new canvas element.
  18. /// </summary>
  19. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  20. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  21. /// default element style is used.</param>
  22. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  23. /// override any similar options set by style.</param>
  24. public GUICanvas(string style, params GUIOption[] options)
  25. {
  26. Internal_CreateInstance(this, style, options);
  27. }
  28. /// <summary>
  29. /// Creates a new canvas element.
  30. /// </summary>
  31. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  32. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  33. /// default element style is used.</param>
  34. public GUICanvas(string style = "")
  35. {
  36. Internal_CreateInstance(this, style, new GUIOption[0]);
  37. }
  38. /// <summary>
  39. /// Creates a new canvas element.
  40. /// </summary>
  41. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  42. /// override any similar options set by style.</param>
  43. public GUICanvas(params GUIOption[] options)
  44. {
  45. Internal_CreateInstance(this, "", options);
  46. }
  47. /// <summary>
  48. /// Draws a line going from <paramref name="a"/> to <paramref name="b"/>.
  49. /// </summary>
  50. /// <param name="a">Starting point of the line, relative to the canvas origin (top-left).</param>
  51. /// <param name="b">Ending point of the line, relative to the canvas origin (top-left).</param>
  52. /// <param name="depth">Depth at which to draw the element. Elements with higher depth will be drawn before others.
  53. /// Additionally elements of the same type (triangle or line) will be drawn in order they are
  54. /// submitted if they share the same depth.</param>
  55. public void DrawLine(Vector2I a, Vector2I b, byte depth = 128)
  56. {
  57. Color color = Color.White;
  58. Internal_DrawLine(mCachedPtr, ref a, ref b, ref color, depth);
  59. }
  60. /// <summary>
  61. /// Draws a line going from <paramref name="a"/> to <paramref name="b"/>.
  62. /// </summary>
  63. /// <param name="a">Starting point of the line, relative to the canvas origin (top-left).</param>
  64. /// <param name="b">Ending point of the line, relative to the canvas origin (top-left).</param>
  65. /// <param name="color">Color of the line.</param>
  66. /// <param name="depth">Depth at which to draw the element. Elements with higher depth will be drawn before others.
  67. /// Additionally elements of the same type (triangle or line) will be drawn in order they are
  68. /// submitted if they share the same depth.</param>
  69. public void DrawLine(Vector2I a, Vector2I b, Color color, byte depth = 128)
  70. {
  71. Internal_DrawLine(mCachedPtr, ref a, ref b, ref color, depth);
  72. }
  73. /// <summary>
  74. /// Draws multiple lines following the path by the provided vertices. First vertex connects to the second vertex,
  75. /// and every following vertex connects to the previous vertex.
  76. /// </summary>
  77. /// <param name="vertices">Points to use for drawing the line. Must have at least two elements. All points are
  78. /// relative to the canvas origin(top-left).</param>
  79. /// <param name="depth">Depth at which to draw the element. Elements with higher depth will be drawn before others.
  80. /// Additionally elements of the same type (triangle or line) will be drawn in order they are
  81. /// submitted if they share the same depth.</param>
  82. public void DrawPolyLine(Vector2I[] vertices, byte depth = 128)
  83. {
  84. Color color = Color.White;
  85. Internal_DrawPolyLine(mCachedPtr, vertices, ref color, depth);
  86. }
  87. /// <summary>
  88. /// Draws multiple lines following the path by the provided vertices. First vertex connects to the second vertex,
  89. /// and every following vertex connects to the previous vertex.
  90. /// </summary>
  91. /// <param name="vertices">Points to use for drawing the line. Must have at least two elements. All points are
  92. /// relative to the canvas origin(top-left).</param>
  93. /// <param name="color">Color of the line.</param>
  94. /// <param name="depth">Depth at which to draw the element. Elements with higher depth will be drawn before others.
  95. /// Additionally elements of the same type (triangle or line) will be drawn in order they are
  96. /// submitted if they share the same depth.</param>
  97. public void DrawPolyLine(Vector2I[] vertices, Color color, byte depth = 128)
  98. {
  99. Internal_DrawPolyLine(mCachedPtr, vertices, ref color, depth);
  100. }
  101. /// <summary>
  102. /// Draws a quad with a the provided texture displayed.
  103. /// </summary>
  104. /// <param name="texture">Texture to draw.</param>
  105. /// <param name="area">Position and size of the texture to draw. Position is relative to the canvas origin
  106. /// (top-left). If size is zero, the default texture size will be used.</param>
  107. /// <param name="scaleMode">Scale mode to use when sizing the texture. Only relevant if the provided quad size
  108. /// doesn't match the texture size.</param>
  109. /// <param name="depth">Depth at which to draw the element. Elements with higher depth will be drawn before others.
  110. /// Additionally elements of the same type (triangle or line) will be drawn in order they are
  111. /// submitted if they share the same depth.</param>
  112. public void DrawTexture(SpriteTexture texture, Rect2I area,
  113. GUITextureScaleMode scaleMode = GUITextureScaleMode.StretchToFit, byte depth = 128)
  114. {
  115. IntPtr texturePtr = IntPtr.Zero;
  116. if (texture != null)
  117. texturePtr = texture.GetCachedPtr();
  118. Color color = Color.White;
  119. Internal_DrawTexture(mCachedPtr, texturePtr, ref area, scaleMode, ref color, depth);
  120. }
  121. /// <summary>
  122. /// Draws a quad with a the provided texture displayed.
  123. /// </summary>
  124. /// <param name="texture">Texture to draw.</param>
  125. /// <param name="area">Position and size of the texture to draw. Position is relative to the canvas origin
  126. /// (top-left). If size is zero, the default texture size will be used.</param>
  127. /// <param name="color">Color to tint the drawn texture with.</param>
  128. /// <param name="scaleMode">Scale mode to use when sizing the texture. Only relevant if the provided quad size
  129. /// doesn't match the texture size.</param>
  130. /// <param name="depth">Depth at which to draw the element. Elements with higher depth will be drawn before others.
  131. /// Additionally elements of the same type (triangle or line) will be drawn in order they are
  132. /// submitted if they share the same depth.</param>
  133. public void DrawTexture(SpriteTexture texture, Rect2I area, Color color,
  134. GUITextureScaleMode scaleMode = GUITextureScaleMode.StretchToFit, byte depth = 128)
  135. {
  136. IntPtr texturePtr = IntPtr.Zero;
  137. if (texture != null)
  138. texturePtr = texture.GetCachedPtr();
  139. Internal_DrawTexture(mCachedPtr, texturePtr, ref area, scaleMode, ref color, depth);
  140. }
  141. /// <summary>
  142. /// Draws a triangle strip. First three vertices are used to form the initial triangle, and every next vertex will
  143. /// form a triangle with the previous two.
  144. /// </summary>
  145. /// <param name="vertices">A set of points defining the triangles. Must have at least three elements. All points
  146. /// are relative to the canvas origin(top-left).</param>
  147. /// <param name="depth">Depth at which to draw the element. Elements with higher depth will be drawn before others.
  148. /// Additionally elements of the same type (triangle or line) will be drawn in order they are
  149. /// submitted if they share the same depth.</param>
  150. public void DrawTriangleStrip(Vector2I[] vertices, byte depth = 128)
  151. {
  152. Color color = Color.White;
  153. Internal_DrawTriangleStrip(mCachedPtr, vertices, ref color, depth);
  154. }
  155. /// <summary>
  156. /// Draws a triangle strip. First three vertices are used to form the initial triangle, and every next vertex will
  157. /// form a triangle with the previous two.
  158. /// </summary>
  159. /// <param name="vertices">A set of points defining the triangles. Must have at least three elements. All points
  160. /// are relative to the canvas origin(top-left).</param>
  161. /// <param name="color">Color of the triangles.</param>
  162. /// <param name="depth">Depth at which to draw the element. Elements with higher depth will be drawn before others.
  163. /// Additionally elements of the same type (triangle or line) will be drawn in order they are
  164. /// submitted if they share the same depth.</param>
  165. public void DrawTriangleStrip(Vector2I[] vertices, Color color, byte depth = 128)
  166. {
  167. Internal_DrawTriangleStrip(mCachedPtr, vertices, ref color, depth);
  168. }
  169. /// <summary>
  170. /// Draws a triangle list. Every three vertices in the list represent a unique triangle.
  171. /// </summary>
  172. /// <param name="vertices">A set of points defining the triangles. Must have at least three elements, and its size
  173. /// must be a multiple of three.</param>
  174. /// <param name="depth">Depth at which to draw the element. Elements with higher depth will be drawn before others.
  175. /// Additionally elements of the same type (triangle or line) will be drawn in order they are
  176. /// submitted if they share the same depth.</param>
  177. public void DrawTriangleList(Vector2I[] vertices, byte depth = 128)
  178. {
  179. Color color = Color.White;
  180. Internal_DrawTriangleList(mCachedPtr, vertices, ref color, depth);
  181. }
  182. /// <summary>
  183. /// Draws a triangle list. Every three vertices in the list represent a unique triangle.
  184. /// </summary>
  185. /// <param name="vertices">A set of points defining the triangles. Must have at least three elements, and its size
  186. /// must be a multiple of three.</param>
  187. /// <param name="color">Color of the triangles.</param>
  188. /// <param name="depth">Depth at which to draw the element. Elements with higher depth will be drawn before others.
  189. /// Additionally elements of the same type (triangle or line) will be drawn in order they are
  190. /// submitted if they share the same depth.</param>
  191. public void DrawTriangleList(Vector2I[] vertices, Color color, byte depth = 128)
  192. {
  193. Internal_DrawTriangleList(mCachedPtr, vertices, ref color, depth);
  194. }
  195. /// <summary>
  196. /// Draws a piece of text with the wanted font. The text will be aligned to the top-left corner of the provided
  197. /// position, and will not be word wrapped.
  198. /// </summary>
  199. /// <param name="text">Text to draw.</param>
  200. /// <param name="position">Position of the text to draw. This represents the top-left corner of the text. It is
  201. /// relative to the canvas origin(top-left).</param>
  202. /// <param name="font">Font to draw the text with.</param>
  203. /// <param name="color">Color of the text.</param>
  204. /// <param name="size">Size of the font.</param>
  205. /// <param name="depth">Depth at which to draw the element. Elements with higher depth will be drawn before others.
  206. /// Additionally elements of the same type (triangle or line) will be drawn in order they are
  207. /// submitted if they share the same depth.</param>
  208. public void DrawText(string text, Vector2I position, Font font, Color color, int size = 10, byte depth = 128)
  209. {
  210. IntPtr fontPtr = IntPtr.Zero;
  211. if (font != null)
  212. fontPtr = font.GetCachedPtr();
  213. Internal_DrawText(mCachedPtr, text, ref position, fontPtr, size, ref color, depth);
  214. }
  215. /// <summary>
  216. /// Draws a piece of text with the wanted font. The text will be aligned to the top-left corner of the provided
  217. /// position, and will not be word wrapped.
  218. /// </summary>
  219. /// <param name="text">Text to draw.</param>
  220. /// <param name="position">Position of the text to draw. This represents the top-left corner of the text. It is
  221. /// relative to the canvas origin(top-left).</param>
  222. /// <param name="font">Font to draw the text with.</param>
  223. /// <param name="size">Size of the font.</param>
  224. /// <param name="depth">Depth at which to draw the element. Elements with higher depth will be drawn before others.
  225. /// Additionally elements of the same type (triangle or line) will be drawn in order they are
  226. /// submitted if they share the same depth.</param>
  227. public void DrawText(string text, Vector2I position, Font font, int size = 10, byte depth = 128)
  228. {
  229. IntPtr fontPtr = IntPtr.Zero;
  230. if (font != null)
  231. fontPtr = font.GetCachedPtr();
  232. Color color = Color.White;
  233. Internal_DrawText(mCachedPtr, text, ref position, fontPtr, size, ref color, depth);
  234. }
  235. /// <summary>
  236. /// Clears the canvas, removing any previously drawn elements.
  237. /// </summary>
  238. public void Clear()
  239. {
  240. Internal_Clear(mCachedPtr);
  241. }
  242. [MethodImpl(MethodImplOptions.InternalCall)]
  243. private static extern void Internal_CreateInstance(GUICanvas instance, string style, GUIOption[] options);
  244. [MethodImpl(MethodImplOptions.InternalCall)]
  245. private static extern void Internal_DrawLine(IntPtr nativeInstance, ref Vector2I a, ref Vector2I b,
  246. ref Color color, byte depth);
  247. [MethodImpl(MethodImplOptions.InternalCall)]
  248. private static extern void Internal_DrawPolyLine(IntPtr nativeInstance, Vector2I[] vertices,
  249. ref Color color, byte depth);
  250. [MethodImpl(MethodImplOptions.InternalCall)]
  251. private static extern void Internal_DrawTexture(IntPtr nativeInstance, IntPtr texture, ref Rect2I area,
  252. GUITextureScaleMode scaleMode, ref Color color, byte depth);
  253. [MethodImpl(MethodImplOptions.InternalCall)]
  254. private static extern void Internal_DrawTriangleStrip(IntPtr nativeInstance, Vector2I[] vertices, ref Color color,
  255. byte depth);
  256. [MethodImpl(MethodImplOptions.InternalCall)]
  257. private static extern void Internal_DrawTriangleList(IntPtr nativeInstance, Vector2I[] vertices, ref Color color,
  258. byte depth);
  259. [MethodImpl(MethodImplOptions.InternalCall)]
  260. private static extern void Internal_DrawText(IntPtr nativeInstance, string text, ref Vector2I position,
  261. IntPtr font, int size, ref Color color, byte depth);
  262. [MethodImpl(MethodImplOptions.InternalCall)]
  263. private static extern void Internal_Clear(IntPtr nativeInstance);
  264. }
  265. /** @} */
  266. }