/home/lindsay/xeolabs/xeogl-next/xeogl/src/input/input.js
API Docs for:

File: /home/lindsay/xeolabs/xeogl-next/xeogl/src/input/input.js

  1. /**
  2. Publishes keyboard and mouse events that occur on the parent {{#crossLink "Scene"}}{{/crossLink}}'s {{#crossLink "Canvas"}}{{/crossLink}}.
  3.  
  4. * Each {{#crossLink "Scene"}}{{/crossLink}} provides an Input on itself as a read-only property.
  5.  
  6. ## Usage
  7.  
  8. In this example, we're subscribing to some mouse and key events that will occur on
  9. a {{#crossLink "Scene"}}Scene's{{/crossLink}} {{#crossLink "Canvas"}}Canvas{{/crossLink}}.
  10.  
  11. ````javascript
  12. var myScene = new xeogl.Scene();
  13.  
  14. var input = myScene.input;
  15.  
  16. // We'll save a handle to this subscription
  17. // to show how to unsubscribe, further down
  18. var handle = input.on("mousedown", function(coords) {
  19. console.log("Mouse down at: x=" + coords[0] + ", y=" + coords[1]);
  20. });
  21.  
  22. input.on("mouseup", function(coords) {
  23. console.log("Mouse up at: x=" + coords[0] + ", y=" + coords[1]);
  24. });
  25.  
  26. input.on("mouseclicked", function(coords) {
  27. console.log("Mouse clicked at: x=" + coords[0] + ", y=" + coords[1]);
  28. });
  29.  
  30. input.on("dblclick", function(coords) {
  31. console.log("Double-click at: x=" + coords[0] + ", y=" + coords[1]);
  32. });
  33.  
  34. input.on("keydown", function(keyCode) {
  35. switch (keyCode) {
  36.  
  37. case this.KEY_A:
  38. console.log("The 'A' key is down");
  39. break;
  40.  
  41. case this.KEY_B:
  42. console.log("The 'B' key is down");
  43. break;
  44.  
  45. case this.KEY_C:
  46. console.log("The 'C' key is down");
  47. break;
  48.  
  49. default:
  50. console.log("Some other key is down");
  51. }
  52. });
  53.  
  54. input.on("keyup", function(keyCode) {
  55. switch (keyCode) {
  56.  
  57. case this.KEY_A:
  58. console.log("The 'A' key is up");
  59. break;
  60.  
  61. case this.KEY_B:
  62. console.log("The 'B' key is up");
  63. break;
  64.  
  65. case this.KEY_C:
  66. console.log("The 'C' key is up");
  67. break;
  68.  
  69. default:
  70. console.log("Some other key is up");
  71. }
  72. });
  73.  
  74. // TODO: ALT and CTRL keys etc
  75. ````
  76.  
  77. ### Unsubscribing from Events
  78.  
  79. In the snippet above, we saved a handle to one of our event subscriptions.
  80.  
  81. We can then use that handle to unsubscribe again, like this:
  82.  
  83. ````javascript
  84. input.off(handle);
  85. ````
  86.  
  87. @class Input
  88. @module xeogl
  89. @submodule input
  90. @extends Component
  91. */
  92. import {core} from "./../core.js";
  93. import {Component} from '../component.js';
  94. import {math} from '../math/math.js';
  95.  
  96. const type = "xeogl.Input";
  97.  
  98. class Input extends Component {
  99.  
  100. /**
  101. JavaScript class name for this Component.
  102.  
  103. For example: "xeogl.AmbientLight", "xeogl.MetallicMaterial" etc.
  104.  
  105. @property type
  106. @type String
  107. @final
  108. */
  109. get type() {
  110. return type;
  111. }
  112.  
  113. init(cfg) {
  114.  
  115. super.init(cfg);
  116.  
  117. const self = this;
  118.  
  119. // Key codes
  120.  
  121. /**
  122. * Code for the BACKSPACE key.
  123. * @property KEY_BACKSPACE
  124. * @final
  125. * @type Number
  126. */
  127. // Key codes
  128.  
  129. /**
  130. * Code for the BACKSPACE key.
  131. * @property KEY_BACKSPACE
  132. * @final
  133. * @type Number
  134. */
  135. this.KEY_BACKSPACE = 8;
  136.  
  137. /**
  138. * Code for the TAB key.
  139. * @property KEY_TAB
  140. * @final
  141. * @type Number
  142. */
  143. this.KEY_TAB = 9;
  144.  
  145. /**
  146. * Code for the ENTER key.
  147. * @property KEY_ENTER
  148. * @final
  149. * @type Number
  150. */
  151. this.KEY_ENTER = 13;
  152.  
  153. /**
  154. * Code for the SHIFT key.
  155. * @property KEY_SHIFT
  156. * @final
  157. * @type Number
  158. */
  159. this.KEY_SHIFT = 16;
  160.  
  161. /**
  162. * Code for the CTRL key.
  163. * @property KEY_CTRL
  164. * @final
  165. * @type Number
  166. */
  167. this.KEY_CTRL = 17;
  168.  
  169. /**
  170. * Code for the ALT key.
  171. * @property KEY_ALT
  172. * @final
  173. * @type Number
  174. */
  175. this.KEY_ALT = 18;
  176.  
  177. /**
  178. * Code for the PAUSE_BREAK key.
  179. * @property KEY_PAUSE_BREAK
  180. * @final
  181. * @type Number
  182. */
  183. this.KEY_PAUSE_BREAK = 19;
  184.  
  185. /**
  186. * Code for the CAPS_LOCK key.
  187. * @property KEY_CAPS_LOCK
  188. * @final
  189. * @type Number
  190. */
  191. this.KEY_CAPS_LOCK = 20;
  192.  
  193. /**
  194. * Code for the ESCAPE key.
  195. * @property KEY_ESCAPE
  196. * @final
  197. * @type Number
  198. */
  199. this.KEY_ESCAPE = 27;
  200.  
  201. /**
  202. * Code for the PAGE_UP key.
  203. * @property KEY_PAGE_UP
  204. * @final
  205. * @type Number
  206. */
  207. this.KEY_PAGE_UP = 33;
  208.  
  209. /**
  210. * Code for the PAGE_DOWN key.
  211. * @property KEY_PAGE_DOWN
  212. * @final
  213. * @type Number
  214. */
  215. this.KEY_PAGE_DOWN = 34;
  216.  
  217. /**
  218. * Code for the END key.
  219. * @property KEY_END
  220. * @final
  221. * @type Number
  222. */
  223. this.KEY_END = 35;
  224.  
  225. /**
  226. * Code for the HOME key.
  227. * @property KEY_HOME
  228. * @final
  229. * @type Number
  230. */
  231. this.KEY_HOME = 36;
  232.  
  233. /**
  234. * Code for the LEFT_ARROW key.
  235. * @property KEY_LEFT_ARROW
  236. * @final
  237. * @type Number
  238. */
  239. this.KEY_LEFT_ARROW = 37;
  240.  
  241. /**
  242. * Code for the UP_ARROW key.
  243. * @property KEY_UP_ARROW
  244. * @final
  245. * @type Number
  246. */
  247. this.KEY_UP_ARROW = 38;
  248.  
  249. /**
  250. * Code for the RIGHT_ARROW key.
  251. * @property KEY_RIGHT_ARROW
  252. * @final
  253. * @type Number
  254. */
  255. this.KEY_RIGHT_ARROW = 39;
  256.  
  257. /**
  258. * Code for the DOWN_ARROW key.
  259. * @property KEY_DOWN_ARROW
  260. * @final
  261. * @type Number
  262. */
  263. this.KEY_DOWN_ARROW = 40;
  264.  
  265. /**
  266. * Code for the INSERT key.
  267. * @property KEY_INSERT
  268. * @final
  269. * @type Number
  270. */
  271. this.KEY_INSERT = 45;
  272.  
  273. /**
  274. * Code for the DELETE key.
  275. * @property KEY_DELETE
  276. * @final
  277. * @type Number
  278. */
  279. this.KEY_DELETE = 46;
  280.  
  281. /**
  282. * Code for the 0 key.
  283. * @property KEY_NUM_0
  284. * @final
  285. * @type Number
  286. */
  287. this.KEY_NUM_0 = 48;
  288.  
  289. /**
  290. * Code for the 1 key.
  291. * @property KEY_NUM_1
  292. * @final
  293. * @type Number
  294. */
  295. this.KEY_NUM_1 = 49;
  296.  
  297. /**
  298. * Code for the 2 key.
  299. * @property KEY_NUM_2
  300. * @final
  301. * @type Number
  302. */
  303. this.KEY_NUM_2 = 50;
  304.  
  305. /**
  306. * Code for the 3 key.
  307. * @property KEY_NUM_3
  308. * @final
  309. * @type Number
  310. */
  311. this.KEY_NUM_3 = 51;
  312.  
  313. /**
  314. * Code for the 4 key.
  315. * @property KEY_NUM_4
  316. * @final
  317. * @type Number
  318. */
  319. this.KEY_NUM_4 = 52;
  320.  
  321. /**
  322. * Code for the 5 key.
  323. * @property KEY_NUM_5
  324. * @final
  325. * @type Number
  326. */
  327. this.KEY_NUM_5 = 53;
  328.  
  329. /**
  330. * Code for the 6 key.
  331. * @property KEY_NUM_6
  332. * @final
  333. * @type Number
  334. */
  335. this.KEY_NUM_6 = 54;
  336.  
  337. /**
  338. * Code for the 7 key.
  339. * @property KEY_NUM_7
  340. * @final
  341. * @type Number
  342. */
  343. this.KEY_NUM_7 = 55;
  344.  
  345. /**
  346. * Code for the 8 key.
  347. * @property KEY_NUM_8
  348. * @final
  349. * @type Number
  350. */
  351. this.KEY_NUM_8 = 56;
  352.  
  353. /**
  354. * Code for the 9 key.
  355. * @property KEY_NUM_9
  356. * @final
  357. * @type Number
  358. */
  359. this.KEY_NUM_9 = 57;
  360.  
  361. /**
  362. * Code for the A key.
  363. * @property KEY_A
  364. * @final
  365. * @type Number
  366. */
  367. this.KEY_A = 65;
  368.  
  369. /**
  370. * Code for the B key.
  371. * @property KEY_B
  372. * @final
  373. * @type Number
  374. */
  375. this.KEY_B = 66;
  376.  
  377. /**
  378. * Code for the C key.
  379. * @property KEY_C
  380. * @final
  381. * @type Number
  382. */
  383. this.KEY_C = 67;
  384.  
  385. /**
  386. * Code for the D key.
  387. * @property KEY_D
  388. * @final
  389. * @type Number
  390. */
  391. this.KEY_D = 68;
  392.  
  393. /**
  394. * Code for the E key.
  395. * @property KEY_E
  396. * @final
  397. * @type Number
  398. */
  399. this.KEY_E = 69;
  400.  
  401. /**
  402. * Code for the F key.
  403. * @property KEY_F
  404. * @final
  405. * @type Number
  406. */
  407. this.KEY_F = 70;
  408.  
  409. /**
  410. * Code for the G key.
  411. * @property KEY_G
  412. * @final
  413. * @type Number
  414. */
  415. this.KEY_G = 71;
  416.  
  417. /**
  418. * Code for the H key.
  419. * @property KEY_H
  420. * @final
  421. * @type Number
  422. */
  423. this.KEY_H = 72;
  424.  
  425. /**
  426. * Code for the I key.
  427. * @property KEY_I
  428. * @final
  429. * @type Number
  430. */
  431. this.KEY_I = 73;
  432.  
  433. /**
  434. * Code for the J key.
  435. * @property KEY_J
  436. * @final
  437. * @type Number
  438. */
  439. this.KEY_J = 74;
  440.  
  441. /**
  442. * Code for the K key.
  443. * @property KEY_K
  444. * @final
  445. * @type Number
  446. */
  447. this.KEY_K = 75;
  448.  
  449. /**
  450. * Code for the L key.
  451. * @property KEY_L
  452. * @final
  453. * @type Number
  454. */
  455. this.KEY_L = 76;
  456.  
  457. /**
  458. * Code for the M key.
  459. * @property KEY_M
  460. * @final
  461. * @type Number
  462. */
  463. this.KEY_M = 77;
  464.  
  465. /**
  466. * Code for the N key.
  467. * @property KEY_N
  468. * @final
  469. * @type Number
  470. */
  471. this.KEY_N = 78;
  472.  
  473. /**
  474. * Code for the O key.
  475. * @property KEY_O
  476. * @final
  477. * @type Number
  478. */
  479. this.KEY_O = 79;
  480.  
  481. /**
  482. * Code for the P key.
  483. * @property KEY_P
  484. * @final
  485. * @type Number
  486. */
  487. this.KEY_P = 80;
  488.  
  489. /**
  490. * Code for the Q key.
  491. * @property KEY_Q
  492. * @final
  493. * @type Number
  494. */
  495. this.KEY_Q = 81;
  496.  
  497. /**
  498. * Code for the R key.
  499. * @property KEY_R
  500. * @final
  501. * @type Number
  502. */
  503. this.KEY_R = 82;
  504.  
  505. /**
  506. * Code for the S key.
  507. * @property KEY_S
  508. * @final
  509. * @type Number
  510. */
  511. this.KEY_S = 83;
  512.  
  513. /**
  514. * Code for the T key.
  515. * @property KEY_T
  516. * @final
  517. * @type Number
  518. */
  519. this.KEY_T = 84;
  520.  
  521. /**
  522. * Code for the U key.
  523. * @property KEY_U
  524. * @final
  525. * @type Number
  526. */
  527. this.KEY_U = 85;
  528.  
  529. /**
  530. * Code for the V key.
  531. * @property KEY_V
  532. * @final
  533. * @type Number
  534. */
  535. this.KEY_V = 86;
  536.  
  537. /**
  538. * Code for the W key.
  539. * @property KEY_W
  540. * @final
  541. * @type Number
  542. */
  543. this.KEY_W = 87;
  544.  
  545. /**
  546. * Code for the X key.
  547. * @property KEY_X
  548. * @final
  549. * @type Number
  550. */
  551. this.KEY_X = 88;
  552.  
  553. /**
  554. * Code for the Y key.
  555. * @property KEY_Y
  556. * @final
  557. * @type Number
  558. */
  559. this.KEY_Y = 89;
  560.  
  561. /**
  562. * Code for the Z key.
  563. * @property KEY_Z
  564. * @final
  565. * @type Number
  566. */
  567. this.KEY_Z = 90;
  568.  
  569. /**
  570. * Code for the LEFT_WINDOW key.
  571. * @property KEY_LEFT_WINDOW
  572. * @final
  573. * @type Number
  574. */
  575. this.KEY_LEFT_WINDOW = 91;
  576.  
  577. /**
  578. * Code for the RIGHT_WINDOW key.
  579. * @property KEY_RIGHT_WINDOW
  580. * @final
  581. * @type Number
  582. */
  583. this.KEY_RIGHT_WINDOW = 92;
  584.  
  585. /**
  586. * Code for the SELECT key.
  587. * @property KEY_SELECT
  588. * @final
  589. * @type Number
  590. */
  591. this.KEY_SELECT_KEY = 93;
  592.  
  593. /**
  594. * Code for the number pad 0 key.
  595. * @property KEY_NUMPAD_0
  596. * @final
  597. * @type Number
  598. */
  599. this.KEY_NUMPAD_0 = 96;
  600.  
  601. /**
  602. * Code for the number pad 1 key.
  603. * @property KEY_NUMPAD_1
  604. * @final
  605. * @type Number
  606. */
  607. this.KEY_NUMPAD_1 = 97;
  608.  
  609. /**
  610. * Code for the number pad 2 key.
  611. * @property KEY_NUMPAD 2
  612. * @final
  613. * @type Number
  614. */
  615. this.KEY_NUMPAD_2 = 98;
  616.  
  617. /**
  618. * Code for the number pad 3 key.
  619. * @property KEY_NUMPAD_3
  620. * @final
  621. * @type Number
  622. */
  623. this.KEY_NUMPAD_3 = 99;
  624.  
  625. /**
  626. * Code for the number pad 4 key.
  627. * @property KEY_NUMPAD_4
  628. * @final
  629. * @type Number
  630. */
  631. this.KEY_NUMPAD_4 = 100;
  632.  
  633. /**
  634. * Code for the number pad 5 key.
  635. * @property KEY_NUMPAD_5
  636. * @final
  637. * @type Number
  638. */
  639. this.KEY_NUMPAD_5 = 101;
  640.  
  641. /**
  642. * Code for the number pad 6 key.
  643. * @property KEY_NUMPAD_6
  644. * @final
  645. * @type Number
  646. */
  647. this.KEY_NUMPAD_6 = 102;
  648.  
  649. /**
  650. * Code for the number pad 7 key.
  651. * @property KEY_NUMPAD_7
  652. * @final
  653. * @type Number
  654. */
  655. this.KEY_NUMPAD_7 = 103;
  656.  
  657. /**
  658. * Code for the number pad 8 key.
  659. * @property KEY_NUMPAD_8
  660. * @final
  661. * @type Number
  662. */
  663. this.KEY_NUMPAD_8 = 104;
  664.  
  665. /**
  666. * Code for the number pad 9 key.
  667. * @property KEY_NUMPAD_9
  668. * @final
  669. * @type Number
  670. */
  671. this.KEY_NUMPAD_9 = 105;
  672.  
  673. /**
  674. * Code for the MULTIPLY key.
  675. * @property KEY_MULTIPLY
  676. * @final
  677. * @type Number
  678. */
  679. this.KEY_MULTIPLY = 106;
  680.  
  681. /**
  682. * Code for the ADD key.
  683. * @property KEY_ADD
  684. * @final
  685. * @type Number
  686. */
  687. this.KEY_ADD = 107;
  688.  
  689. /**
  690. * Code for the SUBTRACT key.
  691. * @property KEY_SUBTRACT
  692. * @final
  693. * @type Number
  694. */
  695. this.KEY_SUBTRACT = 109;
  696.  
  697. /**
  698. * Code for the DECIMAL POINT key.
  699. * @property KEY_DECIMAL_POINT
  700. * @final
  701. * @type Number
  702. */
  703. this.KEY_DECIMAL_POINT = 110;
  704.  
  705. /**
  706. * Code for the DIVIDE key.
  707. * @property KEY_DIVIDE
  708. * @final
  709. * @type Number
  710. */
  711. this.KEY_DIVIDE = 111;
  712.  
  713. /**
  714. * Code for the F1 key.
  715. * @property KEY_F1
  716. * @final
  717. * @type Number
  718. */
  719. this.KEY_F1 = 112;
  720.  
  721. /**
  722. * Code for the F2 key.
  723. * @property KEY_F2
  724. * @final
  725. * @type Number
  726. */
  727. this.KEY_F2 = 113;
  728.  
  729. /**
  730. * Code for the F3 key.
  731. * @property KEY_F3
  732. * @final
  733. * @type Number
  734. */
  735. this.KEY_F3 = 114;
  736.  
  737. /**
  738. * Code for the F4 key.
  739. * @property KEY_F4
  740. * @final
  741. * @type Number
  742. */
  743. this.KEY_F4 = 115;
  744.  
  745. /**
  746. * Code for the F5 key.
  747. * @property KEY_F5
  748. * @final
  749. * @type Number
  750. */
  751. this.KEY_F5 = 116;
  752.  
  753. /**
  754. * Code for the F6 key.
  755. * @property KEY_F6
  756. * @final
  757. * @type Number
  758. */
  759. this.KEY_F6 = 117;
  760.  
  761. /**
  762. * Code for the F7 key.
  763. * @property KEY_F7
  764. * @final
  765. * @type Number
  766. */
  767. this.KEY_F7 = 118;
  768.  
  769. /**
  770. * Code for the F8 key.
  771. * @property KEY_F8
  772. * @final
  773. * @type Number
  774. */
  775. this.KEY_F8 = 119;
  776.  
  777. /**
  778. * Code for the F9 key.
  779. * @property KEY_F9
  780. * @final
  781. * @type Number
  782. */
  783. this.KEY_F9 = 120;
  784.  
  785. /**
  786. * Code for the F10 key.
  787. * @property KEY_F10
  788. * @final
  789. * @type Number
  790. */
  791. this.KEY_F10 = 121;
  792.  
  793. /**
  794. * Code for the F11 key.
  795. * @property KEY_F11
  796. * @final
  797. * @type Number
  798. */
  799. this.KEY_F11 = 122;
  800.  
  801. /**
  802. * Code for the F12 key.
  803. * @property KEY_F12
  804. * @final
  805. * @type Number
  806. */
  807. this.KEY_F12 = 123;
  808.  
  809. /**
  810. * Code for the NUM_LOCK key.
  811. * @property KEY_NUM_LOCK
  812. * @final
  813. * @type Number
  814. */
  815. this.KEY_NUM_LOCK = 144;
  816.  
  817. /**
  818. * Code for the SCROLL_LOCK key.
  819. * @property KEY_SCROLL_LOCK
  820. * @final
  821. * @type Number
  822. */
  823. this.KEY_SCROLL_LOCK = 145;
  824.  
  825. /**
  826. * Code for the SEMI_COLON key.
  827. * @property KEY_SEMI_COLON
  828. * @final
  829. * @type Number
  830. */
  831. this.KEY_SEMI_COLON = 186;
  832.  
  833. /**
  834. * Code for the EQUAL_SIGN key.
  835. * @property KEY_EQUAL_SIGN
  836. * @final
  837. * @type Number
  838. */
  839. this.KEY_EQUAL_SIGN = 187;
  840.  
  841. /**
  842. * Code for the COMMA key.
  843. * @property KEY_COMMA
  844. * @final
  845. * @type Number
  846. */
  847. this.KEY_COMMA = 188;
  848.  
  849. /**
  850. * Code for the DASH key.
  851. * @property KEY_DASH
  852. * @final
  853. * @type Number
  854. */
  855. this.KEY_DASH = 189;
  856.  
  857. /**
  858. * Code for the PERIOD key.
  859. * @property KEY_PERIOD
  860. * @final
  861. * @type Number
  862. */
  863. this.KEY_PERIOD = 190;
  864.  
  865. /**
  866. * Code for the FORWARD_SLASH key.
  867. * @property KEY_FORWARD_SLASH
  868. * @final
  869. * @type Number
  870. */
  871. this.KEY_FORWARD_SLASH = 191;
  872.  
  873. /**
  874. * Code for the GRAVE_ACCENT key.
  875. * @property KEY_GRAVE_ACCENT
  876. * @final
  877. * @type Number
  878. */
  879. this.KEY_GRAVE_ACCENT = 192;
  880.  
  881. /**
  882. * Code for the OPEN_BRACKET key.
  883. * @property KEY_OPEN_BRACKET
  884. * @final
  885. * @type Number
  886. */
  887. this.KEY_OPEN_BRACKET = 219;
  888.  
  889. /**
  890. * Code for the BACK_SLASH key.
  891. * @property KEY_BACK_SLASH
  892. * @final
  893. * @type Number
  894. */
  895. this.KEY_BACK_SLASH = 220;
  896.  
  897. /**
  898. * Code for the CLOSE_BRACKET key.
  899. * @property KEY_CLOSE_BRACKET
  900. * @final
  901. * @type Number
  902. */
  903. this.KEY_CLOSE_BRACKET = 221;
  904.  
  905. /**
  906. * Code for the SINGLE_QUOTE key.
  907. * @property KEY_SINGLE_QUOTE
  908. * @final
  909. * @type Number
  910. */
  911. this.KEY_SINGLE_QUOTE = 222;
  912.  
  913. /**
  914. * Code for the SPACE key.
  915. * @property KEY_SPACE
  916. * @final
  917. * @type Number
  918. */
  919. this.KEY_SPACE = 32;
  920. this.KEY_BACKSPACE = 8;
  921.  
  922. /**
  923. * Code for the TAB key.
  924. * @property KEY_TAB
  925. * @final
  926. * @type Number
  927. */
  928. this.KEY_TAB = 9;
  929.  
  930. /**
  931. * Code for the ENTER key.
  932. * @property KEY_ENTER
  933. * @final
  934. * @type Number
  935. */
  936. this.KEY_ENTER = 13;
  937.  
  938. /**
  939. * Code for the SHIFT key.
  940. * @property KEY_SHIFT
  941. * @final
  942. * @type Number
  943. */
  944. this.KEY_SHIFT = 16;
  945.  
  946. /**
  947. * Code for the CTRL key.
  948. * @property KEY_CTRL
  949. * @final
  950. * @type Number
  951. */
  952. this.KEY_CTRL = 17;
  953.  
  954. /**
  955. * Code for the ALT key.
  956. * @property KEY_ALT
  957. * @final
  958. * @type Number
  959. */
  960. this.KEY_ALT = 18;
  961.  
  962. /**
  963. * Code for the PAUSE_BREAK key.
  964. * @property KEY_PAUSE_BREAK
  965. * @final
  966. * @type Number
  967. */
  968. this.KEY_PAUSE_BREAK = 19;
  969.  
  970. /**
  971. * Code for the CAPS_LOCK key.
  972. * @property KEY_CAPS_LOCK
  973. * @final
  974. * @type Number
  975. */
  976. this.KEY_CAPS_LOCK = 20;
  977.  
  978. /**
  979. * Code for the ESCAPE key.
  980. * @property KEY_ESCAPE
  981. * @final
  982. * @type Number
  983. */
  984. this.KEY_ESCAPE = 27;
  985.  
  986. /**
  987. * Code for the PAGE_UP key.
  988. * @property KEY_PAGE_UP
  989. * @final
  990. * @type Number
  991. */
  992. this.KEY_PAGE_UP = 33;
  993.  
  994. /**
  995. * Code for the PAGE_DOWN key.
  996. * @property KEY_PAGE_DOWN
  997. * @final
  998. * @type Number
  999. */
  1000. this.KEY_PAGE_DOWN = 34;
  1001.  
  1002. /**
  1003. * Code for the END key.
  1004. * @property KEY_END
  1005. * @final
  1006. * @type Number
  1007. */
  1008. this.KEY_END = 35;
  1009.  
  1010. /**
  1011. * Code for the HOME key.
  1012. * @property KEY_HOME
  1013. * @final
  1014. * @type Number
  1015. */
  1016. this.KEY_HOME = 36;
  1017.  
  1018. /**
  1019. * Code for the LEFT_ARROW key.
  1020. * @property KEY_LEFT_ARROW
  1021. * @final
  1022. * @type Number
  1023. */
  1024. this.KEY_LEFT_ARROW = 37;
  1025.  
  1026. /**
  1027. * Code for the UP_ARROW key.
  1028. * @property KEY_UP_ARROW
  1029. * @final
  1030. * @type Number
  1031. */
  1032. this.KEY_UP_ARROW = 38;
  1033.  
  1034. /**
  1035. * Code for the RIGHT_ARROW key.
  1036. * @property KEY_RIGHT_ARROW
  1037. * @final
  1038. * @type Number
  1039. */
  1040. this.KEY_RIGHT_ARROW = 39;
  1041.  
  1042. /**
  1043. * Code for the DOWN_ARROW key.
  1044. * @property KEY_DOWN_ARROW
  1045. * @final
  1046. * @type Number
  1047. */
  1048. this.KEY_DOWN_ARROW = 40;
  1049.  
  1050. /**
  1051. * Code for the INSERT key.
  1052. * @property KEY_INSERT
  1053. * @final
  1054. * @type Number
  1055. */
  1056. this.KEY_INSERT = 45;
  1057.  
  1058. /**
  1059. * Code for the DELETE key.
  1060. * @property KEY_DELETE
  1061. * @final
  1062. * @type Number
  1063. */
  1064. this.KEY_DELETE = 46;
  1065.  
  1066. /**
  1067. * Code for the 0 key.
  1068. * @property KEY_NUM_0
  1069. * @final
  1070. * @type Number
  1071. */
  1072. this.KEY_NUM_0 = 48;
  1073.  
  1074. /**
  1075. * Code for the 1 key.
  1076. * @property KEY_NUM_1
  1077. * @final
  1078. * @type Number
  1079. */
  1080. this.KEY_NUM_1 = 49;
  1081.  
  1082. /**
  1083. * Code for the 2 key.
  1084. * @property KEY_NUM_2
  1085. * @final
  1086. * @type Number
  1087. */
  1088. this.KEY_NUM_2 = 50;
  1089.  
  1090. /**
  1091. * Code for the 3 key.
  1092. * @property KEY_NUM_3
  1093. * @final
  1094. * @type Number
  1095. */
  1096. this.KEY_NUM_3 = 51;
  1097.  
  1098. /**
  1099. * Code for the 4 key.
  1100. * @property KEY_NUM_4
  1101. * @final
  1102. * @type Number
  1103. */
  1104. this.KEY_NUM_4 = 52;
  1105.  
  1106. /**
  1107. * Code for the 5 key.
  1108. * @property KEY_NUM_5
  1109. * @final
  1110. * @type Number
  1111. */
  1112. this.KEY_NUM_5 = 53;
  1113.  
  1114. /**
  1115. * Code for the 6 key.
  1116. * @property KEY_NUM_6
  1117. * @final
  1118. * @type Number
  1119. */
  1120. this.KEY_NUM_6 = 54;
  1121.  
  1122. /**
  1123. * Code for the 7 key.
  1124. * @property KEY_NUM_7
  1125. * @final
  1126. * @type Number
  1127. */
  1128. this.KEY_NUM_7 = 55;
  1129.  
  1130. /**
  1131. * Code for the 8 key.
  1132. * @property KEY_NUM_8
  1133. * @final
  1134. * @type Number
  1135. */
  1136. this.KEY_NUM_8 = 56;
  1137.  
  1138. /**
  1139. * Code for the 9 key.
  1140. * @property KEY_NUM_9
  1141. * @final
  1142. * @type Number
  1143. */
  1144. this.KEY_NUM_9 = 57;
  1145.  
  1146. /**
  1147. * Code for the A key.
  1148. * @property KEY_A
  1149. * @final
  1150. * @type Number
  1151. */
  1152. this.KEY_A = 65;
  1153.  
  1154. /**
  1155. * Code for the B key.
  1156. * @property KEY_B
  1157. * @final
  1158. * @type Number
  1159. */
  1160. this.KEY_B = 66;
  1161.  
  1162. /**
  1163. * Code for the C key.
  1164. * @property KEY_C
  1165. * @final
  1166. * @type Number
  1167. */
  1168. this.KEY_C = 67;
  1169.  
  1170. /**
  1171. * Code for the D key.
  1172. * @property KEY_D
  1173. * @final
  1174. * @type Number
  1175. */
  1176. this.KEY_D = 68;
  1177.  
  1178. /**
  1179. * Code for the E key.
  1180. * @property KEY_E
  1181. * @final
  1182. * @type Number
  1183. */
  1184. this.KEY_E = 69;
  1185.  
  1186. /**
  1187. * Code for the F key.
  1188. * @property KEY_F
  1189. * @final
  1190. * @type Number
  1191. */
  1192. this.KEY_F = 70;
  1193.  
  1194. /**
  1195. * Code for the G key.
  1196. * @property KEY_G
  1197. * @final
  1198. * @type Number
  1199. */
  1200. this.KEY_G = 71;
  1201.  
  1202. /**
  1203. * Code for the H key.
  1204. * @property KEY_H
  1205. * @final
  1206. * @type Number
  1207. */
  1208. this.KEY_H = 72;
  1209.  
  1210. /**
  1211. * Code for the I key.
  1212. * @property KEY_I
  1213. * @final
  1214. * @type Number
  1215. */
  1216. this.KEY_I = 73;
  1217.  
  1218. /**
  1219. * Code for the J key.
  1220. * @property KEY_J
  1221. * @final
  1222. * @type Number
  1223. */
  1224. this.KEY_J = 74;
  1225.  
  1226. /**
  1227. * Code for the K key.
  1228. * @property KEY_K
  1229. * @final
  1230. * @type Number
  1231. */
  1232. this.KEY_K = 75;
  1233.  
  1234. /**
  1235. * Code for the L key.
  1236. * @property KEY_L
  1237. * @final
  1238. * @type Number
  1239. */
  1240. this.KEY_L = 76;
  1241.  
  1242. /**
  1243. * Code for the M key.
  1244. * @property KEY_M
  1245. * @final
  1246. * @type Number
  1247. */
  1248. this.KEY_M = 77;
  1249.  
  1250. /**
  1251. * Code for the N key.
  1252. * @property KEY_N
  1253. * @final
  1254. * @type Number
  1255. */
  1256. this.KEY_N = 78;
  1257.  
  1258. /**
  1259. * Code for the O key.
  1260. * @property KEY_O
  1261. * @final
  1262. * @type Number
  1263. */
  1264. this.KEY_O = 79;
  1265.  
  1266. /**
  1267. * Code for the P key.
  1268. * @property KEY_P
  1269. * @final
  1270. * @type Number
  1271. */
  1272. this.KEY_P = 80;
  1273.  
  1274. /**
  1275. * Code for the Q key.
  1276. * @property KEY_Q
  1277. * @final
  1278. * @type Number
  1279. */
  1280. this.KEY_Q = 81;
  1281.  
  1282. /**
  1283. * Code for the R key.
  1284. * @property KEY_R
  1285. * @final
  1286. * @type Number
  1287. */
  1288. this.KEY_R = 82;
  1289.  
  1290. /**
  1291. * Code for the S key.
  1292. * @property KEY_S
  1293. * @final
  1294. * @type Number
  1295. */
  1296. this.KEY_S = 83;
  1297.  
  1298. /**
  1299. * Code for the T key.
  1300. * @property KEY_T
  1301. * @final
  1302. * @type Number
  1303. */
  1304. this.KEY_T = 84;
  1305.  
  1306. /**
  1307. * Code for the U key.
  1308. * @property KEY_U
  1309. * @final
  1310. * @type Number
  1311. */
  1312. this.KEY_U = 85;
  1313.  
  1314. /**
  1315. * Code for the V key.
  1316. * @property KEY_V
  1317. * @final
  1318. * @type Number
  1319. */
  1320. this.KEY_V = 86;
  1321.  
  1322. /**
  1323. * Code for the W key.
  1324. * @property KEY_W
  1325. * @final
  1326. * @type Number
  1327. */
  1328. this.KEY_W = 87;
  1329.  
  1330. /**
  1331. * Code for the X key.
  1332. * @property KEY_X
  1333. * @final
  1334. * @type Number
  1335. */
  1336. this.KEY_X = 88;
  1337.  
  1338. /**
  1339. * Code for the Y key.
  1340. * @property KEY_Y
  1341. * @final
  1342. * @type Number
  1343. */
  1344. this.KEY_Y = 89;
  1345.  
  1346. /**
  1347. * Code for the Z key.
  1348. * @property KEY_Z
  1349. * @final
  1350. * @type Number
  1351. */
  1352. this.KEY_Z = 90;
  1353.  
  1354. /**
  1355. * Code for the LEFT_WINDOW key.
  1356. * @property KEY_LEFT_WINDOW
  1357. * @final
  1358. * @type Number
  1359. */
  1360. this.KEY_LEFT_WINDOW = 91;
  1361.  
  1362. /**
  1363. * Code for the RIGHT_WINDOW key.
  1364. * @property KEY_RIGHT_WINDOW
  1365. * @final
  1366. * @type Number
  1367. */
  1368. this.KEY_RIGHT_WINDOW = 92;
  1369.  
  1370. /**
  1371. * Code for the SELECT key.
  1372. * @property KEY_SELECT
  1373. * @final
  1374. * @type Number
  1375. */
  1376. this.KEY_SELECT_KEY = 93;
  1377.  
  1378. /**
  1379. * Code for the number pad 0 key.
  1380. * @property KEY_NUMPAD_0
  1381. * @final
  1382. * @type Number
  1383. */
  1384. this.KEY_NUMPAD_0 = 96;
  1385.  
  1386. /**
  1387. * Code for the number pad 1 key.
  1388. * @property KEY_NUMPAD_1
  1389. * @final
  1390. * @type Number
  1391. */
  1392. this.KEY_NUMPAD_1 = 97;
  1393.  
  1394. /**
  1395. * Code for the number pad 2 key.
  1396. * @property KEY_NUMPAD 2
  1397. * @final
  1398. * @type Number
  1399. */
  1400. this.KEY_NUMPAD_2 = 98;
  1401.  
  1402. /**
  1403. * Code for the number pad 3 key.
  1404. * @property KEY_NUMPAD_3
  1405. * @final
  1406. * @type Number
  1407. */
  1408. this.KEY_NUMPAD_3 = 99;
  1409.  
  1410. /**
  1411. * Code for the number pad 4 key.
  1412. * @property KEY_NUMPAD_4
  1413. * @final
  1414. * @type Number
  1415. */
  1416. this.KEY_NUMPAD_4 = 100;
  1417.  
  1418. /**
  1419. * Code for the number pad 5 key.
  1420. * @property KEY_NUMPAD_5
  1421. * @final
  1422. * @type Number
  1423. */
  1424. this.KEY_NUMPAD_5 = 101;
  1425.  
  1426. /**
  1427. * Code for the number pad 6 key.
  1428. * @property KEY_NUMPAD_6
  1429. * @final
  1430. * @type Number
  1431. */
  1432. this.KEY_NUMPAD_6 = 102;
  1433.  
  1434. /**
  1435. * Code for the number pad 7 key.
  1436. * @property KEY_NUMPAD_7
  1437. * @final
  1438. * @type Number
  1439. */
  1440. this.KEY_NUMPAD_7 = 103;
  1441.  
  1442. /**
  1443. * Code for the number pad 8 key.
  1444. * @property KEY_NUMPAD_8
  1445. * @final
  1446. * @type Number
  1447. */
  1448. this.KEY_NUMPAD_8 = 104;
  1449.  
  1450. /**
  1451. * Code for the number pad 9 key.
  1452. * @property KEY_NUMPAD_9
  1453. * @final
  1454. * @type Number
  1455. */
  1456. this.KEY_NUMPAD_9 = 105;
  1457.  
  1458. /**
  1459. * Code for the MULTIPLY key.
  1460. * @property KEY_MULTIPLY
  1461. * @final
  1462. * @type Number
  1463. */
  1464. this.KEY_MULTIPLY = 106;
  1465.  
  1466. /**
  1467. * Code for the ADD key.
  1468. * @property KEY_ADD
  1469. * @final
  1470. * @type Number
  1471. */
  1472. this.KEY_ADD = 107;
  1473.  
  1474. /**
  1475. * Code for the SUBTRACT key.
  1476. * @property KEY_SUBTRACT
  1477. * @final
  1478. * @type Number
  1479. */
  1480. this.KEY_SUBTRACT = 109;
  1481.  
  1482. /**
  1483. * Code for the DECIMAL POINT key.
  1484. * @property KEY_DECIMAL_POINT
  1485. * @final
  1486. * @type Number
  1487. */
  1488. this.KEY_DECIMAL_POINT = 110;
  1489.  
  1490. /**
  1491. * Code for the DIVIDE key.
  1492. * @property KEY_DIVIDE
  1493. * @final
  1494. * @type Number
  1495. */
  1496. this.KEY_DIVIDE = 111;
  1497.  
  1498. /**
  1499. * Code for the F1 key.
  1500. * @property KEY_F1
  1501. * @final
  1502. * @type Number
  1503. */
  1504. this.KEY_F1 = 112;
  1505.  
  1506. /**
  1507. * Code for the F2 key.
  1508. * @property KEY_F2
  1509. * @final
  1510. * @type Number
  1511. */
  1512. this.KEY_F2 = 113;
  1513.  
  1514. /**
  1515. * Code for the F3 key.
  1516. * @property KEY_F3
  1517. * @final
  1518. * @type Number
  1519. */
  1520. this.KEY_F3 = 114;
  1521.  
  1522. /**
  1523. * Code for the F4 key.
  1524. * @property KEY_F4
  1525. * @final
  1526. * @type Number
  1527. */
  1528. this.KEY_F4 = 115;
  1529.  
  1530. /**
  1531. * Code for the F5 key.
  1532. * @property KEY_F5
  1533. * @final
  1534. * @type Number
  1535. */
  1536. this.KEY_F5 = 116;
  1537.  
  1538. /**
  1539. * Code for the F6 key.
  1540. * @property KEY_F6
  1541. * @final
  1542. * @type Number
  1543. */
  1544. this.KEY_F6 = 117;
  1545.  
  1546. /**
  1547. * Code for the F7 key.
  1548. * @property KEY_F7
  1549. * @final
  1550. * @type Number
  1551. */
  1552. this.KEY_F7 = 118;
  1553.  
  1554. /**
  1555. * Code for the F8 key.
  1556. * @property KEY_F8
  1557. * @final
  1558. * @type Number
  1559. */
  1560. this.KEY_F8 = 119;
  1561.  
  1562. /**
  1563. * Code for the F9 key.
  1564. * @property KEY_F9
  1565. * @final
  1566. * @type Number
  1567. */
  1568. this.KEY_F9 = 120;
  1569.  
  1570. /**
  1571. * Code for the F10 key.
  1572. * @property KEY_F10
  1573. * @final
  1574. * @type Number
  1575. */
  1576. this.KEY_F10 = 121;
  1577.  
  1578. /**
  1579. * Code for the F11 key.
  1580. * @property KEY_F11
  1581. * @final
  1582. * @type Number
  1583. */
  1584. this.KEY_F11 = 122;
  1585.  
  1586. /**
  1587. * Code for the F12 key.
  1588. * @property KEY_F12
  1589. * @final
  1590. * @type Number
  1591. */
  1592. this.KEY_F12 = 123;
  1593.  
  1594. /**
  1595. * Code for the NUM_LOCK key.
  1596. * @property KEY_NUM_LOCK
  1597. * @final
  1598. * @type Number
  1599. */
  1600. this.KEY_NUM_LOCK = 144;
  1601.  
  1602. /**
  1603. * Code for the SCROLL_LOCK key.
  1604. * @property KEY_SCROLL_LOCK
  1605. * @final
  1606. * @type Number
  1607. */
  1608. this.KEY_SCROLL_LOCK = 145;
  1609.  
  1610. /**
  1611. * Code for the SEMI_COLON key.
  1612. * @property KEY_SEMI_COLON
  1613. * @final
  1614. * @type Number
  1615. */
  1616. this.KEY_SEMI_COLON = 186;
  1617.  
  1618. /**
  1619. * Code for the EQUAL_SIGN key.
  1620. * @property KEY_EQUAL_SIGN
  1621. * @final
  1622. * @type Number
  1623. */
  1624. this.KEY_EQUAL_SIGN = 187;
  1625.  
  1626. /**
  1627. * Code for the COMMA key.
  1628. * @property KEY_COMMA
  1629. * @final
  1630. * @type Number
  1631. */
  1632. this.KEY_COMMA = 188;
  1633.  
  1634. /**
  1635. * Code for the DASH key.
  1636. * @property KEY_DASH
  1637. * @final
  1638. * @type Number
  1639. */
  1640. this.KEY_DASH = 189;
  1641.  
  1642. /**
  1643. * Code for the PERIOD key.
  1644. * @property KEY_PERIOD
  1645. * @final
  1646. * @type Number
  1647. */
  1648. this.KEY_PERIOD = 190;
  1649.  
  1650. /**
  1651. * Code for the FORWARD_SLASH key.
  1652. * @property KEY_FORWARD_SLASH
  1653. * @final
  1654. * @type Number
  1655. */
  1656. this.KEY_FORWARD_SLASH = 191;
  1657.  
  1658. /**
  1659. * Code for the GRAVE_ACCENT key.
  1660. * @property KEY_GRAVE_ACCENT
  1661. * @final
  1662. * @type Number
  1663. */
  1664. this.KEY_GRAVE_ACCENT = 192;
  1665.  
  1666. /**
  1667. * Code for the OPEN_BRACKET key.
  1668. * @property KEY_OPEN_BRACKET
  1669. * @final
  1670. * @type Number
  1671. */
  1672. this.KEY_OPEN_BRACKET = 219;
  1673.  
  1674. /**
  1675. * Code for the BACK_SLASH key.
  1676. * @property KEY_BACK_SLASH
  1677. * @final
  1678. * @type Number
  1679. */
  1680. this.KEY_BACK_SLASH = 220;
  1681.  
  1682. /**
  1683. * Code for the CLOSE_BRACKET key.
  1684. * @property KEY_CLOSE_BRACKET
  1685. * @final
  1686. * @type Number
  1687. */
  1688. this.KEY_CLOSE_BRACKET = 221;
  1689.  
  1690. /**
  1691. * Code for the SINGLE_QUOTE key.
  1692. * @property KEY_SINGLE_QUOTE
  1693. * @final
  1694. * @type Number
  1695. */
  1696. this.KEY_SINGLE_QUOTE = 222;
  1697.  
  1698. /**
  1699. * Code for the SPACE key.
  1700. * @property KEY_SPACE
  1701. * @final
  1702. * @type Number
  1703. */
  1704. this.KEY_SPACE = 32;
  1705.  
  1706. this._element = cfg.element;
  1707.  
  1708. // True when ALT down
  1709. this.altDown = false;
  1710.  
  1711. /** True whenever CTRL is down
  1712. *
  1713. * @type {boolean}
  1714. */
  1715. this.ctrlDown = false;
  1716.  
  1717. /** True whenever left mouse button is down
  1718. *
  1719. * @type {boolean}
  1720. */
  1721. this.mouseDownLeft = false;
  1722.  
  1723. /** True whenever middle mouse button is down
  1724. *
  1725. * @type {boolean}
  1726. */
  1727. this.mouseDownMiddle = false;
  1728.  
  1729. /** True whenever right mouse button is down
  1730. *
  1731. * @type {boolean}
  1732. */
  1733. this.mouseDownRight = false;
  1734.  
  1735. /** Flag for each key that's down
  1736. *
  1737. * @type {boolean}
  1738. */
  1739. this.keyDown = [];
  1740.  
  1741. /** True while input enabled
  1742. *
  1743. * @type {boolean}
  1744. */
  1745. this.enabled = true;
  1746.  
  1747. /** True while mouse is over the parent {{#crossLink "Scene"}}Scene's{{/crossLink}} {{#crossLink "Canvas"}}Canvas{{/crossLink}}
  1748. *
  1749. * @type {boolean}
  1750. */
  1751. this.mouseover = false;
  1752.  
  1753. // Capture input events and publish them on this component
  1754.  
  1755. document.addEventListener("keydown", this._keyDownListener = function (e) {
  1756.  
  1757. if (!self.enabled) {
  1758. return;
  1759. }
  1760.  
  1761. if (e.target.tagName !== "INPUT" && e.target.tagName !== "TEXTAREA") {
  1762.  
  1763. if (e.ctrlKey) {
  1764. self.ctrlDown = true;
  1765.  
  1766. } else if (e.altKey) {
  1767. self.altDown = true;
  1768.  
  1769. } else {
  1770. self.keyDown[e.keyCode] = true;
  1771.  
  1772. /**
  1773. * Fired whenever a key is pressed while the parent
  1774. * {{#crossLink "Scene"}}Scene{{/crossLink}}'s {{#crossLink "Canvas"}}Canvas{{/crossLink}} has input focus.
  1775. * @event keydown
  1776. * @param value {Number} The key code, for example {{#crossLink "Input/KEY_LEFT_ARROW:property"}}{{/crossLink}},
  1777. */
  1778. self.fire("keydown", e.keyCode, true);
  1779. }
  1780. }
  1781.  
  1782. if (self.mouseover) {
  1783. e.preventDefault();
  1784. }
  1785.  
  1786. }, true);
  1787.  
  1788. document.addEventListener("keyup", this._keyUpListener = function (e) {
  1789.  
  1790. if (!self.enabled) {
  1791. return;
  1792. }
  1793.  
  1794. if (e.target.tagName !== "INPUT" && e.target.tagName !== "TEXTAREA") {
  1795.  
  1796. if (e.ctrlKey) {
  1797. self.ctrlDown = false;
  1798.  
  1799. } else if (e.altKey) {
  1800. self.altDown = false;
  1801.  
  1802. } else {
  1803. self.keyDown[e.keyCode] = false;
  1804.  
  1805. /**
  1806. * Fired whenever a key is released while the parent
  1807. * {{#crossLink "Scene"}}Scene{{/crossLink}}'s {{#crossLink "Canvas"}}Canvas{{/crossLink}} has input focus.
  1808. * @event keyup
  1809. * @param value {Number} The key code, for example {{#crossLink "Input/KEY_LEFT_ARROW:property"}}{{/crossLink}},
  1810. */
  1811. self.fire("keyup", e.keyCode, true);
  1812. }
  1813. }
  1814. });
  1815.  
  1816. cfg.element.addEventListener("mouseenter", this._mouseEnterListener = function (e) {
  1817.  
  1818. if (!self.enabled) {
  1819. return;
  1820. }
  1821.  
  1822. self.mouseover = true;
  1823.  
  1824. const coords = self._getClickCoordsWithinElement(e);
  1825.  
  1826. /**
  1827. * Fired whenever the mouse is moved into of the parent
  1828. * {{#crossLink "Scene"}}Scene{{/crossLink}}'s {{#crossLink "Canvas"}}Canvas{{/crossLink}}.
  1829. * @event mouseenter
  1830. * @param value {[Number, Number]} The mouse coordinates within the {{#crossLink "Canvas"}}Canvas{{/crossLink}},
  1831. */
  1832. self.fire("mouseenter", coords, true);
  1833. });
  1834.  
  1835. cfg.element.addEventListener("mouseleave", this._mouseLeaveListener = function (e) {
  1836.  
  1837. if (!self.enabled) {
  1838. return;
  1839. }
  1840.  
  1841. self.mouseover = false;
  1842.  
  1843. const coords = self._getClickCoordsWithinElement(e);
  1844.  
  1845. /**
  1846. * Fired whenever the mouse is moved out of the parent
  1847. * {{#crossLink "Scene"}}Scene{{/crossLink}}'s {{#crossLink "Canvas"}}Canvas{{/crossLink}}.
  1848. * @event mouseleave
  1849. * @param value {[Number, Number]} The mouse coordinates within the {{#crossLink "Canvas"}}Canvas{{/crossLink}},
  1850. */
  1851. self.fire("mouseleave", coords, true);
  1852. });
  1853.  
  1854.  
  1855. cfg.element.addEventListener("mousedown", this._mouseDownListener = function (e) {
  1856.  
  1857. if (!self.enabled) {
  1858. return;
  1859. }
  1860.  
  1861. switch (e.which) {
  1862.  
  1863. case 1:// Left button
  1864. self.mouseDownLeft = true;
  1865. break;
  1866.  
  1867. case 2:// Middle/both buttons
  1868. self.mouseDownMiddle = true;
  1869. break;
  1870.  
  1871. case 3:// Right button
  1872. self.mouseDownRight = true;
  1873. break;
  1874.  
  1875. default:
  1876. break;
  1877. }
  1878.  
  1879. const coords = self._getClickCoordsWithinElement(e);
  1880.  
  1881. cfg.element.focus();
  1882.  
  1883. /**
  1884. * Fired whenever the mouse is pressed over the parent
  1885. * {{#crossLink "Scene"}}Scene{{/crossLink}}'s {{#crossLink "Canvas"}}Canvas{{/crossLink}}.
  1886. * @event mousedown
  1887. * @param value {[Number, Number]} The mouse coordinates within the {{#crossLink "Canvas"}}Canvas{{/crossLink}},
  1888. */
  1889. self.fire("mousedown", coords, true);
  1890.  
  1891. if (self.mouseover) {
  1892. e.preventDefault();
  1893. }
  1894. });
  1895.  
  1896. document.addEventListener("mouseup", this._mouseUpListener = function (e) {
  1897.  
  1898. if (!self.enabled) {
  1899. return;
  1900. }
  1901.  
  1902. switch (e.which) {
  1903.  
  1904. case 1:// Left button
  1905. self.mouseDownLeft = false;
  1906. break;
  1907.  
  1908. case 2:// Middle/both buttons
  1909. self.mouseDownMiddle = false;
  1910. break;
  1911.  
  1912. case 3:// Right button
  1913. self.mouseDownRight = false;
  1914. break;
  1915.  
  1916. default:
  1917. break;
  1918. }
  1919.  
  1920. const coords = self._getClickCoordsWithinElement(e);
  1921.  
  1922. /**
  1923. * Fired whenever the mouse is released over the parent
  1924. * {{#crossLink "Scene"}}Scene{{/crossLink}}'s {{#crossLink "Canvas"}}Canvas{{/crossLink}}.
  1925. * @event mouseup
  1926. * @param value {[Number, Number]} The mouse coordinates within the {{#crossLink "Canvas"}}Canvas{{/crossLink}},
  1927. */
  1928. self.fire("mouseup", coords, true);
  1929.  
  1930. if (self.mouseover) {
  1931. e.preventDefault();
  1932. }
  1933. }, true);
  1934.  
  1935. document.addEventListener("dblclick", this._dblClickListener = function (e) {
  1936.  
  1937. if (!self.enabled) {
  1938. return;
  1939. }
  1940.  
  1941. switch (e.which) {
  1942.  
  1943. case 1:// Left button
  1944. self.mouseDownLeft = false;
  1945. self.mouseDownRight = false;
  1946. break;
  1947.  
  1948. case 2:// Middle/both buttons
  1949. self.mouseDownMiddle = false;
  1950. break;
  1951.  
  1952. case 3:// Right button
  1953. self.mouseDownLeft = false;
  1954. self.mouseDownRight = false;
  1955. break;
  1956.  
  1957. default:
  1958. break;
  1959. }
  1960.  
  1961. const coords = self._getClickCoordsWithinElement(e);
  1962.  
  1963. /**
  1964. * Fired whenever the mouse is double-clicked over the parent
  1965. * {{#crossLink "Scene"}}Scene{{/crossLink}}'s {{#crossLink "Canvas"}}Canvas{{/crossLink}}.
  1966. * @event dblclick
  1967. * @param value {[Number, Number]} The mouse coordinates within the {{#crossLink "Canvas"}}Canvas{{/crossLink}},
  1968. */
  1969. self.fire("dblclick", coords, true);
  1970.  
  1971. if (self.mouseover) {
  1972. e.preventDefault();
  1973. }
  1974. });
  1975.  
  1976. cfg.element.addEventListener("mousemove", this._mouseMoveListener = function (e) {
  1977.  
  1978. if (!self.enabled) {
  1979. return;
  1980. }
  1981.  
  1982. const coords = self._getClickCoordsWithinElement(e);
  1983.  
  1984. /**
  1985. * Fired whenever the mouse is moved over the parent
  1986. * {{#crossLink "Scene"}}Scene{{/crossLink}}'s {{#crossLink "Canvas"}}Canvas{{/crossLink}}.
  1987. * @event mousedown
  1988. * @param value {[Number, Number]} The mouse coordinates within the {{#crossLink "Canvas"}}Canvas{{/crossLink}},
  1989. */
  1990. self.fire("mousemove", coords, true);
  1991.  
  1992. if (self.mouseover) {
  1993. e.preventDefault();
  1994. }
  1995. });
  1996.  
  1997. cfg.element.addEventListener("wheel", this._mouseWheelListener = function (e, d) {
  1998.  
  1999. if (!self.enabled) {
  2000. return;
  2001. }
  2002.  
  2003. const delta = Math.max(-1, Math.min(1, -e.deltaY * 40));
  2004.  
  2005. /**
  2006. * Fired whenever the mouse wheel is moved over the parent
  2007. * {{#crossLink "Scene"}}Scene{{/crossLink}}'s {{#crossLink "Canvas"}}Canvas{{/crossLink}}.
  2008. * @event mousewheel
  2009. * @param delta {Number} The mouse wheel delta,
  2010. */
  2011. self.fire("mousewheel", delta, true);
  2012. }, {passive: true});
  2013.  
  2014. // mouseclicked
  2015.  
  2016. (function () {
  2017.  
  2018. let downX;
  2019. let downY;
  2020.  
  2021. // Tolerance between down and up positions for a mouse click
  2022. const tolerance = 2;
  2023.  
  2024. self.on("mousedown", function (params) {
  2025. downX = params[0];
  2026. downY = params[1];
  2027. });
  2028.  
  2029. self.on("mouseup", function (params) {
  2030.  
  2031. if (downX >= (params[0] - tolerance) &&
  2032. downX <= (params[0] + tolerance) &&
  2033. downY >= (params[1] - tolerance) &&
  2034. downY <= (params[1] + tolerance)) {
  2035.  
  2036. /**
  2037. * Fired whenever the mouse is clicked over the parent
  2038. * {{#crossLink "Scene"}}Scene{{/crossLink}}'s {{#crossLink "Canvas"}}Canvas{{/crossLink}}.
  2039. * @event mouseclicked
  2040. * @param value {[Number, Number]} The mouse coordinates within the {{#crossLink "Canvas"}}Canvas{{/crossLink}},
  2041. */
  2042. self.fire("mouseclicked", params, true);
  2043. }
  2044. });
  2045. })();
  2046.  
  2047.  
  2048. // VR
  2049.  
  2050. (function () {
  2051.  
  2052. const orientationAngleLookup = {
  2053. 'landscape-primary': 90,
  2054. 'landscape-secondary': -90,
  2055. 'portrait-secondary': 180,
  2056. 'portrait-primary': 0
  2057. };
  2058.  
  2059. let orientation;
  2060. let orientationAngle;
  2061. const acceleration = math.vec3();
  2062. const accelerationIncludingGravity = math.vec3();
  2063.  
  2064. const orientationChangeEvent = {
  2065. orientation: null,
  2066. orientationAngle: 0
  2067. };
  2068.  
  2069. const deviceMotionEvent = {
  2070. orientationAngle: 0,
  2071. acceleration: null,
  2072. accelerationIncludingGravity: accelerationIncludingGravity,
  2073. rotationRate: math.vec3(),
  2074. interval: 0
  2075. };
  2076.  
  2077. const deviceOrientationEvent = {
  2078. alpha: 0,
  2079. beta: 0,
  2080. gamma: 0,
  2081. absolute: false
  2082. };
  2083.  
  2084. if (window.OrientationChangeEvent) {
  2085. window.addEventListener('orientationchange', self._orientationchangedListener = function () {
  2086.  
  2087. orientation = window.screen.orientation || window.screen.mozOrientation || window.msOrientation || null;
  2088. orientationAngle = orientation ? (orientationAngleLookup[orientation] || 0) : 0;
  2089.  
  2090. orientationChangeEvent.orientation = orientation;
  2091. orientationChangeEvent.orientationAngle = orientationAngle;
  2092.  
  2093. /**
  2094. * Fired when the orientation of the device has changed.
  2095. *
  2096. * @event orientationchange
  2097. * @param orientation The orientation: "landscape-primary", "landscape-secondary", "portrait-secondary" or "portrait-primary"
  2098. * @param orientationAngle The orientation angle in degrees: 90 for landscape-primary, -90 for landscape-secondary, 180 for portrait-secondary or 0 for portrait-primary.
  2099. */
  2100. self.fire("orientationchange", orientationChangeEvent);
  2101. },
  2102. false);
  2103. }
  2104.  
  2105. if (window.DeviceMotionEvent) {
  2106. window.addEventListener('devicemotion', self._deviceMotionListener = function (e) {
  2107.  
  2108. deviceMotionEvent.interval = e.interval;
  2109. deviceMotionEvent.orientationAngle = orientationAngle;
  2110.  
  2111. const accel = e.acceleration;
  2112.  
  2113. if (accel) {
  2114. acceleration[0] = accel.x;
  2115. acceleration[1] = accel.y;
  2116. acceleration[2] = accel.z;
  2117. deviceMotionEvent.acceleration = acceleration;
  2118. } else {
  2119. deviceMotionEvent.acceleration = null;
  2120. }
  2121.  
  2122. const accelGrav = e.accelerationIncludingGravity;
  2123.  
  2124. if (accelGrav) {
  2125. accelerationIncludingGravity[0] = accelGrav.x;
  2126. accelerationIncludingGravity[1] = accelGrav.y;
  2127. accelerationIncludingGravity[2] = accelGrav.z;
  2128. deviceMotionEvent.accelerationIncludingGravity = accelerationIncludingGravity;
  2129. } else {
  2130. deviceMotionEvent.accelerationIncludingGravity = null;
  2131. }
  2132.  
  2133. deviceMotionEvent.rotationRate = e.rotationRate;
  2134.  
  2135. /**
  2136. * Fires on a regular interval and returns data about the rotation
  2137. * (in degrees per second) and acceleration (in meters per second squared) of the device, at that moment in
  2138. * time. Some devices do not have the hardware to exclude the effect of gravity.
  2139. *
  2140. * @event devicemotion
  2141. * @param Float32Array acceleration The acceleration of the device, in meters per second squared, as a 3-element vector. This value has taken into account the effect of gravity and removed it from the figures. This value may not exist if the hardware doesn't know how to remove gravity from the acceleration data.
  2142. * @param Float32Array accelerationIncludingGravity The acceleration of the device, in meters per second squared, as a 3-element vector. This value includes the effect of gravity, and may be the only value available on devices that don't have a gyroscope to allow them to properly remove gravity from the data.
  2143. * @param, Number interval The interval, in milliseconds, at which this event is fired. The next event will be fired in approximately this amount of time.
  2144. * @param Float32Array rotationRate The rates of rotation of the device about each axis, in degrees per second.
  2145. */
  2146. self.fire("devicemotion", deviceMotionEvent);
  2147. },
  2148. false);
  2149. }
  2150.  
  2151. if (window.DeviceOrientationEvent) {
  2152. window.addEventListener("deviceorientation", self._deviceOrientListener = function (e) {
  2153.  
  2154. deviceOrientationEvent.gamma = e.gamma;
  2155. deviceOrientationEvent.beta = e.beta;
  2156. deviceOrientationEvent.alpha = e.alpha;
  2157. deviceOrientationEvent.absolute = e.absolute;
  2158.  
  2159. /**
  2160. * Fired when fresh data is available from an orientation sensor about the current orientation
  2161. * of the device as compared to the Earth coordinate frame. This data is gathered from a
  2162. * magnetometer inside the device. See
  2163. * <a href="https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Orientation_and_motion_data_explained">Orientation and motion data explained</a> for more info.
  2164. *
  2165. * @event deviceorientation
  2166. * @param Number alpha The current orientation of the device around the Z axis in degrees; that is, how far the device is rotated around a line perpendicular to the device.
  2167. * @param Number beta The current orientation of the device around the X axis in degrees; that is, how far the device is tipped forward or backward.
  2168. * @param Number gamma The current orientation of the device around the Y axis in degrees; that is, how far the device is turned left or right.
  2169. * @param Boolean absolute This value is true if the orientation is provided as a difference between the device coordinate frame and the Earth coordinate frame; if the device can't detect the Earth coordinate frame, this value is false.
  2170. */
  2171. self.fire("deviceorientation", deviceOrientationEvent);
  2172. },
  2173. false);
  2174. }
  2175. })();
  2176. }
  2177.  
  2178. _getClickCoordsWithinElement(event) {
  2179. const coords = [0, 0];
  2180. if (!event) {
  2181. event = window.event;
  2182. coords.x = event.x;
  2183. coords.y = event.y;
  2184. }
  2185. else {
  2186. let element = event.target;
  2187. let totalOffsetLeft = 0;
  2188. let totalOffsetTop = 0;
  2189.  
  2190. while (element.offsetParent) {
  2191. totalOffsetLeft += element.offsetLeft;
  2192. totalOffsetTop += element.offsetTop;
  2193. element = element.offsetParent;
  2194. }
  2195. coords[0] = event.pageX - totalOffsetLeft;
  2196. coords[1] = event.pageY - totalOffsetTop;
  2197. }
  2198. return coords;
  2199. }
  2200.  
  2201. /**
  2202. * Enable or disable all input handlers
  2203. *
  2204. * @param enable
  2205. */
  2206. setEnabled(enable) {
  2207. if (this.enabled !== enable) {
  2208. this.fire("enabled", this.enabled = enable);
  2209. }
  2210. }
  2211.  
  2212. destroy() {
  2213. super.destroy();
  2214. // Prevent memory leak when destroying canvas/WebGL context
  2215. document.removeEventListener("keydown", this._keyDownListener);
  2216. document.removeEventListener("keyup", this._keyUpListener);
  2217. this._element.removeEventListener("mouseenter", this._mouseEnterListener);
  2218. this._element.removeEventListener("mouseleave", this._mouseLeaveListener);
  2219. this._element.removeEventListener("mousedown", this._mouseDownListener);
  2220. document.removeEventListener("mouseup", this._mouseDownListener);
  2221. document.removeEventListener("dblclick", this._dblClickListener);
  2222. this._element.removeEventListener("mousemove", this._mouseMoveListener);
  2223. this._element.removeEventListener("wheel", this._mouseWheelListener);
  2224. if (window.OrientationChangeEvent) {
  2225. window.removeEventListener('orientationchange', this._orientationchangedListener);
  2226. }
  2227. if (window.DeviceMotionEvent) {
  2228. window.removeEventListener('devicemotion', this._deviceMotionListener);
  2229. }
  2230. if (window.DeviceOrientationEvent) {
  2231. window.addEventListener("deviceorientation", this._deviceOrientListener);
  2232. }
  2233. }
  2234. }
  2235.  
  2236. export{Input};
  2237.