-
Notifications
You must be signed in to change notification settings - Fork 1
/
dialog.lua
348 lines (273 loc) · 9.22 KB
/
dialog.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
local dialog = {}
local inactivestate = {}
local typingstate = {}
local waitingstate = {}
local choosingstate = {}
local fadingstate = {}
local pausingstate = {}
-- The default dialog state is "inactive".
dialog.currentstate = inactivestate
-- Common subroutines
--- Require that a dialog thread exists.
-- Many states in the dialog engine resume the thread which controls
-- dialogs (dialog.thread). These states must call requirethread()
-- at the beginning of any function which resumes dialog.thread
-- in order to ensure that the environment is sane.
local function requirethread()
assert(dialog.thread ~= nil, 'attempted dialog without thread')
end
local function drawletterbox()
love.graphics.setColor(0, 0, 0, 255)
love.graphics.rectangle('fill',
0, virtualheight - 30,
virtualwidth, 30)
end
-- Typing state
function typingstate.update()
assert(dialog.lettersshown ~= nil, 'dialog.lettersshown is undefined')
if dialog.lettersshown < #dialog.currentmessage then
bloop:stop()
dialog.lettersshown = dialog.lettersshown + 1
bloop:play()
if dialog.lettersshown == #dialog.currentmessage then
dialog.animationtimer = 0
dialog.currentstate = waitingstate
end
end
end
function typingstate.draw()
assert(dialog.currentmessage ~= nil, 'dialog.currentmessage is nil')
assert(dialog.lettersshown ~= nil, 'dialog.lettersshown is nil')
love.graphics.setFont(font)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.print(string.sub(dialog.currentmessage,
0, dialog.lettersshown),
0, virtualheight - 30)
end
-- Waiting state
function waitingstate.update()
assert(dialog.animationtimer ~= nil, 'dialog.animationtimer is nil')
dialog.animationtimer = (dialog.animationtimer + 1) % 50
end
function waitingstate.draw()
assert(dialog.currentmessage ~= nil, 'dialog.currentmessage is nil')
assert(dialog.animationtimer ~= nil, 'dialog.animationtimer is nil')
love.graphics.setFont(font)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.print(dialog.currentmessage, 0, 90)
love.graphics.setColor(255, 255, 255, (255 / 25) * math.abs(dialog.animationtimer - 25))
love.graphics.rectangle('fill', virtualwidth - 5, virtualheight - 5, 3, 3)
end
function waitingstate.mousepressed(x, y, button, istouch)
requirethread()
coroutine.resume(dialog.thread)
end
-- Choosing state
function choosingstate.draw()
assert(dialog.choices ~= nil, 'dialog.choices is nil')
-- Draw the choices.
love.graphics.setFont(font)
for i, choice in ipairs(dialog.choices) do
local yoff = (i - 1) * 6
-- Hilight the selected choice.
if i == dialog.selectedchoice then
love.graphics.setColor(0, 0, 255, 255)
love.graphics.rectangle('fill',
0, virtualheight - 30 + yoff,
virtualwidth, 6)
love.graphics.setColor(255, 255, 255, 255)
else
love.graphics.setColor(100, 100, 255, 255)
end
love.graphics.print(dialog.choices[i], 0, virtualheight - 30 + yoff)
end
end
function choosingstate.mousemoved(x, y, dx, dy, istouch)
assert(dialog.choices ~= nil, 'dialog.choices is nil')
x = x / scalefactor
y = y / scalefactor
if y < virtualheight - 30 or y > virtualheight
or x < 0 or x > virtualwidth then
dialog.pointeronpanel = false
dialog.selectedchoice = nil
else
dialog.pointeronpanel = true
dialog.selectedchoice = math.floor((y - virtualheight + 30) / 6) + 1
if dialog.selectedchoice > #dialog.choices then
dialog.selectedchoice = nil
end
end
end
function choosingstate.mousepressed(x, y, button, istouch)
requirethread()
if dialog.selectedchoice then
coroutine.resume(dialog.thread, dialog.selectedchoice)
elseif not dialog.pointeronpanel and dialog.cancelable then
dialog.choices = nil
dialog.selectedchoice = nil
dialog.pointeronpanel = nil
dialog.currentstate = inactivestate
dialog.thread = nil
end
end
-- Pausing state
function pausingstate.update()
requirethread()
assert(dialog.pausetimer ~= nil, 'dialog.pausetimer is nil')
if dialog.pausetimer == 0 then
coroutine.resume(dialog.thread)
else
dialog.pausetimer = dialog.pausetimer - 1
end
end
-- Fading state
function fadingstate.update()
requirethread()
assert(dialog.fadedirection ~= nil, 'dialog.fadedirection is undefined')
assert(dialog.fadeopacity ~= nil, 'dialog.fadeopacity is undefined')
assert(dialog.fadeduration ~= nil, 'dialog.fadeduration is undefined')
local direction = 0
if dialog.fadedirection == 'in' then
direction = -1
else
direction = 1
end
local diff = (255 / dialog.fadeduration) * direction
dialog.fadeopacity = dialog.fadeopacity + diff
if dialog.fadeopacity < 0 then
dialog.fadeopacity = 0
elseif dialog.fadeopacity > 255 then
dialog.fadeopacity = 255
end
if dialog.fadeopacity == 0 or dialog.fadeopacity == 255 then
coroutine.resume(dialog.thread)
end
end
function fadingstate.draw()
assert(dialog.fadeopacity ~= nil, 'dialog.fadeopacity is nil')
love.graphics.setColor(0, 0, 0, dialog.fadeopacity)
love.graphics.rectangle('fill', 0, 0, virtualwidth, virtualheight)
end
--- Show arbitrary text in the dialog box when the dialog is inactive.
-- @param str the string to show
function dialog.showtext(str)
if not dialog.isactive() then
love.graphics.setFont(font)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.print(str, 0, virtualheight - 30)
end
end
-- Generate dialog callbacks (e.g., dialog.draw, dialog.update)
do
local function donothing() end
local function runifdefined(proc, ...)
if proc == nil then
proc = donothing
end
proc(...)
end
local function statecallerproc(name)
return function (...)
runifdefined(dialog.currentstate[name], ...)
end
end
dialog.update = statecallerproc('update')
dialog.draw = function ()
drawletterbox()
runifdefined(dialog.currentstate.draw)
end
dialog.mousemoved = statecallerproc('mousemoved')
dialog.mousepressed = statecallerproc('mousepressed')
dialog.keypressed = statecallerproc('keypressed')
end
-- Dialog operators
function dialog.say(str)
dialog.lettersshown = 0
dialog.currentmessage = str
dialog.currentstate = typingstate
coroutine.yield()
dialog.lettersshown = nil
dialog.currentmessage = nil
dialog.currentstate = inactivestate
end
local function dochoose(...)
dialog.choices = {...}
dialog.selectedchoice = nil
dialog.pointeronpanel = false
dialog.currentstate = choosingstate
local result = coroutine.yield()
dialog.choices = nil
dialog.selectedchoice = nil
dialog.pointeronpanel = nil
dialog.currentstate = inactivestate
return result
end
function dialog.choosecancelable(...)
dialog.cancelable = true
local result = dochoose(...)
dialog.cancelable = nil
return result
end
function dialog.choose(...)
dialog.cancelable = false
local result = dochoose(...)
dialog.cancelable = nil
return result
end
function dialog.pause(duration)
dialog.pausetimer = duration
dialog.currentstate = pausingstate
coroutine.yield()
dialog.pausetimer = nil
dialog.currentstate = inactivestate
end
function dialog.fadein(duration)
dialog.fadeopacity = 255
dialog.fadedirection = 'in'
dialog.fadeduration = duration
dialog.currentstate = fadingstate
coroutine.yield()
dialog.fadeopacity = nil
dialog.fadedirection = nil
dialog.fadeduration = nil
dialog.currentstate = inactivestate
end
function dialog.fadeout(duration)
dialog.fadeopacity = 0
dialog.fadedirection = 'out'
dialog.fadeduration = duration
dialog.currentstate = fadingstate
coroutine.yield()
dialog.fadeopacity = nil
dialog.fadedirection = nil
dialog.fadeduration = nil
dialog.currentstate = inactivestate
end
--- Start a dialog.
-- @param action the action of the dialog
function dialog.start(action)
dialog.thread = coroutine.create(action)
coroutine.resume(dialog.thread)
end
--- Return whether the dialog system is fading.
-- @return whether the dialog system is fading
local function isfading()
return dialog.currentstate == fadingstate
end
--- Return whether a choice is being presented to the user.
-- @return whether a choice is being presented to the user
local function isgivingchoice()
return dialog.currentstate == choosingstate
end
--- Return whether a dialog is currently active.
-- @return whether it is active
function dialog.isactive()
return dialog.currentstate ~= inactivestate
end
--- Return whether the current dialog is grabbing the user input.
-- @return whether the dialog is grabbing the user input
function dialog.isgrabbing()
return dialog.currentstate == waitingstate
or dialog.currentstate == choosingstate
end
return dialog