From 4686bbf61593f70b7456c8becbc4dc381bf088fb Mon Sep 17 00:00:00 2001 From: Arkerthan <arkerthan@gmail.com> Date: Sat, 16 May 2020 22:29:23 +0200 Subject: [PATCH] basic implementation for history and auto generated quick links --- src/gui/quicklinks.js | 106 +++++++++++++++++++++++++++ src/gui/storyCaption.tw | 2 + src/pregmod/managePersonalAffairs.tw | 2 +- src/uncategorized/main.tw | 2 +- src/uncategorized/manageArcology.tw | 2 +- src/uncategorized/managePenthouse.tw | 2 +- src/uncategorized/options.tw | 2 +- src/uncategorized/slaveInteract.tw | 2 +- 8 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 src/gui/quicklinks.js diff --git a/src/gui/quicklinks.js b/src/gui/quicklinks.js new file mode 100644 index 00000000000..6a3eacbc410 --- /dev/null +++ b/src/gui/quicklinks.js @@ -0,0 +1,106 @@ +App.UI.quickMenu = (function() { + // setup safe passages + const jumpFrom = Story.lookup("tags", "jump-from-safe").map(passage => passage.title); + const jumpTo = Story.lookup("tags", "jump-to-safe").map(passage => passage.title); + + // setup hotkeys list + const hotkeys = cleanHotkeys({ + Main: "m", + "Manage Personal Affairs": "x" + }); + + let hotkeysEnabled = false; + + // register hotkeys + for (const passage in hotkeys) { + Mousetrap.bind(hotkeys[passage], () => { + if (hotkeysEnabled) { + Engine.play(passage); + } + }); + } + + // setup history + let history = []; + let currentPassage; + let historyNavigation = false; + $(document).on(':passageend', function() { + if (currentPassage === State.passage) { + // only reloaded page + return; + } + + // if navigated here normally, add passage to history, otherwise remove last entry from history + if (!historyNavigation) { + // if last passage can be jumped to add passage to history, otherwise clear history + if (jumpTo.includes(currentPassage)) { + history.push(currentPassage); + } else { + history = []; + } + } else { + historyNavigation = false; + history.shift(); + } + currentPassage = State.passage; + }); + Mousetrap.bind("backspace", () => { + // jump back in history + if (history.length > 0 && jumpFrom.includes(State.passage)) { + historyNavigation = true; + Engine.play(history[0]); + } + }); + + function generateMenu() { + if (!jumpFrom.includes(State.passage)) { + hotkeysEnabled = false; + return ""; + } + + const fragment = document.createDocumentFragment(); + + for (let i = 0; i < jumpTo.length; i++) { + if (jumpTo[i] !== State.passage) { + fragment.append(generatePassageLink(jumpTo[i])); + } + } + + hotkeysEnabled = true; + return fragment; + } + + function generatePassageLink(passage) { + const div = document.createElement("div"); + const a = document.createElement("a"); + a.append(passage); + a.onclick = () => { + Engine.play(passage); + }; + div.append(a); + if (hotkeys[passage]) { + div.append(" ", App.UI.DOM.makeElement("span", `[${hotkeys[passage]}]`, "hotkey")); + } + return div; + } + + /** + * Cleans out all keys that are not contained in jumpTo and therefore not considered safe. + * + * @param {object} keys + * @returns {object} + */ + function cleanHotkeys(keys) { + const result = {}; + for (const key in keys) { + if (jumpTo.includes(key)) { + result[key] = keys[key]; + } else { + console.log("Found hotkey to unsafe passage: " + key); + } + } + return result; + } + + return generateMenu; +})(); diff --git a/src/gui/storyCaption.tw b/src/gui/storyCaption.tw index 9f9122d65aa..0872e886510 100644 --- a/src/gui/storyCaption.tw +++ b/src/gui/storyCaption.tw @@ -1,5 +1,7 @@ :: StoryCaption [nobr] +<<includeDOM App.UI.quickMenu()>> + <<set _Pass = passage()>> <<if $ui != "start">> <<userButton>> <br> <</if>> diff --git a/src/pregmod/managePersonalAffairs.tw b/src/pregmod/managePersonalAffairs.tw index d60db903217..3b36ea4884a 100644 --- a/src/pregmod/managePersonalAffairs.tw +++ b/src/pregmod/managePersonalAffairs.tw @@ -1,4 +1,4 @@ -:: Manage Personal Affairs [nobr] +:: Manage Personal Affairs [nobr jump-to-safe jump-from-safe] <<set $nextButton = "Back", $nextLink = "Main", $encyclopedia = "Being in Charge">> <<run PCTitle()>> diff --git a/src/uncategorized/main.tw b/src/uncategorized/main.tw index 44bfce82fbb..7df482f44fe 100644 --- a/src/uncategorized/main.tw +++ b/src/uncategorized/main.tw @@ -1,4 +1,4 @@ -:: Main [nobr] +:: Main [nobr jump-to-safe jump-from-safe] /* make sure the first thing that happens is the error check */ <<set _errors = App.MainView.errors()>> diff --git a/src/uncategorized/manageArcology.tw b/src/uncategorized/manageArcology.tw index c197b19c1e8..a3f4578e91d 100644 --- a/src/uncategorized/manageArcology.tw +++ b/src/uncategorized/manageArcology.tw @@ -1,4 +1,4 @@ -:: Manage Arcology [nobr] +:: Manage Arcology [nobr jump-to-safe jump-from-safe] <<set $nextButton = "Back", $nextLink = "Main", $encyclopedia = "The X-Series Arcology">> diff --git a/src/uncategorized/managePenthouse.tw b/src/uncategorized/managePenthouse.tw index f7e0b11df9e..a12ed7c07e5 100644 --- a/src/uncategorized/managePenthouse.tw +++ b/src/uncategorized/managePenthouse.tw @@ -1,4 +1,4 @@ -:: Manage Penthouse [nobr] +:: Manage Penthouse [nobr jump-to-safe jump-from-safe] <<set $nextButton = "Back", $nextLink = "Main", $encyclopedia = "What the Upgrades Do">> diff --git a/src/uncategorized/options.tw b/src/uncategorized/options.tw index d48ba7a26b4..cb985f3f58f 100644 --- a/src/uncategorized/options.tw +++ b/src/uncategorized/options.tw @@ -1,4 +1,4 @@ -:: Options [nobr] +:: Options [nobr jump-to-safe jump-from-safe] <style> .subHeading { diff --git a/src/uncategorized/slaveInteract.tw b/src/uncategorized/slaveInteract.tw index 1ea450e1874..30529127934 100644 --- a/src/uncategorized/slaveInteract.tw +++ b/src/uncategorized/slaveInteract.tw @@ -1,4 +1,4 @@ -:: Slave Interact [nobr] +:: Slave Interact [nobr jump-from-safe] <<set $nextButton = "Confirm changes", $nextLink = "Main">> <<set _SL = $slaves.length, _CL = $canines.length, _HL = $hooved.length, _FL = $felines.length>> -- GitLab