EditorWindow.cs 4.9 KB

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