EditorWindow.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 BansheeEngine;
  6. namespace BansheeEditor
  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. get { return Internal_IsActive(mCachedPtr); }
  74. }
  75. /// <summary>
  76. /// GUI panel that you may use for adding GUI elements to the window.
  77. /// </summary>
  78. public GUIPanel GUI;
  79. /// <summary>
  80. /// Returns a list of all currently open editor windows.
  81. /// </summary>
  82. public static EditorWindow[] AllWindows
  83. {
  84. get { return Internal_GetAllWindows(); }
  85. }
  86. /// <summary>
  87. /// Opens an editor window. If window is already open it returns the existing instance.
  88. /// </summary>
  89. /// <typeparam name="T">Type of window to open.</typeparam>
  90. /// <returns>Instance of the open window.</returns>
  91. public static T OpenWindow<T>() where T : EditorWindow
  92. {
  93. return (T)Internal_CreateOrGetInstance(typeof(T).Namespace, typeof(T).Name);
  94. }
  95. /// <summary>
  96. /// Retrieves instance of an open window.
  97. /// </summary>
  98. /// <typeparam name="T">Type of window to retrieve the instance of.</typeparam>
  99. /// <returns>Instance of the winodow if it is open, null otherwise.</returns>
  100. public static T GetWindow<T>() where T : EditorWindow
  101. {
  102. return (T)Internal_GetInstance(typeof(T).Namespace, typeof(T).Name);
  103. }
  104. /// <summary>
  105. /// Converts coordinates in screen space to coordinates relative to the window.
  106. /// </summary>
  107. /// <param name="screenPos">Coordinates in screen space.</param>
  108. /// <returns>Coordinates relative to the window.</returns>
  109. public Vector2I ScreenToWindowPos(Vector2I screenPos)
  110. {
  111. Vector2I windowPos;
  112. Internal_ScreenToWindowPos(mCachedPtr, ref screenPos, out windowPos);
  113. return windowPos;
  114. }
  115. /// <summary>
  116. /// Converts coordinates relative to the window to screen space to coordinates.
  117. /// </summary>
  118. /// <param name="windowPos">Coordinates relative to the window.</param>
  119. /// <returns>Coordinates in screen space.</returns>
  120. public Vector2I WindowToScreenPos(Vector2I windowPos)
  121. {
  122. Vector2I screenPos;
  123. Internal_WindowToScreenPos(mCachedPtr, ref windowPos, out screenPos);
  124. return screenPos;
  125. }
  126. /// <summary>
  127. /// Triggered whenever the window size ranges.
  128. /// </summary>
  129. /// <param name="width">New width of the window in pixels.</param>
  130. /// <param name="height">New height of the window in pixels.</param>
  131. protected virtual void WindowResized(int width, int height)
  132. {
  133. }
  134. /// <summary>
  135. /// Triggered whenever the window gains or loses focus.
  136. /// </summary>
  137. /// <param name="inFocus">True if focus was gained, false otherwise.</param>
  138. protected virtual void FocusChanged(bool inFocus)
  139. {
  140. }
  141. /// <summary>
  142. /// Name of the window to display in the title bar.
  143. /// </summary>
  144. /// <returns>Name of the window to display in the title bar.</returns>
  145. protected virtual LocString GetDisplayName()
  146. {
  147. return GetType().Name;
  148. }
  149. [MethodImpl(MethodImplOptions.InternalCall)]
  150. private static extern EditorWindow Internal_CreateOrGetInstance(string ns, string typeName);
  151. [MethodImpl(MethodImplOptions.InternalCall)]
  152. private static extern EditorWindow Internal_GetInstance(string ns, string typeName);
  153. [MethodImpl(MethodImplOptions.InternalCall)]
  154. private static extern EditorWindow[] Internal_GetAllWindows();
  155. [MethodImpl(MethodImplOptions.InternalCall)]
  156. private static extern int Internal_GetWidth(IntPtr nativeInstance);
  157. [MethodImpl(MethodImplOptions.InternalCall)]
  158. private static extern int Internal_GetHeight(IntPtr nativeInstance);
  159. [MethodImpl(MethodImplOptions.InternalCall)]
  160. private static extern bool Internal_HasFocus(IntPtr nativeInstance);
  161. [MethodImpl(MethodImplOptions.InternalCall)]
  162. private static extern void Internal_SetFocus(IntPtr nativeInstance, bool focus);
  163. [MethodImpl(MethodImplOptions.InternalCall)]
  164. private static extern bool Internal_IsActive(IntPtr nativeInstance);
  165. [MethodImpl(MethodImplOptions.InternalCall)]
  166. private static extern bool Internal_IsPointerHovering(IntPtr nativeInstance);
  167. [MethodImpl(MethodImplOptions.InternalCall)]
  168. private static extern void Internal_GetBounds(IntPtr nativeInstance, out Rect2I bounds);
  169. [MethodImpl(MethodImplOptions.InternalCall)]
  170. private static extern void Internal_ScreenToWindowPos(IntPtr nativeInstance, ref Vector2I position, out Vector2I windowPos);
  171. [MethodImpl(MethodImplOptions.InternalCall)]
  172. private static extern void Internal_WindowToScreenPos(IntPtr nativeInstance, ref Vector2I position, out Vector2I screenPos);
  173. }
  174. /** @} */
  175. }