DropDownWindow.cs 5.2 KB

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