EditorWindow.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /// <summary>
  8. /// Base class for all editor windows. Editor window can be docked on the main editor window or float as a separate
  9. /// window. User is allowed to reposition and resize the window as he wishes. Editor window GUI is fully customizable.
  10. /// </summary>
  11. public class EditorWindow : ScriptObject
  12. {
  13. /// <summary>
  14. /// Width of the window in pixels.
  15. /// </summary>
  16. public int Width
  17. {
  18. get { return Internal_GetWidth(mCachedPtr); }
  19. }
  20. /// <summary>
  21. /// Height of the window in pixels.
  22. /// </summary>
  23. public int Height
  24. {
  25. get { return Internal_GetHeight(mCachedPtr); }
  26. }
  27. /// <summary>
  28. /// Screen space bounds of the window.
  29. /// </summary>
  30. public Rect2I Bounds
  31. {
  32. get
  33. {
  34. Rect2I bounds;
  35. Internal_GetBounds(mCachedPtr, out bounds);
  36. return bounds;
  37. }
  38. }
  39. /// <summary>
  40. /// Determines whether the editor window currently has keyboard focus (has been clicked on most recently).
  41. /// </summary>
  42. public bool HasFocus { get { return Internal_HasFocus(mCachedPtr); } }
  43. /// <summary>
  44. /// Determines is the mouse pointer currently hovering over the editor window.
  45. /// </summary>
  46. public bool IsPointerHovering { get { return Internal_IsPointerHovering(mCachedPtr); } }
  47. /// <summary>
  48. /// Checks if the window's tab is currently active. If the window is floating or not sharing space with any other
  49. /// windows (just a single tab), it is always considered active.
  50. /// </summary>
  51. public bool Active { get { return Internal_IsActive(mCachedPtr); } }
  52. protected GUIPanel GUI;
  53. /// <summary>
  54. /// Opens an editor window. If window is already open it returns the existing instance.
  55. /// </summary>
  56. /// <typeparam name="T">Type of window to open.</typeparam>
  57. /// <returns>Instance of the open window.</returns>
  58. public static T OpenWindow<T>() where T : EditorWindow
  59. {
  60. return (T)Internal_CreateOrGetInstance(typeof(T).Namespace, typeof(T).Name);
  61. }
  62. /// <summary>
  63. /// Retrieves instance of an open window.
  64. /// </summary>
  65. /// <typeparam name="T">Type of window to retrieve the instance of.</typeparam>
  66. /// <returns>Instance of the winodow if it is open, null otherwise.</returns>
  67. public static T GetWindow<T>() where T : EditorWindow
  68. {
  69. return (T)Internal_GetInstance(typeof(T).Namespace, typeof(T).Name);
  70. }
  71. /// <summary>
  72. /// Converts coordinates in screen space to coordinates relative to the window.
  73. /// </summary>
  74. /// <param name="screenPos">Coordinates in screen space.</param>
  75. /// <returns>Coordinates relative to the window.</returns>
  76. public Vector2I ScreenToWindowPos(Vector2I screenPos)
  77. {
  78. Vector2I windowPos;
  79. Internal_ScreenToWindowPos(mCachedPtr, ref screenPos, out windowPos);
  80. return windowPos;
  81. }
  82. /// <summary>
  83. /// Converts coordinates relative to the window to screen space to coordinates.
  84. /// </summary>
  85. /// <param name="windowPos">Coordinates relative to the window.</param>
  86. /// <returns>Coordinates in screen space.</returns>
  87. public Vector2I WindowToScreenPos(Vector2I windowPos)
  88. {
  89. Vector2I screenPos;
  90. Internal_WindowToScreenPos(mCachedPtr, ref windowPos, out screenPos);
  91. return screenPos;
  92. }
  93. /// <summary>
  94. /// Triggered whenever the window size ranges.
  95. /// </summary>
  96. /// <param name="width">New width of the window in pixels.</param>
  97. /// <param name="height">New height of the window in pixels.</param>
  98. protected virtual void WindowResized(int width, int height)
  99. {
  100. }
  101. /// <summary>
  102. /// Triggered whenever the window gains or loses focus.
  103. /// </summary>
  104. /// <param name="inFocus">True if focus was gained, false otherwise.</param>
  105. protected virtual void FocusChanged(bool inFocus)
  106. {
  107. }
  108. /// <summary>
  109. /// Name of the window to display in the title bar.
  110. /// </summary>
  111. /// <returns>Name of the window to display in the title bar.</returns>
  112. protected virtual LocString GetDisplayName()
  113. {
  114. return GetType().Name;
  115. }
  116. [MethodImpl(MethodImplOptions.InternalCall)]
  117. private static extern EditorWindow Internal_CreateOrGetInstance(string ns, string typeName);
  118. [MethodImpl(MethodImplOptions.InternalCall)]
  119. private static extern EditorWindow Internal_GetInstance(string ns, string typeName);
  120. [MethodImpl(MethodImplOptions.InternalCall)]
  121. private static extern int Internal_GetWidth(IntPtr nativeInstance);
  122. [MethodImpl(MethodImplOptions.InternalCall)]
  123. private static extern int Internal_GetHeight(IntPtr nativeInstance);
  124. [MethodImpl(MethodImplOptions.InternalCall)]
  125. private static extern bool Internal_HasFocus(IntPtr nativeInstance);
  126. [MethodImpl(MethodImplOptions.InternalCall)]
  127. private static extern bool Internal_IsActive(IntPtr nativeInstance);
  128. [MethodImpl(MethodImplOptions.InternalCall)]
  129. private static extern bool Internal_IsPointerHovering(IntPtr nativeInstance);
  130. [MethodImpl(MethodImplOptions.InternalCall)]
  131. private static extern void Internal_GetBounds(IntPtr nativeInstance, out Rect2I bounds);
  132. [MethodImpl(MethodImplOptions.InternalCall)]
  133. private static extern void Internal_ScreenToWindowPos(IntPtr nativeInstance, ref Vector2I position, out Vector2I windowPos);
  134. [MethodImpl(MethodImplOptions.InternalCall)]
  135. private static extern void Internal_WindowToScreenPos(IntPtr nativeInstance, ref Vector2I position, out Vector2I screenPos);
  136. }
  137. }