Dialog.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. require('TemplateLoader');
  2. class Dialog {
  3. constructor() {
  4. this.leftDialog = null;
  5. this.rightDialog = null;
  6. this.lastMessage = "right";
  7. }
  8. async attach(parent) {
  9. this.leftDialog = document.createElement('div');
  10. this.leftDialog.id = 'dialog-container-left';
  11. this.leftDialog.className = "animate-visibility";
  12. this.leftDialog.addEventListener('click', (event) => this.advance(event));
  13. document.body.appendChild(this.leftDialog);
  14. this.rightDialog = document.createElement('div');
  15. this.rightDialog.id = 'dialog-container-right';
  16. this.rightDialog.className = "animate-visibility";
  17. this.rightDialog.addEventListener('click', (event) => this.advance(event));
  18. document.body.appendChild(this.rightDialog);
  19. }
  20. draw(context, delta) {
  21. }
  22. advance(event) {
  23. switch (this.lastMessage) {
  24. case "left":
  25. this.clickRightDialog();
  26. this.lastMessage = "right";
  27. break;
  28. case "right":
  29. this.clickLeftDialog();
  30. this.lastMessage = "left";
  31. break;
  32. }
  33. }
  34. clickLeftDialog(event) {
  35. if (this.leftDialog.childNodes.length > 0) {
  36. this.leftDialog.removeChild(this.leftDialog.childNodes[0]);
  37. return true;
  38. }
  39. return false;
  40. }
  41. clickRightDialog(event) {
  42. if (this.rightDialog.childNodes.length > 0) {
  43. this.rightDialog.removeChild(this.rightDialog.childNodes[0]);
  44. return true;
  45. }
  46. return false;
  47. }
  48. leftSay(message, className) {
  49. let messageSpan = document.createElement("span");
  50. messageSpan.className = "message";
  51. if (className) {
  52. messageSpan.className += " " + className;
  53. }
  54. messageSpan.innerHTML = message;
  55. this.leftDialog.appendChild(messageSpan);
  56. let containerHeight = this.leftDialog.offsetHeight;
  57. let childrenHeight = 0;
  58. for (let i = 0; i < this.leftDialog.childNodes.length; i++) {
  59. childrenHeight += this.leftDialog.childNodes[i].offsetHeight;
  60. }
  61. if (childrenHeight >= containerHeight) {
  62. this.leftDialog.removeChild(this.leftDialog.childNodes[0]);
  63. }
  64. messageSpan.style.opacity = 1;
  65. this.lastMessage = "left";
  66. }
  67. rightSay(message, className) {
  68. let messageSpan = document.createElement("span");
  69. messageSpan.className = "message";
  70. if (className) {
  71. messageSpan.className += " " + className;
  72. }
  73. messageSpan.innerHTML = message;
  74. this.rightDialog.appendChild(messageSpan);
  75. let containerHeight = this.rightDialog.offsetHeight;
  76. let childrenHeight = 0;
  77. for (let i = 0; i < this.rightDialog.childNodes.length; i++) {
  78. childrenHeight += this.rightDialog.childNodes[i].offsetHeight;
  79. }
  80. if (childrenHeight >= containerHeight) {
  81. this.rightDialog.removeChild(this.rightDialog.childNodes[0]);
  82. }
  83. messageSpan.style.opacity = 1;
  84. this.lastMessage = "right";
  85. }
  86. leftClear() {
  87. this.leftDialog.innerHTML = "";
  88. }
  89. rightClear() {
  90. this.rightDialog.innerHTML = "";
  91. }
  92. }