EditorWindow.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. /// <summary>
  18. /// Width of the window in pixels.
  19. /// </summary>
  20. public int Width
  21. {
  22. get { return Internal_GetWidth(mCachedPtr); }
  23. }
  24. /// <summary>
  25. /// Height of the window in pixels.
  26. /// </summary>
  27. public int Height
  28. {
  29. get { return Internal_GetHeight(mCachedPtr); }
  30. }
  31. /// <summary>
  32. /// Screen space bounds of the window.
  33. /// </summary>
  34. public Rect2I Bounds
  35. {
  36. get
  37. {
  38. Rect2I bounds;
  39. Internal_GetBounds(mCachedPtr, out bounds);
  40. return bounds;
  41. }
  42. }
  43. /// <summary>
  44. /// Determines whether the editor window currently has keyboard focus (has been clicked on most recently). Window
  45. /// that isn't active (is not the active tab) cannot be put into focus without activating it first.
  46. /// </summary>
  47. public bool HasFocus
  48. {
  49. get { return Internal_HasFocus(mCachedPtr); }
  50. set { Internal_SetFocus(mCachedPtr, value); }
  51. }
  52. /// <summary>
  53. /// Determines is the mouse pointer currently hovering over the editor window.
  54. /// </summary>
  55. public bool IsPointerHovering { get { return Internal_IsPointerHovering(mCachedPtr); } }
  56. /// <summary>
  57. /// Checks if the window's tab is currently active. If the window is floating or not sharing space with any other
  58. /// windows (just a single tab), it is always considered active.
  59. /// </summary>
  60. public bool Active { get { return Internal_IsActive(mCachedPtr); } }
  61. protected GUIPanel GUI;
  62. /// <summary>
  63. /// Opens an editor window. If window is already open it returns the existing instance.
  64. /// </summary>
  65. /// <typeparam name="T">Type of window to open.</typeparam>
  66. /// <returns>Instance of the open window.</returns>
  67. public static T OpenWindow<T>() where T : EditorWindow
  68. {
  69. return (T)Internal_CreateOrGetInstance(typeof(T).Namespace, typeof(T).Name);
  70. }
  71. /// <summary>
  72. /// Retrieves instance of an open window.
  73. /// </summary>
  74. /// <typeparam name="T">Type of window to retrieve the instance of.</typeparam>
  75. /// <returns>Instance of the winodow if it is open, null otherwise.</returns>
  76. public static T GetWindow<T>() where T : EditorWindow
  77. {
  78. return (T)Internal_GetInstance(typeof(T).Namespace, typeof(T).Name);
  79. }
  80. /// <summary>
  81. /// Converts coordinates in screen space to coordinates relative to the window.
  82. /// </summary>
  83. /// <param name="screenPos">Coordinates in screen space.</param>
  84. /// <returns>Coordinates relative to the window.</returns>
  85. public Vector2I ScreenToWindowPos(Vector2I screenPos)
  86. {
  87. Vector2I windowPos;
  88. Internal_ScreenToWindowPos(mCachedPtr, ref screenPos, out windowPos);
  89. return windowPos;
  90. }
  91. /// <summary>
  92. /// Converts coordinates relative to the window to screen space to coordinates.
  93. /// </summary>
  94. /// <param name="windowPos">Coordinates relative to the window.</param>
  95. /// <returns>Coordinates in screen space.</returns>
  96. public Vector2I WindowToScreenPos(Vector2I windowPos)
  97. {
  98. Vector2I screenPos;
  99. Internal_WindowToScreenPos(mCachedPtr, ref windowPos, out screenPos);
  100. return screenPos;
  101. }
  102. /// <summary>
  103. /// Triggered whenever the window size ranges.
  104. /// </summary>
  105. /// <param name="width">New width of the window in pixels.</param>
  106. /// <param name="height">New height of the window in pixels.</param>
  107. protected virtual void WindowResized(int width, int height)
  108. {
  109. }
  110. /// <summary>
  111. /// Triggered whenever the window gains or loses focus.
  112. /// </summary>
  113. /// <param name="inFocus">True if focus was gained, false otherwise.</param>
  114. protected virtual void FocusChanged(bool inFocus)
  115. {
  116. }
  117. /// <summary>
  118. /// Name of the window to display in the title bar.
  119. /// </summary>
  120. /// <returns>Name of the window to display in the title bar.</returns>
  121. protected virtual LocString GetDisplayName()
  122. {
  123. return GetType().Name;
  124. }
  125. [MethodImpl(MethodImplOptions.InternalCall)]
  126. private static extern EditorWindow Internal_CreateOrGetInstance(string ns, string typeName);
  127. [MethodImpl(MethodImplOptions.InternalCall)]
  128. private static extern EditorWindow Internal_GetInstance(string ns, string typeName);
  129. [MethodImpl(MethodImplOptions.InternalCall)]
  130. private static extern int Internal_GetWidth(IntPtr nativeInstance);
  131. [MethodImpl(MethodImplOptions.InternalCall)]
  132. private static extern int Internal_GetHeight(IntPtr nativeInstance);
  133. [MethodImpl(MethodImplOptions.InternalCall)]
  134. private static extern bool Internal_HasFocus(IntPtr nativeInstance);
  135. [MethodImpl(MethodImplOptions.InternalCall)]
  136. private static extern void Internal_SetFocus(IntPtr nativeInstance, bool focus);
  137. [MethodImpl(MethodImplOptions.InternalCall)]
  138. private static extern bool Internal_IsActive(IntPtr nativeInstance);
  139. [MethodImpl(MethodImplOptions.InternalCall)]
  140. private static extern bool Internal_IsPointerHovering(IntPtr nativeInstance);
  141. [MethodImpl(MethodImplOptions.InternalCall)]
  142. private static extern void Internal_GetBounds(IntPtr nativeInstance, out Rect2I bounds);
  143. [MethodImpl(MethodImplOptions.InternalCall)]
  144. private static extern void Internal_ScreenToWindowPos(IntPtr nativeInstance, ref Vector2I position, out Vector2I windowPos);
  145. [MethodImpl(MethodImplOptions.InternalCall)]
  146. private static extern void Internal_WindowToScreenPos(IntPtr nativeInstance, ref Vector2I position, out Vector2I screenPos);
  147. }
  148. /** @} */
  149. }