From 4e6f14118ffe5f66ecc9bae3eb8c823922af1442 Mon Sep 17 00:00:00 2001 From: "Simon (Darkside) Jackson" Date: Sun, 29 Apr 2018 12:10:34 +0100 Subject: [PATCH] Updated ColorSampler and TiltWindow with multi-touch support. Also resolved an annoying warning from the BoxSlider (Resolves #234) --- Scripts/Controls/BoxSlider.cs | 2 +- Scripts/Controls/ColorPicker/ColorSampler.cs | 39 +++++++++++--- Scripts/Controls/ColorPicker/TiltWindow.cs | 53 ++++++++++++-------- Scripts/Utilities/UIExtensionMethods.cs | 39 ++++++++++++++ 4 files changed, 103 insertions(+), 30 deletions(-) diff --git a/Scripts/Controls/BoxSlider.cs b/Scripts/Controls/BoxSlider.cs index 68ede86..84ac654 100644 --- a/Scripts/Controls/BoxSlider.cs +++ b/Scripts/Controls/BoxSlider.cs @@ -137,7 +137,7 @@ namespace UnityEngine.UI.Extensions SetX(m_ValueX, false); SetY(m_ValueY, false); // Update rects since other things might affect them even if value didn't change. - UpdateVisuals(); + if(!Application.isPlaying) UpdateVisuals(); var prefabType = UnityEditor.PrefabUtility.GetPrefabType(this); if (prefabType != UnityEditor.PrefabType.Prefab && !Application.isPlaying) diff --git a/Scripts/Controls/ColorPicker/ColorSampler.cs b/Scripts/Controls/ColorPicker/ColorSampler.cs index d1f359c..2aeb1d6 100644 --- a/Scripts/Controls/ColorPicker/ColorSampler.cs +++ b/Scripts/Controls/ColorPicker/ColorSampler.cs @@ -1,3 +1,9 @@ +/// Credit judah4 +/// Sourced from - http://forum.unity3d.com/threads/color-picker.267043/ +/// Updated by SimonDarksideJ - Updated to use touch position rather than mouse for multi-touch + +using UnityEngine.EventSystems; + namespace UnityEngine.UI.Extensions.ColorPicker { /// @@ -8,10 +14,13 @@ namespace UnityEngine.UI.Extensions.ColorPicker /// /// This does not work well with a world space UI as positioning is working with screen space. /// - public class ColorSampler : MonoBehaviour - { - [SerializeField] + public class ColorSampler : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler + { + private Vector2 m_screenPos; + + [SerializeField] protected Button sampler; + private RectTransform sampleRectTransform; [SerializeField] protected Outline samplerOutline; @@ -25,7 +34,8 @@ namespace UnityEngine.UI.Extensions.ColorPicker protected virtual void OnEnable() { screenCapture = ScreenCapture.CaptureScreenshotAsTexture(); - sampler.gameObject.SetActive(true); + sampleRectTransform = sampler.GetComponent(); + sampler.gameObject.SetActive(true); sampler.onClick.AddListener(SelectColor); } @@ -41,8 +51,8 @@ namespace UnityEngine.UI.Extensions.ColorPicker if (screenCapture == null) return; - sampler.transform.position = Input.mousePosition; - color = screenCapture.GetPixel((int)Input.mousePosition.x, (int)Input.mousePosition.y); + sampleRectTransform.position = m_screenPos; + color = screenCapture.GetPixel((int)m_screenPos.x, (int)m_screenPos.y); HandleSamplerColoring(); } @@ -66,5 +76,20 @@ namespace UnityEngine.UI.Extensions.ColorPicker enabled = false; } - } + + public void OnPointerDown(PointerEventData eventData) + { + m_screenPos = eventData.position; + } + + public void OnPointerUp(PointerEventData eventData) + { + m_screenPos = Vector2.zero; + } + + public void OnDrag(PointerEventData eventData) + { + m_screenPos = eventData.position; + } + } } diff --git a/Scripts/Controls/ColorPicker/TiltWindow.cs b/Scripts/Controls/ColorPicker/TiltWindow.cs index 98f6d8a..d87bc51 100644 --- a/Scripts/Controls/ColorPicker/TiltWindow.cs +++ b/Scripts/Controls/ColorPicker/TiltWindow.cs @@ -1,34 +1,43 @@ ///Credit judah4 ///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/ +using UnityEngine.EventSystems; namespace UnityEngine.UI.Extensions { - public class TiltWindow : MonoBehaviour -{ - public Vector2 range = new Vector2(5f, 3f); + public class TiltWindow : MonoBehaviour, IDragHandler + { + public Vector2 range = new Vector2(5f, 3f); - Transform mTrans; - Quaternion mStart; - Vector2 mRot = Vector2.zero; + private Transform mTrans; + private Quaternion mStart; + private Vector2 mRot = Vector2.zero; + private Vector2 m_screenPos; - void Start () - { - mTrans = transform; - mStart = mTrans.localRotation; - } - void Update () - { - Vector3 pos = Input.mousePosition; + void Start() + { + mTrans = transform; + mStart = mTrans.localRotation; + } - float halfWidth = Screen.width * 0.5f; - float halfHeight = Screen.height * 0.5f; - float x = Mathf.Clamp((pos.x - halfWidth) / halfWidth, -1f, 1f); - float y = Mathf.Clamp((pos.y - halfHeight) / halfHeight, -1f, 1f); - mRot = Vector2.Lerp(mRot, new Vector2(x, y), Time.deltaTime * 5f); + void Update() + { + Vector3 pos = m_screenPos; - mTrans.localRotation = mStart * Quaternion.Euler(-mRot.y * range.y, mRot.x * range.x, 0f); - } -} + float halfWidth = Screen.width * 0.5f; + float halfHeight = Screen.height * 0.5f; + float x = Mathf.Clamp((pos.x - halfWidth) / halfWidth, -1f, 1f); + float y = Mathf.Clamp((pos.y - halfHeight) / halfHeight, -1f, 1f); + mRot = Vector2.Lerp(mRot, new Vector2(x, y), Time.deltaTime * 5f); + + mTrans.localRotation = mStart * Quaternion.Euler(-mRot.y * range.y, mRot.x * range.x, 0f); + } + + + public void OnDrag(PointerEventData eventData) + { + m_screenPos = eventData.position; + } + } } \ No newline at end of file diff --git a/Scripts/Utilities/UIExtensionMethods.cs b/Scripts/Utilities/UIExtensionMethods.cs index bb49049..e879a75 100644 --- a/Scripts/Utilities/UIExtensionMethods.cs +++ b/Scripts/Utilities/UIExtensionMethods.cs @@ -22,5 +22,44 @@ namespace UnityEngine.UI.Extensions return parentCanvas; } + public static Vector2 TransformInputBasedOnCanvasType(this Vector2 input, Canvas canvas) + { + if (canvas.renderMode == RenderMode.ScreenSpaceOverlay) + { + return canvas.GetEventCamera().ScreenToWorldPoint(input); + } + else + { + return input; + } + } + + public static Vector3 TransformInputBasedOnCanvasType(this Vector2 input, RectTransform rt) + { + var canvas = rt.GetParentCanvas(); + if (input == Vector2.zero || canvas.renderMode == RenderMode.ScreenSpaceOverlay) + { + return input; + } + else + { + // Needs work :S + Vector2 movePos; + + RectTransformUtility.ScreenPointToLocalPointInRectangle( + rt, + input, canvas.GetEventCamera(), + out movePos); + + Vector3 output = canvas.transform.TransformPoint(movePos); + return output; + } + } + + public static Camera GetEventCamera(this Canvas input) + { + return input.worldCamera == null ? Camera.main : input.worldCamera; + + } } }