This set of macros/functions aims to provide an easy way to set up content that is revealed bit-by-bit via user interaction.
| Author | Cyrus Firheir | 
| Website | https://github.com/cyrusfirheir/cycy-wrote-custom-macros/tree/master/click-to-proceed | 
| Story format | SugarCube 2 | 
| Last checked | Wed Feb 24 2021 | 
| License | Unlicense | 
| Download | ctp.zip  | 
This set of macros/functions aims to provide an easy way to set up content that is revealed bit-by-bit via user interaction.
Using nested <<linkreplace>> and <<linkappend>> works, but gets tedious and is often prone to errors. The CTP (Click To Proceed: original-est name ever) macros make it a bit easier by turning them into blocks instead of nests.
If using the Twine desktop/web app, copy contents of click-to-proceed.js to Story JavaScript, and contents of click-to-proceed.css to Story Stylesheet.
If using a compiler like Tweego, drop click-to-proceed.js and click-to-proceed.css to your source folder.
click-to-proceed.twee-config.yaml can also be added to the workspace if using the Twee 3 Language Tools VSCode extension, for macro definitions.
See more examples.
<<ctp "testID">>
  This is the first string.
<<ctpNext clear>>
  Second! It cleared the first one out!
<<ctpNext nobr>>
  Third, but with nobr...
<<ctpNext 2s>>
  The fourth shows up 2 seconds late.
<<ctpNext t8n>>
  And the final one. With a transition!
<</ctp>>
<<link "Next">>
  <<ctpAdvance "testID">>
<</link>>
<<link "Back">>
  <<ctpBack "testID">>
<</link>>
Keywords for controlling behavior:
clear: Clears the content of the previous block. Use for replacing.nobr: Appends content to the same line as the last block instead of going to a new line.t8n or transition: Custom CSS animation based transition (400ms fade-in by default).3s or 250ms) to delay the display of the block by.Additional keywords for individual blocks (<<ctpNext>>, <<ctpHead>>, <<ctpTail>>) in a chain (these are used to break out of behavior set by the <<ctp>> macro):
noClear: Overrides clear and keeps previous blocks in place instead of replacing.br: Overrides nobr and adds a line break before the current block.noT8n or noTransition: Overrides t8n or transition and removes the transition for the current block.<<ctp>> for the current block.redo: When going back up the chain, re-render this block.CTP objectThe CTP custom object is set up as follows:
id: (string) Unique ID.selector: (string) CSS selector to target to output to. When used by the macro, this is the slugified form of id.stack: (array) An array of content objects.head and tail: (object) Contain the <<ctpHead>> and <<ctpTail>> respectively. Structured as content objects.log: (object) Keeps track of blocks and their behaviors:index: (whole number) Current index of block (zero-based).delayed: (boolean) Whether the current block is delayed or not.Example:
var ctpTest = new CTP({
  id: "ctpTest",
  selector: "#ctp-test-id"
});
Each entry in the stack of content (plus the head and tail) is stored in an object structured as follows:
var content = {
  index: 0, // whole number [string for "head" and "tail"]
  clear: false, // boolean
  nobr: false, // boolean
  transition: false, // boolean
  delay: 0, // time in milliseconds
  content: "Actual content to be put out to DOM" // string
}
Back and Next buttons hide when not needed<<ctp "testID">>
  <<set _ctp to CTP.getCTP("testID")>>
  This is the first string.
<<ctpHead>>
  <<if _ctp.log.index gt 0>>
    <<button "Back">>
      <<ctpBack "testID">>
    <</button>>
  <</if>>
<<ctpNext clear>>
  Second! It cleared the first one out!
<<ctpNext nobr>>
  Third, but with nobr..
<<ctpNext 500ms>>
  The fourth shows up half a second late.
<<ctpNext t8n>>
  And the final one. With a transition!
<<ctpTail>>
  <<if _ctp.log.index lt _ctp.stack.length - 1>>
    <<button "Next">>
      <<ctpAdvance "testID">>
    <</button>>
  <</if>>
<</ctp>>
State.variables.ctpTest = new CTP({
  id: "ctpTest",
  selector: "#ctp-test-id"
});
State.variables.ctpTest
  .add("This is the first string.")
  .add("Second! It cleared the first one out!", "clear")
  .add("Third, but with nobr...", "nobr")
  .add("And the final one. With a transition!", "t8n");
In Passage:
<div id="#ctp-test-id">
  <<= $ctpTest.out()>>
</div>
<<link "Advance">>
  <<run $ctpTest.advance()>>
  <!-- Because $ctpTest was created manually, using the <<ctpAdvance>>
    macro won't work. To be able to use <<ctpAdvance>>, the CTP object
    needs to be set as a property of State.variables["#macro-ctp-dump"]
    as that is what is used internally to store CTP objects created via
    the macros. -->
<</link>>
Demo Twee code:
:: Start <<ctp "testID">> Click next to show a new line of text. <<ctpNext>> Click again to reveal the next line... after 2 seconds. <<ctpNext 2s>> Now click again to reveal more text in this line. <<ctpNext nobr>> The next one will clear the text. <<ctpNext clear>> That is all. <</ctp>> <<link "Next">> <<ctpAdvance "testID">> <</link>> <<link "Back">> <<ctpBack "testID">> <</link>>