/* * @author Valentin Simonov / http://va.lent.in/ */ using System; using TouchScript.Core; using TouchScript.Pointers; using UnityEngine; namespace TouchScript.InputSources { #region Consts public delegate void PointerDelegate(Pointer pointer); #endregion /// /// Base class for all pointer input sources. /// public abstract class InputSource : MonoBehaviour, IInputSource { #region Public properties /// /// Gets or sets current remapper. /// /// Optional remapper to use to change screen coordinates which go into the TouchManager. public ICoordinatesRemapper CoordinatesRemapper { get { return coordinatesRemapper; } set { if (coordinatesRemapper == value) return; coordinatesRemapper = value; updateCoordinatesRemapper(value); } } #endregion #region Private variables /// [SerializeField] [HideInInspector] protected bool basicEditor = true; private ICoordinatesRemapper coordinatesRemapper; private TouchManagerInstance touchManager; protected int screenWidth; protected int screenHeight; #endregion #region Public methods /// public virtual bool UpdateInput() { return false; } /// public virtual bool CancelPointer(Pointer pointer, bool shouldReturn) { return false; } #endregion #region Internal methods /// public virtual void INTERNAL_DiscardPointer(Pointer pointer) {} /// public virtual void INTERNAL_UpdateResolution() { screenWidth = Screen.width; screenHeight = Screen.height; } #endregion #region Unity methods /// /// Unity OnEnable callback. /// private void OnEnable() { touchManager = TouchManagerInstance.Instance; if (touchManager == null) throw new InvalidOperationException("TouchManager instance is required!"); touchManager.AddInput(this); init(); INTERNAL_UpdateResolution(); } /// /// Unity OnDestroy callback. /// protected virtual void OnDisable() { if (touchManager != null) { touchManager.RemoveInput(this); touchManager = null; } } #endregion #region Protected methods /// /// Initializes the input source. /// protected virtual void init() {} /// /// Adds the pointer to the system. /// /// The pointer to add. protected virtual void addPointer(Pointer pointer) { touchManager.INTERNAL_AddPointer(pointer); } /// /// Mark pointer as updated. /// /// The pointer to update. protected virtual void updatePointer(Pointer pointer) { if (pointer == null) return; touchManager.INTERNAL_UpdatePointer(pointer.Id); } /// /// Mark the pointer as touching the surface. /// /// The pointer. protected virtual void pressPointer(Pointer pointer) { if (pointer == null) return; touchManager.INTERNAL_PressPointer(pointer.Id); } /// /// Mark the pointer as no longer touching the surface. /// /// The pointer. protected virtual void releasePointer(Pointer pointer) { if (pointer == null) return; pointer.Buttons &= ~Pointer.PointerButtonState.AnyButtonPressed; touchManager.INTERNAL_ReleasePointer(pointer.Id); } /// /// Removes the pointer. /// /// The pointer. protected virtual void removePointer(Pointer pointer) { if (pointer == null) return; touchManager.INTERNAL_RemovePointer(pointer.Id); } /// /// Cancels the pointer. /// /// The pointer. protected virtual void cancelPointer(Pointer pointer) { if (pointer == null) return; touchManager.INTERNAL_CancelPointer(pointer.Id); } /// /// Called from setter to update touch handlers with the new value. /// /// The new remapper. protected virtual void updateCoordinatesRemapper(ICoordinatesRemapper remapper) {} /// /// Remaps the coordinates using the if it is set. /// /// The position. /// Remapped position if is set; the value of position argument otherwise. protected virtual Vector2 remapCoordinates(Vector2 position) { if (coordinatesRemapper != null) return coordinatesRemapper.Remap(position); return position; } #endregion } }