tmedwards / sugarcube-2

SugarCube is a free (gratis and libre) story format for Twine/Twee.
https://www.motoslave.net/sugarcube/2/
BSD 2-Clause "Simplified" License
185 stars 42 forks source link

Add a teletype/typewriter macro #50

Closed tmedwards closed 4 years ago

tmedwards commented 4 years ago

Add a teletype/typewriter macro that handles most content, rather than being limited to strings—e.g., as Typed.js is. Invocations within the same passage should coordinate—i.e., they should be processed in order of appearance, rather than all attempting to type at once.

The prototype would be something like:

<<type speed [start delay] [keep|none]>> … <</type>>

E.g.,

<<type 40ms>>
    Type characters from this content every 40 milliseconds.  Including [[links]] and ''other markup''!
<</type>>

<<type 40ms start 2s>>
    Type characters from this content every 40 milliseconds, starting after a 2 second delay.
<</type>>

<<type 40ms keep>>
    Type characters from this content every 40 milliseconds, keeping the cursor after typing is complete.
<</type>>

<<type 40ms none>>
    Type characters from this content every 40 milliseconds, not using a cursor at all.
<</type>>

EDIT: Updated prototype and examples:

greyelf commented 4 years ago

How exactly would the Typing processor handle HTML elements embedded within the 'text content'?

tmedwards commented 4 years ago

It types them out. Specifically:

  1. On startup, it consumes the text of the given source node and, recursively, all descendant nodes and their text, if any, leaving the source node empty of text and descendant nodes.
  2. When processing begins, it moves the source node's text back a code point at a time and then each of the descendant nodes and their text a code point at a time.

By "text" in the above I mean anything with a .nodeValue—e.g., text nodes, comments, CDATA sections.

Because it uses the original nodes, no handlers or anything else should be lost, so even interactive content should survive processing. Tested with the link markup, <<link>> macros, and a few other things.

Additionally. There's some handling of special cases, like nodes that have been set to display: none;, which are simply zipped through without delay.

EDIT: Minor clarification.

greyelf commented 4 years ago

And what about styling related markup/elements? (bold, colour, etc...)

tmedwards commented 4 years ago

Elements are nodes, so they get typed out per my last reply. Well, they get appended to their original parent and their text content, if any, is typed out.