A pixel art painter geared specifically at NES pixel art. Includes export for .chr binary file as well as palette and namespace data.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

119 lines
3.4KB

  1. export class EventCaller{
  2. constructor(){
  3. this.__listeners = {};
  4. }
  5. get totalListeners(){
  6. var count = 0;
  7. for (key in Object.keys(this.__listeners)){
  8. count += this.__listeners[key].length
  9. }
  10. return count;
  11. }
  12. is_listening(eventName, callback, owner=null){
  13. if (typeof(eventName) !== 'string')
  14. throw new TypeError("Expected eventName to be string.");
  15. if (eventName.length <= 0)
  16. throw new ValueError("Argument eventName cannot be a zero-length string.");
  17. if (this.__listeners.hasOwnProperty(eventName)){
  18. if (typeof(callback) !== 'function')
  19. throw new TypeError("Expected callback argument to be a function or class method.");
  20. for (var i=0; i < this.__listeners[eventName].length; i++){
  21. if (this.__listeners[eventName][i][0] === callback &&
  22. this.__listeners[eventName][i][1] === owner){
  23. return true;
  24. }
  25. }
  26. }
  27. return false;
  28. }
  29. listen(eventName, callback, owner=null, once=false){
  30. try{
  31. if (!this.is_listening(eventName, callback, owner)){
  32. if (!this.__listeners.hasOwnProperty(eventName)){
  33. this.__listeners[eventName] = [];
  34. }
  35. this.__listeners[eventName].push([callback, owner, once]);
  36. }
  37. } catch (e) {
  38. throw e;
  39. }
  40. return this;
  41. }
  42. unlisten(eventName, callback, owner=null){
  43. if (typeof(eventName) !== 'string')
  44. throw new TypeError("Expected eventName to be string.");
  45. if (eventName.length <= 0)
  46. throw new ValueError("Argument eventName cannot be a zero-length string.");
  47. if (this.__listeners.hasOwnProperty(eventName)){
  48. if (typeof(callback) !== 'function')
  49. throw new TypeError("Expected callback argument to be a function or class method.");
  50. for (var i=0; i < this.__listeners[eventName].length; i++){
  51. if (this.__listeners[eventName][i][0] === callback &&
  52. this.__listeners[eventName][i][1] === owner){
  53. this.__listeners[eventName].splice(i, 1);
  54. if (this.__listeners[eventName].length <= 0)
  55. delete this.__listeners[eventName];
  56. break;
  57. }
  58. }
  59. }
  60. return this;
  61. }
  62. unlisten_event(eventName){
  63. if (typeof(eventName) !== 'string')
  64. throw new TypeError("Expected eventName to be string.");
  65. if (eventName.length <= 0)
  66. throw new ValueError("Argument eventName cannot be a zero-length string.");
  67. if (this.__listener.hasOwnProperty(eventName))
  68. delete this.__listener[eventName];
  69. return this;
  70. }
  71. unlisten_all(){
  72. // NOTE: Perhaps it's better to loop through and delete each property? This should do for now though.
  73. this.__listeners = {};
  74. }
  75. emit(eventName, args=null){
  76. if (typeof(eventName) !== 'string')
  77. throw new TypeError("Expected eventName to be string.");
  78. if (eventName.length <= 0)
  79. throw new ValueError("Argument eventName cannot be a zero-length string.");
  80. var once = [];
  81. if (this.__listeners.hasOwnProperty(eventName)){
  82. for (var i=0; i < this.__listeners[eventName].length; i++){
  83. var cb = this.__listeners[eventName][i][0];
  84. var own = this.__listeners[eventName][i][1];
  85. if (this.__listeners[eventName][i][2] === true)
  86. once.push([cb, own]);
  87. cb.apply(own, args);
  88. }
  89. if (once.length > 0){
  90. for (var i=0; i < once.length; i++){
  91. this.unlisten(eventName, once[i][0], once[i][1]);
  92. }
  93. }
  94. }
  95. return this;
  96. }
  97. }