Merged in UIKnob_Improvements (pull request #101)

UIKnob Improvements
pull/413/head
Simon Jackson 2020-10-21 23:13:05 +00:00
commit 184954ea37
5 changed files with 160 additions and 81 deletions

@ -1 +1 @@
Subproject commit 906bad37c84bc2abc156aeef50bcb93b85ea08b3 Subproject commit 1dd5fcbc307cdf1ab3dba07b76637f3429c4be93

View File

@ -1,6 +1,7 @@
/// Credit Tomasz Schelenz /// Credit Tomasz Schelenz
/// Sourced from - https://bitbucket.org/SimonDarksideJ/unity-ui-extensions/issues/46/feature-uiknob#comment-29243988 /// Sourced from - https://bitbucket.org/SimonDarksideJ/unity-ui-extensions/issues/46/feature-uiknob#comment-29243988
using System;
using UnityEngine.Events; using UnityEngine.Events;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
@ -31,17 +32,21 @@ namespace UnityEngine.UI.Extensions
[Tooltip("Direction of rotation CW - clockwise, CCW - counterClockwise")] [Tooltip("Direction of rotation CW - clockwise, CCW - counterClockwise")]
public Direction direction = Direction.CW; public Direction direction = Direction.CW;
[HideInInspector] [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")] [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")] [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")] [Tooltip("Clamp output value between 0 and 1, useful with loops > 1")]
public bool clampOutput01 = false; public bool ClampOutput01 = false;
[Tooltip("snap to position?")] [Tooltip("snap to position?")]
public bool snapToPosition = false; public bool SnapToPosition = false;
[Tooltip("Number of positions to snap")] [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)] [Space(30)]
public KnobFloatValueEvent OnValueChanged; public KnobFloatValueEvent OnValueChanged;
private float _currentLoops = 0; private float _currentLoops = 0;
@ -55,7 +60,48 @@ namespace UnityEngine.UI.Extensions
protected override void Awake() protected override void Awake()
{ {
_screenSpaceOverlay = GetComponentInParent<Canvas>().rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay; _screenSpaceOverlay = GetComponentInParent<Canvas>().rootCanvas.renderMode == RenderMode.ScreenSpaceOverlay;
}
protected override void Start()
{
CheckForParentTouchMask();
}
private void CheckForParentTouchMask()
{
if (ParentTouchMask)
{
Image maskImage = ParentTouchMask.gameObject.GetOrAddComponent<Image>();
maskImage.color = MaskBackground;
EventTrigger trigger = ParentTouchMask.gameObject.GetOrAddComponent<EventTrigger>();
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) public override void OnPointerUp(PointerEventData eventData)
@ -71,7 +117,6 @@ namespace UnityEngine.UI.Extensions
_canDrag = false; _canDrag = false;
} }
public override void OnPointerDown(PointerEventData eventData) public override void OnPointerDown(PointerEventData eventData)
{ {
_canDrag = true; _canDrag = true;
@ -115,83 +160,117 @@ namespace UnityEngine.UI.Extensions
if (direction == Direction.CW) if (direction == Direction.CW)
{ {
knobValue = 1 - (finalRotation.eulerAngles.z / 360f); KnobValue = 1 - (finalRotation.eulerAngles.z / 360f);
if (snapToPosition) if (SnapToPosition)
{ {
SnapToPosition(ref knobValue); SnapToPositionValue(ref KnobValue);
finalRotation.eulerAngles = new Vector3(0, 0, 360 - 360 * knobValue); finalRotation.eulerAngles = new Vector3(0, 0, 360 - 360 * KnobValue);
} }
} }
else else
{ {
knobValue = (finalRotation.eulerAngles.z / 360f); KnobValue = (finalRotation.eulerAngles.z / 360f);
if (snapToPosition) if (SnapToPosition)
{ {
SnapToPosition(ref knobValue); SnapToPositionValue(ref KnobValue);
finalRotation.eulerAngles = new Vector3(0, 0, 360 * knobValue); finalRotation.eulerAngles = new Vector3(0, 0, 360 * KnobValue);
} }
} }
UpdateKnobValue();
transform.rotation = finalRotation;
InvokeEvents(KnobValue + _currentLoops);
_previousValue = KnobValue;
}
private void UpdateKnobValue()
{
//PREVENT OVERROTATION //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++; _currentLoops++;
} }
else if (knobValue > 0.5f && _currentLoops >= 1) else if (KnobValue > 0.5f && _currentLoops >= 1)
{ {
_currentLoops--; _currentLoops--;
} }
else else
{ {
if (knobValue > 0.5f && _currentLoops == 0) if (KnobValue > 0.5f && _currentLoops == 0)
{ {
knobValue = 0; KnobValue = 0;
transform.localEulerAngles = Vector3.zero; transform.localEulerAngles = Vector3.zero;
InvokeEvents(knobValue + _currentLoops); InvokeEvents(KnobValue + _currentLoops);
return; 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; transform.localEulerAngles = Vector3.zero;
InvokeEvents(knobValue + _currentLoops); InvokeEvents(KnobValue + _currentLoops);
return; return;
} }
} }
} }
//CHECK MAX VALUE //CHECK MAX VALUE
if (maxValue > 0) if (MaxValue > 0)
{ {
if (knobValue + _currentLoops > maxValue) if (KnobValue + _currentLoops > MaxValue)
{ {
knobValue = maxValue; KnobValue = MaxValue;
float maxAngle = direction == Direction.CW ? 360f - 360f * maxValue : 360f * maxValue; float maxAngle = direction == Direction.CW ? 360f - 360f * MaxValue : 360f * MaxValue;
transform.localEulerAngles = new Vector3(0, 0, maxAngle); transform.localEulerAngles = new Vector3(0, 0, maxAngle);
InvokeEvents(knobValue); InvokeEvents(KnobValue);
return; 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; float newValue = Mathf.Round(knobValue / snapStep) * snapStep;
knobValue = newValue; knobValue = newValue;
} }
private void InvokeEvents(float value) private void InvokeEvents(float value)
{ {
if (clampOutput01) if (ClampOutput01)
value /= loops; value /= Loops;
OnValueChanged.Invoke(value); OnValueChanged.Invoke(value);
} }

View File

@ -5,7 +5,6 @@
using System; using System;
using UnityEngine.Events; using UnityEngine.Events;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
using UnityEngine.UIElements;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {

View File

@ -4,7 +4,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
#if !ENABLE_LEGACY_INPUT_MANAGER #if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls; using UnityEngine.InputSystem.Controls;
#endif #endif
@ -13,7 +13,7 @@ namespace UnityEngine.UI.Extensions
{ {
public static class UIExtensionsInputManager 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 bool[] mouseButtons = new bool[3] { false, false, false };
private static Dictionary<KeyCode, bool> keys = new Dictionary<KeyCode, bool>(); private static Dictionary<KeyCode, bool> keys = new Dictionary<KeyCode, bool>();
private static Dictionary<String, bool> buttons = new Dictionary<String, bool>(); private static Dictionary<String, bool> buttons = new Dictionary<String, bool>();
@ -21,23 +21,21 @@ namespace UnityEngine.UI.Extensions
public static bool GetMouseButton(int button) public static bool GetMouseButton(int button)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER #if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
return Input.GetMouseButton(button);
#else
if (Mouse.current == null) if (Mouse.current == null)
{ {
return false; return false;
} }
return Mouse.current.leftButton.isPressed; return Mouse.current.leftButton.isPressed;
#else
return Input.GetMouseButton(button);
#endif #endif
} }
public static bool GetMouseButtonDown(int button) public static bool GetMouseButtonDown(int button)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER #if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
return Input.GetMouseButtonDown(button);
#else
if (Mouse.current == null) if (Mouse.current == null)
{ {
return false; return false;
@ -52,14 +50,14 @@ namespace UnityEngine.UI.Extensions
} }
} }
return false; return false;
#else
return Input.GetMouseButtonDown(button);
#endif #endif
} }
public static bool GetMouseButtonUp(int button) public static bool GetMouseButtonUp(int button)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER #if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
return Input.GetMouseButtonUp(button);
#else
if (Mouse.current == null) if (Mouse.current == null)
{ {
return false; return false;
@ -71,14 +69,14 @@ namespace UnityEngine.UI.Extensions
return true; return true;
} }
return false; return false;
#else
return Input.GetMouseButtonUp(button);
#endif #endif
} }
public static bool GetButton(string input) public static bool GetButton(string input)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER #if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
return Input.GetButton(input);
#else
ButtonControl buttonPressed = GetButtonControlFromString(input); ButtonControl buttonPressed = GetButtonControlFromString(input);
if (!buttons.ContainsKey(input)) if (!buttons.ContainsKey(input))
@ -87,10 +85,12 @@ namespace UnityEngine.UI.Extensions
} }
return buttonPressed != null ? buttonPressed.isPressed : false; return buttonPressed != null ? buttonPressed.isPressed : false;
#else
return Input.GetButton(input);
#endif #endif
} }
#if !ENABLE_LEGACY_INPUT_MANAGER #if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
private static ButtonControl GetButtonControlFromString(string input) private static ButtonControl GetButtonControlFromString(string input)
{ {
if (Gamepad.current == null) if (Gamepad.current == null)
@ -112,9 +112,7 @@ namespace UnityEngine.UI.Extensions
public static bool GetButtonDown(string input) public static bool GetButtonDown(string input)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER #if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
return Input.GetButtonDown(input);
#else
ButtonControl buttonPressed = GetButtonControlFromString(input); ButtonControl buttonPressed = GetButtonControlFromString(input);
if (buttonPressed.isPressed) if (buttonPressed.isPressed)
@ -135,14 +133,14 @@ namespace UnityEngine.UI.Extensions
buttons[input] = false; buttons[input] = false;
} }
return false; return false;
#else
return Input.GetButtonDown(input);
#endif #endif
} }
public static bool GetButtonUp(string input) public static bool GetButtonUp(string input)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER #if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
return Input.GetButtonUp(input);
#else
ButtonControl buttonPressed = GetButtonControlFromString(input); ButtonControl buttonPressed = GetButtonControlFromString(input);
if (buttons[input] && !buttonPressed.isPressed) if (buttons[input] && !buttonPressed.isPressed)
@ -151,14 +149,14 @@ namespace UnityEngine.UI.Extensions
return true; return true;
} }
return false; return false;
#else
return Input.GetButtonUp(input);
#endif #endif
} }
public static bool GetKey(KeyCode key) public static bool GetKey(KeyCode key)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER #if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
return Input.GetKey(key);
#else
KeyControl keyPressed = GetKeyControlFromKeyCode(key); KeyControl keyPressed = GetKeyControlFromKeyCode(key);
if (!keys.ContainsKey(key)) if (!keys.ContainsKey(key))
{ {
@ -166,10 +164,12 @@ namespace UnityEngine.UI.Extensions
} }
return keyPressed != null ? keyPressed.isPressed : false; return keyPressed != null ? keyPressed.isPressed : false;
#else
return Input.GetKey(key);
#endif #endif
} }
#if !ENABLE_LEGACY_INPUT_MANAGER #if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
private static KeyControl GetKeyControlFromKeyCode(KeyCode key) private static KeyControl GetKeyControlFromKeyCode(KeyCode key)
{ {
if (Keyboard.current == null) if (Keyboard.current == null)
@ -203,9 +203,7 @@ namespace UnityEngine.UI.Extensions
public static bool GetKeyDown(KeyCode key) public static bool GetKeyDown(KeyCode key)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER #if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
return Input.GetKeyDown(key);
#else
KeyControl keyPressed = GetKeyControlFromKeyCode(key); KeyControl keyPressed = GetKeyControlFromKeyCode(key);
if (keyPressed.isPressed) if (keyPressed.isPressed)
{ {
@ -225,14 +223,14 @@ namespace UnityEngine.UI.Extensions
keys[key] = false; keys[key] = false;
} }
return false; return false;
#else
return Input.GetKeyDown(key);
#endif #endif
} }
public static bool GetKeyUp(KeyCode key) public static bool GetKeyUp(KeyCode key)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER #if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
return Input.GetKeyUp(key);
#else
KeyControl keyPressed = GetKeyControlFromKeyCode(key); KeyControl keyPressed = GetKeyControlFromKeyCode(key);
if (keys[key] && !keyPressed.isPressed) if (keys[key] && !keyPressed.isPressed)
{ {
@ -240,14 +238,14 @@ namespace UnityEngine.UI.Extensions
return true; return true;
} }
return false; return false;
#else
return Input.GetKeyUp(key);
#endif #endif
} }
public static float GetAxisRaw(string axis) public static float GetAxisRaw(string axis)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER #if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
return Input.GetAxisRaw(axis);
#else
if (Gamepad.current == null) if (Gamepad.current == null)
{ {
return 0f; return 0f;
@ -262,6 +260,8 @@ namespace UnityEngine.UI.Extensions
} }
return 0f; return 0f;
#else
return Input.GetAxisRaw(axis);
#endif #endif
} }
@ -269,10 +269,10 @@ namespace UnityEngine.UI.Extensions
{ {
get get
{ {
#if ENABLE_LEGACY_INPUT_MANAGER #if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
return Input.mousePosition;
#else
return Mouse.current.position.ReadValue(); return Mouse.current.position.ReadValue();
#else
return Input.mousePosition;
#endif #endif
} }
} }
@ -281,10 +281,10 @@ namespace UnityEngine.UI.Extensions
{ {
get get
{ {
#if ENABLE_LEGACY_INPUT_MANAGER #if UNITY_2019_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
return Input.mouseScrollDelta;
#else
return Mouse.current.position.ReadValue(); return Mouse.current.position.ReadValue();
#else
return Input.mouseScrollDelta;
#endif #endif
} }
} }

View File

@ -10,6 +10,7 @@ using UnityEngine.EventSystems;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
[AddComponentMenu("UI/Extensions/UI Magnetic Infinite Scroll")] [AddComponentMenu("UI/Extensions/UI Magnetic Infinite Scroll")]
[RequireComponent(typeof(ScrollRect))]
public class UI_MagneticInfiniteScroll : UI_InfiniteScroll, IDragHandler, IEndDragHandler, IScrollHandler public class UI_MagneticInfiniteScroll : UI_InfiniteScroll, IDragHandler, IEndDragHandler, IScrollHandler
{ {
public event Action<GameObject> OnNewSelect; public event Action<GameObject> OnNewSelect;