DropDownWindow.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 drop down window implementations. Drop down windows are temporary windows that open above all
  9. /// other GUI, and close as soon user clicks outside of them.
  10. /// </summary>
  11. public class DropDownWindow : ScriptObject
  12. {
  13. private int width;
  14. private int height;
  15. /// <summary>
  16. /// Width of the window in pixels.
  17. /// </summary>
  18. public int Width
  19. {
  20. get { return width; }
  21. set { Internal_SetWidth(mCachedPtr, value); width = value; }
  22. }
  23. /// <summary>
  24. /// Height of the window in pixels.
  25. /// </summary>
  26. public int Height
  27. {
  28. get { return height; }
  29. set { Internal_SetHeight(mCachedPtr, value); height = value; }
  30. }
  31. protected GUIPanel GUI;
  32. /// <summary>
  33. /// Opens a new drop down window at the specified location.
  34. /// </summary>
  35. /// <typeparam name="T">Type of the drop down window to open.</typeparam>
  36. /// <param name="parent">Parent editor window to open the drop down window in.</param>
  37. /// <param name="position">Position relative to the parent editor window at which to open the drop down window.
  38. /// </param>
  39. /// <returns>Instance of the opened drop down window.</returns>
  40. public static T Open<T>(EditorWindow parent, Vector2I position) where T : DropDownWindow, new()
  41. {
  42. T window = new T();
  43. window.Initialize(parent, position);
  44. return window;
  45. }
  46. /// <summary>
  47. /// Constructs a new drop down window.
  48. /// </summary>
  49. /// <param name="width">Width of the window in pixels.</param>
  50. /// <param name="height">Height of the window in pixels.</param>
  51. protected DropDownWindow(int width = 200, int height = 200)
  52. {
  53. this.width = width;
  54. this.height = height;
  55. }
  56. /// <summary>
  57. /// Initializes the drop down window after construction. Must be called before use.
  58. /// </summary>
  59. /// <param name="parent">Parent editor window to open the drop down window in.</param>
  60. /// <param name="position">Position relative to the parent editor window at which to open the drop down window.
  61. /// </param>
  62. private void Initialize(EditorWindow parent, Vector2I position)
  63. {
  64. IntPtr parentPtr = IntPtr.Zero;
  65. if (parent != null)
  66. parentPtr = parent.GetCachedPtr();
  67. Internal_CreateInstance(this, parentPtr, position, width, height);
  68. }
  69. /// <summary>
  70. /// Converts coordinates in screen space to coordinates relative to the drop down window.
  71. /// </summary>
  72. /// <param name="screenPos">Coordinates in screen space.</param>
  73. /// <returns>Coordinates relative to the drop down window.</returns>
  74. protected Vector2I ScreenToWindowPos(Vector2I screenPos)
  75. {
  76. Vector2I windowPos;
  77. Internal_ScreenToWindowPos(mCachedPtr, screenPos, out windowPos);
  78. return windowPos;
  79. }
  80. /// <summary>
  81. /// Converts coordinates relative to the drop down window to screen space to coordinates.
  82. /// </summary>
  83. /// <param name="windowPos">Coordinates relative to the drop down window.</param>
  84. /// <returns>Coordinates in screen space.</returns>
  85. protected Vector2I WindowToScreenPos(Vector2I windowPos)
  86. {
  87. Vector2I screenPos;
  88. Internal_WindowToScreenPos(mCachedPtr, windowPos, out screenPos);
  89. return screenPos;
  90. }
  91. /// <summary>
  92. /// Closes the drop down window.
  93. /// </summary>
  94. protected void Close()
  95. {
  96. Internal_Close(mCachedPtr);
  97. }
  98. [MethodImpl(MethodImplOptions.InternalCall)]
  99. private static extern void Internal_CreateInstance(DropDownWindow instance, IntPtr parentWindow, Vector2I position, int width, int height);
  100. [MethodImpl(MethodImplOptions.InternalCall)]
  101. private static extern void Internal_Close(IntPtr nativeInstance);
  102. [MethodImpl(MethodImplOptions.InternalCall)]
  103. private static extern void Internal_SetWidth(IntPtr nativeInstance, int value);
  104. [MethodImpl(MethodImplOptions.InternalCall)]
  105. private static extern void Internal_SetHeight(IntPtr nativeInstance, int value);
  106. [MethodImpl(MethodImplOptions.InternalCall)]
  107. private static extern void Internal_ScreenToWindowPos(IntPtr nativeInstance, Vector2I position, out Vector2I windowPos);
  108. [MethodImpl(MethodImplOptions.InternalCall)]
  109. private static extern void Internal_WindowToScreenPos(IntPtr nativeInstance, Vector2I position, out Vector2I screenPos);
  110. }
  111. }