Elements.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 a pornographic interactive fiction game set in a post-apocalyptic world ravaged by a magical structure."));
  95. Elements.CurrentTurnHandler.printAsContent(say);
  96. }
  97. export function getStyle (name : string) {
  98. try {
  99. for (var i = 0; i < document.styleSheets.length; i++) {
  100. var rules = document.styleSheets[i]['rules'] || document.styleSheets[i]['cssRules'];
  101. for (var x in rules) {
  102. if (typeof rules[x].selectorText == 'string' && rules[x].selectorText == name) {
  103. return rules[x].style;
  104. }
  105. }
  106. }
  107. } catch (e) {
  108. console.warn("Can't read CSS styles", e);
  109. return {};
  110. }
  111. }
  112. }
  113. if (Elements.isMobile) {
  114. Elements.mainPage.classList.add("mobile");
  115. }
  116. window.addEventListener("resize", function () {
  117. Elements.updateScreenSize();
  118. });
  119. module MachineBegins {
  120. export let HideLoadingDivRule = MachineBegins.rulebook.createAndAddRule({
  121. name : "Hide loading div",
  122. code : runner => {
  123. Elements.stopLoading();
  124. }
  125. });
  126. }
  127. module TurnSequence {
  128. export let LazilyFixMenus = TurnSequence.rulebook.createAndAddRule({
  129. name : "Fix menus that were never closed because the dev is stupid",
  130. firstPriority : Rule.PRIORITY_LOWEST,
  131. priority : Rule.PRIORITY_LOWEST,
  132. code : () => {
  133. Elements.resetMenus();
  134. }
  135. })
  136. }