HandleDrawing.cs 13 KB

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