A pixel art painter geared specifically at NES pixel art. Includes export for .chr binary file as well as palette and namespace data.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

125 行
3.6KB

  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(){
  76. var args = Array.from(arguments);
  77. if (args.length <= 0)
  78. throw new Error("Missing required eventName argument.");
  79. var eventName = args[0];
  80. args = args.slice(1);
  81. if (typeof(eventName) !== 'string')
  82. throw new TypeError("Expected eventName to be string.");
  83. if (eventName.length <= 0)
  84. throw new ValueError("Argument eventName cannot be a zero-length string.");
  85. var once = [];
  86. if (this.__listeners.hasOwnProperty(eventName)){
  87. for (var i=0; i < this.__listeners[eventName].length; i++){
  88. var cb = this.__listeners[eventName][i][0];
  89. var own = this.__listeners[eventName][i][1];
  90. if (this.__listeners[eventName][i][2] === true)
  91. once.push([cb, own]);
  92. cb.apply(own, args);
  93. }
  94. if (once.length > 0){
  95. for (var i=0; i < once.length; i++){
  96. this.unlisten(eventName, once[i][0], once[i][1]);
  97. }
  98. }
  99. }
  100. return this;
  101. }
  102. }