Key.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * Key is used by Keyboard, Pointer, etc, to represent a key state.
  3. *
  4. * @class
  5. */
  6. function Key()
  7. {
  8. /**
  9. * Indicates if this key is currently pressed.
  10. *
  11. * @type {boolean}
  12. */
  13. this.pressed = false;
  14. /**
  15. * Indicates if this key was just pressed.
  16. *
  17. * @type {boolean}
  18. */
  19. this.justPressed = false;
  20. /**
  21. * Indicates if this key was just released.
  22. *
  23. * @type {boolean}
  24. */
  25. this.justReleased = false;
  26. }
  27. /**
  28. * Key down event.
  29. *
  30. * @type {number}
  31. */
  32. Key.DOWN = -1;
  33. /**
  34. * Key up event.
  35. *
  36. * @type {number}
  37. */
  38. Key.UP = 1;
  39. /**
  40. * Key reset event.
  41. *
  42. * @type {number}
  43. */
  44. Key.RESET = 0;
  45. Key.prototype.constructor = Key;
  46. /**
  47. * Update Key status based on new key state.
  48. *
  49. * @param {number} action Key action that was performed.
  50. */
  51. Key.prototype.update = function(action)
  52. {
  53. this.justPressed = false;
  54. this.justReleased = false;
  55. if(action === Key.DOWN)
  56. {
  57. if(this.pressed === false)
  58. {
  59. this.justPressed = true;
  60. }
  61. this.pressed = true;
  62. }
  63. else if(action === Key.UP)
  64. {
  65. if(this.pressed)
  66. {
  67. this.justReleased = true;
  68. }
  69. this.pressed = false;
  70. }
  71. else if(action === Key.RESET)
  72. {
  73. this.justReleased = false;
  74. this.justPressed = false;
  75. }
  76. };
  77. /**
  78. * Set this key attributes manually.
  79. *
  80. * @param {boolean} justPressed Indicates if the button was just pressed.
  81. * @param {boolean} pressed Indicates if the button is currently being pressed.
  82. * @param {boolean} justReleased Indicates if the button was just released.
  83. */
  84. Key.prototype.set = function(justPressed, pressed, justReleased)
  85. {
  86. this.justPressed = justPressed;
  87. this.pressed = pressed;
  88. this.justReleased = justReleased;
  89. };
  90. /**
  91. * Reset key to default values.
  92. */
  93. Key.prototype.reset = function()
  94. {
  95. this.justPressed = false;
  96. this.pressed = false;
  97. this.justReleased = false;
  98. };
  99. export {Key};