singletouch.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. var SINGLE_TOUCH_INPUT_MAP = {
  2. touchstart: INPUT_START,
  3. touchmove: INPUT_MOVE,
  4. touchend: INPUT_END,
  5. touchcancel: INPUT_CANCEL
  6. };
  7. var SINGLE_TOUCH_TARGET_EVENTS = 'touchstart';
  8. var SINGLE_TOUCH_WINDOW_EVENTS = 'touchstart touchmove touchend touchcancel';
  9. /**
  10. * Touch events input
  11. * @constructor
  12. * @extends Input
  13. */
  14. function SingleTouchInput() {
  15. this.evTarget = SINGLE_TOUCH_TARGET_EVENTS;
  16. this.evWin = SINGLE_TOUCH_WINDOW_EVENTS;
  17. this.started = false;
  18. Input.apply(this, arguments);
  19. }
  20. inherit(SingleTouchInput, Input, {
  21. handler: function TEhandler(ev) {
  22. var type = SINGLE_TOUCH_INPUT_MAP[ev.type];
  23. // should we handle the touch events?
  24. if (type === INPUT_START) {
  25. this.started = true;
  26. }
  27. if (!this.started) {
  28. return;
  29. }
  30. var touches = normalizeSingleTouches.call(this, ev, type);
  31. // when done, reset the started state
  32. if (type & (INPUT_END | INPUT_CANCEL) && touches[0].length - touches[1].length === 0) {
  33. this.started = false;
  34. }
  35. this.callback(this.manager, type, {
  36. pointers: touches[0],
  37. changedPointers: touches[1],
  38. pointerType: INPUT_TYPE_TOUCH,
  39. srcEvent: ev
  40. });
  41. }
  42. });
  43. /**
  44. * @this {TouchInput}
  45. * @param {Object} ev
  46. * @param {Number} type flag
  47. * @returns {undefined|Array} [all, changed]
  48. */
  49. function normalizeSingleTouches(ev, type) {
  50. var all = toArray(ev.touches);
  51. var changed = toArray(ev.changedTouches);
  52. if (type & (INPUT_END | INPUT_CANCEL)) {
  53. all = uniqueArray(all.concat(changed), 'identifier', true);
  54. }
  55. return [all, changed];
  56. }