1
1

Elements.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /// <reference path="../World/MachineBegins.ts" />
  2. /// <reference path="../World/TurnSequence.ts" />
  3. module Elements {
  4. export var animationTime = 1000;
  5. export var isMobile = /Mobi/i.test(navigator.userAgent); // This correctly lets Mobile users who requested the Desktop app or are on a sufficiently big tablet to get the right desktop version
  6. export var screenWidth = document.body.clientWidth;
  7. export var screenHeight = document.body.clientHeight;
  8. export function getLinkElements (image? : boolean) {
  9. if (CurrentTurnHandler == undefined) return [];
  10. if (CurrentTurnHandler.isTurn() || image) {
  11. return [CurrentTurnHandler.currentTurnTab];
  12. } else {
  13. return [HyperlinkHandler.linkedActionsTab, RoomHandler.currentRoomTab, HyperlinkHandler.commonActionsTab, CurrentTurnHandler.currentTurnTab, RoomHandler.currentRoomExits];
  14. }
  15. }
  16. export function updateScreenSize () {
  17. screenWidth = document.body.clientWidth;
  18. screenHeight = document.body.clientHeight;
  19. }
  20. export var mainPage = document.getElementById("mainPage");
  21. /**
  22. * Adds the blur effect to the screen
  23. */
  24. export function startTurn () {
  25. mainPage.classList.add("turn");
  26. }
  27. /**
  28. * Remove the blur effect from the screen
  29. */
  30. export function endTurn () {
  31. mainPage.classList.remove("turn");
  32. }
  33. /**
  34. * Are we currently in a turn?
  35. * @returns {boolean}
  36. */
  37. export function isInTurn () {
  38. return mainPage.classList.contains("turn");
  39. }
  40. let menuOutsideofTurn = false;
  41. let menus = 0;
  42. export function startMenu () {
  43. if (!isInTurn()) {
  44. menuOutsideofTurn = true;
  45. startTurn();
  46. }
  47. mainPage.classList.add("mainmenu");
  48. menus++;
  49. Elements.CurrentTurnHandler.clear();
  50. Controls.KeyHandler.reset();
  51. }
  52. export function endMenu () {
  53. Elements.CurrentTurnHandler.clear();
  54. if (--menus == 0) {
  55. if (menuOutsideofTurn) {
  56. endTurn();
  57. }
  58. resetMenus();
  59. }
  60. }
  61. export function resetMenus () {
  62. if (menus != 0) {
  63. console.error("A menu wasn't finished properly. Please correct the dialogue.");
  64. }
  65. menus = 0;
  66. menuOutsideofTurn = false;
  67. mainPage.classList.remove("mainmenu");
  68. }
  69. export function clearMainScreen () {
  70. Elements.CurrentTurnHandler.clear();
  71. }
  72. let loadingScreen = document.getElementById("loadingScreen");
  73. export function stopLoading () {
  74. document.body.removeChild(loadingScreen);
  75. }
  76. export async function waitForAnyKey () {
  77. let say = new Say(Say.CENTERED, new SayBold("Press any key to continue."));
  78. let elements = await say.getHTML("p", ["content"]);
  79. Elements.CurrentTurnHandler.print(...elements);
  80. await Controls.KeyHandler.getNextKey();
  81. Elements.CurrentTurnHandler.unprint(...elements);
  82. }
  83. export async function waitForSpaceKey () {
  84. let say = new Say(Say.CENTERED, new SayBold("Press Space to continue."));
  85. let elements = await say.getHTML("p", ["content"]);
  86. Elements.CurrentTurnHandler.print(...elements);
  87. let lastKey = "notSpace";
  88. while (lastKey != "Space" && lastKey != "Click") {
  89. lastKey = (await Controls.KeyHandler.getNextKey()).evCode;
  90. }
  91. Elements.CurrentTurnHandler.unprint(...elements);
  92. }
  93. export function printObeliskLogo () {
  94. let say = new Say(Say.CENTERED, new SayImage("introLogo"), Say.LINE_BREAK, new SayItalic("The Obelisk is an adult interactive fiction game set in a post-apocalyptic world ravaged by a magical structure."));
  95. Elements.CurrentTurnHandler.printAsContent(say);
  96. }
  97. }
  98. if (Elements.isMobile) {
  99. Elements.mainPage.classList.add("mobile");
  100. }
  101. window.addEventListener("resize", function () {
  102. Elements.updateScreenSize();
  103. });
  104. module MachineBegins {
  105. export let HideLoadingDivRule = MachineBegins.rulebook.createAndAddRule({
  106. name : "Hide loading div",
  107. code : runner => {
  108. Elements.stopLoading();
  109. }
  110. });
  111. }
  112. module TurnSequence {
  113. export let LazilyFixMenus = TurnSequence.rulebook.createAndAddRule({
  114. name : "Fix menus that were never closed because the dev is stupid",
  115. firstPriority : Rule.PRIORITY_LOWEST,
  116. priority : Rule.PRIORITY_LOWEST,
  117. code : () => {
  118. Elements.resetMenus();
  119. }
  120. })
  121. }