EditorWindow.cs 5.4 KB

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