HandleDrawing.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System.Runtime.CompilerServices;
  4. using BansheeEngine;
  5. using System;
  6. namespace BansheeEditor
  7. {
  8. /// <summary>
  9. /// Contains various method that can be used for drawing handles. These methods should only be called from
  10. /// <see cref="Handle.Draw"/> method or its overrides.
  11. /// </summary>
  12. public sealed class HandleDrawing
  13. {
  14. /// <summary>
  15. /// Determines the color that will be used on any following draw method.
  16. /// </summary>
  17. public static Color Color
  18. {
  19. set { Internal_SetColor(ref value); }
  20. }
  21. /// <summary>
  22. /// Determines the world transform that will be applied to any following draw method.
  23. /// </summary>
  24. public static Matrix4 Transform
  25. {
  26. set { Internal_SetTransform(ref value); }
  27. }
  28. /// <summary>
  29. /// Layer bitfield that controls whether a handle is considered visible in a specific camera. Handle layer
  30. /// must match camera layer in order for the camera to render it.
  31. /// </summary>
  32. public static UInt64 Layer
  33. {
  34. set { Internal_SetLayer(value); }
  35. }
  36. /// <summary>
  37. /// Draws an axis aligned solid cube.
  38. /// </summary>
  39. /// <param name="position">World coordinates of the center of the cube.</param>
  40. /// <param name="extents">Extents defining the half-size of the cube in each dimension.</param>
  41. /// <param name="size">Uniform scale to apply on top of the existing transform. Primarily used for maintaining
  42. /// handle size regardless of distance from camera.</param>
  43. public static void DrawCube(Vector3 position, Vector3 extents, float size = 1.0f)
  44. {
  45. Internal_DrawCube(ref position, ref extents, size);
  46. }
  47. /// <summary>
  48. /// Draws a solid sphere.
  49. /// </summary>
  50. /// <param name="position">World coordinates of the center of the sphere.</param>
  51. /// <param name="radius">Sphere radius.</param>
  52. /// <param name="size">Uniform scale to apply on top of the existing transform. Primarily used for maintaining
  53. /// handle size regardless of distance from camera.</param>
  54. public static void DrawSphere(Vector3 position, float radius, float size = 1.0f)
  55. {
  56. Internal_DrawSphere(ref position, radius, size);
  57. }
  58. /// <summary>
  59. /// Draws an axis aligned wireframe cube.
  60. /// </summary>
  61. /// <param name="position">World coordinates of the center of the cube.</param>
  62. /// <param name="extents">Extents defining the half-size of the cube in each dimension.</param>
  63. /// <param name="size">Uniform scale to apply on top of the existing transform. Primarily used for maintaining
  64. /// handle size regardless of distance from camera.</param>
  65. public static void DrawWireCube(Vector3 position, Vector3 extents, float size = 1.0f)
  66. {
  67. Internal_DrawWireCube(ref position, ref extents, size);
  68. }
  69. /// <summary>
  70. /// Draws a wireframe sphere.
  71. /// </summary>
  72. /// <param name="position">World coordinates of the center of the sphere.</param>
  73. /// <param name="radius">Sphere radius.</param>
  74. /// <param name="size">Uniform scale to apply on top of the existing transform. Primarily used for maintaining
  75. /// handle size regardless of distance from camera.</param>
  76. public static void DrawWireSphere(Vector3 position, float radius, float size = 1.0f)
  77. {
  78. Internal_DrawWireSphere(ref position, radius, size);
  79. }
  80. /// <summary>
  81. /// Draws a solid cone.
  82. /// </summary>
  83. /// <param name="coneBase">Location of the cone base (center of the cone disc).</param>
  84. /// <param name="normal">Normal pointing from the base to the cone point.</param>
  85. /// <param name="height">Distance from the origin to the cone point.</param>
  86. /// <param name="radius">Radius of the cone disc.</param>
  87. /// <param name="size">Uniform scale to apply on top of the existing transform. Primarily used for maintaining
  88. /// handle size regardless of distance from camera.</param>
  89. public static void DrawCone(Vector3 coneBase, Vector3 normal, float height, float radius, float size = 1.0f)
  90. {
  91. Internal_DrawCone(ref coneBase, ref normal, height, radius, size);
  92. }
  93. /// <summary>
  94. /// Draws a 3D line.
  95. /// </summary>
  96. /// <param name="start">Starting point for the line.</param>
  97. /// <param name="end">Ending point for the line.</param>
  98. /// <param name="size">Uniform scale to apply on top of the existing transform. Primarily used for maintaining
  99. /// handle size regardless of distance from camera.</param>
  100. public static void DrawLine(Vector3 start, Vector3 end, float size = 1.0f)
  101. {
  102. Internal_DrawLine(ref start, ref end, size);
  103. }
  104. /// <summary>
  105. /// Draws a solid double-sided disc.
  106. /// </summary>
  107. /// <param name="position">Center of the disc.</param>
  108. /// <param name="normal">Normal towards which to orient the disc.</param>
  109. /// <param name="radius">Radius of the disc.</param>
  110. /// <param name="size">Uniform scale to apply on top of the existing transform. Primarily used for maintaining
  111. /// handle size regardless of distance from camera.</param>
  112. public static void DrawDisc(Vector3 position, Vector3 normal, float radius, float size = 1.0f)
  113. {
  114. Internal_DrawDisc(ref position, ref normal, radius, size);
  115. }
  116. /// <summary>
  117. /// Draws a wireframe disc.
  118. /// </summary>
  119. /// <param name="position">Center of the disc.</param>
  120. /// <param name="normal">Normal towards which to orient the disc.</param>
  121. /// <param name="radius">Radius of the disc.</param>
  122. /// <param name="size">Uniform scale to apply on top of the existing transform. Primarily used for maintaining
  123. /// handle size regardless of distance from camera.</param>
  124. public static void DrawWireDisc(Vector3 position, Vector3 normal, float radius, float size = 1.0f)
  125. {
  126. Internal_DrawWireDisc(ref position, ref normal, radius, size);
  127. }
  128. /// <summary>
  129. /// Draws a solid double-sided arc.
  130. /// </summary>
  131. /// <param name="position">Center of the disc out of which the arc is cut out of.</param>
  132. /// <param name="normal">Normal towards which to orient the arc.</param>
  133. /// <param name="radius">Radius of the disc out of which the arc is cut out of.</param>
  134. /// <param name="startAngle">Angle at which the arc starts.</param>
  135. /// <param name="amountAngle">Length of the arc.</param>
  136. /// <param name="size">Uniform scale to apply on top of the existing transform. Primarily used for maintaining
  137. /// handle size regardless of distance from camera.</param>
  138. public static void DrawArc(Vector3 position, Vector3 normal, float radius, Degree startAngle, Degree amountAngle,
  139. float size = 1.0f)
  140. {
  141. Internal_DrawArc(ref position, ref normal, radius, ref startAngle, ref amountAngle, size);
  142. }
  143. /// <summary>
  144. /// Draws a wireframe arc.
  145. /// </summary>
  146. /// <param name="position">Center of the disc out of which the arc is cut out of.</param>
  147. /// <param name="normal">Normal towards which to orient the arc.</param>
  148. /// <param name="radius">Radius of the disc out of which the arc is cut out of.</param>
  149. /// <param name="startAngle">Angle at which the arc starts.</param>
  150. /// <param name="amountAngle">Length of the arc.</param>
  151. /// <param name="size">Uniform scale to apply on top of the existing transform. Primarily used for maintaining
  152. /// handle size regardless of distance from camera.</param>
  153. public static void DrawWireArc(Vector3 position, Vector3 normal, float radius, Degree startAngle,
  154. Degree amountAngle, float size = 1.0f)
  155. {
  156. Internal_DrawWireArc(ref position, ref normal, radius, ref startAngle, ref amountAngle, size);
  157. }
  158. /// <summary>
  159. /// Draws a single-sided rectangle in 3D.
  160. /// </summary>
  161. /// <param name="area">Determines the position, orientation and size of the rectangle.</param>
  162. /// <param name="size">Uniform scale to apply on top of the existing transform. Primarily used for maintaining
  163. /// handle size regardless of distance from camera.</param>
  164. public static void DrawRect(Rect3 area, float size = 1.0f)
  165. {
  166. Vector3 center = area.Center;
  167. Vector3 axisHorz = area.AxisHorz;
  168. Vector3 axisVert = area.AxisVert;
  169. Internal_DrawRect(ref center, ref axisHorz, ref axisVert, area.ExtentHorz, area.ExtentVert, size);
  170. }
  171. /// <summary>
  172. /// Draws camera aligned text at the specified position.
  173. /// </summary>
  174. /// <param name="position">World position to center the text on.</param>
  175. /// <param name="text">String to draw.</param>
  176. /// <param name="font">Font used for drawing the characters.</param>
  177. /// <param name="fontSize">Size of the characters, in points.</param>
  178. /// <param name="size">Uniform scale to apply on top of the existing transform. Primarily used for maintaining
  179. /// handle size regardless of distance from camera.</param>
  180. public static void DrawText(Vector3 position, string text, Font font = null, int fontSize = 16, float size = 1.0f)
  181. {
  182. IntPtr scriptFont = IntPtr.Zero;
  183. if (font != null)
  184. scriptFont = font.GetCachedPtr();
  185. Internal_DrawText(ref position, text, scriptFont, fontSize, size);
  186. }
  187. [MethodImpl(MethodImplOptions.InternalCall)]
  188. private static extern void Internal_SetColor(ref Color color);
  189. [MethodImpl(MethodImplOptions.InternalCall)]
  190. private static extern void Internal_SetTransform(ref Matrix4 transform);
  191. [MethodImpl(MethodImplOptions.InternalCall)]
  192. private static extern void Internal_SetLayer(UInt64 layer);
  193. [MethodImpl(MethodImplOptions.InternalCall)]
  194. private static extern void Internal_DrawCube(ref Vector3 position, ref Vector3 extents, float size);
  195. [MethodImpl(MethodImplOptions.InternalCall)]
  196. private static extern void Internal_DrawSphere(ref Vector3 position, float radius, float size);
  197. [MethodImpl(MethodImplOptions.InternalCall)]
  198. private static extern void Internal_DrawWireCube(ref Vector3 position, ref Vector3 extents, float size);
  199. [MethodImpl(MethodImplOptions.InternalCall)]
  200. private static extern void Internal_DrawWireSphere(ref Vector3 position, float radius, float size);
  201. [MethodImpl(MethodImplOptions.InternalCall)]
  202. private static extern void Internal_DrawCone(ref Vector3 coneBase, ref Vector3 normal, float height, float radius, float size);
  203. [MethodImpl(MethodImplOptions.InternalCall)]
  204. private static extern void Internal_DrawLine(ref Vector3 start, ref Vector3 end, float size);
  205. [MethodImpl(MethodImplOptions.InternalCall)]
  206. private static extern void Internal_DrawDisc(ref Vector3 position, ref Vector3 normal, float radius, float size);
  207. [MethodImpl(MethodImplOptions.InternalCall)]
  208. private static extern void Internal_DrawWireDisc(ref Vector3 position, ref Vector3 normal, float radius, float size);
  209. [MethodImpl(MethodImplOptions.InternalCall)]
  210. private static extern void Internal_DrawArc(ref Vector3 position, ref Vector3 normal, float radius, ref Degree startAngle, ref Degree amountAngle, float size);
  211. [MethodImpl(MethodImplOptions.InternalCall)]
  212. private static extern void Internal_DrawWireArc(ref Vector3 position, ref Vector3 normal, float radius, ref Degree startAngle, ref Degree amountAngle, float size);
  213. [MethodImpl(MethodImplOptions.InternalCall)]
  214. private static extern void Internal_DrawRect(ref Vector3 center, ref Vector3 axisH, ref Vector3 axisV, float extentH, float extentV, float size);
  215. [MethodImpl(MethodImplOptions.InternalCall)]
  216. private static extern void Internal_DrawText(ref Vector3 position, string text, IntPtr font, int fontSize, float size);
  217. }
  218. }