Source: extern/input.js

  1. import register from "../util/register.js";
  2. import m_input_fact from "../intern/input.js";
  3. import m_cam_fact from "../intern/camera.js";
  4. import m_cfg_fact from "../intern/config.js";
  5. import m_cont_fact from "../intern/container.js";
  6. import m_obj_util_fact from "../intern/obj_util.js";
  7. import m_print_fact from "../intern/print.js";
  8. import m_scs_fact from "../intern/scenes.js";
  9. import * as m_vec4 from "../libs/gl_matrix/vec4.js";
  10. /**
  11. * Low-level input device API.
  12. * For more generic cases use {@link module:controls|sensor-based API}.
  13. * @module input
  14. * @local DeviceType
  15. * @local DeviceVectorParameterSync
  16. * @local DeviceValueParameterSync
  17. * @local DeviceParameterAsync
  18. * @local DeviceHMDType
  19. * @local DeviceConfig
  20. * @local MouseLocationCallback
  21. * @local MouseDownWhichCallback
  22. * @local MouseUpWhichCallback
  23. * @local MouseWheelCallback
  24. * @local KeyboardDownCallback
  25. * @local KeyboardUpCallback
  26. * @local TouchStartCallback
  27. * @local TouchMoveCallback
  28. * @local TouchEndCallback
  29. * @local GyroscopeQuatCallback
  30. * @local GyroscopeAnglesCallback
  31. */
  32. function Input(ns, exports) {
  33. var m_input = m_input_fact(ns);
  34. var m_cam = m_cam_fact(ns);
  35. var m_cfg = m_cfg_fact(ns);
  36. var m_cont = m_cont_fact(ns);
  37. var m_obj_util= m_obj_util_fact(ns);
  38. var m_print = m_print_fact(ns);
  39. var m_scs = m_scs_fact(ns);
  40. var _vec4_tmp = m_vec4.create();
  41. var _vec4_tmp2 = m_vec4.create();
  42. var _splited_screen = false;
  43. var cfg_dbg = m_cfg.debug_subs;
  44. var cfg_def = m_cfg.defaults;
  45. /**
  46. * Type of the names of the synchronous vector parameter of a device.
  47. * @typedef {Float32Array} DeviceVectorParameterSync
  48. */
  49. /**
  50. * Type of the names of the synchronous value parameter of a device.
  51. * @typedef {number} DeviceValueParameterSync
  52. */
  53. /**
  54. * Type of the names of the asynchronous parameter of a device.
  55. * @typedef {number} DeviceParameterAsync
  56. */
  57. /**
  58. * HMD type enum. Value of {@link module:input.HMD_WEBVR_TYPE|input.HMD_WEBVR_TYPE}.
  59. * @typedef {number} DeviceHMDType
  60. */
  61. /**
  62. * Type of the names of the device config.
  63. * @typedef {number} DeviceConfig
  64. */
  65. /**
  66. * Gamepad D-pad up button ID.
  67. * @const {number} module:input.GMPD_BUTTON_12
  68. */
  69. exports.GMPD_BUTTON_12 = m_input.GMPD_BUTTON_12;
  70. /**
  71. * Gamepad D-pad down button ID.
  72. * @const {number} module:input.GMPD_BUTTON_13
  73. */
  74. exports.GMPD_BUTTON_13 = m_input.GMPD_BUTTON_13;
  75. /**
  76. * Gamepad D-pad right button ID.
  77. * @const {number} module:input.GMPD_BUTTON_15
  78. */
  79. exports.GMPD_BUTTON_15 = m_input.GMPD_BUTTON_15;
  80. /**
  81. * Gamepad D-pad left button ID.
  82. * @const {number} module:input.GMPD_BUTTON_14
  83. */
  84. exports.GMPD_BUTTON_14 = m_input.GMPD_BUTTON_14;
  85. /**
  86. * Gamepad face panel up button ID.
  87. * @const {number} module:input.GMPD_BUTTON_3
  88. */
  89. exports.GMPD_BUTTON_3 = m_input.GMPD_BUTTON_3;
  90. /**
  91. * Gamepad face panel down button ID.
  92. * @const {number} module:input.GMPD_BUTTON_0
  93. */
  94. exports.GMPD_BUTTON_0 = m_input.GMPD_BUTTON_0;
  95. /**
  96. * Gamepad face panel right button ID.
  97. * @const {number} module:input.GMPD_BUTTON_1
  98. */
  99. exports.GMPD_BUTTON_1 = m_input.GMPD_BUTTON_1;
  100. /**
  101. * Gamepad face panel left button ID.
  102. * @const {number} module:input.GMPD_BUTTON_2
  103. */
  104. exports.GMPD_BUTTON_2 = m_input.GMPD_BUTTON_2;
  105. /**
  106. * Gamepad right top shoulder button ID.
  107. * @const {number} module:input.GMPD_BUTTON_5
  108. */
  109. exports.GMPD_BUTTON_5 = m_input.GMPD_BUTTON_5;
  110. /**
  111. * Gamepad right bottom shoulder button ID.
  112. * @const {number} module:input.GMPD_BUTTON_7
  113. */
  114. exports.GMPD_BUTTON_7 = m_input.GMPD_BUTTON_7;
  115. /**
  116. * Gamepad left top shoulder button ID.
  117. * @const {number} module:input.GMPD_BUTTON_4
  118. */
  119. exports.GMPD_BUTTON_4 = m_input.GMPD_BUTTON_4;
  120. /**
  121. * Gamepad left bottom shoulder button ID.
  122. * @const {number} module:input.GMPD_BUTTON_6
  123. */
  124. exports.GMPD_BUTTON_6 = m_input.GMPD_BUTTON_6;
  125. /**
  126. * Gamepad select/back button ID.
  127. * @const {number} module:input.GMPD_BUTTON_8
  128. */
  129. exports.GMPD_BUTTON_8 = m_input.GMPD_BUTTON_8;
  130. /**
  131. * Gamepad start/forward button ID.
  132. * @const {number} module:input.GMPD_BUTTON_9
  133. */
  134. exports.GMPD_BUTTON_9 = m_input.GMPD_BUTTON_9;
  135. /**
  136. * Gamepad left analog button ID.
  137. * @const {number} module:input.GMPD_BUTTON_10
  138. */
  139. exports.GMPD_BUTTON_10 = m_input.GMPD_BUTTON_10;
  140. /**
  141. * Gamepad right analog button ID.
  142. * @const {number} module:input.GMPD_BUTTON_11
  143. */
  144. exports.GMPD_BUTTON_11 = m_input.GMPD_BUTTON_11;
  145. /**
  146. * Gamepad main button ID.
  147. * @const {number} module:input.GMPD_BUTTON_16
  148. */
  149. exports.GMPD_BUTTON_16 = m_input.GMPD_BUTTON_16;
  150. /**
  151. * Gamepad button.
  152. * @const {number} module:input.GMPD_BUTTON_17
  153. */
  154. exports.GMPD_BUTTON_17 = m_input.GMPD_BUTTON_17;
  155. /**
  156. * Gamepad button.
  157. * @const {number} module:input.GMPD_BUTTON_18
  158. */
  159. exports.GMPD_BUTTON_18 = m_input.GMPD_BUTTON_18;
  160. /**
  161. * Gamepad button.
  162. * @const {number} module:input.GMPD_BUTTON_19
  163. */
  164. exports.GMPD_BUTTON_19 = m_input.GMPD_BUTTON_19;
  165. /**
  166. * Gamepad button.
  167. * @const {number} module:input.GMPD_BUTTON_20
  168. */
  169. exports.GMPD_BUTTON_20 = m_input.GMPD_BUTTON_20;
  170. /**
  171. * Gamepad button.
  172. * @const {number} module:input.GMPD_BUTTON_21
  173. */
  174. exports.GMPD_BUTTON_21 = m_input.GMPD_BUTTON_21;
  175. /**
  176. * Gamepad button.
  177. * @const {number} module:input.GMPD_BUTTON_22
  178. */
  179. exports.GMPD_BUTTON_22 = m_input.GMPD_BUTTON_22;
  180. /**
  181. * Gamepad button.
  182. * @const {number} module:input.GMPD_BUTTON_23
  183. */
  184. exports.GMPD_BUTTON_23 = m_input.GMPD_BUTTON_23;
  185. /**
  186. * Gamepad button.
  187. * @const {number} module:input.GMPD_BUTTON_24
  188. */
  189. exports.GMPD_BUTTON_24 = m_input.GMPD_BUTTON_24;
  190. /**
  191. * Gamepad button.
  192. * @const {number} module:input.GMPD_BUTTON_25
  193. */
  194. exports.GMPD_BUTTON_25 = m_input.GMPD_BUTTON_25;
  195. /**
  196. * Gamepad track pad ID.
  197. * @const {number} module:input.GMPD_TRACKPAD_BUTTON
  198. */
  199. exports.GMPD_TRACKPAD_BUTTON = m_input.GMPD_TRACKPAD_BUTTON;
  200. /**
  201. * Gamepad trigger ID.
  202. * @const {number} module:input.GMPD_TRIGGER_BUTTON
  203. */
  204. exports.GMPD_TRIGGER_BUTTON = m_input.GMPD_TRIGGER_BUTTON;
  205. /**
  206. * Gamepad grips ID.
  207. * @const {number} module:input.GMPD_GRIPS_BUTTON
  208. */
  209. exports.GMPD_GRIPS_BUTTON = m_input.GMPD_GRIPS_BUTTON;
  210. /**
  211. * Gamepad grips ID.
  212. * @const {number} module:input.GMPD_MENU_BUTTON
  213. */
  214. exports.GMPD_MENU_BUTTON = m_input.GMPD_MENU_BUTTON;
  215. /**
  216. * Gamepad axis.
  217. * @const {number} module:input.GMPD_AXIS_0
  218. */
  219. exports.GMPD_AXIS_0 = m_input.GMPD_AXIS_0;
  220. /**
  221. * Gamepad axis.
  222. * @const {number} module:input.GMPD_AXIS_1
  223. */
  224. exports.GMPD_AXIS_1 = m_input.GMPD_AXIS_1;
  225. /**
  226. * Gamepad axis.
  227. * @const {number} module:input.GMPD_AXIS_2
  228. */
  229. exports.GMPD_AXIS_2 = m_input.GMPD_AXIS_2;
  230. /**
  231. * Gamepad axis.
  232. * @const {number} module:input.GMPD_AXIS_3
  233. */
  234. exports.GMPD_AXIS_3 = m_input.GMPD_AXIS_3;
  235. /**
  236. * Gamepad axis.
  237. * @const {number} module:input.GMPD_AXIS_4
  238. */
  239. exports.GMPD_AXIS_4 = m_input.GMPD_AXIS_4;
  240. /**
  241. * Gamepad axis.
  242. * @const {number} module:input.GMPD_AXIS_5
  243. */
  244. exports.GMPD_AXIS_5 = m_input.GMPD_AXIS_5;
  245. /**
  246. * Gamepad axis.
  247. * @const {number} module:input.GMPD_AXIS_6
  248. */
  249. exports.GMPD_AXIS_6 = m_input.GMPD_AXIS_6;
  250. /**
  251. * Gamepad axis.
  252. * @const {number} module:input.GMPD_AXIS_7
  253. */
  254. exports.GMPD_AXIS_7 = m_input.GMPD_AXIS_7;
  255. /**
  256. * Gamepad axis.
  257. * @const {number} module:input.GMPD_AXIS_8
  258. */
  259. exports.GMPD_AXIS_8 = m_input.GMPD_AXIS_8;
  260. /**
  261. * Gamepad axis.
  262. * @const {number} module:input.GMPD_AXIS_9
  263. */
  264. exports.GMPD_AXIS_9 = m_input.GMPD_AXIS_9;
  265. /**
  266. * Gamepad axis.
  267. * @const {number} module:input.GMPD_AXIS_10
  268. */
  269. exports.GMPD_AXIS_10 = m_input.GMPD_AXIS_10;
  270. /**
  271. * Gamepad axis.
  272. * @const {number} module:input.GMPD_AXIS_11
  273. */
  274. exports.GMPD_AXIS_11 = m_input.GMPD_AXIS_11;
  275. /**
  276. * Parameter of HMD orientation quaternion.
  277. * @const {DeviceVectorParameterSync} module:input.HMD_ORIENTATION_QUAT
  278. */
  279. exports.HMD_ORIENTATION_QUAT = m_input.HMD_ORIENTATION_QUAT;
  280. /**
  281. * Parameter of HMD position.
  282. * @const {DeviceVectorParameterSync} module:input.HMD_POSITION
  283. */
  284. exports.HMD_POSITION = m_input.HMD_POSITION;
  285. /**
  286. * Parameter of HMD type.
  287. * @const {DeviceValueParameterSync} module:input.HMD_WEBVR_TYPE
  288. */
  289. exports.HMD_WEBVR_TYPE = m_input.HMD_WEBVR_TYPE;
  290. /**
  291. * WebVR (old) desktop HMD
  292. * @const {DeviceHMDType} module:input.HMD_WEBVR_DESKTOP
  293. */
  294. exports.HMD_WEBVR_DESKTOP = m_input.HMD_WEBVR_DESKTOP;
  295. /**
  296. * WebVR (old) mobile HMD
  297. * @const {DeviceHMDType} module:input.HMD_WEBVR_MOBILE
  298. */
  299. exports.HMD_WEBVR_MOBILE = m_input.HMD_WEBVR_MOBILE;
  300. /**
  301. * Non-WebVR HMD
  302. * @const {DeviceHMDType} module:input.HMD_NON_WEBVR
  303. */
  304. exports.HMD_NON_WEBVR = m_input.HMD_NON_WEBVR;
  305. /**
  306. * WebVR API 1.0
  307. * @const {DeviceHMDType} module:input.HMD_WEBVR1
  308. */
  309. exports.HMD_WEBVR1 = m_input.HMD_WEBVR1;
  310. /**
  311. * WebVR API 1.1
  312. * @const {DeviceHMDType} module:input.HMD_WEBVR1_1
  313. */
  314. exports.HMD_WEBVR1_1 = m_input.HMD_WEBVR1_1;
  315. /**
  316. * Parameter of HMD eye distance.
  317. * @const {DeviceConfig} module:input.HMD_EYE_DISTANCE
  318. */
  319. var HMD_EYE_DISTANCE = m_input.HMD_EYE_DISTANCE
  320. exports.HMD_EYE_DISTANCE = HMD_EYE_DISTANCE;
  321. /**
  322. * Parameter of HMD distortion coefficients.
  323. * @const {DeviceConfig} module:input.HMD_DISTORTION
  324. */
  325. exports.HMD_DISTORTION = m_input.HMD_DISTORTION;
  326. /**
  327. * Parameter of HMD baseline distance.
  328. * @const {DeviceConfig} module:input.HMD_BASELINE_DIST
  329. */
  330. exports.HMD_BASELINE_DIST = m_input.HMD_BASELINE_DIST;
  331. /**
  332. * Parameter of HMD screen to lens distance.
  333. * @const {DeviceConfig} module:input.HMD_SCREEN_LENS_DIST
  334. */
  335. exports.HMD_SCREEN_LENS_DIST = m_input.HMD_SCREEN_LENS_DIST;
  336. /**
  337. * Parameter of mobile screen width.
  338. * @const {DeviceConfig} module:input.HMD_SCREEN_WIDTH
  339. */
  340. exports.HMD_SCREEN_WIDTH = m_input.HMD_SCREEN_WIDTH;
  341. /**
  342. * Parameter of mobile screen height.
  343. * @const {DeviceConfig} module:input.HMD_SCREEN_HEIGHT
  344. */
  345. exports.HMD_SCREEN_HEIGHT = m_input.HMD_SCREEN_HEIGHT;
  346. /**
  347. * Parameter of mobile bevel size.
  348. * @const {DeviceConfig} module:input.HMD_BEVEL_SIZE
  349. */
  350. exports.HMD_BEVEL_SIZE = m_input.HMD_BEVEL_SIZE;
  351. /**
  352. * Parameter of the mouse pointer coordinates.
  353. * @const {DeviceVectorParameterSync | DeviceParameterAsync} module:input.MOUSE_LOCATION
  354. */
  355. exports.MOUSE_LOCATION = m_input.MOUSE_LOCATION;
  356. /**
  357. * Parameter of the mouse downed key.
  358. * @const {DeviceParameterAsync} module:input.MOUSE_DOWN_WHICH
  359. */
  360. exports.MOUSE_DOWN_WHICH = m_input.MOUSE_DOWN_WHICH;
  361. /**
  362. * Parameter of the mouse upped key.
  363. * @const {DeviceParameterAsync} module:input.MOUSE_UP_WHICH
  364. */
  365. exports.MOUSE_UP_WHICH = m_input.MOUSE_UP_WHICH;
  366. /**
  367. * Parameter of the vertical scroll amount.
  368. * @const {DeviceParameterAsync} module:input.MOUSE_WHEEL
  369. */
  370. exports.MOUSE_WHEEL = m_input.MOUSE_WHEEL;
  371. /**
  372. * Parameter of the keyboard upped key.
  373. * @const {DeviceParameterAsync} module:input.KEYBOARD_UP
  374. */
  375. exports.KEYBOARD_UP = m_input.KEYBOARD_UP;
  376. /**
  377. * Parameter of the keyboard downed key.
  378. * @const {DeviceParameterAsync} module:input.KEYBOARD_DOWN
  379. */
  380. exports.KEYBOARD_DOWN = m_input.KEYBOARD_DOWN;
  381. /**
  382. * Parameter of started touch point list on the touch surface.
  383. * @const {DeviceParameterAsync} module:input.TOUCH_START
  384. */
  385. exports.TOUCH_START = m_input.TOUCH_START;
  386. /**
  387. * Parameter of moving touch list on the touch surface.
  388. * @const {DeviceParameterAsync} module:input.TOUCH_MOVE
  389. */
  390. exports.TOUCH_MOVE = m_input.TOUCH_MOVE;
  391. /**
  392. * Parameter of ended touch point list on the touch surface.
  393. * @const {DeviceParameterAsync} module:input.TOUCH_END
  394. */
  395. exports.TOUCH_END = m_input.TOUCH_END;
  396. /**
  397. * Parameter of gyroscope orientation quaternion.
  398. * @const {DeviceParameterAsync} module:input.GYRO_ORIENTATION_QUAT
  399. */
  400. exports.GYRO_ORIENTATION_QUAT = m_input.GYRO_ORIENTATION_QUAT;
  401. /**
  402. * Parameter of gyroscope orientation angles.
  403. * @const {DeviceParameterAsync} module:input.GYRO_ORIENTATION_ANGLES
  404. */
  405. exports.GYRO_ORIENTATION_ANGLES = m_input.GYRO_ORIENTATION_ANGLES;
  406. var SYNC_VECTOR_PARAMS = {};
  407. SYNC_VECTOR_PARAMS[m_input.DEVICE_HMD] = [m_input.HMD_ORIENTATION_QUAT,
  408. m_input.HMD_POSITION];
  409. SYNC_VECTOR_PARAMS[m_input.DEVICE_MOUSE] = [m_input.MOUSE_LOCATION];
  410. var SYNC_VALUE_PARAMS = {};
  411. SYNC_VALUE_PARAMS[m_input.DEVICE_HMD] = [m_input.HMD_WEBVR_TYPE];
  412. var ASYNC_PARAMS = {};
  413. ASYNC_PARAMS[m_input.DEVICE_MOUSE] = [m_input.MOUSE_LOCATION,
  414. m_input.MOUSE_DOWN_WHICH, m_input.MOUSE_UP_WHICH, m_input.MOUSE_WHEEL];
  415. ASYNC_PARAMS[m_input.DEVICE_KEYBOARD] = [m_input.KEYBOARD_UP,
  416. m_input.KEYBOARD_DOWN];
  417. ASYNC_PARAMS[m_input.DEVICE_TOUCH] = [m_input.TOUCH_START, m_input.TOUCH_MOVE,
  418. m_input.TOUCH_END];
  419. ASYNC_PARAMS[m_input.DEVICE_GYRO] = [m_input.GYRO_ORIENTATION_QUAT,
  420. m_input.GYRO_ORIENTATION_ANGLES];
  421. var CONF = {};
  422. CONF[m_input.DEVICE_HMD] = [m_input.HMD_DISTORTION,
  423. m_input.HMD_EYE_DISTANCE, m_input.HMD_BASELINE_DIST,
  424. m_input.HMD_SCREEN_LENS_DIST, m_input.HMD_SCREEN_WIDTH,
  425. m_input.HMD_SCREEN_HEIGHT, m_input.HMD_BEVEL_SIZE];
  426. /**
  427. * The callback for the mouse location corresponding to the
  428. * {@link module:input.MOUSE_LOCATION|MOUSE_LOCATION} param.
  429. * @callback MouseLocationCallback
  430. * @param {Float32Array} location List: the horizontal mouse coordinate, the
  431. * vertical mouse coordinate.
  432. * @example
  433. * var m_input = require("input");
  434. *
  435. * var mouse_location_cb = function(location) {
  436. * console.log("The horizontal location: " + location[0]);
  437. * console.log("The vertical location: " + location[1]);
  438. * };
  439. * var device = m_input.get_device_by_type_element(m_input.DEVICE_MOUSE);
  440. * m_input.attach_param_cb(device, m_input.MOUSE_LOCATION, mouse_location_cb);
  441. */
  442. /**
  443. * The callback for the pressed mouse button corresponding to the
  444. * {@link module:input.MOUSE_DOWN_WHICH|MOUSE_DOWN_WHICH} param.
  445. * @callback MouseDownWhichCallback
  446. * @param {number} which Number indicates button that was pressed on the mouse
  447. * (1 -- left button, 2 -- middle button, 3 -- right button).
  448. * @example
  449. * var m_input = require("input");
  450. *
  451. * var mouse_down_which_cb = function(which) {
  452. * console.log("Pressed button: " + which);
  453. * };
  454. * var device = m_input.get_device_by_type_element(m_input.DEVICE_MOUSE);
  455. * m_input.attach_param_cb(device, m_input.MOUSE_DOWN_WHICH, mouse_down_which_cb);
  456. */
  457. /**
  458. * The callback for the released mouse button corresponding to the
  459. * {@link module:input.MOUSE_UP_WHICH|MOUSE_UP_WHICH} param.
  460. * @callback MouseUpWhichCallback
  461. * @param {number} which Number indicates button that was released on the mouse
  462. * (1 -- left button, 2 -- middle button, 3 -- right button).
  463. * @example
  464. * var m_input = require("input");
  465. *
  466. * var mouse_up_which_cb = function(which) {
  467. * console.log("Released button: " + which);
  468. * };
  469. * var device = m_input.get_device_by_type_element(m_input.DEVICE_MOUSE);
  470. * m_input.attach_param_cb(device, m_input.MOUSE_UP_WHICH, mouse_up_which_cb);
  471. */
  472. /**
  473. * The callback for the mouse scroll corresponding to the
  474. * {@link module:input.MOUSE_WHEEL|MOUSE_WHEEL} param.
  475. * @callback MouseWheelCallback
  476. * @param {number} delta The vertical mouse scroll amount.
  477. * @example
  478. * var m_input = require("input");
  479. *
  480. * var mouse_wheel_cb = function(delta) {
  481. * console.log("Scroll amount: " + delta);
  482. * };
  483. * var device = m_input.get_device_by_type_element(m_input.DEVICE_MOUSE);
  484. * m_input.attach_param_cb(device, m_input.MOUSE_WHEEL, mouse_wheel_cb);
  485. */
  486. /**
  487. * The callback for the pressed keyboard button corresponding to the
  488. * {@link module:input.KEYBOARD_DOWN|KEYBOARD_DOWN} param.
  489. * @callback KeyboardDownCallback
  490. * @param {number} key_code Number indicates button that was pressed on the
  491. * keyboard (use constants KEY_* from module {@link module:controls|controls}).
  492. * @example
  493. * var m_input = require("input");
  494. *
  495. * var keyboard_down_cb = function(key_code) {
  496. * console.log("Pressed keyboard button: " + key_code);
  497. * };
  498. * var device = m_input.get_device_by_type_element(m_input.DEVICE_KEYBOARD);
  499. * m_input.attach_param_cb(device, m_input.KEYBOARD_DOWN, keyboard_down_cb);
  500. */
  501. /**
  502. * The callback for the released keyboard button corresponding to the
  503. * {@link module:input.KEYBOARD_UP|KEYBOARD_UP} param.
  504. * @callback KeyboardUpCallback
  505. * @param {number} key_code Number indicates button that was released on the
  506. * keyboard (use constants KEY_* from module {@link module:controls|controls}).
  507. * @example
  508. * var m_input = require("input");
  509. *
  510. * var keyboard_up_cb = function(key_code) {
  511. * console.log("Released keyboard button: " + key_code);
  512. * };
  513. * var device = m_input.get_device_by_type_element(m_input.DEVICE_KEYBOARD);
  514. * m_input.attach_param_cb(device, m_input.KEYBOARD_UP, keyboard_up_cb);
  515. */
  516. /**
  517. * The callback for the start of touch surface contacting corresponding to the
  518. * {@link module:input.TOUCH_START|TOUCH_START} param.
  519. * @callback TouchStartCallback
  520. * @param {Array} touches List of touch objects corresponding to the
  521. * contacting with the touch surface points.
  522. * @example
  523. * var m_input = require("input");
  524. *
  525. * var touch_start_cb = function(touches) {
  526. * for (var i = 0; i < touches.length; ++i)
  527. * console.log("Touch contact " + touches[i].identifier + " has position" +
  528. * + " x: " + touches[i].clientX + " y: " + touches[i].clientY);
  529. * };
  530. * var device = m_input.get_device_by_type_element(m_input.DEVICE_TOUCH);
  531. * m_input.attach_param_cb(device, m_input.TOUCH_START, touch_start_cb);
  532. */
  533. /**
  534. * The callback for the contact moving on the touch surface corresponding to the
  535. * {@link module:input.TOUCH_MOVE|TOUCH_MOVE} param.
  536. * @callback TouchMoveCallback
  537. * @param {Array} touches List of touch objects corresponding to the
  538. * contacting with the touch surface points.
  539. * @example
  540. * var m_input = require("input");
  541. *
  542. * var touch_move_cb = function(touches) {
  543. * for (var i = 0; i < touches.length; ++i)
  544. * console.log("Touch contact " + touches[i].identifier + " has position" +
  545. * + " x: " + touches[i].clientX + " y: " + touches[i].clientY);
  546. * };
  547. * var device = m_input.get_device_by_type_element(m_input.DEVICE_TOUCH);
  548. * m_input.attach_param_cb(device, m_input.TOUCH_MOVE, touch_move_cb);
  549. */
  550. /**
  551. * The callback for the end of touch surface contacting corresponding to the
  552. * {@link module:input.TOUCH_END|TOUCH_END} param.
  553. * @callback TouchEndCallback
  554. * @param {Array} touches List of touch objects corresponding to the
  555. * contacting with the touch surface points.
  556. * @example
  557. * var m_input = require("input");
  558. *
  559. * var touch_end_cb = function(touches) {
  560. * for (var i = 0; i < touches.length; ++i)
  561. * console.log("Touch contact " + touches[i].identifier + " has position" +
  562. * + " x: " + touches[i].clientX + " y: " + touches[i].clientY);
  563. * };
  564. * var device = m_input.get_device_by_type_element(m_input.DEVICE_TOUCH);
  565. * m_input.attach_param_cb(device, m_input.TOUCH_END, touch_end_cb);
  566. */
  567. /**
  568. * The callback for the quaternion of the gyroscope orientation corresponding
  569. * to the {@link module:input.GYRO_ORIENTATION_QUAT|GYRO_ORIENTATION_QUAT} param.
  570. * @callback GyroscopeQuatCallback
  571. * @param {Quat} quat Quaternion vector corresponding to the gyroscope orientation.
  572. * @example
  573. * var m_input = require("input");
  574. *
  575. * var gyroscope_quat_cb = function(quat) {
  576. * console.log("Gyroscope orientation quaternion is " + quat);
  577. * };
  578. * var device = m_input.get_device_by_type_element(m_input.DEVICE_GYRO);
  579. * m_input.attach_param_cb(device, m_input.GYRO_ORIENTATION_QUAT, gyroscope_quat_cb);
  580. */
  581. /**
  582. * The callback for the Euler angles of the gyroscope orientation corresponding
  583. * to the {@link module:input.GYRO_ORIENTATION_ANGLES|GYRO_ORIENTATION_ANGLES} param.
  584. * @callback GyroscopeAnglesCallback
  585. * @param {Euler} angles Euler angles (in radians) corresponding to the gyroscope
  586. * orientation.
  587. * @example
  588. * var m_input = require("input");
  589. *
  590. * var gyroscope_angles_cb = function(angles) {
  591. * console.log("Gyroscope orientation Euler angles are " + angles);
  592. * };
  593. * var device = m_input.get_device_by_type_element(m_input.DEVICE_GYRO);
  594. * m_input.attach_param_cb(device, m_input.GYRO_ORIENTATION_ANGLES, gyroscope_angles_cb);
  595. */
  596. /**
  597. * Device type enum.
  598. * @typedef {number} DeviceType
  599. */
  600. /**
  601. * Gyroscope device type.
  602. * @const {DeviceType} module:input.DEVICE_GYRO
  603. */
  604. exports.DEVICE_GYRO = m_input.DEVICE_GYRO;
  605. /**
  606. * Head mounted device type.
  607. * @const {DeviceType} module:input.DEVICE_HMD
  608. */
  609. exports.DEVICE_HMD = m_input.DEVICE_HMD;
  610. /**
  611. * Mouse device type.
  612. * @const {DeviceType} module:input.DEVICE_MOUSE
  613. */
  614. exports.DEVICE_MOUSE = m_input.DEVICE_MOUSE;
  615. /**
  616. * Keyboard device type.
  617. * @const {DeviceType} module:input.DEVICE_KEYBOARD
  618. */
  619. exports.DEVICE_KEYBOARD = m_input.DEVICE_KEYBOARD;
  620. /**
  621. * Touch device type.
  622. * @const {DeviceType} module:input.DEVICE_TOUCH
  623. */
  624. exports.DEVICE_TOUCH = m_input.DEVICE_TOUCH;
  625. /**
  626. * Check if the device can be used.
  627. * @method module:input.can_use_device
  628. * @param {DeviceType} type Device type.
  629. * @returns {boolean} Result of the check
  630. */
  631. exports.can_use_device = m_input.can_use_device;
  632. /**
  633. * Get device object by associated device type and DOM element.
  634. * If device is not available, then return null.
  635. * @method module:input.get_device_by_type_element
  636. * @param {DeviceType} type Device type.
  637. * @param {?HTMLElement} element HTML element to add event listeners to.
  638. * @returns {?Object} Device object.
  639. */
  640. exports.get_device_by_type_element = m_input.get_device_by_type_element;
  641. /**
  642. * Switch triggering of the browser default actions for registered events.
  643. * @param {Object} device Device object. Use
  644. * {@link module:input.get_device_by_type_element|input.get_device_by_type_element} to obtain it.
  645. * @param {boolean} prevent_default Prevent default flag.
  646. * @method module:input.switch_prevent_default
  647. */
  648. exports.switch_prevent_default = function(device, prevent_default) {
  649. if (device)
  650. m_input.switch_prevent_default(device, prevent_default);
  651. }
  652. /**
  653. * Register device. Right now it should be used for DEVICE_HMD.
  654. * @method module:input.register_device
  655. * @deprecated Not needed anymore.
  656. */
  657. exports.register_device = function() {
  658. m_print.error_once("input.register_device() deprecated");
  659. }
  660. /**
  661. * Reset device. The device parameters values return to zero.
  662. * Right now it should be used for DEVICE_HMD.
  663. * @param {Object} device Device object. Use
  664. * {@link module:input.get_device_by_type_element|input.get_device_by_type_element} to obtain it.
  665. * @method module:input.reset_device
  666. */
  667. exports.reset_device = function(device) {
  668. if (!device || device.type != m_input.DEVICE_HMD) {
  669. m_print.error("reset_device is undefined for device.");
  670. return;
  671. }
  672. m_input.reset_device(device);
  673. }
  674. /**
  675. * Get parameter vector.
  676. * @param {Object} device Device object. Use
  677. * {@link module:input.get_device_by_type_element|input.get_device_by_type_element} to obtain it.
  678. * @param {DeviceVectorParameterSync} param Name of the device vector parameter.
  679. * @param {Float32Array} dest Destination vector.
  680. * @returns {?Float32Array} Destination vector.
  681. * @method module:input.get_vector_param
  682. */
  683. exports.get_vector_param = function(device, param, dest) {
  684. if (device && device.type in SYNC_VECTOR_PARAMS &&
  685. SYNC_VECTOR_PARAMS[device.type].indexOf(param) >= 0)
  686. return m_input.get_vector_param(device, param, dest);
  687. else
  688. m_print.error("device hasn't param: ", param);
  689. return null;
  690. }
  691. /**
  692. * Get parameter value.
  693. * @param {Object} device Device object. Use
  694. * {@link module:input.get_device_by_type_element|input.get_device_by_type_element} to obtain it.
  695. * @param {DeviceValueParameterSync} param Name of the device value parameter.
  696. * @returns {number|boolean|null} Parameter value.
  697. * @method module:input.get_value_param
  698. */
  699. exports.get_value_param = function(device, param) {
  700. if (device && device.type in SYNC_VALUE_PARAMS &&
  701. SYNC_VALUE_PARAMS[device.type].indexOf(param) >= 0)
  702. return m_input.get_value_param(device, param);
  703. else
  704. m_print.error("device hasn't param: ", param);
  705. return null;
  706. }
  707. /**
  708. * Attach callback to the device parameter. It is called when parameter is changed.
  709. * @param {Object} device Device object. Use
  710. * {@link module:input.get_device_by_type_element|input.get_device_by_type_element} to obtain it.
  711. * @param {DeviceParameterAsync} param Name of the device parameter.
  712. * @param {MouseLocationCallback|MouseDownWhichCallback|MouseUpWhichCallback|
  713. * MouseWheelCallback|KeyboardDownCallback|KeyboardUpCallback|
  714. * TouchStartCallback|TouchMoveCallback|TouchEndCallback|
  715. * GyroscopeQuatCallback|GyroscopeAnglesCallback} [cb = null] Callback.
  716. * @method module:input.attach_param_cb
  717. */
  718. exports.attach_param_cb = function(device, param, cb) {
  719. cb = cb || null;
  720. if (device && device.type in ASYNC_PARAMS &&
  721. ASYNC_PARAMS[device.type].indexOf(param) >= 0)
  722. return m_input.attach_param_cb(device, param, cb);
  723. else
  724. m_print.error("device hasn't param: ", param);
  725. }
  726. /**
  727. * Detach callback from the device parameter.
  728. * @param {Object} device Device object. Use
  729. * {@link module:input.get_device_by_type_element|input.get_device_by_type_element} to obtain it.
  730. * @param {DeviceParameterAsync} param Name of the device parameter.
  731. * @param {MouseLocationCallback|MouseDownWhichCallback|MouseUpWhichCallback|
  732. * MouseWheelCallback|KeyboardDownCallback|KeyboardUpCallback|
  733. * TouchStartCallback|TouchMoveCallback|TouchEndCallback|
  734. * GyroscopeQuatCallback|GyroscopeAnglesCallback} [cb = null] Callback.
  735. * @method module:input.detach_param_cb
  736. */
  737. exports.detach_param_cb = function(device, param, cb) {
  738. cb = cb || null;
  739. if (device && device.type in ASYNC_PARAMS &&
  740. ASYNC_PARAMS[device.type].indexOf(param) >= 0)
  741. return m_input.detach_param_cb(device, param, cb);
  742. else
  743. m_print.error("device hasn't param: ", param);
  744. }
  745. exports.set_config = function(device, config, value) {
  746. if (device && device.type in CONF &&
  747. CONF[device.type].indexOf(config) >= 0)
  748. return m_input.set_config(device, config, value);
  749. else
  750. m_print.error("device hasn't config: ", config);
  751. }
  752. /**
  753. * Request fullscreen mode.
  754. * @method module:input.request_fullscreen_hmd
  755. * @deprecated Use {@link module:screen.request_fullscreen_hmd} instead
  756. */
  757. exports.request_fullscreen_hmd = function() {
  758. m_print.error_deprecated("request_fullscreen_hmd", "screen.request_fullscreen_hmd");
  759. if (m_scs.check_active()) {
  760. var cam_obj = m_scs.get_camera(m_scs.get_active());
  761. if (!cam_obj)
  762. return;
  763. } else
  764. return;
  765. enable_split_screen(cam_obj);
  766. var hmd_device = m_input.get_device_by_type_element(m_input.DEVICE_HMD);
  767. if (hmd_device) {
  768. var webvr_display = hmd_device.webvr_display;
  769. // TODO: add "&& !webvr_display.isPresenting" to "if" (optimization).
  770. // Right now webvr_display.isPresenting is allways "true" after
  771. // first requestPresent.
  772. if (webvr_display && !webvr_display.isPresenting) {
  773. var capabilities = webvr_display.capabilities;
  774. if (!capabilities.canPresent)
  775. m_print.error("HMD fullscreen request failed.");
  776. else {
  777. var canvas = m_cont.get_canvas();
  778. webvr_display.requestPresent([{source: canvas}]).then(function () {
  779. // TODO: check code below
  780. // There was strange behavior Samsung Internet on GearVR
  781. if (!cfg_def.is_mobile_device) {
  782. var left_eye = webvr_display.getEyeParameters("left");
  783. var right_eye = webvr_display.getEyeParameters("right");
  784. m_cont.resize(
  785. Math.max(left_eye.renderWidth, right_eye.renderWidth) * 2,
  786. Math.max(left_eye.renderHeight, right_eye.renderHeight), false);
  787. }
  788. }, function () {
  789. m_print.error("HMD fullscreen request failed.");
  790. });
  791. }
  792. }
  793. }
  794. }
  795. /**
  796. * Enable "split screen" mode.
  797. * @method module:input.enable_split_screen
  798. * @param {Object3D} camobj Camera 3D-object.
  799. * @returns {boolean} "Split screen" mode is enabled.
  800. * @deprecated Use {@link module:screen.request_split_screen} instead
  801. */
  802. exports.enable_split_screen = enable_split_screen;
  803. function enable_split_screen(camobj) {
  804. m_print.error_deprecated("enable_split_screen", "screen.request_split_screen");
  805. var hmd_device = m_input.get_device_by_type_element(m_input.DEVICE_HMD);
  806. if (!hmd_device)
  807. return false;
  808. if (_splited_screen)
  809. return true;
  810. var hmd_left_fov = m_input.get_vector_param(hmd_device, m_input.HMD_FOV_LEFT, _vec4_tmp);
  811. var hmd_right_fov = m_input.get_vector_param(hmd_device, m_input.HMD_FOV_RIGHT, _vec4_tmp2);
  812. m_cam.set_hmd_fov(camobj, hmd_left_fov, hmd_right_fov);
  813. var eye_distance = m_input.get_value_param(hmd_device, HMD_EYE_DISTANCE);
  814. if (eye_distance) {
  815. var active_scene = m_scs.get_active();
  816. var cam_scene_data = m_obj_util.get_scene_data(camobj, active_scene);
  817. var cameras = cam_scene_data.cameras;
  818. m_cam.set_eye_distance(cameras, eye_distance);
  819. }
  820. var hmd_params = {};
  821. hmd_params.base_line_factor = 0.5;
  822. hmd_params.inter_lens_factor = 0.5;
  823. hmd_params.enable_hmd_stereo = true;
  824. var hmd_type = m_input.get_value_param(hmd_device, m_input.HMD_WEBVR_TYPE)
  825. if (hmd_type & (m_input.HMD_NON_WEBVR | m_input.HMD_WEBVR_MOBILE |
  826. m_input.HMD_WEBVR_DESKTOP)) {
  827. hmd_params.distortion_coefs = [
  828. hmd_device.distortion_coefs[0],
  829. hmd_device.distortion_coefs[1]
  830. ];
  831. hmd_params.chromatic_aberration_coefs = [
  832. hmd_device.chromatic_aberration_coefs[0],
  833. hmd_device.chromatic_aberration_coefs[1],
  834. hmd_device.chromatic_aberration_coefs[2],
  835. hmd_device.chromatic_aberration_coefs[3]
  836. ];
  837. if (!hmd_device.webvr_hmd_device)
  838. if (hmd_device.base_line_dist && hmd_device.height_dist && hmd_device.bevel_size)
  839. hmd_params.base_line_factor = (hmd_device.base_line_dist - hmd_device.bevel_size) /
  840. hmd_device.height_dist;
  841. else if (!hmd_device.bevel_size)
  842. hmd_params.base_line_factor = hmd_device.base_line_dist / hmd_device.height_dist;
  843. if (hmd_device.inter_lens_dist && hmd_device.width_dist && !hmd_device.webvr_hmd_device)
  844. hmd_params.inter_lens_factor = hmd_device.inter_lens_dist /
  845. hmd_device.width_dist;
  846. // NOTE: prevent crash in case of difference of slink's dimensions.
  847. // For example: "debug" slink ~ 0.5 X 1.0,
  848. // "origin" slink ~ distortion_scale * 0.5 X distortion_scale * 1.0
  849. if (!cfg_dbg.enabled) {
  850. var distortion_scale = (1 + hmd_device.distortion_coefs[0] + hmd_device.distortion_coefs[1]);
  851. m_scs.multiply_size_mult(distortion_scale, distortion_scale);
  852. }
  853. }
  854. m_scs.set_hmd_params(hmd_params);
  855. m_cont.resize_to_container(true);
  856. m_input.reset_device(hmd_device);
  857. _splited_screen = true;
  858. return true;
  859. }
  860. /**
  861. * Disable "split screen" mode.
  862. * @method module:input.disable_split_screen
  863. * @returns {boolean} "Split screen" mode is disabled.
  864. * @deprecated Use {@link module:screen.exit_split_screen} instead
  865. */
  866. exports.disable_split_screen = function() {
  867. m_print.error_deprecated("disable_split_screen", "screen.exit_split_screen");
  868. var hmd_device = m_input.get_device_by_type_element(m_input.DEVICE_HMD)
  869. if (!_splited_screen || !hmd_device || !hmd_device.registered)
  870. return false;
  871. // set up non-vr mode
  872. var hmd_params = {};
  873. hmd_params.enable_hmd_stereo = false;
  874. m_scs.set_hmd_params(hmd_params);
  875. var distortion_scale = 1 / (1 + hmd_device.distortion_coefs[0] + hmd_device.distortion_coefs[1]);
  876. m_scs.multiply_size_mult(distortion_scale, distortion_scale);
  877. // resize screen to canvas resolution (non-vr mode)
  878. m_cont.resize_to_container(true);
  879. return true;
  880. }
  881. /**
  882. * Set gamepad button key value.
  883. * @method module:input.set_gamepad_key
  884. * @param {number} gamepad_id Connected gamepad number.
  885. * @param {number} btn Button or axis identifier.
  886. * @param {number} key Button key value.
  887. */
  888. exports.set_gamepad_key = function(gamepad_id, btn, key) {
  889. switch(gamepad_id) {
  890. case 0:
  891. var type = m_input.DEVICE_GAMEPAD0;
  892. break;
  893. case 1:
  894. var type = m_input.DEVICE_GAMEPAD1;
  895. break;
  896. case 2:
  897. var type = m_input.DEVICE_GAMEPAD2;
  898. break;
  899. case 3:
  900. var type = m_input.DEVICE_GAMEPAD3;
  901. break;
  902. default:
  903. var type = m_input.DEVICE_GAMEPAD0;
  904. }
  905. var device = m_input.get_device_by_type_element(type);
  906. m_input.set_config(device, btn, key);
  907. };
  908. /**
  909. * Get pressed button key value.
  910. * @method module:input.get_pressed_gmpd_btn
  911. * @param {number} gamepad_id Gamepad identifier (Connected device number 0-3).
  912. * @returns {number} Pressed button key value.
  913. */
  914. exports.get_pressed_gmpd_btn = m_input.get_pressed_gmpd_btn;
  915. /**
  916. * Get moved axis key value.
  917. * @method module:input.get_moved_gmpd_axis
  918. * @param {number} gamepad_id Gamepad identifier (Connected device number 0-3).
  919. * @returns {number} Moved axis key value.
  920. */
  921. exports.get_moved_gmpd_axis = m_input.get_moved_gmpd_axis;
  922. /**
  923. * Get gamepad position.
  924. * @method module:input.get_gamepad_position
  925. * @param {number} gamepad_id Gamepad identifier (Connected device number 0-3).
  926. * @param {Vec3} dest Destination vector.
  927. * @returns {Vec3} Gamepad position.
  928. */
  929. exports.get_gamepad_position = m_input.get_gamepad_position;
  930. /**
  931. * Get gamepad orientation.
  932. * @method module:input.get_gamepad_orientation
  933. * @param {number} gamepad_id Gamepad identifier (Connected device number 0-3).
  934. * @param {Quat} dest Destination vector.
  935. * @returns {Quat} Gamepad orientation.
  936. */
  937. exports.get_gamepad_orientation = m_input.get_gamepad_orientation;
  938. /**
  939. * Check available gamepads indices.
  940. * @method module:input.check_enable_gamepad_indices
  941. * @returns {Array} Numeric array with indices
  942. * @cc_externs getGamepads webkitGetGamepads
  943. */
  944. exports.check_enable_gamepad_indices = check_enable_gamepad_indices;
  945. function check_enable_gamepad_indices() {
  946. var gamepads = navigator.getGamepads ? navigator.getGamepads() :
  947. (navigator.webkitGetGamepads ? navigator.webkitGetGamepads : []);
  948. var indices = [];
  949. for (var i = 0; i < gamepads.length; i++)
  950. if (gamepads[i])
  951. indices.push(i);
  952. return indices;
  953. }
  954. /**
  955. * Get gamepad id of VR controller.
  956. * @method module:input.get_vr_controller_id
  957. * @param {number} controller_id VR controller id.
  958. * @return {number} Gamepad id.
  959. * @example
  960. * var m_input = require("input");
  961. *
  962. * // get the id of the first VR controller (among all gamepads)
  963. * var vr_id = m_input.get_vr_controller_id(0);
  964. */
  965. exports.get_vr_controller_id = function(controller_id) {
  966. var gamepads = navigator.getGamepads ? navigator.getGamepads() :
  967. (navigator.webkitGetGamepads ? navigator.webkitGetGamepads : []);
  968. // CHECK: there could be more than 4 gamepads in Firefox Nightly
  969. for (var i = 0, j = 0; i < 4; i++) {
  970. var gamepad = gamepads[i];
  971. // use gamepad.id == "", bcz there is a bug in Firefox Nightly
  972. if (gamepad && (gamepad.id == "" ||
  973. gamepad.id == "OpenVR Gamepad" ||
  974. gamepad.id == "Oculus Touch (Left)" ||
  975. gamepad.id == "Oculus Touch (Right)"
  976. )) {
  977. if (j == controller_id)
  978. return j;
  979. j++;
  980. }
  981. }
  982. return controller_id;
  983. }
  984. /**
  985. * Register the listener on the click event. Callback is called on right
  986. * after click on element (without 300ms delay).
  987. * @method module:input.add_click_listener
  988. * @param {HTMLElement} element DOM element.
  989. * @param {Callback} callback Callback.
  990. */
  991. exports.add_click_listener = function(element, callback) {
  992. if (!element || !callback)
  993. return;
  994. m_input.add_click_listener(element, callback);
  995. }
  996. /**
  997. * Unregister the listener registered with
  998. * {@link module:input.add_click_listener | input.add_click_listener}.
  999. * @method module:input.remove_click_listener
  1000. * @param {HTMLElement} element DOM element.
  1001. * @param {Callback} callback Callback.
  1002. */
  1003. exports.remove_click_listener = function(element, callback) {
  1004. if (!element || !callback)
  1005. return;
  1006. m_input.remove_click_listener(element, callback);
  1007. }
  1008. }
  1009. var input_factory = register("input", Input);
  1010. export default input_factory;