ValueDragger.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. using UnityEngine.Events;
  8. using UnityEngine.EventSystems;
  9. namespace TheraBytes.BetterUi
  10. {
  11. [HelpURL("https://documentation.therabytes.de/better-ui/ValueDragger.html")]
  12. [AddComponentMenu("Better UI/Controls/Value Dragger", 30)]
  13. public class ValueDragger : BetterSelectable, IDragHandler, IBeginDragHandler, IPointerClickHandler
  14. {
  15. #region Nested Types
  16. [Serializable]
  17. public class ValueDragEvent : UnityEvent<float> { }
  18. public enum DragDirection
  19. {
  20. Horizontal = 0, // maps to Vector2's X index
  21. Vertical = 1, // maps to Vector2's Y index
  22. }
  23. [Serializable]
  24. public class DragSettings : IScreenConfigConnection
  25. {
  26. public DragDirection Direction = DragDirection.Horizontal;
  27. public bool Invert;
  28. [SerializeField]
  29. string screenConfigName;
  30. public string ScreenConfigName { get { return screenConfigName; } set { screenConfigName = value; } }
  31. }
  32. [Serializable]
  33. public class DragSettingsConfigCollection : SizeConfigCollection<DragSettings> { }
  34. [Serializable]
  35. public class ValueSettings : IScreenConfigConnection
  36. {
  37. public bool HasMinValue;
  38. public float MinValue = 0f;
  39. public bool HasMaxValue;
  40. public float MaxValue = 1f;
  41. public bool WholeNumbers;
  42. [SerializeField]
  43. string screenConfigName;
  44. public string ScreenConfigName { get { return screenConfigName; } set { screenConfigName = value; } }
  45. }
  46. [Serializable]
  47. public class ValueSettingsConfigCollection : SizeConfigCollection<ValueSettings> { }
  48. #endregion
  49. [SerializeField]
  50. DragSettings fallbackDragSettings = new DragSettings();
  51. [SerializeField]
  52. DragSettingsConfigCollection customDragSettings = new DragSettingsConfigCollection();
  53. [SerializeField]
  54. ValueSettings fallbackValueSettings = new ValueSettings();
  55. [SerializeField]
  56. ValueSettingsConfigCollection customValueSettings = new ValueSettingsConfigCollection();
  57. [SerializeField]
  58. FloatSizeModifier fallbackDragDistance = new FloatSizeModifier(1, float.Epsilon, 10000);
  59. [SerializeField]
  60. FloatSizeConfigCollection customDragDistance = new FloatSizeConfigCollection();
  61. [SerializeField]
  62. float value;
  63. [SerializeField]
  64. ValueDragEvent onValueChanged = new ValueDragEvent();
  65. float internalValue;
  66. public DragSettings CurrentDragSettings
  67. {
  68. get { return customDragSettings.GetCurrentItem(fallbackDragSettings); }
  69. }
  70. public ValueSettings CurrentValueSettings
  71. {
  72. get { return customValueSettings.GetCurrentItem(fallbackValueSettings); }
  73. }
  74. public FloatSizeModifier CurrentDragDistanceSizer
  75. {
  76. get { return customDragDistance.GetCurrentItem(fallbackDragDistance); }
  77. }
  78. public float Value { get { return this.value; } set { ApplyValue(value); } }
  79. public ValueDragEvent OnValueChanged { get { return onValueChanged; } set { onValueChanged = value; } }
  80. void IBeginDragHandler.OnBeginDrag(PointerEventData eventData)
  81. {
  82. internalValue = value;
  83. CurrentDragDistanceSizer.CalculateSize(this);
  84. }
  85. void IDragHandler.OnDrag(PointerEventData eventData)
  86. {
  87. var dragSettings = CurrentDragSettings;
  88. int axis = (int)dragSettings.Direction;
  89. float delta = eventData.delta[axis];
  90. float divisor = CurrentDragDistanceSizer.LastCalculatedSize;
  91. internalValue += (dragSettings.Invert)
  92. ? -delta / divisor
  93. : delta / divisor;
  94. ApplyValue(internalValue);
  95. }
  96. void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
  97. {
  98. // consume the click by implementing this interface.
  99. // we don't want to let the click pass through to a control higher up in the hierarchy.
  100. }
  101. private void ApplyValue(float val)
  102. {
  103. var valueSettings = CurrentValueSettings;
  104. if(valueSettings.HasMinValue && val < valueSettings.MinValue)
  105. {
  106. val = valueSettings.MinValue;
  107. }
  108. else if(valueSettings.HasMaxValue && val > valueSettings.MaxValue)
  109. {
  110. val = valueSettings.MaxValue;
  111. }
  112. if(valueSettings.WholeNumbers)
  113. {
  114. val = (int)val;
  115. }
  116. if (val != value)
  117. {
  118. value = val;
  119. onValueChanged.Invoke(value);
  120. }
  121. }
  122. }
  123. }