using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace CarbonInput {
///
/// Touch control simulating a single gamepad button.
///
public class TouchButton : BaseTouchInput, IPointerDownHandler, IPointerUpHandler, IDragHandler {
///
/// The this control emulates.
///
public CButton Button;
///
/// Opacity of this control if it is pressed.
///
[Tooltip("Opacity of this control if it is pressed.")]
[Range(0, 1)]
public float OpacityPressed = 0.5f;
///
/// Opacity of this control if it is not pressed.
///
[Tooltip("Opacity of this control if it is not pressed.")]
[Range(0, 1)]
public float OpacityReleased = 1f;
void Start() {
InitMapping();
UpdateState(false);
}
///
/// Updates the state of this control. This methods sets the opacity and the state in the .
///
///
public void UpdateState(bool pressed) {
var image = GetComponent();
var color = image.color;
color.a = pressed ? OpacityPressed : OpacityReleased;
image.color = color;
if(Mapping != null) Mapping[Button] = pressed;
}
public void OnPointerDown(PointerEventData eventData) {
UpdateState(true);
}
public void OnPointerUp(PointerEventData eventData) {
UpdateState(false);
}
public void OnDrag(PointerEventData eventData) {
RectTransform rect = GetComponent();
UpdateState(RectTransformUtility.RectangleContainsScreenPoint(rect, eventData.position));
}
}
}