123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- module Controls {
- export function createBigButton (text : string, index : number, resolve : (t : number) => void) {
- let p = document.createElement("p");
- p.classList.add("choice");
- p.appendChild(document.createTextNode(text));
- markButton(p, index, resolve);
- return p;
- }
- export function createSmallButton (text : string, index : number, resolve : (t : number) => void) {
- let p = document.createElement("p");
- p.classList.add("choice");
- p.classList.add("small");
- p.appendChild(document.createTextNode(text));
- markButton(p, index, resolve);
- return p;
- }
- function markButton (p : HTMLElement, index : number, resolve : (t : number) => void) {
- p.addEventListener("click", () => {
- resolve(index);
- });
- Controls.KeyHandler.applyCode(p, Controls.KeyHandler.getFirstKeyCode());
- }
- export async function giveChoices (big : boolean, ...choices : Array<Say | string>) : Promise<Array<any>> {
- let buttons;
- let classList = ["choice"];
- if (big != true) {
- classList.push("small");
- }
- let chosenPromise = <Promise<number>> new Promise((async (resolve) => {
- Controls.KeyHandler.reset();
- let say = new Say();
- for (let i = 0; i < choices.length; i++) {
- let choice = choices[i];
- if (choice instanceof Say) {
- let button = (await choice.getHTML("p", classList, true))[0];
- markButton(button, i, resolve);
- say.add(button);
- } else {
- if (big == true) {
- say.add(createBigButton(choice, i, resolve));
- } else {
- say.add(createSmallButton(choice, i, resolve));
- }
- }
- }
- buttons = await say.getHTMLContent();
- Elements.CurrentTurnHandler.print(...(buttons));
- }));
- let chosen = await chosenPromise;
- Elements.CurrentTurnHandler.unprint(...buttons);
- return [choices[chosen], chosen];
- }
- export async function askForConsent (question? : string | Say) : Promise<boolean> {
- let elements;
- if (question != undefined) {
- if (typeof question == "string") {
- question = new Say(question);
- }
- elements = await Elements.CurrentTurnHandler.getSayElementsAsContent(question);
- Elements.CurrentTurnHandler.print(...elements);
- }
- let choices = ["Yes", "No"];
- let choice = await giveChoices(false, ...choices);
- if (elements != undefined) {
- Elements.CurrentTurnHandler.unprint(...elements);
- }
- return choice[1] == 0;
- }
- }
|