GUIColorField.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using bs;
  6. namespace bs.Editor
  7. {
  8. /** @addtogroup GUI-Editor
  9. * @{
  10. */
  11. /// <summary>
  12. /// Editor GUI element that displays a color and an optional label. Allows the user to select a color using the color
  13. /// picker.
  14. /// </summary>
  15. public sealed class GUIColorField : GUIElement
  16. {
  17. public delegate void OnChangedDelegate(Color newValue);
  18. /// <summary>
  19. /// Triggered when the color in the field changes.
  20. /// </summary>
  21. public event OnChangedDelegate OnChanged;
  22. /// <summary>
  23. /// Color displayed by the field.
  24. /// </summary>
  25. public Color Value
  26. {
  27. get
  28. {
  29. Color value;
  30. Internal_GetValue(mCachedPtr, out value);
  31. return value;
  32. }
  33. set { Internal_SetValue(mCachedPtr, ref value); }
  34. }
  35. /// <summary>
  36. /// Creates a new color field element with a label.
  37. /// </summary>
  38. /// <param name="title">Content to display on the label.</param>
  39. /// <param name="titleWidth">Width of the title label in pixels.</param>
  40. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  41. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  42. /// default element style is used.</param>
  43. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  44. /// override any similar options set by style.</param>
  45. public GUIColorField(GUIContent title, int titleWidth = 100, string style = "", params GUIOption[] options)
  46. {
  47. Internal_CreateInstance(this, ref title, titleWidth, style, options, true);
  48. }
  49. /// <summary>
  50. /// Creates a new color field element without a label.
  51. /// </summary>
  52. /// <param name="style">Optional style to use for the element. Style controls the look of the element, as well as
  53. /// default layout options. Style will be retrieved from the active GUISkin. If not specified
  54. /// default element style is used.</param>
  55. /// <param name="options">Options that allow you to control how is the element positioned and sized. This will
  56. /// override any similar options set by style.</param>
  57. public GUIColorField(string style = "", params GUIOption[] options)
  58. {
  59. GUIContent emptyContent = new GUIContent();
  60. Internal_CreateInstance(this, ref emptyContent, 0, style, options, false);
  61. }
  62. /// <summary>
  63. /// Colors the element with a specific tint.
  64. /// </summary>
  65. /// <param name="color">Tint to apply to the element.</param>
  66. public void SetTint(Color color)
  67. {
  68. Internal_SetTint(mCachedPtr, ref color);
  69. }
  70. /// <summary>
  71. /// Triggered when the user closes the color picker window.
  72. /// </summary>
  73. /// <param name="selected">True if the user confirms color selection, false if he cancels.</param>
  74. /// <param name="color">Newly selected color.</param>
  75. private void ColorPickerClosed(bool selected, Color color)
  76. {
  77. if (!selected)
  78. return;
  79. if (Value != color)
  80. {
  81. Value = color;
  82. OnChanged?.Invoke(color);
  83. }
  84. }
  85. /// <summary>
  86. /// Triggered by the runtime when the user clicks on the color field.
  87. /// </summary>
  88. private void Internal_DoOnClicked()
  89. {
  90. ColorPicker.Show(Value, ColorPickerClosed);
  91. }
  92. [MethodImpl(MethodImplOptions.InternalCall)]
  93. private static extern void Internal_CreateInstance(GUIColorField instance, ref GUIContent title, int titleWidth,
  94. string style, GUIOption[] options, bool withTitle);
  95. [MethodImpl(MethodImplOptions.InternalCall)]
  96. private static extern void Internal_GetValue(IntPtr nativeInstance, out Color value);
  97. [MethodImpl(MethodImplOptions.InternalCall)]
  98. private static extern void Internal_SetValue(IntPtr nativeInstance, ref Color value);
  99. [MethodImpl(MethodImplOptions.InternalCall)]
  100. private static extern void Internal_SetTint(IntPtr nativeInstance, ref Color color);
  101. }
  102. /** @} */
  103. }