Controls.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. module Controls {
  2. export function createBigButton (text : string, index : number, resolve : (t : number) => void) {
  3. let p = document.createElement("p");
  4. p.classList.add("choice");
  5. p.appendChild(document.createTextNode(text));
  6. markButton(p, index, resolve);
  7. return p;
  8. }
  9. export function createSmallButton (text : string, index : number, resolve : (t : number) => void) {
  10. let p = document.createElement("p");
  11. p.classList.add("choice");
  12. p.classList.add("small");
  13. p.appendChild(document.createTextNode(text));
  14. markButton(p, index, resolve);
  15. return p;
  16. }
  17. function markButton (p : HTMLElement, index : number, resolve : (t : number) => void) {
  18. p.addEventListener("click", () => {
  19. resolve(index);
  20. });
  21. Controls.KeyHandler.applyCode(p, Controls.KeyHandler.getFirstKeyCode());
  22. }
  23. export async function giveChoices (big : boolean, ...choices : Array<Say | string>) : Promise<Array<any>> {
  24. let buttons;
  25. let classList = ["choice"];
  26. if (big != true) {
  27. classList.push("small");
  28. }
  29. let chosenPromise = <Promise<number>> new Promise((async (resolve) => {
  30. Controls.KeyHandler.reset();
  31. let say = new Say();
  32. for (let i = 0; i < choices.length; i++) {
  33. let choice = choices[i];
  34. if (choice instanceof Say) {
  35. let button = (await choice.getHTML("p", classList, true))[0];
  36. markButton(button, i, resolve);
  37. say.add(button);
  38. } else {
  39. if (big == true) {
  40. say.add(createBigButton(choice, i, resolve));
  41. } else {
  42. say.add(createSmallButton(choice, i, resolve));
  43. }
  44. }
  45. }
  46. buttons = await say.getHTMLContent();
  47. Elements.CurrentTurnHandler.print(...(buttons));
  48. }));
  49. let chosen = await chosenPromise;
  50. Elements.CurrentTurnHandler.unprint(...buttons);
  51. return [choices[chosen], chosen];
  52. }
  53. export async function askForConsent (question? : string | Say) : Promise<boolean> {
  54. let elements;
  55. if (question != undefined) {
  56. if (typeof question == "string") {
  57. question = new Say(question);
  58. }
  59. elements = await Elements.CurrentTurnHandler.getSayElementsAsContent(question);
  60. Elements.CurrentTurnHandler.print(...elements);
  61. }
  62. let choices = ["Yes", "No"];
  63. let choice = await giveChoices(false, ...choices);
  64. if (elements != undefined) {
  65. Elements.CurrentTurnHandler.unprint(...elements);
  66. }
  67. return choice[1] == 0;
  68. }
  69. }