nodeComponents/click/click.js

  1. // dependencies
  2. import _ from 'lodash';
  3. import * as THREE from 'three';
  4. // local dependencies
  5. import store from '../../store/store';
  6. import C from '../../constants/constants';
  7. import { simulate } from '../../rootComponents/rootComponents';
  8. import { getCallbackID, worldToScreenSpace, getNodePosition } from '../../util/helpers';
  9. export default (node) =>
  10. /**
  11. * Click the position (center) of a given existing node. You can pass `debug` to see cursor.
  12. *
  13. * @module click
  14. * @param {boolean} [debug=false] - Allows to turn on the visuals for the `click` action.
  15. * @returns {self} - Chainable api by returning the node.
  16. *
  17. * @example
  18. * $$$
  19. * .find('Cube_1') // an element in the scene with a name of `Cube_1`
  20. * .click(); // clicks the node
  21. *
  22. * $$$
  23. * .find('Cube_1')
  24. * .click(true); // you can also pass in `debug` as `true` to see the cursor
  25. */
  26. (debug = false) => {
  27. const position = getNodePosition(node);
  28. const canvas = store.get(C.RENDERER).domElement;
  29. const screenSpace = worldToScreenSpace(position.x, position.y, position.z, canvas);
  30. const eventData = {
  31. clientX: screenSpace.x,
  32. clientY: screenSpace.y
  33. };
  34. simulate(C.MOUSE_MOVE, eventData, debug);
  35. simulate(C.CLICK, eventData, debug);
  36. // trigger all associated events
  37. const callbackID = getCallbackID(node, C.CLICK);
  38. const callbacks = store.get(callbackID);
  39. if (callbacks) {
  40. _.each(callbacks, (cb) => cb(node));
  41. }
  42. return node;
  43. };