AbstractTouchElement.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using UnityEngine;
  3. namespace FBInput
  4. {
  5. public abstract class AbstractTouchElement : BaseBehaviour
  6. {
  7. protected bool IsActive
  8. {
  9. get
  10. {
  11. return this._fingerId != -1;
  12. }
  13. }
  14. protected virtual void Update()
  15. {
  16. if (UnityEngine.Input.touchCount > 0)
  17. {
  18. Touch touch = default(Touch);
  19. if (this.IsActive)
  20. {
  21. for (int i = 0; i < UnityEngine.Input.touchCount; i++)
  22. {
  23. touch = UnityEngine.Input.GetTouch(i);
  24. if (touch.fingerId == this._fingerId)
  25. {
  26. break;
  27. }
  28. }
  29. }
  30. else
  31. {
  32. for (int j = 0; j < UnityEngine.Input.touchCount; j++)
  33. {
  34. touch = UnityEngine.Input.GetTouch(j);
  35. if (touch.phase == TouchPhase.Began)
  36. {
  37. Vector2 vector = UITools.ScreenPositionToLocalPosition(touch.position);
  38. if (this.TestTouchArea(vector))
  39. {
  40. this._fingerId = touch.fingerId;
  41. this.OnTouchDown(vector);
  42. }
  43. }
  44. }
  45. }
  46. if (this.IsActive)
  47. {
  48. Vector2 localPosition = UITools.ScreenPositionToLocalPosition(touch.position);
  49. if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
  50. {
  51. this._fingerId = -1;
  52. this.OnTouchUp(localPosition);
  53. }
  54. else
  55. {
  56. this.OnTouch(localPosition);
  57. }
  58. }
  59. }
  60. else
  61. {
  62. if (!this.IsActive && Input.GetMouseButton(0))
  63. {
  64. Vector2 vector2 = UITools.ScreenPositionToLocalPosition(UnityEngine.Input.mousePosition);
  65. if (this.TestTouchArea(vector2))
  66. {
  67. this._fingerId = 0;
  68. this.OnTouchDown(vector2);
  69. }
  70. }
  71. if (this.IsActive)
  72. {
  73. Vector2 localPosition2 = UITools.ScreenPositionToLocalPosition(UnityEngine.Input.mousePosition);
  74. if (!Input.GetMouseButton(0))
  75. {
  76. this._fingerId = -1;
  77. this.OnTouchUp(localPosition2);
  78. }
  79. else
  80. {
  81. this.OnTouch(localPosition2);
  82. }
  83. }
  84. }
  85. }
  86. protected virtual void OnTouchDown(Vector2 touchPosition)
  87. {
  88. }
  89. protected virtual void OnTouchUp(Vector2 localPosition)
  90. {
  91. }
  92. protected virtual void OnTouch(Vector2 localPosition)
  93. {
  94. }
  95. protected virtual bool TestTouchArea(Vector2 localPosition)
  96. {
  97. return true;
  98. }
  99. private int _fingerId = -1;
  100. }
  101. }