GUIColorField.cs 4.5 KB

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