-
Notifications
You must be signed in to change notification settings - Fork 20
Examples
TypingLabels are a drop-in replacement for regular Scene2D Labels, so all you need is to change your Labels to TypingLabels and the library will be working already.
// BEFORE
Label label = new Label("Hello world!", skin);
stage.add(label);
// AFTER
TypingLabel label = new TypingLabel("Hello world!", skin);
stage.add(label);You can also use Tokens to modify the text's behavior. See the Tokens page to learn more about them.
// Create some text with tokens
String text = "{COLOR=GREEN}Hello,{WAIT} world!"
+ "{COLOR=ORANGE}{SLOWER} Did you know orange is my favorite color?";
// Create a TypingLabel instance with your custom text
TypingLabel label = new TypingLabel(text, skin);
// Add the actor to any widget like you normally would
stage.add(label);Variable tokens are replaced by any values you assign at runtime. e.g. you can create a text Hello {VAR=name}! and set the variable value with label.setVariable("name", "Mr. Bob").
// Create some text with tokens
String text = "Would you like to buy {VAR=item} for only {VAR=price}?";
// Create a TypingLabel instance with your custom text
TypingLabel label = new TypingLabel(text, skin);
// Optionally assign variables to replace {VAR} tokens
label.setVariable("item", "an Obsidian Sword");
label.setVariable("price", "25,000 coins");
// Add the actor to any widget like you normally would
stage.add(label);Alternatively you can replace replace tokens by using a listener call if you need more flexibility:
String text = "Would you like to buy {VAR=item} for only {VAR=price}?";
TypingLabel label = new TypingLabel(text, skin);
// Create a TypingListener to listen to variable replacements
label.setTypingListener(new TypingAdapter() {
public String variableReplacement (String variable) {
if("item".equals(variable)) return "an Obsidian Sword";
if("price".equals(variable)) return "{SHAKE}25,000 coins{ENDSHAKE}";
return "-- Unknown Variable --";
}
});
stage.add(label);It's also possible to have global variables, a good example would be the player's name:
// Register the global variable when your game starts.
TypingConfig.GLOBAL_VARS.put("player", "Bob");
// Create some text with tokens
String text = "Welcome back {VAR=player}, how are you doing?";
// Create a TypingLabel instance with your custom text
TypingLabel label = new TypingLabel(text, skin);If Conditionals are replaced by values provided within the token parameters themselves. This token has a complex syntax, so lets break it down first:
Usage:
{IF=name;key1=value1;key2=value2;key3=value3;defaultValue}
- name: Name of the variable to be checked against the parameter keys.
- keys: If the variable's value is equal to this key...
- values: Then this value will be used to replace the token.
- defaultValue: Or else, an optional default value (without a key) is used instead.
- First, TypingLabel gets the value of the variable you define in the
nameparameter, exactly like {VAR} tokens. - Then it iterates through the parameters, which have the
key=valuesyntax. - If the variable's value is equal to the key (case is ignored), then its value is picked to replace the token.
- Default values can be set as well, just add a parameter without keys, preferably at the last slot.
// Create label
String text = "Have you seen {IF=GENDER;f=her;m=him;t=them} recently? I've heard {IF=GENDER;f=she's;m=he's;t=they're} doing well.";
TypingLabel label = new TypingLabel(text, skin);
// Assign variables to be processed by {IF} tokens (also affect {VAR} tokens)
label.setVariable("gender", "f");
// Final Text:
// Have you seen her recently? I've heard she's doing well.// Create label
String text = "I'm currently {IF=profession;gamedev=a game developer;unemployed}.";
TypingLabel label = new TypingLabel(text, skin);
// Assign variables to be processed by {IF} tokens (also affect {VAR} tokens)
label.setVariable("profession", "gamedev");
// Final Text:
// I'm currently a game developer.The real power of If Conditionals come in play when you have to translate your text to multiple languages. (See Internationalization and Localization)
Lets say your game allows the player to choose the character's gender, and you want your dialogue texts to reflect that choice.
Here's a couple of .properties files with the same entry in multiple languages:
# Main_en.properties (English)
have_you_seen=Have you seen {IF=GENDER;f=her;m=him;t=them} today?
give_back=I've been meaning to give back their umbrella.
# Main_pt_BR.properties (Brazilian Portuguese)
have_you_seen=Você viu {IF=GENDER;f=ela;m=ele;t=elu} hoje?
give_back=Eu preciso devolver o guarda-chuvas {IF=GENDER;f=dela;m=dele;t=delu}.Notice how the give_back string in English doesn't need any pronouns, but it does in Portuguese.
Any parameter that doesn't have a key is considered a default value, and is cached while TypingLabel processes the token. If no parameter keys match the variable's value, the default key is returned.
-
If no default values are defined, the variable itself is used.
GENDER is null TEXT: Have you seen {IF=GENDER;f=her;m=him;t=them} recently? RESULT: Have you seen GENDER recently? -
Define a string as the default value.
PROFESSION is null TEXT: I'm {IF=PROFESSION;gamedv=a game developer;unemployed} now. RESULT: I'm unemployed now. -
Define a string as the default value, omitting the key but keeping the equal sign.
PROFESSION is null TEXT: I'm {IF=PROFESSION;gamedev=a game developer;=unemployed} now. RESULT: I'm unemployed now. -
Define an empty string as the default value. The equal sign is required in this case.
GENDER is null TEXT: My pronouns are: {IF=GENDER;f=she/her;m=he/him;t=them/their;=} RESULT: My pronouns are: -
Minimal syntax.
GENDER is null TEXT: Have you seen {IF=GENDER;Charmander} recently? RESULT: Have you seen Charmander recently?
You can use event tokens to be notified when that exact part of the text is processed. Really handy if you want to spawn fire particles or play a sound when a certain word appears.
To use events, simply put an event token in the middle of the text and set a listener to the label.
// Create text with events
String text = "{SPEED=0.25}This is an {EVENT=event_name}example of "
+ "how to {EVENT=spawn_fire}fire {EVENT=play_sound}events!";
TypingLabel label = new TypingLabel(text, skin);
// Create a TypingListener or TypingAdapter to listen to events as they're fired
label.setTypingListener(new TypingAdapter() {
public void event (String event) {
System.out.println("Received text event: " + event);
}
public void end () {
System.out.println("This is called when the text reaches the end.");
}
});