EditorWindow.cs 7.6 KB

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