EditorWindow.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /// Determines whether the editor window currently has keyboard focus (has been clicked on most recently).
  29. /// </summary>
  30. public bool HasFocus { get { return Internal_HasFocus(mCachedPtr); } }
  31. protected GUIPanel GUI;
  32. /// <summary>
  33. /// Opens an editor window. If window is already open it returns the existing instance.
  34. /// </summary>
  35. /// <typeparam name="T">Type of window to open.</typeparam>
  36. /// <returns>Instance of the open window.</returns>
  37. public static T OpenWindow<T>() where T : EditorWindow
  38. {
  39. return (T)Internal_CreateOrGetInstance(typeof(T).Namespace, typeof(T).Name);
  40. }
  41. /// <summary>
  42. /// Retrieves instance of an open window.
  43. /// </summary>
  44. /// <typeparam name="T">Type of window to retrieve the instance of.</typeparam>
  45. /// <returns>Instance of the winodow if it is open, null otherwise.</returns>
  46. public static T GetWindow<T>() where T : EditorWindow
  47. {
  48. return (T)Internal_GetInstance(typeof(T).Namespace, typeof(T).Name);
  49. }
  50. /// <summary>
  51. /// Converts coordinates in screen space to coordinates relative to the window.
  52. /// </summary>
  53. /// <param name="screenPos">Coordinates in screen space.</param>
  54. /// <returns>Coordinates relative to the window.</returns>
  55. public Vector2I ScreenToWindowPos(Vector2I screenPos)
  56. {
  57. Vector2I windowPos;
  58. Internal_ScreenToWindowPos(mCachedPtr, screenPos, out windowPos);
  59. return windowPos;
  60. }
  61. /// <summary>
  62. /// Converts coordinates relative to the window to screen space to coordinates.
  63. /// </summary>
  64. /// <param name="windowPos">Coordinates relative to the window.</param>
  65. /// <returns>Coordinates in screen space.</returns>
  66. public Vector2I WindowToScreenPos(Vector2I windowPos)
  67. {
  68. Vector2I screenPos;
  69. Internal_WindowToScreenPos(mCachedPtr, windowPos, out screenPos);
  70. return screenPos;
  71. }
  72. /// <summary>
  73. /// Triggered whenever the window size ranges.
  74. /// </summary>
  75. /// <param name="width">New width of the window in pixels.</param>
  76. /// <param name="height">New height of the window in pixels.</param>
  77. protected virtual void WindowResized(int width, int height)
  78. {
  79. }
  80. /// <summary>
  81. /// Triggered whenever the window gains or loses focus.
  82. /// </summary>
  83. /// <param name="inFocus">True if focus was gained, false otherwise.</param>
  84. protected virtual void FocusChanged(bool inFocus)
  85. {
  86. }
  87. /// <summary>
  88. /// Name of the window to display in the title bar.
  89. /// </summary>
  90. /// <returns>Name of the window to display in the title bar.</returns>
  91. protected virtual LocString GetDisplayName()
  92. {
  93. return GetType().Name;
  94. }
  95. [MethodImpl(MethodImplOptions.InternalCall)]
  96. private static extern EditorWindow Internal_CreateOrGetInstance(string ns, string typeName);
  97. [MethodImpl(MethodImplOptions.InternalCall)]
  98. private static extern EditorWindow Internal_GetInstance(string ns, string typeName);
  99. [MethodImpl(MethodImplOptions.InternalCall)]
  100. private static extern int Internal_GetWidth(IntPtr nativeInstance);
  101. [MethodImpl(MethodImplOptions.InternalCall)]
  102. private static extern int Internal_GetHeight(IntPtr nativeInstance);
  103. [MethodImpl(MethodImplOptions.InternalCall)]
  104. private static extern bool Internal_HasFocus(IntPtr nativeInstance);
  105. [MethodImpl(MethodImplOptions.InternalCall)]
  106. private static extern void Internal_ScreenToWindowPos(IntPtr nativeInstance, Vector2I position, out Vector2I windowPos);
  107. [MethodImpl(MethodImplOptions.InternalCall)]
  108. private static extern void Internal_WindowToScreenPos(IntPtr nativeInstance, Vector2I position, out Vector2I screenPos);
  109. }
  110. }