GUICanvas.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. public void DrawLine(Vector2I a, Vector2I b)
  53. {
  54. Color color = Color.White;
  55. Internal_DrawLine(mCachedPtr, ref a, ref b, ref color);
  56. }
  57. /// <summary>
  58. /// Draws a line going from <paramref name="a"/> to <paramref name="b"/>.
  59. /// </summary>
  60. /// <param name="a">Starting point of the line, relative to the canvas origin (top-left).</param>
  61. /// <param name="b">Ending point of the line, relative to the canvas origin (top-left).</param>
  62. /// <param name="color">Color of the line.</param>
  63. public void DrawLine(Vector2I a, Vector2I b, Color color)
  64. {
  65. Internal_DrawLine(mCachedPtr, ref a, ref b, ref color);
  66. }
  67. /// <summary>
  68. /// Draws multiple lines following the path by the provided vertices. First vertex connects to the second vertex,
  69. /// and every following vertex connects to the previous vertex.
  70. /// </summary>
  71. /// <param name="vertices">Points to use for drawing the line. Must have at least two elements. All points are
  72. /// relative to the canvas origin(top-left).</param>
  73. public void DrawPolyLine(Vector2I[] vertices)
  74. {
  75. Color color = Color.White;
  76. Internal_DrawPolyLine(mCachedPtr, vertices, ref color);
  77. }
  78. /// <summary>
  79. /// Draws multiple lines following the path by the provided vertices. First vertex connects to the second vertex,
  80. /// and every following vertex connects to the previous vertex.
  81. /// </summary>
  82. /// <param name="vertices">Points to use for drawing the line. Must have at least two elements. All points are
  83. /// relative to the canvas origin(top-left).</param>
  84. /// <param name="color">Color of the line.</param>
  85. public void DrawPolyLine(Vector2I[] vertices, Color color)
  86. {
  87. Internal_DrawPolyLine(mCachedPtr, vertices, ref color);
  88. }
  89. /// <summary>
  90. /// Draws a quad with a the provided texture displayed.
  91. /// </summary>
  92. /// <param name="texture">Texture to draw.</param>
  93. /// <param name="area">Position and size of the texture to draw. Position is relative to the canvas origin
  94. /// (top-left). If size is zero, the default texture size will be used.</param>
  95. /// <param name="scaleMode">Scale mode to use when sizing the texture. Only relevant if the provided quad size
  96. /// doesn't match the texture size.</param>
  97. public void DrawTexture(SpriteTexture texture, Rect2I area,
  98. GUITextureScaleMode scaleMode = GUITextureScaleMode.StretchToFit)
  99. {
  100. IntPtr texturePtr = IntPtr.Zero;
  101. if (texture != null)
  102. texturePtr = texture.GetCachedPtr();
  103. Color color = Color.White;
  104. Internal_DrawTexture(mCachedPtr, texturePtr, ref area, scaleMode, ref color);
  105. }
  106. /// <summary>
  107. /// Draws a quad with a the provided texture displayed.
  108. /// </summary>
  109. /// <param name="texture">Texture to draw.</param>
  110. /// <param name="area">Position and size of the texture to draw. Position is relative to the canvas origin
  111. /// (top-left). If size is zero, the default texture size will be used.</param>
  112. /// <param name="color">Color to tint the drawn texture with.</param>
  113. /// <param name="scaleMode">Scale mode to use when sizing the texture. Only relevant if the provided quad size
  114. /// doesn't match the texture size.</param>
  115. public void DrawTexture(SpriteTexture texture, Rect2I area, Color color,
  116. GUITextureScaleMode scaleMode = GUITextureScaleMode.StretchToFit)
  117. {
  118. IntPtr texturePtr = IntPtr.Zero;
  119. if (texture != null)
  120. texturePtr = texture.GetCachedPtr();
  121. Internal_DrawTexture(mCachedPtr, texturePtr, ref area, scaleMode, ref color);
  122. }
  123. /// <summary>
  124. /// Draws a triangle strip. First three vertices are used to form the initial triangle, and every next vertex will
  125. /// form a triangle with the previous two.
  126. /// </summary>
  127. /// <param name="vertices">A set of points defining the triangles. Must have at least three elements. All points
  128. /// are relative to the canvas origin(top-left).</param>
  129. public void DrawTriangleStrip(Vector2I[] vertices)
  130. {
  131. Color color = Color.White;
  132. Internal_DrawTriangleStrip(mCachedPtr, vertices, ref color);
  133. }
  134. /// <summary>
  135. /// Draws a triangle strip. First three vertices are used to form the initial triangle, and every next vertex will
  136. /// form a triangle with the previous two.
  137. /// </summary>
  138. /// <param name="vertices">A set of points defining the triangles. Must have at least three elements. All points
  139. /// are relative to the canvas origin(top-left).</param>
  140. /// <param name="color">Color of the triangles.</param>
  141. public void DrawTriangleStrip(Vector2I[] vertices, Color color)
  142. {
  143. Internal_DrawTriangleStrip(mCachedPtr, vertices, ref color);
  144. }
  145. /// <summary>
  146. /// Draws a triangle list. Every three vertices in the list represent a unique triangle.
  147. /// </summary>
  148. /// <param name="vertices">A set of points defining the triangles. Must have at least three elements, and its size
  149. /// must be a multiple of three.</param>
  150. public void DrawTriangleList(Vector2I[] vertices)
  151. {
  152. Color color = Color.White;
  153. Internal_DrawTriangleList(mCachedPtr, vertices, ref color);
  154. }
  155. /// <summary>
  156. /// Draws a triangle list. Every three vertices in the list represent a unique triangle.
  157. /// </summary>
  158. /// <param name="vertices">A set of points defining the triangles. Must have at least three elements, and its size
  159. /// must be a multiple of three.</param>
  160. /// <param name="color">Color of the triangles.</param>
  161. public void DrawTriangleList(Vector2I[] vertices, Color color)
  162. {
  163. Internal_DrawTriangleList(mCachedPtr, vertices, ref color);
  164. }
  165. /// <summary>
  166. /// Draws a piece of text with the wanted font. The text will be aligned to the top-left corner of the provided
  167. /// position, and will not be word wrapped.
  168. /// </summary>
  169. /// <param name="text">Text to draw.</param>
  170. /// <param name="position">Position of the text to draw. This represents the top-left corner of the text. It is
  171. /// relative to the canvas origin(top-left).</param>
  172. /// <param name="font">Font to draw the text with.</param>
  173. /// <param name="color">Color of the text.</param>
  174. /// <param name="size">Size of the font.</param>
  175. public void DrawText(string text, Vector2I position, Font font, Color color, int size = 10)
  176. {
  177. IntPtr fontPtr = IntPtr.Zero;
  178. if (font != null)
  179. fontPtr = font.GetCachedPtr();
  180. Internal_DrawText(mCachedPtr, text, ref position, fontPtr, size, ref color);
  181. }
  182. /// <summary>
  183. /// Draws a piece of text with the wanted font. The text will be aligned to the top-left corner of the provided
  184. /// position, and will not be word wrapped.
  185. /// </summary>
  186. /// <param name="text">Text to draw.</param>
  187. /// <param name="position">Position of the text to draw. This represents the top-left corner of the text. It is
  188. /// relative to the canvas origin(top-left).</param>
  189. /// <param name="font">Font to draw the text with.</param>
  190. /// <param name="size">Size of the font.</param>
  191. public void DrawText(string text, Vector2I position, Font font, int size = 10)
  192. {
  193. IntPtr fontPtr = IntPtr.Zero;
  194. if (font != null)
  195. fontPtr = font.GetCachedPtr();
  196. Color color = Color.White;
  197. Internal_DrawText(mCachedPtr, text, ref position, fontPtr, size, ref color);
  198. }
  199. /// <summary>
  200. /// Clears the canvas, removing any previously drawn elements.
  201. /// </summary>
  202. public void Clear()
  203. {
  204. Internal_Clear(mCachedPtr);
  205. }
  206. [MethodImpl(MethodImplOptions.InternalCall)]
  207. private static extern void Internal_CreateInstance(GUICanvas instance, string style, GUIOption[] options);
  208. [MethodImpl(MethodImplOptions.InternalCall)]
  209. private static extern void Internal_DrawLine(IntPtr nativeInstance, ref Vector2I a, ref Vector2I b,
  210. ref Color color);
  211. [MethodImpl(MethodImplOptions.InternalCall)]
  212. private static extern void Internal_DrawPolyLine(IntPtr nativeInstance, Vector2I[] vertices,
  213. ref Color color);
  214. [MethodImpl(MethodImplOptions.InternalCall)]
  215. private static extern void Internal_DrawTexture(IntPtr nativeInstance, IntPtr texture, ref Rect2I area,
  216. GUITextureScaleMode scaleMode, ref Color color);
  217. [MethodImpl(MethodImplOptions.InternalCall)]
  218. private static extern void Internal_DrawTriangleStrip(IntPtr nativeInstance, Vector2I[] vertices, ref Color color);
  219. [MethodImpl(MethodImplOptions.InternalCall)]
  220. private static extern void Internal_DrawTriangleList(IntPtr nativeInstance, Vector2I[] vertices, ref Color color);
  221. [MethodImpl(MethodImplOptions.InternalCall)]
  222. private static extern void Internal_DrawText(IntPtr nativeInstance, string text, ref Vector2I position,
  223. IntPtr font, int size, ref Color color);
  224. [MethodImpl(MethodImplOptions.InternalCall)]
  225. private static extern void Internal_Clear(IntPtr nativeInstance);
  226. }
  227. /** @} */
  228. }