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