EditorWindow.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. using bs;
  6. namespace bs.Editor
  7. {
  8. /** @addtogroup Window
  9. * @{
  10. */
  11. /// <summary>
  12. /// Base class for all editor windows. Editor window can be docked on the main editor window or float as a separate
  13. /// window. User is allowed to reposition and resize the window as he wishes. Editor window GUI is fully customizable.
  14. /// </summary>
  15. public class EditorWindow : ScriptObject
  16. {
  17. #pragma warning disable 649 // Value assigned by the runtime
  18. private UndoRedo undoRedo;
  19. #pragma warning restore 649
  20. /// <summary>
  21. /// Width of the window in pixels.
  22. /// </summary>
  23. public int Width
  24. {
  25. get { return Internal_GetWidth(mCachedPtr); }
  26. }
  27. /// <summary>
  28. /// Height of the window in pixels.
  29. /// </summary>
  30. public int Height
  31. {
  32. get { return Internal_GetHeight(mCachedPtr); }
  33. }
  34. /// <summary>
  35. /// Screen space bounds of the window.
  36. /// </summary>
  37. public Rect2I Bounds
  38. {
  39. get
  40. {
  41. Rect2I bounds;
  42. Internal_GetBounds(mCachedPtr, out bounds);
  43. return bounds;
  44. }
  45. }
  46. /// <summary>
  47. /// Determines whether the editor window currently has keyboard focus (has been clicked on most recently). Window
  48. /// that isn't active (is not the active tab) cannot be put into focus without activating it first.
  49. /// </summary>
  50. public bool HasFocus
  51. {
  52. get { return Internal_HasFocus(mCachedPtr); }
  53. set { Internal_SetFocus(mCachedPtr, value); }
  54. }
  55. /// <summary>
  56. /// Returns the local undo/redo stack specific to this editor window. Only windows marked with [UndoRedoLocal]
  57. /// attribute will return a non-null value.
  58. /// </summary>
  59. public UndoRedo UndoRedo
  60. {
  61. get { return undoRedo; }
  62. }
  63. /// <summary>
  64. /// Determines is the mouse pointer currently hovering over the editor window.
  65. /// </summary>
  66. public bool IsPointerHovering { get { return Internal_IsPointerHovering(mCachedPtr); } }
  67. /// <summary>
  68. /// Checks if the window's tab is currently active. If the window is floating or not sharing space with any other
  69. /// windows (just a single tab), it is always considered active.
  70. /// </summary>
  71. public bool Active
  72. {
  73. set { Internal_SetActive(mCachedPtr, value); }
  74. get { return Internal_IsActive(mCachedPtr); }
  75. }
  76. /// <summary>
  77. /// GUI panel that you may use for adding GUI elements to the window.
  78. /// </summary>
  79. public GUIPanel GUI;
  80. /// <summary>
  81. /// Returns a list of all currently open editor windows.
  82. /// </summary>
  83. public static EditorWindow[] AllWindows
  84. {
  85. get { return Internal_GetAllWindows(); }
  86. }
  87. /// <summary>
  88. /// Opens an editor window. If window is already open it returns the existing instance.
  89. /// </summary>
  90. /// <typeparam name="T">Type of window to open.</typeparam>
  91. /// <returns>Instance of the open window.</returns>
  92. public static T OpenWindow<T>() where T : EditorWindow
  93. {
  94. return (T)Internal_CreateOrGetInstance(typeof(T).Namespace, typeof(T).Name);
  95. }
  96. /// <summary>
  97. /// Retrieves instance of an open window.
  98. /// </summary>
  99. /// <typeparam name="T">Type of window to retrieve the instance of.</typeparam>
  100. /// <returns>Instance of the winodow if it is open, null otherwise.</returns>
  101. public static T GetWindow<T>() where T : EditorWindow
  102. {
  103. return (T)Internal_GetInstance(typeof(T).Namespace, typeof(T).Name);
  104. }
  105. /// <summary>
  106. /// Converts coordinates in screen space to coordinates relative to the window.
  107. /// </summary>
  108. /// <param name="screenPos">Coordinates in screen space.</param>
  109. /// <returns>Coordinates relative to the window.</returns>
  110. public Vector2I ScreenToWindowPos(Vector2I screenPos)
  111. {
  112. Vector2I windowPos;
  113. Internal_ScreenToWindowPos(mCachedPtr, ref screenPos, out windowPos);
  114. return windowPos;
  115. }
  116. /// <summary>
  117. /// Converts coordinates relative to the window to screen space to coordinates.
  118. /// </summary>
  119. /// <param name="windowPos">Coordinates relative to the window.</param>
  120. /// <returns>Coordinates in screen space.</returns>
  121. public Vector2I WindowToScreenPos(Vector2I windowPos)
  122. {
  123. Vector2I screenPos;
  124. Internal_WindowToScreenPos(mCachedPtr, ref windowPos, out screenPos);
  125. return screenPos;
  126. }
  127. /// <summary>
  128. /// Triggered whenever the window size ranges.
  129. /// </summary>
  130. /// <param name="width">New width of the window in pixels.</param>
  131. /// <param name="height">New height of the window in pixels.</param>
  132. protected virtual void WindowResized(int width, int height)
  133. {
  134. }
  135. /// <summary>
  136. /// Triggered whenever the window gains or loses focus.
  137. /// </summary>
  138. /// <param name="inFocus">True if focus was gained, false otherwise.</param>
  139. protected virtual void FocusChanged(bool inFocus)
  140. {
  141. }
  142. /// <summary>
  143. /// Name of the window to display in the title bar.
  144. /// </summary>
  145. /// <returns>Name of the window to display in the title bar.</returns>
  146. protected virtual LocString GetDisplayName()
  147. {
  148. return GetType().Name;
  149. }
  150. [MethodImpl(MethodImplOptions.InternalCall)]
  151. private static extern EditorWindow Internal_CreateOrGetInstance(string ns, string typeName);
  152. [MethodImpl(MethodImplOptions.InternalCall)]
  153. private static extern EditorWindow Internal_GetInstance(string ns, string typeName);
  154. [MethodImpl(MethodImplOptions.InternalCall)]
  155. private static extern EditorWindow[] Internal_GetAllWindows();
  156. [MethodImpl(MethodImplOptions.InternalCall)]
  157. private static extern int Internal_GetWidth(IntPtr nativeInstance);
  158. [MethodImpl(MethodImplOptions.InternalCall)]
  159. private static extern int Internal_GetHeight(IntPtr nativeInstance);
  160. [MethodImpl(MethodImplOptions.InternalCall)]
  161. private static extern bool Internal_HasFocus(IntPtr nativeInstance);
  162. [MethodImpl(MethodImplOptions.InternalCall)]
  163. private static extern void Internal_SetFocus(IntPtr nativeInstance, bool focus);
  164. [MethodImpl(MethodImplOptions.InternalCall)]
  165. private static extern bool Internal_SetActive(IntPtr nativeInstance, bool active);
  166. [MethodImpl(MethodImplOptions.InternalCall)]
  167. private static extern bool Internal_IsActive(IntPtr nativeInstance);
  168. [MethodImpl(MethodImplOptions.InternalCall)]
  169. private static extern bool Internal_IsPointerHovering(IntPtr nativeInstance);
  170. [MethodImpl(MethodImplOptions.InternalCall)]
  171. private static extern void Internal_GetBounds(IntPtr nativeInstance, out Rect2I bounds);
  172. [MethodImpl(MethodImplOptions.InternalCall)]
  173. private static extern void Internal_ScreenToWindowPos(IntPtr nativeInstance, ref Vector2I position, out Vector2I windowPos);
  174. [MethodImpl(MethodImplOptions.InternalCall)]
  175. private static extern void Internal_WindowToScreenPos(IntPtr nativeInstance, ref Vector2I position, out Vector2I screenPos);
  176. }
  177. /** @} */
  178. }