Updated ColorSampler and TiltWindow with multi-touch support.

Also resolved an annoying warning from the BoxSlider (Resolves #234)
pull/413/head
Simon (Darkside) Jackson 2018-04-29 12:10:34 +01:00
parent 75bcf05f32
commit 4e6f14118f
4 changed files with 103 additions and 30 deletions

View File

@ -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)

View File

@ -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
{
/// <summary>
@ -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.
/// </summary>
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<RectTransform>();
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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}