-- It implements a character based tagging system which should allow you to implement any kind of text effect possible, from setting a character's color to making it become visible, shake and play sounds.
-- There are two main things happening in the example above: first we're creating TextTags and then we're creating a text object that uses those tags.
-- The way each tag works is fairly simple: a tag accepts 3 functions, init, update and draw, and each of those functions operates on the text's characters one at a time.
-- In the example above, the text without tags is 'This text is yellow while this text is shaking and this text is normal'
-- For each of the characters in that string, different functions will be applied based on what tags were previously applied to it.
-- init, update and draw functions take in 3 arguments in common:
-- c - the character in question, a table containing .x, .y, .r, .sx, .sy, .ox, .oy and .character attributes.
-- i - the index of character in the string
-- text - the reference to the text object
-- The update function also takes in dt as the second argument.
--
-- After we're done creating TextTags, we have to create the actual text object.
-- The way this is done is by specifying each line of the text object, along with its font, alignment, height multipliers and height offsets.
-- The first argument (text_data) is a table of tables containing all relevant info:
-- text - the actual string containing the text to be displayed, along with any tag information
-- font - the font to be used for the text
-- alignment (optional) - how the text should align itself, possible values are 'center', 'justified', 'right', if not specified then by default it's 'left'
-- height_offset (optional) - how many pixels the line below this one should be offset by
-- height_multiplier (optional) - multiplier over the font's height for placing the line below
-- The text object itself also has .w and .h which corresponds to the width of the biggest line and height of all lines + offsets, respectively.
-- If 'alignment_width' is set to a specific line then that line will be automatically set to that width, and if it is the biggest then .w will also be set to that value.
-- Reapplies all modifications (wrap width, justification, etc).
functionText:set_text(text_data)
self.lines=self:parse(text_data)
self:format_text()
for_,lineinipairs(self.lines)do
fori,cinipairs(line.characters)do
fork,vinpairs(self.text_tags)do
for_,taginipairs(c.tags)do
iftag==kthen
ifv.actions.initthen
v.actions.init(c,i,self)
end
end
end
end
end
end
end
-- Sets the line's alignment width.
-- This is used to align the text according to the alignment option
-- For instance, if the alignment width is 200 and the alignment is 'right', then the right edge used for this alignment will be 200 units to the right
-- Lines are automatically placed vertically using the font's height for spacing, but you can increase or decrease this distance by setting these values.