/// Credit Tomasz Schelenz
/// Sourced from - https://bitbucket.org/SimonDarksideJ/unity-ui-extensions/issues/46/feature-uiknob#comment-29243988
using System;
using UnityEngine.Events;
using UnityEngine.EventSystems;
///
/// KNOB controller
///
/// Fields
/// - direction - direction of rotation CW - clockwise CCW - counter clock wise
/// - knobValue - Output value of the control
/// - maxValue - max value knob can rotate to, if higher than loops value or set to 0 - it will be ignored, and max value will be based on loops
/// - loops - how any turns around knob can do
/// - clampOutput01 - if true the output knobValue will be clamped between 0 and 1 regardless of number of loops.
/// - snapToPosition - snap to step. NOTE: max value will override the step.
/// - snapStepsPerLoop - how many snap positions are in one knob loop;
/// - OnValueChanged - event that is called every frame while rotating knob, sends argument of knobValue
/// NOTES
/// - script works only in images rotation on Z axis;
/// - while dragging outside of control, the rotation will be canceled
///
///
namespace UnityEngine.UI.Extensions
{
[RequireComponent(typeof(Image))]
[AddComponentMenu("UI/Extensions/UI_Knob")]
public class UI_Knob : Selectable, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler, IDragHandler, IInitializePotentialDragHandler
{
public enum Direction { CW, CCW };
[Tooltip("Direction of rotation CW - clockwise, CCW - counterClockwise")]
public Direction direction = Direction.CW;
[HideInInspector]
public float KnobValue;
[Tooltip("Max value of the knob, maximum RAW output value knob can reach, overrides snap step, IF set to 0 or higher than loops, max value will be set by loops")]
public float MaxValue = 0;
[Tooltip("How many rotations knob can do, if higher than max value, the latter will limit max value")]
public int Loops = 0;
[Tooltip("Clamp output value between 0 and 1, useful with loops > 1")]
public bool ClampOutput01 = false;
[Tooltip("snap to position?")]
public bool SnapToPosition = false;
[Tooltip("Number of positions to snap")]
public int SnapStepsPerLoop = 10;
[Tooltip("Parent touch area to extend the touch radius")]
public RectTransform ParentTouchMask;
[Tooltip("Default background color of the touch mask. Defaults as transparent")]
public Color MaskBackground = new Color(0, 0, 0, 0);
[Space(30)]
public KnobFloatValueEvent OnValueChanged;
private float _currentLoops = 0;
private float _previousValue = 0;
private float _initAngle;
private float _currentAngle;
private Vector2 _currentVector;
private Quaternion _initRotation;
private bool _canDrag = false;
private bool _screenSpaceOverlay;
protected override void Awake()
{
_screenSpaceOverlay = GetComponentInParent