Skip to content

Instantly share code, notes, and snippets.

@ChapelR
Last active December 26, 2019 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChapelR/4292fcb4981a767065cf77df7c92fa72 to your computer and use it in GitHub Desktop.
Save ChapelR/4292fcb4981a767065cf77df7c92fa72 to your computer and use it in GitHub Desktop.
Continue macros.

The Continue Macro Set

Macro: <<cont>>

Syntax: <<cont [keywords]>>...<</cont>>

This macro waits for user input before evaluating its contents, and can be used to create situations where the user can click anywhere, or press any key (with the right keywords), to continue.

Arguments:

  • keywords: by default, the contents of the macro are executed silently, like a link. You can pass the macro the keyword append to cause it to instead display it's content in place. By default only mouse clicks will activate the macro, but you can use the keyword keypress to make any keypress also activate the macro.

Examples:

/* click anywhere to go to the next passage */
<<cont>><<goto 'Next'>><</cont>>

/* click anywhere or press any key to continue */
You slowly turn around...
<<cont append keypress>>The ghost is behind you!<</cont>>

Macro: <<ignore>>

Syntax: <<ignore selectorList>>

By default clicks on any link or button elements or any elements with the role="button" attribute, and clicks on dialogs or the sidebar will not trigger the <<cont>> macro. Additionally, any element with the class continue-macro-ignore will also be ignored. You can use this macro to add more selectors to the ignore list. Note that this macro can only be triggered before the story starts—for example, from the StoryInit special passage.

Arguments:

  • selectorList: a list of jQuery-style selectors.

Examples:

/* ignore <i> elements */
<<ignore 'i'>>

/* ignore elements with the classes 'click' or 'hello' */
<<ignore '.click' '.hello'>>

Function: cont()

Syntax: cont(keypress, callback) or setup.cont(keypress, callback)

The cont() function can be used to create a similar effect to the continue macro from JavaScript.

Arguments:

  • keypress (boolean): whether keypresses should activate the continue effect. Similar in function to the keypress keyword of the <<cont>> macro.
  • callback (function): the function to run when the continue effect is triggered.

Function: cont.ignore()

Syntax: cont.ignore(selectorList) or setup.cont.ignore(selectorList)

Adds to the ignore list just like <<ignore>>. Must be used before the story starts.

Arguments:

  • selectorList (string|string array): a list of jQuery-style selectors.

Function: cont.reset()

Syntax: cont.reset(selectorList) or setup.cont.reset(selectorList)

This function removes all current continue event handlers, and can be used to add to the ignore list if supplied with appropriate arguments.

Arguments:

  • selectorList (string|string array): a list of jQuery-style selectors.
(function () {
'use strict';
// selectors to ignore
var ignored = ['a', ':button', '*[role="button"]', '.continue-macro-ignore', '#ui-bar', '#ui-dialog'];
function ignoreMe () {
$(document).on('click.continue-macro keyup.continue-macro', ignored.join(', '), function (ev) {
ev.stopPropagation();
});
}
function addIgnore () {
if (State.length > 0) {
return false;
}
var args = [].slice.call(arguments).flatten();
ignored = ignored.concat(args);
return true;
}
$(document).one(':passagerender', function () {
ignoreMe();
});
// continue functions
function cont (press, cb) {
if (!cb || typeof cb !== 'function') {
return;
}
var events = 'click.continue-macro';
if (press) {
events = events + ' keyup.continue-macro';
}
$(document).one(events, function () {
cb();
});
}
function reset () {
var args = [].slice.call(arguments).flatten();
ignored = ignored.concat(args);
$(document).off('click.continue-macro keyup.continue-macro');
ignoreMe();
}
// macros
// <<ignore selectors...>>
Macro.add('ignore', {
handler : function () {
var check = addIgnore(this.args);
if (!check) {
return this.error('the <<ignore>> macro should only be run from StoryInit or equivalent.');
}
}
});
// <<cont [append] [press]>>Code<</cont>>
Macro.add('cont', {
tags : null,
handler : function () {
var append = this.args.includes('append'),
press = this.args.includesAny('key', 'keypress', 'press', 'button'),
self = this,
wiki = self.payload[0].contents;
cont(press, function () {
if (append) {
$(self.output).wiki(wiki);
} else {
$.wiki(wiki);
}
});
}
});
// APIs
setup.cont = cont;
setup.cont.ignore = addIgnore;
setup.cont.reset = reset;
window.cont = window.cont || setup.cont;
}());
// continue.min.js, for SugarCube 2, by Chapel
;!function(){"use strict";var t=["a",":button",'*[role="button"]',".continue-macro-ignore","#ui-bar","#ui-dialog"];function o(){$(document).on("click.continue-macro keyup.continue-macro",t.join(", "),function(n){n.stopPropagation()})}function n(){if(0<State.length)return!1;var n=[].slice.call(arguments).flatten();return t=t.concat(n),!0}function e(n,t){if(t&&"function"==typeof t){var o="click.continue-macro";n&&(o+=" keyup.continue-macro"),$(document).one(o,function(){t()})}}$(document).one(":passagerender",function(){o()}),Macro.add("ignore",{handler:function(){if(!n(this.args))return this.error("the <<ignore>> macro should only be run from StoryInit or equivalent.")}}),Macro.add("cont",{tags:null,handler:function(){var n=this.args.includes("append"),t=this.args.includesAny("key","keypress","press","button"),o=this,c=o.payload[0].contents;e(t,function(){n?$(o.output).wiki(c):$.wiki(c)})}}),setup.cont=e,setup.cont.ignore=n,setup.cont.reset=function(){var n=[].slice.call(arguments).flatten();t=t.concat(n),$(document).off("click.continue-macro keyup.continue-macro"),o()},window.cont=window.cont||setup.cont}();
// end continue.min.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment