DropDownWindow.cs 5.3 KB

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