EditorWindow.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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).
  43. /// </summary>
  44. public bool HasFocus { get { return Internal_HasFocus(mCachedPtr); } }
  45. /// <summary>
  46. /// Determines is the mouse pointer currently hovering over the editor window.
  47. /// </summary>
  48. public bool IsPointerHovering { get { return Internal_IsPointerHovering(mCachedPtr); } }
  49. /// <summary>
  50. /// Checks if the window's tab is currently active. If the window is floating or not sharing space with any other
  51. /// windows (just a single tab), it is always considered active.
  52. /// </summary>
  53. public bool Active { get { return Internal_IsActive(mCachedPtr); } }
  54. protected GUIPanel GUI;
  55. /// <summary>
  56. /// Opens an editor window. If window is already open it returns the existing instance.
  57. /// </summary>
  58. /// <typeparam name="T">Type of window to open.</typeparam>
  59. /// <returns>Instance of the open window.</returns>
  60. public static T OpenWindow<T>() where T : EditorWindow
  61. {
  62. return (T)Internal_CreateOrGetInstance(typeof(T).Namespace, typeof(T).Name);
  63. }
  64. /// <summary>
  65. /// Retrieves instance of an open window.
  66. /// </summary>
  67. /// <typeparam name="T">Type of window to retrieve the instance of.</typeparam>
  68. /// <returns>Instance of the winodow if it is open, null otherwise.</returns>
  69. public static T GetWindow<T>() where T : EditorWindow
  70. {
  71. return (T)Internal_GetInstance(typeof(T).Namespace, typeof(T).Name);
  72. }
  73. /// <summary>
  74. /// Converts coordinates in screen space to coordinates relative to the window.
  75. /// </summary>
  76. /// <param name="screenPos">Coordinates in screen space.</param>
  77. /// <returns>Coordinates relative to the window.</returns>
  78. public Vector2I ScreenToWindowPos(Vector2I screenPos)
  79. {
  80. Vector2I windowPos;
  81. Internal_ScreenToWindowPos(mCachedPtr, ref screenPos, out windowPos);
  82. return windowPos;
  83. }
  84. /// <summary>
  85. /// Converts coordinates relative to the window to screen space to coordinates.
  86. /// </summary>
  87. /// <param name="windowPos">Coordinates relative to the window.</param>
  88. /// <returns>Coordinates in screen space.</returns>
  89. public Vector2I WindowToScreenPos(Vector2I windowPos)
  90. {
  91. Vector2I screenPos;
  92. Internal_WindowToScreenPos(mCachedPtr, ref windowPos, out screenPos);
  93. return screenPos;
  94. }
  95. /// <summary>
  96. /// Triggered whenever the window size ranges.
  97. /// </summary>
  98. /// <param name="width">New width of the window in pixels.</param>
  99. /// <param name="height">New height of the window in pixels.</param>
  100. protected virtual void WindowResized(int width, int height)
  101. {
  102. }
  103. /// <summary>
  104. /// Triggered whenever the window gains or loses focus.
  105. /// </summary>
  106. /// <param name="inFocus">True if focus was gained, false otherwise.</param>
  107. protected virtual void FocusChanged(bool inFocus)
  108. {
  109. }
  110. /// <summary>
  111. /// Name of the window to display in the title bar.
  112. /// </summary>
  113. /// <returns>Name of the window to display in the title bar.</returns>
  114. protected virtual LocString GetDisplayName()
  115. {
  116. return GetType().Name;
  117. }
  118. [MethodImpl(MethodImplOptions.InternalCall)]
  119. private static extern EditorWindow Internal_CreateOrGetInstance(string ns, string typeName);
  120. [MethodImpl(MethodImplOptions.InternalCall)]
  121. private static extern EditorWindow Internal_GetInstance(string ns, string typeName);
  122. [MethodImpl(MethodImplOptions.InternalCall)]
  123. private static extern int Internal_GetWidth(IntPtr nativeInstance);
  124. [MethodImpl(MethodImplOptions.InternalCall)]
  125. private static extern int Internal_GetHeight(IntPtr nativeInstance);
  126. [MethodImpl(MethodImplOptions.InternalCall)]
  127. private static extern bool Internal_HasFocus(IntPtr nativeInstance);
  128. [MethodImpl(MethodImplOptions.InternalCall)]
  129. private static extern bool Internal_IsActive(IntPtr nativeInstance);
  130. [MethodImpl(MethodImplOptions.InternalCall)]
  131. private static extern bool Internal_IsPointerHovering(IntPtr nativeInstance);
  132. [MethodImpl(MethodImplOptions.InternalCall)]
  133. private static extern void Internal_GetBounds(IntPtr nativeInstance, out Rect2I bounds);
  134. [MethodImpl(MethodImplOptions.InternalCall)]
  135. private static extern void Internal_ScreenToWindowPos(IntPtr nativeInstance, ref Vector2I position, out Vector2I windowPos);
  136. [MethodImpl(MethodImplOptions.InternalCall)]
  137. private static extern void Internal_WindowToScreenPos(IntPtr nativeInstance, ref Vector2I position, out Vector2I screenPos);
  138. }
  139. }