Skip to content

Commit f10d169

Browse files
committed
Tasty Text 2.0 release
1 parent a50e3a1 commit f10d169

8 files changed

Lines changed: 867 additions & 781 deletions

File tree

File renamed without changes.

main.lua

Lines changed: 115 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,174 +1,130 @@
1-
function love.load()
2-
sprite = love.graphics.newImage('smiley.png')
3-
4-
--[[
5-
<> = default delimiters for handlers (default can be changed; see code)
6-
\< = put escape character \ to prevent tag parsing and print <
7-
\> = put escape character \ to include > in tag name or print >
8-
9-
tag names can't have \ at end because \ escapes >
10-
tag names can't have space characters
11-
12-
NOTES
13-
=====
14-
15-
Previous color and font are restored after each text:draw().
16-
17-
Graphical transformations don't affect other rows (scale,rotate,etc).
18-
19-
All text align to the top of each row.
20-
21-
When drawing partial text, a drawn tag with love.graphics.push not paired up with a tag with love.graphics.pop will eventually error.
22-
--]]
23-
24-
message = [[
25-
Delimiter test: \<1> <2\> <<3\> <4\>\> <<5\>\>
26-
<red>This is red<reset>
27-
This is <green>green<reset>
28-
This is a picture:<pic>
29-
This is <shake>SHAKING</shake>
30-
UTF8 test: а б в г д е ё ж з и й к л м н о
31-
This is <font>Vera Sans</font>
32-
Wrap test with Lorem Ipsum:<small>
33-
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris nisi ipsum, aliquet et iaculis in, malesuada nec erat. Integer quis nulla vel risus consequat varius. Aliquam blandit imperdiet lectus non vestibulum. Nam lorem leo, bibendum id luctus vitae, tincidunt vel tellus. Praesent ac sagittis nibh. Nam sapien orci, venenatis quis gravida sed, mattis ut lorem. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Ut lectus orci, adipiscing quis euismod a, consequat eget nulla. Sed lobortis turpis in erat fringilla posuere.
34-
35-
Vivamus commodo ultricies scelerisque. In hac habitasse platea dictumst. Fusce tempor euismod mollis. Ut lobortis commodo nulla, ac adipiscing urna auctor quis. Cras facilisis cursus metus, vel cursus leo posuere non. Aliquam sit amet vulputate orci. Vivamus ut ante ante, non hendrerit quam. Cras ligula libero, elementum id posuere sollicitudin, gravida ut nibh</font>]]
36-
37-
smallFont = love.graphics.newFont(12)
38-
normalFont = love.graphics.newFont(18)
39-
cyrillicFont = love.graphics.newFont('font.ttf',24)
40-
41-
--[[
42-
Handlers are text objects substituting a tag
43-
Each handler table can have the following field:
44-
45-
draw : callback used at tag location
46-
font : font to use onward from tag location (MUST USE THIS FIELD FOR CUSTOM FONTS TO CORRECTLY WRAP AND ALIGN)
47-
width : (default: 0) width of text object; used for correct wrapping
48-
length: (default: 0) "view length" of text object; obj's with bigger lengths take more time to finish scrolling
49-
50-
--]]
51-
52-
handlers = {
53-
red = {
54-
draw = function(self) love.graphics.setColor(255,0,0) end,
55-
},
56-
green = {
57-
draw = function(self) love.graphics.setColor(0,255,0) end,
58-
},
59-
reset = {
60-
draw = function(self) love.graphics.setColor(255,255,255) end,
61-
},
62-
pic = {
63-
draw = function(self) love.graphics.draw(sprite,0,0) end,
64-
width = 24,
65-
-- long length = more time to finish
66-
length = 50,
67-
},
68-
font = {
69-
font = normalFont,
70-
},
71-
['/font']= {
72-
font = cyrillicFont,
73-
},
74-
small = {
75-
font = smallFont,
76-
},
77-
shake = {
78-
draw = function(self) ox = math.sin(t*10)*10 love.graphics.translate(ox,0) end,
1+
local text = require 'src.tastytext'
2+
local font = love.graphics.newFont(24)
3+
local smallfont= love.graphics.newFont(12)
4+
local cyrillic = love.graphics.newFont('cyrillic.ttf',24)
5+
6+
local str = 'Welcome to the Tasty Text demo'..
7+
'\nThis "<red>wo<green>rd<white>" should be half <red>red <white>and '..
8+
'<green>green<white>. '..
9+
'\nYou can escape the brackets with "\\": \\\\< or \\\\>'..
10+
'\n<smallfont>This sentence should have a small font<font>. '..
11+
'Different fonts are aligned to the default font baseline. '..
12+
'You can also insert images: <smiley> Cool huh? '..
13+
'\nYou can use custom draw functions: <shake>Here is an example to '..
14+
'make the text shake.</shake> UTF8 is also supported. Here is a '..
15+
'Cyrillic test: <cyrillic>В чащах юга жил бы цитрус? '..
16+
'Да, но фальшивый экземпляр!<font>'
17+
18+
local help = 'Press f1 to toggle help '..
19+
'\nPress left/down/right to change alignment. '..
20+
'\nPress a/s/d to change sub-alignment '..
21+
'\nPress space to scroll the text '..
22+
'\nPress tab to pause scrolling '..
23+
'\nPress pgdn and pgup to change limit '
24+
local show_help = true
25+
26+
local tags
27+
tags = {
28+
red = {255,0,0},
29+
green = {0,255,0},
30+
white = {255,255,255},
31+
smallfont= smallfont,
32+
font = font,
33+
smiley = love.graphics.newImage 'smiley.png',
34+
cyrillic = cyrillic,
35+
shake = {
36+
draw = function(chunk,x)
37+
local dt = love.timer.getDelta()
38+
chunk.properties.t = chunk.properties.t + dt
39+
love.graphics.translate(math.sin(chunk.properties.t*50),0)
40+
end,
41+
properties = {
42+
t = 0,
7943
},
80-
['/shake'] = {
81-
draw = function(self) love.graphics.translate(-ox,0) end,
82-
},
83-
}
84-
85-
width = 800
86-
height = 24
87-
lib = require 'text'
88-
89-
-- arg 1: string
90-
-- arg 2: maximum width before wrapping
91-
-- arg 3: default font
92-
-- arg 4: row height
93-
-- arg 5: table of handler for each tag
94-
text = lib(message,width,cyrillicFont,height,handlers)
95-
text.align = 'left'
96-
text.subalign = 'left'
97-
98-
t = 0
99-
100-
instruction = [[
101-
Press left or right to change alignment
102-
Press up or down to change subalignment
103-
Press space to reset scrolling]]
104-
105-
-- =======================
106-
-- TEST
107-
-- =======================
108-
109-
assert(text:getTotalHeight() == text:getRowCount()*height)
110-
assert(text:getViewHeight() == text:getTotalHeight())
111-
assert(text:getWidth() == width)
112-
assert(text:getRowHeight() == height)
113-
114-
text:setAlign('center')
115-
assert(text:getAlign() == 'center')
116-
117-
text:setRowHeight(height)
118-
assert(text:getRowHeight() == height)
119-
120-
text:setViewLength(text:getLength())
121-
assert(text:getViewLength() == text:getLength())
122-
123-
-- =======================
124-
-- /TEST
125-
-- =======================
126-
end
44+
},
45+
['/shake'] = {
46+
draw = function(chunk)
47+
love.graphics.translate(-math.sin(tags.shake.properties.t*50),0)
48+
end,
49+
},
50+
}
51+
52+
local height = font:getHeight()
53+
local limit = 400
54+
local test_text= text.new(str,limit,font,tags,height)
55+
test_text.align= 'center'
56+
local first = 0
57+
local last = test_text.length
58+
local fd = -2
59+
local ld = 2
12760

12861
function love.keypressed(k)
129-
if k == 'right' then
130-
if text.align == 'left' then
131-
text.align = 'center'
132-
else
133-
text.align = 'right'
134-
end
135-
end
13662
if k == 'left' then
137-
if text.align == 'right' then
138-
text.align = 'center'
139-
else
140-
text.align = 'left'
141-
end
63+
test_text.align = 'left'
14264
end
143-
if k == 'up' then
144-
if text.subalign == 'right' then
145-
text.subalign = 'center'
146-
else
147-
text.subalign = 'left'
148-
end
65+
if k == 'right' then
66+
test_text.align = 'right'
14967
end
15068
if k == 'down' then
151-
if text.subalign == 'left' then
152-
text.subalign = 'center'
153-
else
154-
text.subalign = 'right'
155-
end
69+
test_text.align = 'center'
70+
end
71+
if k == 'a' then
72+
test_text.subalign = 'left'
73+
end
74+
if k == 'd' then
75+
test_text.subalign = 'right'
76+
end
77+
if k == 's' then
78+
test_text.subalign = 'center'
79+
end
80+
if k == ' ' then
81+
first = math.floor(test_text.length/2)
82+
last = math.floor(test_text.length/2)
83+
end
84+
if k == 'escape' then
85+
love.event.push 'quit'
86+
end
87+
if k == 'tab' then
88+
pause = not pause
89+
end
90+
if k == 'pagedown' then
91+
limit = limit - 10
92+
test_text = text.new(str,limit,font,tags,height)
93+
end
94+
if k == 'pageup' then
95+
limit = limit + 10
96+
test_text = text.new(str,limit,font,tags,height)
97+
end
98+
if k == 'f1' then
99+
show_help = not show_help
156100
end
157101
end
158102

103+
local t = 0
159104
function love.update(dt)
160-
if love.keyboard.isDown(' ') then t=0 end
161-
t = t+dt
162-
text:setViewLength(math.ceil(t*50))
105+
t = t + dt
106+
if t > 1/30 and not pause then
107+
t = 0
108+
last = math.max(last+ld,1)
109+
first = math.max(first+fd,1)
110+
test_text:setSub(first,last)
111+
end
163112
end
164113

165114
function love.draw()
166-
text:draw()
167-
love.graphics.print('align: '..text.align,0,550)
168-
love.graphics.print('subalign: '..text.subalign,0,572)
169-
love.graphics.print(instruction,0,500)
170-
end
171-
172-
function love.quit()
173-
115+
love.graphics.setColor(255,0,0)
116+
love.graphics.push()
117+
love.graphics.translate((love.graphics.getWidth()-limit)/2,0)
118+
love.graphics.line(0,0,0,600)
119+
love.graphics.line(limit,0,limit,600)
120+
love.graphics.setColor(255,255,255)
121+
test_text:draw()
122+
love.graphics.pop()
123+
124+
if show_help then
125+
love.graphics.setColor(64,64,64)
126+
love.graphics.rectangle('fill',0,0,300,100)
127+
love.graphics.setColor(255,255,255)
128+
love.graphics.print(help)
129+
end
174130
end

0 commit comments

Comments
 (0)