2
0

EditorWindow.cs 8.0 KB

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