EditorWindow.cs 6.9 KB

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