diff --git a/Examples~ b/Examples~ index 906bad3..1dd5fcb 160000 --- a/Examples~ +++ b/Examples~ @@ -1 +1 @@ -Subproject commit 906bad37c84bc2abc156aeef50bcb93b85ea08b3 +Subproject commit 1dd5fcbc307cdf1ab3dba07b76637f3429c4be93 diff --git a/Runtime/Scripts/Controls/UI_Knob.cs b/Runtime/Scripts/Controls/UI_Knob.cs index f3e4396..9ca6119 100644 --- a/Runtime/Scripts/Controls/UI_Knob.cs +++ b/Runtime/Scripts/Controls/UI_Knob.cs @@ -1,6 +1,7 @@ /// 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; @@ -31,17 +32,21 @@ namespace UnityEngine.UI.Extensions [Tooltip("Direction of rotation CW - clockwise, CCW - counterClockwise")] public Direction direction = Direction.CW; [HideInInspector] - public float knobValue; + 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; + 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 = 1; + public int Loops = 0; [Tooltip("Clamp output value between 0 and 1, useful with loops > 1")] - public bool clampOutput01 = false; + public bool ClampOutput01 = false; [Tooltip("snap to position?")] - public bool snapToPosition = false; + public bool SnapToPosition = false; [Tooltip("Number of positions to snap")] - public int snapStepsPerLoop = 10; + 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; @@ -55,7 +60,48 @@ namespace UnityEngine.UI.Extensions protected override void Awake() { - _screenSpaceOverlay = GetComponentInParent().rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay; + _screenSpaceOverlay = GetComponentInParent().rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay; + } + + protected override void Start() + { + CheckForParentTouchMask(); + } + + private void CheckForParentTouchMask() + { + if (ParentTouchMask) + { + Image maskImage = ParentTouchMask.gameObject.GetOrAddComponent(); + maskImage.color = MaskBackground; + EventTrigger trigger = ParentTouchMask.gameObject.GetOrAddComponent(); + trigger.triggers.Clear(); + //PointerDownEvent + EventTrigger.Entry pointerDownEntry = new EventTrigger.Entry(); + pointerDownEntry.eventID = EventTriggerType.PointerDown; + pointerDownEntry.callback.AddListener((data) => { OnPointerDown((PointerEventData)data); }); + trigger.triggers.Add(pointerDownEntry); + //PointerUpEvent + EventTrigger.Entry pointerUpEntry = new EventTrigger.Entry(); + pointerUpEntry.eventID = EventTriggerType.PointerUp; + pointerUpEntry.callback.AddListener((data) => { OnPointerUp((PointerEventData)data); }); + trigger.triggers.Add(pointerUpEntry); + //PointerEnterEvent + EventTrigger.Entry pointerEnterEntry = new EventTrigger.Entry(); + pointerEnterEntry.eventID = EventTriggerType.PointerEnter; + pointerEnterEntry.callback.AddListener((data) => { OnPointerEnter((PointerEventData)data); }); + trigger.triggers.Add(pointerEnterEntry); + //PointerExitEvent + EventTrigger.Entry pointerExitEntry = new EventTrigger.Entry(); + pointerExitEntry.eventID = EventTriggerType.PointerExit; + pointerExitEntry.callback.AddListener((data) => { OnPointerExit((PointerEventData)data); }); + trigger.triggers.Add(pointerExitEntry); + //DragEvent + EventTrigger.Entry dragEntry = new EventTrigger.Entry(); + dragEntry.eventID = EventTriggerType.Drag; + dragEntry.callback.AddListener((data) => { OnDrag((PointerEventData)data); }); + trigger.triggers.Add(dragEntry); + } } public override void OnPointerUp(PointerEventData eventData) @@ -71,7 +117,6 @@ namespace UnityEngine.UI.Extensions _canDrag = false; } - public override void OnPointerDown(PointerEventData eventData) { _canDrag = true; @@ -115,83 +160,117 @@ namespace UnityEngine.UI.Extensions if (direction == Direction.CW) { - knobValue = 1 - (finalRotation.eulerAngles.z / 360f); + KnobValue = 1 - (finalRotation.eulerAngles.z / 360f); - if (snapToPosition) + if (SnapToPosition) { - SnapToPosition(ref knobValue); - finalRotation.eulerAngles = new Vector3(0, 0, 360 - 360 * knobValue); + SnapToPositionValue(ref KnobValue); + finalRotation.eulerAngles = new Vector3(0, 0, 360 - 360 * KnobValue); } } else { - knobValue = (finalRotation.eulerAngles.z / 360f); + KnobValue = (finalRotation.eulerAngles.z / 360f); - if (snapToPosition) + if (SnapToPosition) { - SnapToPosition(ref knobValue); - finalRotation.eulerAngles = new Vector3(0, 0, 360 * knobValue); + SnapToPositionValue(ref KnobValue); + finalRotation.eulerAngles = new Vector3(0, 0, 360 * KnobValue); } } + UpdateKnobValue(); + + transform.rotation = finalRotation; + InvokeEvents(KnobValue + _currentLoops); + + _previousValue = KnobValue; + } + + private void UpdateKnobValue() + { //PREVENT OVERROTATION - if (Mathf.Abs(knobValue - _previousValue) > 0.5f) + if (Mathf.Abs(KnobValue - _previousValue) > 0.5f) { - if (knobValue < 0.5f && loops > 1 && _currentLoops < loops - 1) + if (KnobValue < 0.5f && Loops > 1 && _currentLoops < Loops - 1) { _currentLoops++; } - else if (knobValue > 0.5f && _currentLoops >= 1) + else if (KnobValue > 0.5f && _currentLoops >= 1) { _currentLoops--; } else { - if (knobValue > 0.5f && _currentLoops == 0) + if (KnobValue > 0.5f && _currentLoops == 0) { - knobValue = 0; + KnobValue = 0; transform.localEulerAngles = Vector3.zero; - InvokeEvents(knobValue + _currentLoops); + InvokeEvents(KnobValue + _currentLoops); return; } - else if (knobValue < 0.5f && _currentLoops == loops - 1) + else if (KnobValue < 0.5f && _currentLoops == Loops - 1) { - knobValue = 1; + KnobValue = 1; transform.localEulerAngles = Vector3.zero; - InvokeEvents(knobValue + _currentLoops); + InvokeEvents(KnobValue + _currentLoops); return; } } } //CHECK MAX VALUE - if (maxValue > 0) + if (MaxValue > 0) { - if (knobValue + _currentLoops > maxValue) + if (KnobValue + _currentLoops > MaxValue) { - knobValue = maxValue; - float maxAngle = direction == Direction.CW ? 360f - 360f * maxValue : 360f * maxValue; + KnobValue = MaxValue; + float maxAngle = direction == Direction.CW ? 360f - 360f * MaxValue : 360f * MaxValue; transform.localEulerAngles = new Vector3(0, 0, maxAngle); - InvokeEvents(knobValue); + InvokeEvents(KnobValue); return; } } - - transform.rotation = finalRotation; - InvokeEvents(knobValue + _currentLoops); - - _previousValue = knobValue; } - private void SnapToPosition(ref float knobValue) + + public void SetKnobValue(float value, int loops = 0) { - float snapStep = 1 / (float)snapStepsPerLoop; + Quaternion newRoation = Quaternion.identity; + KnobValue = value; + _currentLoops = loops; + + if (SnapToPosition) + { + SnapToPositionValue(ref KnobValue); + + } + if (direction == Direction.CW) + { + newRoation.eulerAngles = new Vector3(0, 0, 360 - 360 * KnobValue); + } + else + { + newRoation.eulerAngles = new Vector3(0, 0, 360 * KnobValue); + } + + UpdateKnobValue(); + + transform.rotation = newRoation; + InvokeEvents(KnobValue + _currentLoops); + + _previousValue = KnobValue; + } + + private void SnapToPositionValue(ref float knobValue) + { + float snapStep = 1 / (float)SnapStepsPerLoop; float newValue = Mathf.Round(knobValue / snapStep) * snapStep; knobValue = newValue; } private void InvokeEvents(float value) { - if (clampOutput01) - value /= loops; + if (ClampOutput01) + value /= Loops; OnValueChanged.Invoke(value); } diff --git a/Runtime/Scripts/Layout/ScrollSnapBase.cs b/Runtime/Scripts/Layout/ScrollSnapBase.cs index ff75bae..570ce4b 100644 --- a/Runtime/Scripts/Layout/ScrollSnapBase.cs +++ b/Runtime/Scripts/Layout/ScrollSnapBase.cs @@ -5,7 +5,6 @@ using System; using UnityEngine.Events; using UnityEngine.EventSystems; -using UnityEngine.UIElements; namespace UnityEngine.UI.Extensions { diff --git a/Runtime/Scripts/Utilities/UIExtensionsInputManager.cs b/Runtime/Scripts/Utilities/UIExtensionsInputManager.cs index 450fcce..80306de 100644 --- a/Runtime/Scripts/Utilities/UIExtensionsInputManager.cs +++ b/Runtime/Scripts/Utilities/UIExtensionsInputManager.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Generic; -#if !ENABLE_LEGACY_INPUT_MANAGER +#if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER using UnityEngine.InputSystem; using UnityEngine.InputSystem.Controls; #endif @@ -13,7 +13,7 @@ namespace UnityEngine.UI.Extensions { public static class UIExtensionsInputManager { -#if !ENABLE_LEGACY_INPUT_MANAGER +#if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER private static bool[] mouseButtons = new bool[3] { false, false, false }; private static Dictionary keys = new Dictionary(); private static Dictionary buttons = new Dictionary(); @@ -21,23 +21,21 @@ namespace UnityEngine.UI.Extensions public static bool GetMouseButton(int button) { -#if ENABLE_LEGACY_INPUT_MANAGER - return Input.GetMouseButton(button); -#else +#if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER if (Mouse.current == null) { return false; } return Mouse.current.leftButton.isPressed; +#else + return Input.GetMouseButton(button); #endif } public static bool GetMouseButtonDown(int button) { -#if ENABLE_LEGACY_INPUT_MANAGER - return Input.GetMouseButtonDown(button); -#else +#if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER if (Mouse.current == null) { return false; @@ -52,14 +50,14 @@ namespace UnityEngine.UI.Extensions } } return false; +#else + return Input.GetMouseButtonDown(button); #endif } public static bool GetMouseButtonUp(int button) { -#if ENABLE_LEGACY_INPUT_MANAGER - return Input.GetMouseButtonUp(button); -#else +#if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER if (Mouse.current == null) { return false; @@ -71,14 +69,14 @@ namespace UnityEngine.UI.Extensions return true; } return false; +#else + return Input.GetMouseButtonUp(button); #endif } public static bool GetButton(string input) { -#if ENABLE_LEGACY_INPUT_MANAGER - return Input.GetButton(input); -#else +#if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER ButtonControl buttonPressed = GetButtonControlFromString(input); if (!buttons.ContainsKey(input)) @@ -87,10 +85,12 @@ namespace UnityEngine.UI.Extensions } return buttonPressed != null ? buttonPressed.isPressed : false; +#else + return Input.GetButton(input); #endif } -#if !ENABLE_LEGACY_INPUT_MANAGER +#if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER private static ButtonControl GetButtonControlFromString(string input) { if (Gamepad.current == null) @@ -112,9 +112,7 @@ namespace UnityEngine.UI.Extensions public static bool GetButtonDown(string input) { -#if ENABLE_LEGACY_INPUT_MANAGER - return Input.GetButtonDown(input); -#else +#if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER ButtonControl buttonPressed = GetButtonControlFromString(input); if (buttonPressed.isPressed) @@ -135,14 +133,14 @@ namespace UnityEngine.UI.Extensions buttons[input] = false; } return false; +#else + return Input.GetButtonDown(input); #endif } public static bool GetButtonUp(string input) { -#if ENABLE_LEGACY_INPUT_MANAGER - return Input.GetButtonUp(input); -#else +#if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER ButtonControl buttonPressed = GetButtonControlFromString(input); if (buttons[input] && !buttonPressed.isPressed) @@ -151,14 +149,14 @@ namespace UnityEngine.UI.Extensions return true; } return false; +#else + return Input.GetButtonUp(input); #endif } public static bool GetKey(KeyCode key) { -#if ENABLE_LEGACY_INPUT_MANAGER - return Input.GetKey(key); -#else +#if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER KeyControl keyPressed = GetKeyControlFromKeyCode(key); if (!keys.ContainsKey(key)) { @@ -166,10 +164,12 @@ namespace UnityEngine.UI.Extensions } return keyPressed != null ? keyPressed.isPressed : false; +#else + return Input.GetKey(key); #endif } -#if !ENABLE_LEGACY_INPUT_MANAGER +#if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER private static KeyControl GetKeyControlFromKeyCode(KeyCode key) { if (Keyboard.current == null) @@ -203,9 +203,7 @@ namespace UnityEngine.UI.Extensions public static bool GetKeyDown(KeyCode key) { -#if ENABLE_LEGACY_INPUT_MANAGER - return Input.GetKeyDown(key); -#else +#if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER KeyControl keyPressed = GetKeyControlFromKeyCode(key); if (keyPressed.isPressed) { @@ -225,14 +223,14 @@ namespace UnityEngine.UI.Extensions keys[key] = false; } return false; +#else + return Input.GetKeyDown(key); #endif } public static bool GetKeyUp(KeyCode key) { -#if ENABLE_LEGACY_INPUT_MANAGER - return Input.GetKeyUp(key); -#else +#if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER KeyControl keyPressed = GetKeyControlFromKeyCode(key); if (keys[key] && !keyPressed.isPressed) { @@ -240,14 +238,14 @@ namespace UnityEngine.UI.Extensions return true; } return false; +#else + return Input.GetKeyUp(key); #endif } public static float GetAxisRaw(string axis) { -#if ENABLE_LEGACY_INPUT_MANAGER - return Input.GetAxisRaw(axis); -#else +#if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER if (Gamepad.current == null) { return 0f; @@ -262,6 +260,8 @@ namespace UnityEngine.UI.Extensions } return 0f; +#else + return Input.GetAxisRaw(axis); #endif } @@ -269,10 +269,10 @@ namespace UnityEngine.UI.Extensions { get { -#if ENABLE_LEGACY_INPUT_MANAGER - return Input.mousePosition; -#else +#if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER return Mouse.current.position.ReadValue(); +#else + return Input.mousePosition; #endif } } @@ -281,10 +281,10 @@ namespace UnityEngine.UI.Extensions { get { -#if ENABLE_LEGACY_INPUT_MANAGER - return Input.mouseScrollDelta; -#else +#if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER return Mouse.current.position.ReadValue(); +#else + return Input.mouseScrollDelta; #endif } } diff --git a/Runtime/Scripts/Utilities/UI_MagneticInfiniteScroll.cs b/Runtime/Scripts/Utilities/UI_MagneticInfiniteScroll.cs index 0d9b6dc..f6ca3ff 100644 --- a/Runtime/Scripts/Utilities/UI_MagneticInfiniteScroll.cs +++ b/Runtime/Scripts/Utilities/UI_MagneticInfiniteScroll.cs @@ -10,6 +10,7 @@ using UnityEngine.EventSystems; namespace UnityEngine.UI.Extensions { [AddComponentMenu("UI/Extensions/UI Magnetic Infinite Scroll")] + [RequireComponent(typeof(ScrollRect))] public class UI_MagneticInfiniteScroll : UI_InfiniteScroll, IDragHandler, IEndDragHandler, IScrollHandler { public event Action OnNewSelect;