EditorWindow.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /// Checks if the window's tab is currently active. If the window is floating or not sharing space with any other
  45. /// windows (just a single tab), it is always considered active.
  46. /// </summary>
  47. public bool Active { get { return Internal_IsActive(mCachedPtr); } }
  48. protected GUIPanel GUI;
  49. /// <summary>
  50. /// Opens an editor window. If window is already open it returns the existing instance.
  51. /// </summary>
  52. /// <typeparam name="T">Type of window to open.</typeparam>
  53. /// <returns>Instance of the open window.</returns>
  54. public static T OpenWindow<T>() where T : EditorWindow
  55. {
  56. return (T)Internal_CreateOrGetInstance(typeof(T).Namespace, typeof(T).Name);
  57. }
  58. /// <summary>
  59. /// Retrieves instance of an open window.
  60. /// </summary>
  61. /// <typeparam name="T">Type of window to retrieve the instance of.</typeparam>
  62. /// <returns>Instance of the winodow if it is open, null otherwise.</returns>
  63. public static T GetWindow<T>() where T : EditorWindow
  64. {
  65. return (T)Internal_GetInstance(typeof(T).Namespace, typeof(T).Name);
  66. }
  67. /// <summary>
  68. /// Converts coordinates in screen space to coordinates relative to the window.
  69. /// </summary>
  70. /// <param name="screenPos">Coordinates in screen space.</param>
  71. /// <returns>Coordinates relative to the window.</returns>
  72. public Vector2I ScreenToWindowPos(Vector2I screenPos)
  73. {
  74. Vector2I windowPos;
  75. Internal_ScreenToWindowPos(mCachedPtr, ref screenPos, out windowPos);
  76. return windowPos;
  77. }
  78. /// <summary>
  79. /// Converts coordinates relative to the window to screen space to coordinates.
  80. /// </summary>
  81. /// <param name="windowPos">Coordinates relative to the window.</param>
  82. /// <returns>Coordinates in screen space.</returns>
  83. public Vector2I WindowToScreenPos(Vector2I windowPos)
  84. {
  85. Vector2I screenPos;
  86. Internal_WindowToScreenPos(mCachedPtr, ref windowPos, out screenPos);
  87. return screenPos;
  88. }
  89. /// <summary>
  90. /// Triggered whenever the window size ranges.
  91. /// </summary>
  92. /// <param name="width">New width of the window in pixels.</param>
  93. /// <param name="height">New height of the window in pixels.</param>
  94. protected virtual void WindowResized(int width, int height)
  95. {
  96. }
  97. /// <summary>
  98. /// Triggered whenever the window gains or loses focus.
  99. /// </summary>
  100. /// <param name="inFocus">True if focus was gained, false otherwise.</param>
  101. protected virtual void FocusChanged(bool inFocus)
  102. {
  103. }
  104. /// <summary>
  105. /// Name of the window to display in the title bar.
  106. /// </summary>
  107. /// <returns>Name of the window to display in the title bar.</returns>
  108. protected virtual LocString GetDisplayName()
  109. {
  110. return GetType().Name;
  111. }
  112. [MethodImpl(MethodImplOptions.InternalCall)]
  113. private static extern EditorWindow Internal_CreateOrGetInstance(string ns, string typeName);
  114. [MethodImpl(MethodImplOptions.InternalCall)]
  115. private static extern EditorWindow Internal_GetInstance(string ns, string typeName);
  116. [MethodImpl(MethodImplOptions.InternalCall)]
  117. private static extern int Internal_GetWidth(IntPtr nativeInstance);
  118. [MethodImpl(MethodImplOptions.InternalCall)]
  119. private static extern int Internal_GetHeight(IntPtr nativeInstance);
  120. [MethodImpl(MethodImplOptions.InternalCall)]
  121. private static extern bool Internal_HasFocus(IntPtr nativeInstance);
  122. [MethodImpl(MethodImplOptions.InternalCall)]
  123. private static extern bool Internal_IsActive(IntPtr nativeInstance);
  124. [MethodImpl(MethodImplOptions.InternalCall)]
  125. private static extern void Internal_GetBounds(IntPtr nativeInstance, out Rect2I bounds);
  126. [MethodImpl(MethodImplOptions.InternalCall)]
  127. private static extern void Internal_ScreenToWindowPos(IntPtr nativeInstance, ref Vector2I position, out Vector2I windowPos);
  128. [MethodImpl(MethodImplOptions.InternalCall)]
  129. private static extern void Internal_WindowToScreenPos(IntPtr nativeInstance, ref Vector2I position, out Vector2I screenPos);
  130. }
  131. }