gamepad_user_gesture.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "device/gamepad/gamepad_user_gesture.h"
  5. #include <math.h>
  6. #include <algorithm>
  7. #include "device/gamepad/public/cpp/gamepads.h"
  8. namespace {
  9. // A big enough deadzone to detect accidental presses.
  10. const float kAxisMoveAmountThreshold = 0.5;
  11. }
  12. namespace device {
  13. bool GamepadsHaveUserGesture(const Gamepads& gamepads) {
  14. for (size_t i = 0; i < Gamepads::kItemsLengthCap; i++) {
  15. const Gamepad& pad = gamepads.items[i];
  16. // If the device is physically connected, then check the buttons and axes
  17. // to see if there is currently an intentional user action.
  18. if (pad.connected) {
  19. // Only VR Controllers have a display id, and are only reported as
  20. // connected during WebVR presentation, so the user is definitely
  21. // expecting their controller to be used. Note that this will also
  22. // satisfy the gesture requirement for all other connected controllers,
  23. // exposing them too. This is unfortunate, but worth the tradeoff and will
  24. // go away in the future when WebVR is fully replaced with WebXR.
  25. if (pad.display_id != 0)
  26. return true;
  27. for (size_t button_index = 0; button_index < pad.buttons_length;
  28. button_index++) {
  29. if (pad.buttons[button_index].pressed)
  30. return true;
  31. }
  32. for (size_t axes_index = 0; axes_index < pad.axes_length; axes_index++) {
  33. if (fabs(pad.axes[axes_index]) > kAxisMoveAmountThreshold)
  34. return true;
  35. }
  36. }
  37. }
  38. return false;
  39. }
  40. } // namespace device