HandleDrawing.cs 13 KB

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