-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.lua
157 lines (129 loc) · 3.7 KB
/
ui.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
--button class
Button = inherits(AnimatedSprite, {
state = 1,
drawmode = "absolute"
})
local button_mt = class(Button)
function Button:init(x, y, anim, action)
local o = {
x = x,
y = y,
anim = animations[anim],
action = action
}
o.bounds = Rectangle:new(o.x, o.y, o.anim.width, o.anim.height)
return o
end
function Button:new(x, y, anim, action)
return setmetatable(Button:init(x, y, anim, action), button_mt)
end
function Button:add(x, y, anim, action)
table.insert(p, Button:new(x, y, anim, action))
end
function Button:update(dt)
if self.bounds:containspoint(mouse.x, mouse.y, self.drawmode) then
self.state = 2
if mouse.p or mouse.held then
self.state = 3
if mouse.p then self:action() end
end
else
self.state = 1
end
self:animate(dt)
end
function Button:animate(dt)
self.frame = self.state
end
--textbox class
Textbox = {
bank = "",
speed = 0.01, --delay in seconds per letter
cooldown = 0,
pixels = 0,
pause = 0
}
local textbox_mt = class(Textbox)
function Textbox:init(x, y, text, opts)
opts = opts or {}
--[[
opts fields:
shadow: text shadow color in { red, green, blue }
hpad/vpad: text position offset within textbox
font: specify font
image: specify image for textbox background
scroll: true if this textbox should have scrolling text
]]
local o = {
x = x, y = y,
text = "",
drawmode = "absolute"
}
for k,v in pairs(opts) do
o[k] = v
end
if o.scroll then o.bank = text else o.text = text end
local word = o.bank:match("(%w+)(.+)")
local wordsize = fonts["ElixiR"]:getWidth(" " .. (word or ""))
o.pixels = wordsize
o.width = o.width or (o.image and o.image:getWidth()) or 100
return o
end
function Textbox:new(x, y, text, opts)
return setmetatable(Textbox:init(x, y, text, opts), textbox_mt)
end
function Textbox:add(x, y, text, opts)
table.insert(p, Textbox:new(x, y, text, opts))
end
-- pull the next character from the text bank and display it in the textbox
-- insert a new line if necessary
function Textbox:nextletter()
local char = self.bank:sub(1, 1)
if char == " " then
local word = self.bank:match("(%w+)(.+)")
local wordsize = fonts["ElixiR"]:getWidth(" " .. (word or ""))
if self.pixels + wordsize > self.width - (self.hpad * 2) then
self.text = self.text .. "\n"
self.pixels = 0
char = ""
end
self.pixels = self.pixels + wordsize
elseif char == "," then
self.pause = 0.05
elseif char == "." or char == "?" or char == "!" then
self.pause = 0.2
end
self.text = self.text .. char
self.bank = self.bank:sub(2)
end
function Textbox:update(dt)
if self.pause > 0 then
self.pause = self.pause - dt
return
else
self.pause = 0
end
if self.bank ~= "" then
self.cooldown = self.cooldown + dt
if self.cooldown > self.speed then
self:nextletter()
self.cooldown = self.cooldown - self.speed
end
end
end
function Textbox:draw()
local textpos = {
x = self.x + (self.hpad or 0),
y = self.y + (self.vpad or 0)
}
if self.font then love.graphics.setFont(self.font) end
if self.image then
love.graphics.draw(self.image, self.x, self.y)
end
if self.shadow then
love.graphics.setColor(self.shadow)
love.graphics.print(self.text, textpos.x + 1, textpos.y + 1, 0)
love.graphics.setColor(1, 1, 1, 1)
end
love.graphics.print(self.text, textpos.x, textpos.y, 0)
end