Add-ons / Speech Box System

This set of macros, styles, and functions can be used to quickly generate simple speech boxes for character dialogue with a place for portraits, names, and text.

Author ChapelR
Website https://twinelab.net/custom-macros-for-sugarcube-2/#/speech-box-system
Story format SugarCube 2
Last checked Thu Jul 21 2022
License Unlicense
Download speech.zip

Index

Overview

This set of macros, styles, and functions can be used to quickly generate simple speech boxes for character dialogue with a place for portraits, names, and text. Simply associate characters with image sources in your StoryInit passage or story JavaScript and the script will generate purpose-built macros at runtime, or you can supply character names and image sources to the <<say>> macro in any given passage for on-the-fly characters.

Speech box example

The default look and styling is very bare bones, and meant to be a basic starting point. It is highly recommended you add your own styles to make the speech boxes better match your game. You can, of course, use them as-is if you want to, though.

📝 Note: Unlike most of my macros, this code relies on CSS files as well as JavaScript! Be sure to download and install the relevant CSS files as well!

⚠️ Warning: If you want to use this system with the typed.js integration module, you'll need to use this custom JavaScript in place of that provided by the module: https://gist.github.com/tmedwards/4921bdcd7bfbb955530c135ee3feff83

Macros

<<character>>

<<character>>

Syntax: <<character name [displayname] imageSrc>>

This macro associates a character name, which will automatically become a macro, with an image resourcem provided as a URL (can be relative or absolute). This macro is essentially a macro factory—it creates other macros based on the arguments you pass. This means that the name argument must therefore follow the parameters of normal macros: it must not contain spaces or special characters outside what macro names ordiarily allow. If you need character names without these limitations, you can use the <<say>> macro (see below).

⚠️ Attention: This macro will only function if run before the story starts, that is before the first passage is loaded. The StoryInit special passage is therefore the best place to define characters.

Arguments:

Usage:

/* creating characters and associating them with images (goes in StoryInit) */
<<character 'lisa' 'images/portraits/lisa.jpg'>>
<<character 'bob' 'https://some.website.net/url/to/an/image_file.png'>>
<<character 'billy' 'data:image/jpg;base64, [base64 encoded data]'>>
<<character 'maxine' '???' 'images/portraits/unkown-person.jpg'>>
/* using the generated macros in passages */
<<lisa>>Hey there!<</lisa>>
<<maxine>>Do i know you?<</maxine>>
<<billy>>You just use the name you passed into the {{{<<character>>}}}
macro as its own macro to create speech boxes!<</billy>>

READ: Documentation

<<say>>

<<say>>

Syntax: `<<say name [imageSrc]>>...<>`` This macro can be used to create speech boxes that aren't based on predefined characters, allowing you to use names that would be unsuitable as macro names, use bit characters that aren't worth defining, or change a character's name or image, or use characters that don't have associated images. Arguments:

<<say 'Lisa' 'images/portraits/lisa.jpg'>>Hey there!<</say>>
/* without an image */
<<say 'Some Guy'>>You n'wah!<</say>>

READ: Documentation

JavaScript API

setup.addCharacter()

setup.addCharacter()

Syntax: setup.addCharacter(name, [displayname,] imageSrc)

This function serves the same purpose as the <<character>> macro, and creates macros in the exact same way.

Returns: nothing.

Arguments:

Usage:

var $speechBox = setup.say('#passages', 'Lisa', 'Hey there!', 'images/portraits/lisa.jpg');

setup.say()

setup.say()

Syntax: setup.say(outputElement, name [, content] [, imageSrc])

This function is roughly equivalent to the <<say>> macro, and can be used to create speech boxes from arguments in a similar way, and then append the box to the indicated output element.

Returns: A reference to the jQuery-wrapped speech box element.

Arguments:

Usage:

var $speechBox = setup.say('#passages', 'Lisa', 'Hey there!', 'images/portraits/lisa.jpg');

Other usage notes

Styling options

The generated HTML structure of the speech box element looks something like this:

<div class="say">
    <img src="[imageSrc]">
    <p>[Name]</p>
    <p>[Content]</p>
</div>

The .say <div> is also given a class based on the slugified name given to the <<say>> or generated macro, so you can use per-character styles.

Selectors you may want to target:

Live demo

Demo Twee code:

:: Start
<<say Lisa>>Meow!<</say>>

:: StoryInit
<<character 'Lisa' 'https://loremflickr.com/120/120'>>