GUIColorField.cs 4.8 KB

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