EditorWindow.cs 6.9 KB

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