A symbol-by-symbol text writer for Defold Engine. Developed for rich game dialogs. Support UTF-8 symbols (via utf-8.lua). So Russian and other 2+ bytes symbol languages are supported.
Add library to dependency:
https://github.com/Insality/defold-printer/archive/master.zip
Place printer template on you gui scene. Setup font of text and set size of printer background.
Text will appearing from top-left of this background. You can setup alpha of this node to see text area.
setup in gui module:
local printer = require "printer.printer"
function init(self)
self.printer = printer.new(self, template_name)
end
function update(self, dt)
printer.update(self)
end
function final(self)
printer.final(self)
end
And usage will be like this:
self.printer:print("This is just simple text")
Style is lua table with parameters. Default style look like:
default = {
font_height = 28,
spacing = 1,
scale = 1,
waving = false,
color = "#FFFFFF",
speed = 0.05,
appear = 0.01,
shaking = 0,
sound = false,
can_skip = true,
shake_on_write = 0,
}
font_height
in pixels, height of every symbol or imagespacing
in pixels, distance between symbolsscale
set scale of every symbol to this valuewaving
set true, to enable waving symbol effectcolor
string in hex, like "#CACACA". Set color of every text symbolspeed
in seconds. The speed of text typingappear
in seconds. The speed of symbol appearing. Via gui.animate from alpha 0 -> 1shaking
in pixels. Set value > 0 to enable shaking of every symbolsound
string. Name of sound, what will be played, when text is start appearing. Need to rewrite printer.play_sound function.can_skip
if false, printer.instant_appear will not work, while text with this style is appearingshake_on_write
when true, shake all text symbols when any symbol is start appearing
To setup your styles, use printer.add_styles( {styles} )
. Styles is array of lua-table with style parameters.
By default, all new print text have default style. To change it, you need point needed style like this:
{my_style}This is styled text
You can reset style to default by using {/}
. Example:
{my_style}This is styled text. {/}But this no
You can use {n}
to make new line. Example:
This row on first line.{n}This line on second line
You can mix many styles in one row. Example:
{Illidan_name}Illidan{/}: you are {red}not {waving}prepared{/}!
To handle next behavior, like to appear all text if it printing, or go to next text if it already showed, you can use something like this on touch event:
if self.printer.is_print then
self.printer:instant_appear()
else
self.index = self.index + 1
if self.index <= #self.texts then
self.printer:print(self.texts[self.index])
end
end
You can predefine styles for special sources. It will become default style for current text. For example, if your setup source style:
printer.add_source_style("bob", "bob_style")
add call next:
printer:print("Any text you want", "bob")
Instead of default style, it will be printed with style bob_style
You can predefine styles for special word. For example, if you setup word style:
printer.add_word_style("powerful", "cool_style")
and will print next text: The Defold is amazing, powerful engine
It will auto apply style and will looks similar to:
The Defold is amazing, {cool_style}powerful{/} engine
Point: the word is case sensitive. You can use it for coloring characters name in your game. The word style will extend default style of current text.
Point: It will work with source predefined styles. Just extend default style of source predefined text.
You can insert images in your text via {image:name}
syntax.
It will place image node and call gui.play_flipbook(node, name)
to this
Example:
printer:print("Lets trade this for 500 {image:coins}!)
You can create multiply printer instances with printer.new, using different templates
self.printer:print("This is {red}test with red style")
self.printer:print("This is {amazing}multi-{blue}styled text")
self.printer:print("This is text with{n}new line. And image here {image:coins}")
self.printer:print("This is {red}red text and {/}return to default")
self.printer:print("This is with source to use another default style", "Illidan")
Create new printer instance to use it for print text
self
gui self contextprefab_name
printer template name of gui scene
Start printing text on selected instance
instance
printer instance, created by printer.newtext
string, text to start printingsource
string, name of source to setup other default style for this text
Boolean value to check, if printer now printing text or not
Instantly appear all nodes, what currently printing. If text is already printed, have no effect
instance
printer instance, created by printer.new
Clear all current printed text
instance
printer instance, created by printer.new
To play sound, you should override this function.
name
string, name of played sound
Add custom styles to printer module
styles
lua table with style parameters. Elements are: {style_id: {params}}
Add one custom source style to printer module
source
string, source id to use it as source param in printer.print functionstyle
string, style id
Add one custom word style to printer module
word
string, word what will be wrapped with pointed stylestyle
string, style id
Place it in gui update function to update all print instance you have.
self
gui self contextdt
dt parameter from update script
Place it in gui final function to correct final printer component
self
gui self context
Insality
My code is under MIT license
utf8 module MIT