Controls.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 () : Promise<boolean> {
  54. let choices = ["Yes", "No"];
  55. let choice = await giveChoices(false, ...choices);
  56. return choice[1] == 0;
  57. }
  58. }