GUICanvas.cs 13 KB

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