imageviewerGamepad.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. window.addEventListener('gamepadconnected', (e) => {
  2. const index = e.gamepad.index;
  3. let isWaiting = false;
  4. setInterval(async() => {
  5. if (!opts.js_modal_lightbox_gamepad || isWaiting) return;
  6. const gamepad = navigator.getGamepads()[index];
  7. const xValue = gamepad.axes[0];
  8. if (xValue <= -0.3) {
  9. modalPrevImage(e);
  10. isWaiting = true;
  11. } else if (xValue >= 0.3) {
  12. modalNextImage(e);
  13. isWaiting = true;
  14. }
  15. if (isWaiting) {
  16. await sleepUntil(() => {
  17. const xValue = navigator.getGamepads()[index].axes[0];
  18. if (xValue < 0.3 && xValue > -0.3) {
  19. return true;
  20. }
  21. }, opts.js_modal_lightbox_gamepad_repeat);
  22. isWaiting = false;
  23. }
  24. }, 10);
  25. });
  26. /*
  27. Primarily for vr controller type pointer devices.
  28. I use the wheel event because there's currently no way to do it properly with web xr.
  29. */
  30. let isScrolling = false;
  31. window.addEventListener('wheel', (e) => {
  32. if (!opts.js_modal_lightbox_gamepad || isScrolling) return;
  33. isScrolling = true;
  34. if (e.deltaX <= -0.6) {
  35. modalPrevImage(e);
  36. } else if (e.deltaX >= 0.6) {
  37. modalNextImage(e);
  38. }
  39. setTimeout(() => {
  40. isScrolling = false;
  41. }, opts.js_modal_lightbox_gamepad_repeat);
  42. });
  43. function sleepUntil(f, timeout) {
  44. return new Promise((resolve) => {
  45. const timeStart = new Date();
  46. const wait = setInterval(function() {
  47. if (f() || new Date() - timeStart > timeout) {
  48. clearInterval(wait);
  49. resolve();
  50. }
  51. }, 20);
  52. });
  53. }