Source: extern/controls.js

  1. import register from "../util/register.js";
  2. import m_ctl_fact from "../intern/controls.js";
  3. import m_phy_fact from "../intern/physics.js";
  4. import m_print_fact from "../intern/print.js";
  5. /**
  6. * Implements the event-driven model of Blend4Web.
  7. *
  8. * <dl>
  9. * <dt>Sensor</dt>
  10. *
  11. * <dd>Sensor is a programming entity intended for detecting events occurred in
  12. * the scene. Some sensors may carry a payload. For example the
  13. * ray-tracing sensor (Ray Sensor)
  14. * provides the relative length of the intersection ray.</dd>
  15. *
  16. * <dt>Sensor Manifold (or simply manifold)</dt>
  17. *
  18. * <dd>Sensors should be present in one or multiple collections - so called
  19. * sensor manifolds. A manifold is a logic container associated with a scene
  20. * object. It generates a response (pulse) to a defined set of sensor events by
  21. * executing a callback function.</dd>
  22. * </dl>
  23. *
  24. * <p>In order to use the input-output sensors, register the corresponding event
  25. * listeners by using register_* methods.</p>
  26. *
  27. * @see https://www.blend4web.com/doc/en/developers.html#event-model
  28. *
  29. * @module controls
  30. * @local ManifoldCallback
  31. * @local ManifoldLogicFunction
  32. * @local CollisionPayload
  33. * @local RayPayload
  34. * @local SensorCallback
  35. */
  36. function Controls(ns, exports) {
  37. var m_ctl = m_ctl_fact(ns);
  38. var m_phy = m_phy_fact(ns);
  39. var m_print = m_print_fact(ns);
  40. /**
  41. * Manifold's callback. It is executed when the manifold generates a pulse.
  42. * @callback ManifoldCallback
  43. * @param {?Object3D} obj Object 3D, or null to denote the global object
  44. * @param {string} id Manifold ID
  45. * @param {number} pulse Additional callback condition for CT_TRIGGER or
  46. * CT_CONTINUOUS manifolds: +1 or -1
  47. * @param {*} [param] Callback parameter. The user-defined parameter which is
  48. * passed to create_sensor_manifold(). Can be used, for example, as a storage
  49. * object to communicate between different manifolds.
  50. */
  51. /**
  52. * Manifold's logic function. Specifies a logic expression which consists of
  53. * sensor values. This logic expression will be evaluated every frame. As a
  54. * result, the manifold changes its internal state and fires the callback.
  55. * @callback ManifoldLogicFunction
  56. * @param {Array} s Numeric array with sensor values.
  57. * @returns {number} Result of evaluation of the logic expression
  58. */
  59. /**
  60. * Collision sensor payload.
  61. * @callback CollisionPayload
  62. * @param {?Object3D} coll_obj The target collision object, i.e the object
  63. * the source object collides with (null for no collision or when this object
  64. * is represented by collision material).
  65. * @param {?Vec3} coll_pos Position of collision point.
  66. * @param {?Vec3} coll_norm Normal of collision point.
  67. * @param {?number} coll_dist Distance between collision points of colliding
  68. * objects.
  69. * @cc_externs coll_obj coll_pos coll_norm coll_dist
  70. */
  71. /**
  72. * Ray sensor payload.
  73. * @callback RayPayload
  74. * @param {number} hit_fract Fraction of ray length where hit has occurred (0-1)
  75. * or -1 if there is no hit anymore.
  76. * @param {?Object3D} obj_hit The hit object.
  77. * @param {number} hit_time Time the hit happened.
  78. * @param {Vec3} hit_pos Hit position in world space.
  79. * @param {Vec3} hit_norm Hit normal in world space.
  80. * @cc_externs hit_fract obj_hit hit_time hit_pos hit_norm
  81. */
  82. /**
  83. * Special callback for callback-sensor. It's executed every frame and
  84. * its return value is copied into the sensor value. Should return a numeric value.
  85. * @callback SensorCallback
  86. */
  87. /**
  88. * Manifold control type: positive.
  89. * Such manifold executes the callback each frame when the result of
  90. * evaluation of its logic function is positive.
  91. * @const module:controls.CT_POSITIVE
  92. */
  93. exports.CT_POSITIVE = m_ctl.CT_POSITIVE;
  94. /**
  95. * Manifold control type: continuous.
  96. * Such manifold executes the callback with a positive pulse (+1) each frame
  97. * when the result of evaluation of its logic function is non-zero.
  98. * It executes a callback with a negative pulse (-1) once the logic function
  99. * evaluates to zero.
  100. * @const module:controls.CT_CONTINUOUS
  101. */
  102. exports.CT_CONTINUOUS = m_ctl.CT_CONTINUOUS;
  103. /**
  104. * Manifold control type: trigger.
  105. * Such manifold executes the callback with a single positive pulse (+1) once the result of
  106. * evaluation of its logic function is non-zero.
  107. * It executes a callback with a single negative pulse (-1) once the logic function
  108. * evaluates to zero.
  109. * @const module:controls.CT_TRIGGER
  110. */
  111. exports.CT_TRIGGER = m_ctl.CT_TRIGGER;
  112. /**
  113. * Manifold control type: shot.
  114. * Such manifold executes the callback once the result of evaluation of its
  115. * logic function becomes a non-zero value.
  116. * @const module:controls.CT_SHOT
  117. */
  118. exports.CT_SHOT = m_ctl.CT_SHOT;
  119. /**
  120. * Manifold control type: level.
  121. * Such manifold executes the callback each time the result of
  122. * evaluation of its logic function is changed.
  123. * @const module:controls.CT_LEVEL
  124. */
  125. exports.CT_LEVEL = m_ctl.CT_LEVEL;
  126. /**
  127. * Manifold control type: change.
  128. * Such manifold executes the callback each time the value
  129. * of any sensor is changed. The logic function is ignored.
  130. * @const module:controls.CT_CHANGE
  131. */
  132. exports.CT_CHANGE = m_ctl.CT_CHANGE;
  133. /**
  134. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  135. * @const module:controls.KEY_BACKSPACE
  136. */
  137. exports.KEY_BACKSPACE = 8;
  138. /**
  139. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  140. * @const module:controls.KEY_TAB
  141. */
  142. exports.KEY_TAB = 9;
  143. /**
  144. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  145. * @const module:controls.KEY_ENTER
  146. */
  147. exports.KEY_ENTER = 13;
  148. /**
  149. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  150. * @const module:controls.KEY_SHIFT
  151. */
  152. exports.KEY_SHIFT = 16;
  153. /**
  154. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  155. * @const module:controls.KEY_CTRL
  156. */
  157. exports.KEY_CTRL = 17;
  158. /**
  159. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  160. * @const module:controls.KEY_ALT
  161. */
  162. exports.KEY_ALT = 18;
  163. /**
  164. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  165. * @const module:controls.KEY_PAUSE
  166. */
  167. exports.KEY_PAUSE = 19;
  168. /**
  169. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  170. * @const module:controls.KEY_CAPSLOCK
  171. */
  172. exports.KEY_CAPSLOCK = 20;
  173. /**
  174. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  175. * @const module:controls.KEY_ESC
  176. */
  177. exports.KEY_ESC = 27;
  178. /**
  179. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  180. * @const module:controls.KEY_SPACE
  181. */
  182. exports.KEY_SPACE = 32;
  183. /**
  184. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  185. * @const module:controls.KEY_LEFT
  186. */
  187. exports.KEY_LEFT = 37;
  188. /**
  189. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  190. * @const module:controls.KEY_UP
  191. */
  192. exports.KEY_UP = 38;
  193. /**
  194. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  195. * @const module:controls.KEY_RIGHT
  196. */
  197. exports.KEY_RIGHT = 39;
  198. /**
  199. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  200. * @const module:controls.KEY_DOWN
  201. */
  202. exports.KEY_DOWN = 40;
  203. /**
  204. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  205. * @const module:controls.KEY_1
  206. */
  207. exports.KEY_1 = 49;
  208. /**
  209. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  210. * @const module:controls.KEY_2
  211. */
  212. exports.KEY_2 = 50;
  213. /**
  214. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  215. * @const module:controls.KEY_3
  216. */
  217. exports.KEY_3 = 51;
  218. /**
  219. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  220. * @const module:controls.KEY_4
  221. */
  222. exports.KEY_4 = 52;
  223. /**
  224. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  225. * @const module:controls.KEY_5
  226. */
  227. exports.KEY_5 = 53;
  228. /**
  229. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  230. * @const module:controls.KEY_6
  231. */
  232. exports.KEY_6 = 54;
  233. /**
  234. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  235. * @const module:controls.KEY_7
  236. */
  237. exports.KEY_7 = 55;
  238. /**
  239. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  240. * @const module:controls.KEY_8
  241. */
  242. exports.KEY_8 = 56;
  243. /**
  244. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  245. * @const module:controls.KEY_9
  246. */
  247. exports.KEY_9 = 57;
  248. /**
  249. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  250. * @const module:controls.KEY_A
  251. */
  252. exports.KEY_A = 65;
  253. /**
  254. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  255. * @const module:controls.KEY_B
  256. */
  257. exports.KEY_B = 66;
  258. /**
  259. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  260. * @const module:controls.KEY_C
  261. */
  262. exports.KEY_C = 67;
  263. /**
  264. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  265. * @const module:controls.KEY_D
  266. */
  267. exports.KEY_D = 68;
  268. /**
  269. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  270. * @const module:controls.KEY_E
  271. */
  272. exports.KEY_E = 69;
  273. /**
  274. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  275. * @const module:controls.KEY_F
  276. */
  277. exports.KEY_F = 70;
  278. /**
  279. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  280. * @const module:controls.KEY_G
  281. */
  282. exports.KEY_G = 71;
  283. /**
  284. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  285. * @const module:controls.KEY_H
  286. */
  287. exports.KEY_H = 72;
  288. /**
  289. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  290. * @const module:controls.KEY_I
  291. */
  292. exports.KEY_I = 73;
  293. /**
  294. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  295. * @const module:controls.KEY_J
  296. */
  297. exports.KEY_J = 74;
  298. /**
  299. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  300. * @const module:controls.KEY_K
  301. */
  302. exports.KEY_K = 75;
  303. /**
  304. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  305. * @const module:controls.KEY_L
  306. */
  307. exports.KEY_L = 76;
  308. /**
  309. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  310. * @const module:controls.KEY_M
  311. */
  312. exports.KEY_M = 77;
  313. /**
  314. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  315. * @const module:controls.KEY_N
  316. */
  317. exports.KEY_N = 78;
  318. /**
  319. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  320. * @const module:controls.KEY_O
  321. */
  322. exports.KEY_O = 79;
  323. /**
  324. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  325. * @const module:controls.KEY_P
  326. */
  327. exports.KEY_P = 80;
  328. /**
  329. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  330. * @const module:controls.KEY_Q
  331. */
  332. exports.KEY_Q = 81;
  333. /**
  334. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  335. * @const module:controls.KEY_R
  336. */
  337. exports.KEY_R = 82;
  338. /**
  339. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  340. * @const module:controls.KEY_S
  341. */
  342. exports.KEY_S = 83;
  343. /**
  344. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  345. * @const module:controls.KEY_T
  346. */
  347. exports.KEY_T = 84;
  348. /**
  349. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  350. * @const module:controls.KEY_U
  351. */
  352. exports.KEY_U = 85;
  353. /**
  354. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  355. * @const module:controls.KEY_V
  356. */
  357. exports.KEY_V = 86;
  358. /**
  359. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  360. * @const module:controls.KEY_W
  361. */
  362. exports.KEY_W = 87;
  363. /**
  364. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  365. * @const module:controls.KEY_X
  366. */
  367. exports.KEY_X = 88;
  368. /**
  369. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  370. * @const module:controls.KEY_Y
  371. */
  372. exports.KEY_Y = 89;
  373. /**
  374. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  375. * @const module:controls.KEY_Z
  376. */
  377. exports.KEY_Z = 90;
  378. /**
  379. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  380. * @const module:controls.KEY_NUM0
  381. */
  382. exports.KEY_NUM0 = 96;
  383. /**
  384. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  385. * @const module:controls.KEY_NUM1
  386. */
  387. exports.KEY_NUM1 = 97;
  388. /**
  389. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  390. * @const module:controls.KEY_NUM2
  391. */
  392. exports.KEY_NUM2 = 98;
  393. /**
  394. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  395. * @const module:controls.KEY_NUM3
  396. */
  397. exports.KEY_NUM3 = 99;
  398. /**
  399. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  400. * @const module:controls.KEY_NUM4
  401. */
  402. exports.KEY_NUM4 = 100;
  403. /**
  404. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  405. * @const module:controls.KEY_NUM5
  406. */
  407. exports.KEY_NUM5 = 101;
  408. /**
  409. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  410. * @const module:controls.KEY_NUM6
  411. */
  412. exports.KEY_NUM6 = 102;
  413. /**
  414. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  415. * @const module:controls.KEY_NUM7
  416. */
  417. exports.KEY_NUM7 = 103;
  418. /**
  419. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  420. * @const module:controls.KEY_NUM8
  421. */
  422. exports.KEY_NUM8 = 104;
  423. /**
  424. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  425. * @const module:controls.KEY_NUM9
  426. */
  427. exports.KEY_NUM9 = 105;
  428. /**
  429. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  430. * @const module:controls.KEY_NUM9
  431. */
  432. exports.KEY_MULT = 106;
  433. /**
  434. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  435. * @const module:controls.KEY_NUM9
  436. */
  437. exports.KEY_ADD = 107;
  438. /**
  439. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  440. * @const module:controls.KEY_NUM9
  441. */
  442. exports.KEY_SUB = 109;
  443. /**
  444. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  445. * Can only be used when NUM LOCK is turned on.
  446. * @const module:controls.KEY_DEC_POINT
  447. */
  448. exports.KEY_DEC_POINT = 110;
  449. /**
  450. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  451. * @const module:controls.KEY_NUM9
  452. */
  453. exports.KEY_DIV = 111;
  454. /**
  455. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  456. * @const module:controls.KEY_SEMI_COLON
  457. */
  458. exports.KEY_SEMI_COLON = 186;
  459. /**
  460. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  461. * @const module:controls.KEY_EQUAL_SIGN
  462. */
  463. exports.KEY_EQUAL_SIGN = 187;
  464. /**
  465. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  466. * @const module:controls.KEY_COMMA
  467. */
  468. exports.KEY_COMMA = 188;
  469. /**
  470. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  471. * @const module:controls.KEY_DASH
  472. */
  473. exports.KEY_DASH = 189;
  474. /**
  475. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  476. * @const module:controls.KEY_PERIOD
  477. */
  478. exports.KEY_PERIOD = 190;
  479. /**
  480. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  481. * @const module:controls.KEY_FORWARD_SLASH
  482. */
  483. exports.KEY_FORWARD_SLASH = 191;
  484. /**
  485. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  486. * @const module:controls.KEY_GRAVE_ACCENT
  487. */
  488. exports.KEY_GRAVE_ACCENT = 192;
  489. /**
  490. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  491. * @const module:controls.KEY_LEFT_SQ_BRACKET
  492. */
  493. exports.KEY_LEFT_SQ_BRACKET = 219;
  494. /**
  495. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  496. * @const module:controls.KEY_BACK_SLASH
  497. */
  498. exports.KEY_BACK_SLASH = 220;
  499. /**
  500. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  501. * @const module:controls.KEY_RIGHT_SQ_BRACKET
  502. */
  503. exports.KEY_RIGHT_SQ_BRACKET = 221;
  504. /**
  505. * Keyboard sensor parameter. Corresponds to keyCode property of KeyboardEvent.
  506. * @const module:controls.KEY_SINGLE_QUOTE
  507. */
  508. exports.KEY_SINGLE_QUOTE = 222;
  509. /**
  510. * Payload value of a touch movement sensor. Returned by get_sensor_payload()
  511. * for single-finger pan gestures.
  512. * @const module:controls.PL_SINGLE_TOUCH_MOVE
  513. */
  514. exports.PL_SINGLE_TOUCH_MOVE = m_ctl.PL_SINGLE_TOUCH_MOVE;
  515. /**
  516. * Payload value of a touch movement sensor. Returned by get_sensor_payload()
  517. * for multi-finger zoom gestures.
  518. * @const module:controls.PL_MULTITOUCH_MOVE_ZOOM
  519. */
  520. exports.PL_MULTITOUCH_MOVE_ZOOM = m_ctl.PL_MULTITOUCH_MOVE_ZOOM;
  521. /**
  522. * Payload value of a touch movement sensor. Returned by get_sensor_payload()
  523. * for multi-finger pan gestures.
  524. * @const module:controls.PL_MULTITOUCH_MOVE_PAN
  525. */
  526. exports.PL_MULTITOUCH_MOVE_PAN = m_ctl.PL_MULTITOUCH_MOVE_PAN;
  527. /**
  528. * Payload value of a touch movement sensor. Returned by get_sensor_payload()
  529. * for multi-finger rotate gestures.
  530. * @const module:controls.PL_MULTITOUCH_MOVE_ROTATE
  531. */
  532. exports.PL_MULTITOUCH_MOVE_ROTATE = m_ctl.PL_MULTITOUCH_MOVE_ROTATE;
  533. /**
  534. * Default logic AND function for sensor manifold
  535. * @const module:controls.default_AND_logic_fun
  536. */
  537. exports.default_AND_logic_fun = m_ctl.default_AND_logic_fun;
  538. /**
  539. * Default logic OR function for sensor manifold
  540. * @const module:controls.default_OR_logic_fun
  541. */
  542. exports.default_OR_logic_fun = m_ctl.default_OR_logic_fun;
  543. /**
  544. * Create a gamepad button sensor.
  545. * @method module:controls.create_gamepad_btn_sensor
  546. * @param {number} ind Button number
  547. * @param {number} [number] Connected gamepad number
  548. * @returns {Sensor} Sensor object
  549. */
  550. exports.create_gamepad_btn_sensor = m_ctl.create_gamepad_btn_sensor;
  551. /**
  552. * Create a gamepad an axis sensor.
  553. * @method module:controls.create_gamepad_axis_sensor
  554. * @param {number} axis Axis number
  555. * @param {number} [number] Connected gamepad number
  556. * @returns {Sensor} Sensor object
  557. */
  558. exports.create_gamepad_axis_sensor = m_ctl.create_gamepad_axis_sensor;
  559. /**
  560. * Create a gamepad position sensor.
  561. * @method module:controls.create_gamepad_position_sensor
  562. * @param {number} [number] Connected gamepad number
  563. * @returns {Sensor} Sensor object
  564. */
  565. exports.create_gamepad_position_sensor = m_ctl.create_gamepad_position_sensor;
  566. /**
  567. * Create a gamepad orientation sensor.
  568. * @method module:controls.create_gamepad_orientation_sensor
  569. * @param {number} [number] Connected gamepad number
  570. * @returns {Sensor} Sensor object
  571. */
  572. exports.create_gamepad_orientation_sensor = m_ctl.create_gamepad_orientation_sensor;
  573. /**
  574. * Create a custom sensor.
  575. * A custom sensor can be controlled manually by using the get_custom_sensor()
  576. * and set_custom_sensor() methods.
  577. * @method module:controls.create_custom_sensor
  578. * @param {number} value Initial custom sensor value
  579. * @returns {Sensor} Sensor object
  580. */
  581. exports.create_custom_sensor = m_ctl.create_custom_sensor;
  582. /**
  583. * Create a keyboard sensor.
  584. * This sensor carries the following payload values:
  585. * 0 --- button wasn't pressed at the last frame, and it wasn't pressed at the current frame,
  586. * 1 --- button wasn't pressed at the last frame, but it was pressed at the current frame,
  587. * 2 --- button was pressed at the last frame, and it was pressed at the current frame,
  588. * 3 --- button was pressed at the last frame, and it wasn't pressed at the current frame.
  589. * @method module:controls.create_keyboard_sensor
  590. * @param {number} key Sensor key KEY_*
  591. * @param {HTMLElement} [element=Canvas container element] HTML element
  592. * @returns {Sensor} Sensor object
  593. */
  594. exports.create_keyboard_sensor = m_ctl.create_keyboard_sensor;
  595. /**
  596. * Create a collision sensor.
  597. * Detects collisions between the object and the entities (objects or physics
  598. * materials) with the specified collision ID. If the collision ID is not
  599. * specified, the sensor will detect collisions with any entities.
  600. * This sensor carries the following {@link CollisionPayload|payload}.
  601. * @method module:controls.create_collision_sensor
  602. * @param {Object3D} obj_src Collision object.
  603. * @param {?string} [collision_id="ANY"] Collision ID, "ANY" for any collision ID
  604. * @param {boolean} [calc_pos_norm=false] Should the sensor return the
  605. * collision position/normal/distance or not.
  606. * @returns {Sensor} Sensor object
  607. */
  608. exports.create_collision_sensor = function(obj_src, collision_id, calc_pos_norm) {
  609. collision_id = collision_id || "ANY";
  610. calc_pos_norm = calc_pos_norm || false;
  611. return m_ctl.create_collision_sensor(obj_src, collision_id, calc_pos_norm);
  612. }
  613. /**
  614. * Create a collision impulse sensor.
  615. * It is intended to obtain the value of the impulse (that is, mass multiplied
  616. * by velocity) applied to the object at the collision point.
  617. * @method module:controls.create_collision_impulse_sensor
  618. * @param {Object3D} obj Collision object.
  619. * @returns {Sensor} Sensor object
  620. */
  621. exports.create_collision_impulse_sensor = m_ctl.create_collision_impulse_sensor;
  622. /**
  623. * Create a ray sensor.
  624. * The sensor casts a ray between the from and to positions.
  625. * These positions are specified relatively to the object's origin
  626. * (ign_src_rot = true), in the world space (obj_src = null) or in the local
  627. * space (ign_src_rot = false).
  628. * Checks intersection of this ray with the specified collision ID. If the
  629. * collision ID is not specified, the sensor will detect collisions with any
  630. * entities.
  631. * This sensor carries the following {@link RayPayload|payload}.
  632. * @method module:controls.create_ray_sensor
  633. * @param {?Object3D} obj_src Source object, pass a non-null value to perform
  634. * ray casting in object space, e.g. from/to vectors specified in object space.
  635. * @param {Vec3} from From vector.
  636. * @param {Vec3} to To vector.
  637. * @param {?string} [collision_id="ANY"] Collision ID, "ANY" for any collision ID
  638. * @param {boolean} [is_binary_value=false] Calculate the value of the sensor as
  639. * a binary (hit/non-hit) instead of hit fraction.
  640. * @param {boolean} [calc_pos_norm=false] Calculate hit position/normal
  641. * (accessed from payload object).
  642. * @param {boolean} [ign_src_rot=false] Ignore any rotation of the source object
  643. * during ray casting.
  644. * @returns {Sensor} Sensor object.
  645. */
  646. exports.create_ray_sensor = function(obj_src, from, to, collision_id,
  647. is_binary_value, calc_pos_norm, ign_src_rot) {
  648. if (obj_src && !m_phy.obj_has_physics(obj_src)) {
  649. m_print.error_once("No physics for object " + obj_src.name);
  650. return;
  651. }
  652. collision_id = collision_id || "ANY";
  653. is_binary_value = is_binary_value || false;
  654. calc_pos_norm = calc_pos_norm || false;
  655. ign_src_rot = ign_src_rot || false;
  656. return m_ctl.create_ray_sensor(obj_src, from, to, collision_id, is_binary_value,
  657. calc_pos_norm, ign_src_rot);
  658. }
  659. /**
  660. * Create a mouse click sensor.
  661. * The sensor's payload is an object
  662. * with useful properties like coordinates, and index of button
  663. * @method module:controls.create_mouse_click_sensor
  664. * @param {HTMLElement} [element=Canvas container element] HTML element
  665. * @returns {Sensor} Sensor object
  666. * @cc_externs coords which
  667. */
  668. exports.create_mouse_click_sensor = m_ctl.create_mouse_click_sensor;
  669. /**
  670. * Create a mouse wheel sensor.
  671. * The sensor's value is 1 for a single wheel notch scrolled away from the user.
  672. * @method module:controls.create_mouse_wheel_sensor
  673. * @param {HTMLElement} [element=Canvas container element] HTML element
  674. * @returns {Sensor} Sensor object
  675. */
  676. exports.create_mouse_wheel_sensor = m_ctl.create_mouse_wheel_sensor;
  677. /**
  678. * Create a mouse movement sensor.
  679. * The sensor's value is a number of pixels, the sensor's payload is an object
  680. * with useful properties like coordinates
  681. * @method module:controls.create_mouse_move_sensor
  682. * @param {string} [axis="XY"] Coordinate(s) to track: "X", "Y", "XY"
  683. * @param {HTMLElement} [element] HTML element. The canvas container will be use by default.
  684. * @returns {Sensor} Sensor object
  685. * @cc_externs coords
  686. */
  687. exports.create_mouse_move_sensor = m_ctl.create_mouse_move_sensor;
  688. /**
  689. * Create a mouse move pointerlock sensor.
  690. * The sensor's value is a number of pixels, the sensor's payload is an object
  691. * with useful properties like coordinates
  692. * @method module:controls.create_plock_mouse_sensor
  693. * @param {HTMLElement} [element=Canvas container element] HTML element
  694. * @returns {Sensor} Sensor object
  695. * @cc_externs coords
  696. */
  697. exports.create_plock_mouse_sensor = m_ctl.create_plock_mouse_sensor;
  698. /**
  699. * Create a mouse pointerlock sensor.
  700. * The sensor's value is a pointerlock state
  701. * @method module:controls.create_plock_sensor
  702. * @param {HTMLElement} [element=Canvas container element] HTML element
  703. * @returns {Sensor} Sensor object
  704. */
  705. exports.create_plock_sensor = m_ctl.create_plock_sensor;
  706. /**
  707. * Create a touch movement sensor.
  708. * The sensor's value is a number of pixels, the sensor's payload is an object
  709. * with useful properties like coordinates
  710. * @method module:controls.create_touch_move_sensor
  711. * @param {string} [axis="XY"] Coordinate(s) to track: "X", "Y" or "XY"
  712. * @param {HTMLElement} [element] HTML element. The canvas container will be use by default.
  713. * @returns {Sensor} Sensor object
  714. * @cc_externs coords gesture
  715. */
  716. exports.create_touch_move_sensor = m_ctl.create_touch_move_sensor;
  717. /**
  718. * Create a touch zoom sensor.
  719. * The sensor's value is the distance difference in pixels.
  720. * @method module:controls.create_touch_zoom_sensor
  721. * @param {HTMLElement} [element=Canvas container element] HTML element
  722. * @returns {Sensor} Sensor object
  723. */
  724. exports.create_touch_zoom_sensor = m_ctl.create_touch_zoom_sensor;
  725. /**
  726. * Create a touch rotate sensor.
  727. * The sensor's value is the angle (in radians) from -PI to PI.
  728. * @method module:controls.create_touch_rotate_sensor
  729. * @param {HTMLElement} [element=Canvas container element] HTML element
  730. * @returns {Sensor} Sensor object
  731. */
  732. exports.create_touch_rotate_sensor = m_ctl.create_touch_rotate_sensor;
  733. /**
  734. * Create a touch click sensor.
  735. * The sensor's value is 1 for a touched touchscreen. The sensor's payload is an object
  736. * with useful properties like coordinates
  737. * @method module:controls.create_touch_click_sensor
  738. * @param {HTMLElement} [element=Canvas container element] HTML element
  739. * @returns {Sensor} Sensor object
  740. * @cc_externs coords
  741. */
  742. exports.create_touch_click_sensor = m_ctl.create_touch_click_sensor;
  743. /**
  744. * Create a motion sensor.
  745. * The sensor's value is 1 if the object is in motion.
  746. * @method module:controls.create_motion_sensor
  747. * @param {Object3D} obj Object 3D
  748. * @param {number} [threshold=0.1] Translation velocity threshold,
  749. * units (meters) per second
  750. * @param {number} [rotation_threshold=0.1] Rotation velocity threshold,
  751. * radians per second
  752. * @returns {Sensor} Sensor object
  753. */
  754. exports.create_motion_sensor = m_ctl.create_motion_sensor;
  755. /**
  756. * Create a velocity sensor.
  757. * The sensor's value is 1 if abs() of the object's vertical velocity exceeds
  758. * the threshold.
  759. * @method module:controls.create_vertical_velocity_sensor
  760. * @param {Object3D} obj Object 3D
  761. * @param {number} [threshold=1.0] Vertical velocity threshold,
  762. * units (meters) per second
  763. * @returns {Sensor} Sensor object
  764. */
  765. exports.create_vertical_velocity_sensor = m_ctl.create_vertical_velocity_sensor;
  766. /**
  767. * Create a gyroscope angle sensor.
  768. * The sensor's payload stores the Euler angles of orientation (in radians)
  769. * of a mobile device.
  770. * @method module:controls.create_gyro_angles_sensor
  771. * @returns {Sensor} Sensor object
  772. */
  773. exports.create_gyro_angles_sensor = m_ctl.create_gyro_angles_sensor;
  774. /**
  775. * Create a gyroscope quaternion sensor.
  776. * The sensor's payload stores the quaternion of orientation
  777. * of a mobile device.
  778. * @method module:controls.create_gyro_quat_sensor
  779. * @returns {Sensor} Sensor object
  780. */
  781. exports.create_gyro_quat_sensor = m_ctl.create_gyro_quat_sensor;
  782. /**
  783. * Create a gyroscope delta sensor.
  784. * The sensor's payload stores the differences (in radians) between Euler angles
  785. * of the current orientation and the previous orientation of a mobile device.
  786. * @method module:controls.create_gyro_delta_sensor
  787. * @returns {Sensor} Sensor object
  788. */
  789. exports.create_gyro_delta_sensor = m_ctl.create_gyro_delta_sensor;
  790. /**
  791. * Create a HMD quaternion sensor.
  792. * The sensor's payload stores the quaternion of orientation of a HMD.
  793. * @method module:controls.create_hmd_quat_sensor
  794. * @returns {Sensor} Sensor object
  795. */
  796. exports.create_hmd_quat_sensor = m_ctl.create_hmd_quat_sensor;
  797. /**
  798. * Create a HMD position sensor.
  799. * The sensor's payload stores the vector of HMD position.
  800. * @method module:controls.create_hmd_position_sensor
  801. * @returns {Sensor} Sensor object
  802. */
  803. exports.create_hmd_position_sensor = m_ctl.create_hmd_position_sensor;
  804. /**
  805. * Create a timer sensor.
  806. * The sensor's value becomes 1 for the frame which comes next after the
  807. * period of time has elapsed. After that, its value becomes 0 again.
  808. * The timer's precision depends on FPS, so it is not effective for measuring
  809. * short intervals.
  810. * @method module:controls.create_timer_sensor
  811. * @param {number} period Timer period, in seconds
  812. * @param {boolean} [do_repeat=false] Re-start the timer upon expiration
  813. * @returns {Sensor} Sensor object
  814. */
  815. exports.create_timer_sensor = function(period, do_repeat) {
  816. return m_ctl.create_timer_sensor(period, do_repeat || false);
  817. }
  818. /**
  819. * Reset the timer sensor and set a new period value.
  820. * @method module:controls.reset_timer_sensor
  821. * @param {Object3D} obj Object 3D
  822. * @param {string} manifold_id Object's manifold ID
  823. * @param {number} num Sensor's number in the manifold
  824. * @param {number} period A new period value for the sensor
  825. */
  826. exports.reset_timer_sensor = m_ctl.reset_timer_sensor;
  827. /**
  828. * Create an elapsed sensor.
  829. * The sensor's value is the time elapsed from the previous frame.
  830. * @method module:controls.create_elapsed_sensor
  831. * @returns {Sensor} Sensor object
  832. */
  833. exports.create_elapsed_sensor = m_ctl.create_elapsed_sensor;
  834. /**
  835. * Create a timeline sensor.
  836. * The sensor's value is the value of the global engine timeline.
  837. * @method module:controls.create_timeline_sensor
  838. * @returns {Sensor} Sensor object
  839. */
  840. exports.create_timeline_sensor = m_ctl.create_timeline_sensor;
  841. /**
  842. * Create a selection sensor for the object.
  843. * The sensor's value becomes 1 when the object is selected by the user.
  844. * @param {Object3D} obj Object 3D
  845. * @param {boolean} [enable_toggle_switch=false] If true, reset the sensor
  846. * (set it to 0) only when another object is selected. If false, reset the
  847. * sensor when the mouse button/touch is released.
  848. * @method module:controls.create_selection_sensor
  849. * @returns {Sensor} Sensor object
  850. */
  851. exports.create_selection_sensor = function(obj, enable_toggle_switch) {
  852. return m_ctl.create_selection_sensor(obj, enable_toggle_switch || false);
  853. }
  854. /**
  855. * Create a callback sensor.
  856. * The given callback is executed every frame and its return value is copied into the sensor value.
  857. * @param {SensorCallback} callback A callback which modifies sensor value.
  858. * @param {number} [value=0] Initial sensor value.
  859. * @method module:controls.create_callback_sensor
  860. * @returns {Sensor} Sensor object
  861. */
  862. exports.create_callback_sensor = function(callback, value) {
  863. return m_ctl.create_callback_sensor(callback, value || 0);
  864. }
  865. /**
  866. * Set the value of the custom sensor.
  867. * @method module:controls.set_custom_sensor
  868. * @param {Sensor} sensor Sensor object
  869. * @param {number} value New sensor value
  870. */
  871. exports.set_custom_sensor = function(sensor, value) {
  872. m_ctl.sensor_set_value(sensor, value);
  873. }
  874. /**
  875. * Get the value of the custom sensor.
  876. * @method module:controls.get_custom_sensor
  877. * @param {Sensor} sensor Sensor object
  878. * @returns {number} Sensor value
  879. */
  880. exports.get_custom_sensor = function(sensor) {
  881. return sensor.value;
  882. }
  883. /**
  884. * Return the value of the sensor from the object's manifold. The sensor is
  885. * identified by its index in the manifold's array of sensors.
  886. * @method module:controls.get_sensor_value
  887. * @param {?Object3D} obj Object 3D, or null to denote the global object
  888. * @param {string} manifold_id Object's manifold ID
  889. * @param {number} num Sensor index in manifold's array
  890. * @returns {number} Sensor value
  891. */
  892. exports.get_sensor_value = m_ctl.get_sensor_value;
  893. /**
  894. * Return the payload data of the sensor from the object's manifold. The sensor
  895. * is identified by its index in the manifold's array of sensors.
  896. * @method module:controls.get_sensor_payload
  897. * @param {?Object3D} obj Object 3D, or null to denote the global object
  898. * @param {string} manifold_id Object's manifold ID
  899. * @param {number} num Sensor index in manifold's array
  900. * @returns {*} Sensor payload
  901. */
  902. exports.get_sensor_payload = m_ctl.get_sensor_payload;
  903. /**
  904. * Create a sensor manifold.
  905. * @method module:controls.create_sensor_manifold
  906. * @param {?Object3D} obj Object 3D to attach the manifold to, or null to denote
  907. * the global object.
  908. * @param {string} id New manifold ID.
  909. * @param {number} type Manifold control type (CT_SHOT, CT_TRIGGER etc).
  910. * @param {Sensor[]} sensors Array of sensors.
  911. * @param {?ManifoldLogicFunction} logic_fun Manifold's logic function, if null
  912. * use default_AND_logic_fun function.
  913. * @param {ManifoldCallback} callback Manifold's callback.
  914. * @param {*} [callback_param] Parameter to pass to the manifold's callback
  915. * (e.g. some state).
  916. */
  917. exports.create_sensor_manifold = function(obj, id, type, sensors,
  918. logic_fun, callback, callback_param) {
  919. callback_param = callback_param === undefined? null: callback_param;
  920. logic_fun = logic_fun || m_ctl.default_AND_logic_fun;
  921. m_ctl.create_sensor_manifold(obj, id, type, sensors, logic_fun, callback,
  922. callback_param);
  923. }
  924. /**
  925. * Convenience function: creates a manifold coupled with a single keyboard
  926. * sensor. Can be used to quickly create a single-key functionality.
  927. * @method module:controls.create_kb_sensor_manifold
  928. * @param {?Object3D} obj Object 3D to attach the manifold to, or null to denote
  929. * the global object
  930. * @param {string} id New manifold ID
  931. * @param {number} type Manifold control type (CT_SHOT, CT_TRIGGER etc)
  932. * @param {number} key Sensor key KEY_*
  933. * @param {ManifoldCallback} callback Manifold's callback
  934. * @param {*} [callback_param] Parameter to pass to the manifold's callback
  935. * (e.g. some state)
  936. */
  937. exports.create_kb_sensor_manifold = function(obj, id, type, key,
  938. callback, callback_param) {
  939. var kb_sensor = m_ctl.create_keyboard_sensor(key);
  940. callback_param = callback_param === undefined? null: callback_param;
  941. var logic_fun = m_ctl.default_AND_logic_fun;
  942. m_ctl.create_sensor_manifold(obj, id, type, [kb_sensor], logic_fun,
  943. callback, callback_param);
  944. }
  945. /**
  946. * Check whether the object has any manifolds attached.
  947. * @method module:controls.check_sensor_manifolds
  948. * @param {?Object3D} obj Object 3D, or null to denote the global object
  949. * @returns {boolean} Result of the check
  950. */
  951. exports.check_sensor_manifolds = function(obj) {
  952. return m_ctl.check_sensor_manifold(obj, null);
  953. }
  954. /**
  955. * Check whether the object has the manifold attached.
  956. * @method module:controls.check_sensor_manifold
  957. * @param {?Object3D} obj Object 3D, or null to denote the global object
  958. * @param {string} id Manifold ID
  959. * @returns {boolean} Result of the check
  960. */
  961. exports.check_sensor_manifold = m_ctl.check_sensor_manifold;
  962. /**
  963. * Remove the sensor manifold registered for the object.
  964. * @method module:controls.remove_sensor_manifold
  965. * @param {?Object3D} obj Object 3D to delete the manifold from, or null to denote
  966. * the global object
  967. * @param {string} [id=null] ID of the sensor manifold, or null to delete all
  968. * manifolds
  969. */
  970. exports.remove_sensor_manifold = m_ctl.remove_sensor_manifold;
  971. /**
  972. * Reset controls for all the objects.
  973. * Calling this method is discouraged, use remove_sensor_manifold() instead.
  974. * @method module:controls.reset
  975. */
  976. exports.reset = m_ctl.reset;
  977. /**
  978. * Set smooth factor for mouse pointerlock sensor.
  979. * @method module:controls.set_plock_smooth_factor
  980. * @param {number} value Set
  981. */
  982. exports.set_plock_smooth_factor = m_ctl.set_plock_smooth_factor;
  983. }
  984. var controls_factory = register("controls", Controls);
  985. export default controls_factory;