TouchEventEmitter.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #include "TouchEventEmitter.h"
  8. namespace facebook {
  9. namespace react {
  10. #pragma mark - Touches
  11. static jsi::Value touchPayload(jsi::Runtime &runtime, Touch const &touch) {
  12. auto object = jsi::Object(runtime);
  13. object.setProperty(runtime, "locationX", touch.offsetPoint.x);
  14. object.setProperty(runtime, "locationY", touch.offsetPoint.y);
  15. object.setProperty(runtime, "pageX", touch.pagePoint.x);
  16. object.setProperty(runtime, "pageY", touch.pagePoint.y);
  17. object.setProperty(runtime, "screenX", touch.screenPoint.x);
  18. object.setProperty(runtime, "screenY", touch.screenPoint.y);
  19. object.setProperty(runtime, "identifier", touch.identifier);
  20. object.setProperty(runtime, "target", touch.target);
  21. object.setProperty(runtime, "timestamp", touch.timestamp * 1000);
  22. object.setProperty(runtime, "force", touch.force);
  23. return object;
  24. }
  25. static jsi::Value touchesPayload(
  26. jsi::Runtime &runtime,
  27. Touches const &touches) {
  28. auto array = jsi::Array(runtime, touches.size());
  29. int i = 0;
  30. for (auto const &touch : touches) {
  31. array.setValueAtIndex(runtime, i++, touchPayload(runtime, touch));
  32. }
  33. return array;
  34. }
  35. static jsi::Value touchEventPayload(
  36. jsi::Runtime &runtime,
  37. TouchEvent const &event) {
  38. auto object = jsi::Object(runtime);
  39. object.setProperty(
  40. runtime, "touches", touchesPayload(runtime, event.touches));
  41. object.setProperty(
  42. runtime, "changedTouches", touchesPayload(runtime, event.changedTouches));
  43. object.setProperty(
  44. runtime, "targetTouches", touchesPayload(runtime, event.targetTouches));
  45. return object;
  46. }
  47. void TouchEventEmitter::dispatchTouchEvent(
  48. std::string const &type,
  49. TouchEvent const &event,
  50. EventPriority const &priority) const {
  51. dispatchEvent(
  52. type,
  53. [event](jsi::Runtime &runtime) {
  54. return touchEventPayload(runtime, event);
  55. },
  56. priority);
  57. }
  58. void TouchEventEmitter::onTouchStart(TouchEvent const &event) const {
  59. dispatchTouchEvent("touchStart", event, EventPriority::AsynchronousBatched);
  60. }
  61. void TouchEventEmitter::onTouchMove(TouchEvent const &event) const {
  62. dispatchTouchEvent("touchMove", event, EventPriority::AsynchronousBatched);
  63. }
  64. void TouchEventEmitter::onTouchEnd(TouchEvent const &event) const {
  65. dispatchTouchEvent("touchEnd", event, EventPriority::AsynchronousBatched);
  66. }
  67. void TouchEventEmitter::onTouchCancel(TouchEvent const &event) const {
  68. dispatchTouchEvent("touchCancel", event, EventPriority::AsynchronousBatched);
  69. }
  70. } // namespace react
  71. } // namespace facebook